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