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