Skip to content

Commit 29126cf

Browse files
Jeff BrownAndroid (Google) Code Review
authored andcommitted
Merge "Ensure we send at least one wakeup/gotosleep transition." into jb-mr1-dev
2 parents f052093 + 5430835 commit 29126cf

File tree

2 files changed

+63
-30
lines changed

2 files changed

+63
-30
lines changed

services/java/com/android/server/power/Notifier.java

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,20 @@
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
*/
5457
final 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);

services/java/com/android/server/power/PowerManagerService.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,9 @@ private boolean wakeUpNoUpdateLocked(long eventTime) {
828828
switch (mWakefulness) {
829829
case WAKEFULNESS_ASLEEP:
830830
Slog.i(TAG, "Waking up from sleep...");
831+
sendPendingNotificationsLocked();
831832
mNotifier.onWakeUpStarted();
832833
mSendWakeUpFinishedNotificationWhenReady = true;
833-
mSendGoToSleepFinishedNotificationWhenReady = false;
834834
break;
835835
case WAKEFULNESS_DREAMING:
836836
Slog.i(TAG, "Waking up from dream...");
@@ -901,12 +901,13 @@ private boolean goToSleepNoUpdateLocked(long eventTime, int reason) {
901901
break;
902902
}
903903

904+
sendPendingNotificationsLocked();
905+
mNotifier.onGoToSleepStarted(reason);
906+
mSendGoToSleepFinishedNotificationWhenReady = true;
907+
904908
mLastSleepTime = eventTime;
905909
mDirty |= DIRTY_WAKEFULNESS;
906910
mWakefulness = WAKEFULNESS_ASLEEP;
907-
mNotifier.onGoToSleepStarted(reason);
908-
mSendGoToSleepFinishedNotificationWhenReady = true;
909-
mSendWakeUpFinishedNotificationWhenReady = false;
910911

911912
// Report the number of wake locks that will be cleared by going to sleep.
912913
int numWakeLocksCleared = 0;
@@ -1005,7 +1006,9 @@ private void updatePowerStateLocked() {
10051006
updateDisplayPowerStateLocked(dirtyPhase2);
10061007

10071008
// Phase 3: Send notifications, if needed.
1008-
sendPendingNotificationsLocked();
1009+
if (mDisplayReady) {
1010+
sendPendingNotificationsLocked();
1011+
}
10091012

10101013
// Phase 4: Update suspend blocker.
10111014
// Because we might release the last suspend blocker here, we need to make sure
@@ -1014,15 +1017,13 @@ private void updatePowerStateLocked() {
10141017
}
10151018

10161019
private void sendPendingNotificationsLocked() {
1017-
if (mDisplayReady) {
1018-
if (mSendWakeUpFinishedNotificationWhenReady) {
1019-
mSendWakeUpFinishedNotificationWhenReady = false;
1020-
mNotifier.onWakeUpFinished();
1021-
}
1022-
if (mSendGoToSleepFinishedNotificationWhenReady) {
1023-
mSendGoToSleepFinishedNotificationWhenReady = false;
1024-
mNotifier.onGoToSleepFinished();
1025-
}
1020+
if (mSendWakeUpFinishedNotificationWhenReady) {
1021+
mSendWakeUpFinishedNotificationWhenReady = false;
1022+
mNotifier.onWakeUpFinished();
1023+
}
1024+
if (mSendGoToSleepFinishedNotificationWhenReady) {
1025+
mSendGoToSleepFinishedNotificationWhenReady = false;
1026+
mNotifier.onGoToSleepFinished();
10261027
}
10271028
}
10281029

0 commit comments

Comments
 (0)