blob: 1a9257cf17fc37854ef75a30ab10d716f8ff7945 [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;
Gustav Sennton1463d832018-11-06 16:12:48 +0000166 /**
167 * This list contains system generated smart actions from NAS, app-generated smart actions are
168 * stored in Notification.actions marked as SEMANTIC_ACTION_CONTEXTUAL_SUGGESTION.
169 */
170 private ArrayList<Notification.Action> mSystemGeneratedSmartActions;
Tony Makc9acf672018-07-20 13:58:24 +0200171 private ArrayList<CharSequence> mSmartReplies;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500172
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400173 private final List<Adjustment> mAdjustments;
Julia Reynolds503ed942017-10-04 16:04:56 -0400174 private final NotificationStats mStats;
175 private int mUserSentiment;
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500176 private boolean mIsInterruptive;
Gus Prevasa3226492018-10-23 11:10:09 -0400177 private boolean mAudiblyAlerted;
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400178 private boolean mTextChanged;
179 private boolean mRecordedInterruption;
Kenny Guy23991102018-04-05 21:18:38 +0100180 private int mNumberOfSmartRepliesAdded;
181 private boolean mHasSeenSmartReplies;
Rohan Shah590e1b22018-04-10 23:48:47 -0400182 /**
183 * Whether this notification (and its channels) should be considered user locked. Used in
184 * conjunction with user sentiment calculation.
185 */
186 private boolean mIsAppImportanceLocked;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600187 private ArraySet<Uri> mGrantableUris;
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400188
Julia Reynolds924eed12017-01-19 09:52:07 -0500189 public NotificationRecord(Context context, StatusBarNotification sbn,
Rohan Shah590e1b22018-04-10 23:48:47 -0400190 NotificationChannel channel) {
Chris Wren333a61c2014-05-28 16:40:57 -0400191 this.sbn = sbn;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600192 mTargetSdkVersion = LocalServices.getService(PackageManagerInternal.class)
193 .getPackageTargetSdkVersion(sbn.getPackageName());
Julia Reynolds218871e2018-06-13 10:45:21 -0400194 mAm = ActivityManager.getService();
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700195 mUgmInternal = LocalServices.getService(UriGrantsManagerInternal.class);
Christoph Studer365e4c32014-09-18 20:35:36 +0200196 mOriginalFlags = sbn.getNotification().flags;
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200197 mRankingTimeMs = calculateRankingTimeMs(0L);
Chris Wren640e3872015-04-21 13:23:18 -0400198 mCreationTimeMs = sbn.getPostTime();
Chris Wren6650e572015-05-15 17:19:25 -0400199 mUpdateTimeMs = mCreationTimeMs;
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400200 mInterruptionTimeMs = mCreationTimeMs;
Chris Wrenbdf33762015-12-04 15:50:51 -0500201 mContext = context;
Chris Wrencdee8cd2016-01-25 17:10:30 -0500202 stats = new NotificationUsageStats.SingleNotificationStats();
Julia Reynolds924eed12017-01-19 09:52:07 -0500203 mChannel = channel;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000204 mPreChannelsNotification = isPreChannelsNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500205 mSound = calculateSound();
206 mVibration = calculateVibration();
207 mAttributes = calculateAttributes();
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400208 mImportance = calculateInitialImportance();
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500209 mLight = calculateLights();
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400210 mAdjustments = new ArrayList<>();
Julia Reynolds503ed942017-10-04 16:04:56 -0400211 mStats = new NotificationStats();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500212 calculateUserSentiment();
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600213 calculateGrantableUris();
Chris Wrenbdf33762015-12-04 15:50:51 -0500214 }
215
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000216 private boolean isPreChannelsNotification() {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600217 if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(getChannel().getId())) {
218 if (mTargetSdkVersion < Build.VERSION_CODES.O) {
219 return true;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000220 }
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000221 }
222 return false;
223 }
224
Julia Reynolds0c299d42016-11-15 14:37:04 -0500225 private Uri calculateSound() {
Chris Wrenbdf33762015-12-04 15:50:51 -0500226 final Notification n = sbn.getNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500227
Tony Mantlerab55f0f2017-06-16 10:50:00 -0700228 // No notification sounds on tv
229 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
230 return null;
231 }
232
Julia Reynolds924eed12017-01-19 09:52:07 -0500233 Uri sound = mChannel.getSound();
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000234 if (mPreChannelsNotification && (getChannel().getUserLockedFields()
Julia Reynolds0c299d42016-11-15 14:37:04 -0500235 & NotificationChannel.USER_LOCKED_SOUND) == 0) {
236
237 final boolean useDefaultSound = (n.defaults & Notification.DEFAULT_SOUND) != 0;
238 if (useDefaultSound) {
239 sound = Settings.System.DEFAULT_NOTIFICATION_URI;
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400240 } else {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500241 sound = n.sound;
242 }
243 }
244 return sound;
245 }
246
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500247 private Light calculateLights() {
248 int defaultLightColor = mContext.getResources().getColor(
249 com.android.internal.R.color.config_defaultNotificationColor);
250 int defaultLightOn = mContext.getResources().getInteger(
251 com.android.internal.R.integer.config_defaultNotificationLedOn);
252 int defaultLightOff = mContext.getResources().getInteger(
253 com.android.internal.R.integer.config_defaultNotificationLedOff);
254
Julia Reynolds529e3322017-02-06 08:33:01 -0500255 int channelLightColor = getChannel().getLightColor() != 0 ? getChannel().getLightColor()
256 : defaultLightColor;
257 Light light = getChannel().shouldShowLights() ? new Light(channelLightColor,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500258 defaultLightOn, defaultLightOff) : null;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000259 if (mPreChannelsNotification
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500260 && (getChannel().getUserLockedFields()
261 & NotificationChannel.USER_LOCKED_LIGHTS) == 0) {
262 final Notification notification = sbn.getNotification();
263 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
264 light = new Light(notification.ledARGB, notification.ledOnMS,
265 notification.ledOffMS);
266 if ((notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
267 light = new Light(defaultLightColor, defaultLightOn,
268 defaultLightOff);
269 }
270 } else {
271 light = null;
272 }
273 }
274 return light;
275 }
276
Julia Reynolds0c299d42016-11-15 14:37:04 -0500277 private long[] calculateVibration() {
278 long[] vibration;
279 final long[] defaultVibration = NotificationManagerService.getLongArray(
280 mContext.getResources(),
281 com.android.internal.R.array.config_defaultNotificationVibePattern,
282 NotificationManagerService.VIBRATE_PATTERN_MAXLEN,
283 NotificationManagerService.DEFAULT_VIBRATE_PATTERN);
284 if (getChannel().shouldVibrate()) {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500285 vibration = getChannel().getVibrationPattern() == null
286 ? defaultVibration : getChannel().getVibrationPattern();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500287 } else {
288 vibration = null;
289 }
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000290 if (mPreChannelsNotification
Julia Reynolds0c299d42016-11-15 14:37:04 -0500291 && (getChannel().getUserLockedFields()
292 & NotificationChannel.USER_LOCKED_VIBRATION) == 0) {
293 final Notification notification = sbn.getNotification();
294 final boolean useDefaultVibrate =
295 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
296 if (useDefaultVibrate) {
297 vibration = defaultVibration;
298 } else {
299 vibration = notification.vibrate;
300 }
301 }
302 return vibration;
303 }
304
305 private AudioAttributes calculateAttributes() {
306 final Notification n = sbn.getNotification();
Julia Reynolds619a69f2017-01-27 15:11:38 -0500307 AudioAttributes attributes = getChannel().getAudioAttributes();
308 if (attributes == null) {
309 attributes = Notification.AUDIO_ATTRIBUTES_DEFAULT;
310 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500311
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000312 if (mPreChannelsNotification
Julia Reynolds619a69f2017-01-27 15:11:38 -0500313 && (getChannel().getUserLockedFields()
314 & NotificationChannel.USER_LOCKED_SOUND) == 0) {
315 if (n.audioAttributes != null) {
316 // prefer audio attributes to stream type
317 attributes = n.audioAttributes;
318 } else if (n.audioStreamType >= 0
319 && n.audioStreamType < AudioSystem.getNumStreamTypes()) {
320 // the stream type is valid, use it
321 attributes = new AudioAttributes.Builder()
322 .setInternalLegacyStreamType(n.audioStreamType)
323 .build();
324 } else if (n.audioStreamType != AudioSystem.STREAM_DEFAULT) {
325 Log.w(TAG, String.format("Invalid stream type: %d", n.audioStreamType));
326 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500327 }
328 return attributes;
329 }
330
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400331 private int calculateInitialImportance() {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500332 final Notification n = sbn.getNotification();
333 int importance = getChannel().getImportance();
334 int requestedImportance = IMPORTANCE_DEFAULT;
Chris Wrenbdf33762015-12-04 15:50:51 -0500335
336 // Migrate notification flags to scores
337 if (0 != (n.flags & Notification.FLAG_HIGH_PRIORITY)) {
338 n.priority = Notification.PRIORITY_MAX;
339 }
340
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500341 n.priority = NotificationManagerService.clamp(n.priority, Notification.PRIORITY_MIN,
342 Notification.PRIORITY_MAX);
Chris Wrenbdf33762015-12-04 15:50:51 -0500343 switch (n.priority) {
344 case Notification.PRIORITY_MIN:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500345 requestedImportance = IMPORTANCE_MIN;
Julia Reynoldsf0f629f2016-02-25 09:34:04 -0500346 break;
Chris Wrenbdf33762015-12-04 15:50:51 -0500347 case Notification.PRIORITY_LOW:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500348 requestedImportance = IMPORTANCE_LOW;
Chris Wrenbdf33762015-12-04 15:50:51 -0500349 break;
350 case Notification.PRIORITY_DEFAULT:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500351 requestedImportance = IMPORTANCE_DEFAULT;
Chris Wrenbdf33762015-12-04 15:50:51 -0500352 break;
353 case Notification.PRIORITY_HIGH:
Chris Wrenbdf33762015-12-04 15:50:51 -0500354 case Notification.PRIORITY_MAX:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500355 requestedImportance = IMPORTANCE_HIGH;
Chris Wrenbdf33762015-12-04 15:50:51 -0500356 break;
357 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500358 stats.requestedImportance = requestedImportance;
359 stats.isNoisy = mSound != null || mVibration != null;
Chris Wrenbdf33762015-12-04 15:50:51 -0500360
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000361 if (mPreChannelsNotification
Julia Reynoldsa917a112017-03-21 11:09:14 -0400362 && (importance == IMPORTANCE_UNSPECIFIED
363 || (getChannel().getUserLockedFields()
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500364 & USER_LOCKED_IMPORTANCE) == 0)) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500365 if (!stats.isNoisy && requestedImportance > IMPORTANCE_LOW) {
366 requestedImportance = IMPORTANCE_LOW;
Julia Reynolds83fa1072016-02-17 09:10:19 -0500367 }
Julia Reynolds83fa1072016-02-17 09:10:19 -0500368
Julia Reynolds0c299d42016-11-15 14:37:04 -0500369 if (stats.isNoisy) {
370 if (requestedImportance < IMPORTANCE_DEFAULT) {
371 requestedImportance = IMPORTANCE_DEFAULT;
372 }
373 }
374
375 if (n.fullScreenIntent != null) {
376 requestedImportance = IMPORTANCE_HIGH;
377 }
378 importance = requestedImportance;
Chris Wrenbdf33762015-12-04 15:50:51 -0500379 }
380
Chris Wrencdee8cd2016-01-25 17:10:30 -0500381 stats.naturalImportance = importance;
Chris Wrenbdf33762015-12-04 15:50:51 -0500382 return importance;
Chris Wren333a61c2014-05-28 16:40:57 -0400383 }
384
385 // copy any notes that the ranking system may have made before the update
386 public void copyRankingInformation(NotificationRecord previous) {
387 mContactAffinity = previous.mContactAffinity;
388 mRecentlyIntrusive = previous.mRecentlyIntrusive;
Chris Wren54bbef42014-07-09 18:37:56 -0400389 mPackagePriority = previous.mPackagePriority;
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400390 mPackageVisibility = previous.mPackageVisibility;
Chris Wren333a61c2014-05-28 16:40:57 -0400391 mIntercept = previous.mIntercept;
Beverly5a20a5e2018-03-06 15:02:44 -0500392 mHidden = previous.mHidden;
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200393 mRankingTimeMs = calculateRankingTimeMs(previous.getRankingTimeMs());
Chris Wren640e3872015-04-21 13:23:18 -0400394 mCreationTimeMs = previous.mCreationTimeMs;
Chris Wren6650e572015-05-15 17:19:25 -0400395 mVisibleSinceMs = previous.mVisibleSinceMs;
Selim Cinek5b03ce92016-05-18 15:16:58 -0700396 if (previous.sbn.getOverrideGroupKey() != null && !sbn.isAppGroup()) {
Chris Wren8a1638f2016-05-02 16:19:14 -0400397 sbn.setOverrideGroupKey(previous.sbn.getOverrideGroupKey());
398 }
Chris Wren1f602dc2016-04-11 10:33:46 -0400399 // Don't copy importance information or mGlobalSortKey, recompute them.
Chris Wren333a61c2014-05-28 16:40:57 -0400400 }
401
402 public Notification getNotification() { return sbn.getNotification(); }
403 public int getFlags() { return sbn.getNotification().flags; }
Chris Wrenda4bd202014-09-04 15:53:52 -0400404 public UserHandle getUser() { return sbn.getUser(); }
Chris Wren333a61c2014-05-28 16:40:57 -0400405 public String getKey() { return sbn.getKey(); }
Chris Wrenda4bd202014-09-04 15:53:52 -0400406 /** @deprecated Use {@link #getUser()} instead. */
407 public int getUserId() { return sbn.getUserId(); }
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600408 public int getUid() { return sbn.getUid(); }
Chris Wren333a61c2014-05-28 16:40:57 -0400409
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800410 void dump(ProtoOutputStream proto, long fieldId, boolean redact, int state) {
411 final long token = proto.start(fieldId);
412
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500413 proto.write(NotificationRecordProto.KEY, sbn.getKey());
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800414 proto.write(NotificationRecordProto.STATE, state);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500415 if (getChannel() != null) {
416 proto.write(NotificationRecordProto.CHANNEL_ID, getChannel().getId());
417 }
418 proto.write(NotificationRecordProto.CAN_SHOW_LIGHT, getLight() != null);
419 proto.write(NotificationRecordProto.CAN_VIBRATE, getVibration() != null);
420 proto.write(NotificationRecordProto.FLAGS, sbn.getNotification().flags);
421 proto.write(NotificationRecordProto.GROUP_KEY, getGroupKey());
422 proto.write(NotificationRecordProto.IMPORTANCE, getImportance());
423 if (getSound() != null) {
424 proto.write(NotificationRecordProto.SOUND, getSound().toString());
425 }
426 if (getAudioAttributes() != null) {
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800427 getAudioAttributes().writeToProto(proto, NotificationRecordProto.AUDIO_ATTRIBUTES);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500428 }
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800429
430 proto.end(token);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500431 }
432
Dan Sandler0a2308e2017-05-30 19:50:42 -0400433 String formatRemoteViews(RemoteViews rv) {
434 if (rv == null) return "null";
435 return String.format("%s/0x%08x (%d bytes): %s",
436 rv.getPackage(), rv.getLayoutId(), rv.estimateMemoryUsage(), rv.toString());
437 }
438
Dan Sandlera1770312015-07-10 13:59:29 -0400439 void dump(PrintWriter pw, String prefix, Context baseContext, boolean redact) {
Chris Wren333a61c2014-05-28 16:40:57 -0400440 final Notification notification = sbn.getNotification();
Dan Sandlerd63f9322015-05-06 15:18:49 -0400441 final Icon icon = notification.getSmallIcon();
442 String iconStr = String.valueOf(icon);
443 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
444 iconStr += " / " + idDebugString(baseContext, icon.getResPackage(), icon.getResId());
445 }
Chris Wren333a61c2014-05-28 16:40:57 -0400446 pw.println(prefix + this);
Dan Sandler0a2308e2017-05-30 19:50:42 -0400447 prefix = prefix + " ";
Julia Reynoldsa917a112017-03-21 11:09:14 -0400448 pw.println(prefix + "uid=" + sbn.getUid() + " userId=" + sbn.getUserId());
449 pw.println(prefix + "icon=" + iconStr);
Julia Reynolds4db59552017-06-30 13:34:01 -0400450 pw.println(prefix + "flags=0x" + Integer.toHexString(notification.flags));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400451 pw.println(prefix + "pri=" + notification.priority);
452 pw.println(prefix + "key=" + sbn.getKey());
Julia Reynolds503ed942017-10-04 16:04:56 -0400453 pw.println(prefix + "seen=" + mStats.hasSeen());
Julia Reynoldsa917a112017-03-21 11:09:14 -0400454 pw.println(prefix + "groupKey=" + getGroupKey());
455 pw.println(prefix + "fullscreenIntent=" + notification.fullScreenIntent);
456 pw.println(prefix + "contentIntent=" + notification.contentIntent);
457 pw.println(prefix + "deleteIntent=" + notification.deleteIntent);
Aaron Heuckrothb4d6aa72018-07-02 14:58:33 -0400458 pw.println(prefix + "number=" + notification.number);
459 pw.println(prefix + "groupAlertBehavior=" + notification.getGroupAlertBehavior());
Dan Sandler0b4ceb32017-03-29 14:13:55 -0400460
461 pw.print(prefix + "tickerText=");
462 if (!TextUtils.isEmpty(notification.tickerText)) {
463 final String ticker = notification.tickerText.toString();
464 if (redact) {
465 // if the string is long enough, we allow ourselves a few bytes for debugging
466 pw.print(ticker.length() > 16 ? ticker.substring(0,8) : "");
467 pw.println("...");
468 } else {
469 pw.println(ticker);
470 }
471 } else {
472 pw.println("null");
473 }
Dan Sandler0a2308e2017-05-30 19:50:42 -0400474 pw.println(prefix + "contentView=" + formatRemoteViews(notification.contentView));
475 pw.println(prefix + "bigContentView=" + formatRemoteViews(notification.bigContentView));
476 pw.println(prefix + "headsUpContentView="
477 + formatRemoteViews(notification.headsUpContentView));
478 pw.print(prefix + String.format("color=0x%08x", notification.color));
Julia Reynoldsbad42972017-04-25 13:52:49 -0400479 pw.println(prefix + "timeout="
480 + TimeUtils.formatForLogging(notification.getTimeoutAfter()));
Chris Wren333a61c2014-05-28 16:40:57 -0400481 if (notification.actions != null && notification.actions.length > 0) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400482 pw.println(prefix + "actions={");
Chris Wren333a61c2014-05-28 16:40:57 -0400483 final int N = notification.actions.length;
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500484 for (int i = 0; i < N; i++) {
Chris Wren333a61c2014-05-28 16:40:57 -0400485 final Notification.Action action = notification.actions[i];
Chris Wren1ac52a92016-02-24 14:54:52 -0500486 if (action != null) {
487 pw.println(String.format("%s [%d] \"%s\" -> %s",
488 prefix,
489 i,
490 action.title,
491 action.actionIntent == null ? "null" : action.actionIntent.toString()
492 ));
493 }
Chris Wren333a61c2014-05-28 16:40:57 -0400494 }
495 pw.println(prefix + " }");
496 }
497 if (notification.extras != null && notification.extras.size() > 0) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400498 pw.println(prefix + "extras={");
Chris Wren333a61c2014-05-28 16:40:57 -0400499 for (String key : notification.extras.keySet()) {
500 pw.print(prefix + " " + key + "=");
501 Object val = notification.extras.get(key);
502 if (val == null) {
503 pw.println("null");
504 } else {
505 pw.print(val.getClass().getSimpleName());
Dan Sandlera1770312015-07-10 13:59:29 -0400506 if (redact && (val instanceof CharSequence || val instanceof String)) {
Chris Wren333a61c2014-05-28 16:40:57 -0400507 // redact contents from bugreports
508 } else if (val instanceof Bitmap) {
509 pw.print(String.format(" (%dx%d)",
510 ((Bitmap) val).getWidth(),
511 ((Bitmap) val).getHeight()));
512 } else if (val.getClass().isArray()) {
513 final int N = Array.getLength(val);
Dan Sandlera1770312015-07-10 13:59:29 -0400514 pw.print(" (" + N + ")");
515 if (!redact) {
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500516 for (int j = 0; j < N; j++) {
Dan Sandlera1770312015-07-10 13:59:29 -0400517 pw.println();
518 pw.print(String.format("%s [%d] %s",
519 prefix, j, String.valueOf(Array.get(val, j))));
520 }
521 }
Chris Wren333a61c2014-05-28 16:40:57 -0400522 } else {
523 pw.print(" (" + String.valueOf(val) + ")");
524 }
525 pw.println();
526 }
527 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400528 pw.println(prefix + "}");
Chris Wren333a61c2014-05-28 16:40:57 -0400529 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400530 pw.println(prefix + "stats=" + stats.toString());
531 pw.println(prefix + "mContactAffinity=" + mContactAffinity);
532 pw.println(prefix + "mRecentlyIntrusive=" + mRecentlyIntrusive);
533 pw.println(prefix + "mPackagePriority=" + mPackagePriority);
534 pw.println(prefix + "mPackageVisibility=" + mPackageVisibility);
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400535 pw.println(prefix + "mSystemImportance="
536 + NotificationListenerService.Ranking.importanceToString(mSystemImportance));
537 pw.println(prefix + "mAsstImportance="
538 + NotificationListenerService.Ranking.importanceToString(mAssistantImportance));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400539 pw.println(prefix + "mImportance="
Chris Wrenbdf33762015-12-04 15:50:51 -0500540 + NotificationListenerService.Ranking.importanceToString(mImportance));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400541 pw.println(prefix + "mImportanceExplanation=" + mImportanceExplanation);
Rohan Shah590e1b22018-04-10 23:48:47 -0400542 pw.println(prefix + "mIsAppImportanceLocked=" + mIsAppImportanceLocked);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400543 pw.println(prefix + "mIntercept=" + mIntercept);
Beverly5a20a5e2018-03-06 15:02:44 -0500544 pw.println(prefix + "mHidden==" + mHidden);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400545 pw.println(prefix + "mGlobalSortKey=" + mGlobalSortKey);
546 pw.println(prefix + "mRankingTimeMs=" + mRankingTimeMs);
547 pw.println(prefix + "mCreationTimeMs=" + mCreationTimeMs);
548 pw.println(prefix + "mVisibleSinceMs=" + mVisibleSinceMs);
549 pw.println(prefix + "mUpdateTimeMs=" + mUpdateTimeMs);
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400550 pw.println(prefix + "mInterruptionTimeMs=" + mInterruptionTimeMs);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400551 pw.println(prefix + "mSuppressedVisualEffects= " + mSuppressedVisualEffects);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000552 if (mPreChannelsNotification) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400553 pw.println(prefix + String.format("defaults=0x%08x flags=0x%08x",
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500554 notification.defaults, notification.flags));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400555 pw.println(prefix + "n.sound=" + notification.sound);
556 pw.println(prefix + "n.audioStreamType=" + notification.audioStreamType);
557 pw.println(prefix + "n.audioAttributes=" + notification.audioAttributes);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500558 pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d",
559 notification.ledARGB, notification.ledOnMS, notification.ledOffMS));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400560 pw.println(prefix + "vibrate=" + Arrays.toString(notification.vibrate));
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500561 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400562 pw.println(prefix + "mSound= " + mSound);
563 pw.println(prefix + "mVibration= " + mVibration);
564 pw.println(prefix + "mAttributes= " + mAttributes);
565 pw.println(prefix + "mLight= " + mLight);
566 pw.println(prefix + "mShowBadge=" + mShowBadge);
Julia Reynolds4db59552017-06-30 13:34:01 -0400567 pw.println(prefix + "mColorized=" + notification.isColorized());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500568 pw.println(prefix + "mIsInterruptive=" + mIsInterruptive);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400569 pw.println(prefix + "effectiveNotificationChannel=" + getChannel());
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500570 if (getPeopleOverride() != null) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400571 pw.println(prefix + "overridePeople= " + TextUtils.join(",", getPeopleOverride()));
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500572 }
573 if (getSnoozeCriteria() != null) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400574 pw.println(prefix + "snoozeCriteria=" + TextUtils.join(",", getSnoozeCriteria()));
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500575 }
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400576 pw.println(prefix + "mAdjustments=" + mAdjustments);
Chris Wren333a61c2014-05-28 16:40:57 -0400577 }
578
579
580 static String idDebugString(Context baseContext, String packageName, int id) {
581 Context c;
582
583 if (packageName != null) {
584 try {
585 c = baseContext.createPackageContext(packageName, 0);
586 } catch (NameNotFoundException e) {
587 c = baseContext;
588 }
589 } else {
590 c = baseContext;
591 }
592
593 Resources r = c.getResources();
594 try {
595 return r.getResourceName(id);
596 } catch (Resources.NotFoundException e) {
597 return "<name unknown>";
598 }
599 }
600
601 @Override
602 public final String toString() {
603 return String.format(
Julia Reynolds85769912016-10-25 09:08:57 -0400604 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s importance=%d key=%s" +
Rohan Shah590e1b22018-04-10 23:48:47 -0400605 "appImportanceLocked=%s: %s)",
Chris Wren333a61c2014-05-28 16:40:57 -0400606 System.identityHashCode(this),
607 this.sbn.getPackageName(), this.sbn.getUser(), this.sbn.getId(),
Julia Reynolds4db59552017-06-30 13:34:01 -0400608 this.sbn.getTag(), this.mImportance, this.sbn.getKey(),
Rohan Shah590e1b22018-04-10 23:48:47 -0400609 mIsAppImportanceLocked, this.sbn.getNotification());
Chris Wren333a61c2014-05-28 16:40:57 -0400610 }
611
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400612 public void addAdjustment(Adjustment adjustment) {
613 synchronized (mAdjustments) {
614 mAdjustments.add(adjustment);
615 }
616 }
617
618 public void applyAdjustments() {
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400619 long now = System.currentTimeMillis();
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400620 synchronized (mAdjustments) {
621 for (Adjustment adjustment: mAdjustments) {
622 Bundle signals = adjustment.getSignals();
623 if (signals.containsKey(Adjustment.KEY_PEOPLE)) {
624 final ArrayList<String> people =
625 adjustment.getSignals().getStringArrayList(Adjustment.KEY_PEOPLE);
626 setPeopleOverride(people);
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400627 MetricsLogger.action(getAdjustmentLogMaker()
628 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_PEOPLE, people.size()));
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400629 }
630 if (signals.containsKey(Adjustment.KEY_SNOOZE_CRITERIA)) {
631 final ArrayList<SnoozeCriterion> snoozeCriterionList =
632 adjustment.getSignals().getParcelableArrayList(
633 Adjustment.KEY_SNOOZE_CRITERIA);
634 setSnoozeCriteria(snoozeCriterionList);
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400635 MetricsLogger.action(getAdjustmentLogMaker()
636 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_SNOOZE_CRITERIA,
637 snoozeCriterionList.size()));
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400638 }
639 if (signals.containsKey(Adjustment.KEY_GROUP_KEY)) {
640 final String groupOverrideKey =
641 adjustment.getSignals().getString(Adjustment.KEY_GROUP_KEY);
642 setOverrideGroupKey(groupOverrideKey);
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400643 MetricsLogger.action(getAdjustmentLogMaker()
644 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_GROUP_KEY,
645 groupOverrideKey));
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400646 }
Julia Reynolds503ed942017-10-04 16:04:56 -0400647 if (signals.containsKey(Adjustment.KEY_USER_SENTIMENT)) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500648 // Only allow user sentiment update from assistant if user hasn't already
649 // expressed a preference for this channel
Rohan Shah590e1b22018-04-10 23:48:47 -0400650 if (!mIsAppImportanceLocked
651 && (getChannel().getUserLockedFields() & USER_LOCKED_IMPORTANCE) == 0) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500652 setUserSentiment(adjustment.getSignals().getInt(
653 Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEUTRAL));
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400654 MetricsLogger.action(getAdjustmentLogMaker()
655 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_USER_SENTIMENT,
656 getUserSentiment()));
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500657 }
Julia Reynolds503ed942017-10-04 16:04:56 -0400658 }
Tony Mak628cb932018-06-19 18:30:41 +0100659 if (signals.containsKey(Adjustment.KEY_SMART_ACTIONS)) {
Gustav Sennton1463d832018-11-06 16:12:48 +0000660 setSystemGeneratedSmartActions(
661 signals.getParcelableArrayList(Adjustment.KEY_SMART_ACTIONS));
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400662 MetricsLogger.action(getAdjustmentLogMaker()
663 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_SMART_ACTIONS,
Gustav Sennton1463d832018-11-06 16:12:48 +0000664 getSystemGeneratedSmartActions().size()));
Tony Mak628cb932018-06-19 18:30:41 +0100665 }
Tony Makc9acf672018-07-20 13:58:24 +0200666 if (signals.containsKey(Adjustment.KEY_SMART_REPLIES)) {
667 setSmartReplies(signals.getCharSequenceArrayList(Adjustment.KEY_SMART_REPLIES));
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400668 MetricsLogger.action(getAdjustmentLogMaker()
669 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_SMART_REPLIES,
670 getSmartReplies().size()));
Tony Makc9acf672018-07-20 13:58:24 +0200671 }
Tony Mak8a8ddad2018-11-13 17:21:33 +0000672 }
673 applyImportanceFromAdjustments();
674 }
675 }
676
677 /**
678 * Update importance from the adjustment.
679 */
680 public void applyImportanceFromAdjustments() {
681 synchronized (mAdjustments) {
682 for (Adjustment adjustment : mAdjustments) {
683 Bundle signals = adjustment.getSignals();
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400684 if (signals.containsKey(Adjustment.KEY_IMPORTANCE)) {
685 int importance = signals.getInt(Adjustment.KEY_IMPORTANCE);
686 importance = Math.max(IMPORTANCE_UNSPECIFIED, importance);
687 importance = Math.min(IMPORTANCE_HIGH, importance);
688 setAssistantImportance(importance);
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400689 MetricsLogger.action(getAdjustmentLogMaker()
690 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_IMPORTANCE,
691 importance));
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400692 }
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400693 }
694 }
695 }
696
Rohan Shah590e1b22018-04-10 23:48:47 -0400697 public void setIsAppImportanceLocked(boolean isAppImportanceLocked) {
698 mIsAppImportanceLocked = isAppImportanceLocked;
699 calculateUserSentiment();
700 }
701
Chris Wren333a61c2014-05-28 16:40:57 -0400702 public void setContactAffinity(float contactAffinity) {
703 mContactAffinity = contactAffinity;
704 }
705
706 public float getContactAffinity() {
707 return mContactAffinity;
708 }
709
John Spurlock1d881a12015-03-18 19:21:54 -0400710 public void setRecentlyIntrusive(boolean recentlyIntrusive) {
Chris Wren333a61c2014-05-28 16:40:57 -0400711 mRecentlyIntrusive = recentlyIntrusive;
Julia Reynolds309d1c82017-05-03 16:00:20 -0400712 if (recentlyIntrusive) {
713 mLastIntrusive = System.currentTimeMillis();
714 }
Chris Wren333a61c2014-05-28 16:40:57 -0400715 }
716
717 public boolean isRecentlyIntrusive() {
718 return mRecentlyIntrusive;
719 }
720
Julia Reynolds309d1c82017-05-03 16:00:20 -0400721 public long getLastIntrusive() {
722 return mLastIntrusive;
723 }
724
Chris Wren54bbef42014-07-09 18:37:56 -0400725 public void setPackagePriority(int packagePriority) {
John Spurlock6ac5f8d2014-07-18 11:27:54 -0400726 mPackagePriority = packagePriority;
Chris Wren54bbef42014-07-09 18:37:56 -0400727 }
728
729 public int getPackagePriority() {
730 return mPackagePriority;
731 }
732
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400733 public void setPackageVisibilityOverride(int packageVisibility) {
734 mPackageVisibility = packageVisibility;
735 }
736
737 public int getPackageVisibilityOverride() {
738 return mPackageVisibility;
739 }
740
Julia Reynoldsef37f282016-02-12 09:11:27 -0500741 private String getUserExplanation() {
742 if (mUserExplanation == null) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500743 mUserExplanation = mContext.getResources().getString(
744 com.android.internal.R.string.importance_from_user);
Chris Wrenbdf33762015-12-04 15:50:51 -0500745 }
Julia Reynoldsef37f282016-02-12 09:11:27 -0500746 return mUserExplanation;
Chris Wrenbdf33762015-12-04 15:50:51 -0500747 }
748
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400749 /**
750 * Sets the importance value the system thinks the record should have.
751 * e.g. bumping up foreground service notifications or people to people notifications.
752 */
753 public void setSystemImportance(int importance) {
754 mSystemImportance = importance;
755 calculateImportance();
Chris Wrenbdf33762015-12-04 15:50:51 -0500756 }
757
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400758 /**
759 * Sets the importance value the
760 * {@link android.service.notification.NotificationAssistantService} thinks the record should
761 * have.
762 */
763 public void setAssistantImportance(int importance) {
764 mAssistantImportance = importance;
765 calculateImportance();
Julia Reynolds5d25ee72015-11-20 15:38:20 -0500766 }
767
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400768 /**
769 * Recalculates the importance of the record after fields affecting importance have changed
770 */
771 protected void calculateImportance() {
772 mImportance = calculateInitialImportance();
773 mImportanceExplanation = "app";
Julia Reynolds48a6ed92018-10-22 12:52:03 -0400774 if (getChannel().hasUserSetImportance()) {
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400775 mImportanceExplanation = "user";
Chris Wrenbdf33762015-12-04 15:50:51 -0500776 }
Julia Reynolds48a6ed92018-10-22 12:52:03 -0400777 if (!getChannel().hasUserSetImportance() && mAssistantImportance != IMPORTANCE_UNSPECIFIED) {
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400778 mImportance = mAssistantImportance;
779 mImportanceExplanation = "asst";
780 }
781 if (mSystemImportance != IMPORTANCE_UNSPECIFIED) {
782 mImportance = mSystemImportance;
783 mImportanceExplanation = "system";
784 }
Chris Wrenbdf33762015-12-04 15:50:51 -0500785 }
786
787 public int getImportance() {
788 return mImportance;
789 }
790
791 public CharSequence getImportanceExplanation() {
792 return mImportanceExplanation;
793 }
794
Chris Wren333a61c2014-05-28 16:40:57 -0400795 public boolean setIntercepted(boolean intercept) {
796 mIntercept = intercept;
797 return mIntercept;
798 }
799
Brad Stenning9a8b2c82018-08-03 14:14:26 -0700800 /**
801 * Set to affect global sort key.
802 *
803 * @param criticality used in a string based sort thus 0 is the most critical
804 */
805 public void setCriticality(int criticality) {
806 mCriticality = criticality;
807 }
808
809 public int getCriticality() {
810 return mCriticality;
811 }
812
Chris Wren333a61c2014-05-28 16:40:57 -0400813 public boolean isIntercepted() {
814 return mIntercept;
815 }
816
Beverly5a20a5e2018-03-06 15:02:44 -0500817 public void setHidden(boolean hidden) {
818 mHidden = hidden;
819 }
820
821 public boolean isHidden() {
822 return mHidden;
823 }
824
825
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500826 public void setSuppressedVisualEffects(int effects) {
827 mSuppressedVisualEffects = effects;
828 }
829
830 public int getSuppressedVisualEffects() {
831 return mSuppressedVisualEffects;
832 }
833
John Spurlock312d1d02014-07-08 10:24:57 -0400834 public boolean isCategory(String category) {
John Spurlockbfa5dc42014-07-28 23:30:45 -0400835 return Objects.equals(getNotification().category, category);
836 }
837
John Spurlockbfa5dc42014-07-28 23:30:45 -0400838 public boolean isAudioAttributesUsage(int usage) {
Julia Reynolds51eb78f82018-03-07 07:35:21 -0500839 return mAttributes != null && mAttributes.getUsage() == usage;
John Spurlock312d1d02014-07-08 10:24:57 -0400840 }
841
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200842 /**
843 * Returns the timestamp to use for time-based sorting in the ranker.
844 */
845 public long getRankingTimeMs() {
846 return mRankingTimeMs;
847 }
848
849 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400850 * @param now this current time in milliseconds.
851 * @returns the number of milliseconds since the most recent update, or the post time if none.
Chris Wren6650e572015-05-15 17:19:25 -0400852 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400853 public int getFreshnessMs(long now) {
854 return (int) (now - mUpdateTimeMs);
Chris Wren6650e572015-05-15 17:19:25 -0400855 }
856
857 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400858 * @param now this current time in milliseconds.
859 * @returns the number of milliseconds since the the first post, ignoring updates.
Chris Wren640e3872015-04-21 13:23:18 -0400860 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400861 public int getLifespanMs(long now) {
862 return (int) (now - mCreationTimeMs);
Chris Wren640e3872015-04-21 13:23:18 -0400863 }
864
865 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400866 * @param now this current time in milliseconds.
867 * @returns the number of milliseconds since the most recent visibility event, or 0 if never.
Chris Wren6650e572015-05-15 17:19:25 -0400868 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400869 public int getExposureMs(long now) {
870 return mVisibleSinceMs == 0 ? 0 : (int) (now - mVisibleSinceMs);
Chris Wren6650e572015-05-15 17:19:25 -0400871 }
872
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400873 public int getInterruptionMs(long now) {
874 return (int) (now - mInterruptionTimeMs);
875 }
876
Chris Wren6650e572015-05-15 17:19:25 -0400877 /**
878 * Set the visibility of the notification.
879 */
Dieter Hsud39f0d52018-04-14 02:08:30 +0800880 public void setVisibility(boolean visible, int rank, int count) {
Chris Wren6650e572015-05-15 17:19:25 -0400881 final long now = System.currentTimeMillis();
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400882 mVisibleSinceMs = visible ? now : mVisibleSinceMs;
Chris Wren6650e572015-05-15 17:19:25 -0400883 stats.onVisibilityChanged(visible);
Chris Wren9eb5e102017-01-26 13:15:06 -0500884 MetricsLogger.action(getLogMaker(now)
885 .setCategory(MetricsEvent.NOTIFICATION_ITEM)
886 .setType(visible ? MetricsEvent.TYPE_OPEN : MetricsEvent.TYPE_CLOSE)
Dieter Hsud39f0d52018-04-14 02:08:30 +0800887 .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, rank)
888 .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, count));
Chris Wren9eb5e102017-01-26 13:15:06 -0500889 if (visible) {
Julia Reynolds503ed942017-10-04 16:04:56 -0400890 setSeen();
Chris Wren9eb5e102017-01-26 13:15:06 -0500891 MetricsLogger.histogram(mContext, "note_freshness", getFreshnessMs(now));
892 }
Chris Wren6650e572015-05-15 17:19:25 -0400893 EventLogTags.writeNotificationVisibility(getKey(), visible ? 1 : 0,
Chris Wren9eb5e102017-01-26 13:15:06 -0500894 getLifespanMs(now),
895 getFreshnessMs(now),
Chris Wrend1dbc922015-06-19 17:51:16 -0400896 0, // exposure time
897 rank);
Chris Wren6650e572015-05-15 17:19:25 -0400898 }
899
900 /**
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200901 * @param previousRankingTimeMs for updated notifications, {@link #getRankingTimeMs()}
902 * of the previous notification record, 0 otherwise
903 */
904 private long calculateRankingTimeMs(long previousRankingTimeMs) {
905 Notification n = getNotification();
906 // Take developer provided 'when', unless it's in the future.
907 if (n.when != 0 && n.when <= sbn.getPostTime()) {
908 return n.when;
909 }
910 // If we've ranked a previous instance with a timestamp, inherit it. This case is
911 // important in order to have ranking stability for updating notifications.
912 if (previousRankingTimeMs > 0) {
913 return previousRankingTimeMs;
914 }
915 return sbn.getPostTime();
916 }
Chris Wren1031c972014-07-23 13:11:45 +0000917
Christoph Studercd4adf82014-08-19 17:50:49 +0200918 public void setGlobalSortKey(String globalSortKey) {
919 mGlobalSortKey = globalSortKey;
Chris Wren1031c972014-07-23 13:11:45 +0000920 }
921
Christoph Studercd4adf82014-08-19 17:50:49 +0200922 public String getGlobalSortKey() {
923 return mGlobalSortKey;
Chris Wren1031c972014-07-23 13:11:45 +0000924 }
925
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700926 /** Check if any of the listeners have marked this notification as seen by the user. */
927 public boolean isSeen() {
Julia Reynolds503ed942017-10-04 16:04:56 -0400928 return mStats.hasSeen();
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700929 }
930
931 /** Mark the notification as seen by the user. */
932 public void setSeen() {
Julia Reynolds503ed942017-10-04 16:04:56 -0400933 mStats.setSeen();
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400934 if (mTextChanged) {
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400935 setInterruptive(true);
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400936 }
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700937 }
938
Chris Wren1031c972014-07-23 13:11:45 +0000939 public void setAuthoritativeRank(int authoritativeRank) {
940 mAuthoritativeRank = authoritativeRank;
941 }
942
943 public int getAuthoritativeRank() {
944 return mAuthoritativeRank;
945 }
946
947 public String getGroupKey() {
948 return sbn.getGroupKey();
949 }
Chris Wren47633422016-01-22 09:56:59 -0500950
Chris Wrenb3921792017-06-01 13:34:46 -0400951 public void setOverrideGroupKey(String overrideGroupKey) {
952 sbn.setOverrideGroupKey(overrideGroupKey);
953 mGroupLogTag = null;
954 }
955
956 private String getGroupLogTag() {
957 if (mGroupLogTag == null) {
958 mGroupLogTag = shortenTag(sbn.getGroup());
959 }
960 return mGroupLogTag;
961 }
962
963 private String getChannelIdLogTag() {
964 if (mChannelIdLogTag == null) {
965 mChannelIdLogTag = shortenTag(mChannel.getId());
966 }
967 return mChannelIdLogTag;
968 }
969
970 private String shortenTag(String longTag) {
971 if (longTag == null) {
972 return null;
973 }
974 if (longTag.length() < MAX_LOGTAG_LENGTH) {
975 return longTag;
976 } else {
977 return longTag.substring(0, MAX_LOGTAG_LENGTH - 8) + "-" +
978 Integer.toHexString(longTag.hashCode());
979 }
980 }
981
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400982 public NotificationChannel getChannel() {
Julia Reynolds924eed12017-01-19 09:52:07 -0500983 return mChannel;
Julia Reynolds22f02b32016-12-01 15:05:13 -0500984 }
985
Rohan Shah590e1b22018-04-10 23:48:47 -0400986 /**
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400987 * @see PreferencesHelper#getIsAppImportanceLocked(String, int)
Rohan Shah590e1b22018-04-10 23:48:47 -0400988 */
989 public boolean getIsAppImportanceLocked() {
990 return mIsAppImportanceLocked;
991 }
992
Julia Reynolds924eed12017-01-19 09:52:07 -0500993 protected void updateNotificationChannel(NotificationChannel channel) {
994 if (channel != null) {
995 mChannel = channel;
Julia Reynolds22f02b32016-12-01 15:05:13 -0500996 calculateImportance();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500997 calculateUserSentiment();
Julia Reynolds22f02b32016-12-01 15:05:13 -0500998 }
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400999 }
Julia Reynolds0c299d42016-11-15 14:37:04 -05001000
Julia Reynolds924eed12017-01-19 09:52:07 -05001001 public void setShowBadge(boolean showBadge) {
1002 mShowBadge = showBadge;
1003 }
1004
1005 public boolean canShowBadge() {
1006 return mShowBadge;
1007 }
1008
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05001009 public Light getLight() {
1010 return mLight;
1011 }
1012
Julia Reynolds0c299d42016-11-15 14:37:04 -05001013 public Uri getSound() {
1014 return mSound;
1015 }
1016
1017 public long[] getVibration() {
1018 return mVibration;
1019 }
1020
1021 public AudioAttributes getAudioAttributes() {
1022 return mAttributes;
1023 }
Julia Reynolds22f02b32016-12-01 15:05:13 -05001024
1025 public ArrayList<String> getPeopleOverride() {
1026 return mPeopleOverride;
1027 }
1028
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001029 public void setInterruptive(boolean interruptive) {
1030 mIsInterruptive = interruptive;
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -04001031 final long now = System.currentTimeMillis();
1032 mInterruptionTimeMs = interruptive ? now : mInterruptionTimeMs;
1033
1034 if (interruptive) {
1035 MetricsLogger.action(getLogMaker()
1036 .setCategory(MetricsEvent.NOTIFICATION_INTERRUPTION)
1037 .setType(MetricsEvent.TYPE_OPEN)
1038 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_INTERRUPTION_MILLIS,
1039 getInterruptionMs(now)));
1040 MetricsLogger.histogram(mContext, "note_interruptive", getInterruptionMs(now));
1041 }
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001042 }
1043
Gus Prevasa3226492018-10-23 11:10:09 -04001044 public void setAudiblyAlerted(boolean audiblyAlerted) {
1045 mAudiblyAlerted = audiblyAlerted;
1046 }
1047
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -04001048 public void setTextChanged(boolean textChanged) {
1049 mTextChanged = textChanged;
1050 }
1051
1052 public void setRecordedInterruption(boolean recorded) {
1053 mRecordedInterruption = recorded;
1054 }
1055
1056 public boolean hasRecordedInterruption() {
1057 return mRecordedInterruption;
1058 }
1059
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001060 public boolean isInterruptive() {
1061 return mIsInterruptive;
1062 }
1063
Gus Prevasa3226492018-10-23 11:10:09 -04001064 /** Returns true if the notification audibly alerted the user. */
1065 public boolean getAudiblyAlerted() {
1066 return mAudiblyAlerted;
1067 }
1068
Julia Reynolds22f02b32016-12-01 15:05:13 -05001069 protected void setPeopleOverride(ArrayList<String> people) {
1070 mPeopleOverride = people;
1071 }
1072
1073 public ArrayList<SnoozeCriterion> getSnoozeCriteria() {
1074 return mSnoozeCriteria;
1075 }
1076
1077 protected void setSnoozeCriteria(ArrayList<SnoozeCriterion> snoozeCriteria) {
1078 mSnoozeCriteria = snoozeCriteria;
1079 }
Chris Wren9eb5e102017-01-26 13:15:06 -05001080
Julia Reynoldsc65656a2018-02-12 09:55:14 -05001081 private void calculateUserSentiment() {
Rohan Shah590e1b22018-04-10 23:48:47 -04001082 if ((getChannel().getUserLockedFields() & USER_LOCKED_IMPORTANCE) != 0
1083 || mIsAppImportanceLocked) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -05001084 mUserSentiment = USER_SENTIMENT_POSITIVE;
1085 }
1086 }
1087
Julia Reynolds503ed942017-10-04 16:04:56 -04001088 private void setUserSentiment(int userSentiment) {
1089 mUserSentiment = userSentiment;
1090 }
1091
1092 public int getUserSentiment() {
1093 return mUserSentiment;
1094 }
1095
1096 public NotificationStats getStats() {
1097 return mStats;
1098 }
1099
1100 public void recordExpanded() {
1101 mStats.setExpanded();
1102 }
1103
1104 public void recordDirectReplied() {
1105 mStats.setDirectReplied();
1106 }
1107
1108 public void recordDismissalSurface(@NotificationStats.DismissalSurface int surface) {
1109 mStats.setDismissalSurface(surface);
1110 }
1111
Julia Reynoldsfd4099d2018-08-21 11:06:06 -04001112 public void recordDismissalSentiment(@NotificationStats.DismissalSentiment int sentiment) {
1113 mStats.setDismissalSentiment(sentiment);
1114 }
1115
Julia Reynolds503ed942017-10-04 16:04:56 -04001116 public void recordSnoozed() {
1117 mStats.setSnoozed();
1118 }
1119
1120 public void recordViewedSettings() {
1121 mStats.setViewedSettings();
1122 }
1123
Kenny Guy23991102018-04-05 21:18:38 +01001124 public void setNumSmartRepliesAdded(int noReplies) {
1125 mNumberOfSmartRepliesAdded = noReplies;
1126 }
1127
1128 public int getNumSmartRepliesAdded() {
1129 return mNumberOfSmartRepliesAdded;
1130 }
1131
1132 public boolean hasSeenSmartReplies() {
1133 return mHasSeenSmartReplies;
1134 }
1135
1136 public void setSeenSmartReplies(boolean hasSeenSmartReplies) {
1137 mHasSeenSmartReplies = hasSeenSmartReplies;
1138 }
1139
Gustav Sennton1463d832018-11-06 16:12:48 +00001140 public void setSystemGeneratedSmartActions(
1141 ArrayList<Notification.Action> systemGeneratedSmartActions) {
1142 mSystemGeneratedSmartActions = systemGeneratedSmartActions;
Tony Mak628cb932018-06-19 18:30:41 +01001143 }
1144
Gustav Sennton1463d832018-11-06 16:12:48 +00001145 public ArrayList<Notification.Action> getSystemGeneratedSmartActions() {
1146 return mSystemGeneratedSmartActions;
Tony Mak628cb932018-06-19 18:30:41 +01001147 }
1148
Tony Makc9acf672018-07-20 13:58:24 +02001149 public void setSmartReplies(ArrayList<CharSequence> smartReplies) {
1150 mSmartReplies = smartReplies;
1151 }
1152
1153 public ArrayList<CharSequence> getSmartReplies() {
1154 return mSmartReplies;
1155 }
1156
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001157 /**
1158 * @return all {@link Uri} that should have permission granted to whoever
1159 * will be rendering it. This list has already been vetted to only
1160 * include {@link Uri} that the enqueuing app can grant.
1161 */
1162 public @Nullable ArraySet<Uri> getGrantableUris() {
1163 return mGrantableUris;
1164 }
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001165
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001166 /**
1167 * Collect all {@link Uri} that should have permission granted to whoever
1168 * will be rendering it.
1169 */
Julia Reynolds218871e2018-06-13 10:45:21 -04001170 protected void calculateGrantableUris() {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001171 final Notification notification = getNotification();
Jeff Sharkey23b31182018-04-18 21:32:12 -06001172 notification.visitUris((uri) -> {
Julia Reynolds218871e2018-06-13 10:45:21 -04001173 visitGrantableUri(uri, false);
Jeff Sharkey23b31182018-04-18 21:32:12 -06001174 });
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001175
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001176 if (notification.getChannelId() != null) {
1177 NotificationChannel channel = getChannel();
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001178 if (channel != null) {
Julia Reynolds218871e2018-06-13 10:45:21 -04001179 visitGrantableUri(channel.getSound(), (channel.getUserLockedFields()
1180 & NotificationChannel.USER_LOCKED_SOUND) != 0);
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001181 }
1182 }
1183 }
1184
1185 /**
1186 * Note the presence of a {@link Uri} that should have permission granted to
1187 * whoever will be rendering it.
1188 * <p>
1189 * If the enqueuing app has the ability to grant access, it will be added to
1190 * {@link #mGrantableUris}. Otherwise, this will either log or throw
1191 * {@link SecurityException} depending on target SDK of enqueuing app.
1192 */
Julia Reynolds218871e2018-06-13 10:45:21 -04001193 private void visitGrantableUri(Uri uri, boolean userOverriddenUri) {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001194 if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return;
1195
1196 // We can't grant Uri permissions from system
1197 final int sourceUid = sbn.getUid();
1198 if (sourceUid == android.os.Process.SYSTEM_UID) return;
1199
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001200 final long ident = Binder.clearCallingIdentity();
1201 try {
1202 // This will throw SecurityException if caller can't grant
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07001203 mUgmInternal.checkGrantUriPermission(sourceUid, null,
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001204 ContentProvider.getUriWithoutUserId(uri),
1205 Intent.FLAG_GRANT_READ_URI_PERMISSION,
1206 ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid)));
1207
1208 if (mGrantableUris == null) {
1209 mGrantableUris = new ArraySet<>();
1210 }
1211 mGrantableUris.add(uri);
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001212 } catch (SecurityException e) {
Julia Reynolds218871e2018-06-13 10:45:21 -04001213 if (!userOverriddenUri) {
1214 if (mTargetSdkVersion >= Build.VERSION_CODES.P) {
1215 throw e;
1216 } else {
1217 Log.w(TAG, "Ignoring " + uri + " from " + sourceUid + ": " + e.getMessage());
1218 }
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001219 }
1220 } finally {
1221 Binder.restoreCallingIdentity(ident);
1222 }
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001223 }
1224
Chris Wren9eb5e102017-01-26 13:15:06 -05001225 public LogMaker getLogMaker(long now) {
1226 if (mLogMaker == null) {
Chris Wrenb3921792017-06-01 13:34:46 -04001227 // initialize fields that only change on update (so a new record)
Chris Wren9eb5e102017-01-26 13:15:06 -05001228 mLogMaker = new LogMaker(MetricsEvent.VIEW_UNKNOWN)
1229 .setPackageName(sbn.getPackageName())
1230 .addTaggedData(MetricsEvent.NOTIFICATION_ID, sbn.getId())
Chris Wrenb3921792017-06-01 13:34:46 -04001231 .addTaggedData(MetricsEvent.NOTIFICATION_TAG, sbn.getTag())
1232 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID, getChannelIdLogTag());
Chris Wren9eb5e102017-01-26 13:15:06 -05001233 }
Chris Wrenb3921792017-06-01 13:34:46 -04001234 // reset fields that can change between updates, or are used by multiple logs
Chris Wren9eb5e102017-01-26 13:15:06 -05001235 return mLogMaker
Chris Wrena7c1b802017-03-07 10:17:20 -05001236 .clearCategory()
1237 .clearType()
1238 .clearSubtype()
Chris Wren9eb5e102017-01-26 13:15:06 -05001239 .clearTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX)
Chris Wrenb3921792017-06-01 13:34:46 -04001240 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE, mImportance)
1241 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID, getGroupLogTag())
1242 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_SUMMARY,
1243 sbn.getNotification().isGroupSummary() ? 1 : 0)
Chris Wren9eb5e102017-01-26 13:15:06 -05001244 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_CREATE_MILLIS, getLifespanMs(now))
1245 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_UPDATE_MILLIS, getFreshnessMs(now))
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -04001246 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_VISIBLE_MILLIS, getExposureMs(now))
1247 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_INTERRUPTION_MILLIS,
1248 getInterruptionMs(now));
Chris Wren9eb5e102017-01-26 13:15:06 -05001249 }
1250
1251 public LogMaker getLogMaker() {
1252 return getLogMaker(System.currentTimeMillis());
1253 }
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05001254
Julia Reynolds3dfdde02018-10-08 09:17:56 -04001255 public LogMaker getItemLogMaker() {
1256 return getLogMaker().setCategory(MetricsEvent.NOTIFICATION_ITEM);
1257 }
1258
1259 public LogMaker getAdjustmentLogMaker() {
1260 return getLogMaker()
1261 .setCategory(MetricsEvent.NOTIFICATION_ITEM)
1262 .setType(MetricsEvent.NOTIFICATION_ASSISTANT_ADJUSTMENT);
1263 }
1264
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05001265 @VisibleForTesting
1266 static final class Light {
1267 public final int color;
1268 public final int onMs;
1269 public final int offMs;
1270
1271 public Light(int color, int onMs, int offMs) {
1272 this.color = color;
1273 this.onMs = onMs;
1274 this.offMs = offMs;
1275 }
1276
1277 @Override
1278 public boolean equals(Object o) {
1279 if (this == o) return true;
1280 if (o == null || getClass() != o.getClass()) return false;
1281
1282 Light light = (Light) o;
1283
1284 if (color != light.color) return false;
1285 if (onMs != light.onMs) return false;
1286 return offMs == light.offMs;
1287
1288 }
1289
1290 @Override
1291 public int hashCode() {
1292 int result = color;
1293 result = 31 * result + onMs;
1294 result = 31 * result + offMs;
1295 return result;
1296 }
1297
1298 @Override
1299 public String toString() {
1300 return "Light{" +
1301 "color=" + color +
1302 ", onMs=" + onMs +
1303 ", offMs=" + offMs +
1304 '}';
1305 }
1306 }
Chris Wren333a61c2014-05-28 16:40:57 -04001307}