Skip to content

Commit 8c65ee2

Browse files
Joe OnoratoAndroid (Google) Code Review
authored andcommitted
Merge "Add a method to let a properly permissioned app directly manipulate the user activity timeout. We should come up with a better API for this, but this is for a last minute power manager hack to turn off the screen sooner after a phone call ends." into froyo
2 parents 0ac8ac3 + 7999bff commit 8c65ee2

File tree

2 files changed

+77
-30
lines changed

2 files changed

+77
-30
lines changed

core/java/android/os/IPowerManager.aidl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ interface IPowerManager
2626
void releaseWakeLock(IBinder lock, int flags);
2727
void userActivity(long when, boolean noChangeLights);
2828
void userActivityWithForce(long when, boolean noChangeLights, boolean force);
29+
void clearUserActivityTimeout(long now, long timeout);
2930
void setPokeLock(int pokey, IBinder lock, String tag);
3031
int getSupportedWakeLockFlags();
3132
void setStayOnSetting(int val);

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

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,36 +1022,67 @@ public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
10221022
}
10231023
}
10241024

1025-
private void setTimeoutLocked(long now, int nextState)
1026-
{
1025+
private void setTimeoutLocked(long now, int nextState) {
1026+
setTimeoutLocked(now, -1, nextState);
1027+
}
1028+
1029+
// If they gave a timeoutOverride it is the number of seconds
1030+
// to screen-off. Figure out where in the countdown cycle we
1031+
// should jump to.
1032+
private void setTimeoutLocked(long now, long timeoutOverride, int nextState) {
10271033
if (mBootCompleted) {
1028-
mHandler.removeCallbacks(mTimeoutTask);
1029-
mTimeoutTask.nextState = nextState;
1030-
long when = now;
1031-
switch (nextState)
1032-
{
1033-
case SCREEN_BRIGHT:
1034-
when += mKeylightDelay;
1035-
break;
1036-
case SCREEN_DIM:
1037-
if (mDimDelay >= 0) {
1038-
when += mDimDelay;
1039-
break;
1040-
} else {
1041-
Slog.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim");
1034+
synchronized (mLocks) {
1035+
mHandler.removeCallbacks(mTimeoutTask);
1036+
mTimeoutTask.nextState = nextState;
1037+
long when = 0;
1038+
if (timeoutOverride <= 0) {
1039+
switch (nextState)
1040+
{
1041+
case SCREEN_BRIGHT:
1042+
when = now + mKeylightDelay;
1043+
break;
1044+
case SCREEN_DIM:
1045+
if (mDimDelay >= 0) {
1046+
when = now + mDimDelay;
1047+
} else {
1048+
Slog.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim");
1049+
}
1050+
case SCREEN_OFF:
1051+
synchronized (mLocks) {
1052+
when = now + mScreenOffDelay;
1053+
}
1054+
break;
10421055
}
1043-
case SCREEN_OFF:
1044-
synchronized (mLocks) {
1045-
when += mScreenOffDelay;
1056+
} else {
1057+
override: {
1058+
if (timeoutOverride <= mScreenOffDelay) {
1059+
when = now + timeoutOverride;
1060+
nextState = SCREEN_OFF;
1061+
break override;
1062+
}
1063+
timeoutOverride -= mScreenOffDelay;
1064+
1065+
if (mDimDelay >= 0) {
1066+
if (timeoutOverride <= mDimDelay) {
1067+
when = now + timeoutOverride;
1068+
nextState = SCREEN_DIM;
1069+
break override;
1070+
}
1071+
timeoutOverride -= mDimDelay;
1072+
}
1073+
1074+
when = now + timeoutOverride;
1075+
nextState = SCREEN_BRIGHT;
10461076
}
1047-
break;
1048-
}
1049-
if (mSpew) {
1050-
Slog.d(TAG, "setTimeoutLocked now=" + now + " nextState=" + nextState
1051-
+ " when=" + when);
1077+
}
1078+
if (mSpew) {
1079+
Slog.d(TAG, "setTimeoutLocked now=" + now
1080+
+ " timeoutOverride=" + timeoutOverride
1081+
+ " nextState=" + nextState + " when=" + when);
1082+
}
1083+
mHandler.postAtTime(mTimeoutTask, when);
1084+
mNextTimeout = when; // for debugging
10521085
}
1053-
mHandler.postAtTime(mTimeoutTask, when);
1054-
mNextTimeout = when; // for debugging
10551086
}
10561087
}
10571088

@@ -1958,18 +1989,33 @@ private void forceUserActivityLocked() {
19581989

19591990
public void userActivityWithForce(long time, boolean noChangeLights, boolean force) {
19601991
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1961-
userActivity(time, noChangeLights, OTHER_EVENT, force);
1992+
userActivity(time, -1, noChangeLights, OTHER_EVENT, force);
19621993
}
19631994

19641995
public void userActivity(long time, boolean noChangeLights) {
1965-
userActivity(time, noChangeLights, OTHER_EVENT, false);
1996+
userActivity(time, -1, noChangeLights, OTHER_EVENT, false);
19661997
}
19671998

19681999
public void userActivity(long time, boolean noChangeLights, int eventType) {
1969-
userActivity(time, noChangeLights, eventType, false);
2000+
userActivity(time, -1, noChangeLights, eventType, false);
19702001
}
19712002

19722003
public void userActivity(long time, boolean noChangeLights, int eventType, boolean force) {
2004+
userActivity(time, -1, noChangeLights, eventType, force);
2005+
}
2006+
2007+
/*
2008+
* Reset the user activity timeout to now + timeout. This overrides whatever else is going
2009+
* on with user activity. Don't use this function.
2010+
*/
2011+
public void clearUserActivityTimeout(long now, long timeout) {
2012+
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2013+
Slog.i(TAG, "clearUserActivity for " + timeout + "ms from now");
2014+
userActivity(now, timeout, false, OTHER_EVENT, false);
2015+
}
2016+
2017+
private void userActivity(long time, long timeoutOverride, boolean noChangeLights,
2018+
int eventType, boolean force) {
19732019
//mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
19742020

19752021
if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)
@@ -2041,7 +2087,7 @@ public void userActivity(long time, boolean noChangeLights, int eventType, boole
20412087
mWakeLockState = mLocks.reactivateScreenLocksLocked();
20422088
setPowerState(mUserState | mWakeLockState, noChangeLights,
20432089
WindowManagerPolicy.OFF_BECAUSE_OF_USER);
2044-
setTimeoutLocked(time, SCREEN_BRIGHT);
2090+
setTimeoutLocked(time, timeoutOverride, SCREEN_BRIGHT);
20452091
}
20462092
}
20472093
}

0 commit comments

Comments
 (0)