diff options
Diffstat (limited to 'android/app/usage/UsageStatsManager.java')
-rw-r--r-- | android/app/usage/UsageStatsManager.java | 149 |
1 files changed, 143 insertions, 6 deletions
diff --git a/android/app/usage/UsageStatsManager.java b/android/app/usage/UsageStatsManager.java index 3a3e16e0..edb6a74b 100644 --- a/android/app/usage/UsageStatsManager.java +++ b/android/app/usage/UsageStatsManager.java @@ -16,16 +16,18 @@ package android.app.usage; +import android.annotation.IntDef; import android.annotation.RequiresPermission; import android.annotation.SystemApi; import android.annotation.SystemService; -import android.app.usage.AppStandby.StandbyBuckets; import android.content.Context; import android.content.pm.ParceledListSlice; import android.os.RemoteException; import android.os.UserHandle; import android.util.ArrayMap; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.util.Collections; import java.util.List; import java.util.Map; @@ -90,6 +92,76 @@ public final class UsageStatsManager { */ public static final int INTERVAL_COUNT = 4; + + /** + * The app is whitelisted for some reason and the bucket cannot be changed. + * {@hide} + */ + @SystemApi + public static final int STANDBY_BUCKET_EXEMPTED = 5; + + /** + * The app was used very recently, currently in use or likely to be used very soon. + * @see #getAppStandbyBucket() + */ + public static final int STANDBY_BUCKET_ACTIVE = 10; + + /** + * The app was used recently and/or likely to be used in the next few hours. + * @see #getAppStandbyBucket() + */ + public static final int STANDBY_BUCKET_WORKING_SET = 20; + + /** + * The app was used in the last few days and/or likely to be used in the next few days. + * @see #getAppStandbyBucket() + */ + public static final int STANDBY_BUCKET_FREQUENT = 30; + + /** + * The app has not be used for several days and/or is unlikely to be used for several days. + * @see #getAppStandbyBucket() + */ + public static final int STANDBY_BUCKET_RARE = 40; + + /** + * The app has never been used. + * {@hide} + */ + @SystemApi + public static final int STANDBY_BUCKET_NEVER = 50; + + /** {@hide} Reason for bucketing -- default initial state */ + public static final String REASON_DEFAULT = "default"; + + /** {@hide} Reason for bucketing -- timeout */ + public static final String REASON_TIMEOUT = "timeout"; + + /** {@hide} Reason for bucketing -- usage */ + public static final String REASON_USAGE = "usage"; + + /** {@hide} Reason for bucketing -- forced by user / shell command */ + public static final String REASON_FORCED = "forced"; + + /** + * {@hide} + * Reason for bucketing -- predicted. This is a prefix and the UID of the bucketeer will + * be appended. + */ + public static final String REASON_PREDICTED = "predicted"; + + /** @hide */ + @IntDef(flag = false, prefix = { "STANDBY_BUCKET_" }, value = { + STANDBY_BUCKET_EXEMPTED, + STANDBY_BUCKET_ACTIVE, + STANDBY_BUCKET_WORKING_SET, + STANDBY_BUCKET_FREQUENT, + STANDBY_BUCKET_RARE, + STANDBY_BUCKET_NEVER, + }) + @Retention(RetentionPolicy.SOURCE) + public @interface StandbyBuckets {} + private static final UsageEvents sEmptyResults = new UsageEvents(); private final Context mContext; @@ -237,7 +309,7 @@ public final class UsageStatsManager { } /** - * @hide + * {@hide} */ public void setAppInactive(String packageName, boolean inactive) { try { @@ -248,20 +320,52 @@ public final class UsageStatsManager { } /** - * @hide + * Returns the current standby bucket of the calling app. The system determines the standby + * state of the app based on app usage patterns. Standby buckets determine how much an app will + * be restricted from running background tasks such as jobs, alarms and certain PendingIntent + * callbacks. + * <p>Restrictions increase progressively from {@link #STANDBY_BUCKET_ACTIVE} to + * {@link #STANDBY_BUCKET_RARE}, with {@link #STANDBY_BUCKET_ACTIVE} being the least + * restrictive. The battery level of the device might also affect the restrictions. + * + * @return the current standby bucket of the calling app. One of STANDBY_BUCKET_* constants. */ + public @StandbyBuckets int getAppStandbyBucket() { + try { + return mService.getAppStandbyBucket(mContext.getOpPackageName(), + mContext.getOpPackageName(), + mContext.getUserId()); + } catch (RemoteException e) { + } + return STANDBY_BUCKET_ACTIVE; + } + + /** + * {@hide} + * Returns the current standby bucket of the specified app. The caller must hold the permission + * android.permission.PACKAGE_USAGE_STATS. + * @param packageName the package for which to fetch the current standby bucket. + */ + @SystemApi + @RequiresPermission(android.Manifest.permission.PACKAGE_USAGE_STATS) public @StandbyBuckets int getAppStandbyBucket(String packageName) { try { return mService.getAppStandbyBucket(packageName, mContext.getOpPackageName(), mContext.getUserId()); } catch (RemoteException e) { } - return AppStandby.STANDBY_BUCKET_ACTIVE; + return STANDBY_BUCKET_ACTIVE; } /** - * @hide - * Changes the app standby state to the provided bucket. + * {@hide} + * Changes an app's standby bucket to the provided value. The caller can only set the standby + * bucket for a different app than itself. + * @param packageName the package name of the app to set the bucket for. A SecurityException + * will be thrown if the package name is that of the caller. + * @param bucket the standby bucket to set it to, which should be one of STANDBY_BUCKET_*. + * Setting a standby bucket outside of the range of STANDBY_BUCKET_ACTIVE to + * STANDBY_BUCKET_NEVER will result in a SecurityException. */ @SystemApi @RequiresPermission(android.Manifest.permission.CHANGE_APP_IDLE_STATE) @@ -275,6 +379,39 @@ public final class UsageStatsManager { /** * {@hide} + * Returns the current standby bucket of every app that has a bucket assigned to it. + * The caller must hold the permission android.permission.PACKAGE_USAGE_STATS. The key of the + * returned Map is the package name and the value is the bucket assigned to the package. + * @see #getAppStandbyBucket() + */ + @SystemApi + @RequiresPermission(android.Manifest.permission.PACKAGE_USAGE_STATS) + public Map<String, Integer> getAppStandbyBuckets() { + try { + return (Map<String, Integer>) mService.getAppStandbyBuckets( + mContext.getOpPackageName(), mContext.getUserId()); + } catch (RemoteException e) { + } + return Collections.EMPTY_MAP; + } + + /** + * {@hide} + * Changes the app standby bucket for multiple apps at once. The Map is keyed by the package + * name and the value is one of STANDBY_BUCKET_*. + * @param appBuckets a map of package name to bucket value. + */ + @SystemApi + @RequiresPermission(android.Manifest.permission.CHANGE_APP_IDLE_STATE) + public void setAppStandbyBuckets(Map<String, Integer> appBuckets) { + try { + mService.setAppStandbyBuckets(appBuckets, mContext.getUserId()); + } catch (RemoteException e) { + } + } + + /** + * {@hide} * Temporarily whitelist the specified app for a short duration. This is to allow an app * receiving a high priority message to be able to access the network and acquire wakelocks * even if the device is in power-save mode or the app is currently considered inactive. |