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