ZenModeConfig.java revision ae641c9ccd3f81214cee54a5f13804f1765187ad
1/** 2 * Copyright (c) 2014, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.service.notification; 18 19import android.content.ComponentName; 20import android.net.Uri; 21import android.os.Parcel; 22import android.os.Parcelable; 23import android.text.TextUtils; 24import android.util.Slog; 25 26import org.xmlpull.v1.XmlPullParser; 27import org.xmlpull.v1.XmlPullParserException; 28import org.xmlpull.v1.XmlSerializer; 29 30import java.io.IOException; 31import java.util.ArrayList; 32import java.util.Arrays; 33import java.util.Calendar; 34import java.util.Objects; 35 36/** 37 * Persisted configuration for zen mode. 38 * 39 * @hide 40 */ 41public class ZenModeConfig implements Parcelable { 42 private static String TAG = "ZenModeConfig"; 43 44 public static final String SLEEP_MODE_NIGHTS = "nights"; 45 public static final String SLEEP_MODE_WEEKNIGHTS = "weeknights"; 46 public static final String SLEEP_MODE_DAYS_PREFIX = "days:"; 47 48 public static final int SOURCE_ANYONE = 0; 49 public static final int SOURCE_CONTACT = 1; 50 public static final int SOURCE_STAR = 2; 51 public static final int MAX_SOURCE = SOURCE_STAR; 52 53 public static final int[] ALL_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, 54 Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY }; 55 public static final int[] WEEKNIGHT_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, 56 Calendar.WEDNESDAY, Calendar.THURSDAY }; 57 58 private static final int XML_VERSION = 1; 59 private static final String ZEN_TAG = "zen"; 60 private static final String ZEN_ATT_VERSION = "version"; 61 private static final String ALLOW_TAG = "allow"; 62 private static final String ALLOW_ATT_CALLS = "calls"; 63 private static final String ALLOW_ATT_MESSAGES = "messages"; 64 private static final String ALLOW_ATT_FROM = "from"; 65 private static final String SLEEP_TAG = "sleep"; 66 private static final String SLEEP_ATT_MODE = "mode"; 67 68 private static final String SLEEP_ATT_START_HR = "startHour"; 69 private static final String SLEEP_ATT_START_MIN = "startMin"; 70 private static final String SLEEP_ATT_END_HR = "endHour"; 71 private static final String SLEEP_ATT_END_MIN = "endMin"; 72 73 private static final String CONDITION_TAG = "condition"; 74 private static final String CONDITION_ATT_COMPONENT = "component"; 75 private static final String CONDITION_ATT_ID = "id"; 76 77 private static final String EXIT_CONDITION_TAG = "exitCondition"; 78 private static final String EXIT_CONDITION_ATT_ID = "id"; 79 80 public boolean allowCalls; 81 public boolean allowMessages; 82 public int allowFrom = SOURCE_ANYONE; 83 84 public String sleepMode; 85 public int sleepStartHour; 86 public int sleepStartMinute; 87 public int sleepEndHour; 88 public int sleepEndMinute; 89 public ComponentName[] conditionComponents; 90 public Uri[] conditionIds; 91 public Uri exitConditionId; 92 93 public ZenModeConfig() { } 94 95 public ZenModeConfig(Parcel source) { 96 allowCalls = source.readInt() == 1; 97 allowMessages = source.readInt() == 1; 98 if (source.readInt() == 1) { 99 sleepMode = source.readString(); 100 } 101 sleepStartHour = source.readInt(); 102 sleepStartMinute = source.readInt(); 103 sleepEndHour = source.readInt(); 104 sleepEndMinute = source.readInt(); 105 int len = source.readInt(); 106 if (len > 0) { 107 conditionComponents = new ComponentName[len]; 108 source.readTypedArray(conditionComponents, ComponentName.CREATOR); 109 } 110 len = source.readInt(); 111 if (len > 0) { 112 conditionIds = new Uri[len]; 113 source.readTypedArray(conditionIds, Uri.CREATOR); 114 } 115 allowFrom = source.readInt(); 116 exitConditionId = source.readParcelable(null); 117 } 118 119 @Override 120 public void writeToParcel(Parcel dest, int flags) { 121 dest.writeInt(allowCalls ? 1 : 0); 122 dest.writeInt(allowMessages ? 1 : 0); 123 if (sleepMode != null) { 124 dest.writeInt(1); 125 dest.writeString(sleepMode); 126 } else { 127 dest.writeInt(0); 128 } 129 dest.writeInt(sleepStartHour); 130 dest.writeInt(sleepStartMinute); 131 dest.writeInt(sleepEndHour); 132 dest.writeInt(sleepEndMinute); 133 if (conditionComponents != null && conditionComponents.length > 0) { 134 dest.writeInt(conditionComponents.length); 135 dest.writeTypedArray(conditionComponents, 0); 136 } else { 137 dest.writeInt(0); 138 } 139 if (conditionIds != null && conditionIds.length > 0) { 140 dest.writeInt(conditionIds.length); 141 dest.writeTypedArray(conditionIds, 0); 142 } else { 143 dest.writeInt(0); 144 } 145 dest.writeInt(allowFrom); 146 dest.writeParcelable(exitConditionId, 0); 147 } 148 149 @Override 150 public String toString() { 151 return new StringBuilder(ZenModeConfig.class.getSimpleName()).append('[') 152 .append("allowCalls=").append(allowCalls) 153 .append(",allowMessages=").append(allowMessages) 154 .append(",allowFrom=").append(sourceToString(allowFrom)) 155 .append(",sleepMode=").append(sleepMode) 156 .append(",sleepStart=").append(sleepStartHour).append('.').append(sleepStartMinute) 157 .append(",sleepEnd=").append(sleepEndHour).append('.').append(sleepEndMinute) 158 .append(",conditionComponents=") 159 .append(conditionComponents == null ? null : TextUtils.join(",", conditionComponents)) 160 .append(",conditionIds=") 161 .append(conditionIds == null ? null : TextUtils.join(",", conditionIds)) 162 .append(",exitConditionId=").append(exitConditionId) 163 .append(']').toString(); 164 } 165 166 public static String sourceToString(int source) { 167 switch (source) { 168 case SOURCE_ANYONE: 169 return "anyone"; 170 case SOURCE_CONTACT: 171 return "contacts"; 172 case SOURCE_STAR: 173 return "stars"; 174 default: 175 return "UNKNOWN"; 176 } 177 } 178 179 @Override 180 public boolean equals(Object o) { 181 if (!(o instanceof ZenModeConfig)) return false; 182 if (o == this) return true; 183 final ZenModeConfig other = (ZenModeConfig) o; 184 return other.allowCalls == allowCalls 185 && other.allowMessages == allowMessages 186 && other.allowFrom == allowFrom 187 && Objects.equals(other.sleepMode, sleepMode) 188 && other.sleepStartHour == sleepStartHour 189 && other.sleepStartMinute == sleepStartMinute 190 && other.sleepEndHour == sleepEndHour 191 && other.sleepEndMinute == sleepEndMinute 192 && Objects.deepEquals(other.conditionComponents, conditionComponents) 193 && Objects.deepEquals(other.conditionIds, conditionIds) 194 && Objects.equals(other.exitConditionId, exitConditionId); 195 } 196 197 @Override 198 public int hashCode() { 199 return Objects.hash(allowCalls, allowMessages, allowFrom, sleepMode, 200 sleepStartHour, sleepStartMinute, sleepEndHour, sleepEndMinute, 201 Arrays.hashCode(conditionComponents), Arrays.hashCode(conditionIds), 202 exitConditionId); 203 } 204 205 public boolean isValid() { 206 return isValidHour(sleepStartHour) && isValidMinute(sleepStartMinute) 207 && isValidHour(sleepEndHour) && isValidMinute(sleepEndMinute) 208 && isValidSleepMode(sleepMode); 209 } 210 211 public static boolean isValidSleepMode(String sleepMode) { 212 return sleepMode == null || sleepMode.equals(SLEEP_MODE_NIGHTS) 213 || sleepMode.equals(SLEEP_MODE_WEEKNIGHTS) || tryParseDays(sleepMode) != null; 214 } 215 216 public static int[] tryParseDays(String sleepMode) { 217 if (sleepMode == null) return null; 218 sleepMode = sleepMode.trim(); 219 if (SLEEP_MODE_NIGHTS.equals(sleepMode)) return ALL_DAYS; 220 if (SLEEP_MODE_WEEKNIGHTS.equals(sleepMode)) return WEEKNIGHT_DAYS; 221 if (!sleepMode.startsWith(SLEEP_MODE_DAYS_PREFIX)) return null; 222 if (sleepMode.equals(SLEEP_MODE_DAYS_PREFIX)) return null; 223 final String[] tokens = sleepMode.substring(SLEEP_MODE_DAYS_PREFIX.length()).split(","); 224 if (tokens.length == 0) return null; 225 final int[] rt = new int[tokens.length]; 226 for (int i = 0; i < tokens.length; i++) { 227 final int day = tryParseInt(tokens[i], -1); 228 if (day == -1) return null; 229 rt[i] = day; 230 } 231 return rt; 232 } 233 234 private static int tryParseInt(String value, int defValue) { 235 if (TextUtils.isEmpty(value)) return defValue; 236 try { 237 return Integer.valueOf(value); 238 } catch (NumberFormatException e) { 239 return defValue; 240 } 241 } 242 243 public static ZenModeConfig readXml(XmlPullParser parser) 244 throws XmlPullParserException, IOException { 245 int type = parser.getEventType(); 246 if (type != XmlPullParser.START_TAG) return null; 247 String tag = parser.getName(); 248 if (!ZEN_TAG.equals(tag)) return null; 249 final ZenModeConfig rt = new ZenModeConfig(); 250 final int version = safeInt(parser, ZEN_ATT_VERSION, XML_VERSION); 251 final ArrayList<ComponentName> conditionComponents = new ArrayList<ComponentName>(); 252 final ArrayList<Uri> conditionIds = new ArrayList<Uri>(); 253 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { 254 tag = parser.getName(); 255 if (type == XmlPullParser.END_TAG && ZEN_TAG.equals(tag)) { 256 if (!conditionComponents.isEmpty()) { 257 rt.conditionComponents = conditionComponents 258 .toArray(new ComponentName[conditionComponents.size()]); 259 rt.conditionIds = conditionIds.toArray(new Uri[conditionIds.size()]); 260 } 261 return rt; 262 } 263 if (type == XmlPullParser.START_TAG) { 264 if (ALLOW_TAG.equals(tag)) { 265 rt.allowCalls = safeBoolean(parser, ALLOW_ATT_CALLS, false); 266 rt.allowMessages = safeBoolean(parser, ALLOW_ATT_MESSAGES, false); 267 rt.allowFrom = safeInt(parser, ALLOW_ATT_FROM, SOURCE_ANYONE); 268 if (rt.allowFrom < SOURCE_ANYONE || rt.allowFrom > MAX_SOURCE) { 269 throw new IndexOutOfBoundsException("bad source in config:" + rt.allowFrom); 270 } 271 } else if (SLEEP_TAG.equals(tag)) { 272 final String mode = parser.getAttributeValue(null, SLEEP_ATT_MODE); 273 rt.sleepMode = isValidSleepMode(mode)? mode : null; 274 final int startHour = safeInt(parser, SLEEP_ATT_START_HR, 0); 275 final int startMinute = safeInt(parser, SLEEP_ATT_START_MIN, 0); 276 final int endHour = safeInt(parser, SLEEP_ATT_END_HR, 0); 277 final int endMinute = safeInt(parser, SLEEP_ATT_END_MIN, 0); 278 rt.sleepStartHour = isValidHour(startHour) ? startHour : 0; 279 rt.sleepStartMinute = isValidMinute(startMinute) ? startMinute : 0; 280 rt.sleepEndHour = isValidHour(endHour) ? endHour : 0; 281 rt.sleepEndMinute = isValidMinute(endMinute) ? endMinute : 0; 282 } else if (CONDITION_TAG.equals(tag)) { 283 final ComponentName component = 284 safeComponentName(parser, CONDITION_ATT_COMPONENT); 285 final Uri conditionId = safeUri(parser, CONDITION_ATT_ID); 286 if (component != null && conditionId != null) { 287 conditionComponents.add(component); 288 conditionIds.add(conditionId); 289 } 290 } else if (EXIT_CONDITION_TAG.equals(tag)) { 291 rt.exitConditionId = safeUri(parser, EXIT_CONDITION_ATT_ID); 292 } 293 } 294 } 295 throw new IllegalStateException("Failed to reach END_DOCUMENT"); 296 } 297 298 public void writeXml(XmlSerializer out) throws IOException { 299 out.startTag(null, ZEN_TAG); 300 out.attribute(null, ZEN_ATT_VERSION, Integer.toString(XML_VERSION)); 301 302 out.startTag(null, ALLOW_TAG); 303 out.attribute(null, ALLOW_ATT_CALLS, Boolean.toString(allowCalls)); 304 out.attribute(null, ALLOW_ATT_MESSAGES, Boolean.toString(allowMessages)); 305 out.attribute(null, ALLOW_ATT_FROM, Integer.toString(allowFrom)); 306 out.endTag(null, ALLOW_TAG); 307 308 out.startTag(null, SLEEP_TAG); 309 if (sleepMode != null) { 310 out.attribute(null, SLEEP_ATT_MODE, sleepMode); 311 } 312 out.attribute(null, SLEEP_ATT_START_HR, Integer.toString(sleepStartHour)); 313 out.attribute(null, SLEEP_ATT_START_MIN, Integer.toString(sleepStartMinute)); 314 out.attribute(null, SLEEP_ATT_END_HR, Integer.toString(sleepEndHour)); 315 out.attribute(null, SLEEP_ATT_END_MIN, Integer.toString(sleepEndMinute)); 316 out.endTag(null, SLEEP_TAG); 317 318 if (conditionComponents != null && conditionIds != null 319 && conditionComponents.length == conditionIds.length) { 320 for (int i = 0; i < conditionComponents.length; i++) { 321 out.startTag(null, CONDITION_TAG); 322 out.attribute(null, CONDITION_ATT_COMPONENT, 323 conditionComponents[i].flattenToString()); 324 out.attribute(null, CONDITION_ATT_ID, conditionIds[i].toString()); 325 out.endTag(null, CONDITION_TAG); 326 } 327 } 328 if (exitConditionId != null) { 329 out.startTag(null, EXIT_CONDITION_TAG); 330 out.attribute(null, EXIT_CONDITION_ATT_ID, exitConditionId.toString()); 331 out.endTag(null, EXIT_CONDITION_TAG); 332 } 333 out.endTag(null, ZEN_TAG); 334 } 335 336 public static boolean isValidHour(int val) { 337 return val >= 0 && val < 24; 338 } 339 340 public static boolean isValidMinute(int val) { 341 return val >= 0 && val < 60; 342 } 343 344 private static boolean safeBoolean(XmlPullParser parser, String att, boolean defValue) { 345 final String val = parser.getAttributeValue(null, att); 346 if (TextUtils.isEmpty(val)) return defValue; 347 return Boolean.valueOf(val); 348 } 349 350 private static int safeInt(XmlPullParser parser, String att, int defValue) { 351 final String val = parser.getAttributeValue(null, att); 352 return tryParseInt(val, defValue); 353 } 354 355 private static ComponentName safeComponentName(XmlPullParser parser, String att) { 356 final String val = parser.getAttributeValue(null, att); 357 if (TextUtils.isEmpty(val)) return null; 358 return ComponentName.unflattenFromString(val); 359 } 360 361 private static Uri safeUri(XmlPullParser parser, String att) { 362 final String val = parser.getAttributeValue(null, att); 363 if (TextUtils.isEmpty(val)) return null; 364 return Uri.parse(val); 365 } 366 367 @Override 368 public int describeContents() { 369 return 0; 370 } 371 372 public ZenModeConfig copy() { 373 final Parcel parcel = Parcel.obtain(); 374 try { 375 writeToParcel(parcel, 0); 376 parcel.setDataPosition(0); 377 return new ZenModeConfig(parcel); 378 } finally { 379 parcel.recycle(); 380 } 381 } 382 383 public static final Parcelable.Creator<ZenModeConfig> CREATOR 384 = new Parcelable.Creator<ZenModeConfig>() { 385 @Override 386 public ZenModeConfig createFromParcel(Parcel source) { 387 return new ZenModeConfig(source); 388 } 389 390 @Override 391 public ZenModeConfig[] newArray(int size) { 392 return new ZenModeConfig[size]; 393 } 394 }; 395 396 // Built-in countdown conditions, e.g. condition://android/countdown/1399917958951 397 398 private static final String COUNTDOWN_AUTHORITY = "android"; 399 private static final String COUNTDOWN_PATH = "countdown"; 400 401 public static Uri toCountdownConditionId(long time) { 402 return new Uri.Builder().scheme(Condition.SCHEME) 403 .authority(COUNTDOWN_AUTHORITY) 404 .appendPath(COUNTDOWN_PATH) 405 .appendPath(Long.toString(time)) 406 .build(); 407 } 408 409 public static long tryParseCountdownConditionId(Uri conditionId) { 410 if (!Condition.isValidId(conditionId, COUNTDOWN_AUTHORITY)) return 0; 411 if (conditionId.getPathSegments().size() != 2 412 || !COUNTDOWN_PATH.equals(conditionId.getPathSegments().get(0))) return 0; 413 try { 414 return Long.parseLong(conditionId.getPathSegments().get(1)); 415 } catch (RuntimeException e) { 416 Slog.w(TAG, "Error parsing countdown condition: " + conditionId, e); 417 return 0; 418 } 419 } 420 421 public static boolean isValidCountdownConditionId(Uri conditionId) { 422 return tryParseCountdownConditionId(conditionId) != 0; 423 } 424} 425