Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2014, 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 | package com.android.server.notification; |
| 17 | |
Julia Reynolds | f2e499d | 2018-03-30 10:36:42 -0400 | [diff] [blame] | 18 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
| 19 | |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 20 | import com.android.internal.R; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 21 | import com.android.internal.annotations.VisibleForTesting; |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 22 | import com.android.internal.logging.MetricsLogger; |
| 23 | import com.android.internal.logging.nano.MetricsProto; |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 24 | import com.android.internal.util.Preconditions; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 25 | import com.android.internal.util.XmlUtils; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 26 | |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 27 | import android.annotation.IntDef; |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 28 | import android.annotation.NonNull; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 29 | import android.app.Notification; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 30 | import android.app.NotificationChannel; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 31 | import android.app.NotificationChannelGroup; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 32 | import android.app.NotificationManager; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 33 | import android.content.Context; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 34 | import android.content.pm.ApplicationInfo; |
Julia Reynolds | bcc62ea | 2018-03-19 10:26:32 -0400 | [diff] [blame] | 35 | import android.content.pm.PackageInfo; |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 36 | import android.content.pm.PackageManager; |
| 37 | import android.content.pm.PackageManager.NameNotFoundException; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 38 | import android.content.pm.ParceledListSlice; |
Julia Reynolds | bcc62ea | 2018-03-19 10:26:32 -0400 | [diff] [blame] | 39 | import android.content.pm.Signature; |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 40 | import android.metrics.LogMaker; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 41 | import android.os.Build; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 42 | import android.os.UserHandle; |
Julia Reynolds | bcc62ea | 2018-03-19 10:26:32 -0400 | [diff] [blame] | 43 | import android.print.PrintManager; |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 44 | import android.provider.Settings.Secure; |
Julia Reynolds | 5d25ee7 | 2015-11-20 15:38:20 -0500 | [diff] [blame] | 45 | import android.service.notification.NotificationListenerService.Ranking; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 46 | import android.service.notification.RankingHelperProto; |
| 47 | import android.service.notification.RankingHelperProto.RecordProto; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 48 | import android.text.TextUtils; |
| 49 | import android.util.ArrayMap; |
Julia Reynolds | bcc62ea | 2018-03-19 10:26:32 -0400 | [diff] [blame] | 50 | import android.util.Pair; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 51 | import android.util.Slog; |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 52 | import android.util.SparseBooleanArray; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 53 | import android.util.proto.ProtoOutputStream; |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 54 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 55 | import org.json.JSONArray; |
| 56 | import org.json.JSONException; |
| 57 | import org.json.JSONObject; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 58 | import org.xmlpull.v1.XmlPullParser; |
| 59 | import org.xmlpull.v1.XmlPullParserException; |
| 60 | import org.xmlpull.v1.XmlSerializer; |
| 61 | |
| 62 | import java.io.IOException; |
| 63 | import java.io.PrintWriter; |
| 64 | import java.util.ArrayList; |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 65 | import java.util.Arrays; |
Julia Reynolds | f02562a | 2017-01-26 13:33:56 -0500 | [diff] [blame] | 66 | import java.util.Collection; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 67 | import java.util.Collections; |
Shunta Sato | 642b8d4 | 2017-06-13 16:16:13 +0900 | [diff] [blame] | 68 | import java.util.concurrent.ConcurrentHashMap; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 69 | import java.util.List; |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 70 | import java.util.Map; |
| 71 | import java.util.Map.Entry; |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 72 | import java.util.Objects; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 73 | |
| 74 | public class RankingHelper implements RankingConfig { |
| 75 | private static final String TAG = "RankingHelper"; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 76 | |
| 77 | private static final int XML_VERSION = 1; |
| 78 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 79 | static final String TAG_RANKING = "ranking"; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 80 | private static final String TAG_PACKAGE = "package"; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 81 | private static final String TAG_CHANNEL = "channel"; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 82 | private static final String TAG_GROUP = "channelGroup"; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 83 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 84 | private static final String ATT_VERSION = "version"; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 85 | private static final String ATT_NAME = "name"; |
| 86 | private static final String ATT_UID = "uid"; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 87 | private static final String ATT_ID = "id"; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 88 | private static final String ATT_PRIORITY = "priority"; |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 89 | private static final String ATT_VISIBILITY = "visibility"; |
Julia Reynolds | 5d25ee7 | 2015-11-20 15:38:20 -0500 | [diff] [blame] | 90 | private static final String ATT_IMPORTANCE = "importance"; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 91 | private static final String ATT_SHOW_BADGE = "show_badge"; |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 92 | private static final String ATT_APP_USER_LOCKED_FIELDS = "app_user_locked_fields"; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 93 | |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 94 | private static final int DEFAULT_PRIORITY = Notification.PRIORITY_DEFAULT; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 95 | private static final int DEFAULT_VISIBILITY = NotificationManager.VISIBILITY_NO_OVERRIDE; |
| 96 | private static final int DEFAULT_IMPORTANCE = NotificationManager.IMPORTANCE_UNSPECIFIED; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 97 | private static final boolean DEFAULT_SHOW_BADGE = true; |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 98 | /** |
| 99 | * Default value for what fields are user locked. See {@link LockableAppFields} for all lockable |
| 100 | * fields. |
| 101 | */ |
| 102 | private static final int DEFAULT_LOCKED_APP_FIELDS = 0; |
| 103 | |
| 104 | /** |
| 105 | * All user-lockable fields for a given application. |
| 106 | */ |
| 107 | @IntDef({LockableAppFields.USER_LOCKED_IMPORTANCE}) |
| 108 | public @interface LockableAppFields { |
| 109 | int USER_LOCKED_IMPORTANCE = 0x00000001; |
| 110 | } |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 111 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 112 | private final NotificationSignalExtractor[] mSignalExtractors; |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 113 | private final NotificationComparator mPreliminaryComparator; |
Christoph Studer | cd4adf8 | 2014-08-19 17:50:49 +0200 | [diff] [blame] | 114 | private final GlobalSortKeyComparator mFinalComparator = new GlobalSortKeyComparator(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 115 | |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 116 | private final ArrayMap<String, Record> mRecords = new ArrayMap<>(); // pkg|uid => Record |
| 117 | private final ArrayMap<String, NotificationRecord> mProxyByGroupTmp = new ArrayMap<>(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 118 | private final ArrayMap<String, Record> mRestoredWithoutUids = new ArrayMap<>(); // pkg => Record |
Julia Reynolds | bcc62ea | 2018-03-19 10:26:32 -0400 | [diff] [blame] | 119 | private final ArrayMap<Pair<String, Integer>, Boolean> mSystemAppCache = new ArrayMap<>(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 120 | |
| 121 | private final Context mContext; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 122 | private final RankingHandler mRankingHandler; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 123 | private final PackageManager mPm; |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 124 | private SparseBooleanArray mBadgingEnabled; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 125 | |
Julia Reynolds | bcc62ea | 2018-03-19 10:26:32 -0400 | [diff] [blame] | 126 | private Signature[] mSystemSignature; |
| 127 | private String mPermissionControllerPackageName; |
| 128 | private String mServicesSystemSharedLibPackageName; |
| 129 | private String mSharedSystemSharedLibPackageName; |
| 130 | |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 131 | public RankingHelper(Context context, PackageManager pm, RankingHandler rankingHandler, |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 132 | ZenModeHelper zenHelper, NotificationUsageStats usageStats, String[] extractorNames) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 133 | mContext = context; |
| 134 | mRankingHandler = rankingHandler; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 135 | mPm = pm; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 136 | |
Julia Reynolds | 4a02afb | 2016-12-13 13:39:52 -0500 | [diff] [blame] | 137 | mPreliminaryComparator = new NotificationComparator(mContext); |
| 138 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 139 | updateBadgingEnabled(); |
| 140 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 141 | final int N = extractorNames.length; |
| 142 | mSignalExtractors = new NotificationSignalExtractor[N]; |
| 143 | for (int i = 0; i < N; i++) { |
| 144 | try { |
| 145 | Class<?> extractorClass = mContext.getClassLoader().loadClass(extractorNames[i]); |
| 146 | NotificationSignalExtractor extractor = |
| 147 | (NotificationSignalExtractor) extractorClass.newInstance(); |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 148 | extractor.initialize(mContext, usageStats); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 149 | extractor.setConfig(this); |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 150 | extractor.setZenHelper(zenHelper); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 151 | mSignalExtractors[i] = extractor; |
| 152 | } catch (ClassNotFoundException e) { |
| 153 | Slog.w(TAG, "Couldn't find extractor " + extractorNames[i] + ".", e); |
| 154 | } catch (InstantiationException e) { |
| 155 | Slog.w(TAG, "Couldn't instantiate extractor " + extractorNames[i] + ".", e); |
| 156 | } catch (IllegalAccessException e) { |
| 157 | Slog.w(TAG, "Problem accessing extractor " + extractorNames[i] + ".", e); |
| 158 | } |
| 159 | } |
Julia Reynolds | bcc62ea | 2018-03-19 10:26:32 -0400 | [diff] [blame] | 160 | |
| 161 | getSignatures(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 162 | } |
| 163 | |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 164 | @SuppressWarnings("unchecked") |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 165 | public <T extends NotificationSignalExtractor> T findExtractor(Class<T> extractorClass) { |
| 166 | final int N = mSignalExtractors.length; |
| 167 | for (int i = 0; i < N; i++) { |
| 168 | final NotificationSignalExtractor extractor = mSignalExtractors[i]; |
| 169 | if (extractorClass.equals(extractor.getClass())) { |
| 170 | return (T) extractor; |
| 171 | } |
| 172 | } |
| 173 | return null; |
| 174 | } |
| 175 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 176 | public void extractSignals(NotificationRecord r) { |
| 177 | final int N = mSignalExtractors.length; |
| 178 | for (int i = 0; i < N; i++) { |
| 179 | NotificationSignalExtractor extractor = mSignalExtractors[i]; |
| 180 | try { |
| 181 | RankingReconsideration recon = extractor.process(r); |
| 182 | if (recon != null) { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 183 | mRankingHandler.requestReconsideration(recon); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 184 | } |
| 185 | } catch (Throwable t) { |
| 186 | Slog.w(TAG, "NotificationSignalExtractor failed.", t); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 191 | public void readXml(XmlPullParser parser, boolean forRestore) |
| 192 | throws XmlPullParserException, IOException { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 193 | int type = parser.getEventType(); |
| 194 | if (type != XmlPullParser.START_TAG) return; |
| 195 | String tag = parser.getName(); |
| 196 | if (!TAG_RANKING.equals(tag)) return; |
Geoffrey Pitsch | 6eccf00 | 2017-04-05 12:33:59 -0400 | [diff] [blame] | 197 | // Clobber groups and channels with the xml, but don't delete other data that wasn't present |
| 198 | // at the time of serialization. |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 199 | mRestoredWithoutUids.clear(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 200 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 201 | tag = parser.getName(); |
| 202 | if (type == XmlPullParser.END_TAG && TAG_RANKING.equals(tag)) { |
| 203 | return; |
| 204 | } |
| 205 | if (type == XmlPullParser.START_TAG) { |
| 206 | if (TAG_PACKAGE.equals(tag)) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 207 | int uid = XmlUtils.readIntAttribute(parser, ATT_UID, Record.UNKNOWN_UID); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 208 | String name = parser.getAttributeValue(null, ATT_NAME); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 209 | if (!TextUtils.isEmpty(name)) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 210 | if (forRestore) { |
| 211 | try { |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 212 | //TODO: http://b/22388012 |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 213 | uid = mPm.getPackageUidAsUser(name, UserHandle.USER_SYSTEM); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 214 | } catch (NameNotFoundException e) { |
| 215 | // noop |
| 216 | } |
| 217 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 218 | |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 219 | Record r = getOrCreateRecord(name, uid, |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 220 | XmlUtils.readIntAttribute( |
| 221 | parser, ATT_IMPORTANCE, DEFAULT_IMPORTANCE), |
| 222 | XmlUtils.readIntAttribute(parser, ATT_PRIORITY, DEFAULT_PRIORITY), |
| 223 | XmlUtils.readIntAttribute( |
| 224 | parser, ATT_VISIBILITY, DEFAULT_VISIBILITY), |
| 225 | XmlUtils.readBooleanAttribute( |
| 226 | parser, ATT_SHOW_BADGE, DEFAULT_SHOW_BADGE)); |
| 227 | r.importance = XmlUtils.readIntAttribute( |
| 228 | parser, ATT_IMPORTANCE, DEFAULT_IMPORTANCE); |
| 229 | r.priority = XmlUtils.readIntAttribute( |
| 230 | parser, ATT_PRIORITY, DEFAULT_PRIORITY); |
| 231 | r.visibility = XmlUtils.readIntAttribute( |
| 232 | parser, ATT_VISIBILITY, DEFAULT_VISIBILITY); |
| 233 | r.showBadge = XmlUtils.readBooleanAttribute( |
| 234 | parser, ATT_SHOW_BADGE, DEFAULT_SHOW_BADGE); |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 235 | r.lockedAppFields = XmlUtils.readIntAttribute(parser, |
| 236 | ATT_APP_USER_LOCKED_FIELDS, DEFAULT_LOCKED_APP_FIELDS); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 237 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 238 | final int innerDepth = parser.getDepth(); |
| 239 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 240 | && (type != XmlPullParser.END_TAG |
| 241 | || parser.getDepth() > innerDepth)) { |
| 242 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 243 | continue; |
| 244 | } |
| 245 | |
| 246 | String tagName = parser.getName(); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 247 | // Channel groups |
| 248 | if (TAG_GROUP.equals(tagName)) { |
| 249 | String id = parser.getAttributeValue(null, ATT_ID); |
| 250 | CharSequence groupName = parser.getAttributeValue(null, ATT_NAME); |
| 251 | if (!TextUtils.isEmpty(id)) { |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 252 | NotificationChannelGroup group |
| 253 | = new NotificationChannelGroup(id, groupName); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 254 | group.populateFromXml(parser); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 255 | r.groups.put(id, group); |
| 256 | } |
| 257 | } |
| 258 | // Channels |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 259 | if (TAG_CHANNEL.equals(tagName)) { |
| 260 | String id = parser.getAttributeValue(null, ATT_ID); |
Julia Reynolds | 2c891c9 | 2017-03-17 14:23:47 -0400 | [diff] [blame] | 261 | String channelName = parser.getAttributeValue(null, ATT_NAME); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 262 | int channelImportance = XmlUtils.readIntAttribute( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 263 | parser, ATT_IMPORTANCE, DEFAULT_IMPORTANCE); |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 264 | if (!TextUtils.isEmpty(id) && !TextUtils.isEmpty(channelName)) { |
| 265 | NotificationChannel channel = new NotificationChannel(id, |
| 266 | channelName, channelImportance); |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 267 | if (forRestore) { |
| 268 | channel.populateFromXmlForRestore(parser, mContext); |
| 269 | } else { |
| 270 | channel.populateFromXml(parser); |
| 271 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 272 | r.channels.put(id, channel); |
| 273 | } |
| 274 | } |
| 275 | } |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 276 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 277 | try { |
| 278 | deleteDefaultChannelIfNeeded(r); |
| 279 | } catch (NameNotFoundException e) { |
| 280 | Slog.e(TAG, "deleteDefaultChannelIfNeeded - Exception: " + e); |
| 281 | } |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | throw new IllegalStateException("Failed to reach END_DOCUMENT"); |
| 287 | } |
| 288 | |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 289 | private static String recordKey(String pkg, int uid) { |
| 290 | return pkg + "|" + uid; |
| 291 | } |
| 292 | |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 293 | private Record getRecord(String pkg, int uid) { |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 294 | final String key = recordKey(pkg, uid); |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 295 | synchronized (mRecords) { |
| 296 | return mRecords.get(key); |
| 297 | } |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | private Record getOrCreateRecord(String pkg, int uid) { |
| 301 | return getOrCreateRecord(pkg, uid, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 302 | DEFAULT_IMPORTANCE, DEFAULT_PRIORITY, DEFAULT_VISIBILITY, DEFAULT_SHOW_BADGE); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | private Record getOrCreateRecord(String pkg, int uid, int importance, int priority, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 306 | int visibility, boolean showBadge) { |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 307 | final String key = recordKey(pkg, uid); |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 308 | synchronized (mRecords) { |
| 309 | Record r = (uid == Record.UNKNOWN_UID) ? mRestoredWithoutUids.get(pkg) : mRecords.get( |
| 310 | key); |
| 311 | if (r == null) { |
| 312 | r = new Record(); |
| 313 | r.pkg = pkg; |
| 314 | r.uid = uid; |
| 315 | r.importance = importance; |
| 316 | r.priority = priority; |
| 317 | r.visibility = visibility; |
| 318 | r.showBadge = showBadge; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 319 | |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 320 | try { |
| 321 | createDefaultChannelIfNeeded(r); |
| 322 | } catch (NameNotFoundException e) { |
| 323 | Slog.e(TAG, "createDefaultChannelIfNeeded - Exception: " + e); |
| 324 | } |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 325 | |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 326 | if (r.uid == Record.UNKNOWN_UID) { |
| 327 | mRestoredWithoutUids.put(pkg, r); |
| 328 | } else { |
| 329 | mRecords.put(key, r); |
| 330 | } |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 331 | } |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 332 | return r; |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 333 | } |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 334 | } |
| 335 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 336 | private boolean shouldHaveDefaultChannel(Record r) throws NameNotFoundException { |
| 337 | final int userId = UserHandle.getUserId(r.uid); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 338 | final ApplicationInfo applicationInfo = mPm.getApplicationInfoAsUser(r.pkg, 0, userId); |
Dan Sandler | e103b78 | 2017-05-17 16:07:56 -0700 | [diff] [blame] | 339 | if (applicationInfo.targetSdkVersion >= Build.VERSION_CODES.O) { |
Geoffrey Pitsch | 5644ccd | 2017-04-17 11:33:24 -0400 | [diff] [blame] | 340 | // O apps should not have the default channel. |
| 341 | return false; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 342 | } |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 343 | |
Geoffrey Pitsch | 5644ccd | 2017-04-17 11:33:24 -0400 | [diff] [blame] | 344 | // Otherwise, this app should have the default channel. |
| 345 | return true; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 346 | } |
| 347 | |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 348 | private void deleteDefaultChannelIfNeeded(Record r) throws NameNotFoundException { |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 349 | if (!r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) { |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 350 | // Not present |
| 351 | return; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 352 | } |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 353 | |
| 354 | if (shouldHaveDefaultChannel(r)) { |
| 355 | // Keep the default channel until upgraded. |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | // Remove Default Channel. |
| 360 | r.channels.remove(NotificationChannel.DEFAULT_CHANNEL_ID); |
| 361 | } |
| 362 | |
| 363 | private void createDefaultChannelIfNeeded(Record r) throws NameNotFoundException { |
| 364 | if (r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) { |
Julia Reynolds | 17717f5 | 2017-05-09 11:46:06 -0400 | [diff] [blame] | 365 | r.channels.get(NotificationChannel.DEFAULT_CHANNEL_ID).setName( |
| 366 | mContext.getString(R.string.default_notification_channel_label)); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 367 | return; |
| 368 | } |
| 369 | |
| 370 | if (!shouldHaveDefaultChannel(r)) { |
| 371 | // Keep the default channel until upgraded. |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | // Create Default Channel |
| 376 | NotificationChannel channel; |
| 377 | channel = new NotificationChannel( |
| 378 | NotificationChannel.DEFAULT_CHANNEL_ID, |
| 379 | mContext.getString(R.string.default_notification_channel_label), |
| 380 | r.importance); |
| 381 | channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX); |
| 382 | channel.setLockscreenVisibility(r.visibility); |
| 383 | if (r.importance != NotificationManager.IMPORTANCE_UNSPECIFIED) { |
| 384 | channel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); |
| 385 | } |
| 386 | if (r.priority != DEFAULT_PRIORITY) { |
| 387 | channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY); |
| 388 | } |
| 389 | if (r.visibility != DEFAULT_VISIBILITY) { |
| 390 | channel.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY); |
| 391 | } |
| 392 | r.channels.put(channel.getId(), channel); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 393 | } |
| 394 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 395 | public void writeXml(XmlSerializer out, boolean forBackup) throws IOException { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 396 | out.startTag(null, TAG_RANKING); |
| 397 | out.attribute(null, ATT_VERSION, Integer.toString(XML_VERSION)); |
| 398 | |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 399 | synchronized (mRecords) { |
| 400 | final int N = mRecords.size(); |
| 401 | for (int i = 0; i < N; i++) { |
| 402 | final Record r = mRecords.valueAt(i); |
| 403 | //TODO: http://b/22388012 |
| 404 | if (forBackup && UserHandle.getUserId(r.uid) != UserHandle.USER_SYSTEM) { |
| 405 | continue; |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 406 | } |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 407 | final boolean hasNonDefaultSettings = |
| 408 | r.importance != DEFAULT_IMPORTANCE |
| 409 | || r.priority != DEFAULT_PRIORITY |
| 410 | || r.visibility != DEFAULT_VISIBILITY |
| 411 | || r.showBadge != DEFAULT_SHOW_BADGE |
| 412 | || r.lockedAppFields != DEFAULT_LOCKED_APP_FIELDS |
| 413 | || r.channels.size() > 0 |
| 414 | || r.groups.size() > 0; |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 415 | if (hasNonDefaultSettings) { |
| 416 | out.startTag(null, TAG_PACKAGE); |
| 417 | out.attribute(null, ATT_NAME, r.pkg); |
| 418 | if (r.importance != DEFAULT_IMPORTANCE) { |
| 419 | out.attribute(null, ATT_IMPORTANCE, Integer.toString(r.importance)); |
Julia Reynolds | d0a5b16 | 2017-03-18 13:42:09 -0400 | [diff] [blame] | 420 | } |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 421 | if (r.priority != DEFAULT_PRIORITY) { |
| 422 | out.attribute(null, ATT_PRIORITY, Integer.toString(r.priority)); |
| 423 | } |
| 424 | if (r.visibility != DEFAULT_VISIBILITY) { |
| 425 | out.attribute(null, ATT_VISIBILITY, Integer.toString(r.visibility)); |
| 426 | } |
| 427 | out.attribute(null, ATT_SHOW_BADGE, Boolean.toString(r.showBadge)); |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 428 | out.attribute(null, ATT_APP_USER_LOCKED_FIELDS, |
| 429 | Integer.toString(r.lockedAppFields)); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 430 | |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 431 | if (!forBackup) { |
| 432 | out.attribute(null, ATT_UID, Integer.toString(r.uid)); |
| 433 | } |
| 434 | |
| 435 | for (NotificationChannelGroup group : r.groups.values()) { |
| 436 | group.writeXml(out); |
| 437 | } |
| 438 | |
| 439 | for (NotificationChannel channel : r.channels.values()) { |
Bernardo Rufino | c27bb6a | 2017-10-03 13:55:10 +0100 | [diff] [blame] | 440 | if (forBackup) { |
| 441 | if (!channel.isDeleted()) { |
| 442 | channel.writeXmlForBackup(out, mContext); |
| 443 | } |
| 444 | } else { |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 445 | channel.writeXml(out); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | out.endTag(null, TAG_PACKAGE); |
| 450 | } |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 451 | } |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 452 | } |
| 453 | out.endTag(null, TAG_RANKING); |
| 454 | } |
| 455 | |
| 456 | private void updateConfig() { |
| 457 | final int N = mSignalExtractors.length; |
| 458 | for (int i = 0; i < N; i++) { |
| 459 | mSignalExtractors[i].setConfig(this); |
| 460 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 461 | mRankingHandler.requestSort(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | public void sort(ArrayList<NotificationRecord> notificationList) { |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 465 | final int N = notificationList.size(); |
Christoph Studer | cd4adf8 | 2014-08-19 17:50:49 +0200 | [diff] [blame] | 466 | // clear global sort keys |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 467 | for (int i = N - 1; i >= 0; i--) { |
Christoph Studer | cd4adf8 | 2014-08-19 17:50:49 +0200 | [diff] [blame] | 468 | notificationList.get(i).setGlobalSortKey(null); |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Christoph Studer | 8537405 | 2014-10-08 11:19:55 -0700 | [diff] [blame] | 471 | // rank each record individually |
| 472 | Collections.sort(notificationList, mPreliminaryComparator); |
Christoph Studer | cd4adf8 | 2014-08-19 17:50:49 +0200 | [diff] [blame] | 473 | |
| 474 | synchronized (mProxyByGroupTmp) { |
| 475 | // record individual ranking result and nominate proxies for each group |
| 476 | for (int i = N - 1; i >= 0; i--) { |
| 477 | final NotificationRecord record = notificationList.get(i); |
| 478 | record.setAuthoritativeRank(i); |
| 479 | final String groupKey = record.getGroupKey(); |
Julia Reynolds | 3fb989b | 2017-02-28 16:06:55 -0500 | [diff] [blame] | 480 | NotificationRecord existingProxy = mProxyByGroupTmp.get(groupKey); |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 481 | if (existingProxy == null) { |
Christoph Studer | cd4adf8 | 2014-08-19 17:50:49 +0200 | [diff] [blame] | 482 | mProxyByGroupTmp.put(groupKey, record); |
| 483 | } |
| 484 | } |
| 485 | // assign global sort key: |
| 486 | // is_recently_intrusive:group_rank:is_group_summary:group_sort_key:rank |
| 487 | for (int i = 0; i < N; i++) { |
| 488 | final NotificationRecord record = notificationList.get(i); |
| 489 | NotificationRecord groupProxy = mProxyByGroupTmp.get(record.getGroupKey()); |
| 490 | String groupSortKey = record.getNotification().getSortKey(); |
| 491 | |
| 492 | // We need to make sure the developer provided group sort key (gsk) is handled |
| 493 | // correctly: |
| 494 | // gsk="" < gsk=non-null-string < gsk=null |
| 495 | // |
| 496 | // We enforce this by using different prefixes for these three cases. |
| 497 | String groupSortKeyPortion; |
| 498 | if (groupSortKey == null) { |
| 499 | groupSortKeyPortion = "nsk"; |
| 500 | } else if (groupSortKey.equals("")) { |
| 501 | groupSortKeyPortion = "esk"; |
| 502 | } else { |
| 503 | groupSortKeyPortion = "gsk=" + groupSortKey; |
| 504 | } |
| 505 | |
| 506 | boolean isGroupSummary = record.getNotification().isGroupSummary(); |
| 507 | record.setGlobalSortKey( |
| 508 | String.format("intrsv=%c:grnk=0x%04x:gsmry=%c:%s:rnk=0x%04x", |
Selim Cinek | 55a3e73 | 2017-05-25 18:30:10 -0700 | [diff] [blame] | 509 | record.isRecentlyIntrusive() |
| 510 | && record.getImportance() > NotificationManager.IMPORTANCE_MIN |
| 511 | ? '0' : '1', |
Christoph Studer | cd4adf8 | 2014-08-19 17:50:49 +0200 | [diff] [blame] | 512 | groupProxy.getAuthoritativeRank(), |
| 513 | isGroupSummary ? '0' : '1', |
| 514 | groupSortKeyPortion, |
| 515 | record.getAuthoritativeRank())); |
| 516 | } |
| 517 | mProxyByGroupTmp.clear(); |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 518 | } |
Christoph Studer | cd4adf8 | 2014-08-19 17:50:49 +0200 | [diff] [blame] | 519 | |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 520 | // Do a second ranking pass, using group proxies |
| 521 | Collections.sort(notificationList, mFinalComparator); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | public int indexOf(ArrayList<NotificationRecord> notificationList, NotificationRecord target) { |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 525 | return Collections.binarySearch(notificationList, target, mFinalComparator); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 526 | } |
| 527 | |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 528 | /** |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 529 | * Gets importance. |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 530 | */ |
| 531 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 532 | public int getImportance(String packageName, int uid) { |
| 533 | return getOrCreateRecord(packageName, uid).importance; |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 534 | } |
| 535 | |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 536 | |
| 537 | /** |
| 538 | * Returns whether the importance of the corresponding notification is user-locked and shouldn't |
| 539 | * be adjusted by an assistant (via means of a blocking helper, for example). For the channel |
| 540 | * locking field, see {@link NotificationChannel#USER_LOCKED_IMPORTANCE}. |
| 541 | */ |
| 542 | public boolean getIsAppImportanceLocked(String packageName, int uid) { |
| 543 | int userLockedFields = getOrCreateRecord(packageName, uid).lockedAppFields; |
| 544 | return (userLockedFields & LockableAppFields.USER_LOCKED_IMPORTANCE) != 0; |
| 545 | } |
| 546 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 547 | @Override |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 548 | public boolean canShowBadge(String packageName, int uid) { |
| 549 | return getOrCreateRecord(packageName, uid).showBadge; |
| 550 | } |
| 551 | |
| 552 | @Override |
| 553 | public void setShowBadge(String packageName, int uid, boolean showBadge) { |
| 554 | getOrCreateRecord(packageName, uid).showBadge = showBadge; |
| 555 | updateConfig(); |
| 556 | } |
| 557 | |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 558 | @Override |
| 559 | public boolean isGroupBlocked(String packageName, int uid, String groupId) { |
| 560 | if (groupId == null) { |
| 561 | return false; |
| 562 | } |
| 563 | Record r = getOrCreateRecord(packageName, uid); |
| 564 | NotificationChannelGroup group = r.groups.get(groupId); |
| 565 | if (group == null) { |
| 566 | return false; |
| 567 | } |
| 568 | return group.isBlocked(); |
| 569 | } |
| 570 | |
Julia Reynolds | fa04e14 | 2017-04-23 13:32:01 -0400 | [diff] [blame] | 571 | int getPackagePriority(String pkg, int uid) { |
| 572 | return getOrCreateRecord(pkg, uid).priority; |
| 573 | } |
| 574 | |
| 575 | int getPackageVisibility(String pkg, int uid) { |
| 576 | return getOrCreateRecord(pkg, uid).visibility; |
| 577 | } |
| 578 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 579 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 580 | public void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group, |
| 581 | boolean fromTargetApp) { |
| 582 | Preconditions.checkNotNull(pkg); |
| 583 | Preconditions.checkNotNull(group); |
| 584 | Preconditions.checkNotNull(group.getId()); |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 585 | Preconditions.checkNotNull(!TextUtils.isEmpty(group.getName())); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 586 | Record r = getOrCreateRecord(pkg, uid); |
| 587 | if (r == null) { |
| 588 | throw new IllegalArgumentException("Invalid package"); |
| 589 | } |
Dan Sandler | 0171f57 | 2017-06-14 14:03:18 -0400 | [diff] [blame] | 590 | final NotificationChannelGroup oldGroup = r.groups.get(group.getId()); |
| 591 | if (!group.equals(oldGroup)) { |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 592 | // will log for new entries as well as name/description changes |
Dan Sandler | 0171f57 | 2017-06-14 14:03:18 -0400 | [diff] [blame] | 593 | MetricsLogger.action(getChannelGroupLog(group.getId(), pkg)); |
| 594 | } |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 595 | if (oldGroup != null) { |
| 596 | group.setChannels(oldGroup.getChannels()); |
| 597 | |
| 598 | if (fromTargetApp) { |
| 599 | group.setBlocked(oldGroup.isBlocked()); |
| 600 | } |
| 601 | } |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 602 | r.groups.put(group.getId(), group); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | @Override |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 606 | public void createNotificationChannel(String pkg, int uid, NotificationChannel channel, |
Julia Reynolds | 1fe1094 | 2018-03-28 12:46:51 -0400 | [diff] [blame] | 607 | boolean fromTargetApp, boolean hasDndAccess) { |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 608 | Preconditions.checkNotNull(pkg); |
| 609 | Preconditions.checkNotNull(channel); |
| 610 | Preconditions.checkNotNull(channel.getId()); |
Julia Reynolds | 1d97e6a | 2017-03-13 15:05:40 -0400 | [diff] [blame] | 611 | Preconditions.checkArgument(!TextUtils.isEmpty(channel.getName())); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 612 | Record r = getOrCreateRecord(pkg, uid); |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 613 | if (r == null) { |
| 614 | throw new IllegalArgumentException("Invalid package"); |
| 615 | } |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 616 | if (channel.getGroup() != null && !r.groups.containsKey(channel.getGroup())) { |
| 617 | throw new IllegalArgumentException("NotificationChannelGroup doesn't exist"); |
| 618 | } |
Julia Reynolds | 03fa85d | 2017-03-06 15:14:50 -0500 | [diff] [blame] | 619 | if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channel.getId())) { |
| 620 | throw new IllegalArgumentException("Reserved id"); |
| 621 | } |
Julia Reynolds | bcc62ea | 2018-03-19 10:26:32 -0400 | [diff] [blame] | 622 | final boolean isSystemApp = isSystemPackage(pkg, uid); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 623 | NotificationChannel existing = r.channels.get(channel.getId()); |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 624 | // Keep most of the existing settings |
Julia Reynolds | 3d91f11 | 2017-03-03 08:59:53 -0500 | [diff] [blame] | 625 | if (existing != null && fromTargetApp) { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 626 | if (existing.isDeleted()) { |
| 627 | existing.setDeleted(false); |
Dan Sandler | 0171f57 | 2017-06-14 14:03:18 -0400 | [diff] [blame] | 628 | |
| 629 | // log a resurrected channel as if it's new again |
| 630 | MetricsLogger.action(getChannelLog(channel, pkg).setType( |
| 631 | MetricsProto.MetricsEvent.TYPE_OPEN)); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 632 | } |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 633 | |
Julia Reynolds | 2c891c9 | 2017-03-17 14:23:47 -0400 | [diff] [blame] | 634 | existing.setName(channel.getName().toString()); |
| 635 | existing.setDescription(channel.getDescription()); |
Julia Reynolds | f732159 | 2017-05-24 16:09:19 -0400 | [diff] [blame] | 636 | existing.setBlockableSystem(channel.isBlockableSystem()); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 637 | if (existing.getGroup() == null) { |
| 638 | existing.setGroup(channel.getGroup()); |
| 639 | } |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 640 | |
Geoffrey Pitsch | 76a3aa0 | 2017-07-26 15:07:34 -0400 | [diff] [blame] | 641 | // Apps are allowed to downgrade channel importance if the user has not changed any |
| 642 | // fields on this channel yet. |
| 643 | if (existing.getUserLockedFields() == 0 && |
| 644 | channel.getImportance() < existing.getImportance()) { |
| 645 | existing.setImportance(channel.getImportance()); |
| 646 | } |
| 647 | |
Julia Reynolds | 1fe1094 | 2018-03-28 12:46:51 -0400 | [diff] [blame] | 648 | // system apps and dnd access apps can bypass dnd if the user hasn't changed any |
| 649 | // fields on the channel yet |
| 650 | if (existing.getUserLockedFields() == 0 && (isSystemApp || hasDndAccess)) { |
Julia Reynolds | bcc62ea | 2018-03-19 10:26:32 -0400 | [diff] [blame] | 651 | existing.setBypassDnd(channel.canBypassDnd()); |
| 652 | } |
| 653 | |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 654 | updateConfig(); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 655 | return; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 656 | } |
Julia Reynolds | f2e499d | 2018-03-30 10:36:42 -0400 | [diff] [blame] | 657 | if (channel.getImportance() < IMPORTANCE_NONE |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 658 | || channel.getImportance() > NotificationManager.IMPORTANCE_MAX) { |
| 659 | throw new IllegalArgumentException("Invalid importance level"); |
| 660 | } |
Julia Reynolds | bcc62ea | 2018-03-19 10:26:32 -0400 | [diff] [blame] | 661 | |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 662 | // Reset fields that apps aren't allowed to set. |
Julia Reynolds | 1fe1094 | 2018-03-28 12:46:51 -0400 | [diff] [blame] | 663 | if (fromTargetApp && !(isSystemApp || hasDndAccess)) { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 664 | channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX); |
Julia Reynolds | bcc62ea | 2018-03-19 10:26:32 -0400 | [diff] [blame] | 665 | } |
| 666 | if (fromTargetApp) { |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 667 | channel.setLockscreenVisibility(r.visibility); |
| 668 | } |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 669 | clearLockedFields(channel); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 670 | if (channel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) { |
| 671 | channel.setLockscreenVisibility(Ranking.VISIBILITY_NO_OVERRIDE); |
| 672 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 673 | if (!r.showBadge) { |
| 674 | channel.setShowBadge(false); |
| 675 | } |
Julia Reynolds | bcc62ea | 2018-03-19 10:26:32 -0400 | [diff] [blame] | 676 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 677 | r.channels.put(channel.getId(), channel); |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 678 | MetricsLogger.action(getChannelLog(channel, pkg).setType( |
| 679 | MetricsProto.MetricsEvent.TYPE_OPEN)); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 680 | } |
| 681 | |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 682 | void clearLockedFields(NotificationChannel channel) { |
| 683 | channel.unlockFields(channel.getUserLockedFields()); |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 684 | } |
| 685 | |
Julia Reynolds | bcc62ea | 2018-03-19 10:26:32 -0400 | [diff] [blame] | 686 | /** |
| 687 | * Determine whether a package is a "system package", in which case certain things (like |
| 688 | * bypassing DND) should be allowed. |
| 689 | */ |
| 690 | private boolean isSystemPackage(String pkg, int uid) { |
| 691 | Pair<String, Integer> app = new Pair(pkg, uid); |
| 692 | if (mSystemAppCache.containsKey(app)) { |
| 693 | return mSystemAppCache.get(app); |
| 694 | } |
| 695 | |
| 696 | PackageInfo pi; |
| 697 | try { |
| 698 | pi = mPm.getPackageInfoAsUser( |
| 699 | pkg, PackageManager.GET_SIGNATURES, UserHandle.getUserId(uid)); |
| 700 | } catch (NameNotFoundException e) { |
| 701 | Slog.w(TAG, "Can't find pkg", e); |
| 702 | return false; |
| 703 | } |
| 704 | boolean isSystem = (mSystemSignature[0] != null |
| 705 | && mSystemSignature[0].equals(getFirstSignature(pi))) |
| 706 | || pkg.equals(mPermissionControllerPackageName) |
| 707 | || pkg.equals(mServicesSystemSharedLibPackageName) |
| 708 | || pkg.equals(mSharedSystemSharedLibPackageName) |
| 709 | || pkg.equals(PrintManager.PRINT_SPOOLER_PACKAGE_NAME) |
| 710 | || isDeviceProvisioningPackage(pkg); |
| 711 | mSystemAppCache.put(app, isSystem); |
| 712 | return isSystem; |
| 713 | } |
| 714 | |
| 715 | private Signature getFirstSignature(PackageInfo pkg) { |
| 716 | if (pkg != null && pkg.signatures != null && pkg.signatures.length > 0) { |
| 717 | return pkg.signatures[0]; |
| 718 | } |
| 719 | return null; |
| 720 | } |
| 721 | |
| 722 | private Signature getSystemSignature() { |
| 723 | try { |
| 724 | final PackageInfo sys = mPm.getPackageInfoAsUser( |
| 725 | "android", PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM); |
| 726 | return getFirstSignature(sys); |
| 727 | } catch (NameNotFoundException e) { |
| 728 | } |
| 729 | return null; |
| 730 | } |
| 731 | |
| 732 | private boolean isDeviceProvisioningPackage(String packageName) { |
| 733 | String deviceProvisioningPackage = mContext.getResources().getString( |
| 734 | com.android.internal.R.string.config_deviceProvisioningPackage); |
| 735 | return deviceProvisioningPackage != null && deviceProvisioningPackage.equals(packageName); |
| 736 | } |
| 737 | |
| 738 | private void getSignatures() { |
| 739 | mSystemSignature = new Signature[]{getSystemSignature()}; |
| 740 | mPermissionControllerPackageName = mPm.getPermissionControllerPackageName(); |
| 741 | mServicesSystemSharedLibPackageName = mPm.getServicesSystemSharedLibraryPackageName(); |
| 742 | mSharedSystemSharedLibPackageName = mPm.getSharedSystemSharedLibraryPackageName(); |
| 743 | } |
| 744 | |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 745 | @Override |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 746 | public void updateNotificationChannel(String pkg, int uid, NotificationChannel updatedChannel, |
| 747 | boolean fromUser) { |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 748 | Preconditions.checkNotNull(updatedChannel); |
| 749 | Preconditions.checkNotNull(updatedChannel.getId()); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 750 | Record r = getOrCreateRecord(pkg, uid); |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 751 | if (r == null) { |
| 752 | throw new IllegalArgumentException("Invalid package"); |
| 753 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 754 | NotificationChannel channel = r.channels.get(updatedChannel.getId()); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 755 | if (channel == null || channel.isDeleted()) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 756 | throw new IllegalArgumentException("Channel does not exist"); |
| 757 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 758 | if (updatedChannel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) { |
| 759 | updatedChannel.setLockscreenVisibility(Ranking.VISIBILITY_NO_OVERRIDE); |
| 760 | } |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 761 | if (!fromUser) { |
| 762 | updatedChannel.unlockFields(updatedChannel.getUserLockedFields()); |
| 763 | } |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 764 | if (fromUser) { |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 765 | updatedChannel.lockFields(channel.getUserLockedFields()); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 766 | lockFieldsForUpdate(channel, updatedChannel); |
| 767 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 768 | r.channels.put(updatedChannel.getId(), updatedChannel); |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 769 | |
Julia Reynolds | fa04e14 | 2017-04-23 13:32:01 -0400 | [diff] [blame] | 770 | if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(updatedChannel.getId())) { |
| 771 | // copy settings to app level so they are inherited by new channels |
| 772 | // when the app migrates |
| 773 | r.importance = updatedChannel.getImportance(); |
| 774 | r.priority = updatedChannel.canBypassDnd() |
| 775 | ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT; |
| 776 | r.visibility = updatedChannel.getLockscreenVisibility(); |
| 777 | r.showBadge = updatedChannel.canShowBadge(); |
| 778 | } |
| 779 | |
Dan Sandler | 0171f57 | 2017-06-14 14:03:18 -0400 | [diff] [blame] | 780 | if (!channel.equals(updatedChannel)) { |
| 781 | // only log if there are real changes |
| 782 | MetricsLogger.action(getChannelLog(updatedChannel, pkg)); |
| 783 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 784 | updateConfig(); |
| 785 | } |
| 786 | |
| 787 | @Override |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 788 | public NotificationChannel getNotificationChannel(String pkg, int uid, String channelId, |
| 789 | boolean includeDeleted) { |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 790 | Preconditions.checkNotNull(pkg); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 791 | Record r = getOrCreateRecord(pkg, uid); |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 792 | if (r == null) { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 793 | return null; |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 794 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 795 | if (channelId == null) { |
| 796 | channelId = NotificationChannel.DEFAULT_CHANNEL_ID; |
| 797 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 798 | final NotificationChannel nc = r.channels.get(channelId); |
| 799 | if (nc != null && (includeDeleted || !nc.isDeleted())) { |
| 800 | return nc; |
| 801 | } |
| 802 | return null; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | @Override |
| 806 | public void deleteNotificationChannel(String pkg, int uid, String channelId) { |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 807 | Record r = getRecord(pkg, uid); |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 808 | if (r == null) { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 809 | return; |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 810 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 811 | NotificationChannel channel = r.channels.get(channelId); |
| 812 | if (channel != null) { |
| 813 | channel.setDeleted(true); |
Julia Reynolds | 8e0eb37 | 2017-03-21 15:04:50 -0400 | [diff] [blame] | 814 | LogMaker lm = getChannelLog(channel, pkg); |
| 815 | lm.setType(MetricsProto.MetricsEvent.TYPE_CLOSE); |
| 816 | MetricsLogger.action(lm); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 817 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | @Override |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 821 | @VisibleForTesting |
| 822 | public void permanentlyDeleteNotificationChannel(String pkg, int uid, String channelId) { |
| 823 | Preconditions.checkNotNull(pkg); |
| 824 | Preconditions.checkNotNull(channelId); |
| 825 | Record r = getRecord(pkg, uid); |
| 826 | if (r == null) { |
| 827 | return; |
| 828 | } |
| 829 | r.channels.remove(channelId); |
| 830 | } |
| 831 | |
| 832 | @Override |
| 833 | public void permanentlyDeleteNotificationChannels(String pkg, int uid) { |
| 834 | Preconditions.checkNotNull(pkg); |
| 835 | Record r = getRecord(pkg, uid); |
| 836 | if (r == null) { |
| 837 | return; |
| 838 | } |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 839 | int N = r.channels.size() - 1; |
| 840 | for (int i = N; i >= 0; i--) { |
| 841 | String key = r.channels.keyAt(i); |
| 842 | if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(key)) { |
| 843 | r.channels.remove(key); |
| 844 | } |
| 845 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 846 | } |
| 847 | |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 848 | public NotificationChannelGroup getNotificationChannelGroupWithChannels(String pkg, |
| 849 | int uid, String groupId, boolean includeDeleted) { |
| 850 | Preconditions.checkNotNull(pkg); |
| 851 | Record r = getRecord(pkg, uid); |
| 852 | if (r == null || groupId == null || !r.groups.containsKey(groupId)) { |
| 853 | return null; |
| 854 | } |
| 855 | NotificationChannelGroup group = r.groups.get(groupId).clone(); |
| 856 | group.setChannels(new ArrayList<>()); |
| 857 | int N = r.channels.size(); |
| 858 | for (int i = 0; i < N; i++) { |
| 859 | final NotificationChannel nc = r.channels.valueAt(i); |
| 860 | if (includeDeleted || !nc.isDeleted()) { |
| 861 | if (groupId.equals(nc.getGroup())) { |
| 862 | group.addChannel(nc); |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | return group; |
| 867 | } |
| 868 | |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 869 | public NotificationChannelGroup getNotificationChannelGroup(String groupId, String pkg, |
| 870 | int uid) { |
| 871 | Preconditions.checkNotNull(pkg); |
| 872 | Record r = getRecord(pkg, uid); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 873 | if (r == null) { |
| 874 | return null; |
| 875 | } |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 876 | return r.groups.get(groupId); |
| 877 | } |
| 878 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 879 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 880 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups(String pkg, |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 881 | int uid, boolean includeDeleted, boolean includeNonGrouped) { |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 882 | Preconditions.checkNotNull(pkg); |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 883 | Map<String, NotificationChannelGroup> groups = new ArrayMap<>(); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 884 | Record r = getRecord(pkg, uid); |
| 885 | if (r == null) { |
| 886 | return ParceledListSlice.emptyList(); |
| 887 | } |
| 888 | NotificationChannelGroup nonGrouped = new NotificationChannelGroup(null, null); |
| 889 | int N = r.channels.size(); |
| 890 | for (int i = 0; i < N; i++) { |
| 891 | final NotificationChannel nc = r.channels.valueAt(i); |
| 892 | if (includeDeleted || !nc.isDeleted()) { |
| 893 | if (nc.getGroup() != null) { |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 894 | if (r.groups.get(nc.getGroup()) != null) { |
| 895 | NotificationChannelGroup ncg = groups.get(nc.getGroup()); |
| 896 | if (ncg == null) { |
| 897 | ncg = r.groups.get(nc.getGroup()).clone(); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 898 | ncg.setChannels(new ArrayList<>()); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 899 | groups.put(nc.getGroup(), ncg); |
| 900 | |
| 901 | } |
| 902 | ncg.addChannel(nc); |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 903 | } |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 904 | } else { |
| 905 | nonGrouped.addChannel(nc); |
| 906 | } |
| 907 | } |
| 908 | } |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 909 | if (includeNonGrouped && nonGrouped.getChannels().size() > 0) { |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 910 | groups.put(null, nonGrouped); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 911 | } |
Julia Reynolds | 74856c4 | 2017-02-08 14:47:23 -0500 | [diff] [blame] | 912 | return new ParceledListSlice<>(new ArrayList<>(groups.values())); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 913 | } |
| 914 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 915 | public List<NotificationChannel> deleteNotificationChannelGroup(String pkg, int uid, |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 916 | String groupId) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 917 | List<NotificationChannel> deletedChannels = new ArrayList<>(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 918 | Record r = getRecord(pkg, uid); |
| 919 | if (r == null || TextUtils.isEmpty(groupId)) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 920 | return deletedChannels; |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | r.groups.remove(groupId); |
| 924 | |
| 925 | int N = r.channels.size(); |
| 926 | for (int i = 0; i < N; i++) { |
| 927 | final NotificationChannel nc = r.channels.valueAt(i); |
| 928 | if (groupId.equals(nc.getGroup())) { |
| 929 | nc.setDeleted(true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 930 | deletedChannels.add(nc); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 931 | } |
| 932 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 933 | return deletedChannels; |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 934 | } |
| 935 | |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 936 | @Override |
Julia Reynolds | f02562a | 2017-01-26 13:33:56 -0500 | [diff] [blame] | 937 | public Collection<NotificationChannelGroup> getNotificationChannelGroups(String pkg, |
| 938 | int uid) { |
| 939 | Record r = getRecord(pkg, uid); |
| 940 | if (r == null) { |
| 941 | return new ArrayList<>(); |
| 942 | } |
| 943 | return r.groups.values(); |
| 944 | } |
| 945 | |
| 946 | @Override |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 947 | public ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg, int uid, |
| 948 | boolean includeDeleted) { |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 949 | Preconditions.checkNotNull(pkg); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 950 | List<NotificationChannel> channels = new ArrayList<>(); |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 951 | Record r = getRecord(pkg, uid); |
| 952 | if (r == null) { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 953 | return ParceledListSlice.emptyList(); |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 954 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 955 | int N = r.channels.size(); |
| 956 | for (int i = 0; i < N; i++) { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 957 | final NotificationChannel nc = r.channels.valueAt(i); |
| 958 | if (includeDeleted || !nc.isDeleted()) { |
| 959 | channels.add(nc); |
| 960 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 961 | } |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 962 | return new ParceledListSlice<>(channels); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 963 | } |
| 964 | |
Julia Reynolds | 17717f5 | 2017-05-09 11:46:06 -0400 | [diff] [blame] | 965 | /** |
| 966 | * True for pre-O apps that only have the default channel, or pre O apps that have no |
| 967 | * channels yet. This method will create the default channel for pre-O apps that don't have it. |
| 968 | * Should never be true for O+ targeting apps, but that's enforced on boot/when an app |
| 969 | * upgrades. |
| 970 | */ |
| 971 | public boolean onlyHasDefaultChannel(String pkg, int uid) { |
| 972 | Record r = getOrCreateRecord(pkg, uid); |
| 973 | if (r.channels.size() == 1 |
| 974 | && r.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) { |
| 975 | return true; |
| 976 | } |
| 977 | return false; |
| 978 | } |
| 979 | |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 980 | public int getDeletedChannelCount(String pkg, int uid) { |
| 981 | Preconditions.checkNotNull(pkg); |
| 982 | int deletedCount = 0; |
| 983 | Record r = getRecord(pkg, uid); |
| 984 | if (r == null) { |
| 985 | return deletedCount; |
| 986 | } |
| 987 | int N = r.channels.size(); |
| 988 | for (int i = 0; i < N; i++) { |
| 989 | final NotificationChannel nc = r.channels.valueAt(i); |
| 990 | if (nc.isDeleted()) { |
| 991 | deletedCount++; |
| 992 | } |
| 993 | } |
| 994 | return deletedCount; |
| 995 | } |
| 996 | |
Julia Reynolds | f2e499d | 2018-03-30 10:36:42 -0400 | [diff] [blame] | 997 | public int getBlockedChannelCount(String pkg, int uid) { |
| 998 | Preconditions.checkNotNull(pkg); |
| 999 | int blockedCount = 0; |
| 1000 | Record r = getRecord(pkg, uid); |
| 1001 | if (r == null) { |
| 1002 | return blockedCount; |
| 1003 | } |
| 1004 | int N = r.channels.size(); |
| 1005 | for (int i = 0; i < N; i++) { |
| 1006 | final NotificationChannel nc = r.channels.valueAt(i); |
| 1007 | if (!nc.isDeleted() && IMPORTANCE_NONE == nc.getImportance()) { |
| 1008 | blockedCount++; |
| 1009 | } |
| 1010 | } |
| 1011 | return blockedCount; |
| 1012 | } |
| 1013 | |
Julia Reynolds | e273f08 | 2018-04-12 13:48:49 -0400 | [diff] [blame] | 1014 | public int getBlockedAppCount(int userId) { |
| 1015 | int count = 0; |
| 1016 | synchronized (mRecords) { |
| 1017 | final int N = mRecords.size(); |
| 1018 | for (int i = 0; i < N; i++) { |
| 1019 | final Record r = mRecords.valueAt(i); |
| 1020 | if (userId == UserHandle.getUserId(r.uid) |
| 1021 | && r.importance == IMPORTANCE_NONE) { |
| 1022 | count++; |
| 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 | return count; |
| 1027 | } |
| 1028 | |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1029 | /** |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1030 | * Sets importance. |
Julia Reynolds | 92d456e | 2016-01-25 16:36:59 -0500 | [diff] [blame] | 1031 | */ |
| 1032 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1033 | public void setImportance(String pkgName, int uid, int importance) { |
| 1034 | getOrCreateRecord(pkgName, uid).importance = importance; |
Julia Reynolds | a07af88 | 2015-12-17 08:32:48 -0500 | [diff] [blame] | 1035 | updateConfig(); |
| 1036 | } |
| 1037 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1038 | public void setEnabled(String packageName, int uid, boolean enabled) { |
Julia Reynolds | f2e499d | 2018-03-30 10:36:42 -0400 | [diff] [blame] | 1039 | boolean wasEnabled = getImportance(packageName, uid) != IMPORTANCE_NONE; |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1040 | if (wasEnabled == enabled) { |
| 1041 | return; |
| 1042 | } |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 1043 | setImportance(packageName, uid, |
Julia Reynolds | f2e499d | 2018-03-30 10:36:42 -0400 | [diff] [blame] | 1044 | enabled ? DEFAULT_IMPORTANCE : IMPORTANCE_NONE); |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1045 | } |
| 1046 | |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 1047 | /** |
| 1048 | * Sets whether any notifications from the app, represented by the given {@code pkgName} and |
| 1049 | * {@code uid}, have their importance locked by the user. Locked notifications don't get |
| 1050 | * considered for sentiment adjustments (and thus never show a blocking helper). |
| 1051 | */ |
| 1052 | public void setAppImportanceLocked(String packageName, int uid) { |
| 1053 | Record record = getOrCreateRecord(packageName, uid); |
| 1054 | if ((record.lockedAppFields & LockableAppFields.USER_LOCKED_IMPORTANCE) != 0) { |
| 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | record.lockedAppFields = record.lockedAppFields | LockableAppFields.USER_LOCKED_IMPORTANCE; |
| 1059 | updateConfig(); |
| 1060 | } |
| 1061 | |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 1062 | @VisibleForTesting |
| 1063 | void lockFieldsForUpdate(NotificationChannel original, NotificationChannel update) { |
Julia Reynolds | e0b2574 | 2017-05-08 12:55:24 -0400 | [diff] [blame] | 1064 | if (original.canBypassDnd() != update.canBypassDnd()) { |
| 1065 | update.lockFields(NotificationChannel.USER_LOCKED_PRIORITY); |
| 1066 | } |
| 1067 | if (original.getLockscreenVisibility() != update.getLockscreenVisibility()) { |
| 1068 | update.lockFields(NotificationChannel.USER_LOCKED_VISIBILITY); |
| 1069 | } |
| 1070 | if (original.getImportance() != update.getImportance()) { |
| 1071 | update.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); |
| 1072 | } |
| 1073 | if (original.shouldShowLights() != update.shouldShowLights() |
| 1074 | || original.getLightColor() != update.getLightColor()) { |
| 1075 | update.lockFields(NotificationChannel.USER_LOCKED_LIGHTS); |
| 1076 | } |
| 1077 | if (!Objects.equals(original.getSound(), update.getSound())) { |
| 1078 | update.lockFields(NotificationChannel.USER_LOCKED_SOUND); |
| 1079 | } |
| 1080 | if (!Arrays.equals(original.getVibrationPattern(), update.getVibrationPattern()) |
| 1081 | || original.shouldVibrate() != update.shouldVibrate()) { |
| 1082 | update.lockFields(NotificationChannel.USER_LOCKED_VIBRATION); |
| 1083 | } |
| 1084 | if (original.canShowBadge() != update.canShowBadge()) { |
| 1085 | update.lockFields(NotificationChannel.USER_LOCKED_SHOW_BADGE); |
| 1086 | } |
| 1087 | } |
| 1088 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 1089 | public void dump(PrintWriter pw, String prefix, |
| 1090 | @NonNull NotificationManagerService.DumpFilter filter) { |
| 1091 | final int N = mSignalExtractors.length; |
| 1092 | pw.print(prefix); |
| 1093 | pw.print("mSignalExtractors.length = "); |
| 1094 | pw.println(N); |
| 1095 | for (int i = 0; i < N; i++) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1096 | pw.print(prefix); |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 1097 | pw.print(" "); |
| 1098 | pw.println(mSignalExtractors[i].getClass().getSimpleName()); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1099 | } |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 1100 | |
| 1101 | pw.print(prefix); |
| 1102 | pw.println("per-package config:"); |
| 1103 | |
Julia Reynolds | 85aa6cb | 2016-01-08 17:49:11 -0500 | [diff] [blame] | 1104 | pw.println("Records:"); |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 1105 | synchronized (mRecords) { |
| 1106 | dumpRecords(pw, prefix, filter, mRecords); |
| 1107 | } |
Julia Reynolds | 85aa6cb | 2016-01-08 17:49:11 -0500 | [diff] [blame] | 1108 | pw.println("Restored without uid:"); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1109 | dumpRecords(pw, prefix, filter, mRestoredWithoutUids); |
| 1110 | } |
| 1111 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 1112 | public void dump(ProtoOutputStream proto, |
| 1113 | @NonNull NotificationManagerService.DumpFilter filter) { |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 1114 | final int N = mSignalExtractors.length; |
| 1115 | for (int i = 0; i < N; i++) { |
| 1116 | proto.write(RankingHelperProto.NOTIFICATION_SIGNAL_EXTRACTORS, |
| 1117 | mSignalExtractors[i].getClass().getSimpleName()); |
| 1118 | } |
| 1119 | synchronized (mRecords) { |
| 1120 | dumpRecords(proto, RankingHelperProto.RECORDS, filter, mRecords); |
| 1121 | } |
| 1122 | dumpRecords(proto, RankingHelperProto.RECORDS_RESTORED_WITHOUT_UID, filter, |
| 1123 | mRestoredWithoutUids); |
| 1124 | } |
| 1125 | |
| 1126 | private static void dumpRecords(ProtoOutputStream proto, long fieldId, |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 1127 | @NonNull NotificationManagerService.DumpFilter filter, |
| 1128 | ArrayMap<String, Record> records) { |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 1129 | final int N = records.size(); |
| 1130 | long fToken; |
| 1131 | for (int i = 0; i < N; i++) { |
| 1132 | final Record r = records.valueAt(i); |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 1133 | if (filter.matches(r.pkg)) { |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 1134 | fToken = proto.start(fieldId); |
| 1135 | |
| 1136 | proto.write(RecordProto.PACKAGE, r.pkg); |
| 1137 | proto.write(RecordProto.UID, r.uid); |
| 1138 | proto.write(RecordProto.IMPORTANCE, r.importance); |
| 1139 | proto.write(RecordProto.PRIORITY, r.priority); |
| 1140 | proto.write(RecordProto.VISIBILITY, r.visibility); |
| 1141 | proto.write(RecordProto.SHOW_BADGE, r.showBadge); |
| 1142 | |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 1143 | for (NotificationChannel channel : r.channels.values()) { |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 1144 | channel.writeToProto(proto, RecordProto.CHANNELS); |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 1145 | } |
| 1146 | for (NotificationChannelGroup group : r.groups.values()) { |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 1147 | group.writeToProto(proto, RecordProto.CHANNEL_GROUPS); |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | proto.end(fToken); |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1155 | private static void dumpRecords(PrintWriter pw, String prefix, |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 1156 | @NonNull NotificationManagerService.DumpFilter filter, |
| 1157 | ArrayMap<String, Record> records) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1158 | final int N = records.size(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1159 | for (int i = 0; i < N; i++) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1160 | final Record r = records.valueAt(i); |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 1161 | if (filter.matches(r.pkg)) { |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 1162 | pw.print(prefix); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1163 | pw.print(" AppSettings: "); |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 1164 | pw.print(r.pkg); |
| 1165 | pw.print(" ("); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1166 | pw.print(r.uid == Record.UNKNOWN_UID ? "UNKNOWN_UID" : Integer.toString(r.uid)); |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 1167 | pw.print(')'); |
Julia Reynolds | 92d456e | 2016-01-25 16:36:59 -0500 | [diff] [blame] | 1168 | if (r.importance != DEFAULT_IMPORTANCE) { |
| 1169 | pw.print(" importance="); |
| 1170 | pw.print(Ranking.importanceToString(r.importance)); |
| 1171 | } |
| 1172 | if (r.priority != DEFAULT_PRIORITY) { |
| 1173 | pw.print(" priority="); |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1174 | pw.print(Notification.priorityToString(r.priority)); |
Julia Reynolds | 92d456e | 2016-01-25 16:36:59 -0500 | [diff] [blame] | 1175 | } |
| 1176 | if (r.visibility != DEFAULT_VISIBILITY) { |
| 1177 | pw.print(" visibility="); |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1178 | pw.print(Notification.visibilityToString(r.visibility)); |
Julia Reynolds | 92d456e | 2016-01-25 16:36:59 -0500 | [diff] [blame] | 1179 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1180 | pw.print(" showBadge="); |
| 1181 | pw.print(Boolean.toString(r.showBadge)); |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 1182 | pw.println(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1183 | for (NotificationChannel channel : r.channels.values()) { |
| 1184 | pw.print(prefix); |
| 1185 | pw.print(" "); |
| 1186 | pw.print(" "); |
| 1187 | pw.println(channel); |
| 1188 | } |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1189 | for (NotificationChannelGroup group : r.groups.values()) { |
| 1190 | pw.print(prefix); |
| 1191 | pw.print(" "); |
| 1192 | pw.print(" "); |
| 1193 | pw.println(group); |
| 1194 | } |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1195 | } |
| 1196 | } |
| 1197 | } |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 1198 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1199 | public JSONObject dumpJson(NotificationManagerService.DumpFilter filter) { |
| 1200 | JSONObject ranking = new JSONObject(); |
| 1201 | JSONArray records = new JSONArray(); |
| 1202 | try { |
| 1203 | ranking.put("noUid", mRestoredWithoutUids.size()); |
| 1204 | } catch (JSONException e) { |
| 1205 | // pass |
| 1206 | } |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 1207 | synchronized (mRecords) { |
| 1208 | final int N = mRecords.size(); |
| 1209 | for (int i = 0; i < N; i++) { |
| 1210 | final Record r = mRecords.valueAt(i); |
| 1211 | if (filter == null || filter.matches(r.pkg)) { |
| 1212 | JSONObject record = new JSONObject(); |
| 1213 | try { |
| 1214 | record.put("userId", UserHandle.getUserId(r.uid)); |
| 1215 | record.put("packageName", r.pkg); |
| 1216 | if (r.importance != DEFAULT_IMPORTANCE) { |
| 1217 | record.put("importance", Ranking.importanceToString(r.importance)); |
| 1218 | } |
| 1219 | if (r.priority != DEFAULT_PRIORITY) { |
| 1220 | record.put("priority", Notification.priorityToString(r.priority)); |
| 1221 | } |
| 1222 | if (r.visibility != DEFAULT_VISIBILITY) { |
| 1223 | record.put("visibility", Notification.visibilityToString(r.visibility)); |
| 1224 | } |
| 1225 | if (r.showBadge != DEFAULT_SHOW_BADGE) { |
| 1226 | record.put("showBadge", Boolean.valueOf(r.showBadge)); |
| 1227 | } |
| 1228 | for (NotificationChannel channel : r.channels.values()) { |
| 1229 | record.put("channel", channel.toJson()); |
| 1230 | } |
| 1231 | for (NotificationChannelGroup group : r.groups.values()) { |
| 1232 | record.put("group", group.toJson()); |
| 1233 | } |
| 1234 | } catch (JSONException e) { |
| 1235 | // pass |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1236 | } |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 1237 | records.put(record); |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1238 | } |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | try { |
| 1242 | ranking.put("records", records); |
| 1243 | } catch (JSONException e) { |
| 1244 | // pass |
| 1245 | } |
| 1246 | return ranking; |
| 1247 | } |
| 1248 | |
| 1249 | /** |
| 1250 | * Dump only the ban information as structured JSON for the stats collector. |
| 1251 | * |
| 1252 | * This is intentionally redundant with {#link dumpJson} because the old |
| 1253 | * scraper will expect this format. |
| 1254 | * |
| 1255 | * @param filter |
| 1256 | * @return |
| 1257 | */ |
| 1258 | public JSONArray dumpBansJson(NotificationManagerService.DumpFilter filter) { |
| 1259 | JSONArray bans = new JSONArray(); |
| 1260 | Map<Integer, String> packageBans = getPackageBans(); |
| 1261 | for(Entry<Integer, String> ban : packageBans.entrySet()) { |
| 1262 | final int userId = UserHandle.getUserId(ban.getKey()); |
| 1263 | final String packageName = ban.getValue(); |
| 1264 | if (filter == null || filter.matches(packageName)) { |
| 1265 | JSONObject banJson = new JSONObject(); |
| 1266 | try { |
| 1267 | banJson.put("userId", userId); |
| 1268 | banJson.put("packageName", packageName); |
| 1269 | } catch (JSONException e) { |
| 1270 | e.printStackTrace(); |
| 1271 | } |
| 1272 | bans.put(banJson); |
| 1273 | } |
| 1274 | } |
| 1275 | return bans; |
| 1276 | } |
| 1277 | |
| 1278 | public Map<Integer, String> getPackageBans() { |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 1279 | synchronized (mRecords) { |
| 1280 | final int N = mRecords.size(); |
| 1281 | ArrayMap<Integer, String> packageBans = new ArrayMap<>(N); |
| 1282 | for (int i = 0; i < N; i++) { |
| 1283 | final Record r = mRecords.valueAt(i); |
Julia Reynolds | f2e499d | 2018-03-30 10:36:42 -0400 | [diff] [blame] | 1284 | if (r.importance == IMPORTANCE_NONE) { |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 1285 | packageBans.put(r.uid, r.pkg); |
| 1286 | } |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1287 | } |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 1288 | |
| 1289 | return packageBans; |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1290 | } |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1291 | } |
| 1292 | |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 1293 | /** |
| 1294 | * Dump only the channel information as structured JSON for the stats collector. |
| 1295 | * |
| 1296 | * This is intentionally redundant with {#link dumpJson} because the old |
| 1297 | * scraper will expect this format. |
| 1298 | * |
| 1299 | * @param filter |
| 1300 | * @return |
| 1301 | */ |
| 1302 | public JSONArray dumpChannelsJson(NotificationManagerService.DumpFilter filter) { |
| 1303 | JSONArray channels = new JSONArray(); |
| 1304 | Map<String, Integer> packageChannels = getPackageChannels(); |
| 1305 | for(Entry<String, Integer> channelCount : packageChannels.entrySet()) { |
| 1306 | final String packageName = channelCount.getKey(); |
| 1307 | if (filter == null || filter.matches(packageName)) { |
| 1308 | JSONObject channelCountJson = new JSONObject(); |
| 1309 | try { |
| 1310 | channelCountJson.put("packageName", packageName); |
| 1311 | channelCountJson.put("channelCount", channelCount.getValue()); |
| 1312 | } catch (JSONException e) { |
| 1313 | e.printStackTrace(); |
| 1314 | } |
| 1315 | channels.put(channelCountJson); |
| 1316 | } |
| 1317 | } |
| 1318 | return channels; |
| 1319 | } |
| 1320 | |
| 1321 | private Map<String, Integer> getPackageChannels() { |
| 1322 | ArrayMap<String, Integer> packageChannels = new ArrayMap<>(); |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 1323 | synchronized (mRecords) { |
| 1324 | for (int i = 0; i < mRecords.size(); i++) { |
| 1325 | final Record r = mRecords.valueAt(i); |
| 1326 | int channelCount = 0; |
| 1327 | for (int j = 0; j < r.channels.size(); j++) { |
| 1328 | if (!r.channels.valueAt(j).isDeleted()) { |
| 1329 | channelCount++; |
| 1330 | } |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 1331 | } |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 1332 | packageChannels.put(r.pkg, channelCount); |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 1333 | } |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 1334 | } |
| 1335 | return packageChannels; |
| 1336 | } |
| 1337 | |
Julia Reynolds | 2e9bf5f | 2017-05-03 13:23:30 -0400 | [diff] [blame] | 1338 | public void onUserRemoved(int userId) { |
| 1339 | synchronized (mRecords) { |
| 1340 | int N = mRecords.size(); |
| 1341 | for (int i = N - 1; i >= 0 ; i--) { |
| 1342 | Record record = mRecords.valueAt(i); |
| 1343 | if (UserHandle.getUserId(record.uid) == userId) { |
| 1344 | mRecords.removeAt(i); |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | |
Julia Reynolds | 816797a | 2017-08-11 15:47:09 -0400 | [diff] [blame] | 1350 | protected void onLocaleChanged(Context context, int userId) { |
| 1351 | synchronized (mRecords) { |
| 1352 | int N = mRecords.size(); |
| 1353 | for (int i = 0; i < N; i++) { |
| 1354 | Record record = mRecords.valueAt(i); |
| 1355 | if (UserHandle.getUserId(record.uid) == userId) { |
| 1356 | if (record.channels.containsKey(NotificationChannel.DEFAULT_CHANNEL_ID)) { |
| 1357 | record.channels.get(NotificationChannel.DEFAULT_CHANNEL_ID).setName( |
| 1358 | context.getResources().getString( |
| 1359 | R.string.default_notification_channel_label)); |
| 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1366 | public void onPackagesChanged(boolean removingPackage, int changeUserId, String[] pkgList, |
| 1367 | int[] uidList) { |
| 1368 | if (pkgList == null || pkgList.length == 0) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1369 | return; // nothing to do |
| 1370 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1371 | boolean updated = false; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1372 | if (removingPackage) { |
| 1373 | // Remove notification settings for uninstalled package |
| 1374 | int size = Math.min(pkgList.length, uidList.length); |
| 1375 | for (int i = 0; i < size; i++) { |
| 1376 | final String pkg = pkgList[i]; |
| 1377 | final int uid = uidList[i]; |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 1378 | synchronized (mRecords) { |
| 1379 | mRecords.remove(recordKey(pkg, uid)); |
| 1380 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1381 | mRestoredWithoutUids.remove(pkg); |
| 1382 | updated = true; |
| 1383 | } |
| 1384 | } else { |
| 1385 | for (String pkg : pkgList) { |
| 1386 | // Package install |
| 1387 | final Record r = mRestoredWithoutUids.get(pkg); |
| 1388 | if (r != null) { |
| 1389 | try { |
| 1390 | r.uid = mPm.getPackageUidAsUser(r.pkg, changeUserId); |
| 1391 | mRestoredWithoutUids.remove(pkg); |
Julia Reynolds | 4401196 | 2017-04-14 12:29:04 -0400 | [diff] [blame] | 1392 | synchronized (mRecords) { |
| 1393 | mRecords.put(recordKey(r.pkg, r.uid), r); |
| 1394 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1395 | updated = true; |
| 1396 | } catch (NameNotFoundException e) { |
| 1397 | // noop |
| 1398 | } |
| 1399 | } |
| 1400 | // Package upgrade |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1401 | try { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1402 | Record fullRecord = getRecord(pkg, |
| 1403 | mPm.getPackageUidAsUser(pkg, changeUserId)); |
| 1404 | if (fullRecord != null) { |
Julia Reynolds | f26eb91 | 2017-05-22 15:47:06 -0400 | [diff] [blame] | 1405 | createDefaultChannelIfNeeded(fullRecord); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1406 | deleteDefaultChannelIfNeeded(fullRecord); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1407 | } |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 1408 | } catch (NameNotFoundException e) {} |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1409 | } |
| 1410 | } |
Julia Reynolds | 1864ff5 | 2016-11-02 09:54:47 -0400 | [diff] [blame] | 1411 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1412 | if (updated) { |
| 1413 | updateConfig(); |
| 1414 | } |
| 1415 | } |
| 1416 | |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 1417 | private LogMaker getChannelLog(NotificationChannel channel, String pkg) { |
| 1418 | return new LogMaker(MetricsProto.MetricsEvent.ACTION_NOTIFICATION_CHANNEL) |
| 1419 | .setType(MetricsProto.MetricsEvent.TYPE_UPDATE) |
| 1420 | .setPackageName(pkg) |
| 1421 | .addTaggedData(MetricsProto.MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID, |
| 1422 | channel.getId()) |
| 1423 | .addTaggedData(MetricsProto.MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE, |
| 1424 | channel.getImportance()); |
| 1425 | } |
| 1426 | |
Dan Sandler | 0171f57 | 2017-06-14 14:03:18 -0400 | [diff] [blame] | 1427 | private LogMaker getChannelGroupLog(String groupId, String pkg) { |
| 1428 | return new LogMaker(MetricsProto.MetricsEvent.ACTION_NOTIFICATION_CHANNEL_GROUP) |
| 1429 | .setType(MetricsProto.MetricsEvent.TYPE_UPDATE) |
| 1430 | .addTaggedData(MetricsProto.MetricsEvent.FIELD_NOTIFICATION_CHANNEL_GROUP_ID, |
| 1431 | groupId) |
| 1432 | .setPackageName(pkg); |
| 1433 | } |
| 1434 | |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1435 | public void updateBadgingEnabled() { |
| 1436 | if (mBadgingEnabled == null) { |
| 1437 | mBadgingEnabled = new SparseBooleanArray(); |
| 1438 | } |
| 1439 | boolean changed = false; |
| 1440 | // update the cached values |
| 1441 | for (int index = 0; index < mBadgingEnabled.size(); index++) { |
| 1442 | int userId = mBadgingEnabled.keyAt(index); |
| 1443 | final boolean oldValue = mBadgingEnabled.get(userId); |
| 1444 | final boolean newValue = Secure.getIntForUser(mContext.getContentResolver(), |
| 1445 | Secure.NOTIFICATION_BADGING, |
| 1446 | DEFAULT_SHOW_BADGE ? 1 : 0, userId) != 0; |
| 1447 | mBadgingEnabled.put(userId, newValue); |
| 1448 | changed |= oldValue != newValue; |
| 1449 | } |
| 1450 | if (changed) { |
Julia Reynolds | f539577 | 2017-09-06 14:08:23 -0400 | [diff] [blame] | 1451 | updateConfig(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | public boolean badgingEnabled(UserHandle userHandle) { |
| 1456 | int userId = userHandle.getIdentifier(); |
Chris Wren | 13f157f | 2017-05-12 15:02:06 -0400 | [diff] [blame] | 1457 | if (userId == UserHandle.USER_ALL) { |
| 1458 | return false; |
| 1459 | } |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1460 | if (mBadgingEnabled.indexOfKey(userId) < 0) { |
| 1461 | mBadgingEnabled.put(userId, |
| 1462 | Secure.getIntForUser(mContext.getContentResolver(), |
| 1463 | Secure.NOTIFICATION_BADGING, |
| 1464 | DEFAULT_SHOW_BADGE ? 1 : 0, userId) != 0); |
| 1465 | } |
| 1466 | return mBadgingEnabled.get(userId, DEFAULT_SHOW_BADGE); |
| 1467 | } |
| 1468 | |
| 1469 | |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 1470 | private static class Record { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1471 | static int UNKNOWN_UID = UserHandle.USER_NULL; |
| 1472 | |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 1473 | String pkg; |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1474 | int uid = UNKNOWN_UID; |
Julia Reynolds | a07af88 | 2015-12-17 08:32:48 -0500 | [diff] [blame] | 1475 | int importance = DEFAULT_IMPORTANCE; |
Julia Reynolds | 92d456e | 2016-01-25 16:36:59 -0500 | [diff] [blame] | 1476 | int priority = DEFAULT_PRIORITY; |
| 1477 | int visibility = DEFAULT_VISIBILITY; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1478 | boolean showBadge = DEFAULT_SHOW_BADGE; |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 1479 | int lockedAppFields = DEFAULT_LOCKED_APP_FIELDS; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1480 | |
| 1481 | ArrayMap<String, NotificationChannel> channels = new ArrayMap<>(); |
Shunta Sato | 642b8d4 | 2017-06-13 16:16:13 +0900 | [diff] [blame] | 1482 | Map<String, NotificationChannelGroup> groups = new ConcurrentHashMap<>(); |
Julia Reynolds | 233a5f9 | 2015-10-19 13:51:23 -0400 | [diff] [blame] | 1483 | } |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1484 | } |