Add TestApi to force Low Power Standby to be active

Since Low Power Standby depends on multiple factors to activate,
including the private device configuration for the delay after the
device becomes non-interactive, having this TestApi makes testing
faster and more reliable.

Bug: 190822356
Test: atest LowPowerStandbyTest
Change-Id: I5a16083f996bd7421197b08c6632655c8c3d8c29
This commit is contained in:
Robert Horvath
2021-12-06 15:01:29 +01:00
parent 4845efb60d
commit e57eb6d07e
6 changed files with 77 additions and 3 deletions

View File

@@ -1713,6 +1713,7 @@ package android.os {
}
public final class PowerManager {
method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_LOW_POWER_STANDBY, android.Manifest.permission.DEVICE_POWER}) public void forceLowPowerStandbyActive(boolean);
field public static final String ACTION_ENHANCED_DISCHARGE_PREDICTION_CHANGED = "android.os.action.ENHANCED_DISCHARGE_PREDICTION_CHANGED";
field @RequiresPermission(android.Manifest.permission.DEVICE_POWER) public static final int SYSTEM_WAKELOCK = -2147483648; // 0x80000000
}

View File

@@ -68,6 +68,7 @@ interface IPowerManager
boolean isLowPowerStandbySupported();
boolean isLowPowerStandbyEnabled();
void setLowPowerStandbyEnabled(boolean enabled);
void forceLowPowerStandbyActive(boolean active);
@UnsupportedAppUsage
void reboot(boolean confirm, String reason, boolean wait);

View File

@@ -2214,6 +2214,26 @@ public final class PowerManager {
}
}
/**
* Force Low Power Standby restrictions to be active.
* Does nothing if Low Power Standby is not supported.
*
* @see #isLowPowerStandbySupported()
* @hide
*/
@TestApi
@RequiresPermission(anyOf = {
android.Manifest.permission.MANAGE_LOW_POWER_STANDBY,
android.Manifest.permission.DEVICE_POWER
})
public void forceLowPowerStandbyActive(boolean active) {
try {
mService.forceLowPowerStandbyActive(active);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Return whether the given application package name is on the device's power allowlist.
* Apps can be placed on the allowlist through the settings UI invoked by

View File

@@ -149,6 +149,10 @@ public final class LowPowerStandbyController {
@GuardedBy("mLock")
private boolean mIdleSinceNonInteractive;
/** Force Low Power Standby to be active. */
@GuardedBy("mLock")
private boolean mForceActive;
/** Functional interface for providing time. */
@VisibleForTesting
interface Clock {
@@ -211,12 +215,14 @@ public final class LowPowerStandbyController {
(now - mLastInteractiveTimeElapsed) >= mStandbyTimeoutConfig;
final boolean maintenanceMode = mIdleSinceNonInteractive && !mIsDeviceIdle;
final boolean newActive =
mIsEnabled && !mIsInteractive && standbyTimeoutExpired && !maintenanceMode;
mForceActive || (mIsEnabled && !mIsInteractive && standbyTimeoutExpired
&& !maintenanceMode);
if (DEBUG) {
Slog.d(TAG, "updateActiveLocked: mIsEnabled=" + mIsEnabled + ", mIsInteractive="
+ mIsInteractive + ", standbyTimeoutExpired=" + standbyTimeoutExpired
+ ", mIdleSinceNonInteractive=" + mIdleSinceNonInteractive + ", mIsDeviceIdle="
+ mIsDeviceIdle + ", mIsActive=" + mIsActive + ", newActive=" + newActive);
+ mIsDeviceIdle + ", mForceActive=" + mForceActive + ", mIsActive=" + mIsActive
+ ", newActive=" + newActive);
}
if (mIsActive != newActive) {
mIsActive = newActive;
@@ -410,6 +416,13 @@ public final class LowPowerStandbyController {
}
}
void forceActive(boolean active) {
synchronized (mLock) {
mForceActive = active;
updateActiveLocked();
}
}
void dump(PrintWriter pw) {
final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
@@ -428,7 +441,7 @@ public final class LowPowerStandbyController {
ipw.print("mStandbyTimeoutConfig=");
ipw.println(mStandbyTimeoutConfig);
if (mIsEnabled) {
if (mIsActive || mIsEnabled) {
ipw.print("mIsInteractive=");
ipw.println(mIsInteractive);
ipw.print("mLastInteractiveTime=");

View File

@@ -5902,6 +5902,27 @@ public final class PowerManagerService extends SystemService
}
}
@Override // Binder call
@RequiresPermission(anyOf = {
android.Manifest.permission.MANAGE_LOW_POWER_STANDBY,
android.Manifest.permission.DEVICE_POWER
})
public void forceLowPowerStandbyActive(boolean active) {
if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER)
!= PackageManager.PERMISSION_GRANTED) {
mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.MANAGE_LOW_POWER_STANDBY,
"forceLowPowerStandbyActive");
}
final long ident = Binder.clearCallingIdentity();
try {
mLowPowerStandbyController.forceActive(active);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
/**
* Gets the reason for the last time the phone had to reboot.
*

View File

@@ -338,6 +338,24 @@ public class LowPowerStandbyControllerTest {
verify(mPowerManagerInternalMock).setLowPowerStandbyAllowlist(new int[] {});
}
@Test
public void testForceActive() throws Exception {
setLowPowerStandbySupportedConfig(false);
mController.systemReady();
mController.forceActive(true);
mTestLooper.dispatchAll();
assertThat(mController.isActive()).isTrue();
verify(mPowerManagerInternalMock).setLowPowerStandbyActive(true);
mController.forceActive(false);
mTestLooper.dispatchAll();
assertThat(mController.isActive()).isFalse();
verify(mPowerManagerInternalMock).setLowPowerStandbyActive(false);
}
private void setLowPowerStandbySupportedConfig(boolean supported) {
when(mResourcesSpy.getBoolean(
com.android.internal.R.bool.config_lowPowerStandbySupported))