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

Commit 72588ae7 authored by Hui Yu's avatar Hui Yu Committed by Android (Google) Code Review
Browse files

Merge "Add shell am command to set and get app's background restriction level."

parents d2d29b3e 1c5ef039
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
@@ -17847,6 +17847,47 @@ public class ActivityManagerService extends IActivityManager.Stub
        return mAppRestrictionController.getBackgroundRestrictionExemptionReason(uid);
    }
    /**
     * Set an app's background restriction level.
     * This interface is intended for the shell command to use.
     */
    void setBackgroundRestrictionLevel(String packageName, int uid, int userId,
            @RestrictionLevel int level, int reason, int subReason) {
        final int callingUid = Binder.getCallingUid();
        if (callingUid != SYSTEM_UID && callingUid != ROOT_UID && callingUid != SHELL_UID) {
            throw new SecurityException(
                    "No permission to change app restriction level");
        }
        final long callingId = Binder.clearCallingIdentity();
        try {
            final int curBucket = mUsageStatsService.getAppStandbyBucket(packageName, userId,
                    SystemClock.elapsedRealtime());
            mAppRestrictionController.applyRestrictionLevel(packageName, uid, level,
                    null /* trackerInfo */, curBucket, true /* allowUpdateBucket */,
                    reason, subReason);
        } finally {
            Binder.restoreCallingIdentity(callingId);
        }
    }
    /**
     * Get an app's background restriction level.
     * This interface is intended for the shell command to use.
     */
    @RestrictionLevel int getBackgroundRestrictionLevel(String packageName, int userId) {
        final int callingUid = Binder.getCallingUid();
        if (callingUid != SYSTEM_UID && callingUid != ROOT_UID && callingUid != SHELL_UID) {
            throw new SecurityException(
                    "Don't have permission to query app background restriction level");
        }
        final long callingId = Binder.clearCallingIdentity();
        try {
            return mInternal.getRestrictionLevel(packageName, userId);
        } finally {
            Binder.restoreCallingIdentity(callingId);
        }
    }
    /**
     * Force the settings cache to be loaded
     */
+85 −0
Original line number Diff line number Diff line
@@ -23,6 +23,10 @@ import static android.app.ActivityTaskManager.RESIZE_MODE_USER;
import static android.app.WaitResult.launchStateToString;
import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
import static android.app.usage.UsageStatsManager.REASON_MAIN_FORCED_BY_USER;
import static android.app.usage.UsageStatsManager.REASON_SUB_FORCED_SYSTEM_FLAG_UNDEFINED;
import static android.content.pm.PackageManager.MATCH_ANY_USER;
import static android.os.Process.INVALID_UID;
import static android.view.Display.INVALID_DISPLAY;
import static android.window.DisplayAreaOrganizer.FEATURE_UNDEFINED;

@@ -350,6 +354,10 @@ final class ActivityManagerShellCommand extends ShellCommand {
                    return runSetBgAbusiveUids(pw);
                case "list-bg-exemptions-config":
                    return runListBgExemptionsConfig(pw);
                case "set-bg-restriction-level":
                    return runSetBgRestrictionLevel(pw);
                case "get-bg-restriction-level":
                    return runGetBgRestrictionLevel(pw);
                default:
                    return handleDefaultCommands(cmd);
            }
@@ -3374,6 +3382,79 @@ final class ActivityManagerShellCommand extends ShellCommand {
        return 0;
    }

    private @ActivityManager.RestrictionLevel int restrictionNameToLevel(String name) {
        String lower = name.toLowerCase();
        switch (lower) {
            case "unrestricted":
                return ActivityManager.RESTRICTION_LEVEL_UNRESTRICTED;
            case "exempted":
                return ActivityManager.RESTRICTION_LEVEL_EXEMPTED;
            case "adaptive_bucket":
                return ActivityManager.RESTRICTION_LEVEL_ADAPTIVE_BUCKET;
            case "restricted_bucket":
                return ActivityManager.RESTRICTION_LEVEL_RESTRICTED_BUCKET;
            case "background_restricted":
                return ActivityManager.RESTRICTION_LEVEL_BACKGROUND_RESTRICTED;
            case "hibernation":
                return ActivityManager.RESTRICTION_LEVEL_HIBERNATION;
            default:
                return ActivityManager.RESTRICTION_LEVEL_UNKNOWN;
        }
    }

    int runSetBgRestrictionLevel(PrintWriter pw) throws RemoteException {
        int userId = UserHandle.USER_CURRENT;

        String opt;
        while ((opt = getNextOption()) != null) {
            if (opt.equals("--user")) {
                userId = UserHandle.parseUserArg(getNextArgRequired());
            } else {
                getErrPrintWriter().println("Error: Unknown option: " + opt);
                return -1;
            }
        }
        String packageName = getNextArgRequired();
        final String value = getNextArgRequired();
        final int level = restrictionNameToLevel(value);
        if (level == ActivityManager.RESTRICTION_LEVEL_UNKNOWN) {
            pw.println("Error: invalid restriction level");
            return -1;
        }

        int uid = INVALID_UID;
        try {
            final PackageManager pm = mInternal.mContext.getPackageManager();
            uid = pm.getPackageUidAsUser(packageName,
                    PackageManager.PackageInfoFlags.of(MATCH_ANY_USER), userId);
        } catch (PackageManager.NameNotFoundException e) {
            pw.println("Error: userId:" + userId + " package:" + packageName + " is not found");
            return -1;
        }
        mInternal.setBackgroundRestrictionLevel(packageName, uid, userId, level,
                REASON_MAIN_FORCED_BY_USER, REASON_SUB_FORCED_SYSTEM_FLAG_UNDEFINED);
        return 0;
    }

    int runGetBgRestrictionLevel(PrintWriter pw) throws RemoteException {
        int userId = UserHandle.USER_CURRENT;

        String opt;
        while ((opt = getNextOption()) != null) {
            if (opt.equals("--user")) {
                userId = UserHandle.parseUserArg(getNextArgRequired());
            } else {
                getErrPrintWriter().println("Error: Unknown option: " + opt);
                return -1;
            }
        }
        final String packageName = getNextArgRequired();
        final @ActivityManager.RestrictionLevel int level =
                mInternal.getBackgroundRestrictionLevel(packageName, userId);
        pw.println(ActivityManager.restrictionLevelToName(level));
        return 0;
    }

    private Resources getResources(PrintWriter pw) throws RemoteException {
        // system resources does not contain all the device configuration, construct it manually.
        Configuration config = mInterface.getConfiguration();
@@ -3726,6 +3807,10 @@ final class ActivityManagerShellCommand extends ShellCommand {
            pw.println("         Without arguments, it resets to the value defined by platform.");
            pw.println("  set-bg-abusive-uids [uid=percentage][,uid=percentage...]");
            pw.println("         Force setting the battery usage of the given UID.");
            pw.println("  set-bg-restriction-level [--user <USER_ID>] <PACKAGE> unrestricted|exempted|adaptive_bucket|restricted_bucket|background_restricted|hibernation");
            pw.println("         Set an app's background restriction level which in turn map to a app standby bucket.");
            pw.println("  get-bg-restriction-level [--user <USER_ID>] <PACKAGE>");
            pw.println("         Get an app's background restriction level.");
            pw.println();
            Intent.printIntentArgsHelp(pw, "");
        }
+1 −1
Original line number Diff line number Diff line
@@ -2151,7 +2151,7 @@ public final class AppRestrictionController {
        return AppBackgroundRestrictionsInfo.SDK_UNKNOWN;
    }

    private void applyRestrictionLevel(String pkgName, int uid,
    void applyRestrictionLevel(String pkgName, int uid,
            @RestrictionLevel int level, TrackerInfo trackerInfo,
            int curBucket, boolean allowUpdateBucket, int reason, int subReason) {
        int curLevel;