John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 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 | |
| 17 | package android.service.notification; |
| 18 | |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 19 | import android.app.ActivityManager; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 20 | import android.app.NotificationManager.Policy; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 21 | import android.content.ComponentName; |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 22 | import android.content.Context; |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 23 | import android.content.res.Resources; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 24 | import android.net.Uri; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 25 | import android.os.Parcel; |
| 26 | import android.os.Parcelable; |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 27 | import android.os.UserHandle; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 28 | import android.provider.Settings.Global; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 29 | import android.text.TextUtils; |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 30 | import android.text.format.DateFormat; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 31 | import android.util.ArrayMap; |
| 32 | import android.util.ArraySet; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 33 | import android.util.Slog; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 34 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 35 | import com.android.internal.R; |
| 36 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 37 | import org.xmlpull.v1.XmlPullParser; |
| 38 | import org.xmlpull.v1.XmlPullParserException; |
| 39 | import org.xmlpull.v1.XmlSerializer; |
| 40 | |
| 41 | import java.io.IOException; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 42 | import java.util.ArrayList; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 43 | import java.util.Calendar; |
Julia Reynolds | 8e2d2bb | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 44 | import java.util.GregorianCalendar; |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 45 | import java.util.Locale; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 46 | import java.util.Objects; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 47 | import java.util.UUID; |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 48 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 49 | /** |
| 50 | * Persisted configuration for zen mode. |
| 51 | * |
| 52 | * @hide |
| 53 | */ |
| 54 | public class ZenModeConfig implements Parcelable { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 55 | private static String TAG = "ZenModeConfig"; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 56 | |
Chris Wren | 99f963e | 2014-05-28 16:52:42 -0400 | [diff] [blame] | 57 | public static final int SOURCE_ANYONE = 0; |
| 58 | public static final int SOURCE_CONTACT = 1; |
| 59 | public static final int SOURCE_STAR = 2; |
| 60 | public static final int MAX_SOURCE = SOURCE_STAR; |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 61 | private static final int DEFAULT_SOURCE = SOURCE_CONTACT; |
Chris Wren | 99f963e | 2014-05-28 16:52:42 -0400 | [diff] [blame] | 62 | |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 63 | public static final int[] ALL_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, |
| 64 | Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY }; |
| 65 | public static final int[] WEEKNIGHT_DAYS = { Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, |
| 66 | Calendar.WEDNESDAY, Calendar.THURSDAY }; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 67 | public static final int[] WEEKEND_DAYS = { Calendar.FRIDAY, Calendar.SATURDAY }; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 68 | |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 69 | public static final int[] MINUTE_BUCKETS = generateMinuteBuckets(); |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 70 | private static final int SECONDS_MS = 1000; |
| 71 | private static final int MINUTES_MS = 60 * SECONDS_MS; |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 72 | private static final int DAY_MINUTES = 24 * 60; |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 73 | private static final int ZERO_VALUE_MS = 10 * SECONDS_MS; |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 74 | |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 75 | private static final boolean DEFAULT_ALLOW_CALLS = true; |
| 76 | private static final boolean DEFAULT_ALLOW_MESSAGES = false; |
John Spurlock | fc746f8 | 2015-04-03 13:47:14 -0400 | [diff] [blame] | 77 | private static final boolean DEFAULT_ALLOW_REMINDERS = true; |
John Spurlock | 2dac62c | 2014-09-19 15:29:06 -0400 | [diff] [blame] | 78 | private static final boolean DEFAULT_ALLOW_EVENTS = true; |
John Spurlock | 1d7d224 | 2015-04-10 08:10:22 -0400 | [diff] [blame] | 79 | private static final boolean DEFAULT_ALLOW_REPEAT_CALLERS = false; |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 80 | private static final boolean DEFAULT_ALLOW_SCREEN_OFF = true; |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 81 | private static final boolean DEFAULT_ALLOW_SCREEN_ON = true; |
John Spurlock | 2dac62c | 2014-09-19 15:29:06 -0400 | [diff] [blame] | 82 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 83 | private static final int XML_VERSION = 2; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 84 | private static final String ZEN_TAG = "zen"; |
| 85 | private static final String ZEN_ATT_VERSION = "version"; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 86 | private static final String ZEN_ATT_USER = "user"; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 87 | private static final String ALLOW_TAG = "allow"; |
| 88 | private static final String ALLOW_ATT_CALLS = "calls"; |
John Spurlock | 1d7d224 | 2015-04-10 08:10:22 -0400 | [diff] [blame] | 89 | private static final String ALLOW_ATT_REPEAT_CALLERS = "repeatCallers"; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 90 | private static final String ALLOW_ATT_MESSAGES = "messages"; |
Chris Wren | 99f963e | 2014-05-28 16:52:42 -0400 | [diff] [blame] | 91 | private static final String ALLOW_ATT_FROM = "from"; |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 92 | private static final String ALLOW_ATT_CALLS_FROM = "callsFrom"; |
| 93 | private static final String ALLOW_ATT_MESSAGES_FROM = "messagesFrom"; |
John Spurlock | fc746f8 | 2015-04-03 13:47:14 -0400 | [diff] [blame] | 94 | private static final String ALLOW_ATT_REMINDERS = "reminders"; |
John Spurlock | 2dac62c | 2014-09-19 15:29:06 -0400 | [diff] [blame] | 95 | private static final String ALLOW_ATT_EVENTS = "events"; |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 96 | private static final String ALLOW_ATT_SCREEN_OFF = "visualScreenOff"; |
| 97 | private static final String ALLOW_ATT_SCREEN_ON = "visualScreenOn"; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 98 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 99 | private static final String CONDITION_TAG = "condition"; |
| 100 | private static final String CONDITION_ATT_COMPONENT = "component"; |
| 101 | private static final String CONDITION_ATT_ID = "id"; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 102 | private static final String CONDITION_ATT_SUMMARY = "summary"; |
| 103 | private static final String CONDITION_ATT_LINE1 = "line1"; |
| 104 | private static final String CONDITION_ATT_LINE2 = "line2"; |
| 105 | private static final String CONDITION_ATT_ICON = "icon"; |
| 106 | private static final String CONDITION_ATT_STATE = "state"; |
| 107 | private static final String CONDITION_ATT_FLAGS = "flags"; |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 108 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 109 | private static final String MANUAL_TAG = "manual"; |
| 110 | private static final String AUTOMATIC_TAG = "automatic"; |
| 111 | |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 112 | private static final String RULE_ATT_ID = "ruleId"; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 113 | private static final String RULE_ATT_ENABLED = "enabled"; |
| 114 | private static final String RULE_ATT_SNOOZING = "snoozing"; |
| 115 | private static final String RULE_ATT_NAME = "name"; |
| 116 | private static final String RULE_ATT_COMPONENT = "component"; |
| 117 | private static final String RULE_ATT_ZEN = "zen"; |
| 118 | private static final String RULE_ATT_CONDITION_ID = "conditionId"; |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 119 | private static final String RULE_ATT_CREATION_TIME = "creationTime"; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 120 | |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 121 | public boolean allowCalls = DEFAULT_ALLOW_CALLS; |
John Spurlock | 1d7d224 | 2015-04-10 08:10:22 -0400 | [diff] [blame] | 122 | public boolean allowRepeatCallers = DEFAULT_ALLOW_REPEAT_CALLERS; |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 123 | public boolean allowMessages = DEFAULT_ALLOW_MESSAGES; |
John Spurlock | fc746f8 | 2015-04-03 13:47:14 -0400 | [diff] [blame] | 124 | public boolean allowReminders = DEFAULT_ALLOW_REMINDERS; |
John Spurlock | 2dac62c | 2014-09-19 15:29:06 -0400 | [diff] [blame] | 125 | public boolean allowEvents = DEFAULT_ALLOW_EVENTS; |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 126 | public int allowCallsFrom = DEFAULT_SOURCE; |
| 127 | public int allowMessagesFrom = DEFAULT_SOURCE; |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 128 | public int user = UserHandle.USER_SYSTEM; |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 129 | public boolean allowWhenScreenOff = DEFAULT_ALLOW_SCREEN_OFF; |
| 130 | public boolean allowWhenScreenOn = DEFAULT_ALLOW_SCREEN_ON; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 131 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 132 | public ZenRule manualRule; |
| 133 | public ArrayMap<String, ZenRule> automaticRules = new ArrayMap<>(); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 134 | |
| 135 | public ZenModeConfig() { } |
| 136 | |
| 137 | public ZenModeConfig(Parcel source) { |
| 138 | allowCalls = source.readInt() == 1; |
John Spurlock | 1d7d224 | 2015-04-10 08:10:22 -0400 | [diff] [blame] | 139 | allowRepeatCallers = source.readInt() == 1; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 140 | allowMessages = source.readInt() == 1; |
John Spurlock | fc746f8 | 2015-04-03 13:47:14 -0400 | [diff] [blame] | 141 | allowReminders = source.readInt() == 1; |
John Spurlock | 2dac62c | 2014-09-19 15:29:06 -0400 | [diff] [blame] | 142 | allowEvents = source.readInt() == 1; |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 143 | allowCallsFrom = source.readInt(); |
| 144 | allowMessagesFrom = source.readInt(); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 145 | user = source.readInt(); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 146 | manualRule = source.readParcelable(null); |
| 147 | final int len = source.readInt(); |
| 148 | if (len > 0) { |
| 149 | final String[] ids = new String[len]; |
| 150 | final ZenRule[] rules = new ZenRule[len]; |
| 151 | source.readStringArray(ids); |
| 152 | source.readTypedArray(rules, ZenRule.CREATOR); |
| 153 | for (int i = 0; i < len; i++) { |
| 154 | automaticRules.put(ids[i], rules[i]); |
| 155 | } |
| 156 | } |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 157 | allowWhenScreenOff = source.readInt() == 1; |
| 158 | allowWhenScreenOn = source.readInt() == 1; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void writeToParcel(Parcel dest, int flags) { |
| 163 | dest.writeInt(allowCalls ? 1 : 0); |
John Spurlock | 1d7d224 | 2015-04-10 08:10:22 -0400 | [diff] [blame] | 164 | dest.writeInt(allowRepeatCallers ? 1 : 0); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 165 | dest.writeInt(allowMessages ? 1 : 0); |
John Spurlock | fc746f8 | 2015-04-03 13:47:14 -0400 | [diff] [blame] | 166 | dest.writeInt(allowReminders ? 1 : 0); |
John Spurlock | 2dac62c | 2014-09-19 15:29:06 -0400 | [diff] [blame] | 167 | dest.writeInt(allowEvents ? 1 : 0); |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 168 | dest.writeInt(allowCallsFrom); |
| 169 | dest.writeInt(allowMessagesFrom); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 170 | dest.writeInt(user); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 171 | dest.writeParcelable(manualRule, 0); |
| 172 | if (!automaticRules.isEmpty()) { |
| 173 | final int len = automaticRules.size(); |
| 174 | final String[] ids = new String[len]; |
| 175 | final ZenRule[] rules = new ZenRule[len]; |
| 176 | for (int i = 0; i < len; i++) { |
| 177 | ids[i] = automaticRules.keyAt(i); |
| 178 | rules[i] = automaticRules.valueAt(i); |
| 179 | } |
| 180 | dest.writeInt(len); |
| 181 | dest.writeStringArray(ids); |
| 182 | dest.writeTypedArray(rules, 0); |
| 183 | } else { |
| 184 | dest.writeInt(0); |
| 185 | } |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 186 | dest.writeInt(allowWhenScreenOff ? 1 : 0); |
| 187 | dest.writeInt(allowWhenScreenOn ? 1 : 0); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public String toString() { |
| 192 | return new StringBuilder(ZenModeConfig.class.getSimpleName()).append('[') |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 193 | .append("user=").append(user) |
| 194 | .append(",allowCalls=").append(allowCalls) |
| 195 | .append(",allowRepeatCallers=").append(allowRepeatCallers) |
| 196 | .append(",allowMessages=").append(allowMessages) |
| 197 | .append(",allowCallsFrom=").append(sourceToString(allowCallsFrom)) |
| 198 | .append(",allowMessagesFrom=").append(sourceToString(allowMessagesFrom)) |
| 199 | .append(",allowReminders=").append(allowReminders) |
| 200 | .append(",allowEvents=").append(allowEvents) |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 201 | .append(",allowWhenScreenOff=").append(allowWhenScreenOff) |
| 202 | .append(",allowWhenScreenOn=").append(allowWhenScreenOn) |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 203 | .append(",automaticRules=").append(automaticRules) |
| 204 | .append(",manualRule=").append(manualRule) |
| 205 | .append(']').toString(); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 206 | } |
| 207 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 208 | private Diff diff(ZenModeConfig to) { |
| 209 | final Diff d = new Diff(); |
| 210 | if (to == null) { |
| 211 | return d.addLine("config", "delete"); |
| 212 | } |
| 213 | if (user != to.user) { |
| 214 | d.addLine("user", user, to.user); |
| 215 | } |
| 216 | if (allowCalls != to.allowCalls) { |
| 217 | d.addLine("allowCalls", allowCalls, to.allowCalls); |
| 218 | } |
| 219 | if (allowRepeatCallers != to.allowRepeatCallers) { |
| 220 | d.addLine("allowRepeatCallers", allowRepeatCallers, to.allowRepeatCallers); |
| 221 | } |
| 222 | if (allowMessages != to.allowMessages) { |
| 223 | d.addLine("allowMessages", allowMessages, to.allowMessages); |
| 224 | } |
| 225 | if (allowCallsFrom != to.allowCallsFrom) { |
| 226 | d.addLine("allowCallsFrom", allowCallsFrom, to.allowCallsFrom); |
| 227 | } |
| 228 | if (allowMessagesFrom != to.allowMessagesFrom) { |
| 229 | d.addLine("allowMessagesFrom", allowMessagesFrom, to.allowMessagesFrom); |
| 230 | } |
| 231 | if (allowReminders != to.allowReminders) { |
| 232 | d.addLine("allowReminders", allowReminders, to.allowReminders); |
| 233 | } |
| 234 | if (allowEvents != to.allowEvents) { |
| 235 | d.addLine("allowEvents", allowEvents, to.allowEvents); |
| 236 | } |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 237 | if (allowWhenScreenOff != to.allowWhenScreenOff) { |
| 238 | d.addLine("allowWhenScreenOff", allowWhenScreenOff, to.allowWhenScreenOff); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 239 | } |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 240 | if (allowWhenScreenOn != to.allowWhenScreenOn) { |
| 241 | d.addLine("allowWhenScreenOn", allowWhenScreenOn, to.allowWhenScreenOn); |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 242 | } |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 243 | final ArraySet<String> allRules = new ArraySet<>(); |
| 244 | addKeys(allRules, automaticRules); |
| 245 | addKeys(allRules, to.automaticRules); |
| 246 | final int N = allRules.size(); |
| 247 | for (int i = 0; i < N; i++) { |
| 248 | final String rule = allRules.valueAt(i); |
| 249 | final ZenRule fromRule = automaticRules != null ? automaticRules.get(rule) : null; |
| 250 | final ZenRule toRule = to.automaticRules != null ? to.automaticRules.get(rule) : null; |
| 251 | ZenRule.appendDiff(d, "automaticRule[" + rule + "]", fromRule, toRule); |
| 252 | } |
| 253 | ZenRule.appendDiff(d, "manualRule", manualRule, to.manualRule); |
| 254 | return d; |
| 255 | } |
| 256 | |
| 257 | public static Diff diff(ZenModeConfig from, ZenModeConfig to) { |
| 258 | if (from == null) { |
| 259 | final Diff d = new Diff(); |
| 260 | if (to != null) { |
| 261 | d.addLine("config", "insert"); |
| 262 | } |
| 263 | return d; |
| 264 | } |
| 265 | return from.diff(to); |
| 266 | } |
| 267 | |
| 268 | private static <T> void addKeys(ArraySet<T> set, ArrayMap<T, ?> map) { |
| 269 | if (map != null) { |
| 270 | for (int i = 0; i < map.size(); i++) { |
| 271 | set.add(map.keyAt(i)); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 276 | public boolean isValid() { |
| 277 | if (!isValidManualRule(manualRule)) return false; |
| 278 | final int N = automaticRules.size(); |
| 279 | for (int i = 0; i < N; i++) { |
| 280 | if (!isValidAutomaticRule(automaticRules.valueAt(i))) return false; |
| 281 | } |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | private static boolean isValidManualRule(ZenRule rule) { |
| 286 | return rule == null || Global.isValidZenMode(rule.zenMode) && sameCondition(rule); |
| 287 | } |
| 288 | |
| 289 | private static boolean isValidAutomaticRule(ZenRule rule) { |
| 290 | return rule != null && !TextUtils.isEmpty(rule.name) && Global.isValidZenMode(rule.zenMode) |
| 291 | && rule.conditionId != null && sameCondition(rule); |
| 292 | } |
| 293 | |
| 294 | private static boolean sameCondition(ZenRule rule) { |
| 295 | if (rule == null) return false; |
| 296 | if (rule.conditionId == null) { |
| 297 | return rule.condition == null; |
| 298 | } else { |
| 299 | return rule.condition == null || rule.conditionId.equals(rule.condition.id); |
| 300 | } |
| 301 | } |
| 302 | |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 303 | private static int[] generateMinuteBuckets() { |
| 304 | final int maxHrs = 12; |
| 305 | final int[] buckets = new int[maxHrs + 3]; |
| 306 | buckets[0] = 15; |
| 307 | buckets[1] = 30; |
| 308 | buckets[2] = 45; |
| 309 | for (int i = 1; i <= maxHrs; i++) { |
| 310 | buckets[2 + i] = 60 * i; |
| 311 | } |
| 312 | return buckets; |
| 313 | } |
| 314 | |
Chris Wren | 99f963e | 2014-05-28 16:52:42 -0400 | [diff] [blame] | 315 | public static String sourceToString(int source) { |
| 316 | switch (source) { |
| 317 | case SOURCE_ANYONE: |
| 318 | return "anyone"; |
| 319 | case SOURCE_CONTACT: |
| 320 | return "contacts"; |
| 321 | case SOURCE_STAR: |
| 322 | return "stars"; |
| 323 | default: |
| 324 | return "UNKNOWN"; |
| 325 | } |
| 326 | } |
| 327 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 328 | @Override |
| 329 | public boolean equals(Object o) { |
| 330 | if (!(o instanceof ZenModeConfig)) return false; |
| 331 | if (o == this) return true; |
| 332 | final ZenModeConfig other = (ZenModeConfig) o; |
| 333 | return other.allowCalls == allowCalls |
John Spurlock | 1d7d224 | 2015-04-10 08:10:22 -0400 | [diff] [blame] | 334 | && other.allowRepeatCallers == allowRepeatCallers |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 335 | && other.allowMessages == allowMessages |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 336 | && other.allowCallsFrom == allowCallsFrom |
| 337 | && other.allowMessagesFrom == allowMessagesFrom |
John Spurlock | fc746f8 | 2015-04-03 13:47:14 -0400 | [diff] [blame] | 338 | && other.allowReminders == allowReminders |
John Spurlock | 2dac62c | 2014-09-19 15:29:06 -0400 | [diff] [blame] | 339 | && other.allowEvents == allowEvents |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 340 | && other.allowWhenScreenOff == allowWhenScreenOff |
| 341 | && other.allowWhenScreenOn == allowWhenScreenOn |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 342 | && other.user == user |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 343 | && Objects.equals(other.automaticRules, automaticRules) |
| 344 | && Objects.equals(other.manualRule, manualRule); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public int hashCode() { |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 349 | return Objects.hash(allowCalls, allowRepeatCallers, allowMessages, allowCallsFrom, |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 350 | allowMessagesFrom, allowReminders, allowEvents, allowWhenScreenOff, |
| 351 | allowWhenScreenOn, |
| 352 | user, automaticRules, manualRule); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 353 | } |
| 354 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 355 | private static String toDayList(int[] days) { |
| 356 | if (days == null || days.length == 0) return ""; |
| 357 | final StringBuilder sb = new StringBuilder(); |
| 358 | for (int i = 0; i < days.length; i++) { |
| 359 | if (i > 0) sb.append('.'); |
| 360 | sb.append(days[i]); |
| 361 | } |
| 362 | return sb.toString(); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 363 | } |
| 364 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 365 | private static int[] tryParseDayList(String dayList, String sep) { |
| 366 | if (dayList == null) return null; |
| 367 | final String[] tokens = dayList.split(sep); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 368 | if (tokens.length == 0) return null; |
| 369 | final int[] rt = new int[tokens.length]; |
| 370 | for (int i = 0; i < tokens.length; i++) { |
| 371 | final int day = tryParseInt(tokens[i], -1); |
| 372 | if (day == -1) return null; |
| 373 | rt[i] = day; |
| 374 | } |
| 375 | return rt; |
| 376 | } |
| 377 | |
| 378 | private static int tryParseInt(String value, int defValue) { |
| 379 | if (TextUtils.isEmpty(value)) return defValue; |
| 380 | try { |
| 381 | return Integer.valueOf(value); |
| 382 | } catch (NumberFormatException e) { |
| 383 | return defValue; |
| 384 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 385 | } |
| 386 | |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 387 | private static long tryParseLong(String value, long defValue) { |
| 388 | if (TextUtils.isEmpty(value)) return defValue; |
| 389 | try { |
| 390 | return Long.valueOf(value); |
| 391 | } catch (NumberFormatException e) { |
| 392 | return defValue; |
| 393 | } |
| 394 | } |
| 395 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 396 | public static ZenModeConfig readXml(XmlPullParser parser, Migration migration) |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 397 | throws XmlPullParserException, IOException { |
| 398 | int type = parser.getEventType(); |
| 399 | if (type != XmlPullParser.START_TAG) return null; |
| 400 | String tag = parser.getName(); |
| 401 | if (!ZEN_TAG.equals(tag)) return null; |
| 402 | final ZenModeConfig rt = new ZenModeConfig(); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 403 | final int version = safeInt(parser, ZEN_ATT_VERSION, XML_VERSION); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 404 | if (version == 1) { |
| 405 | final XmlV1 v1 = XmlV1.readXml(parser); |
| 406 | return migration.migrate(v1); |
| 407 | } |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 408 | rt.user = safeInt(parser, ZEN_ATT_USER, rt.user); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 409 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 410 | tag = parser.getName(); |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 411 | if (type == XmlPullParser.END_TAG && ZEN_TAG.equals(tag)) { |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 412 | return rt; |
| 413 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 414 | if (type == XmlPullParser.START_TAG) { |
| 415 | if (ALLOW_TAG.equals(tag)) { |
| 416 | rt.allowCalls = safeBoolean(parser, ALLOW_ATT_CALLS, false); |
John Spurlock | 1d7d224 | 2015-04-10 08:10:22 -0400 | [diff] [blame] | 417 | rt.allowRepeatCallers = safeBoolean(parser, ALLOW_ATT_REPEAT_CALLERS, |
| 418 | DEFAULT_ALLOW_REPEAT_CALLERS); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 419 | rt.allowMessages = safeBoolean(parser, ALLOW_ATT_MESSAGES, false); |
John Spurlock | fc746f8 | 2015-04-03 13:47:14 -0400 | [diff] [blame] | 420 | rt.allowReminders = safeBoolean(parser, ALLOW_ATT_REMINDERS, |
| 421 | DEFAULT_ALLOW_REMINDERS); |
John Spurlock | 2dac62c | 2014-09-19 15:29:06 -0400 | [diff] [blame] | 422 | rt.allowEvents = safeBoolean(parser, ALLOW_ATT_EVENTS, DEFAULT_ALLOW_EVENTS); |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 423 | final int from = safeInt(parser, ALLOW_ATT_FROM, -1); |
| 424 | final int callsFrom = safeInt(parser, ALLOW_ATT_CALLS_FROM, -1); |
| 425 | final int messagesFrom = safeInt(parser, ALLOW_ATT_MESSAGES_FROM, -1); |
| 426 | if (isValidSource(callsFrom) && isValidSource(messagesFrom)) { |
| 427 | rt.allowCallsFrom = callsFrom; |
| 428 | rt.allowMessagesFrom = messagesFrom; |
| 429 | } else if (isValidSource(from)) { |
| 430 | Slog.i(TAG, "Migrating existing shared 'from': " + sourceToString(from)); |
| 431 | rt.allowCallsFrom = from; |
| 432 | rt.allowMessagesFrom = from; |
| 433 | } else { |
| 434 | rt.allowCallsFrom = DEFAULT_SOURCE; |
| 435 | rt.allowMessagesFrom = DEFAULT_SOURCE; |
Chris Wren | 99f963e | 2014-05-28 16:52:42 -0400 | [diff] [blame] | 436 | } |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 437 | rt.allowWhenScreenOff = |
| 438 | safeBoolean(parser, ALLOW_ATT_SCREEN_OFF, DEFAULT_ALLOW_SCREEN_OFF); |
| 439 | rt.allowWhenScreenOn = |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 440 | safeBoolean(parser, ALLOW_ATT_SCREEN_ON, DEFAULT_ALLOW_SCREEN_ON); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 441 | } else if (MANUAL_TAG.equals(tag)) { |
John Spurlock | 995a749 | 2015-05-28 22:13:03 -0400 | [diff] [blame] | 442 | rt.manualRule = readRuleXml(parser); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 443 | } else if (AUTOMATIC_TAG.equals(tag)) { |
| 444 | final String id = parser.getAttributeValue(null, RULE_ATT_ID); |
John Spurlock | 995a749 | 2015-05-28 22:13:03 -0400 | [diff] [blame] | 445 | final ZenRule automaticRule = readRuleXml(parser); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 446 | if (id != null && automaticRule != null) { |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 447 | automaticRule.id = id; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 448 | rt.automaticRules.put(id, automaticRule); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 449 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | } |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 453 | throw new IllegalStateException("Failed to reach END_DOCUMENT"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | public void writeXml(XmlSerializer out) throws IOException { |
| 457 | out.startTag(null, ZEN_TAG); |
| 458 | out.attribute(null, ZEN_ATT_VERSION, Integer.toString(XML_VERSION)); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 459 | out.attribute(null, ZEN_ATT_USER, Integer.toString(user)); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 460 | |
| 461 | out.startTag(null, ALLOW_TAG); |
| 462 | out.attribute(null, ALLOW_ATT_CALLS, Boolean.toString(allowCalls)); |
John Spurlock | 1d7d224 | 2015-04-10 08:10:22 -0400 | [diff] [blame] | 463 | out.attribute(null, ALLOW_ATT_REPEAT_CALLERS, Boolean.toString(allowRepeatCallers)); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 464 | out.attribute(null, ALLOW_ATT_MESSAGES, Boolean.toString(allowMessages)); |
John Spurlock | fc746f8 | 2015-04-03 13:47:14 -0400 | [diff] [blame] | 465 | out.attribute(null, ALLOW_ATT_REMINDERS, Boolean.toString(allowReminders)); |
John Spurlock | 2dac62c | 2014-09-19 15:29:06 -0400 | [diff] [blame] | 466 | out.attribute(null, ALLOW_ATT_EVENTS, Boolean.toString(allowEvents)); |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 467 | out.attribute(null, ALLOW_ATT_CALLS_FROM, Integer.toString(allowCallsFrom)); |
| 468 | out.attribute(null, ALLOW_ATT_MESSAGES_FROM, Integer.toString(allowMessagesFrom)); |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 469 | out.attribute(null, ALLOW_ATT_SCREEN_OFF, Boolean.toString(allowWhenScreenOff)); |
| 470 | out.attribute(null, ALLOW_ATT_SCREEN_ON, Boolean.toString(allowWhenScreenOn)); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 471 | out.endTag(null, ALLOW_TAG); |
| 472 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 473 | if (manualRule != null) { |
| 474 | out.startTag(null, MANUAL_TAG); |
| 475 | writeRuleXml(manualRule, out); |
| 476 | out.endTag(null, MANUAL_TAG); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 477 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 478 | final int N = automaticRules.size(); |
| 479 | for (int i = 0; i < N; i++) { |
| 480 | final String id = automaticRules.keyAt(i); |
| 481 | final ZenRule automaticRule = automaticRules.valueAt(i); |
| 482 | out.startTag(null, AUTOMATIC_TAG); |
| 483 | out.attribute(null, RULE_ATT_ID, id); |
| 484 | writeRuleXml(automaticRule, out); |
| 485 | out.endTag(null, AUTOMATIC_TAG); |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 486 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 487 | out.endTag(null, ZEN_TAG); |
| 488 | } |
| 489 | |
John Spurlock | 995a749 | 2015-05-28 22:13:03 -0400 | [diff] [blame] | 490 | public static ZenRule readRuleXml(XmlPullParser parser) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 491 | final ZenRule rt = new ZenRule(); |
| 492 | rt.enabled = safeBoolean(parser, RULE_ATT_ENABLED, true); |
| 493 | rt.snoozing = safeBoolean(parser, RULE_ATT_SNOOZING, false); |
| 494 | rt.name = parser.getAttributeValue(null, RULE_ATT_NAME); |
| 495 | final String zen = parser.getAttributeValue(null, RULE_ATT_ZEN); |
| 496 | rt.zenMode = tryParseZenMode(zen, -1); |
| 497 | if (rt.zenMode == -1) { |
| 498 | Slog.w(TAG, "Bad zen mode in rule xml:" + zen); |
| 499 | return null; |
| 500 | } |
| 501 | rt.conditionId = safeUri(parser, RULE_ATT_CONDITION_ID); |
| 502 | rt.component = safeComponentName(parser, RULE_ATT_COMPONENT); |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 503 | rt.creationTime = safeLong(parser, RULE_ATT_CREATION_TIME, 0); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 504 | rt.condition = readConditionXml(parser); |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 505 | return rt; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | public static void writeRuleXml(ZenRule rule, XmlSerializer out) throws IOException { |
| 509 | out.attribute(null, RULE_ATT_ENABLED, Boolean.toString(rule.enabled)); |
| 510 | out.attribute(null, RULE_ATT_SNOOZING, Boolean.toString(rule.snoozing)); |
| 511 | if (rule.name != null) { |
| 512 | out.attribute(null, RULE_ATT_NAME, rule.name); |
| 513 | } |
| 514 | out.attribute(null, RULE_ATT_ZEN, Integer.toString(rule.zenMode)); |
| 515 | if (rule.component != null) { |
| 516 | out.attribute(null, RULE_ATT_COMPONENT, rule.component.flattenToString()); |
| 517 | } |
| 518 | if (rule.conditionId != null) { |
| 519 | out.attribute(null, RULE_ATT_CONDITION_ID, rule.conditionId.toString()); |
| 520 | } |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 521 | out.attribute(null, RULE_ATT_CREATION_TIME, Long.toString(rule.creationTime)); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 522 | if (rule.condition != null) { |
| 523 | writeConditionXml(rule.condition, out); |
| 524 | } |
| 525 | } |
| 526 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 527 | public static Condition readConditionXml(XmlPullParser parser) { |
| 528 | final Uri id = safeUri(parser, CONDITION_ATT_ID); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 529 | if (id == null) return null; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 530 | final String summary = parser.getAttributeValue(null, CONDITION_ATT_SUMMARY); |
| 531 | final String line1 = parser.getAttributeValue(null, CONDITION_ATT_LINE1); |
| 532 | final String line2 = parser.getAttributeValue(null, CONDITION_ATT_LINE2); |
| 533 | final int icon = safeInt(parser, CONDITION_ATT_ICON, -1); |
| 534 | final int state = safeInt(parser, CONDITION_ATT_STATE, -1); |
| 535 | final int flags = safeInt(parser, CONDITION_ATT_FLAGS, -1); |
| 536 | try { |
| 537 | return new Condition(id, summary, line1, line2, icon, state, flags); |
| 538 | } catch (IllegalArgumentException e) { |
| 539 | Slog.w(TAG, "Unable to read condition xml", e); |
| 540 | return null; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | public static void writeConditionXml(Condition c, XmlSerializer out) throws IOException { |
| 545 | out.attribute(null, CONDITION_ATT_ID, c.id.toString()); |
| 546 | out.attribute(null, CONDITION_ATT_SUMMARY, c.summary); |
| 547 | out.attribute(null, CONDITION_ATT_LINE1, c.line1); |
| 548 | out.attribute(null, CONDITION_ATT_LINE2, c.line2); |
| 549 | out.attribute(null, CONDITION_ATT_ICON, Integer.toString(c.icon)); |
| 550 | out.attribute(null, CONDITION_ATT_STATE, Integer.toString(c.state)); |
| 551 | out.attribute(null, CONDITION_ATT_FLAGS, Integer.toString(c.flags)); |
| 552 | } |
| 553 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 554 | public static boolean isValidHour(int val) { |
| 555 | return val >= 0 && val < 24; |
| 556 | } |
| 557 | |
| 558 | public static boolean isValidMinute(int val) { |
| 559 | return val >= 0 && val < 60; |
| 560 | } |
| 561 | |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 562 | private static boolean isValidSource(int source) { |
| 563 | return source >= SOURCE_ANYONE && source <= MAX_SOURCE; |
| 564 | } |
| 565 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 566 | private static boolean safeBoolean(XmlPullParser parser, String att, boolean defValue) { |
| 567 | final String val = parser.getAttributeValue(null, att); |
Julia Reynolds | 1998ee5 | 2016-02-11 13:49:08 -0500 | [diff] [blame] | 568 | return safeBoolean(val, defValue); |
| 569 | } |
| 570 | |
| 571 | private static boolean safeBoolean(String val, boolean defValue) { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 572 | if (TextUtils.isEmpty(val)) return defValue; |
| 573 | return Boolean.valueOf(val); |
| 574 | } |
| 575 | |
| 576 | private static int safeInt(XmlPullParser parser, String att, int defValue) { |
| 577 | final String val = parser.getAttributeValue(null, att); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 578 | return tryParseInt(val, defValue); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 579 | } |
| 580 | |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 581 | private static ComponentName safeComponentName(XmlPullParser parser, String att) { |
| 582 | final String val = parser.getAttributeValue(null, att); |
| 583 | if (TextUtils.isEmpty(val)) return null; |
| 584 | return ComponentName.unflattenFromString(val); |
| 585 | } |
| 586 | |
| 587 | private static Uri safeUri(XmlPullParser parser, String att) { |
| 588 | final String val = parser.getAttributeValue(null, att); |
| 589 | if (TextUtils.isEmpty(val)) return null; |
| 590 | return Uri.parse(val); |
| 591 | } |
| 592 | |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 593 | private static long safeLong(XmlPullParser parser, String att, long defValue) { |
| 594 | final String val = parser.getAttributeValue(null, att); |
| 595 | return tryParseLong(val, defValue); |
| 596 | } |
| 597 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 598 | @Override |
| 599 | public int describeContents() { |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | public ZenModeConfig copy() { |
| 604 | final Parcel parcel = Parcel.obtain(); |
| 605 | try { |
| 606 | writeToParcel(parcel, 0); |
| 607 | parcel.setDataPosition(0); |
| 608 | return new ZenModeConfig(parcel); |
| 609 | } finally { |
| 610 | parcel.recycle(); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | public static final Parcelable.Creator<ZenModeConfig> CREATOR |
| 615 | = new Parcelable.Creator<ZenModeConfig>() { |
| 616 | @Override |
| 617 | public ZenModeConfig createFromParcel(Parcel source) { |
| 618 | return new ZenModeConfig(source); |
| 619 | } |
| 620 | |
| 621 | @Override |
| 622 | public ZenModeConfig[] newArray(int size) { |
| 623 | return new ZenModeConfig[size]; |
| 624 | } |
| 625 | }; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 626 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 627 | public Policy toNotificationPolicy() { |
| 628 | int priorityCategories = 0; |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 629 | int priorityCallSenders = Policy.PRIORITY_SENDERS_CONTACTS; |
| 630 | int priorityMessageSenders = Policy.PRIORITY_SENDERS_CONTACTS; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 631 | if (allowCalls) { |
| 632 | priorityCategories |= Policy.PRIORITY_CATEGORY_CALLS; |
| 633 | } |
| 634 | if (allowMessages) { |
| 635 | priorityCategories |= Policy.PRIORITY_CATEGORY_MESSAGES; |
| 636 | } |
| 637 | if (allowEvents) { |
| 638 | priorityCategories |= Policy.PRIORITY_CATEGORY_EVENTS; |
| 639 | } |
| 640 | if (allowReminders) { |
| 641 | priorityCategories |= Policy.PRIORITY_CATEGORY_REMINDERS; |
| 642 | } |
| 643 | if (allowRepeatCallers) { |
| 644 | priorityCategories |= Policy.PRIORITY_CATEGORY_REPEAT_CALLERS; |
| 645 | } |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 646 | int suppressedVisualEffects = 0; |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 647 | if (!allowWhenScreenOff) { |
| 648 | suppressedVisualEffects |= Policy.SUPPRESSED_EFFECT_SCREEN_OFF; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 649 | } |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 650 | if (!allowWhenScreenOn) { |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 651 | suppressedVisualEffects |= Policy.SUPPRESSED_EFFECT_SCREEN_ON; |
| 652 | } |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 653 | priorityCallSenders = sourceToPrioritySenders(allowCallsFrom, priorityCallSenders); |
| 654 | priorityMessageSenders = sourceToPrioritySenders(allowMessagesFrom, priorityMessageSenders); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 655 | return new Policy(priorityCategories, priorityCallSenders, priorityMessageSenders, |
| 656 | suppressedVisualEffects); |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | private static int sourceToPrioritySenders(int source, int def) { |
| 660 | switch (source) { |
| 661 | case SOURCE_ANYONE: return Policy.PRIORITY_SENDERS_ANY; |
| 662 | case SOURCE_CONTACT: return Policy.PRIORITY_SENDERS_CONTACTS; |
| 663 | case SOURCE_STAR: return Policy.PRIORITY_SENDERS_STARRED; |
| 664 | default: return def; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 665 | } |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | private static int prioritySendersToSource(int prioritySenders, int def) { |
| 669 | switch (prioritySenders) { |
| 670 | case Policy.PRIORITY_SENDERS_CONTACTS: return SOURCE_CONTACT; |
| 671 | case Policy.PRIORITY_SENDERS_STARRED: return SOURCE_STAR; |
| 672 | case Policy.PRIORITY_SENDERS_ANY: return SOURCE_ANYONE; |
| 673 | default: return def; |
| 674 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | public void applyNotificationPolicy(Policy policy) { |
| 678 | if (policy == null) return; |
| 679 | allowCalls = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_CALLS) != 0; |
| 680 | allowMessages = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_MESSAGES) != 0; |
| 681 | allowEvents = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_EVENTS) != 0; |
| 682 | allowReminders = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_REMINDERS) != 0; |
| 683 | allowRepeatCallers = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_REPEAT_CALLERS) |
| 684 | != 0; |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 685 | allowCallsFrom = prioritySendersToSource(policy.priorityCallSenders, allowCallsFrom); |
| 686 | allowMessagesFrom = prioritySendersToSource(policy.priorityMessageSenders, |
| 687 | allowMessagesFrom); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 688 | if (policy.suppressedVisualEffects != Policy.SUPPRESSED_EFFECTS_UNSET) { |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 689 | allowWhenScreenOff = |
| 690 | (policy.suppressedVisualEffects & Policy.SUPPRESSED_EFFECT_SCREEN_OFF) == 0; |
| 691 | allowWhenScreenOn = |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 692 | (policy.suppressedVisualEffects & Policy.SUPPRESSED_EFFECT_SCREEN_ON) == 0; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 693 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 694 | } |
| 695 | |
Selim Cinek | 9c4a707 | 2014-11-21 17:44:34 +0100 | [diff] [blame] | 696 | public static Condition toTimeCondition(Context context, int minutesFromNow, int userHandle) { |
John Spurlock | bbde267 | 2015-05-13 15:42:04 -0400 | [diff] [blame] | 697 | return toTimeCondition(context, minutesFromNow, userHandle, false /*shortVersion*/); |
| 698 | } |
| 699 | |
| 700 | public static Condition toTimeCondition(Context context, int minutesFromNow, int userHandle, |
| 701 | boolean shortVersion) { |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 702 | final long now = System.currentTimeMillis(); |
| 703 | final long millis = minutesFromNow == 0 ? ZERO_VALUE_MS : minutesFromNow * MINUTES_MS; |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 704 | return toTimeCondition(context, now + millis, minutesFromNow, userHandle, shortVersion); |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 705 | } |
| 706 | |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 707 | public static Condition toTimeCondition(Context context, long time, int minutes, |
John Spurlock | bbde267 | 2015-05-13 15:42:04 -0400 | [diff] [blame] | 708 | int userHandle, boolean shortVersion) { |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 709 | final int num; |
| 710 | String summary, line1, line2; |
Julia Reynolds | 8e2d2bb | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 711 | final CharSequence formattedTime = getFormattedTime(context, time, userHandle); |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 712 | final Resources res = context.getResources(); |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 713 | if (minutes < 60) { |
| 714 | // display as minutes |
| 715 | num = minutes; |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 716 | int summaryResId = shortVersion ? R.plurals.zen_mode_duration_minutes_summary_short |
John Spurlock | bbde267 | 2015-05-13 15:42:04 -0400 | [diff] [blame] | 717 | : R.plurals.zen_mode_duration_minutes_summary; |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 718 | summary = res.getQuantityString(summaryResId, num, num, formattedTime); |
| 719 | int line1ResId = shortVersion ? R.plurals.zen_mode_duration_minutes_short |
John Spurlock | bbde267 | 2015-05-13 15:42:04 -0400 | [diff] [blame] | 720 | : R.plurals.zen_mode_duration_minutes; |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 721 | line1 = res.getQuantityString(line1ResId, num, num, formattedTime); |
| 722 | line2 = res.getString(R.string.zen_mode_until, formattedTime); |
| 723 | } else if (minutes < DAY_MINUTES) { |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 724 | // display as hours |
| 725 | num = Math.round(minutes / 60f); |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 726 | int summaryResId = shortVersion ? R.plurals.zen_mode_duration_hours_summary_short |
John Spurlock | bbde267 | 2015-05-13 15:42:04 -0400 | [diff] [blame] | 727 | : R.plurals.zen_mode_duration_hours_summary; |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 728 | summary = res.getQuantityString(summaryResId, num, num, formattedTime); |
| 729 | int line1ResId = shortVersion ? R.plurals.zen_mode_duration_hours_short |
John Spurlock | bbde267 | 2015-05-13 15:42:04 -0400 | [diff] [blame] | 730 | : R.plurals.zen_mode_duration_hours; |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 731 | line1 = res.getQuantityString(line1ResId, num, num, formattedTime); |
| 732 | line2 = res.getString(R.string.zen_mode_until, formattedTime); |
| 733 | } else { |
| 734 | // display as day/time |
| 735 | summary = line1 = line2 = res.getString(R.string.zen_mode_until, formattedTime); |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 736 | } |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 737 | final Uri id = toCountdownConditionId(time); |
John Spurlock | c90e6fe | 2014-10-28 11:21:42 -0400 | [diff] [blame] | 738 | return new Condition(id, summary, line1, line2, 0, Condition.STATE_TRUE, |
Jason Monk | 4dd8146 | 2014-09-08 10:13:59 -0400 | [diff] [blame] | 739 | Condition.FLAG_RELEVANT_NOW); |
| 740 | } |
| 741 | |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 742 | public static Condition toNextAlarmCondition(Context context, long now, long alarm, |
| 743 | int userHandle) { |
Julia Reynolds | 8e2d2bb | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 744 | final CharSequence formattedTime = getFormattedTime(context, alarm, userHandle); |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 745 | final Resources res = context.getResources(); |
| 746 | final String line1 = res.getString(R.string.zen_mode_alarm, formattedTime); |
| 747 | final Uri id = toCountdownConditionId(alarm); |
| 748 | return new Condition(id, "", line1, "", 0, Condition.STATE_TRUE, |
| 749 | Condition.FLAG_RELEVANT_NOW); |
| 750 | } |
| 751 | |
Julia Reynolds | 8e2d2bb | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 752 | private static CharSequence getFormattedTime(Context context, long time, int userHandle) { |
| 753 | String skeleton = "EEE " + (DateFormat.is24HourFormat(context, userHandle) ? "Hm" : "hma"); |
| 754 | GregorianCalendar now = new GregorianCalendar(); |
| 755 | GregorianCalendar endTime = new GregorianCalendar(); |
| 756 | endTime.setTimeInMillis(time); |
| 757 | if (now.get(Calendar.YEAR) == endTime.get(Calendar.YEAR) |
| 758 | && now.get(Calendar.MONTH) == endTime.get(Calendar.MONTH) |
| 759 | && now.get(Calendar.DATE) == endTime.get(Calendar.DATE)) { |
| 760 | skeleton = DateFormat.is24HourFormat(context, userHandle) ? "Hm" : "hma"; |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 761 | } |
| 762 | final String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton); |
| 763 | return DateFormat.format(pattern, time); |
| 764 | } |
| 765 | |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 766 | // ==== Built-in system conditions ==== |
| 767 | |
John Spurlock | 37bc92c | 2014-11-03 11:01:51 -0500 | [diff] [blame] | 768 | public static final String SYSTEM_AUTHORITY = "android"; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 769 | |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 770 | // ==== Built-in system condition: countdown ==== |
| 771 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 772 | public static final String COUNTDOWN_PATH = "countdown"; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 773 | |
| 774 | public static Uri toCountdownConditionId(long time) { |
| 775 | return new Uri.Builder().scheme(Condition.SCHEME) |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 776 | .authority(SYSTEM_AUTHORITY) |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 777 | .appendPath(COUNTDOWN_PATH) |
| 778 | .appendPath(Long.toString(time)) |
| 779 | .build(); |
| 780 | } |
| 781 | |
| 782 | public static long tryParseCountdownConditionId(Uri conditionId) { |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 783 | if (!Condition.isValidId(conditionId, SYSTEM_AUTHORITY)) return 0; |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 784 | if (conditionId.getPathSegments().size() != 2 |
| 785 | || !COUNTDOWN_PATH.equals(conditionId.getPathSegments().get(0))) return 0; |
| 786 | try { |
| 787 | return Long.parseLong(conditionId.getPathSegments().get(1)); |
| 788 | } catch (RuntimeException e) { |
| 789 | Slog.w(TAG, "Error parsing countdown condition: " + conditionId, e); |
| 790 | return 0; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | public static boolean isValidCountdownConditionId(Uri conditionId) { |
| 795 | return tryParseCountdownConditionId(conditionId) != 0; |
| 796 | } |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 797 | |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 798 | // ==== Built-in system condition: schedule ==== |
| 799 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 800 | public static final String SCHEDULE_PATH = "schedule"; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 801 | |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 802 | public static Uri toScheduleConditionId(ScheduleInfo schedule) { |
| 803 | return new Uri.Builder().scheme(Condition.SCHEME) |
| 804 | .authority(SYSTEM_AUTHORITY) |
| 805 | .appendPath(SCHEDULE_PATH) |
| 806 | .appendQueryParameter("days", toDayList(schedule.days)) |
| 807 | .appendQueryParameter("start", schedule.startHour + "." + schedule.startMinute) |
| 808 | .appendQueryParameter("end", schedule.endHour + "." + schedule.endMinute) |
Julia Reynolds | 1998ee5 | 2016-02-11 13:49:08 -0500 | [diff] [blame] | 809 | .appendQueryParameter("exitAtAlarm", String.valueOf(schedule.exitAtAlarm)) |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 810 | .build(); |
| 811 | } |
| 812 | |
| 813 | public static boolean isValidScheduleConditionId(Uri conditionId) { |
| 814 | return tryParseScheduleConditionId(conditionId) != null; |
| 815 | } |
| 816 | |
| 817 | public static ScheduleInfo tryParseScheduleConditionId(Uri conditionId) { |
| 818 | final boolean isSchedule = conditionId != null |
| 819 | && conditionId.getScheme().equals(Condition.SCHEME) |
| 820 | && conditionId.getAuthority().equals(ZenModeConfig.SYSTEM_AUTHORITY) |
| 821 | && conditionId.getPathSegments().size() == 1 |
| 822 | && conditionId.getPathSegments().get(0).equals(ZenModeConfig.SCHEDULE_PATH); |
| 823 | if (!isSchedule) return null; |
| 824 | final int[] start = tryParseHourAndMinute(conditionId.getQueryParameter("start")); |
| 825 | final int[] end = tryParseHourAndMinute(conditionId.getQueryParameter("end")); |
| 826 | if (start == null || end == null) return null; |
| 827 | final ScheduleInfo rt = new ScheduleInfo(); |
| 828 | rt.days = tryParseDayList(conditionId.getQueryParameter("days"), "\\."); |
| 829 | rt.startHour = start[0]; |
| 830 | rt.startMinute = start[1]; |
| 831 | rt.endHour = end[0]; |
| 832 | rt.endMinute = end[1]; |
Julia Reynolds | 1998ee5 | 2016-02-11 13:49:08 -0500 | [diff] [blame] | 833 | rt.exitAtAlarm = safeBoolean(conditionId.getQueryParameter("exitAtAlarm"), false); |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 834 | return rt; |
| 835 | } |
| 836 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 837 | public static ComponentName getScheduleConditionProvider() { |
| 838 | return new ComponentName(SYSTEM_AUTHORITY, "ScheduleConditionProvider"); |
| 839 | } |
| 840 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 841 | public static class ScheduleInfo { |
| 842 | public int[] days; |
| 843 | public int startHour; |
| 844 | public int startMinute; |
| 845 | public int endHour; |
| 846 | public int endMinute; |
Julia Reynolds | 1998ee5 | 2016-02-11 13:49:08 -0500 | [diff] [blame] | 847 | public boolean exitAtAlarm; |
| 848 | public long nextAlarm; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 849 | |
| 850 | @Override |
| 851 | public int hashCode() { |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | @Override |
| 856 | public boolean equals(Object o) { |
| 857 | if (!(o instanceof ScheduleInfo)) return false; |
| 858 | final ScheduleInfo other = (ScheduleInfo) o; |
| 859 | return toDayList(days).equals(toDayList(other.days)) |
| 860 | && startHour == other.startHour |
| 861 | && startMinute == other.startMinute |
| 862 | && endHour == other.endHour |
Julia Reynolds | 1998ee5 | 2016-02-11 13:49:08 -0500 | [diff] [blame] | 863 | && endMinute == other.endMinute |
| 864 | && exitAtAlarm == other.exitAtAlarm; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | public ScheduleInfo copy() { |
| 868 | final ScheduleInfo rt = new ScheduleInfo(); |
| 869 | if (days != null) { |
| 870 | rt.days = new int[days.length]; |
| 871 | System.arraycopy(days, 0, rt.days, 0, days.length); |
| 872 | } |
| 873 | rt.startHour = startHour; |
| 874 | rt.startMinute = startMinute; |
| 875 | rt.endHour = endHour; |
| 876 | rt.endMinute = endMinute; |
Julia Reynolds | 1998ee5 | 2016-02-11 13:49:08 -0500 | [diff] [blame] | 877 | rt.exitAtAlarm = exitAtAlarm; |
| 878 | rt.nextAlarm = nextAlarm; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 879 | return rt; |
| 880 | } |
| 881 | } |
| 882 | |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 883 | // ==== Built-in system condition: event ==== |
| 884 | |
| 885 | public static final String EVENT_PATH = "event"; |
| 886 | |
| 887 | public static Uri toEventConditionId(EventInfo event) { |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 888 | return new Uri.Builder().scheme(Condition.SCHEME) |
| 889 | .authority(SYSTEM_AUTHORITY) |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 890 | .appendPath(EVENT_PATH) |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 891 | .appendQueryParameter("userId", Long.toString(event.userId)) |
John Spurlock | 995a749 | 2015-05-28 22:13:03 -0400 | [diff] [blame] | 892 | .appendQueryParameter("calendar", event.calendar != null ? event.calendar : "") |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 893 | .appendQueryParameter("reply", Integer.toString(event.reply)) |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 894 | .build(); |
| 895 | } |
| 896 | |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 897 | public static boolean isValidEventConditionId(Uri conditionId) { |
| 898 | return tryParseEventConditionId(conditionId) != null; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 899 | } |
| 900 | |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 901 | public static EventInfo tryParseEventConditionId(Uri conditionId) { |
| 902 | final boolean isEvent = conditionId != null |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 903 | && conditionId.getScheme().equals(Condition.SCHEME) |
| 904 | && conditionId.getAuthority().equals(ZenModeConfig.SYSTEM_AUTHORITY) |
| 905 | && conditionId.getPathSegments().size() == 1 |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 906 | && conditionId.getPathSegments().get(0).equals(EVENT_PATH); |
| 907 | if (!isEvent) return null; |
| 908 | final EventInfo rt = new EventInfo(); |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 909 | rt.userId = tryParseInt(conditionId.getQueryParameter("userId"), UserHandle.USER_NULL); |
John Spurlock | 995a749 | 2015-05-28 22:13:03 -0400 | [diff] [blame] | 910 | rt.calendar = conditionId.getQueryParameter("calendar"); |
| 911 | if (TextUtils.isEmpty(rt.calendar) || tryParseLong(rt.calendar, -1L) != -1L) { |
| 912 | rt.calendar = null; |
| 913 | } |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 914 | rt.reply = tryParseInt(conditionId.getQueryParameter("reply"), 0); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 915 | return rt; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 916 | } |
| 917 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 918 | public static ComponentName getEventConditionProvider() { |
| 919 | return new ComponentName(SYSTEM_AUTHORITY, "EventConditionProvider"); |
| 920 | } |
| 921 | |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 922 | public static class EventInfo { |
John Spurlock | d39af2d | 2015-05-05 09:49:32 -0400 | [diff] [blame] | 923 | public static final int REPLY_ANY_EXCEPT_NO = 0; |
| 924 | public static final int REPLY_YES_OR_MAYBE = 1; |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 925 | public static final int REPLY_YES = 2; |
| 926 | |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 927 | public int userId = UserHandle.USER_NULL; // USER_NULL = unspecified - use current user |
John Spurlock | 995a749 | 2015-05-28 22:13:03 -0400 | [diff] [blame] | 928 | public String calendar; // CalendarContract.Calendars.OWNER_ACCOUNT, or null for any |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 929 | public int reply; |
| 930 | |
| 931 | @Override |
| 932 | public int hashCode() { |
| 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | @Override |
| 937 | public boolean equals(Object o) { |
| 938 | if (!(o instanceof EventInfo)) return false; |
| 939 | final EventInfo other = (EventInfo) o; |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 940 | return userId == other.userId |
John Spurlock | 995a749 | 2015-05-28 22:13:03 -0400 | [diff] [blame] | 941 | && Objects.equals(calendar, other.calendar) |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 942 | && reply == other.reply; |
| 943 | } |
| 944 | |
| 945 | public EventInfo copy() { |
| 946 | final EventInfo rt = new EventInfo(); |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 947 | rt.userId = userId; |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 948 | rt.calendar = calendar; |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 949 | rt.reply = reply; |
| 950 | return rt; |
| 951 | } |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 952 | |
| 953 | public static int resolveUserId(int userId) { |
| 954 | return userId == UserHandle.USER_NULL ? ActivityManager.getCurrentUser() : userId; |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 955 | } |
John Spurlock | d60258f | 2015-04-30 09:30:52 -0400 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | // ==== End built-in system conditions ==== |
| 959 | |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 960 | private static int[] tryParseHourAndMinute(String value) { |
| 961 | if (TextUtils.isEmpty(value)) return null; |
| 962 | final int i = value.indexOf('.'); |
| 963 | if (i < 1 || i >= value.length() - 1) return null; |
| 964 | final int hour = tryParseInt(value.substring(0, i), -1); |
| 965 | final int minute = tryParseInt(value.substring(i + 1), -1); |
| 966 | return isValidHour(hour) && isValidMinute(minute) ? new int[] { hour, minute } : null; |
| 967 | } |
| 968 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 969 | private static int tryParseZenMode(String value, int defValue) { |
| 970 | final int rt = tryParseInt(value, defValue); |
| 971 | return Global.isValidZenMode(rt) ? rt : defValue; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 972 | } |
| 973 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 974 | public static String newRuleId() { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 975 | return UUID.randomUUID().toString().replace("-", ""); |
| 976 | } |
| 977 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 978 | public static String getConditionSummary(Context context, ZenModeConfig config, |
John Spurlock | bbde267 | 2015-05-13 15:42:04 -0400 | [diff] [blame] | 979 | int userHandle, boolean shortVersion) { |
| 980 | return getConditionLine(context, config, userHandle, false /*useLine1*/, shortVersion); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | private static String getConditionLine(Context context, ZenModeConfig config, |
John Spurlock | bbde267 | 2015-05-13 15:42:04 -0400 | [diff] [blame] | 984 | int userHandle, boolean useLine1, boolean shortVersion) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 985 | if (config == null) return ""; |
| 986 | if (config.manualRule != null) { |
| 987 | final Uri id = config.manualRule.conditionId; |
| 988 | if (id == null) { |
| 989 | return context.getString(com.android.internal.R.string.zen_mode_forever); |
| 990 | } |
| 991 | final long time = tryParseCountdownConditionId(id); |
| 992 | Condition c = config.manualRule.condition; |
| 993 | if (time > 0) { |
| 994 | final long now = System.currentTimeMillis(); |
| 995 | final long span = time - now; |
Julia Reynolds | 0842fe8 | 2015-10-01 14:34:22 -0400 | [diff] [blame] | 996 | c = toTimeCondition(context, time, Math.round(span / (float) MINUTES_MS), |
| 997 | userHandle, shortVersion); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 998 | } |
| 999 | final String rt = c == null ? "" : useLine1 ? c.line1 : c.summary; |
| 1000 | return TextUtils.isEmpty(rt) ? "" : rt; |
| 1001 | } |
| 1002 | String summary = ""; |
| 1003 | for (ZenRule automaticRule : config.automaticRules.values()) { |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 1004 | if (automaticRule.isAutomaticActive()) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1005 | if (summary.isEmpty()) { |
| 1006 | summary = automaticRule.name; |
| 1007 | } else { |
| 1008 | summary = context.getResources() |
| 1009 | .getString(R.string.zen_mode_rule_name_combination, summary, |
| 1010 | automaticRule.name); |
| 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | return summary; |
| 1015 | } |
| 1016 | |
| 1017 | public static class ZenRule implements Parcelable { |
| 1018 | public boolean enabled; |
| 1019 | public boolean snoozing; // user manually disabled this instance |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 1020 | public String name; // required for automatic |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1021 | public int zenMode; |
| 1022 | public Uri conditionId; // required for automatic |
| 1023 | public Condition condition; // optional |
| 1024 | public ComponentName component; // optional |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 1025 | public String id; // required for automatic (unique) |
| 1026 | public long creationTime; // required for automatic |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1027 | |
| 1028 | public ZenRule() { } |
| 1029 | |
| 1030 | public ZenRule(Parcel source) { |
| 1031 | enabled = source.readInt() == 1; |
| 1032 | snoozing = source.readInt() == 1; |
| 1033 | if (source.readInt() == 1) { |
| 1034 | name = source.readString(); |
| 1035 | } |
| 1036 | zenMode = source.readInt(); |
| 1037 | conditionId = source.readParcelable(null); |
| 1038 | condition = source.readParcelable(null); |
| 1039 | component = source.readParcelable(null); |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 1040 | if (source.readInt() == 1) { |
| 1041 | id = source.readString(); |
| 1042 | } |
| 1043 | creationTime = source.readLong(); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1044 | } |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 1045 | |
| 1046 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1047 | public int describeContents() { |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 1048 | return 0; |
| 1049 | } |
| 1050 | |
| 1051 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1052 | public void writeToParcel(Parcel dest, int flags) { |
| 1053 | dest.writeInt(enabled ? 1 : 0); |
| 1054 | dest.writeInt(snoozing ? 1 : 0); |
| 1055 | if (name != null) { |
| 1056 | dest.writeInt(1); |
| 1057 | dest.writeString(name); |
| 1058 | } else { |
| 1059 | dest.writeInt(0); |
| 1060 | } |
| 1061 | dest.writeInt(zenMode); |
| 1062 | dest.writeParcelable(conditionId, 0); |
| 1063 | dest.writeParcelable(condition, 0); |
| 1064 | dest.writeParcelable(component, 0); |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 1065 | if (id != null) { |
| 1066 | dest.writeInt(1); |
| 1067 | dest.writeString(id); |
| 1068 | } else { |
| 1069 | dest.writeInt(0); |
| 1070 | } |
| 1071 | dest.writeLong(creationTime); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | @Override |
| 1075 | public String toString() { |
| 1076 | return new StringBuilder(ZenRule.class.getSimpleName()).append('[') |
| 1077 | .append("enabled=").append(enabled) |
| 1078 | .append(",snoozing=").append(snoozing) |
| 1079 | .append(",name=").append(name) |
| 1080 | .append(",zenMode=").append(Global.zenModeToString(zenMode)) |
| 1081 | .append(",conditionId=").append(conditionId) |
| 1082 | .append(",condition=").append(condition) |
| 1083 | .append(",component=").append(component) |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 1084 | .append(",id=").append(id) |
| 1085 | .append(",creationTime=").append(creationTime) |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1086 | .append(']').toString(); |
| 1087 | } |
| 1088 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1089 | private static void appendDiff(Diff d, String item, ZenRule from, ZenRule to) { |
| 1090 | if (d == null) return; |
| 1091 | if (from == null) { |
| 1092 | if (to != null) { |
| 1093 | d.addLine(item, "insert"); |
| 1094 | } |
| 1095 | return; |
| 1096 | } |
| 1097 | from.appendDiff(d, item, to); |
| 1098 | } |
| 1099 | |
| 1100 | private void appendDiff(Diff d, String item, ZenRule to) { |
| 1101 | if (to == null) { |
| 1102 | d.addLine(item, "delete"); |
| 1103 | return; |
| 1104 | } |
| 1105 | if (enabled != to.enabled) { |
| 1106 | d.addLine(item, "enabled", enabled, to.enabled); |
| 1107 | } |
| 1108 | if (snoozing != to.snoozing) { |
| 1109 | d.addLine(item, "snoozing", snoozing, to.snoozing); |
| 1110 | } |
| 1111 | if (!Objects.equals(name, to.name)) { |
| 1112 | d.addLine(item, "name", name, to.name); |
| 1113 | } |
| 1114 | if (zenMode != to.zenMode) { |
| 1115 | d.addLine(item, "zenMode", zenMode, to.zenMode); |
| 1116 | } |
| 1117 | if (!Objects.equals(conditionId, to.conditionId)) { |
| 1118 | d.addLine(item, "conditionId", conditionId, to.conditionId); |
| 1119 | } |
| 1120 | if (!Objects.equals(condition, to.condition)) { |
| 1121 | d.addLine(item, "condition", condition, to.condition); |
| 1122 | } |
| 1123 | if (!Objects.equals(component, to.component)) { |
| 1124 | d.addLine(item, "component", component, to.component); |
| 1125 | } |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 1126 | if (!Objects.equals(id, to.id)) { |
| 1127 | d.addLine(item, "id", id, to.id); |
| 1128 | } |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 1129 | if (creationTime != to.creationTime) { |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 1130 | d.addLine(item, "creationTime", creationTime, to.creationTime); |
| 1131 | } |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1132 | } |
| 1133 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1134 | @Override |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 1135 | public boolean equals(Object o) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1136 | if (!(o instanceof ZenRule)) return false; |
| 1137 | if (o == this) return true; |
| 1138 | final ZenRule other = (ZenRule) o; |
| 1139 | return other.enabled == enabled |
| 1140 | && other.snoozing == snoozing |
| 1141 | && Objects.equals(other.name, name) |
| 1142 | && other.zenMode == zenMode |
| 1143 | && Objects.equals(other.conditionId, conditionId) |
| 1144 | && Objects.equals(other.condition, condition) |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 1145 | && Objects.equals(other.component, component) |
| 1146 | && Objects.equals(other.id, id) |
| 1147 | && other.creationTime == creationTime; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | @Override |
| 1151 | public int hashCode() { |
| 1152 | return Objects.hash(enabled, snoozing, name, zenMode, conditionId, condition, |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 1153 | component, id, creationTime); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1154 | } |
| 1155 | |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 1156 | public boolean isAutomaticActive() { |
| 1157 | return enabled && !snoozing && component != null && isTrueOrUnknown(); |
| 1158 | } |
| 1159 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1160 | public boolean isTrueOrUnknown() { |
John Spurlock | 39581cc | 2015-04-10 11:59:01 -0400 | [diff] [blame] | 1161 | return condition != null && (condition.state == Condition.STATE_TRUE |
| 1162 | || condition.state == Condition.STATE_UNKNOWN); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | public static final Parcelable.Creator<ZenRule> CREATOR |
| 1166 | = new Parcelable.Creator<ZenRule>() { |
| 1167 | @Override |
| 1168 | public ZenRule createFromParcel(Parcel source) { |
| 1169 | return new ZenRule(source); |
| 1170 | } |
| 1171 | @Override |
| 1172 | public ZenRule[] newArray(int size) { |
| 1173 | return new ZenRule[size]; |
| 1174 | } |
| 1175 | }; |
| 1176 | } |
| 1177 | |
| 1178 | // Legacy config |
| 1179 | public static final class XmlV1 { |
| 1180 | public static final String SLEEP_MODE_NIGHTS = "nights"; |
| 1181 | public static final String SLEEP_MODE_WEEKNIGHTS = "weeknights"; |
| 1182 | public static final String SLEEP_MODE_DAYS_PREFIX = "days:"; |
| 1183 | |
| 1184 | private static final String EXIT_CONDITION_TAG = "exitCondition"; |
| 1185 | private static final String EXIT_CONDITION_ATT_COMPONENT = "component"; |
| 1186 | private static final String SLEEP_TAG = "sleep"; |
| 1187 | private static final String SLEEP_ATT_MODE = "mode"; |
| 1188 | private static final String SLEEP_ATT_NONE = "none"; |
| 1189 | |
| 1190 | private static final String SLEEP_ATT_START_HR = "startHour"; |
| 1191 | private static final String SLEEP_ATT_START_MIN = "startMin"; |
| 1192 | private static final String SLEEP_ATT_END_HR = "endHour"; |
| 1193 | private static final String SLEEP_ATT_END_MIN = "endMin"; |
| 1194 | |
| 1195 | public boolean allowCalls; |
| 1196 | public boolean allowMessages; |
| 1197 | public boolean allowReminders = DEFAULT_ALLOW_REMINDERS; |
| 1198 | public boolean allowEvents = DEFAULT_ALLOW_EVENTS; |
| 1199 | public int allowFrom = SOURCE_ANYONE; |
| 1200 | |
| 1201 | public String sleepMode; // nights, weeknights, days:1,2,3 Calendar.days |
| 1202 | public int sleepStartHour; // 0-23 |
| 1203 | public int sleepStartMinute; // 0-59 |
| 1204 | public int sleepEndHour; |
| 1205 | public int sleepEndMinute; |
| 1206 | public boolean sleepNone; // false = priority, true = none |
| 1207 | public ComponentName[] conditionComponents; |
| 1208 | public Uri[] conditionIds; |
| 1209 | public Condition exitCondition; // manual exit condition |
| 1210 | public ComponentName exitConditionComponent; // manual exit condition component |
| 1211 | |
| 1212 | private static boolean isValidSleepMode(String sleepMode) { |
| 1213 | return sleepMode == null || sleepMode.equals(SLEEP_MODE_NIGHTS) |
| 1214 | || sleepMode.equals(SLEEP_MODE_WEEKNIGHTS) || tryParseDays(sleepMode) != null; |
| 1215 | } |
| 1216 | |
| 1217 | public static int[] tryParseDays(String sleepMode) { |
| 1218 | if (sleepMode == null) return null; |
| 1219 | sleepMode = sleepMode.trim(); |
| 1220 | if (SLEEP_MODE_NIGHTS.equals(sleepMode)) return ALL_DAYS; |
| 1221 | if (SLEEP_MODE_WEEKNIGHTS.equals(sleepMode)) return WEEKNIGHT_DAYS; |
| 1222 | if (!sleepMode.startsWith(SLEEP_MODE_DAYS_PREFIX)) return null; |
| 1223 | if (sleepMode.equals(SLEEP_MODE_DAYS_PREFIX)) return null; |
| 1224 | return tryParseDayList(sleepMode.substring(SLEEP_MODE_DAYS_PREFIX.length()), ","); |
| 1225 | } |
| 1226 | |
| 1227 | public static XmlV1 readXml(XmlPullParser parser) |
| 1228 | throws XmlPullParserException, IOException { |
| 1229 | int type; |
| 1230 | String tag; |
| 1231 | XmlV1 rt = new XmlV1(); |
| 1232 | final ArrayList<ComponentName> conditionComponents = new ArrayList<ComponentName>(); |
| 1233 | final ArrayList<Uri> conditionIds = new ArrayList<Uri>(); |
| 1234 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 1235 | tag = parser.getName(); |
| 1236 | if (type == XmlPullParser.END_TAG && ZEN_TAG.equals(tag)) { |
| 1237 | if (!conditionComponents.isEmpty()) { |
| 1238 | rt.conditionComponents = conditionComponents |
| 1239 | .toArray(new ComponentName[conditionComponents.size()]); |
| 1240 | rt.conditionIds = conditionIds.toArray(new Uri[conditionIds.size()]); |
| 1241 | } |
| 1242 | return rt; |
| 1243 | } |
| 1244 | if (type == XmlPullParser.START_TAG) { |
| 1245 | if (ALLOW_TAG.equals(tag)) { |
| 1246 | rt.allowCalls = safeBoolean(parser, ALLOW_ATT_CALLS, false); |
| 1247 | rt.allowMessages = safeBoolean(parser, ALLOW_ATT_MESSAGES, false); |
| 1248 | rt.allowReminders = safeBoolean(parser, ALLOW_ATT_REMINDERS, |
| 1249 | DEFAULT_ALLOW_REMINDERS); |
| 1250 | rt.allowEvents = safeBoolean(parser, ALLOW_ATT_EVENTS, |
| 1251 | DEFAULT_ALLOW_EVENTS); |
| 1252 | rt.allowFrom = safeInt(parser, ALLOW_ATT_FROM, SOURCE_ANYONE); |
| 1253 | if (rt.allowFrom < SOURCE_ANYONE || rt.allowFrom > MAX_SOURCE) { |
| 1254 | throw new IndexOutOfBoundsException("bad source in config:" |
| 1255 | + rt.allowFrom); |
| 1256 | } |
| 1257 | } else if (SLEEP_TAG.equals(tag)) { |
| 1258 | final String mode = parser.getAttributeValue(null, SLEEP_ATT_MODE); |
| 1259 | rt.sleepMode = isValidSleepMode(mode)? mode : null; |
| 1260 | rt.sleepNone = safeBoolean(parser, SLEEP_ATT_NONE, false); |
| 1261 | final int startHour = safeInt(parser, SLEEP_ATT_START_HR, 0); |
| 1262 | final int startMinute = safeInt(parser, SLEEP_ATT_START_MIN, 0); |
| 1263 | final int endHour = safeInt(parser, SLEEP_ATT_END_HR, 0); |
| 1264 | final int endMinute = safeInt(parser, SLEEP_ATT_END_MIN, 0); |
| 1265 | rt.sleepStartHour = isValidHour(startHour) ? startHour : 0; |
| 1266 | rt.sleepStartMinute = isValidMinute(startMinute) ? startMinute : 0; |
| 1267 | rt.sleepEndHour = isValidHour(endHour) ? endHour : 0; |
| 1268 | rt.sleepEndMinute = isValidMinute(endMinute) ? endMinute : 0; |
| 1269 | } else if (CONDITION_TAG.equals(tag)) { |
| 1270 | final ComponentName component = |
| 1271 | safeComponentName(parser, CONDITION_ATT_COMPONENT); |
| 1272 | final Uri conditionId = safeUri(parser, CONDITION_ATT_ID); |
| 1273 | if (component != null && conditionId != null) { |
| 1274 | conditionComponents.add(component); |
| 1275 | conditionIds.add(conditionId); |
| 1276 | } |
| 1277 | } else if (EXIT_CONDITION_TAG.equals(tag)) { |
| 1278 | rt.exitCondition = readConditionXml(parser); |
| 1279 | if (rt.exitCondition != null) { |
| 1280 | rt.exitConditionComponent = |
| 1281 | safeComponentName(parser, EXIT_CONDITION_ATT_COMPONENT); |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | } |
| 1286 | throw new IllegalStateException("Failed to reach END_DOCUMENT"); |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 1287 | } |
| 1288 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 1289 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1290 | public interface Migration { |
| 1291 | ZenModeConfig migrate(XmlV1 v1); |
| 1292 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1293 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1294 | public static class Diff { |
| 1295 | private final ArrayList<String> lines = new ArrayList<>(); |
| 1296 | |
| 1297 | @Override |
| 1298 | public String toString() { |
| 1299 | final StringBuilder sb = new StringBuilder("Diff["); |
| 1300 | final int N = lines.size(); |
| 1301 | for (int i = 0; i < N; i++) { |
| 1302 | if (i > 0) { |
| 1303 | sb.append(','); |
| 1304 | } |
| 1305 | sb.append(lines.get(i)); |
| 1306 | } |
| 1307 | return sb.append(']').toString(); |
| 1308 | } |
| 1309 | |
| 1310 | private Diff addLine(String item, String action) { |
| 1311 | lines.add(item + ":" + action); |
| 1312 | return this; |
| 1313 | } |
| 1314 | |
| 1315 | public Diff addLine(String item, String subitem, Object from, Object to) { |
| 1316 | return addLine(item + "." + subitem, from, to); |
| 1317 | } |
| 1318 | |
| 1319 | public Diff addLine(String item, Object from, Object to) { |
| 1320 | return addLine(item, from + "->" + to); |
| 1321 | } |
| 1322 | } |
| 1323 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1324 | } |