blob: 9886d0aa6a1c14573f8ccfc64df219b2d4ffaf37 [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
613 public void createNotificationChannel(String pkg, int uid, NotificationChannel channel,
614 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 Reynolds5c399c62019-04-08 14:42:53 -0400619 synchronized (mPackagePreferences) {
620 PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
621 if (r == null) {
622 throw new IllegalArgumentException("Invalid package");
623 }
624 if (channel.getGroup() != null && !r.groups.containsKey(channel.getGroup())) {
625 throw new IllegalArgumentException("NotificationChannelGroup doesn't exist");
626 }
627 if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channel.getId())) {
628 throw new IllegalArgumentException("Reserved id");
629 }
630 NotificationChannel existing = r.channels.get(channel.getId());
631 // Keep most of the existing settings
632 if (existing != null && fromTargetApp) {
633 if (existing.isDeleted()) {
634 existing.setDeleted(false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400635
Julia Reynolds5c399c62019-04-08 14:42:53 -0400636 // log a resurrected channel as if it's new again
637 MetricsLogger.action(getChannelLog(channel, pkg).setType(
638 com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_OPEN));
639 }
640
641 existing.setName(channel.getName().toString());
642 existing.setDescription(channel.getDescription());
643 existing.setBlockableSystem(channel.isBlockableSystem());
644 if (existing.getGroup() == null) {
645 existing.setGroup(channel.getGroup());
646 }
647
648 // Apps are allowed to downgrade channel importance if the user has not changed any
649 // fields on this channel yet.
650 final int previousExistingImportance = existing.getImportance();
651 if (existing.getUserLockedFields() == 0 &&
652 channel.getImportance() < existing.getImportance()) {
653 existing.setImportance(channel.getImportance());
654 }
655
656 // system apps and dnd access apps can bypass dnd if the user hasn't changed any
657 // fields on the channel yet
658 if (existing.getUserLockedFields() == 0 && hasDndAccess) {
659 boolean bypassDnd = channel.canBypassDnd();
660 existing.setBypassDnd(bypassDnd);
661
662 if (bypassDnd != mAreChannelsBypassingDnd
663 || previousExistingImportance != existing.getImportance()) {
664 updateChannelsBypassingDnd(mContext.getUserId());
665 }
666 }
667
668 updateConfig();
669 return;
670 }
671 if (channel.getImportance() < IMPORTANCE_NONE
672 || channel.getImportance() > NotificationManager.IMPORTANCE_MAX) {
673 throw new IllegalArgumentException("Invalid importance level");
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400674 }
675
Julia Reynolds5c399c62019-04-08 14:42:53 -0400676 // Reset fields that apps aren't allowed to set.
677 if (fromTargetApp && !hasDndAccess) {
678 channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400679 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400680 if (fromTargetApp) {
681 channel.setLockscreenVisibility(r.visibility);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400682 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400683 clearLockedFieldsLocked(channel);
684 channel.setImportanceLockedByOEM(r.oemLockedImportance);
685 if (!channel.isImportanceLockedByOEM()) {
686 if (r.futureOemLockedChannels.remove(channel.getId())) {
687 channel.setImportanceLockedByOEM(true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400688 }
689 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400690 channel.setImportanceLockedByCriticalDeviceFunction(r.defaultAppLockedImportance);
691 if (channel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
692 channel.setLockscreenVisibility(
693 NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE);
Julia Reynolds413ba842019-01-11 10:38:08 -0500694 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400695 if (!r.showBadge) {
696 channel.setShowBadge(false);
697 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400698
Julia Reynolds5c399c62019-04-08 14:42:53 -0400699 r.channels.put(channel.getId(), channel);
700 if (channel.canBypassDnd() != mAreChannelsBypassingDnd) {
701 updateChannelsBypassingDnd(mContext.getUserId());
702 }
703 MetricsLogger.action(getChannelLog(channel, pkg).setType(
704 com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_OPEN));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400705 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400706 }
707
Julia Reynolds5c399c62019-04-08 14:42:53 -0400708 void clearLockedFieldsLocked(NotificationChannel channel) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400709 channel.unlockFields(channel.getUserLockedFields());
710 }
711
712 @Override
713 public void updateNotificationChannel(String pkg, int uid, NotificationChannel updatedChannel,
714 boolean fromUser) {
715 Preconditions.checkNotNull(updatedChannel);
716 Preconditions.checkNotNull(updatedChannel.getId());
Julia Reynolds5c399c62019-04-08 14:42:53 -0400717 synchronized (mPackagePreferences) {
718 PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
719 if (r == null) {
720 throw new IllegalArgumentException("Invalid package");
721 }
722 NotificationChannel channel = r.channels.get(updatedChannel.getId());
723 if (channel == null || channel.isDeleted()) {
724 throw new IllegalArgumentException("Channel does not exist");
725 }
726 if (updatedChannel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
727 updatedChannel.setLockscreenVisibility(
728 NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE);
729 }
730 if (fromUser) {
731 updatedChannel.lockFields(channel.getUserLockedFields());
732 lockFieldsForUpdateLocked(channel, updatedChannel);
733 } else {
734 updatedChannel.unlockFields(updatedChannel.getUserLockedFields());
735 }
736 // no importance updates are allowed if OEM blocked it
737 updatedChannel.setImportanceLockedByOEM(channel.isImportanceLockedByOEM());
738 if (updatedChannel.isImportanceLockedByOEM()) {
739 updatedChannel.setImportance(channel.getImportance());
740 }
741 updatedChannel.setImportanceLockedByCriticalDeviceFunction(
742 r.defaultAppLockedImportance);
Beverly47679222019-05-16 15:46:11 -0400743 if (updatedChannel.isImportanceLockedByCriticalDeviceFunction()
744 && updatedChannel.getImportance() == IMPORTANCE_NONE) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400745 updatedChannel.setImportance(channel.getImportance());
746 }
Julia Reynolds413ba842019-01-11 10:38:08 -0500747
Julia Reynolds5c399c62019-04-08 14:42:53 -0400748 r.channels.put(updatedChannel.getId(), updatedChannel);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400749
Julia Reynolds5c399c62019-04-08 14:42:53 -0400750 if (onlyHasDefaultChannel(pkg, uid)) {
751 // copy settings to app level so they are inherited by new channels
752 // when the app migrates
753 r.importance = updatedChannel.getImportance();
754 r.priority = updatedChannel.canBypassDnd()
755 ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT;
756 r.visibility = updatedChannel.getLockscreenVisibility();
757 r.showBadge = updatedChannel.canShowBadge();
758 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400759
Julia Reynolds5c399c62019-04-08 14:42:53 -0400760 if (!channel.equals(updatedChannel)) {
761 // only log if there are real changes
762 MetricsLogger.action(getChannelLog(updatedChannel, pkg)
763 .setSubtype(fromUser ? 1 : 0));
764 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400765
Julia Reynolds5c399c62019-04-08 14:42:53 -0400766 if (updatedChannel.canBypassDnd() != mAreChannelsBypassingDnd
767 || channel.getImportance() != updatedChannel.getImportance()) {
768 updateChannelsBypassingDnd(mContext.getUserId());
769 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400770 }
771 updateConfig();
772 }
773
774 @Override
775 public NotificationChannel getNotificationChannel(String pkg, int uid, String channelId,
776 boolean includeDeleted) {
777 Preconditions.checkNotNull(pkg);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400778 synchronized (mPackagePreferences) {
779 PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
780 if (r == null) {
781 return null;
782 }
783 if (channelId == null) {
784 channelId = NotificationChannel.DEFAULT_CHANNEL_ID;
785 }
786 final NotificationChannel nc = r.channels.get(channelId);
787 if (nc != null && (includeDeleted || !nc.isDeleted())) {
788 return nc;
789 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400790 return null;
791 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400792 }
793
794 @Override
795 public void deleteNotificationChannel(String pkg, int uid, String channelId) {
Julia Reynolds5c399c62019-04-08 14:42:53 -0400796 synchronized (mPackagePreferences) {
797 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
798 if (r == null) {
799 return;
800 }
801 NotificationChannel channel = r.channels.get(channelId);
802 if (channel != null) {
803 channel.setDeleted(true);
804 LogMaker lm = getChannelLog(channel, pkg);
805 lm.setType(com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_CLOSE);
806 MetricsLogger.action(lm);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400807
Julia Reynolds5c399c62019-04-08 14:42:53 -0400808 if (mAreChannelsBypassingDnd && channel.canBypassDnd()) {
809 updateChannelsBypassingDnd(mContext.getUserId());
810 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400811 }
812 }
813 }
814
815 @Override
816 @VisibleForTesting
817 public void permanentlyDeleteNotificationChannel(String pkg, int uid, String channelId) {
818 Preconditions.checkNotNull(pkg);
819 Preconditions.checkNotNull(channelId);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400820 synchronized (mPackagePreferences) {
821 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
822 if (r == null) {
823 return;
824 }
825 r.channels.remove(channelId);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400826 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400827 }
828
829 @Override
830 public void permanentlyDeleteNotificationChannels(String pkg, int uid) {
831 Preconditions.checkNotNull(pkg);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400832 synchronized (mPackagePreferences) {
833 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
834 if (r == null) {
835 return;
836 }
837 int N = r.channels.size() - 1;
838 for (int i = N; i >= 0; i--) {
839 String key = r.channels.keyAt(i);
840 if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(key)) {
841 r.channels.remove(key);
842 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400843 }
844 }
845 }
846
Julia Reynolds12ad7ca2019-01-28 09:29:16 -0500847 public boolean shouldHideSilentStatusIcons() {
848 return mHideSilentStatusBarIcons;
849 }
850
851 public void setHideSilentStatusIcons(boolean hide) {
852 mHideSilentStatusBarIcons = hide;
853 }
854
Julia Reynolds413ba842019-01-11 10:38:08 -0500855 public void lockChannelsForOEM(String[] appOrChannelList) {
856 if (appOrChannelList == null) {
857 return;
858 }
859 for (String appOrChannel : appOrChannelList) {
860 if (!TextUtils.isEmpty(appOrChannel)) {
861 String[] appSplit = appOrChannel.split(NON_BLOCKABLE_CHANNEL_DELIM);
862 if (appSplit != null && appSplit.length > 0) {
863 String appName = appSplit[0];
864 String channelId = appSplit.length == 2 ? appSplit[1] : null;
865
866 synchronized (mPackagePreferences) {
867 for (PackagePreferences r : mPackagePreferences.values()) {
868 if (r.pkg.equals(appName)) {
869 if (channelId == null) {
870 // lock all channels for the app
871 r.oemLockedImportance = true;
872 for (NotificationChannel channel : r.channels.values()) {
873 channel.setImportanceLockedByOEM(true);
874 }
875 } else {
876 NotificationChannel channel = r.channels.get(channelId);
877 if (channel != null) {
878 channel.setImportanceLockedByOEM(true);
879 } else {
880 // if this channel shows up in the future, make sure it'll
881 // be locked immediately
882 r.futureOemLockedChannels.add(channelId);
883 }
884 }
885 }
886 }
887 }
888 }
889 }
890 }
891 }
892
Julia Reynoldse7ca31b2019-04-25 15:41:47 -0400893 public void updateDefaultApps(int userId, ArraySet<String> toRemove,
894 ArraySet<Pair<String, Integer>> toAdd) {
Julia Reynolds0c245002019-03-27 16:10:11 -0400895 synchronized (mPackagePreferences) {
896 for (PackagePreferences p : mPackagePreferences.values()) {
897 if (userId == UserHandle.getUserId(p.uid)) {
898 if (toRemove != null && toRemove.contains(p.pkg)) {
899 p.defaultAppLockedImportance = false;
900 for (NotificationChannel channel : p.channels.values()) {
901 channel.setImportanceLockedByCriticalDeviceFunction(false);
902 }
Julia Reynoldse7ca31b2019-04-25 15:41:47 -0400903 }
904 }
905 }
906 if (toAdd != null) {
907 for (Pair<String, Integer> approvedApp : toAdd) {
908 PackagePreferences p = getOrCreatePackagePreferencesLocked(approvedApp.first,
909 approvedApp.second);
910 p.defaultAppLockedImportance = true;
911 for (NotificationChannel channel : p.channels.values()) {
912 channel.setImportanceLockedByCriticalDeviceFunction(true);
Julia Reynolds0c245002019-03-27 16:10:11 -0400913 }
914 }
915 }
916 }
917 }
918
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400919 public NotificationChannelGroup getNotificationChannelGroupWithChannels(String pkg,
920 int uid, String groupId, boolean includeDeleted) {
921 Preconditions.checkNotNull(pkg);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400922 synchronized (mPackagePreferences) {
923 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
924 if (r == null || groupId == null || !r.groups.containsKey(groupId)) {
925 return null;
926 }
927 NotificationChannelGroup group = r.groups.get(groupId).clone();
928 group.setChannels(new ArrayList<>());
929 int N = r.channels.size();
930 for (int i = 0; i < N; i++) {
931 final NotificationChannel nc = r.channels.valueAt(i);
932 if (includeDeleted || !nc.isDeleted()) {
933 if (groupId.equals(nc.getGroup())) {
934 group.addChannel(nc);
935 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400936 }
937 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400938 return group;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400939 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400940 }
941
942 public NotificationChannelGroup getNotificationChannelGroup(String groupId, String pkg,
943 int uid) {
944 Preconditions.checkNotNull(pkg);
Julia Reynolds5c399c62019-04-08 14:42:53 -0400945 synchronized (mPackagePreferences) {
946 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
947 if (r == null) {
948 return null;
949 }
950 return r.groups.get(groupId);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400951 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400952 }
953
954 @Override
955 public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups(String pkg,
Julia Reynolds13ed28b2018-09-21 15:20:13 -0400956 int uid, boolean includeDeleted, boolean includeNonGrouped, boolean includeEmpty) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400957 Preconditions.checkNotNull(pkg);
958 Map<String, NotificationChannelGroup> groups = new ArrayMap<>();
Julia Reynolds5c399c62019-04-08 14:42:53 -0400959 synchronized (mPackagePreferences) {
960 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
961 if (r == null) {
962 return ParceledListSlice.emptyList();
963 }
964 NotificationChannelGroup nonGrouped = new NotificationChannelGroup(null, null);
965 int N = r.channels.size();
966 for (int i = 0; i < N; i++) {
967 final NotificationChannel nc = r.channels.valueAt(i);
968 if (includeDeleted || !nc.isDeleted()) {
969 if (nc.getGroup() != null) {
970 if (r.groups.get(nc.getGroup()) != null) {
971 NotificationChannelGroup ncg = groups.get(nc.getGroup());
972 if (ncg == null) {
973 ncg = r.groups.get(nc.getGroup()).clone();
974 ncg.setChannels(new ArrayList<>());
975 groups.put(nc.getGroup(), ncg);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400976
Julia Reynolds5c399c62019-04-08 14:42:53 -0400977 }
978 ncg.addChannel(nc);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400979 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400980 } else {
981 nonGrouped.addChannel(nc);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400982 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400983 }
984 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400985 if (includeNonGrouped && nonGrouped.getChannels().size() > 0) {
986 groups.put(null, nonGrouped);
987 }
988 if (includeEmpty) {
989 for (NotificationChannelGroup group : r.groups.values()) {
990 if (!groups.containsKey(group.getId())) {
991 groups.put(group.getId(), group);
992 }
Julia Reynolds13ed28b2018-09-21 15:20:13 -0400993 }
994 }
Julia Reynolds5c399c62019-04-08 14:42:53 -0400995 return new ParceledListSlice<>(new ArrayList<>(groups.values()));
Julia Reynolds13ed28b2018-09-21 15:20:13 -0400996 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400997 }
998
999 public List<NotificationChannel> deleteNotificationChannelGroup(String pkg, int uid,
1000 String groupId) {
1001 List<NotificationChannel> deletedChannels = new ArrayList<>();
Julia Reynolds5c399c62019-04-08 14:42:53 -04001002 synchronized (mPackagePreferences) {
1003 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
1004 if (r == null || TextUtils.isEmpty(groupId)) {
1005 return deletedChannels;
1006 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001007
Julia Reynolds5c399c62019-04-08 14:42:53 -04001008 r.groups.remove(groupId);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001009
Julia Reynolds5c399c62019-04-08 14:42:53 -04001010 int N = r.channels.size();
1011 for (int i = 0; i < N; i++) {
1012 final NotificationChannel nc = r.channels.valueAt(i);
1013 if (groupId.equals(nc.getGroup())) {
1014 nc.setDeleted(true);
1015 deletedChannels.add(nc);
1016 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001017 }
1018 }
1019 return deletedChannels;
1020 }
1021
1022 @Override
1023 public Collection<NotificationChannelGroup> getNotificationChannelGroups(String pkg,
1024 int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001025 List<NotificationChannelGroup> groups = new ArrayList<>();
1026 synchronized (mPackagePreferences) {
1027 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
1028 if (r == null) {
1029 return groups;
1030 }
1031 groups.addAll(r.groups.values());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001032 }
Julia Reynolds5c399c62019-04-08 14:42:53 -04001033 return groups;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001034 }
1035
1036 @Override
1037 public ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg, int uid,
1038 boolean includeDeleted) {
1039 Preconditions.checkNotNull(pkg);
1040 List<NotificationChannel> channels = new ArrayList<>();
Julia Reynolds5c399c62019-04-08 14:42:53 -04001041 synchronized (mPackagePreferences) {
1042 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
1043 if (r == null) {
1044 return ParceledListSlice.emptyList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001045 }
Julia Reynolds5c399c62019-04-08 14:42:53 -04001046 int N = r.channels.size();
1047 for (int i = 0; i < N; i++) {
1048 final NotificationChannel nc = r.channels.valueAt(i);
1049 if (includeDeleted || !nc.isDeleted()) {
1050 channels.add(nc);
1051 }
1052 }
1053 return new ParceledListSlice<>(channels);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001054 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001055 }
1056
1057 /**
Beverly0479cde22018-11-09 11:05:34 -05001058 * Gets all notification channels associated with the given pkg and userId that can bypass dnd
1059 */
1060 public ParceledListSlice<NotificationChannel> getNotificationChannelsBypassingDnd(String pkg,
1061 int userId) {
1062 List<NotificationChannel> channels = new ArrayList<>();
1063 synchronized (mPackagePreferences) {
1064 final PackagePreferences r = mPackagePreferences.get(
1065 packagePreferencesKey(pkg, userId));
1066 // notifications from this package aren't blocked
1067 if (r != null && r.importance != IMPORTANCE_NONE) {
1068 for (NotificationChannel channel : r.channels.values()) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001069 if (channelIsLiveLocked(r, channel) && channel.canBypassDnd()) {
Beverly0479cde22018-11-09 11:05:34 -05001070 channels.add(channel);
1071 }
1072 }
1073 }
1074 }
1075 return new ParceledListSlice<>(channels);
1076 }
1077
1078 /**
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001079 * True for pre-O apps that only have the default channel, or pre O apps that have no
1080 * channels yet. This method will create the default channel for pre-O apps that don't have it.
1081 * Should never be true for O+ targeting apps, but that's enforced on boot/when an app
1082 * upgrades.
1083 */
1084 public boolean onlyHasDefaultChannel(String pkg, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001085 synchronized (mPackagePreferences) {
1086 PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
1087 if (r.channels.size() == 1
1088 && r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) {
1089 return true;
1090 }
1091 return false;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001092 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001093 }
1094
1095 public int getDeletedChannelCount(String pkg, int uid) {
1096 Preconditions.checkNotNull(pkg);
1097 int deletedCount = 0;
Julia Reynolds5c399c62019-04-08 14:42:53 -04001098 synchronized (mPackagePreferences) {
1099 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
1100 if (r == null) {
1101 return deletedCount;
1102 }
1103 int N = r.channels.size();
1104 for (int i = 0; i < N; i++) {
1105 final NotificationChannel nc = r.channels.valueAt(i);
1106 if (nc.isDeleted()) {
1107 deletedCount++;
1108 }
1109 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001110 return deletedCount;
1111 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001112 }
1113
1114 public int getBlockedChannelCount(String pkg, int uid) {
1115 Preconditions.checkNotNull(pkg);
1116 int blockedCount = 0;
Julia Reynolds5c399c62019-04-08 14:42:53 -04001117 synchronized (mPackagePreferences) {
1118 PackagePreferences r = getPackagePreferencesLocked(pkg, uid);
1119 if (r == null) {
1120 return blockedCount;
1121 }
1122 int N = r.channels.size();
1123 for (int i = 0; i < N; i++) {
1124 final NotificationChannel nc = r.channels.valueAt(i);
1125 if (!nc.isDeleted() && IMPORTANCE_NONE == nc.getImportance()) {
1126 blockedCount++;
1127 }
1128 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001129 return blockedCount;
1130 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001131 }
1132
1133 public int getBlockedAppCount(int userId) {
1134 int count = 0;
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001135 synchronized (mPackagePreferences) {
1136 final int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001137 for (int i = 0; i < N; i++) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001138 final PackagePreferences r = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001139 if (userId == UserHandle.getUserId(r.uid)
1140 && r.importance == IMPORTANCE_NONE) {
1141 count++;
1142 }
1143 }
1144 }
1145 return count;
1146 }
1147
Beverly0479cde22018-11-09 11:05:34 -05001148 /**
1149 * Returns the number of apps that have at least one notification channel that can bypass DND
1150 * for given particular user
1151 */
1152 public int getAppsBypassingDndCount(int userId) {
1153 int count = 0;
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001154 synchronized (mPackagePreferences) {
Beverly0479cde22018-11-09 11:05:34 -05001155 final int numPackagePreferences = mPackagePreferences.size();
1156 for (int i = 0; i < numPackagePreferences; i++) {
1157 final PackagePreferences r = mPackagePreferences.valueAt(i);
1158 // Package isn't associated with this userId or notifications from this package are
1159 // blocked
1160 if (userId != UserHandle.getUserId(r.uid) || r.importance == IMPORTANCE_NONE) {
1161 continue;
1162 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001163
Beverly0479cde22018-11-09 11:05:34 -05001164 for (NotificationChannel channel : r.channels.values()) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001165 if (channelIsLiveLocked(r, channel) && channel.canBypassDnd()) {
Beverly0479cde22018-11-09 11:05:34 -05001166 count++;
1167 break;
1168 }
1169 }
1170 }
1171 }
1172 return count;
1173 }
1174
1175 /**
1176 * Syncs {@link #mAreChannelsBypassingDnd} with the user's notification policy before
1177 * updating
1178 * @param userId
1179 */
1180 private void syncChannelsBypassingDnd(int userId) {
1181 mAreChannelsBypassingDnd = (mZenModeHelper.getNotificationPolicy().state
1182 & NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND) == 1;
1183 updateChannelsBypassingDnd(userId);
1184 }
1185
1186 /**
1187 * Updates the user's NotificationPolicy based on whether the given userId
1188 * has channels bypassing DND
1189 * @param userId
1190 */
1191 private void updateChannelsBypassingDnd(int userId) {
1192 synchronized (mPackagePreferences) {
1193 final int numPackagePreferences = mPackagePreferences.size();
1194 for (int i = 0; i < numPackagePreferences; i++) {
1195 final PackagePreferences r = mPackagePreferences.valueAt(i);
1196 // Package isn't associated with this userId or notifications from this package are
1197 // blocked
1198 if (userId != UserHandle.getUserId(r.uid) || r.importance == IMPORTANCE_NONE) {
1199 continue;
1200 }
1201
1202 for (NotificationChannel channel : r.channels.values()) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001203 if (channelIsLiveLocked(r, channel) && channel.canBypassDnd()) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001204 if (!mAreChannelsBypassingDnd) {
1205 mAreChannelsBypassingDnd = true;
1206 updateZenPolicy(true);
1207 }
1208 return;
1209 }
1210 }
1211 }
1212 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001213 // If no channels bypass DND, update the zen policy once to disable DND bypass.
1214 if (mAreChannelsBypassingDnd) {
1215 mAreChannelsBypassingDnd = false;
1216 updateZenPolicy(false);
1217 }
1218 }
1219
Julia Reynolds5c399c62019-04-08 14:42:53 -04001220 private boolean channelIsLiveLocked(PackagePreferences pkgPref, NotificationChannel channel) {
Beverly0479cde22018-11-09 11:05:34 -05001221 // Channel is in a group that's blocked
Beverly4f7b53d2018-11-20 09:56:31 -05001222 if (isGroupBlocked(pkgPref.pkg, pkgPref.uid, channel.getGroup())) {
1223 return false;
Beverly0479cde22018-11-09 11:05:34 -05001224 }
1225
1226 // Channel is deleted or is blocked
1227 if (channel.isDeleted() || channel.getImportance() == IMPORTANCE_NONE) {
1228 return false;
1229 }
1230
1231 return true;
1232 }
1233
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001234 public void updateZenPolicy(boolean areChannelsBypassingDnd) {
1235 NotificationManager.Policy policy = mZenModeHelper.getNotificationPolicy();
1236 mZenModeHelper.setNotificationPolicy(new NotificationManager.Policy(
1237 policy.priorityCategories, policy.priorityCallSenders,
1238 policy.priorityMessageSenders, policy.suppressedVisualEffects,
1239 (areChannelsBypassingDnd ? NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND
1240 : 0)));
1241 }
1242
1243 public boolean areChannelsBypassingDnd() {
1244 return mAreChannelsBypassingDnd;
1245 }
1246
1247 /**
1248 * Sets importance.
1249 */
1250 @Override
1251 public void setImportance(String pkgName, int uid, int importance) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001252 synchronized (mPackagePreferences) {
1253 getOrCreatePackagePreferencesLocked(pkgName, uid).importance = importance;
1254 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001255 updateConfig();
1256 }
1257
1258 public void setEnabled(String packageName, int uid, boolean enabled) {
1259 boolean wasEnabled = getImportance(packageName, uid) != IMPORTANCE_NONE;
1260 if (wasEnabled == enabled) {
1261 return;
1262 }
1263 setImportance(packageName, uid,
1264 enabled ? DEFAULT_IMPORTANCE : IMPORTANCE_NONE);
1265 }
1266
1267 /**
1268 * Sets whether any notifications from the app, represented by the given {@code pkgName} and
1269 * {@code uid}, have their importance locked by the user. Locked notifications don't get
1270 * considered for sentiment adjustments (and thus never show a blocking helper).
1271 */
1272 public void setAppImportanceLocked(String packageName, int uid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001273 synchronized (mPackagePreferences) {
1274 PackagePreferences prefs = getOrCreatePackagePreferencesLocked(packageName, uid);
1275 if ((prefs.lockedAppFields & LockableAppFields.USER_LOCKED_IMPORTANCE) != 0) {
1276 return;
1277 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001278
Julia Reynolds5c399c62019-04-08 14:42:53 -04001279 prefs.lockedAppFields =
1280 prefs.lockedAppFields | LockableAppFields.USER_LOCKED_IMPORTANCE;
1281 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001282 updateConfig();
1283 }
1284
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001285 /**
1286 * Returns the delegate for a given package, if it's allowed by the package and the user.
1287 */
1288 public @Nullable String getNotificationDelegate(String sourcePkg, int sourceUid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001289 synchronized (mPackagePreferences) {
1290 PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001291
Julia Reynolds5c399c62019-04-08 14:42:53 -04001292 if (prefs == null || prefs.delegate == null) {
1293 return null;
1294 }
1295 if (!prefs.delegate.mUserAllowed || !prefs.delegate.mEnabled) {
1296 return null;
1297 }
1298 return prefs.delegate.mPkg;
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001299 }
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001300 }
1301
1302 /**
1303 * Used by an app to delegate notification posting privileges to another apps.
1304 */
1305 public void setNotificationDelegate(String sourcePkg, int sourceUid,
1306 String delegatePkg, int delegateUid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001307 synchronized (mPackagePreferences) {
1308 PackagePreferences prefs = getOrCreatePackagePreferencesLocked(sourcePkg, sourceUid);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001309
Julia Reynolds5c399c62019-04-08 14:42:53 -04001310 boolean userAllowed = prefs.delegate == null || prefs.delegate.mUserAllowed;
1311 Delegate delegate = new Delegate(delegatePkg, delegateUid, true, userAllowed);
1312 prefs.delegate = delegate;
1313 }
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001314 updateConfig();
1315 }
1316
1317 /**
1318 * Used by an app to turn off its notification delegate.
1319 */
1320 public void revokeNotificationDelegate(String sourcePkg, int sourceUid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001321 boolean changed = false;
1322 synchronized (mPackagePreferences) {
1323 PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid);
1324 if (prefs != null && prefs.delegate != null) {
1325 prefs.delegate.mEnabled = false;
1326 changed = true;
1327 }
1328 }
1329 if (changed) {
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001330 updateConfig();
1331 }
1332 }
1333
1334 /**
1335 * Toggles whether an app can have a notification delegate on behalf of a user.
1336 */
1337 public void toggleNotificationDelegate(String sourcePkg, int sourceUid, boolean userAllowed) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001338 boolean changed = false;
1339 synchronized (mPackagePreferences) {
1340 PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid);
1341 if (prefs != null && prefs.delegate != null) {
1342 prefs.delegate.mUserAllowed = userAllowed;
1343 changed = true;
1344 }
1345 }
1346 if (changed) {
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001347 updateConfig();
1348 }
1349 }
1350
1351 /**
1352 * Returns whether the given app is allowed on post notifications on behalf of the other given
1353 * app.
1354 */
1355 public boolean isDelegateAllowed(String sourcePkg, int sourceUid,
1356 String potentialDelegatePkg, int potentialDelegateUid) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001357 synchronized (mPackagePreferences) {
1358 PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001359
Julia Reynolds5c399c62019-04-08 14:42:53 -04001360 return prefs != null && prefs.isValidDelegate(potentialDelegatePkg,
1361 potentialDelegateUid);
1362 }
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001363 }
1364
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001365 @VisibleForTesting
Julia Reynolds5c399c62019-04-08 14:42:53 -04001366 void lockFieldsForUpdateLocked(NotificationChannel original, NotificationChannel update) {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001367 if (original.canBypassDnd() != update.canBypassDnd()) {
1368 update.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
1369 }
1370 if (original.getLockscreenVisibility() != update.getLockscreenVisibility()) {
1371 update.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY);
1372 }
1373 if (original.getImportance() != update.getImportance()) {
1374 update.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
1375 }
1376 if (original.shouldShowLights() != update.shouldShowLights()
1377 || original.getLightColor() != update.getLightColor()) {
1378 update.lockFields(NotificationChannel.USER_LOCKED_LIGHTS);
1379 }
1380 if (!Objects.equals(original.getSound(), update.getSound())) {
1381 update.lockFields(NotificationChannel.USER_LOCKED_SOUND);
1382 }
1383 if (!Arrays.equals(original.getVibrationPattern(), update.getVibrationPattern())
1384 || original.shouldVibrate() != update.shouldVibrate()) {
1385 update.lockFields(NotificationChannel.USER_LOCKED_VIBRATION);
1386 }
1387 if (original.canShowBadge() != update.canShowBadge()) {
1388 update.lockFields(NotificationChannel.USER_LOCKED_SHOW_BADGE);
1389 }
Julia Reynolds4509ce72019-01-31 13:12:43 -05001390 if (original.canBubble() != update.canBubble()) {
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001391 update.lockFields(NotificationChannel.USER_LOCKED_ALLOW_BUBBLE);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001392 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001393 }
1394
1395 public void dump(PrintWriter pw, String prefix,
1396 @NonNull NotificationManagerService.DumpFilter filter) {
1397 pw.print(prefix);
1398 pw.println("per-package config:");
1399
Julia Reynolds5c399c62019-04-08 14:42:53 -04001400 pw.println("PackagePreferences:");
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001401 synchronized (mPackagePreferences) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001402 dumpPackagePreferencesLocked(pw, prefix, filter, mPackagePreferences);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001403 }
1404 pw.println("Restored without uid:");
Julia Reynolds5c399c62019-04-08 14:42:53 -04001405 dumpPackagePreferencesLocked(pw, prefix, filter, mRestoredWithoutUids);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001406 }
1407
1408 public void dump(ProtoOutputStream proto,
1409 @NonNull NotificationManagerService.DumpFilter filter) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001410 synchronized (mPackagePreferences) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001411 dumpPackagePreferencesLocked(proto, RankingHelperProto.RECORDS, filter,
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001412 mPackagePreferences);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001413 }
Julia Reynolds5c399c62019-04-08 14:42:53 -04001414 dumpPackagePreferencesLocked(proto, RankingHelperProto.RECORDS_RESTORED_WITHOUT_UID, filter,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001415 mRestoredWithoutUids);
1416 }
1417
Julia Reynolds5c399c62019-04-08 14:42:53 -04001418 private static void dumpPackagePreferencesLocked(PrintWriter pw, String prefix,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001419 @NonNull NotificationManagerService.DumpFilter filter,
Julia Reynolds5c399c62019-04-08 14:42:53 -04001420 ArrayMap<String, PackagePreferences> packagePreferences) {
1421 final int N = packagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001422 for (int i = 0; i < N; i++) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001423 final PackagePreferences r = packagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001424 if (filter.matches(r.pkg)) {
1425 pw.print(prefix);
1426 pw.print(" AppSettings: ");
1427 pw.print(r.pkg);
1428 pw.print(" (");
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001429 pw.print(r.uid == UNKNOWN_UID ? "UNKNOWN_UID" : Integer.toString(r.uid));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001430 pw.print(')');
1431 if (r.importance != DEFAULT_IMPORTANCE) {
1432 pw.print(" importance=");
1433 pw.print(NotificationListenerService.Ranking.importanceToString(r.importance));
1434 }
1435 if (r.priority != DEFAULT_PRIORITY) {
1436 pw.print(" priority=");
1437 pw.print(Notification.priorityToString(r.priority));
1438 }
1439 if (r.visibility != DEFAULT_VISIBILITY) {
1440 pw.print(" visibility=");
1441 pw.print(Notification.visibilityToString(r.visibility));
1442 }
Julia Reynoldse7ca31b2019-04-25 15:41:47 -04001443 if (r.showBadge != DEFAULT_SHOW_BADGE) {
1444 pw.print(" showBadge=");
1445 pw.print(r.showBadge);
1446 }
1447 if (r.defaultAppLockedImportance != DEFAULT_APP_LOCKED_IMPORTANCE) {
1448 pw.print(" defaultAppLocked=");
1449 pw.print(r.defaultAppLockedImportance);
1450 }
1451 if (r.oemLockedImportance != DEFAULT_OEM_LOCKED_IMPORTANCE) {
1452 pw.print(" oemLocked=");
1453 pw.print(r.oemLockedImportance);
1454 }
1455 if (!r.futureOemLockedChannels.isEmpty()) {
1456 pw.print(" futureLockedChannels=");
1457 pw.print(r.futureOemLockedChannels);
1458 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001459 pw.println();
1460 for (NotificationChannel channel : r.channels.values()) {
1461 pw.print(prefix);
1462 channel.dump(pw, " ", filter.redact);
1463 }
1464 for (NotificationChannelGroup group : r.groups.values()) {
1465 pw.print(prefix);
1466 pw.print(" ");
1467 pw.print(" ");
1468 pw.println(group);
1469 }
1470 }
1471 }
1472 }
1473
Julia Reynolds5c399c62019-04-08 14:42:53 -04001474 private static void dumpPackagePreferencesLocked(ProtoOutputStream proto, long fieldId,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001475 @NonNull NotificationManagerService.DumpFilter filter,
Julia Reynolds5c399c62019-04-08 14:42:53 -04001476 ArrayMap<String, PackagePreferences> packagePreferences) {
1477 final int N = packagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001478 long fToken;
1479 for (int i = 0; i < N; i++) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001480 final PackagePreferences r = packagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001481 if (filter.matches(r.pkg)) {
1482 fToken = proto.start(fieldId);
1483
1484 proto.write(RankingHelperProto.RecordProto.PACKAGE, r.pkg);
1485 proto.write(RankingHelperProto.RecordProto.UID, r.uid);
1486 proto.write(RankingHelperProto.RecordProto.IMPORTANCE, r.importance);
1487 proto.write(RankingHelperProto.RecordProto.PRIORITY, r.priority);
1488 proto.write(RankingHelperProto.RecordProto.VISIBILITY, r.visibility);
1489 proto.write(RankingHelperProto.RecordProto.SHOW_BADGE, r.showBadge);
1490
1491 for (NotificationChannel channel : r.channels.values()) {
1492 channel.writeToProto(proto, RankingHelperProto.RecordProto.CHANNELS);
1493 }
1494 for (NotificationChannelGroup group : r.groups.values()) {
1495 group.writeToProto(proto, RankingHelperProto.RecordProto.CHANNEL_GROUPS);
1496 }
1497
1498 proto.end(fToken);
1499 }
1500 }
1501 }
1502
1503 public JSONObject dumpJson(NotificationManagerService.DumpFilter filter) {
1504 JSONObject ranking = new JSONObject();
1505 JSONArray PackagePreferencess = new JSONArray();
1506 try {
1507 ranking.put("noUid", mRestoredWithoutUids.size());
1508 } catch (JSONException e) {
1509 // pass
1510 }
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001511 synchronized (mPackagePreferences) {
1512 final int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001513 for (int i = 0; i < N; i++) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001514 final PackagePreferences r = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001515 if (filter == null || filter.matches(r.pkg)) {
1516 JSONObject PackagePreferences = new JSONObject();
1517 try {
1518 PackagePreferences.put("userId", UserHandle.getUserId(r.uid));
1519 PackagePreferences.put("packageName", r.pkg);
1520 if (r.importance != DEFAULT_IMPORTANCE) {
1521 PackagePreferences.put("importance",
1522 NotificationListenerService.Ranking.importanceToString(
1523 r.importance));
1524 }
1525 if (r.priority != DEFAULT_PRIORITY) {
1526 PackagePreferences.put("priority",
1527 Notification.priorityToString(r.priority));
1528 }
1529 if (r.visibility != DEFAULT_VISIBILITY) {
1530 PackagePreferences.put("visibility",
1531 Notification.visibilityToString(r.visibility));
1532 }
1533 if (r.showBadge != DEFAULT_SHOW_BADGE) {
1534 PackagePreferences.put("showBadge", Boolean.valueOf(r.showBadge));
1535 }
1536 JSONArray channels = new JSONArray();
1537 for (NotificationChannel channel : r.channels.values()) {
1538 channels.put(channel.toJson());
1539 }
1540 PackagePreferences.put("channels", channels);
1541 JSONArray groups = new JSONArray();
1542 for (NotificationChannelGroup group : r.groups.values()) {
1543 groups.put(group.toJson());
1544 }
1545 PackagePreferences.put("groups", groups);
1546 } catch (JSONException e) {
1547 // pass
1548 }
1549 PackagePreferencess.put(PackagePreferences);
1550 }
1551 }
1552 }
1553 try {
1554 ranking.put("PackagePreferencess", PackagePreferencess);
1555 } catch (JSONException e) {
1556 // pass
1557 }
1558 return ranking;
1559 }
1560
1561 /**
1562 * Dump only the ban information as structured JSON for the stats collector.
1563 *
1564 * This is intentionally redundant with {#link dumpJson} because the old
1565 * scraper will expect this format.
1566 *
1567 * @param filter
1568 * @return
1569 */
1570 public JSONArray dumpBansJson(NotificationManagerService.DumpFilter filter) {
1571 JSONArray bans = new JSONArray();
1572 Map<Integer, String> packageBans = getPackageBans();
1573 for (Map.Entry<Integer, String> ban : packageBans.entrySet()) {
1574 final int userId = UserHandle.getUserId(ban.getKey());
1575 final String packageName = ban.getValue();
1576 if (filter == null || filter.matches(packageName)) {
1577 JSONObject banJson = new JSONObject();
1578 try {
1579 banJson.put("userId", userId);
1580 banJson.put("packageName", packageName);
1581 } catch (JSONException e) {
1582 e.printStackTrace();
1583 }
1584 bans.put(banJson);
1585 }
1586 }
1587 return bans;
1588 }
1589
1590 public Map<Integer, String> getPackageBans() {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001591 synchronized (mPackagePreferences) {
1592 final int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001593 ArrayMap<Integer, String> packageBans = new ArrayMap<>(N);
1594 for (int i = 0; i < N; i++) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001595 final PackagePreferences r = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001596 if (r.importance == IMPORTANCE_NONE) {
1597 packageBans.put(r.uid, r.pkg);
1598 }
1599 }
1600
1601 return packageBans;
1602 }
1603 }
1604
1605 /**
1606 * Dump only the channel information as structured JSON for the stats collector.
1607 *
1608 * This is intentionally redundant with {#link dumpJson} because the old
1609 * scraper will expect this format.
1610 *
1611 * @param filter
1612 * @return
1613 */
1614 public JSONArray dumpChannelsJson(NotificationManagerService.DumpFilter filter) {
1615 JSONArray channels = new JSONArray();
1616 Map<String, Integer> packageChannels = getPackageChannels();
1617 for (Map.Entry<String, Integer> channelCount : packageChannels.entrySet()) {
1618 final String packageName = channelCount.getKey();
1619 if (filter == null || filter.matches(packageName)) {
1620 JSONObject channelCountJson = new JSONObject();
1621 try {
1622 channelCountJson.put("packageName", packageName);
1623 channelCountJson.put("channelCount", channelCount.getValue());
1624 } catch (JSONException e) {
1625 e.printStackTrace();
1626 }
1627 channels.put(channelCountJson);
1628 }
1629 }
1630 return channels;
1631 }
1632
1633 private Map<String, Integer> getPackageChannels() {
1634 ArrayMap<String, Integer> packageChannels = new ArrayMap<>();
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001635 synchronized (mPackagePreferences) {
1636 for (int i = 0; i < mPackagePreferences.size(); i++) {
1637 final PackagePreferences r = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001638 int channelCount = 0;
1639 for (int j = 0; j < r.channels.size(); j++) {
1640 if (!r.channels.valueAt(j).isDeleted()) {
1641 channelCount++;
1642 }
1643 }
1644 packageChannels.put(r.pkg, channelCount);
1645 }
1646 }
1647 return packageChannels;
1648 }
1649
Beverly0479cde22018-11-09 11:05:34 -05001650 /**
1651 * Called when user switches
1652 */
1653 public void onUserSwitched(int userId) {
1654 syncChannelsBypassingDnd(userId);
1655 }
1656
1657 /**
1658 * Called when user is unlocked
1659 */
1660 public void onUserUnlocked(int userId) {
1661 syncChannelsBypassingDnd(userId);
1662 }
1663
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001664 public void onUserRemoved(int userId) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001665 synchronized (mPackagePreferences) {
1666 int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001667 for (int i = N - 1; i >= 0; i--) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001668 PackagePreferences PackagePreferences = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001669 if (UserHandle.getUserId(PackagePreferences.uid) == userId) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001670 mPackagePreferences.removeAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001671 }
1672 }
1673 }
1674 }
1675
1676 protected void onLocaleChanged(Context context, int userId) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001677 synchronized (mPackagePreferences) {
1678 int N = mPackagePreferences.size();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001679 for (int i = 0; i < N; i++) {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001680 PackagePreferences PackagePreferences = mPackagePreferences.valueAt(i);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001681 if (UserHandle.getUserId(PackagePreferences.uid) == userId) {
1682 if (PackagePreferences.channels.containsKey(
1683 NotificationChannel.DEFAULT_CHANNEL_ID)) {
1684 PackagePreferences.channels.get(
1685 NotificationChannel.DEFAULT_CHANNEL_ID).setName(
1686 context.getResources().getString(
1687 R.string.default_notification_channel_label));
1688 }
1689 }
1690 }
1691 }
1692 }
1693
Julia Reynolds996c7c12019-05-24 10:25:33 -04001694 public boolean onPackagesChanged(boolean removingPackage, int changeUserId, String[] pkgList,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001695 int[] uidList) {
1696 if (pkgList == null || pkgList.length == 0) {
Julia Reynolds996c7c12019-05-24 10:25:33 -04001697 return false; // nothing to do
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001698 }
1699 boolean updated = false;
1700 if (removingPackage) {
1701 // Remove notification settings for uninstalled package
1702 int size = Math.min(pkgList.length, uidList.length);
1703 for (int i = 0; i < size; i++) {
1704 final String pkg = pkgList[i];
1705 final int uid = uidList[i];
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001706 synchronized (mPackagePreferences) {
1707 mPackagePreferences.remove(packagePreferencesKey(pkg, uid));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001708 }
1709 mRestoredWithoutUids.remove(pkg);
1710 updated = true;
1711 }
1712 } else {
1713 for (String pkg : pkgList) {
1714 // Package install
1715 final PackagePreferences r = mRestoredWithoutUids.get(pkg);
1716 if (r != null) {
1717 try {
1718 r.uid = mPm.getPackageUidAsUser(r.pkg, changeUserId);
1719 mRestoredWithoutUids.remove(pkg);
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001720 synchronized (mPackagePreferences) {
1721 mPackagePreferences.put(packagePreferencesKey(r.pkg, r.uid), r);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001722 }
1723 updated = true;
1724 } catch (PackageManager.NameNotFoundException e) {
1725 // noop
1726 }
1727 }
1728 // Package upgrade
1729 try {
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001730 synchronized (mPackagePreferences) {
Julia Reynolds5c399c62019-04-08 14:42:53 -04001731 PackagePreferences fullPackagePreferences = getPackagePreferencesLocked(pkg,
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001732 mPm.getPackageUidAsUser(pkg, changeUserId));
1733 if (fullPackagePreferences != null) {
Julia Reynolds996c7c12019-05-24 10:25:33 -04001734 updated |= createDefaultChannelIfNeededLocked(fullPackagePreferences);
1735 updated |= deleteDefaultChannelIfNeededLocked(fullPackagePreferences);
Julia Reynoldsb24c62c2018-09-10 10:05:15 -04001736 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001737 }
1738 } catch (PackageManager.NameNotFoundException e) {
1739 }
1740 }
1741 }
1742
1743 if (updated) {
1744 updateConfig();
1745 }
Julia Reynolds996c7c12019-05-24 10:25:33 -04001746 return updated;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001747 }
1748
Julia Reynolds7af51c52019-04-19 11:08:27 -04001749 public void clearData(String pkg, int uid) {
1750 synchronized (mPackagePreferences) {
1751 PackagePreferences p = getPackagePreferencesLocked(pkg, uid);
1752 if (p != null) {
1753 p.channels = new ArrayMap<>();
1754 p.groups = new ArrayMap<>();
1755 p.delegate = null;
1756 p.lockedAppFields = DEFAULT_LOCKED_APP_FIELDS;
1757 p.allowBubble = DEFAULT_ALLOW_BUBBLE;
1758 p.importance = DEFAULT_IMPORTANCE;
1759 p.priority = DEFAULT_PRIORITY;
1760 p.visibility = DEFAULT_VISIBILITY;
1761 p.showBadge = DEFAULT_SHOW_BADGE;
1762 }
1763 }
1764 }
1765
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001766 private LogMaker getChannelLog(NotificationChannel channel, String pkg) {
1767 return new LogMaker(
1768 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1769 .ACTION_NOTIFICATION_CHANNEL)
1770 .setType(com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_UPDATE)
1771 .setPackageName(pkg)
1772 .addTaggedData(
1773 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1774 .FIELD_NOTIFICATION_CHANNEL_ID,
1775 channel.getId())
1776 .addTaggedData(
1777 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1778 .FIELD_NOTIFICATION_CHANNEL_IMPORTANCE,
1779 channel.getImportance());
1780 }
1781
1782 private LogMaker getChannelGroupLog(String groupId, String pkg) {
1783 return new LogMaker(
1784 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1785 .ACTION_NOTIFICATION_CHANNEL_GROUP)
1786 .setType(com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_UPDATE)
1787 .addTaggedData(
1788 com.android.internal.logging.nano.MetricsProto.MetricsEvent
1789 .FIELD_NOTIFICATION_CHANNEL_GROUP_ID,
1790 groupId)
1791 .setPackageName(pkg);
1792 }
1793
Julia Reynolds4509ce72019-01-31 13:12:43 -05001794 public void updateBubblesEnabled() {
1795 if (mBubblesEnabled == null) {
1796 mBubblesEnabled = new SparseBooleanArray();
1797 }
1798 boolean changed = false;
1799 // update the cached values
1800 for (int index = 0; index < mBubblesEnabled.size(); index++) {
1801 int userId = mBubblesEnabled.keyAt(index);
1802 final boolean oldValue = mBubblesEnabled.get(userId);
1803 final boolean newValue = Settings.Secure.getIntForUser(mContext.getContentResolver(),
1804 Settings.Secure.NOTIFICATION_BUBBLES,
1805 DEFAULT_ALLOW_BUBBLE ? 1 : 0, userId) != 0;
1806 mBubblesEnabled.put(userId, newValue);
1807 changed |= oldValue != newValue;
1808 }
1809 if (changed) {
1810 updateConfig();
1811 }
1812 }
1813
1814 public boolean bubblesEnabled(UserHandle userHandle) {
1815 int userId = userHandle.getIdentifier();
1816 if (userId == UserHandle.USER_ALL) {
1817 return false;
1818 }
1819 if (mBubblesEnabled.indexOfKey(userId) < 0) {
1820 mBubblesEnabled.put(userId,
1821 Settings.Secure.getIntForUser(mContext.getContentResolver(),
1822 Settings.Secure.NOTIFICATION_BUBBLES,
1823 DEFAULT_ALLOW_BUBBLE ? 1 : 0, userId) != 0);
1824 }
1825 return mBubblesEnabled.get(userId, DEFAULT_ALLOW_BUBBLE);
1826 }
1827
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001828
1829 public void updateBadgingEnabled() {
1830 if (mBadgingEnabled == null) {
1831 mBadgingEnabled = new SparseBooleanArray();
1832 }
1833 boolean changed = false;
1834 // update the cached values
1835 for (int index = 0; index < mBadgingEnabled.size(); index++) {
1836 int userId = mBadgingEnabled.keyAt(index);
1837 final boolean oldValue = mBadgingEnabled.get(userId);
1838 final boolean newValue = Settings.Secure.getIntForUser(mContext.getContentResolver(),
1839 Settings.Secure.NOTIFICATION_BADGING,
1840 DEFAULT_SHOW_BADGE ? 1 : 0, userId) != 0;
1841 mBadgingEnabled.put(userId, newValue);
1842 changed |= oldValue != newValue;
1843 }
1844 if (changed) {
1845 updateConfig();
1846 }
1847 }
1848
1849 public boolean badgingEnabled(UserHandle userHandle) {
1850 int userId = userHandle.getIdentifier();
1851 if (userId == UserHandle.USER_ALL) {
1852 return false;
1853 }
1854 if (mBadgingEnabled.indexOfKey(userId) < 0) {
1855 mBadgingEnabled.put(userId,
1856 Settings.Secure.getIntForUser(mContext.getContentResolver(),
1857 Settings.Secure.NOTIFICATION_BADGING,
1858 DEFAULT_SHOW_BADGE ? 1 : 0, userId) != 0);
1859 }
1860 return mBadgingEnabled.get(userId, DEFAULT_SHOW_BADGE);
1861 }
1862
1863 private void updateConfig() {
1864 mRankingHandler.requestSort();
1865 }
1866
1867 private static String packagePreferencesKey(String pkg, int uid) {
1868 return pkg + "|" + uid;
1869 }
1870
1871 private static class PackagePreferences {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001872 String pkg;
1873 int uid = UNKNOWN_UID;
1874 int importance = DEFAULT_IMPORTANCE;
1875 int priority = DEFAULT_PRIORITY;
1876 int visibility = DEFAULT_VISIBILITY;
1877 boolean showBadge = DEFAULT_SHOW_BADGE;
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001878 boolean allowBubble = DEFAULT_ALLOW_BUBBLE;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001879 int lockedAppFields = DEFAULT_LOCKED_APP_FIELDS;
Julia Reynolds0c245002019-03-27 16:10:11 -04001880 // these fields are loaded on boot from a different source of truth and so are not
1881 // written to notification policy xml
Julia Reynolds413ba842019-01-11 10:38:08 -05001882 boolean oemLockedImportance = DEFAULT_OEM_LOCKED_IMPORTANCE;
1883 List<String> futureOemLockedChannels = new ArrayList<>();
Julia Reynolds0c245002019-03-27 16:10:11 -04001884 boolean defaultAppLockedImportance = DEFAULT_APP_LOCKED_IMPORTANCE;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001885
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001886 Delegate delegate = null;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001887 ArrayMap<String, NotificationChannel> channels = new ArrayMap<>();
1888 Map<String, NotificationChannelGroup> groups = new ConcurrentHashMap<>();
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04001889
1890 public boolean isValidDelegate(String pkg, int uid) {
1891 return delegate != null && delegate.isAllowed(pkg, uid);
1892 }
1893 }
1894
1895 private static class Delegate {
1896 static final boolean DEFAULT_ENABLED = true;
1897 static final boolean DEFAULT_USER_ALLOWED = true;
1898 String mPkg;
1899 int mUid = UNKNOWN_UID;
1900 boolean mEnabled = DEFAULT_ENABLED;
1901 boolean mUserAllowed = DEFAULT_USER_ALLOWED;
1902
1903 Delegate(String pkg, int uid, boolean enabled, boolean userAllowed) {
1904 mPkg = pkg;
1905 mUid = uid;
1906 mEnabled = enabled;
1907 mUserAllowed = userAllowed;
1908 }
1909
1910 public boolean isAllowed(String pkg, int uid) {
1911 if (pkg == null || uid == UNKNOWN_UID) {
1912 return false;
1913 }
1914 return pkg.equals(mPkg)
1915 && uid == mUid
1916 && (mUserAllowed && mEnabled);
1917 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001918 }
1919}