blob: 23c5763e395a6d9cf1240b69012e8a3c44ecd944 [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 Reynolds996c7c12019-05-24 10:25:33 -0400337 private boolean 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
Julia Reynolds996c7c12019-05-24 10:25:33 -0400341 return false;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400342 }
343
344 if (shouldHaveDefaultChannel(r)) {
345 // Keep the default channel until upgraded.
Julia Reynolds996c7c12019-05-24 10:25:33 -0400346 return false;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400347 }
348
349 // Remove Default Channel.
350 r.channels.remove(NotificationChannel.DEFAULT_CHANNEL_ID);
Julia Reynolds996c7c12019-05-24 10:25:33 -0400351
352 return true;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400353 }
354
Julia Reynolds996c7c12019-05-24 10:25:33 -0400355 private boolean createDefaultChannelIfNeededLocked(PackagePreferences r) throws
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400356 PackageManager.NameNotFoundException {
357 if (r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) {
358 r.channels.get(NotificationChannel.DEFAULT_CHANNEL_ID).setName(mContext.getString(
359 com.android.internal.R.string.default_notification_channel_label));
Julia Reynolds996c7c12019-05-24 10:25:33 -0400360 return false;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400361 }
362
363 if (!shouldHaveDefaultChannel(r)) {
364 // Keep the default channel until upgraded.
Julia Reynolds996c7c12019-05-24 10:25:33 -0400365 return false;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400366 }
367
368 // Create Default Channel
369 NotificationChannel channel;
370 channel = new NotificationChannel(
371 NotificationChannel.DEFAULT_CHANNEL_ID,
372 mContext.getString(R.string.default_notification_channel_label),
373 r.importance);
374 channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX);
375 channel.setLockscreenVisibility(r.visibility);
376 if (r.importance != NotificationManager.IMPORTANCE_UNSPECIFIED) {
377 channel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
378 }
379 if (r.priority != DEFAULT_PRIORITY) {
380 channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
381 }
382 if (r.visibility != DEFAULT_VISIBILITY) {
383 channel.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY);
384 }
385 r.channels.put(channel.getId(), channel);
Julia Reynolds996c7c12019-05-24 10:25:33 -0400386
387 return true;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400388 }
389
Annie Meng8b646fd2019-02-01 18:46:42 +0000390 public void writeXml(XmlSerializer out, boolean forBackup, int userId) throws IOException {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400391 out.startTag(null, TAG_RANKING);
392 out.attribute(null, ATT_VERSION, Integer.toString(XML_VERSION));
Annie Meng8b646fd2019-02-01 18:46:42 +0000393 if (mHideSilentStatusBarIcons != DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS
394 && (!forBackup || userId == UserHandle.USER_SYSTEM)) {
Julia Reynolds12ad7ca2019-01-28 09:29:16 -0500395 out.startTag(null, TAG_STATUS_ICONS);
396 out.attribute(null, ATT_HIDE_SILENT, String.valueOf(mHideSilentStatusBarIcons));
397 out.endTag(null, TAG_STATUS_ICONS);
398 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400399
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400400 synchronized (mPackagePreferences) {
401 final int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400402 for (int i = 0; i < N; i++) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -0400403 final PackagePreferences r = mPackagePreferences.valueAt(i);
Annie Meng8b646fd2019-02-01 18:46:42 +0000404 if (forBackup && UserHandle.getUserId(r.uid) != userId) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400405 continue;
406 }
407 final boolean hasNonDefaultSettings =
408 r.importance != DEFAULT_IMPORTANCE
409 || r.priority != DEFAULT_PRIORITY
410 || r.visibility != DEFAULT_VISIBILITY
411 || r.showBadge != DEFAULT_SHOW_BADGE
412 || r.lockedAppFields != DEFAULT_LOCKED_APP_FIELDS
413 || r.channels.size() > 0
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400414 || r.groups.size() > 0
Julia Reynolds33ab8a02018-12-17 16:19:52 -0500415 || r.delegate != null
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800416 || r.allowBubble != DEFAULT_ALLOW_BUBBLE;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400417 if (hasNonDefaultSettings) {
418 out.startTag(null, TAG_PACKAGE);
419 out.attribute(null, ATT_NAME, r.pkg);
420 if (r.importance != DEFAULT_IMPORTANCE) {
421 out.attribute(null, ATT_IMPORTANCE, Integer.toString(r.importance));
422 }
423 if (r.priority != DEFAULT_PRIORITY) {
424 out.attribute(null, ATT_PRIORITY, Integer.toString(r.priority));
425 }
426 if (r.visibility != DEFAULT_VISIBILITY) {
427 out.attribute(null, ATT_VISIBILITY, Integer.toString(r.visibility));
428 }
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800429 if (r.allowBubble != DEFAULT_ALLOW_BUBBLE) {
430 out.attribute(null, ATT_ALLOW_BUBBLE, Boolean.toString(r.allowBubble));
Julia Reynolds33ab8a02018-12-17 16:19:52 -0500431 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400432 out.attribute(null, ATT_SHOW_BADGE, Boolean.toString(r.showBadge));
433 out.attribute(null, ATT_APP_USER_LOCKED_FIELDS,
434 Integer.toString(r.lockedAppFields));
435
436 if (!forBackup) {
437 out.attribute(null, ATT_UID, Integer.toString(r.uid));
438 }
439
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -0400440 if (r.delegate != null) {
441 out.startTag(null, TAG_DELEGATE);
442
443 out.attribute(null, ATT_NAME, r.delegate.mPkg);
444 out.attribute(null, ATT_UID, Integer.toString(r.delegate.mUid));
445 if (r.delegate.mEnabled != Delegate.DEFAULT_ENABLED) {
446 out.attribute(null, ATT_ENABLED, Boolean.toString(r.delegate.mEnabled));
447 }
448 if (r.delegate.mUserAllowed != Delegate.DEFAULT_USER_ALLOWED) {
449 out.attribute(null, ATT_USER_ALLOWED,
450 Boolean.toString(r.delegate.mUserAllowed));
451 }
452 out.endTag(null, TAG_DELEGATE);
453 }
454
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400455 for (NotificationChannelGroup group : r.groups.values()) {
456 group.writeXml(out);
457 }
458
459 for (NotificationChannel channel : r.channels.values()) {
460 if (forBackup) {
461 if (!channel.isDeleted()) {
462 channel.writeXmlForBackup(out, mContext);
463 }
464 } else {
465 channel.writeXml(out);
466 }
467 }
468
469 out.endTag(null, TAG_PACKAGE);
470 }
471 }
472 }
473 out.endTag(null, TAG_RANKING);
474 }
475
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800476 /**
477 * Sets whether bubbles are allowed.
478 *
479 * @param pkg the package to allow or not allow bubbles for.
480 * @param uid the uid to allow or not allow bubbles for.
481 * @param allowed whether bubbles are allowed.
482 */
483 public void setBubblesAllowed(String pkg, int uid, boolean allowed) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400484 synchronized (mPackagePreferences) {
485 PackagePreferences p = getOrCreatePackagePreferencesLocked(pkg, uid);
486 p.allowBubble = allowed;
487 p.lockedAppFields = p.lockedAppFields | LockableAppFields.USER_LOCKED_BUBBLE;
488 }
Julia Reynolds33ab8a02018-12-17 16:19:52 -0500489 }
490
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800491 /**
492 * Whether bubbles are allowed.
493 *
494 * @param pkg the package to check if bubbles are allowed for
495 * @param uid the uid to check if bubbles are allowed for.
496 * @return whether bubbles are allowed.
497 */
Julia Reynolds4509ce72019-01-31 13:12:43 -0500498 @Override
Mady Mellor9db685a2019-01-23 13:23:37 -0800499 public boolean areBubblesAllowed(String pkg, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400500 synchronized (mPackagePreferences) {
501 return getOrCreatePackagePreferencesLocked(pkg, uid).allowBubble;
502 }
Julia Reynolds33ab8a02018-12-17 16:19:52 -0500503 }
504
505 public int getAppLockedFields(String pkg, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400506 synchronized (mPackagePreferences) {
507 return getOrCreatePackagePreferencesLocked(pkg, uid).lockedAppFields;
508 }
Julia Reynolds33ab8a02018-12-17 16:19:52 -0500509 }
510
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400511 /**
512 * Gets importance.
513 */
514 @Override
515 public int getImportance(String packageName, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400516 synchronized (mPackagePreferences) {
517 return getOrCreatePackagePreferencesLocked(packageName, uid).importance;
518 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400519 }
520
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400521 /**
522 * Returns whether the importance of the corresponding notification is user-locked and shouldn't
523 * be adjusted by an assistant (via means of a blocking helper, for example). For the channel
524 * locking field, see {@link NotificationChannel#USER_LOCKED_IMPORTANCE}.
525 */
526 public boolean getIsAppImportanceLocked(String packageName, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400527 synchronized (mPackagePreferences) {
528 int userLockedFields = getOrCreatePackagePreferencesLocked(packageName, uid).lockedAppFields;
529 return (userLockedFields & LockableAppFields.USER_LOCKED_IMPORTANCE) != 0;
530 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400531 }
532
533 @Override
534 public boolean canShowBadge(String packageName, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400535 synchronized (mPackagePreferences) {
536 return getOrCreatePackagePreferencesLocked(packageName, uid).showBadge;
537 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400538 }
539
540 @Override
541 public void setShowBadge(String packageName, int uid, boolean showBadge) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400542 synchronized (mPackagePreferences) {
543 getOrCreatePackagePreferencesLocked(packageName, uid).showBadge = showBadge;
544 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400545 updateConfig();
546 }
547
548 @Override
549 public boolean isGroupBlocked(String packageName, int uid, String groupId) {
550 if (groupId == null) {
551 return false;
552 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400553 synchronized (mPackagePreferences) {
554 PackagePreferences r = getOrCreatePackagePreferencesLocked(packageName, uid);
555 NotificationChannelGroup group = r.groups.get(groupId);
556 if (group == null) {
557 return false;
558 }
559 return group.isBlocked();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400560 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400561 }
562
563 int getPackagePriority(String pkg, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400564 synchronized (mPackagePreferences) {
565 return getOrCreatePackagePreferencesLocked(pkg, uid).priority;
566 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400567 }
568
569 int getPackageVisibility(String pkg, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400570 synchronized (mPackagePreferences) {
571 return getOrCreatePackagePreferencesLocked(pkg, uid).visibility;
572 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400573 }
574
575 @Override
576 public void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group,
577 boolean fromTargetApp) {
578 Preconditions.checkNotNull(pkg);
579 Preconditions.checkNotNull(group);
580 Preconditions.checkNotNull(group.getId());
581 Preconditions.checkNotNull(!TextUtils.isEmpty(group.getName()));
Julia Reynolds5c399c62019-04-08 14:42:53 -0400582 synchronized (mPackagePreferences) {
583 PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
584 if (r == null) {
585 throw new IllegalArgumentException("Invalid package");
586 }
587 final NotificationChannelGroup oldGroup = r.groups.get(group.getId());
588 if (!group.equals(oldGroup)) {
589 // will log for new entries as well as name/description changes
590 MetricsLogger.action(getChannelGroupLog(group.getId(), pkg));
591 }
592 if (oldGroup != null) {
593 group.setChannels(oldGroup.getChannels());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400594
Julia Reynolds5c399c62019-04-08 14:42:53 -0400595 // apps can't update the blocked status or app overlay permission
596 if (fromTargetApp) {
597 group.setBlocked(oldGroup.isBlocked());
598 group.unlockFields(group.getUserLockedFields());
599 group.lockFields(oldGroup.getUserLockedFields());
600 } else {
601 // but the system can
602 if (group.isBlocked() != oldGroup.isBlocked()) {
603 group.lockFields(NotificationChannelGroup.USER_LOCKED_BLOCKED_STATE);
604 updateChannelsBypassingDnd(mContext.getUserId());
605 }
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400606 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400607 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400608 r.groups.put(group.getId(), group);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400609 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400610 }
611
612 @Override
Julia Reynoldsdafd3a42019-05-24 13:33:28 -0400613 public boolean createNotificationChannel(String pkg, int uid, NotificationChannel channel,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400614 boolean fromTargetApp, boolean hasDndAccess) {
615 Preconditions.checkNotNull(pkg);
616 Preconditions.checkNotNull(channel);
617 Preconditions.checkNotNull(channel.getId());
618 Preconditions.checkArgument(!TextUtils.isEmpty(channel.getName()));
Julia Reynoldsdafd3a42019-05-24 13:33:28 -0400619 boolean needsPolicyFileChange = false;
Julia Reynolds5c399c62019-04-08 14:42:53 -0400620 synchronized (mPackagePreferences) {
621 PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
622 if (r == null) {
623 throw new IllegalArgumentException("Invalid package");
624 }
625 if (channel.getGroup() != null && !r.groups.containsKey(channel.getGroup())) {
626 throw new IllegalArgumentException("NotificationChannelGroup doesn't exist");
627 }
628 if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channel.getId())) {
629 throw new IllegalArgumentException("Reserved id");
630 }
631 NotificationChannel existing = r.channels.get(channel.getId());
632 // Keep most of the existing settings
633 if (existing != null && fromTargetApp) {
634 if (existing.isDeleted()) {
635 existing.setDeleted(false);
Julia Reynoldsdafd3a42019-05-24 13:33:28 -0400636 needsPolicyFileChange = true;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400637
Julia Reynolds5c399c62019-04-08 14:42:53 -0400638 // log a resurrected channel as if it's new again
639 MetricsLogger.action(getChannelLog(channel, pkg).setType(
640 com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_OPEN));
641 }
642
Julia Reynoldsdafd3a42019-05-24 13:33:28 -0400643 if (!Objects.equals(channel.getName().toString(), existing.getName().toString())) {
644 existing.setName(channel.getName().toString());
645 needsPolicyFileChange = true;
646 }
647 if (!Objects.equals(channel.getDescription(), existing.getDescription())) {
648 existing.setDescription(channel.getDescription());
649 needsPolicyFileChange = true;
650 }
651 if (channel.isBlockableSystem() != existing.isBlockableSystem()) {
652 existing.setBlockableSystem(channel.isBlockableSystem());
653 needsPolicyFileChange = true;
654 }
655 if (channel.getGroup() != null && existing.getGroup() == null) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400656 existing.setGroup(channel.getGroup());
Julia Reynoldsdafd3a42019-05-24 13:33:28 -0400657 needsPolicyFileChange = true;
Julia Reynolds5c399c62019-04-08 14:42:53 -0400658 }
659
660 // Apps are allowed to downgrade channel importance if the user has not changed any
661 // fields on this channel yet.
662 final int previousExistingImportance = existing.getImportance();
663 if (existing.getUserLockedFields() == 0 &&
664 channel.getImportance() < existing.getImportance()) {
665 existing.setImportance(channel.getImportance());
Julia Reynoldsdafd3a42019-05-24 13:33:28 -0400666 needsPolicyFileChange = true;
Julia Reynolds5c399c62019-04-08 14:42:53 -0400667 }
668
669 // system apps and dnd access apps can bypass dnd if the user hasn't changed any
670 // fields on the channel yet
671 if (existing.getUserLockedFields() == 0 && hasDndAccess) {
672 boolean bypassDnd = channel.canBypassDnd();
Julia Reynoldsdafd3a42019-05-24 13:33:28 -0400673 if (bypassDnd != existing.canBypassDnd()) {
674 existing.setBypassDnd(bypassDnd);
675 needsPolicyFileChange = true;
Julia Reynolds5c399c62019-04-08 14:42:53 -0400676
Julia Reynoldsdafd3a42019-05-24 13:33:28 -0400677 if (bypassDnd != mAreChannelsBypassingDnd
678 || previousExistingImportance != existing.getImportance()) {
679 updateChannelsBypassingDnd(mContext.getUserId());
680 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400681 }
682 }
683
684 updateConfig();
Julia Reynoldsdafd3a42019-05-24 13:33:28 -0400685 return needsPolicyFileChange;
Julia Reynolds5c399c62019-04-08 14:42:53 -0400686 }
Julia Reynoldsdafd3a42019-05-24 13:33:28 -0400687
688 needsPolicyFileChange = true;
689
Julia Reynolds5c399c62019-04-08 14:42:53 -0400690 if (channel.getImportance() < IMPORTANCE_NONE
691 || channel.getImportance() > NotificationManager.IMPORTANCE_MAX) {
692 throw new IllegalArgumentException("Invalid importance level");
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400693 }
694
Julia Reynolds5c399c62019-04-08 14:42:53 -0400695 // Reset fields that apps aren't allowed to set.
696 if (fromTargetApp && !hasDndAccess) {
697 channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400698 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400699 if (fromTargetApp) {
700 channel.setLockscreenVisibility(r.visibility);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400701 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400702 clearLockedFieldsLocked(channel);
703 channel.setImportanceLockedByOEM(r.oemLockedImportance);
704 if (!channel.isImportanceLockedByOEM()) {
705 if (r.futureOemLockedChannels.remove(channel.getId())) {
706 channel.setImportanceLockedByOEM(true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400707 }
708 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400709 channel.setImportanceLockedByCriticalDeviceFunction(r.defaultAppLockedImportance);
710 if (channel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
711 channel.setLockscreenVisibility(
712 NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE);
Julia Reynolds413ba842019-01-11 10:38:08 -0500713 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400714 if (!r.showBadge) {
715 channel.setShowBadge(false);
716 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400717
Julia Reynolds5c399c62019-04-08 14:42:53 -0400718 r.channels.put(channel.getId(), channel);
719 if (channel.canBypassDnd() != mAreChannelsBypassingDnd) {
720 updateChannelsBypassingDnd(mContext.getUserId());
721 }
722 MetricsLogger.action(getChannelLog(channel, pkg).setType(
723 com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_OPEN));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400724 }
Julia Reynoldsdafd3a42019-05-24 13:33:28 -0400725
726 return needsPolicyFileChange;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400727 }
728
Julia Reynolds5c399c62019-04-08 14:42:53 -0400729 void clearLockedFieldsLocked(NotificationChannel channel) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400730 channel.unlockFields(channel.getUserLockedFields());
731 }
732
733 @Override
734 public void updateNotificationChannel(String pkg, int uid, NotificationChannel updatedChannel,
735 boolean fromUser) {
736 Preconditions.checkNotNull(updatedChannel);
737 Preconditions.checkNotNull(updatedChannel.getId());
Julia Reynolds5c399c62019-04-08 14:42:53 -0400738 synchronized (mPackagePreferences) {
739 PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
740 if (r == null) {
741 throw new IllegalArgumentException("Invalid package");
742 }
743 NotificationChannel channel = r.channels.get(updatedChannel.getId());
744 if (channel == null || channel.isDeleted()) {
745 throw new IllegalArgumentException("Channel does not exist");
746 }
747 if (updatedChannel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
748 updatedChannel.setLockscreenVisibility(
749 NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE);
750 }
751 if (fromUser) {
752 updatedChannel.lockFields(channel.getUserLockedFields());
753 lockFieldsForUpdateLocked(channel, updatedChannel);
754 } else {
755 updatedChannel.unlockFields(updatedChannel.getUserLockedFields());
756 }
757 // no importance updates are allowed if OEM blocked it
758 updatedChannel.setImportanceLockedByOEM(channel.isImportanceLockedByOEM());
759 if (updatedChannel.isImportanceLockedByOEM()) {
760 updatedChannel.setImportance(channel.getImportance());
761 }
762 updatedChannel.setImportanceLockedByCriticalDeviceFunction(
763 r.defaultAppLockedImportance);
Beverly47679222019-05-16 15:46:11 -0400764 if (updatedChannel.isImportanceLockedByCriticalDeviceFunction()
765 && updatedChannel.getImportance() == IMPORTANCE_NONE) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400766 updatedChannel.setImportance(channel.getImportance());
767 }
Julia Reynolds413ba842019-01-11 10:38:08 -0500768
Julia Reynolds5c399c62019-04-08 14:42:53 -0400769 r.channels.put(updatedChannel.getId(), updatedChannel);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400770
Julia Reynolds5c399c62019-04-08 14:42:53 -0400771 if (onlyHasDefaultChannel(pkg, uid)) {
772 // copy settings to app level so they are inherited by new channels
773 // when the app migrates
774 r.importance = updatedChannel.getImportance();
775 r.priority = updatedChannel.canBypassDnd()
776 ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT;
777 r.visibility = updatedChannel.getLockscreenVisibility();
778 r.showBadge = updatedChannel.canShowBadge();
779 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400780
Julia Reynolds5c399c62019-04-08 14:42:53 -0400781 if (!channel.equals(updatedChannel)) {
782 // only log if there are real changes
783 MetricsLogger.action(getChannelLog(updatedChannel, pkg)
784 .setSubtype(fromUser ? 1 : 0));
785 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400786
Julia Reynolds5c399c62019-04-08 14:42:53 -0400787 if (updatedChannel.canBypassDnd() != mAreChannelsBypassingDnd
788 || channel.getImportance() != updatedChannel.getImportance()) {
789 updateChannelsBypassingDnd(mContext.getUserId());
790 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400791 }
792 updateConfig();
793 }
794
795 @Override
796 public NotificationChannel getNotificationChannel(String pkg, int uid, String channelId,
797 boolean includeDeleted) {
798 Preconditions.checkNotNull(pkg);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400799 synchronized (mPackagePreferences) {
800 PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
801 if (r == null) {
802 return null;
803 }
804 if (channelId == null) {
805 channelId = NotificationChannel.DEFAULT_CHANNEL_ID;
806 }
807 final NotificationChannel nc = r.channels.get(channelId);
808 if (nc != null && (includeDeleted || !nc.isDeleted())) {
809 return nc;
810 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400811 return null;
812 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400813 }
814
815 @Override
816 public void deleteNotificationChannel(String pkg, int uid, String channelId) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400817 synchronized (mPackagePreferences) {
818 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
819 if (r == null) {
820 return;
821 }
822 NotificationChannel channel = r.channels.get(channelId);
823 if (channel != null) {
824 channel.setDeleted(true);
825 LogMaker lm = getChannelLog(channel, pkg);
826 lm.setType(com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_CLOSE);
827 MetricsLogger.action(lm);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400828
Julia Reynolds5c399c62019-04-08 14:42:53 -0400829 if (mAreChannelsBypassingDnd && channel.canBypassDnd()) {
830 updateChannelsBypassingDnd(mContext.getUserId());
831 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400832 }
833 }
834 }
835
836 @Override
837 @VisibleForTesting
838 public void permanentlyDeleteNotificationChannel(String pkg, int uid, String channelId) {
839 Preconditions.checkNotNull(pkg);
840 Preconditions.checkNotNull(channelId);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400841 synchronized (mPackagePreferences) {
842 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
843 if (r == null) {
844 return;
845 }
846 r.channels.remove(channelId);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400847 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400848 }
849
850 @Override
851 public void permanentlyDeleteNotificationChannels(String pkg, int uid) {
852 Preconditions.checkNotNull(pkg);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400853 synchronized (mPackagePreferences) {
854 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
855 if (r == null) {
856 return;
857 }
858 int N = r.channels.size() - 1;
859 for (int i = N; i >= 0; i--) {
860 String key = r.channels.keyAt(i);
861 if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(key)) {
862 r.channels.remove(key);
863 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400864 }
865 }
866 }
867
Julia Reynolds12ad7ca2019-01-28 09:29:16 -0500868 public boolean shouldHideSilentStatusIcons() {
869 return mHideSilentStatusBarIcons;
870 }
871
872 public void setHideSilentStatusIcons(boolean hide) {
873 mHideSilentStatusBarIcons = hide;
874 }
875
Julia Reynolds413ba842019-01-11 10:38:08 -0500876 public void lockChannelsForOEM(String[] appOrChannelList) {
877 if (appOrChannelList == null) {
878 return;
879 }
880 for (String appOrChannel : appOrChannelList) {
881 if (!TextUtils.isEmpty(appOrChannel)) {
882 String[] appSplit = appOrChannel.split(NON_BLOCKABLE_CHANNEL_DELIM);
883 if (appSplit != null && appSplit.length > 0) {
884 String appName = appSplit[0];
885 String channelId = appSplit.length == 2 ? appSplit[1] : null;
886
887 synchronized (mPackagePreferences) {
888 for (PackagePreferences r : mPackagePreferences.values()) {
889 if (r.pkg.equals(appName)) {
890 if (channelId == null) {
891 // lock all channels for the app
892 r.oemLockedImportance = true;
893 for (NotificationChannel channel : r.channels.values()) {
894 channel.setImportanceLockedByOEM(true);
895 }
896 } else {
897 NotificationChannel channel = r.channels.get(channelId);
898 if (channel != null) {
899 channel.setImportanceLockedByOEM(true);
900 } else {
901 // if this channel shows up in the future, make sure it'll
902 // be locked immediately
903 r.futureOemLockedChannels.add(channelId);
904 }
905 }
906 }
907 }
908 }
909 }
910 }
911 }
912 }
913
Julia Reynoldse7ca31b2019-04-25 15:41:47 -0400914 public void updateDefaultApps(int userId, ArraySet<String> toRemove,
915 ArraySet<Pair<String, Integer>> toAdd) {
Julia Reynolds0c245002019-03-27 16:10:11 -0400916 synchronized (mPackagePreferences) {
917 for (PackagePreferences p : mPackagePreferences.values()) {
918 if (userId == UserHandle.getUserId(p.uid)) {
919 if (toRemove != null && toRemove.contains(p.pkg)) {
920 p.defaultAppLockedImportance = false;
921 for (NotificationChannel channel : p.channels.values()) {
922 channel.setImportanceLockedByCriticalDeviceFunction(false);
923 }
Julia Reynoldse7ca31b2019-04-25 15:41:47 -0400924 }
925 }
926 }
927 if (toAdd != null) {
928 for (Pair<String, Integer> approvedApp : toAdd) {
929 PackagePreferences p = getOrCreatePackagePreferencesLocked(approvedApp.first,
930 approvedApp.second);
931 p.defaultAppLockedImportance = true;
932 for (NotificationChannel channel : p.channels.values()) {
933 channel.setImportanceLockedByCriticalDeviceFunction(true);
Julia Reynolds0c245002019-03-27 16:10:11 -0400934 }
935 }
936 }
937 }
938 }
939
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400940 public NotificationChannelGroup getNotificationChannelGroupWithChannels(String pkg,
941 int uid, String groupId, boolean includeDeleted) {
942 Preconditions.checkNotNull(pkg);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400943 synchronized (mPackagePreferences) {
944 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
945 if (r == null || groupId == null || !r.groups.containsKey(groupId)) {
946 return null;
947 }
948 NotificationChannelGroup group = r.groups.get(groupId).clone();
949 group.setChannels(new ArrayList<>());
950 int N = r.channels.size();
951 for (int i = 0; i < N; i++) {
952 final NotificationChannel nc = r.channels.valueAt(i);
953 if (includeDeleted || !nc.isDeleted()) {
954 if (groupId.equals(nc.getGroup())) {
955 group.addChannel(nc);
956 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400957 }
958 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400959 return group;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400960 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400961 }
962
963 public NotificationChannelGroup getNotificationChannelGroup(String groupId, String pkg,
964 int uid) {
965 Preconditions.checkNotNull(pkg);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400966 synchronized (mPackagePreferences) {
967 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
968 if (r == null) {
969 return null;
970 }
971 return r.groups.get(groupId);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400972 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400973 }
974
975 @Override
976 public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups(String pkg,
Julia Reynolds13ed28b2018-09-21 15:20:13 -0400977 int uid, boolean includeDeleted, boolean includeNonGrouped, boolean includeEmpty) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400978 Preconditions.checkNotNull(pkg);
979 Map<String, NotificationChannelGroup> groups = new ArrayMap<>();
Julia Reynolds5c399c62019-04-08 14:42:53 -0400980 synchronized (mPackagePreferences) {
981 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
982 if (r == null) {
983 return ParceledListSlice.emptyList();
984 }
985 NotificationChannelGroup nonGrouped = new NotificationChannelGroup(null, null);
986 int N = r.channels.size();
987 for (int i = 0; i < N; i++) {
988 final NotificationChannel nc = r.channels.valueAt(i);
989 if (includeDeleted || !nc.isDeleted()) {
990 if (nc.getGroup() != null) {
991 if (r.groups.get(nc.getGroup()) != null) {
992 NotificationChannelGroup ncg = groups.get(nc.getGroup());
993 if (ncg == null) {
994 ncg = r.groups.get(nc.getGroup()).clone();
995 ncg.setChannels(new ArrayList<>());
996 groups.put(nc.getGroup(), ncg);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400997
Julia Reynolds5c399c62019-04-08 14:42:53 -0400998 }
999 ncg.addChannel(nc);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001000 }
Julia Reynolds5c399c62019-04-08 14:42:53 -04001001 } else {
1002 nonGrouped.addChannel(nc);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001003 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001004 }
1005 }
Julia Reynolds5c399c62019-04-08 14:42:53 -04001006 if (includeNonGrouped && nonGrouped.getChannels().size() > 0) {
1007 groups.put(null, nonGrouped);
1008 }
1009 if (includeEmpty) {
1010 for (NotificationChannelGroup group : r.groups.values()) {
1011 if (!groups.containsKey(group.getId())) {
1012 groups.put(group.getId(), group);
1013 }
Julia Reynolds13ed28b2018-09-21 15:20:13 -04001014 }
1015 }
Julia Reynolds5c399c62019-04-08 14:42:53 -04001016 return new ParceledListSlice<>(new ArrayList<>(groups.values()));
Julia Reynolds13ed28b2018-09-21 15:20:13 -04001017 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001018 }
1019
1020 public List<NotificationChannel> deleteNotificationChannelGroup(String pkg, int uid,
1021 String groupId) {
1022 List<NotificationChannel> deletedChannels = new ArrayList<>();
Julia Reynolds5c399c62019-04-08 14:42:53 -04001023 synchronized (mPackagePreferences) {
1024 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
1025 if (r == null || TextUtils.isEmpty(groupId)) {
1026 return deletedChannels;
1027 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001028
Julia Reynolds5c399c62019-04-08 14:42:53 -04001029 r.groups.remove(groupId);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001030
Julia Reynolds5c399c62019-04-08 14:42:53 -04001031 int N = r.channels.size();
1032 for (int i = 0; i < N; i++) {
1033 final NotificationChannel nc = r.channels.valueAt(i);
1034 if (groupId.equals(nc.getGroup())) {
1035 nc.setDeleted(true);
1036 deletedChannels.add(nc);
1037 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001038 }
1039 }
1040 return deletedChannels;
1041 }
1042
1043 @Override
1044 public Collection<NotificationChannelGroup> getNotificationChannelGroups(String pkg,
1045 int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001046 List<NotificationChannelGroup> groups = new ArrayList<>();
1047 synchronized (mPackagePreferences) {
1048 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
1049 if (r == null) {
1050 return groups;
1051 }
1052 groups.addAll(r.groups.values());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001053 }
Julia Reynolds5c399c62019-04-08 14:42:53 -04001054 return groups;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001055 }
1056
1057 @Override
1058 public ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg, int uid,
1059 boolean includeDeleted) {
1060 Preconditions.checkNotNull(pkg);
1061 List<NotificationChannel> channels = new ArrayList<>();
Julia Reynolds5c399c62019-04-08 14:42:53 -04001062 synchronized (mPackagePreferences) {
1063 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
1064 if (r == null) {
1065 return ParceledListSlice.emptyList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001066 }
Julia Reynolds5c399c62019-04-08 14:42:53 -04001067 int N = r.channels.size();
1068 for (int i = 0; i < N; i++) {
1069 final NotificationChannel nc = r.channels.valueAt(i);
1070 if (includeDeleted || !nc.isDeleted()) {
1071 channels.add(nc);
1072 }
1073 }
1074 return new ParceledListSlice<>(channels);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001075 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001076 }
1077
1078 /**
Beverly0479cde22018-11-09 11:05:34 -05001079 * Gets all notification channels associated with the given pkg and userId that can bypass dnd
1080 */
1081 public ParceledListSlice<NotificationChannel> getNotificationChannelsBypassingDnd(String pkg,
1082 int userId) {
1083 List<NotificationChannel> channels = new ArrayList<>();
1084 synchronized (mPackagePreferences) {
1085 final PackagePreferences r = mPackagePreferences.get(
1086 packagePreferencesKey(pkg, userId));
1087 // notifications from this package aren't blocked
1088 if (r != null && r.importance != IMPORTANCE_NONE) {
1089 for (NotificationChannel channel : r.channels.values()) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001090 if (channelIsLiveLocked(r, channel) && channel.canBypassDnd()) {
Beverly0479cde22018-11-09 11:05:34 -05001091 channels.add(channel);
1092 }
1093 }
1094 }
1095 }
1096 return new ParceledListSlice<>(channels);
1097 }
1098
1099 /**
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001100 * True for pre-O apps that only have the default channel, or pre O apps that have no
1101 * channels yet. This method will create the default channel for pre-O apps that don't have it.
1102 * Should never be true for O+ targeting apps, but that's enforced on boot/when an app
1103 * upgrades.
1104 */
1105 public boolean onlyHasDefaultChannel(String pkg, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001106 synchronized (mPackagePreferences) {
1107 PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
1108 if (r.channels.size() == 1
1109 && r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) {
1110 return true;
1111 }
1112 return false;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001113 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001114 }
1115
1116 public int getDeletedChannelCount(String pkg, int uid) {
1117 Preconditions.checkNotNull(pkg);
1118 int deletedCount = 0;
Julia Reynolds5c399c62019-04-08 14:42:53 -04001119 synchronized (mPackagePreferences) {
1120 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
1121 if (r == null) {
1122 return deletedCount;
1123 }
1124 int N = r.channels.size();
1125 for (int i = 0; i < N; i++) {
1126 final NotificationChannel nc = r.channels.valueAt(i);
1127 if (nc.isDeleted()) {
1128 deletedCount++;
1129 }
1130 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001131 return deletedCount;
1132 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001133 }
1134
1135 public int getBlockedChannelCount(String pkg, int uid) {
1136 Preconditions.checkNotNull(pkg);
1137 int blockedCount = 0;
Julia Reynolds5c399c62019-04-08 14:42:53 -04001138 synchronized (mPackagePreferences) {
1139 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
1140 if (r == null) {
1141 return blockedCount;
1142 }
1143 int N = r.channels.size();
1144 for (int i = 0; i < N; i++) {
1145 final NotificationChannel nc = r.channels.valueAt(i);
1146 if (!nc.isDeleted() && IMPORTANCE_NONE == nc.getImportance()) {
1147 blockedCount++;
1148 }
1149 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001150 return blockedCount;
1151 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001152 }
1153
1154 public int getBlockedAppCount(int userId) {
1155 int count = 0;
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001156 synchronized (mPackagePreferences) {
1157 final int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001158 for (int i = 0; i < N; i++) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001159 final PackagePreferences r = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001160 if (userId == UserHandle.getUserId(r.uid)
1161 && r.importance == IMPORTANCE_NONE) {
1162 count++;
1163 }
1164 }
1165 }
1166 return count;
1167 }
1168
Beverly0479cde22018-11-09 11:05:34 -05001169 /**
1170 * Returns the number of apps that have at least one notification channel that can bypass DND
1171 * for given particular user
1172 */
1173 public int getAppsBypassingDndCount(int userId) {
1174 int count = 0;
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001175 synchronized (mPackagePreferences) {
Beverly0479cde22018-11-09 11:05:34 -05001176 final int numPackagePreferences = mPackagePreferences.size();
1177 for (int i = 0; i < numPackagePreferences; i++) {
1178 final PackagePreferences r = mPackagePreferences.valueAt(i);
1179 // Package isn't associated with this userId or notifications from this package are
1180 // blocked
1181 if (userId != UserHandle.getUserId(r.uid) || r.importance == IMPORTANCE_NONE) {
1182 continue;
1183 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001184
Beverly0479cde22018-11-09 11:05:34 -05001185 for (NotificationChannel channel : r.channels.values()) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001186 if (channelIsLiveLocked(r, channel) && channel.canBypassDnd()) {
Beverly0479cde22018-11-09 11:05:34 -05001187 count++;
1188 break;
1189 }
1190 }
1191 }
1192 }
1193 return count;
1194 }
1195
1196 /**
1197 * Syncs {@link #mAreChannelsBypassingDnd} with the user's notification policy before
1198 * updating
1199 * @param userId
1200 */
1201 private void syncChannelsBypassingDnd(int userId) {
1202 mAreChannelsBypassingDnd = (mZenModeHelper.getNotificationPolicy().state
1203 & NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND) == 1;
1204 updateChannelsBypassingDnd(userId);
1205 }
1206
1207 /**
1208 * Updates the user's NotificationPolicy based on whether the given userId
1209 * has channels bypassing DND
1210 * @param userId
1211 */
1212 private void updateChannelsBypassingDnd(int userId) {
1213 synchronized (mPackagePreferences) {
1214 final int numPackagePreferences = mPackagePreferences.size();
1215 for (int i = 0; i < numPackagePreferences; i++) {
1216 final PackagePreferences r = mPackagePreferences.valueAt(i);
1217 // Package isn't associated with this userId or notifications from this package are
1218 // blocked
1219 if (userId != UserHandle.getUserId(r.uid) || r.importance == IMPORTANCE_NONE) {
1220 continue;
1221 }
1222
1223 for (NotificationChannel channel : r.channels.values()) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001224 if (channelIsLiveLocked(r, channel) && channel.canBypassDnd()) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001225 if (!mAreChannelsBypassingDnd) {
1226 mAreChannelsBypassingDnd = true;
1227 updateZenPolicy(true);
1228 }
1229 return;
1230 }
1231 }
1232 }
1233 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001234 // If no channels bypass DND, update the zen policy once to disable DND bypass.
1235 if (mAreChannelsBypassingDnd) {
1236 mAreChannelsBypassingDnd = false;
1237 updateZenPolicy(false);
1238 }
1239 }
1240
Julia Reynolds5c399c62019-04-08 14:42:53 -04001241 private boolean channelIsLiveLocked(PackagePreferences pkgPref, NotificationChannel channel) {
Beverly0479cde22018-11-09 11:05:34 -05001242 // Channel is in a group that's blocked
Beverly4f7b53d2018-11-20 09:56:31 -05001243 if (isGroupBlocked(pkgPref.pkg, pkgPref.uid, channel.getGroup())) {
1244 return false;
Beverly0479cde22018-11-09 11:05:34 -05001245 }
1246
1247 // Channel is deleted or is blocked
1248 if (channel.isDeleted() || channel.getImportance() == IMPORTANCE_NONE) {
1249 return false;
1250 }
1251
1252 return true;
1253 }
1254
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001255 public void updateZenPolicy(boolean areChannelsBypassingDnd) {
1256 NotificationManager.Policy policy = mZenModeHelper.getNotificationPolicy();
1257 mZenModeHelper.setNotificationPolicy(new NotificationManager.Policy(
1258 policy.priorityCategories, policy.priorityCallSenders,
1259 policy.priorityMessageSenders, policy.suppressedVisualEffects,
1260 (areChannelsBypassingDnd ? NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND
1261 : 0)));
1262 }
1263
1264 public boolean areChannelsBypassingDnd() {
1265 return mAreChannelsBypassingDnd;
1266 }
1267
1268 /**
1269 * Sets importance.
1270 */
1271 @Override
1272 public void setImportance(String pkgName, int uid, int importance) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001273 synchronized (mPackagePreferences) {
1274 getOrCreatePackagePreferencesLocked(pkgName, uid).importance = importance;
1275 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001276 updateConfig();
1277 }
1278
1279 public void setEnabled(String packageName, int uid, boolean enabled) {
1280 boolean wasEnabled = getImportance(packageName, uid) != IMPORTANCE_NONE;
1281 if (wasEnabled == enabled) {
1282 return;
1283 }
1284 setImportance(packageName, uid,
1285 enabled ? DEFAULT_IMPORTANCE : IMPORTANCE_NONE);
1286 }
1287
1288 /**
1289 * Sets whether any notifications from the app, represented by the given {@code pkgName} and
1290 * {@code uid}, have their importance locked by the user. Locked notifications don't get
1291 * considered for sentiment adjustments (and thus never show a blocking helper).
1292 */
1293 public void setAppImportanceLocked(String packageName, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001294 synchronized (mPackagePreferences) {
1295 PackagePreferences prefs = getOrCreatePackagePreferencesLocked(packageName, uid);
1296 if ((prefs.lockedAppFields & LockableAppFields.USER_LOCKED_IMPORTANCE) != 0) {
1297 return;
1298 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001299
Julia Reynolds5c399c62019-04-08 14:42:53 -04001300 prefs.lockedAppFields =
1301 prefs.lockedAppFields | LockableAppFields.USER_LOCKED_IMPORTANCE;
1302 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001303 updateConfig();
1304 }
1305
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001306 /**
1307 * Returns the delegate for a given package, if it's allowed by the package and the user.
1308 */
1309 public @Nullable String getNotificationDelegate(String sourcePkg, int sourceUid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001310 synchronized (mPackagePreferences) {
1311 PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001312
Julia Reynolds5c399c62019-04-08 14:42:53 -04001313 if (prefs == null || prefs.delegate == null) {
1314 return null;
1315 }
1316 if (!prefs.delegate.mUserAllowed || !prefs.delegate.mEnabled) {
1317 return null;
1318 }
1319 return prefs.delegate.mPkg;
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001320 }
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001321 }
1322
1323 /**
1324 * Used by an app to delegate notification posting privileges to another apps.
1325 */
1326 public void setNotificationDelegate(String sourcePkg, int sourceUid,
1327 String delegatePkg, int delegateUid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001328 synchronized (mPackagePreferences) {
1329 PackagePreferences prefs = getOrCreatePackagePreferencesLocked(sourcePkg, sourceUid);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001330
Julia Reynolds5c399c62019-04-08 14:42:53 -04001331 boolean userAllowed = prefs.delegate == null || prefs.delegate.mUserAllowed;
1332 Delegate delegate = new Delegate(delegatePkg, delegateUid, true, userAllowed);
1333 prefs.delegate = delegate;
1334 }
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001335 updateConfig();
1336 }
1337
1338 /**
1339 * Used by an app to turn off its notification delegate.
1340 */
1341 public void revokeNotificationDelegate(String sourcePkg, int sourceUid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001342 boolean changed = false;
1343 synchronized (mPackagePreferences) {
1344 PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid);
1345 if (prefs != null && prefs.delegate != null) {
1346 prefs.delegate.mEnabled = false;
1347 changed = true;
1348 }
1349 }
1350 if (changed) {
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001351 updateConfig();
1352 }
1353 }
1354
1355 /**
1356 * Toggles whether an app can have a notification delegate on behalf of a user.
1357 */
1358 public void toggleNotificationDelegate(String sourcePkg, int sourceUid, boolean userAllowed) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001359 boolean changed = false;
1360 synchronized (mPackagePreferences) {
1361 PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid);
1362 if (prefs != null && prefs.delegate != null) {
1363 prefs.delegate.mUserAllowed = userAllowed;
1364 changed = true;
1365 }
1366 }
1367 if (changed) {
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001368 updateConfig();
1369 }
1370 }
1371
1372 /**
1373 * Returns whether the given app is allowed on post notifications on behalf of the other given
1374 * app.
1375 */
1376 public boolean isDelegateAllowed(String sourcePkg, int sourceUid,
1377 String potentialDelegatePkg, int potentialDelegateUid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001378 synchronized (mPackagePreferences) {
1379 PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001380
Julia Reynolds5c399c62019-04-08 14:42:53 -04001381 return prefs != null && prefs.isValidDelegate(potentialDelegatePkg,
1382 potentialDelegateUid);
1383 }
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001384 }
1385
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001386 @VisibleForTesting
Julia Reynolds5c399c62019-04-08 14:42:53 -04001387 void lockFieldsForUpdateLocked(NotificationChannel original, NotificationChannel update) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001388 if (original.canBypassDnd() != update.canBypassDnd()) {
1389 update.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
1390 }
1391 if (original.getLockscreenVisibility() != update.getLockscreenVisibility()) {
1392 update.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY);
1393 }
1394 if (original.getImportance() != update.getImportance()) {
1395 update.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
1396 }
1397 if (original.shouldShowLights() != update.shouldShowLights()
1398 || original.getLightColor() != update.getLightColor()) {
1399 update.lockFields(NotificationChannel.USER_LOCKED_LIGHTS);
1400 }
1401 if (!Objects.equals(original.getSound(), update.getSound())) {
1402 update.lockFields(NotificationChannel.USER_LOCKED_SOUND);
1403 }
1404 if (!Arrays.equals(original.getVibrationPattern(), update.getVibrationPattern())
1405 || original.shouldVibrate() != update.shouldVibrate()) {
1406 update.lockFields(NotificationChannel.USER_LOCKED_VIBRATION);
1407 }
1408 if (original.canShowBadge() != update.canShowBadge()) {
1409 update.lockFields(NotificationChannel.USER_LOCKED_SHOW_BADGE);
1410 }
Julia Reynolds4509ce72019-01-31 13:12:43 -05001411 if (original.canBubble() != update.canBubble()) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001412 update.lockFields(NotificationChannel.USER_LOCKED_ALLOW_BUBBLE);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001413 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001414 }
1415
1416 public void dump(PrintWriter pw, String prefix,
1417 @NonNull NotificationManagerService.DumpFilter filter) {
1418 pw.print(prefix);
1419 pw.println("per-package config:");
1420
Julia Reynolds5c399c62019-04-08 14:42:53 -04001421 pw.println("PackagePreferences:");
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001422 synchronized (mPackagePreferences) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001423 dumpPackagePreferencesLocked(pw, prefix, filter, mPackagePreferences);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001424 }
1425 pw.println("Restored without uid:");
Julia Reynolds5c399c62019-04-08 14:42:53 -04001426 dumpPackagePreferencesLocked(pw, prefix, filter, mRestoredWithoutUids);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001427 }
1428
1429 public void dump(ProtoOutputStream proto,
1430 @NonNull NotificationManagerService.DumpFilter filter) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001431 synchronized (mPackagePreferences) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001432 dumpPackagePreferencesLocked(proto, RankingHelperProto.RECORDS, filter,
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001433 mPackagePreferences);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001434 }
Julia Reynolds5c399c62019-04-08 14:42:53 -04001435 dumpPackagePreferencesLocked(proto, RankingHelperProto.RECORDS_RESTORED_WITHOUT_UID, filter,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001436 mRestoredWithoutUids);
1437 }
1438
Julia Reynolds5c399c62019-04-08 14:42:53 -04001439 private static void dumpPackagePreferencesLocked(PrintWriter pw, String prefix,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001440 @NonNull NotificationManagerService.DumpFilter filter,
Julia Reynolds5c399c62019-04-08 14:42:53 -04001441 ArrayMap<String, PackagePreferences> packagePreferences) {
1442 final int N = packagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001443 for (int i = 0; i < N; i++) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001444 final PackagePreferences r = packagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001445 if (filter.matches(r.pkg)) {
1446 pw.print(prefix);
1447 pw.print(" AppSettings: ");
1448 pw.print(r.pkg);
1449 pw.print(" (");
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001450 pw.print(r.uid == UNKNOWN_UID ? "UNKNOWN_UID" : Integer.toString(r.uid));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001451 pw.print(')');
1452 if (r.importance != DEFAULT_IMPORTANCE) {
1453 pw.print(" importance=");
1454 pw.print(NotificationListenerService.Ranking.importanceToString(r.importance));
1455 }
1456 if (r.priority != DEFAULT_PRIORITY) {
1457 pw.print(" priority=");
1458 pw.print(Notification.priorityToString(r.priority));
1459 }
1460 if (r.visibility != DEFAULT_VISIBILITY) {
1461 pw.print(" visibility=");
1462 pw.print(Notification.visibilityToString(r.visibility));
1463 }
Julia Reynoldse7ca31b2019-04-25 15:41:47 -04001464 if (r.showBadge != DEFAULT_SHOW_BADGE) {
1465 pw.print(" showBadge=");
1466 pw.print(r.showBadge);
1467 }
1468 if (r.defaultAppLockedImportance != DEFAULT_APP_LOCKED_IMPORTANCE) {
1469 pw.print(" defaultAppLocked=");
1470 pw.print(r.defaultAppLockedImportance);
1471 }
1472 if (r.oemLockedImportance != DEFAULT_OEM_LOCKED_IMPORTANCE) {
1473 pw.print(" oemLocked=");
1474 pw.print(r.oemLockedImportance);
1475 }
1476 if (!r.futureOemLockedChannels.isEmpty()) {
1477 pw.print(" futureLockedChannels=");
1478 pw.print(r.futureOemLockedChannels);
1479 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001480 pw.println();
1481 for (NotificationChannel channel : r.channels.values()) {
1482 pw.print(prefix);
1483 channel.dump(pw, " ", filter.redact);
1484 }
1485 for (NotificationChannelGroup group : r.groups.values()) {
1486 pw.print(prefix);
1487 pw.print(" ");
1488 pw.print(" ");
1489 pw.println(group);
1490 }
1491 }
1492 }
1493 }
1494
Julia Reynolds5c399c62019-04-08 14:42:53 -04001495 private static void dumpPackagePreferencesLocked(ProtoOutputStream proto, long fieldId,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001496 @NonNull NotificationManagerService.DumpFilter filter,
Julia Reynolds5c399c62019-04-08 14:42:53 -04001497 ArrayMap<String, PackagePreferences> packagePreferences) {
1498 final int N = packagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001499 long fToken;
1500 for (int i = 0; i < N; i++) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001501 final PackagePreferences r = packagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001502 if (filter.matches(r.pkg)) {
1503 fToken = proto.start(fieldId);
1504
1505 proto.write(RankingHelperProto.RecordProto.PACKAGE, r.pkg);
1506 proto.write(RankingHelperProto.RecordProto.UID, r.uid);
1507 proto.write(RankingHelperProto.RecordProto.IMPORTANCE, r.importance);
1508 proto.write(RankingHelperProto.RecordProto.PRIORITY, r.priority);
1509 proto.write(RankingHelperProto.RecordProto.VISIBILITY, r.visibility);
1510 proto.write(RankingHelperProto.RecordProto.SHOW_BADGE, r.showBadge);
1511
1512 for (NotificationChannel channel : r.channels.values()) {
1513 channel.writeToProto(proto, RankingHelperProto.RecordProto.CHANNELS);
1514 }
1515 for (NotificationChannelGroup group : r.groups.values()) {
1516 group.writeToProto(proto, RankingHelperProto.RecordProto.CHANNEL_GROUPS);
1517 }
1518
1519 proto.end(fToken);
1520 }
1521 }
1522 }
1523
1524 public JSONObject dumpJson(NotificationManagerService.DumpFilter filter) {
1525 JSONObject ranking = new JSONObject();
1526 JSONArray PackagePreferencess = new JSONArray();
1527 try {
1528 ranking.put("noUid", mRestoredWithoutUids.size());
1529 } catch (JSONException e) {
1530 // pass
1531 }
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001532 synchronized (mPackagePreferences) {
1533 final int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001534 for (int i = 0; i < N; i++) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001535 final PackagePreferences r = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001536 if (filter == null || filter.matches(r.pkg)) {
1537 JSONObject PackagePreferences = new JSONObject();
1538 try {
1539 PackagePreferences.put("userId", UserHandle.getUserId(r.uid));
1540 PackagePreferences.put("packageName", r.pkg);
1541 if (r.importance != DEFAULT_IMPORTANCE) {
1542 PackagePreferences.put("importance",
1543 NotificationListenerService.Ranking.importanceToString(
1544 r.importance));
1545 }
1546 if (r.priority != DEFAULT_PRIORITY) {
1547 PackagePreferences.put("priority",
1548 Notification.priorityToString(r.priority));
1549 }
1550 if (r.visibility != DEFAULT_VISIBILITY) {
1551 PackagePreferences.put("visibility",
1552 Notification.visibilityToString(r.visibility));
1553 }
1554 if (r.showBadge != DEFAULT_SHOW_BADGE) {
1555 PackagePreferences.put("showBadge", Boolean.valueOf(r.showBadge));
1556 }
1557 JSONArray channels = new JSONArray();
1558 for (NotificationChannel channel : r.channels.values()) {
1559 channels.put(channel.toJson());
1560 }
1561 PackagePreferences.put("channels", channels);
1562 JSONArray groups = new JSONArray();
1563 for (NotificationChannelGroup group : r.groups.values()) {
1564 groups.put(group.toJson());
1565 }
1566 PackagePreferences.put("groups", groups);
1567 } catch (JSONException e) {
1568 // pass
1569 }
1570 PackagePreferencess.put(PackagePreferences);
1571 }
1572 }
1573 }
1574 try {
1575 ranking.put("PackagePreferencess", PackagePreferencess);
1576 } catch (JSONException e) {
1577 // pass
1578 }
1579 return ranking;
1580 }
1581
1582 /**
1583 * Dump only the ban information as structured JSON for the stats collector.
1584 *
1585 * This is intentionally redundant with {#link dumpJson} because the old
1586 * scraper will expect this format.
1587 *
1588 * @param filter
1589 * @return
1590 */
1591 public JSONArray dumpBansJson(NotificationManagerService.DumpFilter filter) {
1592 JSONArray bans = new JSONArray();
1593 Map<Integer, String> packageBans = getPackageBans();
1594 for (Map.Entry<Integer, String> ban : packageBans.entrySet()) {
1595 final int userId = UserHandle.getUserId(ban.getKey());
1596 final String packageName = ban.getValue();
1597 if (filter == null || filter.matches(packageName)) {
1598 JSONObject banJson = new JSONObject();
1599 try {
1600 banJson.put("userId", userId);
1601 banJson.put("packageName", packageName);
1602 } catch (JSONException e) {
1603 e.printStackTrace();
1604 }
1605 bans.put(banJson);
1606 }
1607 }
1608 return bans;
1609 }
1610
1611 public Map<Integer, String> getPackageBans() {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001612 synchronized (mPackagePreferences) {
1613 final int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001614 ArrayMap<Integer, String> packageBans = new ArrayMap<>(N);
1615 for (int i = 0; i < N; i++) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001616 final PackagePreferences r = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001617 if (r.importance == IMPORTANCE_NONE) {
1618 packageBans.put(r.uid, r.pkg);
1619 }
1620 }
1621
1622 return packageBans;
1623 }
1624 }
1625
1626 /**
1627 * Dump only the channel information as structured JSON for the stats collector.
1628 *
1629 * This is intentionally redundant with {#link dumpJson} because the old
1630 * scraper will expect this format.
1631 *
1632 * @param filter
1633 * @return
1634 */
1635 public JSONArray dumpChannelsJson(NotificationManagerService.DumpFilter filter) {
1636 JSONArray channels = new JSONArray();
1637 Map<String, Integer> packageChannels = getPackageChannels();
1638 for (Map.Entry<String, Integer> channelCount : packageChannels.entrySet()) {
1639 final String packageName = channelCount.getKey();
1640 if (filter == null || filter.matches(packageName)) {
1641 JSONObject channelCountJson = new JSONObject();
1642 try {
1643 channelCountJson.put("packageName", packageName);
1644 channelCountJson.put("channelCount", channelCount.getValue());
1645 } catch (JSONException e) {
1646 e.printStackTrace();
1647 }
1648 channels.put(channelCountJson);
1649 }
1650 }
1651 return channels;
1652 }
1653
1654 private Map<String, Integer> getPackageChannels() {
1655 ArrayMap<String, Integer> packageChannels = new ArrayMap<>();
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001656 synchronized (mPackagePreferences) {
1657 for (int i = 0; i < mPackagePreferences.size(); i++) {
1658 final PackagePreferences r = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001659 int channelCount = 0;
1660 for (int j = 0; j < r.channels.size(); j++) {
1661 if (!r.channels.valueAt(j).isDeleted()) {
1662 channelCount++;
1663 }
1664 }
1665 packageChannels.put(r.pkg, channelCount);
1666 }
1667 }
1668 return packageChannels;
1669 }
1670
Beverly0479cde22018-11-09 11:05:34 -05001671 /**
1672 * Called when user switches
1673 */
1674 public void onUserSwitched(int userId) {
1675 syncChannelsBypassingDnd(userId);
1676 }
1677
1678 /**
1679 * Called when user is unlocked
1680 */
1681 public void onUserUnlocked(int userId) {
1682 syncChannelsBypassingDnd(userId);
1683 }
1684
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001685 public void onUserRemoved(int userId) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001686 synchronized (mPackagePreferences) {
1687 int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001688 for (int i = N - 1; i >= 0; i--) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001689 PackagePreferences PackagePreferences = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001690 if (UserHandle.getUserId(PackagePreferences.uid) == userId) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001691 mPackagePreferences.removeAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001692 }
1693 }
1694 }
1695 }
1696
1697 protected void onLocaleChanged(Context context, int userId) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001698 synchronized (mPackagePreferences) {
1699 int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001700 for (int i = 0; i < N; i++) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001701 PackagePreferences PackagePreferences = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001702 if (UserHandle.getUserId(PackagePreferences.uid) == userId) {
1703 if (PackagePreferences.channels.containsKey(
1704 NotificationChannel.DEFAULT_CHANNEL_ID)) {
1705 PackagePreferences.channels.get(
1706 NotificationChannel.DEFAULT_CHANNEL_ID).setName(
1707 context.getResources().getString(
1708 R.string.default_notification_channel_label));
1709 }
1710 }
1711 }
1712 }
1713 }
1714
Julia Reynolds996c7c12019-05-24 10:25:33 -04001715 public boolean onPackagesChanged(boolean removingPackage, int changeUserId, String[] pkgList,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001716 int[] uidList) {
1717 if (pkgList == null || pkgList.length == 0) {
Julia Reynolds996c7c12019-05-24 10:25:33 -04001718 return false; // nothing to do
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001719 }
1720 boolean updated = false;
1721 if (removingPackage) {
1722 // Remove notification settings for uninstalled package
1723 int size = Math.min(pkgList.length, uidList.length);
1724 for (int i = 0; i < size; i++) {
1725 final String pkg = pkgList[i];
1726 final int uid = uidList[i];
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001727 synchronized (mPackagePreferences) {
1728 mPackagePreferences.remove(packagePreferencesKey(pkg, uid));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001729 }
1730 mRestoredWithoutUids.remove(pkg);
1731 updated = true;
1732 }
1733 } else {
1734 for (String pkg : pkgList) {
1735 // Package install
1736 final PackagePreferences r = mRestoredWithoutUids.get(pkg);
1737 if (r != null) {
1738 try {
1739 r.uid = mPm.getPackageUidAsUser(r.pkg, changeUserId);
1740 mRestoredWithoutUids.remove(pkg);
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001741 synchronized (mPackagePreferences) {
1742 mPackagePreferences.put(packagePreferencesKey(r.pkg, r.uid), r);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001743 }
1744 updated = true;
1745 } catch (PackageManager.NameNotFoundException e) {
1746 // noop
1747 }
1748 }
1749 // Package upgrade
1750 try {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001751 synchronized (mPackagePreferences) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001752 PackagePreferences fullPackagePreferences = getPackagePreferencesLocked(pkg,
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001753 mPm.getPackageUidAsUser(pkg, changeUserId));
1754 if (fullPackagePreferences != null) {
Julia Reynolds996c7c12019-05-24 10:25:33 -04001755 updated |= createDefaultChannelIfNeededLocked(fullPackagePreferences);
1756 updated |= deleteDefaultChannelIfNeededLocked(fullPackagePreferences);
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001757 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001758 }
1759 } catch (PackageManager.NameNotFoundException e) {
1760 }
1761 }
1762 }
1763
1764 if (updated) {
1765 updateConfig();
1766 }
Julia Reynolds996c7c12019-05-24 10:25:33 -04001767 return updated;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001768 }
1769
Julia Reynolds7af51c52019-04-19 11:08:27 -04001770 public void clearData(String pkg, int uid) {
1771 synchronized (mPackagePreferences) {
1772 PackagePreferences p = getPackagePreferencesLocked(pkg, uid);
1773 if (p != null) {
1774 p.channels = new ArrayMap<>();
1775 p.groups = new ArrayMap<>();
1776 p.delegate = null;
1777 p.lockedAppFields = DEFAULT_LOCKED_APP_FIELDS;
1778 p.allowBubble = DEFAULT_ALLOW_BUBBLE;
1779 p.importance = DEFAULT_IMPORTANCE;
1780 p.priority = DEFAULT_PRIORITY;
1781 p.visibility = DEFAULT_VISIBILITY;
1782 p.showBadge = DEFAULT_SHOW_BADGE;
1783 }
1784 }
1785 }
1786
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001787 private LogMaker getChannelLog(NotificationChannel channel, String pkg) {
1788 return new LogMaker(
1789 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1790 .ACTION_NOTIFICATION_CHANNEL)
1791 .setType(com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_UPDATE)
1792 .setPackageName(pkg)
1793 .addTaggedData(
1794 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1795 .FIELD_NOTIFICATION_CHANNEL_ID,
1796 channel.getId())
1797 .addTaggedData(
1798 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1799 .FIELD_NOTIFICATION_CHANNEL_IMPORTANCE,
1800 channel.getImportance());
1801 }
1802
1803 private LogMaker getChannelGroupLog(String groupId, String pkg) {
1804 return new LogMaker(
1805 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1806 .ACTION_NOTIFICATION_CHANNEL_GROUP)
1807 .setType(com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_UPDATE)
1808 .addTaggedData(
1809 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1810 .FIELD_NOTIFICATION_CHANNEL_GROUP_ID,
1811 groupId)
1812 .setPackageName(pkg);
1813 }
1814
Julia Reynolds4509ce72019-01-31 13:12:43 -05001815 public void updateBubblesEnabled() {
1816 if (mBubblesEnabled == null) {
1817 mBubblesEnabled = new SparseBooleanArray();
1818 }
1819 boolean changed = false;
1820 // update the cached values
1821 for (int index = 0; index < mBubblesEnabled.size(); index++) {
1822 int userId = mBubblesEnabled.keyAt(index);
1823 final boolean oldValue = mBubblesEnabled.get(userId);
1824 final boolean newValue = Settings.Secure.getIntForUser(mContext.getContentResolver(),
1825 Settings.Secure.NOTIFICATION_BUBBLES,
1826 DEFAULT_ALLOW_BUBBLE ? 1 : 0, userId) != 0;
1827 mBubblesEnabled.put(userId, newValue);
1828 changed |= oldValue != newValue;
1829 }
1830 if (changed) {
1831 updateConfig();
1832 }
1833 }
1834
1835 public boolean bubblesEnabled(UserHandle userHandle) {
1836 int userId = userHandle.getIdentifier();
1837 if (userId == UserHandle.USER_ALL) {
1838 return false;
1839 }
1840 if (mBubblesEnabled.indexOfKey(userId) < 0) {
1841 mBubblesEnabled.put(userId,
1842 Settings.Secure.getIntForUser(mContext.getContentResolver(),
1843 Settings.Secure.NOTIFICATION_BUBBLES,
1844 DEFAULT_ALLOW_BUBBLE ? 1 : 0, userId) != 0);
1845 }
1846 return mBubblesEnabled.get(userId, DEFAULT_ALLOW_BUBBLE);
1847 }
1848
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001849
1850 public void updateBadgingEnabled() {
1851 if (mBadgingEnabled == null) {
1852 mBadgingEnabled = new SparseBooleanArray();
1853 }
1854 boolean changed = false;
1855 // update the cached values
1856 for (int index = 0; index < mBadgingEnabled.size(); index++) {
1857 int userId = mBadgingEnabled.keyAt(index);
1858 final boolean oldValue = mBadgingEnabled.get(userId);
1859 final boolean newValue = Settings.Secure.getIntForUser(mContext.getContentResolver(),
1860 Settings.Secure.NOTIFICATION_BADGING,
1861 DEFAULT_SHOW_BADGE ? 1 : 0, userId) != 0;
1862 mBadgingEnabled.put(userId, newValue);
1863 changed |= oldValue != newValue;
1864 }
1865 if (changed) {
1866 updateConfig();
1867 }
1868 }
1869
1870 public boolean badgingEnabled(UserHandle userHandle) {
1871 int userId = userHandle.getIdentifier();
1872 if (userId == UserHandle.USER_ALL) {
1873 return false;
1874 }
1875 if (mBadgingEnabled.indexOfKey(userId) < 0) {
1876 mBadgingEnabled.put(userId,
1877 Settings.Secure.getIntForUser(mContext.getContentResolver(),
1878 Settings.Secure.NOTIFICATION_BADGING,
1879 DEFAULT_SHOW_BADGE ? 1 : 0, userId) != 0);
1880 }
1881 return mBadgingEnabled.get(userId, DEFAULT_SHOW_BADGE);
1882 }
1883
1884 private void updateConfig() {
1885 mRankingHandler.requestSort();
1886 }
1887
1888 private static String packagePreferencesKey(String pkg, int uid) {
1889 return pkg + "|" + uid;
1890 }
1891
1892 private static class PackagePreferences {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001893 String pkg;
1894 int uid = UNKNOWN_UID;
1895 int importance = DEFAULT_IMPORTANCE;
1896 int priority = DEFAULT_PRIORITY;
1897 int visibility = DEFAULT_VISIBILITY;
1898 boolean showBadge = DEFAULT_SHOW_BADGE;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001899 boolean allowBubble = DEFAULT_ALLOW_BUBBLE;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001900 int lockedAppFields = DEFAULT_LOCKED_APP_FIELDS;
Julia Reynolds0c245002019-03-27 16:10:11 -04001901 // these fields are loaded on boot from a different source of truth and so are not
1902 // written to notification policy xml
Julia Reynolds413ba842019-01-11 10:38:08 -05001903 boolean oemLockedImportance = DEFAULT_OEM_LOCKED_IMPORTANCE;
1904 List<String> futureOemLockedChannels = new ArrayList<>();
Julia Reynolds0c245002019-03-27 16:10:11 -04001905 boolean defaultAppLockedImportance = DEFAULT_APP_LOCKED_IMPORTANCE;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001906
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001907 Delegate delegate = null;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001908 ArrayMap<String, NotificationChannel> channels = new ArrayMap<>();
1909 Map<String, NotificationChannelGroup> groups = new ConcurrentHashMap<>();
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001910
1911 public boolean isValidDelegate(String pkg, int uid) {
1912 return delegate != null && delegate.isAllowed(pkg, uid);
1913 }
1914 }
1915
1916 private static class Delegate {
1917 static final boolean DEFAULT_ENABLED = true;
1918 static final boolean DEFAULT_USER_ALLOWED = true;
1919 String mPkg;
1920 int mUid = UNKNOWN_UID;
1921 boolean mEnabled = DEFAULT_ENABLED;
1922 boolean mUserAllowed = DEFAULT_USER_ALLOWED;
1923
1924 Delegate(String pkg, int uid, boolean enabled, boolean userAllowed) {
1925 mPkg = pkg;
1926 mUid = uid;
1927 mEnabled = enabled;
1928 mUserAllowed = userAllowed;
1929 }
1930
1931 public boolean isAllowed(String pkg, int uid) {
1932 if (pkg == null || uid == UNKNOWN_UID) {
1933 return false;
1934 }
1935 return pkg.equals(mPkg)
1936 && uid == mUid
1937 && (mUserAllowed && mEnabled);
1938 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001939 }
1940}