From 815e4878bced20fd9bd54537a2c76aba205e5c85 Mon Sep 17 00:00:00 2001 From: LossyDragon Date: Sun, 16 Feb 2025 15:54:21 -0600 Subject: [PATCH 1/4] Fix "Exit" not terminating service when not in focus --- .../main/java/com/OxGames/Pluvia/service/NotificationHelper.kt | 2 +- app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/OxGames/Pluvia/service/NotificationHelper.kt b/app/src/main/java/com/OxGames/Pluvia/service/NotificationHelper.kt index e2151675..07870f24 100644 --- a/app/src/main/java/com/OxGames/Pluvia/service/NotificationHelper.kt +++ b/app/src/main/java/com/OxGames/Pluvia/service/NotificationHelper.kt @@ -71,7 +71,7 @@ class NotificationHelper(private val context: Context) { val stopIntent = Intent(context, SteamService::class.java).apply { action = ACTION_EXIT } - val stopPendingIntent = PendingIntent.getService( + val stopPendingIntent = PendingIntent.getForegroundService( context, 0, stopIntent, diff --git a/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt b/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt index ba2b5b73..dccb266b 100644 --- a/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt +++ b/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt @@ -1117,6 +1117,8 @@ class SteamService : Service(), IChallengeUrlChanged { // Notification intents when (intent?.action) { NotificationHelper.ACTION_EXIT -> { + Timber.d("Exiting app via notification intent") + SteamService.stop() PluviaApp.events.emit(AndroidEvent.EndProcess) return START_NOT_STICKY } From e976f16f8c2c91a0aa40779b9f0b2ad228a6372f Mon Sep 17 00:00:00 2001 From: Oxters Date: Wed, 19 Feb 2025 15:25:56 -0500 Subject: [PATCH 2/4] Subscribed to the EndProcess event in SteamService and stopped the service when the event is fired --- app/src/main/java/com/OxGames/Pluvia/MainActivity.kt | 1 - .../main/java/com/OxGames/Pluvia/service/SteamService.kt | 8 +++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/OxGames/Pluvia/MainActivity.kt b/app/src/main/java/com/OxGames/Pluvia/MainActivity.kt index 2e094b84..6181fd6d 100644 --- a/app/src/main/java/com/OxGames/Pluvia/MainActivity.kt +++ b/app/src/main/java/com/OxGames/Pluvia/MainActivity.kt @@ -67,7 +67,6 @@ class MainActivity : ComponentActivity() { } private val onEndProcess: (AndroidEvent.EndProcess) -> Unit = { - SteamService.stop() finishAndRemoveTask() } diff --git a/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt b/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt index dccb266b..b2a41bc0 100644 --- a/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt +++ b/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt @@ -190,6 +190,10 @@ class SteamService : Service(), IChallengeUrlChanged { private val dbScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private val serviceScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) + private val onEndProcess: (AndroidEvent.EndProcess) -> Unit = { + SteamService.stop() + } + companion object { const val MAX_SIMULTANEOUS_PICS_REQUESTS = 50 const val PICS_CHANGE_CHECK_DELAY = 60000L @@ -1114,11 +1118,12 @@ class SteamService : Service(), IChallengeUrlChanged { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + PluviaApp.events.on(onEndProcess) + // Notification intents when (intent?.action) { NotificationHelper.ACTION_EXIT -> { Timber.d("Exiting app via notification intent") - SteamService.stop() PluviaApp.events.emit(AndroidEvent.EndProcess) return START_NOT_STICKY } @@ -1293,6 +1298,7 @@ class SteamService : Service(), IChallengeUrlChanged { isStopping = false retryAttempt = 0 + PluviaApp.events.off(onEndProcess) PluviaApp.events.clearAllListenersOf>() } From 5c8270075b9c484e210951728b841eca2c51b3dc Mon Sep 17 00:00:00 2001 From: Oxters Date: Wed, 19 Feb 2025 15:31:38 -0500 Subject: [PATCH 3/4] Moved EndProcess subscription to onCreate to avoid multiple subscriptions to the same event --- app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt b/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt index b2a41bc0..5b8eff62 100644 --- a/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt +++ b/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt @@ -1099,6 +1099,8 @@ class SteamService : Service(), IChallengeUrlChanged { override fun onCreate() { super.onCreate() instance = this + + PluviaApp.events.on(onEndProcess) notificationHelper = NotificationHelper(applicationContext) @@ -1118,8 +1120,6 @@ class SteamService : Service(), IChallengeUrlChanged { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - PluviaApp.events.on(onEndProcess) - // Notification intents when (intent?.action) { NotificationHelper.ACTION_EXIT -> { From 43a92b51c24af70461fc6cb78a252e59e46a4f14 Mon Sep 17 00:00:00 2001 From: Oxters Date: Wed, 19 Feb 2025 15:42:37 -0500 Subject: [PATCH 4/4] Ran format kotlin --- app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt b/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt index 5b8eff62..6782ff42 100644 --- a/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt +++ b/app/src/main/java/com/OxGames/Pluvia/service/SteamService.kt @@ -1099,7 +1099,7 @@ class SteamService : Service(), IChallengeUrlChanged { override fun onCreate() { super.onCreate() instance = this - + PluviaApp.events.on(onEndProcess) notificationHelper = NotificationHelper(applicationContext)