diff --git a/crates/tauri-app/gen/android/app/src/main/java/com/itsgoin/app/MainActivity.kt b/crates/tauri-app/gen/android/app/src/main/java/com/itsgoin/app/MainActivity.kt index 72f3913..f74ba2e 100644 --- a/crates/tauri-app/gen/android/app/src/main/java/com/itsgoin/app/MainActivity.kt +++ b/crates/tauri-app/gen/android/app/src/main/java/com/itsgoin/app/MainActivity.kt @@ -21,6 +21,10 @@ class MainActivity : TauriActivity() { } } + // Genuine launch — clear any prior "user quit" flag so the service is + // allowed to run (and to resume via START_STICKY) again. + NodeService.setUserQuit(this, false) + // Start foreground service to keep connections alive in background val serviceIntent = Intent(this, NodeService::class.java) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { diff --git a/crates/tauri-app/gen/android/app/src/main/java/com/itsgoin/app/NodeService.kt b/crates/tauri-app/gen/android/app/src/main/java/com/itsgoin/app/NodeService.kt index 0112936..0a31bb9 100644 --- a/crates/tauri-app/gen/android/app/src/main/java/com/itsgoin/app/NodeService.kt +++ b/crates/tauri-app/gen/android/app/src/main/java/com/itsgoin/app/NodeService.kt @@ -18,6 +18,28 @@ class NodeService : Service() { const val CHANNEL_ID = "itsgoin_node" const val NOTIFICATION_ID = 1 + // Survives process death, unlike any in-memory flag. START_STICKY + // means Android resurrects this service when its process dies — + // desirable when the SYSTEM kills us in the background, wrong when + // the USER explicitly quit. stopService() is asynchronous, so a + // fast app.exit(0) can kill the process before the stop is + // processed, and the resurrection races ahead of it. This flag is + // how a restarted service learns the quit already happened. + private const val PREFS = "itsgoin_service" + private const val KEY_USER_QUIT = "user_quit" + + @JvmStatic + fun setUserQuit(context: Context, quit: Boolean) { + context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) + .edit() + .putBoolean(KEY_USER_QUIT, quit) + .commit() // synchronous — the process may die moments from now + } + + private fun userQuit(context: Context): Boolean = + context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) + .getBoolean(KEY_USER_QUIT, false) + // Called via JNI from Rust when the user taps the in-app close // button. Foreground services survive Activity exit by design // (keeps connections alive when backgrounded). When the user @@ -25,6 +47,9 @@ class NodeService : Service() { // service in addition to ending the Activity. @JvmStatic fun stopFromNative(context: Context) { + // Record the intent to quit BEFORE asking the service to stop, + // so a sticky restart that beats us to it still sees the flag. + setUserQuit(context, true) val intent = Intent(context, NodeService::class.java) context.stopService(intent) } @@ -54,6 +79,16 @@ class NodeService : Service() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + // A null intent means Android resurrected us under START_STICKY + // rather than MainActivity starting us. If the user had quit, honour + // that: stop immediately and do not come back. MainActivity clears + // the flag whenever the app is genuinely launched, so a system-kill + // restart (the case START_STICKY exists for) still resumes. + if (intent == null && userQuit(this)) { + stopForeground(STOP_FOREGROUND_REMOVE) + stopSelf() + return START_NOT_STICKY + } return START_STICKY } diff --git a/crates/tauri-app/src/lib.rs b/crates/tauri-app/src/lib.rs index a6440b5..2dacbea 100644 --- a/crates/tauri-app/src/lib.rs +++ b/crates/tauri-app/src/lib.rs @@ -1161,7 +1161,16 @@ async fn exit_app(app: tauri::AppHandle) { // explicitly hits the in-app close button, also stop the service // so we actually free the device's network/wakelock. #[cfg(target_os = "android")] - itsgoin_core::android_wifi::stop_node_service(); + { + itsgoin_core::android_wifi::stop_node_service(); + // stopService() is asynchronous: it posts to ActivityManager and the + // service's onDestroy runs afterwards on the main thread. Exiting + // immediately kills the process mid-flight, which Android treats as + // "foreground service died" and restarts under START_STICKY. Give the + // clean path a moment to win; NodeService's persisted user-quit flag + // is the backstop for when it doesn't. + tokio::time::sleep(std::time::Duration::from_millis(400)).await; + } app.exit(0); }