diff --git a/DOCS-bypass.md b/DOCS-bypass.md index 568d6c3..ed0d20d 100644 --- a/DOCS-bypass.md +++ b/DOCS-bypass.md @@ -88,8 +88,9 @@ bypass; this one catches it the evening before, while kuhy can still fix it. every `AlarmScheduler.schedule`, boot and the watchdog, always the next future 21:00) and `NotifBlockReceiver` on `APP_BLOCK_STATE_CHANGED` / `NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED` for the ring channel - (`app_block`, `channel_block`). Both receivers hold the broadcast 25 s so - the headless engine is not frozen mid-push. + (`app_block`, `channel_block`). Both receivers hold the broadcast open + (`NotifRules.holdMs`: 8 s for a foreground broadcast, whose ANR budget is + 10 s, else 25 s) so the headless engine is not frozen mid-push. - **PC** (`_fallback_evening`, rule in `_evening_policy`). The fallback timer also ticks 21:00-21:59; those ticks skip the morning code. When today's newest `notifications` event says off, the PC rings diff --git a/phone_app/android/app/src/main/kotlin/com/kuhy/wake_alarm_sync/NotifAlarm.kt b/phone_app/android/app/src/main/kotlin/com/kuhy/wake_alarm_sync/NotifAlarm.kt index c6ace80..a362764 100644 --- a/phone_app/android/app/src/main/kotlin/com/kuhy/wake_alarm_sync/NotifAlarm.kt +++ b/phone_app/android/app/src/main/kotlin/com/kuhy/wake_alarm_sync/NotifAlarm.kt @@ -32,14 +32,6 @@ object NotifAlarm { private const val REQUEST_CODE = 1007 private const val TAG = "WakeAlarmNotifAlarm" - /** - * How long a receiver holds its broadcast open after starting the - * publish: a receiver that returns lets Android freeze the headless Dart - * engine mid-request (see [PoolSyncReceiver]). Well inside the 60 s a - * background broadcast may stay open. - */ - const val HOLD_MS = 25_000L - fun schedule(context: Context) { val at = NotifRules.nextEvening(LocalDateTime.now()) val millis = at.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() @@ -67,10 +59,12 @@ object NotifAlarm { return "setAndAllowWhileIdle" } - /** Holds [receiver]'s broadcast open for [HOLD_MS], then lets it go. */ - fun hold(receiver: BroadcastReceiver) { + /** Holds [receiver]'s broadcast of [intent] open for [NotifRules.holdMs], then lets it go. */ + fun hold(receiver: BroadcastReceiver, intent: Intent) { + val foreground = (intent.flags and Intent.FLAG_RECEIVER_FOREGROUND) != 0 val pending = receiver.goAsync() - Handler(Looper.getMainLooper()).postDelayed({ pending.finish() }, HOLD_MS) + Handler(Looper.getMainLooper()) + .postDelayed({ pending.finish() }, NotifRules.holdMs(foreground)) } private fun pendingIntent(context: Context): PendingIntent = PendingIntent.getBroadcast( @@ -85,7 +79,7 @@ object NotifAlarm { class NotifCheckReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { NotifAlarm.schedule(context) - NotifAlarm.hold(this) + NotifAlarm.hold(this, intent) NotifState.report(context, "evening") } } @@ -99,7 +93,7 @@ class NotifBlockReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val channel = intent.getStringExtra(NotificationManager.EXTRA_NOTIFICATION_CHANNEL_ID) val trigger = NotifRules.trigger(intent.action, channel) ?: return - NotifAlarm.hold(this) + NotifAlarm.hold(this, intent) NotifState.report(context, trigger) } } diff --git a/phone_app/android/app/src/main/kotlin/com/kuhy/wake_alarm_sync/NotifState.kt b/phone_app/android/app/src/main/kotlin/com/kuhy/wake_alarm_sync/NotifState.kt index 73cdd1d..be2adf8 100644 --- a/phone_app/android/app/src/main/kotlin/com/kuhy/wake_alarm_sync/NotifState.kt +++ b/phone_app/android/app/src/main/kotlin/com/kuhy/wake_alarm_sync/NotifState.kt @@ -24,6 +24,15 @@ object NotifRules { return if (tonight.isAfter(now)) tonight else tonight.plusDays(1) } + /** + * How long a receiver holds its broadcast open after starting the push: + * a receiver that returns lets Android freeze the headless Dart engine + * mid-request (see [PoolSyncReceiver]). A foreground broadcast (the + * block-state ones are sent that way) has a 10 s budget, a background + * one 60 s; overrunning either is an ANR that kills the push. + */ + fun holdMs(foreground: Boolean): Long = if (foreground) 8_000L else 25_000L + /** The `notifications` event's data. */ fun eventData(enabled: Boolean, trigger: String): Map = mapOf("enabled" to enabled, "trigger" to trigger) diff --git a/phone_app/android/app/src/test/kotlin/com/kuhy/wake_alarm_sync/NotifRulesTest.kt b/phone_app/android/app/src/test/kotlin/com/kuhy/wake_alarm_sync/NotifRulesTest.kt index c0e4ed4..5a7a380 100644 --- a/phone_app/android/app/src/test/kotlin/com/kuhy/wake_alarm_sync/NotifRulesTest.kt +++ b/phone_app/android/app/src/test/kotlin/com/kuhy/wake_alarm_sync/NotifRulesTest.kt @@ -45,6 +45,12 @@ class NotifRulesTest { ) } + @Test + fun aForegroundBroadcastIsHeldInsideItsTenSeconds() { + assertTrue(NotifRules.holdMs(foreground = true) < 10_000L) + assertTrue(NotifRules.holdMs(foreground = false) in 10_000L until 60_000L) + } + @Test fun onlyTheAppOrTheRingChannelCounts() { val app = NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED