feat(bedtime): add an opt-in bedtime mode under Settings
Clockula can now turn on a Do Not Disturb mode of its own at bedtime and off at the wake-up time, on the nights the user picks, with an optional reminder before it starts. Digital Wellbeing's Bedtime mode can't be driven by a third-party app, so this is a separate rule: a typed mode with grayscale, dimmed wallpaper, dark theme and hidden always-on display on Android 15+, and a plain DND schedule rule on Android 10-14. The rule's policy always allows alarms (and media). BedtimeEngine owns one AlarmManager slot of its own, on its own receiver and request code, and every bedtime call on the boot, time-change and launch paths runs after the alarm and timer passes, guarded. The rule id is never stored: the platform lists an app's own rules, so restores and reinstalls find or recreate it. Turning bedtime off removes the rule. The screen walks the user through granting DND access and explains the Wellbeing overlap. Settings are part of the JSON backup; older files restore bedtime as off. Refs #1
This commit is contained in:
1 parent
3f431a7f32
commit
69b436cffd
46 files changed
+2132
-16
No files matched your search
@@ -0,0 +1,122 @@
|
||||
package de.jeanlucmakiola.clockula.bedtime
|
||||
|
||||
import de.jeanlucmakiola.clockula.data.prefs.SettingsPrefs
|
||||
import de.jeanlucmakiola.clockula.domain.bedtime.BedtimeSchedule
|
||||
import de.jeanlucmakiola.clockula.domain.bedtime.BedtimeSettings
|
||||
import de.jeanlucmakiola.clockula.domain.time.WallClock
|
||||
import de.jeanlucmakiola.clockula.domain.time.ZoneProvider
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* The bedtime schedule (issue #1), orchestrated the way the other engines
|
||||
* orchestrate theirs: plain Kotlin, the platform behind three seams.
|
||||
*
|
||||
* It owns one AlarmManager slot, armed for the schedule's next event (the
|
||||
* reminder, bedtime, or the wake time), and one Do Not Disturb rule, which it
|
||||
* creates while the schedule is on and Clockula has Do Not Disturb access,
|
||||
* and removes when the schedule is turned off. Every entry point is an
|
||||
* idempotent pass over "where does the schedule stand right now", so a late,
|
||||
* missed or repeated fire settles into the right state.
|
||||
*
|
||||
* It never touches an alarm: the rule lets alarms through, and its slot is
|
||||
* its own.
|
||||
*/
|
||||
@Singleton
|
||||
class BedtimeEngine @Inject constructor(
|
||||
private val settings: SettingsPrefs,
|
||||
private val scheduler: BedtimeScheduler,
|
||||
private val mode: BedtimeMode,
|
||||
private val notifier: BedtimeNotifier,
|
||||
private val wallClock: WallClock,
|
||||
private val zones: ZoneProvider,
|
||||
) {
|
||||
|
||||
private val lock = Mutex()
|
||||
|
||||
val bedtime: Flow<BedtimeSettings> = settings.bedtime
|
||||
|
||||
/**
|
||||
* A change made on the bedtime screen: stored, then applied at once. The
|
||||
* rule is rewritten from the new settings, and the mode is switched to
|
||||
* what the new schedule says now, so turning bedtime on at 23:30 for a
|
||||
* 23:00 bedtime starts it straight away.
|
||||
*/
|
||||
suspend fun update(transform: (BedtimeSettings) -> BedtimeSettings) = lock.withLock {
|
||||
val updated = transform(settings.currentBedtime())
|
||||
settings.setBedtime(updated)
|
||||
apply(updated, refreshRule = true)
|
||||
}
|
||||
|
||||
/**
|
||||
* Everything that can invalidate the slot or the rule: a reboot, an app
|
||||
* update, a clock or zone change, a backup restore, and Do Not Disturb
|
||||
* access being granted or revoked.
|
||||
*/
|
||||
suspend fun resync(refreshRule: Boolean = false) = lock.withLock {
|
||||
apply(settings.currentBedtime(), refreshRule = refreshRule)
|
||||
}
|
||||
|
||||
/**
|
||||
* App launch: re-arm the slot only. AlarmManager forgets it on a force
|
||||
* stop, but the mode's state is left alone here — a launch is not a
|
||||
* bedtime event, and a mode the user switched on or off by hand should
|
||||
* stay that way until the next one.
|
||||
*/
|
||||
suspend fun rearm() = lock.withLock {
|
||||
schedule(settings.currentBedtime())
|
||||
}
|
||||
|
||||
/**
|
||||
* The slot arriving. A reminder fire posts the reminder and leaves the
|
||||
* mode alone; any other fire switches the mode to what the schedule says
|
||||
* now. Either way the next event is armed.
|
||||
*/
|
||||
suspend fun onSlotFired() = lock.withLock {
|
||||
val current = settings.currentBedtime()
|
||||
val now = wallClock.now()
|
||||
val zone = zones.current()
|
||||
val reminderFor = BedtimeSchedule.dueReminder(current, now, zone)
|
||||
if (reminderFor != null) {
|
||||
notifier.showReminder(reminderFor.start, current.reminderMinutes)
|
||||
schedule(current)
|
||||
} else {
|
||||
apply(current, refreshRule = false)
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether Clockula may change Do Not Disturb, for the screen's onboarding row. */
|
||||
fun hasAccess(): Boolean = mode.hasAccess()
|
||||
|
||||
val supportsEffects: Boolean get() = mode.supportsEffects
|
||||
|
||||
fun ruleId(): String? = mode.ruleId()
|
||||
|
||||
private fun apply(current: BedtimeSettings, refreshRule: Boolean) {
|
||||
if (!current.enabled) {
|
||||
scheduler.cancel()
|
||||
notifier.cancelReminder()
|
||||
// Removing the rule ends the mode if it is on. Without access there
|
||||
// is nothing Clockula may remove; revoking access in Settings
|
||||
// already removed the rule.
|
||||
if (mode.hasAccess()) mode.uninstall()
|
||||
return
|
||||
}
|
||||
val now = wallClock.now()
|
||||
val active = BedtimeSchedule.activeWindow(current, now, zones.current()) != null
|
||||
if (active) notifier.cancelReminder()
|
||||
if (mode.hasAccess()) {
|
||||
mode.install(current, refresh = refreshRule)
|
||||
mode.setActive(active)
|
||||
}
|
||||
schedule(current)
|
||||
}
|
||||
|
||||
private fun schedule(current: BedtimeSettings) {
|
||||
val next = BedtimeSchedule.nextEvent(current, wallClock.now(), zones.current())
|
||||
if (next == null) scheduler.cancel() else scheduler.schedule(next)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user