Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions app/src/main/java/com/adsamcik/tracker/app/Application.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.adsamcik.tracker.app

import android.app.ActivityManager
import android.app.ApplicationExitInfo
import android.os.Build
import androidx.annotation.MainThread
import androidx.annotation.WorkerThread
Expand Down Expand Up @@ -186,6 +188,7 @@ class Application : AndroidApplication(), Configuration.Provider {
Reporter.initialize(this@Application)
Logger.initialize(this@Application)
CrashHandler(this@Application).initialize()
logPreviousExitReason()
if (!isRobolectricUnitTest()) {
initializeModules()
}
Expand All @@ -199,6 +202,51 @@ class Application : AndroidApplication(), Configuration.Provider {

private fun isRobolectricUnitTest(): Boolean = Build.FINGERPRINT == "robolectric"

/**
* Logs the reason the previous process instance exited (Android 10+/API 30) so abnormal
* terminations — low-memory kills, OEM/SIGKILL, ANRs, native crashes — become observable
* in crash reports. This is the foundation for crash-informed recovery (e.g. draining the
* durable signal buffer after an abnormal kill) and for respecting a user-requested
* force-stop (REASON_USER_REQUESTED) instead of auto-restarting tracking.
*/
@WorkerThread
private fun logPreviousExitReason() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) return
try {
val activityManager = getSystemService(ActivityManager::class.java) ?: return
val exitInfo = activityManager
.getHistoricalProcessExitReasons(packageName, 0, 1)
.firstOrNull() ?: return

val reasonLabel = when (exitInfo.reason) {
ApplicationExitInfo.REASON_LOW_MEMORY -> "LOW_MEMORY"
ApplicationExitInfo.REASON_SIGNALED -> "SIGNALED"
ApplicationExitInfo.REASON_CRASH -> "CRASH"
ApplicationExitInfo.REASON_CRASH_NATIVE -> "CRASH_NATIVE"
ApplicationExitInfo.REASON_ANR -> "ANR"
ApplicationExitInfo.REASON_USER_REQUESTED -> "USER_REQUESTED"
ApplicationExitInfo.REASON_USER_STOPPED -> "USER_STOPPED"
ApplicationExitInfo.REASON_OTHER -> "OTHER"
else -> "reason=${exitInfo.reason}"
}

val isAbnormal = when (exitInfo.reason) {
ApplicationExitInfo.REASON_LOW_MEMORY,
ApplicationExitInfo.REASON_SIGNALED,
ApplicationExitInfo.REASON_CRASH,
ApplicationExitInfo.REASON_CRASH_NATIVE,
ApplicationExitInfo.REASON_ANR -> true
else -> false
}

val message = "Previous process exit: $reasonLabel (status=${exitInfo.status})"
if (isAbnormal) Reporter.w("App", message) else Reporter.log(message)
} catch (e: RuntimeException) {
// Defensive: getHistoricalProcessExitReasons can throw on some OEM builds.
Reporter.report(e)
}
}

fun startDeferredStartupIfNeeded() {
if (!deferredStartupStarted.compareAndSet(false, true)) return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,12 @@ internal class TrackerService : CoreService(), TrackerTimerReceiver {
}
}

// User-initiated sessions should restart after process death to preserve tracking.
// Auto-tracking sessions can be re-triggered by ActivityWatcherService.
return if (isUserInitiated) START_STICKY else START_NOT_STICKY
// User-initiated sessions use START_REDELIVER_INTENT so that, after a system kill,
// Android re-delivers the ORIGINAL start intent (carrying ARG_IS_USER_INITIATED)
// instead of a null intent — letting the session resume with the correct flags
// rather than relying on the in-memory recovery fallback below. Auto-tracking
// sessions stay START_NOT_STICKY and are re-triggered by ActivityWatcherService.
return if (isUserInitiated) START_REDELIVER_INTENT else START_NOT_STICKY
}

private fun ensureForegroundStarted() {
Expand Down
Loading