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