Skip to content

Commit f3fb895

Browse files
author
Jeff Brown
committed
Implement screen on hack for wireless chargers.
We can't accurately detect whether the device is resting on a wireless charger unless it is actually charging. So we need to tweak the screen on when plugged / unplugged policy accordingly to avoid spurious wakeups. Bug: 7234284 Change-Id: I624b559e2e92b8813b12090bc20eca5f5158997e
1 parent a4d8204 commit f3fb895

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,15 @@ private boolean isPoweredLocked(int plugTypeSet) {
196196
return false;
197197
}
198198

199+
/**
200+
* Returns the current plug type.
201+
*/
202+
public int getPlugType() {
203+
synchronized (mLock) {
204+
return mPlugType;
205+
}
206+
}
207+
199208
/**
200209
* Returns battery level as a percentage.
201210
*/

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

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ public final class PowerManagerService extends IPowerManager.Stub
135135
// minimum screen off timeout should be longer than this.
136136
private static final int SCREEN_DIM_DURATION = 7 * 1000;
137137

138+
// Upper bound on the battery charge percentage in order to consider turning
139+
// the screen on when the device starts charging wirelessly.
140+
// See point of use for more details.
141+
private static final int WIRELESS_CHARGER_TURN_ON_BATTERY_LEVEL_LIMIT = 95;
142+
138143
private Context mContext;
139144
private LightsService mLightsService;
140145
private BatteryService mBatteryService;
@@ -218,6 +223,9 @@ public final class PowerManagerService extends IPowerManager.Stub
218223
// True if the device is plugged into a power source.
219224
private boolean mIsPowered;
220225

226+
// The current plug type, such as BatteryManager.BATTERY_PLUGGED_WIRELESS.
227+
private int mPlugType;
228+
221229
// True if the device should wake up when plugged or unplugged.
222230
private boolean mWakeUpWhenPluggedOrUnpluggedConfig;
223231

@@ -1013,15 +1021,19 @@ private void sendPendingNotificationsLocked() {
10131021
*/
10141022
private void updateIsPoweredLocked(int dirty) {
10151023
if ((dirty & DIRTY_BATTERY_STATE) != 0) {
1016-
boolean wasPowered = mIsPowered;
1024+
final boolean wasPowered = mIsPowered;
1025+
final int oldPlugType = mPlugType;
10171026
mIsPowered = mBatteryService.isPowered(BatteryManager.BATTERY_PLUGGED_ANY);
1027+
mPlugType = mBatteryService.getPlugType();
10181028

10191029
if (DEBUG) {
10201030
Slog.d(TAG, "updateIsPoweredLocked: wasPowered=" + wasPowered
1021-
+ ", mIsPowered=" + mIsPowered);
1031+
+ ", mIsPowered=" + mIsPowered
1032+
+ ", oldPlugType=" + oldPlugType
1033+
+ ", mPlugType=" + mPlugType);
10221034
}
10231035

1024-
if (wasPowered != mIsPowered) {
1036+
if (wasPowered != mIsPowered || oldPlugType != mPlugType) {
10251037
mDirty |= DIRTY_IS_POWERED;
10261038

10271039
// Treat plugging and unplugging the devices as a user activity.
@@ -1030,7 +1042,7 @@ private void updateIsPoweredLocked(int dirty) {
10301042
// Some devices also wake the device when plugged or unplugged because
10311043
// they don't have a charging LED.
10321044
final long now = SystemClock.uptimeMillis();
1033-
if (mWakeUpWhenPluggedOrUnpluggedConfig) {
1045+
if (shouldWakeUpWhenPluggedOrUnpluggedLocked(wasPowered, oldPlugType)) {
10341046
wakeUpNoUpdateLocked(now);
10351047
}
10361048
userActivityNoUpdateLocked(
@@ -1039,6 +1051,44 @@ private void updateIsPoweredLocked(int dirty) {
10391051
}
10401052
}
10411053

1054+
private boolean shouldWakeUpWhenPluggedOrUnpluggedLocked(boolean wasPowered, int oldPlugType) {
1055+
if (mWakeUpWhenPluggedOrUnpluggedConfig) {
1056+
// FIXME: Need more accurate detection of wireless chargers.
1057+
//
1058+
// We are unable to accurately detect whether the device is resting on the
1059+
// charger unless it is actually receiving power. This causes us some grief
1060+
// because the device might not appear to be plugged into the wireless charger
1061+
// unless it actually charging.
1062+
//
1063+
// To avoid spuriously waking the screen, we apply a special policy to
1064+
// wireless chargers.
1065+
//
1066+
// 1. Don't wake the device when unplugged from wireless charger because
1067+
// it might be that the device is still resting on the wireless charger
1068+
// but is not receiving power anymore because the battery is full.
1069+
//
1070+
// 2. Don't wake the device when plugged into a wireless charger if the
1071+
// battery already appears to be mostly full. This situation may indicate
1072+
// that the device was resting on the charger the whole time and simply
1073+
// wasn't receiving power because the battery was full. We can't tell
1074+
// whether the device was just placed on the charger or whether it has
1075+
// been there for half of the night slowly discharging until it hit
1076+
// the point where it needed to start charging again.
1077+
if (wasPowered && !mIsPowered
1078+
&& oldPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
1079+
return false;
1080+
}
1081+
if (!wasPowered && mIsPowered
1082+
&& mPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS
1083+
&& mBatteryService.getBatteryLevel() >=
1084+
WIRELESS_CHARGER_TURN_ON_BATTERY_LEVEL_LIMIT) {
1085+
return false;
1086+
}
1087+
return true;
1088+
}
1089+
return false;
1090+
}
1091+
10421092
/**
10431093
* Updates the value of mStayOn.
10441094
* Sets DIRTY_STAY_ON if a change occurred.
@@ -1891,6 +1941,7 @@ protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
18911941
pw.println(" mDirty=0x" + Integer.toHexString(mDirty));
18921942
pw.println(" mWakefulness=" + wakefulnessToString(mWakefulness));
18931943
pw.println(" mIsPowered=" + mIsPowered);
1944+
pw.println(" mPlugType=" + mPlugType);
18941945
pw.println(" mStayOn=" + mStayOn);
18951946
pw.println(" mBootCompleted=" + mBootCompleted);
18961947
pw.println(" mSystemReady=" + mSystemReady);

0 commit comments

Comments
 (0)