diff options
10 files changed, 98 insertions, 56 deletions
diff --git a/apex/jobscheduler/framework/java/android/os/PowerExemptionManager.java b/apex/jobscheduler/framework/java/android/os/PowerExemptionManager.java index fea7a11ba61a..cb1fccf1e96e 100644 --- a/apex/jobscheduler/framework/java/android/os/PowerExemptionManager.java +++ b/apex/jobscheduler/framework/java/android/os/PowerExemptionManager.java @@ -83,6 +83,12 @@ public class PowerExemptionManager { } /** + * Does not place the app on any temporary allow list. Nullifies the previous call to + * {@link android.app.BroadcastOptions#setTemporaryAppAllowlist(long, int, int, String)}. + * Note: this will not remove the receiver app from the temp allow list. + */ + public static final int TEMPORARY_ALLOW_LIST_TYPE_NONE = -1; + /** * Allow the temp allow list behavior, plus allow foreground service start from background. */ public static final int TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED = 0; @@ -96,6 +102,7 @@ public class PowerExemptionManager { * @hide */ @IntDef(flag = true, prefix = { "TEMPORARY_ALLOW_LIST_TYPE_" }, value = { + TEMPORARY_ALLOW_LIST_TYPE_NONE, TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED, TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED, }) diff --git a/apex/jobscheduler/framework/java/com/android/server/DeviceIdleInternal.java b/apex/jobscheduler/framework/java/com/android/server/DeviceIdleInternal.java index 9bd57a1109de..a9ca5cf5a26a 100644 --- a/apex/jobscheduler/framework/java/com/android/server/DeviceIdleInternal.java +++ b/apex/jobscheduler/framework/java/com/android/server/DeviceIdleInternal.java @@ -17,9 +17,9 @@ package com.android.server; import android.annotation.Nullable; -import android.os.PowerWhitelistManager; -import android.os.PowerWhitelistManager.ReasonCode; -import android.os.PowerWhitelistManager.TempAllowListType; +import android.os.PowerExemptionManager; +import android.os.PowerExemptionManager.ReasonCode; +import android.os.PowerExemptionManager.TempAllowListType; import com.android.server.deviceidle.IDeviceIdleConstraint; @@ -35,7 +35,7 @@ public interface DeviceIdleInternal { /** * Same as {@link #addPowerSaveTempWhitelistApp(int, String, long, int, boolean, int, String)} - * with {@link PowerWhitelistManager#TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED}. + * with {@link PowerExemptionManager#TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED}. */ void addPowerSaveTempWhitelistApp(int callingUid, String packageName, long durationMs, int userId, boolean sync, @ReasonCode int reasonCode, diff --git a/apex/jobscheduler/service/java/com/android/server/DeviceIdleController.java b/apex/jobscheduler/service/java/com/android/server/DeviceIdleController.java index 667fc60fb20b..57c8300b66f6 100644 --- a/apex/jobscheduler/service/java/com/android/server/DeviceIdleController.java +++ b/apex/jobscheduler/service/java/com/android/server/DeviceIdleController.java @@ -16,9 +16,9 @@ package com.android.server; -import static android.os.PowerWhitelistManager.REASON_SHELL; -import static android.os.PowerWhitelistManager.REASON_UNKNOWN; -import static android.os.PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED; +import static android.os.PowerExemptionManager.REASON_SHELL; +import static android.os.PowerExemptionManager.REASON_UNKNOWN; +import static android.os.PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED; import static android.os.Process.INVALID_UID; import android.Manifest; @@ -58,11 +58,11 @@ import android.os.Handler; import android.os.IDeviceIdleController; import android.os.Looper; import android.os.Message; +import android.os.PowerExemptionManager.ReasonCode; +import android.os.PowerExemptionManager.TempAllowListType; import android.os.PowerManager; import android.os.PowerManager.ServiceType; import android.os.PowerManagerInternal; -import android.os.PowerWhitelistManager.ReasonCode; -import android.os.PowerWhitelistManager.TempAllowListType; import android.os.Process; import android.os.RemoteException; import android.os.ResultReceiver; @@ -1947,7 +1947,7 @@ public class DeviceIdleController extends SystemService long durationMs, int userId, boolean sync, @ReasonCode int reasonCode, @Nullable String reason) { addPowerSaveTempAllowlistAppInternal(callingUid, packageName, durationMs, - TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED, + TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED, userId, sync, reasonCode, reason); } @@ -2706,7 +2706,7 @@ public class DeviceIdleController extends SystemService final long token = Binder.clearCallingIdentity(); try { addPowerSaveTempAllowlistAppInternal(callingUid, - packageName, duration, TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED, + packageName, duration, TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED, userId, true, reasonCode, reason); } finally { Binder.restoreCallingIdentity(token); diff --git a/core/api/system-current.txt b/core/api/system-current.txt index 48aa7061666e..a9413f5d803e 100644 --- a/core/api/system-current.txt +++ b/core/api/system-current.txt @@ -8336,6 +8336,7 @@ package android.os { field public static final int REASON_UNKNOWN = 0; // 0x0 field public static final int TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED = 0; // 0x0 field public static final int TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED = 1; // 0x1 + field public static final int TEMPORARY_ALLOW_LIST_TYPE_NONE = -1; // 0xffffffff } public final class PowerManager { diff --git a/core/api/test-current.txt b/core/api/test-current.txt index 7cfe7826377a..561d838daef9 100644 --- a/core/api/test-current.txt +++ b/core/api/test-current.txt @@ -253,6 +253,14 @@ package android.app { method public void offsetBeginAndEndTime(long); } + public class BroadcastOptions { + ctor public BroadcastOptions(@NonNull android.os.Bundle); + method public long getTemporaryAppAllowlistDuration(); + method @Nullable public String getTemporaryAppAllowlistReason(); + method public int getTemporaryAppAllowlistReasonCode(); + method public int getTemporaryAppAllowlistType(); + } + public class DownloadManager { field public static final String COLUMN_MEDIASTORE_URI = "mediastore_uri"; } diff --git a/core/java/android/app/ActivityManagerInternal.java b/core/java/android/app/ActivityManagerInternal.java index f7a35143916e..a7d5b0519db6 100644 --- a/core/java/android/app/ActivityManagerInternal.java +++ b/core/java/android/app/ActivityManagerInternal.java @@ -30,8 +30,8 @@ import android.content.pm.UserInfo; import android.net.Uri; import android.os.Bundle; import android.os.IBinder; -import android.os.PowerWhitelistManager.ReasonCode; -import android.os.PowerWhitelistManager.TempAllowListType; +import android.os.PowerExemptionManager.ReasonCode; +import android.os.PowerExemptionManager.TempAllowListType; import android.os.TransactionTooLargeException; import android.os.WorkSource; import android.util.ArraySet; diff --git a/core/java/android/app/BroadcastOptions.java b/core/java/android/app/BroadcastOptions.java index 477e96b16daf..9da2581e449f 100644 --- a/core/java/android/app/BroadcastOptions.java +++ b/core/java/android/app/BroadcastOptions.java @@ -16,14 +16,16 @@ package android.app; +import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.annotation.SystemApi; +import android.annotation.TestApi; import android.os.Build; import android.os.Bundle; -import android.os.PowerWhitelistManager; -import android.os.PowerWhitelistManager.ReasonCode; -import android.os.PowerWhitelistManager.TempAllowListType; +import android.os.PowerExemptionManager; +import android.os.PowerExemptionManager.ReasonCode; +import android.os.PowerExemptionManager.TempAllowListType; /** * Helper class for building an options Bundle that can be used with @@ -35,8 +37,7 @@ import android.os.PowerWhitelistManager.TempAllowListType; public class BroadcastOptions { private long mTemporaryAppAllowlistDuration; private @TempAllowListType int mTemporaryAppAllowlistType; - private @ReasonCode int mTemporaryAppAllowlistReasonCode = - PowerWhitelistManager.REASON_UNKNOWN; + private @ReasonCode int mTemporaryAppAllowlistReasonCode; private @Nullable String mTemporaryAppAllowlistReason; private int mMinManifestReceiverApiLevel = 0; private int mMaxManifestReceiverApiLevel = Build.VERSION_CODES.CUR_DEVELOPMENT; @@ -47,59 +48,59 @@ public class BroadcastOptions { * How long to temporarily put an app on the power allowlist when executing this broadcast * to it. */ - static final String KEY_TEMPORARY_APP_ALLOWLIST_DURATION + private static final String KEY_TEMPORARY_APP_ALLOWLIST_DURATION = "android:broadcast.temporaryAppAllowlistDuration"; - static final String KEY_TEMPORARY_APP_ALLOWLIST_TYPE + private static final String KEY_TEMPORARY_APP_ALLOWLIST_TYPE = "android:broadcast.temporaryAppAllowlistType"; - static final String KEY_TEMPORARY_APP_ALLOWLIST_REASON_CODE = + private static final String KEY_TEMPORARY_APP_ALLOWLIST_REASON_CODE = "android:broadcast.temporaryAppAllowlistReasonCode"; - static final String KEY_TEMPORARY_APP_ALLOWLIST_REASON = + private static final String KEY_TEMPORARY_APP_ALLOWLIST_REASON = "android:broadcast.temporaryAppAllowlistReason"; /** * Corresponds to {@link #setMinManifestReceiverApiLevel}. */ - static final String KEY_MIN_MANIFEST_RECEIVER_API_LEVEL + private static final String KEY_MIN_MANIFEST_RECEIVER_API_LEVEL = "android:broadcast.minManifestReceiverApiLevel"; /** * Corresponds to {@link #setMaxManifestReceiverApiLevel}. */ - static final String KEY_MAX_MANIFEST_RECEIVER_API_LEVEL + private static final String KEY_MAX_MANIFEST_RECEIVER_API_LEVEL = "android:broadcast.maxManifestReceiverApiLevel"; /** * Corresponds to {@link #setDontSendToRestrictedApps}. */ - static final String KEY_DONT_SEND_TO_RESTRICTED_APPS = + private static final String KEY_DONT_SEND_TO_RESTRICTED_APPS = "android:broadcast.dontSendToRestrictedApps"; /** * Corresponds to {@link #setBackgroundActivityStartsAllowed}. */ - static final String KEY_ALLOW_BACKGROUND_ACTIVITY_STARTS = + private static final String KEY_ALLOW_BACKGROUND_ACTIVITY_STARTS = "android:broadcast.allowBackgroundActivityStarts"; /** * @hide - * @deprecated Use {@link android.os.PowerWhitelistManager# - * TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED} instead. + * @deprecated Use {@link android.os.PowerExemptionManager# + * TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED} instead. */ @Deprecated public static final int TEMPORARY_WHITELIST_TYPE_FOREGROUND_SERVICE_ALLOWED = - PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED; + PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED; /** * @hide - * @deprecated Use {@link android.os.PowerWhitelistManager# - * TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED} instead. + * @deprecated Use {@link android.os.PowerExemptionManager# + * TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED} instead. */ @Deprecated public static final int TEMPORARY_WHITELIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED = - PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED; + PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED; public static BroadcastOptions makeBasic() { BroadcastOptions opts = new BroadcastOptions(); @@ -107,15 +108,22 @@ public class BroadcastOptions { } private BroadcastOptions() { + resetTemporaryAppAllowlist(); } /** @hide */ - public BroadcastOptions(Bundle opts) { - mTemporaryAppAllowlistDuration = opts.getLong(KEY_TEMPORARY_APP_ALLOWLIST_DURATION); - mTemporaryAppAllowlistType = opts.getInt(KEY_TEMPORARY_APP_ALLOWLIST_TYPE); - mTemporaryAppAllowlistReasonCode = opts.getInt(KEY_TEMPORARY_APP_ALLOWLIST_REASON_CODE, - PowerWhitelistManager.REASON_UNKNOWN); - mTemporaryAppAllowlistReason = opts.getString(KEY_TEMPORARY_APP_ALLOWLIST_REASON); + @TestApi + public BroadcastOptions(@NonNull Bundle opts) { + // Match the logic in toBundle(). + if (opts.containsKey(KEY_TEMPORARY_APP_ALLOWLIST_DURATION)) { + mTemporaryAppAllowlistDuration = opts.getLong(KEY_TEMPORARY_APP_ALLOWLIST_DURATION); + mTemporaryAppAllowlistType = opts.getInt(KEY_TEMPORARY_APP_ALLOWLIST_TYPE); + mTemporaryAppAllowlistReasonCode = opts.getInt(KEY_TEMPORARY_APP_ALLOWLIST_REASON_CODE, + PowerExemptionManager.REASON_UNKNOWN); + mTemporaryAppAllowlistReason = opts.getString(KEY_TEMPORARY_APP_ALLOWLIST_REASON); + } else { + resetTemporaryAppAllowlist(); + } mMinManifestReceiverApiLevel = opts.getInt(KEY_MIN_MANIFEST_RECEIVER_API_LEVEL, 0); mMaxManifestReceiverApiLevel = opts.getInt(KEY_MAX_MANIFEST_RECEIVER_API_LEVEL, Build.VERSION_CODES.CUR_DEVELOPMENT); @@ -136,18 +144,21 @@ public class BroadcastOptions { android.Manifest.permission.START_FOREGROUND_SERVICES_FROM_BACKGROUND}) public void setTemporaryAppWhitelistDuration(long duration) { setTemporaryAppAllowlist(duration, - PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED, - PowerWhitelistManager.REASON_UNKNOWN, null); + PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED, + PowerExemptionManager.REASON_UNKNOWN, null); } /** * Set a duration for which the system should temporary place an application on the * power allowlist when this broadcast is being delivered to it, specify the temp allowlist * type. - * @param duration the duration in milliseconds; 0 means to not place on allowlist. - * @param type one of {@link TempAllowListType} + * @param duration the duration in milliseconds. + * 0 means to not place on allowlist, and clears previous call to this method. + * @param type one of {@link TempAllowListType}. + * {@link PowerExemptionManager#TEMPORARY_ALLOW_LIST_TYPE_NONE} means + * to not place on allowlist, and clears previous call to this method. * @param reasonCode one of {@link ReasonCode}, use - * {@link PowerWhitelistManager#REASON_UNKNOWN} if not sure. + * {@link PowerExemptionManager#REASON_UNKNOWN} if not sure. * @param reason A human-readable reason explaining why the app is temp allowlisted. Only * used for logging purposes. Could be null or empty string. */ @@ -160,12 +171,30 @@ public class BroadcastOptions { mTemporaryAppAllowlistType = type; mTemporaryAppAllowlistReasonCode = reasonCode; mTemporaryAppAllowlistReason = reason; + + if (!isTemporaryAppAllowlistSet()) { + resetTemporaryAppAllowlist(); + } + } + + private boolean isTemporaryAppAllowlistSet() { + return mTemporaryAppAllowlistDuration > 0 + && mTemporaryAppAllowlistType + != PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_NONE; + } + + private void resetTemporaryAppAllowlist() { + mTemporaryAppAllowlistDuration = 0; + mTemporaryAppAllowlistType = PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_NONE; + mTemporaryAppAllowlistReasonCode = PowerExemptionManager.REASON_UNKNOWN; + mTemporaryAppAllowlistReason = null; } /** * Return {@link #setTemporaryAppAllowlist}. * @hide */ + @TestApi public long getTemporaryAppAllowlistDuration() { return mTemporaryAppAllowlistDuration; } @@ -174,6 +203,7 @@ public class BroadcastOptions { * Return {@link #mTemporaryAppAllowlistType}. * @hide */ + @TestApi public @TempAllowListType int getTemporaryAppAllowlistType() { return mTemporaryAppAllowlistType; } @@ -182,6 +212,7 @@ public class BroadcastOptions { * Return {@link #mTemporaryAppAllowlistReasonCode}. * @hide */ + @TestApi public @ReasonCode int getTemporaryAppAllowlistReasonCode() { return mTemporaryAppAllowlistReasonCode; } @@ -190,6 +221,7 @@ public class BroadcastOptions { * Return {@link #mTemporaryAppAllowlistReason}. * @hide */ + @TestApi public @Nullable String getTemporaryAppAllowlistReason() { return mTemporaryAppAllowlistReason; } @@ -276,16 +308,10 @@ public class BroadcastOptions { */ public Bundle toBundle() { Bundle b = new Bundle(); - if (mTemporaryAppAllowlistDuration > 0) { + if (isTemporaryAppAllowlistSet()) { b.putLong(KEY_TEMPORARY_APP_ALLOWLIST_DURATION, mTemporaryAppAllowlistDuration); - } - if (mTemporaryAppAllowlistType != 0) { b.putInt(KEY_TEMPORARY_APP_ALLOWLIST_TYPE, mTemporaryAppAllowlistType); - } - if (mTemporaryAppAllowlistReasonCode != PowerWhitelistManager.REASON_UNKNOWN) { b.putInt(KEY_TEMPORARY_APP_ALLOWLIST_REASON_CODE, mTemporaryAppAllowlistReasonCode); - } - if (mTemporaryAppAllowlistReason != null) { b.putString(KEY_TEMPORARY_APP_ALLOWLIST_REASON, mTemporaryAppAllowlistReason); } if (mMinManifestReceiverApiLevel != 0) { diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index fa88d6603840..2ee5c5ada0f3 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -255,11 +255,11 @@ import android.os.Looper; import android.os.Message; import android.os.Parcel; import android.os.ParcelFileDescriptor; +import android.os.PowerExemptionManager.ReasonCode; +import android.os.PowerExemptionManager.TempAllowListType; import android.os.PowerManager; import android.os.PowerManager.ServiceType; import android.os.PowerManagerInternal; -import android.os.PowerWhitelistManager.ReasonCode; -import android.os.PowerWhitelistManager.TempAllowListType; import android.os.Process; import android.os.RemoteCallback; import android.os.RemoteException; diff --git a/services/core/java/com/android/server/am/BroadcastQueue.java b/services/core/java/com/android/server/am/BroadcastQueue.java index 9ecae42ce2c9..7f2eae8ff153 100644 --- a/services/core/java/com/android/server/am/BroadcastQueue.java +++ b/services/core/java/com/android/server/am/BroadcastQueue.java @@ -45,8 +45,8 @@ import android.os.Handler; import android.os.IBinder; import android.os.Looper; import android.os.Message; -import android.os.PowerWhitelistManager; -import android.os.PowerWhitelistManager.TempAllowListType; +import android.os.PowerExemptionManager.ReasonCode; +import android.os.PowerExemptionManager.TempAllowListType; import android.os.Process; import android.os.RemoteException; import android.os.SystemClock; @@ -908,7 +908,7 @@ public final class BroadcastQueue { } final void scheduleTempAllowlistLocked(int uid, long duration, BroadcastRecord r, - @TempAllowListType int type, @PowerWhitelistManager.ReasonCode int reasonCode, + @TempAllowListType int type, @ReasonCode int reasonCode, @Nullable String reason) { if (duration > Integer.MAX_VALUE) { duration = Integer.MAX_VALUE; diff --git a/services/core/java/com/android/server/am/PendingIntentRecord.java b/services/core/java/com/android/server/am/PendingIntentRecord.java index 51666acb8134..f7c777e9cd6c 100644 --- a/services/core/java/com/android/server/am/PendingIntentRecord.java +++ b/services/core/java/com/android/server/am/PendingIntentRecord.java @@ -66,7 +66,7 @@ public final class PendingIntentRecord extends IIntentSender.Stub { /** * Map IBinder to duration specified as Pair<Long, Integer>, Long is allowlist duration in * milliseconds, Integer is allowlist type defined at - * {@link android.os.PowerWhitelistManager.TempAllowListType} + * {@link android.os.PowerExemptionManager.TempAllowListType} */ private ArrayMap<IBinder, TempAllowListDuration> mAllowlistDuration; private RemoteCallbackList<IResultReceiver> mCancelCallbacks; |