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

Commit 5c1799e5 authored by Atneya Nair's avatar Atneya Nair
Browse files

[appops] Add batched audio restriction API

Audio restrictions are enabled over sets of usages. To avoid callback
floods, add a new API which takes an array of usages rather than a
single usage to migrate the main client (Zen).

Test: compiles
Flag: EXEMPT refactor
Bug: 415378815
Change-Id: I02823ea12cc1ea3a35577d8198eaf24fac6193e8
parent 1a81a01d
Loading
Loading
Loading
Loading
+30 −6
Original line number Diff line number Diff line
@@ -8739,24 +8739,48 @@ public class AppOpsManager {
     * Restrictions are temporary additional constraints imposed on top of the persisted rules
     * defined by {@link #setMode}.
     *
     * Audio restrictions are keyed by code/usage pairs (i.e. OP_PLAY_AUDIO, USAGE_MEDIA), and have
     * a value of a MODE and a set of exempted packages, and apply globally.
     *
     * @param code The operation to restrict.
     * @param usage The {@link android.media.AudioAttributes} usage value.
     * @param mode The restriction mode (MODE_IGNORED,MODE_ERRORED) or MODE_ALLOWED to unrestrict.
     * @param usages A set of {@link android.media.AudioAttributes} usage values to apply
     * restrictions to, overriding any restriction currently in place for that usage.
     * @param mode The restriction mode (MODE_IGNORED,MODE_ERRORED) or MODE_ALLOWED to unrestrict
     * (see key above).
     * @param exceptionPackages Optional list of packages to exclude from the restriction.
     * @hide
     */
    @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES)
    @UnsupportedAppUsage
    public void setRestriction(int code, @AttributeUsage int usage, @Mode int mode,
    public void setAudioRestriction(int code, @AttributeUsage int[] usages, @Mode int mode,
            String[] exceptionPackages) {
        try {
            final int uid = Binder.getCallingUid();
            mService.setAudioRestriction(code, usage, uid, mode, exceptionPackages);
            mService.setAudioRestriction(code, usages, mode, exceptionPackages);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * See #setRestriction(int, int[], int, String[]).
     *
     * Included for compatibility
     *
     * @param code The operation to restrict.
     * @param usage The {@link android.media.AudioAttributes} usage value.
     * @param mode The restriction mode (MODE_IGNORED,MODE_ERRORED) or MODE_ALLOWED to unrestrict.
     * @param exceptionPackages Optional list of packages to exclude from the restriction.
     * @deprecated Use {@link #setAudioRestriction(int, int[], int, String[])} instead.
     * @hide
     */
    @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES)
    @Deprecated
    @UnsupportedAppUsage
    public void setRestriction(int code, @AttributeUsage int usage, @Mode int mode,
            String[] exceptionPackages) {
        setAudioRestriction(code, new int[] {usage}, mode, exceptionPackages);
    }


    /** @hide */
    @RequiresPermission(android.Manifest.permission.MANAGE_APP_OPS_MODES)
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
+1 −1
Original line number Diff line number Diff line
@@ -111,7 +111,7 @@ interface IAppOpsService {
    void setMode(int code, int uid, String packageName, int mode);
    @UnsupportedAppUsage(maxTargetSdk = 30, trackingBug = 170729553)
    void resetAllModes(int reqUserId, String reqPackageName);
    void setAudioRestriction(int code, int usage, int uid, int mode, in String[] exceptionPackages);
    void setAudioRestriction(int code, in int[] usages, int mode, in String[] exceptionPackages);

    void setUserRestrictions(in Bundle restrictions, IBinder token, int userHandle);
    void setUserRestriction(int code, boolean restricted, IBinder token, int userHandle, in PackageTagsList excludedPackageTags);
+7 −6
Original line number Diff line number Diff line
@@ -3146,14 +3146,15 @@ public class AppOpsService extends IAppOpsService.Stub {
    }

    @Override
    public void setAudioRestriction(int code, int usage, int uid, int mode,
            String[] exceptionPackages) {
        enforceManageAppOpsModes(Binder.getCallingPid(), Binder.getCallingUid(), uid);
        verifyIncomingUid(uid);
    public void setAudioRestriction(int code, int[] usages, int mode, String[] exceptionPackages) {
        // Audio restrictions apply to all UIDs
        enforceManageAppOpsModes(Binder.getCallingPid(), Binder.getCallingUid(), -1);
        verifyIncomingOp(code);

        for (int usage : usages) {
            mAudioRestrictionManager.setZenModeAudioRestriction(
                    code, usage, mode, exceptionPackages);
        }

        // Only notify default device as other devices are unaffected by restriction changes.
        mHandler.sendMessage(PooledLambda.obtainMessage(