Skip to content

Commit 9fca9e9

Browse files
author
Jeff Brown
committed
Ensure that dreams show while docked.
Fixed a race between the UiModeManagerService and PowerManagerService both of which are trying to wake the device when docked / powered. Bug: 7281240 Change-Id: Ia41fef48f17f2a2eb56549437d295f9a86c95af2
1 parent c38c9be commit 9fca9e9

File tree

2 files changed

+60
-37
lines changed

2 files changed

+60
-37
lines changed

services/java/com/android/server/UiModeManagerService.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import android.os.PowerManager;
3838
import android.os.RemoteException;
3939
import android.os.ServiceManager;
40+
import android.os.SystemClock;
4041
import android.os.UserHandle;
4142
import android.provider.Settings;
4243
import android.service.dreams.DreamService;
@@ -90,6 +91,8 @@ class UiModeManagerService extends IUiModeManager.Stub {
9091
private NotificationManager mNotificationManager;
9192

9293
private StatusBarManager mStatusBarManager;
94+
95+
private final PowerManager mPowerManager;
9396
private final PowerManager.WakeLock mWakeLock;
9497

9598
static Intent buildHomeIntent(String category) {
@@ -163,8 +166,8 @@ public UiModeManagerService(Context context, TwilightService twilight) {
163166
mContext.registerReceiver(mBatteryReceiver,
164167
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
165168

166-
PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
167-
mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
169+
mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
170+
mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
168171

169172
mConfiguration.setToDefaults();
170173

@@ -502,7 +505,17 @@ && isScreenSaverEnabled() && isScreenSaverActivatedOnDock()) {
502505
try {
503506
IDreamManager dreamManagerService = IDreamManager.Stub.asInterface(
504507
ServiceManager.getService(DreamService.DREAM_SERVICE));
505-
dreamManagerService.dream();
508+
if (dreamManagerService != null && !dreamManagerService.isDreaming()) {
509+
// Wake up.
510+
// The power manager will wake up the system when it starts receiving power
511+
// but there is a race between that happening and the UI mode manager
512+
// starting a dream. We want the system to already be awake
513+
// by the time this happens. Otherwise the dream may not start.
514+
mPowerManager.wakeUp(SystemClock.uptimeMillis());
515+
516+
// Dream.
517+
dreamManagerService.dream();
518+
}
506519
} catch (RemoteException ex) {
507520
Slog.e(TAG, "Could not start dream when docked.", ex);
508521
}

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

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,41 +1078,51 @@ private void updateIsPoweredLocked(int dirty) {
10781078
}
10791079

10801080
private boolean shouldWakeUpWhenPluggedOrUnpluggedLocked(boolean wasPowered, int oldPlugType) {
1081-
if (mWakeUpWhenPluggedOrUnpluggedConfig) {
1082-
// FIXME: Need more accurate detection of wireless chargers.
1083-
//
1084-
// We are unable to accurately detect whether the device is resting on the
1085-
// charger unless it is actually receiving power. This causes us some grief
1086-
// because the device might not appear to be plugged into the wireless charger
1087-
// unless it actually charging.
1088-
//
1089-
// To avoid spuriously waking the screen, we apply a special policy to
1090-
// wireless chargers.
1091-
//
1092-
// 1. Don't wake the device when unplugged from wireless charger because
1093-
// it might be that the device is still resting on the wireless charger
1094-
// but is not receiving power anymore because the battery is full.
1095-
//
1096-
// 2. Don't wake the device when plugged into a wireless charger if the
1097-
// battery already appears to be mostly full. This situation may indicate
1098-
// that the device was resting on the charger the whole time and simply
1099-
// wasn't receiving power because the battery was full. We can't tell
1100-
// whether the device was just placed on the charger or whether it has
1101-
// been there for half of the night slowly discharging until it hit
1102-
// the point where it needed to start charging again.
1103-
if (wasPowered && !mIsPowered
1104-
&& oldPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
1105-
return false;
1106-
}
1107-
if (!wasPowered && mIsPowered
1108-
&& mPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS
1109-
&& mBatteryService.getBatteryLevel() >=
1110-
WIRELESS_CHARGER_TURN_ON_BATTERY_LEVEL_LIMIT) {
1111-
return false;
1112-
}
1113-
return true;
1081+
// Don't wake when powered unless configured to do so.
1082+
if (!mWakeUpWhenPluggedOrUnpluggedConfig) {
1083+
return false;
11141084
}
1115-
return false;
1085+
1086+
// FIXME: Need more accurate detection of wireless chargers.
1087+
//
1088+
// We are unable to accurately detect whether the device is resting on the
1089+
// charger unless it is actually receiving power. This causes us some grief
1090+
// because the device might not appear to be plugged into the wireless charger
1091+
// unless it actually charging.
1092+
//
1093+
// To avoid spuriously waking the screen, we apply a special policy to
1094+
// wireless chargers.
1095+
//
1096+
// 1. Don't wake the device when unplugged from wireless charger because
1097+
// it might be that the device is still resting on the wireless charger
1098+
// but is not receiving power anymore because the battery is full.
1099+
//
1100+
// 2. Don't wake the device when plugged into a wireless charger if the
1101+
// battery already appears to be mostly full. This situation may indicate
1102+
// that the device was resting on the charger the whole time and simply
1103+
// wasn't receiving power because the battery was full. We can't tell
1104+
// whether the device was just placed on the charger or whether it has
1105+
// been there for half of the night slowly discharging until it hit
1106+
// the point where it needed to start charging again.
1107+
if (wasPowered && !mIsPowered
1108+
&& oldPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
1109+
return false;
1110+
}
1111+
if (!wasPowered && mIsPowered
1112+
&& mPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS
1113+
&& mBatteryService.getBatteryLevel() >=
1114+
WIRELESS_CHARGER_TURN_ON_BATTERY_LEVEL_LIMIT) {
1115+
return false;
1116+
}
1117+
1118+
// If already dreaming and becoming powered, then don't wake.
1119+
if (mIsPowered && (mWakefulness == WAKEFULNESS_NAPPING
1120+
|| mWakefulness == WAKEFULNESS_DREAMING)) {
1121+
return false;
1122+
}
1123+
1124+
// Otherwise wake up!
1125+
return true;
11161126
}
11171127

11181128
/**

0 commit comments

Comments
 (0)