diff options
author | Ted Bauer <tedbauer@google.com> | 2023-10-19 18:21:20 +0000 |
---|---|---|
committer | Ted Bauer <tedbauer@google.com> | 2023-10-19 19:53:01 +0000 |
commit | 3b4145c476fb1939d2e3ee17ba04cdb7bf0cbc67 (patch) | |
tree | b2716feaa008325a1588a9a4b543be7e73a60cac | |
parent | 80db8278c17157cfbc76c791517f085c74238f1d (diff) | |
download | ConfigInfrastructure-3b4145c476fb1939d2e3ee17ba04cdb7bf0cbc67.tar.gz |
Don't show notif if we rebooted in last 24hr.
Currently, the boot notif appears at a set time each day, if
aconfig flags are staged that day. With this change, it's the
same, except that when it's time to show a notification, we
check if it's been at least 24 hours since the last reboot.
If it has, we show the notification, otherwise we postpone to
the next day.
Test: tested manually on device
Bug: 306199946
Change-Id: I7f0ff9f70f1f49fe4642cfa2f63f5bfdecbca12e
-rw-r--r-- | service/java/com/android/server/deviceconfig/BootNotificationCreator.java | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/service/java/com/android/server/deviceconfig/BootNotificationCreator.java b/service/java/com/android/server/deviceconfig/BootNotificationCreator.java index 38a4e57..0ba1429 100644 --- a/service/java/com/android/server/deviceconfig/BootNotificationCreator.java +++ b/service/java/com/android/server/deviceconfig/BootNotificationCreator.java @@ -24,6 +24,7 @@ import java.time.ZoneId; import java.time.ZonedDateTime; import static android.app.NotificationManager.IMPORTANCE_HIGH; +import static java.time.temporal.ChronoUnit.SECONDS; /** * Creates notifications when flags are staged on the device. @@ -55,8 +56,11 @@ class BootNotificationCreator implements OnPropertiesChangedListener { private Context context; - private static final int REBOOT_HOUR = 18; - private static final int REBOOT_MINUTE = 2; + private static final int REBOOT_HOUR = 10; + private static final int REBOOT_MINUTE = 0; + private static final int MIN_SECONDS_TO_SHOW_NOTIF = 86400; + + private LocalDateTime lastReboot; public BootNotificationCreator(@NonNull Context context) { this.context = context; @@ -69,6 +73,8 @@ class BootNotificationCreator implements OnPropertiesChangedListener { new PostNotificationBroadcastReceiver(), new IntentFilter(ACTION_POST_NOTIFICATION), Context.RECEIVER_EXPORTED); + + this.lastReboot = LocalDateTime.now(ZoneId.systemDefault()); } @Override @@ -106,6 +112,14 @@ class BootNotificationCreator implements OnPropertiesChangedListener { private class PostNotificationBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { + LocalDateTime now = LocalDateTime.now(ZoneId.systemDefault()); + + if (lastReboot.until(now, SECONDS) < MIN_SECONDS_TO_SHOW_NOTIF) { + Slog.w(TAG, "not enough time passed, punting"); + tryAgainIn24Hours(now); + return; + } + PendingIntent pendingIntent = PendingIntent.getBroadcast( context, @@ -130,6 +144,24 @@ class BootNotificationCreator implements OnPropertiesChangedListener { Slog.e(TAG, "failed to post boot notification", e); } } + + private void tryAgainIn24Hours(LocalDateTime currentTime) { + PendingIntent pendingIntent = + PendingIntent.getBroadcast( + context, + /* requestCode= */ 1, + new Intent(ACTION_POST_NOTIFICATION), + PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); + + LocalDateTime postTime = + currentTime.toLocalDate().atTime(REBOOT_HOUR, REBOOT_MINUTE).plusDays(1); + long scheduledPostTimeLong = postTime + .atZone(ZoneId.systemDefault()) + .toInstant() + .toEpochMilli(); + alarmManager.setExact( + AlarmManager.RTC_WAKEUP, scheduledPostTimeLong, pendingIntent); + } } private class HardRebootBroadcastReceiver extends BroadcastReceiver { |