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

Commit f156c049 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Expose App Standby APIs for apps and system"

parents 01a70b2e afbccb7d
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -7209,6 +7209,7 @@ package android.app.usage {
  }
  public final class UsageStatsManager {
    method public int getAppStandbyBucket();
    method public boolean isAppInactive(java.lang.String);
    method public java.util.Map<java.lang.String, android.app.usage.UsageStats> queryAndAggregateUsageStats(long, long);
    method public java.util.List<android.app.usage.ConfigurationStats> queryConfigurations(int, long, long);
@@ -7219,6 +7220,10 @@ package android.app.usage {
    field public static final int INTERVAL_MONTHLY = 2; // 0x2
    field public static final int INTERVAL_WEEKLY = 1; // 0x1
    field public static final int INTERVAL_YEARLY = 3; // 0x3
    field public static final int STANDBY_BUCKET_ACTIVE = 10; // 0xa
    field public static final int STANDBY_BUCKET_FREQUENT = 30; // 0x1e
    field public static final int STANDBY_BUCKET_RARE = 40; // 0x28
    field public static final int STANDBY_BUCKET_WORKING_SET = 20; // 0x14
  }
}
+3 −0
Original line number Diff line number Diff line
@@ -609,8 +609,11 @@ package android.app.usage {
  }

  public final class UsageStatsManager {
    method public int getAppStandbyBucket(java.lang.String);
    method public void setAppStandbyBucket(java.lang.String, int);
    method public void whitelistAppTemporarily(java.lang.String, long, android.os.UserHandle);
    field public static final int STANDBY_BUCKET_EXEMPTED = 5; // 0x5
    field public static final int STANDBY_BUCKET_NEVER = 50; // 0x32
  }

}
+0 −83
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app.usage;

import android.annotation.IntDef;

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

/**
 * Set of constants for app standby buckets and reasons. Apps will be moved into different buckets
 * that affect how frequently they can run in the background or perform other battery-consuming
 * actions. Buckets will be assigned based on how frequently or when the system thinks the user
 * is likely to use the app.
 * @hide
 */
public class AppStandby {

    /** The app was used very recently, currently in use or likely to be used very soon. */
    public static final int STANDBY_BUCKET_ACTIVE = 0;

    // Leave some gap in case we want to increase the number of buckets

    /** The app was used recently and/or likely to be used in the next few hours  */
    public static final int STANDBY_BUCKET_WORKING_SET = 3;

    // Leave some gap in case we want to increase the number of buckets

    /** The app was used in the last few days and/or likely to be used in the next few days */
    public static final int STANDBY_BUCKET_FREQUENT = 6;

    // Leave some gap in case we want to increase the number of buckets

    /** The app has not be used for several days and/or is unlikely to be used for several days */
    public static final int STANDBY_BUCKET_RARE = 9;

    // Leave some gap in case we want to increase the number of buckets

    /** The app has never been used. */
    public static final int STANDBY_BUCKET_NEVER = 12;

    /** Reason for bucketing -- default initial state */
    public static final String REASON_DEFAULT = "default";

    /** Reason for bucketing -- timeout */
    public static final String REASON_TIMEOUT = "timeout";

    /** Reason for bucketing -- usage */
    public static final String REASON_USAGE = "usage";

    /** Reason for bucketing -- forced by user / shell command */
    public static final String REASON_FORCED = "forced";

    /**
     * Reason for bucketing -- predicted. This is a prefix and the UID of the bucketeer will
     * be appended.
     */
    public static final String REASON_PREDICTED = "predicted";

    @IntDef(flag = false, value = {
            STANDBY_BUCKET_ACTIVE,
            STANDBY_BUCKET_WORKING_SET,
            STANDBY_BUCKET_FREQUENT,
            STANDBY_BUCKET_RARE,
            STANDBY_BUCKET_NEVER,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface StandbyBuckets {}
}
+103 −5
Original line number Diff line number Diff line
@@ -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, 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,19 +320,45 @@ 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.
     * 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.
     */
    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
     * {@hide}
     * Changes the app standby state to the provided bucket.
     */
    @SystemApi
+1 −1
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@

package android.app.usage;

import android.app.usage.AppStandby.StandbyBuckets;
import android.app.usage.UsageStatsManager.StandbyBuckets;
import android.content.ComponentName;
import android.content.res.Configuration;

Loading