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