fix: prevent unnecessary service lifecycle on startup for non-users

- stop() now uses stopService() instead of startService(STOP intent).
  stopService() is a no-op when the service isn't running and avoids
  starting a service just to stop it, which could throw on Android 12+.

- disableAllLayers() only runs when transitioning from enabled → disabled,
  not on initial load with false. Users who never enabled the service
  won't trigger any service/WorkManager/AlarmManager calls on login.

https://claude.ai/code/session_01LEPfmgGnwjB9a5SDFw5U8t
This commit is contained in:
Claude
2026-04-22 00:55:15 +00:00
parent fc6b7871c0
commit e4b5b12a0b
2 changed files with 6 additions and 6 deletions
@@ -50,19 +50,23 @@ class AlwaysOnNotificationServiceManager(
} }
private var watchJob: Job? = null private var watchJob: Job? = null
private var wasEnabled = false
/** /**
* Starts watching the given account's always-on setting. * Starts watching the given account's always-on setting.
* When the setting changes, all layers are started or stopped accordingly. * When the setting changes, all layers are started or stopped accordingly.
* On initial load with false, nothing happens (no-op for users who never enabled it).
*/ */
fun watchAccount(account: Account) { fun watchAccount(account: Account) {
watchJob?.cancel() watchJob?.cancel()
wasEnabled = false
watchJob = watchJob =
scope.launch { scope.launch {
account.settings.alwaysOnNotificationService.collectLatest { enabled -> account.settings.alwaysOnNotificationService.collectLatest { enabled ->
if (enabled) { if (enabled) {
wasEnabled = true
enableAllLayers() enableAllLayers()
} else { } else if (wasEnabled) {
disableAllLayers() disableAllLayers()
} }
} }
@@ -95,11 +95,7 @@ class NotificationRelayService : Service() {
} }
fun stop(context: Context) { fun stop(context: Context) {
val intent = context.stopService(Intent(context, NotificationRelayService::class.java))
Intent(context, NotificationRelayService::class.java).apply {
action = ACTION_STOP
}
context.startService(intent)
} }
fun isEnabled(context: Context): Boolean = fun isEnabled(context: Context): Boolean =