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