summaryrefslogtreecommitdiff
path: root/core/java/android/os/PowerManager.java
diff options
context:
space:
mode:
authorLais Andrade <lsandrade@google.com>2022-05-10 15:10:51 +0100
committerLais Andrade <lsandrade@google.com>2022-05-13 19:53:43 +0100
commitc558857fc7fda905b56fbc6c57b80c0336ad3dcf (patch)
treeb00bc8cded2f6628d21959747939feac1485e917 /core/java/android/os/PowerManager.java
parentc9daa03269935ea5b36f7abf4b7bbda0a287afcf (diff)
Check sleep reason and time before cancelling vibrations
Add a method PowerManagerInternal.getLastGoToSleep(), similar to existing method getLastWakeup(), to retrieve global values for last go to sleep events, including system uptime and reason. Retrieve this data in VibratorManagerService before cancelling ongoing vibrations by the broadcast of ACTION_SCREEN_OFF intents to indicate that the screen was turned of when the vibration was still playing. This new logic allows vibrations to continue in the following cases: - the broadcasted event was triggered before the vibration started; - there is already a wakeup event triggered by the time the screen off broadcast is being processed by the vibrator service; - the screen off reason is in allowlist, indicating it's an automatically triggered event (screen timeout or user inattentive); This should handle the following scenarios: - delayed broadcasts of screen off events, that should not cancel vibrations that started when the screen was already off (e.g. notification or ringtone that are allowed to vibrate in that state); - race conditions when the screen automatically turns off right after a ringtone/notification vibration starts, before the notification acquires a screen lock; Fix: 219849350 Test: VibrationSettingsTest Change-Id: I358327192196989a7d4fc49a96b2ab92ec677302
Diffstat (limited to 'core/java/android/os/PowerManager.java')
-rw-r--r--core/java/android/os/PowerManager.java73
1 files changed, 54 insertions, 19 deletions
diff --git a/core/java/android/os/PowerManager.java b/core/java/android/os/PowerManager.java
index 8f2d218a20c1..13ca2c34b27e 100644
--- a/core/java/android/os/PowerManager.java
+++ b/core/java/android/os/PowerManager.java
@@ -463,21 +463,22 @@ public final class PowerManager {
/**
* @hide
*/
- public static String sleepReasonToString(int sleepReason) {
+ public static String sleepReasonToString(@GoToSleepReason int sleepReason) {
switch (sleepReason) {
+ case GO_TO_SLEEP_REASON_ACCESSIBILITY: return "accessibility";
case GO_TO_SLEEP_REASON_APPLICATION: return "application";
case GO_TO_SLEEP_REASON_DEVICE_ADMIN: return "device_admin";
- case GO_TO_SLEEP_REASON_TIMEOUT: return "timeout";
+ case GO_TO_SLEEP_REASON_DEVICE_FOLD: return "device_folded";
+ case GO_TO_SLEEP_REASON_DISPLAY_GROUP_REMOVED: return "display_group_removed";
+ case GO_TO_SLEEP_REASON_DISPLAY_GROUPS_TURNED_OFF: return "display_groups_turned_off";
+ case GO_TO_SLEEP_REASON_FORCE_SUSPEND: return "force_suspend";
+ case GO_TO_SLEEP_REASON_HDMI: return "hdmi";
+ case GO_TO_SLEEP_REASON_INATTENTIVE: return "inattentive";
case GO_TO_SLEEP_REASON_LID_SWITCH: return "lid_switch";
case GO_TO_SLEEP_REASON_POWER_BUTTON: return "power_button";
- case GO_TO_SLEEP_REASON_HDMI: return "hdmi";
+ case GO_TO_SLEEP_REASON_QUIESCENT: return "quiescent";
case GO_TO_SLEEP_REASON_SLEEP_BUTTON: return "sleep_button";
- case GO_TO_SLEEP_REASON_ACCESSIBILITY: return "accessibility";
- case GO_TO_SLEEP_REASON_FORCE_SUSPEND: return "force_suspend";
- case GO_TO_SLEEP_REASON_INATTENTIVE: return "inattentive";
- case GO_TO_SLEEP_REASON_DISPLAY_GROUP_REMOVED: return "display_group_removed";
- case GO_TO_SLEEP_REASON_DISPLAY_GROUPS_TURNED_OFF: return "display_groups_turned_off";
- case GO_TO_SLEEP_REASON_DEVICE_FOLD: return "device_folded";
+ case GO_TO_SLEEP_REASON_TIMEOUT: return "timeout";
default: return Integer.toString(sleepReason);
}
}
@@ -576,18 +577,20 @@ public final class PowerManager {
* @hide
*/
@IntDef(prefix = { "GO_TO_SLEEP_REASON_" }, value = {
+ GO_TO_SLEEP_REASON_ACCESSIBILITY,
GO_TO_SLEEP_REASON_APPLICATION,
GO_TO_SLEEP_REASON_DEVICE_ADMIN,
- GO_TO_SLEEP_REASON_TIMEOUT,
- GO_TO_SLEEP_REASON_LID_SWITCH,
- GO_TO_SLEEP_REASON_POWER_BUTTON,
- GO_TO_SLEEP_REASON_HDMI,
- GO_TO_SLEEP_REASON_SLEEP_BUTTON,
- GO_TO_SLEEP_REASON_ACCESSIBILITY,
+ GO_TO_SLEEP_REASON_DEVICE_FOLD,
+ GO_TO_SLEEP_REASON_DISPLAY_GROUP_REMOVED,
+ GO_TO_SLEEP_REASON_DISPLAY_GROUPS_TURNED_OFF,
GO_TO_SLEEP_REASON_FORCE_SUSPEND,
+ GO_TO_SLEEP_REASON_HDMI,
GO_TO_SLEEP_REASON_INATTENTIVE,
+ GO_TO_SLEEP_REASON_LID_SWITCH,
+ GO_TO_SLEEP_REASON_POWER_BUTTON,
GO_TO_SLEEP_REASON_QUIESCENT,
- GO_TO_SLEEP_REASON_DEVICE_FOLD
+ GO_TO_SLEEP_REASON_SLEEP_BUTTON,
+ GO_TO_SLEEP_REASON_TIMEOUT,
})
@Retention(RetentionPolicy.SOURCE)
public @interface GoToSleepReason{}
@@ -704,6 +707,8 @@ public final class PowerManager {
}
/**
+ * Information related to the device waking up, triggered by {@link #wakeUp}.
+ *
* @hide
*/
public static class WakeData {
@@ -712,9 +717,9 @@ public final class PowerManager {
this.wakeReason = wakeReason;
this.sleepDuration = sleepDuration;
}
- public long wakeTime;
- public @WakeReason int wakeReason;
- public long sleepDuration;
+ public final long wakeTime;
+ public final @WakeReason int wakeReason;
+ public final long sleepDuration;
@Override
public boolean equals(@Nullable Object o) {
@@ -733,6 +738,35 @@ public final class PowerManager {
}
/**
+ * Information related to the device going to sleep, triggered by {@link #goToSleep}.
+ *
+ * @hide
+ */
+ public static class SleepData {
+ public SleepData(long goToSleepUptimeMillis, @GoToSleepReason int goToSleepReason) {
+ this.goToSleepUptimeMillis = goToSleepUptimeMillis;
+ this.goToSleepReason = goToSleepReason;
+ }
+ public final long goToSleepUptimeMillis;
+ public final @GoToSleepReason int goToSleepReason;
+
+ @Override
+ public boolean equals(@Nullable Object o) {
+ if (o instanceof SleepData) {
+ final SleepData other = (SleepData) o;
+ return goToSleepUptimeMillis == other.goToSleepUptimeMillis
+ && goToSleepReason == other.goToSleepReason;
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(goToSleepUptimeMillis, goToSleepReason);
+ }
+ }
+
+ /**
* The value to pass as the 'reason' argument to reboot() to reboot into
* recovery mode for tasks other than applying system updates, such as
* doing factory resets.
@@ -2644,6 +2678,7 @@ public final class PowerManager {
*
* @hide
*/
+ @GoToSleepReason
public int getLastSleepReason() {
try {
return mService.getLastSleepReason();