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