Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1 | /** |
| 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 | |
| 17 | package com.android.server.notification; |
| 18 | |
| 19 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
| 20 | |
| 21 | import android.annotation.IntDef; |
| 22 | import android.annotation.NonNull; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 24 | import android.app.Notification; |
| 25 | import android.app.NotificationChannel; |
| 26 | import android.app.NotificationChannelGroup; |
| 27 | import android.app.NotificationManager; |
| 28 | import android.content.Context; |
| 29 | import android.content.pm.ApplicationInfo; |
| 30 | import android.content.pm.PackageManager; |
| 31 | import android.content.pm.ParceledListSlice; |
| 32 | import android.metrics.LogMaker; |
| 33 | import android.os.Build; |
| 34 | import android.os.UserHandle; |
| 35 | import android.provider.Settings; |
| 36 | import android.service.notification.NotificationListenerService; |
| 37 | import android.service.notification.RankingHelperProto; |
| 38 | import android.text.TextUtils; |
| 39 | import android.util.ArrayMap; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 40 | import android.util.ArraySet; |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 41 | import android.util.Pair; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 42 | import android.util.Slog; |
| 43 | import android.util.SparseBooleanArray; |
| 44 | import android.util.proto.ProtoOutputStream; |
| 45 | |
| 46 | import com.android.internal.R; |
| 47 | import com.android.internal.annotations.VisibleForTesting; |
| 48 | import com.android.internal.logging.MetricsLogger; |
| 49 | import com.android.internal.util.Preconditions; |
| 50 | import com.android.internal.util.XmlUtils; |
| 51 | |
| 52 | import org.json.JSONArray; |
| 53 | import org.json.JSONException; |
| 54 | import org.json.JSONObject; |
| 55 | import org.xmlpull.v1.XmlPullParser; |
| 56 | import org.xmlpull.v1.XmlPullParserException; |
| 57 | import org.xmlpull.v1.XmlSerializer; |
| 58 | |
| 59 | import java.io.IOException; |
| 60 | import java.io.PrintWriter; |
| 61 | import java.util.ArrayList; |
| 62 | import java.util.Arrays; |
| 63 | import java.util.Collection; |
| 64 | import java.util.List; |
| 65 | import java.util.Map; |
| 66 | import java.util.Objects; |
| 67 | import java.util.concurrent.ConcurrentHashMap; |
| 68 | |
| 69 | public class PreferencesHelper implements RankingConfig { |
| 70 | private static final String TAG = "NotificationPrefHelper"; |
| 71 | private static final int XML_VERSION = 1; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 72 | private static final int UNKNOWN_UID = UserHandle.USER_NULL; |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 73 | private static final String NON_BLOCKABLE_CHANNEL_DELIM = ":"; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 74 | |
| 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 Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 80 | private static final String TAG_DELEGATE = "delegate"; |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 81 | private static final String TAG_STATUS_ICONS = "status_icons"; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 82 | |
| 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 Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 87 | private static final String ATT_ALLOW_BUBBLE = "allow_bubble"; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 88 | 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 Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 93 | private static final String ATT_ENABLED = "enabled"; |
| 94 | private static final String ATT_USER_ALLOWED = "allowed"; |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 95 | private static final String ATT_HIDE_SILENT = "hide_silent"; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 96 | |
| 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 Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 100 | @VisibleForTesting |
Julia Reynolds | 2594b47 | 2019-04-03 13:30:16 -0400 | [diff] [blame] | 101 | static final boolean DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS = true; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 102 | private static final boolean DEFAULT_SHOW_BADGE = true; |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 103 | private static final boolean DEFAULT_ALLOW_BUBBLE = true; |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 104 | private static final boolean DEFAULT_OEM_LOCKED_IMPORTANCE = false; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 105 | private static final boolean DEFAULT_APP_LOCKED_IMPORTANCE = false; |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 106 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 107 | /** |
| 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 Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 119 | int USER_LOCKED_BUBBLE = 0x00000002; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // pkg|uid => PackagePreferences |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 123 | private final ArrayMap<String, PackagePreferences> mPackagePreferences = new ArrayMap<>(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 124 | // pkg => PackagePreferences |
| 125 | private final ArrayMap<String, PackagePreferences> mRestoredWithoutUids = new ArrayMap<>(); |
| 126 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 127 | 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 Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 133 | private SparseBooleanArray mBubblesEnabled; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 134 | private boolean mAreChannelsBypassingDnd; |
Julia Reynolds | 2594b47 | 2019-04-03 13:30:16 -0400 | [diff] [blame] | 135 | private boolean mHideSilentStatusBarIcons = DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 136 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 137 | 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 Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 145 | updateBubblesEnabled(); |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 146 | syncChannelsBypassingDnd(mContext.getUserId()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 149 | public void readXml(XmlPullParser parser, boolean forRestore, int userId) |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 150 | 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 Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 155 | synchronized (mPackagePreferences) { |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 156 | // Clobber groups and channels with the xml, but don't delete other data that wasn't |
| 157 | // present at the time of serialization. |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 158 | 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 Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 165 | if (TAG_STATUS_ICONS.equals(tag)) { |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 166 | if (forRestore && userId != UserHandle.USER_SYSTEM) { |
| 167 | continue; |
| 168 | } |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 169 | mHideSilentStatusBarIcons = XmlUtils.readBooleanAttribute( |
| 170 | parser, ATT_HIDE_SILENT, DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS); |
| 171 | } else if (TAG_PACKAGE.equals(tag)) { |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 172 | 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 Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 177 | uid = mPm.getPackageUidAsUser(name, userId); |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 178 | } catch (PackageManager.NameNotFoundException e) { |
| 179 | // noop |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 180 | } |
| 181 | } |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 182 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 183 | PackagePreferences r = getOrCreatePackagePreferencesLocked(name, uid, |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 184 | 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 Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 191 | parser, ATT_SHOW_BADGE, DEFAULT_SHOW_BADGE), |
| 192 | XmlUtils.readBooleanAttribute( |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 193 | parser, ATT_ALLOW_BUBBLE, DEFAULT_ALLOW_BUBBLE)); |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 194 | 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 Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 224 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 225 | } |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 226 | // 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 Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 240 | channel.setImportanceLockedByCriticalDeviceFunction( |
| 241 | r.defaultAppLockedImportance); |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 242 | r.channels.put(id, channel); |
| 243 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 244 | } |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 245 | // 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 Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 266 | } |
| 267 | |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 268 | try { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 269 | deleteDefaultChannelIfNeededLocked(r); |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 270 | } catch (PackageManager.NameNotFoundException e) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 271 | Slog.e(TAG, "deleteDefaultChannelIfNeededLocked - Exception: " + e); |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 272 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | throw new IllegalStateException("Failed to reach END_DOCUMENT"); |
| 279 | } |
| 280 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 281 | private PackagePreferences getPackagePreferencesLocked(String pkg, int uid) { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 282 | final String key = packagePreferencesKey(pkg, uid); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 283 | return mPackagePreferences.get(key); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 284 | } |
| 285 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 286 | private PackagePreferences getOrCreatePackagePreferencesLocked(String pkg, int uid) { |
| 287 | return getOrCreatePackagePreferencesLocked(pkg, uid, |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 288 | DEFAULT_IMPORTANCE, DEFAULT_PRIORITY, DEFAULT_VISIBILITY, DEFAULT_SHOW_BADGE, |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 289 | DEFAULT_ALLOW_BUBBLE); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 290 | } |
| 291 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 292 | private PackagePreferences getOrCreatePackagePreferencesLocked(String pkg, int uid, |
| 293 | int importance, int priority, int visibility, boolean showBadge, boolean allowBubble) { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 294 | final String key = packagePreferencesKey(pkg, uid); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 295 | 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 Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 307 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 308 | try { |
| 309 | createDefaultChannelIfNeededLocked(r); |
| 310 | } catch (PackageManager.NameNotFoundException e) { |
| 311 | Slog.e(TAG, "createDefaultChannelIfNeededLocked - Exception: " + e); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 312 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 313 | |
| 314 | if (r.uid == UNKNOWN_UID) { |
| 315 | mRestoredWithoutUids.put(pkg, r); |
| 316 | } else { |
| 317 | mPackagePreferences.put(key, r); |
| 318 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 319 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 320 | return r; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 321 | } |
| 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 Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 337 | private void deleteDefaultChannelIfNeededLocked(PackagePreferences r) throws |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 338 | PackageManager.NameNotFoundException { |
| 339 | if (!r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) { |
| 340 | // Not present |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | if (shouldHaveDefaultChannel(r)) { |
| 345 | // Keep the default channel until upgraded. |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | // Remove Default Channel. |
| 350 | r.channels.remove(NotificationChannel.DEFAULT_CHANNEL_ID); |
| 351 | } |
| 352 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 353 | private void createDefaultChannelIfNeededLocked(PackagePreferences r) throws |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 354 | PackageManager.NameNotFoundException { |
| 355 | if (r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) { |
| 356 | r.channels.get(NotificationChannel.DEFAULT_CHANNEL_ID).setName(mContext.getString( |
| 357 | com.android.internal.R.string.default_notification_channel_label)); |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | if (!shouldHaveDefaultChannel(r)) { |
| 362 | // Keep the default channel until upgraded. |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | // Create Default Channel |
| 367 | NotificationChannel channel; |
| 368 | channel = new NotificationChannel( |
| 369 | NotificationChannel.DEFAULT_CHANNEL_ID, |
| 370 | mContext.getString(R.string.default_notification_channel_label), |
| 371 | r.importance); |
| 372 | channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX); |
| 373 | channel.setLockscreenVisibility(r.visibility); |
| 374 | if (r.importance != NotificationManager.IMPORTANCE_UNSPECIFIED) { |
| 375 | channel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); |
| 376 | } |
| 377 | if (r.priority != DEFAULT_PRIORITY) { |
| 378 | channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY); |
| 379 | } |
| 380 | if (r.visibility != DEFAULT_VISIBILITY) { |
| 381 | channel.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY); |
| 382 | } |
| 383 | r.channels.put(channel.getId(), channel); |
| 384 | } |
| 385 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 386 | public void writeXml(XmlSerializer out, boolean forBackup, int userId) throws IOException { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 387 | out.startTag(null, TAG_RANKING); |
| 388 | out.attribute(null, ATT_VERSION, Integer.toString(XML_VERSION)); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 389 | if (mHideSilentStatusBarIcons != DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS |
| 390 | && (!forBackup || userId == UserHandle.USER_SYSTEM)) { |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 391 | out.startTag(null, TAG_STATUS_ICONS); |
| 392 | out.attribute(null, ATT_HIDE_SILENT, String.valueOf(mHideSilentStatusBarIcons)); |
| 393 | out.endTag(null, TAG_STATUS_ICONS); |
| 394 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 395 | |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 396 | synchronized (mPackagePreferences) { |
| 397 | final int N = mPackagePreferences.size(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 398 | for (int i = 0; i < N; i++) { |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 399 | final PackagePreferences r = mPackagePreferences.valueAt(i); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 400 | if (forBackup && UserHandle.getUserId(r.uid) != userId) { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 401 | continue; |
| 402 | } |
| 403 | final boolean hasNonDefaultSettings = |
| 404 | r.importance != DEFAULT_IMPORTANCE |
| 405 | || r.priority != DEFAULT_PRIORITY |
| 406 | || r.visibility != DEFAULT_VISIBILITY |
| 407 | || r.showBadge != DEFAULT_SHOW_BADGE |
| 408 | || r.lockedAppFields != DEFAULT_LOCKED_APP_FIELDS |
| 409 | || r.channels.size() > 0 |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 410 | || r.groups.size() > 0 |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 411 | || r.delegate != null |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 412 | || r.allowBubble != DEFAULT_ALLOW_BUBBLE; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 413 | if (hasNonDefaultSettings) { |
| 414 | out.startTag(null, TAG_PACKAGE); |
| 415 | out.attribute(null, ATT_NAME, r.pkg); |
| 416 | if (r.importance != DEFAULT_IMPORTANCE) { |
| 417 | out.attribute(null, ATT_IMPORTANCE, Integer.toString(r.importance)); |
| 418 | } |
| 419 | if (r.priority != DEFAULT_PRIORITY) { |
| 420 | out.attribute(null, ATT_PRIORITY, Integer.toString(r.priority)); |
| 421 | } |
| 422 | if (r.visibility != DEFAULT_VISIBILITY) { |
| 423 | out.attribute(null, ATT_VISIBILITY, Integer.toString(r.visibility)); |
| 424 | } |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 425 | if (r.allowBubble != DEFAULT_ALLOW_BUBBLE) { |
| 426 | out.attribute(null, ATT_ALLOW_BUBBLE, Boolean.toString(r.allowBubble)); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 427 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 428 | out.attribute(null, ATT_SHOW_BADGE, Boolean.toString(r.showBadge)); |
| 429 | out.attribute(null, ATT_APP_USER_LOCKED_FIELDS, |
| 430 | Integer.toString(r.lockedAppFields)); |
| 431 | |
| 432 | if (!forBackup) { |
| 433 | out.attribute(null, ATT_UID, Integer.toString(r.uid)); |
| 434 | } |
| 435 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 436 | if (r.delegate != null) { |
| 437 | out.startTag(null, TAG_DELEGATE); |
| 438 | |
| 439 | out.attribute(null, ATT_NAME, r.delegate.mPkg); |
| 440 | out.attribute(null, ATT_UID, Integer.toString(r.delegate.mUid)); |
| 441 | if (r.delegate.mEnabled != Delegate.DEFAULT_ENABLED) { |
| 442 | out.attribute(null, ATT_ENABLED, Boolean.toString(r.delegate.mEnabled)); |
| 443 | } |
| 444 | if (r.delegate.mUserAllowed != Delegate.DEFAULT_USER_ALLOWED) { |
| 445 | out.attribute(null, ATT_USER_ALLOWED, |
| 446 | Boolean.toString(r.delegate.mUserAllowed)); |
| 447 | } |
| 448 | out.endTag(null, TAG_DELEGATE); |
| 449 | } |
| 450 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 451 | for (NotificationChannelGroup group : r.groups.values()) { |
| 452 | group.writeXml(out); |
| 453 | } |
| 454 | |
| 455 | for (NotificationChannel channel : r.channels.values()) { |
| 456 | if (forBackup) { |
| 457 | if (!channel.isDeleted()) { |
| 458 | channel.writeXmlForBackup(out, mContext); |
| 459 | } |
| 460 | } else { |
| 461 | channel.writeXml(out); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | out.endTag(null, TAG_PACKAGE); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | out.endTag(null, TAG_RANKING); |
| 470 | } |
| 471 | |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 472 | /** |
| 473 | * Sets whether bubbles are allowed. |
| 474 | * |
| 475 | * @param pkg the package to allow or not allow bubbles for. |
| 476 | * @param uid the uid to allow or not allow bubbles for. |
| 477 | * @param allowed whether bubbles are allowed. |
| 478 | */ |
| 479 | public void setBubblesAllowed(String pkg, int uid, boolean allowed) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 480 | synchronized (mPackagePreferences) { |
| 481 | PackagePreferences p = getOrCreatePackagePreferencesLocked(pkg, uid); |
| 482 | p.allowBubble = allowed; |
| 483 | p.lockedAppFields = p.lockedAppFields | LockableAppFields.USER_LOCKED_BUBBLE; |
| 484 | } |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 485 | } |
| 486 | |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 487 | /** |
| 488 | * Whether bubbles are allowed. |
| 489 | * |
| 490 | * @param pkg the package to check if bubbles are allowed for |
| 491 | * @param uid the uid to check if bubbles are allowed for. |
| 492 | * @return whether bubbles are allowed. |
| 493 | */ |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 494 | @Override |
Mady Mellor | 9db685a | 2019-01-23 13:23:37 -0800 | [diff] [blame] | 495 | public boolean areBubblesAllowed(String pkg, int uid) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 496 | synchronized (mPackagePreferences) { |
| 497 | return getOrCreatePackagePreferencesLocked(pkg, uid).allowBubble; |
| 498 | } |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | public int getAppLockedFields(String pkg, int uid) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 502 | synchronized (mPackagePreferences) { |
| 503 | return getOrCreatePackagePreferencesLocked(pkg, uid).lockedAppFields; |
| 504 | } |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 505 | } |
| 506 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 507 | /** |
| 508 | * Gets importance. |
| 509 | */ |
| 510 | @Override |
| 511 | public int getImportance(String packageName, int uid) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 512 | synchronized (mPackagePreferences) { |
| 513 | return getOrCreatePackagePreferencesLocked(packageName, uid).importance; |
| 514 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 515 | } |
| 516 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 517 | /** |
| 518 | * Returns whether the importance of the corresponding notification is user-locked and shouldn't |
| 519 | * be adjusted by an assistant (via means of a blocking helper, for example). For the channel |
| 520 | * locking field, see {@link NotificationChannel#USER_LOCKED_IMPORTANCE}. |
| 521 | */ |
| 522 | public boolean getIsAppImportanceLocked(String packageName, int uid) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 523 | synchronized (mPackagePreferences) { |
| 524 | int userLockedFields = getOrCreatePackagePreferencesLocked(packageName, uid).lockedAppFields; |
| 525 | return (userLockedFields & LockableAppFields.USER_LOCKED_IMPORTANCE) != 0; |
| 526 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | @Override |
| 530 | public boolean canShowBadge(String packageName, int uid) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 531 | synchronized (mPackagePreferences) { |
| 532 | return getOrCreatePackagePreferencesLocked(packageName, uid).showBadge; |
| 533 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | @Override |
| 537 | public void setShowBadge(String packageName, int uid, boolean showBadge) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 538 | synchronized (mPackagePreferences) { |
| 539 | getOrCreatePackagePreferencesLocked(packageName, uid).showBadge = showBadge; |
| 540 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 541 | updateConfig(); |
| 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public boolean isGroupBlocked(String packageName, int uid, String groupId) { |
| 546 | if (groupId == null) { |
| 547 | return false; |
| 548 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 549 | synchronized (mPackagePreferences) { |
| 550 | PackagePreferences r = getOrCreatePackagePreferencesLocked(packageName, uid); |
| 551 | NotificationChannelGroup group = r.groups.get(groupId); |
| 552 | if (group == null) { |
| 553 | return false; |
| 554 | } |
| 555 | return group.isBlocked(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 556 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | int getPackagePriority(String pkg, int uid) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 560 | synchronized (mPackagePreferences) { |
| 561 | return getOrCreatePackagePreferencesLocked(pkg, uid).priority; |
| 562 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | int getPackageVisibility(String pkg, int uid) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 566 | synchronized (mPackagePreferences) { |
| 567 | return getOrCreatePackagePreferencesLocked(pkg, uid).visibility; |
| 568 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | @Override |
| 572 | public void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group, |
| 573 | boolean fromTargetApp) { |
| 574 | Preconditions.checkNotNull(pkg); |
| 575 | Preconditions.checkNotNull(group); |
| 576 | Preconditions.checkNotNull(group.getId()); |
| 577 | Preconditions.checkNotNull(!TextUtils.isEmpty(group.getName())); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 578 | synchronized (mPackagePreferences) { |
| 579 | PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid); |
| 580 | if (r == null) { |
| 581 | throw new IllegalArgumentException("Invalid package"); |
| 582 | } |
| 583 | final NotificationChannelGroup oldGroup = r.groups.get(group.getId()); |
| 584 | if (!group.equals(oldGroup)) { |
| 585 | // will log for new entries as well as name/description changes |
| 586 | MetricsLogger.action(getChannelGroupLog(group.getId(), pkg)); |
| 587 | } |
| 588 | if (oldGroup != null) { |
| 589 | group.setChannels(oldGroup.getChannels()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 590 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 591 | // apps can't update the blocked status or app overlay permission |
| 592 | if (fromTargetApp) { |
| 593 | group.setBlocked(oldGroup.isBlocked()); |
| 594 | group.unlockFields(group.getUserLockedFields()); |
| 595 | group.lockFields(oldGroup.getUserLockedFields()); |
| 596 | } else { |
| 597 | // but the system can |
| 598 | if (group.isBlocked() != oldGroup.isBlocked()) { |
| 599 | group.lockFields(NotificationChannelGroup.USER_LOCKED_BLOCKED_STATE); |
| 600 | updateChannelsBypassingDnd(mContext.getUserId()); |
| 601 | } |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 602 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 603 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 604 | r.groups.put(group.getId(), group); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 605 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | @Override |
| 609 | public void createNotificationChannel(String pkg, int uid, NotificationChannel channel, |
| 610 | boolean fromTargetApp, boolean hasDndAccess) { |
| 611 | Preconditions.checkNotNull(pkg); |
| 612 | Preconditions.checkNotNull(channel); |
| 613 | Preconditions.checkNotNull(channel.getId()); |
| 614 | Preconditions.checkArgument(!TextUtils.isEmpty(channel.getName())); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 615 | synchronized (mPackagePreferences) { |
| 616 | PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid); |
| 617 | if (r == null) { |
| 618 | throw new IllegalArgumentException("Invalid package"); |
| 619 | } |
| 620 | if (channel.getGroup() != null && !r.groups.containsKey(channel.getGroup())) { |
| 621 | throw new IllegalArgumentException("NotificationChannelGroup doesn't exist"); |
| 622 | } |
| 623 | if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channel.getId())) { |
| 624 | throw new IllegalArgumentException("Reserved id"); |
| 625 | } |
| 626 | NotificationChannel existing = r.channels.get(channel.getId()); |
| 627 | // Keep most of the existing settings |
| 628 | if (existing != null && fromTargetApp) { |
| 629 | if (existing.isDeleted()) { |
| 630 | existing.setDeleted(false); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 631 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 632 | // log a resurrected channel as if it's new again |
| 633 | MetricsLogger.action(getChannelLog(channel, pkg).setType( |
| 634 | com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_OPEN)); |
| 635 | } |
| 636 | |
| 637 | existing.setName(channel.getName().toString()); |
| 638 | existing.setDescription(channel.getDescription()); |
| 639 | existing.setBlockableSystem(channel.isBlockableSystem()); |
| 640 | if (existing.getGroup() == null) { |
| 641 | existing.setGroup(channel.getGroup()); |
| 642 | } |
| 643 | |
| 644 | // Apps are allowed to downgrade channel importance if the user has not changed any |
| 645 | // fields on this channel yet. |
| 646 | final int previousExistingImportance = existing.getImportance(); |
| 647 | if (existing.getUserLockedFields() == 0 && |
| 648 | channel.getImportance() < existing.getImportance()) { |
| 649 | existing.setImportance(channel.getImportance()); |
| 650 | } |
| 651 | |
| 652 | // system apps and dnd access apps can bypass dnd if the user hasn't changed any |
| 653 | // fields on the channel yet |
| 654 | if (existing.getUserLockedFields() == 0 && hasDndAccess) { |
| 655 | boolean bypassDnd = channel.canBypassDnd(); |
| 656 | existing.setBypassDnd(bypassDnd); |
| 657 | |
| 658 | if (bypassDnd != mAreChannelsBypassingDnd |
| 659 | || previousExistingImportance != existing.getImportance()) { |
| 660 | updateChannelsBypassingDnd(mContext.getUserId()); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | updateConfig(); |
| 665 | return; |
| 666 | } |
| 667 | if (channel.getImportance() < IMPORTANCE_NONE |
| 668 | || channel.getImportance() > NotificationManager.IMPORTANCE_MAX) { |
| 669 | throw new IllegalArgumentException("Invalid importance level"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 670 | } |
| 671 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 672 | // Reset fields that apps aren't allowed to set. |
| 673 | if (fromTargetApp && !hasDndAccess) { |
| 674 | channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 675 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 676 | if (fromTargetApp) { |
| 677 | channel.setLockscreenVisibility(r.visibility); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 678 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 679 | clearLockedFieldsLocked(channel); |
| 680 | channel.setImportanceLockedByOEM(r.oemLockedImportance); |
| 681 | if (!channel.isImportanceLockedByOEM()) { |
| 682 | if (r.futureOemLockedChannels.remove(channel.getId())) { |
| 683 | channel.setImportanceLockedByOEM(true); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 684 | } |
| 685 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 686 | channel.setImportanceLockedByCriticalDeviceFunction(r.defaultAppLockedImportance); |
| 687 | if (channel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) { |
| 688 | channel.setLockscreenVisibility( |
| 689 | NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE); |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 690 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 691 | if (!r.showBadge) { |
| 692 | channel.setShowBadge(false); |
| 693 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 694 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 695 | r.channels.put(channel.getId(), channel); |
| 696 | if (channel.canBypassDnd() != mAreChannelsBypassingDnd) { |
| 697 | updateChannelsBypassingDnd(mContext.getUserId()); |
| 698 | } |
| 699 | MetricsLogger.action(getChannelLog(channel, pkg).setType( |
| 700 | com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_OPEN)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 701 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 702 | } |
| 703 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 704 | void clearLockedFieldsLocked(NotificationChannel channel) { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 705 | channel.unlockFields(channel.getUserLockedFields()); |
| 706 | } |
| 707 | |
| 708 | @Override |
| 709 | public void updateNotificationChannel(String pkg, int uid, NotificationChannel updatedChannel, |
| 710 | boolean fromUser) { |
| 711 | Preconditions.checkNotNull(updatedChannel); |
| 712 | Preconditions.checkNotNull(updatedChannel.getId()); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 713 | synchronized (mPackagePreferences) { |
| 714 | PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid); |
| 715 | if (r == null) { |
| 716 | throw new IllegalArgumentException("Invalid package"); |
| 717 | } |
| 718 | NotificationChannel channel = r.channels.get(updatedChannel.getId()); |
| 719 | if (channel == null || channel.isDeleted()) { |
| 720 | throw new IllegalArgumentException("Channel does not exist"); |
| 721 | } |
| 722 | if (updatedChannel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) { |
| 723 | updatedChannel.setLockscreenVisibility( |
| 724 | NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE); |
| 725 | } |
| 726 | if (fromUser) { |
| 727 | updatedChannel.lockFields(channel.getUserLockedFields()); |
| 728 | lockFieldsForUpdateLocked(channel, updatedChannel); |
| 729 | } else { |
| 730 | updatedChannel.unlockFields(updatedChannel.getUserLockedFields()); |
| 731 | } |
| 732 | // no importance updates are allowed if OEM blocked it |
| 733 | updatedChannel.setImportanceLockedByOEM(channel.isImportanceLockedByOEM()); |
| 734 | if (updatedChannel.isImportanceLockedByOEM()) { |
| 735 | updatedChannel.setImportance(channel.getImportance()); |
| 736 | } |
| 737 | updatedChannel.setImportanceLockedByCriticalDeviceFunction( |
| 738 | r.defaultAppLockedImportance); |
| 739 | if (updatedChannel.isImportanceLockedByCriticalDeviceFunction()) { |
| 740 | updatedChannel.setImportance(channel.getImportance()); |
| 741 | } |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 742 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 743 | r.channels.put(updatedChannel.getId(), updatedChannel); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 744 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 745 | if (onlyHasDefaultChannel(pkg, uid)) { |
| 746 | // copy settings to app level so they are inherited by new channels |
| 747 | // when the app migrates |
| 748 | r.importance = updatedChannel.getImportance(); |
| 749 | r.priority = updatedChannel.canBypassDnd() |
| 750 | ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT; |
| 751 | r.visibility = updatedChannel.getLockscreenVisibility(); |
| 752 | r.showBadge = updatedChannel.canShowBadge(); |
| 753 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 754 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 755 | if (!channel.equals(updatedChannel)) { |
| 756 | // only log if there are real changes |
| 757 | MetricsLogger.action(getChannelLog(updatedChannel, pkg) |
| 758 | .setSubtype(fromUser ? 1 : 0)); |
| 759 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 760 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 761 | if (updatedChannel.canBypassDnd() != mAreChannelsBypassingDnd |
| 762 | || channel.getImportance() != updatedChannel.getImportance()) { |
| 763 | updateChannelsBypassingDnd(mContext.getUserId()); |
| 764 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 765 | } |
| 766 | updateConfig(); |
| 767 | } |
| 768 | |
| 769 | @Override |
| 770 | public NotificationChannel getNotificationChannel(String pkg, int uid, String channelId, |
| 771 | boolean includeDeleted) { |
| 772 | Preconditions.checkNotNull(pkg); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 773 | synchronized (mPackagePreferences) { |
| 774 | PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid); |
| 775 | if (r == null) { |
| 776 | return null; |
| 777 | } |
| 778 | if (channelId == null) { |
| 779 | channelId = NotificationChannel.DEFAULT_CHANNEL_ID; |
| 780 | } |
| 781 | final NotificationChannel nc = r.channels.get(channelId); |
| 782 | if (nc != null && (includeDeleted || !nc.isDeleted())) { |
| 783 | return nc; |
| 784 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 785 | return null; |
| 786 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | @Override |
| 790 | public void deleteNotificationChannel(String pkg, int uid, String channelId) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 791 | synchronized (mPackagePreferences) { |
| 792 | PackagePreferences r = getPackagePreferencesLocked(pkg, uid); |
| 793 | if (r == null) { |
| 794 | return; |
| 795 | } |
| 796 | NotificationChannel channel = r.channels.get(channelId); |
| 797 | if (channel != null) { |
| 798 | channel.setDeleted(true); |
| 799 | LogMaker lm = getChannelLog(channel, pkg); |
| 800 | lm.setType(com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_CLOSE); |
| 801 | MetricsLogger.action(lm); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 802 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 803 | if (mAreChannelsBypassingDnd && channel.canBypassDnd()) { |
| 804 | updateChannelsBypassingDnd(mContext.getUserId()); |
| 805 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | @Override |
| 811 | @VisibleForTesting |
| 812 | public void permanentlyDeleteNotificationChannel(String pkg, int uid, String channelId) { |
| 813 | Preconditions.checkNotNull(pkg); |
| 814 | Preconditions.checkNotNull(channelId); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 815 | synchronized (mPackagePreferences) { |
| 816 | PackagePreferences r = getPackagePreferencesLocked(pkg, uid); |
| 817 | if (r == null) { |
| 818 | return; |
| 819 | } |
| 820 | r.channels.remove(channelId); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 821 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | @Override |
| 825 | public void permanentlyDeleteNotificationChannels(String pkg, int uid) { |
| 826 | Preconditions.checkNotNull(pkg); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 827 | synchronized (mPackagePreferences) { |
| 828 | PackagePreferences r = getPackagePreferencesLocked(pkg, uid); |
| 829 | if (r == null) { |
| 830 | return; |
| 831 | } |
| 832 | int N = r.channels.size() - 1; |
| 833 | for (int i = N; i >= 0; i--) { |
| 834 | String key = r.channels.keyAt(i); |
| 835 | if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(key)) { |
| 836 | r.channels.remove(key); |
| 837 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | } |
| 841 | |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 842 | public boolean shouldHideSilentStatusIcons() { |
| 843 | return mHideSilentStatusBarIcons; |
| 844 | } |
| 845 | |
| 846 | public void setHideSilentStatusIcons(boolean hide) { |
| 847 | mHideSilentStatusBarIcons = hide; |
| 848 | } |
| 849 | |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 850 | public void lockChannelsForOEM(String[] appOrChannelList) { |
| 851 | if (appOrChannelList == null) { |
| 852 | return; |
| 853 | } |
| 854 | for (String appOrChannel : appOrChannelList) { |
| 855 | if (!TextUtils.isEmpty(appOrChannel)) { |
| 856 | String[] appSplit = appOrChannel.split(NON_BLOCKABLE_CHANNEL_DELIM); |
| 857 | if (appSplit != null && appSplit.length > 0) { |
| 858 | String appName = appSplit[0]; |
| 859 | String channelId = appSplit.length == 2 ? appSplit[1] : null; |
| 860 | |
| 861 | synchronized (mPackagePreferences) { |
| 862 | for (PackagePreferences r : mPackagePreferences.values()) { |
| 863 | if (r.pkg.equals(appName)) { |
| 864 | if (channelId == null) { |
| 865 | // lock all channels for the app |
| 866 | r.oemLockedImportance = true; |
| 867 | for (NotificationChannel channel : r.channels.values()) { |
| 868 | channel.setImportanceLockedByOEM(true); |
| 869 | } |
| 870 | } else { |
| 871 | NotificationChannel channel = r.channels.get(channelId); |
| 872 | if (channel != null) { |
| 873 | channel.setImportanceLockedByOEM(true); |
| 874 | } else { |
| 875 | // if this channel shows up in the future, make sure it'll |
| 876 | // be locked immediately |
| 877 | r.futureOemLockedChannels.add(channelId); |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 888 | public void updateDefaultApps(int userId, ArraySet<String> toRemove, |
| 889 | ArraySet<Pair<String, Integer>> toAdd) { |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 890 | synchronized (mPackagePreferences) { |
| 891 | for (PackagePreferences p : mPackagePreferences.values()) { |
| 892 | if (userId == UserHandle.getUserId(p.uid)) { |
| 893 | if (toRemove != null && toRemove.contains(p.pkg)) { |
| 894 | p.defaultAppLockedImportance = false; |
| 895 | for (NotificationChannel channel : p.channels.values()) { |
| 896 | channel.setImportanceLockedByCriticalDeviceFunction(false); |
| 897 | } |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | } |
| 901 | if (toAdd != null) { |
| 902 | for (Pair<String, Integer> approvedApp : toAdd) { |
| 903 | PackagePreferences p = getOrCreatePackagePreferencesLocked(approvedApp.first, |
| 904 | approvedApp.second); |
| 905 | p.defaultAppLockedImportance = true; |
| 906 | for (NotificationChannel channel : p.channels.values()) { |
| 907 | channel.setImportanceLockedByCriticalDeviceFunction(true); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 914 | public NotificationChannelGroup getNotificationChannelGroupWithChannels(String pkg, |
| 915 | int uid, String groupId, boolean includeDeleted) { |
| 916 | Preconditions.checkNotNull(pkg); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 917 | synchronized (mPackagePreferences) { |
| 918 | PackagePreferences r = getPackagePreferencesLocked(pkg, uid); |
| 919 | if (r == null || groupId == null || !r.groups.containsKey(groupId)) { |
| 920 | return null; |
| 921 | } |
| 922 | NotificationChannelGroup group = r.groups.get(groupId).clone(); |
| 923 | group.setChannels(new ArrayList<>()); |
| 924 | int N = r.channels.size(); |
| 925 | for (int i = 0; i < N; i++) { |
| 926 | final NotificationChannel nc = r.channels.valueAt(i); |
| 927 | if (includeDeleted || !nc.isDeleted()) { |
| 928 | if (groupId.equals(nc.getGroup())) { |
| 929 | group.addChannel(nc); |
| 930 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 931 | } |
| 932 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 933 | return group; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 934 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | public NotificationChannelGroup getNotificationChannelGroup(String groupId, String pkg, |
| 938 | int uid) { |
| 939 | Preconditions.checkNotNull(pkg); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 940 | synchronized (mPackagePreferences) { |
| 941 | PackagePreferences r = getPackagePreferencesLocked(pkg, uid); |
| 942 | if (r == null) { |
| 943 | return null; |
| 944 | } |
| 945 | return r.groups.get(groupId); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 946 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | @Override |
| 950 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups(String pkg, |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 951 | int uid, boolean includeDeleted, boolean includeNonGrouped, boolean includeEmpty) { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 952 | Preconditions.checkNotNull(pkg); |
| 953 | Map<String, NotificationChannelGroup> groups = new ArrayMap<>(); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 954 | synchronized (mPackagePreferences) { |
| 955 | PackagePreferences r = getPackagePreferencesLocked(pkg, uid); |
| 956 | if (r == null) { |
| 957 | return ParceledListSlice.emptyList(); |
| 958 | } |
| 959 | NotificationChannelGroup nonGrouped = new NotificationChannelGroup(null, null); |
| 960 | int N = r.channels.size(); |
| 961 | for (int i = 0; i < N; i++) { |
| 962 | final NotificationChannel nc = r.channels.valueAt(i); |
| 963 | if (includeDeleted || !nc.isDeleted()) { |
| 964 | if (nc.getGroup() != null) { |
| 965 | if (r.groups.get(nc.getGroup()) != null) { |
| 966 | NotificationChannelGroup ncg = groups.get(nc.getGroup()); |
| 967 | if (ncg == null) { |
| 968 | ncg = r.groups.get(nc.getGroup()).clone(); |
| 969 | ncg.setChannels(new ArrayList<>()); |
| 970 | groups.put(nc.getGroup(), ncg); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 971 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 972 | } |
| 973 | ncg.addChannel(nc); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 974 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 975 | } else { |
| 976 | nonGrouped.addChannel(nc); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 977 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 978 | } |
| 979 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 980 | if (includeNonGrouped && nonGrouped.getChannels().size() > 0) { |
| 981 | groups.put(null, nonGrouped); |
| 982 | } |
| 983 | if (includeEmpty) { |
| 984 | for (NotificationChannelGroup group : r.groups.values()) { |
| 985 | if (!groups.containsKey(group.getId())) { |
| 986 | groups.put(group.getId(), group); |
| 987 | } |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 988 | } |
| 989 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 990 | return new ParceledListSlice<>(new ArrayList<>(groups.values())); |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 991 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | public List<NotificationChannel> deleteNotificationChannelGroup(String pkg, int uid, |
| 995 | String groupId) { |
| 996 | List<NotificationChannel> deletedChannels = new ArrayList<>(); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 997 | synchronized (mPackagePreferences) { |
| 998 | PackagePreferences r = getPackagePreferencesLocked(pkg, uid); |
| 999 | if (r == null || TextUtils.isEmpty(groupId)) { |
| 1000 | return deletedChannels; |
| 1001 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1002 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1003 | r.groups.remove(groupId); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1004 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1005 | int N = r.channels.size(); |
| 1006 | for (int i = 0; i < N; i++) { |
| 1007 | final NotificationChannel nc = r.channels.valueAt(i); |
| 1008 | if (groupId.equals(nc.getGroup())) { |
| 1009 | nc.setDeleted(true); |
| 1010 | deletedChannels.add(nc); |
| 1011 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | return deletedChannels; |
| 1015 | } |
| 1016 | |
| 1017 | @Override |
| 1018 | public Collection<NotificationChannelGroup> getNotificationChannelGroups(String pkg, |
| 1019 | int uid) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1020 | List<NotificationChannelGroup> groups = new ArrayList<>(); |
| 1021 | synchronized (mPackagePreferences) { |
| 1022 | PackagePreferences r = getPackagePreferencesLocked(pkg, uid); |
| 1023 | if (r == null) { |
| 1024 | return groups; |
| 1025 | } |
| 1026 | groups.addAll(r.groups.values()); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1027 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1028 | return groups; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | @Override |
| 1032 | public ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg, int uid, |
| 1033 | boolean includeDeleted) { |
| 1034 | Preconditions.checkNotNull(pkg); |
| 1035 | List<NotificationChannel> channels = new ArrayList<>(); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1036 | synchronized (mPackagePreferences) { |
| 1037 | PackagePreferences r = getPackagePreferencesLocked(pkg, uid); |
| 1038 | if (r == null) { |
| 1039 | return ParceledListSlice.emptyList(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1040 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1041 | int N = r.channels.size(); |
| 1042 | for (int i = 0; i < N; i++) { |
| 1043 | final NotificationChannel nc = r.channels.valueAt(i); |
| 1044 | if (includeDeleted || !nc.isDeleted()) { |
| 1045 | channels.add(nc); |
| 1046 | } |
| 1047 | } |
| 1048 | return new ParceledListSlice<>(channels); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1049 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | /** |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 1053 | * Gets all notification channels associated with the given pkg and userId that can bypass dnd |
| 1054 | */ |
| 1055 | public ParceledListSlice<NotificationChannel> getNotificationChannelsBypassingDnd(String pkg, |
| 1056 | int userId) { |
| 1057 | List<NotificationChannel> channels = new ArrayList<>(); |
| 1058 | synchronized (mPackagePreferences) { |
| 1059 | final PackagePreferences r = mPackagePreferences.get( |
| 1060 | packagePreferencesKey(pkg, userId)); |
| 1061 | // notifications from this package aren't blocked |
| 1062 | if (r != null && r.importance != IMPORTANCE_NONE) { |
| 1063 | for (NotificationChannel channel : r.channels.values()) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1064 | if (channelIsLiveLocked(r, channel) && channel.canBypassDnd()) { |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 1065 | channels.add(channel); |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | return new ParceledListSlice<>(channels); |
| 1071 | } |
| 1072 | |
| 1073 | /** |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1074 | * True for pre-O apps that only have the default channel, or pre O apps that have no |
| 1075 | * channels yet. This method will create the default channel for pre-O apps that don't have it. |
| 1076 | * Should never be true for O+ targeting apps, but that's enforced on boot/when an app |
| 1077 | * upgrades. |
| 1078 | */ |
| 1079 | public boolean onlyHasDefaultChannel(String pkg, int uid) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1080 | synchronized (mPackagePreferences) { |
| 1081 | PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid); |
| 1082 | if (r.channels.size() == 1 |
| 1083 | && r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) { |
| 1084 | return true; |
| 1085 | } |
| 1086 | return false; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1087 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | public int getDeletedChannelCount(String pkg, int uid) { |
| 1091 | Preconditions.checkNotNull(pkg); |
| 1092 | int deletedCount = 0; |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1093 | synchronized (mPackagePreferences) { |
| 1094 | PackagePreferences r = getPackagePreferencesLocked(pkg, uid); |
| 1095 | if (r == null) { |
| 1096 | return deletedCount; |
| 1097 | } |
| 1098 | int N = r.channels.size(); |
| 1099 | for (int i = 0; i < N; i++) { |
| 1100 | final NotificationChannel nc = r.channels.valueAt(i); |
| 1101 | if (nc.isDeleted()) { |
| 1102 | deletedCount++; |
| 1103 | } |
| 1104 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1105 | return deletedCount; |
| 1106 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | public int getBlockedChannelCount(String pkg, int uid) { |
| 1110 | Preconditions.checkNotNull(pkg); |
| 1111 | int blockedCount = 0; |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1112 | synchronized (mPackagePreferences) { |
| 1113 | PackagePreferences r = getPackagePreferencesLocked(pkg, uid); |
| 1114 | if (r == null) { |
| 1115 | return blockedCount; |
| 1116 | } |
| 1117 | int N = r.channels.size(); |
| 1118 | for (int i = 0; i < N; i++) { |
| 1119 | final NotificationChannel nc = r.channels.valueAt(i); |
| 1120 | if (!nc.isDeleted() && IMPORTANCE_NONE == nc.getImportance()) { |
| 1121 | blockedCount++; |
| 1122 | } |
| 1123 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1124 | return blockedCount; |
| 1125 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | public int getBlockedAppCount(int userId) { |
| 1129 | int count = 0; |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1130 | synchronized (mPackagePreferences) { |
| 1131 | final int N = mPackagePreferences.size(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1132 | for (int i = 0; i < N; i++) { |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1133 | final PackagePreferences r = mPackagePreferences.valueAt(i); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1134 | if (userId == UserHandle.getUserId(r.uid) |
| 1135 | && r.importance == IMPORTANCE_NONE) { |
| 1136 | count++; |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | return count; |
| 1141 | } |
| 1142 | |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 1143 | /** |
| 1144 | * Returns the number of apps that have at least one notification channel that can bypass DND |
| 1145 | * for given particular user |
| 1146 | */ |
| 1147 | public int getAppsBypassingDndCount(int userId) { |
| 1148 | int count = 0; |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1149 | synchronized (mPackagePreferences) { |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 1150 | final int numPackagePreferences = mPackagePreferences.size(); |
| 1151 | for (int i = 0; i < numPackagePreferences; i++) { |
| 1152 | final PackagePreferences r = mPackagePreferences.valueAt(i); |
| 1153 | // Package isn't associated with this userId or notifications from this package are |
| 1154 | // blocked |
| 1155 | if (userId != UserHandle.getUserId(r.uid) || r.importance == IMPORTANCE_NONE) { |
| 1156 | continue; |
| 1157 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1158 | |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 1159 | for (NotificationChannel channel : r.channels.values()) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1160 | if (channelIsLiveLocked(r, channel) && channel.canBypassDnd()) { |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 1161 | count++; |
| 1162 | break; |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | return count; |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * Syncs {@link #mAreChannelsBypassingDnd} with the user's notification policy before |
| 1172 | * updating |
| 1173 | * @param userId |
| 1174 | */ |
| 1175 | private void syncChannelsBypassingDnd(int userId) { |
| 1176 | mAreChannelsBypassingDnd = (mZenModeHelper.getNotificationPolicy().state |
| 1177 | & NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND) == 1; |
| 1178 | updateChannelsBypassingDnd(userId); |
| 1179 | } |
| 1180 | |
| 1181 | /** |
| 1182 | * Updates the user's NotificationPolicy based on whether the given userId |
| 1183 | * has channels bypassing DND |
| 1184 | * @param userId |
| 1185 | */ |
| 1186 | private void updateChannelsBypassingDnd(int userId) { |
| 1187 | synchronized (mPackagePreferences) { |
| 1188 | final int numPackagePreferences = mPackagePreferences.size(); |
| 1189 | for (int i = 0; i < numPackagePreferences; i++) { |
| 1190 | final PackagePreferences r = mPackagePreferences.valueAt(i); |
| 1191 | // Package isn't associated with this userId or notifications from this package are |
| 1192 | // blocked |
| 1193 | if (userId != UserHandle.getUserId(r.uid) || r.importance == IMPORTANCE_NONE) { |
| 1194 | continue; |
| 1195 | } |
| 1196 | |
| 1197 | for (NotificationChannel channel : r.channels.values()) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1198 | if (channelIsLiveLocked(r, channel) && channel.canBypassDnd()) { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1199 | if (!mAreChannelsBypassingDnd) { |
| 1200 | mAreChannelsBypassingDnd = true; |
| 1201 | updateZenPolicy(true); |
| 1202 | } |
| 1203 | return; |
| 1204 | } |
| 1205 | } |
| 1206 | } |
| 1207 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1208 | // If no channels bypass DND, update the zen policy once to disable DND bypass. |
| 1209 | if (mAreChannelsBypassingDnd) { |
| 1210 | mAreChannelsBypassingDnd = false; |
| 1211 | updateZenPolicy(false); |
| 1212 | } |
| 1213 | } |
| 1214 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1215 | private boolean channelIsLiveLocked(PackagePreferences pkgPref, NotificationChannel channel) { |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 1216 | // Channel is in a group that's blocked |
Beverly | 4f7b53d | 2018-11-20 09:56:31 -0500 | [diff] [blame] | 1217 | if (isGroupBlocked(pkgPref.pkg, pkgPref.uid, channel.getGroup())) { |
| 1218 | return false; |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | // Channel is deleted or is blocked |
| 1222 | if (channel.isDeleted() || channel.getImportance() == IMPORTANCE_NONE) { |
| 1223 | return false; |
| 1224 | } |
| 1225 | |
| 1226 | return true; |
| 1227 | } |
| 1228 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1229 | public void updateZenPolicy(boolean areChannelsBypassingDnd) { |
| 1230 | NotificationManager.Policy policy = mZenModeHelper.getNotificationPolicy(); |
| 1231 | mZenModeHelper.setNotificationPolicy(new NotificationManager.Policy( |
| 1232 | policy.priorityCategories, policy.priorityCallSenders, |
| 1233 | policy.priorityMessageSenders, policy.suppressedVisualEffects, |
| 1234 | (areChannelsBypassingDnd ? NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND |
| 1235 | : 0))); |
| 1236 | } |
| 1237 | |
| 1238 | public boolean areChannelsBypassingDnd() { |
| 1239 | return mAreChannelsBypassingDnd; |
| 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * Sets importance. |
| 1244 | */ |
| 1245 | @Override |
| 1246 | public void setImportance(String pkgName, int uid, int importance) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1247 | synchronized (mPackagePreferences) { |
| 1248 | getOrCreatePackagePreferencesLocked(pkgName, uid).importance = importance; |
| 1249 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1250 | updateConfig(); |
| 1251 | } |
| 1252 | |
| 1253 | public void setEnabled(String packageName, int uid, boolean enabled) { |
| 1254 | boolean wasEnabled = getImportance(packageName, uid) != IMPORTANCE_NONE; |
| 1255 | if (wasEnabled == enabled) { |
| 1256 | return; |
| 1257 | } |
| 1258 | setImportance(packageName, uid, |
| 1259 | enabled ? DEFAULT_IMPORTANCE : IMPORTANCE_NONE); |
| 1260 | } |
| 1261 | |
| 1262 | /** |
| 1263 | * Sets whether any notifications from the app, represented by the given {@code pkgName} and |
| 1264 | * {@code uid}, have their importance locked by the user. Locked notifications don't get |
| 1265 | * considered for sentiment adjustments (and thus never show a blocking helper). |
| 1266 | */ |
| 1267 | public void setAppImportanceLocked(String packageName, int uid) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1268 | synchronized (mPackagePreferences) { |
| 1269 | PackagePreferences prefs = getOrCreatePackagePreferencesLocked(packageName, uid); |
| 1270 | if ((prefs.lockedAppFields & LockableAppFields.USER_LOCKED_IMPORTANCE) != 0) { |
| 1271 | return; |
| 1272 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1273 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1274 | prefs.lockedAppFields = |
| 1275 | prefs.lockedAppFields | LockableAppFields.USER_LOCKED_IMPORTANCE; |
| 1276 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1277 | updateConfig(); |
| 1278 | } |
| 1279 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1280 | /** |
| 1281 | * Returns the delegate for a given package, if it's allowed by the package and the user. |
| 1282 | */ |
| 1283 | public @Nullable String getNotificationDelegate(String sourcePkg, int sourceUid) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1284 | synchronized (mPackagePreferences) { |
| 1285 | PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1286 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1287 | if (prefs == null || prefs.delegate == null) { |
| 1288 | return null; |
| 1289 | } |
| 1290 | if (!prefs.delegate.mUserAllowed || !prefs.delegate.mEnabled) { |
| 1291 | return null; |
| 1292 | } |
| 1293 | return prefs.delegate.mPkg; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1294 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | /** |
| 1298 | * Used by an app to delegate notification posting privileges to another apps. |
| 1299 | */ |
| 1300 | public void setNotificationDelegate(String sourcePkg, int sourceUid, |
| 1301 | String delegatePkg, int delegateUid) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1302 | synchronized (mPackagePreferences) { |
| 1303 | PackagePreferences prefs = getOrCreatePackagePreferencesLocked(sourcePkg, sourceUid); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1304 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1305 | boolean userAllowed = prefs.delegate == null || prefs.delegate.mUserAllowed; |
| 1306 | Delegate delegate = new Delegate(delegatePkg, delegateUid, true, userAllowed); |
| 1307 | prefs.delegate = delegate; |
| 1308 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1309 | updateConfig(); |
| 1310 | } |
| 1311 | |
| 1312 | /** |
| 1313 | * Used by an app to turn off its notification delegate. |
| 1314 | */ |
| 1315 | public void revokeNotificationDelegate(String sourcePkg, int sourceUid) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1316 | boolean changed = false; |
| 1317 | synchronized (mPackagePreferences) { |
| 1318 | PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid); |
| 1319 | if (prefs != null && prefs.delegate != null) { |
| 1320 | prefs.delegate.mEnabled = false; |
| 1321 | changed = true; |
| 1322 | } |
| 1323 | } |
| 1324 | if (changed) { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1325 | updateConfig(); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | /** |
| 1330 | * Toggles whether an app can have a notification delegate on behalf of a user. |
| 1331 | */ |
| 1332 | public void toggleNotificationDelegate(String sourcePkg, int sourceUid, boolean userAllowed) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1333 | boolean changed = false; |
| 1334 | synchronized (mPackagePreferences) { |
| 1335 | PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid); |
| 1336 | if (prefs != null && prefs.delegate != null) { |
| 1337 | prefs.delegate.mUserAllowed = userAllowed; |
| 1338 | changed = true; |
| 1339 | } |
| 1340 | } |
| 1341 | if (changed) { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1342 | updateConfig(); |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | /** |
| 1347 | * Returns whether the given app is allowed on post notifications on behalf of the other given |
| 1348 | * app. |
| 1349 | */ |
| 1350 | public boolean isDelegateAllowed(String sourcePkg, int sourceUid, |
| 1351 | String potentialDelegatePkg, int potentialDelegateUid) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1352 | synchronized (mPackagePreferences) { |
| 1353 | PackagePreferences prefs = getPackagePreferencesLocked(sourcePkg, sourceUid); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1354 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1355 | return prefs != null && prefs.isValidDelegate(potentialDelegatePkg, |
| 1356 | potentialDelegateUid); |
| 1357 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1358 | } |
| 1359 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1360 | @VisibleForTesting |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1361 | void lockFieldsForUpdateLocked(NotificationChannel original, NotificationChannel update) { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1362 | if (original.canBypassDnd() != update.canBypassDnd()) { |
| 1363 | update.lockFields(NotificationChannel.USER_LOCKED_PRIORITY); |
| 1364 | } |
| 1365 | if (original.getLockscreenVisibility() != update.getLockscreenVisibility()) { |
| 1366 | update.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY); |
| 1367 | } |
| 1368 | if (original.getImportance() != update.getImportance()) { |
| 1369 | update.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); |
| 1370 | } |
| 1371 | if (original.shouldShowLights() != update.shouldShowLights() |
| 1372 | || original.getLightColor() != update.getLightColor()) { |
| 1373 | update.lockFields(NotificationChannel.USER_LOCKED_LIGHTS); |
| 1374 | } |
| 1375 | if (!Objects.equals(original.getSound(), update.getSound())) { |
| 1376 | update.lockFields(NotificationChannel.USER_LOCKED_SOUND); |
| 1377 | } |
| 1378 | if (!Arrays.equals(original.getVibrationPattern(), update.getVibrationPattern()) |
| 1379 | || original.shouldVibrate() != update.shouldVibrate()) { |
| 1380 | update.lockFields(NotificationChannel.USER_LOCKED_VIBRATION); |
| 1381 | } |
| 1382 | if (original.canShowBadge() != update.canShowBadge()) { |
| 1383 | update.lockFields(NotificationChannel.USER_LOCKED_SHOW_BADGE); |
| 1384 | } |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 1385 | if (original.canBubble() != update.canBubble()) { |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 1386 | update.lockFields(NotificationChannel.USER_LOCKED_ALLOW_BUBBLE); |
Julia Reynolds | b6bd93d | 2018-10-24 09:22:38 -0400 | [diff] [blame] | 1387 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | public void dump(PrintWriter pw, String prefix, |
| 1391 | @NonNull NotificationManagerService.DumpFilter filter) { |
| 1392 | pw.print(prefix); |
| 1393 | pw.println("per-package config:"); |
| 1394 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1395 | pw.println("PackagePreferences:"); |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1396 | synchronized (mPackagePreferences) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1397 | dumpPackagePreferencesLocked(pw, prefix, filter, mPackagePreferences); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1398 | } |
| 1399 | pw.println("Restored without uid:"); |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1400 | dumpPackagePreferencesLocked(pw, prefix, filter, mRestoredWithoutUids); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | public void dump(ProtoOutputStream proto, |
| 1404 | @NonNull NotificationManagerService.DumpFilter filter) { |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1405 | synchronized (mPackagePreferences) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1406 | dumpPackagePreferencesLocked(proto, RankingHelperProto.RECORDS, filter, |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1407 | mPackagePreferences); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1408 | } |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1409 | dumpPackagePreferencesLocked(proto, RankingHelperProto.RECORDS_RESTORED_WITHOUT_UID, filter, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1410 | mRestoredWithoutUids); |
| 1411 | } |
| 1412 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1413 | private static void dumpPackagePreferencesLocked(PrintWriter pw, String prefix, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1414 | @NonNull NotificationManagerService.DumpFilter filter, |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1415 | ArrayMap<String, PackagePreferences> packagePreferences) { |
| 1416 | final int N = packagePreferences.size(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1417 | for (int i = 0; i < N; i++) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1418 | final PackagePreferences r = packagePreferences.valueAt(i); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1419 | if (filter.matches(r.pkg)) { |
| 1420 | pw.print(prefix); |
| 1421 | pw.print(" AppSettings: "); |
| 1422 | pw.print(r.pkg); |
| 1423 | pw.print(" ("); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1424 | pw.print(r.uid == UNKNOWN_UID ? "UNKNOWN_UID" : Integer.toString(r.uid)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1425 | pw.print(')'); |
| 1426 | if (r.importance != DEFAULT_IMPORTANCE) { |
| 1427 | pw.print(" importance="); |
| 1428 | pw.print(NotificationListenerService.Ranking.importanceToString(r.importance)); |
| 1429 | } |
| 1430 | if (r.priority != DEFAULT_PRIORITY) { |
| 1431 | pw.print(" priority="); |
| 1432 | pw.print(Notification.priorityToString(r.priority)); |
| 1433 | } |
| 1434 | if (r.visibility != DEFAULT_VISIBILITY) { |
| 1435 | pw.print(" visibility="); |
| 1436 | pw.print(Notification.visibilityToString(r.visibility)); |
| 1437 | } |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 1438 | if (r.showBadge != DEFAULT_SHOW_BADGE) { |
| 1439 | pw.print(" showBadge="); |
| 1440 | pw.print(r.showBadge); |
| 1441 | } |
| 1442 | if (r.defaultAppLockedImportance != DEFAULT_APP_LOCKED_IMPORTANCE) { |
| 1443 | pw.print(" defaultAppLocked="); |
| 1444 | pw.print(r.defaultAppLockedImportance); |
| 1445 | } |
| 1446 | if (r.oemLockedImportance != DEFAULT_OEM_LOCKED_IMPORTANCE) { |
| 1447 | pw.print(" oemLocked="); |
| 1448 | pw.print(r.oemLockedImportance); |
| 1449 | } |
| 1450 | if (!r.futureOemLockedChannels.isEmpty()) { |
| 1451 | pw.print(" futureLockedChannels="); |
| 1452 | pw.print(r.futureOemLockedChannels); |
| 1453 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1454 | pw.println(); |
| 1455 | for (NotificationChannel channel : r.channels.values()) { |
| 1456 | pw.print(prefix); |
| 1457 | channel.dump(pw, " ", filter.redact); |
| 1458 | } |
| 1459 | for (NotificationChannelGroup group : r.groups.values()) { |
| 1460 | pw.print(prefix); |
| 1461 | pw.print(" "); |
| 1462 | pw.print(" "); |
| 1463 | pw.println(group); |
| 1464 | } |
| 1465 | } |
| 1466 | } |
| 1467 | } |
| 1468 | |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1469 | private static void dumpPackagePreferencesLocked(ProtoOutputStream proto, long fieldId, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1470 | @NonNull NotificationManagerService.DumpFilter filter, |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1471 | ArrayMap<String, PackagePreferences> packagePreferences) { |
| 1472 | final int N = packagePreferences.size(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1473 | long fToken; |
| 1474 | for (int i = 0; i < N; i++) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1475 | final PackagePreferences r = packagePreferences.valueAt(i); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1476 | if (filter.matches(r.pkg)) { |
| 1477 | fToken = proto.start(fieldId); |
| 1478 | |
| 1479 | proto.write(RankingHelperProto.RecordProto.PACKAGE, r.pkg); |
| 1480 | proto.write(RankingHelperProto.RecordProto.UID, r.uid); |
| 1481 | proto.write(RankingHelperProto.RecordProto.IMPORTANCE, r.importance); |
| 1482 | proto.write(RankingHelperProto.RecordProto.PRIORITY, r.priority); |
| 1483 | proto.write(RankingHelperProto.RecordProto.VISIBILITY, r.visibility); |
| 1484 | proto.write(RankingHelperProto.RecordProto.SHOW_BADGE, r.showBadge); |
| 1485 | |
| 1486 | for (NotificationChannel channel : r.channels.values()) { |
| 1487 | channel.writeToProto(proto, RankingHelperProto.RecordProto.CHANNELS); |
| 1488 | } |
| 1489 | for (NotificationChannelGroup group : r.groups.values()) { |
| 1490 | group.writeToProto(proto, RankingHelperProto.RecordProto.CHANNEL_GROUPS); |
| 1491 | } |
| 1492 | |
| 1493 | proto.end(fToken); |
| 1494 | } |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | public JSONObject dumpJson(NotificationManagerService.DumpFilter filter) { |
| 1499 | JSONObject ranking = new JSONObject(); |
| 1500 | JSONArray PackagePreferencess = new JSONArray(); |
| 1501 | try { |
| 1502 | ranking.put("noUid", mRestoredWithoutUids.size()); |
| 1503 | } catch (JSONException e) { |
| 1504 | // pass |
| 1505 | } |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1506 | synchronized (mPackagePreferences) { |
| 1507 | final int N = mPackagePreferences.size(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1508 | for (int i = 0; i < N; i++) { |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1509 | final PackagePreferences r = mPackagePreferences.valueAt(i); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1510 | if (filter == null || filter.matches(r.pkg)) { |
| 1511 | JSONObject PackagePreferences = new JSONObject(); |
| 1512 | try { |
| 1513 | PackagePreferences.put("userId", UserHandle.getUserId(r.uid)); |
| 1514 | PackagePreferences.put("packageName", r.pkg); |
| 1515 | if (r.importance != DEFAULT_IMPORTANCE) { |
| 1516 | PackagePreferences.put("importance", |
| 1517 | NotificationListenerService.Ranking.importanceToString( |
| 1518 | r.importance)); |
| 1519 | } |
| 1520 | if (r.priority != DEFAULT_PRIORITY) { |
| 1521 | PackagePreferences.put("priority", |
| 1522 | Notification.priorityToString(r.priority)); |
| 1523 | } |
| 1524 | if (r.visibility != DEFAULT_VISIBILITY) { |
| 1525 | PackagePreferences.put("visibility", |
| 1526 | Notification.visibilityToString(r.visibility)); |
| 1527 | } |
| 1528 | if (r.showBadge != DEFAULT_SHOW_BADGE) { |
| 1529 | PackagePreferences.put("showBadge", Boolean.valueOf(r.showBadge)); |
| 1530 | } |
| 1531 | JSONArray channels = new JSONArray(); |
| 1532 | for (NotificationChannel channel : r.channels.values()) { |
| 1533 | channels.put(channel.toJson()); |
| 1534 | } |
| 1535 | PackagePreferences.put("channels", channels); |
| 1536 | JSONArray groups = new JSONArray(); |
| 1537 | for (NotificationChannelGroup group : r.groups.values()) { |
| 1538 | groups.put(group.toJson()); |
| 1539 | } |
| 1540 | PackagePreferences.put("groups", groups); |
| 1541 | } catch (JSONException e) { |
| 1542 | // pass |
| 1543 | } |
| 1544 | PackagePreferencess.put(PackagePreferences); |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | try { |
| 1549 | ranking.put("PackagePreferencess", PackagePreferencess); |
| 1550 | } catch (JSONException e) { |
| 1551 | // pass |
| 1552 | } |
| 1553 | return ranking; |
| 1554 | } |
| 1555 | |
| 1556 | /** |
| 1557 | * Dump only the ban information as structured JSON for the stats collector. |
| 1558 | * |
| 1559 | * This is intentionally redundant with {#link dumpJson} because the old |
| 1560 | * scraper will expect this format. |
| 1561 | * |
| 1562 | * @param filter |
| 1563 | * @return |
| 1564 | */ |
| 1565 | public JSONArray dumpBansJson(NotificationManagerService.DumpFilter filter) { |
| 1566 | JSONArray bans = new JSONArray(); |
| 1567 | Map<Integer, String> packageBans = getPackageBans(); |
| 1568 | for (Map.Entry<Integer, String> ban : packageBans.entrySet()) { |
| 1569 | final int userId = UserHandle.getUserId(ban.getKey()); |
| 1570 | final String packageName = ban.getValue(); |
| 1571 | if (filter == null || filter.matches(packageName)) { |
| 1572 | JSONObject banJson = new JSONObject(); |
| 1573 | try { |
| 1574 | banJson.put("userId", userId); |
| 1575 | banJson.put("packageName", packageName); |
| 1576 | } catch (JSONException e) { |
| 1577 | e.printStackTrace(); |
| 1578 | } |
| 1579 | bans.put(banJson); |
| 1580 | } |
| 1581 | } |
| 1582 | return bans; |
| 1583 | } |
| 1584 | |
| 1585 | public Map<Integer, String> getPackageBans() { |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1586 | synchronized (mPackagePreferences) { |
| 1587 | final int N = mPackagePreferences.size(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1588 | ArrayMap<Integer, String> packageBans = new ArrayMap<>(N); |
| 1589 | for (int i = 0; i < N; i++) { |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1590 | final PackagePreferences r = mPackagePreferences.valueAt(i); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1591 | if (r.importance == IMPORTANCE_NONE) { |
| 1592 | packageBans.put(r.uid, r.pkg); |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | return packageBans; |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | /** |
| 1601 | * Dump only the channel information as structured JSON for the stats collector. |
| 1602 | * |
| 1603 | * This is intentionally redundant with {#link dumpJson} because the old |
| 1604 | * scraper will expect this format. |
| 1605 | * |
| 1606 | * @param filter |
| 1607 | * @return |
| 1608 | */ |
| 1609 | public JSONArray dumpChannelsJson(NotificationManagerService.DumpFilter filter) { |
| 1610 | JSONArray channels = new JSONArray(); |
| 1611 | Map<String, Integer> packageChannels = getPackageChannels(); |
| 1612 | for (Map.Entry<String, Integer> channelCount : packageChannels.entrySet()) { |
| 1613 | final String packageName = channelCount.getKey(); |
| 1614 | if (filter == null || filter.matches(packageName)) { |
| 1615 | JSONObject channelCountJson = new JSONObject(); |
| 1616 | try { |
| 1617 | channelCountJson.put("packageName", packageName); |
| 1618 | channelCountJson.put("channelCount", channelCount.getValue()); |
| 1619 | } catch (JSONException e) { |
| 1620 | e.printStackTrace(); |
| 1621 | } |
| 1622 | channels.put(channelCountJson); |
| 1623 | } |
| 1624 | } |
| 1625 | return channels; |
| 1626 | } |
| 1627 | |
| 1628 | private Map<String, Integer> getPackageChannels() { |
| 1629 | ArrayMap<String, Integer> packageChannels = new ArrayMap<>(); |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1630 | synchronized (mPackagePreferences) { |
| 1631 | for (int i = 0; i < mPackagePreferences.size(); i++) { |
| 1632 | final PackagePreferences r = mPackagePreferences.valueAt(i); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1633 | int channelCount = 0; |
| 1634 | for (int j = 0; j < r.channels.size(); j++) { |
| 1635 | if (!r.channels.valueAt(j).isDeleted()) { |
| 1636 | channelCount++; |
| 1637 | } |
| 1638 | } |
| 1639 | packageChannels.put(r.pkg, channelCount); |
| 1640 | } |
| 1641 | } |
| 1642 | return packageChannels; |
| 1643 | } |
| 1644 | |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 1645 | /** |
| 1646 | * Called when user switches |
| 1647 | */ |
| 1648 | public void onUserSwitched(int userId) { |
| 1649 | syncChannelsBypassingDnd(userId); |
| 1650 | } |
| 1651 | |
| 1652 | /** |
| 1653 | * Called when user is unlocked |
| 1654 | */ |
| 1655 | public void onUserUnlocked(int userId) { |
| 1656 | syncChannelsBypassingDnd(userId); |
| 1657 | } |
| 1658 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1659 | public void onUserRemoved(int userId) { |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1660 | synchronized (mPackagePreferences) { |
| 1661 | int N = mPackagePreferences.size(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1662 | for (int i = N - 1; i >= 0; i--) { |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1663 | PackagePreferences PackagePreferences = mPackagePreferences.valueAt(i); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1664 | if (UserHandle.getUserId(PackagePreferences.uid) == userId) { |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1665 | mPackagePreferences.removeAt(i); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1666 | } |
| 1667 | } |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | protected void onLocaleChanged(Context context, int userId) { |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1672 | synchronized (mPackagePreferences) { |
| 1673 | int N = mPackagePreferences.size(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1674 | for (int i = 0; i < N; i++) { |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1675 | PackagePreferences PackagePreferences = mPackagePreferences.valueAt(i); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1676 | if (UserHandle.getUserId(PackagePreferences.uid) == userId) { |
| 1677 | if (PackagePreferences.channels.containsKey( |
| 1678 | NotificationChannel.DEFAULT_CHANNEL_ID)) { |
| 1679 | PackagePreferences.channels.get( |
| 1680 | NotificationChannel.DEFAULT_CHANNEL_ID).setName( |
| 1681 | context.getResources().getString( |
| 1682 | R.string.default_notification_channel_label)); |
| 1683 | } |
| 1684 | } |
| 1685 | } |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | public void onPackagesChanged(boolean removingPackage, int changeUserId, String[] pkgList, |
| 1690 | int[] uidList) { |
| 1691 | if (pkgList == null || pkgList.length == 0) { |
| 1692 | return; // nothing to do |
| 1693 | } |
| 1694 | boolean updated = false; |
| 1695 | if (removingPackage) { |
| 1696 | // Remove notification settings for uninstalled package |
| 1697 | int size = Math.min(pkgList.length, uidList.length); |
| 1698 | for (int i = 0; i < size; i++) { |
| 1699 | final String pkg = pkgList[i]; |
| 1700 | final int uid = uidList[i]; |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1701 | synchronized (mPackagePreferences) { |
| 1702 | mPackagePreferences.remove(packagePreferencesKey(pkg, uid)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1703 | } |
| 1704 | mRestoredWithoutUids.remove(pkg); |
| 1705 | updated = true; |
| 1706 | } |
| 1707 | } else { |
| 1708 | for (String pkg : pkgList) { |
| 1709 | // Package install |
| 1710 | final PackagePreferences r = mRestoredWithoutUids.get(pkg); |
| 1711 | if (r != null) { |
| 1712 | try { |
| 1713 | r.uid = mPm.getPackageUidAsUser(r.pkg, changeUserId); |
| 1714 | mRestoredWithoutUids.remove(pkg); |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1715 | synchronized (mPackagePreferences) { |
| 1716 | mPackagePreferences.put(packagePreferencesKey(r.pkg, r.uid), r); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1717 | } |
| 1718 | updated = true; |
| 1719 | } catch (PackageManager.NameNotFoundException e) { |
| 1720 | // noop |
| 1721 | } |
| 1722 | } |
| 1723 | // Package upgrade |
| 1724 | try { |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1725 | synchronized (mPackagePreferences) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1726 | PackagePreferences fullPackagePreferences = getPackagePreferencesLocked(pkg, |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1727 | mPm.getPackageUidAsUser(pkg, changeUserId)); |
| 1728 | if (fullPackagePreferences != null) { |
Julia Reynolds | 5c399c6 | 2019-04-08 14:42:53 -0400 | [diff] [blame] | 1729 | createDefaultChannelIfNeededLocked(fullPackagePreferences); |
| 1730 | deleteDefaultChannelIfNeededLocked(fullPackagePreferences); |
Julia Reynolds | b24c62c | 2018-09-10 10:05:15 -0400 | [diff] [blame] | 1731 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1732 | } |
| 1733 | } catch (PackageManager.NameNotFoundException e) { |
| 1734 | } |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | if (updated) { |
| 1739 | updateConfig(); |
| 1740 | } |
| 1741 | } |
| 1742 | |
Julia Reynolds | 7af51c5 | 2019-04-19 11:08:27 -0400 | [diff] [blame] | 1743 | public void clearData(String pkg, int uid) { |
| 1744 | synchronized (mPackagePreferences) { |
| 1745 | PackagePreferences p = getPackagePreferencesLocked(pkg, uid); |
| 1746 | if (p != null) { |
| 1747 | p.channels = new ArrayMap<>(); |
| 1748 | p.groups = new ArrayMap<>(); |
| 1749 | p.delegate = null; |
| 1750 | p.lockedAppFields = DEFAULT_LOCKED_APP_FIELDS; |
| 1751 | p.allowBubble = DEFAULT_ALLOW_BUBBLE; |
| 1752 | p.importance = DEFAULT_IMPORTANCE; |
| 1753 | p.priority = DEFAULT_PRIORITY; |
| 1754 | p.visibility = DEFAULT_VISIBILITY; |
| 1755 | p.showBadge = DEFAULT_SHOW_BADGE; |
| 1756 | } |
| 1757 | } |
| 1758 | } |
| 1759 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1760 | private LogMaker getChannelLog(NotificationChannel channel, String pkg) { |
| 1761 | return new LogMaker( |
| 1762 | com.android.internal.logging.nano.MetricsProto.MetricsEvent |
| 1763 | .ACTION_NOTIFICATION_CHANNEL) |
| 1764 | .setType(com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_UPDATE) |
| 1765 | .setPackageName(pkg) |
| 1766 | .addTaggedData( |
| 1767 | com.android.internal.logging.nano.MetricsProto.MetricsEvent |
| 1768 | .FIELD_NOTIFICATION_CHANNEL_ID, |
| 1769 | channel.getId()) |
| 1770 | .addTaggedData( |
| 1771 | com.android.internal.logging.nano.MetricsProto.MetricsEvent |
| 1772 | .FIELD_NOTIFICATION_CHANNEL_IMPORTANCE, |
| 1773 | channel.getImportance()); |
| 1774 | } |
| 1775 | |
| 1776 | private LogMaker getChannelGroupLog(String groupId, String pkg) { |
| 1777 | return new LogMaker( |
| 1778 | com.android.internal.logging.nano.MetricsProto.MetricsEvent |
| 1779 | .ACTION_NOTIFICATION_CHANNEL_GROUP) |
| 1780 | .setType(com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_UPDATE) |
| 1781 | .addTaggedData( |
| 1782 | com.android.internal.logging.nano.MetricsProto.MetricsEvent |
| 1783 | .FIELD_NOTIFICATION_CHANNEL_GROUP_ID, |
| 1784 | groupId) |
| 1785 | .setPackageName(pkg); |
| 1786 | } |
| 1787 | |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 1788 | public void updateBubblesEnabled() { |
| 1789 | if (mBubblesEnabled == null) { |
| 1790 | mBubblesEnabled = new SparseBooleanArray(); |
| 1791 | } |
| 1792 | boolean changed = false; |
| 1793 | // update the cached values |
| 1794 | for (int index = 0; index < mBubblesEnabled.size(); index++) { |
| 1795 | int userId = mBubblesEnabled.keyAt(index); |
| 1796 | final boolean oldValue = mBubblesEnabled.get(userId); |
| 1797 | final boolean newValue = Settings.Secure.getIntForUser(mContext.getContentResolver(), |
| 1798 | Settings.Secure.NOTIFICATION_BUBBLES, |
| 1799 | DEFAULT_ALLOW_BUBBLE ? 1 : 0, userId) != 0; |
| 1800 | mBubblesEnabled.put(userId, newValue); |
| 1801 | changed |= oldValue != newValue; |
| 1802 | } |
| 1803 | if (changed) { |
| 1804 | updateConfig(); |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | public boolean bubblesEnabled(UserHandle userHandle) { |
| 1809 | int userId = userHandle.getIdentifier(); |
| 1810 | if (userId == UserHandle.USER_ALL) { |
| 1811 | return false; |
| 1812 | } |
| 1813 | if (mBubblesEnabled.indexOfKey(userId) < 0) { |
| 1814 | mBubblesEnabled.put(userId, |
| 1815 | Settings.Secure.getIntForUser(mContext.getContentResolver(), |
| 1816 | Settings.Secure.NOTIFICATION_BUBBLES, |
| 1817 | DEFAULT_ALLOW_BUBBLE ? 1 : 0, userId) != 0); |
| 1818 | } |
| 1819 | return mBubblesEnabled.get(userId, DEFAULT_ALLOW_BUBBLE); |
| 1820 | } |
| 1821 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1822 | |
| 1823 | public void updateBadgingEnabled() { |
| 1824 | if (mBadgingEnabled == null) { |
| 1825 | mBadgingEnabled = new SparseBooleanArray(); |
| 1826 | } |
| 1827 | boolean changed = false; |
| 1828 | // update the cached values |
| 1829 | for (int index = 0; index < mBadgingEnabled.size(); index++) { |
| 1830 | int userId = mBadgingEnabled.keyAt(index); |
| 1831 | final boolean oldValue = mBadgingEnabled.get(userId); |
| 1832 | final boolean newValue = Settings.Secure.getIntForUser(mContext.getContentResolver(), |
| 1833 | Settings.Secure.NOTIFICATION_BADGING, |
| 1834 | DEFAULT_SHOW_BADGE ? 1 : 0, userId) != 0; |
| 1835 | mBadgingEnabled.put(userId, newValue); |
| 1836 | changed |= oldValue != newValue; |
| 1837 | } |
| 1838 | if (changed) { |
| 1839 | updateConfig(); |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | public boolean badgingEnabled(UserHandle userHandle) { |
| 1844 | int userId = userHandle.getIdentifier(); |
| 1845 | if (userId == UserHandle.USER_ALL) { |
| 1846 | return false; |
| 1847 | } |
| 1848 | if (mBadgingEnabled.indexOfKey(userId) < 0) { |
| 1849 | mBadgingEnabled.put(userId, |
| 1850 | Settings.Secure.getIntForUser(mContext.getContentResolver(), |
| 1851 | Settings.Secure.NOTIFICATION_BADGING, |
| 1852 | DEFAULT_SHOW_BADGE ? 1 : 0, userId) != 0); |
| 1853 | } |
| 1854 | return mBadgingEnabled.get(userId, DEFAULT_SHOW_BADGE); |
| 1855 | } |
| 1856 | |
| 1857 | private void updateConfig() { |
| 1858 | mRankingHandler.requestSort(); |
| 1859 | } |
| 1860 | |
| 1861 | private static String packagePreferencesKey(String pkg, int uid) { |
| 1862 | return pkg + "|" + uid; |
| 1863 | } |
| 1864 | |
| 1865 | private static class PackagePreferences { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1866 | String pkg; |
| 1867 | int uid = UNKNOWN_UID; |
| 1868 | int importance = DEFAULT_IMPORTANCE; |
| 1869 | int priority = DEFAULT_PRIORITY; |
| 1870 | int visibility = DEFAULT_VISIBILITY; |
| 1871 | boolean showBadge = DEFAULT_SHOW_BADGE; |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 1872 | boolean allowBubble = DEFAULT_ALLOW_BUBBLE; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1873 | int lockedAppFields = DEFAULT_LOCKED_APP_FIELDS; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 1874 | // these fields are loaded on boot from a different source of truth and so are not |
| 1875 | // written to notification policy xml |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 1876 | boolean oemLockedImportance = DEFAULT_OEM_LOCKED_IMPORTANCE; |
| 1877 | List<String> futureOemLockedChannels = new ArrayList<>(); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 1878 | boolean defaultAppLockedImportance = DEFAULT_APP_LOCKED_IMPORTANCE; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1879 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1880 | Delegate delegate = null; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1881 | ArrayMap<String, NotificationChannel> channels = new ArrayMap<>(); |
| 1882 | Map<String, NotificationChannelGroup> groups = new ConcurrentHashMap<>(); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1883 | |
| 1884 | public boolean isValidDelegate(String pkg, int uid) { |
| 1885 | return delegate != null && delegate.isAllowed(pkg, uid); |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | private static class Delegate { |
| 1890 | static final boolean DEFAULT_ENABLED = true; |
| 1891 | static final boolean DEFAULT_USER_ALLOWED = true; |
| 1892 | String mPkg; |
| 1893 | int mUid = UNKNOWN_UID; |
| 1894 | boolean mEnabled = DEFAULT_ENABLED; |
| 1895 | boolean mUserAllowed = DEFAULT_USER_ALLOWED; |
| 1896 | |
| 1897 | Delegate(String pkg, int uid, boolean enabled, boolean userAllowed) { |
| 1898 | mPkg = pkg; |
| 1899 | mUid = uid; |
| 1900 | mEnabled = enabled; |
| 1901 | mUserAllowed = userAllowed; |
| 1902 | } |
| 1903 | |
| 1904 | public boolean isAllowed(String pkg, int uid) { |
| 1905 | if (pkg == null || uid == UNKNOWN_UID) { |
| 1906 | return false; |
| 1907 | } |
| 1908 | return pkg.equals(mPkg) |
| 1909 | && uid == mUid |
| 1910 | && (mUserAllowed && mEnabled); |
| 1911 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1912 | } |
| 1913 | } |