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