Merge "Add TEMPORARY_ALLOW_LIST_TYPE_NONE." into sc-dev

This commit is contained in:
Makoto Onuki
2021-03-29 21:32:40 +00:00
committed by Android (Google) Code Review
10 changed files with 98 additions and 56 deletions

View File

@@ -82,6 +82,12 @@ public class PowerExemptionManager {
public @interface AllowListEvent {
}
/**
* 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.
*/
@@ -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,
})

View File

@@ -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,

View File

@@ -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);

View File

@@ -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 {

View File

@@ -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";
}

View File

@@ -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;

View File

@@ -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) {

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;