diff --git a/app/src/main/kotlin/com/subtracks/data/net/LocalNetworkAccess.kt b/app/src/main/kotlin/com/subtracks/data/net/LocalNetworkAccess.kt index f94a934b..722690a9 100644 --- a/app/src/main/kotlin/com/subtracks/data/net/LocalNetworkAccess.kt +++ b/app/src/main/kotlin/com/subtracks/data/net/LocalNetworkAccess.kt @@ -26,15 +26,32 @@ class LocalNetworkAccess( if (Build.VERSION.SDK_INT < LOCAL_NETWORK_PERMISSION_SDK) return false if (granted()) return false val host = serverHost(address) ?: return false - val linkAddresses = currentLinkAddresses() - if (linkAddresses.isEmpty()) return false - if (isLocalHostname(host)) return true return withContext(Dispatchers.IO) { + val linkAddresses = currentLinkAddresses() + if (linkAddresses.isEmpty()) return@withContext false + if (isLocalHostname(host)) return@withContext true val resolved = runCatching { InetAddress.getAllByName(host).toList() }.getOrDefault(emptyList()) - resolved.any { candidate -> linkAddresses.any { it.contains(candidate) } } + if (resolved.isEmpty()) return@withContext false + val current = currentLinkAddresses() + resolved.any { candidate -> + !routedThroughVpn(candidate) && current.any { it.contains(candidate) } + } } } + @RequiresApi(LOCAL_NETWORK_PERMISSION_SDK) + private fun routedThroughVpn(address: InetAddress): Boolean { + val manager = context.getSystemService(ConnectivityManager::class.java) ?: return false + val network = manager.activeNetwork ?: return false + val capabilities = manager.getNetworkCapabilities(network) ?: return false + if (!capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) return false + return manager + .getLinkProperties(network) + ?.routes + .orEmpty() + .any { route -> route.destination.contains(address) } + } + @Synchronized fun markDenied(address: String) { deniedAddresses += normalizeServerAddress(address) diff --git a/app/src/main/kotlin/com/subtracks/data/sync/SyncManager.kt b/app/src/main/kotlin/com/subtracks/data/sync/SyncManager.kt index ef232f56..2ba3ea71 100644 --- a/app/src/main/kotlin/com/subtracks/data/sync/SyncManager.kt +++ b/app/src/main/kotlin/com/subtracks/data/sync/SyncManager.kt @@ -48,6 +48,12 @@ class SyncManager( private val _status = MutableStateFlow(SyncStatus.Idle) val status: StateFlow = _status + private val _runningSource = MutableStateFlow(null) + val runningSource: StateFlow = _runningSource + + private val _failedSource = MutableStateFlow(null) + val failedSource: StateFlow = _failedSource + init { scope.launch { for (silent in requests) { @@ -82,9 +88,11 @@ class SyncManager( runCatching { preferences.setLastSyncAt(System.currentTimeMillis()) } .onFailure { Log.w("sync", "could not record the sync attempt", it) } val startedAt = System.nanoTime() + val source = sourceRepository.activeMusicSource() + _runningSource.value = source?.id val result = try { - val source = sourceRepository.activeMusicSource() ?: throw NoServerException() + if (source == null) throw NoServerException() Log.i("sync", "started (source ${source.id})") serverActions.flush() val resetPoint = queueRepository.captureResetPoint() @@ -99,6 +107,7 @@ class SyncManager( "${completed.playlists} playlists, ${completed.playlistSongs} playlist tracks, ${completed.pruned} pruned", ) queueRepository.resetChangedOriginsAndClearPendingOrigins(changedOrigins, resetPoint, source.id) + _failedSource.value = null SyncStatus.Success } catch (cancellation: CancellationException) { throw cancellation @@ -116,7 +125,10 @@ class SyncManager( } } if (!silent) showMessage(message) + _failedSource.value = source?.id SyncStatus.Failed(message) + } finally { + _runningSource.value = null } _status.value = result } diff --git a/app/src/main/kotlin/com/subtracks/ui/AppRoot.kt b/app/src/main/kotlin/com/subtracks/ui/AppRoot.kt index 3830fcff..b1c6ad65 100644 --- a/app/src/main/kotlin/com/subtracks/ui/AppRoot.kt +++ b/app/src/main/kotlin/com/subtracks/ui/AppRoot.kt @@ -122,6 +122,11 @@ class RootViewModel( .map { it?.address } .stateIn(viewModelScope, SharingStarted.Eagerly, null) + val activeSourceId = + sourceRepository + .activeSourceId() + .stateIn(viewModelScope, SharingStarted.Eagerly, null) + val offline = sourceRepository.offline val offlineLoaded = sourceRepository.offlineLoaded @@ -210,23 +215,37 @@ fun SubtracksRoot(root: RootViewModel = koinViewModel()) { val localNetworkGate = rememberLocalNetworkGate() val syncManager = koinInject() val serverAddress by root.serverAddress.collectAsStateWithLifecycle() + val activeSourceId by root.activeSourceId.collectAsStateWithLifecycle() val offline by root.offline.collectAsStateWithLifecycle() val offlineLoaded by root.offlineLoaded.collectAsStateWithLifecycle() val networkRevision = rememberNetworkRevision() - LaunchedEffect(serverAddress, networkRevision, offline, offlineLoaded) { + LaunchedEffect(serverAddress, activeSourceId, networkRevision, offline, offlineLoaded) { if (!offlineLoaded) return@LaunchedEffect if (offline) { localNetworkGate.cancel() return@LaunchedEffect } + val promptedSourceId = activeSourceId val statusBeforePrompt = syncManager.status.value localNetworkGate.run( address = serverAddress, onGranted = { val status = syncManager.status.value val raced = - status is SyncStatus.Running || - (status is SyncStatus.Failed && statusBeforePrompt !is SyncStatus.Failed) + when (status) { + is SyncStatus.Running -> { + syncManager.runningSource.value == promptedSourceId + } + + is SyncStatus.Failed -> { + statusBeforePrompt !is SyncStatus.Failed && + syncManager.failedSource.value == promptedSourceId + } + + else -> { + false + } + } if (raced) syncManager.requestSync() }, onNotNeeded = {}, diff --git a/app/src/main/kotlin/com/subtracks/ui/settings/AddSourceScreen.kt b/app/src/main/kotlin/com/subtracks/ui/settings/AddSourceScreen.kt index a577a144..a2e6a96f 100644 --- a/app/src/main/kotlin/com/subtracks/ui/settings/AddSourceScreen.kt +++ b/app/src/main/kotlin/com/subtracks/ui/settings/AddSourceScreen.kt @@ -72,7 +72,7 @@ fun AddSourceRoute( onGranted: () -> Unit, onDenied: () -> Unit = onGranted, ) { - if (gatedBusy) return + if (!offlineLoaded || gatedBusy) return gatedBusy = true val address = state.address @@ -81,7 +81,7 @@ fun AddSourceRoute( if (viewModel.state.value.address == address) action() } - if (!offlineLoaded || offline) { + if (offline) { finish(onGranted) } else { localNetworkGate.run( @@ -92,7 +92,7 @@ fun AddSourceRoute( } } AddSourceScreen( - state = if (gatedBusy) state.copy(busy = true) else state, + state = if (gatedBusy || !offlineLoaded) state.copy(busy = true) else state, onNameChange = viewModel::setName, onAddressChange = viewModel::setAddress, onUsernameChange = viewModel::setUsername,