Modern Apps git

← Modern-Apps.git

screentime: guard settings launch, picker range, friction break

Commit
efd0293051e86e6b570933dff9ed2be23fb36390
Author
Vayun Mathur <[email protected]>
When
2026-11-19 01:53 UTC (17 days ago)
Parents
140a070
Three runtime throw paths on device:

- DashboardScreen opened ACTION_USAGE_ACCESS_SETTINGS with a bare
  startActivity; a Settings build without that activity throws
  ActivityNotFoundException and crashes the dashboard. Catch it and
  leave the empty dashboard in place.
- WindDownScreen fed the Room-persisted start/end minute straight
  into rememberTimePickerState, which throws IllegalArgumentException
  for hour/minute outside 0..23/0..59. A corrupt or legacy row with
  a minute outside 0..1439 crashed the picker. Coerce into range.
- FrictionActivity's take-a-break handler launched Coordinator
  .onTimerReached in an uncaught IO coroutine; a Room/SQLite failure
  crashed the activity and skipped finish(). Broad-catch like the
  broadcast receivers do for the identical call, and finish in
  finally.

Verification: ./gradlew :screentime:testDevUnitTest and
./check screentime (run after this commit).

Diff

screentime/src/main/java/com/vayunmathur/screentime/ui/DashboardScreen.kt
diff --git a/screentime/src/main/java/com/vayunmathur/screentime/ui/DashboardScreen.kt b/screentime/src/main/java/com/vayunmathur/screentime/ui/DashboardScreen.ktindex b03a48655c..50a4b62ff0 100644--- a/screentime/src/main/java/com/vayunmathur/screentime/ui/DashboardScreen.kt+++ b/screentime/src/main/java/com/vayunmathur/screentime/ui/DashboardScreen.kt@@ -1,5 +1,6 @@ package com.vayunmathur.screentime.ui +import android.content.ActivityNotFoundException import android.content.Intent import android.provider.Settings import androidx.compose.foundation.background@@ -56,10 +57,16 @@ data class DashboardActions( fun DashboardScreen(state: ScreenTimeUiState, actions: DashboardActions) {     val context = LocalContext.current     val openUsageAccess = {-        context.startActivity(-            Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)-                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),-        )+        // Some images ship a Settings build with no usage-access activity; a missing+        // handler must not crash the dashboard.+        try {+            context.startActivity(+                Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)+                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),+            )+        } catch (_: ActivityNotFoundException) {+            // No settings screen to open on this image; the empty dashboard remains.+        }     }     val topUsed = remember(state.apps) { state.apps.maxOfOrNull { it.usedMillis } ?: 0L } 
screentime/src/main/java/com/vayunmathur/screentime/ui/FrictionScreen.kt
diff --git a/screentime/src/main/java/com/vayunmathur/screentime/ui/FrictionScreen.kt b/screentime/src/main/java/com/vayunmathur/screentime/ui/FrictionScreen.ktindex 679d07d444..bee0c8f086 100644--- a/screentime/src/main/java/com/vayunmathur/screentime/ui/FrictionScreen.kt+++ b/screentime/src/main/java/com/vayunmathur/screentime/ui/FrictionScreen.kt@@ -57,11 +57,18 @@ class FrictionActivity : ComponentActivity() {                     onTakeABreak = {                         lifecycleScope.launch(Dispatchers.IO) {                             // A break spends the app immediately: the normal spent path pauses-                            // it until midnight, with no timer row left behind.-                            if (pkg != null) {-                                Coordinator(this@FrictionActivity).onTimerReached(pkg)+                            // it until midnight, with no timer row left behind. A DB failure+                            // here must not crash the activity or leave it on screen: the+                            // receivers broad-catch the identical call for this reason.+                            try {+                                if (pkg != null) {+                                    Coordinator(this@FrictionActivity).onTimerReached(pkg)+                                }+                            } catch (_: Exception) {+                                // Enforcement is retried by the next coordinator pass.+                            } finally {+                                finish()                             }-                            finish()                         }                     },                 )
screentime/src/main/java/com/vayunmathur/screentime/ui/WindDownScreen.kt
diff --git a/screentime/src/main/java/com/vayunmathur/screentime/ui/WindDownScreen.kt b/screentime/src/main/java/com/vayunmathur/screentime/ui/WindDownScreen.ktindex 6e3748d80b..158d54c6ce 100644--- a/screentime/src/main/java/com/vayunmathur/screentime/ui/WindDownScreen.kt+++ b/screentime/src/main/java/com/vayunmathur/screentime/ui/WindDownScreen.kt@@ -106,7 +106,10 @@ fun WindDownScreen(      val active = picking     if (active != null) {-        val initial = if (active == WindDownPicking.Start) schedule.startMinute else schedule.endMinute+        // The schedule is Room-persisted; a corrupt or legacy row can hold a minute+        // outside 0..1439, and TimePickerState throws on an out-of-range hour/minute.+        val initial = (if (active == WindDownPicking.Start) schedule.startMinute else schedule.endMinute)+            .coerceIn(0, MINUTES_PER_DAY - 1)         val timeState = rememberTimePickerState(             initialHour = initial / 60,             initialMinute = initial % 60,@@ -143,6 +146,7 @@ fun WindDownScreen( private enum class WindDownPicking { Start, End }  private const val WINDDOWN_DAYS = 7+private const val MINUTES_PER_DAY = 24 * 60  private fun formatWindDownMinute(minuteOfDay: Int): String =     "%02d:%02d".format(minuteOfDay / 60, minuteOfDay % 60)