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

Commit 21809593 authored by TYM Tsai's avatar TYM Tsai Committed by Android (Google) Code Review
Browse files

Merge "Add adb command of "get-distracting-restriction""

parents 0af86aa0 88a86b20
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -1383,4 +1383,17 @@ public abstract class PackageManagerInternal {
    @Deprecated
    public abstract void legacyReconcileSecondaryDexFiles(String packageName)
            throws LegacyDexoptDisabledException;

    /**
     * Gets {@link PackageManager.DistractionRestriction restrictions} of the given
     * packages of the given user.
     *
     * The corresponding element of the resulting array will be -1 if a given package doesn't exist.
     *
     * @param packageNames The packages under which to check.
     * @param userId The user under which to check.
     * @return an array of distracting restriction state in order of the given packages
     */
    public abstract int[] getDistractingPackageRestrictionsAsUser(
            @NonNull String[] packageNames, int userId);
}
+34 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import com.android.internal.util.ArrayUtils;
import com.android.server.pm.pkg.PackageStateInternal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
@@ -133,6 +134,39 @@ public final class DistractingPackageHelper {
        return unactionedPackages.toArray(new String[0]);
    }


    /**
     * Gets {@link DistractionRestriction restrictions} of the given packages of the given user.
     *
     * The corresponding element of the resulting array will be -1 if a given package doesn't exist.
     *
     * @param packageNames The packages under which to check.
     * @param userId The user under which to check.
     * @return an array of distracting restriction state in order of the given packages
     */
    int[] getDistractingPackageRestrictionsAsUser(@NonNull Computer snapshot,
            @NonNull String[] packageNames, int userId, int callingUid) {
        int[] res = new int[packageNames.length];
        Arrays.fill(res, -1);

        if (ArrayUtils.isEmpty(packageNames)) {
            return res;
        }

        for (int i = 0; i < packageNames.length; i++) {
            final String packageName = packageNames[i];
            final PackageStateInternal packageState =
                    snapshot.getPackageStateForInstalledAndFiltered(
                            packageName, callingUid, userId);
            if (packageState == null) {
                continue;
            }

            res[i] = packageState.getUserStateOrDefault(userId).getDistractionFlags();
        }
        return res;
    }

    /**
     * Removes any {@link DistractionRestriction restrictions} set on given packages.
     *
+10 −0
Original line number Diff line number Diff line
@@ -6793,6 +6793,16 @@ public class PackageManagerService implements PackageSender, TestUtilityService
            mHandler.post(() -> PackageManagerService.this.notifyInstallObserver(packageName,
                    true /* killApp */));
        }

        @Override
        public int[] getDistractingPackageRestrictionsAsUser(
                @NonNull String[] packageNames, int userId) {
            final int callingUid = Binder.getCallingUid();
            final Computer snapshot = snapshotComputer();
            Objects.requireNonNull(packageNames, "packageNames cannot be null");
            return mDistractingPackageHelper.getDistractingPackageRestrictionsAsUser(snapshot,
                    packageNames, userId, callingUid);
        }
    }

    private void setEnabledOverlayPackages(@UserIdInt int userId,
+60 −0
Original line number Diff line number Diff line
@@ -23,6 +23,9 @@ import static android.content.pm.PackageManager.FLAG_PERMISSION_REVOKED_COMPAT;
import static android.content.pm.PackageManager.FLAG_PERMISSION_REVOKE_WHEN_REQUESTED;
import static android.content.pm.PackageManager.FLAG_PERMISSION_USER_FIXED;
import static android.content.pm.PackageManager.FLAG_PERMISSION_USER_SET;
import static android.content.pm.PackageManager.RESTRICTION_HIDE_FROM_SUGGESTIONS;
import static android.content.pm.PackageManager.RESTRICTION_HIDE_NOTIFICATIONS;
import static android.content.pm.PackageManager.RESTRICTION_NONE;

import static com.android.server.LocalManagerRegistry.ManagerNotFoundException;
import static com.android.server.pm.PackageManagerService.PLATFORM_PACKAGE_NAME;
@@ -290,6 +293,8 @@ class PackageManagerShellCommand extends ShellCommand {
                    return runSuspend(false);
                case "set-distracting-restriction":
                    return runSetDistractingRestriction();
                case "get-distracting-restriction":
                    return runGetDistractingRestriction();
                case "grant":
                    return runGrantRevokePermission(true);
                case "revoke":
@@ -2585,6 +2590,58 @@ class PackageManagerShellCommand extends ShellCommand {
        }
    }

    private int runGetDistractingRestriction() {
        final PrintWriter pw = getOutPrintWriter();
        int userId = UserHandle.USER_SYSTEM;
        String opt;
        while ((opt = getNextOption()) != null) {
            switch (opt) {
                case "--user":
                    userId = UserHandle.parseUserArg(getNextArgRequired());
                    break;
                default:
                    pw.println("Error: Unknown option: " + opt);
                    return 1;
            }
        }

        final List<String> packageNames = getRemainingArgs();
        if (packageNames.isEmpty()) {
            pw.println("Error: package name not specified");
            return 1;
        }
        pw.println("Distracting restrictions state for user " + userId);

        final int translatedUserId = translateUserId(userId, UserHandle.USER_NULL,
                "get-distracting");
        final String[] packages = packageNames.toArray(new String[]{});
        int[] res = mPm.getDistractingPackageRestrictionsAsUser(packages, translatedUserId);

        for (int i = 0; i < res.length; i++) {
            final int state = res[i];
            if (state == -1) {
                pw.println(packages[i] + " not found ...");
            } else {
                pw.println(packages[i] + "  state: " + stateToString(state));
            }
        }

        return 0;
    }

    private static String stateToString(@PackageManager.DistractionRestriction int flag) {
        switch (flag) {
            case RESTRICTION_NONE:
                return "NONE";
            case RESTRICTION_HIDE_FROM_SUGGESTIONS:
                return "HIDE_FROM_SUGGESTIONS";
            case RESTRICTION_HIDE_NOTIFICATIONS:
                return "HIDE_NOTIFICATIONS";
            default:
                return "UNKNOWN";
        }
    }

    private int runSuspend(boolean suspendedState) {
        final PrintWriter pw = getOutPrintWriter();
        int userId = UserHandle.USER_SYSTEM;
@@ -4398,6 +4455,9 @@ class PackageManagerShellCommand extends ShellCommand {
        pw.println("    Any existing flags are overwritten, which also means that if no flags are");
        pw.println("    specified then all existing flags will be cleared.");
        pw.println("");
        pw.println("  get-distracting-restriction [--user USER_ID] PACKAGE [PACKAGE...]");
        pw.println("    Gets the specified restriction flags of given package(s) (of the user).");
        pw.println("");
        pw.println("  grant [--user USER_ID] PACKAGE PERMISSION");
        pw.println("  revoke [--user USER_ID] PACKAGE PERMISSION");
        pw.println("    These commands either grant or revoke permissions to apps.  The permissions");