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