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

Commit 051a20f9 authored by Matías Hernández's avatar Matías Hernández Committed by Android (Google) Code Review
Browse files

Merge "Fix user attribution of zen-related calls into NM/ZenModeHelper" into main

parents 6e52a4de 8b8e208b
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -65,6 +65,16 @@ flag {
  }
}

flag {
  name: "modes_multiuser"
  namespace: "systemui"
  description: "Fixes for modes (and DND/Zen in general) when callers are not the current user"
  bug: "323163267"
  metadata {
    purpose: PURPOSE_BUGFIX
  }
}

flag {
  name: "api_tvextender"
  is_exported: true
+61 −26
Original line number Diff line number Diff line
@@ -5683,14 +5683,16 @@ public class NotificationManagerService extends SystemService {
                final int zenMode = zenModeFromInterruptionFilter(interruptionFilter, -1);
                if (zenMode == -1) return;
                UserHandle zenUser = getCallingZenUser();
                if (!canManageGlobalZenPolicy(info.component.getPackageName(), callingUid)) {
                    mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(
                            info.component.getPackageName(), callingUid, zenMode);
                            zenUser, info.component.getPackageName(), callingUid, zenMode);
                } else {
                    int origin = computeZenOrigin(/* fromUser= */ false);
                    Binder.withCleanCallingIdentity(() -> {
                        mZenModeHelper.setManualZenMode(zenMode, /* conditionId= */ null, origin,
                                "listener:" + info.component.flattenToShortString(),
                        mZenModeHelper.setManualZenMode(zenUser, zenMode, /* conditionId= */ null,
                                origin, "listener:" + info.component.flattenToShortString(),
                                /* caller= */ info.component.getPackageName(),
                                callingUid);
                    });
@@ -5745,12 +5747,13 @@ public class NotificationManagerService extends SystemService {
        public void setZenMode(int mode, Uri conditionId, String reason, boolean fromUser) {
            enforceSystemOrSystemUI("INotificationManager.setZenMode");
            enforceUserOriginOnlyFromSystem(fromUser, "setZenMode");
            UserHandle zenUser = getCallingZenUser();
            final int callingUid = Binder.getCallingUid();
            final long identity = Binder.clearCallingIdentity();
            try {
                mZenModeHelper.setManualZenMode(mode, conditionId, computeZenOrigin(fromUser),
                        reason, /* caller= */ null, callingUid);
                mZenModeHelper.setManualZenMode(zenUser, mode, conditionId,
                        computeZenOrigin(fromUser), reason, /* caller= */ null, callingUid);
            } finally {
                Binder.restoreCallingIdentity(identity);
            }
@@ -5760,7 +5763,7 @@ public class NotificationManagerService extends SystemService {
        @Override
        public List<ZenModeConfig.ZenRule> getZenRules() throws RemoteException {
            enforcePolicyAccess(Binder.getCallingUid(), "getZenRules");
            return mZenModeHelper.getZenRules();
            return mZenModeHelper.getZenRules(getCallingZenUser());
        }
        @Override
@@ -5769,14 +5772,14 @@ public class NotificationManagerService extends SystemService {
                throw new IllegalStateException("getAutomaticZenRules called with flag off!");
            }
            enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRules");
            return mZenModeHelper.getAutomaticZenRules();
            return mZenModeHelper.getAutomaticZenRules(getCallingZenUser());
        }
        @Override
        public AutomaticZenRule getAutomaticZenRule(String id) throws RemoteException {
            Objects.requireNonNull(id, "Id is null");
            enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRule");
            return mZenModeHelper.getAutomaticZenRule(id);
            return mZenModeHelper.getAutomaticZenRule(getCallingZenUser(), id);
        }
        @Override
@@ -5791,6 +5794,7 @@ public class NotificationManagerService extends SystemService {
            }
            enforcePolicyAccess(Binder.getCallingUid(), "addAutomaticZenRule");
            enforceUserOriginOnlyFromSystem(fromUser, "addAutomaticZenRule");
            UserHandle zenUser = getCallingZenUser();
            // If the calling app is the system (from any user), take the package name from the
            // rule's owner rather than from the caller's package.
@@ -5801,16 +5805,18 @@ public class NotificationManagerService extends SystemService {
                }
            }
            return mZenModeHelper.addAutomaticZenRule(rulePkg, automaticZenRule,
            return mZenModeHelper.addAutomaticZenRule(zenUser, rulePkg, automaticZenRule,
                    computeZenOrigin(fromUser), "addAutomaticZenRule", Binder.getCallingUid());
        }
        @Override
        public void setManualZenRuleDeviceEffects(ZenDeviceEffects effects) throws RemoteException {
            checkCallerIsSystem();
            UserHandle zenUser = getCallingZenUser();
            mZenModeHelper.setManualZenRuleDeviceEffects(effects, computeZenOrigin(true),
                    "Update manual mode non-policy settings", Binder.getCallingUid());
            mZenModeHelper.setManualZenRuleDeviceEffects(zenUser, effects,
                    computeZenOrigin(true), "Update manual mode non-policy settings",
                    Binder.getCallingUid());
        }
        @Override
@@ -5819,8 +5825,9 @@ public class NotificationManagerService extends SystemService {
            validateAutomaticZenRule(id, automaticZenRule);
            enforcePolicyAccess(Binder.getCallingUid(), "updateAutomaticZenRule");
            enforceUserOriginOnlyFromSystem(fromUser, "updateAutomaticZenRule");
            UserHandle zenUser = getCallingZenUser();
            return mZenModeHelper.updateAutomaticZenRule(id, automaticZenRule,
            return mZenModeHelper.updateAutomaticZenRule(zenUser, id, automaticZenRule,
                    computeZenOrigin(fromUser), "updateAutomaticZenRule", Binder.getCallingUid());
        }
@@ -5886,8 +5893,9 @@ public class NotificationManagerService extends SystemService {
            // Verify that they can modify zen rules.
            enforcePolicyAccess(Binder.getCallingUid(), "removeAutomaticZenRule");
            enforceUserOriginOnlyFromSystem(fromUser, "removeAutomaticZenRule");
            UserHandle zenUser = getCallingZenUser();
            return mZenModeHelper.removeAutomaticZenRule(id, computeZenOrigin(fromUser),
            return mZenModeHelper.removeAutomaticZenRule(zenUser, id, computeZenOrigin(fromUser),
                    "removeAutomaticZenRule", Binder.getCallingUid());
        }
@@ -5897,9 +5905,11 @@ public class NotificationManagerService extends SystemService {
            Objects.requireNonNull(packageName, "Package name is null");
            enforceSystemOrSystemUI("removeAutomaticZenRules");
            enforceUserOriginOnlyFromSystem(fromUser, "removeAutomaticZenRules");
            UserHandle zenUser = getCallingZenUser();
            return mZenModeHelper.removeAutomaticZenRules(packageName, computeZenOrigin(fromUser),
                    packageName + "|removeAutomaticZenRules", Binder.getCallingUid());
            return mZenModeHelper.removeAutomaticZenRules(zenUser, packageName,
                    computeZenOrigin(fromUser), packageName + "|removeAutomaticZenRules",
                    Binder.getCallingUid());
        }
        @Override
@@ -5907,7 +5917,7 @@ public class NotificationManagerService extends SystemService {
            Objects.requireNonNull(owner, "Owner is null");
            enforceSystemOrSystemUI("getRuleInstanceCount");
            return mZenModeHelper.getCurrentInstanceCount(owner);
            return mZenModeHelper.getCurrentInstanceCount(getCallingZenUser(), owner);
        }
        @Override
@@ -5915,7 +5925,7 @@ public class NotificationManagerService extends SystemService {
        public int getAutomaticZenRuleState(@NonNull String id) {
            Objects.requireNonNull(id, "id is null");
            enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRuleState");
            return mZenModeHelper.getAutomaticZenRuleState(id);
            return mZenModeHelper.getAutomaticZenRuleState(getCallingZenUser(), id);
        }
        @Override
@@ -5926,9 +5936,30 @@ public class NotificationManagerService extends SystemService {
            enforcePolicyAccess(Binder.getCallingUid(), "setAutomaticZenRuleState");
            boolean fromUser = (condition.source == Condition.SOURCE_USER_ACTION);
            UserHandle zenUser = getCallingZenUser();
            mZenModeHelper.setAutomaticZenRuleState(id, condition, computeZenOrigin(fromUser),
                    Binder.getCallingUid());
            mZenModeHelper.setAutomaticZenRuleState(zenUser, id, condition,
                    computeZenOrigin(fromUser), Binder.getCallingUid());
        }
        /**
         * Returns the {@link UserHandle} corresponding to the caller that is performing a
         * zen-related operation (such as {@link #setInterruptionFilter},
         * {@link #addAutomaticZenRule}, {@link #setAutomaticZenRuleState}, etc). The user is
         * {@link UserHandle#USER_CURRENT} if the caller is the system or SystemUI (assuming
         * that all interactions in SystemUI are for the "current" user); otherwise it's the user
         * associated to the binder call.
         */
        private UserHandle getCallingZenUser() {
            if (android.app.Flags.modesMultiuser()) {
                if (isCallerSystemOrSystemUiOrShell()) {
                    return UserHandle.CURRENT;
                } else {
                    return Binder.getCallingUserHandle();
                }
            } else {
                return UserHandle.CURRENT;
            }
        }
        @ZenModeConfig.ConfigOrigin
@@ -5964,15 +5995,16 @@ public class NotificationManagerService extends SystemService {
            if (zen == -1) throw new IllegalArgumentException("Invalid filter: " + filter);
            final int callingUid = Binder.getCallingUid();
            enforceUserOriginOnlyFromSystem(fromUser, "setInterruptionFilter");
            UserHandle zenUser = getCallingZenUser();
            if (android.app.Flags.modesApi() && !canManageGlobalZenPolicy(pkg, callingUid)) {
                mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(pkg, callingUid, zen);
                mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(zenUser, pkg, callingUid, zen);
                return;
            }
            final long identity = Binder.clearCallingIdentity();
            try {
                mZenModeHelper.setManualZenMode(zen, null, computeZenOrigin(fromUser),
                mZenModeHelper.setManualZenMode(zenUser, zen, null, computeZenOrigin(fromUser),
                        /* reason= */ "setInterruptionFilter", /* caller= */ pkg,
                        callingUid);
            } finally {
@@ -6268,12 +6300,13 @@ public class NotificationManagerService extends SystemService {
        @Override
        public Policy getNotificationPolicy(String pkg) {
            final int callingUid = Binder.getCallingUid();
            UserHandle zenUser = getCallingZenUser();
            if (android.app.Flags.modesApi() && !canManageGlobalZenPolicy(pkg, callingUid)) {
                return mZenModeHelper.getNotificationPolicyFromImplicitZenRule(pkg);
                return mZenModeHelper.getNotificationPolicyFromImplicitZenRule(zenUser, pkg);
            }
            final long identity = Binder.clearCallingIdentity();
            try {
                return mZenModeHelper.getNotificationPolicy();
                return mZenModeHelper.getNotificationPolicy(zenUser);
            } finally {
                Binder.restoreCallingIdentity(identity);
            }
@@ -6302,6 +6335,7 @@ public class NotificationManagerService extends SystemService {
            enforceUserOriginOnlyFromSystem(fromUser, "setNotificationPolicy");
            int callingUid = Binder.getCallingUid();
            @ZenModeConfig.ConfigOrigin int origin = computeZenOrigin(fromUser);
            UserHandle zenUser = getCallingZenUser();
            boolean isSystemCaller = isCallerSystemOrSystemUiOrShell();
            boolean shouldApplyAsImplicitRule = android.app.Flags.modesApi()
@@ -6311,7 +6345,7 @@ public class NotificationManagerService extends SystemService {
            try {
                final ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(pkg,
                        0, UserHandle.getUserId(callingUid));
                Policy currPolicy = mZenModeHelper.getNotificationPolicy();
                Policy currPolicy = mZenModeHelper.getNotificationPolicy(zenUser);
                if (applicationInfo.targetSdkVersion < Build.VERSION_CODES.P) {
                    int priorityCategories = policy.priorityCategories;
@@ -6369,11 +6403,12 @@ public class NotificationManagerService extends SystemService {
                }
                if (shouldApplyAsImplicitRule) {
                    mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(pkg, callingUid, policy);
                    mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(zenUser, pkg, callingUid,
                            policy);
                } else {
                    ZenLog.traceSetNotificationPolicy(pkg, applicationInfo.targetSdkVersion,
                            policy);
                    mZenModeHelper.setNotificationPolicy(policy, origin, callingUid);
                    mZenModeHelper.setNotificationPolicy(zenUser, policy, origin, callingUid);
                }
            } catch (RemoteException e) {
                Slog.e(TAG, "Failed to set notification policy", e);
+8 −4
Original line number Diff line number Diff line
@@ -2028,7 +2028,8 @@ public class PreferencesHelper implements RankingConfig {
     * </ul>
     */
    void syncChannelsBypassingDnd() {
        mCurrentUserHasChannelsBypassingDnd = (mZenModeHelper.getNotificationPolicy().state
        mCurrentUserHasChannelsBypassingDnd =
                (mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT).state
                        & NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND) != 0;

        updateCurrentUserHasChannelsBypassingDnd(/* callingUid= */ Process.SYSTEM_UID,
@@ -2072,7 +2073,8 @@ public class PreferencesHelper implements RankingConfig {
        if (mCurrentUserHasChannelsBypassingDnd != haveBypassingApps) {
            mCurrentUserHasChannelsBypassingDnd = haveBypassingApps;
            if (android.app.Flags.modesUi()) {
                mZenModeHelper.updateHasPriorityChannels(mCurrentUserHasChannelsBypassingDnd);
                mZenModeHelper.updateHasPriorityChannels(UserHandle.CURRENT,
                        mCurrentUserHasChannelsBypassingDnd);
            } else {
                updateZenPolicy(mCurrentUserHasChannelsBypassingDnd, callingUid,
                        fromSystemOrSystemUi);
@@ -2099,8 +2101,10 @@ public class PreferencesHelper implements RankingConfig {
    //                     PreferencesHelper should otherwise not need to modify actual policy
    public void updateZenPolicy(boolean areChannelsBypassingDnd, int callingUid,
            boolean fromSystemOrSystemUi) {
        NotificationManager.Policy policy = mZenModeHelper.getNotificationPolicy();
        NotificationManager.Policy policy = mZenModeHelper.getNotificationPolicy(
                UserHandle.CURRENT);
        mZenModeHelper.setNotificationPolicy(
                UserHandle.CURRENT,
                new NotificationManager.Policy(
                        policy.priorityCategories, policy.priorityCallSenders,
                        policy.priorityMessageSenders, policy.suppressedVisualEffects,
+5 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.content.ComponentName;
import android.net.Uri;
import android.os.Binder;
import android.os.Process;
import android.os.UserHandle;
import android.service.notification.Condition;
import android.service.notification.IConditionProvider;
import android.service.notification.ZenModeConfig;
@@ -117,7 +118,10 @@ public class ZenModeConditions implements ConditionProviders.Callback {
        ZenModeConfig config = mHelper.getConfig();
        if (config == null) return;
        final int callingUid = Binder.getCallingUid();
        mHelper.setAutomaticZenRuleState(id, condition,

        // This change is known to be for UserHandle.CURRENT because ConditionProviders for
        // background users are not bound.
        mHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, condition,
                callingUid == Process.SYSTEM_UID ? ZenModeConfig.ORIGIN_SYSTEM
                        : ZenModeConfig.ORIGIN_APP,
                callingUid);
+174 −118

File changed.

Preview size limit exceeded, changes collapsed.

Loading