clock: detekt clean (170 tests)
- Commit
1003d786b9e815c81e875aa1a3ad43a7dcb62b37- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-14 23:22 UTC (21 days ago)
- Parents
3c6dd16
Fix all 37 detekt findings: real KDoc for 35 undocumented public classes; BootReceiver.onReceive (LongMethod 74) split into rescheduleAlarms()/restoreTimers(); genuinely unused alarmId parameter removed from AlarmReceiver.postRingNotification. Verified: ./gradlew :clock:detekt :clock:testDevUnitTest :clock:lint --max-workers=32 — detekt 0 findings, lint passes; 170 unit tests with one first-run failure in ReceiversAndNotificationsTest.stopwatchHelperCancelsWhenResetAndPostsWhenRunning that passes in isolation on re-run (flaky, untouched by these edits and absent from /tmp/inventory.json). No @Suppress added.
Diff
clock/src/main/java/com/vayunmathur/clock/MainActivity.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/MainActivity.kt b/clock/src/main/java/com/vayunmathur/clock/MainActivity.ktindex 6d9ddf778a..6f0ec856dc 100644--- a/clock/src/main/java/com/vayunmathur/clock/MainActivity.kt+++ b/clock/src/main/java/com/vayunmathur/clock/MainActivity.kt@@ -37,6 +37,10 @@ import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.TextButton import com.vayunmathur.library.util.DataStoreUtils +/**+ * Entry activity that owns the [ClockViewModel], creates notification channels, and gates+ * the UI on notification and alarm permissions.+ */ class MainActivity : MaActivity() { private val clockViewModel: ClockViewModel by viewModels { ClockViewModelFactory(application, ClockRepository.get(application))
clock/src/main/java/com/vayunmathur/clock/Route.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/Route.kt b/clock/src/main/java/com/vayunmathur/clock/Route.ktindex 60c85a812f..064642f6be 100644--- a/clock/src/main/java/com/vayunmathur/clock/Route.kt+++ b/clock/src/main/java/com/vayunmathur/clock/Route.kt@@ -4,24 +4,34 @@ import com.vayunmathur.library.util.NavKey import kotlinx.datetime.LocalTime import kotlinx.serialization.Serializable +/** Navigation destinations for the clock app: the tabbed main screen and its dialogs. */ @Serializable sealed interface Route : NavKey {+ /** The tabbed main screen, opening on [initialTab]. */ @Serializable data class Main(val initialTab: Int = 0) : Route+ /** The alarm tab destination. */ @Serializable data object Alarm : Route+ /** The world-clock tab destination. */ @Serializable data object Clock : Route+ /** The timer tab destination. */ @Serializable data object Timer : Route+ /** The stopwatch tab destination. */ @Serializable data object Stopwatch : Route+ /** The alarm-settings destination. */ @Serializable data object AlarmSettings : Route+ /** Dialog for choosing world-clock time zones. */ @Serializable data object SelectTimeZonesDialog : Route+ /** Dialog to create a timer, optionally prefilled with a length and label. */ @Serializable data class NewTimerDialog(val lengthSeconds: Int? = null, val message: String? = null) : Route+ /** Dialog to create an alarm, optionally prefilled and optionally skipping the UI. */ @Serializable data class NewAlarmDialog( val hour: Int? = null,@@ -30,6 +40,7 @@ sealed interface Route : NavKey { val days: ArrayList<Int>? = null, val skipUi: Boolean = false, ) : Route+ /** Dialog to set the time of the alarm identified by [id] to [time]. */ @Serializable data class AlarmSetTimeDialog(val id: Long, val time: LocalTime) : Route }
clock/src/main/java/com/vayunmathur/clock/data/ClockDatabase.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/data/ClockDatabase.kt b/clock/src/main/java/com/vayunmathur/clock/data/ClockDatabase.ktindex cc8101f5e3..1b0eecb583 100644--- a/clock/src/main/java/com/vayunmathur/clock/data/ClockDatabase.kt+++ b/clock/src/main/java/com/vayunmathur/clock/data/ClockDatabase.kt@@ -19,6 +19,7 @@ import kotlin.time.Clock import kotlin.time.Duration import kotlin.time.Instant +/** A persisted countdown timer, with its remaining length measured from [remainingStartTime] while running. */ @Entity data class Timer( var isRunning: Boolean,@@ -43,6 +44,7 @@ data class Timer( } } +/** A persisted alarm; [days] is a weekday bitmask with bit 0 as Sunday. */ @Entity data class Alarm( var time: LocalTime,@@ -63,6 +65,7 @@ data class Alarm( override var id: Long = 0, ): DatabaseItem +/** Room access for persisted [Timer] rows. */ @Dao interface TimerDao { @Query("SELECT * FROM Timer")@@ -81,6 +84,7 @@ interface TimerDao { suspend fun delete(value: Timer): Int } +/** Room access for persisted [Alarm] rows, ordered by time. */ @Dao interface AlarmDao { @Query("SELECT * FROM Alarm ORDER BY time")@@ -99,12 +103,14 @@ interface AlarmDao { suspend fun delete(value: Alarm): Int } +/** Room database holding the clock app's timers and alarms. */ @ColumnTypeConverters(DefaultConverters::class) @Database(entities = [Timer::class, Alarm::class], version = 3, exportSchema = false) abstract class ClockDatabase: RoomDatabase() { abstract fun timerDao(): TimerDao abstract fun alarmDao(): AlarmDao + /** Migration definitions upgrading the clock database schema between versions. */ companion object : com.vayunmathur.library.util.DatabaseMigrations { override val migrations: List<Migration> = listOf( Migration(1, 2) {
clock/src/main/java/com/vayunmathur/clock/data/ClockRepository.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/data/ClockRepository.kt b/clock/src/main/java/com/vayunmathur/clock/data/ClockRepository.ktindex d174d60be9..2364413c3b 100644--- a/clock/src/main/java/com/vayunmathur/clock/data/ClockRepository.kt+++ b/clock/src/main/java/com/vayunmathur/clock/data/ClockRepository.kt@@ -45,6 +45,7 @@ class ClockRepository private constructor(context: Context) : suspend fun upsertAlarm(alarm: Alarm): Long = alarmDao.upsert(alarm) suspend fun deleteAlarm(alarm: Alarm): Int = alarmDao.delete(alarm) + /** Holds the process-wide [ClockRepository] singleton. */ companion object { @Volatile private var instance: ClockRepository? = null
clock/src/main/java/com/vayunmathur/clock/platform/AlarmActivity.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/platform/AlarmActivity.kt b/clock/src/main/java/com/vayunmathur/clock/platform/AlarmActivity.ktindex ff40d3209c..45a25ef936 100644--- a/clock/src/main/java/com/vayunmathur/clock/platform/AlarmActivity.kt+++ b/clock/src/main/java/com/vayunmathur/clock/platform/AlarmActivity.kt@@ -20,6 +20,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +/** Full-screen ringing screen shown when an alarm fires, offering dismiss and snooze. */ class AlarmActivity : ComponentActivity() { private var alarmId: Long = -1L private var alarm: Alarm? by mutableStateOf(null)
clock/src/main/java/com/vayunmathur/clock/platform/AlarmReceiver.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/platform/AlarmReceiver.kt b/clock/src/main/java/com/vayunmathur/clock/platform/AlarmReceiver.ktindex 7a95ab05a7..90d4488a13 100644--- a/clock/src/main/java/com/vayunmathur/clock/platform/AlarmReceiver.kt+++ b/clock/src/main/java/com/vayunmathur/clock/platform/AlarmReceiver.kt@@ -18,6 +18,10 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch +/**+ * Receives a fired alarm, posts the insistent ring notification, starts the sound service,+ * and re-arms repeating alarms.+ */ class AlarmReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { createNotificationChannels(context)@@ -47,7 +51,7 @@ class AlarmReceiver : BroadcastReceiver() { // behind (stale notification, second service start) before posting our own. stopPreviousRing(context) - postRingNotification(context, intent, alarmId, pendingIntent)+ postRingNotification(context, intent, pendingIntent) rescheduleAlarm(context, alarmId, isSnooze) startSoundService(context, alarmId) startRingActivity(context, alarmId, ringIntent)@@ -56,7 +60,6 @@ class AlarmReceiver : BroadcastReceiver() { private fun postRingNotification( context: Context, intent: Intent,- alarmId: Long, pendingIntent: PendingIntent, ) { // 3. Build the Notification. For most alarms this is the backstop: FLAG_INSISTENT
clock/src/main/java/com/vayunmathur/clock/platform/AlarmScheduler.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/platform/AlarmScheduler.kt b/clock/src/main/java/com/vayunmathur/clock/platform/AlarmScheduler.ktindex 2b474a3592..c00ddbfc96 100644--- a/clock/src/main/java/com/vayunmathur/clock/platform/AlarmScheduler.kt+++ b/clock/src/main/java/com/vayunmathur/clock/platform/AlarmScheduler.kt@@ -18,6 +18,7 @@ import kotlin.time.Clock import kotlin.time.Duration.Companion.minutes import kotlin.time.Instant +/** Schedules, cancels, and snoozes alarms through AlarmManager, carrying ring settings in the broadcast extras. */ object AlarmScheduler { /** * Whether this alarm fades in, carried in the broadcast so AlarmReceiver can pick a channel
clock/src/main/java/com/vayunmathur/clock/platform/BootReceiver.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/platform/BootReceiver.kt b/clock/src/main/java/com/vayunmathur/clock/platform/BootReceiver.ktindex c94d94f112..3cfd2380d0 100644--- a/clock/src/main/java/com/vayunmathur/clock/platform/BootReceiver.kt+++ b/clock/src/main/java/com/vayunmathur/clock/platform/BootReceiver.kt@@ -37,77 +37,10 @@ class BootReceiver : BroadcastReceiver() { val pendingResult = goAsync() CoroutineScope(SupervisorJob() + Dispatchers.IO).launch { try {- val alarms = repository.getAllAlarms()- var rings = 0- var snoozes = 0 val now = Clock.System.now()- alarms.filter { it.enabled }.forEach { alarm ->- AlarmScheduler.schedule(context, alarm)- rings++- // Snoozes live in AlarmManager, which does not survive a- // reboot; the trigger time is persisted on the alarm, so- // re-arm those still in the future. Spent ones just clear.- val snoozedUntil = alarm.snoozedUntil- if (snoozedUntil != null) {- if (snoozedUntil > now) {- // Re-arm for the persisted instant itself:- // recomputing from whole minutes would ring- // up to 59 seconds early.- AlarmScheduler.scheduleSnoozeAt(- context,- repository,- alarm,- snoozedUntil.toEpochMilliseconds(),- )- snoozes++- } else {- repository.upsertAlarm(alarm.copy(snoozedUntil = null))- }- }- }- // Running timers are AlarmManager entries too, so a reboot- // kills them exactly like alarms; nothing else ever re-armed- // them, and the row would sit at isRunning=true counting- // into negative time with no finish notification. Re-arm- // those still in the future and complete those that- // expired while the device was off. (On a timezone/clock- // change this same pass is a no-op repost: a timer's end- // is a duration from a stored instant, which neither- // change moves.)- var timersRearmed = 0- var timersCompleted = 0- repository.getAllTimers().filter { it.isRunning }.forEach { timer ->- // One bad timer must not take the others down with it.- runCatching {- if (timerRemaining(timer, now) > Duration.ZERO) {- sendTimerNotification(context, timer, true)- timersRearmed++- } else {- repository.upsertTimer(- timer.copy(- isRunning = false,- remainingLength = Duration.ZERO,- remainingStartTime = now,- )- )- // TimerReceiver posts the "time's up"- // notification and re-writes the completed- // row. It is not direct-boot aware, so at- // LOCKED_BOOT_COMPLETED this broadcast may- // go nowhere; the completed row above is- // what the timer list then shows.- context.sendBroadcast(- Intent(context, TimerReceiver::class.java).apply {- putExtra("timer_id", timer.id)- putExtra("timer_name", timer.name)- }- )- timersCompleted++- }- }.onFailure {- Log.error(TAG, "Timer ${timer.id}: could not restore after ${intent.action}", it)- }- }+ val (rings, snoozes) = rescheduleAlarms(context, repository, now)+ val (timersRearmed, timersCompleted) =+ restoreTimers(context, repository, now, intent.action) Log.status( TAG, "${intent.action}: rescheduled $rings alarm(s) and $snoozes snooze(s); " +@@ -127,6 +60,87 @@ class BootReceiver : BroadcastReceiver() { } } + private suspend fun rescheduleAlarms(+ context: Context,+ repository: ClockRepository,+ now: kotlin.time.Instant,+ ): Pair<Int, Int> {+ var rings = 0+ var snoozes = 0+ repository.getAllAlarms().filter { it.enabled }.forEach { alarm ->+ AlarmScheduler.schedule(context, alarm)+ rings+++ // Snoozes live in AlarmManager, which does not survive a reboot; the+ // trigger time is persisted on the alarm, so re-arm those still in the+ // future. Spent ones just clear.+ val snoozedUntil = alarm.snoozedUntil+ if (snoozedUntil != null) {+ if (snoozedUntil > now) {+ // Re-arm for the persisted instant itself: recomputing from whole+ // minutes would ring up to 59 seconds early.+ AlarmScheduler.scheduleSnoozeAt(+ context,+ repository,+ alarm,+ snoozedUntil.toEpochMilliseconds(),+ )+ snoozes+++ } else {+ repository.upsertAlarm(alarm.copy(snoozedUntil = null))+ }+ }+ }+ return rings to snoozes+ }++ private suspend fun restoreTimers(+ context: Context,+ repository: ClockRepository,+ now: kotlin.time.Instant,+ action: String?,+ ): Pair<Int, Int> {+ // Running timers are AlarmManager entries too, so a reboot kills them exactly+ // like alarms; nothing else ever re-armed them, and the row would sit at+ // isRunning=true counting into negative time with no finish notification.+ // Re-arm those still in the future and complete those that expired while the+ // device was off. (On a timezone/clock change this same pass is a no-op+ // repost: a timer's end is a duration from a stored instant, which neither+ // change moves.)+ var timersRearmed = 0+ var timersCompleted = 0+ repository.getAllTimers().filter { it.isRunning }.forEach { timer ->+ // One bad timer must not take the others down with it.+ runCatching {+ if (timerRemaining(timer, now) > Duration.ZERO) {+ sendTimerNotification(context, timer, true)+ timersRearmed+++ } else {+ repository.upsertTimer(+ timer.copy(+ isRunning = false,+ remainingLength = Duration.ZERO,+ remainingStartTime = now,+ )+ )+ // TimerReceiver posts the "time's up" notification and re-writes+ // the completed row. It is not direct-boot aware, so at+ // LOCKED_BOOT_COMPLETED this broadcast may go nowhere; the+ // completed row above is what the timer list then shows.+ context.sendBroadcast(+ Intent(context, TimerReceiver::class.java).apply {+ putExtra("timer_id", timer.id)+ putExtra("timer_name", timer.name)+ }+ )+ timersCompleted+++ }+ }.onFailure {+ Log.error(TAG, "Timer ${timer.id}: could not restore after $action", it)+ }+ }+ return timersRearmed to timersCompleted+ }+ private companion object { const val TAG = "ClockBootReceiver" }
clock/src/main/java/com/vayunmathur/clock/platform/ClockUiContract.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/platform/ClockUiContract.kt b/clock/src/main/java/com/vayunmathur/clock/platform/ClockUiContract.ktindex b9950c4d1d..005bb11cf7 100644--- a/clock/src/main/java/com/vayunmathur/clock/platform/ClockUiContract.kt+++ b/clock/src/main/java/com/vayunmathur/clock/platform/ClockUiContract.kt@@ -41,6 +41,7 @@ interface AlarmActions { fun setSnoozeMinutes(alarm: Alarm, minutes: Int) {} fun setGradualVolumeSeconds(alarm: Alarm, seconds: Int) {} + /** Provides the no-op implementation used by previews. */ companion object { val Noop: AlarmActions = object : AlarmActions {} }@@ -79,6 +80,7 @@ interface TimerActions { /** Play/pause, or restart a timer that has already finished. */ fun toggle(timer: Timer) {} + /** Provides the no-op implementation used by previews. */ companion object { val Noop: TimerActions = object : TimerActions {} }@@ -108,6 +110,7 @@ interface StopwatchActions { fun resetStopwatch() {} fun addLap() {} + /** Provides the no-op implementation used by previews. */ companion object { val Noop: StopwatchActions = object : StopwatchActions {} }
clock/src/main/java/com/vayunmathur/clock/platform/ClockViewModel.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/platform/ClockViewModel.kt b/clock/src/main/java/com/vayunmathur/clock/platform/ClockViewModel.ktindex 8d0684ab59..df4817c77a 100644--- a/clock/src/main/java/com/vayunmathur/clock/platform/ClockViewModel.kt+++ b/clock/src/main/java/com/vayunmathur/clock/platform/ClockViewModel.kt@@ -351,6 +351,7 @@ class ClockViewModel( return map } + /** Tick timings and DataStore keys for alarm defaults. */ companion object { private const val TAG = "ClockViewModel" private const val TICK_MS = 100L
clock/src/main/java/com/vayunmathur/clock/platform/StopwatchActionReceiver.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/platform/StopwatchActionReceiver.kt b/clock/src/main/java/com/vayunmathur/clock/platform/StopwatchActionReceiver.ktindex 4b4263866c..3632790598 100644--- a/clock/src/main/java/com/vayunmathur/clock/platform/StopwatchActionReceiver.kt+++ b/clock/src/main/java/com/vayunmathur/clock/platform/StopwatchActionReceiver.kt@@ -13,6 +13,7 @@ import kotlin.time.Clock import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds +/** Handles stopwatch notification actions (toggle, lap, stop) against DataStore-backed stopwatch state. */ class StopwatchActionReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val action = intent.action ?: return@@ -71,6 +72,7 @@ class StopwatchActionReceiver : BroadcastReceiver() { } } + /** Notification action names, DataStore keys, and notification id for the stopwatch. */ companion object { const val ACTION_TOGGLE = "com.vayunmathur.clock.STOPWATCH_TOGGLE" const val ACTION_LAP = "com.vayunmathur.clock.STOPWATCH_LAP"
clock/src/main/java/com/vayunmathur/clock/platform/StopwatchNotificationHelper.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/platform/StopwatchNotificationHelper.kt b/clock/src/main/java/com/vayunmathur/clock/platform/StopwatchNotificationHelper.ktindex fa0142aa70..e776d6cfb3 100644--- a/clock/src/main/java/com/vayunmathur/clock/platform/StopwatchNotificationHelper.kt+++ b/clock/src/main/java/com/vayunmathur/clock/platform/StopwatchNotificationHelper.kt@@ -12,6 +12,7 @@ import com.vayunmathur.library.util.DataStoreUtils import java.util.Locale import kotlin.time.Clock +/** Builds and updates the ongoing stopwatch notification with pause, lap, and stop actions. */ object StopwatchNotificationHelper { fun updateNotification(context: Context) { val ds = DataStoreUtils.getInstance(context)
clock/src/main/java/com/vayunmathur/clock/platform/TimerActionReceiver.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/platform/TimerActionReceiver.kt b/clock/src/main/java/com/vayunmathur/clock/platform/TimerActionReceiver.ktindex f47d173e81..8ab34c3b52 100644--- a/clock/src/main/java/com/vayunmathur/clock/platform/TimerActionReceiver.kt+++ b/clock/src/main/java/com/vayunmathur/clock/platform/TimerActionReceiver.kt@@ -18,6 +18,7 @@ import kotlinx.coroutines.launch import kotlin.time.Clock import kotlin.time.Duration +/** Handles timer notification actions (pause, resume, cancel, reset) against persisted timers. */ class TimerActionReceiver : BroadcastReceiver() { // Broad catch is deliberate: onReceive must not throw, and Room throws // undocumented RuntimeExceptions (not just SQLiteException).@@ -132,6 +133,7 @@ class TimerActionReceiver : BroadcastReceiver() { am.cancel(pendingAlarm) } + /** Notification action names broadcast to this receiver. */ companion object { const val ACTION_PAUSE = "com.vayunmathur.clock.TIMER_PAUSE" const val ACTION_RESUME = "com.vayunmathur.clock.TIMER_RESUME"
clock/src/main/java/com/vayunmathur/clock/platform/TimerReciever.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/platform/TimerReciever.kt b/clock/src/main/java/com/vayunmathur/clock/platform/TimerReciever.ktindex edbfeeb402..5fdd4df258 100644--- a/clock/src/main/java/com/vayunmathur/clock/platform/TimerReciever.kt+++ b/clock/src/main/java/com/vayunmathur/clock/platform/TimerReciever.kt@@ -15,6 +15,7 @@ import kotlinx.coroutines.launch import kotlin.time.Clock import kotlin.time.Duration +/** Receives a finished timer, posts its completion notification, and marks the timer stopped. */ class TimerReceiver : BroadcastReceiver() { // Broad catch is deliberate: onReceive must not throw, and Room throws // undocumented RuntimeExceptions (not just SQLiteException).
clock/src/main/java/com/vayunmathur/clock/service/AlarmSoundService.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/service/AlarmSoundService.kt b/clock/src/main/java/com/vayunmathur/clock/service/AlarmSoundService.ktindex 22cfbab2e8..fb595d12e6 100644--- a/clock/src/main/java/com/vayunmathur/clock/service/AlarmSoundService.kt+++ b/clock/src/main/java/com/vayunmathur/clock/service/AlarmSoundService.kt@@ -28,6 +28,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +/** Foreground service that plays the ringing alarm's ringtone and vibration until dismissed, snoozed, or timed out. */ class AlarmSoundService : Service() { private var mediaPlayer: MediaPlayer? = null private var vibrator: Vibrator? = null@@ -250,6 +251,7 @@ class AlarmSoundService : Service() { override fun onBind(intent: Intent?) = null + /** Notification id and ring timeout shared with the alarm receiver. */ companion object { const val NOTIFICATION_ID = 1001
clock/src/main/java/com/vayunmathur/clock/ui/dialogs/NewTimerDialog.kt
diff --git a/clock/src/main/java/com/vayunmathur/clock/ui/dialogs/NewTimerDialog.kt b/clock/src/main/java/com/vayunmathur/clock/ui/dialogs/NewTimerDialog.ktindex cd891120f5..5b9415b20d 100644--- a/clock/src/main/java/com/vayunmathur/clock/ui/dialogs/NewTimerDialog.kt+++ b/clock/src/main/java/com/vayunmathur/clock/ui/dialogs/NewTimerDialog.kt@@ -133,6 +133,7 @@ fun NewTimerDialog( ) } +/** Formats up to six raw digits as an hh:mm:ss display string for the new-timer field. */ class TimeVisualTransformation : VisualTransformation { override fun filter(text: AnnotatedString): TransformedText { val raw = text.text.padStart(6, '0')