mirror of
https://github.com/kuhyx/wake-alarm.git
synced 2026-11-22 15:02:17 +01:00
Hold block broadcasts inside their ANR budget
The block-state broadcasts may be foreground (10 s budget): hold 8 s for those, 25 s otherwise (NotifRules.holdMs). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
df314d5dbe
commit
0d66b64fb5
@ -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
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<String, Any?> =
|
||||
mapOf("enabled" to enabled, "trigger" to trigger)
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user