diff options
9 files changed, 1209 insertions, 952 deletions
diff --git a/core/java/android/app/notification.aconfig b/core/java/android/app/notification.aconfig index 1d4c18f6a62c..0fc4291d15ab 100644 --- a/core/java/android/app/notification.aconfig +++ b/core/java/android/app/notification.aconfig @@ -66,6 +66,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 namespace: "systemui" diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index abf3da45d0cb..bb0aaaff4806 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -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); diff --git a/services/core/java/com/android/server/notification/PreferencesHelper.java b/services/core/java/com/android/server/notification/PreferencesHelper.java index d26a5aa5491f..9f0b4b0b6299 100644 --- a/services/core/java/com/android/server/notification/PreferencesHelper.java +++ b/services/core/java/com/android/server/notification/PreferencesHelper.java @@ -2028,8 +2028,9 @@ public class PreferencesHelper implements RankingConfig { * </ul> */ void syncChannelsBypassingDnd() { - mCurrentUserHasChannelsBypassingDnd = (mZenModeHelper.getNotificationPolicy().state - & NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND) != 0; + mCurrentUserHasChannelsBypassingDnd = + (mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT).state + & NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND) != 0; updateCurrentUserHasChannelsBypassingDnd(/* callingUid= */ Process.SYSTEM_UID, /* fromSystemOrSystemUi= */ true); @@ -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, diff --git a/services/core/java/com/android/server/notification/ZenModeConditions.java b/services/core/java/com/android/server/notification/ZenModeConditions.java index b1f010c38d4e..52d0c41614d5 100644 --- a/services/core/java/com/android/server/notification/ZenModeConditions.java +++ b/services/core/java/com/android/server/notification/ZenModeConditions.java @@ -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); diff --git a/services/core/java/com/android/server/notification/ZenModeHelper.java b/services/core/java/com/android/server/notification/ZenModeHelper.java index 5547bd3b34ea..3900870bced3 100644 --- a/services/core/java/com/android/server/notification/ZenModeHelper.java +++ b/services/core/java/com/android/server/notification/ZenModeHelper.java @@ -174,8 +174,7 @@ public class ZenModeHelper { private final RingerModeDelegate mRingerModeDelegate = new RingerModeDelegate(); @VisibleForTesting protected final ZenModeConditions mConditions; - private final Object mConfigsArrayLock = new Object(); - @GuardedBy("mConfigsArrayLock") + @GuardedBy("mConfigLock") @VisibleForTesting final SparseArray<ZenModeConfig> mConfigs = new SparseArray<>(); private final Metrics mMetrics = new Metrics(); private final ConditionProviders.Config mServiceConfig; @@ -222,8 +221,8 @@ public class ZenModeHelper { : readDefaultConfig(mContext.getResources()); updateDefaultConfig(mContext, mDefaultConfig); - mConfig = mDefaultConfig.copy(); - synchronized (mConfigsArrayLock) { + synchronized (mConfigLock) { + mConfig = mDefaultConfig.copy(); mConfigs.put(UserHandle.USER_SYSTEM, mConfig); } mConsolidatedPolicy = mConfig.toNotificationPolicy(); @@ -237,10 +236,6 @@ public class ZenModeHelper { mZenModeEventLogger = zenModeEventLogger; } - public Looper getLooper() { - return mHandler.getLooper(); - } - @Override public String toString() { return TAG; @@ -331,7 +326,7 @@ public class ZenModeHelper { public void onUserRemoved(int user) { if (user < UserHandle.USER_SYSTEM) return; if (DEBUG) Log.d(TAG, "onUserRemoved u=" + user); - synchronized (mConfigsArrayLock) { + synchronized (mConfigLock) { mConfigs.remove(user); } } @@ -350,7 +345,7 @@ public class ZenModeHelper { mUser = user; if (DEBUG) Log.d(TAG, reason + " u=" + user); ZenModeConfig config = null; - synchronized (mConfigsArrayLock) { + synchronized (mConfigLock) { if (mConfigs.get(user) != null) { config = mConfigs.get(user).copy(); } @@ -376,7 +371,9 @@ public class ZenModeHelper { boolean fromSystemOrSystemUi) { final int newZen = NotificationManager.zenModeFromInterruptionFilter(filter, -1); if (newZen != -1) { - setManualZenMode(newZen, null, + // This change is known to be for UserHandle.CURRENT because NLSes for + // background users are unbound. + setManualZenMode(UserHandle.CURRENT, newZen, null, fromSystemOrSystemUi ? ORIGIN_SYSTEM : ORIGIN_APP, /* reason= */ "listener:" + (name != null ? name.flattenToShortString() : null), /* caller= */ name != null ? name.getPackageName() : null, @@ -399,11 +396,12 @@ public class ZenModeHelper { } // TODO: b/310620812 - Make private (or inline) when MODES_API is inlined. - public List<ZenRule> getZenRules() { + public List<ZenRule> getZenRules(UserHandle user) { List<ZenRule> rules = new ArrayList<>(); synchronized (mConfigLock) { - if (mConfig == null) return rules; - for (ZenRule rule : mConfig.automaticRules.values()) { + ZenModeConfig config = getConfigLocked(user); + if (config == null) return rules; + for (ZenRule rule : config.automaticRules.values()) { if (canManageAutomaticZenRule(rule)) { rules.add(rule); } @@ -417,8 +415,8 @@ public class ZenModeHelper { * (which means the owned rules for a regular app, and every rule for system callers) together * with their ids. */ - Map<String, AutomaticZenRule> getAutomaticZenRules() { - List<ZenRule> ruleList = getZenRules(); + Map<String, AutomaticZenRule> getAutomaticZenRules(UserHandle user) { + List<ZenRule> ruleList = getZenRules(user); HashMap<String, AutomaticZenRule> rules = new HashMap<>(ruleList.size()); for (ZenRule rule : ruleList) { rules.put(rule.id, zenRuleToAutomaticZenRule(rule)); @@ -426,11 +424,12 @@ public class ZenModeHelper { return rules; } - public AutomaticZenRule getAutomaticZenRule(String id) { + public AutomaticZenRule getAutomaticZenRule(UserHandle user, String id) { ZenRule rule; synchronized (mConfigLock) { - if (mConfig == null) return null; - rule = mConfig.automaticRules.get(id); + ZenModeConfig config = getConfigLocked(user); + if (config == null) return null; + rule = config.automaticRules.get(id); } if (rule == null) return null; if (canManageAutomaticZenRule(rule)) { @@ -439,8 +438,9 @@ public class ZenModeHelper { return null; } - public String addAutomaticZenRule(String pkg, AutomaticZenRule automaticZenRule, - @ConfigOrigin int origin, String reason, int callingUid) { + public String addAutomaticZenRule(UserHandle user, String pkg, + AutomaticZenRule automaticZenRule, @ConfigOrigin int origin, String reason, + int callingUid) { checkManageRuleOrigin("addAutomaticZenRule", origin); if (!ZenModeConfig.SYSTEM_AUTHORITY.equals(pkg)) { PackageItemInfo component = getServiceInfo(automaticZenRule.getOwner()); @@ -455,10 +455,10 @@ public class ZenModeHelper { ruleInstanceLimit = component.metaData.getInt( ConditionProviderService.META_DATA_RULE_INSTANCE_LIMIT, -1); } - int newRuleInstanceCount = getCurrentInstanceCount(automaticZenRule.getOwner()) - + getCurrentInstanceCount(automaticZenRule.getConfigurationActivity()) + int newRuleInstanceCount = getCurrentInstanceCount(user, automaticZenRule.getOwner()) + + getCurrentInstanceCount(user, automaticZenRule.getConfigurationActivity()) + 1; - int newPackageRuleCount = getPackageRuleCount(pkg) + 1; + int newPackageRuleCount = getPackageRuleCount(user, pkg) + 1; if (newPackageRuleCount > RULE_LIMIT_PER_PACKAGE || (ruleInstanceLimit > 0 && ruleInstanceLimit < newRuleInstanceCount)) { throw new IllegalArgumentException("Rule instance limit exceeded"); @@ -467,15 +467,16 @@ public class ZenModeHelper { ZenModeConfig newConfig; synchronized (mConfigLock) { - if (mConfig == null) { + ZenModeConfig config = getConfigLocked(user); + if (config == null) { throw new AndroidRuntimeException("Could not create rule"); } if (DEBUG) { Log.d(TAG, "addAutomaticZenRule rule= " + automaticZenRule + " reason=" + reason); } - newConfig = mConfig.copy(); + newConfig = config.copy(); ZenRule rule = new ZenRule(); - populateZenRule(pkg, automaticZenRule, rule, origin, /* isNew= */ true); + populateZenRule(pkg, automaticZenRule, newConfig, rule, origin, /* isNew= */ true); rule = maybeRestoreRemovedRule(newConfig, pkg, rule, automaticZenRule, origin); newConfig.automaticRules.put(rule.id, rule); maybeReplaceDefaultRule(newConfig, null, automaticZenRule); @@ -524,7 +525,7 @@ public class ZenModeHelper { // "Preserve" the previous rule by considering the azrToAdd an update instead. // Only app-modifiable fields will actually be modified. - populateZenRule(pkg, azrToAdd, ruleToRestore, origin, /* isNew= */ false); + populateZenRule(pkg, azrToAdd, config, ruleToRestore, origin, /* isNew= */ false); return ruleToRestore; } @@ -558,35 +559,37 @@ public class ZenModeHelper { } } - public boolean updateAutomaticZenRule(String ruleId, AutomaticZenRule automaticZenRule, - @ConfigOrigin int origin, String reason, int callingUid) { + public boolean updateAutomaticZenRule(UserHandle user, String ruleId, + AutomaticZenRule automaticZenRule, @ConfigOrigin int origin, String reason, + int callingUid) { checkManageRuleOrigin("updateAutomaticZenRule", origin); if (ruleId == null) { throw new IllegalArgumentException("ruleId cannot be null"); } synchronized (mConfigLock) { - if (mConfig == null) return false; + ZenModeConfig config = getConfigLocked(user); + if (config == null) return false; if (DEBUG) { Log.d(TAG, "updateAutomaticZenRule zenRule=" + automaticZenRule + " reason=" + reason); } - ZenModeConfig.ZenRule oldRule = mConfig.automaticRules.get(ruleId); + ZenModeConfig.ZenRule oldRule = config.automaticRules.get(ruleId); if (oldRule == null || !canManageAutomaticZenRule(oldRule)) { throw new SecurityException( "Cannot update rules not owned by your condition provider"); } - ZenModeConfig newConfig = mConfig.copy(); + ZenModeConfig newConfig = config.copy(); ZenModeConfig.ZenRule newRule = requireNonNull(newConfig.automaticRules.get(ruleId)); if (!Flags.modesApi()) { if (newRule.enabled != automaticZenRule.isEnabled()) { - dispatchOnAutomaticRuleStatusChanged(mConfig.user, newRule.getPkg(), ruleId, + dispatchOnAutomaticRuleStatusChanged(config.user, newRule.getPkg(), ruleId, automaticZenRule.isEnabled() ? AUTOMATIC_RULE_STATUS_ENABLED : AUTOMATIC_RULE_STATUS_DISABLED); } } - boolean updated = populateZenRule(newRule.pkg, automaticZenRule, newRule, + boolean updated = populateZenRule(newRule.pkg, automaticZenRule, newConfig, newRule, origin, /* isNew= */ false); if (Flags.modesApi() && !updated) { // Bail out so we don't have the side effects of updating a rule (i.e. dropping @@ -619,16 +622,18 @@ public class ZenModeHelper { * * @param zenMode one of the {@code Global#ZEN_MODE_x} values */ - void applyGlobalZenModeAsImplicitZenRule(String callingPkg, int callingUid, int zenMode) { + void applyGlobalZenModeAsImplicitZenRule(UserHandle user, String callingPkg, int callingUid, + int zenMode) { if (!android.app.Flags.modesApi()) { Log.wtf(TAG, "applyGlobalZenModeAsImplicitZenRule called with flag off!"); return; } synchronized (mConfigLock) { - if (mConfig == null) { + ZenModeConfig config = getConfigLocked(user); + if (config == null) { return; } - ZenModeConfig newConfig = mConfig.copy(); + ZenModeConfig newConfig = config.copy(); ZenRule rule = newConfig.automaticRules.get(implicitRuleId(callingPkg)); if (zenMode == Global.ZEN_MODE_OFF) { // Deactivate implicit rule if it exists and is active; otherwise ignore. @@ -650,7 +655,7 @@ public class ZenModeHelper { // would apply if changing the global interruption filter. We only do this // for newly created rules, as existing rules have a pre-existing policy // (whether initialized here or set via app or user). - rule.zenPolicy = mConfig.getZenPolicy().copy(); + rule.zenPolicy = config.getZenPolicy().copy(); newConfig.automaticRules.put(rule.id, rule); } // If the user has changed the rule's *zenMode*, then don't let app overwrite it. @@ -680,17 +685,18 @@ public class ZenModeHelper { * {@link AutomaticZenRule#configurationActivity}. Its zen mode will be set to * {@link Global#ZEN_MODE_IMPORTANT_INTERRUPTIONS}. */ - void applyGlobalPolicyAsImplicitZenRule(String callingPkg, int callingUid, + void applyGlobalPolicyAsImplicitZenRule(UserHandle user, String callingPkg, int callingUid, NotificationManager.Policy policy) { if (!android.app.Flags.modesApi()) { Log.wtf(TAG, "applyGlobalPolicyAsImplicitZenRule called with flag off!"); return; } synchronized (mConfigLock) { - if (mConfig == null) { + ZenModeConfig config = getConfigLocked(user); + if (config == null) { return; } - ZenModeConfig newConfig = mConfig.copy(); + ZenModeConfig newConfig = config.copy(); boolean isNew = false; ZenRule rule = newConfig.automaticRules.get(implicitRuleId(callingPkg)); if (rule == null) { @@ -709,9 +715,10 @@ public class ZenModeHelper { // would take effect if changing the global policy. // Note that NotificationManager.Policy cannot have any unset priority // categories, but *can* have unset visual effects, which is why we do this. - newZenPolicy = mConfig.getZenPolicy().overwrittenWith(newZenPolicy); + newZenPolicy = config.getZenPolicy().overwrittenWith(newZenPolicy); } updatePolicy( + newConfig, rule, newZenPolicy, /* updateBitmask= */ false, @@ -734,25 +741,26 @@ public class ZenModeHelper { * <p>Any unset values in the {@link ZenPolicy} will be mapped to their current defaults. */ @Nullable - Policy getNotificationPolicyFromImplicitZenRule(String callingPkg) { + Policy getNotificationPolicyFromImplicitZenRule(UserHandle user, String callingPkg) { if (!android.app.Flags.modesApi()) { Log.wtf(TAG, "getNotificationPolicyFromImplicitZenRule called with flag off!"); - return getNotificationPolicy(); + return getNotificationPolicy(user); } synchronized (mConfigLock) { - if (mConfig == null) { + ZenModeConfig config = getConfigLocked(user); + if (config == null) { return null; } - ZenRule implicitRule = mConfig.automaticRules.get(implicitRuleId(callingPkg)); + ZenRule implicitRule = config.automaticRules.get(implicitRuleId(callingPkg)); if (implicitRule != null && implicitRule.zenPolicy != null) { - // toNotificationPolicy takes defaults from mConfig, and technically, those are not + // toNotificationPolicy takes defaults from config, and technically those are not // the defaults that would apply if any fields were unset. However, all rules should // have all fields set in their ZenPolicy objects upon rule creation, so in // practice, this is only filling in the areChannelsBypassingDnd field, which is a // state rather than a part of the policy. - return mConfig.toNotificationPolicy(implicitRule.zenPolicy); + return config.toNotificationPolicy(implicitRule.zenPolicy); } else { - return getNotificationPolicy(); + return getNotificationPolicy(user); } } } @@ -798,13 +806,14 @@ public class ZenModeHelper { return rule; } - boolean removeAutomaticZenRule(String id, @ConfigOrigin int origin, String reason, - int callingUid) { + boolean removeAutomaticZenRule(UserHandle user, String id, @ConfigOrigin int origin, + String reason, int callingUid) { checkManageRuleOrigin("removeAutomaticZenRule", origin); ZenModeConfig newConfig; synchronized (mConfigLock) { - if (mConfig == null) return false; - newConfig = mConfig.copy(); + ZenModeConfig config = getConfigLocked(user); + if (config == null) return false; + newConfig = config.copy(); ZenRule ruleToRemove = newConfig.automaticRules.get(id); if (ruleToRemove == null) return false; if (canManageAutomaticZenRule(ruleToRemove)) { @@ -826,18 +835,19 @@ public class ZenModeHelper { "Cannot delete rules not owned by your condition provider"); } dispatchOnAutomaticRuleStatusChanged( - mConfig.user, ruleToRemove.getPkg(), id, AUTOMATIC_RULE_STATUS_REMOVED); + config.user, ruleToRemove.getPkg(), id, AUTOMATIC_RULE_STATUS_REMOVED); return setConfigLocked(newConfig, origin, reason, null, true, callingUid); } } - boolean removeAutomaticZenRules(String packageName, @ConfigOrigin int origin, + boolean removeAutomaticZenRules(UserHandle user, String packageName, @ConfigOrigin int origin, String reason, int callingUid) { checkManageRuleOrigin("removeAutomaticZenRules", origin); ZenModeConfig newConfig; synchronized (mConfigLock) { - if (mConfig == null) return false; - newConfig = mConfig.copy(); + ZenModeConfig config = getConfigLocked(user); + if (config == null) return false; + newConfig = config.copy(); for (int i = newConfig.automaticRules.size() - 1; i >= 0; i--) { ZenRule rule = newConfig.automaticRules.get(newConfig.automaticRules.keyAt(i)); if (Objects.equals(rule.getPkg(), packageName) && canManageAutomaticZenRule(rule)) { @@ -885,12 +895,13 @@ public class ZenModeHelper { } @Condition.State - int getAutomaticZenRuleState(String id) { + int getAutomaticZenRuleState(UserHandle user, String id) { synchronized (mConfigLock) { - if (mConfig == null) { + ZenModeConfig config = getConfigLocked(user); + if (config == null) { return Condition.STATE_UNKNOWN; } - ZenRule rule = mConfig.automaticRules.get(id); + ZenRule rule = config.automaticRules.get(id); if (rule == null || !canManageAutomaticZenRule(rule)) { return Condition.STATE_UNKNOWN; } @@ -903,14 +914,15 @@ public class ZenModeHelper { } } - void setAutomaticZenRuleState(String id, Condition condition, @ConfigOrigin int origin, - int callingUid) { + void setAutomaticZenRuleState(UserHandle user, String id, Condition condition, + @ConfigOrigin int origin, int callingUid) { checkSetRuleStateOrigin("setAutomaticZenRuleState(String id)", origin); ZenModeConfig newConfig; synchronized (mConfigLock) { - if (mConfig == null) return; + ZenModeConfig config = getConfigLocked(user); + if (config == null) return; - newConfig = mConfig.copy(); + newConfig = config.copy(); ZenRule rule = newConfig.automaticRules.get(id); if (Flags.modesApi()) { if (rule != null && canManageAutomaticZenRule(rule)) { @@ -925,13 +937,14 @@ public class ZenModeHelper { } } - void setAutomaticZenRuleState(Uri ruleDefinition, Condition condition, + void setAutomaticZenRuleState(UserHandle user, Uri ruleDefinition, Condition condition, @ConfigOrigin int origin, int callingUid) { checkSetRuleStateOrigin("setAutomaticZenRuleState(Uri ruleDefinition)", origin); ZenModeConfig newConfig; synchronized (mConfigLock) { - if (mConfig == null) return; - newConfig = mConfig.copy(); + ZenModeConfig config = getConfigLocked(user); + if (config == null) return; + newConfig = config.copy(); List<ZenRule> matchingRules = findMatchingRules(newConfig, ruleDefinition, condition); if (Flags.modesApi()) { @@ -1025,13 +1038,16 @@ public class ZenModeHelper { return true; } - public int getCurrentInstanceCount(ComponentName cn) { + public int getCurrentInstanceCount(UserHandle user, ComponentName cn) { if (cn == null) { return 0; } int count = 0; synchronized (mConfigLock) { - for (ZenRule rule : mConfig.automaticRules.values()) { + ZenModeConfig config = getConfigLocked(user); + if (config == null) return 0; + + for (ZenRule rule : config.automaticRules.values()) { if (cn.equals(rule.component) || cn.equals(rule.configurationActivity)) { count++; } @@ -1042,13 +1058,16 @@ public class ZenModeHelper { // Equivalent method to getCurrentInstanceCount, but for all rules associated with a specific // package rather than a condition provider service or activity. - private int getPackageRuleCount(String pkg) { + private int getPackageRuleCount(UserHandle user, String pkg) { if (pkg == null) { return 0; } int count = 0; synchronized (mConfigLock) { - for (ZenRule rule : mConfig.automaticRules.values()) { + ZenModeConfig config = getConfigLocked(user); + if (config == null) return 0; + + for (ZenRule rule : config.automaticRules.values()) { if (pkg.equals(rule.getPkg())) { count++; } @@ -1081,13 +1100,15 @@ public class ZenModeHelper { void updateZenRulesOnLocaleChange() { updateRuleStringsForCurrentLocale(mContext, mDefaultConfig); synchronized (mConfigLock) { - if (mConfig == null) { + ZenModeConfig config = getConfigLocked(UserHandle.CURRENT); + if (config == null) { return; } - ZenModeConfig config = mConfig.copy(); + + ZenModeConfig newConfig = config.copy(); boolean updated = false; for (ZenRule defaultRule : mDefaultConfig.automaticRules.values()) { - ZenRule currRule = config.automaticRules.get(defaultRule.id); + ZenRule currRule = newConfig.automaticRules.get(defaultRule.id); // if default rule wasn't user-modified use localized name // instead of previous system name if (currRule != null @@ -1103,14 +1124,14 @@ public class ZenModeHelper { } } if (Flags.modesApi() && Flags.modesUi()) { - for (ZenRule rule : config.automaticRules.values()) { + for (ZenRule rule : newConfig.automaticRules.values()) { if (SystemZenRules.isSystemOwnedRule(rule)) { updated |= SystemZenRules.updateTriggerDescription(mContext, rule); } } } if (updated) { - setConfigLocked(config, null, ORIGIN_SYSTEM, + setConfigLocked(newConfig, null, ORIGIN_SYSTEM, "updateZenRulesOnLocaleChange", Process.SYSTEM_UID); } } @@ -1170,8 +1191,8 @@ public class ZenModeHelper { * deactivated) unless the update has origin == {@link ZenModeConfig#ORIGIN_USER_IN_SYSTEMUI}. */ @GuardedBy("mConfigLock") - private boolean populateZenRule(String pkg, AutomaticZenRule azr, ZenRule rule, - @ConfigOrigin int origin, boolean isNew) { + private boolean populateZenRule(String pkg, AutomaticZenRule azr, ZenModeConfig config, + ZenRule rule, @ConfigOrigin int origin, boolean isNew) { if (Flags.modesApi()) { boolean modified = false; // These values can always be edited by the app, so we apply changes immediately. @@ -1307,7 +1328,7 @@ public class ZenModeHelper { } // Updates the bitmask and values for all policy fields, based on the origin. - modified |= updatePolicy(rule, azr.getZenPolicy(), updateBitmask, isNew); + modified |= updatePolicy(config, rule, azr.getZenPolicy(), updateBitmask, isNew); // Updates the bitmask and values for all device effect fields, based on the origin. modified |= updateZenDeviceEffects(rule, azr.getDeviceEffects(), @@ -1360,13 +1381,13 @@ public class ZenModeHelper { * <p>Returns {@code true} if the policy of the rule was modified. */ @GuardedBy("mConfigLock") - private boolean updatePolicy(ZenRule zenRule, @Nullable ZenPolicy newPolicy, - boolean updateBitmask, boolean isNew) { + private boolean updatePolicy(ZenModeConfig config, ZenRule zenRule, + @Nullable ZenPolicy newPolicy, boolean updateBitmask, boolean isNew) { if (newPolicy == null) { if (isNew) { // Newly created rule with no provided policy; fill in with the default. zenRule.zenPolicy = - (Flags.modesUi() ? mDefaultConfig.getZenPolicy() : mConfig.getZenPolicy()) + (Flags.modesUi() ? mDefaultConfig.getZenPolicy() : config.getZenPolicy()) .copy(); return true; } @@ -1378,7 +1399,7 @@ public class ZenModeHelper { // fields in the bitmask should be marked as updated. ZenPolicy oldPolicy = zenRule.zenPolicy != null ? zenRule.zenPolicy - : (Flags.modesUi() ? mDefaultConfig.getZenPolicy() : mConfig.getZenPolicy()); + : (Flags.modesUi() ? mDefaultConfig.getZenPolicy() : config.getZenPolicy()); // If this is updating a rule rather than creating a new one, keep any fields from the // old policy if they are unspecified in the new policy. For newly created rules, oldPolicy @@ -1570,17 +1591,20 @@ public class ZenModeHelper { // Update only the hasPriorityChannels state (aka areChannelsBypassingDnd) without modifying // any of the rest of the existing policy. This allows components that only want to modify // this bit (PreferencesHelper) to not have to adjust the rest of the policy. - protected void updateHasPriorityChannels(boolean hasPriorityChannels) { + protected void updateHasPriorityChannels(UserHandle user, boolean hasPriorityChannels) { if (!Flags.modesUi()) { Log.wtf(TAG, "updateHasPriorityChannels called without modes_ui"); } synchronized (mConfigLock) { + ZenModeConfig config = getConfigLocked(user); + if (config == null) return; + // If it already matches, do nothing - if (mConfig.areChannelsBypassingDnd == hasPriorityChannels) { + if (config.areChannelsBypassingDnd == hasPriorityChannels) { return; } - ZenModeConfig newConfig = mConfig.copy(); + ZenModeConfig newConfig = config.copy(); newConfig.areChannelsBypassingDnd = hasPriorityChannels; // The updated calculation of whether there are priority channels is always done by // the system, even if the event causing the calculation had a different origin. @@ -1610,22 +1634,25 @@ public class ZenModeHelper { : AUTOMATIC_RULE_STATUS_DISABLED); } - void setManualZenMode(int zenMode, Uri conditionId, @ConfigOrigin int origin, + void setManualZenMode(UserHandle user, int zenMode, Uri conditionId, @ConfigOrigin int origin, String reason, @Nullable String caller, int callingUid) { - setManualZenMode(zenMode, conditionId, origin, reason, caller, true /*setRingerMode*/, + setManualZenMode(user, zenMode, conditionId, origin, reason, caller, true /*setRingerMode*/, callingUid); } - private void setManualZenMode(int zenMode, Uri conditionId, @ConfigOrigin int origin, - String reason, @Nullable String caller, boolean setRingerMode, int callingUid) { + private void setManualZenMode(UserHandle user, int zenMode, Uri conditionId, + @ConfigOrigin int origin, String reason, @Nullable String caller, boolean setRingerMode, + int callingUid) { ZenModeConfig newConfig; synchronized (mConfigLock) { - if (mConfig == null) return; + ZenModeConfig config = getConfigLocked(user); + if (config == null) return; + if (!Global.isValidZenMode(zenMode)) return; if (DEBUG) Log.d(TAG, "setManualZenMode " + Global.zenModeToString(zenMode) + " conditionId=" + conditionId + " reason=" + reason + " setRingerMode=" + setRingerMode); - newConfig = mConfig.copy(); + newConfig = config.copy(); if (Flags.modesUi()) { newConfig.manualRule.enabler = caller; newConfig.manualRule.conditionId = conditionId != null ? conditionId : Uri.EMPTY; @@ -1668,18 +1695,20 @@ public class ZenModeHelper { } } - public void setManualZenRuleDeviceEffects(ZenDeviceEffects deviceEffects, + public void setManualZenRuleDeviceEffects(UserHandle user, ZenDeviceEffects deviceEffects, @ConfigOrigin int origin, String reason, int callingUid) { if (!Flags.modesUi()) { return; } ZenModeConfig newConfig; synchronized (mConfigLock) { - if (mConfig == null) return; + ZenModeConfig config = getConfigLocked(user); + if (config == null) return; + if (DEBUG) Log.d(TAG, "updateManualRule " + deviceEffects + " reason=" + reason + " callingUid=" + callingUid); - newConfig = mConfig.copy(); + newConfig = config.copy(); newConfig.manualRule.pkg = PACKAGE_ANDROID; newConfig.manualRule.zenDeviceEffects = deviceEffects; @@ -1709,7 +1738,7 @@ public class ZenModeHelper { pw.println(Global.zenModeToString(mZenMode)); pw.print(prefix); pw.println("mConsolidatedPolicy=" + mConsolidatedPolicy.toString()); - synchronized (mConfigsArrayLock) { + synchronized (mConfigLock) { final int N = mConfigs.size(); for (int i = 0; i < N; i++) { dump(pw, prefix, "mConfigs[u=" + mConfigs.keyAt(i) + "]", mConfigs.valueAt(i)); @@ -1835,7 +1864,7 @@ public class ZenModeHelper { public void writeXml(TypedXmlSerializer out, boolean forBackup, Integer version, int userId) throws IOException { - synchronized (mConfigsArrayLock) { + synchronized (mConfigLock) { final int n = mConfigs.size(); for (int i = 0; i < n; i++) { if (forBackup && mConfigs.keyAt(i) != userId) { @@ -1849,24 +1878,29 @@ public class ZenModeHelper { /** * @return user-specified default notification policy for priority only do not disturb */ - public Policy getNotificationPolicy() { + @Nullable + public Policy getNotificationPolicy(UserHandle user) { synchronized (mConfigLock) { - return getNotificationPolicy(mConfig); + return getNotificationPolicy(getConfigLocked(user)); } } - private static Policy getNotificationPolicy(ZenModeConfig config) { + @Nullable + private static Policy getNotificationPolicy(@Nullable ZenModeConfig config) { return config == null ? null : config.toNotificationPolicy(); } /** * Sets the global notification policy used for priority only do not disturb */ - public void setNotificationPolicy(Policy policy, @ConfigOrigin int origin, + public void setNotificationPolicy(UserHandle user, Policy policy, @ConfigOrigin int origin, int callingUid) { synchronized (mConfigLock) { - if (policy == null || mConfig == null) return; - final ZenModeConfig newConfig = mConfig.copy(); + if (policy == null) return; + ZenModeConfig config = getConfigLocked(user); + if (config == null) return; + + final ZenModeConfig newConfig = config.copy(); if (Flags.modesApi() && !Flags.modesUi()) { // Fix for b/337193321 -- propagate changes to notificationPolicy to rules where // the user cannot edit zen policy to emulate the previous "inheritance". @@ -1894,7 +1928,7 @@ public class ZenModeHelper { } /** - * Cleans up obsolete rules: + * Cleans up obsolete rules in the current {@link ZenModeConfig}. * <ul> * <li>Rule instances whose owner is not installed. * <li>Deleted rules that were deleted more than 30 days ago. @@ -1966,6 +2000,27 @@ public class ZenModeHelper { return mDefaultConfig.getZenPolicy(); } + /** + * Returns the {@link ZenModeConfig} corresponding to the supplied {@link UserHandle}. + * The result will be {@link #mConfig} if the user is {@link UserHandle#CURRENT}, or the same + * as {@link #mUser}, otherwise will be the corresponding entry in {@link #mConfigs}. + * + * <p>Remember to continue holding {@link #mConfigLock} while operating on the returned value. + */ + @Nullable + @GuardedBy("mConfigLock") + private ZenModeConfig getConfigLocked(@NonNull UserHandle user) { + if (Flags.modesMultiuser()) { + if (user.getIdentifier() == UserHandle.USER_CURRENT || user.getIdentifier() == mUser) { + return mConfig; + } else { + return mConfigs.get(user.getIdentifier()); + } + } else { + return mConfig; + } + } + @GuardedBy("mConfigLock") private boolean setConfigLocked(ZenModeConfig config, ComponentName triggeringComponent, @ConfigOrigin int origin, String reason, int callingUid) { @@ -1992,7 +2047,7 @@ public class ZenModeHelper { } if (config.user != mUser) { // simply store away for background users - synchronized (mConfigsArrayLock) { + synchronized (mConfigLock) { mConfigs.put(config.user, config); } if (DEBUG) Log.d(TAG, "setConfigLocked: store config for user " + config.user); @@ -2001,7 +2056,7 @@ public class ZenModeHelper { // handle CPS backed conditions - danger! may modify config mConditions.evaluateConfig(config, null, false /*processSubscriptions*/); - synchronized (mConfigsArrayLock) { + synchronized (mConfigLock) { mConfigs.put(config.user, config); } if (DEBUG) Log.d(TAG, "setConfigLocked reason=" + reason, new Throwable()); @@ -2142,7 +2197,8 @@ public class ZenModeHelper { } @GuardedBy("mConfigLock") - private void applyCustomPolicy(ZenPolicy policy, ZenRule rule, boolean useManualConfig) { + private void applyCustomPolicy(ZenModeConfig config, ZenPolicy policy, ZenRule rule, + boolean useManualConfig) { if (rule.zenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) { if (Flags.modesApi() && Flags.modesUi()) { policy.apply(ZenPolicy.getBasePolicyInterruptionFilterNone()); @@ -2168,8 +2224,8 @@ public class ZenModeHelper { } else { if (Flags.modesApi()) { if (useManualConfig) { - // manual rule is configured using the settings stored directly in mConfig - policy.apply(mConfig.getZenPolicy()); + // manual rule is configured using the settings stored directly in ZenModeConfig + policy.apply(config.getZenPolicy()); } else { // under modes_api flag, an active automatic rule with no specified policy // inherits the device default settings as stored in mDefaultConfig. While the @@ -2177,11 +2233,11 @@ public class ZenModeHelper { // catch any that may have fallen through the cracks. Log.wtf(TAG, "active automatic rule found with no specified policy: " + rule); policy.apply(Flags.modesUi() - ? mDefaultConfig.getZenPolicy() : mConfig.getZenPolicy()); + ? mDefaultConfig.getZenPolicy() : config.getZenPolicy()); } } else { // active rule with no specified policy inherits the manual rule config settings - policy.apply(mConfig.getZenPolicy()); + policy.apply(config.getZenPolicy()); } } } @@ -2194,7 +2250,7 @@ public class ZenModeHelper { ZenPolicy policy = new ZenPolicy(); ZenDeviceEffects.Builder deviceEffectsBuilder = new ZenDeviceEffects.Builder(); if (mConfig.isManualActive()) { - applyCustomPolicy(policy, mConfig.manualRule, true); + applyCustomPolicy(mConfig, policy, mConfig.manualRule, true); if (Flags.modesApi()) { deviceEffectsBuilder.add(mConfig.manualRule.zenDeviceEffects); } @@ -2206,7 +2262,7 @@ public class ZenModeHelper { // policy. This is relevant in case some other active rule has a more // restrictive INTERRUPTION_FILTER but a more lenient ZenPolicy! if (!Flags.modesApi() || automaticRule.zenMode != Global.ZEN_MODE_OFF) { - applyCustomPolicy(policy, automaticRule, false); + applyCustomPolicy(mConfig, policy, automaticRule, false); } if (Flags.modesApi()) { deviceEffectsBuilder.add(automaticRule.zenDeviceEffects); @@ -2469,7 +2525,7 @@ public class ZenModeHelper { * Generate pulled atoms about do not disturb configurations. */ public void pullRules(List<StatsEvent> events) { - synchronized (mConfigsArrayLock) { + synchronized (mConfigLock) { final int numConfigs = mConfigs.size(); for (int i = 0; i < numConfigs; i++) { final int user = mConfigs.keyAt(i); @@ -2496,7 +2552,7 @@ public class ZenModeHelper { } } - @GuardedBy("mConfigsArrayLock") + @GuardedBy("mConfigLock") private void ruleToProtoLocked(int user, ZenRule rule, boolean isManualRule, List<StatsEvent> events) { // Make the ID safe. @@ -2601,7 +2657,7 @@ public class ZenModeHelper { } if (newZen != -1) { - setManualZenMode(newZen, null, ORIGIN_SYSTEM, + setManualZenMode(UserHandle.CURRENT, newZen, null, ORIGIN_SYSTEM, "ringerModeInternal", /* caller= */ null, /* setRingerMode= */ false, Process.SYSTEM_UID); } @@ -2646,7 +2702,7 @@ public class ZenModeHelper { break; } if (newZen != -1) { - setManualZenMode(newZen, null, ORIGIN_SYSTEM, + setManualZenMode(UserHandle.CURRENT, newZen, null, ORIGIN_SYSTEM, "ringerModeExternal", caller, false /*setRingerMode*/, Process.SYSTEM_UID); } diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java index dec7f09f2da4..f60ec99edf00 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -7662,7 +7662,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mService.mZenModeHelper = mZenModeHelper; NotificationManager.Policy userPolicy = new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); - when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); + when(mZenModeHelper.getNotificationPolicy(any())).thenReturn(userPolicy); NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_SCREEN_OFF); @@ -7682,7 +7682,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mService.mZenModeHelper = mZenModeHelper; NotificationManager.Policy userPolicy = new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); - when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); + when(mZenModeHelper.getNotificationPolicy(any())).thenReturn(userPolicy); NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_NOTIFICATION_LIST); @@ -7699,7 +7699,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mService.mZenModeHelper = mZenModeHelper; NotificationManager.Policy userPolicy = new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); - when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); + when(mZenModeHelper.getNotificationPolicy(any())).thenReturn(userPolicy); NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_STATUS_BAR); @@ -7717,7 +7717,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mService.mZenModeHelper = mZenModeHelper; NotificationManager.Policy userPolicy = new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); - when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); + when(mZenModeHelper.getNotificationPolicy(any())).thenReturn(userPolicy); NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_SCREEN_OFF); @@ -7736,7 +7736,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mService.mZenModeHelper = mZenModeHelper; NotificationManager.Policy userPolicy = new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); - when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); + when(mZenModeHelper.getNotificationPolicy(any())).thenReturn(userPolicy); NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_NOTIFICATION_LIST | SUPPRESSED_EFFECT_AMBIENT @@ -7756,7 +7756,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mService.mZenModeHelper = mZenModeHelper; NotificationManager.Policy userPolicy = new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_BADGE); - when(mZenModeHelper.getNotificationPolicy()).thenReturn(userPolicy); + when(mZenModeHelper.getNotificationPolicy(any())).thenReturn(userPolicy); NotificationManager.Policy appPolicy = new NotificationManager.Policy(0, 0, 0, SUPPRESSED_EFFECT_SCREEN_ON | SUPPRESSED_EFFECT_STATUS_BAR); @@ -10398,7 +10398,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.addAutomaticZenRule(rule, "com.android.settings", false); // verify that zen mode helper gets passed in a package name of "android" - verify(mockZenModeHelper).addAutomaticZenRule(eq("android"), eq(rule), + verify(mockZenModeHelper).addAutomaticZenRule(any(), eq("android"), eq(rule), eq(ZenModeConfig.ORIGIN_SYSTEM), anyString(), anyInt()); } @@ -10420,7 +10420,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.addAutomaticZenRule(rule, "com.android.settings", false); // verify that zen mode helper gets passed in a package name of "android" - verify(mockZenModeHelper).addAutomaticZenRule(eq("android"), eq(rule), + verify(mockZenModeHelper).addAutomaticZenRule(any(), eq("android"), eq(rule), eq(ZenModeConfig.ORIGIN_SYSTEM), anyString(), anyInt()); } @@ -10440,9 +10440,9 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.addAutomaticZenRule(rule, "another.package", false); // verify that zen mode helper gets passed in the package name from the arg, not the owner - verify(mockZenModeHelper).addAutomaticZenRule( - eq("another.package"), eq(rule), eq(ZenModeConfig.ORIGIN_APP), - anyString(), anyInt()); // doesn't count as a system/systemui call + verify(mockZenModeHelper).addAutomaticZenRule(any(), eq("another.package"), eq(rule), + eq(ZenModeConfig.ORIGIN_APP), anyString(), + anyInt()); // doesn't count as a system/systemui call } @Test @@ -10459,7 +10459,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.addAutomaticZenRule(rule, mPkg, /* fromUser= */ false); - verify(zenModeHelper).addAutomaticZenRule(eq(mPkg), eq(rule), anyInt(), any(), anyInt()); + verify(zenModeHelper).addAutomaticZenRule(any(), eq(mPkg), eq(rule), anyInt(), any(), + anyInt()); } @Test @@ -10494,7 +10495,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.addAutomaticZenRule(rule, mPkg, /* fromUser= */ false); - verify(zenModeHelper).addAutomaticZenRule(eq(mPkg), eq(rule), anyInt(), any(), anyInt()); + verify(zenModeHelper).addAutomaticZenRule(any(), eq(mPkg), eq(rule), anyInt(), any(), + anyInt()); } @Test @@ -10527,7 +10529,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.addAutomaticZenRule(rule, mPkg, /* fromUser= */ false); - verify(zenModeHelper).addAutomaticZenRule(eq(mPkg), eq(rule), anyInt(), any(), anyInt()); + verify(zenModeHelper).addAutomaticZenRule(any(), eq(mPkg), eq(rule), anyInt(), any(), + anyInt()); } private void addAutomaticZenRule_restrictedRuleTypeCannotBeUsedByRegularApps( @@ -10555,7 +10558,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.addAutomaticZenRule(SOME_ZEN_RULE, "pkg", /* fromUser= */ true); - verify(zenModeHelper).addAutomaticZenRule(eq("pkg"), eq(SOME_ZEN_RULE), + verify(zenModeHelper).addAutomaticZenRule(any(), eq("pkg"), eq(SOME_ZEN_RULE), eq(ZenModeConfig.ORIGIN_USER_IN_SYSTEMUI), anyString(), anyInt()); } @@ -10567,7 +10570,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.addAutomaticZenRule(SOME_ZEN_RULE, "pkg", /* fromUser= */ false); - verify(zenModeHelper).addAutomaticZenRule(eq("pkg"), eq(SOME_ZEN_RULE), + verify(zenModeHelper).addAutomaticZenRule(any(), eq("pkg"), eq(SOME_ZEN_RULE), eq(ZenModeConfig.ORIGIN_SYSTEM), anyString(), anyInt()); } @@ -10579,7 +10582,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.addAutomaticZenRule(SOME_ZEN_RULE, "pkg", /* fromUser= */ false); - verify(zenModeHelper).addAutomaticZenRule(eq("pkg"), eq(SOME_ZEN_RULE), + verify(zenModeHelper).addAutomaticZenRule(any(), eq("pkg"), eq(SOME_ZEN_RULE), eq(ZenModeConfig.ORIGIN_APP), anyString(), anyInt()); } @@ -10601,7 +10604,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.updateAutomaticZenRule("id", SOME_ZEN_RULE, /* fromUser= */ true); - verify(zenModeHelper).updateAutomaticZenRule(eq("id"), eq(SOME_ZEN_RULE), + verify(zenModeHelper).updateAutomaticZenRule(any(), eq("id"), eq(SOME_ZEN_RULE), eq(ZenModeConfig.ORIGIN_USER_IN_SYSTEMUI), anyString(), anyInt()); } @@ -10623,7 +10626,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.removeAutomaticZenRule("id", /* fromUser= */ true); - verify(zenModeHelper).removeAutomaticZenRule(eq("id"), + verify(zenModeHelper).removeAutomaticZenRule(any(), eq("id"), eq(ZenModeConfig.ORIGIN_USER_IN_SYSTEMUI), anyString(), anyInt()); } @@ -10648,7 +10651,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { SOURCE_USER_ACTION); mBinderService.setAutomaticZenRuleState("id", withSourceUser); - verify(zenModeHelper).setAutomaticZenRuleState(eq("id"), eq(withSourceUser), + verify(zenModeHelper).setAutomaticZenRuleState(any(), eq("id"), eq(withSourceUser), eq(ZenModeConfig.ORIGIN_USER_IN_APP), anyInt()); } @@ -10663,7 +10666,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { SOURCE_CONTEXT); mBinderService.setAutomaticZenRuleState("id", withSourceContext); - verify(zenModeHelper).setAutomaticZenRuleState(eq("id"), eq(withSourceContext), + verify(zenModeHelper).setAutomaticZenRuleState(any(), eq("id"), eq(withSourceContext), eq(ZenModeConfig.ORIGIN_APP), anyInt()); } @@ -10678,7 +10681,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { SOURCE_USER_ACTION); mBinderService.setAutomaticZenRuleState("id", withSourceContext); - verify(zenModeHelper).setAutomaticZenRuleState(eq("id"), eq(withSourceContext), + verify(zenModeHelper).setAutomaticZenRuleState(any(), eq("id"), eq(withSourceContext), eq(ZenModeConfig.ORIGIN_USER_IN_SYSTEMUI), anyInt()); } @Test @@ -10692,10 +10695,35 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { SOURCE_CONTEXT); mBinderService.setAutomaticZenRuleState("id", withSourceContext); - verify(zenModeHelper).setAutomaticZenRuleState(eq("id"), eq(withSourceContext), + verify(zenModeHelper).setAutomaticZenRuleState(any(), eq("id"), eq(withSourceContext), eq(ZenModeConfig.ORIGIN_SYSTEM), anyInt()); } + + @Test + @EnableFlags(android.app.Flags.FLAG_MODES_MULTIUSER) + public void getAutomaticZenRules_fromSystem_readsWithCurrentUser() throws Exception { + ZenModeHelper zenModeHelper = setUpMockZenTest(); + mService.isSystemUid = true; + + // Representative used to verify getCallingZenUser(). + mBinderService.getAutomaticZenRules(); + + verify(zenModeHelper).getAutomaticZenRules(eq(UserHandle.CURRENT)); + } + + @Test + @EnableFlags(android.app.Flags.FLAG_MODES_MULTIUSER) + public void getAutomaticZenRules_fromNormalPackage_readsWithBinderUser() throws Exception { + ZenModeHelper zenModeHelper = setUpMockZenTest(); + mService.setCallerIsNormalPackage(); + + // Representative used to verify getCallingZenUser(). + mBinderService.getAutomaticZenRules(); + + verify(zenModeHelper).getAutomaticZenRules(eq(Binder.getCallingUserHandle())); + } + /** Prepares for a zen-related test that uses a mocked {@link ZenModeHelper}. */ private ZenModeHelper setUpMockZenTest() { ZenModeHelper zenModeHelper = mock(ZenModeHelper.class); @@ -15815,7 +15843,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { NotificationManager.Policy policy = new NotificationManager.Policy(0, 0, 0); mBinderService.setNotificationPolicy("package", policy, false); - verify(zenHelper).applyGlobalPolicyAsImplicitZenRule(eq("package"), anyInt(), eq(policy)); + verify(zenHelper).applyGlobalPolicyAsImplicitZenRule(any(), eq("package"), anyInt(), + eq(policy)); } @Test @@ -15831,7 +15860,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { NotificationManager.Policy policy = new NotificationManager.Policy(0, 0, 0); mBinderService.setNotificationPolicy("package", policy, false); - verify(zenModeHelper).setNotificationPolicy(eq(policy), anyInt(), anyInt()); + verify(zenModeHelper).setNotificationPolicy(any(), eq(policy), anyInt(), anyInt()); } @Test @@ -15878,9 +15907,9 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.setNotificationPolicy("package", policy, false); if (canSetGlobalPolicy) { - verify(zenModeHelper).setNotificationPolicy(eq(policy), anyInt(), anyInt()); + verify(zenModeHelper).setNotificationPolicy(any(), eq(policy), anyInt(), anyInt()); } else { - verify(zenModeHelper).applyGlobalPolicyAsImplicitZenRule(anyString(), anyInt(), + verify(zenModeHelper).applyGlobalPolicyAsImplicitZenRule(any(), anyString(), anyInt(), eq(policy)); } } @@ -15898,7 +15927,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { NotificationManager.Policy policy = new NotificationManager.Policy(0, 0, 0); mBinderService.setNotificationPolicy("package", policy, false); - verify(zenModeHelper).setNotificationPolicy(eq(policy), anyInt(), anyInt()); + verify(zenModeHelper).setNotificationPolicy(any(), eq(policy), anyInt(), anyInt()); } @Test @@ -15913,7 +15942,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.getNotificationPolicy("package"); - verify(zenHelper).getNotificationPolicyFromImplicitZenRule(eq("package")); + verify(zenHelper).getNotificationPolicyFromImplicitZenRule(any(), eq("package")); } @Test @@ -15928,7 +15957,7 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.setInterruptionFilter("package", INTERRUPTION_FILTER_PRIORITY, false); - verify(zenHelper).applyGlobalZenModeAsImplicitZenRule(eq("package"), anyInt(), + verify(zenHelper).applyGlobalZenModeAsImplicitZenRule(any(), eq("package"), anyInt(), eq(ZEN_MODE_IMPORTANT_INTERRUPTIONS)); } @@ -15945,9 +15974,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.setInterruptionFilter("package", INTERRUPTION_FILTER_PRIORITY, false); - verify(zenModeHelper).setManualZenMode(eq(ZEN_MODE_IMPORTANT_INTERRUPTIONS), eq(null), - eq(ZenModeConfig.ORIGIN_SYSTEM), anyString(), eq("package"), - anyInt()); + verify(zenModeHelper).setManualZenMode(any(), eq(ZEN_MODE_IMPORTANT_INTERRUPTIONS), + eq(null), eq(ZenModeConfig.ORIGIN_SYSTEM), anyString(), eq("package"), anyInt()); } @Test @@ -15991,10 +16019,10 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.setInterruptionFilter("package", INTERRUPTION_FILTER_PRIORITY, false); if (canSetGlobalPolicy) { - verify(zenModeHelper).setManualZenMode(eq(ZEN_MODE_IMPORTANT_INTERRUPTIONS), eq(null), - eq(ZenModeConfig.ORIGIN_APP), anyString(), eq("package"), anyInt()); + verify(zenModeHelper).setManualZenMode(any(), eq(ZEN_MODE_IMPORTANT_INTERRUPTIONS), + eq(null), eq(ZenModeConfig.ORIGIN_APP), anyString(), eq("package"), anyInt()); } else { - verify(zenModeHelper).applyGlobalZenModeAsImplicitZenRule(anyString(), anyInt(), + verify(zenModeHelper).applyGlobalZenModeAsImplicitZenRule(any(), anyString(), anyInt(), eq(ZEN_MODE_IMPORTANT_INTERRUPTIONS)); } } @@ -16013,8 +16041,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.requestInterruptionFilterFromListener(mock(INotificationListener.class), INTERRUPTION_FILTER_PRIORITY); - verify(mService.mZenModeHelper).applyGlobalZenModeAsImplicitZenRule(eq("pkg"), eq(mUid), - eq(ZEN_MODE_IMPORTANT_INTERRUPTIONS)); + verify(mService.mZenModeHelper).applyGlobalZenModeAsImplicitZenRule(any(), eq("pkg"), + eq(mUid), eq(ZEN_MODE_IMPORTANT_INTERRUPTIONS)); } @Test @@ -16031,9 +16059,9 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { mBinderService.requestInterruptionFilterFromListener(mock(INotificationListener.class), INTERRUPTION_FILTER_PRIORITY); - verify(mService.mZenModeHelper).setManualZenMode(eq(ZEN_MODE_IMPORTANT_INTERRUPTIONS), - eq(null), eq(ZenModeConfig.ORIGIN_SYSTEM), anyString(), - eq("pkg"), eq(mUid)); + verify(mService.mZenModeHelper).setManualZenMode(any(), + eq(ZEN_MODE_IMPORTANT_INTERRUPTIONS), eq(null), eq(ZenModeConfig.ORIGIN_SYSTEM), + anyString(), eq("pkg"), eq(mUid)); } @Test @@ -16111,8 +16139,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { throws Exception { setUpRealZenTest(); // Start with hasPriorityChannels=true, allowPriorityChannels=true ("default"). - mService.mZenModeHelper.setNotificationPolicy(new Policy(0, 0, 0, 0, - Policy.policyState(true, true), 0), + mService.mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, + new Policy(0, 0, 0, 0, Policy.policyState(true, true), 0), ZenModeConfig.ORIGIN_SYSTEM, Process.SYSTEM_UID); // The caller will supply states with "wrong" hasPriorityChannels. @@ -16142,8 +16170,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { throws Exception { setUpRealZenTest(); // Start with hasPriorityChannels=true, allowPriorityChannels=true ("default"). - mService.mZenModeHelper.setNotificationPolicy(new Policy(0, 0, 0, 0, - Policy.policyState(true, true), 0), + mService.mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, + new Policy(0, 0, 0, 0, Policy.policyState(true, true), 0), ZenModeConfig.ORIGIN_SYSTEM, Process.SYSTEM_UID); mService.setCallerIsNormalPackage(); diff --git a/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java index 1a1da0f6d408..e1b478cd1a1b 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java @@ -359,7 +359,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND, 0); - when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); + when(mMockZenModeHelper.getNotificationPolicy(any())).thenReturn(mTestNotificationPolicy); when(mAppOpsManager.noteOpNoThrow(anyInt(), anyInt(), anyString(), eq(null), anyString())).thenReturn(MODE_DEFAULT); @@ -493,7 +493,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { private void resetZenModeHelper() { reset(mMockZenModeHelper); - when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); + when(mMockZenModeHelper.getNotificationPolicy(any())).thenReturn(mTestNotificationPolicy); } private void setUpPackageWithUid(String packageName, int uid) throws Exception { @@ -2632,9 +2632,10 @@ public class PreferencesHelperTest extends UiServiceTestCase { uid, false); assertFalse(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, never()).updateHasPriorityChannels(anyBoolean()); + verify(mMockZenModeHelper, never()).updateHasPriorityChannels(any(), anyBoolean()); } else { - verify(mMockZenModeHelper, never()).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, never()).setNotificationPolicy(any(), any(), anyInt(), + anyInt()); } resetZenModeHelper(); @@ -2646,9 +2647,11 @@ public class PreferencesHelperTest extends UiServiceTestCase { uid, false); assertTrue(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(true)); + verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(UserHandle.CURRENT), + eq(true)); } else { - verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, times(1)).setNotificationPolicy(eq(UserHandle.CURRENT), + any(), anyInt(), anyInt()); } resetZenModeHelper(); @@ -2656,18 +2659,21 @@ public class PreferencesHelperTest extends UiServiceTestCase { mHelper.deleteNotificationChannel(PKG_N_MR1, uid, channel.getId(), uid, false); assertTrue(mHelper.areChannelsBypassingDnd()); // channel2 can still bypass DND if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, never()).updateHasPriorityChannels(anyBoolean()); + verify(mMockZenModeHelper, never()).updateHasPriorityChannels(any(), anyBoolean()); } else { - verify(mMockZenModeHelper, never()).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, never()).setNotificationPolicy(any(), any(), anyInt(), + anyInt()); } resetZenModeHelper(); mHelper.deleteNotificationChannel(PKG_N_MR1, uid, channel2.getId(), uid, false); assertFalse(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(false)); + verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(UserHandle.CURRENT), + eq(false)); } else { - verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, times(1)).setNotificationPolicy(eq(UserHandle.CURRENT), + any(), anyInt(), anyInt()); } resetZenModeHelper(); } @@ -2685,9 +2691,10 @@ public class PreferencesHelperTest extends UiServiceTestCase { uid, false); assertFalse(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, never()).updateHasPriorityChannels(anyBoolean()); + verify(mMockZenModeHelper, never()).updateHasPriorityChannels(any(), anyBoolean()); } else { - verify(mMockZenModeHelper, never()).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, never()).setNotificationPolicy(any(), any(), anyInt(), + anyInt()); } resetZenModeHelper(); @@ -2699,9 +2706,11 @@ public class PreferencesHelperTest extends UiServiceTestCase { assertTrue(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(true)); + verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(UserHandle.CURRENT), + eq(true)); } else { - verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, times(1)).setNotificationPolicy(eq(UserHandle.CURRENT), + any(), anyInt(), anyInt()); } resetZenModeHelper(); } @@ -2719,9 +2728,10 @@ public class PreferencesHelperTest extends UiServiceTestCase { uid, false); assertFalse(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, never()).updateHasPriorityChannels(anyBoolean()); + verify(mMockZenModeHelper, never()).updateHasPriorityChannels(any(), anyBoolean()); } else { - verify(mMockZenModeHelper, never()).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, never()).setNotificationPolicy(any(), any(), anyInt(), + anyInt()); } resetZenModeHelper(); @@ -2733,9 +2743,11 @@ public class PreferencesHelperTest extends UiServiceTestCase { uid, false); assertTrue(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(true)); + verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(UserHandle.CURRENT), + eq(true)); } else { - verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, times(1)).setNotificationPolicy(eq(UserHandle.CURRENT), + any(), anyInt(), anyInt()); } resetZenModeHelper(); @@ -2743,18 +2755,21 @@ public class PreferencesHelperTest extends UiServiceTestCase { mHelper.deleteNotificationChannel(PKG_N_MR1, uid, channel.getId(), uid, false); assertTrue(mHelper.areChannelsBypassingDnd()); // channel2 can still bypass DND if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, never()).updateHasPriorityChannels(anyBoolean()); + verify(mMockZenModeHelper, never()).updateHasPriorityChannels(any(), anyBoolean()); } else { - verify(mMockZenModeHelper, never()).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, never()).setNotificationPolicy(any(), any(), anyInt(), + anyInt()); } resetZenModeHelper(); mHelper.deleteNotificationChannel(PKG_N_MR1, uid, channel2.getId(), uid, false); assertFalse(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(false)); + verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(UserHandle.CURRENT), + eq(false)); } else { - verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, times(1)).setNotificationPolicy(eq(UserHandle.CURRENT), + any(), anyInt(), anyInt()); } resetZenModeHelper(); } @@ -2767,7 +2782,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { // start in a 'allowed to bypass dnd state' mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND, 0); - when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); + when(mMockZenModeHelper.getNotificationPolicy(any())).thenReturn(mTestNotificationPolicy); mHelper.syncChannelsBypassingDnd(); // create notification channel that can bypass dnd, but app is blocked @@ -2783,9 +2798,11 @@ public class PreferencesHelperTest extends UiServiceTestCase { uid, false); assertFalse(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(false)); + verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(UserHandle.CURRENT), + eq(false)); } else { - verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, times(1)).setNotificationPolicy(eq(UserHandle.CURRENT), + any(), anyInt(), anyInt()); } resetZenModeHelper(); } @@ -2798,7 +2815,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { // start in a 'allowed to bypass dnd state' mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND, 0); - when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); + when(mMockZenModeHelper.getNotificationPolicy(any())).thenReturn(mTestNotificationPolicy); mHelper.syncChannelsBypassingDnd(); // create notification channel that can bypass dnd, but app is blocked @@ -2809,9 +2826,11 @@ public class PreferencesHelperTest extends UiServiceTestCase { uid, false); assertFalse(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(false)); + verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(UserHandle.CURRENT), + eq(false)); } else { - verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, times(1)).setNotificationPolicy(eq(UserHandle.CURRENT), + any(), anyInt(), anyInt()); } resetZenModeHelper(); } @@ -2824,7 +2843,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { // start in a 'allowed to bypass dnd state' mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND, 0); - when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); + when(mMockZenModeHelper.getNotificationPolicy(any())).thenReturn(mTestNotificationPolicy); mHelper.syncChannelsBypassingDnd(); // create notification channel that can bypass dnd, but app is blocked @@ -2835,9 +2854,11 @@ public class PreferencesHelperTest extends UiServiceTestCase { uid, false); assertFalse(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(false)); + verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(UserHandle.CURRENT), + eq(false)); } else { - verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, times(1)).setNotificationPolicy(eq(UserHandle.CURRENT), + any(), anyInt(), anyInt()); } resetZenModeHelper(); } @@ -2855,9 +2876,10 @@ public class PreferencesHelperTest extends UiServiceTestCase { uid, false); assertFalse(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, never()).updateHasPriorityChannels(anyBoolean()); + verify(mMockZenModeHelper, never()).updateHasPriorityChannels(any(), anyBoolean()); } else { - verify(mMockZenModeHelper, never()).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, never()).setNotificationPolicy(any(), any(), anyInt(), + anyInt()); } resetZenModeHelper(); @@ -2867,9 +2889,11 @@ public class PreferencesHelperTest extends UiServiceTestCase { mHelper.updateNotificationChannel(PKG_N_MR1, uid, channel, true, SYSTEM_UID, true); assertTrue(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(true)); + verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(UserHandle.CURRENT), + eq(true)); } else { - verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, times(1)).setNotificationPolicy(eq(UserHandle.CURRENT), + any(), anyInt(), anyInt()); } resetZenModeHelper(); @@ -2879,9 +2903,11 @@ public class PreferencesHelperTest extends UiServiceTestCase { mHelper.updateNotificationChannel(PKG_N_MR1, uid, channel, true, SYSTEM_UID, true); assertFalse(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(false)); + verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(UserHandle.CURRENT), + eq(false)); } else { - verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, times(1)).setNotificationPolicy(eq(UserHandle.CURRENT), + any(), anyInt(), anyInt()); } resetZenModeHelper(); } @@ -2892,13 +2918,15 @@ public class PreferencesHelperTest extends UiServiceTestCase { // RankingHelper should change to false mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND, 0); - when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); + when(mMockZenModeHelper.getNotificationPolicy(any())).thenReturn(mTestNotificationPolicy); mHelper.syncChannelsBypassingDnd(); assertFalse(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(false)); + verify(mMockZenModeHelper, times(1)).updateHasPriorityChannels(eq(UserHandle.CURRENT), + eq(false)); } else { - verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, times(1)).setNotificationPolicy(eq(UserHandle.CURRENT), + any(), anyInt(), anyInt()); } resetZenModeHelper(); } @@ -2907,12 +2935,13 @@ public class PreferencesHelperTest extends UiServiceTestCase { public void testSetupNewZenModeHelper_cannotBypass() { // start notification policy off with mAreChannelsBypassingDnd = false mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, 0, 0); - when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); + when(mMockZenModeHelper.getNotificationPolicy(any())).thenReturn(mTestNotificationPolicy); assertFalse(mHelper.areChannelsBypassingDnd()); if (android.app.Flags.modesUi()) { - verify(mMockZenModeHelper, never()).updateHasPriorityChannels(anyBoolean()); + verify(mMockZenModeHelper, never()).updateHasPriorityChannels(any(), anyBoolean()); } else { - verify(mMockZenModeHelper, never()).setNotificationPolicy(any(), anyInt(), anyInt()); + verify(mMockZenModeHelper, never()).setNotificationPolicy(any(), any(), anyInt(), + anyInt()); } resetZenModeHelper(); } diff --git a/services/tests/uiservicestests/src/com/android/server/notification/RankingHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/RankingHelperTest.java index 5d4382a6331c..f90034614383 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/RankingHelperTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/RankingHelperTest.java @@ -155,7 +155,7 @@ public class RankingHelperTest extends UiServiceTestCase { mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND, 0); - when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy); + when(mMockZenModeHelper.getNotificationPolicy(any())).thenReturn(mTestNotificationPolicy); mHelper = new RankingHelper(getContext(), mHandler, mConfig, mMockZenModeHelper, mUsageStats, new String[] {ImportanceExtractor.class.getName()}, mock(IPlatformCompat.class), mGroupHelper); diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java index 8b3ac2baa7a0..a19659417b66 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java @@ -21,6 +21,7 @@ import static android.app.AutomaticZenRule.TYPE_IMMERSIVE; import static android.app.AutomaticZenRule.TYPE_SCHEDULE_TIME; import static android.app.AutomaticZenRule.TYPE_UNKNOWN; import static android.app.Flags.FLAG_MODES_API; +import static android.app.Flags.FLAG_MODES_MULTIUSER; import static android.app.Flags.FLAG_MODES_UI; import static android.app.NotificationManager.AUTOMATIC_RULE_STATUS_ACTIVATED; import static android.app.NotificationManager.AUTOMATIC_RULE_STATUS_DEACTIVATED; @@ -61,6 +62,7 @@ import static android.service.notification.Condition.STATE_TRUE; import static android.service.notification.ZenModeConfig.ORIGIN_APP; import static android.service.notification.ZenModeConfig.ORIGIN_INIT; import static android.service.notification.ZenModeConfig.ORIGIN_INIT_USER; +import static android.service.notification.ZenModeConfig.ORIGIN_SYSTEM; import static android.service.notification.ZenModeConfig.ORIGIN_UNKNOWN; import static android.service.notification.ZenModeConfig.ORIGIN_USER_IN_APP; import static android.service.notification.ZenModeConfig.ORIGIN_USER_IN_SYSTEMUI; @@ -689,13 +691,12 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule azr = new AutomaticZenRule.Builder("OriginalName", CONDITION_ID) .setInterruptionFilter(INTERRUPTION_FILTER_NONE) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - azr, ZenModeConfig.ORIGIN_SYSTEM, "reason", SYSTEM_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), azr, ORIGIN_SYSTEM, "reason", SYSTEM_UID); // Enable rule - mZenModeHelper.setAutomaticZenRuleState(ruleId, - new Condition(azr.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, + new Condition(azr.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, SYSTEM_UID); // Confirm that the consolidated policy doesn't allow anything @@ -723,13 +724,12 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule azr = new AutomaticZenRule.Builder("OriginalName", CONDITION_ID) .setInterruptionFilter(INTERRUPTION_FILTER_ALARMS) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - azr, ZenModeConfig.ORIGIN_SYSTEM, "reason", SYSTEM_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), azr, ORIGIN_SYSTEM, "reason", SYSTEM_UID); // Enable rule - mZenModeHelper.setAutomaticZenRuleState(ruleId, - new Condition(azr.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, + new Condition(azr.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, SYSTEM_UID); // Confirm that the consolidated policy allows only alarms and media and nothing else @@ -756,7 +756,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); // Set zen to priority-only with all notification sounds muted (so ringer will be muted) Policy totalSilence = new Policy(0, 0, 0); - mZenModeHelper.setNotificationPolicy(totalSilence, ORIGIN_APP, 1); + mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, totalSilence, ORIGIN_APP, 1); mZenModeHelper.mZenMode = ZEN_MODE_IMPORTANT_INTERRUPTIONS; // 2. verify ringer is unchanged @@ -793,8 +793,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { public void testRingerAffectedStreamsPriorityOnly() { // in priority only mode: // ringtone, notification and system streams are affected by ringer mode - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, Uri.EMPTY, - ORIGIN_APP, "test", "caller", 1); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, + Uri.EMPTY, ORIGIN_APP, "test", "caller", 1); ZenModeHelper.RingerModeDelegate ringerModeDelegateRingerMuted = mZenModeHelper.new RingerModeDelegate(); @@ -810,9 +810,10 @@ public class ZenModeHelperTest extends UiServiceTestCase { // even when ringer is muted (since all ringer sounds cannot bypass DND), // system stream is still affected by ringer mode - mZenModeHelper.setNotificationPolicy(new Policy(0, 0, 0), ORIGIN_APP, 1); - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, Uri.EMPTY, - ORIGIN_APP, "test", "caller", 1); + mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, new Policy(0, 0, 0), ORIGIN_APP, + 1); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, + Uri.EMPTY, ORIGIN_APP, "test", "caller", 1); ZenModeHelper.RingerModeDelegate ringerModeDelegateRingerNotMuted = mZenModeHelper.new RingerModeDelegate(); @@ -919,7 +920,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { // apply zen off multiple times - verify ringer is not set to normal when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_SILENT); for (int i = 0; i < 3; i++) { - mZenModeHelper.setManualZenMode(ZEN_MODE_OFF, Uri.EMPTY, + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_OFF, Uri.EMPTY, ORIGIN_APP, "test", "caller", 1); } verify(mAudioManager, never()).setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL, @@ -944,7 +945,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_SILENT); for (int i = 0; i < 3; i++) { // if zen doesn't change, zen should not reapply itself to the ringer - mZenModeHelper.setManualZenMode(ZEN_MODE_OFF, Uri.EMPTY, + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_OFF, Uri.EMPTY, ORIGIN_APP, "test", "caller", 1); } verify(mAudioManager, never()).setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL, @@ -969,7 +970,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); for (int i = 0; i < 3; i++) { // if zen doesn't change, zen should not reapply itself to the ringer - mZenModeHelper.setManualZenMode(ZEN_MODE_OFF, Uri.EMPTY, + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_OFF, Uri.EMPTY, ORIGIN_APP, "test", "caller", 1); } verify(mAudioManager, never()).setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL, @@ -985,9 +986,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { reset(mAudioManager); // Turn manual zen mode on - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, - ORIGIN_APP, - null, "test", CUSTOM_PKG_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, + ORIGIN_APP, null, "test", CUSTOM_PKG_UID); // audio manager shouldn't do anything until the handler processes its messages verify(mAudioManager, never()).updateRingerModeAffectedStreamsInternal(); @@ -1012,6 +1012,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { // Turn manual zen mode on mZenModeHelper.setManualZenMode( + UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, ORIGIN_APP, @@ -1019,6 +1020,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { "test", CUSTOM_PKG_UID); mZenModeHelper.setManualZenMode( + UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, ORIGIN_APP, @@ -1042,19 +1044,22 @@ public class ZenModeHelperTest extends UiServiceTestCase { @Test public void testParcelConfig() { - mZenModeHelper.setNotificationPolicy(new Policy(PRIORITY_CATEGORY_EVENTS + mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, + new Policy(PRIORITY_CATEGORY_EVENTS | PRIORITY_CATEGORY_MESSAGES | PRIORITY_CATEGORY_REPEAT_CALLERS | PRIORITY_CATEGORY_CONVERSATIONS, PRIORITY_SENDERS_STARRED, PRIORITY_SENDERS_STARRED, 0, CONVERSATION_SENDERS_ANYONE), ORIGIN_UNKNOWN, 1); - mZenModeHelper.setManualZenRuleDeviceEffects(new ZenDeviceEffects.Builder() - .setShouldDimWallpaper(true) - .setShouldDisplayGrayscale(true) - .setShouldUseNightMode(true) - .build(), ORIGIN_UNKNOWN, "test", 1); - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, Uri.EMPTY, - ORIGIN_UNKNOWN, "test", "me", 1); + mZenModeHelper.setManualZenRuleDeviceEffects(UserHandle.CURRENT, + new ZenDeviceEffects.Builder() + .setShouldDimWallpaper(true) + .setShouldDisplayGrayscale(true) + .setShouldUseNightMode(true) + .build(), + ORIGIN_UNKNOWN, "test", 1); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, + Uri.EMPTY, ORIGIN_UNKNOWN, "test", "me", 1); ZenModeConfig actual = mZenModeHelper.mConfig.copy(); @@ -1063,18 +1068,21 @@ public class ZenModeHelperTest extends UiServiceTestCase { @Test public void testWriteXml() throws Exception { - mZenModeHelper.setNotificationPolicy(new Policy(PRIORITY_CATEGORY_EVENTS + mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, + new Policy(PRIORITY_CATEGORY_EVENTS | PRIORITY_CATEGORY_MESSAGES | PRIORITY_CATEGORY_REPEAT_CALLERS | PRIORITY_CATEGORY_CONVERSATIONS, PRIORITY_SENDERS_STARRED, PRIORITY_SENDERS_STARRED, SUPPRESSED_EFFECT_BADGE, CONVERSATION_SENDERS_ANYONE), ORIGIN_UNKNOWN, 1); - mZenModeHelper.setManualZenRuleDeviceEffects(new ZenDeviceEffects.Builder() - .setShouldDimWallpaper(true) - .setShouldDisplayGrayscale(true) - .build(), ORIGIN_UNKNOWN, "test", 1); - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, Uri.EMPTY, - ORIGIN_UNKNOWN, "test", "me", 1); + mZenModeHelper.setManualZenRuleDeviceEffects(UserHandle.CURRENT, + new ZenDeviceEffects.Builder() + .setShouldDimWallpaper(true) + .setShouldDisplayGrayscale(true) + .build(), + ORIGIN_UNKNOWN, "test", 1); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, + Uri.EMPTY, ORIGIN_UNKNOWN, "test", "me", 1); ZenModeConfig expected = mZenModeHelper.mConfig.copy(); if (Flags.modesUi()) { @@ -1094,9 +1102,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { @Test public void testProto() throws InvalidProtocolBufferException { - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, - Flags.modesApi() ? ORIGIN_USER_IN_SYSTEMUI : ZenModeConfig.ORIGIN_SYSTEM, - null, "test", CUSTOM_PKG_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, + Flags.modesApi() ? ORIGIN_USER_IN_SYSTEMUI : ORIGIN_SYSTEM, null, + "test", CUSTOM_PKG_UID); mZenModeHelper.mConfig.automaticRules = new ArrayMap<>(); // no automatic rules @@ -1319,8 +1327,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { List<StatsEvent> events = new LinkedList<>(); mZenModeHelper.pullRules(events); - mZenModeHelper.removeAutomaticZenRule(CUSTOM_RULE_ID, ORIGIN_APP, "test", - CUSTOM_PKG_UID); + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, CUSTOM_RULE_ID, ORIGIN_APP, + "test", CUSTOM_PKG_UID); assertTrue(-1 == mZenModeHelper.mRulesUidCache.getOrDefault(CUSTOM_PKG_NAME + "|" + 0, -1)); } @@ -1348,9 +1356,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { public void testProtoWithManualRule() throws Exception { setupZenConfig(); mZenModeHelper.mConfig.automaticRules = getCustomAutomaticRules(); - mZenModeHelper.setManualZenMode(INTERRUPTION_FILTER_PRIORITY, Uri.EMPTY, - ORIGIN_APP, - "test", "me", 1); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, INTERRUPTION_FILTER_PRIORITY, Uri.EMPTY, + ORIGIN_APP, "test", "me", 1); List<StatsEvent> events = new LinkedList<>(); mZenModeHelper.pullRules(events); @@ -1457,7 +1464,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { setupZenConfig(); // Turn on manual zen mode - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, ORIGIN_USER_IN_SYSTEMUI, "", "someCaller", SYSTEM_UID); ZenModeConfig original = mZenModeHelper.mConfig.copy(); assertThat(original.isManualActive()).isTrue(); @@ -1574,7 +1581,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { @Test public void testReadXmlRulesNotOverridden() throws Exception { setupZenConfig(); - Policy originalPolicy = mZenModeHelper.getNotificationPolicy(); + Policy originalPolicy = mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT); // automatic zen rule is enabled on upgrade so rules should not be overriden to default ArrayMap<String, ZenModeConfig.ZenRule> enabledAutoRule = new ArrayMap<>(); @@ -1597,7 +1604,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { mZenModeHelper.readXml(parser, false, UserHandle.USER_ALL); assertTrue(mZenModeHelper.mConfig.automaticRules.containsKey("customRule")); - assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy()); + assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT)); } @Test @@ -1727,7 +1734,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { @Test public void testReadXmlResetDefaultRules() throws Exception { setupZenConfig(); - Policy originalPolicy = mZenModeHelper.getNotificationPolicy(); + Policy originalPolicy = mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT); // no enabled automatic zen rules and no default rules // so rules should be overridden by default rules @@ -1748,13 +1755,13 @@ public class ZenModeHelperTest extends UiServiceTestCase { assertTrue(rules.containsKey(defaultId)); } - assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy()); + assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT)); } @Test public void testReadXmlAllDisabledRulesResetDefaultRules() throws Exception { setupZenConfig(); - Policy originalPolicy = mZenModeHelper.getNotificationPolicy(); + Policy originalPolicy = mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT); // all automatic zen rules are disabled on upgrade (and default rules don't already exist) // so rules should be overriden by default rules @@ -1785,14 +1792,14 @@ public class ZenModeHelperTest extends UiServiceTestCase { } assertFalse(rules.containsKey("customRule")); - assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy()); + assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT)); } @Test @DisableFlags(FLAG_MODES_UI) // modes_ui has only 1 default rule public void testReadXmlOnlyOneDefaultRuleExists() throws Exception { setupZenConfig(); - Policy originalPolicy = mZenModeHelper.getNotificationPolicy(); + Policy originalPolicy = mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT); // all automatic zen rules are disabled on upgrade and only one default rule exists // so rules should be overriden to the default rules @@ -1839,13 +1846,13 @@ public class ZenModeHelperTest extends UiServiceTestCase { } assertThat(rules).doesNotContainKey("customRule"); - assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy()); + assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT)); } @Test public void testReadXmlDefaultRulesExist() throws Exception { setupZenConfig(); - Policy originalPolicy = mZenModeHelper.getNotificationPolicy(); + Policy originalPolicy = mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT); // Default rules exist so rules should not be overridden by defaults ArrayMap<String, ZenModeConfig.ZenRule> automaticRules = new ArrayMap<>(); @@ -1910,7 +1917,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { } assertThat(rules).containsKey("customRule"); - assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy()); + assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT)); List<StatsEvent> events = new LinkedList<>(); mZenModeHelper.pullRules(events); @@ -1923,7 +1930,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { // When reading XML for something that is already on the modes API system, make sure no // rules' policies get changed. setupZenConfig(); - Policy originalPolicy = mZenModeHelper.getNotificationPolicy(); + Policy originalPolicy = mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT); // Shared for rules ArrayMap<String, ZenModeConfig.ZenRule> enabledAutoRules = new ArrayMap<>(); @@ -1955,7 +1962,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { mZenModeHelper.readXml(parser, false, UserHandle.USER_ALL); // basic check: global config maintained - assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy()); + assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT)); // Find our automatic rules. ArrayMap<String, ZenModeConfig.ZenRule> rules = mZenModeHelper.mConfig.automaticRules; @@ -1972,7 +1979,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { // a custom policy matching the global config for any automatic rule with no specified // policy. setupZenConfig(); - Policy originalPolicy = mZenModeHelper.getNotificationPolicy(); + Policy originalPolicy = mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT); ArrayMap<String, ZenModeConfig.ZenRule> enabledAutoRule = new ArrayMap<>(); ZenModeConfig.ZenRule customRule = new ZenModeConfig.ZenRule(); @@ -1994,7 +2001,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { mZenModeHelper.readXml(parser, false, UserHandle.USER_ALL); // basic check: global config maintained - assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy()); + assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT)); // Find our automatic rule and check that it has a policy set now ArrayMap<String, ZenModeConfig.ZenRule> rules = mZenModeHelper.mConfig.automaticRules; @@ -2024,7 +2031,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { // underspecified ZenPolicy, we fill in all of the gaps with things from the global config // in order to maintain consistency of behavior. setupZenConfig(); - Policy originalPolicy = mZenModeHelper.getNotificationPolicy(); + Policy originalPolicy = mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT); ArrayMap<String, ZenModeConfig.ZenRule> enabledAutoRule = new ArrayMap<>(); ZenModeConfig.ZenRule customRule = new ZenModeConfig.ZenRule(); @@ -2051,7 +2058,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { mZenModeHelper.readXml(parser, false, UserHandle.USER_ALL); // basic check: global config maintained - assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy()); + assertEquals(originalPolicy, mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT)); // Find our automatic rule and check that it has a policy set now ArrayMap<String, ZenModeConfig.ZenRule> rules = mZenModeHelper.mConfig.automaticRules; @@ -2345,8 +2352,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); // We need the package name to be something that's not "android" so there aren't any // existing rules under that package. - String id = mZenModeHelper.addAutomaticZenRule("pkgname", zenRule, ORIGIN_APP, - "test", CUSTOM_PKG_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "pkgname", zenRule, + ORIGIN_APP, "test", CUSTOM_PKG_UID); assertNotNull(id); } try { @@ -2356,8 +2363,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule("pkgname", zenRule, ORIGIN_APP, - "test", CUSTOM_PKG_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "pkgname", zenRule, + ORIGIN_APP, "test", CUSTOM_PKG_UID); fail("allowed too many rules to be created"); } catch (IllegalArgumentException e) { // yay @@ -2377,8 +2384,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(si), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule("pkgname", zenRule, ORIGIN_APP, - "test", CUSTOM_PKG_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "pkgname", zenRule, + ORIGIN_APP, "test", CUSTOM_PKG_UID); assertNotNull(id); } try { @@ -2388,8 +2395,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule("pkgname", zenRule, ORIGIN_APP, - "test", CUSTOM_PKG_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "pkgname", zenRule, + ORIGIN_APP, "test", CUSTOM_PKG_UID); fail("allowed too many rules to be created"); } catch (IllegalArgumentException e) { // yay @@ -2409,8 +2416,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(si), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule("pkgname", zenRule, ORIGIN_APP, - "test", CUSTOM_PKG_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "pkgname", zenRule, + ORIGIN_APP, "test", CUSTOM_PKG_UID); assertNotNull(id); } try { @@ -2420,8 +2427,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule("pkgname", zenRule, ORIGIN_APP, - "test", CUSTOM_PKG_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "pkgname", zenRule, + ORIGIN_APP, "test", CUSTOM_PKG_UID); fail("allowed too many rules to be created"); } catch (IllegalArgumentException e) { // yay @@ -2436,8 +2443,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule("android", zenRule, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "android", zenRule, + ORIGIN_SYSTEM, "test", SYSTEM_UID); assertTrue(id != null); ZenModeConfig.ZenRule ruleInConfig = mZenModeHelper.mConfig.automaticRules.get(id); @@ -2457,8 +2464,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { new ComponentName("android", "ScheduleConditionProvider"), ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule("android", zenRule, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "android", zenRule, + ORIGIN_SYSTEM, "test", SYSTEM_UID); assertTrue(id != null); ZenModeConfig.ZenRule ruleInConfig = mZenModeHelper.mConfig.automaticRules.get(id); @@ -2483,8 +2490,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { new ComponentName("android", "ScheduleConditionProvider"), ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id1 = mZenModeHelper.addAutomaticZenRule("android", zenRule1, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id1 = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "android", zenRule1, + ORIGIN_SYSTEM, "test", SYSTEM_UID); // Zen rule with partially-filled policy: should get all of the filled fields set, and the // rest filled with default state @@ -2498,8 +2505,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { .showFullScreenIntent(true) .build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id2 = mZenModeHelper.addAutomaticZenRule("android", zenRule2, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id2 = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "android", zenRule2, + ORIGIN_SYSTEM, "test", SYSTEM_UID); // rule 1 should exist assertThat(id1).isNotNull(); @@ -2544,9 +2551,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(null, zenRule, ORIGIN_APP, "test", - CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState(zenRule.getConditionId(), + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, null, zenRule, + ORIGIN_APP, "test", CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, zenRule.getConditionId(), new Condition(zenRule.getConditionId(), "", STATE_TRUE), ORIGIN_APP, CUSTOM_PKG_UID); @@ -2564,8 +2571,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(null, zenRule, ORIGIN_APP, "test", - CUSTOM_PKG_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, null, zenRule, + ORIGIN_APP, "test", CUSTOM_PKG_UID); AutomaticZenRule zenRule2 = new AutomaticZenRule("NEW", null, @@ -2574,7 +2581,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - mZenModeHelper.updateAutomaticZenRule(id, zenRule2, ORIGIN_APP, "", CUSTOM_PKG_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, id, zenRule2, ORIGIN_APP, "", + CUSTOM_PKG_UID); ZenModeConfig.ZenRule ruleInConfig = mZenModeHelper.mConfig.automaticRules.get(id); assertEquals("NEW", ruleInConfig.name); @@ -2589,15 +2597,16 @@ public class ZenModeHelperTest extends UiServiceTestCase { new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(null, zenRule, ORIGIN_APP, "test", - CUSTOM_PKG_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, null, zenRule, + ORIGIN_APP, "test", CUSTOM_PKG_UID); assertTrue(id != null); ZenModeConfig.ZenRule ruleInConfig = mZenModeHelper.mConfig.automaticRules.get(id); assertTrue(ruleInConfig != null); assertEquals(zenRule.getName(), ruleInConfig.name); - mZenModeHelper.removeAutomaticZenRule(id, ORIGIN_APP, "test", CUSTOM_PKG_UID); + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, id, ORIGIN_APP, "test", + CUSTOM_PKG_UID); assertNull(mZenModeHelper.mConfig.automaticRules.get(id)); } @@ -2609,16 +2618,16 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), new ZenPolicy.Builder().build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(null, zenRule, ORIGIN_APP, "test", - CUSTOM_PKG_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, null, zenRule, + ORIGIN_APP, "test", CUSTOM_PKG_UID); assertTrue(id != null); ZenModeConfig.ZenRule ruleInConfig = mZenModeHelper.mConfig.automaticRules.get(id); assertTrue(ruleInConfig != null); assertEquals(zenRule.getName(), ruleInConfig.name); - mZenModeHelper.removeAutomaticZenRules(mContext.getPackageName(), ORIGIN_APP, "test", - CUSTOM_PKG_UID); + mZenModeHelper.removeAutomaticZenRules(UserHandle.CURRENT, mContext.getPackageName(), + ORIGIN_APP, "test", CUSTOM_PKG_UID); assertNull(mZenModeHelper.mConfig.automaticRules.get(id)); } @@ -2633,18 +2642,18 @@ public class ZenModeHelperTest extends UiServiceTestCase { new ComponentName(mPkg, "ScheduleConditionProvider"), sharedUri, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(mPkg, zenRule, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, zenRule, + ORIGIN_SYSTEM, "test", SYSTEM_UID); AutomaticZenRule zenRule2 = new AutomaticZenRule("name2", new ComponentName(mPkg, "ScheduleConditionProvider"), sharedUri, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id2 = mZenModeHelper.addAutomaticZenRule(mPkg, zenRule2, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id2 = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, zenRule2, + ORIGIN_SYSTEM, "test", SYSTEM_UID); Condition condition = new Condition(sharedUri, "", STATE_TRUE); - mZenModeHelper.setAutomaticZenRuleState(sharedUri, condition, - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, sharedUri, condition, + ORIGIN_SYSTEM, SYSTEM_UID); for (ZenModeConfig.ZenRule rule : mZenModeHelper.mConfig.automaticRules.values()) { if (rule.id.equals(id)) { @@ -2658,8 +2667,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { } condition = new Condition(sharedUri, "", STATE_FALSE); - mZenModeHelper.setAutomaticZenRuleState(sharedUri, condition, - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, sharedUri, condition, + ORIGIN_SYSTEM, SYSTEM_UID); for (ZenModeConfig.ZenRule rule : mZenModeHelper.mConfig.automaticRules.values()) { if (rule.id.equals(id)) { @@ -2689,14 +2698,15 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setShouldMaximizeDoze(true) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), new AutomaticZenRule.Builder("Rule", CONDITION_ID) .setOwner(OWNER) .setDeviceEffects(zde) .build(), ORIGIN_APP, "reasons", 0); - AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); assertThat(savedRule.getDeviceEffects()).isEqualTo( new ZenDeviceEffects.Builder() .setShouldDisplayGrayscale(true) @@ -2722,14 +2732,15 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setShouldMaximizeDoze(true) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), new AutomaticZenRule.Builder("Rule", CONDITION_ID) .setOwner(OWNER) .setDeviceEffects(zde) .build(), - ZenModeConfig.ORIGIN_SYSTEM, "reasons", 0); + ORIGIN_SYSTEM, "reasons", 0); - AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); assertThat(savedRule.getDeviceEffects()).isEqualTo(zde); } @@ -2749,7 +2760,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setShouldMaximizeDoze(true) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), new AutomaticZenRule.Builder("Rule", CONDITION_ID) .setOwner(OWNER) .setDeviceEffects(zde) @@ -2757,7 +2769,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { ORIGIN_USER_IN_SYSTEMUI, "reasons", 0); - AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); assertThat(savedRule.getDeviceEffects()).isEqualTo(zde); } @@ -2769,26 +2781,27 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setShouldDisableTapToWake(true) .addExtraEffect("extra") .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), new AutomaticZenRule.Builder("Rule", CONDITION_ID) .setOwner(OWNER) .setDeviceEffects(original) .build(), - ZenModeConfig.ORIGIN_SYSTEM, "reasons", 0); + ORIGIN_SYSTEM, "reasons", 0); ZenDeviceEffects updateFromApp = new ZenDeviceEffects.Builder() .setShouldUseNightMode(true) // Good .setShouldMaximizeDoze(true) // Bad .addExtraEffect("should be rejected") // Bad .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, new AutomaticZenRule.Builder("Rule", CONDITION_ID) .setOwner(OWNER) .setDeviceEffects(updateFromApp) .build(), ORIGIN_APP, "reasons", 0); - AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); assertThat(savedRule.getDeviceEffects()).isEqualTo( new ZenDeviceEffects.Builder() .setShouldUseNightMode(true) // From update. @@ -2803,24 +2816,25 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenDeviceEffects original = new ZenDeviceEffects.Builder() .setShouldDisableTapToWake(true) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), new AutomaticZenRule.Builder("Rule", CONDITION_ID) .setOwner(OWNER) .setDeviceEffects(original) .build(), - ZenModeConfig.ORIGIN_SYSTEM, "reasons", 0); + ORIGIN_SYSTEM, "reasons", 0); ZenDeviceEffects updateFromSystem = new ZenDeviceEffects.Builder() .setShouldUseNightMode(true) // Good .setShouldMaximizeDoze(true) // Also good .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, new AutomaticZenRule.Builder("Rule", CONDITION_ID) .setDeviceEffects(updateFromSystem) .build(), - ZenModeConfig.ORIGIN_SYSTEM, "reasons", 0); + ORIGIN_SYSTEM, "reasons", 0); - AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); assertThat(savedRule.getDeviceEffects()).isEqualTo(updateFromSystem); } @@ -2830,12 +2844,13 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenDeviceEffects original = new ZenDeviceEffects.Builder() .setShouldDisableTapToWake(true) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), new AutomaticZenRule.Builder("Rule", CONDITION_ID) .setOwner(OWNER) .setDeviceEffects(original) .build(), - ZenModeConfig.ORIGIN_SYSTEM, "reasons", 0); + ORIGIN_SYSTEM, "reasons", 0); ZenDeviceEffects updateFromUser = new ZenDeviceEffects.Builder() .setShouldUseNightMode(true) @@ -2844,13 +2859,13 @@ public class ZenModeHelperTest extends UiServiceTestCase { // even with this line removed, tap to wake would be set to false. .setShouldDisableTapToWake(false) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, new AutomaticZenRule.Builder("Rule", CONDITION_ID) .setDeviceEffects(updateFromUser) .build(), ORIGIN_USER_IN_SYSTEMUI, "reasons", 0); - AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); assertThat(savedRule.getDeviceEffects()).isEqualTo(updateFromUser); } @@ -2860,7 +2875,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { public void updateAutomaticZenRule_nullPolicy_doesNothing() { // Test that when updateAutomaticZenRule is called with a null policy, nothing changes // about the existing policy. - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), new AutomaticZenRule.Builder("Rule", CONDITION_ID) .setOwner(OWNER) .setZenPolicy(new ZenPolicy.Builder() @@ -2869,13 +2885,13 @@ public class ZenModeHelperTest extends UiServiceTestCase { .build(), ORIGIN_APP, "reasons", 0); - mZenModeHelper.updateAutomaticZenRule(ruleId, + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, new AutomaticZenRule.Builder("Rule", CONDITION_ID) // no zen policy .build(), ORIGIN_APP, "reasons", 0); - AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); assertThat(savedRule.getZenPolicy().getPriorityCategoryCalls()) .isEqualTo(STATE_DISALLOW); } @@ -2886,7 +2902,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { // Test that when updating an automatic zen rule with an existing policy, the newly set // fields overwrite those from the previous policy, but unset fields in the new policy // keep values from the previous one. - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), new AutomaticZenRule.Builder("Rule", CONDITION_ID) .setOwner(OWNER) .setZenPolicy(new ZenPolicy.Builder() @@ -2895,9 +2912,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { .allowReminders(true) .build()) .build(), - ZenModeConfig.ORIGIN_SYSTEM, "reasons", 0); + ORIGIN_SYSTEM, "reasons", 0); - mZenModeHelper.updateAutomaticZenRule(ruleId, + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, new AutomaticZenRule.Builder("Rule", CONDITION_ID) .setZenPolicy(new ZenPolicy.Builder() .allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS) @@ -2905,7 +2922,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { .build(), ORIGIN_APP, "reasons", 0); - AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule savedRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); assertThat(savedRule.getZenPolicy().getPriorityCategoryCalls()) .isEqualTo(STATE_ALLOW); // from update assertThat(savedRule.getZenPolicy().getPriorityCallSenders()) @@ -2931,9 +2948,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule bedtime = new AutomaticZenRule.Builder("Bedtime Mode (TM)", CONDITION_ID) .setType(TYPE_BEDTIME) .build(); - String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule("pkg", bedtime, - ORIGIN_APP, - "reason", CUSTOM_PKG_UID); + String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "pkg", + bedtime, ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()).containsExactly(bedtimeRuleId); } @@ -2952,9 +2968,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule bedtime = new AutomaticZenRule.Builder("Bedtime Mode (TM)", CONDITION_ID) .setType(TYPE_BEDTIME) .build(); - String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule("pkg", bedtime, - ORIGIN_APP, - "reason", CUSTOM_PKG_UID); + String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "pkg", + bedtime, ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()).containsExactly( ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID, bedtimeRuleId); @@ -2974,9 +2989,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule bedtime = new AutomaticZenRule.Builder("Bedtime Mode (TM)", CONDITION_ID) .setType(TYPE_BEDTIME) .build(); - String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule("pkg", bedtime, - ORIGIN_APP, - "reason", CUSTOM_PKG_UID); + String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "pkg", + bedtime, ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()).containsExactly( ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID, bedtimeRuleId); @@ -2995,16 +3009,16 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule futureBedtime = new AutomaticZenRule.Builder("Bedtime (?)", CONDITION_ID) .build(); - String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule(mPkg, futureBedtime, - ORIGIN_APP, "reason", CUSTOM_PKG_UID); + String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, + futureBedtime, ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()) .containsExactly(sleepingRule.id, bedtimeRuleId); AutomaticZenRule bedtime = new AutomaticZenRule.Builder("Bedtime (!)", CONDITION_ID) .setType(TYPE_BEDTIME) .build(); - mZenModeHelper.updateAutomaticZenRule(bedtimeRuleId, bedtime, ORIGIN_APP, "reason", - CUSTOM_PKG_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, bedtimeRuleId, bedtime, + ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.keySet()).containsExactly(bedtimeRuleId); } @@ -3015,16 +3029,16 @@ public class ZenModeHelperTest extends UiServiceTestCase { setupZenConfig(); // note that caller=null because that's how it comes in from NMS.setZenMode - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, - ZenModeConfig.ORIGIN_SYSTEM, "", null, SYSTEM_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, + ORIGIN_SYSTEM, "", null, SYSTEM_UID); // confirm that setting zen mode via setManualZenMode changed the zen mode correctly assertEquals(ZEN_MODE_IMPORTANT_INTERRUPTIONS, mZenModeHelper.mZenMode); assertEquals(true, mZenModeHelper.mConfig.manualRule.allowManualInvocation); // and also that it works to turn it back off again - mZenModeHelper.setManualZenMode(Global.ZEN_MODE_OFF, null, ZenModeConfig.ORIGIN_SYSTEM, - "", null, SYSTEM_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, Global.ZEN_MODE_OFF, null, + ORIGIN_SYSTEM, "", null, SYSTEM_UID); assertEquals(Global.ZEN_MODE_OFF, mZenModeHelper.mZenMode); } @@ -3039,22 +3053,21 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule activeRule = new AutomaticZenRule.Builder("Test", CONDITION_ID) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); - String activeRuleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - activeRule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState(activeRuleId, CONDITION_TRUE, - ORIGIN_APP, - CUSTOM_PKG_UID); + String activeRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), activeRule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, activeRuleId, + CONDITION_TRUE, ORIGIN_APP, CUSTOM_PKG_UID); AutomaticZenRule inactiveRule = new AutomaticZenRule.Builder("Test", CONDITION_ID) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); - String inactiveRuleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - inactiveRule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); + String inactiveRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), inactiveRule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); assertWithMessage("Failure for origin " + origin.name()) .that(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); // User turns DND off. - mZenModeHelper.setManualZenMode(ZEN_MODE_OFF, null, origin.value(), + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_OFF, null, origin.value(), "snoozing", "systemui", SYSTEM_UID); assertWithMessage("Failure for origin " + origin.name()) .that(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_OFF); @@ -3078,21 +3091,20 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule activeRule = new AutomaticZenRule.Builder("Test", CONDITION_ID) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); - String activeRuleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - activeRule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState(activeRuleId, CONDITION_TRUE, - ORIGIN_APP, - CUSTOM_PKG_UID); + String activeRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), activeRule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, activeRuleId, + CONDITION_TRUE, ORIGIN_APP, CUSTOM_PKG_UID); AutomaticZenRule inactiveRule = new AutomaticZenRule.Builder("Test", CONDITION_ID) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); - String inactiveRuleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - inactiveRule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); + String inactiveRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), inactiveRule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); // User turns DND off. - mZenModeHelper.setManualZenMode(ZEN_MODE_OFF, null, origin.value(), + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_OFF, null, origin.value(), "snoozing", "systemui", SYSTEM_UID); ZenModeConfig config = mZenModeHelper.mConfig; if (origin == ZenChangeOrigin.ORIGIN_USER_IN_SYSTEMUI) { @@ -3123,15 +3135,15 @@ public class ZenModeHelperTest extends UiServiceTestCase { setupZenConfig(); // note that caller=null because that's how it comes in from NMS.setZenMode - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, - ZenModeConfig.ORIGIN_SYSTEM, "", null, SYSTEM_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, + ORIGIN_SYSTEM, "", null, SYSTEM_UID); // confirm that setting zen mode via setManualZenMode changed the zen mode correctly assertEquals(ZEN_MODE_IMPORTANT_INTERRUPTIONS, mZenModeHelper.mZenMode); // and also that it works to turn it back off again - mZenModeHelper.setManualZenMode(ZEN_MODE_OFF, null, ZenModeConfig.ORIGIN_SYSTEM, "", - null, SYSTEM_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_OFF, null, + ORIGIN_SYSTEM, "", null, SYSTEM_UID); assertEquals(ZEN_MODE_OFF, mZenModeHelper.mZenMode); } @@ -3144,14 +3156,13 @@ public class ZenModeHelperTest extends UiServiceTestCase { // Turn zen mode on (to important_interruptions) // Need to additionally call the looper in order to finish the post-apply-config process - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, - Flags.modesApi() ? ORIGIN_USER_IN_SYSTEMUI : ZenModeConfig.ORIGIN_SYSTEM, "", - null, SYSTEM_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, + Flags.modesApi() ? ORIGIN_USER_IN_SYSTEMUI : ORIGIN_SYSTEM, "", null, SYSTEM_UID); // Now turn zen mode off, but via a different package UID -- this should get registered as // "not an action by the user" because some other app is changing zen mode - mZenModeHelper.setManualZenMode(ZEN_MODE_OFF, null, ORIGIN_APP, "", null, - CUSTOM_PKG_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_OFF, null, ORIGIN_APP, "", + null, CUSTOM_PKG_UID); // In total, this should be 2 loggable changes assertEquals(2, mZenModeEventLogger.numLoggedChanges()); @@ -3220,22 +3231,21 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, - ORIGIN_APP, "test", CUSTOM_PKG_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_APP, "test", CUSTOM_PKG_UID); // Event 1: Mimic the rule coming on automatically by setting the Condition to STATE_TRUE // Note that pre-modes_ui, this event serves as a test that automatic changes to an app's // that look like they're coming from the system are attributed to the app, but when // modes_ui is true, we opt to trust the provided change origin. - mZenModeHelper.setAutomaticZenRuleState(id, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, new Condition(zenRule.getConditionId(), "", STATE_TRUE), - Flags.modesUi() ? ORIGIN_APP : ZenModeConfig.ORIGIN_SYSTEM, - CUSTOM_PKG_UID); + Flags.modesUi() ? ORIGIN_APP : ORIGIN_SYSTEM, CUSTOM_PKG_UID); // Event 2: "User" turns off the automatic rule (sets it to not enabled) zenRule.setEnabled(false); - mZenModeHelper.updateAutomaticZenRule(id, zenRule, - Flags.modesApi() ? ORIGIN_USER_IN_SYSTEMUI : ZenModeConfig.ORIGIN_SYSTEM, "", + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, id, zenRule, + Flags.modesApi() ? ORIGIN_USER_IN_SYSTEMUI : ORIGIN_SYSTEM, "", SYSTEM_UID); AutomaticZenRule systemRule = new AutomaticZenRule("systemRule", @@ -3244,18 +3254,19 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String systemId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), systemRule, - Flags.modesApi() ? ORIGIN_USER_IN_SYSTEMUI : ZenModeConfig.ORIGIN_SYSTEM, "test", + String systemId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), systemRule, + Flags.modesApi() ? ORIGIN_USER_IN_SYSTEMUI : ORIGIN_SYSTEM, "test", SYSTEM_UID); // Event 3: turn on the system rule - mZenModeHelper.setAutomaticZenRuleState(systemId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, systemId, new Condition(zenRule.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + ORIGIN_SYSTEM, SYSTEM_UID); // Event 4: "User" deletes the rule - mZenModeHelper.removeAutomaticZenRule(systemId, - Flags.modesApi() ? ORIGIN_USER_IN_SYSTEMUI : ZenModeConfig.ORIGIN_SYSTEM, "", + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, systemId, + Flags.modesApi() ? ORIGIN_USER_IN_SYSTEMUI : ORIGIN_SYSTEM, "", SYSTEM_UID); // In total, this represents 4 events assertEquals(4, mZenModeEventLogger.numLoggedChanges()); @@ -3311,7 +3322,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { assertFalse(mZenModeEventLogger.getIsUserAction(2)); assertEquals(SYSTEM_UID, mZenModeEventLogger.getPackageUid(2)); assertThat(mZenModeEventLogger.getChangeOrigin(2)).isEqualTo( - Flags.modesUi() ? ZenModeConfig.ORIGIN_SYSTEM : 0); + Flags.modesUi() ? ORIGIN_SYSTEM : 0); // When the system rule is deleted, we consider this a user action that turns DND off // (again) @@ -3339,27 +3350,27 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, - ORIGIN_APP, "test", CUSTOM_PKG_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_APP, "test", CUSTOM_PKG_UID); // Event 1: Mimic the rule coming on manually when the user turns it on in the app // ("Turn on bedtime now" because user goes to bed earlier). - mZenModeHelper.setAutomaticZenRuleState(id, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, new Condition(zenRule.getConditionId(), "", STATE_TRUE, SOURCE_USER_ACTION), ORIGIN_USER_IN_APP, CUSTOM_PKG_UID); // Event 2: App deactivates the rule automatically (it's 8 AM, bedtime schedule ends) - mZenModeHelper.setAutomaticZenRuleState(id, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, new Condition(zenRule.getConditionId(), "", STATE_FALSE, SOURCE_SCHEDULE), ORIGIN_APP, CUSTOM_PKG_UID); // Event 3: App activates the rule automatically (it's now 11 PM, bedtime schedule starts) - mZenModeHelper.setAutomaticZenRuleState(id, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, new Condition(zenRule.getConditionId(), "", STATE_TRUE, SOURCE_SCHEDULE), ORIGIN_APP, CUSTOM_PKG_UID); // Event 4: User deactivates the rule manually (they get up before 8 AM on the next day) - mZenModeHelper.setAutomaticZenRuleState(id, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, new Condition(zenRule.getConditionId(), "", STATE_FALSE, SOURCE_USER_ACTION), ORIGIN_USER_IN_APP, CUSTOM_PKG_UID); @@ -3427,21 +3438,23 @@ public class ZenModeHelperTest extends UiServiceTestCase { setupZenConfig(); // First just turn zen mode on - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, ORIGIN_USER_IN_SYSTEMUI, "", null, SYSTEM_UID); // Now change the policy slightly; want to confirm that this'll be reflected in the logs ZenModeConfig newConfig = mZenModeHelper.mConfig.copy(); - mZenModeHelper.setNotificationPolicy(new Policy(PRIORITY_CATEGORY_ALARMS, 0, 0), + mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, + new Policy(PRIORITY_CATEGORY_ALARMS, 0, 0), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID); // Turn zen mode off; we want to make sure policy changes do not get logged when zen mode // is off. - mZenModeHelper.setManualZenMode(ZEN_MODE_OFF, null, ZenModeConfig.ORIGIN_SYSTEM, "", - null, SYSTEM_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_OFF, null, + ORIGIN_SYSTEM, "", null, SYSTEM_UID); // Change the policy again - mZenModeHelper.setNotificationPolicy(new Policy(PRIORITY_CATEGORY_REPEAT_CALLERS, 0, 0), + mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, + new Policy(PRIORITY_CATEGORY_REPEAT_CALLERS, 0, 0), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID); // Total events: we only expect ones for turning on, changing policy, and turning off @@ -3485,8 +3498,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_SYSTEM, "test", SYSTEM_UID); // Rule 2, same as rule 1 AutomaticZenRule zenRule2 = new AutomaticZenRule("name2", @@ -3495,8 +3508,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id2 = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule2, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id2 = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule2, ORIGIN_SYSTEM, "test", SYSTEM_UID); // Rule 3, has stricter settings than the default settings ZenModeConfig ruleConfig = mZenModeHelper.mConfig.copy(); @@ -3507,28 +3520,27 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), ruleConfig.getZenPolicy(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id3 = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule3, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id3 = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule3, ORIGIN_SYSTEM, "test", SYSTEM_UID); // First: turn on rule 1 - mZenModeHelper.setAutomaticZenRuleState(id, - new Condition(zenRule.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, + new Condition(zenRule.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, SYSTEM_UID); // Second: turn on rule 2 - mZenModeHelper.setAutomaticZenRuleState(id2, - new Condition(zenRule2.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id2, + new Condition(zenRule2.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, + SYSTEM_UID); // Third: turn on rule 3 - mZenModeHelper.setAutomaticZenRuleState(id3, - new Condition(zenRule3.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id3, + new Condition(zenRule3.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, + SYSTEM_UID); // Fourth: Turn *off* rule 2 - mZenModeHelper.setAutomaticZenRuleState(id2, - new Condition(zenRule2.getConditionId(), "", STATE_FALSE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id2, + new Condition(zenRule2.getConditionId(), "", STATE_FALSE), ORIGIN_SYSTEM, + SYSTEM_UID); // This should result in a total of four events assertEquals(4, mZenModeEventLogger.numLoggedChanges()); @@ -3612,8 +3624,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), manualRulePolicy, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, - ORIGIN_APP, "test", SYSTEM_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_APP, "test", SYSTEM_UID); // Rule 2, same as rule 1 but owned by the system AutomaticZenRule zenRule2 = new AutomaticZenRule("name2", @@ -3622,41 +3634,38 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), manualRulePolicy, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id2 = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule2, - ORIGIN_USER_IN_SYSTEMUI, "test", SYSTEM_UID); + String id2 = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule2, ORIGIN_USER_IN_SYSTEMUI, "test", SYSTEM_UID); // Turn on rule 1; call looks like it's from the system. Because setting a condition is // typically an automatic (non-user-initiated) action, expect the calling UID to be // re-evaluated to the one associated with CUSTOM_PKG_NAME. // When modes_ui is true: we expect the change origin to be the source of truth. - mZenModeHelper.setAutomaticZenRuleState(id, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, new Condition(zenRule.getConditionId(), "", STATE_TRUE), - Flags.modesUi() ? ORIGIN_APP : ZenModeConfig.ORIGIN_SYSTEM, - SYSTEM_UID); + Flags.modesUi() ? ORIGIN_APP : ORIGIN_SYSTEM, SYSTEM_UID); // Second: turn on rule 2. This is a system-owned rule and the UID should not be modified // (nor even looked up; the mock PackageManager won't handle "android" as input). - mZenModeHelper.setAutomaticZenRuleState(id2, - new Condition(zenRule2.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id2, + new Condition(zenRule2.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, + SYSTEM_UID); // Disable rule 1. Because this looks like a user action, the UID should not be modified // from the system-provided one unless modes_ui is true. zenRule.setEnabled(false); - mZenModeHelper.updateAutomaticZenRule(id, zenRule, + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, id, zenRule, ORIGIN_USER_IN_SYSTEMUI, "", SYSTEM_UID); // Add a manual rule. Any manual rule changes should not get calling uids reassigned. - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, - ORIGIN_APP, - "", null, CUSTOM_PKG_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, + ORIGIN_APP, "", null, CUSTOM_PKG_UID); // Change rule 2's condition, but from some other UID. Since it doesn't look like it's from // the system, we keep the UID info. // Note that this probably shouldn't be able to occur in real scenarios. - mZenModeHelper.setAutomaticZenRuleState(id2, - new Condition(zenRule2.getConditionId(), "", STATE_FALSE), - ORIGIN_APP, 12345); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id2, + new Condition(zenRule2.getConditionId(), "", STATE_FALSE), ORIGIN_APP, 12345); // That was 5 events total assertEquals(5, mZenModeEventLogger.numLoggedChanges()); @@ -3712,32 +3721,31 @@ public class ZenModeHelperTest extends UiServiceTestCase { // Turn on zen mode with a manual rule with an enabler set. This should *not* count // as a user action, and *should* get its UID reassigned. - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, - ZenModeConfig.ORIGIN_SYSTEM, "", CUSTOM_PKG_NAME, SYSTEM_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, + ORIGIN_SYSTEM, "", CUSTOM_PKG_NAME, SYSTEM_UID); assertEquals(1, mZenModeEventLogger.numLoggedChanges()); // Now change apps bypassing to true ZenModeConfig newConfig = mZenModeHelper.mConfig.copy(); newConfig.areChannelsBypassingDnd = true; - mZenModeHelper.setNotificationPolicy(newConfig.toNotificationPolicy(), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, newConfig.toNotificationPolicy(), + ORIGIN_SYSTEM, SYSTEM_UID); assertEquals(2, mZenModeEventLogger.numLoggedChanges()); // and then back to false, all without changing anything else newConfig.areChannelsBypassingDnd = false; - mZenModeHelper.setNotificationPolicy(newConfig.toNotificationPolicy(), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, newConfig.toNotificationPolicy(), + ORIGIN_SYSTEM, SYSTEM_UID); assertEquals(3, mZenModeEventLogger.numLoggedChanges()); // Turn off manual mode, call from a package: don't reset UID even though enabler is set - mZenModeHelper.setManualZenMode(ZEN_MODE_OFF, null, ORIGIN_APP, "", + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_OFF, null, ORIGIN_APP, "", CUSTOM_PKG_NAME, 12345); assertEquals(4, mZenModeEventLogger.numLoggedChanges()); // And likewise when turning it back on again - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, - ORIGIN_APP, - "", CUSTOM_PKG_NAME, 12345); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, + ORIGIN_APP, "", CUSTOM_PKG_NAME, 12345); // These are 5 events in total. assertEquals(5, mZenModeEventLogger.numLoggedChanges()); @@ -3782,8 +3790,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { setupZenConfig(); // First just turn zen mode on - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, - ZenModeConfig.ORIGIN_SYSTEM, "", null, SYSTEM_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, + ORIGIN_SYSTEM, "", null, SYSTEM_UID); // Now change only the channels part of the policy; want to confirm that this'll be // reflected in the logs @@ -3793,8 +3801,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { oldPolicy.priorityMessageSenders, oldPolicy.suppressedVisualEffects, STATE_PRIORITY_CHANNELS_BLOCKED, oldPolicy.priorityConversationSenders); - mZenModeHelper.setNotificationPolicy(newPolicy, - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, newPolicy, ORIGIN_SYSTEM, + SYSTEM_UID); // Total events: one for turning on, one for changing policy assertThat(mZenModeEventLogger.numLoggedChanges()).isEqualTo(2); @@ -3834,16 +3842,16 @@ public class ZenModeHelperTest extends UiServiceTestCase { Uri.parse("condition"), null, NotificationManager.INTERRUPTION_FILTER_ALL, true); - String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, - ORIGIN_APP, "test", CUSTOM_PKG_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_APP, "test", CUSTOM_PKG_UID); // Event 1: App activates the rule automatically. - mZenModeHelper.setAutomaticZenRuleState(id, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, new Condition(zenRule.getConditionId(), "", STATE_TRUE, SOURCE_SCHEDULE), ORIGIN_APP, CUSTOM_PKG_UID); // Event 2: App deactivates the rule automatically. - mZenModeHelper.setAutomaticZenRuleState(id, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, new Condition(zenRule.getConditionId(), "", STATE_FALSE, SOURCE_SCHEDULE), ORIGIN_APP, CUSTOM_PKG_UID); @@ -3876,35 +3884,33 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setInterruptionFilter(INTERRUPTION_FILTER_ALL) .setType(TYPE_BEDTIME) .build(); - String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule(mPkg, bedtime, - ORIGIN_APP, - "reason", CUSTOM_PKG_UID); + String bedtimeRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, bedtime, + ORIGIN_APP, "reason", CUSTOM_PKG_UID); // Create immersive rule AutomaticZenRule immersive = new AutomaticZenRule.Builder("Immersed", CONDITION_ID) .setType(TYPE_IMMERSIVE) .setZenPolicy(mZenModeHelper.mConfig.getZenPolicy()) // same as the manual rule .build(); - String immersiveId = mZenModeHelper.addAutomaticZenRule(mPkg, immersive, - ORIGIN_APP, - "reason", CUSTOM_PKG_UID); + String immersiveId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, immersive, + ORIGIN_APP, "reason", CUSTOM_PKG_UID); // Event 1: Activate bedtime rule. This doesn't turn on notification filtering - mZenModeHelper.setAutomaticZenRuleState(bedtimeRuleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, bedtimeRuleId, new Condition(bedtime.getConditionId(), "", STATE_TRUE, SOURCE_SCHEDULE), ORIGIN_APP, CUSTOM_PKG_UID); // Event 2: turn on manual zen mode. Manual rule will have ACTIVE_RULE_TYPE_MANUAL - mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, - ZenModeConfig.ORIGIN_SYSTEM, "", null, SYSTEM_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, + ORIGIN_SYSTEM, "", null, SYSTEM_UID); // Event 3: Turn immersive on - mZenModeHelper.setAutomaticZenRuleState(immersiveId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, immersiveId, new Condition(immersive.getConditionId(), "", STATE_TRUE, SOURCE_SCHEDULE), ORIGIN_APP, CUSTOM_PKG_UID); // Event 4: Turn off bedtime mode, leaving just manual + immersive - mZenModeHelper.setAutomaticZenRuleState(bedtimeRuleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, bedtimeRuleId, new Condition(bedtime.getConditionId(), "", STATE_FALSE, SOURCE_SCHEDULE), ORIGIN_APP, CUSTOM_PKG_UID); @@ -3966,15 +3972,15 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_SYSTEM, "test", SYSTEM_UID); // enable the rule - mZenModeHelper.setAutomaticZenRuleState(id, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, new Condition(zenRule.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + ORIGIN_SYSTEM, SYSTEM_UID); - assertEquals(mZenModeHelper.getNotificationPolicy(), + assertEquals(mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT), mZenModeHelper.getConsolidatedNotificationPolicy()); // inspect the consolidated policy. Based on setupZenConfig() values. @@ -4002,13 +4008,13 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, // null policy NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_SYSTEM, "test", SYSTEM_UID); // enable the rule - mZenModeHelper.setAutomaticZenRuleState(id, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, new Condition(zenRule.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + ORIGIN_SYSTEM, SYSTEM_UID); // inspect the consolidated policy, which should match the device default settings. assertThat(ZenAdapters.notificationPolicyToZenPolicy(mZenModeHelper.mConsolidatedPolicy)) @@ -4040,13 +4046,12 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), customPolicy, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_SYSTEM, "test", SYSTEM_UID); // enable the rule; this will update the consolidated policy - mZenModeHelper.setAutomaticZenRuleState(id, - new Condition(zenRule.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, + new Condition(zenRule.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, SYSTEM_UID); // since this is the only active rule, the consolidated policy should match the custom // policy for every field specified, and take default values (from device default or @@ -4085,13 +4090,12 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), customPolicy, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_SYSTEM, "test", SYSTEM_UID); // enable the rule; this will update the consolidated policy - mZenModeHelper.setAutomaticZenRuleState(id, - new Condition(zenRule.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, + new Condition(zenRule.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, SYSTEM_UID); // since this is the only active rule, the consolidated policy should match the custom // policy for every field specified, and take default values (from either device default @@ -4125,13 +4129,12 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_SYSTEM, "test", SYSTEM_UID); // enable rule 1 - mZenModeHelper.setAutomaticZenRuleState(id, - new Condition(zenRule.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, + new Condition(zenRule.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, SYSTEM_UID); // custom policy for rule 2 ZenPolicy customPolicy = new ZenPolicy.Builder() @@ -4149,13 +4152,13 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), customPolicy, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id2 = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule2, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id2 = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule2, ORIGIN_SYSTEM, "test", SYSTEM_UID); // enable rule 2; this will update the consolidated policy - mZenModeHelper.setAutomaticZenRuleState(id2, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id2, new Condition(zenRule2.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + ORIGIN_SYSTEM, SYSTEM_UID); // now both rules should be on, and the consolidated policy should reflect the most // restrictive option of each of the two @@ -4186,13 +4189,12 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_SYSTEM, "test", SYSTEM_UID); // enable rule 1 - mZenModeHelper.setAutomaticZenRuleState(id, - new Condition(zenRule.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, + new Condition(zenRule.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, SYSTEM_UID); // custom policy for rule 2 ZenPolicy customPolicy = new ZenPolicy.Builder() @@ -4210,13 +4212,13 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), customPolicy, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id2 = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule2, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id2 = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule2, ORIGIN_SYSTEM, "test", SYSTEM_UID); // enable rule 2; this will update the consolidated policy - mZenModeHelper.setAutomaticZenRuleState(id2, - new Condition(zenRule2.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id2, + new Condition(zenRule2.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, + SYSTEM_UID); // now both rules should be on, and the consolidated policy should reflect the most // restrictive option of each of the two @@ -4252,13 +4254,12 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), customPolicy, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_SYSTEM, "test", SYSTEM_UID); // enable the rule; this will update the consolidated policy - mZenModeHelper.setAutomaticZenRuleState(id, - new Condition(zenRule.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, + new Condition(zenRule.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, SYSTEM_UID); // confirm that channels make it through assertTrue(mZenModeHelper.mConsolidatedPolicy.allowPriorityChannels()); @@ -4274,13 +4275,13 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), strictPolicy, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String id2 = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), zenRule2, - ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + String id2 = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule2, ORIGIN_SYSTEM, "test", SYSTEM_UID); // enable rule 2; this will update the consolidated policy - mZenModeHelper.setAutomaticZenRuleState(id2, - new Condition(zenRule2.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id2, + new Condition(zenRule2.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, + SYSTEM_UID); // rule 2 should override rule 1 assertFalse(mZenModeHelper.mConsolidatedPolicy.allowPriorityChannels()); @@ -4305,9 +4306,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { .allowSystem(true) .build(), NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - String rule1Id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - zenRuleWithPriority, ORIGIN_APP, "test", CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState(rule1Id, + String rule1Id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRuleWithPriority, ORIGIN_APP, "test", CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, rule1Id, new Condition(zenRuleWithPriority.getConditionId(), "", STATE_TRUE), ORIGIN_APP, CUSTOM_PKG_UID); @@ -4318,9 +4319,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { Uri.parse("priority"), new ZenPolicy.Builder().disallowAllSounds().build(), NotificationManager.INTERRUPTION_FILTER_ALL, true); - String rule2Id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - zenRuleWithAll, ORIGIN_APP, "test", CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState(rule2Id, + String rule2Id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRuleWithAll, ORIGIN_APP, "test", CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, rule2Id, new Condition(zenRuleWithPriority.getConditionId(), "", STATE_TRUE), ORIGIN_APP, CUSTOM_PKG_UID); @@ -4364,7 +4365,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { rule.triggerDescription = TRIGGER_DESC; mZenModeHelper.mConfig.automaticRules.put(rule.id, rule); - AutomaticZenRule actual = mZenModeHelper.getAutomaticZenRule(rule.id); + AutomaticZenRule actual = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, rule.id); assertEquals(NAME, actual.getName()); assertEquals(OWNER, actual.getOwner()); @@ -4400,8 +4401,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setManualInvocationAllowed(ALLOW_MANUAL) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(OWNER.getPackageName(), azr, - ORIGIN_APP, "add", CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + OWNER.getPackageName(), azr, ORIGIN_APP, "add", CUSTOM_PKG_UID); ZenModeConfig.ZenRule storedRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); @@ -4430,17 +4431,17 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule azrBase = new AutomaticZenRule.Builder("OriginalName", CONDITION_ID) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - azrBase, ORIGIN_APP, "reason", SYSTEM_UID); - AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(ruleId); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), azrBase, ORIGIN_APP, "reason", SYSTEM_UID); + AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); // Checks the name can be changed by the app because the user has not modified it. AutomaticZenRule azrUpdate = new AutomaticZenRule.Builder(rule) .setName("NewName") .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, azrUpdate, ORIGIN_APP, "reason", - SYSTEM_UID); - rule = mZenModeHelper.getAutomaticZenRule(ruleId); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, azrUpdate, ORIGIN_APP, + "reason", SYSTEM_UID); + rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); assertThat(rule.getName()).isEqualTo("NewName"); // The user modifies some other field in the rule, which makes the rule as a whole not @@ -4448,35 +4449,35 @@ public class ZenModeHelperTest extends UiServiceTestCase { azrUpdate = new AutomaticZenRule.Builder(rule) .setInterruptionFilter(INTERRUPTION_FILTER_ALARMS) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, azrUpdate, ORIGIN_USER_IN_SYSTEMUI, "reason", - SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, azrUpdate, + ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); // ...but the app can still modify the name, because the name itself hasn't been modified // by the user. azrUpdate = new AutomaticZenRule.Builder(rule) .setName("NewAppName") .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, azrUpdate, ORIGIN_APP, "reason", - SYSTEM_UID); - rule = mZenModeHelper.getAutomaticZenRule(ruleId); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, azrUpdate, ORIGIN_APP, + "reason", SYSTEM_UID); + rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); assertThat(rule.getName()).isEqualTo("NewAppName"); // The user modifies the name. azrUpdate = new AutomaticZenRule.Builder(rule) .setName("UserProvidedName") .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, azrUpdate, ORIGIN_USER_IN_SYSTEMUI, "reason", - SYSTEM_UID); - rule = mZenModeHelper.getAutomaticZenRule(ruleId); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, azrUpdate, + ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); + rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); assertThat(rule.getName()).isEqualTo("UserProvidedName"); // The app is no longer able to modify the name. azrUpdate = new AutomaticZenRule.Builder(rule) .setName("NewAppName") .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, azrUpdate, ORIGIN_APP, "reason", - SYSTEM_UID); - rule = mZenModeHelper.getAutomaticZenRule(ruleId); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, azrUpdate, ORIGIN_APP, + "reason", SYSTEM_UID); + rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); assertThat(rule.getName()).isEqualTo("UserProvidedName"); } @@ -4490,9 +4491,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setDeviceEffects(new ZenDeviceEffects.Builder().build()) .build(); // Adds the rule using the app, to avoid having any user modified bits set. - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - azrBase, ORIGIN_APP, "reason", SYSTEM_UID); - AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(ruleId); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), azrBase, ORIGIN_APP, "reason", SYSTEM_UID); + AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); // Modifies the filter, icon, zen policy, and device effects ZenPolicy policy = new ZenPolicy.Builder(rule.getZenPolicy()) @@ -4510,9 +4511,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { .build(); // Update the rule with the AZR from origin user. - mZenModeHelper.updateAutomaticZenRule(ruleId, azrUpdate, ORIGIN_USER_IN_SYSTEMUI, "reason", - SYSTEM_UID); - rule = mZenModeHelper.getAutomaticZenRule(ruleId); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, azrUpdate, + ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); + rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); // UPDATE_ORIGIN_USER should change the bitmask and change the values. assertThat(rule.getInterruptionFilter()).isEqualTo(INTERRUPTION_FILTER_PRIORITY); @@ -4547,9 +4548,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { .build()) .build(); // Adds the rule using the app, to avoid having any user modified bits set. - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - azrBase, ORIGIN_APP, "reason", SYSTEM_UID); - AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(ruleId); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), azrBase, ORIGIN_APP, "reason", SYSTEM_UID); + AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); // Modifies the icon, zen policy and device effects ZenPolicy policy = new ZenPolicy.Builder(rule.getZenPolicy()) @@ -4567,9 +4568,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { .build(); // Update the rule with the AZR from origin systemUI. - mZenModeHelper.updateAutomaticZenRule(ruleId, azrUpdate, ZenModeConfig.ORIGIN_SYSTEM, + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, azrUpdate, ORIGIN_SYSTEM, "reason", SYSTEM_UID); - rule = mZenModeHelper.getAutomaticZenRule(ruleId); + rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); // UPDATE_ORIGIN_SYSTEM_OR_SYSTEMUI should change the value but NOT update the bitmask. assertThat(rule.getIconResId()).isEqualTo(ICON_RES_ID); @@ -4597,9 +4598,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { .build()) .build(); // Adds the rule using the app, to avoid having any user modified bits set. - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - azrBase, ORIGIN_APP, "reason", SYSTEM_UID); - AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(ruleId); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), azrBase, ORIGIN_APP, "reason", SYSTEM_UID); + AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); ZenPolicy policy = new ZenPolicy.Builder() .allowReminders(true) @@ -4615,8 +4616,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { // Since the rule is not already user modified, UPDATE_ORIGIN_APP can modify the rule. // The bitmask is not modified. - mZenModeHelper.updateAutomaticZenRule(ruleId, azrUpdate, ORIGIN_APP, "reason", - SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, azrUpdate, ORIGIN_APP, + "reason", SYSTEM_UID); ZenRule storedRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(storedRule.userModifiedFields).isEqualTo(0); @@ -4630,8 +4631,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { assertThat(storedRule.zenDeviceEffectsUserModifiedFields).isEqualTo(0); // Creates another rule, this time from user. This will have user modified bits set. - String ruleIdUser = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - azrBase, ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); + String ruleIdUser = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), azrBase, ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); storedRule = mZenModeHelper.mConfig.automaticRules.get(ruleIdUser); int ruleModifiedFields = storedRule.userModifiedFields; int rulePolicyModifiedFields = storedRule.zenPolicyUserModifiedFields; @@ -4639,9 +4640,10 @@ public class ZenModeHelperTest extends UiServiceTestCase { // Zen rule update coming from the app again. This cannot fully update the rule, because // the rule is already considered user modified. - mZenModeHelper.updateAutomaticZenRule(ruleIdUser, azrUpdate, ORIGIN_APP, + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleIdUser, azrUpdate, ORIGIN_APP, "reason", SYSTEM_UID); - AutomaticZenRule ruleUser = mZenModeHelper.getAutomaticZenRule(ruleIdUser); + AutomaticZenRule ruleUser = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, + ruleIdUser); // The app can only change the value if the rule is not already user modified, // so the rule is not changed, and neither is the bitmask. @@ -4670,9 +4672,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setShouldDisplayGrayscale(true) .build()) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - azrBase, ORIGIN_APP, "reason", SYSTEM_UID); - AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(ruleId); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), azrBase, ORIGIN_APP, "reason", SYSTEM_UID); + AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); // The values are modified but the bitmask is not. assertThat(rule.getZenPolicy().getPriorityCategoryReminders()) @@ -4692,8 +4694,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setDeviceEffects(zde) .build(); // Adds the rule using the app, to avoid having any user modified bits set. - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - azrBase, ORIGIN_APP, "reason", SYSTEM_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), azrBase, ORIGIN_APP, "reason", SYSTEM_UID); AutomaticZenRule azr = new AutomaticZenRule.Builder(azrBase) // Sets Device Effects to null @@ -4702,9 +4704,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { // Zen rule update coming from app, but since the rule isn't already // user modified, it can be updated. - mZenModeHelper.updateAutomaticZenRule(ruleId, azr, ORIGIN_APP, "reason", + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, azr, ORIGIN_APP, "reason", SYSTEM_UID); - AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); // When AZR's ZenDeviceEffects is null, the updated rule's device effects are kept. assertThat(rule.getDeviceEffects()).isEqualTo(zde); @@ -4718,9 +4720,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setZenPolicy(POLICY) .build(); // Adds the rule using the app, to avoid having any user modified bits set. - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - azrBase, ORIGIN_APP, "reason", SYSTEM_UID); - AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(ruleId); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), azrBase, ORIGIN_APP, "reason", SYSTEM_UID); + AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); AutomaticZenRule azr = new AutomaticZenRule.Builder(azrBase) // Set zen policy to null @@ -4729,9 +4731,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { // Zen rule update coming from app, but since the rule isn't already // user modified, it can be updated. - mZenModeHelper.updateAutomaticZenRule(ruleId, azr, ORIGIN_APP, "reason", + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, azr, ORIGIN_APP, "reason", SYSTEM_UID); - rule = mZenModeHelper.getAutomaticZenRule(ruleId); + rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); // When AZR's ZenPolicy is null, we expect the updated rule's policy to be unchanged // (equivalent to the provided policy, with additional fields filled in with defaults). @@ -4750,9 +4752,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { // .setDeviceEffects(new ZenDeviceEffects.Builder().build()) .build(); // Adds the rule using the app, to avoid having any user modified bits set. - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - azrBase, ORIGIN_APP, "reason", SYSTEM_UID); - AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(ruleId); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), azrBase, ORIGIN_APP, "reason", SYSTEM_UID); + AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); // Create a fully populated ZenPolicy. ZenPolicy policy = new ZenPolicy.Builder() @@ -4780,9 +4782,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { // Applies the update to the rule. // Default config defined in getDefaultConfigParser() is used as the original rule. - mZenModeHelper.updateAutomaticZenRule(ruleId, azr, ORIGIN_USER_IN_SYSTEMUI, "reason", - SYSTEM_UID); - rule = mZenModeHelper.getAutomaticZenRule(ruleId); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, azr, + ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); + rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); // New ZenPolicy differs from the default config assertThat(rule.getZenPolicy()).isNotNull(); @@ -4811,9 +4813,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setDeviceEffects(null) .build(); // Adds the rule using the app, to avoid having any user modified bits set. - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - azrBase, ORIGIN_APP, "reason", SYSTEM_UID); - AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(ruleId); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), azrBase, ORIGIN_APP, "reason", SYSTEM_UID); + AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); ZenDeviceEffects deviceEffects = new ZenDeviceEffects.Builder() .setShouldDisplayGrayscale(true) @@ -4823,9 +4825,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { .build(); // Applies the update to the rule. - mZenModeHelper.updateAutomaticZenRule(ruleId, azr, ORIGIN_USER_IN_SYSTEMUI, "reason", - SYSTEM_UID); - rule = mZenModeHelper.getAutomaticZenRule(ruleId); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, azr, + ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); + rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); // New ZenDeviceEffects is used; all fields considered set, since previously were null. assertThat(rule.getDeviceEffects()).isNotNull(); @@ -4848,8 +4850,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - final String createdId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - zenRule, ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + final String createdId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_SYSTEM, "test", SYSTEM_UID); CountDownLatch latch = new CountDownLatch(1); final int[] actualStatus = new int[1]; @@ -4865,7 +4867,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { mZenModeHelper.addCallback(callback); zenRule.setEnabled(false); - mZenModeHelper.updateAutomaticZenRule(createdId, zenRule, ZenModeConfig.ORIGIN_SYSTEM, + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, createdId, zenRule, ORIGIN_SYSTEM, "", SYSTEM_UID); assertTrue(latch.await(500, TimeUnit.MILLISECONDS)); @@ -4883,8 +4885,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, false); - final String createdId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - zenRule, ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + final String createdId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_SYSTEM, "test", SYSTEM_UID); CountDownLatch latch = new CountDownLatch(1); final int[] actualStatus = new int[1]; @@ -4900,7 +4902,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { mZenModeHelper.addCallback(callback); zenRule.setEnabled(true); - mZenModeHelper.updateAutomaticZenRule(createdId, zenRule, ZenModeConfig.ORIGIN_SYSTEM, + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, createdId, zenRule, ORIGIN_SYSTEM, "", SYSTEM_UID); assertTrue(latch.await(500, TimeUnit.MILLISECONDS)); @@ -4919,8 +4921,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - final String createdId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - zenRule, ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + final String createdId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_SYSTEM, "test", SYSTEM_UID); CountDownLatch latch = new CountDownLatch(1); final int[] actualStatus = new int[1]; @@ -4935,9 +4937,9 @@ public class ZenModeHelperTest extends UiServiceTestCase { }; mZenModeHelper.addCallback(callback); - mZenModeHelper.setAutomaticZenRuleState(createdId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, createdId, new Condition(zenRule.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + ORIGIN_SYSTEM, SYSTEM_UID); assertTrue(latch.await(500, TimeUnit.MILLISECONDS)); if (CompatChanges.isChangeEnabled(ZenModeHelper.SEND_ACTIVATION_AZR_STATUSES)) { @@ -4959,8 +4961,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - final String createdId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - zenRule, ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + final String createdId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_SYSTEM, "test", SYSTEM_UID); CountDownLatch latch = new CountDownLatch(1); final int[] actualStatus = new int[2]; @@ -4977,12 +4979,11 @@ public class ZenModeHelperTest extends UiServiceTestCase { }; mZenModeHelper.addCallback(callback); - mZenModeHelper.setAutomaticZenRuleState(createdId, - new Condition(zenRule.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, createdId, + new Condition(zenRule.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, SYSTEM_UID); - mZenModeHelper.setManualZenMode(Global.ZEN_MODE_OFF, null, ZenModeConfig.ORIGIN_SYSTEM, - null, "", SYSTEM_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, Global.ZEN_MODE_OFF, null, + ORIGIN_SYSTEM, null, "", SYSTEM_UID); assertTrue(latch.await(500, TimeUnit.MILLISECONDS)); if (CompatChanges.isChangeEnabled(ZenModeHelper.SEND_ACTIVATION_AZR_STATUSES)) { @@ -5004,8 +5005,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - final String createdId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - zenRule, ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + final String createdId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_SYSTEM, "test", SYSTEM_UID); CountDownLatch latch = new CountDownLatch(1); final int[] actualStatus = new int[2]; @@ -5022,13 +5023,12 @@ public class ZenModeHelperTest extends UiServiceTestCase { }; mZenModeHelper.addCallback(callback); - mZenModeHelper.setAutomaticZenRuleState(createdId, - new Condition(zenRule.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, createdId, + new Condition(zenRule.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, SYSTEM_UID); - mZenModeHelper.setAutomaticZenRuleState(createdId, - new Condition(zenRule.getConditionId(), "", STATE_FALSE), - ORIGIN_APP, CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, createdId, + new Condition(zenRule.getConditionId(), "", STATE_FALSE), ORIGIN_APP, + CUSTOM_PKG_UID); assertTrue(latch.await(500, TimeUnit.MILLISECONDS)); if (CompatChanges.isChangeEnabled(ZenModeHelper.SEND_ACTIVATION_AZR_STATUSES)) { @@ -5049,21 +5049,20 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenModeConfig.toScheduleConditionId(new ScheduleInfo()), null, NotificationManager.INTERRUPTION_FILTER_PRIORITY, true); - final String createdId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - zenRule, ZenModeConfig.ORIGIN_SYSTEM, "test", SYSTEM_UID); + final String createdId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), zenRule, ORIGIN_SYSTEM, "test", SYSTEM_UID); // Event 1: Mimic the rule coming on automatically by setting the Condition to STATE_TRUE - mZenModeHelper.setAutomaticZenRuleState(createdId, - new Condition(zenRule.getConditionId(), "", STATE_TRUE), - ZenModeConfig.ORIGIN_SYSTEM, SYSTEM_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, createdId, + new Condition(zenRule.getConditionId(), "", STATE_TRUE), ORIGIN_SYSTEM, SYSTEM_UID); // Event 2: Snooze rule by turning off DND - mZenModeHelper.setManualZenMode(Global.ZEN_MODE_OFF, null, ZenModeConfig.ORIGIN_SYSTEM, - "", null, SYSTEM_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, Global.ZEN_MODE_OFF, null, + ORIGIN_SYSTEM, "", null, SYSTEM_UID); // Event 3: "User" turns off the automatic rule (sets it to not enabled) zenRule.setEnabled(false); - mZenModeHelper.updateAutomaticZenRule(createdId, zenRule, ZenModeConfig.ORIGIN_SYSTEM, + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, createdId, zenRule, ORIGIN_SYSTEM, "", SYSTEM_UID); assertEquals(OVERRIDE_NONE, @@ -5078,17 +5077,17 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setConfigurationActivity(new ComponentName(mPkg, "cls")) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "reason", - CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, ORIGIN_APP, - CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "reason", CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); AutomaticZenRule updateWithDiff = new AutomaticZenRule.Builder(rule) .setTriggerDescription("Whenever") .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, updateWithDiff, ORIGIN_APP, "reason", - CUSTOM_PKG_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, updateWithDiff, + ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_OFF); assertThat(mZenModeHelper.mConfig.automaticRules.get(ruleId).condition).isNull(); @@ -5102,15 +5101,15 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setConfigurationActivity(new ComponentName(mPkg, "cls")) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "reason", - CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, ORIGIN_APP, - CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "reason", CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); AutomaticZenRule updateUnchanged = new AutomaticZenRule.Builder(rule).build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, updateUnchanged, ORIGIN_APP, "reason", - CUSTOM_PKG_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, updateUnchanged, + ORIGIN_APP, "reason", CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); assertThat(mZenModeHelper.mConfig.automaticRules.get(ruleId).condition).isEqualTo( @@ -5125,17 +5124,17 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setConfigurationActivity(new ComponentName(mPkg, "cls")) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "reason", - CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, ORIGIN_APP, - CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "reason", CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); AutomaticZenRule updateWithDiff = new AutomaticZenRule.Builder(rule) .setTriggerDescription("Whenever") .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, updateWithDiff, ORIGIN_USER_IN_SYSTEMUI, - "reason", SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, updateWithDiff, + ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); assertThat(mZenModeHelper.mConfig.automaticRules.get(ruleId).condition).isEqualTo( @@ -5153,17 +5152,16 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setConfigurationActivity(new ComponentName(mPkg, "cls")) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); - String ruleId = - mZenModeHelper.addAutomaticZenRule( - mPkg, rule, ORIGIN_APP, "reason", CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState( - ruleId, CONDITION_TRUE, ORIGIN_APP, CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "reason", CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); AutomaticZenRule updateWithDiff = new AutomaticZenRule.Builder(rule).setTriggerDescription("Whenever").build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, updateWithDiff, ORIGIN_USER_IN_SYSTEMUI, - "reason", SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, updateWithDiff, + ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); assertThat(mZenModeHelper.mConfig.automaticRules.get(ruleId).condition).isEqualTo( @@ -5178,16 +5176,16 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setConfigurationActivity(new ComponentName(mPkg, "cls")) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "reason", - CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, ORIGIN_APP, - CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "reason", CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); - mZenModeHelper.updateAutomaticZenRule(ruleId, + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, new AutomaticZenRule.Builder(rule).setEnabled(false).build(), ORIGIN_USER_IN_SYSTEMUI, "disable", SYSTEM_UID); - mZenModeHelper.updateAutomaticZenRule(ruleId, + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, new AutomaticZenRule.Builder(rule).setEnabled(true).build(), ORIGIN_USER_IN_SYSTEMUI, "enable", SYSTEM_UID); @@ -5203,16 +5201,16 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule original = new AutomaticZenRule.Builder("Rule", Uri.EMPTY) .setOwner(new ComponentName("android", "some.old.cps")) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule("android", original, - ZenModeConfig.ORIGIN_SYSTEM, "reason", SYSTEM_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "android", original, + ORIGIN_SYSTEM, "reason", SYSTEM_UID); AutomaticZenRule update = new AutomaticZenRule.Builder("Rule", Uri.EMPTY) .setOwner(new ComponentName("android", "brand.new.cps")) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, update, ORIGIN_USER_IN_SYSTEMUI, "reason", - SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, update, + ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); - AutomaticZenRule result = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule result = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); assertThat(result).isNotNull(); assertThat(result.getOwner().getClassName()).isEqualTo("brand.new.cps"); } @@ -5223,16 +5221,16 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule original = new AutomaticZenRule.Builder("Rule", Uri.EMPTY) .setOwner(new ComponentName(mContext.getPackageName(), "old.third.party.cps")) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), original, - ORIGIN_APP, "reason", CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), original, ORIGIN_APP, "reason", CUSTOM_PKG_UID); AutomaticZenRule update = new AutomaticZenRule.Builder("Rule", Uri.EMPTY) .setOwner(new ComponentName(mContext.getPackageName(), "new.third.party.cps")) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, update, ORIGIN_USER_IN_SYSTEMUI, "reason", - SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, update, + ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); - AutomaticZenRule result = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule result = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); assertThat(result).isNotNull(); assertThat(result.getOwner().getClassName()).isEqualTo("old.third.party.cps"); } @@ -5247,14 +5245,14 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setShouldSuppressAmbientDisplay(true) .setShouldDimWallpaper(true) .build()); - mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, ORIGIN_APP, - CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); mTestableLooper.processAllMessages(); verify(mDeviceEffectsApplier).apply(any(), eq(ORIGIN_APP)); // Now delete the (currently active!) rule. For example, assume this is done from settings. - mZenModeHelper.removeAutomaticZenRule(ruleId, ORIGIN_USER_IN_SYSTEMUI, "remove", - SYSTEM_UID); + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, ruleId, ORIGIN_USER_IN_SYSTEMUI, + "remove", SYSTEM_UID); mTestableLooper.processAllMessages(); verify(mDeviceEffectsApplier).apply(eq(NO_EFFECTS), eq(ORIGIN_USER_IN_SYSTEMUI)); @@ -5273,8 +5271,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { String ruleId = addRuleWithEffects(effects); verifyNoMoreInteractions(mDeviceEffectsApplier); - mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, ORIGIN_APP, - CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); mTestableLooper.processAllMessages(); verify(mDeviceEffectsApplier).apply(eq(effects), eq(ORIGIN_APP)); @@ -5287,13 +5285,13 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenDeviceEffects zde = new ZenDeviceEffects.Builder().setShouldUseNightMode(true).build(); String ruleId = addRuleWithEffects(zde); - mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, ORIGIN_APP, - CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); mTestableLooper.processAllMessages(); verify(mDeviceEffectsApplier).apply(eq(zde), eq(ORIGIN_APP)); - mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_FALSE, ORIGIN_APP, - CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, CONDITION_FALSE, + ORIGIN_APP, CUSTOM_PKG_UID); mTestableLooper.processAllMessages(); verify(mDeviceEffectsApplier).apply(eq(NO_EFFECTS), eq(ORIGIN_APP)); @@ -5310,8 +5308,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setShouldDisplayGrayscale(true) .addExtraEffect("ONE") .build()); - mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, ORIGIN_APP, - CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); mTestableLooper.processAllMessages(); verify(mDeviceEffectsApplier).apply( eq(new ZenDeviceEffects.Builder() @@ -5326,8 +5324,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setShouldDimWallpaper(true) .addExtraEffect("TWO") .build()); - mZenModeHelper.setAutomaticZenRuleState(secondRuleId, CONDITION_TRUE, ORIGIN_APP, - CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, secondRuleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); mTestableLooper.processAllMessages(); verify(mDeviceEffectsApplier).apply( @@ -5350,15 +5348,15 @@ public class ZenModeHelperTest extends UiServiceTestCase { .addExtraEffect("extra_effect") .build(); String ruleId = addRuleWithEffects(zde); - mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, ORIGIN_APP, - CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); mTestableLooper.processAllMessages(); verify(mDeviceEffectsApplier).apply(eq(zde), eq(ORIGIN_APP)); // Now create and activate a second rule that doesn't add any more effects. String secondRuleId = addRuleWithEffects(zde); - mZenModeHelper.setAutomaticZenRuleState(secondRuleId, CONDITION_TRUE, ORIGIN_APP, - CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, secondRuleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); mTestableLooper.processAllMessages(); verifyNoMoreInteractions(mDeviceEffectsApplier); @@ -5371,8 +5369,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { String ruleId = addRuleWithEffects(zde); verify(mDeviceEffectsApplier, never()).apply(any(), anyInt()); - mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, ORIGIN_APP, - CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); mTestableLooper.processAllMessages(); verify(mDeviceEffectsApplier, never()).apply(any(), anyInt()); @@ -5412,8 +5410,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule rule = new AutomaticZenRule.Builder("Test", CONDITION_ID) .setDeviceEffects(effects) .build(); - return mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule, - ZenModeConfig.ORIGIN_SYSTEM, "reasons", SYSTEM_UID); + return mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mContext.getPackageName(), + rule, ORIGIN_SYSTEM, "reasons", SYSTEM_UID); } @Test @@ -5426,35 +5424,37 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .setZenPolicy(new ZenPolicy.Builder().allowRepeatCallers(false).build()) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule, - ORIGIN_APP, "add it", CUSTOM_PKG_UID); - assertThat(mZenModeHelper.getAutomaticZenRule(ruleId).getCreationTime()).isEqualTo(1000); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), rule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); + assertThat(mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId).getCreationTime()) + .isEqualTo(1000); // User customizes it. AutomaticZenRule userUpdate = new AutomaticZenRule.Builder(rule) .setInterruptionFilter(INTERRUPTION_FILTER_ALARMS) .setZenPolicy(new ZenPolicy.Builder().allowRepeatCallers(true).build()) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, userUpdate, ORIGIN_USER_IN_SYSTEMUI, - "userUpdate", SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, userUpdate, + ORIGIN_USER_IN_SYSTEMUI, "userUpdate", SYSTEM_UID); // App deletes it. mTestClock.advanceByMillis(1000); - mZenModeHelper.removeAutomaticZenRule(ruleId, ORIGIN_APP, "delete it", + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, ruleId, ORIGIN_APP, "delete it", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(0); assertThat(mZenModeHelper.mConfig.deletedRules).hasSize(1); // App adds it again. mTestClock.advanceByMillis(1000); - String newRuleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule, - ORIGIN_APP, "add it again", CUSTOM_PKG_UID); + String newRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), rule, ORIGIN_APP, "add it again", CUSTOM_PKG_UID); // Verify that the rule was restored: // - id and creation time is the same as the original one. // - ZenPolicy is the one that the user had set. // - rule still has the user-modified fields. - AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(newRuleId); + AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, + newRuleId); assertThat(finalRule.getCreationTime()).isEqualTo(1000); // And not 3000. assertThat(newRuleId).isEqualTo(ruleId); assertThat(finalRule.getInterruptionFilter()).isEqualTo(INTERRUPTION_FILTER_ALARMS); @@ -5481,24 +5481,26 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .setZenPolicy(new ZenPolicy.Builder().allowRepeatCallers(false).build()) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule, - ORIGIN_APP, "add it", CUSTOM_PKG_UID); - assertThat(mZenModeHelper.getAutomaticZenRule(ruleId).getCreationTime()).isEqualTo(1000); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), rule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); + assertThat(mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId).getCreationTime()) + .isEqualTo(1000); // App deletes it. mTestClock.advanceByMillis(1000); - mZenModeHelper.removeAutomaticZenRule(ruleId, ORIGIN_APP, "delete it", + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, ruleId, ORIGIN_APP, "delete it", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(0); assertThat(mZenModeHelper.mConfig.deletedRules).hasSize(0); // App adds it again. mTestClock.advanceByMillis(1000); - String newRuleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule, - ORIGIN_APP, "add it again", CUSTOM_PKG_UID); + String newRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), rule, ORIGIN_APP, "add it again", CUSTOM_PKG_UID); // Verify that the rule was recreated. This means id and creation time are new. - AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(newRuleId); + AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, + newRuleId); assertThat(finalRule.getCreationTime()).isEqualTo(3000); assertThat(newRuleId).isNotEqualTo(ruleId); } @@ -5513,9 +5515,10 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .setZenPolicy(new ZenPolicy.Builder().allowRepeatCallers(false).build()) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule, - ORIGIN_APP, "add it", CUSTOM_PKG_UID); - assertThat(mZenModeHelper.getAutomaticZenRule(ruleId).getCreationTime()).isEqualTo(1000); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), rule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); + assertThat(mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId).getCreationTime()) + .isEqualTo(1000); // User customizes it. mTestClock.advanceByMillis(1000); @@ -5523,24 +5526,26 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setInterruptionFilter(INTERRUPTION_FILTER_ALARMS) .setZenPolicy(new ZenPolicy.Builder().allowRepeatCallers(true).build()) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, userUpdate, ORIGIN_USER_IN_SYSTEMUI, - "userUpdate", SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, userUpdate, + ORIGIN_USER_IN_SYSTEMUI, "userUpdate", SYSTEM_UID); // App deletes it. mTestClock.advanceByMillis(1000); - mZenModeHelper.removeAutomaticZenRule(ruleId, ORIGIN_APP, "delete it", + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, ruleId, ORIGIN_APP, "delete it", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(0); assertThat(mZenModeHelper.mConfig.deletedRules).hasSize(1); // User creates it again (unusual case, but ok). mTestClock.advanceByMillis(1000); - String newRuleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule, - ORIGIN_USER_IN_SYSTEMUI, "add it anew", SYSTEM_UID); + String newRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), rule, ORIGIN_USER_IN_SYSTEMUI, "add it anew", + SYSTEM_UID); // Verify that the rule was recreated. This means id and creation time are new, and the rule // matches the latest data supplied to addAZR. - AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(newRuleId); + AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, + newRuleId); assertThat(finalRule.getCreationTime()).isEqualTo(4000); assertThat(newRuleId).isNotEqualTo(ruleId); assertThat(finalRule.getInterruptionFilter()).isEqualTo(INTERRUPTION_FILTER_PRIORITY); @@ -5561,9 +5566,10 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .setZenPolicy(new ZenPolicy.Builder().allowRepeatCallers(false).build()) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule, - ORIGIN_APP, "add it", CUSTOM_PKG_UID); - assertThat(mZenModeHelper.getAutomaticZenRule(ruleId).getCreationTime()).isEqualTo(1000); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), rule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); + assertThat(mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId).getCreationTime()) + .isEqualTo(1000); // User customizes it. mTestClock.advanceByMillis(1000); @@ -5571,23 +5577,24 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setInterruptionFilter(INTERRUPTION_FILTER_ALARMS) .setZenPolicy(new ZenPolicy.Builder().allowRepeatCallers(true).build()) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, userUpdate, ORIGIN_USER_IN_SYSTEMUI, - "userUpdate", SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, userUpdate, + ORIGIN_USER_IN_SYSTEMUI, "userUpdate", SYSTEM_UID); // User deletes it. mTestClock.advanceByMillis(1000); - mZenModeHelper.removeAutomaticZenRule(ruleId, ORIGIN_USER_IN_SYSTEMUI, "delete it", - SYSTEM_UID); + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, ruleId, ORIGIN_USER_IN_SYSTEMUI, + "delete it", SYSTEM_UID); assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(0); assertThat(mZenModeHelper.mConfig.deletedRules).hasSize(0); // App creates it again. mTestClock.advanceByMillis(1000); - String newRuleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule, - ORIGIN_APP, "add it again", CUSTOM_PKG_UID); + String newRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), rule, ORIGIN_APP, "add it again", CUSTOM_PKG_UID); // Verify that the rule was recreated. This means id and creation time are new. - AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(newRuleId); + AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, + newRuleId); assertThat(finalRule.getCreationTime()).isEqualTo(4000); assertThat(newRuleId).isNotEqualTo(ruleId); } @@ -5601,18 +5608,18 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setOwner(new ComponentName("first", "owner")) .setInterruptionFilter(INTERRUPTION_FILTER_ALL) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule, - ORIGIN_APP, "add it", CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), rule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); // User customizes it. AutomaticZenRule userUpdate = new AutomaticZenRule.Builder(rule) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, userUpdate, ORIGIN_USER_IN_SYSTEMUI, - "userUpdate", SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, userUpdate, + ORIGIN_USER_IN_SYSTEMUI, "userUpdate", SYSTEM_UID); // App deletes it. It's preserved for a possible restoration. - mZenModeHelper.removeAutomaticZenRule(ruleId, ORIGIN_APP, "delete it", + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, ruleId, ORIGIN_APP, "delete it", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(0); assertThat(mZenModeHelper.mConfig.deletedRules).hasSize(1); @@ -5622,12 +5629,14 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setOwner(new ComponentName("second", "owner")) .setInterruptionFilter(INTERRUPTION_FILTER_ALARMS) .build(); - String newRuleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), - readdingWithDifferentOwner, ORIGIN_APP, "add it again", CUSTOM_PKG_UID); + String newRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), readdingWithDifferentOwner, ORIGIN_APP, "add it again", + CUSTOM_PKG_UID); // Verify that the rule was NOT restored: assertThat(newRuleId).isNotEqualTo(ruleId); - AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(newRuleId); + AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, + newRuleId); assertThat(finalRule.getInterruptionFilter()).isEqualTo(INTERRUPTION_FILTER_ALARMS); assertThat(finalRule.getOwner()).isEqualTo(new ComponentName("second", "owner")); @@ -5643,23 +5652,23 @@ public class ZenModeHelperTest extends UiServiceTestCase { mZenModeHelper.mConfig.automaticRules.clear(); // Start with a bunch of customized rules where conditionUris are not unique. - String id1 = mZenModeHelper.addAutomaticZenRule("pkg1", + String id1 = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "pkg1", new AutomaticZenRule.Builder("Test1", Uri.parse("uri1")).build(), ORIGIN_APP, "add it", CUSTOM_PKG_UID); - String id2 = mZenModeHelper.addAutomaticZenRule("pkg1", + String id2 = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "pkg1", new AutomaticZenRule.Builder("Test2", Uri.parse("uri2")).build(), ORIGIN_APP, "add it", CUSTOM_PKG_UID); - String id3 = mZenModeHelper.addAutomaticZenRule("pkg1", + String id3 = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "pkg1", new AutomaticZenRule.Builder("Test3", Uri.parse("uri2")).build(), ORIGIN_APP, "add it", CUSTOM_PKG_UID); - String id4 = mZenModeHelper.addAutomaticZenRule("pkg2", + String id4 = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "pkg2", new AutomaticZenRule.Builder("Test4", Uri.parse("uri1")).build(), ORIGIN_APP, "add it", CUSTOM_PKG_UID); - String id5 = mZenModeHelper.addAutomaticZenRule("pkg2", + String id5 = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, "pkg2", new AutomaticZenRule.Builder("Test5", Uri.parse("uri1")).build(), ORIGIN_APP, "add it", CUSTOM_PKG_UID); @@ -5667,11 +5676,16 @@ public class ZenModeHelperTest extends UiServiceTestCase { zenRule.userModifiedFields = AutomaticZenRule.FIELD_INTERRUPTION_FILTER; } - mZenModeHelper.removeAutomaticZenRule(id1, ORIGIN_APP, "begone", CUSTOM_PKG_UID); - mZenModeHelper.removeAutomaticZenRule(id2, ORIGIN_APP, "begone", CUSTOM_PKG_UID); - mZenModeHelper.removeAutomaticZenRule(id3, ORIGIN_APP, "begone", CUSTOM_PKG_UID); - mZenModeHelper.removeAutomaticZenRule(id4, ORIGIN_APP, "begone", CUSTOM_PKG_UID); - mZenModeHelper.removeAutomaticZenRule(id5, ORIGIN_APP, "begone", CUSTOM_PKG_UID); + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, id1, ORIGIN_APP, "begone", + CUSTOM_PKG_UID); + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, id2, ORIGIN_APP, "begone", + CUSTOM_PKG_UID); + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, id3, ORIGIN_APP, "begone", + CUSTOM_PKG_UID); + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, id4, ORIGIN_APP, "begone", + CUSTOM_PKG_UID); + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, id5, ORIGIN_APP, "begone", + CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.deletedRules.keySet()) .containsExactly("pkg1|uri1", "pkg1|uri2", "pkg2|uri1"); @@ -5685,11 +5699,11 @@ public class ZenModeHelperTest extends UiServiceTestCase { public void removeAllZenRules_preservedForRestoring() { mZenModeHelper.mConfig.automaticRules.clear(); - mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), + mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mContext.getPackageName(), new AutomaticZenRule.Builder("Test1", Uri.parse("uri1")).build(), ORIGIN_APP, "add it", CUSTOM_PKG_UID); - mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), + mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mContext.getPackageName(), new AutomaticZenRule.Builder("Test2", Uri.parse("uri2")).build(), ORIGIN_APP, "add it", CUSTOM_PKG_UID); @@ -5698,8 +5712,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { zenRule.userModifiedFields = AutomaticZenRule.FIELD_INTERRUPTION_FILTER; } - mZenModeHelper.removeAutomaticZenRules(mContext.getPackageName(), ORIGIN_APP, - "begone", CUSTOM_PKG_UID); + mZenModeHelper.removeAutomaticZenRules(UserHandle.CURRENT, mContext.getPackageName(), + ORIGIN_APP, "begone", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.deletedRules).hasSize(2); } @@ -5716,8 +5730,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { mZenModeHelper.mConfig.deletedRules.put(ZenModeConfig.deletedRuleKey(pkg1Rule), pkg1Rule); mZenModeHelper.mConfig.deletedRules.put(ZenModeConfig.deletedRuleKey(pkg2Rule), pkg2Rule); - mZenModeHelper.removeAutomaticZenRules("pkg1", - ZenModeConfig.ORIGIN_SYSTEM, "goodbye pkg1", SYSTEM_UID); + mZenModeHelper.removeAutomaticZenRules(UserHandle.CURRENT, "pkg1", + ORIGIN_SYSTEM, "goodbye pkg1", SYSTEM_UID); // Preserved rules from pkg1 are gone; those from pkg2 are still there. assertThat(mZenModeHelper.mConfig.deletedRules.values().stream().map(r -> r.pkg) @@ -5733,36 +5747,37 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setConditionId(CONDITION_ID) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule, - ORIGIN_APP, "add it", CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), rule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); // User customizes it. AutomaticZenRule userUpdate = new AutomaticZenRule.Builder(rule) .setInterruptionFilter(INTERRUPTION_FILTER_ALARMS) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, userUpdate, ORIGIN_USER_IN_SYSTEMUI, - "userUpdate", SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, userUpdate, + ORIGIN_USER_IN_SYSTEMUI, "userUpdate", SYSTEM_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_OFF); // App activates it. - mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, ORIGIN_APP, - CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_ALARMS); // App deletes it. - mZenModeHelper.removeAutomaticZenRule(ruleId, ORIGIN_APP, "delete it", + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, ruleId, ORIGIN_APP, "delete it", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(0); assertThat(mZenModeHelper.mConfig.deletedRules).hasSize(1); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_OFF); // App adds it again. - String newRuleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule, - ORIGIN_APP, "add it again", CUSTOM_PKG_UID); + String newRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), rule, ORIGIN_APP, "add it again", CUSTOM_PKG_UID); // The rule is restored... assertThat(newRuleId).isEqualTo(ruleId); - AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(newRuleId); + AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, + newRuleId); assertThat(finalRule.getInterruptionFilter()).isEqualTo(INTERRUPTION_FILTER_ALARMS); // ... but it is NOT active @@ -5782,40 +5797,41 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setConditionId(CONDITION_ID) .setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule, - ORIGIN_APP, "add it", CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), rule, ORIGIN_APP, "add it", CUSTOM_PKG_UID); // User customizes it. AutomaticZenRule userUpdate = new AutomaticZenRule.Builder(rule) .setInterruptionFilter(INTERRUPTION_FILTER_ALARMS) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, userUpdate, ORIGIN_USER_IN_SYSTEMUI, - "userUpdate", SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, userUpdate, + ORIGIN_USER_IN_SYSTEMUI, "userUpdate", SYSTEM_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_OFF); // App activates it. - mZenModeHelper.setAutomaticZenRuleState(ruleId, CONDITION_TRUE, ORIGIN_APP, - CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, CONDITION_TRUE, + ORIGIN_APP, CUSTOM_PKG_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_ALARMS); // User snoozes it. - mZenModeHelper.setManualZenMode(ZEN_MODE_OFF, null, ZenModeConfig.ORIGIN_SYSTEM, + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_OFF, null, ORIGIN_SYSTEM, "snoozing", "systemui", SYSTEM_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_OFF); // App deletes it. - mZenModeHelper.removeAutomaticZenRule(ruleId, ORIGIN_APP, "delete it", + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, ruleId, ORIGIN_APP, "delete it", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(0); assertThat(mZenModeHelper.mConfig.deletedRules).hasSize(1); // App adds it again. - String newRuleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), rule, - ORIGIN_APP, "add it again", CUSTOM_PKG_UID); + String newRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), rule, ORIGIN_APP, "add it again", CUSTOM_PKG_UID); // The rule is restored... assertThat(newRuleId).isEqualTo(ruleId); - AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(newRuleId); + AutomaticZenRule finalRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, + newRuleId); assertThat(finalRule.getInterruptionFilter()).isEqualTo(INTERRUPTION_FILTER_ALARMS); // ... but it is NEITHER active NOR snoozed. @@ -5888,7 +5904,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { @Test @EnableFlags(FLAG_MODES_API) public void getAutomaticZenRuleState_ownedRule_returnsRuleState() { - String id = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), + String id = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), new AutomaticZenRule.Builder("Rule", CONDITION_ID) .setConfigurationActivity( new ComponentName(mContext.getPackageName(), "Blah")) @@ -5896,18 +5913,23 @@ public class ZenModeHelperTest extends UiServiceTestCase { ORIGIN_APP, "reasons", CUSTOM_PKG_UID); // Null condition -> STATE_FALSE - assertThat(mZenModeHelper.getAutomaticZenRuleState(id)).isEqualTo(Condition.STATE_FALSE); + assertThat(mZenModeHelper.getAutomaticZenRuleState(UserHandle.CURRENT, id)) + .isEqualTo(Condition.STATE_FALSE); - mZenModeHelper.setAutomaticZenRuleState(id, CONDITION_TRUE, ORIGIN_APP, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, CONDITION_TRUE, ORIGIN_APP, CUSTOM_PKG_UID); - assertThat(mZenModeHelper.getAutomaticZenRuleState(id)).isEqualTo(Condition.STATE_TRUE); + assertThat(mZenModeHelper.getAutomaticZenRuleState(UserHandle.CURRENT, id)) + .isEqualTo(Condition.STATE_TRUE); - mZenModeHelper.setAutomaticZenRuleState(id, CONDITION_FALSE, ORIGIN_APP, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, id, CONDITION_FALSE, ORIGIN_APP, CUSTOM_PKG_UID); - assertThat(mZenModeHelper.getAutomaticZenRuleState(id)).isEqualTo(Condition.STATE_FALSE); + assertThat(mZenModeHelper.getAutomaticZenRuleState(UserHandle.CURRENT, id)) + .isEqualTo(Condition.STATE_FALSE); - mZenModeHelper.removeAutomaticZenRule(id, ORIGIN_APP, "", CUSTOM_PKG_UID); - assertThat(mZenModeHelper.getAutomaticZenRuleState(id)).isEqualTo(Condition.STATE_UNKNOWN); + mZenModeHelper.removeAutomaticZenRule(UserHandle.CURRENT, id, ORIGIN_APP, "", + CUSTOM_PKG_UID); + assertThat(mZenModeHelper.getAutomaticZenRuleState(UserHandle.CURRENT, id)) + .isEqualTo(Condition.STATE_UNKNOWN); } @Test @@ -5922,8 +5944,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { mZenModeHelper.setConfig(config, null, ORIGIN_INIT, "", SYSTEM_UID); assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_ALARMS); - assertThat(mZenModeHelper.getAutomaticZenRuleState("systemRule")).isEqualTo( - Condition.STATE_UNKNOWN); + assertThat(mZenModeHelper.getAutomaticZenRuleState(UserHandle.CURRENT, "systemRule")) + .isEqualTo(Condition.STATE_UNKNOWN); } @Test @@ -5940,7 +5962,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_ALARMS); // Should be ignored. - mZenModeHelper.setAutomaticZenRuleState("otherRule", + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, "otherRule", new Condition(otherRule.conditionId, "off", Condition.STATE_FALSE), ORIGIN_APP, CUSTOM_PKG_UID); @@ -5961,7 +5983,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { assertThat(mZenModeHelper.getZenMode()).isEqualTo(ZEN_MODE_ALARMS); // Should be ignored. - mZenModeHelper.setAutomaticZenRuleState(otherRule.conditionId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, otherRule.conditionId, new Condition(otherRule.conditionId, "off", Condition.STATE_FALSE), ORIGIN_APP, CUSTOM_PKG_UID); @@ -5972,7 +5994,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { @EnableFlags(FLAG_MODES_API) public void testCallbacks_policy() throws Exception { setupZenConfig(); - assertThat(mZenModeHelper.getNotificationPolicy().allowReminders()).isTrue(); + assertThat(mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT).allowReminders()) + .isTrue(); SettableFuture<Policy> futurePolicy = SettableFuture.create(); mZenModeHelper.addCallback(new ZenModeHelper.Callback() { @Override @@ -5982,7 +6005,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { }); Policy totalSilencePolicy = new Policy(0, 0, 0); - mZenModeHelper.setNotificationPolicy(totalSilencePolicy, ORIGIN_APP, CUSTOM_PKG_UID); + mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, totalSilencePolicy, ORIGIN_APP, + CUSTOM_PKG_UID); Policy callbackPolicy = futurePolicy.get(1, TimeUnit.SECONDS); assertThat(callbackPolicy.allowReminders()).isFalse(); @@ -6000,13 +6024,14 @@ public class ZenModeHelperTest extends UiServiceTestCase { } }); - String totalSilenceRuleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), + String totalSilenceRuleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), new AutomaticZenRule.Builder("Rule", CONDITION_ID) .setOwner(OWNER) .setInterruptionFilter(INTERRUPTION_FILTER_NONE) .build(), ORIGIN_APP, "reasons", 0); - mZenModeHelper.setAutomaticZenRuleState(totalSilenceRuleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, totalSilenceRuleId, new Condition(CONDITION_ID, "", STATE_TRUE), ORIGIN_APP, CUSTOM_PKG_UID); Policy callbackPolicy = futureConsolidatedPolicy.get(1, TimeUnit.SECONDS); @@ -6018,8 +6043,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { public void applyGlobalZenModeAsImplicitZenRule_createsImplicitRuleAndActivatesIt() { mZenModeHelper.mConfig.automaticRules.clear(); - mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(CUSTOM_PKG_NAME, CUSTOM_PKG_UID, - ZEN_MODE_IMPORTANT_INTERRUPTIONS); + mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, CUSTOM_PKG_NAME, + CUSTOM_PKG_UID, ZEN_MODE_IMPORTANT_INTERRUPTIONS); assertThat(mZenModeHelper.mConfig.automaticRules.values()) .comparingElementsUsing(IGNORE_METADATA) @@ -6034,13 +6059,14 @@ public class ZenModeHelperTest extends UiServiceTestCase { public void applyGlobalZenModeAsImplicitZenRule_updatesImplicitRuleAndActivatesIt() { mZenModeHelper.mConfig.automaticRules.clear(); - mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(CUSTOM_PKG_NAME, CUSTOM_PKG_UID, - ZEN_MODE_IMPORTANT_INTERRUPTIONS); - mZenModeHelper.setManualZenMode(ZEN_MODE_OFF, null, ORIGIN_APP, "test", "test", 0); + mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, CUSTOM_PKG_NAME, + CUSTOM_PKG_UID, ZEN_MODE_IMPORTANT_INTERRUPTIONS); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_OFF, null, ORIGIN_APP, "test", + "test", 0); assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(1); - mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(CUSTOM_PKG_NAME, CUSTOM_PKG_UID, - ZEN_MODE_ALARMS); + mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, CUSTOM_PKG_NAME, + CUSTOM_PKG_UID, ZEN_MODE_ALARMS); assertThat(mZenModeHelper.mConfig.automaticRules.values()) .comparingElementsUsing(IGNORE_METADATA) @@ -6057,22 +6083,22 @@ public class ZenModeHelperTest extends UiServiceTestCase { String pkg = mContext.getPackageName(); // From app, call "setInterruptionFilter" and create and implicit rule. - mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(pkg, CUSTOM_PKG_UID, + mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, pkg, CUSTOM_PKG_UID, ZEN_MODE_IMPORTANT_INTERRUPTIONS); String ruleId = getOnlyElement(mZenModeHelper.mConfig.automaticRules.keySet()); assertThat(getOnlyElement(mZenModeHelper.mConfig.automaticRules.values()).zenMode) .isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); // From user, update that rule's interruption filter. - AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); AutomaticZenRule userUpdateRule = new AutomaticZenRule.Builder(rule) .setInterruptionFilter(INTERRUPTION_FILTER_ALARMS) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, userUpdateRule, ORIGIN_USER_IN_SYSTEMUI, - "reason", SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, userUpdateRule, + ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); // From app, call "setInterruptionFilter" again. - mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(pkg, CUSTOM_PKG_UID, + mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, pkg, CUSTOM_PKG_UID, ZEN_MODE_NO_INTERRUPTIONS); // The app's update was ignored, and the user's update is still current, and the current @@ -6089,22 +6115,22 @@ public class ZenModeHelperTest extends UiServiceTestCase { String pkg = mContext.getPackageName(); // From app, call "setInterruptionFilter" and create and implicit rule. - mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(pkg, CUSTOM_PKG_UID, + mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, pkg, CUSTOM_PKG_UID, ZEN_MODE_IMPORTANT_INTERRUPTIONS); String ruleId = getOnlyElement(mZenModeHelper.mConfig.automaticRules.keySet()); assertThat(getOnlyElement(mZenModeHelper.mConfig.automaticRules.values()).zenMode) .isEqualTo(ZEN_MODE_IMPORTANT_INTERRUPTIONS); // From user, update something in that rule, but not the interruption filter. - AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); AutomaticZenRule userUpdateRule = new AutomaticZenRule.Builder(rule) .setName("Renamed") .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, userUpdateRule, ORIGIN_USER_IN_SYSTEMUI, - "reason", SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, userUpdateRule, + ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); // From app, call "setInterruptionFilter" again. - mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(pkg, CUSTOM_PKG_UID, + mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, pkg, CUSTOM_PKG_UID, ZEN_MODE_NO_INTERRUPTIONS); // The app's update was accepted, and the current mode is the one that they wanted. @@ -6117,13 +6143,13 @@ public class ZenModeHelperTest extends UiServiceTestCase { @EnableFlags(FLAG_MODES_API) public void applyGlobalZenModeAsImplicitZenRule_modeOff_deactivatesImplicitRule() { mZenModeHelper.mConfig.automaticRules.clear(); - mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(mPkg, CUSTOM_PKG_UID, + mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, mPkg, CUSTOM_PKG_UID, ZEN_MODE_IMPORTANT_INTERRUPTIONS); assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(1); assertThat(mZenModeHelper.mConfig.automaticRules.valueAt(0).condition.state) .isEqualTo(STATE_TRUE); - mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(mPkg, CUSTOM_PKG_UID, + mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, mPkg, CUSTOM_PKG_UID, ZEN_MODE_OFF); assertThat(mZenModeHelper.mConfig.automaticRules.valueAt(0).condition.state) @@ -6135,8 +6161,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { public void applyGlobalZenModeAsImplicitZenRule_modeOffButNoPreviousRule_ignored() { mZenModeHelper.mConfig.automaticRules.clear(); - mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(CUSTOM_PKG_NAME, CUSTOM_PKG_UID, - ZEN_MODE_OFF); + mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, CUSTOM_PKG_NAME, + CUSTOM_PKG_UID, ZEN_MODE_OFF); assertThat(mZenModeHelper.mConfig.automaticRules).isEmpty(); } @@ -6146,18 +6172,19 @@ public class ZenModeHelperTest extends UiServiceTestCase { public void applyGlobalZenModeAsImplicitZenRule_update_unsnoozesRule() { mZenModeHelper.mConfig.automaticRules.clear(); - mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(CUSTOM_PKG_NAME, CUSTOM_PKG_UID, - ZEN_MODE_IMPORTANT_INTERRUPTIONS); + mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, CUSTOM_PKG_NAME, + CUSTOM_PKG_UID, ZEN_MODE_IMPORTANT_INTERRUPTIONS); assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(1); assertThat(mZenModeHelper.mConfig.automaticRules.valueAt(0).getConditionOverride()) .isEqualTo(OVERRIDE_NONE); - mZenModeHelper.setManualZenMode(ZEN_MODE_OFF, null, ORIGIN_APP, "test", "test", 0); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_OFF, null, ORIGIN_APP, "test", + "test", 0); assertThat(mZenModeHelper.mConfig.automaticRules.valueAt(0).getConditionOverride()) .isEqualTo(OVERRIDE_DEACTIVATE); - mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(CUSTOM_PKG_NAME, CUSTOM_PKG_UID, - ZEN_MODE_ALARMS); + mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, CUSTOM_PKG_NAME, + CUSTOM_PKG_UID, ZEN_MODE_ALARMS); assertThat(mZenModeHelper.mConfig.automaticRules.valueAt(0).getConditionOverride()) .isEqualTo(OVERRIDE_NONE); @@ -6171,9 +6198,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { mZenModeHelper.mConfig.automaticRules.clear(); withoutWtfCrash( - () -> mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(CUSTOM_PKG_NAME, - CUSTOM_PKG_UID, - ZEN_MODE_IMPORTANT_INTERRUPTIONS)); + () -> mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, + CUSTOM_PKG_NAME, CUSTOM_PKG_UID, ZEN_MODE_IMPORTANT_INTERRUPTIONS)); assertThat(mZenModeHelper.mConfig.automaticRules).isEmpty(); } @@ -6186,7 +6212,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { Policy policy = new Policy(PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED, Policy.getAllSuppressedVisualEffects(), CONVERSATION_SENDERS_IMPORTANT); - mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(CUSTOM_PKG_NAME, CUSTOM_PKG_UID, policy); + mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(UserHandle.CURRENT, CUSTOM_PKG_NAME, + CUSTOM_PKG_UID, policy); ZenPolicy expectedZenPolicy = new ZenPolicy.Builder() .disallowAllSounds() @@ -6210,14 +6237,15 @@ public class ZenModeHelperTest extends UiServiceTestCase { Policy original = new Policy(PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED, Policy.getAllSuppressedVisualEffects(), CONVERSATION_SENDERS_IMPORTANT); - mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(CUSTOM_PKG_NAME, CUSTOM_PKG_UID, - original); + mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(UserHandle.CURRENT, CUSTOM_PKG_NAME, + CUSTOM_PKG_UID, original); // Change priorityCallSenders: contacts -> starred. Policy updated = new Policy(PRIORITY_CATEGORY_CALLS | PRIORITY_CATEGORY_CONVERSATIONS, PRIORITY_SENDERS_STARRED, PRIORITY_SENDERS_STARRED, Policy.getAllSuppressedVisualEffects(), CONVERSATION_SENDERS_IMPORTANT); - mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(CUSTOM_PKG_NAME, CUSTOM_PKG_UID, updated); + mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(UserHandle.CURRENT, CUSTOM_PKG_NAME, + CUSTOM_PKG_UID, updated); ZenPolicy expectedZenPolicy = new ZenPolicy.Builder() .disallowAllSounds() @@ -6241,7 +6269,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { // From app, call "setNotificationPolicy" and create and implicit rule. Policy originalPolicy = new Policy(PRIORITY_CATEGORY_MEDIA, 0, 0); - mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(pkg, CUSTOM_PKG_UID, originalPolicy); + mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(UserHandle.CURRENT, pkg, CUSTOM_PKG_UID, + originalPolicy); String ruleId = getOnlyElement(mZenModeHelper.mConfig.automaticRules.keySet()); // Store this for checking later. @@ -6249,18 +6278,19 @@ public class ZenModeHelperTest extends UiServiceTestCase { mZenModeHelper.mConfig.getZenPolicy()).allowMedia(true).build(); // From user, update that rule's policy. - AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); ZenPolicy userUpdateZenPolicy = new ZenPolicy.Builder().disallowAllSounds() .allowAlarms(true).build(); AutomaticZenRule userUpdateRule = new AutomaticZenRule.Builder(rule) .setZenPolicy(userUpdateZenPolicy) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, userUpdateRule, ORIGIN_USER_IN_SYSTEMUI, - "reason", SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, userUpdateRule, + ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); // From app, call "setNotificationPolicy" again. Policy appUpdatePolicy = new Policy(PRIORITY_CATEGORY_SYSTEM, 0, 0); - mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(pkg, CUSTOM_PKG_UID, appUpdatePolicy); + mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(UserHandle.CURRENT, pkg, CUSTOM_PKG_UID, + appUpdatePolicy); // The app's update was ignored, and the user's update is still current. assertThat(mZenModeHelper.mConfig.automaticRules.values()) @@ -6281,7 +6311,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { // From app, call "setNotificationPolicy" and create and implicit rule. Policy originalPolicy = new Policy(PRIORITY_CATEGORY_MEDIA, 0, 0); - mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(pkg, CUSTOM_PKG_UID, originalPolicy); + mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(UserHandle.CURRENT, pkg, CUSTOM_PKG_UID, + originalPolicy); String ruleId = getOnlyElement(mZenModeHelper.mConfig.automaticRules.keySet()); // Store this for checking later. @@ -6289,16 +6320,17 @@ public class ZenModeHelperTest extends UiServiceTestCase { mZenModeHelper.mConfig.getZenPolicy()).allowMedia(true).build(); // From user, update something in that rule, but not the ZenPolicy. - AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule rule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId); AutomaticZenRule userUpdateRule = new AutomaticZenRule.Builder(rule) .setName("Rule renamed, not touching policy") .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, userUpdateRule, ORIGIN_USER_IN_SYSTEMUI, - "reason", SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, userUpdateRule, + ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID); // From app, call "setNotificationPolicy" again. Policy appUpdatePolicy = new Policy(PRIORITY_CATEGORY_SYSTEM, 0, 0); - mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(pkg, CUSTOM_PKG_UID, appUpdatePolicy); + mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(UserHandle.CURRENT, pkg, CUSTOM_PKG_UID, + appUpdatePolicy); // The app's update was applied. ZenPolicy appsSecondZenPolicy = new ZenPolicy.Builder() @@ -6316,8 +6348,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { mZenModeHelper.mConfig.automaticRules.clear(); withoutWtfCrash( - () -> mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(CUSTOM_PKG_NAME, - CUSTOM_PKG_UID, new Policy(0, 0, 0))); + () -> mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(UserHandle.CURRENT, + CUSTOM_PKG_NAME, CUSTOM_PKG_UID, new Policy(0, 0, 0))); assertThat(mZenModeHelper.mConfig.automaticRules).isEmpty(); } @@ -6329,11 +6361,11 @@ public class ZenModeHelperTest extends UiServiceTestCase { PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED, Policy.getAllSuppressedVisualEffects(), STATE_FALSE, CONVERSATION_SENDERS_IMPORTANT); - mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(CUSTOM_PKG_NAME, CUSTOM_PKG_UID, - writtenPolicy); + mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(UserHandle.CURRENT, CUSTOM_PKG_NAME, + CUSTOM_PKG_UID, writtenPolicy); Policy readPolicy = mZenModeHelper.getNotificationPolicyFromImplicitZenRule( - CUSTOM_PKG_NAME); + UserHandle.CURRENT, CUSTOM_PKG_NAME); assertThat(readPolicy).isEqualTo(writtenPolicy); } @@ -6343,15 +6375,15 @@ public class ZenModeHelperTest extends UiServiceTestCase { @DisableFlags(FLAG_MODES_UI) public void getNotificationPolicyFromImplicitZenRule_ruleWithoutPolicy_copiesGlobalPolicy() { // Implicit rule will get the global policy at the time of rule creation. - mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(CUSTOM_PKG_NAME, CUSTOM_PKG_UID, - ZEN_MODE_IMPORTANT_INTERRUPTIONS); + mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT, CUSTOM_PKG_NAME, + CUSTOM_PKG_UID, ZEN_MODE_IMPORTANT_INTERRUPTIONS); // If the policy then changes afterwards, it should inherit updates because user cannot // edit the policy in the UI. - mZenModeHelper.setNotificationPolicy(new Policy(PRIORITY_CATEGORY_ALARMS, 0, 0), - ORIGIN_APP, 1); + mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, + new Policy(PRIORITY_CATEGORY_ALARMS, 0, 0), ORIGIN_APP, 1); Policy readPolicy = mZenModeHelper.getNotificationPolicyFromImplicitZenRule( - CUSTOM_PKG_NAME); + UserHandle.CURRENT, CUSTOM_PKG_NAME); assertThat(readPolicy).isNotNull(); assertThat(readPolicy.allowCalls()).isFalse(); @@ -6362,10 +6394,11 @@ public class ZenModeHelperTest extends UiServiceTestCase { @EnableFlags(FLAG_MODES_API) public void getNotificationPolicyFromImplicitZenRule_noImplicitRule_returnsGlobalPolicy() { Policy policy = new Policy(PRIORITY_CATEGORY_CALLS, PRIORITY_SENDERS_STARRED, 0); - mZenModeHelper.setNotificationPolicy(policy, ORIGIN_APP, CUSTOM_PKG_UID); + mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, policy, ORIGIN_APP, + CUSTOM_PKG_UID); Policy readPolicy = mZenModeHelper.getNotificationPolicyFromImplicitZenRule( - CUSTOM_PKG_NAME); + UserHandle.CURRENT, CUSTOM_PKG_NAME); assertThat(readPolicy).isNotNull(); assertThat(readPolicy.allowCalls()).isTrue(); @@ -6398,7 +6431,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZEN_MODE_IMPORTANT_INTERRUPTIONS, previousManualZenPolicy); Policy newManualPolicy = new Policy(PRIORITY_CATEGORY_EVENTS, 0, 0); - mZenModeHelper.setNotificationPolicy(newManualPolicy, ORIGIN_APP, CUSTOM_PKG_UID); + mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, newManualPolicy, ORIGIN_APP, + CUSTOM_PKG_UID); ZenPolicy newManualZenPolicy = ZenAdapters.notificationPolicyToZenPolicy(newManualPolicy); // Only app rules with default or same-as-manual policies were updated. @@ -6426,10 +6460,12 @@ public class ZenModeHelperTest extends UiServiceTestCase { when(mResources.getResourceName(resourceId)).thenReturn(veryLongResourceName); when(mResources.getIdentifier(veryLongResourceName, null, null)).thenReturn(resourceId); - String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(), + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, + mContext.getPackageName(), new AutomaticZenRule.Builder("Rule", CONDITION_ID).setIconResId(resourceId).build(), ORIGIN_APP, "reason", CUSTOM_PKG_UID); - AutomaticZenRule storedRule = mZenModeHelper.getAutomaticZenRule(ruleId); + AutomaticZenRule storedRule = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, + ruleId); assertThat(storedRule.getIconResId()).isEqualTo(0); } @@ -6440,8 +6476,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { ZenDeviceEffects effects = new ZenDeviceEffects.Builder() .setShouldDimWallpaper(true) .build(); - mZenModeHelper.setManualZenRuleDeviceEffects(effects, ORIGIN_USER_IN_SYSTEMUI, "settings", - SYSTEM_UID); + mZenModeHelper.setManualZenRuleDeviceEffects(UserHandle.CURRENT, effects, + ORIGIN_USER_IN_SYSTEMUI, "settings", SYSTEM_UID); assertThat(mZenModeHelper.getConfig().manualRule).isNotNull(); assertThat(mZenModeHelper.getConfig().isManualActive()).isFalse(); @@ -6451,14 +6487,14 @@ public class ZenModeHelperTest extends UiServiceTestCase { @Test @EnableFlags({FLAG_MODES_API, FLAG_MODES_UI}) public void setManualZenRuleDeviceEffects_preexistingMode() { - mZenModeHelper.setManualZenMode(ZEN_MODE_OFF, Uri.EMPTY, ORIGIN_USER_IN_SYSTEMUI, - "create manual rule", "settings", SYSTEM_UID); + mZenModeHelper.setManualZenMode(UserHandle.CURRENT, ZEN_MODE_OFF, Uri.EMPTY, + ORIGIN_USER_IN_SYSTEMUI, "create manual rule", "settings", SYSTEM_UID); ZenDeviceEffects effects = new ZenDeviceEffects.Builder() .setShouldDimWallpaper(true) .build(); - mZenModeHelper.setManualZenRuleDeviceEffects(effects, ORIGIN_USER_IN_SYSTEMUI, "settings", - SYSTEM_UID); + mZenModeHelper.setManualZenRuleDeviceEffects(UserHandle.CURRENT, effects, + ORIGIN_USER_IN_SYSTEMUI, "settings", SYSTEM_UID); assertThat(mZenModeHelper.getConfig().manualRule).isNotNull(); assertThat(mZenModeHelper.getConfig().isManualActive()).isFalse(); @@ -6473,7 +6509,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setEnabled(false) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, startsDisabled, + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, startsDisabled, ORIGIN_APP, "new", CUSTOM_PKG_UID); @@ -6488,7 +6524,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setOwner(new ComponentName(mPkg, "SomeProvider")) .setEnabled(true) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, startsEnabled, + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, startsEnabled, ORIGIN_APP, "new", CUSTOM_PKG_UID); assertThat(mZenModeHelper.mConfig.automaticRules.get(ruleId).disabledOrigin).isEqualTo( @@ -6497,8 +6533,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule nowDisabled = new AutomaticZenRule.Builder(startsEnabled) .setEnabled(false) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, nowDisabled, ORIGIN_USER_IN_SYSTEMUI, "off", - SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, nowDisabled, + ORIGIN_USER_IN_SYSTEMUI, "off", SYSTEM_UID); assertThat(mZenModeHelper.mConfig.automaticRules.get(ruleId).disabledOrigin).isEqualTo( ORIGIN_USER_IN_SYSTEMUI); @@ -6511,14 +6547,14 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setOwner(new ComponentName(mPkg, "SomeProvider")) .setEnabled(true) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, startsEnabled, + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, startsEnabled, ORIGIN_APP, "new", CUSTOM_PKG_UID); AutomaticZenRule nowDisabled = new AutomaticZenRule.Builder(startsEnabled) .setEnabled(false) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, nowDisabled, ORIGIN_USER_IN_SYSTEMUI, "off", - SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, nowDisabled, + ORIGIN_USER_IN_SYSTEMUI, "off", SYSTEM_UID); assertThat(mZenModeHelper.mConfig.automaticRules.get(ruleId).disabledOrigin).isEqualTo( ORIGIN_USER_IN_SYSTEMUI); @@ -6526,8 +6562,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule nowRenamed = new AutomaticZenRule.Builder(nowDisabled) .setName("Fancy pants rule") .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, nowRenamed, ORIGIN_APP, "update", - CUSTOM_PKG_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, nowRenamed, ORIGIN_APP, + "update", CUSTOM_PKG_UID); // Identity of the disabler is preserved. assertThat(mZenModeHelper.mConfig.automaticRules.get(ruleId).disabledOrigin).isEqualTo( @@ -6541,14 +6577,14 @@ public class ZenModeHelperTest extends UiServiceTestCase { .setOwner(new ComponentName(mPkg, "SomeProvider")) .setEnabled(true) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, startsEnabled, + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, startsEnabled, ORIGIN_APP, "new", CUSTOM_PKG_UID); AutomaticZenRule nowDisabled = new AutomaticZenRule.Builder(startsEnabled) .setEnabled(false) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, nowDisabled, ORIGIN_USER_IN_SYSTEMUI, "off", - SYSTEM_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, nowDisabled, + ORIGIN_USER_IN_SYSTEMUI, "off", SYSTEM_UID); assertThat(mZenModeHelper.mConfig.automaticRules.get(ruleId).disabledOrigin).isEqualTo( ORIGIN_USER_IN_SYSTEMUI); @@ -6556,8 +6592,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule nowEnabled = new AutomaticZenRule.Builder(nowDisabled) .setEnabled(true) .build(); - mZenModeHelper.updateAutomaticZenRule(ruleId, nowEnabled, ORIGIN_APP, "on", - CUSTOM_PKG_UID); + mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId, nowEnabled, ORIGIN_APP, + "on", CUSTOM_PKG_UID); // Identity of the disabler was cleared. assertThat(mZenModeHelper.mConfig.automaticRules.get(ruleId).disabledOrigin).isEqualTo( @@ -6570,10 +6606,10 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule rule = new AutomaticZenRule.Builder("Rule", Uri.parse("cond")) .setPackage(mPkg) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "adding", - CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "adding", CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "manual-on", STATE_TRUE, SOURCE_USER_ACTION), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID); @@ -6588,13 +6624,13 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule rule = new AutomaticZenRule.Builder("Rule", Uri.parse("cond")) .setPackage(mPkg) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "adding", - CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "adding", CUSTOM_PKG_UID); Condition autoOn = new Condition(rule.getConditionId(), "auto-on", STATE_TRUE, SOURCE_CONTEXT); ZenRule zenRule; - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "manual-on", STATE_TRUE, SOURCE_USER_ACTION), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); @@ -6602,7 +6638,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_ACTIVATE); assertThat(zenRule.condition).isNull(); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "manual-off", STATE_FALSE, SOURCE_USER_ACTION), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); @@ -6611,7 +6647,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { assertThat(zenRule.condition).isNull(); // Bonus check: app has resumed control over the rule and can now turn it on. - mZenModeHelper.setAutomaticZenRuleState(ruleId, autoOn, ORIGIN_APP, CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, autoOn, ORIGIN_APP, + CUSTOM_PKG_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(zenRule.isActive()).isTrue(); assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_NONE); @@ -6624,21 +6661,22 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule rule = new AutomaticZenRule.Builder("Rule", Uri.parse("cond")) .setPackage(mPkg) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "adding", - CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "adding", CUSTOM_PKG_UID); Condition autoOn = new Condition(rule.getConditionId(), "auto-on", STATE_TRUE, SOURCE_CONTEXT); Condition autoOff = new Condition(rule.getConditionId(), "auto-off", STATE_FALSE, SOURCE_CONTEXT); ZenRule zenRule; - mZenModeHelper.setAutomaticZenRuleState(ruleId, autoOn, ORIGIN_APP, CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, autoOn, ORIGIN_APP, + CUSTOM_PKG_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(zenRule.isActive()).isTrue(); assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_NONE); assertThat(zenRule.condition).isEqualTo(autoOn); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "manual-off", STATE_FALSE, SOURCE_USER_ACTION), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); @@ -6646,7 +6684,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_DEACTIVATE); assertThat(zenRule.condition).isEqualTo(autoOn); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "manual-on", STATE_TRUE, SOURCE_USER_ACTION), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); @@ -6655,7 +6693,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { assertThat(zenRule.condition).isEqualTo(autoOn); // Bonus check: app has resumed control over the rule and can now turn it off. - mZenModeHelper.setAutomaticZenRuleState(ruleId, autoOff, ORIGIN_APP, CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, autoOff, ORIGIN_APP, + CUSTOM_PKG_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(zenRule.isActive()).isFalse(); assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_NONE); @@ -6668,10 +6707,10 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule rule = new AutomaticZenRule.Builder("Rule", Uri.parse("cond")) .setPackage(mPkg) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "adding", - CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "adding", CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "auto-on", STATE_TRUE, SOURCE_CONTEXT), ORIGIN_APP, CUSTOM_PKG_UID); ZenRule zenRuleOn = mZenModeHelper.mConfig.automaticRules.get(ruleId); @@ -6679,7 +6718,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { assertThat(zenRuleOn.getConditionOverride()).isEqualTo(OVERRIDE_NONE); assertThat(zenRuleOn.condition).isNotNull(); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "manual-off", STATE_FALSE, SOURCE_USER_ACTION), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID); ZenRule zenRuleOff = mZenModeHelper.mConfig.automaticRules.get(ruleId); @@ -6694,31 +6733,31 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule rule = new AutomaticZenRule.Builder("Rule", Uri.parse("cond")) .setPackage(mPkg) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "adding", - CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "adding", CUSTOM_PKG_UID); ZenRule zenRule; - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "manual-on", STATE_TRUE, SOURCE_USER_ACTION), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(zenRule.isActive()).isTrue(); assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_ACTIVATE); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "auto-off", STATE_FALSE, SOURCE_CONTEXT), ORIGIN_APP, CUSTOM_PKG_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(zenRule.isActive()).isTrue(); assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_ACTIVATE); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "auto-on", STATE_TRUE, SOURCE_CONTEXT), ORIGIN_APP, CUSTOM_PKG_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(zenRule.isActive()).isTrue(); assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_NONE); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "auto-off", STATE_FALSE, SOURCE_CONTEXT), ORIGIN_APP, CUSTOM_PKG_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); @@ -6731,39 +6770,39 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule rule = new AutomaticZenRule.Builder("Rule", Uri.parse("cond")) .setPackage(mPkg) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "adding", - CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "adding", CUSTOM_PKG_UID); ZenRule zenRule; - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "auto-on", STATE_TRUE, SOURCE_CONTEXT), ORIGIN_APP, CUSTOM_PKG_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(zenRule.isActive()).isTrue(); assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_NONE); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "manual-off", STATE_FALSE, SOURCE_USER_ACTION), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(zenRule.isActive()).isFalse(); assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_DEACTIVATE); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "auto-on", STATE_TRUE, SOURCE_CONTEXT), ORIGIN_APP, CUSTOM_PKG_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(zenRule.isActive()).isFalse(); assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_DEACTIVATE); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "auto-off", STATE_FALSE, SOURCE_CONTEXT), ORIGIN_APP, CUSTOM_PKG_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(zenRule.isActive()).isFalse(); assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_NONE); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "auto-on", STATE_TRUE, SOURCE_CONTEXT), ORIGIN_APP, CUSTOM_PKG_UID); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); @@ -6777,18 +6816,18 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule rule = new AutomaticZenRule.Builder("Rule", Uri.parse("cond")) .setPackage(mPkg) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "adding", - CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "adding", CUSTOM_PKG_UID); // User manually turns on rule from SysUI / Settings... - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "manual-on-from-sysui", STATE_TRUE, SOURCE_USER_ACTION), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID); assertThat(getZenRule(ruleId).isActive()).isTrue(); assertThat(getZenRule(ruleId).getConditionOverride()).isEqualTo(OVERRIDE_ACTIVATE); // ... and they can turn it off manually from inside the app. - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "manual-off-from-app", STATE_FALSE, SOURCE_USER_ACTION), ORIGIN_USER_IN_APP, CUSTOM_PKG_UID); assertThat(getZenRule(ruleId).isActive()).isFalse(); @@ -6801,25 +6840,25 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule rule = new AutomaticZenRule.Builder("Rule", Uri.parse("cond")) .setPackage(mPkg) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "adding", - CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "adding", CUSTOM_PKG_UID); // Rule is activated due to its schedule. - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "auto-on-from-app", STATE_TRUE, SOURCE_SCHEDULE), ORIGIN_APP, CUSTOM_PKG_UID); assertThat(getZenRule(ruleId).isActive()).isTrue(); assertThat(getZenRule(ruleId).getConditionOverride()).isEqualTo(OVERRIDE_NONE); // User manually turns off rule from SysUI / Settings... - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "manual-off-from-sysui", STATE_FALSE, SOURCE_USER_ACTION), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID); assertThat(getZenRule(ruleId).isActive()).isFalse(); assertThat(getZenRule(ruleId).getConditionOverride()).isEqualTo(OVERRIDE_DEACTIVATE); // ... and they can turn it on manually from inside the app. - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "manual-on-from-app", STATE_TRUE, SOURCE_USER_ACTION), ORIGIN_USER_IN_APP, CUSTOM_PKG_UID); assertThat(getZenRule(ruleId).isActive()).isTrue(); @@ -6832,19 +6871,19 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule rule = new AutomaticZenRule.Builder("Rule", Uri.parse("cond")) .setPackage(mPkg) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "adding", - CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "adding", CUSTOM_PKG_UID); // Rule is manually activated by the user in the app. // This turns the rule on, but is NOT an override... - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "manual-on-from-app", STATE_TRUE, SOURCE_USER_ACTION), ORIGIN_USER_IN_APP, CUSTOM_PKG_UID); assertThat(getZenRule(ruleId).isActive()).isTrue(); assertThat(getZenRule(ruleId).getConditionOverride()).isEqualTo(OVERRIDE_NONE); // ... so the app can turn it off when its schedule is over. - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "auto-off-from-app", STATE_FALSE, SOURCE_SCHEDULE), ORIGIN_APP, CUSTOM_PKG_UID); assertThat(getZenRule(ruleId).isActive()).isFalse(); @@ -6909,12 +6948,13 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule rule = new AutomaticZenRule.Builder("Rule", Uri.parse("cond")) .setPackage(mPkg) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "adding", - CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "adding", CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "manual-on", STATE_TRUE, SOURCE_USER_ACTION), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID); - assertThat(mZenModeHelper.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE); + assertThat(mZenModeHelper.getAutomaticZenRuleState(UserHandle.CURRENT, ruleId)) + .isEqualTo(STATE_TRUE); ZenRule zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_ACTIVATE); assertThat(zenRule.condition).isNull(); @@ -6928,12 +6968,14 @@ public class ZenModeHelperTest extends UiServiceTestCase { mZenModeHelper.readXml(parser, false, UserHandle.USER_ALL); if (Flags.modesUi()) { - assertThat(mZenModeHelper.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE); + assertThat(mZenModeHelper.getAutomaticZenRuleState(UserHandle.CURRENT, ruleId)) + .isEqualTo(STATE_TRUE); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_ACTIVATE); assertThat(zenRule.condition).isNull(); } else { - assertThat(mZenModeHelper.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_FALSE); + assertThat(mZenModeHelper.getAutomaticZenRuleState(UserHandle.CURRENT, ruleId)) + .isEqualTo(STATE_FALSE); } } @@ -6944,15 +6986,16 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule rule = new AutomaticZenRule.Builder("Rule", Uri.parse("cond")) .setPackage(mPkg) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, rule, ORIGIN_APP, "adding", - CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, rule, + ORIGIN_APP, "adding", CUSTOM_PKG_UID); + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "auto-on", STATE_TRUE, SOURCE_CONTEXT), ORIGIN_APP, CUSTOM_PKG_UID); - mZenModeHelper.setAutomaticZenRuleState(ruleId, + mZenModeHelper.setAutomaticZenRuleState(UserHandle.CURRENT, ruleId, new Condition(rule.getConditionId(), "snooze", STATE_FALSE, SOURCE_USER_ACTION), ORIGIN_USER_IN_SYSTEMUI, SYSTEM_UID); - assertThat(mZenModeHelper.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_FALSE); + assertThat(mZenModeHelper.getAutomaticZenRuleState(UserHandle.CURRENT, ruleId)) + .isEqualTo(STATE_FALSE); ZenRule zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_DEACTIVATE); assertThat(zenRule.condition).isNotNull(); @@ -6965,7 +7008,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { TypedXmlPullParser parser = getParserForByteStream(xmlBytes); mZenModeHelper.readXml(parser, false, UserHandle.USER_ALL); - assertThat(mZenModeHelper.getAutomaticZenRuleState(ruleId)).isEqualTo(STATE_TRUE); + assertThat(mZenModeHelper.getAutomaticZenRuleState(UserHandle.CURRENT, ruleId)) + .isEqualTo(STATE_TRUE); zenRule = mZenModeHelper.mConfig.automaticRules.get(ruleId); assertThat(zenRule.getConditionOverride()).isEqualTo(OVERRIDE_NONE); assertThat(zenRule.condition).isNotNull(); @@ -6978,8 +7022,8 @@ public class ZenModeHelperTest extends UiServiceTestCase { AutomaticZenRule azr = new AutomaticZenRule.Builder("Rule", Uri.parse("cond")) .setPackage(mPkg) .build(); - String ruleId = mZenModeHelper.addAutomaticZenRule(mPkg, azr, ORIGIN_APP, "adding", - CUSTOM_PKG_UID); + String ruleId = mZenModeHelper.addAutomaticZenRule(UserHandle.CURRENT, mPkg, azr, + ORIGIN_APP, "adding", CUSTOM_PKG_UID); ZenRule zenRule = checkNotNull(mZenModeHelper.mConfig.automaticRules.get(ruleId)); @@ -7039,15 +7083,62 @@ public class ZenModeHelperTest extends UiServiceTestCase { .allowPriorityChannels(false) .build(); - mZenModeHelper.updateHasPriorityChannels(true); - assertThat(mZenModeHelper.getNotificationPolicy().hasPriorityChannels()).isTrue(); + mZenModeHelper.updateHasPriorityChannels(UserHandle.CURRENT, true); + assertThat(mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT).hasPriorityChannels()) + .isTrue(); // getNotificationPolicy() gets its policy from the manual rule; channels not permitted - assertThat(mZenModeHelper.getNotificationPolicy().allowPriorityChannels()).isFalse(); + assertThat(mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT).allowPriorityChannels()) + .isFalse(); + + mZenModeHelper.updateHasPriorityChannels(UserHandle.CURRENT, false); + assertThat(mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT).hasPriorityChannels()) + .isFalse(); + assertThat(mZenModeHelper.getNotificationPolicy(UserHandle.CURRENT).allowPriorityChannels()) + .isFalse(); + } + + @Test + @EnableFlags(FLAG_MODES_MULTIUSER) + public void setManualZenMode_fromCurrentUser_updatesCurrentConfig() { + // Initialize default configurations (default rules) for both users. + mZenModeHelper.onUserSwitched(1); + mZenModeHelper.onUserSwitched(2); + UserHandle currentUser = UserHandle.of(2); + ZenModeHelper.Callback callback = mock(ZenModeHelper.Callback.class); + mZenModeHelper.addCallback(callback); + + mZenModeHelper.setManualZenMode(currentUser, ZEN_MODE_ALARMS, null, ORIGIN_APP, "reason", + mPkg, CUSTOM_PKG_UID); + + assertThat(mZenModeHelper.mConfig.isManualActive()).isTrue(); + assertThat(mZenModeHelper.mConfigs.get(1).isManualActive()).isFalse(); - mZenModeHelper.updateHasPriorityChannels(false); - assertThat(mZenModeHelper.getNotificationPolicy().hasPriorityChannels()).isFalse(); - assertThat(mZenModeHelper.getNotificationPolicy().allowPriorityChannels()).isFalse(); + // And we sent the broadcast announcing the change. + mTestableLooper.processAllMessages(); + verify(callback).onZenModeChanged(); + } + + @Test + @EnableFlags(FLAG_MODES_MULTIUSER) + public void setInterruptionFilter_fromNonCurrentUser_updatesNonCurrentConfig() { + // Initialize default configurations (default rules) for both users. + // Afterwards, 2 is current, and 1 is background. + mZenModeHelper.onUserSwitched(1); + mZenModeHelper.onUserSwitched(2); + UserHandle backgroundUser = UserHandle.of(1); + ZenModeHelper.Callback callback = mock(ZenModeHelper.Callback.class); + mZenModeHelper.addCallback(callback); + + mZenModeHelper.setManualZenMode(backgroundUser, ZEN_MODE_ALARMS, null, ORIGIN_APP, "reason", + mPkg, CUSTOM_PKG_UID); + + assertThat(mZenModeHelper.mConfig.isManualActive()).isFalse(); + assertThat(mZenModeHelper.mConfigs.get(1).isManualActive()).isTrue(); + + // And no broadcasts is sent for "background" changes (they were not evaluated). + mTestableLooper.processAllMessages(); + verify(callback, never()).onZenModeChanged(); } private static void addZenRule(ZenModeConfig config, String id, String ownerPkg, int zenMode, @@ -7122,7 +7213,7 @@ public class ZenModeHelperTest extends UiServiceTestCase { SUPPRESSED_EFFECT_BADGE, 0, CONVERSATION_SENDERS_IMPORTANT); - mZenModeHelper.setNotificationPolicy(customPolicy, ORIGIN_UNKNOWN, 1); + mZenModeHelper.setNotificationPolicy(UserHandle.CURRENT, customPolicy, ORIGIN_UNKNOWN, 1); if (!Flags.modesUi()) { mZenModeHelper.mConfig.manualRule = null; } |