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