RF Sentinel (RFSentinel): free, open-source police detector app for Android. Spots police body cams, Flock ALPR cameras, AirTag trackers, Remote ID drones, smart glasses and fake cell towers from Bluetooth LE and WiFi. No root.
Kotlin airtagalprandroidandroid-autoanti-surveillancebluetooth-low-energybody-cameracounter-surveillancedrone-detectionflock-safetyimsi-catcher-detectorpolice-detectorpolice-scannerprivacyremote-idrtl-sdrsdrsurveillance-detectiontracker-detectionwifi
Crash log: on Android 11+ also log how earlier runs ended (native crash, not responding, killed with a service it used, low memory while in use), read at the next start
CIS-C03b3cc9334618
diff --git a/app/src/main/java/com/rfsentinel/app/RFSentinelApp.kt b/app/src/main/java/com/rfsentinel/app/RFSentinelApp.ktindex 9597893..e776e60 100644--- a/app/src/main/java/com/rfsentinel/app/RFSentinelApp.kt+++ b/app/src/main/java/com/rfsentinel/app/RFSentinelApp.kt@@ -41,6 +41,7 @@ class RFSentinelApp : Application() {override fun onCreate() {super.onCreate()com.rfsentinel.app.util.CrashLog.install(this)+ appScope.launch(Dispatchers.IO) { com.rfsentinel.app.util.CrashLog.recordExits(this@RFSentinelApp) }com.rfsentinel.app.ui.DeviceFilter.excludeAirTags = com.rfsentinel.app.util.Prefs.excludeAirTags(this)com.rfsentinel.app.ui.ThemeManager.install(this)com.rfsentinel.app.ui.ScreenAwake.install(this)diff --git a/app/src/main/java/com/rfsentinel/app/util/CrashLog.kt b/app/src/main/java/com/rfsentinel/app/util/CrashLog.ktindex ef45165..6820b31 100644--- a/app/src/main/java/com/rfsentinel/app/util/CrashLog.kt+++ b/app/src/main/java/com/rfsentinel/app/util/CrashLog.kt@@ -1,5 +1,7 @@package com.rfsentinel.app.util+import android.app.ActivityManager+import android.app.ApplicationExitInfoimport android.content.Contextimport android.content.Intentimport android.os.Build@@ -17,7 +19,8 @@ import java.util.Locale/*** Crashes written to a private file (never sent anywhere) so testers without adb can send them:* Settings > Lists, tools & about > Export crash log, or the prompt after the app closed- * unexpectedly. Keeps the newest ~200 KB.+ * unexpectedly. Keeps the newest ~200 KB. On Android 11+ other unexpected ends (native crash,+ * not responding, killed by the system) are added at the next start ([recordExits]).*/object CrashLog {private const val FILE = "crash_log.txt"@@ -44,6 +47,10 @@ object CrashLog {append("Thread: ${thread.name}\n")append(trace).append("\n")}+ append(context, entry)+ }++ private fun append(context: Context, entry: String) {val f = file(context)val old = if (f.exists()) f.readText() else ""f.writeText((old + entry).takeLast(MAX_BYTES))@@ -51,6 +58,86 @@ object CrashLog {context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit().putBoolean("unseen", true).commit()}+ /**+ * Android 11+: the ways earlier runs ended that [install] can't catch (native crash, not responding, killed with+ * Bluetooth or another service it used, low memory while in use), added to the log once. Looks back at most a week.+ * Android 8-10 keep no such record. Slow (reads traces): call it off the main thread.+ */+ fun recordExits(context: Context) {+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) return+ val prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)+ val seen = prefs.getLong("exits_seen", 0L)+ val exits = runCatching {+ context.getSystemService(ActivityManager::class.java)?.getHistoricalProcessExitReasons(null, 0, 0)+ }.getOrNull().orEmpty()+ val newest = exits.maxOfOrNull { it.timestamp } ?: return+ val since = maxOf(seen, System.currentTimeMillis() - 7 * 24 * 3_600_000L)+ val noted = exits.filter { it.timestamp > since && noteworthy(it.reason, it.importance) }.sortedBy { it.timestamp }+ prefs.edit().putLong("exits_seen", maxOf(newest, seen)).apply()+ if (noted.isNotEmpty()) append(context, noted.joinToString("") { exitEntry(it) })+ }++ /** Worth logging: not a normal end (closed, force-stopped, updated) nor a crash [install] already logged. */+ internal fun noteworthy(reason: Int, importance: Int) = when (reason) {+ ApplicationExitInfo.REASON_CRASH_NATIVE, ApplicationExitInfo.REASON_ANR, ApplicationExitInfo.REASON_DEPENDENCY_DIED,+ ApplicationExitInfo.REASON_INITIALIZATION_FAILURE, ApplicationExitInfo.REASON_EXCESSIVE_RESOURCE_USAGE -> true+ ApplicationExitInfo.REASON_EXIT_SELF, ApplicationExitInfo.REASON_USER_REQUESTED, ApplicationExitInfo.REASON_USER_STOPPED,+ ApplicationExitInfo.REASON_CRASH, ApplicationExitInfo.REASON_PERMISSION_CHANGE, ApplicationExitInfo.REASON_PACKAGE_STATE_CHANGE,+ ApplicationExitInfo.REASON_PACKAGE_UPDATED -> false+ // Low memory, signalled, other: normal for an app in the background, worth a look while on screen or scanning.+ else -> importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE+ }++ private fun reasonLabel(reason: Int) = when (reason) {+ ApplicationExitInfo.REASON_CRASH_NATIVE -> "native crash"+ ApplicationExitInfo.REASON_ANR -> "not responding (ANR)"+ ApplicationExitInfo.REASON_DEPENDENCY_DIED -> "killed because something it used died (Bluetooth or another system service?)"+ ApplicationExitInfo.REASON_INITIALIZATION_FAILURE -> "failed to start"+ ApplicationExitInfo.REASON_EXCESSIVE_RESOURCE_USAGE -> "killed for using too much CPU or memory"+ ApplicationExitInfo.REASON_LOW_MEMORY -> "killed for low memory"+ ApplicationExitInfo.REASON_SIGNALED -> "killed by a signal"+ ApplicationExitInfo.REASON_FREEZER -> "killed while frozen"+ else -> "other (reason $reason)"+ }++ @androidx.annotation.RequiresApi(Build.VERSION_CODES.R)+ private fun exitEntry(e: ApplicationExitInfo): String = buildString {+ append("==== ").append(SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", Locale.US).format(Date(e.timestamp))).append(" ====\n")+ append("RF Sentinel ${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE}) found at its next start: ${reasonLabel(e.reason)}\n")+ append("${Build.MANUFACTURER} ${Build.MODEL}, Android ${Build.VERSION.RELEASE} (API ${Build.VERSION.SDK_INT})\n")+ val where = when {+ e.importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND -> "on screen"+ e.importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE -> "scanning in the background"+ e.importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE -> "visible"+ else -> "in the background"+ }+ append("Process ${e.processName}, $where, status ${e.status}, memory ${e.pss / 1024} MB\n")+ e.description?.let { append("Description: ").append(it).append("\n") }+ val trace = runCatching { e.traceInputStream?.use { it.readBytes() } }.getOrNull()+ when {+ trace == null -> {}+ // A tombstone (protobuf) on Android 12+: its text parts hold the signal, backtrace and library names.+ e.reason == ApplicationExitInfo.REASON_CRASH_NATIVE -> append(printableRuns(trace).take(16_000)).append("\n")+ else -> append(String(trace, Charsets.UTF_8).take(32_000)).append("\n")+ }+ append("\n")+ }++ /** The runs of [min]+ readable ASCII characters in [bytes], one per line. */+ internal fun printableRuns(bytes: ByteArray, min: Int = 6): String {+ val out = StringBuilder()+ var start = -1+ for (i in 0..bytes.size) {+ val readable = i < bytes.size && bytes[i] in 0x20..0x7e+ if (readable && start < 0) start = i+ if (!readable && start >= 0) {+ if (i - start >= min) out.append(String(bytes, start, i - start, Charsets.US_ASCII)).append('\n')+ start = -1+ }+ }+ return out.toString()+ }+fun hasCrashes(context: Context) = file(context).let { it.exists() && it.length() > 0 }/** After a crash, once: offers to send the log. */diff --git a/app/src/test/java/com/rfsentinel/app/util/CrashLogExitTest.kt b/app/src/test/java/com/rfsentinel/app/util/CrashLogExitTest.ktnew file mode 100644index 0000000..1662d85--- /dev/null+++ b/app/src/test/java/com/rfsentinel/app/util/CrashLogExitTest.kt@@ -0,0 +1,32 @@+package com.rfsentinel.app.util++import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_CACHED+import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE+import android.app.ApplicationExitInfo+import org.junit.Assert.assertEquals+import org.junit.Assert.assertFalse+import org.junit.Assert.assertTrue+import org.junit.Test++/** Which earlier ends of the app go into the crash log, and the readable text of a native tombstone. */+class CrashLogExitTest {+ @Test+ fun crashesAndKillsWhileInUseAreLogged() {+ assertTrue(CrashLog.noteworthy(ApplicationExitInfo.REASON_CRASH_NATIVE, IMPORTANCE_CACHED))+ assertTrue(CrashLog.noteworthy(ApplicationExitInfo.REASON_ANR, IMPORTANCE_CACHED))+ assertTrue(CrashLog.noteworthy(ApplicationExitInfo.REASON_DEPENDENCY_DIED, IMPORTANCE_CACHED))+ assertTrue(CrashLog.noteworthy(ApplicationExitInfo.REASON_LOW_MEMORY, IMPORTANCE_FOREGROUND_SERVICE))+ // Normal: in the background, closed by the user, updated, or a crash the handler already logged.+ assertFalse(CrashLog.noteworthy(ApplicationExitInfo.REASON_LOW_MEMORY, IMPORTANCE_CACHED))+ assertFalse(CrashLog.noteworthy(ApplicationExitInfo.REASON_USER_REQUESTED, IMPORTANCE_FOREGROUND_SERVICE))+ assertFalse(CrashLog.noteworthy(ApplicationExitInfo.REASON_PACKAGE_UPDATED, IMPORTANCE_FOREGROUND_SERVICE))+ assertFalse(CrashLog.noteworthy(ApplicationExitInfo.REASON_CRASH, IMPORTANCE_FOREGROUND_SERVICE))+ }++ @Test+ fun tombstoneKeepsItsTextParts() {+ val bytes = byteArrayOf(0x0a, 0x07) + "SIGSEGV".toByteArray() + byteArrayOf(0x12, 0x02) + "ab".toByteArray() ++ byteArrayOf(0x1a, 0x0c) + "libmaplibre.so".toByteArray()+ assertEquals("SIGSEGV\nlibmaplibre.so\n", CrashLog.printableRuns(bytes))+ }+}