blob: 3f1a2487fe2f3bc06ebfcfd8afc496941485b24d [file] [log] [blame]
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001/**
2 * Copyright (c) 2018, 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
17package com.android.server.notification;
18
19import static android.app.NotificationManager.IMPORTANCE_NONE;
20
21import android.annotation.IntDef;
22import android.annotation.NonNull;
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -040023import android.annotation.Nullable;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -040024import android.app.Notification;
25import android.app.NotificationChannel;
26import android.app.NotificationChannelGroup;
27import android.app.NotificationManager;
28import android.content.Context;
29import android.content.pm.ApplicationInfo;
30import android.content.pm.PackageManager;
31import android.content.pm.ParceledListSlice;
32import android.metrics.LogMaker;
33import android.os.Build;
34import android.os.UserHandle;
35import android.provider.Settings;
36import android.service.notification.NotificationListenerService;
37import android.service.notification.RankingHelperProto;
38import android.text.TextUtils;
39import android.util.ArrayMap;
Julia Reynolds0c245002019-03-27 16:10:11 -040040import android.util.ArraySet;
Julia Reynoldse7ca31b2019-04-25 15:41:47 -040041import android.util.Pair;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -040042import android.util.Slog;
43import android.util.SparseBooleanArray;
44import android.util.proto.ProtoOutputStream;
45
46import com.android.internal.R;
47import com.android.internal.annotations.VisibleForTesting;
48import com.android.internal.logging.MetricsLogger;
49import com.android.internal.util.Preconditions;
50import com.android.internal.util.XmlUtils;
51
52import org.json.JSONArray;
53import org.json.JSONException;
54import org.json.JSONObject;
55import org.xmlpull.v1.XmlPullParser;
56import org.xmlpull.v1.XmlPullParserException;
57import org.xmlpull.v1.XmlSerializer;
58
59import java.io.IOException;
60import java.io.PrintWriter;
61import java.util.ArrayList;
62import java.util.Arrays;
63import java.util.Collection;
64import java.util.List;
65import java.util.Map;
66import java.util.Objects;
67import java.util.concurrent.ConcurrentHashMap;
68
69public class PreferencesHelper implements RankingConfig {
70 private static final String TAG = "NotificationPrefHelper";
71 private static final int XML_VERSION = 1;
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -040072 private static final int UNKNOWN_UID = UserHandle.USER_NULL;
Julia Reynolds413ba842019-01-11 10:38:08 -050073 private static final String NON_BLOCKABLE_CHANNEL_DELIM = ":";
Aaron Heuckrothe5bec152018-07-09 16:26:09 -040074
75 @VisibleForTesting
76 static final String TAG_RANKING = "ranking";
77 private static final String TAG_PACKAGE = "package";
78 private static final String TAG_CHANNEL = "channel";
79 private static final String TAG_GROUP = "channelGroup";
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -040080 private static final String TAG_DELEGATE = "delegate";
Julia Reynolds12ad7ca2019-01-28 09:29:16 -050081 private static final String TAG_STATUS_ICONS = "status_icons";
Aaron Heuckrothe5bec152018-07-09 16:26:09 -040082
83 private static final String ATT_VERSION = "version";
84 private static final String ATT_NAME = "name";
85 private static final String ATT_UID = "uid";
86 private static final String ATT_ID = "id";
Mady Mellorc39b4ae2019-01-09 17:11:37 -080087 private static final String ATT_ALLOW_BUBBLE = "allow_bubble";
Aaron Heuckrothe5bec152018-07-09 16:26:09 -040088 private static final String ATT_PRIORITY = "priority";
89 private static final String ATT_VISIBILITY = "visibility";
90 private static final String ATT_IMPORTANCE = "importance";
91 private static final String ATT_SHOW_BADGE = "show_badge";
92 private static final String ATT_APP_USER_LOCKED_FIELDS = "app_user_locked_fields";
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -040093 private static final String ATT_ENABLED = "enabled";
94 private static final String ATT_USER_ALLOWED = "allowed";
Julia Reynolds25692c42019-05-03 15:01:24 -040095 private static final String ATT_HIDE_SILENT = "hide_gentle";
Aaron Heuckrothe5bec152018-07-09 16:26:09 -040096
97 private static final int DEFAULT_PRIORITY = Notification.PRIORITY_DEFAULT;
98 private static final int DEFAULT_VISIBILITY = NotificationManager.VISIBILITY_NO_OVERRIDE;
99 private static final int DEFAULT_IMPORTANCE = NotificationManager.IMPORTANCE_UNSPECIFIED;
Julia Reynolds12ad7ca2019-01-28 09:29:16 -0500100 @VisibleForTesting
Julia Reynolds25692c42019-05-03 15:01:24 -0400101 static final boolean DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS = false;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400102 private static final boolean DEFAULT_SHOW_BADGE = true;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800103 private static final boolean DEFAULT_ALLOW_BUBBLE = true;
Julia Reynolds413ba842019-01-11 10:38:08 -0500104 private static final boolean DEFAULT_OEM_LOCKED_IMPORTANCE = false;
Julia Reynolds0c245002019-03-27 16:10:11 -0400105 private static final boolean DEFAULT_APP_LOCKED_IMPORTANCE = false;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800106
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400107 /**
108 * Default value for what fields are user locked. See {@link LockableAppFields} for all lockable
109 * fields.
110 */
111 private static final int DEFAULT_LOCKED_APP_FIELDS = 0;
112
113 /**
114 * All user-lockable fields for a given application.
115 */
116 @IntDef({LockableAppFields.USER_LOCKED_IMPORTANCE})
117 public @interface LockableAppFields {
118 int USER_LOCKED_IMPORTANCE = 0x00000001;
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800119 int USER_LOCKED_BUBBLE = 0x00000002;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400120 }
121
122 // pkg|uid => PackagePreferences
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400123 private final ArrayMap<String, PackagePreferences> mPackagePreferences = new ArrayMap<>();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400124 // pkg => PackagePreferences
125 private final ArrayMap<String, PackagePreferences> mRestoredWithoutUids = new ArrayMap<>();
126
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400127 private final Context mContext;
128 private final PackageManager mPm;
129 private final RankingHandler mRankingHandler;
130 private final ZenModeHelper mZenModeHelper;
131
132 private SparseBooleanArray mBadgingEnabled;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500133 private SparseBooleanArray mBubblesEnabled;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400134 private boolean mAreChannelsBypassingDnd;
Julia Reynolds2594b472019-04-03 13:30:16 -0400135 private boolean mHideSilentStatusBarIcons = DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400136
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400137 public PreferencesHelper(Context context, PackageManager pm, RankingHandler rankingHandler,
138 ZenModeHelper zenHelper) {
139 mContext = context;
140 mZenModeHelper = zenHelper;
141 mRankingHandler = rankingHandler;
142 mPm = pm;
143
144 updateBadgingEnabled();
Julia Reynolds4509ce72019-01-31 13:12:43 -0500145 updateBubblesEnabled();
Beverly0479cde22018-11-09 11:05:34 -0500146 syncChannelsBypassingDnd(mContext.getUserId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400147 }
148
Annie Meng8b646fd2019-02-01 18:46:42 +0000149 public void readXml(XmlPullParser parser, boolean forRestore, int userId)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400150 throws XmlPullParserException, IOException {
151 int type = parser.getEventType();
152 if (type != XmlPullParser.START_TAG) return;
153 String tag = parser.getName();
154 if (!TAG_RANKING.equals(tag)) return;
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400155 synchronized (mPackagePreferences) {
Julia Reynolds12ad7ca2019-01-28 09:29:16 -0500156 // Clobber groups and channels with the xml, but don't delete other data that wasn't
157 // present at the time of serialization.
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400158 mRestoredWithoutUids.clear();
159 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
160 tag = parser.getName();
161 if (type == XmlPullParser.END_TAG && TAG_RANKING.equals(tag)) {
162 return;
163 }
164 if (type == XmlPullParser.START_TAG) {
Julia Reynolds12ad7ca2019-01-28 09:29:16 -0500165 if (TAG_STATUS_ICONS.equals(tag)) {
Annie Meng8b646fd2019-02-01 18:46:42 +0000166 if (forRestore && userId != UserHandle.USER_SYSTEM) {
167 continue;
168 }
Julia Reynolds12ad7ca2019-01-28 09:29:16 -0500169 mHideSilentStatusBarIcons = XmlUtils.readBooleanAttribute(
170 parser, ATT_HIDE_SILENT, DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS);
171 } else if (TAG_PACKAGE.equals(tag)) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400172 int uid = XmlUtils.readIntAttribute(parser, ATT_UID, UNKNOWN_UID);
173 String name = parser.getAttributeValue(null, ATT_NAME);
174 if (!TextUtils.isEmpty(name)) {
175 if (forRestore) {
176 try {
Annie Meng8b646fd2019-02-01 18:46:42 +0000177 uid = mPm.getPackageUidAsUser(name, userId);
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400178 } catch (PackageManager.NameNotFoundException e) {
179 // noop
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400180 }
181 }
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400182
Julia Reynolds5c399c62019-04-08 14:42:53 -0400183 PackagePreferences r = getOrCreatePackagePreferencesLocked(name, uid,
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400184 XmlUtils.readIntAttribute(
185 parser, ATT_IMPORTANCE, DEFAULT_IMPORTANCE),
186 XmlUtils.readIntAttribute(parser, ATT_PRIORITY,
187 DEFAULT_PRIORITY),
188 XmlUtils.readIntAttribute(
189 parser, ATT_VISIBILITY, DEFAULT_VISIBILITY),
190 XmlUtils.readBooleanAttribute(
Julia Reynolds33ab8a02018-12-17 16:19:52 -0500191 parser, ATT_SHOW_BADGE, DEFAULT_SHOW_BADGE),
192 XmlUtils.readBooleanAttribute(
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800193 parser, ATT_ALLOW_BUBBLE, DEFAULT_ALLOW_BUBBLE));
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400194 r.importance = XmlUtils.readIntAttribute(
195 parser, ATT_IMPORTANCE, DEFAULT_IMPORTANCE);
196 r.priority = XmlUtils.readIntAttribute(
197 parser, ATT_PRIORITY, DEFAULT_PRIORITY);
198 r.visibility = XmlUtils.readIntAttribute(
199 parser, ATT_VISIBILITY, DEFAULT_VISIBILITY);
200 r.showBadge = XmlUtils.readBooleanAttribute(
201 parser, ATT_SHOW_BADGE, DEFAULT_SHOW_BADGE);
202 r.lockedAppFields = XmlUtils.readIntAttribute(parser,
203 ATT_APP_USER_LOCKED_FIELDS, DEFAULT_LOCKED_APP_FIELDS);
204
205 final int innerDepth = parser.getDepth();
206 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
207 && (type != XmlPullParser.END_TAG
208 || parser.getDepth() > innerDepth)) {
209 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
210 continue;
211 }
212
213 String tagName = parser.getName();
214 // Channel groups
215 if (TAG_GROUP.equals(tagName)) {
216 String id = parser.getAttributeValue(null, ATT_ID);
217 CharSequence groupName = parser.getAttributeValue(null,
218 ATT_NAME);
219 if (!TextUtils.isEmpty(id)) {
220 NotificationChannelGroup group
221 = new NotificationChannelGroup(id, groupName);
222 group.populateFromXml(parser);
223 r.groups.put(id, group);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400224 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400225 }
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400226 // Channels
227 if (TAG_CHANNEL.equals(tagName)) {
228 String id = parser.getAttributeValue(null, ATT_ID);
229 String channelName = parser.getAttributeValue(null, ATT_NAME);
230 int channelImportance = XmlUtils.readIntAttribute(
231 parser, ATT_IMPORTANCE, DEFAULT_IMPORTANCE);
232 if (!TextUtils.isEmpty(id) && !TextUtils.isEmpty(channelName)) {
233 NotificationChannel channel = new NotificationChannel(id,
234 channelName, channelImportance);
235 if (forRestore) {
236 channel.populateFromXmlForRestore(parser, mContext);
237 } else {
238 channel.populateFromXml(parser);
239 }
Julia Reynoldse7ca31b2019-04-25 15:41:47 -0400240 channel.setImportanceLockedByCriticalDeviceFunction(
241 r.defaultAppLockedImportance);
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400242 r.channels.put(id, channel);
243 }
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400244 }
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400245 // Delegate
246 if (TAG_DELEGATE.equals(tagName)) {
247 int delegateId =
248 XmlUtils.readIntAttribute(parser, ATT_UID, UNKNOWN_UID);
249 String delegateName =
250 XmlUtils.readStringAttribute(parser, ATT_NAME);
251 boolean delegateEnabled = XmlUtils.readBooleanAttribute(
252 parser, ATT_ENABLED, Delegate.DEFAULT_ENABLED);
253 boolean userAllowed = XmlUtils.readBooleanAttribute(
254 parser, ATT_USER_ALLOWED,
255 Delegate.DEFAULT_USER_ALLOWED);
256 Delegate d = null;
257 if (delegateId != UNKNOWN_UID && !TextUtils.isEmpty(
258 delegateName)) {
259 d = new Delegate(
260 delegateName, delegateId, delegateEnabled,
261 userAllowed);
262 }
263 r.delegate = d;
264 }
265
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400266 }
267
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400268 try {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400269 deleteDefaultChannelIfNeededLocked(r);
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400270 } catch (PackageManager.NameNotFoundException e) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400271 Slog.e(TAG, "deleteDefaultChannelIfNeededLocked - Exception: " + e);
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400272 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400273 }
274 }
275 }
276 }
277 }
278 throw new IllegalStateException("Failed to reach END_DOCUMENT");
279 }
280
Julia Reynolds5c399c62019-04-08 14:42:53 -0400281 private PackagePreferences getPackagePreferencesLocked(String pkg, int uid) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400282 final String key = packagePreferencesKey(pkg, uid);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400283 return mPackagePreferences.get(key);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400284 }
285
Julia Reynolds5c399c62019-04-08 14:42:53 -0400286 private PackagePreferences getOrCreatePackagePreferencesLocked(String pkg, int uid) {
287 return getOrCreatePackagePreferencesLocked(pkg, uid,
Julia Reynolds33ab8a02018-12-17 16:19:52 -0500288 DEFAULT_IMPORTANCE, DEFAULT_PRIORITY, DEFAULT_VISIBILITY, DEFAULT_SHOW_BADGE,
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800289 DEFAULT_ALLOW_BUBBLE);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400290 }
291
Julia Reynolds5c399c62019-04-08 14:42:53 -0400292 private PackagePreferences getOrCreatePackagePreferencesLocked(String pkg, int uid,
293 int importance, int priority, int visibility, boolean showBadge, boolean allowBubble) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400294 final String key = packagePreferencesKey(pkg, uid);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400295 PackagePreferences
296 r = (uid == UNKNOWN_UID) ? mRestoredWithoutUids.get(pkg)
297 : mPackagePreferences.get(key);
298 if (r == null) {
299 r = new PackagePreferences();
300 r.pkg = pkg;
301 r.uid = uid;
302 r.importance = importance;
303 r.priority = priority;
304 r.visibility = visibility;
305 r.showBadge = showBadge;
306 r.allowBubble = allowBubble;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400307
Julia Reynolds5c399c62019-04-08 14:42:53 -0400308 try {
309 createDefaultChannelIfNeededLocked(r);
310 } catch (PackageManager.NameNotFoundException e) {
311 Slog.e(TAG, "createDefaultChannelIfNeededLocked - Exception: " + e);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400312 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400313
314 if (r.uid == UNKNOWN_UID) {
315 mRestoredWithoutUids.put(pkg, r);
316 } else {
317 mPackagePreferences.put(key, r);
318 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400319 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400320 return r;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400321 }
322
323 private boolean shouldHaveDefaultChannel(PackagePreferences r) throws
324 PackageManager.NameNotFoundException {
325 final int userId = UserHandle.getUserId(r.uid);
326 final ApplicationInfo applicationInfo =
327 mPm.getApplicationInfoAsUser(r.pkg, 0, userId);
328 if (applicationInfo.targetSdkVersion >= Build.VERSION_CODES.O) {
329 // O apps should not have the default channel.
330 return false;
331 }
332
333 // Otherwise, this app should have the default channel.
334 return true;
335 }
336
Julia Reynolds5c399c62019-04-08 14:42:53 -0400337 private void deleteDefaultChannelIfNeededLocked(PackagePreferences r) throws
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400338 PackageManager.NameNotFoundException {
339 if (!r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) {
340 // Not present
341 return;
342 }
343
344 if (shouldHaveDefaultChannel(r)) {
345 // Keep the default channel until upgraded.
346 return;
347 }
348
349 // Remove Default Channel.
350 r.channels.remove(NotificationChannel.DEFAULT_CHANNEL_ID);
351 }
352
Julia Reynolds5c399c62019-04-08 14:42:53 -0400353 private void createDefaultChannelIfNeededLocked(PackagePreferences r) throws
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400354 PackageManager.NameNotFoundException {
355 if (r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) {
356 r.channels.get(NotificationChannel.DEFAULT_CHANNEL_ID).setName(mContext.getString(
357 com.android.internal.R.string.default_notification_channel_label));
358 return;
359 }
360
361 if (!shouldHaveDefaultChannel(r)) {
362 // Keep the default channel until upgraded.
363 return;
364 }
365
366 // Create Default Channel
367 NotificationChannel channel;
368 channel = new NotificationChannel(
369 NotificationChannel.DEFAULT_CHANNEL_ID,
370 mContext.getString(R.string.default_notification_channel_label),
371 r.importance);
372 channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX);
373 channel.setLockscreenVisibility(r.visibility);
374 if (r.importance != NotificationManager.IMPORTANCE_UNSPECIFIED) {
375 channel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
376 }
377 if (r.priority != DEFAULT_PRIORITY) {
378 channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
379 }
380 if (r.visibility != DEFAULT_VISIBILITY) {
381 channel.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY);
382 }
383 r.channels.put(channel.getId(), channel);
384 }
385
Annie Meng8b646fd2019-02-01 18:46:42 +0000386 public void writeXml(XmlSerializer out, boolean forBackup, int userId) throws IOException {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400387 out.startTag(null, TAG_RANKING);
388 out.attribute(null, ATT_VERSION, Integer.toString(XML_VERSION));
Annie Meng8b646fd2019-02-01 18:46:42 +0000389 if (mHideSilentStatusBarIcons != DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS
390 && (!forBackup || userId == UserHandle.USER_SYSTEM)) {
Julia Reynolds12ad7ca2019-01-28 09:29:16 -0500391 out.startTag(null, TAG_STATUS_ICONS);
392 out.attribute(null, ATT_HIDE_SILENT, String.valueOf(mHideSilentStatusBarIcons));
393 out.endTag(null, TAG_STATUS_ICONS);
394 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400395
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400396 synchronized (mPackagePreferences) {
397 final int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400398 for (int i = 0; i < N; i++) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400399 final PackagePreferences r = mPackagePreferences.valueAt(i);
Annie Meng8b646fd2019-02-01 18:46:42 +0000400 if (forBackup && UserHandle.getUserId(r.uid) != userId) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400401 continue;
402 }
403 final boolean hasNonDefaultSettings =
404 r.importance != DEFAULT_IMPORTANCE
405 || r.priority != DEFAULT_PRIORITY
406 || r.visibility != DEFAULT_VISIBILITY
407 || r.showBadge != DEFAULT_SHOW_BADGE
408 || r.lockedAppFields != DEFAULT_LOCKED_APP_FIELDS
409 || r.channels.size() > 0
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400410 || r.groups.size() > 0
Julia Reynolds33ab8a02018-12-17 16:19:52 -0500411 || r.delegate != null
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800412 || r.allowBubble != DEFAULT_ALLOW_BUBBLE;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400413 if (hasNonDefaultSettings) {
414 out.startTag(null, TAG_PACKAGE);
415 out.attribute(null, ATT_NAME, r.pkg);
416 if (r.importance != DEFAULT_IMPORTANCE) {
417 out.attribute(null, ATT_IMPORTANCE, Integer.toString(r.importance));
418 }
419 if (r.priority != DEFAULT_PRIORITY) {
420 out.attribute(null, ATT_PRIORITY, Integer.toString(r.priority));
421 }
422 if (r.visibility != DEFAULT_VISIBILITY) {
423 out.attribute(null, ATT_VISIBILITY, Integer.toString(r.visibility));
424 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800425 if (r.allowBubble != DEFAULT_ALLOW_BUBBLE) {
426 out.attribute(null, ATT_ALLOW_BUBBLE, Boolean.toString(r.allowBubble));
Julia Reynolds33ab8a02018-12-17 16:19:52 -0500427 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400428 out.attribute(null, ATT_SHOW_BADGE, Boolean.toString(r.showBadge));
429 out.attribute(null, ATT_APP_USER_LOCKED_FIELDS,
430 Integer.toString(r.lockedAppFields));
431
432 if (!forBackup) {
433 out.attribute(null, ATT_UID, Integer.toString(r.uid));
434 }
435
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400436 if (r.delegate != null) {
437 out.startTag(null, TAG_DELEGATE);
438
439 out.attribute(null, ATT_NAME, r.delegate.mPkg);
440 out.attribute(null, ATT_UID, Integer.toString(r.delegate.mUid));
441 if (r.delegate.mEnabled != Delegate.DEFAULT_ENABLED) {
442 out.attribute(null, ATT_ENABLED, Boolean.toString(r.delegate.mEnabled));
443 }
444 if (r.delegate.mUserAllowed != Delegate.DEFAULT_USER_ALLOWED) {
445 out.attribute(null, ATT_USER_ALLOWED,
446 Boolean.toString(r.delegate.mUserAllowed));
447 }
448 out.endTag(null, TAG_DELEGATE);
449 }
450
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400451 for (NotificationChannelGroup group : r.groups.values()) {
452 group.writeXml(out);
453 }
454
455 for (NotificationChannel channel : r.channels.values()) {
456 if (forBackup) {
457 if (!channel.isDeleted()) {
458 channel.writeXmlForBackup(out, mContext);
459 }
460 } else {
461 channel.writeXml(out);
462 }
463 }
464
465 out.endTag(null, TAG_PACKAGE);
466 }
467 }
468 }
469 out.endTag(null, TAG_RANKING);
470 }
471
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800472 /**
473 * Sets whether bubbles are allowed.
474 *
475 * @param pkg the package to allow or not allow bubbles for.
476 * @param uid the uid to allow or not allow bubbles for.
477 * @param allowed whether bubbles are allowed.
478 */
479 public void setBubblesAllowed(String pkg, int uid, boolean allowed) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400480 synchronized (mPackagePreferences) {
481 PackagePreferences p = getOrCreatePackagePreferencesLocked(pkg, uid);
482 p.allowBubble = allowed;
483 p.lockedAppFields = p.lockedAppFields | LockableAppFields.USER_LOCKED_BUBBLE;
484 }
Julia Reynolds33ab8a02018-12-17 16:19:52 -0500485 }
486
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800487 /**
488 * Whether bubbles are allowed.
489 *
490 * @param pkg the package to check if bubbles are allowed for
491 * @param uid the uid to check if bubbles are allowed for.
492 * @return whether bubbles are allowed.
493 */
Julia Reynolds4509ce72019-01-31 13:12:43 -0500494 @Override
Mady Mellor9db685a2019-01-23 13:23:37 -0800495 public boolean areBubblesAllowed(String pkg, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400496 synchronized (mPackagePreferences) {
497 return getOrCreatePackagePreferencesLocked(pkg, uid).allowBubble;
498 }
Julia Reynolds33ab8a02018-12-17 16:19:52 -0500499 }
500
501 public int getAppLockedFields(String pkg, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400502 synchronized (mPackagePreferences) {
503 return getOrCreatePackagePreferencesLocked(pkg, uid).lockedAppFields;
504 }
Julia Reynolds33ab8a02018-12-17 16:19:52 -0500505 }
506
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400507 /**
508 * Gets importance.
509 */
510 @Override
511 public int getImportance(String packageName, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400512 synchronized (mPackagePreferences) {
513 return getOrCreatePackagePreferencesLocked(packageName, uid).importance;
514 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400515 }
516
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400517 /**
518 * Returns whether the importance of the corresponding notification is user-locked and shouldn't
519 * be adjusted by an assistant (via means of a blocking helper, for example). For the channel
520 * locking field, see {@link NotificationChannel#USER_LOCKED_IMPORTANCE}.
521 */
522 public boolean getIsAppImportanceLocked(String packageName, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400523 synchronized (mPackagePreferences) {
524 int userLockedFields = getOrCreatePackagePreferencesLocked(packageName, uid).lockedAppFields;
525 return (userLockedFields & LockableAppFields.USER_LOCKED_IMPORTANCE) != 0;
526 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400527 }
528
529 @Override
530 public boolean canShowBadge(String packageName, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400531 synchronized (mPackagePreferences) {
532 return getOrCreatePackagePreferencesLocked(packageName, uid).showBadge;
533 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400534 }
535
536 @Override
537 public void setShowBadge(String packageName, int uid, boolean showBadge) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400538 synchronized (mPackagePreferences) {
539 getOrCreatePackagePreferencesLocked(packageName, uid).showBadge = showBadge;
540 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400541 updateConfig();
542 }
543
544 @Override
545 public boolean isGroupBlocked(String packageName, int uid, String groupId) {
546 if (groupId == null) {
547 return false;
548 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400549 synchronized (mPackagePreferences) {
550 PackagePreferences r = getOrCreatePackagePreferencesLocked(packageName, uid);
551 NotificationChannelGroup group = r.groups.get(groupId);
552 if (group == null) {
553 return false;
554 }
555 return group.isBlocked();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400556 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400557 }
558
559 int getPackagePriority(String pkg, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400560 synchronized (mPackagePreferences) {
561 return getOrCreatePackagePreferencesLocked(pkg, uid).priority;
562 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400563 }
564
565 int getPackageVisibility(String pkg, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400566 synchronized (mPackagePreferences) {
567 return getOrCreatePackagePreferencesLocked(pkg, uid).visibility;
568 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400569 }
570
571 @Override
572 public void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group,
573 boolean fromTargetApp) {
574 Preconditions.checkNotNull(pkg);
575 Preconditions.checkNotNull(group);
576 Preconditions.checkNotNull(group.getId());
577 Preconditions.checkNotNull(!TextUtils.isEmpty(group.getName()));
Julia Reynolds5c399c62019-04-08 14:42:53 -0400578 synchronized (mPackagePreferences) {
579 PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
580 if (r == null) {
581 throw new IllegalArgumentException("Invalid package");
582 }
583 final NotificationChannelGroup oldGroup = r.groups.get(group.getId());
584 if (!group.equals(oldGroup)) {
585 // will log for new entries as well as name/description changes
586 MetricsLogger.action(getChannelGroupLog(group.getId(), pkg));
587 }
588 if (oldGroup != null) {
589 group.setChannels(oldGroup.getChannels());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400590
Julia Reynolds5c399c62019-04-08 14:42:53 -0400591 // apps can't update the blocked status or app overlay permission
592 if (fromTargetApp) {
593 group.setBlocked(oldGroup.isBlocked());
594 group.unlockFields(group.getUserLockedFields());
595 group.lockFields(oldGroup.getUserLockedFields());
596 } else {
597 // but the system can
598 if (group.isBlocked() != oldGroup.isBlocked()) {
599 group.lockFields(NotificationChannelGroup.USER_LOCKED_BLOCKED_STATE);
600 updateChannelsBypassingDnd(mContext.getUserId());
601 }
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400602 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400603 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400604 r.groups.put(group.getId(), group);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400605 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400606 }
607
608 @Override
609 public void createNotificationChannel(String pkg, int uid, NotificationChannel channel,
610 boolean fromTargetApp, boolean hasDndAccess) {
611 Preconditions.checkNotNull(pkg);
612 Preconditions.checkNotNull(channel);
613 Preconditions.checkNotNull(channel.getId());
614 Preconditions.checkArgument(!TextUtils.isEmpty(channel.getName()));
Julia Reynolds5c399c62019-04-08 14:42:53 -0400615 synchronized (mPackagePreferences) {
616 PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
617 if (r == null) {
618 throw new IllegalArgumentException("Invalid package");
619 }
620 if (channel.getGroup() != null && !r.groups.containsKey(channel.getGroup())) {
621 throw new IllegalArgumentException("NotificationChannelGroup doesn't exist");
622 }
623 if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channel.getId())) {
624 throw new IllegalArgumentException("Reserved id");
625 }
626 NotificationChannel existing = r.channels.get(channel.getId());
627 // Keep most of the existing settings
628 if (existing != null && fromTargetApp) {
629 if (existing.isDeleted()) {
630 existing.setDeleted(false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400631
Julia Reynolds5c399c62019-04-08 14:42:53 -0400632 // log a resurrected channel as if it's new again
633 MetricsLogger.action(getChannelLog(channel, pkg).setType(
634 com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_OPEN));
635 }
636
637 existing.setName(channel.getName().toString());
638 existing.setDescription(channel.getDescription());
639 existing.setBlockableSystem(channel.isBlockableSystem());
640 if (existing.getGroup() == null) {
641 existing.setGroup(channel.getGroup());
642 }
643
644 // Apps are allowed to downgrade channel importance if the user has not changed any
645 // fields on this channel yet.
646 final int previousExistingImportance = existing.getImportance();
647 if (existing.getUserLockedFields() == 0 &&
648 channel.getImportance() < existing.getImportance()) {
649 existing.setImportance(channel.getImportance());
650 }
651
652 // system apps and dnd access apps can bypass dnd if the user hasn't changed any
653 // fields on the channel yet
654 if (existing.getUserLockedFields() == 0 && hasDndAccess) {
655 boolean bypassDnd = channel.canBypassDnd();
656 existing.setBypassDnd(bypassDnd);
657
658 if (bypassDnd != mAreChannelsBypassingDnd
659 || previousExistingImportance != existing.getImportance()) {
660 updateChannelsBypassingDnd(mContext.getUserId());
661 }
662 }
663
664 updateConfig();
665 return;
666 }
667 if (channel.getImportance() < IMPORTANCE_NONE
668 || channel.getImportance() > NotificationManager.IMPORTANCE_MAX) {
669 throw new IllegalArgumentException("Invalid importance level");
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400670 }
671
Julia Reynolds5c399c62019-04-08 14:42:53 -0400672 // Reset fields that apps aren't allowed to set.
673 if (fromTargetApp && !hasDndAccess) {
674 channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400675 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400676 if (fromTargetApp) {
677 channel.setLockscreenVisibility(r.visibility);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400678 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400679 clearLockedFieldsLocked(channel);
680 channel.setImportanceLockedByOEM(r.oemLockedImportance);
681 if (!channel.isImportanceLockedByOEM()) {
682 if (r.futureOemLockedChannels.remove(channel.getId())) {
683 channel.setImportanceLockedByOEM(true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400684 }
685 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400686 channel.setImportanceLockedByCriticalDeviceFunction(r.defaultAppLockedImportance);
687 if (channel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
688 channel.setLockscreenVisibility(
689 NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE);
Julia Reynolds413ba842019-01-11 10:38:08 -0500690 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400691 if (!r.showBadge) {
692 channel.setShowBadge(false);
693 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400694
Julia Reynolds5c399c62019-04-08 14:42:53 -0400695 r.channels.put(channel.getId(), channel);
696 if (channel.canBypassDnd() != mAreChannelsBypassingDnd) {
697 updateChannelsBypassingDnd(mContext.getUserId());
698 }
699 MetricsLogger.action(getChannelLog(channel, pkg).setType(
700 com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_OPEN));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400701 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400702 }
703
Julia Reynolds5c399c62019-04-08 14:42:53 -0400704 void clearLockedFieldsLocked(NotificationChannel channel) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400705 channel.unlockFields(channel.getUserLockedFields());
706 }
707
708 @Override
709 public void updateNotificationChannel(String pkg, int uid, NotificationChannel updatedChannel,
710 boolean fromUser) {
711 Preconditions.checkNotNull(updatedChannel);
712 Preconditions.checkNotNull(updatedChannel.getId());
Julia Reynolds5c399c62019-04-08 14:42:53 -0400713 synchronized (mPackagePreferences) {
714 PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
715 if (r == null) {
716 throw new IllegalArgumentException("Invalid package");
717 }
718 NotificationChannel channel = r.channels.get(updatedChannel.getId());
719 if (channel == null || channel.isDeleted()) {
720 throw new IllegalArgumentException("Channel does not exist");
721 }
722 if (updatedChannel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
723 updatedChannel.setLockscreenVisibility(
724 NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE);
725 }
726 if (fromUser) {
727 updatedChannel.lockFields(channel.getUserLockedFields());
728 lockFieldsForUpdateLocked(channel, updatedChannel);
729 } else {
730 updatedChannel.unlockFields(updatedChannel.getUserLockedFields());
731 }
732 // no importance updates are allowed if OEM blocked it
733 updatedChannel.setImportanceLockedByOEM(channel.isImportanceLockedByOEM());
734 if (updatedChannel.isImportanceLockedByOEM()) {
735 updatedChannel.setImportance(channel.getImportance());
736 }
737 updatedChannel.setImportanceLockedByCriticalDeviceFunction(
738 r.defaultAppLockedImportance);
739 if (updatedChannel.isImportanceLockedByCriticalDeviceFunction()) {
740 updatedChannel.setImportance(channel.getImportance());
741 }
Julia Reynolds413ba842019-01-11 10:38:08 -0500742
Julia Reynolds5c399c62019-04-08 14:42:53 -0400743 r.channels.put(updatedChannel.getId(), updatedChannel);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400744
Julia Reynolds5c399c62019-04-08 14:42:53 -0400745 if (onlyHasDefaultChannel(pkg, uid)) {
746 // copy settings to app level so they are inherited by new channels
747 // when the app migrates
748 r.importance = updatedChannel.getImportance();
749 r.priority = updatedChannel.canBypassDnd()
750 ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT;
751 r.visibility = updatedChannel.getLockscreenVisibility();
752 r.showBadge = updatedChannel.canShowBadge();
753 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400754
Julia Reynolds5c399c62019-04-08 14:42:53 -0400755 if (!channel.equals(updatedChannel)) {
756 // only log if there are real changes
757 MetricsLogger.action(getChannelLog(updatedChannel, pkg)
758 .setSubtype(fromUser ? 1 : 0));
759 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400760
Julia Reynolds5c399c62019-04-08 14:42:53 -0400761 if (updatedChannel.canBypassDnd() != mAreChannelsBypassingDnd
762 || channel.getImportance() != updatedChannel.getImportance()) {
763 updateChannelsBypassingDnd(mContext.getUserId());
764 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400765 }
766 updateConfig();
767 }
768
769 @Override
770 public NotificationChannel getNotificationChannel(String pkg, int uid, String channelId,
771 boolean includeDeleted) {
772 Preconditions.checkNotNull(pkg);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400773 synchronized (mPackagePreferences) {
774 PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
775 if (r == null) {
776 return null;
777 }
778 if (channelId == null) {
779 channelId = NotificationChannel.DEFAULT_CHANNEL_ID;
780 }
781 final NotificationChannel nc = r.channels.get(channelId);
782 if (nc != null && (includeDeleted || !nc.isDeleted())) {
783 return nc;
784 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400785 return null;
786 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400787 }
788
789 @Override
790 public void deleteNotificationChannel(String pkg, int uid, String channelId) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400791 synchronized (mPackagePreferences) {
792 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
793 if (r == null) {
794 return;
795 }
796 NotificationChannel channel = r.channels.get(channelId);
797 if (channel != null) {
798 channel.setDeleted(true);
799 LogMaker lm = getChannelLog(channel, pkg);
800 lm.setType(com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_CLOSE);
801 MetricsLogger.action(lm);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400802
Julia Reynolds5c399c62019-04-08 14:42:53 -0400803 if (mAreChannelsBypassingDnd && channel.canBypassDnd()) {
804 updateChannelsBypassingDnd(mContext.getUserId());
805 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400806 }
807 }
808 }
809
810 @Override
811 @VisibleForTesting
812 public void permanentlyDeleteNotificationChannel(String pkg, int uid, String channelId) {
813 Preconditions.checkNotNull(pkg);
814 Preconditions.checkNotNull(channelId);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400815 synchronized (mPackagePreferences) {
816 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
817 if (r == null) {
818 return;
819 }
820 r.channels.remove(channelId);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400821 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400822 }
823
824 @Override
825 public void permanentlyDeleteNotificationChannels(String pkg, int uid) {
826 Preconditions.checkNotNull(pkg);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400827 synchronized (mPackagePreferences) {
828 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
829 if (r == null) {
830 return;
831 }
832 int N = r.channels.size() - 1;
833 for (int i = N; i >= 0; i--) {
834 String key = r.channels.keyAt(i);
835 if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(key)) {
836 r.channels.remove(key);
837 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400838 }
839 }
840 }
841
Julia Reynolds12ad7ca2019-01-28 09:29:16 -0500842 public boolean shouldHideSilentStatusIcons() {
843 return mHideSilentStatusBarIcons;
844 }
845
846 public void setHideSilentStatusIcons(boolean hide) {
847 mHideSilentStatusBarIcons = hide;
848 }
849
Julia Reynolds413ba842019-01-11 10:38:08 -0500850 public void lockChannelsForOEM(String[] appOrChannelList) {
851 if (appOrChannelList == null) {
852 return;
853 }
854 for (String appOrChannel : appOrChannelList) {
855 if (!TextUtils.isEmpty(appOrChannel)) {
856 String[] appSplit = appOrChannel.split(NON_BLOCKABLE_CHANNEL_DELIM);
857 if (appSplit != null && appSplit.length > 0) {
858 String appName = appSplit[0];
859 String channelId = appSplit.length == 2 ? appSplit[1] : null;
860
861 synchronized (mPackagePreferences) {
862 for (PackagePreferences r : mPackagePreferences.values()) {
863 if (r.pkg.equals(appName)) {
864 if (channelId == null) {
865 // lock all channels for the app
866 r.oemLockedImportance = true;
867 for (NotificationChannel channel : r.channels.values()) {
868 channel.setImportanceLockedByOEM(true);
869 }
870 } else {
871 NotificationChannel channel = r.channels.get(channelId);
872 if (channel != null) {
873 channel.setImportanceLockedByOEM(true);
874 } else {
875 // if this channel shows up in the future, make sure it'll
876 // be locked immediately
877 r.futureOemLockedChannels.add(channelId);
878 }
879 }
880 }
881 }
882 }
883 }
884 }
885 }
886 }
887
Julia Reynoldse7ca31b2019-04-25 15:41:47 -0400888 public void updateDefaultApps(int userId, ArraySet<String> toRemove,
889 ArraySet<Pair<String, Integer>> toAdd) {
Julia Reynolds0c245002019-03-27 16:10:11 -0400890 synchronized (mPackagePreferences) {
891 for (PackagePreferences p : mPackagePreferences.values()) {
892 if (userId == UserHandle.getUserId(p.uid)) {
893 if (toRemove != null && toRemove.contains(p.pkg)) {
894 p.defaultAppLockedImportance = false;
895 for (NotificationChannel channel : p.channels.values()) {
896 channel.setImportanceLockedByCriticalDeviceFunction(false);
897 }
Julia Reynoldse7ca31b2019-04-25 15:41:47 -0400898 }
899 }
900 }
901 if (toAdd != null) {
902 for (Pair<String, Integer> approvedApp : toAdd) {
903 PackagePreferences p = getOrCreatePackagePreferencesLocked(approvedApp.first,
904 approvedApp.second);
905 p.defaultAppLockedImportance = true;
906 for (NotificationChannel channel : p.channels.values()) {
907 channel.setImportanceLockedByCriticalDeviceFunction(true);
Julia Reynolds0c245002019-03-27 16:10:11 -0400908 }
909 }
910 }
911 }
912 }
913
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400914 public NotificationChannelGroup getNotificationChannelGroupWithChannels(String pkg,
915 int uid, String groupId, boolean includeDeleted) {
916 Preconditions.checkNotNull(pkg);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400917 synchronized (mPackagePreferences) {
918 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
919 if (r == null || groupId == null || !r.groups.containsKey(groupId)) {
920 return null;
921 }
922 NotificationChannelGroup group = r.groups.get(groupId).clone();
923 group.setChannels(new ArrayList<>());
924 int N = r.channels.size();
925 for (int i = 0; i < N; i++) {
926 final NotificationChannel nc = r.channels.valueAt(i);
927 if (includeDeleted || !nc.isDeleted()) {
928 if (groupId.equals(nc.getGroup())) {
929 group.addChannel(nc);
930 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400931 }
932 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400933 return group;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400934 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400935 }
936
937 public NotificationChannelGroup getNotificationChannelGroup(String groupId, String pkg,
938 int uid) {
939 Preconditions.checkNotNull(pkg);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400940 synchronized (mPackagePreferences) {
941 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
942 if (r == null) {
943 return null;
944 }
945 return r.groups.get(groupId);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400946 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400947 }
948
949 @Override
950 public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups(String pkg,
Julia Reynolds13ed28b2018-09-21 15:20:13 -0400951 int uid, boolean includeDeleted, boolean includeNonGrouped, boolean includeEmpty) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400952 Preconditions.checkNotNull(pkg);
953 Map<String, NotificationChannelGroup> groups = new ArrayMap<>();
Julia Reynolds5c399c62019-04-08 14:42:53 -0400954 synchronized (mPackagePreferences) {
955 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
956 if (r == null) {
957 return ParceledListSlice.emptyList();
958 }
959 NotificationChannelGroup nonGrouped = new NotificationChannelGroup(null, null);
960 int N = r.channels.size();
961 for (int i = 0; i < N; i++) {
962 final NotificationChannel nc = r.channels.valueAt(i);
963 if (includeDeleted || !nc.isDeleted()) {
964 if (nc.getGroup() != null) {
965 if (r.groups.get(nc.getGroup()) != null) {
966 NotificationChannelGroup ncg = groups.get(nc.getGroup());
967 if (ncg == null) {
968 ncg = r.groups.get(nc.getGroup()).clone();
969 ncg.setChannels(new ArrayList<>());
970 groups.put(nc.getGroup(), ncg);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400971
Julia Reynolds5c399c62019-04-08 14:42:53 -0400972 }
973 ncg.addChannel(nc);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400974 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400975 } else {
976 nonGrouped.addChannel(nc);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400977 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400978 }
979 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400980 if (includeNonGrouped && nonGrouped.getChannels().size() > 0) {
981 groups.put(null, nonGrouped);
982 }
983 if (includeEmpty) {
984 for (NotificationChannelGroup group : r.groups.values()) {
985 if (!groups.containsKey(group.getId())) {
986 groups.put(group.getId(), group);
987 }
Julia Reynolds13ed28b2018-09-21 15:20:13 -0400988 }
989 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400990 return new ParceledListSlice<>(new ArrayList<>(groups.values()));
Julia Reynolds13ed28b2018-09-21 15:20:13 -0400991 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400992 }
993
994 public List<NotificationChannel> deleteNotificationChannelGroup(String pkg, int uid,
995 String groupId) {
996 List<NotificationChannel> deletedChannels = new ArrayList<>();
Julia Reynolds5c399c62019-04-08 14:42:53 -0400997 synchronized (mPackagePreferences) {
998 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
999 if (r == null || TextUtils.isEmpty(groupId)) {
1000 return deletedChannels;
1001 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001002
Julia Reynolds5c399c62019-04-08 14:42:53 -04001003 r.groups.remove(groupId);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001004
Julia Reynolds5c399c62019-04-08 14:42:53 -04001005 int N = r.channels.size();
1006 for (int i = 0; i < N; i++) {
1007 final NotificationChannel nc = r.channels.valueAt(i);
1008 if (groupId.equals(nc.getGroup())) {
1009 nc.setDeleted(true);
1010 deletedChannels.add(nc);
1011 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001012 }
1013 }
1014 return deletedChannels;
1015 }
1016
1017 @Override
1018 public Collection<NotificationChannelGroup> getNotificationChannelGroups(String pkg,
1019 int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001020 List<NotificationChannelGroup> groups = new ArrayList<>();
1021 synchronized (mPackagePreferences) {
1022 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
1023 if (r == null) {
1024 return groups;
1025 }
1026 groups.addAll(r.groups.values());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001027 }
Julia Reynolds5c399c62019-04-08 14:42:53 -04001028 return groups;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001029 }
1030
1031 @Override
1032 public ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg, int uid,
1033 boolean includeDeleted) {
1034 Preconditions.checkNotNull(pkg);
1035 List<NotificationChannel> channels = new ArrayList<>();
Julia Reynolds5c399c62019-04-08 14:42:53 -04001036 synchronized (mPackagePreferences) {
1037 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
1038 if (r == null) {
1039 return ParceledListSlice.emptyList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001040 }
Julia Reynolds5c399c62019-04-08 14:42:53 -04001041 int N = r.channels.size();
1042 for (int i = 0; i < N; i++) {
1043 final NotificationChannel nc = r.channels.valueAt(i);
1044 if (includeDeleted || !nc.isDeleted()) {
1045 channels.add(nc);
1046 }
1047 }
1048 return new ParceledListSlice<>(channels);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001049 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001050 }
1051
1052 /**
Beverly0479cde22018-11-09 11:05:34 -05001053 * Gets all notification channels associated with the given pkg and userId that can bypass dnd
1054 */
1055 public ParceledListSlice<NotificationChannel> getNotificationChannelsBypassingDnd(String pkg,
1056 int userId) {
1057 List<NotificationChannel> channels = new ArrayList<>();
1058 synchronized (mPackagePreferences) {
1059 final PackagePreferences r = mPackagePreferences.get(
1060 packagePreferencesKey(pkg, userId));
1061 // notifications from this package aren't blocked
1062 if (r != null && r.importance != IMPORTANCE_NONE) {
1063 for (NotificationChannel channel : r.channels.values()) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001064 if (channelIsLiveLocked(r, channel) && channel.canBypassDnd()) {
Beverly0479cde22018-11-09 11:05:34 -05001065 channels.add(channel);
1066 }
1067 }
1068 }
1069 }
1070 return new ParceledListSlice<>(channels);
1071 }
1072
1073 /**
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001074 * True for pre-O apps that only have the default channel, or pre O apps that have no
1075 * channels yet. This method will create the default channel for pre-O apps that don't have it.
1076 * Should never be true for O+ targeting apps, but that's enforced on boot/when an app
1077 * upgrades.
1078 */
1079 public boolean onlyHasDefaultChannel(String pkg, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001080 synchronized (mPackagePreferences) {
1081 PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
1082 if (r.channels.size() == 1
1083 && r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) {
1084 return true;
1085 }
1086 return false;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001087 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001088 }
1089
1090 public int getDeletedChannelCount(String pkg, int uid) {
1091 Preconditions.checkNotNull(pkg);
1092 int deletedCount = 0;
Julia Reynolds5c399c62019-04-08 14:42:53 -04001093 synchronized (mPackagePreferences) {
1094 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
1095 if (r == null) {
1096 return deletedCount;
1097 }
1098 int N = r.channels.size();
1099 for (int i = 0; i < N; i++) {
1100 final NotificationChannel nc = r.channels.valueAt(i);
1101 if (nc.isDeleted()) {
1102 deletedCount++;
1103 }
1104 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001105 return deletedCount;
1106 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001107 }
1108
1109 public int getBlockedChannelCount(String pkg, int uid) {
1110 Preconditions.checkNotNull(pkg);
1111 int blockedCount = 0;
Julia Reynolds5c399c62019-04-08 14:42:53 -04001112 synchronized (mPackagePreferences) {
1113 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
1114 if (r == null) {
1115 return blockedCount;
1116 }
1117 int N = r.channels.size();
1118 for (int i = 0; i < N; i++) {
1119 final NotificationChannel nc = r.channels.valueAt(i);
1120 if (!nc.isDeleted() && IMPORTANCE_NONE == nc.getImportance()) {
1121 blockedCount++;
1122 }
1123 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001124 return blockedCount;
1125 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001126 }
1127
1128 public int getBlockedAppCount(int userId) {
1129 int count = 0;
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001130 synchronized (mPackagePreferences) {
1131 final int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001132 for (int i = 0; i < N; i++) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001133 final PackagePreferences r = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001134 if (userId == UserHandle.getUserId(r.uid)
1135 && r.importance == IMPORTANCE_NONE) {
1136 count++;
1137 }
1138 }
1139 }
1140 return count;
1141 }
1142
Beverly0479cde22018-11-09 11:05:34 -05001143 /**
1144 * Returns the number of apps that have at least one notification channel that can bypass DND
1145 * for given particular user
1146 */
1147 public int getAppsBypassingDndCount(int userId) {
1148 int count = 0;
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001149 synchronized (mPackagePreferences) {
Beverly0479cde22018-11-09 11:05:34 -05001150 final int numPackagePreferences = mPackagePreferences.size();
1151 for (int i = 0; i < numPackagePreferences; i++) {
1152 final PackagePreferences r = mPackagePreferences.valueAt(i);
1153 // Package isn't associated with this userId or notifications from this package are
1154 // blocked
1155 if (userId != UserHandle.getUserId(r.uid) || r.importance == IMPORTANCE_NONE) {
1156 continue;
1157 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001158
Beverly0479cde22018-11-09 11:05:34 -05001159 for (NotificationChannel channel : r.channels.values()) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001160 if (channelIsLiveLocked(r, channel) && channel.canBypassDnd()) {
Beverly0479cde22018-11-09 11:05:34 -05001161 count++;
1162 break;
1163 }
1164 }
1165 }
1166 }
1167 return count;
1168 }
1169
1170 /**
1171 * Syncs {@link #mAreChannelsBypassingDnd} with the user's notification policy before
1172 * updating
1173 * @param userId
1174 */
1175 private void syncChannelsBypassingDnd(int userId) {
1176 mAreChannelsBypassingDnd = (mZenModeHelper.getNotificationPolicy().state
1177 & NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND) == 1;
1178 updateChannelsBypassingDnd(userId);
1179 }
1180
1181 /**
1182 * Updates the user's NotificationPolicy based on whether the given userId
1183 * has channels bypassing DND
1184 * @param userId
1185 */
1186 private void updateChannelsBypassingDnd(int userId) {
1187 synchronized (mPackagePreferences) {
1188 final int numPackagePreferences = mPackagePreferences.size();
1189 for (int i = 0; i < numPackagePreferences; i++) {
1190 final PackagePreferences r = mPackagePreferences.valueAt(i);
1191 // Package isn't associated with this userId or notifications from this package are
1192 // blocked
1193 if (userId != UserHandle.getUserId(r.uid) || r.importance == IMPORTANCE_NONE) {
1194 continue;
1195 }
1196
1197 for (NotificationChannel channel : r.channels.values()) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001198 if (channelIsLiveLocked(r, channel) && channel.canBypassDnd()) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001199 if (!mAreChannelsBypassingDnd) {
1200 mAreChannelsBypassingDnd = true;
1201 updateZenPolicy(true);
1202 }
1203 return;
1204 }
1205 }
1206 }
1207 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001208 // If no channels bypass DND, update the zen policy once to disable DND bypass.
1209 if (mAreChannelsBypassingDnd) {
1210 mAreChannelsBypassingDnd = false;
1211 updateZenPolicy(false);
1212 }
1213 }
1214
Julia Reynolds5c399c62019-04-08 14:42:53 -04001215 private boolean channelIsLiveLocked(PackagePreferences pkgPref, NotificationChannel channel) {
Beverly0479cde22018-11-09 11:05:34 -05001216 // Channel is in a group that's blocked
Beverly4f7b53d2018-11-20 09:56:31 -05001217 if (isGroupBlocked(pkgPref.pkg, pkgPref.uid, channel.getGroup())) {
1218 return false;
Beverly0479cde22018-11-09 11:05:34 -05001219 }
1220
1221 // Channel is deleted or is blocked
1222 if (channel.isDeleted() || channel.getImportance() == IMPORTANCE_NONE) {
1223 return false;
1224 }
1225
1226 return true;
1227 }
1228
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001229 public void updateZenPolicy(boolean areChannelsBypassingDnd) {
1230 NotificationManager.Policy policy = mZenModeHelper.getNotificationPolicy();
1231 mZenModeHelper.setNotificationPolicy(new NotificationManager.Policy(
1232 policy.priorityCategories, policy.priorityCallSenders,
1233 policy.priorityMessageSenders, policy.suppressedVisualEffects,
1234 (areChannelsBypassingDnd ? NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND
1235 : 0)));
1236 }
1237
1238 public boolean areChannelsBypassingDnd() {
1239 return mAreChannelsBypassingDnd;
1240 }
1241
1242 /**
1243 * Sets importance.
1244 */
1245 @Override
1246 public void setImportance(String pkgName, int uid, int importance) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001247 synchronized (mPackagePreferences) {
1248 getOrCreatePackagePreferencesLocked(pkgName, uid).importance = importance;
1249 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001250 updateConfig();
1251 }
1252
1253 public void setEnabled(String packageName, int uid, boolean enabled) {
1254 boolean wasEnabled = getImportance(packageName, uid) != IMPORTANCE_NONE;
1255 if (wasEnabled == enabled) {
1256 return;
1257 }
1258 setImportance(packageName, uid,
1259 enabled ? DEFAULT_IMPORTANCE : IMPORTANCE_NONE);
1260 }
1261
1262 /**
1263 * Sets whether any notifications from the app, represented by the given {@code pkgName} and
1264 * {@code uid}, have their importance locked by the user. Locked notifications don't get
1265 * considered for sentiment adjustments (and thus never show a blocking helper).
1266 */
1267 public void setAppImportanceLocked(String packageName, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001268 synchronized (mPackagePreferences) {
1269 PackagePreferences prefs = getOrCreatePackagePreferencesLocked(packageName, uid);
1270 if ((prefs.lockedAppFields & LockableAppFields.USER_LOCKED_IMPORTANCE) != 0) {
1271 return;
1272 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001273
Julia Reynolds5c399c62019-04-08 14:42:53 -04001274 prefs.lockedAppFields =
1275 prefs.lockedAppFields | LockableAppFields.USER_LOCKED_IMPORTANCE;
1276 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001277 updateConfig();
1278 }
1279
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001280 /**
1281 * Returns the delegate for a given package, if it's allowed by the package and the user.
1282 */
1283 public @Nullable String getNotificationDelegate(String sourcePkg, int sourceUid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001284 synchronized (mPackagePreferences) {
1285 PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001286
Julia Reynolds5c399c62019-04-08 14:42:53 -04001287 if (prefs == null || prefs.delegate == null) {
1288 return null;
1289 }
1290 if (!prefs.delegate.mUserAllowed || !prefs.delegate.mEnabled) {
1291 return null;
1292 }
1293 return prefs.delegate.mPkg;
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001294 }
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001295 }
1296
1297 /**
1298 * Used by an app to delegate notification posting privileges to another apps.
1299 */
1300 public void setNotificationDelegate(String sourcePkg, int sourceUid,
1301 String delegatePkg, int delegateUid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001302 synchronized (mPackagePreferences) {
1303 PackagePreferences prefs = getOrCreatePackagePreferencesLocked(sourcePkg, sourceUid);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001304
Julia Reynolds5c399c62019-04-08 14:42:53 -04001305 boolean userAllowed = prefs.delegate == null || prefs.delegate.mUserAllowed;
1306 Delegate delegate = new Delegate(delegatePkg, delegateUid, true, userAllowed);
1307 prefs.delegate = delegate;
1308 }
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001309 updateConfig();
1310 }
1311
1312 /**
1313 * Used by an app to turn off its notification delegate.
1314 */
1315 public void revokeNotificationDelegate(String sourcePkg, int sourceUid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001316 boolean changed = false;
1317 synchronized (mPackagePreferences) {
1318 PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid);
1319 if (prefs != null && prefs.delegate != null) {
1320 prefs.delegate.mEnabled = false;
1321 changed = true;
1322 }
1323 }
1324 if (changed) {
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001325 updateConfig();
1326 }
1327 }
1328
1329 /**
1330 * Toggles whether an app can have a notification delegate on behalf of a user.
1331 */
1332 public void toggleNotificationDelegate(String sourcePkg, int sourceUid, boolean userAllowed) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001333 boolean changed = false;
1334 synchronized (mPackagePreferences) {
1335 PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid);
1336 if (prefs != null && prefs.delegate != null) {
1337 prefs.delegate.mUserAllowed = userAllowed;
1338 changed = true;
1339 }
1340 }
1341 if (changed) {
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001342 updateConfig();
1343 }
1344 }
1345
1346 /**
1347 * Returns whether the given app is allowed on post notifications on behalf of the other given
1348 * app.
1349 */
1350 public boolean isDelegateAllowed(String sourcePkg, int sourceUid,
1351 String potentialDelegatePkg, int potentialDelegateUid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001352 synchronized (mPackagePreferences) {
1353 PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001354
Julia Reynolds5c399c62019-04-08 14:42:53 -04001355 return prefs != null && prefs.isValidDelegate(potentialDelegatePkg,
1356 potentialDelegateUid);
1357 }
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001358 }
1359
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001360 @VisibleForTesting
Julia Reynolds5c399c62019-04-08 14:42:53 -04001361 void lockFieldsForUpdateLocked(NotificationChannel original, NotificationChannel update) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001362 if (original.canBypassDnd() != update.canBypassDnd()) {
1363 update.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
1364 }
1365 if (original.getLockscreenVisibility() != update.getLockscreenVisibility()) {
1366 update.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY);
1367 }
1368 if (original.getImportance() != update.getImportance()) {
1369 update.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
1370 }
1371 if (original.shouldShowLights() != update.shouldShowLights()
1372 || original.getLightColor() != update.getLightColor()) {
1373 update.lockFields(NotificationChannel.USER_LOCKED_LIGHTS);
1374 }
1375 if (!Objects.equals(original.getSound(), update.getSound())) {
1376 update.lockFields(NotificationChannel.USER_LOCKED_SOUND);
1377 }
1378 if (!Arrays.equals(original.getVibrationPattern(), update.getVibrationPattern())
1379 || original.shouldVibrate() != update.shouldVibrate()) {
1380 update.lockFields(NotificationChannel.USER_LOCKED_VIBRATION);
1381 }
1382 if (original.canShowBadge() != update.canShowBadge()) {
1383 update.lockFields(NotificationChannel.USER_LOCKED_SHOW_BADGE);
1384 }
Julia Reynolds4509ce72019-01-31 13:12:43 -05001385 if (original.canBubble() != update.canBubble()) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001386 update.lockFields(NotificationChannel.USER_LOCKED_ALLOW_BUBBLE);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001387 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001388 }
1389
1390 public void dump(PrintWriter pw, String prefix,
1391 @NonNull NotificationManagerService.DumpFilter filter) {
1392 pw.print(prefix);
1393 pw.println("per-package config:");
1394
Julia Reynolds5c399c62019-04-08 14:42:53 -04001395 pw.println("PackagePreferences:");
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001396 synchronized (mPackagePreferences) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001397 dumpPackagePreferencesLocked(pw, prefix, filter, mPackagePreferences);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001398 }
1399 pw.println("Restored without uid:");
Julia Reynolds5c399c62019-04-08 14:42:53 -04001400 dumpPackagePreferencesLocked(pw, prefix, filter, mRestoredWithoutUids);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001401 }
1402
1403 public void dump(ProtoOutputStream proto,
1404 @NonNull NotificationManagerService.DumpFilter filter) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001405 synchronized (mPackagePreferences) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001406 dumpPackagePreferencesLocked(proto, RankingHelperProto.RECORDS, filter,
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001407 mPackagePreferences);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001408 }
Julia Reynolds5c399c62019-04-08 14:42:53 -04001409 dumpPackagePreferencesLocked(proto, RankingHelperProto.RECORDS_RESTORED_WITHOUT_UID, filter,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001410 mRestoredWithoutUids);
1411 }
1412
Julia Reynolds5c399c62019-04-08 14:42:53 -04001413 private static void dumpPackagePreferencesLocked(PrintWriter pw, String prefix,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001414 @NonNull NotificationManagerService.DumpFilter filter,
Julia Reynolds5c399c62019-04-08 14:42:53 -04001415 ArrayMap<String, PackagePreferences> packagePreferences) {
1416 final int N = packagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001417 for (int i = 0; i < N; i++) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001418 final PackagePreferences r = packagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001419 if (filter.matches(r.pkg)) {
1420 pw.print(prefix);
1421 pw.print(" AppSettings: ");
1422 pw.print(r.pkg);
1423 pw.print(" (");
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001424 pw.print(r.uid == UNKNOWN_UID ? "UNKNOWN_UID" : Integer.toString(r.uid));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001425 pw.print(')');
1426 if (r.importance != DEFAULT_IMPORTANCE) {
1427 pw.print(" importance=");
1428 pw.print(NotificationListenerService.Ranking.importanceToString(r.importance));
1429 }
1430 if (r.priority != DEFAULT_PRIORITY) {
1431 pw.print(" priority=");
1432 pw.print(Notification.priorityToString(r.priority));
1433 }
1434 if (r.visibility != DEFAULT_VISIBILITY) {
1435 pw.print(" visibility=");
1436 pw.print(Notification.visibilityToString(r.visibility));
1437 }
Julia Reynoldse7ca31b2019-04-25 15:41:47 -04001438 if (r.showBadge != DEFAULT_SHOW_BADGE) {
1439 pw.print(" showBadge=");
1440 pw.print(r.showBadge);
1441 }
1442 if (r.defaultAppLockedImportance != DEFAULT_APP_LOCKED_IMPORTANCE) {
1443 pw.print(" defaultAppLocked=");
1444 pw.print(r.defaultAppLockedImportance);
1445 }
1446 if (r.oemLockedImportance != DEFAULT_OEM_LOCKED_IMPORTANCE) {
1447 pw.print(" oemLocked=");
1448 pw.print(r.oemLockedImportance);
1449 }
1450 if (!r.futureOemLockedChannels.isEmpty()) {
1451 pw.print(" futureLockedChannels=");
1452 pw.print(r.futureOemLockedChannels);
1453 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001454 pw.println();
1455 for (NotificationChannel channel : r.channels.values()) {
1456 pw.print(prefix);
1457 channel.dump(pw, " ", filter.redact);
1458 }
1459 for (NotificationChannelGroup group : r.groups.values()) {
1460 pw.print(prefix);
1461 pw.print(" ");
1462 pw.print(" ");
1463 pw.println(group);
1464 }
1465 }
1466 }
1467 }
1468
Julia Reynolds5c399c62019-04-08 14:42:53 -04001469 private static void dumpPackagePreferencesLocked(ProtoOutputStream proto, long fieldId,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001470 @NonNull NotificationManagerService.DumpFilter filter,
Julia Reynolds5c399c62019-04-08 14:42:53 -04001471 ArrayMap<String, PackagePreferences> packagePreferences) {
1472 final int N = packagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001473 long fToken;
1474 for (int i = 0; i < N; i++) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001475 final PackagePreferences r = packagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001476 if (filter.matches(r.pkg)) {
1477 fToken = proto.start(fieldId);
1478
1479 proto.write(RankingHelperProto.RecordProto.PACKAGE, r.pkg);
1480 proto.write(RankingHelperProto.RecordProto.UID, r.uid);
1481 proto.write(RankingHelperProto.RecordProto.IMPORTANCE, r.importance);
1482 proto.write(RankingHelperProto.RecordProto.PRIORITY, r.priority);
1483 proto.write(RankingHelperProto.RecordProto.VISIBILITY, r.visibility);
1484 proto.write(RankingHelperProto.RecordProto.SHOW_BADGE, r.showBadge);
1485
1486 for (NotificationChannel channel : r.channels.values()) {
1487 channel.writeToProto(proto, RankingHelperProto.RecordProto.CHANNELS);
1488 }
1489 for (NotificationChannelGroup group : r.groups.values()) {
1490 group.writeToProto(proto, RankingHelperProto.RecordProto.CHANNEL_GROUPS);
1491 }
1492
1493 proto.end(fToken);
1494 }
1495 }
1496 }
1497
1498 public JSONObject dumpJson(NotificationManagerService.DumpFilter filter) {
1499 JSONObject ranking = new JSONObject();
1500 JSONArray PackagePreferencess = new JSONArray();
1501 try {
1502 ranking.put("noUid", mRestoredWithoutUids.size());
1503 } catch (JSONException e) {
1504 // pass
1505 }
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001506 synchronized (mPackagePreferences) {
1507 final int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001508 for (int i = 0; i < N; i++) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001509 final PackagePreferences r = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001510 if (filter == null || filter.matches(r.pkg)) {
1511 JSONObject PackagePreferences = new JSONObject();
1512 try {
1513 PackagePreferences.put("userId", UserHandle.getUserId(r.uid));
1514 PackagePreferences.put("packageName", r.pkg);
1515 if (r.importance != DEFAULT_IMPORTANCE) {
1516 PackagePreferences.put("importance",
1517 NotificationListenerService.Ranking.importanceToString(
1518 r.importance));
1519 }
1520 if (r.priority != DEFAULT_PRIORITY) {
1521 PackagePreferences.put("priority",
1522 Notification.priorityToString(r.priority));
1523 }
1524 if (r.visibility != DEFAULT_VISIBILITY) {
1525 PackagePreferences.put("visibility",
1526 Notification.visibilityToString(r.visibility));
1527 }
1528 if (r.showBadge != DEFAULT_SHOW_BADGE) {
1529 PackagePreferences.put("showBadge", Boolean.valueOf(r.showBadge));
1530 }
1531 JSONArray channels = new JSONArray();
1532 for (NotificationChannel channel : r.channels.values()) {
1533 channels.put(channel.toJson());
1534 }
1535 PackagePreferences.put("channels", channels);
1536 JSONArray groups = new JSONArray();
1537 for (NotificationChannelGroup group : r.groups.values()) {
1538 groups.put(group.toJson());
1539 }
1540 PackagePreferences.put("groups", groups);
1541 } catch (JSONException e) {
1542 // pass
1543 }
1544 PackagePreferencess.put(PackagePreferences);
1545 }
1546 }
1547 }
1548 try {
1549 ranking.put("PackagePreferencess", PackagePreferencess);
1550 } catch (JSONException e) {
1551 // pass
1552 }
1553 return ranking;
1554 }
1555
1556 /**
1557 * Dump only the ban information as structured JSON for the stats collector.
1558 *
1559 * This is intentionally redundant with {#link dumpJson} because the old
1560 * scraper will expect this format.
1561 *
1562 * @param filter
1563 * @return
1564 */
1565 public JSONArray dumpBansJson(NotificationManagerService.DumpFilter filter) {
1566 JSONArray bans = new JSONArray();
1567 Map<Integer, String> packageBans = getPackageBans();
1568 for (Map.Entry<Integer, String> ban : packageBans.entrySet()) {
1569 final int userId = UserHandle.getUserId(ban.getKey());
1570 final String packageName = ban.getValue();
1571 if (filter == null || filter.matches(packageName)) {
1572 JSONObject banJson = new JSONObject();
1573 try {
1574 banJson.put("userId", userId);
1575 banJson.put("packageName", packageName);
1576 } catch (JSONException e) {
1577 e.printStackTrace();
1578 }
1579 bans.put(banJson);
1580 }
1581 }
1582 return bans;
1583 }
1584
1585 public Map<Integer, String> getPackageBans() {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001586 synchronized (mPackagePreferences) {
1587 final int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001588 ArrayMap<Integer, String> packageBans = new ArrayMap<>(N);
1589 for (int i = 0; i < N; i++) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001590 final PackagePreferences r = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001591 if (r.importance == IMPORTANCE_NONE) {
1592 packageBans.put(r.uid, r.pkg);
1593 }
1594 }
1595
1596 return packageBans;
1597 }
1598 }
1599
1600 /**
1601 * Dump only the channel information as structured JSON for the stats collector.
1602 *
1603 * This is intentionally redundant with {#link dumpJson} because the old
1604 * scraper will expect this format.
1605 *
1606 * @param filter
1607 * @return
1608 */
1609 public JSONArray dumpChannelsJson(NotificationManagerService.DumpFilter filter) {
1610 JSONArray channels = new JSONArray();
1611 Map<String, Integer> packageChannels = getPackageChannels();
1612 for (Map.Entry<String, Integer> channelCount : packageChannels.entrySet()) {
1613 final String packageName = channelCount.getKey();
1614 if (filter == null || filter.matches(packageName)) {
1615 JSONObject channelCountJson = new JSONObject();
1616 try {
1617 channelCountJson.put("packageName", packageName);
1618 channelCountJson.put("channelCount", channelCount.getValue());
1619 } catch (JSONException e) {
1620 e.printStackTrace();
1621 }
1622 channels.put(channelCountJson);
1623 }
1624 }
1625 return channels;
1626 }
1627
1628 private Map<String, Integer> getPackageChannels() {
1629 ArrayMap<String, Integer> packageChannels = new ArrayMap<>();
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001630 synchronized (mPackagePreferences) {
1631 for (int i = 0; i < mPackagePreferences.size(); i++) {
1632 final PackagePreferences r = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001633 int channelCount = 0;
1634 for (int j = 0; j < r.channels.size(); j++) {
1635 if (!r.channels.valueAt(j).isDeleted()) {
1636 channelCount++;
1637 }
1638 }
1639 packageChannels.put(r.pkg, channelCount);
1640 }
1641 }
1642 return packageChannels;
1643 }
1644
Beverly0479cde22018-11-09 11:05:34 -05001645 /**
1646 * Called when user switches
1647 */
1648 public void onUserSwitched(int userId) {
1649 syncChannelsBypassingDnd(userId);
1650 }
1651
1652 /**
1653 * Called when user is unlocked
1654 */
1655 public void onUserUnlocked(int userId) {
1656 syncChannelsBypassingDnd(userId);
1657 }
1658
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001659 public void onUserRemoved(int userId) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001660 synchronized (mPackagePreferences) {
1661 int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001662 for (int i = N - 1; i >= 0; i--) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001663 PackagePreferences PackagePreferences = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001664 if (UserHandle.getUserId(PackagePreferences.uid) == userId) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001665 mPackagePreferences.removeAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001666 }
1667 }
1668 }
1669 }
1670
1671 protected void onLocaleChanged(Context context, int userId) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001672 synchronized (mPackagePreferences) {
1673 int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001674 for (int i = 0; i < N; i++) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001675 PackagePreferences PackagePreferences = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001676 if (UserHandle.getUserId(PackagePreferences.uid) == userId) {
1677 if (PackagePreferences.channels.containsKey(
1678 NotificationChannel.DEFAULT_CHANNEL_ID)) {
1679 PackagePreferences.channels.get(
1680 NotificationChannel.DEFAULT_CHANNEL_ID).setName(
1681 context.getResources().getString(
1682 R.string.default_notification_channel_label));
1683 }
1684 }
1685 }
1686 }
1687 }
1688
1689 public void onPackagesChanged(boolean removingPackage, int changeUserId, String[] pkgList,
1690 int[] uidList) {
1691 if (pkgList == null || pkgList.length == 0) {
1692 return; // nothing to do
1693 }
1694 boolean updated = false;
1695 if (removingPackage) {
1696 // Remove notification settings for uninstalled package
1697 int size = Math.min(pkgList.length, uidList.length);
1698 for (int i = 0; i < size; i++) {
1699 final String pkg = pkgList[i];
1700 final int uid = uidList[i];
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001701 synchronized (mPackagePreferences) {
1702 mPackagePreferences.remove(packagePreferencesKey(pkg, uid));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001703 }
1704 mRestoredWithoutUids.remove(pkg);
1705 updated = true;
1706 }
1707 } else {
1708 for (String pkg : pkgList) {
1709 // Package install
1710 final PackagePreferences r = mRestoredWithoutUids.get(pkg);
1711 if (r != null) {
1712 try {
1713 r.uid = mPm.getPackageUidAsUser(r.pkg, changeUserId);
1714 mRestoredWithoutUids.remove(pkg);
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001715 synchronized (mPackagePreferences) {
1716 mPackagePreferences.put(packagePreferencesKey(r.pkg, r.uid), r);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001717 }
1718 updated = true;
1719 } catch (PackageManager.NameNotFoundException e) {
1720 // noop
1721 }
1722 }
1723 // Package upgrade
1724 try {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001725 synchronized (mPackagePreferences) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001726 PackagePreferences fullPackagePreferences = getPackagePreferencesLocked(pkg,
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001727 mPm.getPackageUidAsUser(pkg, changeUserId));
1728 if (fullPackagePreferences != null) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001729 createDefaultChannelIfNeededLocked(fullPackagePreferences);
1730 deleteDefaultChannelIfNeededLocked(fullPackagePreferences);
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001731 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001732 }
1733 } catch (PackageManager.NameNotFoundException e) {
1734 }
1735 }
1736 }
1737
1738 if (updated) {
1739 updateConfig();
1740 }
1741 }
1742
Julia Reynolds7af51c52019-04-19 11:08:27 -04001743 public void clearData(String pkg, int uid) {
1744 synchronized (mPackagePreferences) {
1745 PackagePreferences p = getPackagePreferencesLocked(pkg, uid);
1746 if (p != null) {
1747 p.channels = new ArrayMap<>();
1748 p.groups = new ArrayMap<>();
1749 p.delegate = null;
1750 p.lockedAppFields = DEFAULT_LOCKED_APP_FIELDS;
1751 p.allowBubble = DEFAULT_ALLOW_BUBBLE;
1752 p.importance = DEFAULT_IMPORTANCE;
1753 p.priority = DEFAULT_PRIORITY;
1754 p.visibility = DEFAULT_VISIBILITY;
1755 p.showBadge = DEFAULT_SHOW_BADGE;
1756 }
1757 }
1758 }
1759
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001760 private LogMaker getChannelLog(NotificationChannel channel, String pkg) {
1761 return new LogMaker(
1762 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1763 .ACTION_NOTIFICATION_CHANNEL)
1764 .setType(com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_UPDATE)
1765 .setPackageName(pkg)
1766 .addTaggedData(
1767 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1768 .FIELD_NOTIFICATION_CHANNEL_ID,
1769 channel.getId())
1770 .addTaggedData(
1771 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1772 .FIELD_NOTIFICATION_CHANNEL_IMPORTANCE,
1773 channel.getImportance());
1774 }
1775
1776 private LogMaker getChannelGroupLog(String groupId, String pkg) {
1777 return new LogMaker(
1778 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1779 .ACTION_NOTIFICATION_CHANNEL_GROUP)
1780 .setType(com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_UPDATE)
1781 .addTaggedData(
1782 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1783 .FIELD_NOTIFICATION_CHANNEL_GROUP_ID,
1784 groupId)
1785 .setPackageName(pkg);
1786 }
1787
Julia Reynolds4509ce72019-01-31 13:12:43 -05001788 public void updateBubblesEnabled() {
1789 if (mBubblesEnabled == null) {
1790 mBubblesEnabled = new SparseBooleanArray();
1791 }
1792 boolean changed = false;
1793 // update the cached values
1794 for (int index = 0; index < mBubblesEnabled.size(); index++) {
1795 int userId = mBubblesEnabled.keyAt(index);
1796 final boolean oldValue = mBubblesEnabled.get(userId);
1797 final boolean newValue = Settings.Secure.getIntForUser(mContext.getContentResolver(),
1798 Settings.Secure.NOTIFICATION_BUBBLES,
1799 DEFAULT_ALLOW_BUBBLE ? 1 : 0, userId) != 0;
1800 mBubblesEnabled.put(userId, newValue);
1801 changed |= oldValue != newValue;
1802 }
1803 if (changed) {
1804 updateConfig();
1805 }
1806 }
1807
1808 public boolean bubblesEnabled(UserHandle userHandle) {
1809 int userId = userHandle.getIdentifier();
1810 if (userId == UserHandle.USER_ALL) {
1811 return false;
1812 }
1813 if (mBubblesEnabled.indexOfKey(userId) < 0) {
1814 mBubblesEnabled.put(userId,
1815 Settings.Secure.getIntForUser(mContext.getContentResolver(),
1816 Settings.Secure.NOTIFICATION_BUBBLES,
1817 DEFAULT_ALLOW_BUBBLE ? 1 : 0, userId) != 0);
1818 }
1819 return mBubblesEnabled.get(userId, DEFAULT_ALLOW_BUBBLE);
1820 }
1821
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001822
1823 public void updateBadgingEnabled() {
1824 if (mBadgingEnabled == null) {
1825 mBadgingEnabled = new SparseBooleanArray();
1826 }
1827 boolean changed = false;
1828 // update the cached values
1829 for (int index = 0; index < mBadgingEnabled.size(); index++) {
1830 int userId = mBadgingEnabled.keyAt(index);
1831 final boolean oldValue = mBadgingEnabled.get(userId);
1832 final boolean newValue = Settings.Secure.getIntForUser(mContext.getContentResolver(),
1833 Settings.Secure.NOTIFICATION_BADGING,
1834 DEFAULT_SHOW_BADGE ? 1 : 0, userId) != 0;
1835 mBadgingEnabled.put(userId, newValue);
1836 changed |= oldValue != newValue;
1837 }
1838 if (changed) {
1839 updateConfig();
1840 }
1841 }
1842
1843 public boolean badgingEnabled(UserHandle userHandle) {
1844 int userId = userHandle.getIdentifier();
1845 if (userId == UserHandle.USER_ALL) {
1846 return false;
1847 }
1848 if (mBadgingEnabled.indexOfKey(userId) < 0) {
1849 mBadgingEnabled.put(userId,
1850 Settings.Secure.getIntForUser(mContext.getContentResolver(),
1851 Settings.Secure.NOTIFICATION_BADGING,
1852 DEFAULT_SHOW_BADGE ? 1 : 0, userId) != 0);
1853 }
1854 return mBadgingEnabled.get(userId, DEFAULT_SHOW_BADGE);
1855 }
1856
1857 private void updateConfig() {
1858 mRankingHandler.requestSort();
1859 }
1860
1861 private static String packagePreferencesKey(String pkg, int uid) {
1862 return pkg + "|" + uid;
1863 }
1864
1865 private static class PackagePreferences {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001866 String pkg;
1867 int uid = UNKNOWN_UID;
1868 int importance = DEFAULT_IMPORTANCE;
1869 int priority = DEFAULT_PRIORITY;
1870 int visibility = DEFAULT_VISIBILITY;
1871 boolean showBadge = DEFAULT_SHOW_BADGE;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001872 boolean allowBubble = DEFAULT_ALLOW_BUBBLE;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001873 int lockedAppFields = DEFAULT_LOCKED_APP_FIELDS;
Julia Reynolds0c245002019-03-27 16:10:11 -04001874 // these fields are loaded on boot from a different source of truth and so are not
1875 // written to notification policy xml
Julia Reynolds413ba842019-01-11 10:38:08 -05001876 boolean oemLockedImportance = DEFAULT_OEM_LOCKED_IMPORTANCE;
1877 List<String> futureOemLockedChannels = new ArrayList<>();
Julia Reynolds0c245002019-03-27 16:10:11 -04001878 boolean defaultAppLockedImportance = DEFAULT_APP_LOCKED_IMPORTANCE;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001879
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001880 Delegate delegate = null;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001881 ArrayMap<String, NotificationChannel> channels = new ArrayMap<>();
1882 Map<String, NotificationChannelGroup> groups = new ConcurrentHashMap<>();
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001883
1884 public boolean isValidDelegate(String pkg, int uid) {
1885 return delegate != null && delegate.isAllowed(pkg, uid);
1886 }
1887 }
1888
1889 private static class Delegate {
1890 static final boolean DEFAULT_ENABLED = true;
1891 static final boolean DEFAULT_USER_ALLOWED = true;
1892 String mPkg;
1893 int mUid = UNKNOWN_UID;
1894 boolean mEnabled = DEFAULT_ENABLED;
1895 boolean mUserAllowed = DEFAULT_USER_ALLOWED;
1896
1897 Delegate(String pkg, int uid, boolean enabled, boolean userAllowed) {
1898 mPkg = pkg;
1899 mUid = uid;
1900 mEnabled = enabled;
1901 mUserAllowed = userAllowed;
1902 }
1903
1904 public boolean isAllowed(String pkg, int uid) {
1905 if (pkg == null || uid == UNKNOWN_UID) {
1906 return false;
1907 }
1908 return pkg.equals(mPkg)
1909 && uid == mUid
1910 && (mUserAllowed && mEnabled);
1911 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001912 }
1913}