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

Commit f727200c authored by Amith Yamasani's avatar Amith Yamasani Committed by Android (Google) Code Review
Browse files

Merge "Add a source parameter for resrictions logging" into main

parents 2e13939d 38060549
Loading
Loading
Loading
Loading
+71 −38
Original line number Diff line number Diff line
@@ -1400,18 +1400,10 @@ public class ActivityManager {
     */
    public static final int RESTRICTION_SUBREASON_MAX_LENGTH = 16;

    /**
     * Restriction reason unknown - do not use directly.
     *
     * For use with noteAppRestrictionEnabled()
     * @hide
     */
    public static final int RESTRICTION_REASON_UNKNOWN = 0;

    /**
     * Restriction reason to be used when this is normal behavior for the state.
     *
     * For use with noteAppRestrictionEnabled()
     * @see #noteAppRestrictionEnabled(String, int, int, boolean, int, String, int, long)
     * @hide
     */
    public static final int RESTRICTION_REASON_DEFAULT = 1;
@@ -1420,7 +1412,7 @@ public class ActivityManager {
     * Restriction reason is some kind of timeout that moves the app to a more restricted state.
     * The threshold should specify how long the app was dormant, in milliseconds.
     *
     * For use with noteAppRestrictionEnabled()
     * @see #noteAppRestrictionEnabled(String, int, int, boolean, int, String, int, long)
     * @hide
     */
    public static final int RESTRICTION_REASON_DORMANT = 2;
@@ -1429,7 +1421,7 @@ public class ActivityManager {
     * Restriction reason to be used when removing a restriction due to direct or indirect usage
     * of the app, especially to undo any automatic restrictions.
     *
     * For use with noteAppRestrictionEnabled()
     * @see #noteAppRestrictionEnabled(String, int, int, boolean, int, String, int, long)
     * @hide
     */
    public static final int RESTRICTION_REASON_USAGE = 3;
@@ -1438,63 +1430,102 @@ public class ActivityManager {
     * Restriction reason to be used when the user chooses to manually restrict the app, through
     * UI or command line interface.
     *
     * For use with noteAppRestrictionEnabled()
     * @see #noteAppRestrictionEnabled(String, int, int, boolean, int, String, int, long)
     * @hide
     */
    public static final int RESTRICTION_REASON_USER = 4;

    /**
     * Restriction reason to be used when the user chooses to manually restrict the app on being
     * prompted by the OS or some anomaly detection algorithm. For example, if the app is causing
     * high battery drain or affecting system performance and the OS recommends that the user
     * restrict the app.
     *
     * For use with noteAppRestrictionEnabled()
     * @hide
     */
    public static final int RESTRICTION_REASON_USER_NUDGED = 5;

    /**
     * Restriction reason to be used when the OS automatically detects that the app is causing
     * system health issues such as performance degradation, battery drain, high memory usage, etc.
     *
     * For use with noteAppRestrictionEnabled()
     * @see #noteAppRestrictionEnabled(String, int, int, boolean, int, String, int, long)
     * @hide
     */
    public static final int RESTRICTION_REASON_SYSTEM_HEALTH = 6;
    public static final int RESTRICTION_REASON_SYSTEM_HEALTH = 5;

    /**
     * Restriction reason to be used when there is a server-side decision made to restrict an app
     * that is showing widespread problems on user devices, or violating policy in some way.
     * Restriction reason to be used when app is doing something that is against policy, such as
     * spamming the user or being deceptive about its intentions.
     *
     * For use with noteAppRestrictionEnabled()
     * @see #noteAppRestrictionEnabled(String, int, int, boolean, int, String, int, long)
     * @hide
     */
    public static final int RESTRICTION_REASON_REMOTE_TRIGGER = 7;
    public static final int RESTRICTION_REASON_POLICY = 6;

    /**
     * Restriction reason to be used when some other problem requires restricting the app.
     *
     * For use with noteAppRestrictionEnabled()
     * @see #noteAppRestrictionEnabled(String, int, int, boolean, int, String, int, long)
     * @hide
     */
    public static final int RESTRICTION_REASON_OTHER = 8;
    public static final int RESTRICTION_REASON_OTHER = 7;

    /** @hide */
    @IntDef(prefix = { "RESTRICTION_REASON_" }, value = {
            RESTRICTION_REASON_UNKNOWN,
            RESTRICTION_REASON_DEFAULT,
            RESTRICTION_REASON_DORMANT,
            RESTRICTION_REASON_USAGE,
            RESTRICTION_REASON_USER,
            RESTRICTION_REASON_USER_NUDGED,
            RESTRICTION_REASON_SYSTEM_HEALTH,
            RESTRICTION_REASON_REMOTE_TRIGGER,
            RESTRICTION_REASON_POLICY,
            RESTRICTION_REASON_OTHER
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface RestrictionReason{}

    /**
     * The source of restriction is the user manually choosing to do so.
     *
     * @see #noteAppRestrictionEnabled(String, int, int, boolean, int, String, int, long)
     * @hide
     */
    public static final int RESTRICTION_SOURCE_USER = 1;

    /**
     * The source of restriction is the user, on being prompted by the system for the specified
     * reason.
     *
     * @see #noteAppRestrictionEnabled(String, int, int, boolean, int, String, int, long)
     * @hide
     */
    public static final int RESTRICTION_SOURCE_USER_NUDGED = 2;

    /**
     * The source of restriction is the system.
     *
     * @see #noteAppRestrictionEnabled(String, int, int, boolean, int, String, int, long)
     * @hide
     */
    public static final int RESTRICTION_SOURCE_SYSTEM = 3;

    /**
     * The source of restriction is the command line interface through the shell or a test.
     *
     * @see #noteAppRestrictionEnabled(String, int, int, boolean, int, String, int, long)
     * @hide
     */
    public static final int RESTRICTION_SOURCE_COMMAND_LINE = 4;

    /**
     * The source of restriction is a configuration pushed from a server.
     *
     * @see #noteAppRestrictionEnabled(String, int, int, boolean, int, String, int, long)
     * @hide
     */
    public static final int RESTRICTION_SOURCE_REMOTE_TRIGGER = 5;

    /** @hide */
    @IntDef(prefix = { "RESTRICTION_SOURCE_" }, value = {
            RESTRICTION_SOURCE_USER,
            RESTRICTION_SOURCE_USER_NUDGED,
            RESTRICTION_SOURCE_SYSTEM,
            RESTRICTION_SOURCE_COMMAND_LINE,
            RESTRICTION_SOURCE_REMOTE_TRIGGER,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface RestrictionSource{}

    /** @hide */
    @android.ravenwood.annotation.RavenwoodKeep
    public static String restrictionLevelToName(@RestrictionLevel int level) {
@@ -6254,7 +6285,7 @@ public class ActivityManager {
     * <p>
     * The {@code enabled} value determines whether the state is being applied or removed.
     * Not all restrictions are actual restrictions. For example,
     * {@link #RESTRICTION_LEVEL_ADAPTIVE} is a normal state, where there is default lifecycle
     * {@link #RESTRICTION_LEVEL_ADAPTIVE_BUCKET} is a normal state, where there is default lifecycle
     * management applied to the app. Also, {@link #RESTRICTION_LEVEL_EXEMPTED} is used when the
     * app is being put in a power-save allowlist.
     * <p>
@@ -6267,6 +6298,7 @@ public class ActivityManager {
     *     true,
     *     RESTRICTION_REASON_USER,
     *     "settings",
     *     RESTRICTION_SOURCE_USER,
     *     0);
     * </pre>
     * Example arguments when app is put in restricted standby bucket for exceeding X hours of jobs:
@@ -6278,6 +6310,7 @@ public class ActivityManager {
     *     true,
     *     RESTRICTION_REASON_SYSTEM_HEALTH,
     *     "job_duration",
     *     RESTRICTION_SOURCE_SYSTEM,
     *     X * 3600 * 1000L);
     * </pre>
     *
@@ -6295,7 +6328,7 @@ public class ActivityManager {
     *                  Examples of system resource usage: wakelock, wakeups, mobile_data,
     *                  binder_calls, memory, excessive_threads, excessive_cpu, gps_scans, etc.
     *                  Examples of user actions: settings, notification, command_line, launch, etc.
     *
     * @param source the source of the action, from {@code RestrictionSource}
     * @param threshold for reasons that are due to exceeding some threshold, the threshold value
     *                  must be specified. The unit of the threshold depends on the reason and/or
     *                  subReason. For time, use milliseconds. For memory, use KB. For count, use
@@ -6308,10 +6341,10 @@ public class ActivityManager {
    public void noteAppRestrictionEnabled(@NonNull String packageName, int uid,
            @RestrictionLevel int restrictionLevel, boolean enabled,
            @RestrictionReason int reason,
            @Nullable String subReason, long threshold) {
            @Nullable String subReason, @RestrictionSource int source, long threshold) {
        try {
            getService().noteAppRestrictionEnabled(packageName, uid, restrictionLevel, enabled,
                    reason, subReason, threshold);
                    reason, subReason, source, threshold);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
+1 −1
Original line number Diff line number Diff line
@@ -1016,5 +1016,5 @@ interface IActivityManager {
     */
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(android.Manifest.permission.DEVICE_POWER)")
    void noteAppRestrictionEnabled(in String packageName, int uid, int restrictionType,
            boolean enabled, int reason, in String subReason, long threshold);
            boolean enabled, int reason, in String subReason, int source, long threshold);
}
+2 −2
Original line number Diff line number Diff line
@@ -210,7 +210,7 @@ public class PowerAllowlistBackend {
                    mAppContext.getSystemService(ActivityManager.class).noteAppRestrictionEnabled(
                            pkg, uid, ActivityManager.RESTRICTION_LEVEL_EXEMPTED,
                            true, ActivityManager.RESTRICTION_REASON_USER,
                            "settings", 0);
                            "settings", ActivityManager.RESTRICTION_SOURCE_USER, 0);
                }
            }

@@ -251,7 +251,7 @@ public class PowerAllowlistBackend {
                    mAppContext.getSystemService(ActivityManager.class).noteAppRestrictionEnabled(
                            pkg, uid, ActivityManager.RESTRICTION_LEVEL_EXEMPTED,
                            false, ActivityManager.RESTRICTION_REASON_USER,
                            "settings", 0);
                            "settings", ActivityManager.RESTRICTION_SOURCE_USER, 0L);
                }
            }

+10 −5
Original line number Diff line number Diff line
@@ -40,6 +40,8 @@ import static android.app.ActivityManager.PROCESS_STATE_TOP;
import static android.app.ActivityManager.RESTRICTION_LEVEL_FORCE_STOPPED;
import static android.app.ActivityManager.RESTRICTION_REASON_DEFAULT;
import static android.app.ActivityManager.RESTRICTION_REASON_USAGE;
import static android.app.ActivityManager.RESTRICTION_SOURCE_SYSTEM;
import static android.app.ActivityManager.RESTRICTION_SOURCE_USER;
import static android.app.ActivityManager.StopUserOnSwitch;
import static android.app.ActivityManager.UidFrozenStateChangedCallback.UID_FROZEN_STATE_FROZEN;
import static android.app.ActivityManager.UidFrozenStateChangedCallback.UID_FROZEN_STATE_UNFROZEN;
@@ -5148,7 +5150,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        if (android.app.Flags.appRestrictionsApi() && wasForceStopped) {
            noteAppRestrictionEnabled(app.info.packageName, app.uid,
                    RESTRICTION_LEVEL_FORCE_STOPPED, false,
                    RESTRICTION_REASON_USAGE, "unknown", 0L);
                    RESTRICTION_REASON_USAGE, "unknown", RESTRICTION_SOURCE_USER, 0L);
        }
        if (!sendBroadcast) {
@@ -14399,7 +14401,8 @@ public class ActivityManagerService extends IActivityManager.Stub
                    if (wasStopped) {
                        noteAppRestrictionEnabled(app.packageName, app.uid,
                                RESTRICTION_LEVEL_FORCE_STOPPED, false,
                                RESTRICTION_REASON_DEFAULT, "restore", 0L);
                                RESTRICTION_REASON_DEFAULT, "restore",
                                RESTRICTION_SOURCE_SYSTEM, 0L);
                    }
                } catch (NameNotFoundException e) {
                    Slog.w(TAG, "No such package", e);
@@ -20316,12 +20319,14 @@ public class ActivityManagerService extends IActivityManager.Stub
     * Log the reason for changing an app restriction. Purely used for logging purposes and does not
     * cause any change to app state.
     *
     * @see ActivityManager#noteAppRestrictionEnabled(String, int, int, boolean, int, String, long)
     * @see ActivityManager#noteAppRestrictionEnabled(String, int, int, boolean, int,
     *          String, int, long)
     */
    @Override
    public void noteAppRestrictionEnabled(String packageName, int uid,
            @RestrictionLevel int restrictionType, boolean enabled,
            @ActivityManager.RestrictionReason int reason, String subReason, long threshold) {
            @ActivityManager.RestrictionReason int reason, String subReason,
            @ActivityManager.RestrictionSource int source, long threshold) {
        if (!android.app.Flags.appRestrictionsApi()) return;
        enforceCallingPermission(android.Manifest.permission.DEVICE_POWER,
@@ -20334,7 +20339,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                uid = mPackageManagerInt.getPackageUid(packageName, 0, userId);
            }
            mAppRestrictionController.noteAppRestrictionEnabled(packageName, uid, restrictionType,
                    enabled, reason, subReason, threshold);
                    enabled, reason, subReason, source, threshold);
        } finally {
            Binder.restoreCallingIdentity(callingId);
        }
+8 −8
Original line number Diff line number Diff line
@@ -31,11 +31,10 @@ import static android.app.ActivityManager.RESTRICTION_LEVEL_UNRESTRICTED;
import static android.app.ActivityManager.RESTRICTION_LEVEL_USER_LAUNCH_ONLY;
import static android.app.ActivityManager.RESTRICTION_REASON_DEFAULT;
import static android.app.ActivityManager.RESTRICTION_REASON_DORMANT;
import static android.app.ActivityManager.RESTRICTION_REASON_REMOTE_TRIGGER;
import static android.app.ActivityManager.RESTRICTION_REASON_POLICY;
import static android.app.ActivityManager.RESTRICTION_REASON_SYSTEM_HEALTH;
import static android.app.ActivityManager.RESTRICTION_REASON_USAGE;
import static android.app.ActivityManager.RESTRICTION_REASON_USER;
import static android.app.ActivityManager.RESTRICTION_REASON_USER_NUDGED;
import static android.app.ActivityManager.RESTRICTION_SUBREASON_MAX_LENGTH;
import static android.app.ActivityManager.UID_OBSERVER_ACTIVE;
import static android.app.ActivityManager.UID_OBSERVER_GONE;
@@ -103,6 +102,7 @@ import android.annotation.UserIdInt;
import android.app.ActivityManager;
import android.app.ActivityManager.RestrictionLevel;
import android.app.ActivityManager.RestrictionReason;
import android.app.ActivityManager.RestrictionSource;
import android.app.ActivityManagerInternal;
import android.app.ActivityManagerInternal.AppBackgroundRestrictionListener;
import android.app.AppOpsManager;
@@ -2378,7 +2378,8 @@ public final class AppRestrictionController {
     */
    public void noteAppRestrictionEnabled(String packageName, int uid,
            @RestrictionLevel int restrictionType, boolean enabled,
            @RestrictionReason int reason, String subReason, long threshold) {
            @RestrictionReason int reason, String subReason, @RestrictionSource int source,
            long threshold) {
        if (DEBUG_BG_RESTRICTION_CONTROLLER) {
            Slog.i(TAG, (enabled ? "restricted " : "unrestricted ") + packageName + " to "
                    + restrictionType + " reason=" + reason + ", subReason=" + subReason
@@ -2397,7 +2398,8 @@ public final class AppRestrictionController {
                enabled,
                getRestrictionChangeReasonStatsd(reason, subReason),
                subReason,
                threshold);
                threshold,
                source);
    }

    private int getRestrictionTypeStatsd(@RestrictionLevel int level) {
@@ -2433,12 +2435,10 @@ public final class AppRestrictionController {
                    FrameworkStatsLog.APP_RESTRICTION_STATE_CHANGED__MAIN_REASON__REASON_USAGE;
            case RESTRICTION_REASON_USER ->
                    FrameworkStatsLog.APP_RESTRICTION_STATE_CHANGED__MAIN_REASON__REASON_USER;
            case RESTRICTION_REASON_USER_NUDGED ->
                    FrameworkStatsLog.APP_RESTRICTION_STATE_CHANGED__MAIN_REASON__REASON_USER_NUDGED;
            case RESTRICTION_REASON_SYSTEM_HEALTH ->
                    FrameworkStatsLog.APP_RESTRICTION_STATE_CHANGED__MAIN_REASON__REASON_SYSTEM_HEALTH;
            case RESTRICTION_REASON_REMOTE_TRIGGER ->
                    FrameworkStatsLog.APP_RESTRICTION_STATE_CHANGED__MAIN_REASON__REASON_REMOTE_TRIGGER;
            case RESTRICTION_REASON_POLICY ->
                    FrameworkStatsLog.APP_RESTRICTION_STATE_CHANGED__MAIN_REASON__REASON_POLICY;
            default ->
                    FrameworkStatsLog.APP_RESTRICTION_STATE_CHANGED__MAIN_REASON__REASON_OTHER;
        };
Loading