ZenModeConfig.java revision 56106ff337e056d2daa5862545f4a06796d9e9a1
1/**
2 * Copyright (c) 2014, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.service.notification;
18
19import android.app.ActivityManager;
20import android.app.NotificationManager.Policy;
21import android.content.ComponentName;
22import android.content.Context;
23import android.content.res.Resources;
24import android.net.Uri;
25import android.os.Parcel;
26import android.os.Parcelable;
27import android.os.UserHandle;
28import android.provider.Settings.Global;
29import android.text.TextUtils;
30import android.text.format.DateFormat;
31import android.util.ArrayMap;
32import android.util.ArraySet;
33import android.util.Slog;
34
35import com.android.internal.R;
36
37import org.xmlpull.v1.XmlPullParser;
38import org.xmlpull.v1.XmlPullParserException;
39import org.xmlpull.v1.XmlSerializer;
40
41import java.io.IOException;
42import java.util.ArrayList;
43import java.util.Calendar;
44import java.util.Locale;
45import java.util.Objects;
46import java.util.UUID;
47
48/**
49 * Persisted configuration for zen mode.
50 *
51 * @hide
52 */
53public class ZenModeConfig implements Parcelable {
54    private static String TAG = "ZenModeConfig";
55
56    public static final int SOURCE_ANYONE = 0;
57    public static final int SOURCE_CONTACT = 1;
58    public static final int SOURCE_STAR = 2;
59    public static final int MAX_SOURCE = SOURCE_STAR;
60    private static final int DEFAULT_SOURCE = SOURCE_CONTACT;
61
62    public static final int[] ALL_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY,
63            Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY };
64    public static final int[] WEEKNIGHT_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY,
65            Calendar.WEDNESDAY, Calendar.THURSDAY };
66    public static final int[] WEEKEND_DAYS = { Calendar.FRIDAY, Calendar.SATURDAY };
67
68    public static final int[] MINUTE_BUCKETS = generateMinuteBuckets();
69    private static final int SECONDS_MS = 1000;
70    private static final int MINUTES_MS = 60 * SECONDS_MS;
71    private static final int ZERO_VALUE_MS = 10 * SECONDS_MS;
72
73    private static final boolean DEFAULT_ALLOW_CALLS = true;
74    private static final boolean DEFAULT_ALLOW_MESSAGES = false;
75    private static final boolean DEFAULT_ALLOW_REMINDERS = true;
76    private static final boolean DEFAULT_ALLOW_EVENTS = true;
77    private static final boolean DEFAULT_ALLOW_REPEAT_CALLERS = false;
78
79    private static final int XML_VERSION = 2;
80    private static final String ZEN_TAG = "zen";
81    private static final String ZEN_ATT_VERSION = "version";
82    private static final String ZEN_ATT_USER = "user";
83    private static final String ALLOW_TAG = "allow";
84    private static final String ALLOW_ATT_CALLS = "calls";
85    private static final String ALLOW_ATT_REPEAT_CALLERS = "repeatCallers";
86    private static final String ALLOW_ATT_MESSAGES = "messages";
87    private static final String ALLOW_ATT_FROM = "from";
88    private static final String ALLOW_ATT_CALLS_FROM = "callsFrom";
89    private static final String ALLOW_ATT_MESSAGES_FROM = "messagesFrom";
90    private static final String ALLOW_ATT_REMINDERS = "reminders";
91    private static final String ALLOW_ATT_EVENTS = "events";
92
93    private static final String CONDITION_TAG = "condition";
94    private static final String CONDITION_ATT_COMPONENT = "component";
95    private static final String CONDITION_ATT_ID = "id";
96    private static final String CONDITION_ATT_SUMMARY = "summary";
97    private static final String CONDITION_ATT_LINE1 = "line1";
98    private static final String CONDITION_ATT_LINE2 = "line2";
99    private static final String CONDITION_ATT_ICON = "icon";
100    private static final String CONDITION_ATT_STATE = "state";
101    private static final String CONDITION_ATT_FLAGS = "flags";
102
103    private static final String MANUAL_TAG = "manual";
104    private static final String AUTOMATIC_TAG = "automatic";
105
106    private static final String RULE_ATT_ID = "ruleId";
107    private static final String RULE_ATT_ENABLED = "enabled";
108    private static final String RULE_ATT_SNOOZING = "snoozing";
109    private static final String RULE_ATT_NAME = "name";
110    private static final String RULE_ATT_COMPONENT = "component";
111    private static final String RULE_ATT_ZEN = "zen";
112    private static final String RULE_ATT_CONDITION_ID = "conditionId";
113    private static final String RULE_ATT_CREATION_TIME = "creationTime";
114
115    public boolean allowCalls = DEFAULT_ALLOW_CALLS;
116    public boolean allowRepeatCallers = DEFAULT_ALLOW_REPEAT_CALLERS;
117    public boolean allowMessages = DEFAULT_ALLOW_MESSAGES;
118    public boolean allowReminders = DEFAULT_ALLOW_REMINDERS;
119    public boolean allowEvents = DEFAULT_ALLOW_EVENTS;
120    public int allowCallsFrom = DEFAULT_SOURCE;
121    public int allowMessagesFrom = DEFAULT_SOURCE;
122    public int user = UserHandle.USER_SYSTEM;
123
124    public ZenRule manualRule;
125    public ArrayMap<String, ZenRule> automaticRules = new ArrayMap<>();
126
127    public ZenModeConfig() { }
128
129    public ZenModeConfig(Parcel source) {
130        allowCalls = source.readInt() == 1;
131        allowRepeatCallers = source.readInt() == 1;
132        allowMessages = source.readInt() == 1;
133        allowReminders = source.readInt() == 1;
134        allowEvents = source.readInt() == 1;
135        allowCallsFrom = source.readInt();
136        allowMessagesFrom = source.readInt();
137        user = source.readInt();
138        manualRule = source.readParcelable(null);
139        final int len = source.readInt();
140        if (len > 0) {
141            final String[] ids = new String[len];
142            final ZenRule[] rules = new ZenRule[len];
143            source.readStringArray(ids);
144            source.readTypedArray(rules, ZenRule.CREATOR);
145            for (int i = 0; i < len; i++) {
146                automaticRules.put(ids[i], rules[i]);
147            }
148        }
149    }
150
151    @Override
152    public void writeToParcel(Parcel dest, int flags) {
153        dest.writeInt(allowCalls ? 1 : 0);
154        dest.writeInt(allowRepeatCallers ? 1 : 0);
155        dest.writeInt(allowMessages ? 1 : 0);
156        dest.writeInt(allowReminders ? 1 : 0);
157        dest.writeInt(allowEvents ? 1 : 0);
158        dest.writeInt(allowCallsFrom);
159        dest.writeInt(allowMessagesFrom);
160        dest.writeInt(user);
161        dest.writeParcelable(manualRule, 0);
162        if (!automaticRules.isEmpty()) {
163            final int len = automaticRules.size();
164            final String[] ids = new String[len];
165            final ZenRule[] rules = new ZenRule[len];
166            for (int i = 0; i < len; i++) {
167                ids[i] = automaticRules.keyAt(i);
168                rules[i] = automaticRules.valueAt(i);
169            }
170            dest.writeInt(len);
171            dest.writeStringArray(ids);
172            dest.writeTypedArray(rules, 0);
173        } else {
174            dest.writeInt(0);
175        }
176    }
177
178    @Override
179    public String toString() {
180        return new StringBuilder(ZenModeConfig.class.getSimpleName()).append('[')
181            .append("user=").append(user)
182            .append(",allowCalls=").append(allowCalls)
183            .append(",allowRepeatCallers=").append(allowRepeatCallers)
184            .append(",allowMessages=").append(allowMessages)
185            .append(",allowCallsFrom=").append(sourceToString(allowCallsFrom))
186            .append(",allowMessagesFrom=").append(sourceToString(allowMessagesFrom))
187            .append(",allowReminders=").append(allowReminders)
188            .append(",allowEvents=").append(allowEvents)
189            .append(",automaticRules=").append(automaticRules)
190            .append(",manualRule=").append(manualRule)
191            .append(']').toString();
192    }
193
194    private Diff diff(ZenModeConfig to) {
195        final Diff d = new Diff();
196        if (to == null) {
197            return d.addLine("config", "delete");
198        }
199        if (user != to.user) {
200            d.addLine("user", user, to.user);
201        }
202        if (allowCalls != to.allowCalls) {
203            d.addLine("allowCalls", allowCalls, to.allowCalls);
204        }
205        if (allowRepeatCallers != to.allowRepeatCallers) {
206            d.addLine("allowRepeatCallers", allowRepeatCallers, to.allowRepeatCallers);
207        }
208        if (allowMessages != to.allowMessages) {
209            d.addLine("allowMessages", allowMessages, to.allowMessages);
210        }
211        if (allowCallsFrom != to.allowCallsFrom) {
212            d.addLine("allowCallsFrom", allowCallsFrom, to.allowCallsFrom);
213        }
214        if (allowMessagesFrom != to.allowMessagesFrom) {
215            d.addLine("allowMessagesFrom", allowMessagesFrom, to.allowMessagesFrom);
216        }
217        if (allowReminders != to.allowReminders) {
218            d.addLine("allowReminders", allowReminders, to.allowReminders);
219        }
220        if (allowEvents != to.allowEvents) {
221            d.addLine("allowEvents", allowEvents, to.allowEvents);
222        }
223        final ArraySet<String> allRules = new ArraySet<>();
224        addKeys(allRules, automaticRules);
225        addKeys(allRules, to.automaticRules);
226        final int N = allRules.size();
227        for (int i = 0; i < N; i++) {
228            final String rule = allRules.valueAt(i);
229            final ZenRule fromRule = automaticRules != null ? automaticRules.get(rule) : null;
230            final ZenRule toRule = to.automaticRules != null ? to.automaticRules.get(rule) : null;
231            ZenRule.appendDiff(d, "automaticRule[" + rule + "]", fromRule, toRule);
232        }
233        ZenRule.appendDiff(d, "manualRule", manualRule, to.manualRule);
234        return d;
235    }
236
237    public static Diff diff(ZenModeConfig from, ZenModeConfig to) {
238        if (from == null) {
239            final Diff d = new Diff();
240            if (to != null) {
241                d.addLine("config", "insert");
242            }
243            return d;
244        }
245        return from.diff(to);
246    }
247
248    private static <T> void addKeys(ArraySet<T> set, ArrayMap<T, ?> map) {
249        if (map != null) {
250            for (int i = 0; i < map.size(); i++) {
251                set.add(map.keyAt(i));
252            }
253        }
254    }
255
256    public boolean isValid() {
257        if (!isValidManualRule(manualRule)) return false;
258        final int N = automaticRules.size();
259        for (int i = 0; i < N; i++) {
260            if (!isValidAutomaticRule(automaticRules.valueAt(i))) return false;
261        }
262        return true;
263    }
264
265    private static boolean isValidManualRule(ZenRule rule) {
266        return rule == null || Global.isValidZenMode(rule.zenMode) && sameCondition(rule);
267    }
268
269    private static boolean isValidAutomaticRule(ZenRule rule) {
270        return rule != null && !TextUtils.isEmpty(rule.name) && Global.isValidZenMode(rule.zenMode)
271                && rule.conditionId != null && sameCondition(rule);
272    }
273
274    private static boolean sameCondition(ZenRule rule) {
275        if (rule == null) return false;
276        if (rule.conditionId == null) {
277            return rule.condition == null;
278        } else {
279            return rule.condition == null || rule.conditionId.equals(rule.condition.id);
280        }
281    }
282
283    private static int[] generateMinuteBuckets() {
284        final int maxHrs = 12;
285        final int[] buckets = new int[maxHrs + 3];
286        buckets[0] = 15;
287        buckets[1] = 30;
288        buckets[2] = 45;
289        for (int i = 1; i <= maxHrs; i++) {
290            buckets[2 + i] = 60 * i;
291        }
292        return buckets;
293    }
294
295    public static String sourceToString(int source) {
296        switch (source) {
297            case SOURCE_ANYONE:
298                return "anyone";
299            case SOURCE_CONTACT:
300                return "contacts";
301            case SOURCE_STAR:
302                return "stars";
303            default:
304                return "UNKNOWN";
305        }
306    }
307
308    @Override
309    public boolean equals(Object o) {
310        if (!(o instanceof ZenModeConfig)) return false;
311        if (o == this) return true;
312        final ZenModeConfig other = (ZenModeConfig) o;
313        return other.allowCalls == allowCalls
314                && other.allowRepeatCallers == allowRepeatCallers
315                && other.allowMessages == allowMessages
316                && other.allowCallsFrom == allowCallsFrom
317                && other.allowMessagesFrom == allowMessagesFrom
318                && other.allowReminders == allowReminders
319                && other.allowEvents == allowEvents
320                && other.user == user
321                && Objects.equals(other.automaticRules, automaticRules)
322                && Objects.equals(other.manualRule, manualRule);
323    }
324
325    @Override
326    public int hashCode() {
327        return Objects.hash(allowCalls, allowRepeatCallers, allowMessages, allowCallsFrom,
328                allowMessagesFrom, allowReminders, allowEvents, user, automaticRules, manualRule);
329    }
330
331    private static String toDayList(int[] days) {
332        if (days == null || days.length == 0) return "";
333        final StringBuilder sb = new StringBuilder();
334        for (int i = 0; i < days.length; i++) {
335            if (i > 0) sb.append('.');
336            sb.append(days[i]);
337        }
338        return sb.toString();
339    }
340
341    private static int[] tryParseDayList(String dayList, String sep) {
342        if (dayList == null) return null;
343        final String[] tokens = dayList.split(sep);
344        if (tokens.length == 0) return null;
345        final int[] rt = new int[tokens.length];
346        for (int i = 0; i < tokens.length; i++) {
347            final int day = tryParseInt(tokens[i], -1);
348            if (day == -1) return null;
349            rt[i] = day;
350        }
351        return rt;
352    }
353
354    private static int tryParseInt(String value, int defValue) {
355        if (TextUtils.isEmpty(value)) return defValue;
356        try {
357            return Integer.valueOf(value);
358        } catch (NumberFormatException e) {
359            return defValue;
360        }
361    }
362
363    private static long tryParseLong(String value, long defValue) {
364        if (TextUtils.isEmpty(value)) return defValue;
365        try {
366            return Long.valueOf(value);
367        } catch (NumberFormatException e) {
368            return defValue;
369        }
370    }
371
372    public static ZenModeConfig readXml(XmlPullParser parser, Migration migration)
373            throws XmlPullParserException, IOException {
374        int type = parser.getEventType();
375        if (type != XmlPullParser.START_TAG) return null;
376        String tag = parser.getName();
377        if (!ZEN_TAG.equals(tag)) return null;
378        final ZenModeConfig rt = new ZenModeConfig();
379        final int version = safeInt(parser, ZEN_ATT_VERSION, XML_VERSION);
380        if (version == 1) {
381            final XmlV1 v1 = XmlV1.readXml(parser);
382            return migration.migrate(v1);
383        }
384        rt.user = safeInt(parser, ZEN_ATT_USER, rt.user);
385        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
386            tag = parser.getName();
387            if (type == XmlPullParser.END_TAG && ZEN_TAG.equals(tag)) {
388                return rt;
389            }
390            if (type == XmlPullParser.START_TAG) {
391                if (ALLOW_TAG.equals(tag)) {
392                    rt.allowCalls = safeBoolean(parser, ALLOW_ATT_CALLS, false);
393                    rt.allowRepeatCallers = safeBoolean(parser, ALLOW_ATT_REPEAT_CALLERS,
394                            DEFAULT_ALLOW_REPEAT_CALLERS);
395                    rt.allowMessages = safeBoolean(parser, ALLOW_ATT_MESSAGES, false);
396                    rt.allowReminders = safeBoolean(parser, ALLOW_ATT_REMINDERS,
397                            DEFAULT_ALLOW_REMINDERS);
398                    rt.allowEvents = safeBoolean(parser, ALLOW_ATT_EVENTS, DEFAULT_ALLOW_EVENTS);
399                    final int from = safeInt(parser, ALLOW_ATT_FROM, -1);
400                    final int callsFrom = safeInt(parser, ALLOW_ATT_CALLS_FROM, -1);
401                    final int messagesFrom = safeInt(parser, ALLOW_ATT_MESSAGES_FROM, -1);
402                    if (isValidSource(callsFrom) && isValidSource(messagesFrom)) {
403                        rt.allowCallsFrom = callsFrom;
404                        rt.allowMessagesFrom = messagesFrom;
405                    } else if (isValidSource(from)) {
406                        Slog.i(TAG, "Migrating existing shared 'from': " + sourceToString(from));
407                        rt.allowCallsFrom = from;
408                        rt.allowMessagesFrom = from;
409                    } else {
410                        rt.allowCallsFrom = DEFAULT_SOURCE;
411                        rt.allowMessagesFrom = DEFAULT_SOURCE;
412                    }
413                } else if (MANUAL_TAG.equals(tag)) {
414                    rt.manualRule = readRuleXml(parser);
415                } else if (AUTOMATIC_TAG.equals(tag)) {
416                    final String id = parser.getAttributeValue(null, RULE_ATT_ID);
417                    final ZenRule automaticRule = readRuleXml(parser);
418                    if (id != null && automaticRule != null) {
419                        automaticRule.id = id;
420                        rt.automaticRules.put(id, automaticRule);
421                    }
422                }
423            }
424        }
425        throw new IllegalStateException("Failed to reach END_DOCUMENT");
426    }
427
428    public void writeXml(XmlSerializer out) throws IOException {
429        out.startTag(null, ZEN_TAG);
430        out.attribute(null, ZEN_ATT_VERSION, Integer.toString(XML_VERSION));
431        out.attribute(null, ZEN_ATT_USER, Integer.toString(user));
432
433        out.startTag(null, ALLOW_TAG);
434        out.attribute(null, ALLOW_ATT_CALLS, Boolean.toString(allowCalls));
435        out.attribute(null, ALLOW_ATT_REPEAT_CALLERS, Boolean.toString(allowRepeatCallers));
436        out.attribute(null, ALLOW_ATT_MESSAGES, Boolean.toString(allowMessages));
437        out.attribute(null, ALLOW_ATT_REMINDERS, Boolean.toString(allowReminders));
438        out.attribute(null, ALLOW_ATT_EVENTS, Boolean.toString(allowEvents));
439        out.attribute(null, ALLOW_ATT_CALLS_FROM, Integer.toString(allowCallsFrom));
440        out.attribute(null, ALLOW_ATT_MESSAGES_FROM, Integer.toString(allowMessagesFrom));
441        out.endTag(null, ALLOW_TAG);
442
443        if (manualRule != null) {
444            out.startTag(null, MANUAL_TAG);
445            writeRuleXml(manualRule, out);
446            out.endTag(null, MANUAL_TAG);
447        }
448        final int N = automaticRules.size();
449        for (int i = 0; i < N; i++) {
450            final String id = automaticRules.keyAt(i);
451            final ZenRule automaticRule = automaticRules.valueAt(i);
452            out.startTag(null, AUTOMATIC_TAG);
453            out.attribute(null, RULE_ATT_ID, id);
454            writeRuleXml(automaticRule, out);
455            out.endTag(null, AUTOMATIC_TAG);
456        }
457        out.endTag(null, ZEN_TAG);
458    }
459
460    public static ZenRule readRuleXml(XmlPullParser parser) {
461        final ZenRule rt = new ZenRule();
462        rt.enabled = safeBoolean(parser, RULE_ATT_ENABLED, true);
463        rt.snoozing = safeBoolean(parser, RULE_ATT_SNOOZING, false);
464        rt.name = parser.getAttributeValue(null, RULE_ATT_NAME);
465        final String zen = parser.getAttributeValue(null, RULE_ATT_ZEN);
466        rt.zenMode = tryParseZenMode(zen, -1);
467        if (rt.zenMode == -1) {
468            Slog.w(TAG, "Bad zen mode in rule xml:" + zen);
469            return null;
470        }
471        rt.conditionId = safeUri(parser, RULE_ATT_CONDITION_ID);
472        rt.component = safeComponentName(parser, RULE_ATT_COMPONENT);
473        rt.creationTime = safeLong(parser, RULE_ATT_CREATION_TIME, 0);
474        rt.condition = readConditionXml(parser);
475        return rt;
476    }
477
478    public static void writeRuleXml(ZenRule rule, XmlSerializer out) throws IOException {
479        out.attribute(null, RULE_ATT_ENABLED, Boolean.toString(rule.enabled));
480        out.attribute(null, RULE_ATT_SNOOZING, Boolean.toString(rule.snoozing));
481        if (rule.name != null) {
482            out.attribute(null, RULE_ATT_NAME, rule.name);
483        }
484        out.attribute(null, RULE_ATT_ZEN, Integer.toString(rule.zenMode));
485        if (rule.component != null) {
486            out.attribute(null, RULE_ATT_COMPONENT, rule.component.flattenToString());
487        }
488        if (rule.conditionId != null) {
489            out.attribute(null, RULE_ATT_CONDITION_ID, rule.conditionId.toString());
490        }
491        out.attribute(null, RULE_ATT_CREATION_TIME, Long.toString(rule.creationTime));
492        if (rule.condition != null) {
493            writeConditionXml(rule.condition, out);
494        }
495    }
496
497    public static Condition readConditionXml(XmlPullParser parser) {
498        final Uri id = safeUri(parser, CONDITION_ATT_ID);
499        if (id == null) return null;
500        final String summary = parser.getAttributeValue(null, CONDITION_ATT_SUMMARY);
501        final String line1 = parser.getAttributeValue(null, CONDITION_ATT_LINE1);
502        final String line2 = parser.getAttributeValue(null, CONDITION_ATT_LINE2);
503        final int icon = safeInt(parser, CONDITION_ATT_ICON, -1);
504        final int state = safeInt(parser, CONDITION_ATT_STATE, -1);
505        final int flags = safeInt(parser, CONDITION_ATT_FLAGS, -1);
506        try {
507            return new Condition(id, summary, line1, line2, icon, state, flags);
508        } catch (IllegalArgumentException e) {
509            Slog.w(TAG, "Unable to read condition xml", e);
510            return null;
511        }
512    }
513
514    public static void writeConditionXml(Condition c, XmlSerializer out) throws IOException {
515        out.attribute(null, CONDITION_ATT_ID, c.id.toString());
516        out.attribute(null, CONDITION_ATT_SUMMARY, c.summary);
517        out.attribute(null, CONDITION_ATT_LINE1, c.line1);
518        out.attribute(null, CONDITION_ATT_LINE2, c.line2);
519        out.attribute(null, CONDITION_ATT_ICON, Integer.toString(c.icon));
520        out.attribute(null, CONDITION_ATT_STATE, Integer.toString(c.state));
521        out.attribute(null, CONDITION_ATT_FLAGS, Integer.toString(c.flags));
522    }
523
524    public static boolean isValidHour(int val) {
525        return val >= 0 && val < 24;
526    }
527
528    public static boolean isValidMinute(int val) {
529        return val >= 0 && val < 60;
530    }
531
532    private static boolean isValidSource(int source) {
533        return source >= SOURCE_ANYONE && source <= MAX_SOURCE;
534    }
535
536    private static boolean safeBoolean(XmlPullParser parser, String att, boolean defValue) {
537        final String val = parser.getAttributeValue(null, att);
538        if (TextUtils.isEmpty(val)) return defValue;
539        return Boolean.valueOf(val);
540    }
541
542    private static int safeInt(XmlPullParser parser, String att, int defValue) {
543        final String val = parser.getAttributeValue(null, att);
544        return tryParseInt(val, defValue);
545    }
546
547    private static ComponentName safeComponentName(XmlPullParser parser, String att) {
548        final String val = parser.getAttributeValue(null, att);
549        if (TextUtils.isEmpty(val)) return null;
550        return ComponentName.unflattenFromString(val);
551    }
552
553    private static Uri safeUri(XmlPullParser parser, String att) {
554        final String val = parser.getAttributeValue(null, att);
555        if (TextUtils.isEmpty(val)) return null;
556        return Uri.parse(val);
557    }
558
559    private static long safeLong(XmlPullParser parser, String att, long defValue) {
560        final String val = parser.getAttributeValue(null, att);
561        return tryParseLong(val, defValue);
562    }
563
564    public ArraySet<String> getAutomaticRuleNames() {
565        final ArraySet<String> rt = new ArraySet<String>();
566        for (int i = 0; i < automaticRules.size(); i++) {
567            rt.add(automaticRules.valueAt(i).name);
568        }
569        return rt;
570    }
571
572    @Override
573    public int describeContents() {
574        return 0;
575    }
576
577    public ZenModeConfig copy() {
578        final Parcel parcel = Parcel.obtain();
579        try {
580            writeToParcel(parcel, 0);
581            parcel.setDataPosition(0);
582            return new ZenModeConfig(parcel);
583        } finally {
584            parcel.recycle();
585        }
586    }
587
588    public static final Parcelable.Creator<ZenModeConfig> CREATOR
589            = new Parcelable.Creator<ZenModeConfig>() {
590        @Override
591        public ZenModeConfig createFromParcel(Parcel source) {
592            return new ZenModeConfig(source);
593        }
594
595        @Override
596        public ZenModeConfig[] newArray(int size) {
597            return new ZenModeConfig[size];
598        }
599    };
600
601    public Policy toNotificationPolicy() {
602        int priorityCategories = 0;
603        int priorityCallSenders = Policy.PRIORITY_SENDERS_CONTACTS;
604        int priorityMessageSenders = Policy.PRIORITY_SENDERS_CONTACTS;
605        if (allowCalls) {
606            priorityCategories |= Policy.PRIORITY_CATEGORY_CALLS;
607        }
608        if (allowMessages) {
609            priorityCategories |= Policy.PRIORITY_CATEGORY_MESSAGES;
610        }
611        if (allowEvents) {
612            priorityCategories |= Policy.PRIORITY_CATEGORY_EVENTS;
613        }
614        if (allowReminders) {
615            priorityCategories |= Policy.PRIORITY_CATEGORY_REMINDERS;
616        }
617        if (allowRepeatCallers) {
618            priorityCategories |= Policy.PRIORITY_CATEGORY_REPEAT_CALLERS;
619        }
620        priorityCallSenders = sourceToPrioritySenders(allowCallsFrom, priorityCallSenders);
621        priorityMessageSenders = sourceToPrioritySenders(allowMessagesFrom, priorityMessageSenders);
622        return new Policy(priorityCategories, priorityCallSenders, priorityMessageSenders);
623    }
624
625    private static int sourceToPrioritySenders(int source, int def) {
626        switch (source) {
627            case SOURCE_ANYONE: return Policy.PRIORITY_SENDERS_ANY;
628            case SOURCE_CONTACT: return Policy.PRIORITY_SENDERS_CONTACTS;
629            case SOURCE_STAR: return Policy.PRIORITY_SENDERS_STARRED;
630            default: return def;
631        }
632    }
633
634    private static int prioritySendersToSource(int prioritySenders, int def) {
635        switch (prioritySenders) {
636            case Policy.PRIORITY_SENDERS_CONTACTS: return SOURCE_CONTACT;
637            case Policy.PRIORITY_SENDERS_STARRED: return SOURCE_STAR;
638            case Policy.PRIORITY_SENDERS_ANY: return SOURCE_ANYONE;
639            default: return def;
640        }
641    }
642
643    public void applyNotificationPolicy(Policy policy) {
644        if (policy == null) return;
645        allowCalls = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_CALLS) != 0;
646        allowMessages = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_MESSAGES) != 0;
647        allowEvents = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_EVENTS) != 0;
648        allowReminders = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_REMINDERS) != 0;
649        allowRepeatCallers = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_REPEAT_CALLERS)
650                != 0;
651        allowCallsFrom = prioritySendersToSource(policy.priorityCallSenders, allowCallsFrom);
652        allowMessagesFrom = prioritySendersToSource(policy.priorityMessageSenders,
653                allowMessagesFrom);
654    }
655
656    public static Condition toTimeCondition(Context context, int minutesFromNow, int userHandle) {
657        return toTimeCondition(context, minutesFromNow, userHandle, false /*shortVersion*/);
658    }
659
660    public static Condition toTimeCondition(Context context, int minutesFromNow, int userHandle,
661            boolean shortVersion) {
662        final long now = System.currentTimeMillis();
663        final long millis = minutesFromNow == 0 ? ZERO_VALUE_MS : minutesFromNow * MINUTES_MS;
664        return toTimeCondition(context, now + millis, minutesFromNow, now, userHandle,
665                shortVersion);
666    }
667
668    public static Condition toTimeCondition(Context context, long time, int minutes, long now,
669            int userHandle, boolean shortVersion) {
670        final int num, summaryResId, line1ResId;
671        if (minutes < 60) {
672            // display as minutes
673            num = minutes;
674            summaryResId = shortVersion ? R.plurals.zen_mode_duration_minutes_summary_short
675                    : R.plurals.zen_mode_duration_minutes_summary;
676            line1ResId = shortVersion ? R.plurals.zen_mode_duration_minutes_short
677                    : R.plurals.zen_mode_duration_minutes;
678        } else {
679            // display as hours
680            num =  Math.round(minutes / 60f);
681            summaryResId = shortVersion ? R.plurals.zen_mode_duration_hours_summary_short
682                    : R.plurals.zen_mode_duration_hours_summary;
683            line1ResId = shortVersion ? R.plurals.zen_mode_duration_hours_short
684                    : R.plurals.zen_mode_duration_hours;
685        }
686        final String skeleton = DateFormat.is24HourFormat(context, userHandle) ? "Hm" : "hma";
687        final String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton);
688        final CharSequence formattedTime = DateFormat.format(pattern, time);
689        final Resources res = context.getResources();
690        final String summary = res.getQuantityString(summaryResId, num, num, formattedTime);
691        final String line1 = res.getQuantityString(line1ResId, num, num, formattedTime);
692        final String line2 = res.getString(R.string.zen_mode_until, formattedTime);
693        final Uri id = toCountdownConditionId(time);
694        return new Condition(id, summary, line1, line2, 0, Condition.STATE_TRUE,
695                Condition.FLAG_RELEVANT_NOW);
696    }
697
698    // ==== Built-in system conditions ====
699
700    public static final String SYSTEM_AUTHORITY = "android";
701
702    // ==== Built-in system condition: countdown ====
703
704    public static final String COUNTDOWN_PATH = "countdown";
705
706    public static Uri toCountdownConditionId(long time) {
707        return new Uri.Builder().scheme(Condition.SCHEME)
708                .authority(SYSTEM_AUTHORITY)
709                .appendPath(COUNTDOWN_PATH)
710                .appendPath(Long.toString(time))
711                .build();
712    }
713
714    public static long tryParseCountdownConditionId(Uri conditionId) {
715        if (!Condition.isValidId(conditionId, SYSTEM_AUTHORITY)) return 0;
716        if (conditionId.getPathSegments().size() != 2
717                || !COUNTDOWN_PATH.equals(conditionId.getPathSegments().get(0))) return 0;
718        try {
719            return Long.parseLong(conditionId.getPathSegments().get(1));
720        } catch (RuntimeException e) {
721            Slog.w(TAG, "Error parsing countdown condition: " + conditionId, e);
722            return 0;
723        }
724    }
725
726    public static boolean isValidCountdownConditionId(Uri conditionId) {
727        return tryParseCountdownConditionId(conditionId) != 0;
728    }
729
730    // ==== Built-in system condition: schedule ====
731
732    public static final String SCHEDULE_PATH = "schedule";
733
734    public static Uri toScheduleConditionId(ScheduleInfo schedule) {
735        return new Uri.Builder().scheme(Condition.SCHEME)
736                .authority(SYSTEM_AUTHORITY)
737                .appendPath(SCHEDULE_PATH)
738                .appendQueryParameter("days", toDayList(schedule.days))
739                .appendQueryParameter("start", schedule.startHour + "." + schedule.startMinute)
740                .appendQueryParameter("end", schedule.endHour + "." + schedule.endMinute)
741                .build();
742    }
743
744    public static boolean isValidScheduleConditionId(Uri conditionId) {
745        return tryParseScheduleConditionId(conditionId) != null;
746    }
747
748    public static ScheduleInfo tryParseScheduleConditionId(Uri conditionId) {
749        final boolean isSchedule =  conditionId != null
750                && conditionId.getScheme().equals(Condition.SCHEME)
751                && conditionId.getAuthority().equals(ZenModeConfig.SYSTEM_AUTHORITY)
752                && conditionId.getPathSegments().size() == 1
753                && conditionId.getPathSegments().get(0).equals(ZenModeConfig.SCHEDULE_PATH);
754        if (!isSchedule) return null;
755        final int[] start = tryParseHourAndMinute(conditionId.getQueryParameter("start"));
756        final int[] end = tryParseHourAndMinute(conditionId.getQueryParameter("end"));
757        if (start == null || end == null) return null;
758        final ScheduleInfo rt = new ScheduleInfo();
759        rt.days = tryParseDayList(conditionId.getQueryParameter("days"), "\\.");
760        rt.startHour = start[0];
761        rt.startMinute = start[1];
762        rt.endHour = end[0];
763        rt.endMinute = end[1];
764        return rt;
765    }
766
767    public static ComponentName getScheduleConditionProvider() {
768        return new ComponentName(SYSTEM_AUTHORITY, "ScheduleConditionProvider");
769    }
770
771    public static class ScheduleInfo {
772        public int[] days;
773        public int startHour;
774        public int startMinute;
775        public int endHour;
776        public int endMinute;
777
778        @Override
779        public int hashCode() {
780            return 0;
781        }
782
783        @Override
784        public boolean equals(Object o) {
785            if (!(o instanceof ScheduleInfo)) return false;
786            final ScheduleInfo other = (ScheduleInfo) o;
787            return toDayList(days).equals(toDayList(other.days))
788                    && startHour == other.startHour
789                    && startMinute == other.startMinute
790                    && endHour == other.endHour
791                    && endMinute == other.endMinute;
792        }
793
794        public ScheduleInfo copy() {
795            final ScheduleInfo rt = new ScheduleInfo();
796            if (days != null) {
797                rt.days = new int[days.length];
798                System.arraycopy(days, 0, rt.days, 0, days.length);
799            }
800            rt.startHour = startHour;
801            rt.startMinute = startMinute;
802            rt.endHour = endHour;
803            rt.endMinute = endMinute;
804            return rt;
805        }
806    }
807
808    // ==== Built-in system condition: event ====
809
810    public static final String EVENT_PATH = "event";
811
812    public static Uri toEventConditionId(EventInfo event) {
813        return new Uri.Builder().scheme(Condition.SCHEME)
814                .authority(SYSTEM_AUTHORITY)
815                .appendPath(EVENT_PATH)
816                .appendQueryParameter("userId", Long.toString(event.userId))
817                .appendQueryParameter("calendar", event.calendar != null ? event.calendar : "")
818                .appendQueryParameter("reply", Integer.toString(event.reply))
819                .build();
820    }
821
822    public static boolean isValidEventConditionId(Uri conditionId) {
823        return tryParseEventConditionId(conditionId) != null;
824    }
825
826    public static EventInfo tryParseEventConditionId(Uri conditionId) {
827        final boolean isEvent = conditionId != null
828                && conditionId.getScheme().equals(Condition.SCHEME)
829                && conditionId.getAuthority().equals(ZenModeConfig.SYSTEM_AUTHORITY)
830                && conditionId.getPathSegments().size() == 1
831                && conditionId.getPathSegments().get(0).equals(EVENT_PATH);
832        if (!isEvent) return null;
833        final EventInfo rt = new EventInfo();
834        rt.userId = tryParseInt(conditionId.getQueryParameter("userId"), UserHandle.USER_NULL);
835        rt.calendar = conditionId.getQueryParameter("calendar");
836        if (TextUtils.isEmpty(rt.calendar) || tryParseLong(rt.calendar, -1L) != -1L) {
837            rt.calendar = null;
838        }
839        rt.reply = tryParseInt(conditionId.getQueryParameter("reply"), 0);
840        return rt;
841    }
842
843    public static ComponentName getEventConditionProvider() {
844        return new ComponentName(SYSTEM_AUTHORITY, "EventConditionProvider");
845    }
846
847    public static class EventInfo {
848        public static final int REPLY_ANY_EXCEPT_NO = 0;
849        public static final int REPLY_YES_OR_MAYBE = 1;
850        public static final int REPLY_YES = 2;
851
852        public int userId = UserHandle.USER_NULL;  // USER_NULL = unspecified - use current user
853        public String calendar;  // CalendarContract.Calendars.OWNER_ACCOUNT, or null for any
854        public int reply;
855
856        @Override
857        public int hashCode() {
858            return 0;
859        }
860
861        @Override
862        public boolean equals(Object o) {
863            if (!(o instanceof EventInfo)) return false;
864            final EventInfo other = (EventInfo) o;
865            return userId == other.userId
866                    && Objects.equals(calendar, other.calendar)
867                    && reply == other.reply;
868        }
869
870        public EventInfo copy() {
871            final EventInfo rt = new EventInfo();
872            rt.userId = userId;
873            rt.calendar = calendar;
874            rt.reply = reply;
875            return rt;
876        }
877
878        public static int resolveUserId(int userId) {
879            return userId == UserHandle.USER_NULL ? ActivityManager.getCurrentUser() : userId;
880        }
881    }
882
883    // ==== End built-in system conditions ====
884
885    private static int[] tryParseHourAndMinute(String value) {
886        if (TextUtils.isEmpty(value)) return null;
887        final int i = value.indexOf('.');
888        if (i < 1 || i >= value.length() - 1) return null;
889        final int hour = tryParseInt(value.substring(0, i), -1);
890        final int minute = tryParseInt(value.substring(i + 1), -1);
891        return isValidHour(hour) && isValidMinute(minute) ? new int[] { hour, minute } : null;
892    }
893
894    private static int tryParseZenMode(String value, int defValue) {
895        final int rt = tryParseInt(value, defValue);
896        return Global.isValidZenMode(rt) ? rt : defValue;
897    }
898
899    public String newRuleId() {
900        return UUID.randomUUID().toString().replace("-", "");
901    }
902
903    public static String getConditionLine1(Context context, ZenModeConfig config,
904            int userHandle, boolean shortVersion) {
905        return getConditionLine(context, config, userHandle, true /*useLine1*/, shortVersion);
906    }
907
908    public static String getConditionSummary(Context context, ZenModeConfig config,
909            int userHandle, boolean shortVersion) {
910        return getConditionLine(context, config, userHandle, false /*useLine1*/, shortVersion);
911    }
912
913    private static String getConditionLine(Context context, ZenModeConfig config,
914            int userHandle, boolean useLine1, boolean shortVersion) {
915        if (config == null) return "";
916        if (config.manualRule != null) {
917            final Uri id = config.manualRule.conditionId;
918            if (id == null) {
919                return context.getString(com.android.internal.R.string.zen_mode_forever);
920            }
921            final long time = tryParseCountdownConditionId(id);
922            Condition c = config.manualRule.condition;
923            if (time > 0) {
924                final long now = System.currentTimeMillis();
925                final long span = time - now;
926                c = toTimeCondition(context,
927                        time, Math.round(span / (float) MINUTES_MS), now, userHandle, shortVersion);
928            }
929            final String rt = c == null ? "" : useLine1 ? c.line1 : c.summary;
930            return TextUtils.isEmpty(rt) ? "" : rt;
931        }
932        String summary = "";
933        for (ZenRule automaticRule : config.automaticRules.values()) {
934            if (automaticRule.isAutomaticActive()) {
935                if (summary.isEmpty()) {
936                    summary = automaticRule.name;
937                } else {
938                    summary = context.getResources()
939                            .getString(R.string.zen_mode_rule_name_combination, summary,
940                                    automaticRule.name);
941                }
942            }
943        }
944        return summary;
945    }
946
947    public static class ZenRule implements Parcelable {
948        public boolean enabled;
949        public boolean snoozing;         // user manually disabled this instance
950        public String name;              // required for automatic (unique)
951        public int zenMode;
952        public Uri conditionId;          // required for automatic
953        public Condition condition;      // optional
954        public ComponentName component;  // optional
955        public String id;                // required for automatic (unique)
956        public long creationTime;        // required for automatic
957
958        public ZenRule() { }
959
960        public ZenRule(Parcel source) {
961            enabled = source.readInt() == 1;
962            snoozing = source.readInt() == 1;
963            if (source.readInt() == 1) {
964                name = source.readString();
965            }
966            zenMode = source.readInt();
967            conditionId = source.readParcelable(null);
968            condition = source.readParcelable(null);
969            component = source.readParcelable(null);
970            if (source.readInt() == 1) {
971                id = source.readString();
972            }
973            creationTime = source.readLong();
974        }
975
976        @Override
977        public int describeContents() {
978            return 0;
979        }
980
981        @Override
982        public void writeToParcel(Parcel dest, int flags) {
983            dest.writeInt(enabled ? 1 : 0);
984            dest.writeInt(snoozing ? 1 : 0);
985            if (name != null) {
986                dest.writeInt(1);
987                dest.writeString(name);
988            } else {
989                dest.writeInt(0);
990            }
991            dest.writeInt(zenMode);
992            dest.writeParcelable(conditionId, 0);
993            dest.writeParcelable(condition, 0);
994            dest.writeParcelable(component, 0);
995            if (id != null) {
996                dest.writeInt(1);
997                dest.writeString(id);
998            } else {
999                dest.writeInt(0);
1000            }
1001            dest.writeLong(creationTime);
1002        }
1003
1004        @Override
1005        public String toString() {
1006            return new StringBuilder(ZenRule.class.getSimpleName()).append('[')
1007                    .append("enabled=").append(enabled)
1008                    .append(",snoozing=").append(snoozing)
1009                    .append(",name=").append(name)
1010                    .append(",zenMode=").append(Global.zenModeToString(zenMode))
1011                    .append(",conditionId=").append(conditionId)
1012                    .append(",condition=").append(condition)
1013                    .append(",component=").append(component)
1014                    .append(",id=").append(id)
1015                    .append(",creationTime=").append(creationTime)
1016                    .append(']').toString();
1017        }
1018
1019        private static void appendDiff(Diff d, String item, ZenRule from, ZenRule to) {
1020            if (d == null) return;
1021            if (from == null) {
1022                if (to != null) {
1023                    d.addLine(item, "insert");
1024                }
1025                return;
1026            }
1027            from.appendDiff(d, item, to);
1028        }
1029
1030        private void appendDiff(Diff d, String item, ZenRule to) {
1031            if (to == null) {
1032                d.addLine(item, "delete");
1033                return;
1034            }
1035            if (enabled != to.enabled) {
1036                d.addLine(item, "enabled", enabled, to.enabled);
1037            }
1038            if (snoozing != to.snoozing) {
1039                d.addLine(item, "snoozing", snoozing, to.snoozing);
1040            }
1041            if (!Objects.equals(name, to.name)) {
1042                d.addLine(item, "name", name, to.name);
1043            }
1044            if (zenMode != to.zenMode) {
1045                d.addLine(item, "zenMode", zenMode, to.zenMode);
1046            }
1047            if (!Objects.equals(conditionId, to.conditionId)) {
1048                d.addLine(item, "conditionId", conditionId, to.conditionId);
1049            }
1050            if (!Objects.equals(condition, to.condition)) {
1051                d.addLine(item, "condition", condition, to.condition);
1052            }
1053            if (!Objects.equals(component, to.component)) {
1054                d.addLine(item, "component", component, to.component);
1055            }
1056            if (!Objects.equals(id, to.id)) {
1057                d.addLine(item, "id", id, to.id);
1058            }
1059            if (creationTime == to.creationTime) {
1060                d.addLine(item, "creationTime", creationTime, to.creationTime);
1061            }
1062        }
1063
1064        @Override
1065        public boolean equals(Object o) {
1066            if (!(o instanceof ZenRule)) return false;
1067            if (o == this) return true;
1068            final ZenRule other = (ZenRule) o;
1069            return other.enabled == enabled
1070                    && other.snoozing == snoozing
1071                    && Objects.equals(other.name, name)
1072                    && other.zenMode == zenMode
1073                    && Objects.equals(other.conditionId, conditionId)
1074                    && Objects.equals(other.condition, condition)
1075                    && Objects.equals(other.component, component)
1076                    && Objects.equals(other.id, id)
1077                    && other.creationTime == creationTime;
1078        }
1079
1080        @Override
1081        public int hashCode() {
1082            return Objects.hash(enabled, snoozing, name, zenMode, conditionId, condition,
1083                    component, id, creationTime);
1084        }
1085
1086        public boolean isAutomaticActive() {
1087            return enabled && !snoozing && component != null && isTrueOrUnknown();
1088        }
1089
1090        public boolean isTrueOrUnknown() {
1091            return condition != null && (condition.state == Condition.STATE_TRUE
1092                    || condition.state == Condition.STATE_UNKNOWN);
1093        }
1094
1095        public static final Parcelable.Creator<ZenRule> CREATOR
1096                = new Parcelable.Creator<ZenRule>() {
1097            @Override
1098            public ZenRule createFromParcel(Parcel source) {
1099                return new ZenRule(source);
1100            }
1101            @Override
1102            public ZenRule[] newArray(int size) {
1103                return new ZenRule[size];
1104            }
1105        };
1106    }
1107
1108    // Legacy config
1109    public static final class XmlV1 {
1110        public static final String SLEEP_MODE_NIGHTS = "nights";
1111        public static final String SLEEP_MODE_WEEKNIGHTS = "weeknights";
1112        public static final String SLEEP_MODE_DAYS_PREFIX = "days:";
1113
1114        private static final String EXIT_CONDITION_TAG = "exitCondition";
1115        private static final String EXIT_CONDITION_ATT_COMPONENT = "component";
1116        private static final String SLEEP_TAG = "sleep";
1117        private static final String SLEEP_ATT_MODE = "mode";
1118        private static final String SLEEP_ATT_NONE = "none";
1119
1120        private static final String SLEEP_ATT_START_HR = "startHour";
1121        private static final String SLEEP_ATT_START_MIN = "startMin";
1122        private static final String SLEEP_ATT_END_HR = "endHour";
1123        private static final String SLEEP_ATT_END_MIN = "endMin";
1124
1125        public boolean allowCalls;
1126        public boolean allowMessages;
1127        public boolean allowReminders = DEFAULT_ALLOW_REMINDERS;
1128        public boolean allowEvents = DEFAULT_ALLOW_EVENTS;
1129        public int allowFrom = SOURCE_ANYONE;
1130
1131        public String sleepMode;     // nights, weeknights, days:1,2,3  Calendar.days
1132        public int sleepStartHour;   // 0-23
1133        public int sleepStartMinute; // 0-59
1134        public int sleepEndHour;
1135        public int sleepEndMinute;
1136        public boolean sleepNone;    // false = priority, true = none
1137        public ComponentName[] conditionComponents;
1138        public Uri[] conditionIds;
1139        public Condition exitCondition;  // manual exit condition
1140        public ComponentName exitConditionComponent;  // manual exit condition component
1141
1142        private static boolean isValidSleepMode(String sleepMode) {
1143            return sleepMode == null || sleepMode.equals(SLEEP_MODE_NIGHTS)
1144                    || sleepMode.equals(SLEEP_MODE_WEEKNIGHTS) || tryParseDays(sleepMode) != null;
1145        }
1146
1147        public static int[] tryParseDays(String sleepMode) {
1148            if (sleepMode == null) return null;
1149            sleepMode = sleepMode.trim();
1150            if (SLEEP_MODE_NIGHTS.equals(sleepMode)) return ALL_DAYS;
1151            if (SLEEP_MODE_WEEKNIGHTS.equals(sleepMode)) return WEEKNIGHT_DAYS;
1152            if (!sleepMode.startsWith(SLEEP_MODE_DAYS_PREFIX)) return null;
1153            if (sleepMode.equals(SLEEP_MODE_DAYS_PREFIX)) return null;
1154            return tryParseDayList(sleepMode.substring(SLEEP_MODE_DAYS_PREFIX.length()), ",");
1155        }
1156
1157        public static XmlV1 readXml(XmlPullParser parser)
1158                throws XmlPullParserException, IOException {
1159            int type;
1160            String tag;
1161            XmlV1 rt = new XmlV1();
1162            final ArrayList<ComponentName> conditionComponents = new ArrayList<ComponentName>();
1163            final ArrayList<Uri> conditionIds = new ArrayList<Uri>();
1164            while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
1165                tag = parser.getName();
1166                if (type == XmlPullParser.END_TAG && ZEN_TAG.equals(tag)) {
1167                    if (!conditionComponents.isEmpty()) {
1168                        rt.conditionComponents = conditionComponents
1169                                .toArray(new ComponentName[conditionComponents.size()]);
1170                        rt.conditionIds = conditionIds.toArray(new Uri[conditionIds.size()]);
1171                    }
1172                    return rt;
1173                }
1174                if (type == XmlPullParser.START_TAG) {
1175                    if (ALLOW_TAG.equals(tag)) {
1176                        rt.allowCalls = safeBoolean(parser, ALLOW_ATT_CALLS, false);
1177                        rt.allowMessages = safeBoolean(parser, ALLOW_ATT_MESSAGES, false);
1178                        rt.allowReminders = safeBoolean(parser, ALLOW_ATT_REMINDERS,
1179                                DEFAULT_ALLOW_REMINDERS);
1180                        rt.allowEvents = safeBoolean(parser, ALLOW_ATT_EVENTS,
1181                                DEFAULT_ALLOW_EVENTS);
1182                        rt.allowFrom = safeInt(parser, ALLOW_ATT_FROM, SOURCE_ANYONE);
1183                        if (rt.allowFrom < SOURCE_ANYONE || rt.allowFrom > MAX_SOURCE) {
1184                            throw new IndexOutOfBoundsException("bad source in config:"
1185                                    + rt.allowFrom);
1186                        }
1187                    } else if (SLEEP_TAG.equals(tag)) {
1188                        final String mode = parser.getAttributeValue(null, SLEEP_ATT_MODE);
1189                        rt.sleepMode = isValidSleepMode(mode)? mode : null;
1190                        rt.sleepNone = safeBoolean(parser, SLEEP_ATT_NONE, false);
1191                        final int startHour = safeInt(parser, SLEEP_ATT_START_HR, 0);
1192                        final int startMinute = safeInt(parser, SLEEP_ATT_START_MIN, 0);
1193                        final int endHour = safeInt(parser, SLEEP_ATT_END_HR, 0);
1194                        final int endMinute = safeInt(parser, SLEEP_ATT_END_MIN, 0);
1195                        rt.sleepStartHour = isValidHour(startHour) ? startHour : 0;
1196                        rt.sleepStartMinute = isValidMinute(startMinute) ? startMinute : 0;
1197                        rt.sleepEndHour = isValidHour(endHour) ? endHour : 0;
1198                        rt.sleepEndMinute = isValidMinute(endMinute) ? endMinute : 0;
1199                    } else if (CONDITION_TAG.equals(tag)) {
1200                        final ComponentName component =
1201                                safeComponentName(parser, CONDITION_ATT_COMPONENT);
1202                        final Uri conditionId = safeUri(parser, CONDITION_ATT_ID);
1203                        if (component != null && conditionId != null) {
1204                            conditionComponents.add(component);
1205                            conditionIds.add(conditionId);
1206                        }
1207                    } else if (EXIT_CONDITION_TAG.equals(tag)) {
1208                        rt.exitCondition = readConditionXml(parser);
1209                        if (rt.exitCondition != null) {
1210                            rt.exitConditionComponent =
1211                                    safeComponentName(parser, EXIT_CONDITION_ATT_COMPONENT);
1212                        }
1213                    }
1214                }
1215            }
1216            throw new IllegalStateException("Failed to reach END_DOCUMENT");
1217        }
1218    }
1219
1220    public interface Migration {
1221        ZenModeConfig migrate(XmlV1 v1);
1222    }
1223
1224    public static class Diff {
1225        private final ArrayList<String> lines = new ArrayList<>();
1226
1227        @Override
1228        public String toString() {
1229            final StringBuilder sb = new StringBuilder("Diff[");
1230            final int N = lines.size();
1231            for (int i = 0; i < N; i++) {
1232                if (i > 0) {
1233                    sb.append(',');
1234                }
1235                sb.append(lines.get(i));
1236            }
1237            return sb.append(']').toString();
1238        }
1239
1240        private Diff addLine(String item, String action) {
1241            lines.add(item + ":" + action);
1242            return this;
1243        }
1244
1245        public Diff addLine(String item, String subitem, Object from, Object to) {
1246            return addLine(item + "." + subitem, from, to);
1247        }
1248
1249        public Diff addLine(String item, Object from, Object to) {
1250            return addLine(item, from + "->" + to);
1251        }
1252    }
1253
1254}
1255