From 6ea7b3231cd52564d39e3237d8cd37da4d8ae448 Mon Sep 17 00:00:00 2001 From: cogwheel0 <172976095+cogwheel0@users.noreply.github.com> Date: Mon, 20 Oct 2025 23:47:04 +0530 Subject: [PATCH] refactor: Improve background streaming service management - Added action handling for stopping the background streaming service, allowing for a clean shutdown when requested. - Enhanced the stopStreaming method to ensure proper foreground service termination based on Android version compatibility. - Updated keepAlive method to conditionally start the foreground service based on the Android version, improving service management and reliability. - Implemented error handling for service start and stop operations to enhance robustness and provide clearer feedback on failures. --- .../conduit/BackgroundStreamingHandler.kt | 63 +++++++++++++------ 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/android/app/src/main/kotlin/app/cogwheel/conduit/BackgroundStreamingHandler.kt b/android/app/src/main/kotlin/app/cogwheel/conduit/BackgroundStreamingHandler.kt index a05d9cd..8f91bb2 100644 --- a/android/app/src/main/kotlin/app/cogwheel/conduit/BackgroundStreamingHandler.kt +++ b/android/app/src/main/kotlin/app/cogwheel/conduit/BackgroundStreamingHandler.kt @@ -44,6 +44,13 @@ class BackgroundStreamingService : Service() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + when (intent?.action) { + ACTION_STOP -> { + stopStreaming() + return START_NOT_STICKY + } + } + val notification = createMinimalNotification() val desiredType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { resolveForegroundServiceType(intent) @@ -67,15 +74,12 @@ class BackgroundStreamingService : Service() { acquireWakeLock() println("BackgroundStreamingService: Started foreground service") } - ACTION_STOP -> { - stopStreaming() - } "KEEP_ALIVE" -> { keepAlive() } } - return START_STICKY // Restart if killed by system + return START_STICKY } private fun startForegroundInternal(notification: Notification, type: Int): Boolean { @@ -175,9 +179,18 @@ class BackgroundStreamingService : Service() { private fun stopStreaming() { activeStreams.clear() releaseWakeLock() - stopForeground(true) + + if (isForeground) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + stopForeground(STOP_FOREGROUND_REMOVE) + } else { + @Suppress("DEPRECATION") + stopForeground(true) + } + isForeground = false + } + stopSelf() - isForeground = false println("BackgroundStreamingService: Service stopped") } @@ -312,9 +325,13 @@ class BackgroundStreamingHandler(private val activity: MainActivity) : MethodCal } private fun stopForegroundService() { - val serviceIntent = Intent(context, BackgroundStreamingService::class.java) - serviceIntent.action = BackgroundStreamingService.ACTION_STOP - context.startService(serviceIntent) + try { + val serviceIntent = Intent(context, BackgroundStreamingService::class.java) + serviceIntent.action = BackgroundStreamingService.ACTION_STOP + context.stopService(serviceIntent) + } catch (e: Exception) { + println("BackgroundStreamingHandler: Failed to stop foreground service: ${e.message}") + } } private fun startBackgroundMonitoring() { @@ -354,15 +371,25 @@ class BackgroundStreamingHandler(private val activity: MainActivity) : MethodCal } private fun keepAlive() { - // Just notify the service to refresh - val serviceIntent = Intent(context, BackgroundStreamingService::class.java) - serviceIntent.action = "KEEP_ALIVE" - serviceIntent.putExtra("streamCount", activeStreams.size) - serviceIntent.putExtra( - BackgroundStreamingService.EXTRA_REQUIRES_MICROPHONE, - streamsRequiringMic.isNotEmpty(), - ) - context.startService(serviceIntent) + if (activeStreams.isEmpty()) return + + try { + val serviceIntent = Intent(context, BackgroundStreamingService::class.java) + serviceIntent.action = "KEEP_ALIVE" + serviceIntent.putExtra("streamCount", activeStreams.size) + serviceIntent.putExtra( + BackgroundStreamingService.EXTRA_REQUIRES_MICROPHONE, + streamsRequiringMic.isNotEmpty(), + ) + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(serviceIntent) + } else { + context.startService(serviceIntent) + } + } catch (e: Exception) { + println("BackgroundStreamingHandler: Failed to keep alive service: ${e.message}") + } } private fun createNotificationChannel() {