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

Commit 0cca2a37 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Change naming pattern of implicit zen rules" into main

parents efb674c0 dc92aabe
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -5337,7 +5337,9 @@
    <!-- Zen mode - name of default automatic calendar time-based rule that is triggered every night (when sleeping). [CHAR LIMIT=40] -->
    <string name="zen_mode_default_every_night_name">Sleeping</string>

    <!-- Zen mode - Trigger description of the rule, indicating which app owns it. [CHAR_LIMIT=100] -->
    <!-- Implicit zen mode - Name of the rule, indicating which app owns it. [CHAR_LIMIT=30] -->
    <string name="zen_mode_implicit_name">Do Not Disturb (<xliff:g id="app_name" example="Gmail">%1$s</xliff:g>)</string>
    <!-- Implicit zen mode - Trigger description of the rule, indicating which app owns it. [CHAR_LIMIT=100] -->
    <string name="zen_mode_implicit_trigger_description">Managed by <xliff:g id="app_name">%1$s</xliff:g></string>
    <!-- Zen mode - Condition summary when a rule is activated due to a call to setInterruptionFilter(). [CHAR_LIMIT=NONE] -->
    <string name="zen_mode_implicit_activated">On</string>
+1 −0
Original line number Diff line number Diff line
@@ -2652,6 +2652,7 @@
  <java-symbol type="string" name="zen_mode_default_weekends_name" />
  <java-symbol type="string" name="zen_mode_default_events_name" />
  <java-symbol type="string" name="zen_mode_default_every_night_name" />
  <java-symbol type="string" name="zen_mode_implicit_name" />
  <java-symbol type="string" name="zen_mode_implicit_trigger_description" />
  <java-symbol type="string" name="zen_mode_implicit_activated" />
  <java-symbol type="string" name="zen_mode_implicit_deactivated" />
+46 −19
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import static android.service.notification.ZenModeConfig.ORIGIN_USER_IN_SYSTEMUI
import static android.service.notification.ZenModeConfig.ZenRule.OVERRIDE_ACTIVATE;
import static android.service.notification.ZenModeConfig.ZenRule.OVERRIDE_DEACTIVATE;
import static android.service.notification.ZenModeConfig.implicitRuleId;
import static android.service.notification.ZenModeConfig.isImplicitRuleId;

import static com.android.internal.util.FrameworkStatsLog.DND_MODE_RULE;
import static com.android.internal.util.Preconditions.checkArgument;
@@ -170,7 +171,6 @@ public class ZenModeHelper {
    private final Clock mClock;
    private final SettingsObserver mSettingsObserver;
    private final AppOpsManager mAppOps;
    private final NotificationManager mNotificationManager;
    private final ZenModeConfig mDefaultConfig;
    private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
    private final ZenModeFiltering mFiltering;
@@ -217,7 +217,6 @@ public class ZenModeHelper {
        mClock = clock;
        addCallback(mMetrics);
        mAppOps = context.getSystemService(AppOpsManager.class);
        mNotificationManager = context.getSystemService(NotificationManager.class);

        mDefaultConfig = Flags.modesUi()
                ? ZenModeConfig.getDefaultConfig()
@@ -660,7 +659,12 @@ public class ZenModeHelper {
                    // (whether initialized here or set via app or user).
                    rule.zenPolicy = config.getZenPolicy().copy();
                    newConfig.automaticRules.put(rule.id, rule);
                } else {
                    if (Flags.modesUi()) {
                        updateImplicitZenRuleNameAndDescription(rule);
                    }
                }

                // If the user has changed the rule's *zenMode*, then don't let app overwrite it.
                // We allow the update if the user has only changed other aspects of the rule.
                if ((rule.userModifiedFields & AutomaticZenRule.FIELD_INTERRUPTION_FILTER) == 0) {
@@ -707,7 +711,12 @@ public class ZenModeHelper {
                rule = newImplicitZenRule(callingPkg);
                rule.zenMode = Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
                newConfig.automaticRules.put(rule.id, rule);
            } else {
                if (Flags.modesUi()) {
                    updateImplicitZenRuleNameAndDescription(rule);
                }
            }

            // If the user has changed the rule's *ZenPolicy*, then don't let app overwrite it.
            // We allow the update if the user has only changed other aspects of the rule.
            if (rule.zenPolicyUserModifiedFields == 0) {
@@ -777,24 +786,8 @@ public class ZenModeHelper {
        rule.id = implicitRuleId(pkg);
        rule.pkg = pkg;
        rule.creationTime = mClock.millis();

        Binder.withCleanCallingIdentity(() -> {
            try {
                ApplicationInfo applicationInfo = mPm.getApplicationInfo(pkg, 0);
                rule.name = applicationInfo.loadLabel(mPm).toString();
                if (!Flags.modesUi()) {
                    rule.iconResName = drawableResIdToResName(pkg, applicationInfo.icon);
                }
            } catch (PackageManager.NameNotFoundException e) {
                // Should not happen, since it's the app calling us (?)
                Log.w(TAG, "Package not found for creating implicit zen rule");
                rule.name = "Unknown";
            }
        });

        updateImplicitZenRuleNameAndDescription(rule);
        rule.type = AutomaticZenRule.TYPE_OTHER;
        rule.triggerDescription = mContext.getString(R.string.zen_mode_implicit_trigger_description,
                rule.name);
        rule.condition = null;
        rule.conditionId = new Uri.Builder()
                .scheme(Condition.SCHEME)
@@ -809,6 +802,38 @@ public class ZenModeHelper {
        return rule;
    }

    private void updateImplicitZenRuleNameAndDescription(ZenRule rule) {
        checkArgument(isImplicitRuleId(rule.id));
        requireNonNull(rule.pkg, "Implicit rule is not associated to package yet!");

        String pkgAppName = Binder.withCleanCallingIdentity(() -> {
            try {
                ApplicationInfo applicationInfo = mPm.getApplicationInfo(rule.pkg, 0);
                return applicationInfo.loadLabel(mPm).toString();
            } catch (PackageManager.NameNotFoundException e) {
                // Should not happen. When creating it's the app calling us, and when updating
                // the rule would've been deleted if the package was removed.
                Slog.e(TAG, "Package not found when updating implicit zen rule name", e);
                return null;
            }
        });

        if (pkgAppName != null) {
            if ((rule.userModifiedFields & AutomaticZenRule.FIELD_NAME) == 0) {
                if (Flags.modesUi()) {
                    rule.name = mContext.getString(R.string.zen_mode_implicit_name, pkgAppName);
                } else {
                    rule.name = pkgAppName;
                }
            }
            rule.triggerDescription = mContext.getString(
                    R.string.zen_mode_implicit_trigger_description, pkgAppName);
        } else if (rule.name == null) {
            // We must give a new rule SOME name. But this path should never be hit.
            rule.name = "Unknown";
        }
    }

    boolean removeAutomaticZenRule(UserHandle user, String id, @ConfigOrigin int origin,
            String reason, int callingUid) {
        checkManageRuleOrigin("removeAutomaticZenRule", origin);
@@ -1130,6 +1155,8 @@ public class ZenModeHelper {
                for (ZenRule rule : newConfig.automaticRules.values()) {
                    if (SystemZenRules.isSystemOwnedRule(rule)) {
                        updated |= SystemZenRules.updateTriggerDescription(mContext, rule);
                    } else if (isImplicitRuleId(rule.id)) {
                        updateImplicitZenRuleNameAndDescription(rule);
                    }
                }
            }
+92 −3
Original line number Diff line number Diff line
@@ -6248,6 +6248,50 @@ public class ZenModeHelperTest extends UiServiceTestCase {
                .isEqualTo(STATE_TRUE);
    }

    @Test
    @EnableFlags({FLAG_MODES_API, FLAG_MODES_UI})
    public void applyGlobalZenModeAsImplicitZenRule_again_refreshesRuleName() {
        mZenModeHelper.mConfig.automaticRules.clear();
        mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT,
                mContext.getPackageName(), CUSTOM_PKG_UID, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
        assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(1);
        assertThat(mZenModeHelper.mConfig.automaticRules.valueAt(0).name)
                .isEqualTo("Do Not Disturb (" + CUSTOM_APP_LABEL + ")");
        // "Break" the rule name to check that applying again restores it.
        mZenModeHelper.mConfig.automaticRules.valueAt(0).name = "BOOM!";

        mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT,
                mContext.getPackageName(), CUSTOM_PKG_UID, ZEN_MODE_ALARMS);

        assertThat(mZenModeHelper.mConfig.automaticRules.valueAt(0).name)
                .isEqualTo("Do Not Disturb (" + CUSTOM_APP_LABEL + ")");
    }

    @Test
    @EnableFlags({FLAG_MODES_API, FLAG_MODES_UI})
    public void applyGlobalZenModeAsImplicitZenRule_again_doesNotChangeCustomizedRuleName() {
        mZenModeHelper.mConfig.automaticRules.clear();
        mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT,
                mContext.getPackageName(), CUSTOM_PKG_UID, ZEN_MODE_IMPORTANT_INTERRUPTIONS);
        assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(1);
        assertThat(mZenModeHelper.mConfig.automaticRules.valueAt(0).name)
                .isEqualTo("Do Not Disturb (" + CUSTOM_APP_LABEL + ")");
        String ruleId = ZenModeConfig.implicitRuleId(mContext.getPackageName());

        // User chooses a new name.
        AutomaticZenRule azr = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId);
        mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId,
                new AutomaticZenRule.Builder(azr).setName("User chose this").build(),
                ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID);

        // App triggers the rule again.
        mZenModeHelper.applyGlobalZenModeAsImplicitZenRule(UserHandle.CURRENT,
                mContext.getPackageName(), CUSTOM_PKG_UID, ZEN_MODE_ALARMS);

        assertThat(mZenModeHelper.mConfig.automaticRules.valueAt(0).name)
                .isEqualTo("User chose this");
    }

    @Test
    @DisableFlags(FLAG_MODES_API)
    public void applyGlobalZenModeAsImplicitZenRule_flagOff_ignored() {
@@ -6398,6 +6442,50 @@ public class ZenModeHelperTest extends UiServiceTestCase {
                .isEqualTo(originalEffectiveZenPolicy.overwrittenWith(appsSecondZenPolicy));
    }

    @Test
    @EnableFlags({FLAG_MODES_API, FLAG_MODES_UI})
    public void applyGlobalPolicyAsImplicitZenRule_again_refreshesRuleName() {
        mZenModeHelper.mConfig.automaticRules.clear();
        mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(UserHandle.CURRENT,
                mContext.getPackageName(), CUSTOM_PKG_UID, new Policy(0, 0, 0));
        assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(1);
        assertThat(mZenModeHelper.mConfig.automaticRules.valueAt(0).name)
                .isEqualTo("Do Not Disturb (" + CUSTOM_APP_LABEL + ")");
        // "Break" the rule name to check that updating it again restores it.
        mZenModeHelper.mConfig.automaticRules.valueAt(0).name = "BOOM!";

        mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(UserHandle.CURRENT,
                mContext.getPackageName(), CUSTOM_PKG_UID, new Policy(0, 0, 0));

        assertThat(mZenModeHelper.mConfig.automaticRules.valueAt(0).name)
                .isEqualTo("Do Not Disturb (" + CUSTOM_APP_LABEL + ")");
    }

    @Test
    @EnableFlags({FLAG_MODES_API, FLAG_MODES_UI})
    public void applyGlobalPolicyAsImplicitZenRule_again_doesNotChangeCustomizedRuleName() {
        mZenModeHelper.mConfig.automaticRules.clear();
        mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(UserHandle.CURRENT,
                mContext.getPackageName(), CUSTOM_PKG_UID, new Policy(0, 0, 0));
        assertThat(mZenModeHelper.mConfig.automaticRules).hasSize(1);
        assertThat(mZenModeHelper.mConfig.automaticRules.valueAt(0).name)
                .isEqualTo("Do Not Disturb (" + CUSTOM_APP_LABEL + ")");
        String ruleId = ZenModeConfig.implicitRuleId(mContext.getPackageName());

        // User chooses a new name.
        AutomaticZenRule azr = mZenModeHelper.getAutomaticZenRule(UserHandle.CURRENT, ruleId);
        mZenModeHelper.updateAutomaticZenRule(UserHandle.CURRENT, ruleId,
                new AutomaticZenRule.Builder(azr).setName("User chose this").build(),
                ORIGIN_USER_IN_SYSTEMUI, "reason", SYSTEM_UID);

        // App updates the implicit rule again.
        mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(UserHandle.CURRENT,
                mContext.getPackageName(), CUSTOM_PKG_UID, new Policy(0, 0, 0));

        assertThat(mZenModeHelper.mConfig.automaticRules.valueAt(0).name)
                .isEqualTo("User chose this");
    }

    @Test
    @DisableFlags(FLAG_MODES_API)
    public void applyGlobalPolicyAsImplicitZenRule_flagOff_ignored() {
@@ -7247,9 +7335,10 @@ public class ZenModeHelperTest extends UiServiceTestCase {
        rule.zenMode = zenMode;
        rule.zenPolicy = policy;
        rule.pkg = ownerPkg;
        if (Flags.modesUi()) {
            rule.name = mContext.getString(R.string.zen_mode_implicit_name, CUSTOM_APP_LABEL);
        } else {
            rule.name = CUSTOM_APP_LABEL;
        if (!Flags.modesUi()) {
            rule.iconResName = ICON_RES_NAME;
        }
        rule.triggerDescription = mContext.getString(R.string.zen_mode_implicit_trigger_description,
                CUSTOM_APP_LABEL);