diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionArtifacts.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionArtifacts.kt index ab78d79624..2b4744574f 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionArtifacts.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionArtifacts.kt @@ -544,6 +544,7 @@ object SessionArtifacts { "session died of a crash, the entry is in there unless the device rebooted.\n" + (if (newest) exitReasons(context) else "") ) + restartNote(File(dir, "device.txt").lastModified(), bootedAt())?.let { recoveryNote.appendText(it) } if (newest && lastExitForMemory(context)) killedForMemory = true } if (recoveryPreviouslyAttempted && !runtimeCaptured.exists() && @@ -686,5 +687,21 @@ object SessionArtifacts { return gone.size } + private fun bootedAt(): Long = System.currentTimeMillis() - android.os.SystemClock.elapsedRealtime() + + internal fun restartNote(sessionStartedAt: Long, bootedAt: Long): String? { + if (sessionStartedAt <= 0L) return null + val stamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US) + return if (bootedAt > sessionStartedAt + BOOT_CLOCK_SLACK_MS) { + "The device itself probably restarted after this session began (estimated boot ${stamp.format(Date(bootedAt))}, from the clock and uptime, so a clock change can fake it):\n" + + "if so, the whole system went down and crash.log is the new boot's buffer and says nothing about the session.\n" + } else { + "The device does not appear to have restarted since this session began (estimated boot ${stamp.format(Date(bootedAt))}, from the clock and uptime):\n" + + "only this app's processes ended, so crash.log and the exit records cover the end.\n" + } + } + + private const val BOOT_CLOCK_SLACK_MS = 60_000L + private fun now(): String = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US).format(Date()) } diff --git a/app/src/test/java/com/droiddeck/launcher/session/SessionRestartNoteTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SessionRestartNoteTest.kt new file mode 100644 index 0000000000..230afe7db9 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/SessionRestartNoteTest.kt @@ -0,0 +1,30 @@ +package com.droiddeck.launcher.session + +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test + +class SessionRestartNoteTest { + private val started = 1_731_100_000_000L + + @Test fun aBootAfterTheSessionBeganIsReportedAsARestart() { + val note = SessionArtifacts.restartNote(started, started + 20 * 60_000L) + assertNotNull(note) + assertTrue(note!!.startsWith("The device itself probably restarted")) + } + + @Test fun aBootBeforeTheSessionBeganMeansOnlyTheAppEnded() { + val note = SessionArtifacts.restartNote(started, started - 3 * 3_600_000L) + assertTrue(note!!.startsWith("The device does not appear to have restarted")) + } + + @Test fun clockCorrectionsWithinAMinuteAreNotARestart() { + val note = SessionArtifacts.restartNote(started, started + 30_000L) + assertTrue(note!!.startsWith("The device does not appear to have restarted")) + } + + @Test fun aFolderWithoutAStartTimeSaysNothing() { + assertNull(SessionArtifacts.restartNote(0L, started)) + } +}