John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2014, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.notification; |
| 18 | |
John Spurlock | 8403b75 | 2014-12-10 12:47:01 -0500 | [diff] [blame] | 19 | import static android.media.AudioAttributes.USAGE_NOTIFICATION; |
Jean-Michel Trivi | 89c3b29 | 2014-07-20 11:41:02 -0700 | [diff] [blame] | 20 | import static android.media.AudioAttributes.USAGE_NOTIFICATION_RINGTONE; |
Julia Reynolds | 3fe81be | 2016-02-03 09:10:06 -0500 | [diff] [blame] | 21 | import static android.media.AudioAttributes.USAGE_UNKNOWN; |
| 22 | import static android.media.AudioAttributes.USAGE_VIRTUAL_SOURCE; |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 23 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 24 | import android.app.AppOpsManager; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 25 | import android.app.AutomaticZenRule; |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 26 | import android.app.NotificationManager; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 27 | import android.app.NotificationManager.Policy; |
John Spurlock | 312d1d0 | 2014-07-08 10:24:57 -0400 | [diff] [blame] | 28 | import android.content.ComponentName; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 29 | import android.content.ContentResolver; |
| 30 | import android.content.Context; |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 31 | import android.content.Intent; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 33 | import android.content.pm.ResolveInfo; |
| 34 | import android.content.pm.ServiceInfo; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 35 | import android.content.res.Resources; |
| 36 | import android.content.res.XmlResourceParser; |
| 37 | import android.database.ContentObserver; |
| 38 | import android.media.AudioManager; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 39 | import android.media.AudioManagerInternal; |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 40 | import android.media.AudioSystem; |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 41 | import android.media.VolumePolicy; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 42 | import android.net.Uri; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 43 | import android.os.Binder; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 44 | import android.os.Bundle; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 45 | import android.os.Handler; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 46 | import android.os.Looper; |
| 47 | import android.os.Message; |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 48 | import android.os.Process; |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 49 | import android.os.SystemClock; |
John Spurlock | b5e767b | 2014-07-27 11:53:20 -0400 | [diff] [blame] | 50 | import android.os.UserHandle; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 51 | import android.provider.Settings.Global; |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 52 | import android.service.notification.ConditionProviderService; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 53 | import android.service.notification.ZenModeConfig; |
John Spurlock | cb9aa20 | 2015-05-08 17:35:22 -0400 | [diff] [blame] | 54 | import android.service.notification.ZenModeConfig.EventInfo; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 55 | import android.service.notification.ZenModeConfig.ScheduleInfo; |
| 56 | import android.service.notification.ZenModeConfig.ZenRule; |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 57 | import android.text.TextUtils; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 58 | import android.util.Log; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 59 | import android.util.SparseArray; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 60 | |
| 61 | import com.android.internal.R; |
John Spurlock | 25d01ee | 2015-06-03 12:17:46 -0400 | [diff] [blame] | 62 | import com.android.internal.logging.MetricsLogger; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 63 | import com.android.server.LocalServices; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 64 | |
| 65 | import libcore.io.IoUtils; |
| 66 | |
| 67 | import org.xmlpull.v1.XmlPullParser; |
| 68 | import org.xmlpull.v1.XmlPullParserException; |
| 69 | import org.xmlpull.v1.XmlSerializer; |
| 70 | |
| 71 | import java.io.IOException; |
| 72 | import java.io.PrintWriter; |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 73 | import java.util.ArrayList; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 74 | import java.util.List; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 75 | import java.util.Objects; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * NotificationManagerService helper for functionality related to zen mode. |
| 79 | */ |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 80 | public class ZenModeHelper { |
| 81 | static final String TAG = "ZenModeHelper"; |
| 82 | static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 83 | |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 84 | // The amount of time rules instances can exist without their owning app being installed. |
| 85 | private static final int RULE_INSTANCE_GRACE_PERIOD = 1000 * 60 * 60 * 72; |
| 86 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 87 | private final Context mContext; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 88 | private final H mHandler; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 89 | private final SettingsObserver mSettingsObserver; |
| 90 | private final AppOpsManager mAppOps; |
| 91 | private final ZenModeConfig mDefaultConfig; |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 92 | private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>(); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 93 | private final ZenModeFiltering mFiltering; |
| 94 | private final RingerModeDelegate mRingerModeDelegate = new RingerModeDelegate(); |
| 95 | private final ZenModeConditions mConditions; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 96 | private final SparseArray<ZenModeConfig> mConfigs = new SparseArray<>(); |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 97 | private final Metrics mMetrics = new Metrics(); |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 98 | private final ConditionProviders.Config mServiceConfig; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 99 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 100 | private int mZenMode; |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 101 | private int mUser = UserHandle.USER_SYSTEM; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 102 | private ZenModeConfig mConfig; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 103 | private AudioManagerInternal mAudioManager; |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 104 | private PackageManager mPm; |
John Spurlock | 8403b75 | 2014-12-10 12:47:01 -0500 | [diff] [blame] | 105 | private boolean mEffectsSuppressed; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 106 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 107 | public ZenModeHelper(Context context, Looper looper, ConditionProviders conditionProviders) { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 108 | mContext = context; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 109 | mHandler = new H(looper); |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 110 | addCallback(mMetrics); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 111 | mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); |
| 112 | mDefaultConfig = readDefaultConfig(context.getResources()); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 113 | appendDefaultScheduleRules(mDefaultConfig); |
John Spurlock | cb9aa20 | 2015-05-08 17:35:22 -0400 | [diff] [blame] | 114 | appendDefaultEventRules(mDefaultConfig); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 115 | mConfig = mDefaultConfig; |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 116 | mConfigs.put(UserHandle.USER_SYSTEM, mConfig); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 117 | mSettingsObserver = new SettingsObserver(mHandler); |
| 118 | mSettingsObserver.observe(); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 119 | mFiltering = new ZenModeFiltering(mContext); |
| 120 | mConditions = new ZenModeConditions(this, conditionProviders); |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 121 | mServiceConfig = conditionProviders.getConfig(); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 122 | } |
| 123 | |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 124 | public Looper getLooper() { |
| 125 | return mHandler.getLooper(); |
| 126 | } |
| 127 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 128 | @Override |
| 129 | public String toString() { |
| 130 | return TAG; |
| 131 | } |
| 132 | |
| 133 | public boolean matchesCallFilter(UserHandle userHandle, Bundle extras, |
| 134 | ValidateNotificationPeople validator, int contactsTimeoutMs, float timeoutAffinity) { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 135 | synchronized (mConfig) { |
| 136 | return ZenModeFiltering.matchesCallFilter(mContext, mZenMode, mConfig, userHandle, |
| 137 | extras, |
| 138 | validator, contactsTimeoutMs, timeoutAffinity); |
| 139 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | public boolean isCall(NotificationRecord record) { |
| 143 | return mFiltering.isCall(record); |
| 144 | } |
| 145 | |
| 146 | public boolean shouldIntercept(NotificationRecord record) { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 147 | synchronized (mConfig) { |
| 148 | return mFiltering.shouldIntercept(mZenMode, mConfig, record); |
| 149 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 152 | public boolean shouldSuppressWhenScreenOff() { |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 153 | synchronized (mConfig) { |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 154 | return !mConfig.allowWhenScreenOff; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 158 | public boolean shouldSuppressWhenScreenOn() { |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 159 | synchronized (mConfig) { |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 160 | return !mConfig.allowWhenScreenOn; |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 164 | public void addCallback(Callback callback) { |
| 165 | mCallbacks.add(callback); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 166 | } |
| 167 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 168 | public void removeCallback(Callback callback) { |
| 169 | mCallbacks.remove(callback); |
| 170 | } |
| 171 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 172 | public void initZenMode() { |
| 173 | if (DEBUG) Log.d(TAG, "initZenMode"); |
| 174 | evaluateZenMode("init", true /*setRingerMode*/); |
| 175 | } |
| 176 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 177 | public void onSystemReady() { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 178 | if (DEBUG) Log.d(TAG, "onSystemReady"); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 179 | mAudioManager = LocalServices.getService(AudioManagerInternal.class); |
| 180 | if (mAudioManager != null) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 181 | mAudioManager.setRingerModeDelegate(mRingerModeDelegate); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 182 | } |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 183 | mPm = mContext.getPackageManager(); |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 184 | mHandler.postMetricsTimer(); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 185 | cleanUpZenRules(); |
Julia Reynolds | 8ac6303 | 2015-08-31 15:19:43 -0400 | [diff] [blame] | 186 | evaluateZenMode("onSystemReady", true); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 187 | } |
| 188 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 189 | public void onUserSwitched(int user) { |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 190 | loadConfigForUser(user, "onUserSwitched"); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | public void onUserRemoved(int user) { |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 194 | if (user < UserHandle.USER_SYSTEM) return; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 195 | if (DEBUG) Log.d(TAG, "onUserRemoved u=" + user); |
| 196 | mConfigs.remove(user); |
| 197 | } |
| 198 | |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 199 | public void onUserUnlocked(int user) { |
| 200 | loadConfigForUser(user, "onUserUnlocked"); |
| 201 | } |
| 202 | |
| 203 | private void loadConfigForUser(int user, String reason) { |
| 204 | if (mUser == user || user < UserHandle.USER_SYSTEM) return; |
| 205 | mUser = user; |
| 206 | if (DEBUG) Log.d(TAG, reason + " u=" + user); |
| 207 | ZenModeConfig config = mConfigs.get(user); |
| 208 | if (config == null) { |
| 209 | if (DEBUG) Log.d(TAG, reason + " generating default config for user " + user); |
| 210 | config = mDefaultConfig.copy(); |
| 211 | config.user = user; |
| 212 | } |
| 213 | synchronized (mConfig) { |
| 214 | setConfigLocked(config, reason); |
| 215 | } |
| 216 | cleanUpZenRules(); |
| 217 | } |
| 218 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 219 | public int getZenModeListenerInterruptionFilter() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 220 | return NotificationManager.zenModeToInterruptionFilter(mZenMode); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 221 | } |
| 222 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 223 | public void requestFromListener(ComponentName name, int filter) { |
| 224 | final int newZen = NotificationManager.zenModeFromInterruptionFilter(filter, -1); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 225 | if (newZen != -1) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 226 | setManualZenMode(newZen, null, |
| 227 | "listener:" + (name != null ? name.flattenToShortString() : null)); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
John Spurlock | 8403b75 | 2014-12-10 12:47:01 -0500 | [diff] [blame] | 231 | public void setEffectsSuppressed(boolean effectsSuppressed) { |
| 232 | if (mEffectsSuppressed == effectsSuppressed) return; |
| 233 | mEffectsSuppressed = effectsSuppressed; |
| 234 | applyRestrictions(); |
| 235 | } |
| 236 | |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 237 | public int getZenMode() { |
| 238 | return mZenMode; |
| 239 | } |
| 240 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 241 | public List<ZenRule> getZenRules() { |
| 242 | List<ZenRule> rules = new ArrayList<>(); |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 243 | synchronized (mConfig) { |
| 244 | if (mConfig == null) return rules; |
| 245 | for (ZenRule rule : mConfig.automaticRules.values()) { |
| 246 | if (canManageAutomaticZenRule(rule)) { |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 247 | rules.add(rule); |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 248 | } |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | return rules; |
| 252 | } |
| 253 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 254 | public AutomaticZenRule getAutomaticZenRule(String id) { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 255 | ZenRule rule; |
| 256 | synchronized (mConfig) { |
| 257 | if (mConfig == null) return null; |
| 258 | rule = mConfig.automaticRules.get(id); |
| 259 | } |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 260 | if (rule == null) return null; |
| 261 | if (canManageAutomaticZenRule(rule)) { |
| 262 | return createAutomaticZenRule(rule); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 263 | } |
| 264 | return null; |
| 265 | } |
| 266 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 267 | public String addAutomaticZenRule(AutomaticZenRule automaticZenRule, String reason) { |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 268 | if (!isSystemRule(automaticZenRule)) { |
| 269 | ServiceInfo owner = getServiceInfo(automaticZenRule.getOwner()); |
| 270 | if (owner == null) { |
| 271 | throw new IllegalArgumentException("Owner is not a condition provider service"); |
| 272 | } |
| 273 | |
Julia Reynolds | 7f2f441 | 2016-03-01 12:33:48 -0500 | [diff] [blame] | 274 | int ruleInstanceLimit = -1; |
| 275 | if (owner.metaData != null) { |
| 276 | ruleInstanceLimit = owner.metaData.getInt( |
| 277 | ConditionProviderService.META_DATA_RULE_INSTANCE_LIMIT, -1); |
| 278 | } |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 279 | if (ruleInstanceLimit > 0 && ruleInstanceLimit |
| 280 | < (getCurrentInstanceCount(automaticZenRule.getOwner()) + 1)) { |
| 281 | throw new IllegalArgumentException("Rule instance limit exceeded"); |
| 282 | } |
| 283 | } |
| 284 | |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 285 | ZenModeConfig newConfig; |
| 286 | synchronized (mConfig) { |
| 287 | if (mConfig == null) return null; |
| 288 | if (DEBUG) { |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 289 | Log.d(TAG, "addAutomaticZenRule rule= " + automaticZenRule + " reason=" + reason); |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 290 | } |
| 291 | newConfig = mConfig.copy(); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 292 | ZenRule rule = new ZenRule(); |
| 293 | populateZenRule(automaticZenRule, rule, true); |
| 294 | newConfig.automaticRules.put(rule.id, rule); |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 295 | if (setConfigLocked(newConfig, reason, true)) { |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 296 | return rule.id; |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 297 | } else { |
| 298 | return null; |
| 299 | } |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 303 | public boolean updateAutomaticZenRule(String ruleId, AutomaticZenRule automaticZenRule, |
| 304 | String reason) { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 305 | ZenModeConfig newConfig; |
| 306 | synchronized (mConfig) { |
| 307 | if (mConfig == null) return false; |
| 308 | if (DEBUG) { |
| 309 | Log.d(TAG, "updateAutomaticZenRule zenRule=" + automaticZenRule |
| 310 | + " reason=" + reason); |
| 311 | } |
| 312 | newConfig = mConfig.copy(); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 313 | ZenModeConfig.ZenRule rule; |
| 314 | if (ruleId == null) { |
| 315 | throw new IllegalArgumentException("Rule doesn't exist"); |
| 316 | } else { |
| 317 | rule = newConfig.automaticRules.get(ruleId); |
| 318 | if (rule == null || !canManageAutomaticZenRule(rule)) { |
| 319 | throw new SecurityException( |
| 320 | "Cannot update rules not owned by your condition provider"); |
| 321 | } |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 322 | } |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 323 | populateZenRule(automaticZenRule, rule, false); |
| 324 | newConfig.automaticRules.put(ruleId, rule); |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 325 | return setConfigLocked(newConfig, reason, true); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 326 | } |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 327 | } |
| 328 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 329 | public boolean removeAutomaticZenRule(String id, String reason) { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 330 | ZenModeConfig newConfig; |
| 331 | synchronized (mConfig) { |
| 332 | if (mConfig == null) return false; |
| 333 | newConfig = mConfig.copy(); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 334 | ZenRule rule = newConfig.automaticRules.get(id); |
| 335 | if (rule == null) return false; |
| 336 | if (canManageAutomaticZenRule(rule)) { |
| 337 | newConfig.automaticRules.remove(id); |
| 338 | if (DEBUG) Log.d(TAG, "removeZenRule zenRule=" + id + " reason=" + reason); |
| 339 | } else { |
| 340 | throw new SecurityException( |
| 341 | "Cannot delete rules not owned by your condition provider"); |
| 342 | } |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 343 | return setConfigLocked(newConfig, reason, true); |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 344 | } |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 345 | } |
| 346 | |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 347 | public boolean removeAutomaticZenRules(String packageName, String reason) { |
| 348 | ZenModeConfig newConfig; |
| 349 | synchronized (mConfig) { |
| 350 | if (mConfig == null) return false; |
| 351 | newConfig = mConfig.copy(); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 352 | for (int i = newConfig.automaticRules.size() - 1; i >= 0; i--) { |
| 353 | ZenRule rule = newConfig.automaticRules.get(newConfig.automaticRules.keyAt(i)); |
| 354 | if (rule.component.getPackageName().equals(packageName) |
| 355 | && canManageAutomaticZenRule(rule)) { |
| 356 | newConfig.automaticRules.removeAt(i); |
| 357 | } |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 358 | } |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 359 | return setConfigLocked(newConfig, reason, true); |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 360 | } |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 361 | } |
| 362 | |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 363 | public int getCurrentInstanceCount(ComponentName owner) { |
| 364 | int count = 0; |
| 365 | synchronized (mConfig) { |
| 366 | for (ZenRule rule : mConfig.automaticRules.values()) { |
| 367 | if (rule.component != null && rule.component.equals(owner)) { |
| 368 | count++; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | return count; |
| 373 | } |
| 374 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 375 | public boolean canManageAutomaticZenRule(ZenRule rule) { |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 376 | final int callingUid = Binder.getCallingUid(); |
| 377 | if (callingUid == 0 || callingUid == Process.SYSTEM_UID) { |
| 378 | return true; |
| 379 | } else if (mContext.checkCallingPermission(android.Manifest.permission.MANAGE_NOTIFICATIONS) |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 380 | == PackageManager.PERMISSION_GRANTED) { |
| 381 | return true; |
| 382 | } else { |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 383 | String[] packages = mPm.getPackagesForUid(Binder.getCallingUid()); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 384 | if (packages != null) { |
| 385 | final int packageCount = packages.length; |
| 386 | for (int i = 0; i < packageCount; i++) { |
| 387 | if (packages[i].equals(rule.component.getPackageName())) { |
| 388 | return true; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | return false; |
| 393 | } |
| 394 | } |
| 395 | |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 396 | private boolean isSystemRule(AutomaticZenRule rule) { |
| 397 | return ZenModeConfig.SYSTEM_AUTHORITY.equals(rule.getOwner().getPackageName()); |
| 398 | } |
| 399 | |
| 400 | private ServiceInfo getServiceInfo(ComponentName owner) { |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 401 | Intent queryIntent = new Intent(); |
| 402 | queryIntent.setComponent(owner); |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 403 | List<ResolveInfo> installedServices = mPm.queryIntentServicesAsUser( |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 404 | queryIntent, |
| 405 | PackageManager.GET_SERVICES | PackageManager.GET_META_DATA, |
| 406 | UserHandle.getCallingUserId()); |
| 407 | if (installedServices != null) { |
| 408 | for (int i = 0, count = installedServices.size(); i < count; i++) { |
| 409 | ResolveInfo resolveInfo = installedServices.get(i); |
| 410 | ServiceInfo info = resolveInfo.serviceInfo; |
| 411 | if (mServiceConfig.bindPermission.equals(info.permission)) { |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 412 | return info; |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | } |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 416 | return null; |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 417 | } |
| 418 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 419 | private void populateZenRule(AutomaticZenRule automaticZenRule, ZenRule rule, boolean isNew) { |
| 420 | if (isNew) { |
| 421 | rule.id = ZenModeConfig.newRuleId(); |
| 422 | rule.creationTime = System.currentTimeMillis(); |
| 423 | rule.component = automaticZenRule.getOwner(); |
| 424 | } |
| 425 | |
| 426 | if (rule.enabled != automaticZenRule.isEnabled()) { |
| 427 | rule.snoozing = false; |
| 428 | } |
| 429 | rule.name = automaticZenRule.getName(); |
| 430 | rule.condition = null; |
| 431 | rule.conditionId = automaticZenRule.getConditionId(); |
| 432 | rule.enabled = automaticZenRule.isEnabled(); |
| 433 | rule.zenMode = NotificationManager.zenModeFromInterruptionFilter( |
| 434 | automaticZenRule.getInterruptionFilter(), Global.ZEN_MODE_OFF); |
| 435 | } |
| 436 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 437 | private AutomaticZenRule createAutomaticZenRule(ZenRule rule) { |
| 438 | return new AutomaticZenRule(rule.name, rule.component, rule.conditionId, |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 439 | NotificationManager.zenModeToInterruptionFilter(rule.zenMode), rule.enabled, |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 440 | rule.creationTime); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 441 | } |
| 442 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 443 | public void setManualZenMode(int zenMode, Uri conditionId, String reason) { |
| 444 | setManualZenMode(zenMode, conditionId, reason, true /*setRingerMode*/); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 445 | } |
| 446 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 447 | private void setManualZenMode(int zenMode, Uri conditionId, String reason, |
| 448 | boolean setRingerMode) { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 449 | ZenModeConfig newConfig; |
| 450 | synchronized (mConfig) { |
| 451 | if (mConfig == null) return; |
| 452 | if (!Global.isValidZenMode(zenMode)) return; |
| 453 | if (DEBUG) Log.d(TAG, "setManualZenMode " + Global.zenModeToString(zenMode) |
| 454 | + " conditionId=" + conditionId + " reason=" + reason |
| 455 | + " setRingerMode=" + setRingerMode); |
| 456 | newConfig = mConfig.copy(); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 457 | if (zenMode == Global.ZEN_MODE_OFF) { |
| 458 | newConfig.manualRule = null; |
| 459 | for (ZenRule automaticRule : newConfig.automaticRules.values()) { |
| 460 | if (automaticRule.isAutomaticActive()) { |
| 461 | automaticRule.snoozing = true; |
| 462 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 463 | } |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 464 | } else { |
| 465 | final ZenRule newRule = new ZenRule(); |
| 466 | newRule.enabled = true; |
| 467 | newRule.zenMode = zenMode; |
| 468 | newRule.conditionId = conditionId; |
| 469 | newConfig.manualRule = newRule; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 470 | } |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 471 | setConfigLocked(newConfig, reason, setRingerMode); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 472 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | public void dump(PrintWriter pw, String prefix) { |
| 476 | pw.print(prefix); pw.print("mZenMode="); |
| 477 | pw.println(Global.zenModeToString(mZenMode)); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 478 | dump(pw, prefix, "mDefaultConfig", mDefaultConfig); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 479 | final int N = mConfigs.size(); |
| 480 | for (int i = 0; i < N; i++) { |
| 481 | dump(pw, prefix, "mConfigs[u=" + mConfigs.keyAt(i) + "]", mConfigs.valueAt(i)); |
| 482 | } |
| 483 | pw.print(prefix); pw.print("mUser="); pw.println(mUser); |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 484 | synchronized (mConfig) { |
| 485 | dump(pw, prefix, "mConfig", mConfig); |
| 486 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 487 | pw.print(prefix); pw.print("mEffectsSuppressed="); pw.println(mEffectsSuppressed); |
John Spurlock | 1d7d224 | 2015-04-10 08:10:22 -0400 | [diff] [blame] | 488 | mFiltering.dump(pw, prefix); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 489 | mConditions.dump(pw, prefix); |
| 490 | } |
| 491 | |
| 492 | private static void dump(PrintWriter pw, String prefix, String var, ZenModeConfig config) { |
| 493 | pw.print(prefix); pw.print(var); pw.print('='); |
| 494 | if (config == null) { |
| 495 | pw.println(config); |
| 496 | return; |
| 497 | } |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 498 | pw.printf("allow(calls=%s,callsFrom=%s,repeatCallers=%s,messages=%s,messagesFrom=%s," |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 499 | + "events=%s,reminders=%s,whenScreenOff,whenScreenOn=%s)\n", |
Julia Reynolds | 6ee2617 | 2015-09-28 11:34:48 -0400 | [diff] [blame] | 500 | config.allowCalls, ZenModeConfig.sourceToString(config.allowCallsFrom), |
| 501 | config.allowRepeatCallers, config.allowMessages, |
| 502 | ZenModeConfig.sourceToString(config.allowMessagesFrom), |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 503 | config.allowEvents, config.allowReminders, config.allowWhenScreenOff, |
| 504 | config.allowWhenScreenOn); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 505 | pw.print(prefix); pw.print(" manualRule="); pw.println(config.manualRule); |
| 506 | if (config.automaticRules.isEmpty()) return; |
| 507 | final int N = config.automaticRules.size(); |
| 508 | for (int i = 0; i < N; i++) { |
| 509 | pw.print(prefix); pw.print(i == 0 ? " automaticRules=" : " "); |
| 510 | pw.println(config.automaticRules.valueAt(i)); |
| 511 | } |
| 512 | } |
| 513 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 514 | public void readXml(XmlPullParser parser, boolean forRestore) |
| 515 | throws XmlPullParserException, IOException { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 516 | final ZenModeConfig config = ZenModeConfig.readXml(parser, mConfigMigration); |
| 517 | if (config != null) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 518 | if (forRestore) { |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 519 | //TODO: http://b/22388012 |
| 520 | if (config.user != UserHandle.USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 521 | return; |
| 522 | } |
| 523 | config.manualRule = null; // don't restore the manual rule |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 524 | long time = System.currentTimeMillis(); |
John Spurlock | 995a749 | 2015-05-28 22:13:03 -0400 | [diff] [blame] | 525 | if (config.automaticRules != null) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 526 | for (ZenRule automaticRule : config.automaticRules.values()) { |
John Spurlock | 995a749 | 2015-05-28 22:13:03 -0400 | [diff] [blame] | 527 | // don't restore transient state from restored automatic rules |
| 528 | automaticRule.snoozing = false; |
| 529 | automaticRule.condition = null; |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 530 | automaticRule.creationTime = time; |
John Spurlock | 995a749 | 2015-05-28 22:13:03 -0400 | [diff] [blame] | 531 | } |
| 532 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 533 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 534 | if (DEBUG) Log.d(TAG, "readXml"); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 535 | synchronized (mConfig) { |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 536 | setConfigLocked(config, "readXml"); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 537 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 541 | public void writeXml(XmlSerializer out, boolean forBackup) throws IOException { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 542 | final int N = mConfigs.size(); |
| 543 | for (int i = 0; i < N; i++) { |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 544 | //TODO: http://b/22388012 |
| 545 | if (forBackup && mConfigs.keyAt(i) != UserHandle.USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 546 | continue; |
| 547 | } |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 548 | mConfigs.valueAt(i).writeXml(out); |
| 549 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 550 | } |
| 551 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 552 | public Policy getNotificationPolicy() { |
| 553 | return getNotificationPolicy(mConfig); |
| 554 | } |
| 555 | |
| 556 | private static Policy getNotificationPolicy(ZenModeConfig config) { |
| 557 | return config == null ? null : config.toNotificationPolicy(); |
| 558 | } |
| 559 | |
| 560 | public void setNotificationPolicy(Policy policy) { |
| 561 | if (policy == null || mConfig == null) return; |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 562 | synchronized (mConfig) { |
| 563 | final ZenModeConfig newConfig = mConfig.copy(); |
| 564 | newConfig.applyNotificationPolicy(policy); |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 565 | setConfigLocked(newConfig, "setNotificationPolicy"); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 566 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 567 | } |
| 568 | |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 569 | /** |
| 570 | * Removes old rule instances whose owner is not installed. |
| 571 | */ |
| 572 | private void cleanUpZenRules() { |
| 573 | long currentTime = System.currentTimeMillis(); |
| 574 | synchronized (mConfig) { |
| 575 | final ZenModeConfig newConfig = mConfig.copy(); |
| 576 | if (newConfig.automaticRules != null) { |
| 577 | for (int i = newConfig.automaticRules.size() - 1; i >= 0; i--) { |
| 578 | ZenRule rule = newConfig.automaticRules.get(newConfig.automaticRules.keyAt(i)); |
| 579 | if (RULE_INSTANCE_GRACE_PERIOD < (currentTime - rule.creationTime)) { |
| 580 | try { |
Jeff Sharkey | c7bacab | 2016-02-09 15:56:11 -0700 | [diff] [blame] | 581 | mPm.getPackageInfo(rule.component.getPackageName(), |
| 582 | PackageManager.MATCH_UNINSTALLED_PACKAGES); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 583 | } catch (PackageManager.NameNotFoundException e) { |
| 584 | newConfig.automaticRules.removeAt(i); |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | } |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 589 | setConfigLocked(newConfig, "cleanUpZenRules"); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * @return a copy of the zen mode configuration |
| 595 | */ |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 596 | public ZenModeConfig getConfig() { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 597 | synchronized (mConfig) { |
| 598 | return mConfig.copy(); |
| 599 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 600 | } |
| 601 | |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 602 | public boolean setConfigLocked(ZenModeConfig config, String reason) { |
| 603 | return setConfigLocked(config, reason, true /*setRingerMode*/); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 604 | } |
| 605 | |
Eric Laurent | e0ced4d | 2015-09-30 17:44:28 -0700 | [diff] [blame] | 606 | public void setConfigAsync(ZenModeConfig config, String reason) { |
| 607 | mHandler.postSetConfig(config, reason); |
| 608 | } |
| 609 | |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 610 | private boolean setConfigLocked(ZenModeConfig config, String reason, boolean setRingerMode) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 611 | final long identity = Binder.clearCallingIdentity(); |
| 612 | try { |
| 613 | if (config == null || !config.isValid()) { |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 614 | Log.w(TAG, "Invalid config in setConfigLocked; " + config); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 615 | return false; |
| 616 | } |
| 617 | if (config.user != mUser) { |
| 618 | // simply store away for background users |
| 619 | mConfigs.put(config.user, config); |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 620 | if (DEBUG) Log.d(TAG, "setConfigLocked: store config for user " + config.user); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 621 | return true; |
| 622 | } |
| 623 | mConditions.evaluateConfig(config, false /*processSubscriptions*/); // may modify config |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 624 | mConfigs.put(config.user, config); |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 625 | if (DEBUG) Log.d(TAG, "setConfigLocked reason=" + reason, new Throwable()); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 626 | ZenLog.traceConfig(reason, mConfig, config); |
| 627 | final boolean policyChanged = !Objects.equals(getNotificationPolicy(mConfig), |
| 628 | getNotificationPolicy(config)); |
Julia Reynolds | 9a25da1 | 2016-01-06 16:19:28 -0500 | [diff] [blame] | 629 | if (!config.equals(mConfig)) { |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 630 | dispatchOnConfigChanged(); |
| 631 | } |
| 632 | if (policyChanged) { |
| 633 | dispatchOnPolicyChanged(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 634 | } |
Julia Reynolds | 9a25da1 | 2016-01-06 16:19:28 -0500 | [diff] [blame] | 635 | mConfig = config; |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 636 | final String val = Integer.toString(config.hashCode()); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 637 | Global.putString(mContext.getContentResolver(), Global.ZEN_MODE_CONFIG_ETAG, val); |
| 638 | if (!evaluateZenMode(reason, setRingerMode)) { |
| 639 | applyRestrictions(); // evaluateZenMode will also apply restrictions if changed |
| 640 | } |
| 641 | mConditions.evaluateConfig(config, true /*processSubscriptions*/); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 642 | return true; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 643 | } finally { |
| 644 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 645 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | private int getZenModeSetting() { |
| 649 | return Global.getInt(mContext.getContentResolver(), Global.ZEN_MODE, Global.ZEN_MODE_OFF); |
| 650 | } |
| 651 | |
| 652 | private void setZenModeSetting(int zen) { |
| 653 | Global.putInt(mContext.getContentResolver(), Global.ZEN_MODE, zen); |
| 654 | } |
| 655 | |
Julia Reynolds | 9b11fdb | 2015-07-31 09:49:55 -0400 | [diff] [blame] | 656 | private int getPreviousRingerModeSetting() { |
| 657 | return Global.getInt(mContext.getContentResolver(), |
| 658 | Global.ZEN_MODE_RINGER_LEVEL, AudioManager.RINGER_MODE_NORMAL); |
| 659 | } |
| 660 | |
| 661 | private void setPreviousRingerModeSetting(Integer previousRingerLevel) { |
| 662 | Global.putString( |
| 663 | mContext.getContentResolver(), Global.ZEN_MODE_RINGER_LEVEL, |
| 664 | previousRingerLevel == null ? null : Integer.toString(previousRingerLevel)); |
| 665 | } |
| 666 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 667 | private boolean evaluateZenMode(String reason, boolean setRingerMode) { |
| 668 | if (DEBUG) Log.d(TAG, "evaluateZenMode"); |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 669 | final int zenBefore = mZenMode; |
Julia Reynolds | 8ac6303 | 2015-08-31 15:19:43 -0400 | [diff] [blame] | 670 | final int zen = computeZenMode(); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 671 | ZenLog.traceSetZenMode(zen, reason); |
| 672 | mZenMode = zen; |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 673 | updateRingerModeAffectedStreams(); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 674 | setZenModeSetting(mZenMode); |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 675 | if (setRingerMode) { |
| 676 | applyZenToRingerMode(); |
| 677 | } |
| 678 | applyRestrictions(); |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 679 | if (zen != zenBefore) { |
Julia Reynolds | 8ac6303 | 2015-08-31 15:19:43 -0400 | [diff] [blame] | 680 | mHandler.postDispatchOnZenModeChanged(); |
| 681 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 682 | return true; |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 683 | } |
| 684 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 685 | private void updateRingerModeAffectedStreams() { |
| 686 | if (mAudioManager != null) { |
| 687 | mAudioManager.updateRingerModeAffectedStreamsInternal(); |
| 688 | } |
| 689 | } |
| 690 | |
Julia Reynolds | 8ac6303 | 2015-08-31 15:19:43 -0400 | [diff] [blame] | 691 | private int computeZenMode() { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 692 | synchronized (mConfig) { |
| 693 | if (mConfig == null) return Global.ZEN_MODE_OFF; |
| 694 | if (mConfig.manualRule != null) return mConfig.manualRule.zenMode; |
| 695 | int zen = Global.ZEN_MODE_OFF; |
| 696 | for (ZenRule automaticRule : mConfig.automaticRules.values()) { |
| 697 | if (automaticRule.isAutomaticActive()) { |
| 698 | if (zenSeverity(automaticRule.zenMode) > zenSeverity(zen)) { |
| 699 | zen = automaticRule.zenMode; |
| 700 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 701 | } |
| 702 | } |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 703 | return zen; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 704 | } |
John Spurlock | 8403b75 | 2014-12-10 12:47:01 -0500 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | private void applyRestrictions() { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 708 | final boolean zen = mZenMode != Global.ZEN_MODE_OFF; |
John Spurlock | 8403b75 | 2014-12-10 12:47:01 -0500 | [diff] [blame] | 709 | |
| 710 | // notification restrictions |
| 711 | final boolean muteNotifications = mEffectsSuppressed; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 712 | // call restrictions |
Zach Johnson | f1b4814 | 2015-08-24 15:38:27 -0700 | [diff] [blame] | 713 | final boolean muteCalls = zen && !mConfig.allowCalls && !mConfig.allowRepeatCallers; |
Julia Reynolds | 3fe81be | 2016-02-03 09:10:06 -0500 | [diff] [blame] | 714 | // total silence restrictions |
| 715 | final boolean muteEverything = mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS; |
John Spurlock | 25d01ee | 2015-06-03 12:17:46 -0400 | [diff] [blame] | 716 | |
Julia Reynolds | 3fe81be | 2016-02-03 09:10:06 -0500 | [diff] [blame] | 717 | for (int i = USAGE_UNKNOWN; i <= USAGE_VIRTUAL_SOURCE; i++) { |
| 718 | if (i == USAGE_NOTIFICATION) { |
| 719 | applyRestrictions(muteNotifications || muteEverything, i); |
| 720 | } else if (i == USAGE_NOTIFICATION_RINGTONE) { |
| 721 | applyRestrictions(muteCalls || muteEverything, i); |
| 722 | } else { |
| 723 | applyRestrictions(muteEverything, i); |
| 724 | } |
| 725 | } |
John Spurlock | 8403b75 | 2014-12-10 12:47:01 -0500 | [diff] [blame] | 726 | } |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 727 | |
John Spurlock | 8403b75 | 2014-12-10 12:47:01 -0500 | [diff] [blame] | 728 | private void applyRestrictions(boolean mute, int usage) { |
| 729 | final String[] exceptionPackages = null; // none (for now) |
| 730 | mAppOps.setRestriction(AppOpsManager.OP_VIBRATE, usage, |
| 731 | mute ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED, |
| 732 | exceptionPackages); |
| 733 | mAppOps.setRestriction(AppOpsManager.OP_PLAY_AUDIO, usage, |
| 734 | mute ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED, |
| 735 | exceptionPackages); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 736 | } |
| 737 | |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 738 | private void applyZenToRingerMode() { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 739 | if (mAudioManager == null) return; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 740 | // force the ringer mode into compliance |
| 741 | final int ringerModeInternal = mAudioManager.getRingerModeInternal(); |
| 742 | int newRingerModeInternal = ringerModeInternal; |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 743 | switch (mZenMode) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 744 | case Global.ZEN_MODE_NO_INTERRUPTIONS: |
John Spurlock | 4f1163c | 2015-04-02 17:41:21 -0400 | [diff] [blame] | 745 | case Global.ZEN_MODE_ALARMS: |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 746 | if (ringerModeInternal != AudioManager.RINGER_MODE_SILENT) { |
Julia Reynolds | 9b11fdb | 2015-07-31 09:49:55 -0400 | [diff] [blame] | 747 | setPreviousRingerModeSetting(ringerModeInternal); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 748 | newRingerModeInternal = AudioManager.RINGER_MODE_SILENT; |
John Spurlock | 8c01d88 | 2014-07-28 13:37:13 -0400 | [diff] [blame] | 749 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 750 | break; |
| 751 | case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: |
| 752 | case Global.ZEN_MODE_OFF: |
| 753 | if (ringerModeInternal == AudioManager.RINGER_MODE_SILENT) { |
Julia Reynolds | 9b11fdb | 2015-07-31 09:49:55 -0400 | [diff] [blame] | 754 | newRingerModeInternal = getPreviousRingerModeSetting(); |
| 755 | setPreviousRingerModeSetting(null); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 756 | } |
| 757 | break; |
John Spurlock | 8c01d88 | 2014-07-28 13:37:13 -0400 | [diff] [blame] | 758 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 759 | if (newRingerModeInternal != -1) { |
| 760 | mAudioManager.setRingerModeInternal(newRingerModeInternal, TAG); |
| 761 | } |
| 762 | } |
| 763 | |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 764 | private void dispatchOnConfigChanged() { |
| 765 | for (Callback callback : mCallbacks) { |
| 766 | callback.onConfigChanged(); |
| 767 | } |
| 768 | } |
| 769 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 770 | private void dispatchOnPolicyChanged() { |
| 771 | for (Callback callback : mCallbacks) { |
| 772 | callback.onPolicyChanged(); |
| 773 | } |
| 774 | } |
| 775 | |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 776 | private void dispatchOnZenModeChanged() { |
| 777 | for (Callback callback : mCallbacks) { |
| 778 | callback.onZenModeChanged(); |
| 779 | } |
| 780 | } |
| 781 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 782 | private ZenModeConfig readDefaultConfig(Resources resources) { |
| 783 | XmlResourceParser parser = null; |
| 784 | try { |
| 785 | parser = resources.getXml(R.xml.default_zen_mode_config); |
| 786 | while (parser.next() != XmlPullParser.END_DOCUMENT) { |
| 787 | final ZenModeConfig config = ZenModeConfig.readXml(parser, mConfigMigration); |
| 788 | if (config != null) return config; |
| 789 | } |
| 790 | } catch (Exception e) { |
| 791 | Log.w(TAG, "Error reading default zen mode config from resource", e); |
| 792 | } finally { |
| 793 | IoUtils.closeQuietly(parser); |
| 794 | } |
| 795 | return new ZenModeConfig(); |
| 796 | } |
| 797 | |
| 798 | private void appendDefaultScheduleRules(ZenModeConfig config) { |
| 799 | if (config == null) return; |
| 800 | |
| 801 | final ScheduleInfo weeknights = new ScheduleInfo(); |
| 802 | weeknights.days = ZenModeConfig.WEEKNIGHT_DAYS; |
| 803 | weeknights.startHour = 22; |
| 804 | weeknights.endHour = 7; |
| 805 | final ZenRule rule1 = new ZenRule(); |
| 806 | rule1.enabled = false; |
| 807 | rule1.name = mContext.getResources() |
| 808 | .getString(R.string.zen_mode_default_weeknights_name); |
| 809 | rule1.conditionId = ZenModeConfig.toScheduleConditionId(weeknights); |
| 810 | rule1.zenMode = Global.ZEN_MODE_ALARMS; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 811 | rule1.component = ScheduleConditionProvider.COMPONENT; |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 812 | rule1.id = ZenModeConfig.newRuleId(); |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 813 | rule1.creationTime = System.currentTimeMillis(); |
| 814 | config.automaticRules.put(rule1.id, rule1); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 815 | |
| 816 | final ScheduleInfo weekends = new ScheduleInfo(); |
| 817 | weekends.days = ZenModeConfig.WEEKEND_DAYS; |
| 818 | weekends.startHour = 23; |
| 819 | weekends.startMinute = 30; |
| 820 | weekends.endHour = 10; |
| 821 | final ZenRule rule2 = new ZenRule(); |
| 822 | rule2.enabled = false; |
| 823 | rule2.name = mContext.getResources() |
| 824 | .getString(R.string.zen_mode_default_weekends_name); |
| 825 | rule2.conditionId = ZenModeConfig.toScheduleConditionId(weekends); |
| 826 | rule2.zenMode = Global.ZEN_MODE_ALARMS; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 827 | rule2.component = ScheduleConditionProvider.COMPONENT; |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 828 | rule2.id = ZenModeConfig.newRuleId(); |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 829 | rule2.creationTime = System.currentTimeMillis(); |
| 830 | config.automaticRules.put(rule2.id, rule2); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 831 | } |
| 832 | |
John Spurlock | cb9aa20 | 2015-05-08 17:35:22 -0400 | [diff] [blame] | 833 | private void appendDefaultEventRules(ZenModeConfig config) { |
| 834 | if (config == null) return; |
| 835 | |
| 836 | final EventInfo events = new EventInfo(); |
John Spurlock | 995a749 | 2015-05-28 22:13:03 -0400 | [diff] [blame] | 837 | events.calendar = null; // any calendar |
John Spurlock | cb9aa20 | 2015-05-08 17:35:22 -0400 | [diff] [blame] | 838 | events.reply = EventInfo.REPLY_YES_OR_MAYBE; |
| 839 | final ZenRule rule = new ZenRule(); |
| 840 | rule.enabled = false; |
| 841 | rule.name = mContext.getResources().getString(R.string.zen_mode_default_events_name); |
| 842 | rule.conditionId = ZenModeConfig.toEventConditionId(events); |
| 843 | rule.zenMode = Global.ZEN_MODE_ALARMS; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 844 | rule.component = EventConditionProvider.COMPONENT; |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 845 | rule.id = ZenModeConfig.newRuleId(); |
Julia Reynolds | 56106ff | 2015-09-30 14:42:53 -0400 | [diff] [blame] | 846 | rule.creationTime = System.currentTimeMillis(); |
| 847 | config.automaticRules.put(rule.id, rule); |
John Spurlock | cb9aa20 | 2015-05-08 17:35:22 -0400 | [diff] [blame] | 848 | } |
| 849 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 850 | private static int zenSeverity(int zen) { |
| 851 | switch (zen) { |
| 852 | case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return 1; |
| 853 | case Global.ZEN_MODE_ALARMS: return 2; |
| 854 | case Global.ZEN_MODE_NO_INTERRUPTIONS: return 3; |
| 855 | default: return 0; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | private final ZenModeConfig.Migration mConfigMigration = new ZenModeConfig.Migration() { |
| 860 | @Override |
| 861 | public ZenModeConfig migrate(ZenModeConfig.XmlV1 v1) { |
| 862 | if (v1 == null) return null; |
| 863 | final ZenModeConfig rt = new ZenModeConfig(); |
| 864 | rt.allowCalls = v1.allowCalls; |
| 865 | rt.allowEvents = v1.allowEvents; |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 866 | rt.allowCallsFrom = v1.allowFrom; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 867 | rt.allowMessages = v1.allowMessages; |
John Spurlock | a492d1d | 2015-05-05 18:30:28 -0400 | [diff] [blame] | 868 | rt.allowMessagesFrom = v1.allowFrom; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 869 | rt.allowReminders = v1.allowReminders; |
| 870 | // don't migrate current exit condition |
| 871 | final int[] days = ZenModeConfig.XmlV1.tryParseDays(v1.sleepMode); |
| 872 | if (days != null && days.length > 0) { |
| 873 | Log.i(TAG, "Migrating existing V1 downtime to single schedule"); |
| 874 | final ScheduleInfo schedule = new ScheduleInfo(); |
| 875 | schedule.days = days; |
| 876 | schedule.startHour = v1.sleepStartHour; |
| 877 | schedule.startMinute = v1.sleepStartMinute; |
| 878 | schedule.endHour = v1.sleepEndHour; |
| 879 | schedule.endMinute = v1.sleepEndMinute; |
| 880 | final ZenRule rule = new ZenRule(); |
| 881 | rule.enabled = true; |
| 882 | rule.name = mContext.getResources() |
| 883 | .getString(R.string.zen_mode_downtime_feature_name); |
| 884 | rule.conditionId = ZenModeConfig.toScheduleConditionId(schedule); |
| 885 | rule.zenMode = v1.sleepNone ? Global.ZEN_MODE_NO_INTERRUPTIONS |
| 886 | : Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 887 | rule.component = ScheduleConditionProvider.COMPONENT; |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 888 | rt.automaticRules.put(ZenModeConfig.newRuleId(), rule); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 889 | } else { |
| 890 | Log.i(TAG, "No existing V1 downtime found, generating default schedules"); |
| 891 | appendDefaultScheduleRules(rt); |
| 892 | } |
John Spurlock | cb9aa20 | 2015-05-08 17:35:22 -0400 | [diff] [blame] | 893 | appendDefaultEventRules(rt); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 894 | return rt; |
| 895 | } |
| 896 | }; |
| 897 | |
| 898 | private final class RingerModeDelegate implements AudioManagerInternal.RingerModeDelegate { |
| 899 | @Override |
| 900 | public String toString() { |
| 901 | return TAG; |
| 902 | } |
| 903 | |
| 904 | @Override |
| 905 | public int onSetRingerModeInternal(int ringerModeOld, int ringerModeNew, String caller, |
| 906 | int ringerModeExternal, VolumePolicy policy) { |
| 907 | final boolean isChange = ringerModeOld != ringerModeNew; |
| 908 | |
| 909 | int ringerModeExternalOut = ringerModeNew; |
| 910 | |
| 911 | int newZen = -1; |
| 912 | switch (ringerModeNew) { |
| 913 | case AudioManager.RINGER_MODE_SILENT: |
| 914 | if (isChange && policy.doNotDisturbWhenSilent) { |
| 915 | if (mZenMode != Global.ZEN_MODE_NO_INTERRUPTIONS |
| 916 | && mZenMode != Global.ZEN_MODE_ALARMS) { |
John Spurlock | b02c744 | 2015-04-14 09:32:25 -0400 | [diff] [blame] | 917 | newZen = Global.ZEN_MODE_ALARMS; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 918 | } |
Julia Reynolds | 9b11fdb | 2015-07-31 09:49:55 -0400 | [diff] [blame] | 919 | setPreviousRingerModeSetting(ringerModeOld); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 920 | } |
| 921 | break; |
| 922 | case AudioManager.RINGER_MODE_VIBRATE: |
| 923 | case AudioManager.RINGER_MODE_NORMAL: |
| 924 | if (isChange && ringerModeOld == AudioManager.RINGER_MODE_SILENT |
| 925 | && (mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS |
| 926 | || mZenMode == Global.ZEN_MODE_ALARMS)) { |
| 927 | newZen = Global.ZEN_MODE_OFF; |
| 928 | } else if (mZenMode != Global.ZEN_MODE_OFF) { |
| 929 | ringerModeExternalOut = AudioManager.RINGER_MODE_SILENT; |
| 930 | } |
| 931 | break; |
| 932 | } |
| 933 | if (newZen != -1) { |
| 934 | setManualZenMode(newZen, null, "ringerModeInternal", false /*setRingerMode*/); |
| 935 | } |
| 936 | |
| 937 | if (isChange || newZen != -1 || ringerModeExternal != ringerModeExternalOut) { |
| 938 | ZenLog.traceSetRingerModeInternal(ringerModeOld, ringerModeNew, caller, |
| 939 | ringerModeExternal, ringerModeExternalOut); |
| 940 | } |
| 941 | return ringerModeExternalOut; |
| 942 | } |
| 943 | |
| 944 | @Override |
| 945 | public int onSetRingerModeExternal(int ringerModeOld, int ringerModeNew, String caller, |
| 946 | int ringerModeInternal, VolumePolicy policy) { |
| 947 | int ringerModeInternalOut = ringerModeNew; |
| 948 | final boolean isChange = ringerModeOld != ringerModeNew; |
| 949 | final boolean isVibrate = ringerModeInternal == AudioManager.RINGER_MODE_VIBRATE; |
| 950 | |
| 951 | int newZen = -1; |
| 952 | switch (ringerModeNew) { |
| 953 | case AudioManager.RINGER_MODE_SILENT: |
| 954 | if (isChange) { |
| 955 | if (mZenMode == Global.ZEN_MODE_OFF) { |
John Spurlock | 05715ec | 2015-05-13 11:19:19 -0400 | [diff] [blame] | 956 | newZen = Global.ZEN_MODE_ALARMS; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 957 | } |
| 958 | ringerModeInternalOut = isVibrate ? AudioManager.RINGER_MODE_VIBRATE |
John Spurlock | 05715ec | 2015-05-13 11:19:19 -0400 | [diff] [blame] | 959 | : AudioManager.RINGER_MODE_SILENT; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 960 | } else { |
| 961 | ringerModeInternalOut = ringerModeInternal; |
| 962 | } |
| 963 | break; |
| 964 | case AudioManager.RINGER_MODE_VIBRATE: |
| 965 | case AudioManager.RINGER_MODE_NORMAL: |
| 966 | if (mZenMode != Global.ZEN_MODE_OFF) { |
| 967 | newZen = Global.ZEN_MODE_OFF; |
| 968 | } |
| 969 | break; |
| 970 | } |
| 971 | if (newZen != -1) { |
| 972 | setManualZenMode(newZen, null, "ringerModeExternal", false /*setRingerMode*/); |
| 973 | } |
| 974 | |
| 975 | ZenLog.traceSetRingerModeExternal(ringerModeOld, ringerModeNew, caller, |
| 976 | ringerModeInternal, ringerModeInternalOut); |
| 977 | return ringerModeInternalOut; |
| 978 | } |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 979 | |
| 980 | @Override |
| 981 | public boolean canVolumeDownEnterSilent() { |
| 982 | return mZenMode == Global.ZEN_MODE_OFF; |
| 983 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 984 | |
| 985 | @Override |
| 986 | public int getRingerModeAffectedStreams(int streams) { |
| 987 | // ringtone, notification and system streams are always affected by ringer mode |
| 988 | streams |= (1 << AudioSystem.STREAM_RING) | |
| 989 | (1 << AudioSystem.STREAM_NOTIFICATION) | |
| 990 | (1 << AudioSystem.STREAM_SYSTEM); |
| 991 | |
| 992 | // alarm and music streams are only affected by ringer mode when in total silence |
| 993 | if (mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) { |
| 994 | streams |= (1 << AudioSystem.STREAM_ALARM) | |
| 995 | (1 << AudioSystem.STREAM_MUSIC); |
| 996 | } else { |
| 997 | streams &= ~((1 << AudioSystem.STREAM_ALARM) | |
| 998 | (1 << AudioSystem.STREAM_MUSIC)); |
| 999 | } |
| 1000 | return streams; |
| 1001 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | private final class SettingsObserver extends ContentObserver { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1005 | private final Uri ZEN_MODE = Global.getUriFor(Global.ZEN_MODE); |
| 1006 | |
| 1007 | public SettingsObserver(Handler handler) { |
| 1008 | super(handler); |
| 1009 | } |
| 1010 | |
| 1011 | public void observe() { |
| 1012 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1013 | resolver.registerContentObserver(ZEN_MODE, false /*notifyForDescendents*/, this); |
| 1014 | update(null); |
| 1015 | } |
| 1016 | |
| 1017 | @Override |
| 1018 | public void onChange(boolean selfChange, Uri uri) { |
| 1019 | update(uri); |
| 1020 | } |
| 1021 | |
| 1022 | public void update(Uri uri) { |
| 1023 | if (ZEN_MODE.equals(uri)) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1024 | if (mZenMode != getZenModeSetting()) { |
| 1025 | if (DEBUG) Log.d(TAG, "Fixing zen mode setting"); |
| 1026 | setZenModeSetting(mZenMode); |
| 1027 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1032 | private final class Metrics extends Callback { |
| 1033 | private static final String COUNTER_PREFIX = "dnd_mode_"; |
| 1034 | private static final long MINIMUM_LOG_PERIOD_MS = 60 * 1000; |
| 1035 | |
| 1036 | private int mPreviousZenMode = -1; |
| 1037 | private long mBeginningMs = 0L; |
| 1038 | |
| 1039 | @Override |
| 1040 | void onZenModeChanged() { |
| 1041 | emit(); |
| 1042 | } |
| 1043 | |
| 1044 | private void emit() { |
| 1045 | mHandler.postMetricsTimer(); |
| 1046 | final long now = SystemClock.elapsedRealtime(); |
| 1047 | final long since = (now - mBeginningMs); |
| 1048 | if (mPreviousZenMode != mZenMode || since > MINIMUM_LOG_PERIOD_MS) { |
| 1049 | if (mPreviousZenMode != -1) { |
| 1050 | MetricsLogger.count(mContext, COUNTER_PREFIX + mPreviousZenMode, (int) since); |
| 1051 | } |
| 1052 | mPreviousZenMode = mZenMode; |
| 1053 | mBeginningMs = now; |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1058 | private final class H extends Handler { |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1059 | private static final int MSG_DISPATCH = 1; |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1060 | private static final int MSG_METRICS = 2; |
Eric Laurent | e0ced4d | 2015-09-30 17:44:28 -0700 | [diff] [blame] | 1061 | private static final int MSG_SET_CONFIG = 3; |
| 1062 | |
| 1063 | private final class ConfigMessageData { |
| 1064 | public final ZenModeConfig config; |
| 1065 | public final String reason; |
| 1066 | |
| 1067 | ConfigMessageData(ZenModeConfig config, String reason) { |
| 1068 | this.config = config; |
| 1069 | this.reason = reason; |
| 1070 | } |
| 1071 | } |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1072 | |
| 1073 | private static final long METRICS_PERIOD_MS = 6 * 60 * 60 * 1000; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1074 | |
| 1075 | private H(Looper looper) { |
| 1076 | super(looper); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1077 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1078 | |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1079 | private void postDispatchOnZenModeChanged() { |
| 1080 | removeMessages(MSG_DISPATCH); |
| 1081 | sendEmptyMessage(MSG_DISPATCH); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1082 | } |
| 1083 | |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1084 | private void postMetricsTimer() { |
| 1085 | removeMessages(MSG_METRICS); |
| 1086 | sendEmptyMessageDelayed(MSG_METRICS, METRICS_PERIOD_MS); |
| 1087 | } |
| 1088 | |
Eric Laurent | e0ced4d | 2015-09-30 17:44:28 -0700 | [diff] [blame] | 1089 | private void postSetConfig(ZenModeConfig config, String reason) { |
| 1090 | sendMessage(obtainMessage(MSG_SET_CONFIG, new ConfigMessageData(config, reason))); |
| 1091 | } |
| 1092 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1093 | @Override |
| 1094 | public void handleMessage(Message msg) { |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1095 | switch (msg.what) { |
| 1096 | case MSG_DISPATCH: |
| 1097 | dispatchOnZenModeChanged(); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1098 | break; |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1099 | case MSG_METRICS: |
| 1100 | mMetrics.emit(); |
| 1101 | break; |
Eric Laurent | e0ced4d | 2015-09-30 17:44:28 -0700 | [diff] [blame] | 1102 | case MSG_SET_CONFIG: |
| 1103 | ConfigMessageData configData = (ConfigMessageData)msg.obj; |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 1104 | synchronized (mConfig) { |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 1105 | setConfigLocked(configData.config, configData.reason); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 1106 | } |
Eric Laurent | e0ced4d | 2015-09-30 17:44:28 -0700 | [diff] [blame] | 1107 | break; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1111 | |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 1112 | public static class Callback { |
| 1113 | void onConfigChanged() {} |
| 1114 | void onZenModeChanged() {} |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1115 | void onPolicyChanged() {} |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1116 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1117 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1118 | } |