diff --git a/core/java/android/os/PowerManager.java b/core/java/android/os/PowerManager.java index 814a248cbb049..d11f3ce0d1123 100644 --- a/core/java/android/os/PowerManager.java +++ b/core/java/android/os/PowerManager.java @@ -525,6 +525,25 @@ public final class PowerManager { @Retention(RetentionPolicy.SOURCE) public @interface WakeReason{} + /** + * @hide + */ + @IntDef(prefix = { "GO_TO_SLEEP_REASON_" }, value = { + 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_FORCE_SUSPEND, + GO_TO_SLEEP_REASON_INATTENTIVE, + GO_TO_SLEEP_REASON_QUIESCENT + }) + @Retention(RetentionPolicy.SOURCE) + public @interface GoToSleepReason{} + /** * Wake up reason code: Waking for an unknown reason. * @hide diff --git a/core/java/android/view/WindowManagerPolicyConstants.java b/core/java/android/view/WindowManagerPolicyConstants.java index dd55f049b364c..7668d80d3cb13 100644 --- a/core/java/android/view/WindowManagerPolicyConstants.java +++ b/core/java/android/view/WindowManagerPolicyConstants.java @@ -17,6 +17,7 @@ package android.view; import android.annotation.IntDef; +import android.os.PowerManager; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -108,6 +109,27 @@ public interface WindowManagerPolicyConstants { void onPointerEvent(MotionEvent motionEvent); } + @IntDef(prefix = { "OFF_BECAUSE_OF_" }, value = { + OFF_BECAUSE_OF_ADMIN, + OFF_BECAUSE_OF_USER, + OFF_BECAUSE_OF_TIMEOUT, + }) + @Retention(RetentionPolicy.SOURCE) + public @interface OffReason{} + + static @OffReason int translateSleepReasonToOffReason( + @PowerManager.GoToSleepReason int reason) { + switch (reason) { + case PowerManager.GO_TO_SLEEP_REASON_DEVICE_ADMIN: + return OFF_BECAUSE_OF_ADMIN; + case PowerManager.GO_TO_SLEEP_REASON_TIMEOUT: + case PowerManager.GO_TO_SLEEP_REASON_INATTENTIVE: + return OFF_BECAUSE_OF_TIMEOUT; + default: + return OFF_BECAUSE_OF_USER; + } + } + /** Screen turned off because of a device admin */ int OFF_BECAUSE_OF_ADMIN = 1; /** Screen turned off because of power button */ @@ -137,6 +159,23 @@ public interface WindowManagerPolicyConstants { } } + static @OnReason int translateWakeReasonToOnReason(@PowerManager.WakeReason int reason) { + switch (reason) { + case PowerManager.WAKE_REASON_POWER_BUTTON: + case PowerManager.WAKE_REASON_PLUGGED_IN: + case PowerManager.WAKE_REASON_GESTURE: + case PowerManager.WAKE_REASON_CAMERA_LAUNCH: + case PowerManager.WAKE_REASON_WAKE_KEY: + case PowerManager.WAKE_REASON_WAKE_MOTION: + case PowerManager.WAKE_REASON_LID: + return ON_BECAUSE_OF_USER; + case PowerManager.WAKE_REASON_APPLICATION: + return ON_BECAUSE_OF_APPLICATION; + default: + return ON_BECAUSE_OF_UNKNOWN; + } + } + /** Screen turned on because of a user-initiated action. */ int ON_BECAUSE_OF_USER = 1; /** Screen turned on because of an application request or event */ diff --git a/core/java/com/android/internal/policy/IKeyguardService.aidl b/core/java/com/android/internal/policy/IKeyguardService.aidl index 54f31f9199cbe..b01e4a83a438e 100644 --- a/core/java/com/android/internal/policy/IKeyguardService.aidl +++ b/core/java/com/android/internal/policy/IKeyguardService.aidl @@ -42,26 +42,29 @@ oneway interface IKeyguardService { /** * Called when the device has started going to sleep. * - * @param why {@link #OFF_BECAUSE_OF_USER}, {@link #OFF_BECAUSE_OF_ADMIN}, - * or {@link #OFF_BECAUSE_OF_TIMEOUT}. + * @param pmSleepReason One of PowerManager.GO_TO_SLEEP_REASON_*, detailing the specific reason + * we're going to sleep, such as GO_TO_SLEEP_REASON_POWER_BUTTON or GO_TO_SLEEP_REASON_TIMEOUT. */ - void onStartedGoingToSleep(int reason); + void onStartedGoingToSleep(int pmSleepReason); /** * Called when the device has finished going to sleep. * - * @param why {@link #OFF_BECAUSE_OF_USER}, {@link #OFF_BECAUSE_OF_ADMIN}, - * or {@link #OFF_BECAUSE_OF_TIMEOUT}. + * @param pmSleepReason One of PowerManager.GO_TO_SLEEP_REASON_*, detailing the specific reason + * we're going to sleep, such as GO_TO_SLEEP_REASON_POWER_BUTTON or GO_TO_SLEEP_REASON_TIMEOUT. * @param cameraGestureTriggered whether the camera gesture was triggered between * {@link #onStartedGoingToSleep} and this method; if it's been * triggered, we shouldn't lock the device. */ - void onFinishedGoingToSleep(int reason, boolean cameraGestureTriggered); + void onFinishedGoingToSleep(int pmSleepReason, boolean cameraGestureTriggered); /** * Called when the device has started waking up. + + * @param pmWakeReason One of PowerManager.WAKE_REASON_*, detailing the reason we're waking up, + * such as WAKE_REASON_POWER_BUTTON or WAKE_REASON_GESTURE. */ - void onStartedWakingUp(); + void onStartedWakingUp(int pmWakeReason); /** * Called when the device has finished waking up. diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardLifecyclesDispatcher.java b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardLifecyclesDispatcher.java index f527775449bf8..5019e65c7182d 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardLifecyclesDispatcher.java +++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardLifecyclesDispatcher.java @@ -18,6 +18,7 @@ package com.android.systemui.keyguard; import android.os.Handler; import android.os.Message; +import android.os.PowerManager; import com.android.systemui.dagger.SysUISingleton; @@ -53,6 +54,17 @@ public class KeyguardLifecyclesDispatcher { mHandler.obtainMessage(what).sendToTarget(); } + /** + * @param what Message to send. + * @param pmReason Reason this message was triggered - this should be a value from either + * {@link PowerManager.WakeReason} or {@link PowerManager.GoToSleepReason}. + */ + void dispatch(int what, int pmReason) { + final Message message = mHandler.obtainMessage(what); + message.arg1 = pmReason; + message.sendToTarget(); + } + private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { @@ -70,13 +82,13 @@ public class KeyguardLifecyclesDispatcher { mScreenLifecycle.dispatchScreenTurnedOff(); break; case STARTED_WAKING_UP: - mWakefulnessLifecycle.dispatchStartedWakingUp(); + mWakefulnessLifecycle.dispatchStartedWakingUp(msg.arg1 /* pmReason */); break; case FINISHED_WAKING_UP: mWakefulnessLifecycle.dispatchFinishedWakingUp(); break; case STARTED_GOING_TO_SLEEP: - mWakefulnessLifecycle.dispatchStartedGoingToSleep(); + mWakefulnessLifecycle.dispatchStartedGoingToSleep(msg.arg1 /* pmReason */); break; case FINISHED_GOING_TO_SLEEP: mWakefulnessLifecycle.dispatchFinishedGoingToSleep(); diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java index cb83656c38424..1b033e91b76b7 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java +++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java @@ -24,9 +24,11 @@ import android.os.Binder; import android.os.Bundle; import android.os.Debug; import android.os.IBinder; +import android.os.PowerManager; import android.os.Process; import android.os.Trace; import android.util.Log; +import android.view.WindowManagerPolicyConstants; import com.android.internal.policy.IKeyguardDismissCallback; import com.android.internal.policy.IKeyguardDrawnCallback; @@ -117,27 +119,32 @@ public class KeyguardService extends Service { } @Override // Binder interface - public void onStartedGoingToSleep(int reason) { + public void onStartedGoingToSleep(@PowerManager.GoToSleepReason int pmSleepReason) { checkPermission(); - mKeyguardViewMediator.onStartedGoingToSleep(reason); + mKeyguardViewMediator.onStartedGoingToSleep( + WindowManagerPolicyConstants.translateSleepReasonToOffReason(pmSleepReason)); mKeyguardLifecyclesDispatcher.dispatch( - KeyguardLifecyclesDispatcher.STARTED_GOING_TO_SLEEP); + KeyguardLifecyclesDispatcher.STARTED_GOING_TO_SLEEP, pmSleepReason); } @Override // Binder interface - public void onFinishedGoingToSleep(int reason, boolean cameraGestureTriggered) { + public void onFinishedGoingToSleep( + @PowerManager.GoToSleepReason int pmSleepReason, boolean cameraGestureTriggered) { checkPermission(); - mKeyguardViewMediator.onFinishedGoingToSleep(reason, cameraGestureTriggered); + mKeyguardViewMediator.onFinishedGoingToSleep( + WindowManagerPolicyConstants.translateSleepReasonToOffReason(pmSleepReason), + cameraGestureTriggered); mKeyguardLifecyclesDispatcher.dispatch( KeyguardLifecyclesDispatcher.FINISHED_GOING_TO_SLEEP); } @Override // Binder interface - public void onStartedWakingUp() { + public void onStartedWakingUp(@PowerManager.WakeReason int pmWakeReason) { Trace.beginSection("KeyguardService.mBinder#onStartedWakingUp"); checkPermission(); mKeyguardViewMediator.onStartedWakingUp(); - mKeyguardLifecyclesDispatcher.dispatch(KeyguardLifecyclesDispatcher.STARTED_WAKING_UP); + mKeyguardLifecyclesDispatcher.dispatch( + KeyguardLifecyclesDispatcher.STARTED_WAKING_UP, pmWakeReason); Trace.endSection(); } diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java index f79b9910fff40..bf87ac46cff1c 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java +++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java @@ -85,13 +85,11 @@ import com.android.keyguard.KeyguardUpdateMonitorCallback; import com.android.keyguard.KeyguardViewController; import com.android.keyguard.ViewMediatorCallback; import com.android.systemui.Dumpable; -import com.android.systemui.R; import com.android.systemui.SystemUI; import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.classifier.FalsingCollector; import com.android.systemui.dagger.qualifiers.UiBackground; import com.android.systemui.dump.DumpManager; -import com.android.systemui.keyguard.KeyguardService; import com.android.systemui.keyguard.dagger.KeyguardModule; import com.android.systemui.navigationbar.NavigationModeController; import com.android.systemui.shared.system.QuickStepContract; @@ -863,11 +861,11 @@ public class KeyguardViewMediator extends SystemUI implements Dumpable { /** * Called to let us know the screen was turned off. - * @param why either {@link WindowManagerPolicyConstants#OFF_BECAUSE_OF_USER} or - * {@link WindowManagerPolicyConstants#OFF_BECAUSE_OF_TIMEOUT}. + * @param offReason either {@link WindowManagerPolicyConstants#OFF_BECAUSE_OF_USER} or + * {@link WindowManagerPolicyConstants#OFF_BECAUSE_OF_TIMEOUT}. */ - public void onStartedGoingToSleep(int why) { - if (DEBUG) Log.d(TAG, "onStartedGoingToSleep(" + why + ")"); + public void onStartedGoingToSleep(@WindowManagerPolicyConstants.OffReason int offReason) { + if (DEBUG) Log.d(TAG, "onStartedGoingToSleep(" + offReason + ")"); synchronized (this) { mDeviceInteractive = false; mGoingToSleep = true; @@ -900,8 +898,11 @@ public class KeyguardViewMediator extends SystemUI implements Dumpable { } } else if (mShowing) { mPendingReset = true; - } else if ((why == WindowManagerPolicyConstants.OFF_BECAUSE_OF_TIMEOUT && timeout > 0) - || (why == WindowManagerPolicyConstants.OFF_BECAUSE_OF_USER && !lockImmediately)) { + } else if ( + (offReason == WindowManagerPolicyConstants.OFF_BECAUSE_OF_TIMEOUT + && timeout > 0) + || (offReason == WindowManagerPolicyConstants.OFF_BECAUSE_OF_USER + && !lockImmediately)) { doKeyguardLaterLocked(timeout); mLockLater = true; } else if (!mLockPatternUtils.isLockScreenDisabled(currentUser)) { @@ -912,12 +913,18 @@ public class KeyguardViewMediator extends SystemUI implements Dumpable { playSounds(true); } } - mUpdateMonitor.dispatchStartedGoingToSleep(why); + mUpdateMonitor.dispatchStartedGoingToSleep(offReason); notifyStartedGoingToSleep(); } - public void onFinishedGoingToSleep(int why, boolean cameraGestureTriggered) { - if (DEBUG) Log.d(TAG, "onFinishedGoingToSleep(" + why + ")"); + /** + * Called to let us know the screen finished turning off. + * @param offReason either {@link WindowManagerPolicyConstants#OFF_BECAUSE_OF_USER} or + * {@link WindowManagerPolicyConstants#OFF_BECAUSE_OF_TIMEOUT}. + */ + public void onFinishedGoingToSleep( + @WindowManagerPolicyConstants.OffReason int offReason, boolean cameraGestureTriggered) { + if (DEBUG) Log.d(TAG, "onFinishedGoingToSleep(" + offReason + ")"); synchronized (this) { mDeviceInteractive = false; mGoingToSleep = false; @@ -957,7 +964,7 @@ public class KeyguardViewMediator extends SystemUI implements Dumpable { } } - mUpdateMonitor.dispatchFinishedGoingToSleep(why); + mUpdateMonitor.dispatchFinishedGoingToSleep(offReason); } private boolean isKeyguardServiceEnabled() { diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/WakefulnessLifecycle.java b/packages/SystemUI/src/com/android/systemui/keyguard/WakefulnessLifecycle.java index 5161deb0e4dea..de00d50b6e361 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/WakefulnessLifecycle.java +++ b/packages/SystemUI/src/com/android/systemui/keyguard/WakefulnessLifecycle.java @@ -17,6 +17,7 @@ package com.android.systemui.keyguard; import android.annotation.IntDef; +import android.os.PowerManager; import android.os.Trace; import com.android.systemui.Dumpable; @@ -51,6 +52,9 @@ public class WakefulnessLifecycle extends Lifecycle mPolicy.startedWakingUp(mInteractiveChangeReason)); // Send interactive broadcast. mPendingInteractiveState = INTERACTIVE_STATE_AWAKE; @@ -470,13 +463,7 @@ public class Notifier { } else { // Going to sleep... // Tell the policy that we started going to sleep. - final int why = translateOffReason(mInteractiveChangeReason); - mHandler.post(new Runnable() { - @Override - public void run() { - mPolicy.startedGoingToSleep(why); - } - }); + mHandler.post(() -> mPolicy.startedGoingToSleep(mInteractiveChangeReason)); } } } @@ -492,20 +479,17 @@ public class Notifier { (int) (SystemClock.uptimeMillis() - mInteractiveChangeStartTime); if (mInteractive) { // Finished waking up... - final int why = translateOnReason(mInteractiveChangeReason); - mHandler.post(new Runnable() { - @Override - public void run() { - LogMaker log = new LogMaker(MetricsEvent.SCREEN); - log.setType(MetricsEvent.TYPE_OPEN); - log.setSubtype(why); - log.setLatency(interactiveChangeLatency); - log.addTaggedData( - MetricsEvent.FIELD_SCREEN_WAKE_REASON, mInteractiveChangeReason); - MetricsLogger.action(log); - EventLogTags.writePowerScreenState(1, 0, 0, 0, interactiveChangeLatency); - mPolicy.finishedWakingUp(why); - } + mHandler.post(() -> { + LogMaker log = new LogMaker(MetricsEvent.SCREEN); + log.setType(MetricsEvent.TYPE_OPEN); + log.setSubtype(WindowManagerPolicyConstants.translateWakeReasonToOnReason( + mInteractiveChangeReason)); + log.setLatency(interactiveChangeLatency); + log.addTaggedData( + MetricsEvent.FIELD_SCREEN_WAKE_REASON, mInteractiveChangeReason); + MetricsLogger.action(log); + EventLogTags.writePowerScreenState(1, 0, 0, 0, interactiveChangeLatency); + mPolicy.finishedWakingUp(mInteractiveChangeReason); }); } else { // Finished going to sleep... @@ -521,20 +505,19 @@ public class Notifier { } // Tell the policy we finished going to sleep. - final int why = translateOffReason(mInteractiveChangeReason); - mHandler.post(new Runnable() { - @Override - public void run() { - LogMaker log = new LogMaker(MetricsEvent.SCREEN); - log.setType(MetricsEvent.TYPE_CLOSE); - log.setSubtype(why); - log.setLatency(interactiveChangeLatency); - log.addTaggedData( - MetricsEvent.FIELD_SCREEN_SLEEP_REASON, mInteractiveChangeReason); - MetricsLogger.action(log); - EventLogTags.writePowerScreenState(0, why, 0, 0, interactiveChangeLatency); - mPolicy.finishedGoingToSleep(why); - } + final int offReason = WindowManagerPolicyConstants.translateSleepReasonToOffReason( + mInteractiveChangeReason); + mHandler.post(() -> { + LogMaker log = new LogMaker(MetricsEvent.SCREEN); + log.setType(MetricsEvent.TYPE_CLOSE); + log.setSubtype(offReason); + log.setLatency(interactiveChangeLatency); + log.addTaggedData( + MetricsEvent.FIELD_SCREEN_SLEEP_REASON, mInteractiveChangeReason); + MetricsLogger.action(log); + EventLogTags.writePowerScreenState( + 0, offReason, 0, 0, interactiveChangeLatency); + mPolicy.finishedGoingToSleep(mInteractiveChangeReason); }); // Send non-interactive broadcast. @@ -545,35 +528,6 @@ public class Notifier { } } - private static int translateOffReason(int reason) { - switch (reason) { - case PowerManager.GO_TO_SLEEP_REASON_DEVICE_ADMIN: - return WindowManagerPolicy.OFF_BECAUSE_OF_ADMIN; - case PowerManager.GO_TO_SLEEP_REASON_TIMEOUT: - case PowerManager.GO_TO_SLEEP_REASON_INATTENTIVE: - return WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT; - default: - return WindowManagerPolicy.OFF_BECAUSE_OF_USER; - } - } - - private static @OnReason int translateOnReason(@WakeReason int reason) { - switch (reason) { - case PowerManager.WAKE_REASON_POWER_BUTTON: - case PowerManager.WAKE_REASON_PLUGGED_IN: - case PowerManager.WAKE_REASON_GESTURE: - case PowerManager.WAKE_REASON_CAMERA_LAUNCH: - case PowerManager.WAKE_REASON_WAKE_KEY: - case PowerManager.WAKE_REASON_WAKE_MOTION: - case PowerManager.WAKE_REASON_LID: - return WindowManagerPolicy.ON_BECAUSE_OF_USER; - case PowerManager.WAKE_REASON_APPLICATION: - return WindowManagerPolicy.ON_BECAUSE_OF_APPLICATION; - default: - return WindowManagerPolicy.ON_BECAUSE_OF_UNKNOWN; - } - } - /** * Called when there has been user activity. */ diff --git a/services/tests/wmtests/src/com/android/server/wm/TestWindowManagerPolicy.java b/services/tests/wmtests/src/com/android/server/wm/TestWindowManagerPolicy.java index ccf2394e7f81b..e4b865fd2941e 100644 --- a/services/tests/wmtests/src/com/android/server/wm/TestWindowManagerPolicy.java +++ b/services/tests/wmtests/src/com/android/server/wm/TestWindowManagerPolicy.java @@ -28,6 +28,7 @@ import android.content.res.CompatibilityInfo; import android.content.res.Configuration; import android.os.Bundle; import android.os.IBinder; +import android.os.PowerManager.GoToSleepReason; import android.os.PowerManager.WakeReason; import android.os.RemoteException; import android.util.proto.ProtoOutputStream; @@ -177,19 +178,19 @@ class TestWindowManagerPolicy implements WindowManagerPolicy { } @Override - public void startedWakingUp(@WakeReason int reason) { + public void startedWakingUp(@WakeReason int wakeReason) { } @Override - public void finishedWakingUp(@WakeReason int reason) { + public void finishedWakingUp(@WakeReason int wakeReason) { } @Override - public void startedGoingToSleep(int why) { + public void startedGoingToSleep(@GoToSleepReason int sleepReason) { } @Override - public void finishedGoingToSleep(int why) { + public void finishedGoingToSleep(@GoToSleepReason int sleepReason) { } @Override