Merge "Acquire display suspend blocker for DozeService" into qt-r1-dev

This commit is contained in:
Santos Cordon
2019-08-03 16:29:20 +00:00
committed by Android (Google) Code Review
3 changed files with 83 additions and 3 deletions

View File

@@ -161,7 +161,7 @@ public class AttentionDetector {
context.getContentResolver().registerContentObserver(Settings.System.getUriFor( context.getContentResolver().registerContentObserver(Settings.System.getUriFor(
Settings.System.ADAPTIVE_SLEEP), Settings.System.ADAPTIVE_SLEEP),
false, new ContentObserver(new Handler()) { false, new ContentObserver(new Handler(context.getMainLooper())) {
@Override @Override
public void onChange(boolean selfChange) { public void onChange(boolean selfChange) {
updateEnabledFromSettings(context); updateEnabledFromSettings(context);

View File

@@ -2724,6 +2724,14 @@ public final class PowerManagerService extends SystemService
return true; return true;
} }
} }
if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_DOZE
&& mDisplayPowerRequest.dozeScreenState == Display.STATE_ON) {
// Although we are in DOZE and would normally allow the device to suspend,
// the doze service has explicitly requested the display to remain in the ON
// state which means we should hold the display suspend blocker.
return true;
}
if (mScreenBrightnessBoostInProgress) { if (mScreenBrightnessBoostInProgress) {
return true; return true;
} }
@@ -4858,7 +4866,8 @@ public final class PowerManagerService extends SystemService
} }
} }
private final class LocalService extends PowerManagerInternal { @VisibleForTesting
final class LocalService extends PowerManagerInternal {
@Override @Override
public void setScreenBrightnessOverrideFromWindowManager(int screenBrightness) { public void setScreenBrightnessOverrideFromWindowManager(int screenBrightness) {
if (screenBrightness < PowerManager.BRIGHTNESS_DEFAULT if (screenBrightness < PowerManager.BRIGHTNESS_DEFAULT

View File

@@ -23,7 +23,10 @@ import static android.os.PowerManagerInternal.WAKEFULNESS_DREAMING;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.argThat;
@@ -59,6 +62,7 @@ import android.os.SystemClock;
import android.os.SystemProperties; import android.os.SystemProperties;
import android.os.UserHandle; import android.os.UserHandle;
import android.provider.Settings; import android.provider.Settings;
import android.view.Display;
import androidx.test.InstrumentationRegistry; import androidx.test.InstrumentationRegistry;
@@ -157,6 +161,10 @@ public class PowerManagerServiceTest {
mResourcesSpy = spy(mContextSpy.getResources()); mResourcesSpy = spy(mContextSpy.getResources());
when(mContextSpy.getResources()).thenReturn(mResourcesSpy); when(mContextSpy.getResources()).thenReturn(mResourcesSpy);
when(mDisplayManagerInternalMock.requestPowerState(any(), anyBoolean())).thenReturn(true);
}
private PowerManagerService createService() {
mService = new PowerManagerService(mContextSpy, new Injector() { mService = new PowerManagerService(mContextSpy, new Injector() {
@Override @Override
Notifier createNotifier(Looper looper, Context context, IBatteryStats batteryStats, Notifier createNotifier(Looper looper, Context context, IBatteryStats batteryStats,
@@ -166,7 +174,7 @@ public class PowerManagerServiceTest {
@Override @Override
SuspendBlocker createSuspendBlocker(PowerManagerService service, String name) { SuspendBlocker createSuspendBlocker(PowerManagerService service, String name) {
return mock(SuspendBlocker.class); return super.createSuspendBlocker(service, name);
} }
@Override @Override
@@ -191,6 +199,7 @@ public class PowerManagerServiceTest {
return mAmbientDisplayConfigurationMock; return mAmbientDisplayConfigurationMock;
} }
}); });
return mService;
} }
@After @After
@@ -262,6 +271,7 @@ public class PowerManagerServiceTest {
@Test @Test
public void testUpdatePowerScreenPolicy_UpdateDisplayPowerRequest() { public void testUpdatePowerScreenPolicy_UpdateDisplayPowerRequest() {
createService();
mService.updatePowerRequestFromBatterySaverPolicy(mDisplayPowerRequest); mService.updatePowerRequestFromBatterySaverPolicy(mDisplayPowerRequest);
assertThat(mDisplayPowerRequest.lowPowerMode).isEqualTo(BATTERY_SAVER_ENABLED); assertThat(mDisplayPowerRequest.lowPowerMode).isEqualTo(BATTERY_SAVER_ENABLED);
assertThat(mDisplayPowerRequest.screenLowPowerBrightnessFactor) assertThat(mDisplayPowerRequest.screenLowPowerBrightnessFactor)
@@ -270,6 +280,7 @@ public class PowerManagerServiceTest {
@Test @Test
public void testGetLastShutdownReasonInternal() { public void testGetLastShutdownReasonInternal() {
createService();
SystemProperties.set(TEST_LAST_REBOOT_PROPERTY, "shutdown,thermal"); SystemProperties.set(TEST_LAST_REBOOT_PROPERTY, "shutdown,thermal");
int reason = mService.getLastShutdownReasonInternal(TEST_LAST_REBOOT_PROPERTY); int reason = mService.getLastShutdownReasonInternal(TEST_LAST_REBOOT_PROPERTY);
SystemProperties.set(TEST_LAST_REBOOT_PROPERTY, ""); SystemProperties.set(TEST_LAST_REBOOT_PROPERTY, "");
@@ -278,6 +289,7 @@ public class PowerManagerServiceTest {
@Test @Test
public void testGetDesiredScreenPolicy_WithVR() throws Exception { public void testGetDesiredScreenPolicy_WithVR() throws Exception {
createService();
// Brighten up the screen // Brighten up the screen
mService.setWakefulnessLocked(WAKEFULNESS_AWAKE, PowerManager.WAKE_REASON_UNKNOWN, 0); mService.setWakefulnessLocked(WAKEFULNESS_AWAKE, PowerManager.WAKE_REASON_UNKNOWN, 0);
assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo( assertThat(mService.getDesiredScreenPolicyLocked()).isEqualTo(
@@ -307,11 +319,13 @@ public class PowerManagerServiceTest {
@Test @Test
public void testWakefulnessAwake_InitialValue() throws Exception { public void testWakefulnessAwake_InitialValue() throws Exception {
createService();
assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE); assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE);
} }
@Test @Test
public void testWakefulnessSleep_NoDozeSleepFlag() throws Exception { public void testWakefulnessSleep_NoDozeSleepFlag() throws Exception {
createService();
// Start with AWAKE state // Start with AWAKE state
startSystem(); startSystem();
assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE); assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE);
@@ -324,6 +338,7 @@ public class PowerManagerServiceTest {
@Test @Test
public void testWakefulnessAwake_AcquireCausesWakeup() throws Exception { public void testWakefulnessAwake_AcquireCausesWakeup() throws Exception {
createService();
startSystem(); startSystem();
forceSleep(); forceSleep();
@@ -355,6 +370,7 @@ public class PowerManagerServiceTest {
@Test @Test
public void testWakefulnessAwake_IPowerManagerWakeUp() throws Exception { public void testWakefulnessAwake_IPowerManagerWakeUp() throws Exception {
createService();
startSystem(); startSystem();
forceSleep(); forceSleep();
mService.getBinderServiceInstance().wakeUp(SystemClock.uptimeMillis(), mService.getBinderServiceInstance().wakeUp(SystemClock.uptimeMillis(),
@@ -369,6 +385,8 @@ public class PowerManagerServiceTest {
@Test @Test
public void testWakefulnessAwake_ShouldWakeUpWhenPluggedIn() throws Exception { public void testWakefulnessAwake_ShouldWakeUpWhenPluggedIn() throws Exception {
boolean powerState; boolean powerState;
createService();
startSystem(); startSystem();
forceSleep(); forceSleep();
@@ -444,6 +462,7 @@ public class PowerManagerServiceTest {
@Test @Test
public void testWakefulnessDoze_goToSleep() throws Exception { public void testWakefulnessDoze_goToSleep() throws Exception {
createService();
// Start with AWAKE state // Start with AWAKE state
startSystem(); startSystem();
assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE); assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE);
@@ -457,6 +476,7 @@ public class PowerManagerServiceTest {
@Test @Test
public void testWasDeviceIdleFor_true() { public void testWasDeviceIdleFor_true() {
int interval = 1000; int interval = 1000;
createService();
mService.onUserActivity(); mService.onUserActivity();
SystemClock.sleep(interval + 1 /* just a little more */); SystemClock.sleep(interval + 1 /* just a little more */);
assertThat(mService.wasDeviceIdleForInternal(interval)).isTrue(); assertThat(mService.wasDeviceIdleForInternal(interval)).isTrue();
@@ -465,12 +485,14 @@ public class PowerManagerServiceTest {
@Test @Test
public void testWasDeviceIdleFor_false() { public void testWasDeviceIdleFor_false() {
int interval = 1000; int interval = 1000;
createService();
mService.onUserActivity(); mService.onUserActivity();
assertThat(mService.wasDeviceIdleForInternal(interval)).isFalse(); assertThat(mService.wasDeviceIdleForInternal(interval)).isFalse();
} }
@Test @Test
public void testForceSuspend_putsDeviceToSleep() { public void testForceSuspend_putsDeviceToSleep() {
createService();
mService.systemReady(null); mService.systemReady(null);
mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED); mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED);
@@ -497,6 +519,8 @@ public class PowerManagerServiceTest {
final int flags = PowerManager.PARTIAL_WAKE_LOCK; final int flags = PowerManager.PARTIAL_WAKE_LOCK;
final String pkg = mContextSpy.getOpPackageName(); final String pkg = mContextSpy.getOpPackageName();
createService();
// Set up the Notification mock to keep track of the wakelocks that are currently // Set up the Notification mock to keep track of the wakelocks that are currently
// active or disabled. We'll use this to verify that wakelocks are disabled when // active or disabled. We'll use this to verify that wakelocks are disabled when
// they should be. // they should be.
@@ -541,7 +565,54 @@ public class PowerManagerServiceTest {
@Test @Test
public void testForceSuspend_forceSuspendFailurePropogated() { public void testForceSuspend_forceSuspendFailurePropogated() {
createService();
when(mNativeWrapperMock.nativeForceSuspend()).thenReturn(false); when(mNativeWrapperMock.nativeForceSuspend()).thenReturn(false);
assertThat(mService.getBinderServiceInstance().forceSuspend()).isFalse(); assertThat(mService.getBinderServiceInstance().forceSuspend()).isFalse();
} }
@Test
public void testSetDozeOverrideFromDreamManager_triggersSuspendBlocker() throws Exception {
final String suspendBlockerName = "PowerManagerService.Display";
final String tag = "acq_causes_wakeup";
final String packageName = "pkg.name";
final IBinder token = new Binder();
final boolean[] isAcquired = new boolean[1];
doAnswer(inv -> {
if (suspendBlockerName.equals(inv.getArguments()[0])) {
isAcquired[0] = false;
}
return null;
}).when(mNativeWrapperMock).nativeReleaseSuspendBlocker(any());
doAnswer(inv -> {
if (suspendBlockerName.equals(inv.getArguments()[0])) {
isAcquired[0] = true;
}
return null;
}).when(mNativeWrapperMock).nativeAcquireSuspendBlocker(any());
// Need to create the service after we stub the mocks for this test because some of the
// mocks are used during the constructor.
createService();
// Start with AWAKE state
startSystem();
assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_AWAKE);
assertTrue(isAcquired[0]);
// Take a nap and verify we no longer hold the blocker
int flags = PowerManager.DOZE_WAKE_LOCK;
mService.getBinderServiceInstance().acquireWakeLock(token, flags, tag, packageName,
null /* workSource */, null /* historyTag */);
mService.getBinderServiceInstance().goToSleep(SystemClock.uptimeMillis(),
PowerManager.GO_TO_SLEEP_REASON_APPLICATION, 0);
assertThat(mService.getWakefulness()).isEqualTo(WAKEFULNESS_DOZING);
assertFalse(isAcquired[0]);
// Override the display state by DreamManager and verify is reacquires the blocker.
mService.getLocalServiceInstance()
.setDozeOverrideFromDreamManager(Display.STATE_ON, PowerManager.BRIGHTNESS_DEFAULT);
assertTrue(isAcquired[0]);
}
} }