Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e57eb6d0 authored by Robert Horvath's avatar Robert Horvath
Browse files

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
parent 4845efb6
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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
  }
+1 −0
Original line number Diff line number Diff line
@@ -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);
+20 −0
Original line number Diff line number Diff line
@@ -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
+16 −3
Original line number Diff line number Diff line
@@ -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=");
+21 −0
Original line number Diff line number Diff line
@@ -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.
         *
Loading