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