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