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

Commit 835283f0 authored by Kweku Adams's avatar Kweku Adams
Browse files

Adding temp whitelist methods to PowerWhitelistManager interface.

Migrating temp whitelist APIs to PowerWhitelistManager to allow
telephony to add apps to the temp whitelist.

Bug: 138239060
Bug: 140141678
Bug: 141155196
Bug: 142420609
Bug: 144864180
Test: atest PowerWhitelistTest
Change-Id: Iefd6ecf89aad46de042ef3a1ec0c20d6c35da5a6
parent 0f2b7fd2
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -44,6 +44,10 @@ public class DeviceIdleManager {
        mService = service;
    }

    IDeviceIdleController getService() {
        return mService;
    }

    /**
     * @return package names the system has white-listed to opt out of power save restrictions,
     * except for device idle mode.
+1 −0
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ interface IDeviceIdleController {
    void addPowerSaveTempWhitelistApp(String name, long duration, int userId, String reason);
    long addPowerSaveTempWhitelistAppForMms(String name, int userId, String reason);
    long addPowerSaveTempWhitelistAppForSms(String name, int userId, String reason);
    long whitelistAppTemporarily(String name, int userId, String reason);
    void exitIdle(String reason);
    int setPreIdleTimeoutMode(int Mode);
    void resetPreIdleTimeoutMode();
+85 −0
Original line number Diff line number Diff line
@@ -16,12 +16,17 @@

package android.os;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.TestApi;
import android.content.Context;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Interface to access and modify the power save whitelist.
 *
@@ -32,11 +37,91 @@ import android.content.Context;
@SystemService(Context.POWER_WHITELIST_MANAGER)
public class PowerWhitelistManager {
    private final Context mContext;
    // Proxy to DeviceIdleController for now
    // TODO: migrate to PowerWhitelistController
    private final IDeviceIdleController mService;

    /**
     * Indicates that an unforeseen event has occurred and the app should be whitelisted to handle
     * it.
     */
    public static final int EVENT_UNSPECIFIED = 0;

    /**
     * Indicates that an SMS event has occurred and the app should be whitelisted to handle it.
     */
    public static final int EVENT_SMS = 1;

    /**
     * Indicates that an MMS event has occurred and the app should be whitelisted to handle it.
     */
    public static final int EVENT_MMS = 2;

    /**
     * @hide
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(prefix = {"EVENT_"}, value = {
            EVENT_UNSPECIFIED,
            EVENT_SMS,
            EVENT_MMS,
    })
    public @interface WhitelistEvent {
    }

    /**
     * @hide
     */
    public PowerWhitelistManager(@NonNull Context context) {
        mContext = context;
        mService = context.getSystemService(DeviceIdleManager.class).getService();
    }

    /**
     * Add an app to the temporary whitelist for a short amount of time.
     *
     * @param packageName The package to add to the temp whitelist
     * @param durationMs How long to keep the app on the temp whitelist for (in milliseconds)
     */
    @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST)
    public void whitelistAppTemporarily(@NonNull String packageName, long durationMs) {
        String reason = "from:" + UserHandle.formatUid(Binder.getCallingUid());
        try {
            mService.addPowerSaveTempWhitelistApp(packageName, durationMs, mContext.getUserId(),
                    reason);
        } catch (RemoteException e) {
            e.rethrowFromSystemServer();
        }
    }

    /**
     * Add an app to the temporary whitelist for a short amount of time for a specific reason.
     *
     * @param packageName The package to add to the temp whitelist
     * @param event The reason to add the app to the temp whitelist
     * @param reason A human-readable reason explaining why the app is temp whitelisted. Only used
     *               for logging purposes
     * @return The duration (in milliseconds) that the app is whitelisted for
     */
    @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST)
    public long whitelistAppTemporarilyForEvent(@NonNull String packageName,
            @WhitelistEvent int event, @NonNull String reason) {
        try {
            switch (event) {
                case EVENT_MMS:
                    return mService.addPowerSaveTempWhitelistAppForMms(
                            packageName, mContext.getUserId(), reason);
                case EVENT_SMS:
                    return mService.addPowerSaveTempWhitelistAppForSms(
                            packageName, mContext.getUserId(), reason);
                case EVENT_UNSPECIFIED:
                default:
                    return mService.whitelistAppTemporarily(
                            packageName, mContext.getUserId(), reason);
            }
        } catch (RemoteException e) {
            e.rethrowFromSystemServer();
            return 0;
        }
    }
}
+11 −1
Original line number Diff line number Diff line
@@ -1662,7 +1662,17 @@ public class DeviceIdleController extends SystemService
            return isPowerSaveWhitelistAppInternal(name);
        }

        @Override public void addPowerSaveTempWhitelistApp(String packageName, long duration,
        @Override
        public long whitelistAppTemporarily(String packageName, int userId, String reason)
                throws RemoteException {
            // At least 10 seconds.
            long duration = Math.max(10_000L, mConstants.MAX_TEMP_APP_WHITELIST_DURATION / 2);
            addPowerSaveTempWhitelistAppChecked(packageName, duration, userId, reason);
            return duration;
        }

        @Override
        public void addPowerSaveTempWhitelistApp(String packageName, long duration,
                int userId, String reason) throws RemoteException {
            addPowerSaveTempWhitelistAppChecked(packageName, duration, userId, reason);
        }
+7 −1
Original line number Diff line number Diff line
@@ -1283,7 +1283,7 @@ package android.app.usage {
    method @RequiresPermission(allOf={android.Manifest.permission.SUSPEND_APPS, android.Manifest.permission.OBSERVE_APP_USAGE}) public void unregisterAppUsageLimitObserver(int);
    method @RequiresPermission(android.Manifest.permission.OBSERVE_APP_USAGE) public void unregisterAppUsageObserver(int);
    method @RequiresPermission(android.Manifest.permission.OBSERVE_APP_USAGE) public void unregisterUsageSessionObserver(int);
    method @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST) public void whitelistAppTemporarily(String, long, android.os.UserHandle);
    method @Deprecated @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST) public void whitelistAppTemporarily(String, long, android.os.UserHandle);
    field public static final String EXTRA_OBSERVER_ID = "android.app.usage.extra.OBSERVER_ID";
    field public static final String EXTRA_TIME_LIMIT = "android.app.usage.extra.TIME_LIMIT";
    field public static final String EXTRA_TIME_USED = "android.app.usage.extra.TIME_USED";
@@ -5746,6 +5746,11 @@ package android.os {
  }
  public class PowerWhitelistManager {
    method @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST) public void whitelistAppTemporarily(@NonNull String, long);
    method @RequiresPermission(android.Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST) public long whitelistAppTemporarilyForEvent(@NonNull String, int, @NonNull String);
    field public static final int EVENT_MMS = 2; // 0x2
    field public static final int EVENT_SMS = 1; // 0x1
    field public static final int EVENT_UNSPECIFIED = 0; // 0x0
  }
  public class RecoverySystem {
@@ -5874,6 +5879,7 @@ package android.os {
  }
  public final class UserHandle implements android.os.Parcelable {
    method @NonNull public static String formatUid(int);
    method public static int getAppId(int);
    method public int getIdentifier();
    method @Deprecated public boolean isOwner();
Loading