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; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 21 | import android.app.Notification; |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 22 | import android.app.NotificationManager; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 23 | import android.app.NotificationManager.Policy; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 24 | import android.app.PendingIntent; |
John Spurlock | 312d1d0 | 2014-07-08 10:24:57 -0400 | [diff] [blame] | 25 | import android.content.ComponentName; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 26 | import android.content.ContentResolver; |
| 27 | import android.content.Context; |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 28 | import android.content.Intent; |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 29 | import android.content.pm.ActivityInfo; |
| 30 | import android.content.pm.PackageItemInfo; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 31 | import android.content.pm.PackageManager; |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 32 | import android.content.pm.ResolveInfo; |
| 33 | import android.content.pm.ServiceInfo; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 34 | import android.content.res.Resources; |
| 35 | import android.content.res.XmlResourceParser; |
| 36 | import android.database.ContentObserver; |
Julia Reynolds | 9aa1c9e | 2018-04-09 11:31:15 -0400 | [diff] [blame] | 37 | import android.graphics.drawable.Icon; |
Jean-Michel Trivi | e743bda | 2016-09-09 11:56:48 -0700 | [diff] [blame] | 38 | import android.media.AudioAttributes; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 39 | import android.media.AudioManager; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 40 | import android.media.AudioManagerInternal; |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 41 | import android.media.AudioSystem; |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 42 | import android.media.VolumePolicy; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 43 | import android.net.Uri; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 44 | import android.os.Binder; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 45 | import android.os.Bundle; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 46 | import android.os.Handler; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 47 | import android.os.Looper; |
| 48 | import android.os.Message; |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 49 | import android.os.Process; |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 50 | import android.os.SystemClock; |
John Spurlock | b5e767b | 2014-07-27 11:53:20 -0400 | [diff] [blame] | 51 | import android.os.UserHandle; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 52 | import android.provider.Settings; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 53 | import android.provider.Settings.Global; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 54 | import android.service.notification.Condition; |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 55 | import android.service.notification.ConditionProviderService; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 56 | import android.service.notification.ZenModeConfig; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 57 | import android.service.notification.ZenModeConfig.ZenRule; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 58 | import android.service.notification.ZenModeProto; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 59 | import android.service.notification.ZenPolicy; |
Julia Reynolds | 87c4277 | 2016-05-16 09:52:17 -0400 | [diff] [blame] | 60 | import android.util.AndroidRuntimeException; |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 61 | import android.util.ArrayMap; |
John Spurlock | 4db0d98 | 2014-08-13 09:19:03 -0400 | [diff] [blame] | 62 | import android.util.Log; |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 63 | import android.util.Slog; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 64 | import android.util.SparseArray; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 65 | import android.util.proto.ProtoOutputStream; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 66 | |
| 67 | import com.android.internal.R; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 68 | import com.android.internal.annotations.VisibleForTesting; |
John Spurlock | 25d01ee | 2015-06-03 12:17:46 -0400 | [diff] [blame] | 69 | import com.android.internal.logging.MetricsLogger; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 70 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
| 71 | import com.android.internal.notification.SystemNotificationChannels; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 72 | import com.android.server.LocalServices; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 73 | |
| 74 | import libcore.io.IoUtils; |
| 75 | |
| 76 | import org.xmlpull.v1.XmlPullParser; |
| 77 | import org.xmlpull.v1.XmlPullParserException; |
| 78 | import org.xmlpull.v1.XmlSerializer; |
| 79 | |
| 80 | import java.io.IOException; |
| 81 | import java.io.PrintWriter; |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 82 | import java.util.ArrayList; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 83 | import java.util.List; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 84 | import java.util.Objects; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * NotificationManagerService helper for functionality related to zen mode. |
| 88 | */ |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 89 | public class ZenModeHelper { |
| 90 | static final String TAG = "ZenModeHelper"; |
| 91 | static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 92 | |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 93 | // The amount of time rules instances can exist without their owning app being installed. |
| 94 | private static final int RULE_INSTANCE_GRACE_PERIOD = 1000 * 60 * 60 * 72; |
| 95 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 96 | private final Context mContext; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 97 | private final H mHandler; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 98 | private final SettingsObserver mSettingsObserver; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 99 | @VisibleForTesting protected final AppOpsManager mAppOps; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 100 | @VisibleForTesting protected final NotificationManager mNotificationManager; |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 101 | @VisibleForTesting protected ZenModeConfig mDefaultConfig; |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 102 | private final ArrayList<Callback> mCallbacks = new ArrayList<Callback>(); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 103 | private final ZenModeFiltering mFiltering; |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 104 | protected final RingerModeDelegate mRingerModeDelegate = new |
| 105 | RingerModeDelegate(); |
Beverly | 2f43b64 | 2018-08-14 09:43:11 -0400 | [diff] [blame] | 106 | @VisibleForTesting protected final ZenModeConditions mConditions; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 107 | private final SparseArray<ZenModeConfig> mConfigs = new SparseArray<>(); |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 108 | private final Metrics mMetrics = new Metrics(); |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 109 | private final ConditionProviders.Config mServiceConfig; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 110 | |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 111 | @VisibleForTesting protected int mZenMode; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 112 | @VisibleForTesting protected NotificationManager.Policy mConsolidatedPolicy; |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 113 | private int mUser = UserHandle.USER_SYSTEM; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 114 | @VisibleForTesting protected ZenModeConfig mConfig; |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 115 | @VisibleForTesting protected AudioManagerInternal mAudioManager; |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 116 | protected PackageManager mPm; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 117 | private long mSuppressedEffects; |
| 118 | |
| 119 | public static final long SUPPRESSED_EFFECT_NOTIFICATIONS = 1; |
| 120 | public static final long SUPPRESSED_EFFECT_CALLS = 1 << 1; |
| 121 | public static final long SUPPRESSED_EFFECT_ALL = SUPPRESSED_EFFECT_CALLS |
| 122 | | SUPPRESSED_EFFECT_NOTIFICATIONS; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 123 | |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 124 | @VisibleForTesting protected boolean mIsBootComplete; |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 125 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 126 | public ZenModeHelper(Context context, Looper looper, ConditionProviders conditionProviders) { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 127 | mContext = context; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 128 | mHandler = new H(looper); |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 129 | addCallback(mMetrics); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 130 | mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 131 | mNotificationManager = context.getSystemService(NotificationManager.class); |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 132 | |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 133 | mDefaultConfig = readDefaultConfig(mContext.getResources()); |
| 134 | updateDefaultAutomaticRuleNames(); |
| 135 | mConfig = mDefaultConfig.copy(); |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 136 | mConfigs.put(UserHandle.USER_SYSTEM, mConfig); |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 137 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 138 | mSettingsObserver = new SettingsObserver(mHandler); |
| 139 | mSettingsObserver.observe(); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 140 | mFiltering = new ZenModeFiltering(mContext); |
| 141 | mConditions = new ZenModeConditions(this, conditionProviders); |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 142 | mServiceConfig = conditionProviders.getConfig(); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 143 | } |
| 144 | |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 145 | public Looper getLooper() { |
| 146 | return mHandler.getLooper(); |
| 147 | } |
| 148 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 149 | @Override |
| 150 | public String toString() { |
| 151 | return TAG; |
| 152 | } |
| 153 | |
| 154 | public boolean matchesCallFilter(UserHandle userHandle, Bundle extras, |
| 155 | ValidateNotificationPeople validator, int contactsTimeoutMs, float timeoutAffinity) { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 156 | synchronized (mConfig) { |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 157 | return ZenModeFiltering.matchesCallFilter(mContext, mZenMode, mConsolidatedPolicy, |
| 158 | userHandle, extras, validator, contactsTimeoutMs, timeoutAffinity); |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 159 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | public boolean isCall(NotificationRecord record) { |
| 163 | return mFiltering.isCall(record); |
| 164 | } |
| 165 | |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 166 | public void recordCaller(NotificationRecord record) { |
| 167 | mFiltering.recordCall(record); |
| 168 | } |
| 169 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 170 | public boolean shouldIntercept(NotificationRecord record) { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 171 | synchronized (mConfig) { |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 172 | return mFiltering.shouldIntercept(mZenMode, mConsolidatedPolicy, record); |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 173 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 174 | } |
| 175 | |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 176 | public void addCallback(Callback callback) { |
| 177 | mCallbacks.add(callback); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 178 | } |
| 179 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 180 | public void removeCallback(Callback callback) { |
| 181 | mCallbacks.remove(callback); |
| 182 | } |
| 183 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 184 | public void initZenMode() { |
| 185 | if (DEBUG) Log.d(TAG, "initZenMode"); |
| 186 | evaluateZenMode("init", true /*setRingerMode*/); |
| 187 | } |
| 188 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 189 | public void onSystemReady() { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 190 | if (DEBUG) Log.d(TAG, "onSystemReady"); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 191 | mAudioManager = LocalServices.getService(AudioManagerInternal.class); |
| 192 | if (mAudioManager != null) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 193 | mAudioManager.setRingerModeDelegate(mRingerModeDelegate); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 194 | } |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 195 | mPm = mContext.getPackageManager(); |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 196 | mHandler.postMetricsTimer(); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 197 | cleanUpZenRules(); |
Julia Reynolds | 8ac6303 | 2015-08-31 15:19:43 -0400 | [diff] [blame] | 198 | evaluateZenMode("onSystemReady", true); |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 199 | mIsBootComplete = true; |
| 200 | showZenUpgradeNotification(mZenMode); |
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 | } |
| 227 | synchronized (mConfig) { |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 228 | setConfigLocked(config, null, reason); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 229 | } |
| 230 | cleanUpZenRules(); |
| 231 | } |
| 232 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 233 | public int getZenModeListenerInterruptionFilter() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 234 | return NotificationManager.zenModeToInterruptionFilter(mZenMode); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 235 | } |
| 236 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 237 | public void requestFromListener(ComponentName name, int filter) { |
| 238 | final int newZen = NotificationManager.zenModeFromInterruptionFilter(filter, -1); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 239 | if (newZen != -1) { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 240 | setManualZenMode(newZen, null, name != null ? name.getPackageName() : null, |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 241 | "listener:" + (name != null ? name.flattenToShortString() : null)); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 245 | public void setSuppressedEffects(long suppressedEffects) { |
| 246 | if (mSuppressedEffects == suppressedEffects) return; |
| 247 | mSuppressedEffects = suppressedEffects; |
John Spurlock | 8403b75 | 2014-12-10 12:47:01 -0500 | [diff] [blame] | 248 | applyRestrictions(); |
| 249 | } |
| 250 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 251 | public long getSuppressedEffects() { |
| 252 | return mSuppressedEffects; |
| 253 | } |
| 254 | |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 255 | public int getZenMode() { |
| 256 | return mZenMode; |
| 257 | } |
| 258 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 259 | public List<ZenRule> getZenRules() { |
| 260 | List<ZenRule> rules = new ArrayList<>(); |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 261 | synchronized (mConfig) { |
| 262 | if (mConfig == null) return rules; |
| 263 | for (ZenRule rule : mConfig.automaticRules.values()) { |
| 264 | if (canManageAutomaticZenRule(rule)) { |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 265 | rules.add(rule); |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 266 | } |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | return rules; |
| 270 | } |
| 271 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 272 | public AutomaticZenRule getAutomaticZenRule(String id) { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 273 | ZenRule rule; |
| 274 | synchronized (mConfig) { |
| 275 | if (mConfig == null) return null; |
| 276 | rule = mConfig.automaticRules.get(id); |
| 277 | } |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 278 | if (rule == null) return null; |
| 279 | if (canManageAutomaticZenRule(rule)) { |
| 280 | return createAutomaticZenRule(rule); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 281 | } |
| 282 | return null; |
| 283 | } |
| 284 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 285 | public String addAutomaticZenRule(AutomaticZenRule automaticZenRule, String reason) { |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 286 | if (!isSystemRule(automaticZenRule)) { |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 287 | PackageItemInfo component = getServiceInfo(automaticZenRule.getOwner()); |
| 288 | if (component == null) { |
| 289 | component = getActivityInfo(automaticZenRule.getConfigurationActivity()); |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 290 | } |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 291 | if (component == null) { |
| 292 | throw new IllegalArgumentException("Lacking enabled CPS or config activity"); |
| 293 | } |
Julia Reynolds | 7f2f441 | 2016-03-01 12:33:48 -0500 | [diff] [blame] | 294 | int ruleInstanceLimit = -1; |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 295 | if (component.metaData != null) { |
| 296 | ruleInstanceLimit = component.metaData.getInt( |
Julia Reynolds | 7f2f441 | 2016-03-01 12:33:48 -0500 | [diff] [blame] | 297 | ConditionProviderService.META_DATA_RULE_INSTANCE_LIMIT, -1); |
| 298 | } |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 299 | int newRuleInstanceCount = getCurrentInstanceCount(automaticZenRule.getOwner()) |
| 300 | + getCurrentInstanceCount(automaticZenRule.getConfigurationActivity()) |
| 301 | + 1; |
| 302 | if (ruleInstanceLimit > 0 && ruleInstanceLimit < newRuleInstanceCount) { |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 303 | throw new IllegalArgumentException("Rule instance limit exceeded"); |
| 304 | } |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 305 | |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 306 | } |
| 307 | |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 308 | ZenModeConfig newConfig; |
| 309 | synchronized (mConfig) { |
Julia Reynolds | 87c4277 | 2016-05-16 09:52:17 -0400 | [diff] [blame] | 310 | if (mConfig == null) { |
| 311 | throw new AndroidRuntimeException("Could not create rule"); |
| 312 | } |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 313 | if (DEBUG) { |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 314 | Log.d(TAG, "addAutomaticZenRule rule= " + automaticZenRule + " reason=" + reason); |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 315 | } |
| 316 | newConfig = mConfig.copy(); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 317 | ZenRule rule = new ZenRule(); |
| 318 | populateZenRule(automaticZenRule, rule, true); |
Beverly | a0d07ab | 2018-11-28 16:49:23 -0500 | [diff] [blame] | 319 | newConfig.automaticRules.put(rule.id, rule); |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 320 | if (setConfigLocked(newConfig, reason, rule.component, true)) { |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 321 | return rule.id; |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 322 | } else { |
Julia Reynolds | 87c4277 | 2016-05-16 09:52:17 -0400 | [diff] [blame] | 323 | throw new AndroidRuntimeException("Could not create rule"); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 324 | } |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 328 | public boolean updateAutomaticZenRule(String ruleId, AutomaticZenRule automaticZenRule, |
| 329 | String reason) { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 330 | ZenModeConfig newConfig; |
| 331 | synchronized (mConfig) { |
| 332 | if (mConfig == null) return false; |
| 333 | if (DEBUG) { |
| 334 | Log.d(TAG, "updateAutomaticZenRule zenRule=" + automaticZenRule |
| 335 | + " reason=" + reason); |
| 336 | } |
| 337 | newConfig = mConfig.copy(); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 338 | ZenModeConfig.ZenRule rule; |
| 339 | if (ruleId == null) { |
| 340 | throw new IllegalArgumentException("Rule doesn't exist"); |
| 341 | } else { |
| 342 | rule = newConfig.automaticRules.get(ruleId); |
| 343 | if (rule == null || !canManageAutomaticZenRule(rule)) { |
| 344 | throw new SecurityException( |
| 345 | "Cannot update rules not owned by your condition provider"); |
| 346 | } |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 347 | } |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 348 | populateZenRule(automaticZenRule, rule, false); |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 349 | return setConfigLocked(newConfig, reason, rule.component, true); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 350 | } |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 353 | public boolean removeAutomaticZenRule(String id, String reason) { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 354 | ZenModeConfig newConfig; |
| 355 | synchronized (mConfig) { |
| 356 | if (mConfig == null) return false; |
| 357 | newConfig = mConfig.copy(); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 358 | ZenRule rule = newConfig.automaticRules.get(id); |
| 359 | if (rule == null) return false; |
| 360 | if (canManageAutomaticZenRule(rule)) { |
| 361 | newConfig.automaticRules.remove(id); |
| 362 | if (DEBUG) Log.d(TAG, "removeZenRule zenRule=" + id + " reason=" + reason); |
| 363 | } else { |
| 364 | throw new SecurityException( |
| 365 | "Cannot delete rules not owned by your condition provider"); |
| 366 | } |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 367 | return setConfigLocked(newConfig, reason, null, true); |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 368 | } |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 369 | } |
| 370 | |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 371 | public boolean removeAutomaticZenRules(String packageName, String reason) { |
| 372 | ZenModeConfig newConfig; |
| 373 | synchronized (mConfig) { |
| 374 | if (mConfig == null) return false; |
| 375 | newConfig = mConfig.copy(); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 376 | for (int i = newConfig.automaticRules.size() - 1; i >= 0; i--) { |
| 377 | ZenRule rule = newConfig.automaticRules.get(newConfig.automaticRules.keyAt(i)); |
Julia Reynolds | 10e663b | 2019-01-07 10:15:12 -0500 | [diff] [blame] | 378 | if (rule.pkg.equals(packageName) && canManageAutomaticZenRule(rule)) { |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 379 | newConfig.automaticRules.removeAt(i); |
| 380 | } |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 381 | } |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 382 | return setConfigLocked(newConfig, reason, null, true); |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 383 | } |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 384 | } |
| 385 | |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 386 | public void setAutomaticZenRuleState(String id, Condition condition) { |
| 387 | ZenModeConfig newConfig; |
| 388 | synchronized (mConfig) { |
| 389 | if (mConfig == null) return; |
| 390 | |
| 391 | newConfig = mConfig.copy(); |
| 392 | } |
| 393 | setAutomaticZenRuleState(newConfig, newConfig.automaticRules.get(id), condition); |
| 394 | } |
| 395 | |
| 396 | public void setAutomaticZenRuleState(Uri ruleDefinition, Condition condition) { |
| 397 | ZenModeConfig newConfig; |
| 398 | synchronized (mConfig) { |
| 399 | if (mConfig == null) return; |
| 400 | newConfig = mConfig.copy(); |
| 401 | } |
| 402 | |
| 403 | setAutomaticZenRuleState(newConfig, |
| 404 | findMatchingRule(newConfig, ruleDefinition, condition), |
| 405 | condition); |
| 406 | } |
| 407 | |
| 408 | private void setAutomaticZenRuleState(ZenModeConfig config, ZenRule rule, Condition condition) { |
| 409 | if (rule == null) return; |
| 410 | |
| 411 | rule.condition = condition; |
| 412 | updateSnoozing(rule); |
| 413 | setConfigLocked(config, rule.component, "conditionChanged"); |
| 414 | } |
| 415 | |
| 416 | private ZenRule findMatchingRule(ZenModeConfig config, Uri id, Condition condition) { |
| 417 | if (ruleMatches(id, condition, config.manualRule)) { |
| 418 | return config.manualRule; |
| 419 | } else { |
| 420 | for (ZenRule automaticRule : config.automaticRules.values()) { |
| 421 | if (ruleMatches(id, condition, automaticRule)) { |
| 422 | return automaticRule; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | return null; |
| 427 | } |
| 428 | |
| 429 | private boolean ruleMatches(Uri id, Condition condition, ZenRule rule) { |
| 430 | if (id == null || rule == null || rule.conditionId == null) return false; |
| 431 | if (!rule.conditionId.equals(id)) return false; |
| 432 | if (Objects.equals(condition, rule.condition)) return false; |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | private boolean updateSnoozing(ZenRule rule) { |
| 437 | if (rule != null && rule.snoozing && !rule.isTrueOrUnknown()) { |
| 438 | rule.snoozing = false; |
| 439 | if (DEBUG) Log.d(TAG, "Snoozing reset for " + rule.conditionId); |
| 440 | return true; |
| 441 | } |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | public int getCurrentInstanceCount(ComponentName cn) { |
| 446 | if (cn == null) { |
| 447 | return 0; |
| 448 | } |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 449 | int count = 0; |
| 450 | synchronized (mConfig) { |
| 451 | for (ZenRule rule : mConfig.automaticRules.values()) { |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 452 | if (cn.equals(rule.component) || cn.equals(rule.configurationActivity)) { |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 453 | count++; |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | return count; |
| 458 | } |
| 459 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 460 | public boolean canManageAutomaticZenRule(ZenRule rule) { |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 461 | final int callingUid = Binder.getCallingUid(); |
| 462 | if (callingUid == 0 || callingUid == Process.SYSTEM_UID) { |
| 463 | return true; |
| 464 | } else if (mContext.checkCallingPermission(android.Manifest.permission.MANAGE_NOTIFICATIONS) |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 465 | == PackageManager.PERMISSION_GRANTED) { |
| 466 | return true; |
| 467 | } else { |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 468 | String[] packages = mPm.getPackagesForUid(Binder.getCallingUid()); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 469 | if (packages != null) { |
| 470 | final int packageCount = packages.length; |
| 471 | for (int i = 0; i < packageCount; i++) { |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 472 | if (packages[i].equals(rule.pkg)) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 473 | return true; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | return false; |
| 478 | } |
| 479 | } |
| 480 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 481 | protected void updateDefaultZenRules() { |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 482 | updateDefaultAutomaticRuleNames(); |
| 483 | for (ZenRule defaultRule : mDefaultConfig.automaticRules.values()) { |
| 484 | ZenRule currRule = mConfig.automaticRules.get(defaultRule.id); |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 485 | // if default rule wasn't user-modified nor enabled, use localized name |
| 486 | // instead of previous system name |
| 487 | if (currRule != null && !currRule.modified && !currRule.enabled |
| 488 | && !defaultRule.name.equals(currRule.name)) { |
| 489 | if (canManageAutomaticZenRule(currRule)) { |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 490 | if (DEBUG) Slog.d(TAG, "Locale change - updating default zen rule name " |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 491 | + "from " + currRule.name + " to " + defaultRule.name); |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 492 | // update default rule (if locale changed, name of rule will change) |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 493 | currRule.name = defaultRule.name; |
| 494 | updateAutomaticZenRule(defaultRule.id, createAutomaticZenRule(currRule), |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 495 | "locale changed"); |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 501 | private boolean isSystemRule(AutomaticZenRule rule) { |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 502 | return rule.getOwner() != null |
| 503 | && ZenModeConfig.SYSTEM_AUTHORITY.equals(rule.getOwner().getPackageName()); |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | private ServiceInfo getServiceInfo(ComponentName owner) { |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 507 | Intent queryIntent = new Intent(); |
| 508 | queryIntent.setComponent(owner); |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 509 | List<ResolveInfo> installedServices = mPm.queryIntentServicesAsUser( |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 510 | queryIntent, |
| 511 | PackageManager.GET_SERVICES | PackageManager.GET_META_DATA, |
| 512 | UserHandle.getCallingUserId()); |
| 513 | if (installedServices != null) { |
| 514 | for (int i = 0, count = installedServices.size(); i < count; i++) { |
| 515 | ResolveInfo resolveInfo = installedServices.get(i); |
| 516 | ServiceInfo info = resolveInfo.serviceInfo; |
| 517 | if (mServiceConfig.bindPermission.equals(info.permission)) { |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 518 | return info; |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | } |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 522 | return null; |
Julia Reynolds | 0ae2b0b | 2016-01-14 09:58:03 -0500 | [diff] [blame] | 523 | } |
| 524 | |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 525 | private ActivityInfo getActivityInfo(ComponentName configActivity) { |
| 526 | Intent queryIntent = new Intent(); |
| 527 | queryIntent.setComponent(configActivity); |
| 528 | List<ResolveInfo> installedComponents = mPm.queryIntentActivitiesAsUser( |
| 529 | queryIntent, |
| 530 | PackageManager.GET_ACTIVITIES | PackageManager.GET_META_DATA, |
| 531 | UserHandle.getCallingUserId()); |
| 532 | if (installedComponents != null) { |
| 533 | for (int i = 0, count = installedComponents.size(); i < count; i++) { |
| 534 | ResolveInfo resolveInfo = installedComponents.get(i); |
| 535 | return resolveInfo.activityInfo; |
| 536 | } |
| 537 | } |
| 538 | return null; |
| 539 | } |
| 540 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 541 | private void populateZenRule(AutomaticZenRule automaticZenRule, ZenRule rule, boolean isNew) { |
| 542 | if (isNew) { |
| 543 | rule.id = ZenModeConfig.newRuleId(); |
| 544 | rule.creationTime = System.currentTimeMillis(); |
| 545 | rule.component = automaticZenRule.getOwner(); |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 546 | rule.configurationActivity = automaticZenRule.getConfigurationActivity(); |
| 547 | rule.pkg = (rule.component != null) |
| 548 | ? rule.component.getPackageName() |
| 549 | : rule.configurationActivity.getPackageName(); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | if (rule.enabled != automaticZenRule.isEnabled()) { |
| 553 | rule.snoozing = false; |
| 554 | } |
| 555 | rule.name = automaticZenRule.getName(); |
| 556 | rule.condition = null; |
| 557 | rule.conditionId = automaticZenRule.getConditionId(); |
| 558 | rule.enabled = automaticZenRule.isEnabled(); |
Beverly | 49ba9a6 | 2018-11-02 13:19:31 -0400 | [diff] [blame] | 559 | rule.modified = automaticZenRule.isModified(); |
Beverly | 1219670 | 2018-12-12 15:05:51 -0500 | [diff] [blame] | 560 | rule.zenPolicy = automaticZenRule.getZenPolicy(); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 561 | rule.zenMode = NotificationManager.zenModeFromInterruptionFilter( |
| 562 | automaticZenRule.getInterruptionFilter(), Global.ZEN_MODE_OFF); |
| 563 | } |
| 564 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 565 | protected AutomaticZenRule createAutomaticZenRule(ZenRule rule) { |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 566 | return new AutomaticZenRule(rule.name, rule.component, rule.configurationActivity, |
| 567 | rule.conditionId, rule.zenPolicy, |
| 568 | NotificationManager.zenModeToInterruptionFilter(rule.zenMode), |
| 569 | rule.enabled, rule.creationTime); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 570 | } |
| 571 | |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 572 | public void setManualZenMode(int zenMode, Uri conditionId, String caller, String reason) { |
| 573 | setManualZenMode(zenMode, conditionId, reason, caller, true /*setRingerMode*/); |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 574 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 575 | Settings.Secure.SHOW_ZEN_SETTINGS_SUGGESTION, 0); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 576 | } |
| 577 | |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 578 | private void setManualZenMode(int zenMode, Uri conditionId, String reason, String caller, |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 579 | boolean setRingerMode) { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 580 | ZenModeConfig newConfig; |
| 581 | synchronized (mConfig) { |
| 582 | if (mConfig == null) return; |
| 583 | if (!Global.isValidZenMode(zenMode)) return; |
| 584 | if (DEBUG) Log.d(TAG, "setManualZenMode " + Global.zenModeToString(zenMode) |
| 585 | + " conditionId=" + conditionId + " reason=" + reason |
| 586 | + " setRingerMode=" + setRingerMode); |
| 587 | newConfig = mConfig.copy(); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 588 | if (zenMode == Global.ZEN_MODE_OFF) { |
| 589 | newConfig.manualRule = null; |
| 590 | for (ZenRule automaticRule : newConfig.automaticRules.values()) { |
| 591 | if (automaticRule.isAutomaticActive()) { |
| 592 | automaticRule.snoozing = true; |
| 593 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 594 | } |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 595 | } else { |
| 596 | final ZenRule newRule = new ZenRule(); |
| 597 | newRule.enabled = true; |
| 598 | newRule.zenMode = zenMode; |
| 599 | newRule.conditionId = conditionId; |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 600 | newRule.enabler = caller; |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 601 | newConfig.manualRule = newRule; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 602 | } |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 603 | setConfigLocked(newConfig, reason, null, setRingerMode); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 604 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 605 | } |
| 606 | |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 607 | void dump(ProtoOutputStream proto) { |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 608 | proto.write(ZenModeProto.ZEN_MODE, mZenMode); |
| 609 | synchronized (mConfig) { |
| 610 | if (mConfig.manualRule != null) { |
Kweku Adams | 9954633 | 2018-01-24 17:03:50 -0800 | [diff] [blame] | 611 | mConfig.manualRule.writeToProto(proto, ZenModeProto.ENABLED_ACTIVE_CONDITIONS); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 612 | } |
| 613 | for (ZenRule rule : mConfig.automaticRules.values()) { |
| 614 | if (rule.enabled && rule.condition.state == Condition.STATE_TRUE |
| 615 | && !rule.snoozing) { |
Kweku Adams | 9954633 | 2018-01-24 17:03:50 -0800 | [diff] [blame] | 616 | rule.writeToProto(proto, ZenModeProto.ENABLED_ACTIVE_CONDITIONS); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 617 | } |
| 618 | } |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 619 | mConfig.toNotificationPolicy().writeToProto(proto, ZenModeProto.POLICY); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 620 | proto.write(ZenModeProto.SUPPRESSED_EFFECTS, mSuppressedEffects); |
| 621 | } |
| 622 | } |
| 623 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 624 | public void dump(PrintWriter pw, String prefix) { |
| 625 | pw.print(prefix); pw.print("mZenMode="); |
| 626 | pw.println(Global.zenModeToString(mZenMode)); |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 627 | pw.print("mConsolidatedPolicy=" + mConsolidatedPolicy.toString()); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 628 | final int N = mConfigs.size(); |
| 629 | for (int i = 0; i < N; i++) { |
| 630 | dump(pw, prefix, "mConfigs[u=" + mConfigs.keyAt(i) + "]", mConfigs.valueAt(i)); |
| 631 | } |
| 632 | pw.print(prefix); pw.print("mUser="); pw.println(mUser); |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 633 | synchronized (mConfig) { |
| 634 | dump(pw, prefix, "mConfig", mConfig); |
| 635 | } |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 636 | |
| 637 | pw.print(prefix); pw.print("mSuppressedEffects="); pw.println(mSuppressedEffects); |
John Spurlock | 1d7d224 | 2015-04-10 08:10:22 -0400 | [diff] [blame] | 638 | mFiltering.dump(pw, prefix); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 639 | mConditions.dump(pw, prefix); |
| 640 | } |
| 641 | |
| 642 | private static void dump(PrintWriter pw, String prefix, String var, ZenModeConfig config) { |
| 643 | pw.print(prefix); pw.print(var); pw.print('='); |
| 644 | if (config == null) { |
| 645 | pw.println(config); |
| 646 | return; |
| 647 | } |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 648 | pw.printf("allow(alarms=%b,media=%b,system=%b,calls=%b,callsFrom=%s,repeatCallers=%b," |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 649 | + "messages=%b,messagesFrom=%s,events=%b,reminders=%b)\n", |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 650 | config.allowAlarms, config.allowMedia, config.allowSystem, |
Julia Reynolds | 6ee2617 | 2015-09-28 11:34:48 -0400 | [diff] [blame] | 651 | config.allowCalls, ZenModeConfig.sourceToString(config.allowCallsFrom), |
| 652 | config.allowRepeatCallers, config.allowMessages, |
| 653 | ZenModeConfig.sourceToString(config.allowMessagesFrom), |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 654 | config.allowEvents, config.allowReminders); |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 655 | pw.printf(" disallow(visualEffects=%s)\n", config.suppressedVisualEffects); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 656 | pw.print(prefix); pw.print(" manualRule="); pw.println(config.manualRule); |
| 657 | if (config.automaticRules.isEmpty()) return; |
| 658 | final int N = config.automaticRules.size(); |
| 659 | for (int i = 0; i < N; i++) { |
| 660 | pw.print(prefix); pw.print(i == 0 ? " automaticRules=" : " "); |
| 661 | pw.println(config.automaticRules.valueAt(i)); |
| 662 | } |
| 663 | } |
| 664 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 665 | public void readXml(XmlPullParser parser, boolean forRestore) |
| 666 | throws XmlPullParserException, IOException { |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 667 | ZenModeConfig config = ZenModeConfig.readXml(parser); |
| 668 | String reason = "readXml"; |
| 669 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 670 | if (config != null) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 671 | if (forRestore) { |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 672 | //TODO: http://b/22388012 |
| 673 | if (config.user != UserHandle.USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 674 | return; |
| 675 | } |
| 676 | config.manualRule = null; // don't restore the manual rule |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 677 | } |
| 678 | |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 679 | // booleans to determine whether to reset the rules to the default rules |
| 680 | boolean allRulesDisabled = true; |
| 681 | boolean hasDefaultRules = config.automaticRules.containsAll( |
| 682 | ZenModeConfig.DEFAULT_RULE_IDS); |
| 683 | |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 684 | long time = System.currentTimeMillis(); |
| 685 | if (config.automaticRules != null && config.automaticRules.size() > 0) { |
| 686 | for (ZenRule automaticRule : config.automaticRules.values()) { |
| 687 | if (forRestore) { |
John Spurlock | 995a749 | 2015-05-28 22:13:03 -0400 | [diff] [blame] | 688 | // don't restore transient state from restored automatic rules |
| 689 | automaticRule.snoozing = false; |
| 690 | automaticRule.condition = null; |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 691 | automaticRule.creationTime = time; |
John Spurlock | 995a749 | 2015-05-28 22:13:03 -0400 | [diff] [blame] | 692 | } |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 693 | |
| 694 | allRulesDisabled &= !automaticRule.enabled; |
John Spurlock | 995a749 | 2015-05-28 22:13:03 -0400 | [diff] [blame] | 695 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 696 | } |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 697 | |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 698 | if (!hasDefaultRules && allRulesDisabled |
| 699 | && (forRestore || config.version < ZenModeConfig.XML_VERSION)) { |
| 700 | // reset zen automatic rules to default on restore or upgrade if: |
| 701 | // - doesn't already have default rules and |
| 702 | // - all previous automatic rules were disabled |
| 703 | config.automaticRules = new ArrayMap<>(); |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 704 | for (ZenRule rule : mDefaultConfig.automaticRules.values()) { |
| 705 | config.automaticRules.put(rule.id, rule); |
| 706 | } |
Beverly | ad3841a | 2018-07-31 11:23:35 -0400 | [diff] [blame] | 707 | reason += ", reset to default rules"; |
| 708 | } |
| 709 | |
| 710 | if (config.version < ZenModeConfig.XML_VERSION) { |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 711 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 712 | Settings.Secure.SHOW_ZEN_UPGRADE_NOTIFICATION, 1); |
Beverly | 301e92a | 2018-04-27 09:43:05 -0400 | [diff] [blame] | 713 | } else { |
| 714 | // devices not restoring/upgrading already have updated zen settings |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 715 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 716 | Settings.Secure.ZEN_SETTINGS_UPDATED, 1); |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 717 | } |
| 718 | if (DEBUG) Log.d(TAG, reason); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 719 | synchronized (mConfig) { |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 720 | setConfigLocked(config, null, reason); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 721 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 725 | public void writeXml(XmlSerializer out, boolean forBackup, Integer version) throws IOException { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 726 | final int N = mConfigs.size(); |
| 727 | for (int i = 0; i < N; i++) { |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 728 | //TODO: http://b/22388012 |
| 729 | if (forBackup && mConfigs.keyAt(i) != UserHandle.USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 730 | continue; |
| 731 | } |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 732 | mConfigs.valueAt(i).writeXml(out, version); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 733 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 734 | } |
| 735 | |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 736 | /** |
| 737 | * @return user-specified default notification policy for priority only do not disturb |
| 738 | */ |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 739 | public Policy getNotificationPolicy() { |
| 740 | return getNotificationPolicy(mConfig); |
| 741 | } |
| 742 | |
| 743 | private static Policy getNotificationPolicy(ZenModeConfig config) { |
| 744 | return config == null ? null : config.toNotificationPolicy(); |
| 745 | } |
| 746 | |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 747 | /** |
| 748 | * Sets the global notification policy used for priority only do not disturb |
| 749 | */ |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 750 | public void setNotificationPolicy(Policy policy) { |
| 751 | if (policy == null || mConfig == null) return; |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 752 | synchronized (mConfig) { |
| 753 | final ZenModeConfig newConfig = mConfig.copy(); |
| 754 | newConfig.applyNotificationPolicy(policy); |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 755 | setConfigLocked(newConfig, null, "setNotificationPolicy"); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 756 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 757 | } |
| 758 | |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 759 | /** |
| 760 | * Removes old rule instances whose owner is not installed. |
| 761 | */ |
| 762 | private void cleanUpZenRules() { |
| 763 | long currentTime = System.currentTimeMillis(); |
| 764 | synchronized (mConfig) { |
| 765 | final ZenModeConfig newConfig = mConfig.copy(); |
| 766 | if (newConfig.automaticRules != null) { |
| 767 | for (int i = newConfig.automaticRules.size() - 1; i >= 0; i--) { |
| 768 | ZenRule rule = newConfig.automaticRules.get(newConfig.automaticRules.keyAt(i)); |
| 769 | if (RULE_INSTANCE_GRACE_PERIOD < (currentTime - rule.creationTime)) { |
| 770 | try { |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 771 | if (rule.pkg != null) { |
| 772 | mPm.getPackageInfo(rule.pkg, PackageManager.MATCH_ANY_USER); |
| 773 | } |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 774 | } catch (PackageManager.NameNotFoundException e) { |
| 775 | newConfig.automaticRules.removeAt(i); |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | } |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 780 | setConfigLocked(newConfig, null, "cleanUpZenRules"); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * @return a copy of the zen mode configuration |
| 786 | */ |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 787 | public ZenModeConfig getConfig() { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 788 | synchronized (mConfig) { |
| 789 | return mConfig.copy(); |
| 790 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 791 | } |
| 792 | |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 793 | /** |
| 794 | * @return a copy of the zen mode consolidated policy |
| 795 | */ |
| 796 | public Policy getConsolidatedNotificationPolicy() { |
| 797 | if (mConsolidatedPolicy == null) { |
| 798 | return null; |
| 799 | } |
| 800 | return mConsolidatedPolicy.copy(); |
| 801 | } |
| 802 | |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 803 | public boolean setConfigLocked(ZenModeConfig config, ComponentName triggeringComponent, |
| 804 | String reason) { |
| 805 | return setConfigLocked(config, reason, triggeringComponent, true /*setRingerMode*/); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 806 | } |
| 807 | |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 808 | public void setConfig(ZenModeConfig config, ComponentName triggeringComponent, String reason) { |
Julia Reynolds | 89aeab0 | 2016-09-15 11:07:50 -0400 | [diff] [blame] | 809 | synchronized (mConfig) { |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 810 | setConfigLocked(config, triggeringComponent, reason); |
Julia Reynolds | 89aeab0 | 2016-09-15 11:07:50 -0400 | [diff] [blame] | 811 | } |
Eric Laurent | e0ced4d | 2015-09-30 17:44:28 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 814 | private boolean setConfigLocked(ZenModeConfig config, String reason, |
| 815 | ComponentName triggeringComponent, boolean setRingerMode) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 816 | final long identity = Binder.clearCallingIdentity(); |
| 817 | try { |
| 818 | if (config == null || !config.isValid()) { |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 819 | Log.w(TAG, "Invalid config in setConfigLocked; " + config); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 820 | return false; |
| 821 | } |
| 822 | if (config.user != mUser) { |
| 823 | // simply store away for background users |
| 824 | mConfigs.put(config.user, config); |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 825 | if (DEBUG) Log.d(TAG, "setConfigLocked: store config for user " + config.user); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 826 | return true; |
| 827 | } |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 828 | // handle CPS backed conditions - danger! may modify config |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 829 | mConditions.evaluateConfig(config, null, false /*processSubscriptions*/); |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 830 | |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 831 | mConfigs.put(config.user, config); |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 832 | if (DEBUG) Log.d(TAG, "setConfigLocked reason=" + reason, new Throwable()); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 833 | ZenLog.traceConfig(reason, mConfig, config); |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 834 | |
| 835 | // send some broadcasts |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 836 | final boolean policyChanged = !Objects.equals(getNotificationPolicy(mConfig), |
| 837 | getNotificationPolicy(config)); |
Julia Reynolds | 9a25da1 | 2016-01-06 16:19:28 -0500 | [diff] [blame] | 838 | if (!config.equals(mConfig)) { |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 839 | dispatchOnConfigChanged(); |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 840 | updateConsolidatedPolicy(reason); |
Julia Reynolds | 5a43aaa | 2015-12-15 13:54:41 -0500 | [diff] [blame] | 841 | } |
| 842 | if (policyChanged) { |
| 843 | dispatchOnPolicyChanged(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 844 | } |
Julia Reynolds | 9a25da1 | 2016-01-06 16:19:28 -0500 | [diff] [blame] | 845 | mConfig = config; |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 846 | mHandler.postApplyConfig(config, reason, triggeringComponent, setRingerMode); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 847 | return true; |
Julia Reynolds | cb507b8 | 2017-09-07 13:53:40 -0400 | [diff] [blame] | 848 | } catch (SecurityException e) { |
| 849 | Log.wtf(TAG, "Invalid rule in config", e); |
| 850 | return false; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 851 | } finally { |
| 852 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 853 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 854 | } |
| 855 | |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 856 | private void applyConfig(ZenModeConfig config, String reason, |
| 857 | ComponentName triggeringComponent, boolean setRingerMode) { |
Julia Reynolds | d82e981 | 2016-04-19 13:27:41 -0400 | [diff] [blame] | 858 | final String val = Integer.toString(config.hashCode()); |
| 859 | Global.putString(mContext.getContentResolver(), Global.ZEN_MODE_CONFIG_ETAG, val); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 860 | evaluateZenMode(reason, setRingerMode); |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 861 | mConditions.evaluateConfig(config, triggeringComponent, true /*processSubscriptions*/); |
Julia Reynolds | d82e981 | 2016-04-19 13:27:41 -0400 | [diff] [blame] | 862 | } |
| 863 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 864 | private int getZenModeSetting() { |
| 865 | return Global.getInt(mContext.getContentResolver(), Global.ZEN_MODE, Global.ZEN_MODE_OFF); |
| 866 | } |
| 867 | |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 868 | @VisibleForTesting |
| 869 | protected void setZenModeSetting(int zen) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 870 | Global.putInt(mContext.getContentResolver(), Global.ZEN_MODE, zen); |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 871 | showZenUpgradeNotification(zen); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 872 | } |
| 873 | |
Julia Reynolds | 9b11fdb | 2015-07-31 09:49:55 -0400 | [diff] [blame] | 874 | private int getPreviousRingerModeSetting() { |
| 875 | return Global.getInt(mContext.getContentResolver(), |
| 876 | Global.ZEN_MODE_RINGER_LEVEL, AudioManager.RINGER_MODE_NORMAL); |
| 877 | } |
| 878 | |
| 879 | private void setPreviousRingerModeSetting(Integer previousRingerLevel) { |
| 880 | Global.putString( |
| 881 | mContext.getContentResolver(), Global.ZEN_MODE_RINGER_LEVEL, |
| 882 | previousRingerLevel == null ? null : Integer.toString(previousRingerLevel)); |
| 883 | } |
| 884 | |
Beverly | e4ee1cc | 2018-03-06 17:21:08 -0500 | [diff] [blame] | 885 | @VisibleForTesting |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 886 | protected void evaluateZenMode(String reason, boolean setRingerMode) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 887 | if (DEBUG) Log.d(TAG, "evaluateZenMode"); |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 888 | if (mConfig == null) return; |
| 889 | final int policyHashBefore = mConsolidatedPolicy == null ? 0 |
| 890 | : mConsolidatedPolicy.hashCode(); |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 891 | final int zenBefore = mZenMode; |
Julia Reynolds | 8ac6303 | 2015-08-31 15:19:43 -0400 | [diff] [blame] | 892 | final int zen = computeZenMode(); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 893 | ZenLog.traceSetZenMode(zen, reason); |
| 894 | mZenMode = zen; |
| 895 | setZenModeSetting(mZenMode); |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 896 | updateConsolidatedPolicy(reason); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 897 | updateRingerModeAffectedStreams(); |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 898 | if (setRingerMode && (zen != zenBefore || (zen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS |
| 899 | && policyHashBefore != mConsolidatedPolicy.hashCode()))) { |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 900 | applyZenToRingerMode(); |
| 901 | } |
| 902 | applyRestrictions(); |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 903 | if (zen != zenBefore) { |
Julia Reynolds | 8ac6303 | 2015-08-31 15:19:43 -0400 | [diff] [blame] | 904 | mHandler.postDispatchOnZenModeChanged(); |
| 905 | } |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 906 | } |
| 907 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 908 | private void updateRingerModeAffectedStreams() { |
| 909 | if (mAudioManager != null) { |
| 910 | mAudioManager.updateRingerModeAffectedStreamsInternal(); |
| 911 | } |
| 912 | } |
| 913 | |
Julia Reynolds | 8ac6303 | 2015-08-31 15:19:43 -0400 | [diff] [blame] | 914 | private int computeZenMode() { |
Beverly | e4ee1cc | 2018-03-06 17:21:08 -0500 | [diff] [blame] | 915 | if (mConfig == null) return Global.ZEN_MODE_OFF; |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 916 | synchronized (mConfig) { |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 917 | if (mConfig.manualRule != null) return mConfig.manualRule.zenMode; |
| 918 | int zen = Global.ZEN_MODE_OFF; |
| 919 | for (ZenRule automaticRule : mConfig.automaticRules.values()) { |
| 920 | if (automaticRule.isAutomaticActive()) { |
| 921 | if (zenSeverity(automaticRule.zenMode) > zenSeverity(zen)) { |
Beverly | 301e92a | 2018-04-27 09:43:05 -0400 | [diff] [blame] | 922 | // automatic rule triggered dnd and user hasn't seen update dnd dialog |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 923 | if (Settings.Secure.getInt(mContext.getContentResolver(), |
| 924 | Settings.Secure.ZEN_SETTINGS_SUGGESTION_VIEWED, 1) == 0) { |
| 925 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 926 | Settings.Secure.SHOW_ZEN_SETTINGS_SUGGESTION, 1); |
Beverly | 301e92a | 2018-04-27 09:43:05 -0400 | [diff] [blame] | 927 | } |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 928 | zen = automaticRule.zenMode; |
| 929 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 930 | } |
| 931 | } |
Julia Reynolds | aaf191c | 2015-10-26 15:53:28 -0400 | [diff] [blame] | 932 | return zen; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 933 | } |
John Spurlock | 8403b75 | 2014-12-10 12:47:01 -0500 | [diff] [blame] | 934 | } |
| 935 | |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 936 | private void updateConsolidatedPolicy(String reason) { |
| 937 | if (mConfig == null) return; |
| 938 | synchronized (mConfig) { |
| 939 | ZenPolicy policy = new ZenPolicy(); |
| 940 | for (ZenRule automaticRule : mConfig.automaticRules.values()) { |
| 941 | if (automaticRule.isAutomaticActive()) { |
| 942 | policy.apply(automaticRule.zenPolicy); |
| 943 | } |
| 944 | } |
| 945 | Policy newPolicy = mConfig.toNotificationPolicy(policy); |
| 946 | if (!Objects.equals(mConsolidatedPolicy, newPolicy)) { |
| 947 | mConsolidatedPolicy = newPolicy; |
| 948 | dispatchOnConsolidatedPolicyChanged(); |
| 949 | ZenLog.traceSetConsolidatedZenPolicy(mConsolidatedPolicy, reason); |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
Beverly | f3b92d2 | 2018-09-18 15:13:17 -0400 | [diff] [blame] | 954 | private void updateDefaultAutomaticRuleNames() { |
| 955 | for (ZenRule rule : mDefaultConfig.automaticRules.values()) { |
| 956 | if (ZenModeConfig.EVENTS_DEFAULT_RULE_ID.equals(rule.id)) { |
| 957 | rule.name = mContext.getResources() |
| 958 | .getString(R.string.zen_mode_default_events_name); |
| 959 | } else if (ZenModeConfig.EVERY_NIGHT_DEFAULT_RULE_ID.equals(rule.id)) { |
| 960 | rule.name = mContext.getResources() |
| 961 | .getString(R.string.zen_mode_default_every_night_name); |
| 962 | } |
| 963 | } |
Beverly | 30bfbca | 2017-10-17 14:38:20 -0400 | [diff] [blame] | 964 | } |
| 965 | |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 966 | @VisibleForTesting |
| 967 | protected void applyRestrictions() { |
Beverly | d820bc2 | 2018-01-11 15:28:33 -0500 | [diff] [blame] | 968 | final boolean zenPriorityOnly = mZenMode == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 969 | final boolean zenSilence = mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS; |
Beverly | d820bc2 | 2018-01-11 15:28:33 -0500 | [diff] [blame] | 970 | final boolean zenAlarmsOnly = mZenMode == Global.ZEN_MODE_ALARMS; |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 971 | final boolean allowCalls = mConsolidatedPolicy.allowCalls(); |
| 972 | final boolean allowRepeatCallers = mConsolidatedPolicy.allowRepeatCallers(); |
| 973 | final boolean allowSystem = mConsolidatedPolicy.allowSystem(); |
| 974 | final boolean allowMedia = mConsolidatedPolicy.allowMedia(); |
| 975 | final boolean allowAlarms = mConsolidatedPolicy.allowAlarms(); |
John Spurlock | 8403b75 | 2014-12-10 12:47:01 -0500 | [diff] [blame] | 976 | |
| 977 | // notification restrictions |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 978 | final boolean muteNotifications = |
| 979 | (mSuppressedEffects & SUPPRESSED_EFFECT_NOTIFICATIONS) != 0; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 980 | // call restrictions |
Beverly | d820bc2 | 2018-01-11 15:28:33 -0500 | [diff] [blame] | 981 | final boolean muteCalls = zenAlarmsOnly |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 982 | || (zenPriorityOnly && !allowCalls && !allowRepeatCallers) |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 983 | || (mSuppressedEffects & SUPPRESSED_EFFECT_CALLS) != 0; |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 984 | // alarm restrictions |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 985 | final boolean muteAlarms = zenPriorityOnly && !allowAlarms; |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 986 | // media restrictions |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 987 | final boolean muteMedia = zenPriorityOnly && !allowMedia; |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 988 | // system restrictions |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 989 | final boolean muteSystem = zenAlarmsOnly || (zenPriorityOnly && !allowSystem); |
Julia Reynolds | 3fe81be | 2016-02-03 09:10:06 -0500 | [diff] [blame] | 990 | // total silence restrictions |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 991 | final boolean muteEverything = zenSilence || (zenPriorityOnly |
| 992 | && ZenModeConfig.areAllZenBehaviorSoundsMuted(mConsolidatedPolicy)); |
John Spurlock | 25d01ee | 2015-06-03 12:17:46 -0400 | [diff] [blame] | 993 | |
Jean-Michel Trivi | e743bda | 2016-09-09 11:56:48 -0700 | [diff] [blame] | 994 | for (int usage : AudioAttributes.SDK_USAGES) { |
| 995 | final int suppressionBehavior = AudioAttributes.SUPPRESSIBLE_USAGES.get(usage); |
Jean-Michel Trivi | 7a84eae | 2017-06-20 14:58:18 -0700 | [diff] [blame] | 996 | if (suppressionBehavior == AudioAttributes.SUPPRESSIBLE_NEVER) { |
| 997 | applyRestrictions(false /*mute*/, usage); |
| 998 | } else if (suppressionBehavior == AudioAttributes.SUPPRESSIBLE_NOTIFICATION) { |
Jean-Michel Trivi | e743bda | 2016-09-09 11:56:48 -0700 | [diff] [blame] | 999 | applyRestrictions(muteNotifications || muteEverything, usage); |
| 1000 | } else if (suppressionBehavior == AudioAttributes.SUPPRESSIBLE_CALL) { |
| 1001 | applyRestrictions(muteCalls || muteEverything, usage); |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 1002 | } else if (suppressionBehavior == AudioAttributes.SUPPRESSIBLE_ALARM) { |
| 1003 | applyRestrictions(muteAlarms || muteEverything, usage); |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1004 | } else if (suppressionBehavior == AudioAttributes.SUPPRESSIBLE_MEDIA) { |
| 1005 | applyRestrictions(muteMedia || muteEverything, usage); |
| 1006 | } else if (suppressionBehavior == AudioAttributes.SUPPRESSIBLE_SYSTEM) { |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 1007 | if (usage == AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) { |
| 1008 | // normally DND will only restrict touch sounds, not haptic feedback/vibrations |
| 1009 | applyRestrictions(muteSystem || muteEverything, usage, |
| 1010 | AppOpsManager.OP_PLAY_AUDIO); |
| 1011 | applyRestrictions(false, usage, AppOpsManager.OP_VIBRATE); |
| 1012 | } else { |
| 1013 | applyRestrictions(muteSystem || muteEverything, usage); |
| 1014 | } |
Julia Reynolds | 3fe81be | 2016-02-03 09:10:06 -0500 | [diff] [blame] | 1015 | } else { |
Jean-Michel Trivi | e743bda | 2016-09-09 11:56:48 -0700 | [diff] [blame] | 1016 | applyRestrictions(muteEverything, usage); |
Julia Reynolds | 3fe81be | 2016-02-03 09:10:06 -0500 | [diff] [blame] | 1017 | } |
| 1018 | } |
John Spurlock | 8403b75 | 2014-12-10 12:47:01 -0500 | [diff] [blame] | 1019 | } |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 1020 | |
Beverly | e2d9a23 | 2017-11-08 18:14:59 -0500 | [diff] [blame] | 1021 | |
Beverly | 0421687 | 2017-09-28 10:55:32 -0400 | [diff] [blame] | 1022 | @VisibleForTesting |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 1023 | protected void applyRestrictions(boolean mute, int usage, int code) { |
John Spurlock | 8403b75 | 2014-12-10 12:47:01 -0500 | [diff] [blame] | 1024 | final String[] exceptionPackages = null; // none (for now) |
Beverly | e2d9a23 | 2017-11-08 18:14:59 -0500 | [diff] [blame] | 1025 | |
Dianne Hackborn | bf1b57d | 2018-03-07 12:42:47 -0800 | [diff] [blame] | 1026 | // Only do this if we are executing within the system process... otherwise |
| 1027 | // we are running as test code, so don't have access to the protected call. |
| 1028 | if (Process.myUid() == Process.SYSTEM_UID) { |
| 1029 | final long ident = Binder.clearCallingIdentity(); |
| 1030 | try { |
| 1031 | mAppOps.setRestriction(code, usage, |
| 1032 | mute ? AppOpsManager.MODE_IGNORED : AppOpsManager.MODE_ALLOWED, |
| 1033 | exceptionPackages); |
| 1034 | } finally { |
| 1035 | Binder.restoreCallingIdentity(ident); |
| 1036 | } |
| 1037 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1038 | } |
| 1039 | |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 1040 | @VisibleForTesting |
Beverly | 738bffd | 2018-03-12 10:46:17 -0400 | [diff] [blame] | 1041 | protected void applyRestrictions(boolean mute, int usage) { |
| 1042 | applyRestrictions(mute, usage, AppOpsManager.OP_VIBRATE); |
| 1043 | applyRestrictions(mute, usage, AppOpsManager.OP_PLAY_AUDIO); |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | @VisibleForTesting |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 1048 | protected void applyZenToRingerMode() { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1049 | if (mAudioManager == null) return; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1050 | // force the ringer mode into compliance |
| 1051 | final int ringerModeInternal = mAudioManager.getRingerModeInternal(); |
| 1052 | int newRingerModeInternal = ringerModeInternal; |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1053 | switch (mZenMode) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1054 | case Global.ZEN_MODE_NO_INTERRUPTIONS: |
John Spurlock | 4f1163c | 2015-04-02 17:41:21 -0400 | [diff] [blame] | 1055 | case Global.ZEN_MODE_ALARMS: |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1056 | if (ringerModeInternal != AudioManager.RINGER_MODE_SILENT) { |
Julia Reynolds | 9b11fdb | 2015-07-31 09:49:55 -0400 | [diff] [blame] | 1057 | setPreviousRingerModeSetting(ringerModeInternal); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1058 | newRingerModeInternal = AudioManager.RINGER_MODE_SILENT; |
John Spurlock | 8c01d88 | 2014-07-28 13:37:13 -0400 | [diff] [blame] | 1059 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1060 | break; |
| 1061 | case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: |
Beverly | 9e4214d | 2018-03-26 11:10:10 -0400 | [diff] [blame] | 1062 | // do not apply zen to ringer, streams zen muted in AudioService |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 1063 | break; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1064 | case Global.ZEN_MODE_OFF: |
| 1065 | if (ringerModeInternal == AudioManager.RINGER_MODE_SILENT) { |
Julia Reynolds | 9b11fdb | 2015-07-31 09:49:55 -0400 | [diff] [blame] | 1066 | newRingerModeInternal = getPreviousRingerModeSetting(); |
| 1067 | setPreviousRingerModeSetting(null); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1068 | } |
| 1069 | break; |
John Spurlock | 8c01d88 | 2014-07-28 13:37:13 -0400 | [diff] [blame] | 1070 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1071 | if (newRingerModeInternal != -1) { |
| 1072 | mAudioManager.setRingerModeInternal(newRingerModeInternal, TAG); |
| 1073 | } |
| 1074 | } |
| 1075 | |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 1076 | private void dispatchOnConfigChanged() { |
| 1077 | for (Callback callback : mCallbacks) { |
| 1078 | callback.onConfigChanged(); |
| 1079 | } |
| 1080 | } |
| 1081 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1082 | private void dispatchOnPolicyChanged() { |
| 1083 | for (Callback callback : mCallbacks) { |
| 1084 | callback.onPolicyChanged(); |
| 1085 | } |
| 1086 | } |
| 1087 | |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 1088 | private void dispatchOnConsolidatedPolicyChanged() { |
| 1089 | for (Callback callback : mCallbacks) { |
| 1090 | callback.onConsolidatedPolicyChanged(); |
| 1091 | } |
| 1092 | } |
| 1093 | |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 1094 | private void dispatchOnZenModeChanged() { |
| 1095 | for (Callback callback : mCallbacks) { |
| 1096 | callback.onZenModeChanged(); |
| 1097 | } |
| 1098 | } |
| 1099 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1100 | private ZenModeConfig readDefaultConfig(Resources resources) { |
| 1101 | XmlResourceParser parser = null; |
| 1102 | try { |
| 1103 | parser = resources.getXml(R.xml.default_zen_mode_config); |
| 1104 | while (parser.next() != XmlPullParser.END_DOCUMENT) { |
Julia Reynolds | 206c7e9 | 2016-09-15 10:38:03 -0400 | [diff] [blame] | 1105 | final ZenModeConfig config = ZenModeConfig.readXml(parser); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1106 | if (config != null) return config; |
| 1107 | } |
| 1108 | } catch (Exception e) { |
| 1109 | Log.w(TAG, "Error reading default zen mode config from resource", e); |
| 1110 | } finally { |
| 1111 | IoUtils.closeQuietly(parser); |
| 1112 | } |
| 1113 | return new ZenModeConfig(); |
| 1114 | } |
| 1115 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1116 | private static int zenSeverity(int zen) { |
| 1117 | switch (zen) { |
| 1118 | case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return 1; |
| 1119 | case Global.ZEN_MODE_ALARMS: return 2; |
| 1120 | case Global.ZEN_MODE_NO_INTERRUPTIONS: return 3; |
| 1121 | default: return 0; |
| 1122 | } |
| 1123 | } |
| 1124 | |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 1125 | @VisibleForTesting |
| 1126 | protected final class RingerModeDelegate implements AudioManagerInternal.RingerModeDelegate { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1127 | @Override |
| 1128 | public String toString() { |
| 1129 | return TAG; |
| 1130 | } |
| 1131 | |
| 1132 | @Override |
| 1133 | public int onSetRingerModeInternal(int ringerModeOld, int ringerModeNew, String caller, |
| 1134 | int ringerModeExternal, VolumePolicy policy) { |
| 1135 | final boolean isChange = ringerModeOld != ringerModeNew; |
| 1136 | |
| 1137 | int ringerModeExternalOut = ringerModeNew; |
| 1138 | |
Beverly | e4ee1cc | 2018-03-06 17:21:08 -0500 | [diff] [blame] | 1139 | if (mZenMode == Global.ZEN_MODE_OFF |
| 1140 | || (mZenMode == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS |
| 1141 | && !ZenModeConfig.areAllPriorityOnlyNotificationZenSoundsMuted(mConfig))) { |
| 1142 | // in priority only with ringer not muted, save ringer mode changes |
| 1143 | // in dnd off, save ringer mode changes |
| 1144 | setPreviousRingerModeSetting(ringerModeNew); |
| 1145 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1146 | int newZen = -1; |
| 1147 | switch (ringerModeNew) { |
| 1148 | case AudioManager.RINGER_MODE_SILENT: |
| 1149 | if (isChange && policy.doNotDisturbWhenSilent) { |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 1150 | if (mZenMode == Global.ZEN_MODE_OFF) { |
Julia Reynolds | f3252be | 2018-01-17 09:26:21 -0500 | [diff] [blame] | 1151 | newZen = Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1152 | } |
Julia Reynolds | 9b11fdb | 2015-07-31 09:49:55 -0400 | [diff] [blame] | 1153 | setPreviousRingerModeSetting(ringerModeOld); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1154 | } |
| 1155 | break; |
| 1156 | case AudioManager.RINGER_MODE_VIBRATE: |
| 1157 | case AudioManager.RINGER_MODE_NORMAL: |
| 1158 | if (isChange && ringerModeOld == AudioManager.RINGER_MODE_SILENT |
| 1159 | && (mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 1160 | || mZenMode == Global.ZEN_MODE_ALARMS |
| 1161 | || (mZenMode == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS |
| 1162 | && ZenModeConfig.areAllPriorityOnlyNotificationZenSoundsMuted( |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1163 | mConfig)))) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1164 | newZen = Global.ZEN_MODE_OFF; |
| 1165 | } else if (mZenMode != Global.ZEN_MODE_OFF) { |
| 1166 | ringerModeExternalOut = AudioManager.RINGER_MODE_SILENT; |
| 1167 | } |
| 1168 | break; |
| 1169 | } |
Beverly | 85f5241 | 2018-02-27 10:41:13 -0500 | [diff] [blame] | 1170 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1171 | if (newZen != -1) { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 1172 | setManualZenMode(newZen, null, "ringerModeInternal", null, |
| 1173 | false /*setRingerMode*/); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1174 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1175 | if (isChange || newZen != -1 || ringerModeExternal != ringerModeExternalOut) { |
| 1176 | ZenLog.traceSetRingerModeInternal(ringerModeOld, ringerModeNew, caller, |
| 1177 | ringerModeExternal, ringerModeExternalOut); |
| 1178 | } |
| 1179 | return ringerModeExternalOut; |
| 1180 | } |
| 1181 | |
| 1182 | @Override |
| 1183 | public int onSetRingerModeExternal(int ringerModeOld, int ringerModeNew, String caller, |
| 1184 | int ringerModeInternal, VolumePolicy policy) { |
| 1185 | int ringerModeInternalOut = ringerModeNew; |
| 1186 | final boolean isChange = ringerModeOld != ringerModeNew; |
| 1187 | final boolean isVibrate = ringerModeInternal == AudioManager.RINGER_MODE_VIBRATE; |
| 1188 | |
| 1189 | int newZen = -1; |
| 1190 | switch (ringerModeNew) { |
| 1191 | case AudioManager.RINGER_MODE_SILENT: |
| 1192 | if (isChange) { |
| 1193 | if (mZenMode == Global.ZEN_MODE_OFF) { |
Julia Reynolds | f3252be | 2018-01-17 09:26:21 -0500 | [diff] [blame] | 1194 | newZen = Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1195 | } |
| 1196 | ringerModeInternalOut = isVibrate ? AudioManager.RINGER_MODE_VIBRATE |
John Spurlock | 05715ec | 2015-05-13 11:19:19 -0400 | [diff] [blame] | 1197 | : AudioManager.RINGER_MODE_SILENT; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1198 | } else { |
| 1199 | ringerModeInternalOut = ringerModeInternal; |
| 1200 | } |
| 1201 | break; |
| 1202 | case AudioManager.RINGER_MODE_VIBRATE: |
| 1203 | case AudioManager.RINGER_MODE_NORMAL: |
| 1204 | if (mZenMode != Global.ZEN_MODE_OFF) { |
| 1205 | newZen = Global.ZEN_MODE_OFF; |
| 1206 | } |
| 1207 | break; |
| 1208 | } |
| 1209 | if (newZen != -1) { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 1210 | setManualZenMode(newZen, null, "ringerModeExternal", caller, |
| 1211 | false /*setRingerMode*/); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | ZenLog.traceSetRingerModeExternal(ringerModeOld, ringerModeNew, caller, |
| 1215 | ringerModeInternal, ringerModeInternalOut); |
| 1216 | return ringerModeInternalOut; |
| 1217 | } |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 1218 | |
| 1219 | @Override |
| 1220 | public boolean canVolumeDownEnterSilent() { |
| 1221 | return mZenMode == Global.ZEN_MODE_OFF; |
| 1222 | } |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1223 | |
| 1224 | @Override |
| 1225 | public int getRingerModeAffectedStreams(int streams) { |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1226 | // ringtone and notification streams are always affected by ringer mode |
| 1227 | // system stream is affected by ringer mode when not in priority-only |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1228 | streams |= (1 << AudioSystem.STREAM_RING) | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1229 | (1 << AudioSystem.STREAM_NOTIFICATION) | |
| 1230 | (1 << AudioSystem.STREAM_SYSTEM); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1231 | |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1232 | if (mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS) { |
Beverly | 925cde8 | 2018-01-23 09:31:23 -0500 | [diff] [blame] | 1233 | // alarm and music streams affected by ringer mode when in total silence |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1234 | streams |= (1 << AudioSystem.STREAM_ALARM) | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1235 | (1 << AudioSystem.STREAM_MUSIC); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1236 | } else { |
| 1237 | streams &= ~((1 << AudioSystem.STREAM_ALARM) | |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 1238 | (1 << AudioSystem.STREAM_MUSIC)); |
| 1239 | } |
| 1240 | |
| 1241 | if (mZenMode == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS |
| 1242 | && ZenModeConfig.areAllPriorityOnlyNotificationZenSoundsMuted(mConfig)) { |
| 1243 | // system stream is not affected by ringer mode in priority only when the ringer |
| 1244 | // is zen muted (all other notification categories are muted) |
| 1245 | streams &= ~(1 << AudioSystem.STREAM_SYSTEM); |
| 1246 | } else { |
| 1247 | streams |= (1 << AudioSystem.STREAM_SYSTEM); |
John Spurlock | 50ced3f | 2015-05-11 16:00:09 -0400 | [diff] [blame] | 1248 | } |
| 1249 | return streams; |
| 1250 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | private final class SettingsObserver extends ContentObserver { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1254 | private final Uri ZEN_MODE = Global.getUriFor(Global.ZEN_MODE); |
| 1255 | |
| 1256 | public SettingsObserver(Handler handler) { |
| 1257 | super(handler); |
| 1258 | } |
| 1259 | |
| 1260 | public void observe() { |
| 1261 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1262 | resolver.registerContentObserver(ZEN_MODE, false /*notifyForDescendents*/, this); |
| 1263 | update(null); |
| 1264 | } |
| 1265 | |
| 1266 | @Override |
| 1267 | public void onChange(boolean selfChange, Uri uri) { |
| 1268 | update(uri); |
| 1269 | } |
| 1270 | |
| 1271 | public void update(Uri uri) { |
| 1272 | if (ZEN_MODE.equals(uri)) { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1273 | if (mZenMode != getZenModeSetting()) { |
| 1274 | if (DEBUG) Log.d(TAG, "Fixing zen mode setting"); |
| 1275 | setZenModeSetting(mZenMode); |
| 1276 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 1281 | private void showZenUpgradeNotification(int zen) { |
Nick | a485ec6 | 2018-07-03 11:32:39 -0700 | [diff] [blame] | 1282 | final boolean isWatch = mContext.getPackageManager().hasSystemFeature( |
| 1283 | PackageManager.FEATURE_WATCH); |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 1284 | final boolean showNotification = mIsBootComplete |
Julia Reynolds | 76bfa60 | 2018-04-23 09:38:47 -0400 | [diff] [blame] | 1285 | && zen != Global.ZEN_MODE_OFF |
Nick | a485ec6 | 2018-07-03 11:32:39 -0700 | [diff] [blame] | 1286 | && !isWatch |
Beverly | 9087cd1 | 2018-09-21 15:30:49 -0400 | [diff] [blame] | 1287 | && Settings.Secure.getInt(mContext.getContentResolver(), |
Beverly | 6c60b90 | 2018-08-17 10:10:46 -0400 | [diff] [blame] | 1288 | Settings.Secure.SHOW_ZEN_UPGRADE_NOTIFICATION, 0) != 0 |
Beverly | 9087cd1 | 2018-09-21 15:30:49 -0400 | [diff] [blame] | 1289 | && Settings.Secure.getInt(mContext.getContentResolver(), |
Beverly | 6c60b90 | 2018-08-17 10:10:46 -0400 | [diff] [blame] | 1290 | Settings.Secure.ZEN_SETTINGS_UPDATED, 0) != 1; |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 1291 | |
Nick | a485ec6 | 2018-07-03 11:32:39 -0700 | [diff] [blame] | 1292 | if (isWatch) { |
Beverly | 9087cd1 | 2018-09-21 15:30:49 -0400 | [diff] [blame] | 1293 | Settings.Secure.putInt(mContext.getContentResolver(), |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 1294 | Settings.Secure.SHOW_ZEN_UPGRADE_NOTIFICATION, 0); |
Nick | a485ec6 | 2018-07-03 11:32:39 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 1297 | if (showNotification) { |
| 1298 | mNotificationManager.notify(TAG, SystemMessage.NOTE_ZEN_UPGRADE, |
| 1299 | createZenUpgradeNotification()); |
Beverly | 91d0a63 | 2018-07-02 16:45:00 -0400 | [diff] [blame] | 1300 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 1301 | Settings.Secure.SHOW_ZEN_UPGRADE_NOTIFICATION, 0); |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | @VisibleForTesting |
| 1306 | protected Notification createZenUpgradeNotification() { |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 1307 | final Bundle extras = new Bundle(); |
| 1308 | extras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME, |
| 1309 | mContext.getResources().getString(R.string.global_action_settings)); |
Julia Reynolds | 0b8455d | 2018-03-14 11:38:44 -0400 | [diff] [blame] | 1310 | int title = R.string.zen_upgrade_notification_title; |
| 1311 | int content = R.string.zen_upgrade_notification_content; |
Julia Reynolds | 76bfa60 | 2018-04-23 09:38:47 -0400 | [diff] [blame] | 1312 | int drawable = R.drawable.ic_zen_24dp; |
Julia Reynolds | 0b8455d | 2018-03-14 11:38:44 -0400 | [diff] [blame] | 1313 | if (NotificationManager.Policy.areAllVisualEffectsSuppressed( |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 1314 | getConsolidatedNotificationPolicy().suppressedVisualEffects)) { |
Julia Reynolds | 0b8455d | 2018-03-14 11:38:44 -0400 | [diff] [blame] | 1315 | title = R.string.zen_upgrade_notification_visd_title; |
| 1316 | content = R.string.zen_upgrade_notification_visd_content; |
Julia Reynolds | 76bfa60 | 2018-04-23 09:38:47 -0400 | [diff] [blame] | 1317 | drawable = R.drawable.ic_dnd_block_notifications; |
Julia Reynolds | 0b8455d | 2018-03-14 11:38:44 -0400 | [diff] [blame] | 1318 | } |
Julia Reynolds | 76bfa60 | 2018-04-23 09:38:47 -0400 | [diff] [blame] | 1319 | |
Julia Reynolds | 9aa1c9e | 2018-04-09 11:31:15 -0400 | [diff] [blame] | 1320 | Intent onboardingIntent = new Intent(Settings.ZEN_MODE_ONBOARDING); |
| 1321 | onboardingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
Julia Reynolds | 0b8455d | 2018-03-14 11:38:44 -0400 | [diff] [blame] | 1322 | return new Notification.Builder(mContext, SystemNotificationChannels.DO_NOT_DISTURB) |
Julia Reynolds | 9aa1c9e | 2018-04-09 11:31:15 -0400 | [diff] [blame] | 1323 | .setAutoCancel(true) |
Beverly | d8b15ee | 2018-02-13 14:37:38 -0500 | [diff] [blame] | 1324 | .setSmallIcon(R.drawable.ic_settings_24dp) |
Julia Reynolds | 76bfa60 | 2018-04-23 09:38:47 -0400 | [diff] [blame] | 1325 | .setLargeIcon(Icon.createWithResource(mContext, drawable)) |
Julia Reynolds | 0b8455d | 2018-03-14 11:38:44 -0400 | [diff] [blame] | 1326 | .setContentTitle(mContext.getResources().getString(title)) |
| 1327 | .setContentText(mContext.getResources().getString(content)) |
Julia Reynolds | 9aa1c9e | 2018-04-09 11:31:15 -0400 | [diff] [blame] | 1328 | .setContentIntent(PendingIntent.getActivity(mContext, 0, onboardingIntent, |
| 1329 | PendingIntent.FLAG_UPDATE_CURRENT)) |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 1330 | .setAutoCancel(true) |
| 1331 | .setLocalOnly(true) |
| 1332 | .addExtras(extras) |
Julia Reynolds | 0b8455d | 2018-03-14 11:38:44 -0400 | [diff] [blame] | 1333 | .setStyle(new Notification.BigTextStyle()) |
Beverly | 3bae4e5 | 2018-02-07 12:32:02 -0500 | [diff] [blame] | 1334 | .build(); |
| 1335 | } |
| 1336 | |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1337 | private final class Metrics extends Callback { |
Julia Reynolds | 3dfdde0 | 2018-10-08 09:17:56 -0400 | [diff] [blame] | 1338 | private static final String COUNTER_MODE_PREFIX = "dnd_mode_"; |
| 1339 | private static final String COUNTER_TYPE_PREFIX = "dnd_type_"; |
| 1340 | private static final int DND_OFF = 0; |
| 1341 | private static final int DND_ON_MANUAL = 1; |
| 1342 | private static final int DND_ON_AUTOMATIC = 2; |
| 1343 | private static final String COUNTER_RULE = "dnd_rule_count"; |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1344 | private static final long MINIMUM_LOG_PERIOD_MS = 60 * 1000; |
| 1345 | |
Julia Reynolds | 3dfdde0 | 2018-10-08 09:17:56 -0400 | [diff] [blame] | 1346 | // Total silence, alarms only, priority only |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1347 | private int mPreviousZenMode = -1; |
Julia Reynolds | 3dfdde0 | 2018-10-08 09:17:56 -0400 | [diff] [blame] | 1348 | private long mModeLogTimeMs = 0L; |
| 1349 | |
| 1350 | private int mNumZenRules = -1; |
| 1351 | private long mRuleCountLogTime = 0L; |
| 1352 | |
| 1353 | // automatic (1) vs manual (0) vs dnd off (2) |
| 1354 | private int mPreviousZenType = -1; |
| 1355 | private long mTypeLogTimeMs = 0L; |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1356 | |
| 1357 | @Override |
| 1358 | void onZenModeChanged() { |
| 1359 | emit(); |
| 1360 | } |
| 1361 | |
Julia Reynolds | 3dfdde0 | 2018-10-08 09:17:56 -0400 | [diff] [blame] | 1362 | @Override |
| 1363 | void onConfigChanged() { |
| 1364 | emit(); |
| 1365 | } |
| 1366 | |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1367 | private void emit() { |
| 1368 | mHandler.postMetricsTimer(); |
Julia Reynolds | 3dfdde0 | 2018-10-08 09:17:56 -0400 | [diff] [blame] | 1369 | emitZenMode(); |
| 1370 | emitRules(); |
| 1371 | emitDndType(); |
| 1372 | } |
| 1373 | |
| 1374 | private void emitZenMode() { |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1375 | final long now = SystemClock.elapsedRealtime(); |
Julia Reynolds | 3dfdde0 | 2018-10-08 09:17:56 -0400 | [diff] [blame] | 1376 | final long since = (now - mModeLogTimeMs); |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1377 | if (mPreviousZenMode != mZenMode || since > MINIMUM_LOG_PERIOD_MS) { |
| 1378 | if (mPreviousZenMode != -1) { |
Julia Reynolds | 3dfdde0 | 2018-10-08 09:17:56 -0400 | [diff] [blame] | 1379 | MetricsLogger.count( |
| 1380 | mContext, COUNTER_MODE_PREFIX + mPreviousZenMode, (int) since); |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1381 | } |
| 1382 | mPreviousZenMode = mZenMode; |
Julia Reynolds | 3dfdde0 | 2018-10-08 09:17:56 -0400 | [diff] [blame] | 1383 | mModeLogTimeMs = now; |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | private void emitRules() { |
| 1388 | final long now = SystemClock.elapsedRealtime(); |
| 1389 | final long since = (now - mRuleCountLogTime); |
| 1390 | synchronized (mConfig) { |
| 1391 | int numZenRules = mConfig.automaticRules.size(); |
| 1392 | if (mNumZenRules != numZenRules |
| 1393 | || since > MINIMUM_LOG_PERIOD_MS) { |
| 1394 | if (mNumZenRules != -1) { |
| 1395 | MetricsLogger.count(mContext, COUNTER_RULE, |
| 1396 | numZenRules - mNumZenRules); |
| 1397 | } |
| 1398 | mNumZenRules = numZenRules; |
| 1399 | |
| 1400 | mRuleCountLogTime = since; |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | private void emitDndType() { |
| 1406 | final long now = SystemClock.elapsedRealtime(); |
| 1407 | final long since = (now - mTypeLogTimeMs); |
| 1408 | synchronized (mConfig) { |
| 1409 | boolean dndOn = mZenMode != Global.ZEN_MODE_OFF; |
| 1410 | int zenType = !dndOn ? DND_OFF |
| 1411 | : (mConfig.manualRule != null) ? DND_ON_MANUAL : DND_ON_AUTOMATIC; |
| 1412 | if (zenType != mPreviousZenType |
| 1413 | || since > MINIMUM_LOG_PERIOD_MS) { |
| 1414 | if (mPreviousZenType != -1) { |
| 1415 | MetricsLogger.count( |
| 1416 | mContext, COUNTER_TYPE_PREFIX + mPreviousZenType, (int) since); |
| 1417 | } |
| 1418 | mTypeLogTimeMs = now; |
| 1419 | mPreviousZenType = zenType; |
| 1420 | } |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | } |
| 1424 | |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1425 | private final class H extends Handler { |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1426 | private static final int MSG_DISPATCH = 1; |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1427 | private static final int MSG_METRICS = 2; |
Julia Reynolds | d82e981 | 2016-04-19 13:27:41 -0400 | [diff] [blame] | 1428 | private static final int MSG_APPLY_CONFIG = 4; |
Eric Laurent | e0ced4d | 2015-09-30 17:44:28 -0700 | [diff] [blame] | 1429 | |
| 1430 | private final class ConfigMessageData { |
| 1431 | public final ZenModeConfig config; |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 1432 | public ComponentName triggeringComponent; |
Eric Laurent | e0ced4d | 2015-09-30 17:44:28 -0700 | [diff] [blame] | 1433 | public final String reason; |
Julia Reynolds | d82e981 | 2016-04-19 13:27:41 -0400 | [diff] [blame] | 1434 | public final boolean setRingerMode; |
Eric Laurent | e0ced4d | 2015-09-30 17:44:28 -0700 | [diff] [blame] | 1435 | |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 1436 | ConfigMessageData(ZenModeConfig config, String reason, |
| 1437 | ComponentName triggeringComponent, boolean setRingerMode) { |
Julia Reynolds | d82e981 | 2016-04-19 13:27:41 -0400 | [diff] [blame] | 1438 | this.config = config; |
| 1439 | this.reason = reason; |
| 1440 | this.setRingerMode = setRingerMode; |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 1441 | this.triggeringComponent = triggeringComponent; |
Eric Laurent | e0ced4d | 2015-09-30 17:44:28 -0700 | [diff] [blame] | 1442 | } |
| 1443 | } |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1444 | |
| 1445 | private static final long METRICS_PERIOD_MS = 6 * 60 * 60 * 1000; |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1446 | |
| 1447 | private H(Looper looper) { |
| 1448 | super(looper); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1449 | } |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1450 | |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1451 | private void postDispatchOnZenModeChanged() { |
| 1452 | removeMessages(MSG_DISPATCH); |
| 1453 | sendEmptyMessage(MSG_DISPATCH); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1454 | } |
| 1455 | |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1456 | private void postMetricsTimer() { |
| 1457 | removeMessages(MSG_METRICS); |
| 1458 | sendEmptyMessageDelayed(MSG_METRICS, METRICS_PERIOD_MS); |
| 1459 | } |
| 1460 | |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 1461 | private void postApplyConfig(ZenModeConfig config, String reason, |
| 1462 | ComponentName triggeringComponent, boolean setRingerMode) { |
Julia Reynolds | d82e981 | 2016-04-19 13:27:41 -0400 | [diff] [blame] | 1463 | sendMessage(obtainMessage(MSG_APPLY_CONFIG, |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 1464 | new ConfigMessageData(config, reason, triggeringComponent, setRingerMode))); |
Julia Reynolds | d82e981 | 2016-04-19 13:27:41 -0400 | [diff] [blame] | 1465 | } |
| 1466 | |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1467 | @Override |
| 1468 | public void handleMessage(Message msg) { |
John Spurlock | 5762779 | 2014-12-11 11:29:54 -0500 | [diff] [blame] | 1469 | switch (msg.what) { |
| 1470 | case MSG_DISPATCH: |
| 1471 | dispatchOnZenModeChanged(); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1472 | break; |
Chris Wren | 98d235b | 2015-05-27 18:25:17 -0400 | [diff] [blame] | 1473 | case MSG_METRICS: |
| 1474 | mMetrics.emit(); |
| 1475 | break; |
Julia Reynolds | d82e981 | 2016-04-19 13:27:41 -0400 | [diff] [blame] | 1476 | case MSG_APPLY_CONFIG: |
| 1477 | ConfigMessageData applyConfigData = (ConfigMessageData) msg.obj; |
| 1478 | applyConfig(applyConfigData.config, applyConfigData.reason, |
Julia Reynolds | 8f05600 | 2018-07-13 15:12:29 -0400 | [diff] [blame] | 1479 | applyConfigData.triggeringComponent, applyConfigData.setRingerMode); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1480 | } |
| 1481 | } |
| 1482 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1483 | |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 1484 | public static class Callback { |
| 1485 | void onConfigChanged() {} |
| 1486 | void onZenModeChanged() {} |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1487 | void onPolicyChanged() {} |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 1488 | void onConsolidatedPolicyChanged() {} |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1489 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1490 | } |