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