blob: 2aec3eaf757cae6e20924dbbddfce67819b492de [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;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060052import android.os.RemoteException;
Chris Wrenda4bd202014-09-04 15:53:52 -040053import android.os.UserHandle;
Julia Reynolds0c299d42016-11-15 14:37:04 -050054import android.provider.Settings;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040055import android.service.notification.Adjustment;
Julia Reynolds5d25ee72015-11-20 15:38:20 -050056import android.service.notification.NotificationListenerService;
Julia Reynoldsc9842c12017-02-07 12:46:41 -050057import android.service.notification.NotificationRecordProto;
Julia Reynolds503ed942017-10-04 16:04:56 -040058import android.service.notification.NotificationStats;
Julia Reynolds22f02b32016-12-01 15:05:13 -050059import android.service.notification.SnoozeCriterion;
Chris Wren333a61c2014-05-28 16:40:57 -040060import android.service.notification.StatusBarNotification;
Julia Reynolds4b82f6d2017-01-04 10:47:41 -050061import android.text.TextUtils;
Julia Reynoldse0d711f2017-09-01 08:50:47 -040062import android.util.ArraySet;
Julia Reynoldsf0f629f2016-02-25 09:34:04 -050063import android.util.Log;
Julia Reynolds2a128742016-11-28 14:29:25 -050064import android.util.TimeUtils;
Julia Reynoldsc9842c12017-02-07 12:46:41 -050065import android.util.proto.ProtoOutputStream;
Dan Sandler0a2308e2017-05-30 19:50:42 -040066import android.widget.RemoteViews;
John Spurlockbfa5dc42014-07-28 23:30:45 -040067
Chris Wren1031c972014-07-23 13:11:45 +000068import com.android.internal.annotations.VisibleForTesting;
Chris Wren9eb5e102017-01-26 13:15:06 -050069import com.android.internal.logging.MetricsLogger;
70import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Chris Wren6650e572015-05-15 17:19:25 -040071import com.android.server.EventLogTags;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060072import com.android.server.LocalServices;
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;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060098 final int mTargetSdkVersion;
Christoph Studer365e4c32014-09-18 20:35:36 +020099 final int mOriginalFlags;
Chris Wrenbdf33762015-12-04 15:50:51 -0500100 private final Context mContext;
Christoph Studer365e4c32014-09-18 20:35:36 +0200101
Chris Wren333a61c2014-05-28 16:40:57 -0400102 NotificationUsageStats.SingleNotificationStats stats;
103 boolean isCanceled;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600104 IBinder permissionOwner;
Chris Wren333a61c2014-05-28 16:40:57 -0400105
106 // These members are used by NotificationSignalExtractors
107 // to communicate with the ranking module.
108 private float mContactAffinity;
109 private boolean mRecentlyIntrusive;
Julia Reynolds309d1c82017-05-03 16:00:20 -0400110 private long mLastIntrusive;
Chris Wren333a61c2014-05-28 16:40:57 -0400111
112 // is this notification currently being intercepted by Zen Mode?
113 private boolean mIntercept;
Chris Wren333a61c2014-05-28 16:40:57 -0400114
Beverly5a20a5e2018-03-06 15:02:44 -0500115 // is this notification hidden since the app pkg is suspended?
116 private boolean mHidden;
117
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200118 // The timestamp used for ranking.
119 private long mRankingTimeMs;
120
Chris Wren640e3872015-04-21 13:23:18 -0400121 // The first post time, stable across updates.
122 private long mCreationTimeMs;
123
Chris Wren6650e572015-05-15 17:19:25 -0400124 // The most recent visibility event.
125 private long mVisibleSinceMs;
126
127 // The most recent update time, or the creation time if no updates.
128 private long mUpdateTimeMs;
129
Chris Wrena3446562014-06-03 18:11:47 -0400130 // Is this record an update of an old record?
131 public boolean isUpdate;
Chris Wren54bbef42014-07-09 18:37:56 -0400132 private int mPackagePriority;
Chris Wrena3446562014-06-03 18:11:47 -0400133
Chris Wren1031c972014-07-23 13:11:45 +0000134 private int mAuthoritativeRank;
Christoph Studercd4adf82014-08-19 17:50:49 +0200135 private String mGlobalSortKey;
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400136 private int mPackageVisibility;
Julia Reynoldsef37f282016-02-12 09:11:27 -0500137 private int mUserImportance = IMPORTANCE_UNSPECIFIED;
Chris Wren47633422016-01-22 09:56:59 -0500138 private int mImportance = IMPORTANCE_UNSPECIFIED;
Chris Wrenbdf33762015-12-04 15:50:51 -0500139 private CharSequence mImportanceExplanation = null;
Chris Wren1031c972014-07-23 13:11:45 +0000140
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500141 private int mSuppressedVisualEffects = 0;
Julia Reynoldsef37f282016-02-12 09:11:27 -0500142 private String mUserExplanation;
Chris Wrenbdf33762015-12-04 15:50:51 -0500143 private String mPeopleExplanation;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000144 private boolean mPreChannelsNotification = true;
Julia Reynolds0c299d42016-11-15 14:37:04 -0500145 private Uri mSound;
146 private long[] mVibration;
147 private AudioAttributes mAttributes;
Julia Reynolds924eed12017-01-19 09:52:07 -0500148 private NotificationChannel mChannel;
Julia Reynolds22f02b32016-12-01 15:05:13 -0500149 private ArrayList<String> mPeopleOverride;
150 private ArrayList<SnoozeCriterion> mSnoozeCriteria;
Julia Reynolds924eed12017-01-19 09:52:07 -0500151 private boolean mShowBadge;
Chris Wren9eb5e102017-01-26 13:15:06 -0500152 private LogMaker mLogMaker;
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500153 private Light mLight;
Chris Wrenb3921792017-06-01 13:34:46 -0400154 private String mGroupLogTag;
155 private String mChannelIdLogTag;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500156
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400157 private final List<Adjustment> mAdjustments;
Julia Reynolds503ed942017-10-04 16:04:56 -0400158 private final NotificationStats mStats;
159 private int mUserSentiment;
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500160 private boolean mIsInterruptive;
Kenny Guy23991102018-04-05 21:18:38 +0100161 private int mNumberOfSmartRepliesAdded;
162 private boolean mHasSeenSmartReplies;
Rohan Shah590e1b22018-04-10 23:48:47 -0400163 /**
164 * Whether this notification (and its channels) should be considered user locked. Used in
165 * conjunction with user sentiment calculation.
166 */
167 private boolean mIsAppImportanceLocked;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600168 private ArraySet<Uri> mGrantableUris;
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400169
Julia Reynolds924eed12017-01-19 09:52:07 -0500170 public NotificationRecord(Context context, StatusBarNotification sbn,
Rohan Shah590e1b22018-04-10 23:48:47 -0400171 NotificationChannel channel) {
Chris Wren333a61c2014-05-28 16:40:57 -0400172 this.sbn = sbn;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600173 mTargetSdkVersion = LocalServices.getService(PackageManagerInternal.class)
174 .getPackageTargetSdkVersion(sbn.getPackageName());
Christoph Studer365e4c32014-09-18 20:35:36 +0200175 mOriginalFlags = sbn.getNotification().flags;
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200176 mRankingTimeMs = calculateRankingTimeMs(0L);
Chris Wren640e3872015-04-21 13:23:18 -0400177 mCreationTimeMs = sbn.getPostTime();
Chris Wren6650e572015-05-15 17:19:25 -0400178 mUpdateTimeMs = mCreationTimeMs;
Chris Wrenbdf33762015-12-04 15:50:51 -0500179 mContext = context;
Chris Wrencdee8cd2016-01-25 17:10:30 -0500180 stats = new NotificationUsageStats.SingleNotificationStats();
Julia Reynolds924eed12017-01-19 09:52:07 -0500181 mChannel = channel;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000182 mPreChannelsNotification = isPreChannelsNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500183 mSound = calculateSound();
184 mVibration = calculateVibration();
185 mAttributes = calculateAttributes();
186 mImportance = calculateImportance();
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500187 mLight = calculateLights();
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400188 mAdjustments = new ArrayList<>();
Julia Reynolds503ed942017-10-04 16:04:56 -0400189 mStats = new NotificationStats();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500190 calculateUserSentiment();
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600191 calculateGrantableUris();
Chris Wrenbdf33762015-12-04 15:50:51 -0500192 }
193
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000194 private boolean isPreChannelsNotification() {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600195 if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(getChannel().getId())) {
196 if (mTargetSdkVersion < Build.VERSION_CODES.O) {
197 return true;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000198 }
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000199 }
200 return false;
201 }
202
Julia Reynolds0c299d42016-11-15 14:37:04 -0500203 private Uri calculateSound() {
Chris Wrenbdf33762015-12-04 15:50:51 -0500204 final Notification n = sbn.getNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500205
Tony Mantlerab55f0f2017-06-16 10:50:00 -0700206 // No notification sounds on tv
207 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
208 return null;
209 }
210
Julia Reynolds924eed12017-01-19 09:52:07 -0500211 Uri sound = mChannel.getSound();
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000212 if (mPreChannelsNotification && (getChannel().getUserLockedFields()
Julia Reynolds0c299d42016-11-15 14:37:04 -0500213 & NotificationChannel.USER_LOCKED_SOUND) == 0) {
214
215 final boolean useDefaultSound = (n.defaults & Notification.DEFAULT_SOUND) != 0;
216 if (useDefaultSound) {
217 sound = Settings.System.DEFAULT_NOTIFICATION_URI;
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400218 } else {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500219 sound = n.sound;
220 }
221 }
222 return sound;
223 }
224
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500225 private Light calculateLights() {
226 int defaultLightColor = mContext.getResources().getColor(
227 com.android.internal.R.color.config_defaultNotificationColor);
228 int defaultLightOn = mContext.getResources().getInteger(
229 com.android.internal.R.integer.config_defaultNotificationLedOn);
230 int defaultLightOff = mContext.getResources().getInteger(
231 com.android.internal.R.integer.config_defaultNotificationLedOff);
232
Julia Reynolds529e3322017-02-06 08:33:01 -0500233 int channelLightColor = getChannel().getLightColor() != 0 ? getChannel().getLightColor()
234 : defaultLightColor;
235 Light light = getChannel().shouldShowLights() ? new Light(channelLightColor,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500236 defaultLightOn, defaultLightOff) : null;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000237 if (mPreChannelsNotification
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500238 && (getChannel().getUserLockedFields()
239 & NotificationChannel.USER_LOCKED_LIGHTS) == 0) {
240 final Notification notification = sbn.getNotification();
241 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
242 light = new Light(notification.ledARGB, notification.ledOnMS,
243 notification.ledOffMS);
244 if ((notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
245 light = new Light(defaultLightColor, defaultLightOn,
246 defaultLightOff);
247 }
248 } else {
249 light = null;
250 }
251 }
252 return light;
253 }
254
Julia Reynolds0c299d42016-11-15 14:37:04 -0500255 private long[] calculateVibration() {
256 long[] vibration;
257 final long[] defaultVibration = NotificationManagerService.getLongArray(
258 mContext.getResources(),
259 com.android.internal.R.array.config_defaultNotificationVibePattern,
260 NotificationManagerService.VIBRATE_PATTERN_MAXLEN,
261 NotificationManagerService.DEFAULT_VIBRATE_PATTERN);
262 if (getChannel().shouldVibrate()) {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500263 vibration = getChannel().getVibrationPattern() == null
264 ? defaultVibration : getChannel().getVibrationPattern();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500265 } else {
266 vibration = null;
267 }
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000268 if (mPreChannelsNotification
Julia Reynolds0c299d42016-11-15 14:37:04 -0500269 && (getChannel().getUserLockedFields()
270 & NotificationChannel.USER_LOCKED_VIBRATION) == 0) {
271 final Notification notification = sbn.getNotification();
272 final boolean useDefaultVibrate =
273 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
274 if (useDefaultVibrate) {
275 vibration = defaultVibration;
276 } else {
277 vibration = notification.vibrate;
278 }
279 }
280 return vibration;
281 }
282
283 private AudioAttributes calculateAttributes() {
284 final Notification n = sbn.getNotification();
Julia Reynolds619a69f2017-01-27 15:11:38 -0500285 AudioAttributes attributes = getChannel().getAudioAttributes();
286 if (attributes == null) {
287 attributes = Notification.AUDIO_ATTRIBUTES_DEFAULT;
288 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500289
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000290 if (mPreChannelsNotification
Julia Reynolds619a69f2017-01-27 15:11:38 -0500291 && (getChannel().getUserLockedFields()
292 & NotificationChannel.USER_LOCKED_SOUND) == 0) {
293 if (n.audioAttributes != null) {
294 // prefer audio attributes to stream type
295 attributes = n.audioAttributes;
296 } else if (n.audioStreamType >= 0
297 && n.audioStreamType < AudioSystem.getNumStreamTypes()) {
298 // the stream type is valid, use it
299 attributes = new AudioAttributes.Builder()
300 .setInternalLegacyStreamType(n.audioStreamType)
301 .build();
302 } else if (n.audioStreamType != AudioSystem.STREAM_DEFAULT) {
303 Log.w(TAG, String.format("Invalid stream type: %d", n.audioStreamType));
304 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500305 }
306 return attributes;
307 }
308
309 private int calculateImportance() {
310 final Notification n = sbn.getNotification();
311 int importance = getChannel().getImportance();
312 int requestedImportance = IMPORTANCE_DEFAULT;
Chris Wrenbdf33762015-12-04 15:50:51 -0500313
314 // Migrate notification flags to scores
315 if (0 != (n.flags & Notification.FLAG_HIGH_PRIORITY)) {
316 n.priority = Notification.PRIORITY_MAX;
317 }
318
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500319 n.priority = NotificationManagerService.clamp(n.priority, Notification.PRIORITY_MIN,
320 Notification.PRIORITY_MAX);
Chris Wrenbdf33762015-12-04 15:50:51 -0500321 switch (n.priority) {
322 case Notification.PRIORITY_MIN:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500323 requestedImportance = IMPORTANCE_MIN;
Julia Reynoldsf0f629f2016-02-25 09:34:04 -0500324 break;
Chris Wrenbdf33762015-12-04 15:50:51 -0500325 case Notification.PRIORITY_LOW:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500326 requestedImportance = IMPORTANCE_LOW;
Chris Wrenbdf33762015-12-04 15:50:51 -0500327 break;
328 case Notification.PRIORITY_DEFAULT:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500329 requestedImportance = IMPORTANCE_DEFAULT;
Chris Wrenbdf33762015-12-04 15:50:51 -0500330 break;
331 case Notification.PRIORITY_HIGH:
Chris Wrenbdf33762015-12-04 15:50:51 -0500332 case Notification.PRIORITY_MAX:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500333 requestedImportance = IMPORTANCE_HIGH;
Chris Wrenbdf33762015-12-04 15:50:51 -0500334 break;
335 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500336 stats.requestedImportance = requestedImportance;
337 stats.isNoisy = mSound != null || mVibration != null;
Chris Wrenbdf33762015-12-04 15:50:51 -0500338
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000339 if (mPreChannelsNotification
Julia Reynoldsa917a112017-03-21 11:09:14 -0400340 && (importance == IMPORTANCE_UNSPECIFIED
341 || (getChannel().getUserLockedFields()
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500342 & USER_LOCKED_IMPORTANCE) == 0)) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500343 if (!stats.isNoisy && requestedImportance > IMPORTANCE_LOW) {
344 requestedImportance = IMPORTANCE_LOW;
Julia Reynolds83fa1072016-02-17 09:10:19 -0500345 }
Julia Reynolds83fa1072016-02-17 09:10:19 -0500346
Julia Reynolds0c299d42016-11-15 14:37:04 -0500347 if (stats.isNoisy) {
348 if (requestedImportance < IMPORTANCE_DEFAULT) {
349 requestedImportance = IMPORTANCE_DEFAULT;
350 }
351 }
352
353 if (n.fullScreenIntent != null) {
354 requestedImportance = IMPORTANCE_HIGH;
355 }
356 importance = requestedImportance;
Chris Wrenbdf33762015-12-04 15:50:51 -0500357 }
358
Chris Wrencdee8cd2016-01-25 17:10:30 -0500359 stats.naturalImportance = importance;
Chris Wrenbdf33762015-12-04 15:50:51 -0500360 return importance;
Chris Wren333a61c2014-05-28 16:40:57 -0400361 }
362
363 // copy any notes that the ranking system may have made before the update
364 public void copyRankingInformation(NotificationRecord previous) {
365 mContactAffinity = previous.mContactAffinity;
366 mRecentlyIntrusive = previous.mRecentlyIntrusive;
Chris Wren54bbef42014-07-09 18:37:56 -0400367 mPackagePriority = previous.mPackagePriority;
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400368 mPackageVisibility = previous.mPackageVisibility;
Chris Wren333a61c2014-05-28 16:40:57 -0400369 mIntercept = previous.mIntercept;
Beverly5a20a5e2018-03-06 15:02:44 -0500370 mHidden = previous.mHidden;
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200371 mRankingTimeMs = calculateRankingTimeMs(previous.getRankingTimeMs());
Chris Wren640e3872015-04-21 13:23:18 -0400372 mCreationTimeMs = previous.mCreationTimeMs;
Chris Wren6650e572015-05-15 17:19:25 -0400373 mVisibleSinceMs = previous.mVisibleSinceMs;
Selim Cinek5b03ce92016-05-18 15:16:58 -0700374 if (previous.sbn.getOverrideGroupKey() != null && !sbn.isAppGroup()) {
Chris Wren8a1638f2016-05-02 16:19:14 -0400375 sbn.setOverrideGroupKey(previous.sbn.getOverrideGroupKey());
376 }
Chris Wren1f602dc2016-04-11 10:33:46 -0400377 // Don't copy importance information or mGlobalSortKey, recompute them.
Chris Wren333a61c2014-05-28 16:40:57 -0400378 }
379
380 public Notification getNotification() { return sbn.getNotification(); }
381 public int getFlags() { return sbn.getNotification().flags; }
Chris Wrenda4bd202014-09-04 15:53:52 -0400382 public UserHandle getUser() { return sbn.getUser(); }
Chris Wren333a61c2014-05-28 16:40:57 -0400383 public String getKey() { return sbn.getKey(); }
Chris Wrenda4bd202014-09-04 15:53:52 -0400384 /** @deprecated Use {@link #getUser()} instead. */
385 public int getUserId() { return sbn.getUserId(); }
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600386 public int getUid() { return sbn.getUid(); }
Chris Wren333a61c2014-05-28 16:40:57 -0400387
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800388 void dump(ProtoOutputStream proto, long fieldId, boolean redact, int state) {
389 final long token = proto.start(fieldId);
390
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500391 proto.write(NotificationRecordProto.KEY, sbn.getKey());
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800392 proto.write(NotificationRecordProto.STATE, state);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500393 if (getChannel() != null) {
394 proto.write(NotificationRecordProto.CHANNEL_ID, getChannel().getId());
395 }
396 proto.write(NotificationRecordProto.CAN_SHOW_LIGHT, getLight() != null);
397 proto.write(NotificationRecordProto.CAN_VIBRATE, getVibration() != null);
398 proto.write(NotificationRecordProto.FLAGS, sbn.getNotification().flags);
399 proto.write(NotificationRecordProto.GROUP_KEY, getGroupKey());
400 proto.write(NotificationRecordProto.IMPORTANCE, getImportance());
401 if (getSound() != null) {
402 proto.write(NotificationRecordProto.SOUND, getSound().toString());
403 }
404 if (getAudioAttributes() != null) {
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800405 getAudioAttributes().writeToProto(proto, NotificationRecordProto.AUDIO_ATTRIBUTES);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500406 }
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800407
408 proto.end(token);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500409 }
410
Dan Sandler0a2308e2017-05-30 19:50:42 -0400411 String formatRemoteViews(RemoteViews rv) {
412 if (rv == null) return "null";
413 return String.format("%s/0x%08x (%d bytes): %s",
414 rv.getPackage(), rv.getLayoutId(), rv.estimateMemoryUsage(), rv.toString());
415 }
416
Dan Sandlera1770312015-07-10 13:59:29 -0400417 void dump(PrintWriter pw, String prefix, Context baseContext, boolean redact) {
Chris Wren333a61c2014-05-28 16:40:57 -0400418 final Notification notification = sbn.getNotification();
Dan Sandlerd63f9322015-05-06 15:18:49 -0400419 final Icon icon = notification.getSmallIcon();
420 String iconStr = String.valueOf(icon);
421 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
422 iconStr += " / " + idDebugString(baseContext, icon.getResPackage(), icon.getResId());
423 }
Chris Wren333a61c2014-05-28 16:40:57 -0400424 pw.println(prefix + this);
Dan Sandler0a2308e2017-05-30 19:50:42 -0400425 prefix = prefix + " ";
Julia Reynoldsa917a112017-03-21 11:09:14 -0400426 pw.println(prefix + "uid=" + sbn.getUid() + " userId=" + sbn.getUserId());
427 pw.println(prefix + "icon=" + iconStr);
Julia Reynolds4db59552017-06-30 13:34:01 -0400428 pw.println(prefix + "flags=0x" + Integer.toHexString(notification.flags));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400429 pw.println(prefix + "pri=" + notification.priority);
430 pw.println(prefix + "key=" + sbn.getKey());
Julia Reynolds503ed942017-10-04 16:04:56 -0400431 pw.println(prefix + "seen=" + mStats.hasSeen());
Julia Reynoldsa917a112017-03-21 11:09:14 -0400432 pw.println(prefix + "groupKey=" + getGroupKey());
433 pw.println(prefix + "fullscreenIntent=" + notification.fullScreenIntent);
434 pw.println(prefix + "contentIntent=" + notification.contentIntent);
435 pw.println(prefix + "deleteIntent=" + notification.deleteIntent);
Dan Sandler0b4ceb32017-03-29 14:13:55 -0400436
437 pw.print(prefix + "tickerText=");
438 if (!TextUtils.isEmpty(notification.tickerText)) {
439 final String ticker = notification.tickerText.toString();
440 if (redact) {
441 // if the string is long enough, we allow ourselves a few bytes for debugging
442 pw.print(ticker.length() > 16 ? ticker.substring(0,8) : "");
443 pw.println("...");
444 } else {
445 pw.println(ticker);
446 }
447 } else {
448 pw.println("null");
449 }
Dan Sandler0a2308e2017-05-30 19:50:42 -0400450 pw.println(prefix + "contentView=" + formatRemoteViews(notification.contentView));
451 pw.println(prefix + "bigContentView=" + formatRemoteViews(notification.bigContentView));
452 pw.println(prefix + "headsUpContentView="
453 + formatRemoteViews(notification.headsUpContentView));
454 pw.print(prefix + String.format("color=0x%08x", notification.color));
Julia Reynoldsbad42972017-04-25 13:52:49 -0400455 pw.println(prefix + "timeout="
456 + TimeUtils.formatForLogging(notification.getTimeoutAfter()));
Chris Wren333a61c2014-05-28 16:40:57 -0400457 if (notification.actions != null && notification.actions.length > 0) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400458 pw.println(prefix + "actions={");
Chris Wren333a61c2014-05-28 16:40:57 -0400459 final int N = notification.actions.length;
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500460 for (int i = 0; i < N; i++) {
Chris Wren333a61c2014-05-28 16:40:57 -0400461 final Notification.Action action = notification.actions[i];
Chris Wren1ac52a92016-02-24 14:54:52 -0500462 if (action != null) {
463 pw.println(String.format("%s [%d] \"%s\" -> %s",
464 prefix,
465 i,
466 action.title,
467 action.actionIntent == null ? "null" : action.actionIntent.toString()
468 ));
469 }
Chris Wren333a61c2014-05-28 16:40:57 -0400470 }
471 pw.println(prefix + " }");
472 }
473 if (notification.extras != null && notification.extras.size() > 0) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400474 pw.println(prefix + "extras={");
Chris Wren333a61c2014-05-28 16:40:57 -0400475 for (String key : notification.extras.keySet()) {
476 pw.print(prefix + " " + key + "=");
477 Object val = notification.extras.get(key);
478 if (val == null) {
479 pw.println("null");
480 } else {
481 pw.print(val.getClass().getSimpleName());
Dan Sandlera1770312015-07-10 13:59:29 -0400482 if (redact && (val instanceof CharSequence || val instanceof String)) {
Chris Wren333a61c2014-05-28 16:40:57 -0400483 // redact contents from bugreports
484 } else if (val instanceof Bitmap) {
485 pw.print(String.format(" (%dx%d)",
486 ((Bitmap) val).getWidth(),
487 ((Bitmap) val).getHeight()));
488 } else if (val.getClass().isArray()) {
489 final int N = Array.getLength(val);
Dan Sandlera1770312015-07-10 13:59:29 -0400490 pw.print(" (" + N + ")");
491 if (!redact) {
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500492 for (int j = 0; j < N; j++) {
Dan Sandlera1770312015-07-10 13:59:29 -0400493 pw.println();
494 pw.print(String.format("%s [%d] %s",
495 prefix, j, String.valueOf(Array.get(val, j))));
496 }
497 }
Chris Wren333a61c2014-05-28 16:40:57 -0400498 } else {
499 pw.print(" (" + String.valueOf(val) + ")");
500 }
501 pw.println();
502 }
503 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400504 pw.println(prefix + "}");
Chris Wren333a61c2014-05-28 16:40:57 -0400505 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400506 pw.println(prefix + "stats=" + stats.toString());
507 pw.println(prefix + "mContactAffinity=" + mContactAffinity);
508 pw.println(prefix + "mRecentlyIntrusive=" + mRecentlyIntrusive);
509 pw.println(prefix + "mPackagePriority=" + mPackagePriority);
510 pw.println(prefix + "mPackageVisibility=" + mPackageVisibility);
511 pw.println(prefix + "mUserImportance="
Julia Reynoldsef37f282016-02-12 09:11:27 -0500512 + NotificationListenerService.Ranking.importanceToString(mUserImportance));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400513 pw.println(prefix + "mImportance="
Chris Wrenbdf33762015-12-04 15:50:51 -0500514 + NotificationListenerService.Ranking.importanceToString(mImportance));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400515 pw.println(prefix + "mImportanceExplanation=" + mImportanceExplanation);
Rohan Shah590e1b22018-04-10 23:48:47 -0400516 pw.println(prefix + "mIsAppImportanceLocked=" + mIsAppImportanceLocked);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400517 pw.println(prefix + "mIntercept=" + mIntercept);
Beverly5a20a5e2018-03-06 15:02:44 -0500518 pw.println(prefix + "mHidden==" + mHidden);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400519 pw.println(prefix + "mGlobalSortKey=" + mGlobalSortKey);
520 pw.println(prefix + "mRankingTimeMs=" + mRankingTimeMs);
521 pw.println(prefix + "mCreationTimeMs=" + mCreationTimeMs);
522 pw.println(prefix + "mVisibleSinceMs=" + mVisibleSinceMs);
523 pw.println(prefix + "mUpdateTimeMs=" + mUpdateTimeMs);
524 pw.println(prefix + "mSuppressedVisualEffects= " + mSuppressedVisualEffects);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000525 if (mPreChannelsNotification) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400526 pw.println(prefix + String.format("defaults=0x%08x flags=0x%08x",
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500527 notification.defaults, notification.flags));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400528 pw.println(prefix + "n.sound=" + notification.sound);
529 pw.println(prefix + "n.audioStreamType=" + notification.audioStreamType);
530 pw.println(prefix + "n.audioAttributes=" + notification.audioAttributes);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500531 pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d",
532 notification.ledARGB, notification.ledOnMS, notification.ledOffMS));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400533 pw.println(prefix + "vibrate=" + Arrays.toString(notification.vibrate));
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500534 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400535 pw.println(prefix + "mSound= " + mSound);
536 pw.println(prefix + "mVibration= " + mVibration);
537 pw.println(prefix + "mAttributes= " + mAttributes);
538 pw.println(prefix + "mLight= " + mLight);
539 pw.println(prefix + "mShowBadge=" + mShowBadge);
Julia Reynolds4db59552017-06-30 13:34:01 -0400540 pw.println(prefix + "mColorized=" + notification.isColorized());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500541 pw.println(prefix + "mIsInterruptive=" + mIsInterruptive);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400542 pw.println(prefix + "effectiveNotificationChannel=" + getChannel());
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500543 if (getPeopleOverride() != null) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400544 pw.println(prefix + "overridePeople= " + TextUtils.join(",", getPeopleOverride()));
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500545 }
546 if (getSnoozeCriteria() != null) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400547 pw.println(prefix + "snoozeCriteria=" + TextUtils.join(",", getSnoozeCriteria()));
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500548 }
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400549 pw.println(prefix + "mAdjustments=" + mAdjustments);
Chris Wren333a61c2014-05-28 16:40:57 -0400550 }
551
552
553 static String idDebugString(Context baseContext, String packageName, int id) {
554 Context c;
555
556 if (packageName != null) {
557 try {
558 c = baseContext.createPackageContext(packageName, 0);
559 } catch (NameNotFoundException e) {
560 c = baseContext;
561 }
562 } else {
563 c = baseContext;
564 }
565
566 Resources r = c.getResources();
567 try {
568 return r.getResourceName(id);
569 } catch (Resources.NotFoundException e) {
570 return "<name unknown>";
571 }
572 }
573
574 @Override
575 public final String toString() {
576 return String.format(
Julia Reynolds85769912016-10-25 09:08:57 -0400577 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s importance=%d key=%s" +
Rohan Shah590e1b22018-04-10 23:48:47 -0400578 "appImportanceLocked=%s: %s)",
Chris Wren333a61c2014-05-28 16:40:57 -0400579 System.identityHashCode(this),
580 this.sbn.getPackageName(), this.sbn.getUser(), this.sbn.getId(),
Julia Reynolds4db59552017-06-30 13:34:01 -0400581 this.sbn.getTag(), this.mImportance, this.sbn.getKey(),
Rohan Shah590e1b22018-04-10 23:48:47 -0400582 mIsAppImportanceLocked, this.sbn.getNotification());
Chris Wren333a61c2014-05-28 16:40:57 -0400583 }
584
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400585 public void addAdjustment(Adjustment adjustment) {
586 synchronized (mAdjustments) {
587 mAdjustments.add(adjustment);
588 }
589 }
590
591 public void applyAdjustments() {
592 synchronized (mAdjustments) {
593 for (Adjustment adjustment: mAdjustments) {
594 Bundle signals = adjustment.getSignals();
595 if (signals.containsKey(Adjustment.KEY_PEOPLE)) {
596 final ArrayList<String> people =
597 adjustment.getSignals().getStringArrayList(Adjustment.KEY_PEOPLE);
598 setPeopleOverride(people);
599 }
600 if (signals.containsKey(Adjustment.KEY_SNOOZE_CRITERIA)) {
601 final ArrayList<SnoozeCriterion> snoozeCriterionList =
602 adjustment.getSignals().getParcelableArrayList(
603 Adjustment.KEY_SNOOZE_CRITERIA);
604 setSnoozeCriteria(snoozeCriterionList);
605 }
606 if (signals.containsKey(Adjustment.KEY_GROUP_KEY)) {
607 final String groupOverrideKey =
608 adjustment.getSignals().getString(Adjustment.KEY_GROUP_KEY);
609 setOverrideGroupKey(groupOverrideKey);
610 }
Julia Reynolds503ed942017-10-04 16:04:56 -0400611 if (signals.containsKey(Adjustment.KEY_USER_SENTIMENT)) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500612 // Only allow user sentiment update from assistant if user hasn't already
613 // expressed a preference for this channel
Rohan Shah590e1b22018-04-10 23:48:47 -0400614 if (!mIsAppImportanceLocked
615 && (getChannel().getUserLockedFields() & USER_LOCKED_IMPORTANCE) == 0) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500616 setUserSentiment(adjustment.getSignals().getInt(
617 Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEUTRAL));
618 }
Julia Reynolds503ed942017-10-04 16:04:56 -0400619 }
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400620 }
621 }
622 }
623
Rohan Shah590e1b22018-04-10 23:48:47 -0400624 public void setIsAppImportanceLocked(boolean isAppImportanceLocked) {
625 mIsAppImportanceLocked = isAppImportanceLocked;
626 calculateUserSentiment();
627 }
628
Chris Wren333a61c2014-05-28 16:40:57 -0400629 public void setContactAffinity(float contactAffinity) {
630 mContactAffinity = contactAffinity;
Chris Wrenbdf33762015-12-04 15:50:51 -0500631 if (mImportance < IMPORTANCE_DEFAULT &&
Chris Wrenc977f812016-06-13 21:24:53 +0000632 mContactAffinity > ValidateNotificationPeople.VALID_CONTACT) {
Chris Wrenbdf33762015-12-04 15:50:51 -0500633 setImportance(IMPORTANCE_DEFAULT, getPeopleExplanation());
634 }
Chris Wren333a61c2014-05-28 16:40:57 -0400635 }
636
637 public float getContactAffinity() {
638 return mContactAffinity;
639 }
640
John Spurlock1d881a12015-03-18 19:21:54 -0400641 public void setRecentlyIntrusive(boolean recentlyIntrusive) {
Chris Wren333a61c2014-05-28 16:40:57 -0400642 mRecentlyIntrusive = recentlyIntrusive;
Julia Reynolds309d1c82017-05-03 16:00:20 -0400643 if (recentlyIntrusive) {
644 mLastIntrusive = System.currentTimeMillis();
645 }
Chris Wren333a61c2014-05-28 16:40:57 -0400646 }
647
648 public boolean isRecentlyIntrusive() {
649 return mRecentlyIntrusive;
650 }
651
Julia Reynolds309d1c82017-05-03 16:00:20 -0400652 public long getLastIntrusive() {
653 return mLastIntrusive;
654 }
655
Chris Wren54bbef42014-07-09 18:37:56 -0400656 public void setPackagePriority(int packagePriority) {
John Spurlock6ac5f8d2014-07-18 11:27:54 -0400657 mPackagePriority = packagePriority;
Chris Wren54bbef42014-07-09 18:37:56 -0400658 }
659
660 public int getPackagePriority() {
661 return mPackagePriority;
662 }
663
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400664 public void setPackageVisibilityOverride(int packageVisibility) {
665 mPackageVisibility = packageVisibility;
666 }
667
668 public int getPackageVisibilityOverride() {
669 return mPackageVisibility;
670 }
671
Julia Reynoldsef37f282016-02-12 09:11:27 -0500672 public void setUserImportance(int importance) {
673 mUserImportance = importance;
674 applyUserImportance();
Chris Wrenbdf33762015-12-04 15:50:51 -0500675 }
676
Julia Reynoldsef37f282016-02-12 09:11:27 -0500677 private String getUserExplanation() {
678 if (mUserExplanation == null) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500679 mUserExplanation = mContext.getResources().getString(
680 com.android.internal.R.string.importance_from_user);
Chris Wrenbdf33762015-12-04 15:50:51 -0500681 }
Julia Reynoldsef37f282016-02-12 09:11:27 -0500682 return mUserExplanation;
Chris Wrenbdf33762015-12-04 15:50:51 -0500683 }
684
685 private String getPeopleExplanation() {
686 if (mPeopleExplanation == null) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500687 mPeopleExplanation = mContext.getResources().getString(
688 com.android.internal.R.string.importance_from_person);
Chris Wrenbdf33762015-12-04 15:50:51 -0500689 }
690 return mPeopleExplanation;
691 }
692
Julia Reynoldsef37f282016-02-12 09:11:27 -0500693 private void applyUserImportance() {
Julia Reynolds85769912016-10-25 09:08:57 -0400694 if (mUserImportance != IMPORTANCE_UNSPECIFIED) {
Julia Reynoldsef37f282016-02-12 09:11:27 -0500695 mImportance = mUserImportance;
696 mImportanceExplanation = getUserExplanation();
Julia Reynolds5d25ee72015-11-20 15:38:20 -0500697 }
698 }
699
Julia Reynoldsef37f282016-02-12 09:11:27 -0500700 public int getUserImportance() {
701 return mUserImportance;
Julia Reynolds5d25ee72015-11-20 15:38:20 -0500702 }
703
Chris Wrenbdf33762015-12-04 15:50:51 -0500704 public void setImportance(int importance, CharSequence explanation) {
Julia Reynolds85769912016-10-25 09:08:57 -0400705 if (importance != IMPORTANCE_UNSPECIFIED) {
Chris Wrenbdf33762015-12-04 15:50:51 -0500706 mImportance = importance;
707 mImportanceExplanation = explanation;
708 }
Julia Reynoldsef37f282016-02-12 09:11:27 -0500709 applyUserImportance();
Chris Wrenbdf33762015-12-04 15:50:51 -0500710 }
711
712 public int getImportance() {
713 return mImportance;
714 }
715
716 public CharSequence getImportanceExplanation() {
717 return mImportanceExplanation;
718 }
719
Chris Wren333a61c2014-05-28 16:40:57 -0400720 public boolean setIntercepted(boolean intercept) {
721 mIntercept = intercept;
722 return mIntercept;
723 }
724
725 public boolean isIntercepted() {
726 return mIntercept;
727 }
728
Beverly5a20a5e2018-03-06 15:02:44 -0500729 public void setHidden(boolean hidden) {
730 mHidden = hidden;
731 }
732
733 public boolean isHidden() {
734 return mHidden;
735 }
736
737
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500738 public void setSuppressedVisualEffects(int effects) {
739 mSuppressedVisualEffects = effects;
740 }
741
742 public int getSuppressedVisualEffects() {
743 return mSuppressedVisualEffects;
744 }
745
John Spurlock312d1d02014-07-08 10:24:57 -0400746 public boolean isCategory(String category) {
John Spurlockbfa5dc42014-07-28 23:30:45 -0400747 return Objects.equals(getNotification().category, category);
748 }
749
John Spurlockbfa5dc42014-07-28 23:30:45 -0400750 public boolean isAudioAttributesUsage(int usage) {
Julia Reynolds51eb78f82018-03-07 07:35:21 -0500751 return mAttributes != null && mAttributes.getUsage() == usage;
John Spurlock312d1d02014-07-08 10:24:57 -0400752 }
753
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200754 /**
755 * Returns the timestamp to use for time-based sorting in the ranker.
756 */
757 public long getRankingTimeMs() {
758 return mRankingTimeMs;
759 }
760
761 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400762 * @param now this current time in milliseconds.
763 * @returns the number of milliseconds since the most recent update, or the post time if none.
Chris Wren6650e572015-05-15 17:19:25 -0400764 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400765 public int getFreshnessMs(long now) {
766 return (int) (now - mUpdateTimeMs);
Chris Wren6650e572015-05-15 17:19:25 -0400767 }
768
769 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400770 * @param now this current time in milliseconds.
771 * @returns the number of milliseconds since the the first post, ignoring updates.
Chris Wren640e3872015-04-21 13:23:18 -0400772 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400773 public int getLifespanMs(long now) {
774 return (int) (now - mCreationTimeMs);
Chris Wren640e3872015-04-21 13:23:18 -0400775 }
776
777 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400778 * @param now this current time in milliseconds.
779 * @returns the number of milliseconds since the most recent visibility event, or 0 if never.
Chris Wren6650e572015-05-15 17:19:25 -0400780 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400781 public int getExposureMs(long now) {
782 return mVisibleSinceMs == 0 ? 0 : (int) (now - mVisibleSinceMs);
Chris Wren6650e572015-05-15 17:19:25 -0400783 }
784
785 /**
786 * Set the visibility of the notification.
787 */
Dieter Hsud39f0d52018-04-14 02:08:30 +0800788 public void setVisibility(boolean visible, int rank, int count) {
Chris Wren6650e572015-05-15 17:19:25 -0400789 final long now = System.currentTimeMillis();
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400790 mVisibleSinceMs = visible ? now : mVisibleSinceMs;
Chris Wren6650e572015-05-15 17:19:25 -0400791 stats.onVisibilityChanged(visible);
Chris Wren9eb5e102017-01-26 13:15:06 -0500792 MetricsLogger.action(getLogMaker(now)
793 .setCategory(MetricsEvent.NOTIFICATION_ITEM)
794 .setType(visible ? MetricsEvent.TYPE_OPEN : MetricsEvent.TYPE_CLOSE)
Dieter Hsud39f0d52018-04-14 02:08:30 +0800795 .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, rank)
796 .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, count));
Chris Wren9eb5e102017-01-26 13:15:06 -0500797 if (visible) {
Julia Reynolds503ed942017-10-04 16:04:56 -0400798 setSeen();
Chris Wren9eb5e102017-01-26 13:15:06 -0500799 MetricsLogger.histogram(mContext, "note_freshness", getFreshnessMs(now));
800 }
Chris Wren6650e572015-05-15 17:19:25 -0400801 EventLogTags.writeNotificationVisibility(getKey(), visible ? 1 : 0,
Chris Wren9eb5e102017-01-26 13:15:06 -0500802 getLifespanMs(now),
803 getFreshnessMs(now),
Chris Wrend1dbc922015-06-19 17:51:16 -0400804 0, // exposure time
805 rank);
Chris Wren6650e572015-05-15 17:19:25 -0400806 }
807
808 /**
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200809 * @param previousRankingTimeMs for updated notifications, {@link #getRankingTimeMs()}
810 * of the previous notification record, 0 otherwise
811 */
812 private long calculateRankingTimeMs(long previousRankingTimeMs) {
813 Notification n = getNotification();
814 // Take developer provided 'when', unless it's in the future.
815 if (n.when != 0 && n.when <= sbn.getPostTime()) {
816 return n.when;
817 }
818 // If we've ranked a previous instance with a timestamp, inherit it. This case is
819 // important in order to have ranking stability for updating notifications.
820 if (previousRankingTimeMs > 0) {
821 return previousRankingTimeMs;
822 }
823 return sbn.getPostTime();
824 }
Chris Wren1031c972014-07-23 13:11:45 +0000825
Christoph Studercd4adf82014-08-19 17:50:49 +0200826 public void setGlobalSortKey(String globalSortKey) {
827 mGlobalSortKey = globalSortKey;
Chris Wren1031c972014-07-23 13:11:45 +0000828 }
829
Christoph Studercd4adf82014-08-19 17:50:49 +0200830 public String getGlobalSortKey() {
831 return mGlobalSortKey;
Chris Wren1031c972014-07-23 13:11:45 +0000832 }
833
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700834 /** Check if any of the listeners have marked this notification as seen by the user. */
835 public boolean isSeen() {
Julia Reynolds503ed942017-10-04 16:04:56 -0400836 return mStats.hasSeen();
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700837 }
838
839 /** Mark the notification as seen by the user. */
840 public void setSeen() {
Julia Reynolds503ed942017-10-04 16:04:56 -0400841 mStats.setSeen();
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700842 }
843
Chris Wren1031c972014-07-23 13:11:45 +0000844 public void setAuthoritativeRank(int authoritativeRank) {
845 mAuthoritativeRank = authoritativeRank;
846 }
847
848 public int getAuthoritativeRank() {
849 return mAuthoritativeRank;
850 }
851
852 public String getGroupKey() {
853 return sbn.getGroupKey();
854 }
Chris Wren47633422016-01-22 09:56:59 -0500855
Chris Wrenb3921792017-06-01 13:34:46 -0400856 public void setOverrideGroupKey(String overrideGroupKey) {
857 sbn.setOverrideGroupKey(overrideGroupKey);
858 mGroupLogTag = null;
859 }
860
861 private String getGroupLogTag() {
862 if (mGroupLogTag == null) {
863 mGroupLogTag = shortenTag(sbn.getGroup());
864 }
865 return mGroupLogTag;
866 }
867
868 private String getChannelIdLogTag() {
869 if (mChannelIdLogTag == null) {
870 mChannelIdLogTag = shortenTag(mChannel.getId());
871 }
872 return mChannelIdLogTag;
873 }
874
875 private String shortenTag(String longTag) {
876 if (longTag == null) {
877 return null;
878 }
879 if (longTag.length() < MAX_LOGTAG_LENGTH) {
880 return longTag;
881 } else {
882 return longTag.substring(0, MAX_LOGTAG_LENGTH - 8) + "-" +
883 Integer.toHexString(longTag.hashCode());
884 }
885 }
886
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400887 public NotificationChannel getChannel() {
Julia Reynolds924eed12017-01-19 09:52:07 -0500888 return mChannel;
Julia Reynolds22f02b32016-12-01 15:05:13 -0500889 }
890
Rohan Shah590e1b22018-04-10 23:48:47 -0400891 /**
892 * @see RankingHelper#getIsAppImportanceLocked(String, int)
893 */
894 public boolean getIsAppImportanceLocked() {
895 return mIsAppImportanceLocked;
896 }
897
Julia Reynolds924eed12017-01-19 09:52:07 -0500898 protected void updateNotificationChannel(NotificationChannel channel) {
899 if (channel != null) {
900 mChannel = channel;
Julia Reynolds22f02b32016-12-01 15:05:13 -0500901 calculateImportance();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500902 calculateUserSentiment();
Julia Reynolds22f02b32016-12-01 15:05:13 -0500903 }
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400904 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500905
Julia Reynolds924eed12017-01-19 09:52:07 -0500906 public void setShowBadge(boolean showBadge) {
907 mShowBadge = showBadge;
908 }
909
910 public boolean canShowBadge() {
911 return mShowBadge;
912 }
913
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500914 public Light getLight() {
915 return mLight;
916 }
917
Julia Reynolds0c299d42016-11-15 14:37:04 -0500918 public Uri getSound() {
919 return mSound;
920 }
921
922 public long[] getVibration() {
923 return mVibration;
924 }
925
926 public AudioAttributes getAudioAttributes() {
927 return mAttributes;
928 }
Julia Reynolds22f02b32016-12-01 15:05:13 -0500929
930 public ArrayList<String> getPeopleOverride() {
931 return mPeopleOverride;
932 }
933
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500934 public void setInterruptive(boolean interruptive) {
935 mIsInterruptive = interruptive;
936 }
937
938 public boolean isInterruptive() {
939 return mIsInterruptive;
940 }
941
Julia Reynolds22f02b32016-12-01 15:05:13 -0500942 protected void setPeopleOverride(ArrayList<String> people) {
943 mPeopleOverride = people;
944 }
945
946 public ArrayList<SnoozeCriterion> getSnoozeCriteria() {
947 return mSnoozeCriteria;
948 }
949
950 protected void setSnoozeCriteria(ArrayList<SnoozeCriterion> snoozeCriteria) {
951 mSnoozeCriteria = snoozeCriteria;
952 }
Chris Wren9eb5e102017-01-26 13:15:06 -0500953
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500954 private void calculateUserSentiment() {
Rohan Shah590e1b22018-04-10 23:48:47 -0400955 if ((getChannel().getUserLockedFields() & USER_LOCKED_IMPORTANCE) != 0
956 || mIsAppImportanceLocked) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500957 mUserSentiment = USER_SENTIMENT_POSITIVE;
958 }
959 }
960
Julia Reynolds503ed942017-10-04 16:04:56 -0400961 private void setUserSentiment(int userSentiment) {
962 mUserSentiment = userSentiment;
963 }
964
965 public int getUserSentiment() {
966 return mUserSentiment;
967 }
968
969 public NotificationStats getStats() {
970 return mStats;
971 }
972
973 public void recordExpanded() {
974 mStats.setExpanded();
975 }
976
977 public void recordDirectReplied() {
978 mStats.setDirectReplied();
979 }
980
981 public void recordDismissalSurface(@NotificationStats.DismissalSurface int surface) {
982 mStats.setDismissalSurface(surface);
983 }
984
985 public void recordSnoozed() {
986 mStats.setSnoozed();
987 }
988
989 public void recordViewedSettings() {
990 mStats.setViewedSettings();
991 }
992
Kenny Guy23991102018-04-05 21:18:38 +0100993 public void setNumSmartRepliesAdded(int noReplies) {
994 mNumberOfSmartRepliesAdded = noReplies;
995 }
996
997 public int getNumSmartRepliesAdded() {
998 return mNumberOfSmartRepliesAdded;
999 }
1000
1001 public boolean hasSeenSmartReplies() {
1002 return mHasSeenSmartReplies;
1003 }
1004
1005 public void setSeenSmartReplies(boolean hasSeenSmartReplies) {
1006 mHasSeenSmartReplies = hasSeenSmartReplies;
1007 }
1008
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001009 /**
1010 * @return all {@link Uri} that should have permission granted to whoever
1011 * will be rendering it. This list has already been vetted to only
1012 * include {@link Uri} that the enqueuing app can grant.
1013 */
1014 public @Nullable ArraySet<Uri> getGrantableUris() {
1015 return mGrantableUris;
1016 }
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001017
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001018 /**
1019 * Collect all {@link Uri} that should have permission granted to whoever
1020 * will be rendering it.
1021 */
1022 private void calculateGrantableUris() {
1023 final Notification notification = getNotification();
Jeff Sharkey23b31182018-04-18 21:32:12 -06001024 notification.visitUris((uri) -> {
1025 visitGrantableUri(uri);
1026 });
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001027
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001028 if (notification.getChannelId() != null) {
1029 NotificationChannel channel = getChannel();
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001030 if (channel != null) {
Jeff Sharkey23b31182018-04-18 21:32:12 -06001031 visitGrantableUri(channel.getSound());
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001032 }
1033 }
1034 }
1035
1036 /**
1037 * Note the presence of a {@link Uri} that should have permission granted to
1038 * whoever will be rendering it.
1039 * <p>
1040 * If the enqueuing app has the ability to grant access, it will be added to
1041 * {@link #mGrantableUris}. Otherwise, this will either log or throw
1042 * {@link SecurityException} depending on target SDK of enqueuing app.
1043 */
Jeff Sharkey23b31182018-04-18 21:32:12 -06001044 private void visitGrantableUri(Uri uri) {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001045 if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return;
1046
1047 // We can't grant Uri permissions from system
1048 final int sourceUid = sbn.getUid();
1049 if (sourceUid == android.os.Process.SYSTEM_UID) return;
1050
1051 final IActivityManager am = ActivityManager.getService();
1052 final long ident = Binder.clearCallingIdentity();
1053 try {
1054 // This will throw SecurityException if caller can't grant
1055 am.checkGrantUriPermission(sourceUid, null,
1056 ContentProvider.getUriWithoutUserId(uri),
1057 Intent.FLAG_GRANT_READ_URI_PERMISSION,
1058 ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid)));
1059
1060 if (mGrantableUris == null) {
1061 mGrantableUris = new ArraySet<>();
1062 }
1063 mGrantableUris.add(uri);
1064 } catch (RemoteException ignored) {
1065 // Ignored because we're in same process
1066 } catch (SecurityException e) {
1067 if (mTargetSdkVersion >= Build.VERSION_CODES.P) {
1068 throw e;
1069 } else {
1070 Log.w(TAG, "Ignoring " + uri + " from " + sourceUid + ": " + e.getMessage());
1071 }
1072 } finally {
1073 Binder.restoreCallingIdentity(ident);
1074 }
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001075 }
1076
Chris Wren9eb5e102017-01-26 13:15:06 -05001077 public LogMaker getLogMaker(long now) {
1078 if (mLogMaker == null) {
Chris Wrenb3921792017-06-01 13:34:46 -04001079 // initialize fields that only change on update (so a new record)
Chris Wren9eb5e102017-01-26 13:15:06 -05001080 mLogMaker = new LogMaker(MetricsEvent.VIEW_UNKNOWN)
1081 .setPackageName(sbn.getPackageName())
1082 .addTaggedData(MetricsEvent.NOTIFICATION_ID, sbn.getId())
Chris Wrenb3921792017-06-01 13:34:46 -04001083 .addTaggedData(MetricsEvent.NOTIFICATION_TAG, sbn.getTag())
1084 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_ID, getChannelIdLogTag());
Chris Wren9eb5e102017-01-26 13:15:06 -05001085 }
Chris Wrenb3921792017-06-01 13:34:46 -04001086 // reset fields that can change between updates, or are used by multiple logs
Chris Wren9eb5e102017-01-26 13:15:06 -05001087 return mLogMaker
Chris Wrena7c1b802017-03-07 10:17:20 -05001088 .clearCategory()
1089 .clearType()
1090 .clearSubtype()
Chris Wren9eb5e102017-01-26 13:15:06 -05001091 .clearTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX)
Chris Wrenb3921792017-06-01 13:34:46 -04001092 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE, mImportance)
1093 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_ID, getGroupLogTag())
1094 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_GROUP_SUMMARY,
1095 sbn.getNotification().isGroupSummary() ? 1 : 0)
Chris Wren9eb5e102017-01-26 13:15:06 -05001096 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_CREATE_MILLIS, getLifespanMs(now))
1097 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_UPDATE_MILLIS, getFreshnessMs(now))
1098 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_VISIBLE_MILLIS, getExposureMs(now));
1099 }
1100
1101 public LogMaker getLogMaker() {
1102 return getLogMaker(System.currentTimeMillis());
1103 }
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05001104
1105 @VisibleForTesting
1106 static final class Light {
1107 public final int color;
1108 public final int onMs;
1109 public final int offMs;
1110
1111 public Light(int color, int onMs, int offMs) {
1112 this.color = color;
1113 this.onMs = onMs;
1114 this.offMs = offMs;
1115 }
1116
1117 @Override
1118 public boolean equals(Object o) {
1119 if (this == o) return true;
1120 if (o == null || getClass() != o.getClass()) return false;
1121
1122 Light light = (Light) o;
1123
1124 if (color != light.color) return false;
1125 if (onMs != light.onMs) return false;
1126 return offMs == light.offMs;
1127
1128 }
1129
1130 @Override
1131 public int hashCode() {
1132 int result = color;
1133 result = 31 * result + onMs;
1134 result = 31 * result + offMs;
1135 return result;
1136 }
1137
1138 @Override
1139 public String toString() {
1140 return "Light{" +
1141 "color=" + color +
1142 ", onMs=" + onMs +
1143 ", offMs=" + offMs +
1144 '}';
1145 }
1146 }
Chris Wren333a61c2014-05-28 16:40:57 -04001147}