Skip to content

Commit fd04143

Browse files
Imre SunyiJohan Redestig
authored andcommitted
Shutdown when capacity is 0% and no charging or when battery is dead
Android framework does not shutdown when battery capacity is 0% and a charger is attached (USB or AC). This handling is incomplete since a charger might very well be attached but charging has stopped because USB suspended or the charging algorithm has stopped because of battery safety handling. Also shutdown when battery is reported 'dead'. This might happen although charging is present. Change-Id: If328260ebf4d38f912e4d2fad204431cbb19c993
1 parent f3ee6f8 commit fd04143

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

services/java/com/android/server/BatteryService.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,13 @@ public BatteryService(Context context) {
136136

137137
final boolean isPowered() {
138138
// assume we are powered if battery state is unknown so the "stay on while plugged in" option will work.
139-
return (mAcOnline || mUsbOnline || mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN);
139+
// Do not look at the plug status to check if we are powered.
140+
// Charger can be plugged but not charging because of i.e. USB suspend,
141+
// battery temperature reasons etc. We are powered only if battery is
142+
// being charged. This function fill return false if charger is
143+
// connected and battery is reported full.
144+
return (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING ||
145+
mBatteryStatus == BatteryManager.BATTERY_STATUS_UNKNOWN);
140146
}
141147

142148
final boolean isPowered(int plugTypeSet) {
@@ -148,11 +154,13 @@ final boolean isPowered(int plugTypeSet) {
148154
if (plugTypeSet == 0) {
149155
return false;
150156
}
157+
158+
// we are not powered when plug is connected and not charging
151159
int plugTypeBit = 0;
152-
if (mAcOnline) {
160+
if (mAcOnline && mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
153161
plugTypeBit |= BatteryManager.BATTERY_PLUGGED_AC;
154162
}
155-
if (mUsbOnline) {
163+
if (mUsbOnline && mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
156164
plugTypeBit |= BatteryManager.BATTERY_PLUGGED_USB;
157165
}
158166
return (plugTypeSet & plugTypeBit) != 0;
@@ -183,7 +191,11 @@ void systemReady() {
183191
private final void shutdownIfNoPower() {
184192
// shut down gracefully if our battery is critically low and we are not powered.
185193
// wait until the system has booted before attempting to display the shutdown dialog.
186-
if (mBatteryLevel == 0 && !isPowered() && ActivityManagerNative.isSystemReady()) {
194+
// Also shutdown if battery is reported to be 'dead' independent of
195+
// battery level and power supply.
196+
if (((mBatteryLevel == 0 && !isPowered()) ||
197+
mBatteryHealth == BatteryManager.BATTERY_HEALTH_DEAD) &&
198+
ActivityManagerNative.isSystemReady()) {
187199
Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
188200
intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
189201
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

0 commit comments

Comments
 (0)