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