Don't poke user activity when dream ends

The dream may end for various reasons, including some that do not
represent user activity, such as app crashes or the app being updated.
In these cases, the user activity should not be poked, as this would
reset the inattentive sleep timer.

Bug: 215559033
Test: atest PowerManagerServiceTest
Test: Kill dream app, observe `dumpsys power` lastUserActivityTime
Test: Kill dream app, verify inattentive sleep timer not affected
Change-Id: If9b61cab3d5e21b89f3c0ab7fe541d46bc865f97
Merged-In: If9b61cab3d5e21b89f3c0ab7fe541d46bc865f97
This commit is contained in:
Robert Horvath
2022-03-09 11:19:05 +01:00
parent e0934dadbe
commit 3c7fa15aaa
3 changed files with 36 additions and 3 deletions

View File

@@ -566,7 +566,8 @@ public final class PowerManager {
WAKE_REASON_HDMI,
WAKE_REASON_DISPLAY_GROUP_ADDED,
WAKE_REASON_DISPLAY_GROUP_TURNED_ON,
WAKE_REASON_UNFOLD_DEVICE
WAKE_REASON_UNFOLD_DEVICE,
WAKE_REASON_DREAM_FINISHED
})
@Retention(RetentionPolicy.SOURCE)
public @interface WakeReason{}
@@ -672,6 +673,12 @@ public final class PowerManager {
*/
public static final int WAKE_REASON_UNFOLD_DEVICE = 12;
/**
* Wake up reason code: Waking the device due to the dream finishing.
* @hide
*/
public static final int WAKE_REASON_DREAM_FINISHED = 13;
/**
* Convert the wake reason to a string for debugging purposes.
* @hide
@@ -691,6 +698,7 @@ public final class PowerManager {
case WAKE_REASON_DISPLAY_GROUP_ADDED: return "WAKE_REASON_DISPLAY_GROUP_ADDED";
case WAKE_REASON_DISPLAY_GROUP_TURNED_ON: return "WAKE_REASON_DISPLAY_GROUP_TURNED_ON";
case WAKE_REASON_UNFOLD_DEVICE: return "WAKE_REASON_UNFOLD_DEVICE";
case WAKE_REASON_DREAM_FINISHED: return "WAKE_REASON_DREAM_FINISHED";
default: return Integer.toString(wakeReason);
}
}

View File

@@ -667,8 +667,12 @@ public final class PowerManagerService extends SystemService
int reason, int uid, int opUid, String opPackageName, String details) {
if (wakefulness == WAKEFULNESS_AWAKE) {
// Kick user activity to prevent newly awake group from timing out instantly.
// The dream may end without user activity if the dream app crashes / is updated,
// don't poke the user activity timer for these wakes.
int flags = reason == PowerManager.WAKE_REASON_DREAM_FINISHED
? PowerManager.USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS : 0;
userActivityNoUpdateLocked(mPowerGroups.get(groupId), eventTime,
PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, uid);
PowerManager.USER_ACTIVITY_EVENT_OTHER, flags, uid);
}
mDirty |= DIRTY_DISPLAY_GROUP_WAKEFULNESS;
updateGlobalWakefulnessLocked(eventTime, reason, uid, opUid, opPackageName, details);
@@ -3211,7 +3215,7 @@ public final class PowerManagerService extends SystemService
}
} else {
wakePowerGroupLocked(powerGroup, now,
PowerManager.WAKE_REASON_UNKNOWN,
PowerManager.WAKE_REASON_DREAM_FINISHED,
"android.server.power:DREAM_FINISHED", Process.SYSTEM_UID,
mContext.getOpPackageName(), Process.SYSTEM_UID);
}

View File

@@ -886,6 +886,27 @@ public class PowerManagerServiceTest {
PowerManager.GO_TO_SLEEP_REASON_INATTENTIVE);
}
@Test
public void testInattentiveSleep_dreamEnds_goesToSleepAfterTimeout() {
setMinimumScreenOffTimeoutConfig(5);
setAttentiveTimeout(30000);
createService();
startSystem();
advanceTime(10000);
forceDream();
advanceTime(10000);
final String pkg = mContextSpy.getOpPackageName();
mService.getBinderServiceInstance().wakeUp(mClock.now(),
PowerManager.WAKE_REASON_DREAM_FINISHED, "PowerManagerServiceTest:DREAM_FINISHED",
pkg);
advanceTime(10001);
assertThat(mService.getGlobalWakefulnessLocked()).isEqualTo(WAKEFULNESS_ASLEEP);
assertThat(mService.getBinderServiceInstance().getLastSleepReason()).isEqualTo(
PowerManager.GO_TO_SLEEP_REASON_INATTENTIVE);
}
@Test
public void testScreenOffTimeout_goesToSleepAfterTimeout() {
final DisplayInfo info = new DisplayInfo();