3939
4040/**
4141 * Sends broadcasts about important power state changes.
42- *
42+ * <p>
4343 * This methods of this class may be called by the power manager service while
4444 * its lock is being held. Internally it takes care of sending broadcasts to
4545 * notify other components of the system or applications asynchronously.
46- *
46+ * </p><p>
4747 * The notifier is designed to collapse unnecessary broadcasts when it is not
4848 * possible for the system to have observed an intermediate state.
49- *
50- * For example, if the device wakes up, goes to sleep and wakes up again immediately
51- * before the go to sleep broadcast has been sent, then no broadcast will be
52- * sent about the system going to sleep and waking up.
49+ * </p><p>
50+ * For example, if the device wakes up, goes to sleep, wakes up again and goes to
51+ * sleep again before the wake up notification is sent, then the system will
52+ * be told about only one wake up and sleep. However, we always notify the
53+ * fact that at least one transition occurred. It is especially important to
54+ * tell the system when we go to sleep so that it can lock the keyguard if needed.
55+ * </p>
5356 */
5457final class Notifier {
5558 private static final String TAG = "PowerManagerNotifier" ;
@@ -79,6 +82,10 @@ final class Notifier {
7982 private int mActualPowerState ;
8083 private int mLastGoToSleepReason ;
8184
85+ // True if there is a pending transition that needs to be reported.
86+ private boolean mPendingWakeUpBroadcast ;
87+ private boolean mPendingGoToSleepBroadcast ;
88+
8289 // The currently broadcasted power state. This reflects what other parts of the
8390 // system have observed.
8491 private int mBroadcastedPowerState ;
@@ -219,6 +226,7 @@ public void onWakeUpStarted() {
219226 synchronized (mLock ) {
220227 if (mActualPowerState != POWER_STATE_AWAKE ) {
221228 mActualPowerState = POWER_STATE_AWAKE ;
229+ mPendingWakeUpBroadcast = true ;
222230 updatePendingBroadcastLocked ();
223231 }
224232 }
@@ -264,6 +272,7 @@ public void onGoToSleepFinished() {
264272 synchronized (mLock ) {
265273 if (mActualPowerState != POWER_STATE_ASLEEP ) {
266274 mActualPowerState = POWER_STATE_ASLEEP ;
275+ mPendingGoToSleepBroadcast = true ;
267276 if (mUserActivityPending ) {
268277 mUserActivityPending = false ;
269278 mHandler .removeMessages (MSG_USER_ACTIVITY );
@@ -300,7 +309,8 @@ public void onUserActivity(int event, int uid) {
300309 private void updatePendingBroadcastLocked () {
301310 if (!mBroadcastInProgress
302311 && mActualPowerState != POWER_STATE_UNKNOWN
303- && mActualPowerState != mBroadcastedPowerState ) {
312+ && (mPendingWakeUpBroadcast || mPendingGoToSleepBroadcast
313+ || mActualPowerState != mBroadcastedPowerState )) {
304314 mBroadcastInProgress = true ;
305315 mSuspendBlocker .acquire ();
306316 Message msg = mHandler .obtainMessage (MSG_BROADCAST );
@@ -309,6 +319,11 @@ private void updatePendingBroadcastLocked() {
309319 }
310320 }
311321
322+ private void finishPendingBroadcastLocked () {
323+ mBroadcastInProgress = false ;
324+ mSuspendBlocker .release ();
325+ }
326+
312327 private void sendUserActivity () {
313328 synchronized (mLock ) {
314329 if (!mUserActivityPending ) {
@@ -324,18 +339,35 @@ private void sendNextBroadcast() {
324339 final int powerState ;
325340 final int goToSleepReason ;
326341 synchronized (mLock ) {
327- if (mActualPowerState == POWER_STATE_UNKNOWN
328- || mActualPowerState == mBroadcastedPowerState ) {
329- mBroadcastInProgress = false ;
330- mSuspendBlocker .release ();
331- return ;
342+ if (mBroadcastedPowerState == POWER_STATE_UNKNOWN ) {
343+ // Broadcasted power state is unknown. Send wake up.
344+ mPendingWakeUpBroadcast = false ;
345+ mBroadcastedPowerState = POWER_STATE_AWAKE ;
346+ } else if (mBroadcastedPowerState == POWER_STATE_AWAKE ) {
347+ // Broadcasted power state is awake. Send asleep if needed.
348+ if (mPendingWakeUpBroadcast || mPendingGoToSleepBroadcast
349+ || mActualPowerState == POWER_STATE_ASLEEP ) {
350+ mPendingGoToSleepBroadcast = false ;
351+ mBroadcastedPowerState = POWER_STATE_ASLEEP ;
352+ } else {
353+ finishPendingBroadcastLocked ();
354+ return ;
355+ }
356+ } else {
357+ // Broadcasted power state is asleep. Send awake if needed.
358+ if (mPendingWakeUpBroadcast || mPendingGoToSleepBroadcast
359+ || mActualPowerState == POWER_STATE_AWAKE ) {
360+ mPendingWakeUpBroadcast = false ;
361+ mBroadcastedPowerState = POWER_STATE_AWAKE ;
362+ } else {
363+ finishPendingBroadcastLocked ();
364+ return ;
365+ }
332366 }
333367
334- powerState = mActualPowerState ;
335- goToSleepReason = mLastGoToSleepReason ;
336-
337- mBroadcastedPowerState = powerState ;
338368 mBroadcastStartTime = SystemClock .uptimeMillis ();
369+ powerState = mBroadcastedPowerState ;
370+ goToSleepReason = mLastGoToSleepReason ;
339371 }
340372
341373 EventLog .writeEvent (EventLogTags .POWER_SCREEN_BROADCAST_SEND , 1 );
0 commit comments