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