@@ -162,6 +162,11 @@ public final class PowerManagerService extends IPowerManager.Stub
162162 // Poll interval in milliseconds for watching boot animation finished.
163163 private static final int BOOT_ANIMATION_POLL_INTERVAL = 200 ;
164164
165+ // If the battery level drops by this percentage and the user activity timeout
166+ // has expired, then assume the device is receiving insufficient current to charge
167+ // effectively and terminate the dream.
168+ private static final int DREAM_BATTERY_LEVEL_DRAIN_CUTOFF = 5 ;
169+
165170 private Context mContext ;
166171 private LightsService mLightsService ;
167172 private BatteryService mBatteryService ;
@@ -256,6 +261,14 @@ public final class PowerManagerService extends IPowerManager.Stub
256261 // The current plug type, such as BatteryManager.BATTERY_PLUGGED_WIRELESS.
257262 private int mPlugType ;
258263
264+ // The current battery level percentage.
265+ private int mBatteryLevel ;
266+
267+ // The battery level percentage at the time the dream started.
268+ // This is used to terminate a dream and go to sleep if the battery is
269+ // draining faster than it is charging and the user activity timeout has expired.
270+ private int mBatteryLevelWhenDreamStarted ;
271+
259272 // True if the device should wake up when plugged or unplugged.
260273 private boolean mWakeUpWhenPluggedOrUnpluggedConfig ;
261274
@@ -1067,12 +1080,14 @@ private void updateIsPoweredLocked(int dirty) {
10671080 final int oldPlugType = mPlugType ;
10681081 mIsPowered = mBatteryService .isPowered (BatteryManager .BATTERY_PLUGGED_ANY );
10691082 mPlugType = mBatteryService .getPlugType ();
1083+ mBatteryLevel = mBatteryService .getBatteryLevel ();
10701084
10711085 if (DEBUG ) {
10721086 Slog .d (TAG , "updateIsPoweredLocked: wasPowered=" + wasPowered
10731087 + ", mIsPowered=" + mIsPowered
10741088 + ", oldPlugType=" + oldPlugType
1075- + ", mPlugType=" + mPlugType );
1089+ + ", mPlugType=" + mPlugType
1090+ + ", mBatteryLevel=" + mBatteryLevel );
10761091 }
10771092
10781093 if (wasPowered != mIsPowered || oldPlugType != mPlugType ) {
@@ -1126,8 +1141,7 @@ private boolean shouldWakeUpWhenPluggedOrUnpluggedLocked(boolean wasPowered, int
11261141 }
11271142 if (!wasPowered && mIsPowered
11281143 && mPlugType == BatteryManager .BATTERY_PLUGGED_WIRELESS
1129- && mBatteryService .getBatteryLevel () >=
1130- WIRELESS_CHARGER_TURN_ON_BATTERY_LEVEL_LIMIT ) {
1144+ && mBatteryLevel >= WIRELESS_CHARGER_TURN_ON_BATTERY_LEVEL_LIMIT ) {
11311145 return false ;
11321146 }
11331147
@@ -1403,7 +1417,7 @@ private void handleSandman() { // runs on handler thread
14031417 mSandmanScheduled = false ;
14041418 boolean canDream = canDreamLocked ();
14051419 if (DEBUG_SPEW ) {
1406- Log .d (TAG , "handleSandman: canDream=" + canDream
1420+ Slog .d (TAG , "handleSandman: canDream=" + canDream
14071421 + ", mWakefulness=" + wakefulnessToString (mWakefulness ));
14081422 }
14091423
@@ -1431,10 +1445,24 @@ private void handleSandman() { // runs on handler thread
14311445 if (mWakefulness == WAKEFULNESS_NAPPING ) {
14321446 mWakefulness = WAKEFULNESS_DREAMING ;
14331447 mDirty |= DIRTY_WAKEFULNESS ;
1448+ mBatteryLevelWhenDreamStarted = mBatteryLevel ;
14341449 updatePowerStateLocked ();
14351450 continueDreaming = true ;
14361451 } else if (mWakefulness == WAKEFULNESS_DREAMING ) {
1437- continueDreaming = true ;
1452+ if (!isBeingKeptAwakeLocked ()
1453+ && mBatteryLevel < mBatteryLevelWhenDreamStarted
1454+ - DREAM_BATTERY_LEVEL_DRAIN_CUTOFF ) {
1455+ // If the user activity timeout expired and the battery appears
1456+ // to be draining faster than it is charging then stop dreaming
1457+ // and go to sleep.
1458+ Slog .i (TAG , "Stopping dream because the battery appears to "
1459+ + "be draining faster than it is charging. "
1460+ + "Battery level when dream started: "
1461+ + mBatteryLevelWhenDreamStarted + "%. "
1462+ + "Battery level now: " + mBatteryLevel + "%." );
1463+ } else {
1464+ continueDreaming = true ;
1465+ }
14381466 }
14391467 }
14401468 if (!continueDreaming ) {
@@ -2094,6 +2122,8 @@ protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
20942122 pw .println (" mWakefulness=" + wakefulnessToString (mWakefulness ));
20952123 pw .println (" mIsPowered=" + mIsPowered );
20962124 pw .println (" mPlugType=" + mPlugType );
2125+ pw .println (" mBatteryLevel=" + mBatteryLevel );
2126+ pw .println (" mBatteryLevelWhenDreamStarted=" + mBatteryLevelWhenDreamStarted );
20972127 pw .println (" mStayOn=" + mStayOn );
20982128 pw .println (" mProximityPositive=" + mProximityPositive );
20992129 pw .println (" mBootCompleted=" + mBootCompleted );
0 commit comments