blob: e9f2718fe2b3f2935a0ea3bf2ea130a2f272b5ee [file] [log] [blame]
Chris Wren333a61c2014-05-28 16:40:57 -04001/*
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 */
16package com.android.server.notification;
17
Julia Reynoldsc65656a2018-02-12 09:55:14 -050018import static android.app.NotificationChannel.USER_LOCKED_IMPORTANCE;
Julia Reynolds85769912016-10-25 09:08:57 -040019import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
20import static android.app.NotificationManager.IMPORTANCE_HIGH;
21import static android.app.NotificationManager.IMPORTANCE_LOW;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060022import static android.app.NotificationManager.IMPORTANCE_MIN;
23import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
Julia Reynolds503ed942017-10-04 16:04:56 -040024import static android.service.notification.NotificationListenerService.Ranking
25 .USER_SENTIMENT_NEUTRAL;
Julia Reynoldsc65656a2018-02-12 09:55:14 -050026import static android.service.notification.NotificationListenerService.Ranking
27 .USER_SENTIMENT_POSITIVE;
Chris Wrenbdf33762015-12-04 15:50:51 -050028
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060029import android.annotation.Nullable;
30import android.app.ActivityManager;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060031import android.app.IActivityManager;
Chris Wren333a61c2014-05-28 16:40:57 -040032import android.app.Notification;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040033import android.app.NotificationChannel;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060034import android.content.ContentProvider;
35import android.content.ContentResolver;
Chris Wren333a61c2014-05-28 16:40:57 -040036import android.content.Context;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060037import android.content.Intent;
Tony Mantlerab55f0f2017-06-16 10:50:00 -070038import android.content.pm.PackageManager;
Chris Wren333a61c2014-05-28 16:40:57 -040039import android.content.pm.PackageManager.NameNotFoundException;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060040import android.content.pm.PackageManagerInternal;
Chris Wren333a61c2014-05-28 16:40:57 -040041import android.content.res.Resources;
42import android.graphics.Bitmap;
Dan Sandlerd63f9322015-05-06 15:18:49 -040043import android.graphics.drawable.Icon;
John Spurlockbfa5dc42014-07-28 23:30:45 -040044import android.media.AudioAttributes;
Julia Reynolds0c299d42016-11-15 14:37:04 -050045import android.media.AudioSystem;
Chris Wren9eb5e102017-01-26 13:15:06 -050046import android.metrics.LogMaker;
Julia Reynolds0c299d42016-11-15 14:37:04 -050047import android.net.Uri;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060048import android.os.Binder;
Julia Reynolds0c299d42016-11-15 14:37:04 -050049import android.os.Build;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040050import android.os.Bundle;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060051import android.os.IBinder;
Chris Wrenda4bd202014-09-04 15:53:52 -040052import android.os.UserHandle;
Julia Reynolds0c299d42016-11-15 14:37:04 -050053import android.provider.Settings;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040054import android.service.notification.Adjustment;
Julia Reynolds5d25ee72015-11-20 15:38:20 -050055import android.service.notification.NotificationListenerService;
Julia Reynoldsc9842c12017-02-07 12:46:41 -050056import android.service.notification.NotificationRecordProto;
Julia Reynolds503ed942017-10-04 16:04:56 -040057import android.service.notification.NotificationStats;
Julia Reynolds22f02b32016-12-01 15:05:13 -050058import android.service.notification.SnoozeCriterion;
Chris Wren333a61c2014-05-28 16:40:57 -040059import android.service.notification.StatusBarNotification;
Julia Reynolds4b82f6d2017-01-04 10:47:41 -050060import android.text.TextUtils;
Julia Reynoldse0d711f2017-09-01 08:50:47 -040061import android.util.ArraySet;
Julia Reynoldsf0f629f2016-02-25 09:34:04 -050062import android.util.Log;
Julia Reynolds2a128742016-11-28 14:29:25 -050063import android.util.TimeUtils;
Julia Reynoldsc9842c12017-02-07 12:46:41 -050064import android.util.proto.ProtoOutputStream;
Dan Sandler0a2308e2017-05-30 19:50:42 -040065import android.widget.RemoteViews;
John Spurlockbfa5dc42014-07-28 23:30:45 -040066
Chris Wren1031c972014-07-23 13:11:45 +000067import com.android.internal.annotations.VisibleForTesting;
Chris Wren9eb5e102017-01-26 13:15:06 -050068import com.android.internal.logging.MetricsLogger;
69import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Chris Wren6650e572015-05-15 17:19:25 -040070import com.android.server.EventLogTags;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060071import com.android.server.LocalServices;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070072import com.android.server.uri.UriGrantsManagerInternal;
Chris Wren333a61c2014-05-28 16:40:57 -040073
74import java.io.PrintWriter;
75import java.lang.reflect.Array;
Julia Reynolds22f02b32016-12-01 15:05:13 -050076import java.util.ArrayList;
Chris Wren333a61c2014-05-28 16:40:57 -040077import java.util.Arrays;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040078import java.util.List;
John Spurlock312d1d02014-07-08 10:24:57 -040079import java.util.Objects;
Chris Wren333a61c2014-05-28 16:40:57 -040080
81/**
82 * Holds data about notifications that should not be shared with the
83 * {@link android.service.notification.NotificationListenerService}s.
84 *
85 * <p>These objects should not be mutated unless the code is synchronized
Julia Reynolds88860ce2017-06-01 16:55:49 -040086 * on {@link NotificationManagerService#mNotificationLock}, and any
Chris Wren333a61c2014-05-28 16:40:57 -040087 * modification should be followed by a sorting of that list.</p>
88 *
89 * <p>Is sortable by {@link NotificationComparator}.</p>
90 *
91 * {@hide}
92 */
93public final class NotificationRecord {
Julia Reynoldsf0f629f2016-02-25 09:34:04 -050094 static final String TAG = "NotificationRecord";
95 static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
Chris Wrenb3921792017-06-01 13:34:46 -040096 private static final int MAX_LOGTAG_LENGTH = 35;
Chris Wren333a61c2014-05-28 16:40:57 -040097 final StatusBarNotification sbn;
Julia Reynolds218871e2018-06-13 10:45:21 -040098 IActivityManager mAm;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070099 UriGrantsManagerInternal mUgmInternal;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600100 final int mTargetSdkVersion;
Christoph Studer365e4c32014-09-18 20:35:36 +0200101 final int mOriginalFlags;
Chris Wrenbdf33762015-12-04 15:50:51 -0500102 private final Context mContext;
Christoph Studer365e4c32014-09-18 20:35:36 +0200103
Chris Wren333a61c2014-05-28 16:40:57 -0400104 NotificationUsageStats.SingleNotificationStats stats;
105 boolean isCanceled;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600106 IBinder permissionOwner;
Chris Wren333a61c2014-05-28 16:40:57 -0400107
108 // These members are used by NotificationSignalExtractors
109 // to communicate with the ranking module.
110 private float mContactAffinity;
111 private boolean mRecentlyIntrusive;
Julia Reynolds309d1c82017-05-03 16:00:20 -0400112 private long mLastIntrusive;
Chris Wren333a61c2014-05-28 16:40:57 -0400113
114 // is this notification currently being intercepted by Zen Mode?
115 private boolean mIntercept;
Chris Wren333a61c2014-05-28 16:40:57 -0400116
Beverly5a20a5e2018-03-06 15:02:44 -0500117 // is this notification hidden since the app pkg is suspended?
118 private boolean mHidden;
119
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200120 // The timestamp used for ranking.
121 private long mRankingTimeMs;
122
Chris Wren640e3872015-04-21 13:23:18 -0400123 // The first post time, stable across updates.
124 private long mCreationTimeMs;
125
Chris Wren6650e572015-05-15 17:19:25 -0400126 // The most recent visibility event.
127 private long mVisibleSinceMs;
128
129 // The most recent update time, or the creation time if no updates.
130 private long mUpdateTimeMs;
131
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400132 // The most recent interruption time, or the creation time if no updates. Differs from the
133 // above value because updates are filtered based on whether they actually interrupted the
134 // user
135 private long mInterruptionTimeMs;
136
Chris Wrena3446562014-06-03 18:11:47 -0400137 // Is this record an update of an old record?
138 public boolean isUpdate;
Chris Wren54bbef42014-07-09 18:37:56 -0400139 private int mPackagePriority;
Chris Wrena3446562014-06-03 18:11:47 -0400140
Chris Wren1031c972014-07-23 13:11:45 +0000141 private int mAuthoritativeRank;
Christoph Studercd4adf82014-08-19 17:50:49 +0200142 private String mGlobalSortKey;
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400143 private int mPackageVisibility;
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400144 private int mSystemImportance = IMPORTANCE_UNSPECIFIED;
145 private int mAssistantImportance = IMPORTANCE_UNSPECIFIED;
Chris Wren47633422016-01-22 09:56:59 -0500146 private int mImportance = IMPORTANCE_UNSPECIFIED;
Brad Stenning9a8b2c82018-08-03 14:14:26 -0700147 // Field used in global sort key to bypass normal notifications
148 private int mCriticality = CriticalNotificationExtractor.NORMAL;
Chris Wrenbdf33762015-12-04 15:50:51 -0500149 private CharSequence mImportanceExplanation = null;
Chris Wren1031c972014-07-23 13:11:45 +0000150
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500151 private int mSuppressedVisualEffects = 0;
Julia Reynoldsef37f282016-02-12 09:11:27 -0500152 private String mUserExplanation;
Chris Wrenbdf33762015-12-04 15:50:51 -0500153 private String mPeopleExplanation;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000154 private boolean mPreChannelsNotification = true;
Julia Reynolds0c299d42016-11-15 14:37:04 -0500155 private Uri mSound;
156 private long[] mVibration;
157 private AudioAttributes mAttributes;
Julia Reynolds924eed12017-01-19 09:52:07 -0500158 private NotificationChannel mChannel;
Julia Reynolds22f02b32016-12-01 15:05:13 -0500159 private ArrayList<String> mPeopleOverride;
160 private ArrayList<SnoozeCriterion> mSnoozeCriteria;
Julia Reynolds924eed12017-01-19 09:52:07 -0500161 private boolean mShowBadge;
Chris Wren9eb5e102017-01-26 13:15:06 -0500162 private LogMaker mLogMaker;
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500163 private Light mLight;
Chris Wrenb3921792017-06-01 13:34:46 -0400164 private String mGroupLogTag;
165 private String mChannelIdLogTag;
Tony Mak628cb932018-06-19 18:30:41 +0100166 private ArrayList<Notification.Action> mSmartActions;
Tony Makc9acf672018-07-20 13:58:24 +0200167 private ArrayList<CharSequence> mSmartReplies;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500168
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400169 private final List<Adjustment> mAdjustments;
Julia Reynolds503ed942017-10-04 16:04:56 -0400170 private final NotificationStats mStats;
171 private int mUserSentiment;
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500172 private boolean mIsInterruptive;
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400173 private boolean mTextChanged;
174 private boolean mRecordedInterruption;
Kenny Guy23991102018-04-05 21:18:38 +0100175 private int mNumberOfSmartRepliesAdded;
176 private boolean mHasSeenSmartReplies;
Rohan Shah590e1b22018-04-10 23:48:47 -0400177 /**
178 * Whether this notification (and its channels) should be considered user locked. Used in
179 * conjunction with user sentiment calculation.
180 */
181 private boolean mIsAppImportanceLocked;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600182 private ArraySet<Uri> mGrantableUris;
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400183
Julia Reynolds924eed12017-01-19 09:52:07 -0500184 public NotificationRecord(Context context, StatusBarNotification sbn,
Rohan Shah590e1b22018-04-10 23:48:47 -0400185 NotificationChannel channel) {
Chris Wren333a61c2014-05-28 16:40:57 -0400186 this.sbn = sbn;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600187 mTargetSdkVersion = LocalServices.getService(PackageManagerInternal.class)
188 .getPackageTargetSdkVersion(sbn.getPackageName());
Julia Reynolds218871e2018-06-13 10:45:21 -0400189 mAm = ActivityManager.getService();
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700190 mUgmInternal = LocalServices.getService(UriGrantsManagerInternal.class);
Christoph Studer365e4c32014-09-18 20:35:36 +0200191 mOriginalFlags = sbn.getNotification().flags;
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200192 mRankingTimeMs = calculateRankingTimeMs(0L);
Chris Wren640e3872015-04-21 13:23:18 -0400193 mCreationTimeMs = sbn.getPostTime();
Chris Wren6650e572015-05-15 17:19:25 -0400194 mUpdateTimeMs = mCreationTimeMs;
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400195 mInterruptionTimeMs = mCreationTimeMs;
Chris Wrenbdf33762015-12-04 15:50:51 -0500196 mContext = context;
Chris Wrencdee8cd2016-01-25 17:10:30 -0500197 stats = new NotificationUsageStats.SingleNotificationStats();
Julia Reynolds924eed12017-01-19 09:52:07 -0500198 mChannel = channel;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000199 mPreChannelsNotification = isPreChannelsNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500200 mSound = calculateSound();
201 mVibration = calculateVibration();
202 mAttributes = calculateAttributes();
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400203 mImportance = calculateInitialImportance();
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500204 mLight = calculateLights();
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400205 mAdjustments = new ArrayList<>();
Julia Reynolds503ed942017-10-04 16:04:56 -0400206 mStats = new NotificationStats();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500207 calculateUserSentiment();
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600208 calculateGrantableUris();
Chris Wrenbdf33762015-12-04 15:50:51 -0500209 }
210
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000211 private boolean isPreChannelsNotification() {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600212 if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(getChannel().getId())) {
213 if (mTargetSdkVersion < Build.VERSION_CODES.O) {
214 return true;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000215 }
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000216 }
217 return false;
218 }
219
Julia Reynolds0c299d42016-11-15 14:37:04 -0500220 private Uri calculateSound() {
Chris Wrenbdf33762015-12-04 15:50:51 -0500221 final Notification n = sbn.getNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500222
Tony Mantlerab55f0f2017-06-16 10:50:00 -0700223 // No notification sounds on tv
224 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
225 return null;
226 }
227
Julia Reynolds924eed12017-01-19 09:52:07 -0500228 Uri sound = mChannel.getSound();
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000229 if (mPreChannelsNotification && (getChannel().getUserLockedFields()
Julia Reynolds0c299d42016-11-15 14:37:04 -0500230 & NotificationChannel.USER_LOCKED_SOUND) == 0) {
231
232 final boolean useDefaultSound = (n.defaults & Notification.DEFAULT_SOUND) != 0;
233 if (useDefaultSound) {
234 sound = Settings.System.DEFAULT_NOTIFICATION_URI;
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400235 } else {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500236 sound = n.sound;
237 }
238 }
239 return sound;
240 }
241
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500242 private Light calculateLights() {
243 int defaultLightColor = mContext.getResources().getColor(
244 com.android.internal.R.color.config_defaultNotificationColor);
245 int defaultLightOn = mContext.getResources().getInteger(
246 com.android.internal.R.integer.config_defaultNotificationLedOn);
247 int defaultLightOff = mContext.getResources().getInteger(
248 com.android.internal.R.integer.config_defaultNotificationLedOff);
249
Julia Reynolds529e3322017-02-06 08:33:01 -0500250 int channelLightColor = getChannel().getLightColor() != 0 ? getChannel().getLightColor()
251 : defaultLightColor;
252 Light light = getChannel().shouldShowLights() ? new Light(channelLightColor,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500253 defaultLightOn, defaultLightOff) : null;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000254 if (mPreChannelsNotification
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500255 && (getChannel().getUserLockedFields()
256 & NotificationChannel.USER_LOCKED_LIGHTS) == 0) {
257 final Notification notification = sbn.getNotification();
258 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
259 light = new Light(notification.ledARGB, notification.ledOnMS,
260 notification.ledOffMS);
261 if ((notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
262 light = new Light(defaultLightColor, defaultLightOn,
263 defaultLightOff);
264 }
265 } else {
266 light = null;
267 }
268 }
269 return light;
270 }
271
Julia Reynolds0c299d42016-11-15 14:37:04 -0500272 private long[] calculateVibration() {
273 long[] vibration;
274 final long[] defaultVibration = NotificationManagerService.getLongArray(
275 mContext.getResources(),
276 com.android.internal.R.array.config_defaultNotificationVibePattern,
277 NotificationManagerService.VIBRATE_PATTERN_MAXLEN,
278 NotificationManagerService.DEFAULT_VIBRATE_PATTERN);
279 if (getChannel().shouldVibrate()) {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500280 vibration = getChannel().getVibrationPattern() == null
281 ? defaultVibration : getChannel().getVibrationPattern();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500282 } else {
283 vibration = null;
284 }
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000285 if (mPreChannelsNotification
Julia Reynolds0c299d42016-11-15 14:37:04 -0500286 && (getChannel().getUserLockedFields()
287 & NotificationChannel.USER_LOCKED_VIBRATION) == 0) {
288 final Notification notification = sbn.getNotification();
289 final boolean useDefaultVibrate =
290 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
291 if (useDefaultVibrate) {
292 vibration = defaultVibration;
293 } else {
294 vibration = notification.vibrate;
295 }
296 }
297 return vibration;
298 }
299
300 private AudioAttributes calculateAttributes() {
301 final Notification n = sbn.getNotification();
Julia Reynolds619a69f2017-01-27 15:11:38 -0500302 AudioAttributes attributes = getChannel().getAudioAttributes();
303 if (attributes == null) {
304 attributes = Notification.AUDIO_ATTRIBUTES_DEFAULT;
305 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500306
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000307 if (mPreChannelsNotification
Julia Reynolds619a69f2017-01-27 15:11:38 -0500308 && (getChannel().getUserLockedFields()
309 & NotificationChannel.USER_LOCKED_SOUND) == 0) {
310 if (n.audioAttributes != null) {
311 // prefer audio attributes to stream type
312 attributes = n.audioAttributes;
313 } else if (n.audioStreamType >= 0
314 && n.audioStreamType < AudioSystem.getNumStreamTypes()) {
315 // the stream type is valid, use it
316 attributes = new AudioAttributes.Builder()
317 .setInternalLegacyStreamType(n.audioStreamType)
318 .build();
319 } else if (n.audioStreamType != AudioSystem.STREAM_DEFAULT) {
320 Log.w(TAG, String.format("Invalid stream type: %d", n.audioStreamType));
321 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500322 }
323 return attributes;
324 }
325
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400326 private int calculateInitialImportance() {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500327 final Notification n = sbn.getNotification();
328 int importance = getChannel().getImportance();
329 int requestedImportance = IMPORTANCE_DEFAULT;
Chris Wrenbdf33762015-12-04 15:50:51 -0500330
331 // Migrate notification flags to scores
332 if (0 != (n.flags & Notification.FLAG_HIGH_PRIORITY)) {
333 n.priority = Notification.PRIORITY_MAX;
334 }
335
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500336 n.priority = NotificationManagerService.clamp(n.priority, Notification.PRIORITY_MIN,
337 Notification.PRIORITY_MAX);
Chris Wrenbdf33762015-12-04 15:50:51 -0500338 switch (n.priority) {
339 case Notification.PRIORITY_MIN:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500340 requestedImportance = IMPORTANCE_MIN;
Julia Reynoldsf0f629f2016-02-25 09:34:04 -0500341 break;
Chris Wrenbdf33762015-12-04 15:50:51 -0500342 case Notification.PRIORITY_LOW:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500343 requestedImportance = IMPORTANCE_LOW;
Chris Wrenbdf33762015-12-04 15:50:51 -0500344 break;
345 case Notification.PRIORITY_DEFAULT:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500346 requestedImportance = IMPORTANCE_DEFAULT;
Chris Wrenbdf33762015-12-04 15:50:51 -0500347 break;
348 case Notification.PRIORITY_HIGH:
Chris Wrenbdf33762015-12-04 15:50:51 -0500349 case Notification.PRIORITY_MAX:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500350 requestedImportance = IMPORTANCE_HIGH;
Chris Wrenbdf33762015-12-04 15:50:51 -0500351 break;
352 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500353 stats.requestedImportance = requestedImportance;
354 stats.isNoisy = mSound != null || mVibration != null;
Chris Wrenbdf33762015-12-04 15:50:51 -0500355
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000356 if (mPreChannelsNotification
Julia Reynoldsa917a112017-03-21 11:09:14 -0400357 && (importance == IMPORTANCE_UNSPECIFIED
358 || (getChannel().getUserLockedFields()
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500359 & USER_LOCKED_IMPORTANCE) == 0)) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500360 if (!stats.isNoisy && requestedImportance > IMPORTANCE_LOW) {
361 requestedImportance = IMPORTANCE_LOW;
Julia Reynolds83fa1072016-02-17 09:10:19 -0500362 }
Julia Reynolds83fa1072016-02-17 09:10:19 -0500363
Julia Reynolds0c299d42016-11-15 14:37:04 -0500364 if (stats.isNoisy) {
365 if (requestedImportance < IMPORTANCE_DEFAULT) {
366 requestedImportance = IMPORTANCE_DEFAULT;
367 }
368 }
369
370 if (n.fullScreenIntent != null) {
371 requestedImportance = IMPORTANCE_HIGH;
372 }
373 importance = requestedImportance;
Chris Wrenbdf33762015-12-04 15:50:51 -0500374 }
375
Chris Wrencdee8cd2016-01-25 17:10:30 -0500376 stats.naturalImportance = importance;
Chris Wrenbdf33762015-12-04 15:50:51 -0500377 return importance;
Chris Wren333a61c2014-05-28 16:40:57 -0400378 }
379
380 // copy any notes that the ranking system may have made before the update
381 public void copyRankingInformation(NotificationRecord previous) {
382 mContactAffinity = previous.mContactAffinity;
383 mRecentlyIntrusive = previous.mRecentlyIntrusive;
Chris Wren54bbef42014-07-09 18:37:56 -0400384 mPackagePriority = previous.mPackagePriority;
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400385 mPackageVisibility = previous.mPackageVisibility;
Chris Wren333a61c2014-05-28 16:40:57 -0400386 mIntercept = previous.mIntercept;
Beverly5a20a5e2018-03-06 15:02:44 -0500387 mHidden = previous.mHidden;
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200388 mRankingTimeMs = calculateRankingTimeMs(previous.getRankingTimeMs());
Chris Wren640e3872015-04-21 13:23:18 -0400389 mCreationTimeMs = previous.mCreationTimeMs;
Chris Wren6650e572015-05-15 17:19:25 -0400390 mVisibleSinceMs = previous.mVisibleSinceMs;
Selim Cinek5b03ce92016-05-18 15:16:58 -0700391 if (previous.sbn.getOverrideGroupKey() != null && !sbn.isAppGroup()) {
Chris Wren8a1638f2016-05-02 16:19:14 -0400392 sbn.setOverrideGroupKey(previous.sbn.getOverrideGroupKey());
393 }
Chris Wren1f602dc2016-04-11 10:33:46 -0400394 // Don't copy importance information or mGlobalSortKey, recompute them.
Chris Wren333a61c2014-05-28 16:40:57 -0400395 }
396
397 public Notification getNotification() { return sbn.getNotification(); }
398 public int getFlags() { return sbn.getNotification().flags; }
Chris Wrenda4bd202014-09-04 15:53:52 -0400399 public UserHandle getUser() { return sbn.getUser(); }
Chris Wren333a61c2014-05-28 16:40:57 -0400400 public String getKey() { return sbn.getKey(); }
Chris Wrenda4bd202014-09-04 15:53:52 -0400401 /** @deprecated Use {@link #getUser()} instead. */
402 public int getUserId() { return sbn.getUserId(); }
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600403 public int getUid() { return sbn.getUid(); }
Chris Wren333a61c2014-05-28 16:40:57 -0400404
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800405 void dump(ProtoOutputStream proto, long fieldId, boolean redact, int state) {
406 final long token = proto.start(fieldId);
407
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500408 proto.write(NotificationRecordProto.KEY, sbn.getKey());
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800409 proto.write(NotificationRecordProto.STATE, state);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500410 if (getChannel() != null) {
411 proto.write(NotificationRecordProto.CHANNEL_ID, getChannel().getId());
412 }
413 proto.write(NotificationRecordProto.CAN_SHOW_LIGHT, getLight() != null);
414 proto.write(NotificationRecordProto.CAN_VIBRATE, getVibration() != null);
415 proto.write(NotificationRecordProto.FLAGS, sbn.getNotification().flags);
416 proto.write(NotificationRecordProto.GROUP_KEY, getGroupKey());
417 proto.write(NotificationRecordProto.IMPORTANCE, getImportance());
418 if (getSound() != null) {
419 proto.write(NotificationRecordProto.SOUND, getSound().toString());
420 }
421 if (getAudioAttributes() != null) {
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800422 getAudioAttributes().writeToProto(proto, NotificationRecordProto.AUDIO_ATTRIBUTES);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500423 }
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800424
425 proto.end(token);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500426 }
427
Dan Sandler0a2308e2017-05-30 19:50:42 -0400428 String formatRemoteViews(RemoteViews rv) {
429 if (rv == null) return "null";
430 return String.format("%s/0x%08x (%d bytes): %s",
431 rv.getPackage(), rv.getLayoutId(), rv.estimateMemoryUsage(), rv.toString());
432 }
433
Dan Sandlera1770312015-07-10 13:59:29 -0400434 void dump(PrintWriter pw, String prefix, Context baseContext, boolean redact) {
Chris Wren333a61c2014-05-28 16:40:57 -0400435 final Notification notification = sbn.getNotification();
Dan Sandlerd63f9322015-05-06 15:18:49 -0400436 final Icon icon = notification.getSmallIcon();
437 String iconStr = String.valueOf(icon);
438 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
439 iconStr += " / " + idDebugString(baseContext, icon.getResPackage(), icon.getResId());
440 }
Chris Wren333a61c2014-05-28 16:40:57 -0400441 pw.println(prefix + this);
Dan Sandler0a2308e2017-05-30 19:50:42 -0400442 prefix = prefix + " ";
Julia Reynoldsa917a112017-03-21 11:09:14 -0400443 pw.println(prefix + "uid=" + sbn.getUid() + " userId=" + sbn.getUserId());
444 pw.println(prefix + "icon=" + iconStr);
Julia Reynolds4db59552017-06-30 13:34:01 -0400445 pw.println(prefix + "flags=0x" + Integer.toHexString(notification.flags));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400446 pw.println(prefix + "pri=" + notification.priority);
447 pw.println(prefix + "key=" + sbn.getKey());
Julia Reynolds503ed942017-10-04 16:04:56 -0400448 pw.println(prefix + "seen=" + mStats.hasSeen());
Julia Reynoldsa917a112017-03-21 11:09:14 -0400449 pw.println(prefix + "groupKey=" + getGroupKey());
450 pw.println(prefix + "fullscreenIntent=" + notification.fullScreenIntent);
451 pw.println(prefix + "contentIntent=" + notification.contentIntent);
452 pw.println(prefix + "deleteIntent=" + notification.deleteIntent);
Aaron Heuckrothb4d6aa72018-07-02 14:58:33 -0400453 pw.println(prefix + "number=" + notification.number);
454 pw.println(prefix + "groupAlertBehavior=" + notification.getGroupAlertBehavior());
Dan Sandler0b4ceb32017-03-29 14:13:55 -0400455
456 pw.print(prefix + "tickerText=");
457 if (!TextUtils.isEmpty(notification.tickerText)) {
458 final String ticker = notification.tickerText.toString();
459 if (redact) {
460 // if the string is long enough, we allow ourselves a few bytes for debugging
461 pw.print(ticker.length() > 16 ? ticker.substring(0,8) : "");
462 pw.println("...");
463 } else {
464 pw.println(ticker);
465 }
466 } else {
467 pw.println("null");
468 }
Dan Sandler0a2308e2017-05-30 19:50:42 -0400469 pw.println(prefix + "contentView=" + formatRemoteViews(notification.contentView));
470 pw.println(prefix + "bigContentView=" + formatRemoteViews(notification.bigContentView));
471 pw.println(prefix + "headsUpContentView="
472 + formatRemoteViews(notification.headsUpContentView));
473 pw.print(prefix + String.format("color=0x%08x", notification.color));
Julia Reynoldsbad42972017-04-25 13:52:49 -0400474 pw.println(prefix + "timeout="
475 + TimeUtils.formatForLogging(notification.getTimeoutAfter()));
Chris Wren333a61c2014-05-28 16:40:57 -0400476 if (notification.actions != null && notification.actions.length > 0) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400477 pw.println(prefix + "actions={");
Chris Wren333a61c2014-05-28 16:40:57 -0400478 final int N = notification.actions.length;
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500479 for (int i = 0; i < N; i++) {
Chris Wren333a61c2014-05-28 16:40:57 -0400480 final Notification.Action action = notification.actions[i];
Chris Wren1ac52a92016-02-24 14:54:52 -0500481 if (action != null) {
482 pw.println(String.format("%s [%d] \"%s\" -> %s",
483 prefix,
484 i,
485 action.title,
486 action.actionIntent == null ? "null" : action.actionIntent.toString()
487 ));
488 }
Chris Wren333a61c2014-05-28 16:40:57 -0400489 }
490 pw.println(prefix + " }");
491 }
492 if (notification.extras != null && notification.extras.size() > 0) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400493 pw.println(prefix + "extras={");
Chris Wren333a61c2014-05-28 16:40:57 -0400494 for (String key : notification.extras.keySet()) {
495 pw.print(prefix + " " + key + "=");
496 Object val = notification.extras.get(key);
497 if (val == null) {
498 pw.println("null");
499 } else {
500 pw.print(val.getClass().getSimpleName());
Dan Sandlera1770312015-07-10 13:59:29 -0400501 if (redact && (val instanceof CharSequence || val instanceof String)) {
Chris Wren333a61c2014-05-28 16:40:57 -0400502 // redact contents from bugreports
503 } else if (val instanceof Bitmap) {
504 pw.print(String.format(" (%dx%d)",
505 ((Bitmap) val).getWidth(),
506 ((Bitmap) val).getHeight()));
507 } else if (val.getClass().isArray()) {
508 final int N = Array.getLength(val);
Dan Sandlera1770312015-07-10 13:59:29 -0400509 pw.print(" (" + N + ")");
510 if (!redact) {
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500511 for (int j = 0; j < N; j++) {
Dan Sandlera1770312015-07-10 13:59:29 -0400512 pw.println();
513 pw.print(String.format("%s [%d] %s",
514 prefix, j, String.valueOf(Array.get(val, j))));
515 }
516 }
Chris Wren333a61c2014-05-28 16:40:57 -0400517 } else {
518 pw.print(" (" + String.valueOf(val) + ")");
519 }
520 pw.println();
521 }
522 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400523 pw.println(prefix + "}");
Chris Wren333a61c2014-05-28 16:40:57 -0400524 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400525 pw.println(prefix + "stats=" + stats.toString());
526 pw.println(prefix + "mContactAffinity=" + mContactAffinity);
527 pw.println(prefix + "mRecentlyIntrusive=" + mRecentlyIntrusive);
528 pw.println(prefix + "mPackagePriority=" + mPackagePriority);
529 pw.println(prefix + "mPackageVisibility=" + mPackageVisibility);
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400530 pw.println(prefix + "mSystemImportance="
531 + NotificationListenerService.Ranking.importanceToString(mSystemImportance));
532 pw.println(prefix + "mAsstImportance="
533 + NotificationListenerService.Ranking.importanceToString(mAssistantImportance));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400534 pw.println(prefix + "mImportance="
Chris Wrenbdf33762015-12-04 15:50:51 -0500535 + NotificationListenerService.Ranking.importanceToString(mImportance));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400536 pw.println(prefix + "mImportanceExplanation=" + mImportanceExplanation);
Rohan Shah590e1b22018-04-10 23:48:47 -0400537 pw.println(prefix + "mIsAppImportanceLocked=" + mIsAppImportanceLocked);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400538 pw.println(prefix + "mIntercept=" + mIntercept);
Beverly5a20a5e2018-03-06 15:02:44 -0500539 pw.println(prefix + "mHidden==" + mHidden);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400540 pw.println(prefix + "mGlobalSortKey=" + mGlobalSortKey);
541 pw.println(prefix + "mRankingTimeMs=" + mRankingTimeMs);
542 pw.println(prefix + "mCreationTimeMs=" + mCreationTimeMs);
543 pw.println(prefix + "mVisibleSinceMs=" + mVisibleSinceMs);
544 pw.println(prefix + "mUpdateTimeMs=" + mUpdateTimeMs);
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400545 pw.println(prefix + "mInterruptionTimeMs=" + mInterruptionTimeMs);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400546 pw.println(prefix + "mSuppressedVisualEffects= " + mSuppressedVisualEffects);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000547 if (mPreChannelsNotification) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400548 pw.println(prefix + String.format("defaults=0x%08x flags=0x%08x",
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500549 notification.defaults, notification.flags));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400550 pw.println(prefix + "n.sound=" + notification.sound);
551 pw.println(prefix + "n.audioStreamType=" + notification.audioStreamType);
552 pw.println(prefix + "n.audioAttributes=" + notification.audioAttributes);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500553 pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d",
554 notification.ledARGB, notification.ledOnMS, notification.ledOffMS));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400555 pw.println(prefix + "vibrate=" + Arrays.toString(notification.vibrate));
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500556 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400557 pw.println(prefix + "mSound= " + mSound);
558 pw.println(prefix + "mVibration= " + mVibration);
559 pw.println(prefix + "mAttributes= " + mAttributes);
560 pw.println(prefix + "mLight= " + mLight);
561 pw.println(prefix + "mShowBadge=" + mShowBadge);
Julia Reynolds4db59552017-06-30 13:34:01 -0400562 pw.println(prefix + "mColorized=" + notification.isColorized());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500563 pw.println(prefix + "mIsInterruptive=" + mIsInterruptive);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400564 pw.println(prefix + "effectiveNotificationChannel=" + getChannel());
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500565 if (getPeopleOverride() != null) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400566 pw.println(prefix + "overridePeople= " + TextUtils.join(",", getPeopleOverride()));
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500567 }
568 if (getSnoozeCriteria() != null) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400569 pw.println(prefix + "snoozeCriteria=" + TextUtils.join(",", getSnoozeCriteria()));
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500570 }
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400571 pw.println(prefix + "mAdjustments=" + mAdjustments);
Chris Wren333a61c2014-05-28 16:40:57 -0400572 }
573
574
575 static String idDebugString(Context baseContext, String packageName, int id) {
576 Context c;
577
578 if (packageName != null) {
579 try {
580 c = baseContext.createPackageContext(packageName, 0);
581 } catch (NameNotFoundException e) {
582 c = baseContext;
583 }
584 } else {
585 c = baseContext;
586 }
587
588 Resources r = c.getResources();
589 try {
590 return r.getResourceName(id);
591 } catch (Resources.NotFoundException e) {
592 return "<name unknown>";
593 }
594 }
595
596 @Override
597 public final String toString() {
598 return String.format(
Julia Reynolds85769912016-10-25 09:08:57 -0400599 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s importance=%d key=%s" +
Rohan Shah590e1b22018-04-10 23:48:47 -0400600 "appImportanceLocked=%s: %s)",
Chris Wren333a61c2014-05-28 16:40:57 -0400601 System.identityHashCode(this),
602 this.sbn.getPackageName(), this.sbn.getUser(), this.sbn.getId(),
Julia Reynolds4db59552017-06-30 13:34:01 -0400603 this.sbn.getTag(), this.mImportance, this.sbn.getKey(),
Rohan Shah590e1b22018-04-10 23:48:47 -0400604 mIsAppImportanceLocked, this.sbn.getNotification());
Chris Wren333a61c2014-05-28 16:40:57 -0400605 }
606
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400607 public void addAdjustment(Adjustment adjustment) {
608 synchronized (mAdjustments) {
609 mAdjustments.add(adjustment);
610 }
611 }
612
613 public void applyAdjustments() {
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400614 long now = System.currentTimeMillis();
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400615 synchronized (mAdjustments) {
616 for (Adjustment adjustment: mAdjustments) {
617 Bundle signals = adjustment.getSignals();
618 if (signals.containsKey(Adjustment.KEY_PEOPLE)) {
619 final ArrayList<String> people =
620 adjustment.getSignals().getStringArrayList(Adjustment.KEY_PEOPLE);
621 setPeopleOverride(people);
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400622 MetricsLogger.action(getAdjustmentLogMaker()
623 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_PEOPLE, people.size()));
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400624 }
625 if (signals.containsKey(Adjustment.KEY_SNOOZE_CRITERIA)) {
626 final ArrayList<SnoozeCriterion> snoozeCriterionList =
627 adjustment.getSignals().getParcelableArrayList(
628 Adjustment.KEY_SNOOZE_CRITERIA);
629 setSnoozeCriteria(snoozeCriterionList);
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400630 MetricsLogger.action(getAdjustmentLogMaker()
631 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_SNOOZE_CRITERIA,
632 snoozeCriterionList.size()));
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400633 }
634 if (signals.containsKey(Adjustment.KEY_GROUP_KEY)) {
635 final String groupOverrideKey =
636 adjustment.getSignals().getString(Adjustment.KEY_GROUP_KEY);
637 setOverrideGroupKey(groupOverrideKey);
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400638 MetricsLogger.action(getAdjustmentLogMaker()
639 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_GROUP_KEY,
640 groupOverrideKey));
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400641 }
Julia Reynolds503ed942017-10-04 16:04:56 -0400642 if (signals.containsKey(Adjustment.KEY_USER_SENTIMENT)) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500643 // Only allow user sentiment update from assistant if user hasn't already
644 // expressed a preference for this channel
Rohan Shah590e1b22018-04-10 23:48:47 -0400645 if (!mIsAppImportanceLocked
646 && (getChannel().getUserLockedFields() & USER_LOCKED_IMPORTANCE) == 0) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500647 setUserSentiment(adjustment.getSignals().getInt(
648 Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEUTRAL));
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400649 MetricsLogger.action(getAdjustmentLogMaker()
650 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_USER_SENTIMENT,
651 getUserSentiment()));
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500652 }
Julia Reynolds503ed942017-10-04 16:04:56 -0400653 }
Tony Mak628cb932018-06-19 18:30:41 +0100654 if (signals.containsKey(Adjustment.KEY_SMART_ACTIONS)) {
655 setSmartActions(signals.getParcelableArrayList(Adjustment.KEY_SMART_ACTIONS));
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400656 MetricsLogger.action(getAdjustmentLogMaker()
657 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_SMART_ACTIONS,
658 getSmartActions().size()));
Tony Mak628cb932018-06-19 18:30:41 +0100659 }
Tony Makc9acf672018-07-20 13:58:24 +0200660 if (signals.containsKey(Adjustment.KEY_SMART_REPLIES)) {
661 setSmartReplies(signals.getCharSequenceArrayList(Adjustment.KEY_SMART_REPLIES));
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400662 MetricsLogger.action(getAdjustmentLogMaker()
663 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_SMART_REPLIES,
664 getSmartReplies().size()));
Tony Makc9acf672018-07-20 13:58:24 +0200665 }
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400666 if (signals.containsKey(Adjustment.KEY_IMPORTANCE)) {
667 int importance = signals.getInt(Adjustment.KEY_IMPORTANCE);
668 importance = Math.max(IMPORTANCE_UNSPECIFIED, importance);
669 importance = Math.min(IMPORTANCE_HIGH, importance);
670 setAssistantImportance(importance);
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400671 MetricsLogger.action(getAdjustmentLogMaker()
672 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_IMPORTANCE,
673 importance));
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400674 }
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400675 }
676 }
677 }
678
Rohan Shah590e1b22018-04-10 23:48:47 -0400679 public void setIsAppImportanceLocked(boolean isAppImportanceLocked) {
680 mIsAppImportanceLocked = isAppImportanceLocked;
681 calculateUserSentiment();
682 }
683
Chris Wren333a61c2014-05-28 16:40:57 -0400684 public void setContactAffinity(float contactAffinity) {
685 mContactAffinity = contactAffinity;
686 }
687
688 public float getContactAffinity() {
689 return mContactAffinity;
690 }
691
John Spurlock1d881a12015-03-18 19:21:54 -0400692 public void setRecentlyIntrusive(boolean recentlyIntrusive) {
Chris Wren333a61c2014-05-28 16:40:57 -0400693 mRecentlyIntrusive = recentlyIntrusive;
Julia Reynolds309d1c82017-05-03 16:00:20 -0400694 if (recentlyIntrusive) {
695 mLastIntrusive = System.currentTimeMillis();
696 }
Chris Wren333a61c2014-05-28 16:40:57 -0400697 }
698
699 public boolean isRecentlyIntrusive() {
700 return mRecentlyIntrusive;
701 }
702
Julia Reynolds309d1c82017-05-03 16:00:20 -0400703 public long getLastIntrusive() {
704 return mLastIntrusive;
705 }
706
Chris Wren54bbef42014-07-09 18:37:56 -0400707 public void setPackagePriority(int packagePriority) {
John Spurlock6ac5f8d2014-07-18 11:27:54 -0400708 mPackagePriority = packagePriority;
Chris Wren54bbef42014-07-09 18:37:56 -0400709 }
710
711 public int getPackagePriority() {
712 return mPackagePriority;
713 }
714
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400715 public void setPackageVisibilityOverride(int packageVisibility) {
716 mPackageVisibility = packageVisibility;
717 }
718
719 public int getPackageVisibilityOverride() {
720 return mPackageVisibility;
721 }
722
Julia Reynoldsef37f282016-02-12 09:11:27 -0500723 private String getUserExplanation() {
724 if (mUserExplanation == null) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500725 mUserExplanation = mContext.getResources().getString(
726 com.android.internal.R.string.importance_from_user);
Chris Wrenbdf33762015-12-04 15:50:51 -0500727 }
Julia Reynoldsef37f282016-02-12 09:11:27 -0500728 return mUserExplanation;
Chris Wrenbdf33762015-12-04 15:50:51 -0500729 }
730
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400731 /**
732 * Sets the importance value the system thinks the record should have.
733 * e.g. bumping up foreground service notifications or people to people notifications.
734 */
735 public void setSystemImportance(int importance) {
736 mSystemImportance = importance;
737 calculateImportance();
Chris Wrenbdf33762015-12-04 15:50:51 -0500738 }
739
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400740 /**
741 * Sets the importance value the
742 * {@link android.service.notification.NotificationAssistantService} thinks the record should
743 * have.
744 */
745 public void setAssistantImportance(int importance) {
746 mAssistantImportance = importance;
747 calculateImportance();
Julia Reynolds5d25ee72015-11-20 15:38:20 -0500748 }
749
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400750 /**
751 * Recalculates the importance of the record after fields affecting importance have changed
752 */
753 protected void calculateImportance() {
754 mImportance = calculateInitialImportance();
755 mImportanceExplanation = "app";
Julia Reynolds48a6ed92018-10-22 12:52:03 -0400756 if (getChannel().hasUserSetImportance()) {
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400757 mImportanceExplanation = "user";
Chris Wrenbdf33762015-12-04 15:50:51 -0500758 }
Julia Reynolds48a6ed92018-10-22 12:52:03 -0400759 if (!getChannel().hasUserSetImportance() && mAssistantImportance != IMPORTANCE_UNSPECIFIED) {
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400760 mImportance = mAssistantImportance;
761 mImportanceExplanation = "asst";
762 }
763 if (mSystemImportance != IMPORTANCE_UNSPECIFIED) {
764 mImportance = mSystemImportance;
765 mImportanceExplanation = "system";
766 }
Chris Wrenbdf33762015-12-04 15:50:51 -0500767 }
768
769 public int getImportance() {
770 return mImportance;
771 }
772
773 public CharSequence getImportanceExplanation() {
774 return mImportanceExplanation;
775 }
776
Chris Wren333a61c2014-05-28 16:40:57 -0400777 public boolean setIntercepted(boolean intercept) {
778 mIntercept = intercept;
779 return mIntercept;
780 }
781
Brad Stenning9a8b2c82018-08-03 14:14:26 -0700782 /**
783 * Set to affect global sort key.
784 *
785 * @param criticality used in a string based sort thus 0 is the most critical
786 */
787 public void setCriticality(int criticality) {
788 mCriticality = criticality;
789 }
790
791 public int getCriticality() {
792 return mCriticality;
793 }
794
Chris Wren333a61c2014-05-28 16:40:57 -0400795 public boolean isIntercepted() {
796 return mIntercept;
797 }
798
Beverly5a20a5e2018-03-06 15:02:44 -0500799 public void setHidden(boolean hidden) {
800 mHidden = hidden;
801 }
802
803 public boolean isHidden() {
804 return mHidden;
805 }
806
807
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500808 public void setSuppressedVisualEffects(int effects) {
809 mSuppressedVisualEffects = effects;
810 }
811
812 public int getSuppressedVisualEffects() {
813 return mSuppressedVisualEffects;
814 }
815
John Spurlock312d1d02014-07-08 10:24:57 -0400816 public boolean isCategory(String category) {
John Spurlockbfa5dc42014-07-28 23:30:45 -0400817 return Objects.equals(getNotification().category, category);
818 }
819
John Spurlockbfa5dc42014-07-28 23:30:45 -0400820 public boolean isAudioAttributesUsage(int usage) {
Julia Reynolds51eb78f82018-03-07 07:35:21 -0500821 return mAttributes != null && mAttributes.getUsage() == usage;
John Spurlock312d1d02014-07-08 10:24:57 -0400822 }
823
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200824 /**
825 * Returns the timestamp to use for time-based sorting in the ranker.
826 */
827 public long getRankingTimeMs() {
828 return mRankingTimeMs;
829 }
830
831 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400832 * @param now this current time in milliseconds.
833 * @returns the number of milliseconds since the most recent update, or the post time if none.
Chris Wren6650e572015-05-15 17:19:25 -0400834 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400835 public int getFreshnessMs(long now) {
836 return (int) (now - mUpdateTimeMs);
Chris Wren6650e572015-05-15 17:19:25 -0400837 }
838
839 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400840 * @param now this current time in milliseconds.
841 * @returns the number of milliseconds since the the first post, ignoring updates.
Chris Wren640e3872015-04-21 13:23:18 -0400842 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400843 public int getLifespanMs(long now) {
844 return (int) (now - mCreationTimeMs);
Chris Wren640e3872015-04-21 13:23:18 -0400845 }
846
847 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400848 * @param now this current time in milliseconds.
849 * @returns the number of milliseconds since the most recent visibility event, or 0 if never.
Chris Wren6650e572015-05-15 17:19:25 -0400850 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400851 public int getExposureMs(long now) {
852 return mVisibleSinceMs == 0 ? 0 : (int) (now - mVisibleSinceMs);
Chris Wren6650e572015-05-15 17:19:25 -0400853 }
854
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400855 public int getInterruptionMs(long now) {
856 return (int) (now - mInterruptionTimeMs);
857 }
858
Chris Wren6650e572015-05-15 17:19:25 -0400859 /**
860 * Set the visibility of the notification.
861 */
Dieter Hsud39f0d52018-04-14 02:08:30 +0800862 public void setVisibility(boolean visible, int rank, int count) {
Chris Wren6650e572015-05-15 17:19:25 -0400863 final long now = System.currentTimeMillis();
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400864 mVisibleSinceMs = visible ? now : mVisibleSinceMs;
Chris Wren6650e572015-05-15 17:19:25 -0400865 stats.onVisibilityChanged(visible);
Chris Wren9eb5e102017-01-26 13:15:06 -0500866 MetricsLogger.action(getLogMaker(now)
867 .setCategory(MetricsEvent.NOTIFICATION_ITEM)
868 .setType(visible ? MetricsEvent.TYPE_OPEN : MetricsEvent.TYPE_CLOSE)
Dieter Hsud39f0d52018-04-14 02:08:30 +0800869 .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, rank)
870 .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, count));
Chris Wren9eb5e102017-01-26 13:15:06 -0500871 if (visible) {
Julia Reynolds503ed942017-10-04 16:04:56 -0400872 setSeen();
Chris Wren9eb5e102017-01-26 13:15:06 -0500873 MetricsLogger.histogram(mContext, "note_freshness", getFreshnessMs(now));
874 }
Chris Wren6650e572015-05-15 17:19:25 -0400875 EventLogTags.writeNotificationVisibility(getKey(), visible ? 1 : 0,
Chris Wren9eb5e102017-01-26 13:15:06 -0500876 getLifespanMs(now),
877 getFreshnessMs(now),
Chris Wrend1dbc922015-06-19 17:51:16 -0400878 0, // exposure time
879 rank);
Chris Wren6650e572015-05-15 17:19:25 -0400880 }
881
882 /**
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200883 * @param previousRankingTimeMs for updated notifications, {@link #getRankingTimeMs()}
884 * of the previous notification record, 0 otherwise
885 */
886 private long calculateRankingTimeMs(long previousRankingTimeMs) {
887 Notification n = getNotification();
888 // Take developer provided 'when', unless it's in the future.
889 if (n.when != 0 && n.when <= sbn.getPostTime()) {
890 return n.when;
891 }
892 // If we've ranked a previous instance with a timestamp, inherit it. This case is
893 // important in order to have ranking stability for updating notifications.
894 if (previousRankingTimeMs > 0) {
895 return previousRankingTimeMs;
896 }
897 return sbn.getPostTime();
898 }
Chris Wren1031c972014-07-23 13:11:45 +0000899
Christoph Studercd4adf82014-08-19 17:50:49 +0200900 public void setGlobalSortKey(String globalSortKey) {
901 mGlobalSortKey = globalSortKey;
Chris Wren1031c972014-07-23 13:11:45 +0000902 }
903
Christoph Studercd4adf82014-08-19 17:50:49 +0200904 public String getGlobalSortKey() {
905 return mGlobalSortKey;
Chris Wren1031c972014-07-23 13:11:45 +0000906 }
907
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700908 /** Check if any of the listeners have marked this notification as seen by the user. */
909 public boolean isSeen() {
Julia Reynolds503ed942017-10-04 16:04:56 -0400910 return mStats.hasSeen();
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700911 }
912
913 /** Mark the notification as seen by the user. */
914 public void setSeen() {
Julia Reynolds503ed942017-10-04 16:04:56 -0400915 mStats.setSeen();
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400916 if (mTextChanged) {
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400917 setInterruptive(true);
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400918 }
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700919 }
920
Chris Wren1031c972014-07-23 13:11:45 +0000921 public void setAuthoritativeRank(int authoritativeRank) {
922 mAuthoritativeRank = authoritativeRank;
923 }
924
925 public int getAuthoritativeRank() {
926 return mAuthoritativeRank;
927 }
928
929 public String getGroupKey() {
930 return sbn.getGroupKey();
931 }
Chris Wren47633422016-01-22 09:56:59 -0500932
Chris Wrenb3921792017-06-01 13:34:46 -0400933 public void setOverrideGroupKey(String overrideGroupKey) {
934 sbn.setOverrideGroupKey(overrideGroupKey);
935 mGroupLogTag = null;
936 }
937
938 private String getGroupLogTag() {
939 if (mGroupLogTag == null) {
940 mGroupLogTag = shortenTag(sbn.getGroup());
941 }
942 return mGroupLogTag;
943 }
944
945 private String getChannelIdLogTag() {
946 if (mChannelIdLogTag == null) {
947 mChannelIdLogTag = shortenTag(mChannel.getId());
948 }
949 return mChannelIdLogTag;
950 }
951
952 private String shortenTag(String longTag) {
953 if (longTag == null) {
954 return null;
955 }
956 if (longTag.length() < MAX_LOGTAG_LENGTH) {
957 return longTag;
958 } else {
959 return longTag.substring(0, MAX_LOGTAG_LENGTH - 8) + "-" +
960 Integer.toHexString(longTag.hashCode());
961 }
962 }
963
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400964 public NotificationChannel getChannel() {
Julia Reynolds924eed12017-01-19 09:52:07 -0500965 return mChannel;
Julia Reynolds22f02b32016-12-01 15:05:13 -0500966 }
967
Rohan Shah590e1b22018-04-10 23:48:47 -0400968 /**
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400969 * @see PreferencesHelper#getIsAppImportanceLocked(String, int)
Rohan Shah590e1b22018-04-10 23:48:47 -0400970 */
971 public boolean getIsAppImportanceLocked() {
972 return mIsAppImportanceLocked;
973 }
974
Julia Reynolds924eed12017-01-19 09:52:07 -0500975 protected void updateNotificationChannel(NotificationChannel channel) {
976 if (channel != null) {
977 mChannel = channel;
Julia Reynolds22f02b32016-12-01 15:05:13 -0500978 calculateImportance();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500979 calculateUserSentiment();
Julia Reynolds22f02b32016-12-01 15:05:13 -0500980 }
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400981 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500982
Julia Reynolds924eed12017-01-19 09:52:07 -0500983 public void setShowBadge(boolean showBadge) {
984 mShowBadge = showBadge;
985 }
986
987 public boolean canShowBadge() {
988 return mShowBadge;
989 }
990
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500991 public Light getLight() {
992 return mLight;
993 }
994
Julia Reynolds0c299d42016-11-15 14:37:04 -0500995 public Uri getSound() {
996 return mSound;
997 }
998
999 public long[] getVibration() {
1000 return mVibration;
1001 }
1002
1003 public AudioAttributes getAudioAttributes() {
1004 return mAttributes;
1005 }
Julia Reynolds22f02b32016-12-01 15:05:13 -05001006
1007 public ArrayList<String> getPeopleOverride() {
1008 return mPeopleOverride;
1009 }
1010
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001011 public void setInterruptive(boolean interruptive) {
1012 mIsInterruptive = interruptive;
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -04001013 final long now = System.currentTimeMillis();
1014 mInterruptionTimeMs = interruptive ? now : mInterruptionTimeMs;
1015
1016 if (interruptive) {
1017 MetricsLogger.action(getLogMaker()
1018 .setCategory(MetricsEvent.NOTIFICATION_INTERRUPTION)
1019 .setType(MetricsEvent.TYPE_OPEN)
1020 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_INTERRUPTION_MILLIS,
1021 getInterruptionMs(now)));
1022 MetricsLogger.histogram(mContext, "note_interruptive", getInterruptionMs(now));
1023 }
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001024 }
1025
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -04001026 public void setTextChanged(boolean textChanged) {
1027 mTextChanged = textChanged;
1028 }
1029
1030 public void setRecordedInterruption(boolean recorded) {
1031 mRecordedInterruption = recorded;
1032 }
1033
1034 public boolean hasRecordedInterruption() {
1035 return mRecordedInterruption;
1036 }
1037
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001038 public boolean isInterruptive() {
1039 return mIsInterruptive;
1040 }
1041
Julia Reynolds22f02b32016-12-01 15:05:13 -05001042 protected void setPeopleOverride(ArrayList<String> people) {
1043 mPeopleOverride = people;
1044 }
1045
1046 public ArrayList<SnoozeCriterion> getSnoozeCriteria() {
1047 return mSnoozeCriteria;
1048 }
1049
1050 protected void setSnoozeCriteria(ArrayList<SnoozeCriterion> snoozeCriteria) {
1051 mSnoozeCriteria = snoozeCriteria;
1052 }
Chris Wren9eb5e102017-01-26 13:15:06 -05001053
Julia Reynoldsc65656a2018-02-12 09:55:14 -05001054 private void calculateUserSentiment() {
Rohan Shah590e1b22018-04-10 23:48:47 -04001055 if ((getChannel().getUserLockedFields() & USER_LOCKED_IMPORTANCE) != 0
1056 || mIsAppImportanceLocked) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -05001057 mUserSentiment = USER_SENTIMENT_POSITIVE;
1058 }
1059 }
1060
Julia Reynolds503ed942017-10-04 16:04:56 -04001061 private void setUserSentiment(int userSentiment) {
1062 mUserSentiment = userSentiment;
1063 }
1064
1065 public int getUserSentiment() {
1066 return mUserSentiment;
1067 }
1068
1069 public NotificationStats getStats() {
1070 return mStats;
1071 }
1072
1073 public void recordExpanded() {
1074 mStats.setExpanded();
1075 }
1076
1077 public void recordDirectReplied() {
1078 mStats.setDirectReplied();
1079 }
1080
1081 public void recordDismissalSurface(@NotificationStats.DismissalSurface int surface) {
1082 mStats.setDismissalSurface(surface);
1083 }
1084
Julia Reynoldsfd4099d2018-08-21 11:06:06 -04001085 public void recordDismissalSentiment(@NotificationStats.DismissalSentiment int sentiment) {
1086 mStats.setDismissalSentiment(sentiment);
1087 }
1088
Julia Reynolds503ed942017-10-04 16:04:56 -04001089 public void recordSnoozed() {
1090 mStats.setSnoozed();
1091 }
1092
1093 public void recordViewedSettings() {
1094 mStats.setViewedSettings();
1095 }
1096
Kenny Guy23991102018-04-05 21:18:38 +01001097 public void setNumSmartRepliesAdded(int noReplies) {
1098 mNumberOfSmartRepliesAdded = noReplies;
1099 }
1100
1101 public int getNumSmartRepliesAdded() {
1102 return mNumberOfSmartRepliesAdded;
1103 }
1104
1105 public boolean hasSeenSmartReplies() {
1106 return mHasSeenSmartReplies;
1107 }
1108
1109 public void setSeenSmartReplies(boolean hasSeenSmartReplies) {
1110 mHasSeenSmartReplies = hasSeenSmartReplies;
1111 }
1112
Tony Mak628cb932018-06-19 18:30:41 +01001113 public void setSmartActions(ArrayList<Notification.Action> smartActions) {
1114 mSmartActions = smartActions;
1115 }
1116
1117 public ArrayList<Notification.Action> getSmartActions() {
1118 return mSmartActions;
1119 }
1120
Tony Makc9acf672018-07-20 13:58:24 +02001121 public void setSmartReplies(ArrayList<CharSequence> smartReplies) {
1122 mSmartReplies = smartReplies;
1123 }
1124
1125 public ArrayList<CharSequence> getSmartReplies() {
1126 return mSmartReplies;
1127 }
1128
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001129 /**
1130 * @return all {@link Uri} that should have permission granted to whoever
1131 * will be rendering it. This list has already been vetted to only
1132 * include {@link Uri} that the enqueuing app can grant.
1133 */
1134 public @Nullable ArraySet<Uri> getGrantableUris() {
1135 return mGrantableUris;
1136 }
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001137
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001138 /**
1139 * Collect all {@link Uri} that should have permission granted to whoever
1140 * will be rendering it.
1141 */
Julia Reynolds218871e2018-06-13 10:45:21 -04001142 protected void calculateGrantableUris() {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001143 final Notification notification = getNotification();
Jeff Sharkey23b31182018-04-18 21:32:12 -06001144 notification.visitUris((uri) -> {
Julia Reynolds218871e2018-06-13 10:45:21 -04001145 visitGrantableUri(uri, false);
Jeff Sharkey23b31182018-04-18 21:32:12 -06001146 });
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001147
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001148 if (notification.getChannelId() != null) {
1149 NotificationChannel channel = getChannel();
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001150 if (channel != null) {
Julia Reynolds218871e2018-06-13 10:45:21 -04001151 visitGrantableUri(channel.getSound(), (channel.getUserLockedFields()
1152 & NotificationChannel.USER_LOCKED_SOUND) != 0);
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001153 }
1154 }
1155 }
1156
1157 /**
1158 * Note the presence of a {@link Uri} that should have permission granted to
1159 * whoever will be rendering it.
1160 * <p>
1161 * If the enqueuing app has the ability to grant access, it will be added to
1162 * {@link #mGrantableUris}. Otherwise, this will either log or throw
1163 * {@link SecurityException} depending on target SDK of enqueuing app.
1164 */
Julia Reynolds218871e2018-06-13 10:45:21 -04001165 private void visitGrantableUri(Uri uri, boolean userOverriddenUri) {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001166 if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return;
1167
1168 // We can't grant Uri permissions from system
1169 final int sourceUid = sbn.getUid();
1170 if (sourceUid == android.os.Process.SYSTEM_UID) return;
1171
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001172 final long ident = Binder.clearCallingIdentity();
1173 try {
1174 // This will throw SecurityException if caller can't grant
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07001175 mUgmInternal.checkGrantUriPermission(sourceUid, null,
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001176 ContentProvider.getUriWithoutUserId(uri),
1177 Intent.FLAG_GRANT_READ_URI_PERMISSION,
1178 ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid)));
1179
1180 if (mGrantableUris == null) {
1181 mGrantableUris = new ArraySet<>();
1182 }
1183 mGrantableUris.add(uri);
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001184 } catch (SecurityException e) {
Julia Reynolds218871e2018-06-13 10:45:21 -04001185 if (!userOverriddenUri) {
1186 if (mTargetSdkVersion >= Build.VERSION_CODES.P) {
1187 throw e;
1188 } else {
1189 Log.w(TAG, "Ignoring " + uri + " from " + sourceUid + ": " + e.getMessage());
1190 }
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001191 }
1192 } finally {
1193 Binder.restoreCallingIdentity(ident);
1194 }
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001195 }
1196
Chris Wren9eb5e102017-01-26 13:15:06 -05001197 public LogMaker getLogMaker(long now) {
1198 if (mLogMaker == null) {
Chris Wrenb3921792017-06-01 13:34:46 -04001199 // initialize fields that only change on update (so a new record)
Chris Wren9eb5e102017-01-26 13:15:06 -05001200 mLogMaker = new LogMaker(MetricsEvent.VIEW_UNKNOWN)
1201 .setPackageName(sbn.getPackageName())
1202 .addTaggedData(MetricsEvent.NOTIFICATION_ID, sbn.getId())
Chris Wrenb3921792017-06-01 13:34:46 -04001203 .addTaggedData(MetricsEvent.NOTIFICATION_TAG, sbn.getTag())
1204 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID, getChannelIdLogTag());
Chris Wren9eb5e102017-01-26 13:15:06 -05001205 }
Chris Wrenb3921792017-06-01 13:34:46 -04001206 // reset fields that can change between updates, or are used by multiple logs
Chris Wren9eb5e102017-01-26 13:15:06 -05001207 return mLogMaker
Chris Wrena7c1b802017-03-07 10:17:20 -05001208 .clearCategory()
1209 .clearType()
1210 .clearSubtype()
Chris Wren9eb5e102017-01-26 13:15:06 -05001211 .clearTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX)
Chris Wrenb3921792017-06-01 13:34:46 -04001212 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE, mImportance)
1213 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID, getGroupLogTag())
1214 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_SUMMARY,
1215 sbn.getNotification().isGroupSummary() ? 1 : 0)
Chris Wren9eb5e102017-01-26 13:15:06 -05001216 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_CREATE_MILLIS, getLifespanMs(now))
1217 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_UPDATE_MILLIS, getFreshnessMs(now))
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -04001218 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_VISIBLE_MILLIS, getExposureMs(now))
1219 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_INTERRUPTION_MILLIS,
1220 getInterruptionMs(now));
Chris Wren9eb5e102017-01-26 13:15:06 -05001221 }
1222
1223 public LogMaker getLogMaker() {
1224 return getLogMaker(System.currentTimeMillis());
1225 }
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05001226
Julia Reynolds3dfdde02018-10-08 09:17:56 -04001227 public LogMaker getItemLogMaker() {
1228 return getLogMaker().setCategory(MetricsEvent.NOTIFICATION_ITEM);
1229 }
1230
1231 public LogMaker getAdjustmentLogMaker() {
1232 return getLogMaker()
1233 .setCategory(MetricsEvent.NOTIFICATION_ITEM)
1234 .setType(MetricsEvent.NOTIFICATION_ASSISTANT_ADJUSTMENT);
1235 }
1236
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05001237 @VisibleForTesting
1238 static final class Light {
1239 public final int color;
1240 public final int onMs;
1241 public final int offMs;
1242
1243 public Light(int color, int onMs, int offMs) {
1244 this.color = color;
1245 this.onMs = onMs;
1246 this.offMs = offMs;
1247 }
1248
1249 @Override
1250 public boolean equals(Object o) {
1251 if (this == o) return true;
1252 if (o == null || getClass() != o.getClass()) return false;
1253
1254 Light light = (Light) o;
1255
1256 if (color != light.color) return false;
1257 if (onMs != light.onMs) return false;
1258 return offMs == light.offMs;
1259
1260 }
1261
1262 @Override
1263 public int hashCode() {
1264 int result = color;
1265 result = 31 * result + onMs;
1266 result = 31 * result + offMs;
1267 return result;
1268 }
1269
1270 @Override
1271 public String toString() {
1272 return "Light{" +
1273 "color=" + color +
1274 ", onMs=" + onMs +
1275 ", offMs=" + offMs +
1276 '}';
1277 }
1278 }
Chris Wren333a61c2014-05-28 16:40:57 -04001279}