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

Commit f3b92d24 authored by Beverly's avatar Beverly
Browse files

Load default zen rules from xml

Change-Id: Ic82b5d048346127847685791164f0decc8b61f92
Fixes: 114200998
Test: atest frameworks/base/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java
Test: atest frameworks/base/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
parent b11b75ac
Loading
Loading
Loading
Loading
+14 −2
Original line number Diff line number Diff line
@@ -146,6 +146,7 @@ public class ZenModeConfig implements Parcelable {
    private static final String RULE_ATT_CONDITION_ID = "conditionId";
    private static final String RULE_ATT_CREATION_TIME = "creationTime";
    private static final String RULE_ATT_ENABLER = "enabler";
    private static final String RULE_ATT_MODIFIED = "modified";

    @UnsupportedAppUsage
    public boolean allowAlarms = DEFAULT_ALLOW_ALARMS;
@@ -633,6 +634,7 @@ public class ZenModeConfig implements Parcelable {
            Slog.i(TAG, "Updating zenMode of automatic rule " + rt.name);
            rt.zenMode = Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
        }
        rt.modified = safeBoolean(parser, RULE_ATT_MODIFIED, false);
        return rt;
    }

@@ -656,6 +658,7 @@ public class ZenModeConfig implements Parcelable {
        if (rule.condition != null) {
            writeConditionXml(rule.condition, out);
        }
        out.attribute(null, RULE_ATT_MODIFIED, Boolean.toString(rule.modified));
    }

    public static Condition readConditionXml(XmlPullParser parser) {
@@ -1456,6 +1459,7 @@ public class ZenModeConfig implements Parcelable {
        public long creationTime;        // required for automatic
        public String enabler;          // package name, only used for manual rules.
        public ZenPolicy zenPolicy;
        public boolean modified;    // rule has been modified from initial creation

        public ZenRule() { }

@@ -1477,6 +1481,7 @@ public class ZenModeConfig implements Parcelable {
                enabler = source.readString();
            }
            zenPolicy = source.readParcelable(null);
            modified = source.readInt() == 1;
        }

        @Override
@@ -1512,6 +1517,7 @@ public class ZenModeConfig implements Parcelable {
                dest.writeInt(0);
            }
            dest.writeParcelable(zenPolicy, 0);
            dest.writeInt(modified ? 1 : 0);
        }

        @Override
@@ -1528,6 +1534,7 @@ public class ZenModeConfig implements Parcelable {
                    .append(",creationTime=").append(creationTime)
                    .append(",enabler=").append(enabler)
                    .append(",zenPolicy=").append(zenPolicy)
                    .append(",modified=").append(modified)
                    .append(']').toString();
        }

@@ -1554,6 +1561,7 @@ public class ZenModeConfig implements Parcelable {
            if (zenPolicy != null) {
                zenPolicy.writeToProto(proto, ZenRuleProto.ZEN_POLICY);
            }
            proto.write(ZenRuleProto.MODIFIED, modified);
            proto.end(token);
        }

@@ -1606,6 +1614,9 @@ public class ZenModeConfig implements Parcelable {
            if (!Objects.equals(zenPolicy, to.zenPolicy)) {
                d.addLine(item, "zenPolicy", zenPolicy, to.zenPolicy);
            }
            if (modified != to.modified) {
                d.addLine(item, "modified", modified, to.modified);
            }
        }

        @Override
@@ -1622,13 +1633,14 @@ public class ZenModeConfig implements Parcelable {
                    && Objects.equals(other.component, component)
                    && Objects.equals(other.id, id)
                    && Objects.equals(other.enabler, enabler)
                    && Objects.equals(other.zenPolicy, zenPolicy);
                    && Objects.equals(other.zenPolicy, zenPolicy)
                    && other.modified == modified;
        }

        @Override
        public int hashCode() {
            return Objects.hash(enabled, snoozing, name, zenMode, conditionId, condition,
                    component, id, enabler, zenPolicy);
                    component, id, enabler, zenPolicy, modified);
        }

        public boolean isAutomaticActive() {
+3 −0
Original line number Diff line number Diff line
@@ -201,6 +201,9 @@ message ZenRuleProto {
    optional ConditionProto condition = 9;
    optional android.content.ComponentNameProto component = 10;
    optional ZenPolicyProto zenPolicy = 11;

    // Indicates whether this ZenRule has been modified after its initial creation
    optional bool modified = 12 [ (android.privacy).dest = DEST_AUTOMATIC ];
}

// A dump from com.android.server.notification.ZenModeHelper.
+7 −3
Original line number Diff line number Diff line
@@ -18,13 +18,17 @@
-->

<!-- Default configuration for zen mode.  See android.service.notification.ZenModeConfig. -->
<zen version="7">
<zen version="8">
    <allow alarms="true" media="true" system="false" calls="true" callsFrom="2" messages="false"
           reminders="false" events="false" repeatCallers="true" />

    <automatic ruleId="EVENTS_DEFAULT_RULE" enabled="false" snoozing="false" name="Event" zen="1"
               component="android/com.android.server.notification.EventConditionProvider"
               conditionId="condition://android/event?userId=-10000&amp;calendar=&amp;reply=1"/>
    <automatic ruleId="EVERY_NIGHT_DEFAULT_RULE" enabled="false" snoozing="false" name="Sleeping"
               zen="1" component="android/com.android.server.notification.ScheduleConditionProvider"
               conditionId="condition://android/schedule?days=1.2.3.4.5.6.7&amp;start=22.0&amp;end=7.0&amp;exitAtAlarm=true"/>
    <!-- all visual effects that exist as of P -->
    <disallow visualEffects="511" />

    <!-- whether there are notification channels that can bypass dnd -->
    <state areChannelsBypassingDnd="false" />
</zen>
+29 −76
Original line number Diff line number Diff line
@@ -52,8 +52,6 @@ import android.provider.Settings.Global;
import android.service.notification.Condition;
import android.service.notification.ConditionProviderService;
import android.service.notification.ZenModeConfig;
import android.service.notification.ZenModeConfig.EventInfo;
import android.service.notification.ZenModeConfig.ScheduleInfo;
import android.service.notification.ZenModeConfig.ZenRule;
import android.service.notification.ZenModeProto;
import android.util.AndroidRuntimeException;
@@ -119,8 +117,6 @@ public class ZenModeHelper {
    public static final long SUPPRESSED_EFFECT_ALL = SUPPRESSED_EFFECT_CALLS
            | SUPPRESSED_EFFECT_NOTIFICATIONS;

    protected String mDefaultRuleEveryNightName;
    protected String mDefaultRuleEventsName;
    @VisibleForTesting protected boolean mIsBootComplete;

    public ZenModeHelper(Context context, Looper looper, ConditionProviders conditionProviders) {
@@ -130,9 +126,9 @@ public class ZenModeHelper {
        mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        mNotificationManager =  context.getSystemService(NotificationManager.class);

        mDefaultConfig = new ZenModeConfig();
        setDefaultZenRules(mContext);
        mConfig = mDefaultConfig;
        mDefaultConfig = readDefaultConfig(mContext.getResources());
        updateDefaultAutomaticRuleNames();
        mConfig = mDefaultConfig.copy();
        mConfigs.put(UserHandle.USER_SYSTEM, mConfig);

        mSettingsObserver = new SettingsObserver(mHandler);
@@ -311,7 +307,9 @@ public class ZenModeHelper {
            newConfig = mConfig.copy();
            ZenRule rule = new ZenRule();
            populateZenRule(automaticZenRule, rule, true);
            newConfig.automaticRules.put(rule.id, rule);
            if (newConfig.automaticRules.put(rule.id, rule) != null) {
                rule.modified = true;
            }
            if (setConfigLocked(newConfig, reason, rule.component, true)) {
                return rule.id;
            } else {
@@ -341,7 +339,9 @@ public class ZenModeHelper {
                }
            }
            populateZenRule(automaticZenRule, rule, false);
            newConfig.automaticRules.put(ruleId, rule);
            if (newConfig.automaticRules.put(ruleId, rule) != null) {
                rule.modified = true;
            }
            return setConfigLocked(newConfig, reason, rule.component, true);
        }
    }
@@ -413,17 +413,6 @@ public class ZenModeHelper {
        }
    }

    public void setDefaultZenRules(Context context) {
        mDefaultConfig = readDefaultConfig(context.getResources());
        appendDefaultRules(mDefaultConfig);
    }

    private void appendDefaultRules (ZenModeConfig config) {
        getDefaultRuleNames();
        appendDefaultEveryNightRule(config);
        appendDefaultEventRules(config);
    }

    // Checks zen rule properties are the same (doesn't check creation time, name nor enabled)
    // used to check if default rules were customized or not
    private boolean ruleValuesEqual(AutomaticZenRule rule, ZenRule defaultRule) {
@@ -437,22 +426,16 @@ public class ZenModeHelper {
    }

    protected void updateDefaultZenRules() {
        ZenModeConfig configDefaultRules = new ZenModeConfig();
        appendDefaultRules(configDefaultRules); // "new" localized default rules
        for (String ruleId : ZenModeConfig.DEFAULT_RULE_IDS) {
            AutomaticZenRule currRule = getAutomaticZenRule(ruleId);
            ZenRule defaultRule = configDefaultRules.automaticRules.get(ruleId);
            // if default rule wasn't customized, use localized name instead of previous
            if (ruleValuesEqual(currRule, defaultRule) &&
                    !defaultRule.name.equals(currRule.getName())) {
        updateDefaultAutomaticRuleNames();
        for (ZenRule defaultRule : mDefaultConfig.automaticRules.values()) {
            ZenRule currRule = mConfig.automaticRules.get(defaultRule.id);
            // if default rule wasn't modified, use localized name instead of previous
            if (!currRule.modified && !defaultRule.name.equals(currRule.name)) {
                if (canManageAutomaticZenRule(defaultRule)) {
                    if (DEBUG) Slog.d(TAG, "Locale change - updating default zen rule name "
                            + "from " + currRule.getName() + " to " + defaultRule.name);
                            + "from " + currRule.name + " to " + defaultRule.name);
                    // update default rule (if locale changed, name of rule will change)
                    AutomaticZenRule defaultAutoRule = createAutomaticZenRule(defaultRule);
                    // ensure enabled state is carried over from current rule
                    defaultAutoRule.setEnabled(currRule.isEnabled());
                    updateAutomaticZenRule(ruleId, defaultAutoRule,
                    updateAutomaticZenRule(defaultRule.id, createAutomaticZenRule(defaultRule),
                            "locale changed");
                }
            }
@@ -642,7 +625,9 @@ public class ZenModeHelper {
                // - doesn't already have default rules and
                // - all previous automatic rules were disabled
                config.automaticRules = new ArrayMap<>();
                appendDefaultRules(config);
                for (ZenRule rule : mDefaultConfig.automaticRules.values()) {
                    config.automaticRules.put(rule.id, rule);
                }
                reason += ", reset to default rules";
            }

@@ -854,12 +839,16 @@ public class ZenModeHelper {
        }
    }

    private void getDefaultRuleNames() {
        // on locale-change, these values differ
        mDefaultRuleEveryNightName = mContext.getResources()
                .getString(R.string.zen_mode_default_every_night_name);
        mDefaultRuleEventsName = mContext.getResources()
    private void updateDefaultAutomaticRuleNames() {
        for (ZenRule rule : mDefaultConfig.automaticRules.values()) {
            if (ZenModeConfig.EVENTS_DEFAULT_RULE_ID.equals(rule.id)) {
                rule.name = mContext.getResources()
                        .getString(R.string.zen_mode_default_events_name);
            } else if (ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID.equals(rule.id)) {
                rule.name = mContext.getResources()
                        .getString(R.string.zen_mode_default_every_night_name);
            }
        }
    }

    @VisibleForTesting
@@ -1001,42 +990,6 @@ public class ZenModeHelper {
        return new ZenModeConfig();
    }

    private void appendDefaultEveryNightRule(ZenModeConfig config) {
        if (config == null) return;

        final ScheduleInfo weeknights = new ScheduleInfo();
        weeknights.days = ZenModeConfig.ALL_DAYS;
        weeknights.startHour = 22;
        weeknights.endHour = 7;
        weeknights.exitAtAlarm = true;
        final ZenRule rule = new ZenRule();
        rule.enabled = false;
        rule.name = mDefaultRuleEveryNightName;
        rule.conditionId = ZenModeConfig.toScheduleConditionId(weeknights);
        rule.zenMode = Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
        rule.component = ScheduleConditionProvider.COMPONENT;
        rule.id = ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID;
        rule.creationTime = System.currentTimeMillis();
        config.automaticRules.put(rule.id, rule);
    }

    private void appendDefaultEventRules(ZenModeConfig config) {
        if (config == null) return;

        final EventInfo events = new EventInfo();
        events.calendar = null; // any calendar
        events.reply = EventInfo.REPLY_YES_OR_MAYBE;
        final ZenRule rule = new ZenRule();
        rule.enabled = false;
        rule.name = mDefaultRuleEventsName;
        rule.conditionId = ZenModeConfig.toEventConditionId(events);
        rule.zenMode = Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
        rule.component = EventConditionProvider.COMPONENT;
        rule.id = ZenModeConfig.EVENTS_DEFAULT_RULE_ID;
        rule.creationTime = System.currentTimeMillis();
        config.automaticRules.put(rule.id, rule);
    }

    private static int zenSeverity(int zen) {
        switch (zen) {
            case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return 1;
+338 −7

File changed.

Preview size limit exceeded, changes collapsed.