blob: 2bbbffc203f4f31bfdf09c2f354a577842699f22 [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 Reynolds27c0a962018-12-10 12:37:28 -050024import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_NEUTRAL;
25import static android.service.notification.NotificationListenerService.Ranking.USER_SENTIMENT_POSITIVE;
Chris Wrenbdf33762015-12-04 15:50:51 -050026
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060027import android.annotation.Nullable;
28import android.app.ActivityManager;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060029import android.app.IActivityManager;
Chris Wren333a61c2014-05-28 16:40:57 -040030import android.app.Notification;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040031import android.app.NotificationChannel;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060032import android.content.ContentProvider;
33import android.content.ContentResolver;
Chris Wren333a61c2014-05-28 16:40:57 -040034import android.content.Context;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060035import android.content.Intent;
Tony Mantlerab55f0f2017-06-16 10:50:00 -070036import android.content.pm.PackageManager;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060037import android.content.pm.PackageManagerInternal;
Julia Reynoldsd61bdf12020-02-25 12:25:07 -050038import android.content.pm.ShortcutInfo;
Chris Wren333a61c2014-05-28 16:40:57 -040039import android.graphics.Bitmap;
John Spurlockbfa5dc42014-07-28 23:30:45 -040040import android.media.AudioAttributes;
Julia Reynolds0c299d42016-11-15 14:37:04 -050041import android.media.AudioSystem;
Chris Wren9eb5e102017-01-26 13:15:06 -050042import android.metrics.LogMaker;
Julia Reynolds0c299d42016-11-15 14:37:04 -050043import android.net.Uri;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060044import android.os.Binder;
Julia Reynolds0c299d42016-11-15 14:37:04 -050045import android.os.Build;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040046import android.os.Bundle;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060047import android.os.IBinder;
Chris Wrenda4bd202014-09-04 15:53:52 -040048import android.os.UserHandle;
Julia Reynolds0c299d42016-11-15 14:37:04 -050049import android.provider.Settings;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040050import android.service.notification.Adjustment;
Julia Reynolds5d25ee72015-11-20 15:38:20 -050051import android.service.notification.NotificationListenerService;
Julia Reynoldsc9842c12017-02-07 12:46:41 -050052import android.service.notification.NotificationRecordProto;
Julia Reynolds503ed942017-10-04 16:04:56 -040053import android.service.notification.NotificationStats;
Julia Reynolds22f02b32016-12-01 15:05:13 -050054import android.service.notification.SnoozeCriterion;
Chris Wren333a61c2014-05-28 16:40:57 -040055import android.service.notification.StatusBarNotification;
Julia Reynolds4b82f6d2017-01-04 10:47:41 -050056import android.text.TextUtils;
Julia Reynoldse0d711f2017-09-01 08:50:47 -040057import android.util.ArraySet;
Danning Chen10326cf2020-01-16 13:29:13 -080058import android.util.FeatureFlagUtils;
Julia Reynoldsf0f629f2016-02-25 09:34:04 -050059import android.util.Log;
Julia Reynolds2a128742016-11-28 14:29:25 -050060import android.util.TimeUtils;
Julia Reynoldsc9842c12017-02-07 12:46:41 -050061import android.util.proto.ProtoOutputStream;
Dan Sandler0a2308e2017-05-30 19:50:42 -040062import android.widget.RemoteViews;
John Spurlockbfa5dc42014-07-28 23:30:45 -040063
Chris Wren1031c972014-07-23 13:11:45 +000064import com.android.internal.annotations.VisibleForTesting;
Chris Wren9eb5e102017-01-26 13:15:06 -050065import com.android.internal.logging.MetricsLogger;
66import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Chris Wren6650e572015-05-15 17:19:25 -040067import com.android.server.EventLogTags;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060068import com.android.server.LocalServices;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070069import com.android.server.uri.UriGrantsManagerInternal;
Chris Wren333a61c2014-05-28 16:40:57 -040070
71import java.io.PrintWriter;
72import java.lang.reflect.Array;
Julia Reynolds22f02b32016-12-01 15:05:13 -050073import java.util.ArrayList;
Chris Wren333a61c2014-05-28 16:40:57 -040074import java.util.Arrays;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040075import java.util.List;
John Spurlock312d1d02014-07-08 10:24:57 -040076import java.util.Objects;
Chris Wren333a61c2014-05-28 16:40:57 -040077
78/**
79 * Holds data about notifications that should not be shared with the
80 * {@link android.service.notification.NotificationListenerService}s.
81 *
82 * <p>These objects should not be mutated unless the code is synchronized
Julia Reynolds88860ce2017-06-01 16:55:49 -040083 * on {@link NotificationManagerService#mNotificationLock}, and any
Chris Wren333a61c2014-05-28 16:40:57 -040084 * modification should be followed by a sorting of that list.</p>
85 *
86 * <p>Is sortable by {@link NotificationComparator}.</p>
87 *
88 * {@hide}
89 */
90public final class NotificationRecord {
Julia Reynoldsf0f629f2016-02-25 09:34:04 -050091 static final String TAG = "NotificationRecord";
92 static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
Julia Reynoldsd6730072019-01-04 12:52:52 -050093 // the period after which a notification is updated where it can make sound
94 private static final int MAX_SOUND_DELAY_MS = 2000;
Julia Reynolds24edc002020-01-29 16:35:32 -050095 private final StatusBarNotification sbn;
Julia Reynolds218871e2018-06-13 10:45:21 -040096 IActivityManager mAm;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070097 UriGrantsManagerInternal mUgmInternal;
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.
Julia Reynoldsd6730072019-01-04 12:52:52 -0500128 @VisibleForTesting
129 final long mUpdateTimeMs;
Chris Wren6650e572015-05-15 17:19:25 -0400130
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400131 // The most recent interruption time, or the creation time if no updates. Differs from the
132 // above value because updates are filtered based on whether they actually interrupted the
133 // user
134 private long mInterruptionTimeMs;
135
Gus Prevas7306b902018-12-11 10:57:06 -0500136 // The most recent time the notification made noise or buzzed the device, or -1 if it did not.
137 private long mLastAudiblyAlertedMs;
138
Chris Wrena3446562014-06-03 18:11:47 -0400139 // Is this record an update of an old record?
140 public boolean isUpdate;
Chris Wren54bbef42014-07-09 18:37:56 -0400141 private int mPackagePriority;
Chris Wrena3446562014-06-03 18:11:47 -0400142
Chris Wren1031c972014-07-23 13:11:45 +0000143 private int mAuthoritativeRank;
Christoph Studercd4adf82014-08-19 17:50:49 +0200144 private String mGlobalSortKey;
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400145 private int mPackageVisibility;
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400146 private int mSystemImportance = IMPORTANCE_UNSPECIFIED;
147 private int mAssistantImportance = IMPORTANCE_UNSPECIFIED;
Chris Wren47633422016-01-22 09:56:59 -0500148 private int mImportance = IMPORTANCE_UNSPECIFIED;
Alex Mang730d9492019-12-03 18:06:09 -0800149 private float mRankingScore = 0f;
Brad Stenning9a8b2c82018-08-03 14:14:26 -0700150 // Field used in global sort key to bypass normal notifications
151 private int mCriticality = CriticalNotificationExtractor.NORMAL;
Will Brockman934b8e32019-03-08 11:14:45 -0500152 // A MetricsEvent.NotificationImportanceExplanation, tracking source of mImportance.
153 private int mImportanceExplanationCode = MetricsEvent.IMPORTANCE_EXPLANATION_UNKNOWN;
154 // A MetricsEvent.NotificationImportanceExplanation for initial importance.
155 private int mInitialImportanceExplanationCode = MetricsEvent.IMPORTANCE_EXPLANATION_UNKNOWN;
Chris Wren1031c972014-07-23 13:11:45 +0000156
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500157 private int mSuppressedVisualEffects = 0;
Julia Reynoldsef37f282016-02-12 09:11:27 -0500158 private String mUserExplanation;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000159 private boolean mPreChannelsNotification = true;
Julia Reynolds0c299d42016-11-15 14:37:04 -0500160 private Uri mSound;
161 private long[] mVibration;
162 private AudioAttributes mAttributes;
Julia Reynolds924eed12017-01-19 09:52:07 -0500163 private NotificationChannel mChannel;
Julia Reynolds22f02b32016-12-01 15:05:13 -0500164 private ArrayList<String> mPeopleOverride;
165 private ArrayList<SnoozeCriterion> mSnoozeCriteria;
Julia Reynolds924eed12017-01-19 09:52:07 -0500166 private boolean mShowBadge;
Julia Reynolds4509ce72019-01-31 13:12:43 -0500167 private boolean mAllowBubble;
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500168 private Light mLight;
Danning Chen10326cf2020-01-16 13:29:13 -0800169 private boolean mIsNotConversationOverride;
Julia Reynoldsd61bdf12020-02-25 12:25:07 -0500170 private ShortcutInfo mShortcutInfo;
Gustav Sennton1463d832018-11-06 16:12:48 +0000171 /**
172 * This list contains system generated smart actions from NAS, app-generated smart actions are
Gustav Sennton005d7a02019-01-04 13:41:32 +0000173 * stored in Notification.actions with isContextual() set to true.
Gustav Sennton1463d832018-11-06 16:12:48 +0000174 */
175 private ArrayList<Notification.Action> mSystemGeneratedSmartActions;
Tony Makc9acf672018-07-20 13:58:24 +0200176 private ArrayList<CharSequence> mSmartReplies;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500177
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400178 private final List<Adjustment> mAdjustments;
Jan Althaus367eb8c2019-05-23 11:31:42 +0200179 private String mAdjustmentIssuer;
Julia Reynolds503ed942017-10-04 16:04:56 -0400180 private final NotificationStats mStats;
181 private int mUserSentiment;
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500182 private boolean mIsInterruptive;
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400183 private boolean mTextChanged;
184 private boolean mRecordedInterruption;
Kenny Guy23991102018-04-05 21:18:38 +0100185 private int mNumberOfSmartRepliesAdded;
Gustav Senntond25a64d2018-12-07 10:58:39 +0000186 private int mNumberOfSmartActionsAdded;
187 private boolean mSuggestionsGeneratedByAssistant;
Milo Sredkov13d88112019-02-01 12:23:24 +0000188 private boolean mEditChoicesBeforeSending;
Kenny Guy23991102018-04-05 21:18:38 +0100189 private boolean mHasSeenSmartReplies;
Mady Mellor13f9bc82020-03-24 19:09:28 -0700190 private boolean mFlagBubbleRemoved;
Julia Reynolds65031622020-02-27 09:11:25 -0500191 private boolean mPostSilently;
Rohan Shah590e1b22018-04-10 23:48:47 -0400192 /**
193 * Whether this notification (and its channels) should be considered user locked. Used in
194 * conjunction with user sentiment calculation.
195 */
196 private boolean mIsAppImportanceLocked;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600197 private ArraySet<Uri> mGrantableUris;
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400198
Julia Reynolds924eed12017-01-19 09:52:07 -0500199 public NotificationRecord(Context context, StatusBarNotification sbn,
Rohan Shah590e1b22018-04-10 23:48:47 -0400200 NotificationChannel channel) {
Chris Wren333a61c2014-05-28 16:40:57 -0400201 this.sbn = sbn;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600202 mTargetSdkVersion = LocalServices.getService(PackageManagerInternal.class)
203 .getPackageTargetSdkVersion(sbn.getPackageName());
Julia Reynolds218871e2018-06-13 10:45:21 -0400204 mAm = ActivityManager.getService();
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700205 mUgmInternal = LocalServices.getService(UriGrantsManagerInternal.class);
Christoph Studer365e4c32014-09-18 20:35:36 +0200206 mOriginalFlags = sbn.getNotification().flags;
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200207 mRankingTimeMs = calculateRankingTimeMs(0L);
Chris Wren640e3872015-04-21 13:23:18 -0400208 mCreationTimeMs = sbn.getPostTime();
Chris Wren6650e572015-05-15 17:19:25 -0400209 mUpdateTimeMs = mCreationTimeMs;
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400210 mInterruptionTimeMs = mCreationTimeMs;
Chris Wrenbdf33762015-12-04 15:50:51 -0500211 mContext = context;
Chris Wrencdee8cd2016-01-25 17:10:30 -0500212 stats = new NotificationUsageStats.SingleNotificationStats();
Julia Reynolds924eed12017-01-19 09:52:07 -0500213 mChannel = channel;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000214 mPreChannelsNotification = isPreChannelsNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500215 mSound = calculateSound();
216 mVibration = calculateVibration();
217 mAttributes = calculateAttributes();
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400218 mImportance = calculateInitialImportance();
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500219 mLight = calculateLights();
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400220 mAdjustments = new ArrayList<>();
Julia Reynolds503ed942017-10-04 16:04:56 -0400221 mStats = new NotificationStats();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500222 calculateUserSentiment();
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600223 calculateGrantableUris();
Chris Wrenbdf33762015-12-04 15:50:51 -0500224 }
225
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000226 private boolean isPreChannelsNotification() {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600227 if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(getChannel().getId())) {
228 if (mTargetSdkVersion < Build.VERSION_CODES.O) {
229 return true;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000230 }
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000231 }
232 return false;
233 }
234
Julia Reynolds0c299d42016-11-15 14:37:04 -0500235 private Uri calculateSound() {
Julia Reynolds24edc002020-01-29 16:35:32 -0500236 final Notification n = getSbn().getNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500237
Tony Mantlerab55f0f2017-06-16 10:50:00 -0700238 // No notification sounds on tv
239 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
240 return null;
241 }
242
Julia Reynolds924eed12017-01-19 09:52:07 -0500243 Uri sound = mChannel.getSound();
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000244 if (mPreChannelsNotification && (getChannel().getUserLockedFields()
Julia Reynolds0c299d42016-11-15 14:37:04 -0500245 & NotificationChannel.USER_LOCKED_SOUND) == 0) {
246
247 final boolean useDefaultSound = (n.defaults & Notification.DEFAULT_SOUND) != 0;
248 if (useDefaultSound) {
249 sound = Settings.System.DEFAULT_NOTIFICATION_URI;
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400250 } else {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500251 sound = n.sound;
252 }
253 }
254 return sound;
255 }
256
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500257 private Light calculateLights() {
258 int defaultLightColor = mContext.getResources().getColor(
259 com.android.internal.R.color.config_defaultNotificationColor);
260 int defaultLightOn = mContext.getResources().getInteger(
261 com.android.internal.R.integer.config_defaultNotificationLedOn);
262 int defaultLightOff = mContext.getResources().getInteger(
263 com.android.internal.R.integer.config_defaultNotificationLedOff);
264
Julia Reynolds529e3322017-02-06 08:33:01 -0500265 int channelLightColor = getChannel().getLightColor() != 0 ? getChannel().getLightColor()
266 : defaultLightColor;
267 Light light = getChannel().shouldShowLights() ? new Light(channelLightColor,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500268 defaultLightOn, defaultLightOff) : null;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000269 if (mPreChannelsNotification
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500270 && (getChannel().getUserLockedFields()
271 & NotificationChannel.USER_LOCKED_LIGHTS) == 0) {
Julia Reynolds24edc002020-01-29 16:35:32 -0500272 final Notification notification = getSbn().getNotification();
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500273 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
274 light = new Light(notification.ledARGB, notification.ledOnMS,
275 notification.ledOffMS);
276 if ((notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
277 light = new Light(defaultLightColor, defaultLightOn,
278 defaultLightOff);
279 }
280 } else {
281 light = null;
282 }
283 }
284 return light;
285 }
286
Julia Reynolds0c299d42016-11-15 14:37:04 -0500287 private long[] calculateVibration() {
288 long[] vibration;
289 final long[] defaultVibration = NotificationManagerService.getLongArray(
290 mContext.getResources(),
291 com.android.internal.R.array.config_defaultNotificationVibePattern,
292 NotificationManagerService.VIBRATE_PATTERN_MAXLEN,
293 NotificationManagerService.DEFAULT_VIBRATE_PATTERN);
294 if (getChannel().shouldVibrate()) {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500295 vibration = getChannel().getVibrationPattern() == null
296 ? defaultVibration : getChannel().getVibrationPattern();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500297 } else {
298 vibration = null;
299 }
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000300 if (mPreChannelsNotification
Julia Reynolds0c299d42016-11-15 14:37:04 -0500301 && (getChannel().getUserLockedFields()
302 & NotificationChannel.USER_LOCKED_VIBRATION) == 0) {
Julia Reynolds24edc002020-01-29 16:35:32 -0500303 final Notification notification = getSbn().getNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500304 final boolean useDefaultVibrate =
305 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
306 if (useDefaultVibrate) {
307 vibration = defaultVibration;
308 } else {
309 vibration = notification.vibrate;
310 }
311 }
312 return vibration;
313 }
314
315 private AudioAttributes calculateAttributes() {
Julia Reynolds24edc002020-01-29 16:35:32 -0500316 final Notification n = getSbn().getNotification();
Julia Reynolds619a69f2017-01-27 15:11:38 -0500317 AudioAttributes attributes = getChannel().getAudioAttributes();
318 if (attributes == null) {
319 attributes = Notification.AUDIO_ATTRIBUTES_DEFAULT;
320 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500321
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000322 if (mPreChannelsNotification
Julia Reynolds619a69f2017-01-27 15:11:38 -0500323 && (getChannel().getUserLockedFields()
324 & NotificationChannel.USER_LOCKED_SOUND) == 0) {
325 if (n.audioAttributes != null) {
326 // prefer audio attributes to stream type
327 attributes = n.audioAttributes;
328 } else if (n.audioStreamType >= 0
329 && n.audioStreamType < AudioSystem.getNumStreamTypes()) {
330 // the stream type is valid, use it
331 attributes = new AudioAttributes.Builder()
332 .setInternalLegacyStreamType(n.audioStreamType)
333 .build();
334 } else if (n.audioStreamType != AudioSystem.STREAM_DEFAULT) {
335 Log.w(TAG, String.format("Invalid stream type: %d", n.audioStreamType));
336 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500337 }
338 return attributes;
339 }
340
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400341 private int calculateInitialImportance() {
Julia Reynolds24edc002020-01-29 16:35:32 -0500342 final Notification n = getSbn().getNotification();
Will Brockman934b8e32019-03-08 11:14:45 -0500343 int importance = getChannel().getImportance(); // Post-channels notifications use this
344 mInitialImportanceExplanationCode = getChannel().hasUserSetImportance()
345 ? MetricsEvent.IMPORTANCE_EXPLANATION_USER
346 : MetricsEvent.IMPORTANCE_EXPLANATION_APP;
Chris Wrenbdf33762015-12-04 15:50:51 -0500347
Will Brockman934b8e32019-03-08 11:14:45 -0500348 // Migrate notification priority flag to a priority value.
Chris Wrenbdf33762015-12-04 15:50:51 -0500349 if (0 != (n.flags & Notification.FLAG_HIGH_PRIORITY)) {
350 n.priority = Notification.PRIORITY_MAX;
351 }
352
Will Brockman934b8e32019-03-08 11:14:45 -0500353 // Convert priority value to an importance value, used only for pre-channels notifications.
354 int requestedImportance = IMPORTANCE_DEFAULT;
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500355 n.priority = NotificationManagerService.clamp(n.priority, Notification.PRIORITY_MIN,
356 Notification.PRIORITY_MAX);
Chris Wrenbdf33762015-12-04 15:50:51 -0500357 switch (n.priority) {
358 case Notification.PRIORITY_MIN:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500359 requestedImportance = IMPORTANCE_MIN;
Julia Reynoldsf0f629f2016-02-25 09:34:04 -0500360 break;
Chris Wrenbdf33762015-12-04 15:50:51 -0500361 case Notification.PRIORITY_LOW:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500362 requestedImportance = IMPORTANCE_LOW;
Chris Wrenbdf33762015-12-04 15:50:51 -0500363 break;
364 case Notification.PRIORITY_DEFAULT:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500365 requestedImportance = IMPORTANCE_DEFAULT;
Chris Wrenbdf33762015-12-04 15:50:51 -0500366 break;
367 case Notification.PRIORITY_HIGH:
Chris Wrenbdf33762015-12-04 15:50:51 -0500368 case Notification.PRIORITY_MAX:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500369 requestedImportance = IMPORTANCE_HIGH;
Chris Wrenbdf33762015-12-04 15:50:51 -0500370 break;
371 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500372 stats.requestedImportance = requestedImportance;
373 stats.isNoisy = mSound != null || mVibration != null;
Chris Wrenbdf33762015-12-04 15:50:51 -0500374
Will Brockman934b8e32019-03-08 11:14:45 -0500375 // For pre-channels notifications, apply system overrides and then use requestedImportance
376 // as importance.
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000377 if (mPreChannelsNotification
Julia Reynoldsa917a112017-03-21 11:09:14 -0400378 && (importance == IMPORTANCE_UNSPECIFIED
Will Brockman934b8e32019-03-08 11:14:45 -0500379 || (!getChannel().hasUserSetImportance()))) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500380 if (!stats.isNoisy && requestedImportance > IMPORTANCE_LOW) {
381 requestedImportance = IMPORTANCE_LOW;
Julia Reynolds83fa1072016-02-17 09:10:19 -0500382 }
Julia Reynolds83fa1072016-02-17 09:10:19 -0500383
Julia Reynolds0c299d42016-11-15 14:37:04 -0500384 if (stats.isNoisy) {
385 if (requestedImportance < IMPORTANCE_DEFAULT) {
386 requestedImportance = IMPORTANCE_DEFAULT;
387 }
388 }
389
390 if (n.fullScreenIntent != null) {
391 requestedImportance = IMPORTANCE_HIGH;
392 }
393 importance = requestedImportance;
Will Brockman934b8e32019-03-08 11:14:45 -0500394 mInitialImportanceExplanationCode =
395 MetricsEvent.IMPORTANCE_EXPLANATION_APP_PRE_CHANNELS;
Chris Wrenbdf33762015-12-04 15:50:51 -0500396 }
397
Chris Wrencdee8cd2016-01-25 17:10:30 -0500398 stats.naturalImportance = importance;
Chris Wrenbdf33762015-12-04 15:50:51 -0500399 return importance;
Chris Wren333a61c2014-05-28 16:40:57 -0400400 }
401
402 // copy any notes that the ranking system may have made before the update
403 public void copyRankingInformation(NotificationRecord previous) {
404 mContactAffinity = previous.mContactAffinity;
405 mRecentlyIntrusive = previous.mRecentlyIntrusive;
Chris Wren54bbef42014-07-09 18:37:56 -0400406 mPackagePriority = previous.mPackagePriority;
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400407 mPackageVisibility = previous.mPackageVisibility;
Chris Wren333a61c2014-05-28 16:40:57 -0400408 mIntercept = previous.mIntercept;
Beverly5a20a5e2018-03-06 15:02:44 -0500409 mHidden = previous.mHidden;
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200410 mRankingTimeMs = calculateRankingTimeMs(previous.getRankingTimeMs());
Chris Wren640e3872015-04-21 13:23:18 -0400411 mCreationTimeMs = previous.mCreationTimeMs;
Chris Wren6650e572015-05-15 17:19:25 -0400412 mVisibleSinceMs = previous.mVisibleSinceMs;
Julia Reynolds24edc002020-01-29 16:35:32 -0500413 if (previous.getSbn().getOverrideGroupKey() != null && !getSbn().isAppGroup()) {
414 getSbn().setOverrideGroupKey(previous.getSbn().getOverrideGroupKey());
Chris Wren8a1638f2016-05-02 16:19:14 -0400415 }
Chris Wren1f602dc2016-04-11 10:33:46 -0400416 // Don't copy importance information or mGlobalSortKey, recompute them.
Chris Wren333a61c2014-05-28 16:40:57 -0400417 }
418
Julia Reynolds24edc002020-01-29 16:35:32 -0500419 public Notification getNotification() { return getSbn().getNotification(); }
420 public int getFlags() { return getSbn().getNotification().flags; }
421 public UserHandle getUser() { return getSbn().getUser(); }
422 public String getKey() { return getSbn().getKey(); }
Chris Wrenda4bd202014-09-04 15:53:52 -0400423 /** @deprecated Use {@link #getUser()} instead. */
Julia Reynolds24edc002020-01-29 16:35:32 -0500424 public int getUserId() { return getSbn().getUserId(); }
425 public int getUid() { return getSbn().getUid(); }
Chris Wren333a61c2014-05-28 16:40:57 -0400426
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800427 void dump(ProtoOutputStream proto, long fieldId, boolean redact, int state) {
428 final long token = proto.start(fieldId);
429
Julia Reynolds24edc002020-01-29 16:35:32 -0500430 proto.write(NotificationRecordProto.KEY, getSbn().getKey());
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800431 proto.write(NotificationRecordProto.STATE, state);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500432 if (getChannel() != null) {
433 proto.write(NotificationRecordProto.CHANNEL_ID, getChannel().getId());
434 }
435 proto.write(NotificationRecordProto.CAN_SHOW_LIGHT, getLight() != null);
436 proto.write(NotificationRecordProto.CAN_VIBRATE, getVibration() != null);
Julia Reynolds24edc002020-01-29 16:35:32 -0500437 proto.write(NotificationRecordProto.FLAGS, getSbn().getNotification().flags);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500438 proto.write(NotificationRecordProto.GROUP_KEY, getGroupKey());
439 proto.write(NotificationRecordProto.IMPORTANCE, getImportance());
440 if (getSound() != null) {
441 proto.write(NotificationRecordProto.SOUND, getSound().toString());
442 }
443 if (getAudioAttributes() != null) {
Jeffrey Huangcb782852019-12-05 11:28:11 -0800444 getAudioAttributes().dumpDebug(proto, NotificationRecordProto.AUDIO_ATTRIBUTES);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500445 }
Julia Reynolds24edc002020-01-29 16:35:32 -0500446 proto.write(NotificationRecordProto.PACKAGE, getSbn().getPackageName());
447 proto.write(NotificationRecordProto.DELEGATE_PACKAGE, getSbn().getOpPkg());
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800448
449 proto.end(token);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500450 }
451
Dan Sandler0a2308e2017-05-30 19:50:42 -0400452 String formatRemoteViews(RemoteViews rv) {
453 if (rv == null) return "null";
454 return String.format("%s/0x%08x (%d bytes): %s",
455 rv.getPackage(), rv.getLayoutId(), rv.estimateMemoryUsage(), rv.toString());
456 }
457
Dan Sandlera1770312015-07-10 13:59:29 -0400458 void dump(PrintWriter pw, String prefix, Context baseContext, boolean redact) {
Julia Reynolds24edc002020-01-29 16:35:32 -0500459 final Notification notification = getSbn().getNotification();
Chris Wren333a61c2014-05-28 16:40:57 -0400460 pw.println(prefix + this);
Dan Sandler0a2308e2017-05-30 19:50:42 -0400461 prefix = prefix + " ";
Julia Reynolds24edc002020-01-29 16:35:32 -0500462 pw.println(prefix + "uid=" + getSbn().getUid() + " userId=" + getSbn().getUserId());
463 pw.println(prefix + "opPkg=" + getSbn().getOpPkg());
Julia Reynoldsc4ced652019-07-10 12:41:55 -0400464 pw.println(prefix + "icon=" + notification.getSmallIcon());
Julia Reynolds4db59552017-06-30 13:34:01 -0400465 pw.println(prefix + "flags=0x" + Integer.toHexString(notification.flags));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400466 pw.println(prefix + "pri=" + notification.priority);
Julia Reynolds24edc002020-01-29 16:35:32 -0500467 pw.println(prefix + "key=" + getSbn().getKey());
Julia Reynolds503ed942017-10-04 16:04:56 -0400468 pw.println(prefix + "seen=" + mStats.hasSeen());
Julia Reynoldsa917a112017-03-21 11:09:14 -0400469 pw.println(prefix + "groupKey=" + getGroupKey());
470 pw.println(prefix + "fullscreenIntent=" + notification.fullScreenIntent);
471 pw.println(prefix + "contentIntent=" + notification.contentIntent);
472 pw.println(prefix + "deleteIntent=" + notification.deleteIntent);
Aaron Heuckrothb4d6aa72018-07-02 14:58:33 -0400473 pw.println(prefix + "number=" + notification.number);
474 pw.println(prefix + "groupAlertBehavior=" + notification.getGroupAlertBehavior());
Julia Reynolds434a2922020-01-21 13:33:59 -0500475 pw.println(prefix + "when=" + notification.when);
Dan Sandler0b4ceb32017-03-29 14:13:55 -0400476
477 pw.print(prefix + "tickerText=");
478 if (!TextUtils.isEmpty(notification.tickerText)) {
479 final String ticker = notification.tickerText.toString();
480 if (redact) {
481 // if the string is long enough, we allow ourselves a few bytes for debugging
482 pw.print(ticker.length() > 16 ? ticker.substring(0,8) : "");
483 pw.println("...");
484 } else {
485 pw.println(ticker);
486 }
487 } else {
488 pw.println("null");
489 }
Dan Sandler0a2308e2017-05-30 19:50:42 -0400490 pw.println(prefix + "contentView=" + formatRemoteViews(notification.contentView));
491 pw.println(prefix + "bigContentView=" + formatRemoteViews(notification.bigContentView));
492 pw.println(prefix + "headsUpContentView="
493 + formatRemoteViews(notification.headsUpContentView));
494 pw.print(prefix + String.format("color=0x%08x", notification.color));
Julia Reynoldsbad42972017-04-25 13:52:49 -0400495 pw.println(prefix + "timeout="
496 + TimeUtils.formatForLogging(notification.getTimeoutAfter()));
Chris Wren333a61c2014-05-28 16:40:57 -0400497 if (notification.actions != null && notification.actions.length > 0) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400498 pw.println(prefix + "actions={");
Chris Wren333a61c2014-05-28 16:40:57 -0400499 final int N = notification.actions.length;
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500500 for (int i = 0; i < N; i++) {
Chris Wren333a61c2014-05-28 16:40:57 -0400501 final Notification.Action action = notification.actions[i];
Chris Wren1ac52a92016-02-24 14:54:52 -0500502 if (action != null) {
503 pw.println(String.format("%s [%d] \"%s\" -> %s",
504 prefix,
505 i,
506 action.title,
507 action.actionIntent == null ? "null" : action.actionIntent.toString()
508 ));
509 }
Chris Wren333a61c2014-05-28 16:40:57 -0400510 }
511 pw.println(prefix + " }");
512 }
513 if (notification.extras != null && notification.extras.size() > 0) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400514 pw.println(prefix + "extras={");
Chris Wren333a61c2014-05-28 16:40:57 -0400515 for (String key : notification.extras.keySet()) {
516 pw.print(prefix + " " + key + "=");
517 Object val = notification.extras.get(key);
518 if (val == null) {
519 pw.println("null");
520 } else {
521 pw.print(val.getClass().getSimpleName());
Dan Sandlera1770312015-07-10 13:59:29 -0400522 if (redact && (val instanceof CharSequence || val instanceof String)) {
Chris Wren333a61c2014-05-28 16:40:57 -0400523 // redact contents from bugreports
524 } else if (val instanceof Bitmap) {
525 pw.print(String.format(" (%dx%d)",
526 ((Bitmap) val).getWidth(),
527 ((Bitmap) val).getHeight()));
528 } else if (val.getClass().isArray()) {
529 final int N = Array.getLength(val);
Dan Sandlera1770312015-07-10 13:59:29 -0400530 pw.print(" (" + N + ")");
531 if (!redact) {
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500532 for (int j = 0; j < N; j++) {
Dan Sandlera1770312015-07-10 13:59:29 -0400533 pw.println();
534 pw.print(String.format("%s [%d] %s",
535 prefix, j, String.valueOf(Array.get(val, j))));
536 }
537 }
Chris Wren333a61c2014-05-28 16:40:57 -0400538 } else {
539 pw.print(" (" + String.valueOf(val) + ")");
540 }
541 pw.println();
542 }
543 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400544 pw.println(prefix + "}");
Chris Wren333a61c2014-05-28 16:40:57 -0400545 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400546 pw.println(prefix + "stats=" + stats.toString());
547 pw.println(prefix + "mContactAffinity=" + mContactAffinity);
548 pw.println(prefix + "mRecentlyIntrusive=" + mRecentlyIntrusive);
549 pw.println(prefix + "mPackagePriority=" + mPackagePriority);
550 pw.println(prefix + "mPackageVisibility=" + mPackageVisibility);
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400551 pw.println(prefix + "mSystemImportance="
552 + NotificationListenerService.Ranking.importanceToString(mSystemImportance));
553 pw.println(prefix + "mAsstImportance="
554 + NotificationListenerService.Ranking.importanceToString(mAssistantImportance));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400555 pw.println(prefix + "mImportance="
Chris Wrenbdf33762015-12-04 15:50:51 -0500556 + NotificationListenerService.Ranking.importanceToString(mImportance));
Will Brockman934b8e32019-03-08 11:14:45 -0500557 pw.println(prefix + "mImportanceExplanation=" + getImportanceExplanation());
Rohan Shah590e1b22018-04-10 23:48:47 -0400558 pw.println(prefix + "mIsAppImportanceLocked=" + mIsAppImportanceLocked);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400559 pw.println(prefix + "mIntercept=" + mIntercept);
Beverly5a20a5e2018-03-06 15:02:44 -0500560 pw.println(prefix + "mHidden==" + mHidden);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400561 pw.println(prefix + "mGlobalSortKey=" + mGlobalSortKey);
562 pw.println(prefix + "mRankingTimeMs=" + mRankingTimeMs);
563 pw.println(prefix + "mCreationTimeMs=" + mCreationTimeMs);
564 pw.println(prefix + "mVisibleSinceMs=" + mVisibleSinceMs);
565 pw.println(prefix + "mUpdateTimeMs=" + mUpdateTimeMs);
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400566 pw.println(prefix + "mInterruptionTimeMs=" + mInterruptionTimeMs);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400567 pw.println(prefix + "mSuppressedVisualEffects= " + mSuppressedVisualEffects);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000568 if (mPreChannelsNotification) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400569 pw.println(prefix + String.format("defaults=0x%08x flags=0x%08x",
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500570 notification.defaults, notification.flags));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400571 pw.println(prefix + "n.sound=" + notification.sound);
572 pw.println(prefix + "n.audioStreamType=" + notification.audioStreamType);
573 pw.println(prefix + "n.audioAttributes=" + notification.audioAttributes);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500574 pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d",
575 notification.ledARGB, notification.ledOnMS, notification.ledOffMS));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400576 pw.println(prefix + "vibrate=" + Arrays.toString(notification.vibrate));
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500577 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400578 pw.println(prefix + "mSound= " + mSound);
579 pw.println(prefix + "mVibration= " + mVibration);
580 pw.println(prefix + "mAttributes= " + mAttributes);
581 pw.println(prefix + "mLight= " + mLight);
582 pw.println(prefix + "mShowBadge=" + mShowBadge);
Julia Reynolds4db59552017-06-30 13:34:01 -0400583 pw.println(prefix + "mColorized=" + notification.isColorized());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500584 pw.println(prefix + "mIsInterruptive=" + mIsInterruptive);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400585 pw.println(prefix + "effectiveNotificationChannel=" + getChannel());
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500586 if (getPeopleOverride() != null) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400587 pw.println(prefix + "overridePeople= " + TextUtils.join(",", getPeopleOverride()));
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500588 }
589 if (getSnoozeCriteria() != null) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400590 pw.println(prefix + "snoozeCriteria=" + TextUtils.join(",", getSnoozeCriteria()));
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500591 }
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400592 pw.println(prefix + "mAdjustments=" + mAdjustments);
Julia Reynoldsfa273072020-04-14 15:31:21 -0400593 pw.println(prefix + "shortcut=" + notification.getShortcutId()
594 + " found valid? " + (mShortcutInfo != null));
Chris Wren333a61c2014-05-28 16:40:57 -0400595 }
596
Chris Wren333a61c2014-05-28 16:40:57 -0400597 @Override
598 public final String toString() {
599 return String.format(
Julia Reynolds85769912016-10-25 09:08:57 -0400600 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s importance=%d key=%s" +
Julia Reynolds34523cd2019-07-02 10:13:29 -0400601 ": %s)",
Chris Wren333a61c2014-05-28 16:40:57 -0400602 System.identityHashCode(this),
Julia Reynolds24edc002020-01-29 16:35:32 -0500603 this.getSbn().getPackageName(), this.getSbn().getUser(), this.getSbn().getId(),
604 this.getSbn().getTag(), this.mImportance, this.getSbn().getKey(),
605 this.getSbn().getNotification());
Chris Wren333a61c2014-05-28 16:40:57 -0400606 }
607
Julia Reynolds27c0a962018-12-10 12:37:28 -0500608 public boolean hasAdjustment(String key) {
609 synchronized (mAdjustments) {
610 for (Adjustment adjustment : mAdjustments) {
611 if (adjustment.getSignals().containsKey(key)) {
612 return true;
613 }
614 }
615 }
616 return false;
617 }
618
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400619 public void addAdjustment(Adjustment adjustment) {
620 synchronized (mAdjustments) {
621 mAdjustments.add(adjustment);
622 }
623 }
624
625 public void applyAdjustments() {
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400626 long now = System.currentTimeMillis();
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400627 synchronized (mAdjustments) {
628 for (Adjustment adjustment: mAdjustments) {
629 Bundle signals = adjustment.getSignals();
630 if (signals.containsKey(Adjustment.KEY_PEOPLE)) {
631 final ArrayList<String> people =
632 adjustment.getSignals().getStringArrayList(Adjustment.KEY_PEOPLE);
633 setPeopleOverride(people);
634 }
635 if (signals.containsKey(Adjustment.KEY_SNOOZE_CRITERIA)) {
636 final ArrayList<SnoozeCriterion> snoozeCriterionList =
637 adjustment.getSignals().getParcelableArrayList(
638 Adjustment.KEY_SNOOZE_CRITERIA);
639 setSnoozeCriteria(snoozeCriterionList);
640 }
641 if (signals.containsKey(Adjustment.KEY_GROUP_KEY)) {
642 final String groupOverrideKey =
643 adjustment.getSignals().getString(Adjustment.KEY_GROUP_KEY);
644 setOverrideGroupKey(groupOverrideKey);
645 }
Julia Reynolds503ed942017-10-04 16:04:56 -0400646 if (signals.containsKey(Adjustment.KEY_USER_SENTIMENT)) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500647 // Only allow user sentiment update from assistant if user hasn't already
648 // expressed a preference for this channel
Rohan Shah590e1b22018-04-10 23:48:47 -0400649 if (!mIsAppImportanceLocked
650 && (getChannel().getUserLockedFields() & USER_LOCKED_IMPORTANCE) == 0) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500651 setUserSentiment(adjustment.getSignals().getInt(
652 Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEUTRAL));
653 }
Julia Reynolds503ed942017-10-04 16:04:56 -0400654 }
Tony Maka4716992019-01-24 15:41:59 +0000655 if (signals.containsKey(Adjustment.KEY_CONTEXTUAL_ACTIONS)) {
Gustav Sennton1463d832018-11-06 16:12:48 +0000656 setSystemGeneratedSmartActions(
Tony Maka4716992019-01-24 15:41:59 +0000657 signals.getParcelableArrayList(Adjustment.KEY_CONTEXTUAL_ACTIONS));
Tony Mak628cb932018-06-19 18:30:41 +0100658 }
Tony Maka4716992019-01-24 15:41:59 +0000659 if (signals.containsKey(Adjustment.KEY_TEXT_REPLIES)) {
660 setSmartReplies(signals.getCharSequenceArrayList(Adjustment.KEY_TEXT_REPLIES));
Tony Makc9acf672018-07-20 13:58:24 +0200661 }
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400662 if (signals.containsKey(Adjustment.KEY_IMPORTANCE)) {
663 int importance = signals.getInt(Adjustment.KEY_IMPORTANCE);
664 importance = Math.max(IMPORTANCE_UNSPECIFIED, importance);
665 importance = Math.min(IMPORTANCE_HIGH, importance);
666 setAssistantImportance(importance);
667 }
Alex Mang730d9492019-12-03 18:06:09 -0800668 if (signals.containsKey(Adjustment.KEY_RANKING_SCORE)) {
669 mRankingScore = signals.getFloat(Adjustment.KEY_RANKING_SCORE);
670 }
Danning Chen10326cf2020-01-16 13:29:13 -0800671 if (signals.containsKey(Adjustment.KEY_NOT_CONVERSATION)) {
672 mIsNotConversationOverride = signals.getBoolean(
673 Adjustment.KEY_NOT_CONVERSATION);
674 }
Jan Althaus367eb8c2019-05-23 11:31:42 +0200675 if (!signals.isEmpty() && adjustment.getIssuer() != null) {
676 mAdjustmentIssuer = adjustment.getIssuer();
677 }
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400678 }
Will Brockmanb020b5e2019-01-25 10:37:30 -0500679 // We have now gotten all the information out of the adjustments and can forget them.
680 mAdjustments.clear();
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400681 }
682 }
683
Will Brockman2b6959e2020-01-22 09:59:50 -0500684 String getAdjustmentIssuer() {
685 return mAdjustmentIssuer;
686 }
687
Rohan Shah590e1b22018-04-10 23:48:47 -0400688 public void setIsAppImportanceLocked(boolean isAppImportanceLocked) {
689 mIsAppImportanceLocked = isAppImportanceLocked;
690 calculateUserSentiment();
691 }
692
Chris Wren333a61c2014-05-28 16:40:57 -0400693 public void setContactAffinity(float contactAffinity) {
694 mContactAffinity = contactAffinity;
695 }
696
697 public float getContactAffinity() {
698 return mContactAffinity;
699 }
700
John Spurlock1d881a12015-03-18 19:21:54 -0400701 public void setRecentlyIntrusive(boolean recentlyIntrusive) {
Chris Wren333a61c2014-05-28 16:40:57 -0400702 mRecentlyIntrusive = recentlyIntrusive;
Julia Reynolds309d1c82017-05-03 16:00:20 -0400703 if (recentlyIntrusive) {
704 mLastIntrusive = System.currentTimeMillis();
705 }
Chris Wren333a61c2014-05-28 16:40:57 -0400706 }
707
708 public boolean isRecentlyIntrusive() {
709 return mRecentlyIntrusive;
710 }
711
Julia Reynolds309d1c82017-05-03 16:00:20 -0400712 public long getLastIntrusive() {
713 return mLastIntrusive;
714 }
715
Chris Wren54bbef42014-07-09 18:37:56 -0400716 public void setPackagePriority(int packagePriority) {
John Spurlock6ac5f8d2014-07-18 11:27:54 -0400717 mPackagePriority = packagePriority;
Chris Wren54bbef42014-07-09 18:37:56 -0400718 }
719
720 public int getPackagePriority() {
721 return mPackagePriority;
722 }
723
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400724 public void setPackageVisibilityOverride(int packageVisibility) {
725 mPackageVisibility = packageVisibility;
726 }
727
728 public int getPackageVisibilityOverride() {
729 return mPackageVisibility;
730 }
731
Julia Reynoldsef37f282016-02-12 09:11:27 -0500732 private String getUserExplanation() {
733 if (mUserExplanation == null) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500734 mUserExplanation = mContext.getResources().getString(
735 com.android.internal.R.string.importance_from_user);
Chris Wrenbdf33762015-12-04 15:50:51 -0500736 }
Julia Reynoldsef37f282016-02-12 09:11:27 -0500737 return mUserExplanation;
Chris Wrenbdf33762015-12-04 15:50:51 -0500738 }
739
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400740 /**
741 * Sets the importance value the system thinks the record should have.
742 * e.g. bumping up foreground service notifications or people to people notifications.
743 */
744 public void setSystemImportance(int importance) {
745 mSystemImportance = importance;
Julia Reynolds27c0a962018-12-10 12:37:28 -0500746 // System importance is only changed in enqueue, so it's ok for us to calculate the
747 // importance directly instead of waiting for signal extractor.
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400748 calculateImportance();
Chris Wrenbdf33762015-12-04 15:50:51 -0500749 }
750
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400751 /**
752 * Sets the importance value the
753 * {@link android.service.notification.NotificationAssistantService} thinks the record should
754 * have.
755 */
756 public void setAssistantImportance(int importance) {
757 mAssistantImportance = importance;
Julia Reynolds27c0a962018-12-10 12:37:28 -0500758 // Unlike the system importance, the assistant importance can change on posted
759 // notifications, so don't calculateImportance() here, but wait for the signal extractors.
760 }
761
762 /**
763 * Returns the importance set by the assistant, or IMPORTANCE_UNSPECIFIED if the assistant
764 * hasn't set it.
765 */
766 public int getAssistantImportance() {
767 return mAssistantImportance;
Julia Reynolds5d25ee72015-11-20 15:38:20 -0500768 }
769
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400770 /**
Will Brockman934b8e32019-03-08 11:14:45 -0500771 * Recalculates the importance of the record after fields affecting importance have changed,
772 * and records an explanation.
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400773 */
774 protected void calculateImportance() {
775 mImportance = calculateInitialImportance();
Will Brockman934b8e32019-03-08 11:14:45 -0500776 mImportanceExplanationCode = mInitialImportanceExplanationCode;
777
778 // Consider Notification Assistant and system overrides to importance. If both, system wins.
Julia Reynolds27c0a962018-12-10 12:37:28 -0500779 if (!getChannel().hasUserSetImportance()
Julia Reynolds413ba842019-01-11 10:38:08 -0500780 && mAssistantImportance != IMPORTANCE_UNSPECIFIED
Julia Reynolds0c245002019-03-27 16:10:11 -0400781 && !getChannel().isImportanceLockedByOEM()
782 && !getChannel().isImportanceLockedByCriticalDeviceFunction()) {
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400783 mImportance = mAssistantImportance;
Will Brockman934b8e32019-03-08 11:14:45 -0500784 mImportanceExplanationCode = MetricsEvent.IMPORTANCE_EXPLANATION_ASST;
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400785 }
786 if (mSystemImportance != IMPORTANCE_UNSPECIFIED) {
787 mImportance = mSystemImportance;
Will Brockman934b8e32019-03-08 11:14:45 -0500788 mImportanceExplanationCode = MetricsEvent.IMPORTANCE_EXPLANATION_SYSTEM;
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400789 }
Chris Wrenbdf33762015-12-04 15:50:51 -0500790 }
791
792 public int getImportance() {
793 return mImportance;
794 }
795
Will Brockman2b6959e2020-01-22 09:59:50 -0500796 int getInitialImportance() {
797 return stats.naturalImportance;
798 }
799
Alex Mang730d9492019-12-03 18:06:09 -0800800 public float getRankingScore() {
801 return mRankingScore;
802 }
803
Will Brockman2b6959e2020-01-22 09:59:50 -0500804 int getImportanceExplanationCode() {
805 return mImportanceExplanationCode;
806 }
807
808 int getInitialImportanceExplanationCode() {
809 return mInitialImportanceExplanationCode;
810 }
811
Chris Wrenbdf33762015-12-04 15:50:51 -0500812 public CharSequence getImportanceExplanation() {
Will Brockman934b8e32019-03-08 11:14:45 -0500813 switch (mImportanceExplanationCode) {
814 case MetricsEvent.IMPORTANCE_EXPLANATION_UNKNOWN:
815 return null;
816 case MetricsEvent.IMPORTANCE_EXPLANATION_APP:
817 case MetricsEvent.IMPORTANCE_EXPLANATION_APP_PRE_CHANNELS:
818 return "app";
819 case MetricsEvent.IMPORTANCE_EXPLANATION_USER:
820 return "user";
821 case MetricsEvent.IMPORTANCE_EXPLANATION_ASST:
822 return "asst";
823 case MetricsEvent.IMPORTANCE_EXPLANATION_SYSTEM:
824 return "system";
825 }
826 return null;
Chris Wrenbdf33762015-12-04 15:50:51 -0500827 }
828
Chris Wren333a61c2014-05-28 16:40:57 -0400829 public boolean setIntercepted(boolean intercept) {
830 mIntercept = intercept;
831 return mIntercept;
832 }
833
Brad Stenning9a8b2c82018-08-03 14:14:26 -0700834 /**
835 * Set to affect global sort key.
836 *
837 * @param criticality used in a string based sort thus 0 is the most critical
838 */
839 public void setCriticality(int criticality) {
840 mCriticality = criticality;
841 }
842
843 public int getCriticality() {
844 return mCriticality;
845 }
846
Chris Wren333a61c2014-05-28 16:40:57 -0400847 public boolean isIntercepted() {
848 return mIntercept;
849 }
850
Julia Reynoldsd6730072019-01-04 12:52:52 -0500851 public boolean isNewEnoughForAlerting(long now) {
852 return getFreshnessMs(now) <= MAX_SOUND_DELAY_MS;
853 }
854
Beverly5a20a5e2018-03-06 15:02:44 -0500855 public void setHidden(boolean hidden) {
856 mHidden = hidden;
857 }
858
859 public boolean isHidden() {
860 return mHidden;
861 }
862
Julia Reynolds65031622020-02-27 09:11:25 -0500863 /**
864 * Override of all alerting information on the channel and notification. Used when notifications
865 * are reposted in response to direct user action and thus don't need to alert.
866 */
867 public void setPostSilently(boolean postSilently) {
868 mPostSilently = postSilently;
869 }
870
871 public boolean shouldPostSilently() {
872 return mPostSilently;
873 }
Beverly5a20a5e2018-03-06 15:02:44 -0500874
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500875 public void setSuppressedVisualEffects(int effects) {
876 mSuppressedVisualEffects = effects;
877 }
878
879 public int getSuppressedVisualEffects() {
880 return mSuppressedVisualEffects;
881 }
882
John Spurlock312d1d02014-07-08 10:24:57 -0400883 public boolean isCategory(String category) {
John Spurlockbfa5dc42014-07-28 23:30:45 -0400884 return Objects.equals(getNotification().category, category);
885 }
886
John Spurlockbfa5dc42014-07-28 23:30:45 -0400887 public boolean isAudioAttributesUsage(int usage) {
Julia Reynolds51eb78f82018-03-07 07:35:21 -0500888 return mAttributes != null && mAttributes.getUsage() == usage;
John Spurlock312d1d02014-07-08 10:24:57 -0400889 }
890
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200891 /**
892 * Returns the timestamp to use for time-based sorting in the ranker.
893 */
894 public long getRankingTimeMs() {
895 return mRankingTimeMs;
896 }
897
898 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400899 * @param now this current time in milliseconds.
900 * @returns the number of milliseconds since the most recent update, or the post time if none.
Chris Wren6650e572015-05-15 17:19:25 -0400901 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400902 public int getFreshnessMs(long now) {
903 return (int) (now - mUpdateTimeMs);
Chris Wren6650e572015-05-15 17:19:25 -0400904 }
905
906 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400907 * @param now this current time in milliseconds.
908 * @returns the number of milliseconds since the the first post, ignoring updates.
Chris Wren640e3872015-04-21 13:23:18 -0400909 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400910 public int getLifespanMs(long now) {
911 return (int) (now - mCreationTimeMs);
Chris Wren640e3872015-04-21 13:23:18 -0400912 }
913
914 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400915 * @param now this current time in milliseconds.
916 * @returns the number of milliseconds since the most recent visibility event, or 0 if never.
Chris Wren6650e572015-05-15 17:19:25 -0400917 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400918 public int getExposureMs(long now) {
919 return mVisibleSinceMs == 0 ? 0 : (int) (now - mVisibleSinceMs);
Chris Wren6650e572015-05-15 17:19:25 -0400920 }
921
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400922 public int getInterruptionMs(long now) {
923 return (int) (now - mInterruptionTimeMs);
924 }
925
Evan Lairdd04af512020-01-09 11:18:09 -0500926 public long getUpdateTimeMs() {
927 return mUpdateTimeMs;
928 }
929
Chris Wren6650e572015-05-15 17:19:25 -0400930 /**
931 * Set the visibility of the notification.
932 */
Will Brockmand3d49332020-02-10 19:43:03 -0500933 public void setVisibility(boolean visible, int rank, int count,
934 NotificationRecordLogger notificationRecordLogger) {
Chris Wren6650e572015-05-15 17:19:25 -0400935 final long now = System.currentTimeMillis();
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400936 mVisibleSinceMs = visible ? now : mVisibleSinceMs;
Chris Wren6650e572015-05-15 17:19:25 -0400937 stats.onVisibilityChanged(visible);
Chris Wren9eb5e102017-01-26 13:15:06 -0500938 MetricsLogger.action(getLogMaker(now)
939 .setCategory(MetricsEvent.NOTIFICATION_ITEM)
940 .setType(visible ? MetricsEvent.TYPE_OPEN : MetricsEvent.TYPE_CLOSE)
Dieter Hsud39f0d52018-04-14 02:08:30 +0800941 .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, rank)
942 .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, count));
Chris Wren9eb5e102017-01-26 13:15:06 -0500943 if (visible) {
Julia Reynolds503ed942017-10-04 16:04:56 -0400944 setSeen();
Chris Wren9eb5e102017-01-26 13:15:06 -0500945 MetricsLogger.histogram(mContext, "note_freshness", getFreshnessMs(now));
946 }
Chris Wren6650e572015-05-15 17:19:25 -0400947 EventLogTags.writeNotificationVisibility(getKey(), visible ? 1 : 0,
Chris Wren9eb5e102017-01-26 13:15:06 -0500948 getLifespanMs(now),
949 getFreshnessMs(now),
Chris Wrend1dbc922015-06-19 17:51:16 -0400950 0, // exposure time
951 rank);
Will Brockmand3d49332020-02-10 19:43:03 -0500952 notificationRecordLogger.logNotificationVisibility(this, visible);
Chris Wren6650e572015-05-15 17:19:25 -0400953 }
954
955 /**
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200956 * @param previousRankingTimeMs for updated notifications, {@link #getRankingTimeMs()}
957 * of the previous notification record, 0 otherwise
958 */
959 private long calculateRankingTimeMs(long previousRankingTimeMs) {
960 Notification n = getNotification();
961 // Take developer provided 'when', unless it's in the future.
Julia Reynolds24edc002020-01-29 16:35:32 -0500962 if (n.when != 0 && n.when <= getSbn().getPostTime()) {
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200963 return n.when;
964 }
965 // If we've ranked a previous instance with a timestamp, inherit it. This case is
966 // important in order to have ranking stability for updating notifications.
967 if (previousRankingTimeMs > 0) {
968 return previousRankingTimeMs;
969 }
Julia Reynolds24edc002020-01-29 16:35:32 -0500970 return getSbn().getPostTime();
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200971 }
Chris Wren1031c972014-07-23 13:11:45 +0000972
Christoph Studercd4adf82014-08-19 17:50:49 +0200973 public void setGlobalSortKey(String globalSortKey) {
974 mGlobalSortKey = globalSortKey;
Chris Wren1031c972014-07-23 13:11:45 +0000975 }
976
Christoph Studercd4adf82014-08-19 17:50:49 +0200977 public String getGlobalSortKey() {
978 return mGlobalSortKey;
Chris Wren1031c972014-07-23 13:11:45 +0000979 }
980
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700981 /** Check if any of the listeners have marked this notification as seen by the user. */
982 public boolean isSeen() {
Julia Reynolds503ed942017-10-04 16:04:56 -0400983 return mStats.hasSeen();
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700984 }
985
986 /** Mark the notification as seen by the user. */
987 public void setSeen() {
Julia Reynolds503ed942017-10-04 16:04:56 -0400988 mStats.setSeen();
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400989 if (mTextChanged) {
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400990 setInterruptive(true);
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400991 }
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700992 }
993
Chris Wren1031c972014-07-23 13:11:45 +0000994 public void setAuthoritativeRank(int authoritativeRank) {
995 mAuthoritativeRank = authoritativeRank;
996 }
997
998 public int getAuthoritativeRank() {
999 return mAuthoritativeRank;
1000 }
1001
1002 public String getGroupKey() {
Julia Reynolds24edc002020-01-29 16:35:32 -05001003 return getSbn().getGroupKey();
Chris Wren1031c972014-07-23 13:11:45 +00001004 }
Chris Wren47633422016-01-22 09:56:59 -05001005
Chris Wrenb3921792017-06-01 13:34:46 -04001006 public void setOverrideGroupKey(String overrideGroupKey) {
Julia Reynolds24edc002020-01-29 16:35:32 -05001007 getSbn().setOverrideGroupKey(overrideGroupKey);
Chris Wrenb3921792017-06-01 13:34:46 -04001008 }
1009
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001010 public NotificationChannel getChannel() {
Julia Reynolds924eed12017-01-19 09:52:07 -05001011 return mChannel;
Julia Reynolds22f02b32016-12-01 15:05:13 -05001012 }
1013
Rohan Shah590e1b22018-04-10 23:48:47 -04001014 /**
Julia Reynoldsefcdff42018-08-09 09:42:56 -04001015 * @see PreferencesHelper#getIsAppImportanceLocked(String, int)
Rohan Shah590e1b22018-04-10 23:48:47 -04001016 */
1017 public boolean getIsAppImportanceLocked() {
1018 return mIsAppImportanceLocked;
1019 }
1020
Julia Reynolds924eed12017-01-19 09:52:07 -05001021 protected void updateNotificationChannel(NotificationChannel channel) {
1022 if (channel != null) {
1023 mChannel = channel;
Julia Reynolds22f02b32016-12-01 15:05:13 -05001024 calculateImportance();
Julia Reynoldsc65656a2018-02-12 09:55:14 -05001025 calculateUserSentiment();
Julia Reynolds22f02b32016-12-01 15:05:13 -05001026 }
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001027 }
Julia Reynolds0c299d42016-11-15 14:37:04 -05001028
Julia Reynolds924eed12017-01-19 09:52:07 -05001029 public void setShowBadge(boolean showBadge) {
1030 mShowBadge = showBadge;
1031 }
1032
Julia Reynolds4509ce72019-01-31 13:12:43 -05001033 public boolean canBubble() {
1034 return mAllowBubble;
1035 }
1036
1037 public void setAllowBubble(boolean allow) {
1038 mAllowBubble = allow;
1039 }
1040
Julia Reynolds924eed12017-01-19 09:52:07 -05001041 public boolean canShowBadge() {
1042 return mShowBadge;
1043 }
1044
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05001045 public Light getLight() {
1046 return mLight;
1047 }
1048
Julia Reynolds0c299d42016-11-15 14:37:04 -05001049 public Uri getSound() {
1050 return mSound;
1051 }
1052
1053 public long[] getVibration() {
1054 return mVibration;
1055 }
1056
1057 public AudioAttributes getAudioAttributes() {
1058 return mAttributes;
1059 }
Julia Reynolds22f02b32016-12-01 15:05:13 -05001060
1061 public ArrayList<String> getPeopleOverride() {
1062 return mPeopleOverride;
1063 }
1064
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001065 public void setInterruptive(boolean interruptive) {
1066 mIsInterruptive = interruptive;
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -04001067 final long now = System.currentTimeMillis();
1068 mInterruptionTimeMs = interruptive ? now : mInterruptionTimeMs;
1069
1070 if (interruptive) {
1071 MetricsLogger.action(getLogMaker()
1072 .setCategory(MetricsEvent.NOTIFICATION_INTERRUPTION)
1073 .setType(MetricsEvent.TYPE_OPEN)
1074 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_INTERRUPTION_MILLIS,
1075 getInterruptionMs(now)));
1076 MetricsLogger.histogram(mContext, "note_interruptive", getInterruptionMs(now));
1077 }
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001078 }
1079
Gus Prevasa3226492018-10-23 11:10:09 -04001080 public void setAudiblyAlerted(boolean audiblyAlerted) {
Gus Prevas7306b902018-12-11 10:57:06 -05001081 mLastAudiblyAlertedMs = audiblyAlerted ? System.currentTimeMillis() : -1;
Gus Prevasa3226492018-10-23 11:10:09 -04001082 }
1083
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -04001084 public void setTextChanged(boolean textChanged) {
1085 mTextChanged = textChanged;
1086 }
1087
1088 public void setRecordedInterruption(boolean recorded) {
1089 mRecordedInterruption = recorded;
1090 }
1091
1092 public boolean hasRecordedInterruption() {
1093 return mRecordedInterruption;
1094 }
1095
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001096 public boolean isInterruptive() {
1097 return mIsInterruptive;
1098 }
1099
Gus Prevas7306b902018-12-11 10:57:06 -05001100 /** Returns the time the notification audibly alerted the user. */
1101 public long getLastAudiblyAlertedMs() {
1102 return mLastAudiblyAlertedMs;
Gus Prevasa3226492018-10-23 11:10:09 -04001103 }
1104
Julia Reynolds22f02b32016-12-01 15:05:13 -05001105 protected void setPeopleOverride(ArrayList<String> people) {
1106 mPeopleOverride = people;
1107 }
1108
1109 public ArrayList<SnoozeCriterion> getSnoozeCriteria() {
1110 return mSnoozeCriteria;
1111 }
1112
1113 protected void setSnoozeCriteria(ArrayList<SnoozeCriterion> snoozeCriteria) {
1114 mSnoozeCriteria = snoozeCriteria;
1115 }
Chris Wren9eb5e102017-01-26 13:15:06 -05001116
Julia Reynoldsc65656a2018-02-12 09:55:14 -05001117 private void calculateUserSentiment() {
Rohan Shah590e1b22018-04-10 23:48:47 -04001118 if ((getChannel().getUserLockedFields() & USER_LOCKED_IMPORTANCE) != 0
1119 || mIsAppImportanceLocked) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -05001120 mUserSentiment = USER_SENTIMENT_POSITIVE;
1121 }
1122 }
1123
Julia Reynolds503ed942017-10-04 16:04:56 -04001124 private void setUserSentiment(int userSentiment) {
1125 mUserSentiment = userSentiment;
1126 }
1127
1128 public int getUserSentiment() {
1129 return mUserSentiment;
1130 }
1131
1132 public NotificationStats getStats() {
1133 return mStats;
1134 }
1135
1136 public void recordExpanded() {
1137 mStats.setExpanded();
1138 }
1139
1140 public void recordDirectReplied() {
1141 mStats.setDirectReplied();
1142 }
1143
1144 public void recordDismissalSurface(@NotificationStats.DismissalSurface int surface) {
1145 mStats.setDismissalSurface(surface);
1146 }
1147
Julia Reynoldsfd4099d2018-08-21 11:06:06 -04001148 public void recordDismissalSentiment(@NotificationStats.DismissalSentiment int sentiment) {
1149 mStats.setDismissalSentiment(sentiment);
1150 }
1151
Julia Reynolds503ed942017-10-04 16:04:56 -04001152 public void recordSnoozed() {
1153 mStats.setSnoozed();
1154 }
1155
1156 public void recordViewedSettings() {
1157 mStats.setViewedSettings();
1158 }
1159
Kenny Guy23991102018-04-05 21:18:38 +01001160 public void setNumSmartRepliesAdded(int noReplies) {
1161 mNumberOfSmartRepliesAdded = noReplies;
1162 }
1163
1164 public int getNumSmartRepliesAdded() {
1165 return mNumberOfSmartRepliesAdded;
1166 }
1167
Gustav Senntond25a64d2018-12-07 10:58:39 +00001168 public void setNumSmartActionsAdded(int noActions) {
1169 mNumberOfSmartActionsAdded = noActions;
1170 }
1171
1172 public int getNumSmartActionsAdded() {
1173 return mNumberOfSmartActionsAdded;
1174 }
1175
1176 public void setSuggestionsGeneratedByAssistant(boolean generatedByAssistant) {
1177 mSuggestionsGeneratedByAssistant = generatedByAssistant;
1178 }
1179
1180 public boolean getSuggestionsGeneratedByAssistant() {
1181 return mSuggestionsGeneratedByAssistant;
1182 }
1183
Milo Sredkov13d88112019-02-01 12:23:24 +00001184 public boolean getEditChoicesBeforeSending() {
1185 return mEditChoicesBeforeSending;
1186 }
1187
1188 public void setEditChoicesBeforeSending(boolean editChoicesBeforeSending) {
1189 mEditChoicesBeforeSending = editChoicesBeforeSending;
1190 }
1191
Kenny Guy23991102018-04-05 21:18:38 +01001192 public boolean hasSeenSmartReplies() {
1193 return mHasSeenSmartReplies;
1194 }
1195
1196 public void setSeenSmartReplies(boolean hasSeenSmartReplies) {
1197 mHasSeenSmartReplies = hasSeenSmartReplies;
1198 }
1199
Gustav Sennton44dc5882018-12-13 14:38:50 +00001200 /**
1201 * Returns whether this notification has been visible and expanded at the same time.
1202 */
1203 public boolean hasBeenVisiblyExpanded() {
1204 return stats.hasBeenVisiblyExpanded();
1205 }
1206
Mady Mellor13f9bc82020-03-24 19:09:28 -07001207 /**
1208 * When the bubble state on a notif changes due to user action (e.g. dismiss a bubble) then
1209 * this value is set until an update or bubble change event due to user action (e.g. create
1210 * bubble from sysui)
1211 **/
1212 public boolean isFlagBubbleRemoved() {
1213 return mFlagBubbleRemoved;
1214 }
1215
1216 public void setFlagBubbleRemoved(boolean flagBubbleRemoved) {
1217 mFlagBubbleRemoved = flagBubbleRemoved;
1218 }
1219
Gustav Sennton1463d832018-11-06 16:12:48 +00001220 public void setSystemGeneratedSmartActions(
1221 ArrayList<Notification.Action> systemGeneratedSmartActions) {
1222 mSystemGeneratedSmartActions = systemGeneratedSmartActions;
Tony Mak628cb932018-06-19 18:30:41 +01001223 }
1224
Gustav Sennton1463d832018-11-06 16:12:48 +00001225 public ArrayList<Notification.Action> getSystemGeneratedSmartActions() {
1226 return mSystemGeneratedSmartActions;
Tony Mak628cb932018-06-19 18:30:41 +01001227 }
1228
Tony Makc9acf672018-07-20 13:58:24 +02001229 public void setSmartReplies(ArrayList<CharSequence> smartReplies) {
1230 mSmartReplies = smartReplies;
1231 }
1232
1233 public ArrayList<CharSequence> getSmartReplies() {
1234 return mSmartReplies;
1235 }
1236
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001237 /**
Julia Reynolds95334132018-12-19 11:15:35 -05001238 * Returns whether this notification was posted by a secondary app
1239 */
1240 public boolean isProxied() {
Julia Reynolds24edc002020-01-29 16:35:32 -05001241 return !Objects.equals(getSbn().getPackageName(), getSbn().getOpPkg());
Julia Reynolds95334132018-12-19 11:15:35 -05001242 }
1243
1244 /**
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001245 * @return all {@link Uri} that should have permission granted to whoever
1246 * will be rendering it. This list has already been vetted to only
1247 * include {@link Uri} that the enqueuing app can grant.
1248 */
1249 public @Nullable ArraySet<Uri> getGrantableUris() {
1250 return mGrantableUris;
1251 }
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001252
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001253 /**
1254 * Collect all {@link Uri} that should have permission granted to whoever
1255 * will be rendering it.
1256 */
Julia Reynolds218871e2018-06-13 10:45:21 -04001257 protected void calculateGrantableUris() {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001258 final Notification notification = getNotification();
Jeff Sharkey23b31182018-04-18 21:32:12 -06001259 notification.visitUris((uri) -> {
Julia Reynolds218871e2018-06-13 10:45:21 -04001260 visitGrantableUri(uri, false);
Jeff Sharkey23b31182018-04-18 21:32:12 -06001261 });
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001262
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001263 if (notification.getChannelId() != null) {
1264 NotificationChannel channel = getChannel();
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001265 if (channel != null) {
Julia Reynolds218871e2018-06-13 10:45:21 -04001266 visitGrantableUri(channel.getSound(), (channel.getUserLockedFields()
1267 & NotificationChannel.USER_LOCKED_SOUND) != 0);
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001268 }
1269 }
1270 }
1271
1272 /**
1273 * Note the presence of a {@link Uri} that should have permission granted to
1274 * whoever will be rendering it.
1275 * <p>
1276 * If the enqueuing app has the ability to grant access, it will be added to
1277 * {@link #mGrantableUris}. Otherwise, this will either log or throw
1278 * {@link SecurityException} depending on target SDK of enqueuing app.
1279 */
Julia Reynolds218871e2018-06-13 10:45:21 -04001280 private void visitGrantableUri(Uri uri, boolean userOverriddenUri) {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001281 if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return;
1282
1283 // We can't grant Uri permissions from system
Julia Reynolds24edc002020-01-29 16:35:32 -05001284 final int sourceUid = getSbn().getUid();
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001285 if (sourceUid == android.os.Process.SYSTEM_UID) return;
1286
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001287 final long ident = Binder.clearCallingIdentity();
1288 try {
1289 // This will throw SecurityException if caller can't grant
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07001290 mUgmInternal.checkGrantUriPermission(sourceUid, null,
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001291 ContentProvider.getUriWithoutUserId(uri),
1292 Intent.FLAG_GRANT_READ_URI_PERMISSION,
1293 ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid)));
1294
1295 if (mGrantableUris == null) {
1296 mGrantableUris = new ArraySet<>();
1297 }
1298 mGrantableUris.add(uri);
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001299 } catch (SecurityException e) {
Julia Reynolds218871e2018-06-13 10:45:21 -04001300 if (!userOverriddenUri) {
1301 if (mTargetSdkVersion >= Build.VERSION_CODES.P) {
1302 throw e;
1303 } else {
1304 Log.w(TAG, "Ignoring " + uri + " from " + sourceUid + ": " + e.getMessage());
1305 }
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001306 }
1307 } finally {
1308 Binder.restoreCallingIdentity(ident);
1309 }
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001310 }
1311
Chris Wren9eb5e102017-01-26 13:15:06 -05001312 public LogMaker getLogMaker(long now) {
Julia Reynolds24edc002020-01-29 16:35:32 -05001313 LogMaker lm = getSbn().getLogMaker()
Chris Wrenb3921792017-06-01 13:34:46 -04001314 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE, mImportance)
Chris Wren9eb5e102017-01-26 13:15:06 -05001315 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_CREATE_MILLIS, getLifespanMs(now))
1316 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_UPDATE_MILLIS, getFreshnessMs(now))
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -04001317 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_VISIBLE_MILLIS, getExposureMs(now))
1318 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_INTERRUPTION_MILLIS,
1319 getInterruptionMs(now));
Will Brockman934b8e32019-03-08 11:14:45 -05001320 // Record results of the calculateImportance() calculation if available.
1321 if (mImportanceExplanationCode != MetricsEvent.IMPORTANCE_EXPLANATION_UNKNOWN) {
1322 lm.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_EXPLANATION,
1323 mImportanceExplanationCode);
1324 // To avoid redundancy, we log the initial importance information only if it was
1325 // overridden.
1326 if (((mImportanceExplanationCode == MetricsEvent.IMPORTANCE_EXPLANATION_ASST)
1327 || (mImportanceExplanationCode == MetricsEvent.IMPORTANCE_EXPLANATION_SYSTEM))
1328 && (stats.naturalImportance != IMPORTANCE_UNSPECIFIED)) {
1329 // stats.naturalImportance is due to one of the 3 sources of initial importance.
1330 lm.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_INITIAL_EXPLANATION,
1331 mInitialImportanceExplanationCode);
1332 lm.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_INITIAL,
1333 stats.naturalImportance);
1334 }
Will Brockman605d61b2019-03-29 13:08:49 -04001335 }
1336 // Log Assistant override if present, whether or not importance calculation is complete.
1337 if (mAssistantImportance != IMPORTANCE_UNSPECIFIED) {
1338 lm.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_IMPORTANCE_ASST,
Will Brockman934b8e32019-03-08 11:14:45 -05001339 mAssistantImportance);
Will Brockman934b8e32019-03-08 11:14:45 -05001340 }
Jan Althaus367eb8c2019-05-23 11:31:42 +02001341 // Log the issuer of any adjustments that may have affected this notification. We only log
1342 // the hash here as NotificationItem events are frequent, and the number of NAS
1343 // implementations (and hence the chance of collisions) is low.
1344 if (mAdjustmentIssuer != null) {
1345 lm.addTaggedData(MetricsEvent.FIELD_NOTIFICATION_ASSISTANT_SERVICE_HASH,
1346 mAdjustmentIssuer.hashCode());
1347 }
Will Brockman934b8e32019-03-08 11:14:45 -05001348 return lm;
Chris Wren9eb5e102017-01-26 13:15:06 -05001349 }
1350
1351 public LogMaker getLogMaker() {
1352 return getLogMaker(System.currentTimeMillis());
1353 }
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05001354
Julia Reynolds3dfdde02018-10-08 09:17:56 -04001355 public LogMaker getItemLogMaker() {
1356 return getLogMaker().setCategory(MetricsEvent.NOTIFICATION_ITEM);
1357 }
1358
Chris Wren6cbf57b2019-12-12 15:33:21 -05001359 public boolean hasUndecoratedRemoteView() {
1360 Notification notification = getNotification();
1361 Class<? extends Notification.Style> style = notification.getNotificationStyle();
1362 boolean hasDecoratedStyle = style != null
1363 && (Notification.DecoratedCustomViewStyle.class.equals(style)
1364 || Notification.DecoratedMediaCustomViewStyle.class.equals(style));
1365 boolean hasCustomRemoteView = notification.contentView != null
1366 || notification.bigContentView != null
1367 || notification.headsUpContentView != null;
1368 return hasCustomRemoteView && !hasDecoratedStyle;
1369 }
1370
Julia Reynoldsd61bdf12020-02-25 12:25:07 -05001371 public void setShortcutInfo(ShortcutInfo shortcutInfo) {
1372 mShortcutInfo = shortcutInfo;
1373 }
1374
Julia Reynolds138111f2020-02-26 11:17:39 -05001375 public ShortcutInfo getShortcutInfo() {
1376 return mShortcutInfo;
1377 }
1378
Julia Reynoldsd61bdf12020-02-25 12:25:07 -05001379 /**
1380 * Whether this notification is a conversation notification.
1381 */
Danning Chen10326cf2020-01-16 13:29:13 -08001382 public boolean isConversation() {
1383 Notification notification = getNotification();
1384 if (mChannel.isDemoted()
1385 || !Notification.MessagingStyle.class.equals(notification.getNotificationStyle())) {
1386 return false;
1387 }
Julia Reynoldse24faa22020-04-02 12:44:47 -04001388 if (mShortcutInfo == null && Settings.Global.getInt(mContext.getContentResolver(),
1389 Settings.Global.REQUIRE_SHORTCUTS_FOR_CONVERSATIONS, 0) == 1) {
Danning Chen10326cf2020-01-16 13:29:13 -08001390 return false;
1391 }
1392 if (mIsNotConversationOverride) {
1393 return false;
1394 }
Danning Chen10326cf2020-01-16 13:29:13 -08001395 return true;
1396 }
1397
Julia Reynolds24edc002020-01-29 16:35:32 -05001398 StatusBarNotification getSbn() {
1399 return sbn;
1400 }
1401
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05001402 @VisibleForTesting
1403 static final class Light {
1404 public final int color;
1405 public final int onMs;
1406 public final int offMs;
1407
1408 public Light(int color, int onMs, int offMs) {
1409 this.color = color;
1410 this.onMs = onMs;
1411 this.offMs = offMs;
1412 }
1413
1414 @Override
1415 public boolean equals(Object o) {
1416 if (this == o) return true;
1417 if (o == null || getClass() != o.getClass()) return false;
1418
1419 Light light = (Light) o;
1420
1421 if (color != light.color) return false;
1422 if (onMs != light.onMs) return false;
1423 return offMs == light.offMs;
1424
1425 }
1426
1427 @Override
1428 public int hashCode() {
1429 int result = color;
1430 result = 31 * result + onMs;
1431 result = 31 * result + offMs;
1432 return result;
1433 }
1434
1435 @Override
1436 public String toString() {
1437 return "Light{" +
1438 "color=" + color +
1439 ", onMs=" + onMs +
1440 ", offMs=" + offMs +
1441 '}';
1442 }
1443 }
Chris Wren333a61c2014-05-28 16:40:57 -04001444}