ZenModeConfig.java revision 0842fe87b27b9e4a7aecfec25b93dba2d39f398a
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.Locale; 45import java.util.Objects; 46import java.util.UUID; 47 48/** 49 * Persisted configuration for zen mode. 50 * 51 * @hide 52 */ 53public class ZenModeConfig implements Parcelable { 54 private static String TAG = "ZenModeConfig"; 55 56 public static final int SOURCE_ANYONE = 0; 57 public static final int SOURCE_CONTACT = 1; 58 public static final int SOURCE_STAR = 2; 59 public static final int MAX_SOURCE = SOURCE_STAR; 60 private static final int DEFAULT_SOURCE = SOURCE_CONTACT; 61 62 public static final int[] ALL_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, 63 Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY }; 64 public static final int[] WEEKNIGHT_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, 65 Calendar.WEDNESDAY, Calendar.THURSDAY }; 66 public static final int[] WEEKEND_DAYS = { Calendar.FRIDAY, Calendar.SATURDAY }; 67 68 public static final int[] MINUTE_BUCKETS = generateMinuteBuckets(); 69 private static final int SECONDS_MS = 1000; 70 private static final int MINUTES_MS = 60 * SECONDS_MS; 71 private static final int DAY_MINUTES = 24 * 60; 72 private static final int ZERO_VALUE_MS = 10 * SECONDS_MS; 73 74 private static final boolean DEFAULT_ALLOW_CALLS = true; 75 private static final boolean DEFAULT_ALLOW_MESSAGES = false; 76 private static final boolean DEFAULT_ALLOW_REMINDERS = true; 77 private static final boolean DEFAULT_ALLOW_EVENTS = true; 78 private static final boolean DEFAULT_ALLOW_REPEAT_CALLERS = false; 79 80 private static final int XML_VERSION = 2; 81 private static final String ZEN_TAG = "zen"; 82 private static final String ZEN_ATT_VERSION = "version"; 83 private static final String ZEN_ATT_USER = "user"; 84 private static final String ALLOW_TAG = "allow"; 85 private static final String ALLOW_ATT_CALLS = "calls"; 86 private static final String ALLOW_ATT_REPEAT_CALLERS = "repeatCallers"; 87 private static final String ALLOW_ATT_MESSAGES = "messages"; 88 private static final String ALLOW_ATT_FROM = "from"; 89 private static final String ALLOW_ATT_CALLS_FROM = "callsFrom"; 90 private static final String ALLOW_ATT_MESSAGES_FROM = "messagesFrom"; 91 private static final String ALLOW_ATT_REMINDERS = "reminders"; 92 private static final String ALLOW_ATT_EVENTS = "events"; 93 94 private static final String CONDITION_TAG = "condition"; 95 private static final String CONDITION_ATT_COMPONENT = "component"; 96 private static final String CONDITION_ATT_ID = "id"; 97 private static final String CONDITION_ATT_SUMMARY = "summary"; 98 private static final String CONDITION_ATT_LINE1 = "line1"; 99 private static final String CONDITION_ATT_LINE2 = "line2"; 100 private static final String CONDITION_ATT_ICON = "icon"; 101 private static final String CONDITION_ATT_STATE = "state"; 102 private static final String CONDITION_ATT_FLAGS = "flags"; 103 104 private static final String MANUAL_TAG = "manual"; 105 private static final String AUTOMATIC_TAG = "automatic"; 106 107 private static final String RULE_ATT_ID = "ruleId"; 108 private static final String RULE_ATT_ENABLED = "enabled"; 109 private static final String RULE_ATT_SNOOZING = "snoozing"; 110 private static final String RULE_ATT_NAME = "name"; 111 private static final String RULE_ATT_COMPONENT = "component"; 112 private static final String RULE_ATT_ZEN = "zen"; 113 private static final String RULE_ATT_CONDITION_ID = "conditionId"; 114 private static final String RULE_ATT_CREATION_TIME = "creationTime"; 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_SYSTEM; 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 automaticRule.id = id; 421 rt.automaticRules.put(id, automaticRule); 422 } 423 } 424 } 425 } 426 throw new IllegalStateException("Failed to reach END_DOCUMENT"); 427 } 428 429 public void writeXml(XmlSerializer out) throws IOException { 430 out.startTag(null, ZEN_TAG); 431 out.attribute(null, ZEN_ATT_VERSION, Integer.toString(XML_VERSION)); 432 out.attribute(null, ZEN_ATT_USER, Integer.toString(user)); 433 434 out.startTag(null, ALLOW_TAG); 435 out.attribute(null, ALLOW_ATT_CALLS, Boolean.toString(allowCalls)); 436 out.attribute(null, ALLOW_ATT_REPEAT_CALLERS, Boolean.toString(allowRepeatCallers)); 437 out.attribute(null, ALLOW_ATT_MESSAGES, Boolean.toString(allowMessages)); 438 out.attribute(null, ALLOW_ATT_REMINDERS, Boolean.toString(allowReminders)); 439 out.attribute(null, ALLOW_ATT_EVENTS, Boolean.toString(allowEvents)); 440 out.attribute(null, ALLOW_ATT_CALLS_FROM, Integer.toString(allowCallsFrom)); 441 out.attribute(null, ALLOW_ATT_MESSAGES_FROM, Integer.toString(allowMessagesFrom)); 442 out.endTag(null, ALLOW_TAG); 443 444 if (manualRule != null) { 445 out.startTag(null, MANUAL_TAG); 446 writeRuleXml(manualRule, out); 447 out.endTag(null, MANUAL_TAG); 448 } 449 final int N = automaticRules.size(); 450 for (int i = 0; i < N; i++) { 451 final String id = automaticRules.keyAt(i); 452 final ZenRule automaticRule = automaticRules.valueAt(i); 453 out.startTag(null, AUTOMATIC_TAG); 454 out.attribute(null, RULE_ATT_ID, id); 455 writeRuleXml(automaticRule, out); 456 out.endTag(null, AUTOMATIC_TAG); 457 } 458 out.endTag(null, ZEN_TAG); 459 } 460 461 public static ZenRule readRuleXml(XmlPullParser parser) { 462 final ZenRule rt = new ZenRule(); 463 rt.enabled = safeBoolean(parser, RULE_ATT_ENABLED, true); 464 rt.snoozing = safeBoolean(parser, RULE_ATT_SNOOZING, false); 465 rt.name = parser.getAttributeValue(null, RULE_ATT_NAME); 466 final String zen = parser.getAttributeValue(null, RULE_ATT_ZEN); 467 rt.zenMode = tryParseZenMode(zen, -1); 468 if (rt.zenMode == -1) { 469 Slog.w(TAG, "Bad zen mode in rule xml:" + zen); 470 return null; 471 } 472 rt.conditionId = safeUri(parser, RULE_ATT_CONDITION_ID); 473 rt.component = safeComponentName(parser, RULE_ATT_COMPONENT); 474 rt.creationTime = safeLong(parser, RULE_ATT_CREATION_TIME, 0); 475 rt.condition = readConditionXml(parser); 476 return rt; 477 } 478 479 public static void writeRuleXml(ZenRule rule, XmlSerializer out) throws IOException { 480 out.attribute(null, RULE_ATT_ENABLED, Boolean.toString(rule.enabled)); 481 out.attribute(null, RULE_ATT_SNOOZING, Boolean.toString(rule.snoozing)); 482 if (rule.name != null) { 483 out.attribute(null, RULE_ATT_NAME, rule.name); 484 } 485 out.attribute(null, RULE_ATT_ZEN, Integer.toString(rule.zenMode)); 486 if (rule.component != null) { 487 out.attribute(null, RULE_ATT_COMPONENT, rule.component.flattenToString()); 488 } 489 if (rule.conditionId != null) { 490 out.attribute(null, RULE_ATT_CONDITION_ID, rule.conditionId.toString()); 491 } 492 out.attribute(null, RULE_ATT_CREATION_TIME, Long.toString(rule.creationTime)); 493 if (rule.condition != null) { 494 writeConditionXml(rule.condition, out); 495 } 496 } 497 498 public static Condition readConditionXml(XmlPullParser parser) { 499 final Uri id = safeUri(parser, CONDITION_ATT_ID); 500 if (id == null) return null; 501 final String summary = parser.getAttributeValue(null, CONDITION_ATT_SUMMARY); 502 final String line1 = parser.getAttributeValue(null, CONDITION_ATT_LINE1); 503 final String line2 = parser.getAttributeValue(null, CONDITION_ATT_LINE2); 504 final int icon = safeInt(parser, CONDITION_ATT_ICON, -1); 505 final int state = safeInt(parser, CONDITION_ATT_STATE, -1); 506 final int flags = safeInt(parser, CONDITION_ATT_FLAGS, -1); 507 try { 508 return new Condition(id, summary, line1, line2, icon, state, flags); 509 } catch (IllegalArgumentException e) { 510 Slog.w(TAG, "Unable to read condition xml", e); 511 return null; 512 } 513 } 514 515 public static void writeConditionXml(Condition c, XmlSerializer out) throws IOException { 516 out.attribute(null, CONDITION_ATT_ID, c.id.toString()); 517 out.attribute(null, CONDITION_ATT_SUMMARY, c.summary); 518 out.attribute(null, CONDITION_ATT_LINE1, c.line1); 519 out.attribute(null, CONDITION_ATT_LINE2, c.line2); 520 out.attribute(null, CONDITION_ATT_ICON, Integer.toString(c.icon)); 521 out.attribute(null, CONDITION_ATT_STATE, Integer.toString(c.state)); 522 out.attribute(null, CONDITION_ATT_FLAGS, Integer.toString(c.flags)); 523 } 524 525 public static boolean isValidHour(int val) { 526 return val >= 0 && val < 24; 527 } 528 529 public static boolean isValidMinute(int val) { 530 return val >= 0 && val < 60; 531 } 532 533 private static boolean isValidSource(int source) { 534 return source >= SOURCE_ANYONE && source <= MAX_SOURCE; 535 } 536 537 private static boolean safeBoolean(XmlPullParser parser, String att, boolean defValue) { 538 final String val = parser.getAttributeValue(null, att); 539 if (TextUtils.isEmpty(val)) return defValue; 540 return Boolean.valueOf(val); 541 } 542 543 private static int safeInt(XmlPullParser parser, String att, int defValue) { 544 final String val = parser.getAttributeValue(null, att); 545 return tryParseInt(val, defValue); 546 } 547 548 private static ComponentName safeComponentName(XmlPullParser parser, String att) { 549 final String val = parser.getAttributeValue(null, att); 550 if (TextUtils.isEmpty(val)) return null; 551 return ComponentName.unflattenFromString(val); 552 } 553 554 private static Uri safeUri(XmlPullParser parser, String att) { 555 final String val = parser.getAttributeValue(null, att); 556 if (TextUtils.isEmpty(val)) return null; 557 return Uri.parse(val); 558 } 559 560 private static long safeLong(XmlPullParser parser, String att, long defValue) { 561 final String val = parser.getAttributeValue(null, att); 562 return tryParseLong(val, defValue); 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, minutes, 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 int minutes = Math.round((alarm-now) / (float) MINUTES_MS); 698 final CharSequence formattedTime = getFormattedTime(context, alarm, minutes, userHandle); 699 final Resources res = context.getResources(); 700 final String line1 = res.getString(R.string.zen_mode_alarm, formattedTime); 701 final Uri id = toCountdownConditionId(alarm); 702 return new Condition(id, "", line1, "", 0, Condition.STATE_TRUE, 703 Condition.FLAG_RELEVANT_NOW); 704 } 705 706 private static CharSequence getFormattedTime(Context context, long time, int minutes, 707 int userHandle) { 708 String skeleton = DateFormat.is24HourFormat(context, userHandle) ? "Hm" : "hma"; 709 if (minutes > DAY_MINUTES) { 710 skeleton = "EEE " + (DateFormat.is24HourFormat(context, userHandle) ? "Hm" : "hma"); 711 } 712 final String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton); 713 return DateFormat.format(pattern, time); 714 } 715 716 // ==== Built-in system conditions ==== 717 718 public static final String SYSTEM_AUTHORITY = "android"; 719 720 // ==== Built-in system condition: countdown ==== 721 722 public static final String COUNTDOWN_PATH = "countdown"; 723 724 public static Uri toCountdownConditionId(long time) { 725 return new Uri.Builder().scheme(Condition.SCHEME) 726 .authority(SYSTEM_AUTHORITY) 727 .appendPath(COUNTDOWN_PATH) 728 .appendPath(Long.toString(time)) 729 .build(); 730 } 731 732 public static long tryParseCountdownConditionId(Uri conditionId) { 733 if (!Condition.isValidId(conditionId, SYSTEM_AUTHORITY)) return 0; 734 if (conditionId.getPathSegments().size() != 2 735 || !COUNTDOWN_PATH.equals(conditionId.getPathSegments().get(0))) return 0; 736 try { 737 return Long.parseLong(conditionId.getPathSegments().get(1)); 738 } catch (RuntimeException e) { 739 Slog.w(TAG, "Error parsing countdown condition: " + conditionId, e); 740 return 0; 741 } 742 } 743 744 public static boolean isValidCountdownConditionId(Uri conditionId) { 745 return tryParseCountdownConditionId(conditionId) != 0; 746 } 747 748 // ==== Built-in system condition: schedule ==== 749 750 public static final String SCHEDULE_PATH = "schedule"; 751 752 public static Uri toScheduleConditionId(ScheduleInfo schedule) { 753 return new Uri.Builder().scheme(Condition.SCHEME) 754 .authority(SYSTEM_AUTHORITY) 755 .appendPath(SCHEDULE_PATH) 756 .appendQueryParameter("days", toDayList(schedule.days)) 757 .appendQueryParameter("start", schedule.startHour + "." + schedule.startMinute) 758 .appendQueryParameter("end", schedule.endHour + "." + schedule.endMinute) 759 .build(); 760 } 761 762 public static boolean isValidScheduleConditionId(Uri conditionId) { 763 return tryParseScheduleConditionId(conditionId) != null; 764 } 765 766 public static ScheduleInfo tryParseScheduleConditionId(Uri conditionId) { 767 final boolean isSchedule = conditionId != null 768 && conditionId.getScheme().equals(Condition.SCHEME) 769 && conditionId.getAuthority().equals(ZenModeConfig.SYSTEM_AUTHORITY) 770 && conditionId.getPathSegments().size() == 1 771 && conditionId.getPathSegments().get(0).equals(ZenModeConfig.SCHEDULE_PATH); 772 if (!isSchedule) return null; 773 final int[] start = tryParseHourAndMinute(conditionId.getQueryParameter("start")); 774 final int[] end = tryParseHourAndMinute(conditionId.getQueryParameter("end")); 775 if (start == null || end == null) return null; 776 final ScheduleInfo rt = new ScheduleInfo(); 777 rt.days = tryParseDayList(conditionId.getQueryParameter("days"), "\\."); 778 rt.startHour = start[0]; 779 rt.startMinute = start[1]; 780 rt.endHour = end[0]; 781 rt.endMinute = end[1]; 782 return rt; 783 } 784 785 public static ComponentName getScheduleConditionProvider() { 786 return new ComponentName(SYSTEM_AUTHORITY, "ScheduleConditionProvider"); 787 } 788 789 public static class ScheduleInfo { 790 public int[] days; 791 public int startHour; 792 public int startMinute; 793 public int endHour; 794 public int endMinute; 795 796 @Override 797 public int hashCode() { 798 return 0; 799 } 800 801 @Override 802 public boolean equals(Object o) { 803 if (!(o instanceof ScheduleInfo)) return false; 804 final ScheduleInfo other = (ScheduleInfo) o; 805 return toDayList(days).equals(toDayList(other.days)) 806 && startHour == other.startHour 807 && startMinute == other.startMinute 808 && endHour == other.endHour 809 && endMinute == other.endMinute; 810 } 811 812 public ScheduleInfo copy() { 813 final ScheduleInfo rt = new ScheduleInfo(); 814 if (days != null) { 815 rt.days = new int[days.length]; 816 System.arraycopy(days, 0, rt.days, 0, days.length); 817 } 818 rt.startHour = startHour; 819 rt.startMinute = startMinute; 820 rt.endHour = endHour; 821 rt.endMinute = endMinute; 822 return rt; 823 } 824 } 825 826 // ==== Built-in system condition: event ==== 827 828 public static final String EVENT_PATH = "event"; 829 830 public static Uri toEventConditionId(EventInfo event) { 831 return new Uri.Builder().scheme(Condition.SCHEME) 832 .authority(SYSTEM_AUTHORITY) 833 .appendPath(EVENT_PATH) 834 .appendQueryParameter("userId", Long.toString(event.userId)) 835 .appendQueryParameter("calendar", event.calendar != null ? event.calendar : "") 836 .appendQueryParameter("reply", Integer.toString(event.reply)) 837 .build(); 838 } 839 840 public static boolean isValidEventConditionId(Uri conditionId) { 841 return tryParseEventConditionId(conditionId) != null; 842 } 843 844 public static EventInfo tryParseEventConditionId(Uri conditionId) { 845 final boolean isEvent = conditionId != null 846 && conditionId.getScheme().equals(Condition.SCHEME) 847 && conditionId.getAuthority().equals(ZenModeConfig.SYSTEM_AUTHORITY) 848 && conditionId.getPathSegments().size() == 1 849 && conditionId.getPathSegments().get(0).equals(EVENT_PATH); 850 if (!isEvent) return null; 851 final EventInfo rt = new EventInfo(); 852 rt.userId = tryParseInt(conditionId.getQueryParameter("userId"), UserHandle.USER_NULL); 853 rt.calendar = conditionId.getQueryParameter("calendar"); 854 if (TextUtils.isEmpty(rt.calendar) || tryParseLong(rt.calendar, -1L) != -1L) { 855 rt.calendar = null; 856 } 857 rt.reply = tryParseInt(conditionId.getQueryParameter("reply"), 0); 858 return rt; 859 } 860 861 public static ComponentName getEventConditionProvider() { 862 return new ComponentName(SYSTEM_AUTHORITY, "EventConditionProvider"); 863 } 864 865 public static class EventInfo { 866 public static final int REPLY_ANY_EXCEPT_NO = 0; 867 public static final int REPLY_YES_OR_MAYBE = 1; 868 public static final int REPLY_YES = 2; 869 870 public int userId = UserHandle.USER_NULL; // USER_NULL = unspecified - use current user 871 public String calendar; // CalendarContract.Calendars.OWNER_ACCOUNT, or null for any 872 public int reply; 873 874 @Override 875 public int hashCode() { 876 return 0; 877 } 878 879 @Override 880 public boolean equals(Object o) { 881 if (!(o instanceof EventInfo)) return false; 882 final EventInfo other = (EventInfo) o; 883 return userId == other.userId 884 && Objects.equals(calendar, other.calendar) 885 && reply == other.reply; 886 } 887 888 public EventInfo copy() { 889 final EventInfo rt = new EventInfo(); 890 rt.userId = userId; 891 rt.calendar = calendar; 892 rt.reply = reply; 893 return rt; 894 } 895 896 public static int resolveUserId(int userId) { 897 return userId == UserHandle.USER_NULL ? ActivityManager.getCurrentUser() : userId; 898 } 899 } 900 901 // ==== End built-in system conditions ==== 902 903 private static int[] tryParseHourAndMinute(String value) { 904 if (TextUtils.isEmpty(value)) return null; 905 final int i = value.indexOf('.'); 906 if (i < 1 || i >= value.length() - 1) return null; 907 final int hour = tryParseInt(value.substring(0, i), -1); 908 final int minute = tryParseInt(value.substring(i + 1), -1); 909 return isValidHour(hour) && isValidMinute(minute) ? new int[] { hour, minute } : null; 910 } 911 912 private static int tryParseZenMode(String value, int defValue) { 913 final int rt = tryParseInt(value, defValue); 914 return Global.isValidZenMode(rt) ? rt : defValue; 915 } 916 917 public static String newRuleId() { 918 return UUID.randomUUID().toString().replace("-", ""); 919 } 920 921 public static String getConditionSummary(Context context, ZenModeConfig config, 922 int userHandle, boolean shortVersion) { 923 return getConditionLine(context, config, userHandle, false /*useLine1*/, shortVersion); 924 } 925 926 private static String getConditionLine(Context context, ZenModeConfig config, 927 int userHandle, boolean useLine1, boolean shortVersion) { 928 if (config == null) return ""; 929 if (config.manualRule != null) { 930 final Uri id = config.manualRule.conditionId; 931 if (id == null) { 932 return context.getString(com.android.internal.R.string.zen_mode_forever); 933 } 934 final long time = tryParseCountdownConditionId(id); 935 Condition c = config.manualRule.condition; 936 if (time > 0) { 937 final long now = System.currentTimeMillis(); 938 final long span = time - now; 939 c = toTimeCondition(context, time, Math.round(span / (float) MINUTES_MS), 940 userHandle, shortVersion); 941 } 942 final String rt = c == null ? "" : useLine1 ? c.line1 : c.summary; 943 return TextUtils.isEmpty(rt) ? "" : rt; 944 } 945 String summary = ""; 946 for (ZenRule automaticRule : config.automaticRules.values()) { 947 if (automaticRule.isAutomaticActive()) { 948 if (summary.isEmpty()) { 949 summary = automaticRule.name; 950 } else { 951 summary = context.getResources() 952 .getString(R.string.zen_mode_rule_name_combination, summary, 953 automaticRule.name); 954 } 955 } 956 } 957 return summary; 958 } 959 960 public static class ZenRule implements Parcelable { 961 public boolean enabled; 962 public boolean snoozing; // user manually disabled this instance 963 public String name; // required for automatic (unique) 964 public int zenMode; 965 public Uri conditionId; // required for automatic 966 public Condition condition; // optional 967 public ComponentName component; // optional 968 public String id; // required for automatic (unique) 969 public long creationTime; // required for automatic 970 971 public ZenRule() { } 972 973 public ZenRule(Parcel source) { 974 enabled = source.readInt() == 1; 975 snoozing = source.readInt() == 1; 976 if (source.readInt() == 1) { 977 name = source.readString(); 978 } 979 zenMode = source.readInt(); 980 conditionId = source.readParcelable(null); 981 condition = source.readParcelable(null); 982 component = source.readParcelable(null); 983 if (source.readInt() == 1) { 984 id = source.readString(); 985 } 986 creationTime = source.readLong(); 987 } 988 989 @Override 990 public int describeContents() { 991 return 0; 992 } 993 994 @Override 995 public void writeToParcel(Parcel dest, int flags) { 996 dest.writeInt(enabled ? 1 : 0); 997 dest.writeInt(snoozing ? 1 : 0); 998 if (name != null) { 999 dest.writeInt(1); 1000 dest.writeString(name); 1001 } else { 1002 dest.writeInt(0); 1003 } 1004 dest.writeInt(zenMode); 1005 dest.writeParcelable(conditionId, 0); 1006 dest.writeParcelable(condition, 0); 1007 dest.writeParcelable(component, 0); 1008 if (id != null) { 1009 dest.writeInt(1); 1010 dest.writeString(id); 1011 } else { 1012 dest.writeInt(0); 1013 } 1014 dest.writeLong(creationTime); 1015 } 1016 1017 @Override 1018 public String toString() { 1019 return new StringBuilder(ZenRule.class.getSimpleName()).append('[') 1020 .append("enabled=").append(enabled) 1021 .append(",snoozing=").append(snoozing) 1022 .append(",name=").append(name) 1023 .append(",zenMode=").append(Global.zenModeToString(zenMode)) 1024 .append(",conditionId=").append(conditionId) 1025 .append(",condition=").append(condition) 1026 .append(",component=").append(component) 1027 .append(",id=").append(id) 1028 .append(",creationTime=").append(creationTime) 1029 .append(']').toString(); 1030 } 1031 1032 private static void appendDiff(Diff d, String item, ZenRule from, ZenRule to) { 1033 if (d == null) return; 1034 if (from == null) { 1035 if (to != null) { 1036 d.addLine(item, "insert"); 1037 } 1038 return; 1039 } 1040 from.appendDiff(d, item, to); 1041 } 1042 1043 private void appendDiff(Diff d, String item, ZenRule to) { 1044 if (to == null) { 1045 d.addLine(item, "delete"); 1046 return; 1047 } 1048 if (enabled != to.enabled) { 1049 d.addLine(item, "enabled", enabled, to.enabled); 1050 } 1051 if (snoozing != to.snoozing) { 1052 d.addLine(item, "snoozing", snoozing, to.snoozing); 1053 } 1054 if (!Objects.equals(name, to.name)) { 1055 d.addLine(item, "name", name, to.name); 1056 } 1057 if (zenMode != to.zenMode) { 1058 d.addLine(item, "zenMode", zenMode, to.zenMode); 1059 } 1060 if (!Objects.equals(conditionId, to.conditionId)) { 1061 d.addLine(item, "conditionId", conditionId, to.conditionId); 1062 } 1063 if (!Objects.equals(condition, to.condition)) { 1064 d.addLine(item, "condition", condition, to.condition); 1065 } 1066 if (!Objects.equals(component, to.component)) { 1067 d.addLine(item, "component", component, to.component); 1068 } 1069 if (!Objects.equals(id, to.id)) { 1070 d.addLine(item, "id", id, to.id); 1071 } 1072 if (creationTime == to.creationTime) { 1073 d.addLine(item, "creationTime", creationTime, to.creationTime); 1074 } 1075 } 1076 1077 @Override 1078 public boolean equals(Object o) { 1079 if (!(o instanceof ZenRule)) return false; 1080 if (o == this) return true; 1081 final ZenRule other = (ZenRule) o; 1082 return other.enabled == enabled 1083 && other.snoozing == snoozing 1084 && Objects.equals(other.name, name) 1085 && other.zenMode == zenMode 1086 && Objects.equals(other.conditionId, conditionId) 1087 && Objects.equals(other.condition, condition) 1088 && Objects.equals(other.component, component) 1089 && Objects.equals(other.id, id) 1090 && other.creationTime == creationTime; 1091 } 1092 1093 @Override 1094 public int hashCode() { 1095 return Objects.hash(enabled, snoozing, name, zenMode, conditionId, condition, 1096 component, id, creationTime); 1097 } 1098 1099 public boolean isAutomaticActive() { 1100 return enabled && !snoozing && component != null && isTrueOrUnknown(); 1101 } 1102 1103 public boolean isTrueOrUnknown() { 1104 return condition != null && (condition.state == Condition.STATE_TRUE 1105 || condition.state == Condition.STATE_UNKNOWN); 1106 } 1107 1108 public static final Parcelable.Creator<ZenRule> CREATOR 1109 = new Parcelable.Creator<ZenRule>() { 1110 @Override 1111 public ZenRule createFromParcel(Parcel source) { 1112 return new ZenRule(source); 1113 } 1114 @Override 1115 public ZenRule[] newArray(int size) { 1116 return new ZenRule[size]; 1117 } 1118 }; 1119 } 1120 1121 // Legacy config 1122 public static final class XmlV1 { 1123 public static final String SLEEP_MODE_NIGHTS = "nights"; 1124 public static final String SLEEP_MODE_WEEKNIGHTS = "weeknights"; 1125 public static final String SLEEP_MODE_DAYS_PREFIX = "days:"; 1126 1127 private static final String EXIT_CONDITION_TAG = "exitCondition"; 1128 private static final String EXIT_CONDITION_ATT_COMPONENT = "component"; 1129 private static final String SLEEP_TAG = "sleep"; 1130 private static final String SLEEP_ATT_MODE = "mode"; 1131 private static final String SLEEP_ATT_NONE = "none"; 1132 1133 private static final String SLEEP_ATT_START_HR = "startHour"; 1134 private static final String SLEEP_ATT_START_MIN = "startMin"; 1135 private static final String SLEEP_ATT_END_HR = "endHour"; 1136 private static final String SLEEP_ATT_END_MIN = "endMin"; 1137 1138 public boolean allowCalls; 1139 public boolean allowMessages; 1140 public boolean allowReminders = DEFAULT_ALLOW_REMINDERS; 1141 public boolean allowEvents = DEFAULT_ALLOW_EVENTS; 1142 public int allowFrom = SOURCE_ANYONE; 1143 1144 public String sleepMode; // nights, weeknights, days:1,2,3 Calendar.days 1145 public int sleepStartHour; // 0-23 1146 public int sleepStartMinute; // 0-59 1147 public int sleepEndHour; 1148 public int sleepEndMinute; 1149 public boolean sleepNone; // false = priority, true = none 1150 public ComponentName[] conditionComponents; 1151 public Uri[] conditionIds; 1152 public Condition exitCondition; // manual exit condition 1153 public ComponentName exitConditionComponent; // manual exit condition component 1154 1155 private static boolean isValidSleepMode(String sleepMode) { 1156 return sleepMode == null || sleepMode.equals(SLEEP_MODE_NIGHTS) 1157 || sleepMode.equals(SLEEP_MODE_WEEKNIGHTS) || tryParseDays(sleepMode) != null; 1158 } 1159 1160 public static int[] tryParseDays(String sleepMode) { 1161 if (sleepMode == null) return null; 1162 sleepMode = sleepMode.trim(); 1163 if (SLEEP_MODE_NIGHTS.equals(sleepMode)) return ALL_DAYS; 1164 if (SLEEP_MODE_WEEKNIGHTS.equals(sleepMode)) return WEEKNIGHT_DAYS; 1165 if (!sleepMode.startsWith(SLEEP_MODE_DAYS_PREFIX)) return null; 1166 if (sleepMode.equals(SLEEP_MODE_DAYS_PREFIX)) return null; 1167 return tryParseDayList(sleepMode.substring(SLEEP_MODE_DAYS_PREFIX.length()), ","); 1168 } 1169 1170 public static XmlV1 readXml(XmlPullParser parser) 1171 throws XmlPullParserException, IOException { 1172 int type; 1173 String tag; 1174 XmlV1 rt = new XmlV1(); 1175 final ArrayList<ComponentName> conditionComponents = new ArrayList<ComponentName>(); 1176 final ArrayList<Uri> conditionIds = new ArrayList<Uri>(); 1177 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { 1178 tag = parser.getName(); 1179 if (type == XmlPullParser.END_TAG && ZEN_TAG.equals(tag)) { 1180 if (!conditionComponents.isEmpty()) { 1181 rt.conditionComponents = conditionComponents 1182 .toArray(new ComponentName[conditionComponents.size()]); 1183 rt.conditionIds = conditionIds.toArray(new Uri[conditionIds.size()]); 1184 } 1185 return rt; 1186 } 1187 if (type == XmlPullParser.START_TAG) { 1188 if (ALLOW_TAG.equals(tag)) { 1189 rt.allowCalls = safeBoolean(parser, ALLOW_ATT_CALLS, false); 1190 rt.allowMessages = safeBoolean(parser, ALLOW_ATT_MESSAGES, false); 1191 rt.allowReminders = safeBoolean(parser, ALLOW_ATT_REMINDERS, 1192 DEFAULT_ALLOW_REMINDERS); 1193 rt.allowEvents = safeBoolean(parser, ALLOW_ATT_EVENTS, 1194 DEFAULT_ALLOW_EVENTS); 1195 rt.allowFrom = safeInt(parser, ALLOW_ATT_FROM, SOURCE_ANYONE); 1196 if (rt.allowFrom < SOURCE_ANYONE || rt.allowFrom > MAX_SOURCE) { 1197 throw new IndexOutOfBoundsException("bad source in config:" 1198 + rt.allowFrom); 1199 } 1200 } else if (SLEEP_TAG.equals(tag)) { 1201 final String mode = parser.getAttributeValue(null, SLEEP_ATT_MODE); 1202 rt.sleepMode = isValidSleepMode(mode)? mode : null; 1203 rt.sleepNone = safeBoolean(parser, SLEEP_ATT_NONE, false); 1204 final int startHour = safeInt(parser, SLEEP_ATT_START_HR, 0); 1205 final int startMinute = safeInt(parser, SLEEP_ATT_START_MIN, 0); 1206 final int endHour = safeInt(parser, SLEEP_ATT_END_HR, 0); 1207 final int endMinute = safeInt(parser, SLEEP_ATT_END_MIN, 0); 1208 rt.sleepStartHour = isValidHour(startHour) ? startHour : 0; 1209 rt.sleepStartMinute = isValidMinute(startMinute) ? startMinute : 0; 1210 rt.sleepEndHour = isValidHour(endHour) ? endHour : 0; 1211 rt.sleepEndMinute = isValidMinute(endMinute) ? endMinute : 0; 1212 } else if (CONDITION_TAG.equals(tag)) { 1213 final ComponentName component = 1214 safeComponentName(parser, CONDITION_ATT_COMPONENT); 1215 final Uri conditionId = safeUri(parser, CONDITION_ATT_ID); 1216 if (component != null && conditionId != null) { 1217 conditionComponents.add(component); 1218 conditionIds.add(conditionId); 1219 } 1220 } else if (EXIT_CONDITION_TAG.equals(tag)) { 1221 rt.exitCondition = readConditionXml(parser); 1222 if (rt.exitCondition != null) { 1223 rt.exitConditionComponent = 1224 safeComponentName(parser, EXIT_CONDITION_ATT_COMPONENT); 1225 } 1226 } 1227 } 1228 } 1229 throw new IllegalStateException("Failed to reach END_DOCUMENT"); 1230 } 1231 } 1232 1233 public interface Migration { 1234 ZenModeConfig migrate(XmlV1 v1); 1235 } 1236 1237 public static class Diff { 1238 private final ArrayList<String> lines = new ArrayList<>(); 1239 1240 @Override 1241 public String toString() { 1242 final StringBuilder sb = new StringBuilder("Diff["); 1243 final int N = lines.size(); 1244 for (int i = 0; i < N; i++) { 1245 if (i > 0) { 1246 sb.append(','); 1247 } 1248 sb.append(lines.get(i)); 1249 } 1250 return sb.append(']').toString(); 1251 } 1252 1253 private Diff addLine(String item, String action) { 1254 lines.add(item + ":" + action); 1255 return this; 1256 } 1257 1258 public Diff addLine(String item, String subitem, Object from, Object to) { 1259 return addLine(item + "." + subitem, from, to); 1260 } 1261 1262 public Diff addLine(String item, Object from, Object to) { 1263 return addLine(item, from + "->" + to); 1264 } 1265 } 1266 1267} 1268