diff --git a/android/app/src/main/kotlin/com/zarz/spotiflac/DownloadService.kt b/android/app/src/main/kotlin/com/zarz/spotiflac/DownloadService.kt index 23783a56..a2c76b4a 100644 --- a/android/app/src/main/kotlin/com/zarz/spotiflac/DownloadService.kt +++ b/android/app/src/main/kotlin/com/zarz/spotiflac/DownloadService.kt @@ -949,7 +949,11 @@ class DownloadService : Service() { settingsJson = settingsJson, includeItems = true, ) - scheduleNativeVerificationNotification(generation) + // Publish immediately. If Flutter is alive it will + // replace this same notification ID while owning + // the interactive challenge; if Flutter is + // suspended, the native alert remains visible. + showNativeVerificationRequired() updateNotification(0L, 0L) retryCurrentRequest = true } else { @@ -1267,21 +1271,6 @@ class DownloadService : Service() { } } - private fun scheduleNativeVerificationNotification(generation: Long) { - serviceScope.launch { - // Give an active Flutter poller time to take ownership of the - // verification flow. If Flutter is suspended, the service remains - // paused and publishes the alert itself. - delay(2_000L) - if (generation == nativeWorkerGeneration && - nativeWorkerVerificationPaused && - !nativeWorkerCancelRequested - ) { - showNativeVerificationRequired() - } - } - } - private fun showNativeVerificationRequired() { val pendingIntent = PendingIntent.getActivity( this, diff --git a/lib/providers/download_queue_provider_verification.dart b/lib/providers/download_queue_provider_verification.dart index 3cecb891..6307d89e 100644 --- a/lib/providers/download_queue_provider_verification.dart +++ b/lib/providers/download_queue_provider_verification.dart @@ -51,21 +51,7 @@ extension _DownloadQueueVerificationGate on DownloadQueueNotifier { 'Verification required for $normalizedExtensionId while app is in ' 'background; deferring challenge until the app is foregrounded', ); - try { - await _notificationService.showVerificationRequired(); - } catch (error) { - _log.w( - 'Failed to show the verification-required notification: $error', - ); - } await _waitForForeground(); - try { - await _notificationService.cancelVerificationRequired(); - } catch (error) { - _log.w( - 'Failed to clear the verification-required notification: $error', - ); - } } }, ); @@ -105,7 +91,24 @@ extension _DownloadQueueVerificationGate on DownloadQueueNotifier { errorType: DownloadErrorType.verificationRequired, ); - final verified = await _openVerificationAndWait(targetService); + try { + await _notificationService.showVerificationRequired(); + } catch (error) { + _log.w('Failed to show the verification-required notification: $error'); + } + + late final bool verified; + try { + verified = await _openVerificationAndWait(targetService); + } finally { + try { + await _notificationService.cancelVerificationRequired(); + } catch (error) { + _log.w( + 'Failed to clear the verification-required notification: $error', + ); + } + } final current = _findItemById(item.id); if (current == null || _isLocallyCancelled(item.id, item: current)) { _log.i('Verification completed after item was removed or cancelled');