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