Skip to content

Commit 17f36d1

Browse files
Jeff BrownAndroid (Google) Code Review
authored andcommitted
Merge "Allow phone to go to sleep while in call." into jb-mr1-dev
2 parents 82245c1 + 93cbbb2 commit 17f36d1

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

core/java/android/os/PowerManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ public final class PowerManager {
178178
/**
179179
* Wake lock level: Turns the screen off when the proximity sensor activates.
180180
* <p>
181+
* If the proximity sensor detects that an object is nearby, the screen turns off
182+
* immediately. Shortly after the object moves away, the screen turns on again.
183+
* </p><p>
184+
* A proximity wake lock does not prevent the device from falling asleep
185+
* unlike {@link #FULL_WAKE_LOCK}, {@link #SCREEN_BRIGHT_WAKE_LOCK} and
186+
* {@link #SCREEN_DIM_WAKE_LOCK}. If there is no user activity and no other
187+
* wake locks are held, then the device will fall asleep (and lock) as usual.
188+
* However, the device will not fall asleep while the screen has been turned off
189+
* by the proximity sensor because it effectively counts as ongoing user activity.
190+
* </p><p>
181191
* Since not all devices have proximity sensors, use {@link #isWakeLockLevelSupported}
182192
* to determine whether this wake lock level is supported.
183193
* </p>

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ final class DisplayPowerController {
117117
private static final int PROXIMITY_NEGATIVE = 0;
118118
private static final int PROXIMITY_POSITIVE = 1;
119119

120-
// Proximity sensor debounce delay in milliseconds.
121-
private static final int PROXIMITY_SENSOR_DEBOUNCE_DELAY = 250;
120+
// Proximity sensor debounce delay in milliseconds for positive or negative transitions.
121+
private static final int PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY = 0;
122+
private static final int PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY = 500;
122123

123124
// Trigger proximity if distance is less than 5 cm.
124125
private static final float TYPICAL_PROXIMITY_THRESHOLD = 5.0f;
@@ -562,6 +563,7 @@ private void updatePowerState() {
562563
if (!mScreenOffBecauseOfProximity
563564
&& mProximity == PROXIMITY_POSITIVE) {
564565
mScreenOffBecauseOfProximity = true;
566+
sendOnProximityPositive();
565567
setScreenOn(false);
566568
}
567569
} else if (mWaitingForNegativeProximity
@@ -734,8 +736,13 @@ private void handleProximitySensorEvent(long time, boolean positive) {
734736
// Only accept a proximity sensor reading if it remains
735737
// stable for the entire debounce delay.
736738
mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED);
737-
mPendingProximity = positive ? PROXIMITY_POSITIVE : PROXIMITY_NEGATIVE;
738-
mPendingProximityDebounceTime = time + PROXIMITY_SENSOR_DEBOUNCE_DELAY;
739+
if (positive) {
740+
mPendingProximity = PROXIMITY_POSITIVE;
741+
mPendingProximityDebounceTime = time + PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY;
742+
} else {
743+
mPendingProximity = PROXIMITY_NEGATIVE;
744+
mPendingProximityDebounceTime = time + PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY;
745+
}
739746
debounceProximitySensor();
740747
}
741748

@@ -973,6 +980,17 @@ public void run() {
973980
}
974981
};
975982

983+
private void sendOnProximityPositive() {
984+
mCallbackHandler.post(mOnProximityPositiveRunnable);
985+
}
986+
987+
private final Runnable mOnProximityPositiveRunnable = new Runnable() {
988+
@Override
989+
public void run() {
990+
mCallbacks.onProximityPositive();
991+
}
992+
};
993+
976994
private void sendOnProximityNegative() {
977995
mCallbackHandler.post(mOnProximityNegativeRunnable);
978996
}
@@ -1090,6 +1108,7 @@ private static boolean wantScreenOn(int state) {
10901108
*/
10911109
public interface Callbacks {
10921110
void onStateChanged();
1111+
void onProximityPositive();
10931112
void onProximityNegative();
10941113
}
10951114

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public final class PowerManagerService extends IPowerManager.Stub
9898
private static final int DIRTY_STAY_ON = 1 << 7;
9999
// Dirty bit: battery state changed
100100
private static final int DIRTY_BATTERY_STATE = 1 << 8;
101+
// Dirty bit: proximity state changed
102+
private static final int DIRTY_PROXIMITY_POSITIVE = 1 << 9;
101103

102104
// Wakefulness: The device is asleep and can only be awoken by a call to wakeUp().
103105
// The screen should be off or in the process of being turned off by the display controller.
@@ -258,6 +260,9 @@ public final class PowerManagerService extends IPowerManager.Stub
258260
// True if the device should stay on.
259261
private boolean mStayOn;
260262

263+
// True if the proximity sensor reads a positive result.
264+
private boolean mProximityPositive;
265+
261266
// Screen brightness setting limits.
262267
private int mScreenBrightnessSettingMinimum;
263268
private int mScreenBrightnessSettingMaximum;
@@ -1101,12 +1106,17 @@ private boolean shouldWakeUpWhenPluggedOrUnpluggedLocked(boolean wasPowered, int
11011106
*/
11021107
private void updateStayOnLocked(int dirty) {
11031108
if ((dirty & (DIRTY_BATTERY_STATE | DIRTY_SETTINGS)) != 0) {
1109+
final boolean wasStayOn = mStayOn;
11041110
if (mStayOnWhilePluggedInSetting != 0
11051111
&& !isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) {
11061112
mStayOn = mBatteryService.isPowered(mStayOnWhilePluggedInSetting);
11071113
} else {
11081114
mStayOn = false;
11091115
}
1116+
1117+
if (mStayOn != wasStayOn) {
1118+
mDirty |= DIRTY_STAY_ON;
1119+
}
11101120
}
11111121
}
11121122

@@ -1265,7 +1275,7 @@ private int getScreenDimDurationLocked(int screenOffTimeout) {
12651275
private boolean updateWakefulnessLocked(int dirty) {
12661276
boolean changed = false;
12671277
if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_BOOT_COMPLETED
1268-
| DIRTY_WAKEFULNESS | DIRTY_STAY_ON)) != 0) {
1278+
| DIRTY_WAKEFULNESS | DIRTY_STAY_ON | DIRTY_PROXIMITY_POSITIVE)) != 0) {
12691279
if (mWakefulness == WAKEFULNESS_AWAKE && isItBedTimeYetLocked()) {
12701280
if (DEBUG_SPEW) {
12711281
Slog.d(TAG, "updateWakefulnessLocked: Bed time...");
@@ -1288,17 +1298,17 @@ private boolean updateWakefulnessLocked(int dirty) {
12881298
* to being fully awake or else go to sleep for good.
12891299
*/
12901300
private boolean isItBedTimeYetLocked() {
1291-
return mBootCompleted && !isScreenBeingKeptOnLocked();
1301+
return mBootCompleted && !isBeingKeptAwakeLocked();
12921302
}
12931303

12941304
/**
1295-
* Returns true if the screen is being kept on by a wake lock, user activity
1305+
* Returns true if the device is being kept awake by a wake lock, user activity
12961306
* or the stay on while powered setting.
12971307
*/
1298-
private boolean isScreenBeingKeptOnLocked() {
1308+
private boolean isBeingKeptAwakeLocked() {
12991309
return mStayOn
1300-
|| (mWakeLockSummary & (WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_SCREEN_DIM
1301-
| WAKE_LOCK_PROXIMITY_SCREEN_OFF)) != 0
1310+
|| mProximityPositive
1311+
|| (mWakeLockSummary & (WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_SCREEN_DIM)) != 0
13021312
|| (mUserActivitySummary & (USER_ACTIVITY_SCREEN_BRIGHT
13031313
| USER_ACTIVITY_SCREEN_DIM)) != 0;
13041314
}
@@ -1314,6 +1324,7 @@ private void updateDreamLocked(int dirty) {
13141324
| DIRTY_SETTINGS
13151325
| DIRTY_IS_POWERED
13161326
| DIRTY_STAY_ON
1327+
| DIRTY_PROXIMITY_POSITIVE
13171328
| DIRTY_BATTERY_STATE)) != 0) {
13181329
scheduleSandmanLocked();
13191330
}
@@ -1401,7 +1412,7 @@ private boolean canDreamLocked() {
14011412
&& mDreamsEnabledSetting
14021413
&& mDisplayPowerRequest.screenState != DisplayPowerRequest.SCREEN_STATE_OFF
14031414
&& mBootCompleted
1404-
&& (mIsPowered || isScreenBeingKeptOnLocked());
1415+
&& (mIsPowered || isBeingKeptAwakeLocked());
14051416
}
14061417

14071418
/**
@@ -1527,8 +1538,17 @@ public void onStateChanged() {
15271538
updatePowerStateLocked();
15281539
}
15291540

1541+
@Override
1542+
public void onProximityPositive() {
1543+
mProximityPositive = true;
1544+
mDirty |= DIRTY_PROXIMITY_POSITIVE;
1545+
updatePowerStateLocked();
1546+
}
1547+
15301548
@Override
15311549
public void onProximityNegative() {
1550+
mProximityPositive = false;
1551+
mDirty |= DIRTY_PROXIMITY_POSITIVE;
15321552
userActivityNoUpdateLocked(SystemClock.uptimeMillis(),
15331553
PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID);
15341554
updatePowerStateLocked();
@@ -1986,6 +2006,7 @@ protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
19862006
pw.println(" mIsPowered=" + mIsPowered);
19872007
pw.println(" mPlugType=" + mPlugType);
19882008
pw.println(" mStayOn=" + mStayOn);
2009+
pw.println(" mProximityPositive=" + mProximityPositive);
19892010
pw.println(" mBootCompleted=" + mBootCompleted);
19902011
pw.println(" mSystemReady=" + mSystemReady);
19912012
pw.println(" mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary));

0 commit comments

Comments
 (0)