ZenModeConfig.java revision 10a7cc127531c1917eab11fb953b2dcc84b7ec95
1/**
2 * Copyright (c) 2014, The Android Open Source Project
3 *
4 * Licensed under the Apache License,  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 static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
20import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS;
21import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
22
23import android.app.ActivityManager;
24import android.app.AlarmManager;
25import android.app.NotificationManager;
26import android.app.NotificationManager.Policy;
27import android.content.ComponentName;
28import android.content.Context;
29import android.content.pm.ApplicationInfo;
30import android.content.pm.PackageManager;
31import android.content.res.Resources;
32import android.net.Uri;
33import android.os.Parcel;
34import android.os.Parcelable;
35import android.os.UserHandle;
36import android.provider.Settings.Global;
37import android.text.TextUtils;
38import android.text.format.DateFormat;
39import android.util.ArrayMap;
40import android.util.ArraySet;
41import android.util.Slog;
42import android.util.proto.ProtoOutputStream;
43
44import com.android.internal.R;
45
46import org.xmlpull.v1.XmlPullParser;
47import org.xmlpull.v1.XmlPullParserException;
48import org.xmlpull.v1.XmlSerializer;
49
50import java.io.IOException;
51import java.util.ArrayList;
52import java.util.Arrays;
53import java.util.Calendar;
54import java.util.Date;
55import java.util.GregorianCalendar;
56import java.util.List;
57import java.util.Locale;
58import java.util.Objects;
59import java.util.TimeZone;
60import java.util.UUID;
61
62/**
63 * Persisted configuration for zen mode.
64 *
65 * @hide
66 */
67public class ZenModeConfig implements Parcelable {
68    private static String TAG = "ZenModeConfig";
69
70    public static final int SOURCE_ANYONE = 0;
71    public static final int SOURCE_CONTACT = 1;
72    public static final int SOURCE_STAR = 2;
73    public static final int MAX_SOURCE = SOURCE_STAR;
74    private static final int DEFAULT_SOURCE = SOURCE_CONTACT;
75    private static final int DEFAULT_CALLS_SOURCE = SOURCE_STAR;
76
77    public static final String EVENTS_DEFAULT_RULE_ID = "EVENTS_DEFAULT_RULE";
78    public static final String EVERY_NIGHT_DEFAULT_RULE_ID = "EVERY_NIGHT_DEFAULT_RULE";
79    public static final List<String> DEFAULT_RULE_IDS = Arrays.asList(EVERY_NIGHT_DEFAULT_RULE_ID,
80            EVENTS_DEFAULT_RULE_ID);
81
82    public static final int[] ALL_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY,
83            Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY };
84
85    public static final int[] MINUTE_BUCKETS = generateMinuteBuckets();
86    private static final int SECONDS_MS = 1000;
87    private static final int MINUTES_MS = 60 * SECONDS_MS;
88    private static final int DAY_MINUTES = 24 * 60;
89    private static final int ZERO_VALUE_MS = 10 * SECONDS_MS;
90
91    // Default allow categories set in readXml() from default_zen_mode_config.xml,
92    // fallback/upgrade values:
93    private static final boolean DEFAULT_ALLOW_ALARMS = true;
94    private static final boolean DEFAULT_ALLOW_MEDIA = true;
95    private static final boolean DEFAULT_ALLOW_SYSTEM = false;
96    private static final boolean DEFAULT_ALLOW_CALLS = true;
97    private static final boolean DEFAULT_ALLOW_MESSAGES = false;
98    private static final boolean DEFAULT_ALLOW_REMINDERS = false;
99    private static final boolean DEFAULT_ALLOW_EVENTS = false;
100    private static final boolean DEFAULT_ALLOW_REPEAT_CALLERS = true;
101    private static final boolean DEFAULT_CHANNELS_BYPASSING_DND = false;
102    private static final int DEFAULT_SUPPRESSED_VISUAL_EFFECTS = 0;
103
104    public static final int XML_VERSION = 8;
105    public static final String ZEN_TAG = "zen";
106    private static final String ZEN_ATT_VERSION = "version";
107    private static final String ZEN_ATT_USER = "user";
108    private static final String ALLOW_TAG = "allow";
109    private static final String ALLOW_ATT_ALARMS = "alarms";
110    private static final String ALLOW_ATT_MEDIA = "media";
111    private static final String ALLOW_ATT_SYSTEM = "system";
112    private static final String ALLOW_ATT_CALLS = "calls";
113    private static final String ALLOW_ATT_REPEAT_CALLERS = "repeatCallers";
114    private static final String ALLOW_ATT_MESSAGES = "messages";
115    private static final String ALLOW_ATT_FROM = "from";
116    private static final String ALLOW_ATT_CALLS_FROM = "callsFrom";
117    private static final String ALLOW_ATT_MESSAGES_FROM = "messagesFrom";
118    private static final String ALLOW_ATT_REMINDERS = "reminders";
119    private static final String ALLOW_ATT_EVENTS = "events";
120    private static final String ALLOW_ATT_SCREEN_OFF = "visualScreenOff";
121    private static final String ALLOW_ATT_SCREEN_ON = "visualScreenOn";
122    private static final String DISALLOW_TAG = "disallow";
123    private static final String DISALLOW_ATT_VISUAL_EFFECTS = "visualEffects";
124    private static final String STATE_TAG = "state";
125    private static final String STATE_ATT_CHANNELS_BYPASSING_DND = "areChannelsBypassingDnd";
126
127    private static final String CONDITION_ATT_ID = "id";
128    private static final String CONDITION_ATT_SUMMARY = "summary";
129    private static final String CONDITION_ATT_LINE1 = "line1";
130    private static final String CONDITION_ATT_LINE2 = "line2";
131    private static final String CONDITION_ATT_ICON = "icon";
132    private static final String CONDITION_ATT_STATE = "state";
133    private static final String CONDITION_ATT_FLAGS = "flags";
134
135    private static final String MANUAL_TAG = "manual";
136    private static final String AUTOMATIC_TAG = "automatic";
137
138    private static final String RULE_ATT_ID = "ruleId";
139    private static final String RULE_ATT_ENABLED = "enabled";
140    private static final String RULE_ATT_SNOOZING = "snoozing";
141    private static final String RULE_ATT_NAME = "name";
142    private static final String RULE_ATT_COMPONENT = "component";
143    private static final String RULE_ATT_ZEN = "zen";
144    private static final String RULE_ATT_CONDITION_ID = "conditionId";
145    private static final String RULE_ATT_CREATION_TIME = "creationTime";
146    private static final String RULE_ATT_ENABLER = "enabler";
147
148    public boolean allowAlarms = DEFAULT_ALLOW_ALARMS;
149    public boolean allowMedia = DEFAULT_ALLOW_MEDIA;
150    public boolean allowSystem = DEFAULT_ALLOW_SYSTEM;
151    public boolean allowCalls = DEFAULT_ALLOW_CALLS;
152    public boolean allowRepeatCallers = DEFAULT_ALLOW_REPEAT_CALLERS;
153    public boolean allowMessages = DEFAULT_ALLOW_MESSAGES;
154    public boolean allowReminders = DEFAULT_ALLOW_REMINDERS;
155    public boolean allowEvents = DEFAULT_ALLOW_EVENTS;
156    public int allowCallsFrom = DEFAULT_CALLS_SOURCE;
157    public int allowMessagesFrom = DEFAULT_SOURCE;
158    public int user = UserHandle.USER_SYSTEM;
159    public int suppressedVisualEffects = DEFAULT_SUPPRESSED_VISUAL_EFFECTS;
160    public boolean areChannelsBypassingDnd = DEFAULT_CHANNELS_BYPASSING_DND;
161    public int version;
162
163    public ZenRule manualRule;
164    public ArrayMap<String, ZenRule> automaticRules = new ArrayMap<>();
165
166    public ZenModeConfig() { }
167
168    public ZenModeConfig(Parcel source) {
169        allowCalls = source.readInt() == 1;
170        allowRepeatCallers = source.readInt() == 1;
171        allowMessages = source.readInt() == 1;
172        allowReminders = source.readInt() == 1;
173        allowEvents = source.readInt() == 1;
174        allowCallsFrom = source.readInt();
175        allowMessagesFrom = source.readInt();
176        user = source.readInt();
177        manualRule = source.readParcelable(null);
178        final int len = source.readInt();
179        if (len > 0) {
180            final String[] ids = new String[len];
181            final ZenRule[] rules = new ZenRule[len];
182            source.readStringArray(ids);
183            source.readTypedArray(rules, ZenRule.CREATOR);
184            for (int i = 0; i < len; i++) {
185                automaticRules.put(ids[i], rules[i]);
186            }
187        }
188        allowAlarms = source.readInt() == 1;
189        allowMedia = source.readInt() == 1;
190        allowSystem = source.readInt() == 1;
191        suppressedVisualEffects = source.readInt();
192        areChannelsBypassingDnd = source.readInt() == 1;
193    }
194
195    @Override
196    public void writeToParcel(Parcel dest, int flags) {
197        dest.writeInt(allowCalls ? 1 : 0);
198        dest.writeInt(allowRepeatCallers ? 1 : 0);
199        dest.writeInt(allowMessages ? 1 : 0);
200        dest.writeInt(allowReminders ? 1 : 0);
201        dest.writeInt(allowEvents ? 1 : 0);
202        dest.writeInt(allowCallsFrom);
203        dest.writeInt(allowMessagesFrom);
204        dest.writeInt(user);
205        dest.writeParcelable(manualRule, 0);
206        if (!automaticRules.isEmpty()) {
207            final int len = automaticRules.size();
208            final String[] ids = new String[len];
209            final ZenRule[] rules = new ZenRule[len];
210            for (int i = 0; i < len; i++) {
211                ids[i] = automaticRules.keyAt(i);
212                rules[i] = automaticRules.valueAt(i);
213            }
214            dest.writeInt(len);
215            dest.writeStringArray(ids);
216            dest.writeTypedArray(rules, 0);
217        } else {
218            dest.writeInt(0);
219        }
220        dest.writeInt(allowAlarms ? 1 : 0);
221        dest.writeInt(allowMedia ? 1 : 0);
222        dest.writeInt(allowSystem ? 1 : 0);
223        dest.writeInt(suppressedVisualEffects);
224        dest.writeInt(areChannelsBypassingDnd ? 1 : 0);
225    }
226
227    @Override
228    public String toString() {
229        return new StringBuilder(ZenModeConfig.class.getSimpleName()).append('[')
230                .append("user=").append(user)
231                .append(",allowAlarms=").append(allowAlarms)
232                .append(",allowMedia=").append(allowMedia)
233                .append(",allowSystem=").append(allowSystem)
234                .append(",allowReminders=").append(allowReminders)
235                .append(",allowEvents=").append(allowEvents)
236                .append(",allowCalls=").append(allowCalls)
237                .append(",allowRepeatCallers=").append(allowRepeatCallers)
238                .append(",allowMessages=").append(allowMessages)
239                .append(",allowCallsFrom=").append(sourceToString(allowCallsFrom))
240                .append(",allowMessagesFrom=").append(sourceToString(allowMessagesFrom))
241                .append(",suppressedVisualEffects=").append(suppressedVisualEffects)
242                .append(",areChannelsBypassingDnd=").append(areChannelsBypassingDnd)
243                .append(",automaticRules=").append(automaticRules)
244                .append(",manualRule=").append(manualRule)
245                .append(']').toString();
246    }
247
248    private Diff diff(ZenModeConfig to) {
249        final Diff d = new Diff();
250        if (to == null) {
251            return d.addLine("config", "delete");
252        }
253        if (user != to.user) {
254            d.addLine("user", user, to.user);
255        }
256        if (allowAlarms != to.allowAlarms) {
257            d.addLine("allowAlarms", allowAlarms, to.allowAlarms);
258        }
259        if (allowMedia != to.allowMedia) {
260            d.addLine("allowMedia", allowMedia, to.allowMedia);
261        }
262        if (allowSystem != to.allowSystem) {
263            d.addLine("allowSystem", allowSystem, to.allowSystem);
264        }
265        if (allowCalls != to.allowCalls) {
266            d.addLine("allowCalls", allowCalls, to.allowCalls);
267        }
268        if (allowReminders != to.allowReminders) {
269            d.addLine("allowReminders", allowReminders, to.allowReminders);
270        }
271        if (allowEvents != to.allowEvents) {
272            d.addLine("allowEvents", allowEvents, to.allowEvents);
273        }
274        if (allowRepeatCallers != to.allowRepeatCallers) {
275            d.addLine("allowRepeatCallers", allowRepeatCallers, to.allowRepeatCallers);
276        }
277        if (allowMessages != to.allowMessages) {
278            d.addLine("allowMessages", allowMessages, to.allowMessages);
279        }
280        if (allowCallsFrom != to.allowCallsFrom) {
281            d.addLine("allowCallsFrom", allowCallsFrom, to.allowCallsFrom);
282        }
283        if (allowMessagesFrom != to.allowMessagesFrom) {
284            d.addLine("allowMessagesFrom", allowMessagesFrom, to.allowMessagesFrom);
285        }
286        if (suppressedVisualEffects != to.suppressedVisualEffects) {
287            d.addLine("suppressedVisualEffects", suppressedVisualEffects,
288                    to.suppressedVisualEffects);
289        }
290        final ArraySet<String> allRules = new ArraySet<>();
291        addKeys(allRules, automaticRules);
292        addKeys(allRules, to.automaticRules);
293        final int N = allRules.size();
294        for (int i = 0; i < N; i++) {
295            final String rule = allRules.valueAt(i);
296            final ZenRule fromRule = automaticRules != null ? automaticRules.get(rule) : null;
297            final ZenRule toRule = to.automaticRules != null ? to.automaticRules.get(rule) : null;
298            ZenRule.appendDiff(d, "automaticRule[" + rule + "]", fromRule, toRule);
299        }
300        ZenRule.appendDiff(d, "manualRule", manualRule, to.manualRule);
301
302        if (areChannelsBypassingDnd != to.areChannelsBypassingDnd) {
303            d.addLine("areChannelsBypassingDnd", areChannelsBypassingDnd,
304                    to.areChannelsBypassingDnd);
305        }
306        return d;
307    }
308
309    public static Diff diff(ZenModeConfig from, ZenModeConfig to) {
310        if (from == null) {
311            final Diff d = new Diff();
312            if (to != null) {
313                d.addLine("config", "insert");
314            }
315            return d;
316        }
317        return from.diff(to);
318    }
319
320    private static <T> void addKeys(ArraySet<T> set, ArrayMap<T, ?> map) {
321        if (map != null) {
322            for (int i = 0; i < map.size(); i++) {
323                set.add(map.keyAt(i));
324            }
325        }
326    }
327
328    public boolean isValid() {
329        if (!isValidManualRule(manualRule)) return false;
330        final int N = automaticRules.size();
331        for (int i = 0; i < N; i++) {
332            if (!isValidAutomaticRule(automaticRules.valueAt(i))) return false;
333        }
334        return true;
335    }
336
337    private static boolean isValidManualRule(ZenRule rule) {
338        return rule == null || Global.isValidZenMode(rule.zenMode) && sameCondition(rule);
339    }
340
341    private static boolean isValidAutomaticRule(ZenRule rule) {
342        return rule != null && !TextUtils.isEmpty(rule.name) && Global.isValidZenMode(rule.zenMode)
343                && rule.conditionId != null && sameCondition(rule);
344    }
345
346    private static boolean sameCondition(ZenRule rule) {
347        if (rule == null) return false;
348        if (rule.conditionId == null) {
349            return rule.condition == null;
350        } else {
351            return rule.condition == null || rule.conditionId.equals(rule.condition.id);
352        }
353    }
354
355    private static int[] generateMinuteBuckets() {
356        final int maxHrs = 12;
357        final int[] buckets = new int[maxHrs + 3];
358        buckets[0] = 15;
359        buckets[1] = 30;
360        buckets[2] = 45;
361        for (int i = 1; i <= maxHrs; i++) {
362            buckets[2 + i] = 60 * i;
363        }
364        return buckets;
365    }
366
367    public static String sourceToString(int source) {
368        switch (source) {
369            case SOURCE_ANYONE:
370                return "anyone";
371            case SOURCE_CONTACT:
372                return "contacts";
373            case SOURCE_STAR:
374                return "stars";
375            default:
376                return "UNKNOWN";
377        }
378    }
379
380    @Override
381    public boolean equals(Object o) {
382        if (!(o instanceof ZenModeConfig)) return false;
383        if (o == this) return true;
384        final ZenModeConfig other = (ZenModeConfig) o;
385        return other.allowAlarms == allowAlarms
386                && other.allowMedia == allowMedia
387                && other.allowSystem == allowSystem
388                && other.allowCalls == allowCalls
389                && other.allowRepeatCallers == allowRepeatCallers
390                && other.allowMessages == allowMessages
391                && other.allowCallsFrom == allowCallsFrom
392                && other.allowMessagesFrom == allowMessagesFrom
393                && other.allowReminders == allowReminders
394                && other.allowEvents == allowEvents
395                && other.user == user
396                && Objects.equals(other.automaticRules, automaticRules)
397                && Objects.equals(other.manualRule, manualRule)
398                && other.suppressedVisualEffects == suppressedVisualEffects
399                && other.areChannelsBypassingDnd == areChannelsBypassingDnd;
400    }
401
402    @Override
403    public int hashCode() {
404        return Objects.hash(allowAlarms, allowMedia, allowSystem, allowCalls,
405                allowRepeatCallers, allowMessages,
406                allowCallsFrom, allowMessagesFrom, allowReminders, allowEvents,
407                user, automaticRules, manualRule,
408                suppressedVisualEffects, areChannelsBypassingDnd);
409    }
410
411    private static String toDayList(int[] days) {
412        if (days == null || days.length == 0) return "";
413        final StringBuilder sb = new StringBuilder();
414        for (int i = 0; i < days.length; i++) {
415            if (i > 0) sb.append('.');
416            sb.append(days[i]);
417        }
418        return sb.toString();
419    }
420
421    private static int[] tryParseDayList(String dayList, String sep) {
422        if (dayList == null) return null;
423        final String[] tokens = dayList.split(sep);
424        if (tokens.length == 0) return null;
425        final int[] rt = new int[tokens.length];
426        for (int i = 0; i < tokens.length; i++) {
427            final int day = tryParseInt(tokens[i], -1);
428            if (day == -1) return null;
429            rt[i] = day;
430        }
431        return rt;
432    }
433
434    private static int tryParseInt(String value, int defValue) {
435        if (TextUtils.isEmpty(value)) return defValue;
436        try {
437            return Integer.parseInt(value);
438        } catch (NumberFormatException e) {
439            return defValue;
440        }
441    }
442
443    private static long tryParseLong(String value, long defValue) {
444        if (TextUtils.isEmpty(value)) return defValue;
445        try {
446            return Long.parseLong(value);
447        } catch (NumberFormatException e) {
448            return defValue;
449        }
450    }
451
452    public static ZenModeConfig readXml(XmlPullParser parser)
453            throws XmlPullParserException, IOException {
454        int type = parser.getEventType();
455        if (type != XmlPullParser.START_TAG) return null;
456        String tag = parser.getName();
457        if (!ZEN_TAG.equals(tag)) return null;
458        final ZenModeConfig rt = new ZenModeConfig();
459        rt.version = safeInt(parser, ZEN_ATT_VERSION, XML_VERSION);
460        rt.user = safeInt(parser, ZEN_ATT_USER, rt.user);
461        boolean readSuppressedEffects = false;
462        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
463            tag = parser.getName();
464            if (type == XmlPullParser.END_TAG && ZEN_TAG.equals(tag)) {
465                return rt;
466            }
467            if (type == XmlPullParser.START_TAG) {
468                if (ALLOW_TAG.equals(tag)) {
469                    rt.allowCalls = safeBoolean(parser, ALLOW_ATT_CALLS,
470                            DEFAULT_ALLOW_CALLS);
471                    rt.allowRepeatCallers = safeBoolean(parser, ALLOW_ATT_REPEAT_CALLERS,
472                            DEFAULT_ALLOW_REPEAT_CALLERS);
473                    rt.allowMessages = safeBoolean(parser, ALLOW_ATT_MESSAGES,
474                            DEFAULT_ALLOW_MESSAGES);
475                    rt.allowReminders = safeBoolean(parser, ALLOW_ATT_REMINDERS,
476                            DEFAULT_ALLOW_REMINDERS);
477                    rt.allowEvents = safeBoolean(parser, ALLOW_ATT_EVENTS, DEFAULT_ALLOW_EVENTS);
478                    final int from = safeInt(parser, ALLOW_ATT_FROM, -1);
479                    final int callsFrom = safeInt(parser, ALLOW_ATT_CALLS_FROM, -1);
480                    final int messagesFrom = safeInt(parser, ALLOW_ATT_MESSAGES_FROM, -1);
481                    if (isValidSource(callsFrom) && isValidSource(messagesFrom)) {
482                        rt.allowCallsFrom = callsFrom;
483                        rt.allowMessagesFrom = messagesFrom;
484                    } else if (isValidSource(from)) {
485                        Slog.i(TAG, "Migrating existing shared 'from': " + sourceToString(from));
486                        rt.allowCallsFrom = from;
487                        rt.allowMessagesFrom = from;
488                    } else {
489                        rt.allowCallsFrom = DEFAULT_CALLS_SOURCE;
490                        rt.allowMessagesFrom = DEFAULT_SOURCE;
491                    }
492                    rt.allowAlarms = safeBoolean(parser, ALLOW_ATT_ALARMS, DEFAULT_ALLOW_ALARMS);
493                    rt.allowMedia = safeBoolean(parser, ALLOW_ATT_MEDIA,
494                            DEFAULT_ALLOW_MEDIA);
495                    rt.allowSystem = safeBoolean(parser, ALLOW_ATT_SYSTEM, DEFAULT_ALLOW_SYSTEM);
496
497                    // migrate old suppressed visual effects fields, if they still exist in the xml
498                    Boolean allowWhenScreenOff = unsafeBoolean(parser, ALLOW_ATT_SCREEN_OFF);
499                    if (allowWhenScreenOff != null) {
500                        readSuppressedEffects = true;
501                        if (allowWhenScreenOff) {
502                            rt.suppressedVisualEffects |= SUPPRESSED_EFFECT_LIGHTS
503                                    | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT;
504                        }
505                    }
506                    Boolean allowWhenScreenOn = unsafeBoolean(parser, ALLOW_ATT_SCREEN_ON);
507                    if (allowWhenScreenOn != null) {
508                        readSuppressedEffects = true;
509                        if (allowWhenScreenOn) {
510                            rt.suppressedVisualEffects |= SUPPRESSED_EFFECT_PEEK;
511                        }
512                    }
513                    if (readSuppressedEffects) {
514                        Slog.d(TAG, "Migrated visual effects to " + rt.suppressedVisualEffects);
515                    }
516                } else if (DISALLOW_TAG.equals(tag) && !readSuppressedEffects) {
517                    // only read from suppressed visual effects field if we haven't just migrated
518                    // the values from allowOn/allowOff, lest we wipe out those settings
519                    rt.suppressedVisualEffects = safeInt(parser, DISALLOW_ATT_VISUAL_EFFECTS,
520                            DEFAULT_SUPPRESSED_VISUAL_EFFECTS);
521                } else if (MANUAL_TAG.equals(tag)) {
522                    rt.manualRule = readRuleXml(parser);
523                } else if (AUTOMATIC_TAG.equals(tag)) {
524                    final String id = parser.getAttributeValue(null, RULE_ATT_ID);
525                    final ZenRule automaticRule = readRuleXml(parser);
526                    if (id != null && automaticRule != null) {
527                        automaticRule.id = id;
528                        rt.automaticRules.put(id, automaticRule);
529                    }
530                } else if (STATE_TAG.equals(tag)) {
531                    rt.areChannelsBypassingDnd = safeBoolean(parser,
532                            STATE_ATT_CHANNELS_BYPASSING_DND, DEFAULT_CHANNELS_BYPASSING_DND);
533                }
534            }
535        }
536        throw new IllegalStateException("Failed to reach END_DOCUMENT");
537    }
538
539    /**
540     * Writes XML of current ZenModeConfig
541     * @param out serializer
542     * @param version uses XML_VERSION if version is null
543     * @throws IOException
544     */
545    public void writeXml(XmlSerializer out, Integer version) throws IOException {
546        out.startTag(null, ZEN_TAG);
547        out.attribute(null, ZEN_ATT_VERSION, version == null
548                ? Integer.toString(XML_VERSION) : Integer.toString(version));
549        out.attribute(null, ZEN_ATT_USER, Integer.toString(user));
550        out.startTag(null, ALLOW_TAG);
551        out.attribute(null, ALLOW_ATT_CALLS, Boolean.toString(allowCalls));
552        out.attribute(null, ALLOW_ATT_REPEAT_CALLERS, Boolean.toString(allowRepeatCallers));
553        out.attribute(null, ALLOW_ATT_MESSAGES, Boolean.toString(allowMessages));
554        out.attribute(null, ALLOW_ATT_REMINDERS, Boolean.toString(allowReminders));
555        out.attribute(null, ALLOW_ATT_EVENTS, Boolean.toString(allowEvents));
556        out.attribute(null, ALLOW_ATT_CALLS_FROM, Integer.toString(allowCallsFrom));
557        out.attribute(null, ALLOW_ATT_MESSAGES_FROM, Integer.toString(allowMessagesFrom));
558        out.attribute(null, ALLOW_ATT_ALARMS, Boolean.toString(allowAlarms));
559        out.attribute(null, ALLOW_ATT_MEDIA, Boolean.toString(allowMedia));
560        out.attribute(null, ALLOW_ATT_SYSTEM, Boolean.toString(allowSystem));
561        out.endTag(null, ALLOW_TAG);
562
563        out.startTag(null, DISALLOW_TAG);
564        out.attribute(null, DISALLOW_ATT_VISUAL_EFFECTS, Integer.toString(suppressedVisualEffects));
565        out.endTag(null, DISALLOW_TAG);
566
567        if (manualRule != null) {
568            out.startTag(null, MANUAL_TAG);
569            writeRuleXml(manualRule, out);
570            out.endTag(null, MANUAL_TAG);
571        }
572        final int N = automaticRules.size();
573        for (int i = 0; i < N; i++) {
574            final String id = automaticRules.keyAt(i);
575            final ZenRule automaticRule = automaticRules.valueAt(i);
576            out.startTag(null, AUTOMATIC_TAG);
577            out.attribute(null, RULE_ATT_ID, id);
578            writeRuleXml(automaticRule, out);
579            out.endTag(null, AUTOMATIC_TAG);
580        }
581
582        out.startTag(null, STATE_TAG);
583        out.attribute(null, STATE_ATT_CHANNELS_BYPASSING_DND,
584                Boolean.toString(areChannelsBypassingDnd));
585        out.endTag(null, STATE_TAG);
586
587        out.endTag(null, ZEN_TAG);
588    }
589
590    public static ZenRule readRuleXml(XmlPullParser parser) {
591        final ZenRule rt = new ZenRule();
592        rt.enabled = safeBoolean(parser, RULE_ATT_ENABLED, true);
593        rt.snoozing = safeBoolean(parser, RULE_ATT_SNOOZING, false);
594        rt.name = parser.getAttributeValue(null, RULE_ATT_NAME);
595        final String zen = parser.getAttributeValue(null, RULE_ATT_ZEN);
596        rt.zenMode = tryParseZenMode(zen, -1);
597        if (rt.zenMode == -1) {
598            Slog.w(TAG, "Bad zen mode in rule xml:" + zen);
599            return null;
600        }
601        rt.conditionId = safeUri(parser, RULE_ATT_CONDITION_ID);
602        rt.component = safeComponentName(parser, RULE_ATT_COMPONENT);
603        rt.creationTime = safeLong(parser, RULE_ATT_CREATION_TIME, 0);
604        rt.enabler = parser.getAttributeValue(null, RULE_ATT_ENABLER);
605        rt.condition = readConditionXml(parser);
606
607        // all default rules and user created rules updated to zenMode important interruptions
608        if (rt.zenMode != Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS
609                && Condition.isValidId(rt.conditionId, SYSTEM_AUTHORITY)) {
610            Slog.i(TAG, "Updating zenMode of automatic rule " + rt.name);
611            rt.zenMode = Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
612        }
613        return rt;
614    }
615
616    public static void writeRuleXml(ZenRule rule, XmlSerializer out) throws IOException {
617        out.attribute(null, RULE_ATT_ENABLED, Boolean.toString(rule.enabled));
618        out.attribute(null, RULE_ATT_SNOOZING, Boolean.toString(rule.snoozing));
619        if (rule.name != null) {
620            out.attribute(null, RULE_ATT_NAME, rule.name);
621        }
622        out.attribute(null, RULE_ATT_ZEN, Integer.toString(rule.zenMode));
623        if (rule.component != null) {
624            out.attribute(null, RULE_ATT_COMPONENT, rule.component.flattenToString());
625        }
626        if (rule.conditionId != null) {
627            out.attribute(null, RULE_ATT_CONDITION_ID, rule.conditionId.toString());
628        }
629        out.attribute(null, RULE_ATT_CREATION_TIME, Long.toString(rule.creationTime));
630        if (rule.enabler != null) {
631            out.attribute(null, RULE_ATT_ENABLER, rule.enabler);
632        }
633        if (rule.condition != null) {
634            writeConditionXml(rule.condition, out);
635        }
636    }
637
638    public static Condition readConditionXml(XmlPullParser parser) {
639        final Uri id = safeUri(parser, CONDITION_ATT_ID);
640        if (id == null) return null;
641        final String summary = parser.getAttributeValue(null, CONDITION_ATT_SUMMARY);
642        final String line1 = parser.getAttributeValue(null, CONDITION_ATT_LINE1);
643        final String line2 = parser.getAttributeValue(null, CONDITION_ATT_LINE2);
644        final int icon = safeInt(parser, CONDITION_ATT_ICON, -1);
645        final int state = safeInt(parser, CONDITION_ATT_STATE, -1);
646        final int flags = safeInt(parser, CONDITION_ATT_FLAGS, -1);
647        try {
648            return new Condition(id, summary, line1, line2, icon, state, flags);
649        } catch (IllegalArgumentException e) {
650            Slog.w(TAG, "Unable to read condition xml", e);
651            return null;
652        }
653    }
654
655    public static void writeConditionXml(Condition c, XmlSerializer out) throws IOException {
656        out.attribute(null, CONDITION_ATT_ID, c.id.toString());
657        out.attribute(null, CONDITION_ATT_SUMMARY, c.summary);
658        out.attribute(null, CONDITION_ATT_LINE1, c.line1);
659        out.attribute(null, CONDITION_ATT_LINE2, c.line2);
660        out.attribute(null, CONDITION_ATT_ICON, Integer.toString(c.icon));
661        out.attribute(null, CONDITION_ATT_STATE, Integer.toString(c.state));
662        out.attribute(null, CONDITION_ATT_FLAGS, Integer.toString(c.flags));
663    }
664
665    public static boolean isValidHour(int val) {
666        return val >= 0 && val < 24;
667    }
668
669    public static boolean isValidMinute(int val) {
670        return val >= 0 && val < 60;
671    }
672
673    private static boolean isValidSource(int source) {
674        return source >= SOURCE_ANYONE && source <= MAX_SOURCE;
675    }
676
677    private static Boolean unsafeBoolean(XmlPullParser parser, String att) {
678        final String val = parser.getAttributeValue(null, att);
679        if (TextUtils.isEmpty(val)) return null;
680        return Boolean.parseBoolean(val);
681    }
682
683    private static boolean safeBoolean(XmlPullParser parser, String att, boolean defValue) {
684        final String val = parser.getAttributeValue(null, att);
685        return safeBoolean(val, defValue);
686    }
687
688    private static boolean safeBoolean(String val, boolean defValue) {
689        if (TextUtils.isEmpty(val)) return defValue;
690        return Boolean.parseBoolean(val);
691    }
692
693    private static int safeInt(XmlPullParser parser, String att, int defValue) {
694        final String val = parser.getAttributeValue(null, att);
695        return tryParseInt(val, defValue);
696    }
697
698    private static ComponentName safeComponentName(XmlPullParser parser, String att) {
699        final String val = parser.getAttributeValue(null, att);
700        if (TextUtils.isEmpty(val)) return null;
701        return ComponentName.unflattenFromString(val);
702    }
703
704    private static Uri safeUri(XmlPullParser parser, String att) {
705        final String val = parser.getAttributeValue(null, att);
706        if (TextUtils.isEmpty(val)) return null;
707        return Uri.parse(val);
708    }
709
710    private static long safeLong(XmlPullParser parser, String att, long defValue) {
711        final String val = parser.getAttributeValue(null, att);
712        return tryParseLong(val, defValue);
713    }
714
715    @Override
716    public int describeContents() {
717        return 0;
718    }
719
720    public ZenModeConfig copy() {
721        final Parcel parcel = Parcel.obtain();
722        try {
723            writeToParcel(parcel, 0);
724            parcel.setDataPosition(0);
725            return new ZenModeConfig(parcel);
726        } finally {
727            parcel.recycle();
728        }
729    }
730
731    public static final Parcelable.Creator<ZenModeConfig> CREATOR
732            = new Parcelable.Creator<ZenModeConfig>() {
733        @Override
734        public ZenModeConfig createFromParcel(Parcel source) {
735            return new ZenModeConfig(source);
736        }
737
738        @Override
739        public ZenModeConfig[] newArray(int size) {
740            return new ZenModeConfig[size];
741        }
742    };
743
744    public Policy toNotificationPolicy() {
745        int priorityCategories = 0;
746        int priorityCallSenders = Policy.PRIORITY_SENDERS_CONTACTS;
747        int priorityMessageSenders = Policy.PRIORITY_SENDERS_CONTACTS;
748        if (allowCalls) {
749            priorityCategories |= Policy.PRIORITY_CATEGORY_CALLS;
750        }
751        if (allowMessages) {
752            priorityCategories |= Policy.PRIORITY_CATEGORY_MESSAGES;
753        }
754        if (allowEvents) {
755            priorityCategories |= Policy.PRIORITY_CATEGORY_EVENTS;
756        }
757        if (allowReminders) {
758            priorityCategories |= Policy.PRIORITY_CATEGORY_REMINDERS;
759        }
760        if (allowRepeatCallers) {
761            priorityCategories |= Policy.PRIORITY_CATEGORY_REPEAT_CALLERS;
762        }
763        if (allowAlarms) {
764            priorityCategories |= Policy.PRIORITY_CATEGORY_ALARMS;
765        }
766        if (allowMedia) {
767            priorityCategories |= Policy.PRIORITY_CATEGORY_MEDIA;
768        }
769        if (allowSystem) {
770            priorityCategories |= Policy.PRIORITY_CATEGORY_SYSTEM;
771        }
772        priorityCallSenders = sourceToPrioritySenders(allowCallsFrom, priorityCallSenders);
773        priorityMessageSenders = sourceToPrioritySenders(allowMessagesFrom, priorityMessageSenders);
774        return new Policy(priorityCategories, priorityCallSenders, priorityMessageSenders,
775                suppressedVisualEffects, areChannelsBypassingDnd
776                ? Policy.STATE_CHANNELS_BYPASSING_DND : 0);
777    }
778
779    /**
780     * Creates scheduleCalendar from a condition id
781     * @param conditionId
782     * @return ScheduleCalendar with info populated with conditionId
783     */
784    public static ScheduleCalendar toScheduleCalendar(Uri conditionId) {
785        final ScheduleInfo schedule = ZenModeConfig.tryParseScheduleConditionId(conditionId);
786        if (schedule == null || schedule.days == null || schedule.days.length == 0) return null;
787        final ScheduleCalendar sc = new ScheduleCalendar();
788        sc.setSchedule(schedule);
789        sc.setTimeZone(TimeZone.getDefault());
790        return sc;
791    }
792
793    private static int sourceToPrioritySenders(int source, int def) {
794        switch (source) {
795            case SOURCE_ANYONE: return Policy.PRIORITY_SENDERS_ANY;
796            case SOURCE_CONTACT: return Policy.PRIORITY_SENDERS_CONTACTS;
797            case SOURCE_STAR: return Policy.PRIORITY_SENDERS_STARRED;
798            default: return def;
799        }
800    }
801
802    private static int prioritySendersToSource(int prioritySenders, int def) {
803        switch (prioritySenders) {
804            case Policy.PRIORITY_SENDERS_CONTACTS: return SOURCE_CONTACT;
805            case Policy.PRIORITY_SENDERS_STARRED: return SOURCE_STAR;
806            case Policy.PRIORITY_SENDERS_ANY: return SOURCE_ANYONE;
807            default: return def;
808        }
809    }
810
811    public void applyNotificationPolicy(Policy policy) {
812        if (policy == null) return;
813        allowAlarms = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_ALARMS) != 0;
814        allowMedia = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_MEDIA) != 0;
815        allowSystem = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_SYSTEM) != 0;
816        allowEvents = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_EVENTS) != 0;
817        allowReminders = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_REMINDERS) != 0;
818        allowCalls = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_CALLS) != 0;
819        allowMessages = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_MESSAGES) != 0;
820        allowRepeatCallers = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_REPEAT_CALLERS)
821                != 0;
822        allowCallsFrom = prioritySendersToSource(policy.priorityCallSenders, allowCallsFrom);
823        allowMessagesFrom = prioritySendersToSource(policy.priorityMessageSenders,
824                allowMessagesFrom);
825        if (policy.suppressedVisualEffects != Policy.SUPPRESSED_EFFECTS_UNSET) {
826            suppressedVisualEffects = policy.suppressedVisualEffects;
827        }
828        if (policy.state != Policy.STATE_UNSET) {
829            areChannelsBypassingDnd = (policy.state & Policy.STATE_CHANNELS_BYPASSING_DND) != 0;
830        }
831    }
832
833    public static Condition toTimeCondition(Context context, int minutesFromNow, int userHandle) {
834        return toTimeCondition(context, minutesFromNow, userHandle, false /*shortVersion*/);
835    }
836
837    public static Condition toTimeCondition(Context context, int minutesFromNow, int userHandle,
838            boolean shortVersion) {
839        final long now = System.currentTimeMillis();
840        final long millis = minutesFromNow == 0 ? ZERO_VALUE_MS : minutesFromNow * MINUTES_MS;
841        return toTimeCondition(context, now + millis, minutesFromNow, userHandle, shortVersion);
842    }
843
844    public static Condition toTimeCondition(Context context, long time, int minutes,
845            int userHandle, boolean shortVersion) {
846        final int num;
847        String summary, line1, line2;
848        final CharSequence formattedTime =
849                getFormattedTime(context, time, isToday(time), userHandle);
850        final Resources res = context.getResources();
851        if (minutes < 60) {
852            // display as minutes
853            num = minutes;
854            int summaryResId = shortVersion ? R.plurals.zen_mode_duration_minutes_summary_short
855                    : R.plurals.zen_mode_duration_minutes_summary;
856            summary = res.getQuantityString(summaryResId, num, num, formattedTime);
857            int line1ResId = shortVersion ? R.plurals.zen_mode_duration_minutes_short
858                    : R.plurals.zen_mode_duration_minutes;
859            line1 = res.getQuantityString(line1ResId, num, num, formattedTime);
860            line2 = res.getString(R.string.zen_mode_until, formattedTime);
861        } else if (minutes < DAY_MINUTES) {
862            // display as hours
863            num =  Math.round(minutes / 60f);
864            int summaryResId = shortVersion ? R.plurals.zen_mode_duration_hours_summary_short
865                    : R.plurals.zen_mode_duration_hours_summary;
866            summary = res.getQuantityString(summaryResId, num, num, formattedTime);
867            int line1ResId = shortVersion ? R.plurals.zen_mode_duration_hours_short
868                    : R.plurals.zen_mode_duration_hours;
869            line1 = res.getQuantityString(line1ResId, num, num, formattedTime);
870            line2 = res.getString(R.string.zen_mode_until, formattedTime);
871        } else {
872            // display as day/time
873            summary = line1 = line2 = res.getString(R.string.zen_mode_until, formattedTime);
874        }
875        final Uri id = toCountdownConditionId(time, false);
876        return new Condition(id, summary, line1, line2, 0, Condition.STATE_TRUE,
877                Condition.FLAG_RELEVANT_NOW);
878    }
879
880    /**
881     * Converts countdown to alarm parameters into a condition with user facing summary
882     */
883    public static Condition toNextAlarmCondition(Context context, long alarm,
884            int userHandle) {
885        boolean isSameDay = isToday(alarm);
886        final CharSequence formattedTime = getFormattedTime(context, alarm, isSameDay, userHandle);
887        final Resources res = context.getResources();
888        final String line1 = res.getString(R.string.zen_mode_until, formattedTime);
889        final Uri id = toCountdownConditionId(alarm, true);
890        return new Condition(id, "", line1, "", 0, Condition.STATE_TRUE,
891                Condition.FLAG_RELEVANT_NOW);
892    }
893
894    /**
895     * Creates readable time from time in milliseconds
896     */
897    public static CharSequence getFormattedTime(Context context, long time, boolean isSameDay,
898            int userHandle) {
899        String skeleton = (!isSameDay ? "EEE " : "")
900                + (DateFormat.is24HourFormat(context, userHandle) ? "Hm" : "hma");
901        final String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton);
902        return DateFormat.format(pattern, time);
903    }
904
905    /**
906     * Determines whether a time in milliseconds is today or not
907     */
908    public static boolean isToday(long time) {
909        GregorianCalendar now = new GregorianCalendar();
910        GregorianCalendar endTime = new GregorianCalendar();
911        endTime.setTimeInMillis(time);
912        if (now.get(Calendar.YEAR) == endTime.get(Calendar.YEAR)
913                && now.get(Calendar.MONTH) == endTime.get(Calendar.MONTH)
914                && now.get(Calendar.DATE) == endTime.get(Calendar.DATE)) {
915            return true;
916        }
917        return false;
918    }
919
920    // ==== Built-in system conditions ====
921
922    public static final String SYSTEM_AUTHORITY = "android";
923
924    // ==== Built-in system condition: countdown ====
925
926    public static final String COUNTDOWN_PATH = "countdown";
927
928    public static final String IS_ALARM_PATH = "alarm";
929
930    /**
931     * Converts countdown condition parameters into a condition id.
932     */
933    public static Uri toCountdownConditionId(long time, boolean alarm) {
934        return new Uri.Builder().scheme(Condition.SCHEME)
935                .authority(SYSTEM_AUTHORITY)
936                .appendPath(COUNTDOWN_PATH)
937                .appendPath(Long.toString(time))
938                .appendPath(IS_ALARM_PATH)
939                .appendPath(Boolean.toString(alarm))
940                .build();
941    }
942
943    public static long tryParseCountdownConditionId(Uri conditionId) {
944        if (!Condition.isValidId(conditionId, SYSTEM_AUTHORITY)) return 0;
945        if (conditionId.getPathSegments().size() < 2
946                || !COUNTDOWN_PATH.equals(conditionId.getPathSegments().get(0))) return 0;
947        try {
948            return Long.parseLong(conditionId.getPathSegments().get(1));
949        } catch (RuntimeException e) {
950            Slog.w(TAG, "Error parsing countdown condition: " + conditionId, e);
951            return 0;
952        }
953    }
954
955    /**
956     * Returns whether this condition is a countdown condition.
957     */
958    public static boolean isValidCountdownConditionId(Uri conditionId) {
959        return tryParseCountdownConditionId(conditionId) != 0;
960    }
961
962    /**
963     * Returns whether this condition is a countdown to an alarm.
964     */
965    public static boolean isValidCountdownToAlarmConditionId(Uri conditionId) {
966        if (tryParseCountdownConditionId(conditionId) != 0) {
967            if (conditionId.getPathSegments().size() < 4
968                    || !IS_ALARM_PATH.equals(conditionId.getPathSegments().get(2))) {
969                return false;
970            }
971            try {
972                return Boolean.parseBoolean(conditionId.getPathSegments().get(3));
973            } catch (RuntimeException e) {
974                Slog.w(TAG, "Error parsing countdown alarm condition: " + conditionId, e);
975                return false;
976            }
977        }
978        return false;
979    }
980
981    // ==== Built-in system condition: schedule ====
982
983    public static final String SCHEDULE_PATH = "schedule";
984
985    public static Uri toScheduleConditionId(ScheduleInfo schedule) {
986        return new Uri.Builder().scheme(Condition.SCHEME)
987                .authority(SYSTEM_AUTHORITY)
988                .appendPath(SCHEDULE_PATH)
989                .appendQueryParameter("days", toDayList(schedule.days))
990                .appendQueryParameter("start", schedule.startHour + "." + schedule.startMinute)
991                .appendQueryParameter("end", schedule.endHour + "." + schedule.endMinute)
992                .appendQueryParameter("exitAtAlarm", String.valueOf(schedule.exitAtAlarm))
993                .build();
994    }
995
996    public static boolean isValidScheduleConditionId(Uri conditionId) {
997        ScheduleInfo info;
998        try {
999            info = tryParseScheduleConditionId(conditionId);
1000        } catch (NullPointerException | ArrayIndexOutOfBoundsException e) {
1001            return false;
1002        }
1003
1004        if (info == null || info.days == null || info.days.length == 0) {
1005            return false;
1006        }
1007        return true;
1008    }
1009
1010    public static ScheduleInfo tryParseScheduleConditionId(Uri conditionId) {
1011        final boolean isSchedule =  conditionId != null
1012                && conditionId.getScheme().equals(Condition.SCHEME)
1013                && conditionId.getAuthority().equals(ZenModeConfig.SYSTEM_AUTHORITY)
1014                && conditionId.getPathSegments().size() == 1
1015                && conditionId.getPathSegments().get(0).equals(ZenModeConfig.SCHEDULE_PATH);
1016        if (!isSchedule) return null;
1017        final int[] start = tryParseHourAndMinute(conditionId.getQueryParameter("start"));
1018        final int[] end = tryParseHourAndMinute(conditionId.getQueryParameter("end"));
1019        if (start == null || end == null) return null;
1020        final ScheduleInfo rt = new ScheduleInfo();
1021        rt.days = tryParseDayList(conditionId.getQueryParameter("days"), "\\.");
1022        rt.startHour = start[0];
1023        rt.startMinute = start[1];
1024        rt.endHour = end[0];
1025        rt.endMinute = end[1];
1026        rt.exitAtAlarm = safeBoolean(conditionId.getQueryParameter("exitAtAlarm"), false);
1027        return rt;
1028    }
1029
1030    public static ComponentName getScheduleConditionProvider() {
1031        return new ComponentName(SYSTEM_AUTHORITY, "ScheduleConditionProvider");
1032    }
1033
1034    public static class ScheduleInfo {
1035        public int[] days;
1036        public int startHour;
1037        public int startMinute;
1038        public int endHour;
1039        public int endMinute;
1040        public boolean exitAtAlarm;
1041        public long nextAlarm;
1042
1043        @Override
1044        public int hashCode() {
1045            return 0;
1046        }
1047
1048        @Override
1049        public boolean equals(Object o) {
1050            if (!(o instanceof ScheduleInfo)) return false;
1051            final ScheduleInfo other = (ScheduleInfo) o;
1052            return toDayList(days).equals(toDayList(other.days))
1053                    && startHour == other.startHour
1054                    && startMinute == other.startMinute
1055                    && endHour == other.endHour
1056                    && endMinute == other.endMinute
1057                    && exitAtAlarm == other.exitAtAlarm;
1058        }
1059
1060        public ScheduleInfo copy() {
1061            final ScheduleInfo rt = new ScheduleInfo();
1062            if (days != null) {
1063                rt.days = new int[days.length];
1064                System.arraycopy(days, 0, rt.days, 0, days.length);
1065            }
1066            rt.startHour = startHour;
1067            rt.startMinute = startMinute;
1068            rt.endHour = endHour;
1069            rt.endMinute = endMinute;
1070            rt.exitAtAlarm = exitAtAlarm;
1071            rt.nextAlarm = nextAlarm;
1072            return rt;
1073        }
1074
1075        @Override
1076        public String toString() {
1077            return "ScheduleInfo{" +
1078                    "days=" + Arrays.toString(days) +
1079                    ", startHour=" + startHour +
1080                    ", startMinute=" + startMinute +
1081                    ", endHour=" + endHour +
1082                    ", endMinute=" + endMinute +
1083                    ", exitAtAlarm=" + exitAtAlarm +
1084                    ", nextAlarm=" + ts(nextAlarm) +
1085                    '}';
1086        }
1087
1088        protected static String ts(long time) {
1089            return new Date(time) + " (" + time + ")";
1090        }
1091    }
1092
1093    // ==== Built-in system condition: event ====
1094
1095    public static final String EVENT_PATH = "event";
1096
1097    public static Uri toEventConditionId(EventInfo event) {
1098        return new Uri.Builder().scheme(Condition.SCHEME)
1099                .authority(SYSTEM_AUTHORITY)
1100                .appendPath(EVENT_PATH)
1101                .appendQueryParameter("userId", Long.toString(event.userId))
1102                .appendQueryParameter("calendar", event.calendar != null ? event.calendar : "")
1103                .appendQueryParameter("reply", Integer.toString(event.reply))
1104                .build();
1105    }
1106
1107    public static boolean isValidEventConditionId(Uri conditionId) {
1108        return tryParseEventConditionId(conditionId) != null;
1109    }
1110
1111    public static EventInfo tryParseEventConditionId(Uri conditionId) {
1112        final boolean isEvent = conditionId != null
1113                && conditionId.getScheme().equals(Condition.SCHEME)
1114                && conditionId.getAuthority().equals(ZenModeConfig.SYSTEM_AUTHORITY)
1115                && conditionId.getPathSegments().size() == 1
1116                && conditionId.getPathSegments().get(0).equals(EVENT_PATH);
1117        if (!isEvent) return null;
1118        final EventInfo rt = new EventInfo();
1119        rt.userId = tryParseInt(conditionId.getQueryParameter("userId"), UserHandle.USER_NULL);
1120        rt.calendar = conditionId.getQueryParameter("calendar");
1121        if (TextUtils.isEmpty(rt.calendar) || tryParseLong(rt.calendar, -1L) != -1L) {
1122            rt.calendar = null;
1123        }
1124        rt.reply = tryParseInt(conditionId.getQueryParameter("reply"), 0);
1125        return rt;
1126    }
1127
1128    public static ComponentName getEventConditionProvider() {
1129        return new ComponentName(SYSTEM_AUTHORITY, "EventConditionProvider");
1130    }
1131
1132    public static class EventInfo {
1133        public static final int REPLY_ANY_EXCEPT_NO = 0;
1134        public static final int REPLY_YES_OR_MAYBE = 1;
1135        public static final int REPLY_YES = 2;
1136
1137        public int userId = UserHandle.USER_NULL;  // USER_NULL = unspecified - use current user
1138        public String calendar;  // CalendarContract.Calendars.OWNER_ACCOUNT, or null for any
1139        public int reply;
1140
1141        @Override
1142        public int hashCode() {
1143            return 0;
1144        }
1145
1146        @Override
1147        public boolean equals(Object o) {
1148            if (!(o instanceof EventInfo)) return false;
1149            final EventInfo other = (EventInfo) o;
1150            return userId == other.userId
1151                    && Objects.equals(calendar, other.calendar)
1152                    && reply == other.reply;
1153        }
1154
1155        public EventInfo copy() {
1156            final EventInfo rt = new EventInfo();
1157            rt.userId = userId;
1158            rt.calendar = calendar;
1159            rt.reply = reply;
1160            return rt;
1161        }
1162
1163        public static int resolveUserId(int userId) {
1164            return userId == UserHandle.USER_NULL ? ActivityManager.getCurrentUser() : userId;
1165        }
1166    }
1167
1168    // ==== End built-in system conditions ====
1169
1170    private static int[] tryParseHourAndMinute(String value) {
1171        if (TextUtils.isEmpty(value)) return null;
1172        final int i = value.indexOf('.');
1173        if (i < 1 || i >= value.length() - 1) return null;
1174        final int hour = tryParseInt(value.substring(0, i), -1);
1175        final int minute = tryParseInt(value.substring(i + 1), -1);
1176        return isValidHour(hour) && isValidMinute(minute) ? new int[] { hour, minute } : null;
1177    }
1178
1179    private static int tryParseZenMode(String value, int defValue) {
1180        final int rt = tryParseInt(value, defValue);
1181        return Global.isValidZenMode(rt) ? rt : defValue;
1182    }
1183
1184    public static String newRuleId() {
1185        return UUID.randomUUID().toString().replace("-", "");
1186    }
1187
1188    /**
1189     * Gets the name of the app associated with owner
1190     */
1191    public static String getOwnerCaption(Context context, String owner) {
1192        final PackageManager pm = context.getPackageManager();
1193        try {
1194            final ApplicationInfo info = pm.getApplicationInfo(owner, 0);
1195            if (info != null) {
1196                final CharSequence seq = info.loadLabel(pm);
1197                if (seq != null) {
1198                    final String str = seq.toString().trim();
1199                    if (str.length() > 0) {
1200                        return str;
1201                    }
1202                }
1203            }
1204        } catch (Throwable e) {
1205            Slog.w(TAG, "Error loading owner caption", e);
1206        }
1207        return "";
1208    }
1209
1210    public static String getConditionSummary(Context context, ZenModeConfig config,
1211            int userHandle, boolean shortVersion) {
1212        return getConditionLine(context, config, userHandle, false /*useLine1*/, shortVersion);
1213    }
1214
1215    private static String getConditionLine(Context context, ZenModeConfig config,
1216            int userHandle, boolean useLine1, boolean shortVersion) {
1217        if (config == null) return "";
1218        String summary = "";
1219        if (config.manualRule != null) {
1220            final Uri id = config.manualRule.conditionId;
1221            if (config.manualRule.enabler != null) {
1222                summary = getOwnerCaption(context, config.manualRule.enabler);
1223            } else {
1224                if (id == null) {
1225                    summary = context.getString(com.android.internal.R.string.zen_mode_forever);
1226                } else {
1227                    final long time = tryParseCountdownConditionId(id);
1228                    Condition c = config.manualRule.condition;
1229                    if (time > 0) {
1230                        final long now = System.currentTimeMillis();
1231                        final long span = time - now;
1232                        c = toTimeCondition(context, time, Math.round(span / (float) MINUTES_MS),
1233                                userHandle, shortVersion);
1234                    }
1235                    final String rt = c == null ? "" : useLine1 ? c.line1 : c.summary;
1236                    summary = TextUtils.isEmpty(rt) ? "" : rt;
1237                }
1238            }
1239        }
1240        for (ZenRule automaticRule : config.automaticRules.values()) {
1241            if (automaticRule.isAutomaticActive()) {
1242                if (summary.isEmpty()) {
1243                    summary = automaticRule.name;
1244                } else {
1245                    summary = context.getResources()
1246                            .getString(R.string.zen_mode_rule_name_combination, summary,
1247                                    automaticRule.name);
1248                }
1249
1250            }
1251        }
1252        return summary;
1253    }
1254
1255    public static class ZenRule implements Parcelable {
1256        public boolean enabled;
1257        public boolean snoozing;         // user manually disabled this instance
1258        public String name;              // required for automatic
1259        public int zenMode;
1260        public Uri conditionId;          // required for automatic
1261        public Condition condition;      // optional
1262        public ComponentName component;  // optional
1263        public String id;                // required for automatic (unique)
1264        public long creationTime;        // required for automatic
1265        public String enabler;          // package name, only used for manual rules.
1266
1267        public ZenRule() { }
1268
1269        public ZenRule(Parcel source) {
1270            enabled = source.readInt() == 1;
1271            snoozing = source.readInt() == 1;
1272            if (source.readInt() == 1) {
1273                name = source.readString();
1274            }
1275            zenMode = source.readInt();
1276            conditionId = source.readParcelable(null);
1277            condition = source.readParcelable(null);
1278            component = source.readParcelable(null);
1279            if (source.readInt() == 1) {
1280                id = source.readString();
1281            }
1282            creationTime = source.readLong();
1283            if (source.readInt() == 1) {
1284                enabler = source.readString();
1285            }
1286        }
1287
1288        @Override
1289        public int describeContents() {
1290            return 0;
1291        }
1292
1293        @Override
1294        public void writeToParcel(Parcel dest, int flags) {
1295            dest.writeInt(enabled ? 1 : 0);
1296            dest.writeInt(snoozing ? 1 : 0);
1297            if (name != null) {
1298                dest.writeInt(1);
1299                dest.writeString(name);
1300            } else {
1301                dest.writeInt(0);
1302            }
1303            dest.writeInt(zenMode);
1304            dest.writeParcelable(conditionId, 0);
1305            dest.writeParcelable(condition, 0);
1306            dest.writeParcelable(component, 0);
1307            if (id != null) {
1308                dest.writeInt(1);
1309                dest.writeString(id);
1310            } else {
1311                dest.writeInt(0);
1312            }
1313            dest.writeLong(creationTime);
1314            if (enabler != null) {
1315                dest.writeInt(1);
1316                dest.writeString(enabler);
1317            } else {
1318                dest.writeInt(0);
1319            }
1320        }
1321
1322        @Override
1323        public String toString() {
1324            return new StringBuilder(ZenRule.class.getSimpleName()).append('[')
1325                    .append("enabled=").append(enabled)
1326                    .append(",snoozing=").append(snoozing)
1327                    .append(",name=").append(name)
1328                    .append(",zenMode=").append(Global.zenModeToString(zenMode))
1329                    .append(",conditionId=").append(conditionId)
1330                    .append(",condition=").append(condition)
1331                    .append(",component=").append(component)
1332                    .append(",id=").append(id)
1333                    .append(",creationTime=").append(creationTime)
1334                    .append(",enabler=").append(enabler)
1335                    .append(']').toString();
1336        }
1337
1338        /** @hide */
1339        public void writeToProto(ProtoOutputStream proto, long fieldId) {
1340            final long token = proto.start(fieldId);
1341
1342            proto.write(ZenRuleProto.ID, id);
1343            proto.write(ZenRuleProto.NAME, name);
1344            proto.write(ZenRuleProto.CREATION_TIME_MS, creationTime);
1345            proto.write(ZenRuleProto.ENABLED, enabled);
1346            proto.write(ZenRuleProto.ENABLER, enabler);
1347            proto.write(ZenRuleProto.IS_SNOOZING, snoozing);
1348            proto.write(ZenRuleProto.ZEN_MODE, zenMode);
1349            if (conditionId != null) {
1350                proto.write(ZenRuleProto.CONDITION_ID, conditionId.toString());
1351            }
1352            if (condition != null) {
1353                condition.writeToProto(proto, ZenRuleProto.CONDITION);
1354            }
1355            if (component != null) {
1356                component.writeToProto(proto, ZenRuleProto.COMPONENT);
1357            }
1358
1359            proto.end(token);
1360        }
1361
1362        private static void appendDiff(Diff d, String item, ZenRule from, ZenRule to) {
1363            if (d == null) return;
1364            if (from == null) {
1365                if (to != null) {
1366                    d.addLine(item, "insert");
1367                }
1368                return;
1369            }
1370            from.appendDiff(d, item, to);
1371        }
1372
1373        private void appendDiff(Diff d, String item, ZenRule to) {
1374            if (to == null) {
1375                d.addLine(item, "delete");
1376                return;
1377            }
1378            if (enabled != to.enabled) {
1379                d.addLine(item, "enabled", enabled, to.enabled);
1380            }
1381            if (snoozing != to.snoozing) {
1382                d.addLine(item, "snoozing", snoozing, to.snoozing);
1383            }
1384            if (!Objects.equals(name, to.name)) {
1385                d.addLine(item, "name", name, to.name);
1386            }
1387            if (zenMode != to.zenMode) {
1388                d.addLine(item, "zenMode", zenMode, to.zenMode);
1389            }
1390            if (!Objects.equals(conditionId, to.conditionId)) {
1391                d.addLine(item, "conditionId", conditionId, to.conditionId);
1392            }
1393            if (!Objects.equals(condition, to.condition)) {
1394                d.addLine(item, "condition", condition, to.condition);
1395            }
1396            if (!Objects.equals(component, to.component)) {
1397                d.addLine(item, "component", component, to.component);
1398            }
1399            if (!Objects.equals(id, to.id)) {
1400                d.addLine(item, "id", id, to.id);
1401            }
1402            if (creationTime != to.creationTime) {
1403                d.addLine(item, "creationTime", creationTime, to.creationTime);
1404            }
1405            if (enabler != to.enabler) {
1406                d.addLine(item, "enabler", enabler, to.enabler);
1407            }
1408        }
1409
1410        @Override
1411        public boolean equals(Object o) {
1412            if (!(o instanceof ZenRule)) return false;
1413            if (o == this) return true;
1414            final ZenRule other = (ZenRule) o;
1415            return other.enabled == enabled
1416                    && other.snoozing == snoozing
1417                    && Objects.equals(other.name, name)
1418                    && other.zenMode == zenMode
1419                    && Objects.equals(other.conditionId, conditionId)
1420                    && Objects.equals(other.condition, condition)
1421                    && Objects.equals(other.component, component)
1422                    && Objects.equals(other.id, id)
1423                    && other.creationTime == creationTime
1424                    && Objects.equals(other.enabler, enabler);
1425        }
1426
1427        @Override
1428        public int hashCode() {
1429            return Objects.hash(enabled, snoozing, name, zenMode, conditionId, condition,
1430                    component, id, creationTime, enabler);
1431        }
1432
1433        public boolean isAutomaticActive() {
1434            return enabled && !snoozing && component != null && isTrueOrUnknown();
1435        }
1436
1437        public boolean isTrueOrUnknown() {
1438            return condition != null && (condition.state == Condition.STATE_TRUE
1439                    || condition.state == Condition.STATE_UNKNOWN);
1440        }
1441
1442        public static final Parcelable.Creator<ZenRule> CREATOR
1443                = new Parcelable.Creator<ZenRule>() {
1444            @Override
1445            public ZenRule createFromParcel(Parcel source) {
1446                return new ZenRule(source);
1447            }
1448            @Override
1449            public ZenRule[] newArray(int size) {
1450                return new ZenRule[size];
1451            }
1452        };
1453    }
1454
1455    public static class Diff {
1456        private final ArrayList<String> lines = new ArrayList<>();
1457
1458        @Override
1459        public String toString() {
1460            final StringBuilder sb = new StringBuilder("Diff[");
1461            final int N = lines.size();
1462            for (int i = 0; i < N; i++) {
1463                if (i > 0) {
1464                    sb.append(',');
1465                }
1466                sb.append(lines.get(i));
1467            }
1468            return sb.append(']').toString();
1469        }
1470
1471        private Diff addLine(String item, String action) {
1472            lines.add(item + ":" + action);
1473            return this;
1474        }
1475
1476        public Diff addLine(String item, String subitem, Object from, Object to) {
1477            return addLine(item + "." + subitem, from, to);
1478        }
1479
1480        public Diff addLine(String item, Object from, Object to) {
1481            return addLine(item, from + "->" + to);
1482        }
1483    }
1484
1485    /**
1486     * Determines whether dnd behavior should mute all notification/ringer sounds
1487     * (sounds associated with ringer volume discluding system)
1488     */
1489    public static boolean areAllPriorityOnlyNotificationZenSoundsMuted(NotificationManager.Policy
1490            policy) {
1491        boolean allowReminders = (policy.priorityCategories
1492                & NotificationManager.Policy.PRIORITY_CATEGORY_REMINDERS) != 0;
1493        boolean allowCalls = (policy.priorityCategories
1494                & NotificationManager.Policy.PRIORITY_CATEGORY_CALLS) != 0;
1495        boolean allowMessages = (policy.priorityCategories
1496                & NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES) != 0;
1497        boolean allowEvents = (policy.priorityCategories
1498                & NotificationManager.Policy.PRIORITY_CATEGORY_EVENTS) != 0;
1499        boolean allowRepeatCallers = (policy.priorityCategories
1500                & NotificationManager.Policy.PRIORITY_CATEGORY_REPEAT_CALLERS) != 0;
1501        boolean areChannelsBypassingDnd = (policy.state & Policy.STATE_CHANNELS_BYPASSING_DND) != 0;
1502        return !allowReminders && !allowCalls && !allowMessages && !allowEvents
1503                && !allowRepeatCallers && !areChannelsBypassingDnd;
1504    }
1505
1506    /**
1507     * Determines if DND is currently overriding the ringer
1508     */
1509    public static boolean isZenOverridingRinger(int zen, ZenModeConfig zenConfig) {
1510        return zen == Global.ZEN_MODE_NO_INTERRUPTIONS
1511                || zen == Global.ZEN_MODE_ALARMS
1512                || (zen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS
1513                && ZenModeConfig.areAllPriorityOnlyNotificationZenSoundsMuted(zenConfig));
1514    }
1515
1516    /**
1517     * Determines whether dnd behavior should mute all sounds controlled by ringer
1518     */
1519    public static boolean areAllPriorityOnlyNotificationZenSoundsMuted(ZenModeConfig config) {
1520        return !config.allowReminders && !config.allowCalls && !config.allowMessages
1521                && !config.allowEvents && !config.allowRepeatCallers
1522                && !config.areChannelsBypassingDnd;
1523    }
1524
1525    /**
1526     * Determines whether all dnd mutes all sounds
1527     */
1528    public static boolean areAllZenBehaviorSoundsMuted(ZenModeConfig config) {
1529        return !config.allowAlarms  && !config.allowMedia && !config.allowSystem
1530                && areAllPriorityOnlyNotificationZenSoundsMuted(config);
1531    }
1532
1533    /**
1534     * Returns a description of the current do not disturb settings from config.
1535     * - If turned on manually and end time is known, returns end time.
1536     * - If turned on manually and end time is on forever until turned off, return null if
1537     * describeForeverCondition is false, else return String describing indefinite behavior
1538     * - If turned on by an automatic rule, returns the automatic rule name.
1539     * - If on due to an app, returns the app name.
1540     * - If there's a combination of rules/apps that trigger, then shows the one that will
1541     *  last the longest if applicable.
1542     * @return null if DND is off or describeForeverCondition is false and
1543     * DND is on forever (until turned off)
1544     */
1545    public static String getDescription(Context context, boolean zenOn, ZenModeConfig config,
1546            boolean describeForeverCondition) {
1547        if (!zenOn || config == null) {
1548            return null;
1549        }
1550
1551        String secondaryText = "";
1552        long latestEndTime = -1;
1553
1554        // DND turned on by manual rule
1555        if (config.manualRule != null) {
1556            final Uri id = config.manualRule.conditionId;
1557            if (config.manualRule.enabler != null) {
1558                // app triggered manual rule
1559                String appName = getOwnerCaption(context, config.manualRule.enabler);
1560                if (!appName.isEmpty()) {
1561                    secondaryText = appName;
1562                }
1563            } else {
1564                if (id == null) {
1565                    // Do not disturb manually triggered to remain on forever until turned off
1566                    if (describeForeverCondition) {
1567                        return context.getString(R.string.zen_mode_forever);
1568                    } else {
1569                        return null;
1570                    }
1571                } else {
1572                    latestEndTime = tryParseCountdownConditionId(id);
1573                    if (latestEndTime > 0) {
1574                        final CharSequence formattedTime = getFormattedTime(context,
1575                                latestEndTime, isToday(latestEndTime),
1576                                context.getUserId());
1577                        secondaryText = context.getString(R.string.zen_mode_until, formattedTime);
1578                    }
1579                }
1580            }
1581        }
1582
1583        // DND turned on by an automatic rule
1584        for (ZenRule automaticRule : config.automaticRules.values()) {
1585            if (automaticRule.isAutomaticActive()) {
1586                if (isValidEventConditionId(automaticRule.conditionId)
1587                        || isValidScheduleConditionId(automaticRule.conditionId)) {
1588                    // set text if automatic rule end time is the latest active rule end time
1589                    long endTime = parseAutomaticRuleEndTime(context, automaticRule.conditionId);
1590                    if (endTime > latestEndTime) {
1591                        latestEndTime = endTime;
1592                        secondaryText = automaticRule.name;
1593                    }
1594                } else {
1595                    // set text if 3rd party rule
1596                    return automaticRule.name;
1597                }
1598            }
1599        }
1600
1601        return !secondaryText.equals("") ? secondaryText : null;
1602    }
1603
1604    private static long parseAutomaticRuleEndTime(Context context, Uri id) {
1605        if (isValidEventConditionId(id)) {
1606            // cannot look up end times for events
1607            return Long.MAX_VALUE;
1608        }
1609
1610        if (isValidScheduleConditionId(id)) {
1611            ScheduleCalendar schedule = toScheduleCalendar(id);
1612            long endTimeMs = schedule.getNextChangeTime(System.currentTimeMillis());
1613
1614            // check if automatic rule will end on next alarm
1615            if (schedule.exitAtAlarm()) {
1616                long nextAlarm = getNextAlarm(context);
1617                schedule.maybeSetNextAlarm(System.currentTimeMillis(), nextAlarm);
1618                if (schedule.shouldExitForAlarm(endTimeMs)) {
1619                    return nextAlarm;
1620                }
1621            }
1622
1623            return endTimeMs;
1624        }
1625
1626        return -1;
1627    }
1628
1629    private static long getNextAlarm(Context context) {
1630        final AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
1631        final AlarmManager.AlarmClockInfo info = alarms.getNextAlarmClock(context.getUserId());
1632        return info != null ? info.getTriggerTime() : 0;
1633    }
1634}
1635