blob: 02fc51f89e624e8f63505917e1471bcfe253b2e3 [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;
Chris Wren333a61c2014-05-28 16:40:57 -040037import android.content.pm.PackageManager.NameNotFoundException;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060038import android.content.pm.PackageManagerInternal;
Chris Wren333a61c2014-05-28 16:40:57 -040039import android.content.res.Resources;
40import android.graphics.Bitmap;
Dan Sandlerd63f9322015-05-06 15:18:49 -040041import android.graphics.drawable.Icon;
John Spurlockbfa5dc42014-07-28 23:30:45 -040042import android.media.AudioAttributes;
Julia Reynolds0c299d42016-11-15 14:37:04 -050043import android.media.AudioSystem;
Chris Wren9eb5e102017-01-26 13:15:06 -050044import android.metrics.LogMaker;
Julia Reynolds0c299d42016-11-15 14:37:04 -050045import android.net.Uri;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060046import android.os.Binder;
Julia Reynolds0c299d42016-11-15 14:37:04 -050047import android.os.Build;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040048import android.os.Bundle;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060049import android.os.IBinder;
Chris Wrenda4bd202014-09-04 15:53:52 -040050import android.os.UserHandle;
Julia Reynolds0c299d42016-11-15 14:37:04 -050051import android.provider.Settings;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040052import android.service.notification.Adjustment;
Julia Reynolds5d25ee72015-11-20 15:38:20 -050053import android.service.notification.NotificationListenerService;
Julia Reynoldsc9842c12017-02-07 12:46:41 -050054import android.service.notification.NotificationRecordProto;
Julia Reynolds503ed942017-10-04 16:04:56 -040055import android.service.notification.NotificationStats;
Julia Reynolds22f02b32016-12-01 15:05:13 -050056import android.service.notification.SnoozeCriterion;
Chris Wren333a61c2014-05-28 16:40:57 -040057import android.service.notification.StatusBarNotification;
Julia Reynolds4b82f6d2017-01-04 10:47:41 -050058import android.text.TextUtils;
Julia Reynoldse0d711f2017-09-01 08:50:47 -040059import android.util.ArraySet;
Julia Reynoldsf0f629f2016-02-25 09:34:04 -050060import android.util.Log;
Julia Reynolds2a128742016-11-28 14:29:25 -050061import android.util.TimeUtils;
Julia Reynoldsc9842c12017-02-07 12:46:41 -050062import android.util.proto.ProtoOutputStream;
Dan Sandler0a2308e2017-05-30 19:50:42 -040063import android.widget.RemoteViews;
John Spurlockbfa5dc42014-07-28 23:30:45 -040064
Chris Wren1031c972014-07-23 13:11:45 +000065import com.android.internal.annotations.VisibleForTesting;
Chris Wren9eb5e102017-01-26 13:15:06 -050066import com.android.internal.logging.MetricsLogger;
67import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Chris Wren6650e572015-05-15 17:19:25 -040068import com.android.server.EventLogTags;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060069import com.android.server.LocalServices;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070070import com.android.server.uri.UriGrantsManagerInternal;
Chris Wren333a61c2014-05-28 16:40:57 -040071
72import java.io.PrintWriter;
73import java.lang.reflect.Array;
Julia Reynolds22f02b32016-12-01 15:05:13 -050074import java.util.ArrayList;
Chris Wren333a61c2014-05-28 16:40:57 -040075import java.util.Arrays;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040076import java.util.List;
John Spurlock312d1d02014-07-08 10:24:57 -040077import java.util.Objects;
Chris Wren333a61c2014-05-28 16:40:57 -040078
79/**
80 * Holds data about notifications that should not be shared with the
81 * {@link android.service.notification.NotificationListenerService}s.
82 *
83 * <p>These objects should not be mutated unless the code is synchronized
Julia Reynolds88860ce2017-06-01 16:55:49 -040084 * on {@link NotificationManagerService#mNotificationLock}, and any
Chris Wren333a61c2014-05-28 16:40:57 -040085 * modification should be followed by a sorting of that list.</p>
86 *
87 * <p>Is sortable by {@link NotificationComparator}.</p>
88 *
89 * {@hide}
90 */
91public final class NotificationRecord {
Julia Reynoldsf0f629f2016-02-25 09:34:04 -050092 static final String TAG = "NotificationRecord";
93 static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
Julia Reynoldsd6730072019-01-04 12:52:52 -050094 // the period after which a notification is updated where it can make sound
95 private static final int MAX_SOUND_DELAY_MS = 2000;
Chris Wren333a61c2014-05-28 16:40:57 -040096 final StatusBarNotification sbn;
Julia Reynolds218871e2018-06-13 10:45:21 -040097 IActivityManager mAm;
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -070098 UriGrantsManagerInternal mUgmInternal;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -060099 final int mTargetSdkVersion;
Christoph Studer365e4c32014-09-18 20:35:36 +0200100 final int mOriginalFlags;
Chris Wrenbdf33762015-12-04 15:50:51 -0500101 private final Context mContext;
Christoph Studer365e4c32014-09-18 20:35:36 +0200102
Chris Wren333a61c2014-05-28 16:40:57 -0400103 NotificationUsageStats.SingleNotificationStats stats;
104 boolean isCanceled;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600105 IBinder permissionOwner;
Chris Wren333a61c2014-05-28 16:40:57 -0400106
107 // These members are used by NotificationSignalExtractors
108 // to communicate with the ranking module.
109 private float mContactAffinity;
110 private boolean mRecentlyIntrusive;
Julia Reynolds309d1c82017-05-03 16:00:20 -0400111 private long mLastIntrusive;
Chris Wren333a61c2014-05-28 16:40:57 -0400112
113 // is this notification currently being intercepted by Zen Mode?
114 private boolean mIntercept;
Chris Wren333a61c2014-05-28 16:40:57 -0400115
Beverly5a20a5e2018-03-06 15:02:44 -0500116 // is this notification hidden since the app pkg is suspended?
117 private boolean mHidden;
118
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200119 // The timestamp used for ranking.
120 private long mRankingTimeMs;
121
Chris Wren640e3872015-04-21 13:23:18 -0400122 // The first post time, stable across updates.
123 private long mCreationTimeMs;
124
Chris Wren6650e572015-05-15 17:19:25 -0400125 // The most recent visibility event.
126 private long mVisibleSinceMs;
127
128 // The most recent update time, or the creation time if no updates.
Julia Reynoldsd6730072019-01-04 12:52:52 -0500129 @VisibleForTesting
130 final long mUpdateTimeMs;
Chris Wren6650e572015-05-15 17:19:25 -0400131
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400132 // The most recent interruption time, or the creation time if no updates. Differs from the
133 // above value because updates are filtered based on whether they actually interrupted the
134 // user
135 private long mInterruptionTimeMs;
136
Gus Prevas7306b902018-12-11 10:57:06 -0500137 // The most recent time the notification made noise or buzzed the device, or -1 if it did not.
138 private long mLastAudiblyAlertedMs;
139
Chris Wrena3446562014-06-03 18:11:47 -0400140 // Is this record an update of an old record?
141 public boolean isUpdate;
Chris Wren54bbef42014-07-09 18:37:56 -0400142 private int mPackagePriority;
Chris Wrena3446562014-06-03 18:11:47 -0400143
Chris Wren1031c972014-07-23 13:11:45 +0000144 private int mAuthoritativeRank;
Christoph Studercd4adf82014-08-19 17:50:49 +0200145 private String mGlobalSortKey;
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400146 private int mPackageVisibility;
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400147 private int mSystemImportance = IMPORTANCE_UNSPECIFIED;
148 private int mAssistantImportance = IMPORTANCE_UNSPECIFIED;
Chris Wren47633422016-01-22 09:56:59 -0500149 private int mImportance = IMPORTANCE_UNSPECIFIED;
Brad Stenning9a8b2c82018-08-03 14:14:26 -0700150 // Field used in global sort key to bypass normal notifications
151 private int mCriticality = CriticalNotificationExtractor.NORMAL;
Chris Wrenbdf33762015-12-04 15:50:51 -0500152 private CharSequence mImportanceExplanation = null;
Chris Wren1031c972014-07-23 13:11:45 +0000153
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500154 private int mSuppressedVisualEffects = 0;
Julia Reynoldsef37f282016-02-12 09:11:27 -0500155 private String mUserExplanation;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000156 private boolean mPreChannelsNotification = true;
Julia Reynolds0c299d42016-11-15 14:37:04 -0500157 private Uri mSound;
158 private long[] mVibration;
159 private AudioAttributes mAttributes;
Julia Reynolds924eed12017-01-19 09:52:07 -0500160 private NotificationChannel mChannel;
Julia Reynolds22f02b32016-12-01 15:05:13 -0500161 private ArrayList<String> mPeopleOverride;
162 private ArrayList<SnoozeCriterion> mSnoozeCriteria;
Julia Reynolds924eed12017-01-19 09:52:07 -0500163 private boolean mShowBadge;
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500164 private Light mLight;
Gustav Sennton1463d832018-11-06 16:12:48 +0000165 /**
166 * This list contains system generated smart actions from NAS, app-generated smart actions are
Gustav Sennton005d7a02019-01-04 13:41:32 +0000167 * stored in Notification.actions with isContextual() set to true.
Gustav Sennton1463d832018-11-06 16:12:48 +0000168 */
169 private ArrayList<Notification.Action> mSystemGeneratedSmartActions;
Tony Makc9acf672018-07-20 13:58:24 +0200170 private ArrayList<CharSequence> mSmartReplies;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500171
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400172 private final List<Adjustment> mAdjustments;
Julia Reynolds503ed942017-10-04 16:04:56 -0400173 private final NotificationStats mStats;
174 private int mUserSentiment;
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500175 private boolean mIsInterruptive;
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400176 private boolean mTextChanged;
177 private boolean mRecordedInterruption;
Kenny Guy23991102018-04-05 21:18:38 +0100178 private int mNumberOfSmartRepliesAdded;
Gustav Senntond25a64d2018-12-07 10:58:39 +0000179 private int mNumberOfSmartActionsAdded;
180 private boolean mSuggestionsGeneratedByAssistant;
Kenny Guy23991102018-04-05 21:18:38 +0100181 private boolean mHasSeenSmartReplies;
Rohan Shah590e1b22018-04-10 23:48:47 -0400182 /**
183 * Whether this notification (and its channels) should be considered user locked. Used in
184 * conjunction with user sentiment calculation.
185 */
186 private boolean mIsAppImportanceLocked;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600187 private ArraySet<Uri> mGrantableUris;
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400188
Julia Reynolds924eed12017-01-19 09:52:07 -0500189 public NotificationRecord(Context context, StatusBarNotification sbn,
Rohan Shah590e1b22018-04-10 23:48:47 -0400190 NotificationChannel channel) {
Chris Wren333a61c2014-05-28 16:40:57 -0400191 this.sbn = sbn;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600192 mTargetSdkVersion = LocalServices.getService(PackageManagerInternal.class)
193 .getPackageTargetSdkVersion(sbn.getPackageName());
Julia Reynolds218871e2018-06-13 10:45:21 -0400194 mAm = ActivityManager.getService();
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700195 mUgmInternal = LocalServices.getService(UriGrantsManagerInternal.class);
Christoph Studer365e4c32014-09-18 20:35:36 +0200196 mOriginalFlags = sbn.getNotification().flags;
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200197 mRankingTimeMs = calculateRankingTimeMs(0L);
Chris Wren640e3872015-04-21 13:23:18 -0400198 mCreationTimeMs = sbn.getPostTime();
Chris Wren6650e572015-05-15 17:19:25 -0400199 mUpdateTimeMs = mCreationTimeMs;
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400200 mInterruptionTimeMs = mCreationTimeMs;
Chris Wrenbdf33762015-12-04 15:50:51 -0500201 mContext = context;
Chris Wrencdee8cd2016-01-25 17:10:30 -0500202 stats = new NotificationUsageStats.SingleNotificationStats();
Julia Reynolds924eed12017-01-19 09:52:07 -0500203 mChannel = channel;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000204 mPreChannelsNotification = isPreChannelsNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500205 mSound = calculateSound();
206 mVibration = calculateVibration();
207 mAttributes = calculateAttributes();
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400208 mImportance = calculateInitialImportance();
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500209 mLight = calculateLights();
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400210 mAdjustments = new ArrayList<>();
Julia Reynolds503ed942017-10-04 16:04:56 -0400211 mStats = new NotificationStats();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500212 calculateUserSentiment();
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600213 calculateGrantableUris();
Chris Wrenbdf33762015-12-04 15:50:51 -0500214 }
215
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000216 private boolean isPreChannelsNotification() {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600217 if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(getChannel().getId())) {
218 if (mTargetSdkVersion < Build.VERSION_CODES.O) {
219 return true;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000220 }
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000221 }
222 return false;
223 }
224
Julia Reynolds0c299d42016-11-15 14:37:04 -0500225 private Uri calculateSound() {
Chris Wrenbdf33762015-12-04 15:50:51 -0500226 final Notification n = sbn.getNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500227
Tony Mantlerab55f0f2017-06-16 10:50:00 -0700228 // No notification sounds on tv
229 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
230 return null;
231 }
232
Julia Reynolds924eed12017-01-19 09:52:07 -0500233 Uri sound = mChannel.getSound();
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000234 if (mPreChannelsNotification && (getChannel().getUserLockedFields()
Julia Reynolds0c299d42016-11-15 14:37:04 -0500235 & NotificationChannel.USER_LOCKED_SOUND) == 0) {
236
237 final boolean useDefaultSound = (n.defaults & Notification.DEFAULT_SOUND) != 0;
238 if (useDefaultSound) {
239 sound = Settings.System.DEFAULT_NOTIFICATION_URI;
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400240 } else {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500241 sound = n.sound;
242 }
243 }
244 return sound;
245 }
246
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500247 private Light calculateLights() {
248 int defaultLightColor = mContext.getResources().getColor(
249 com.android.internal.R.color.config_defaultNotificationColor);
250 int defaultLightOn = mContext.getResources().getInteger(
251 com.android.internal.R.integer.config_defaultNotificationLedOn);
252 int defaultLightOff = mContext.getResources().getInteger(
253 com.android.internal.R.integer.config_defaultNotificationLedOff);
254
Julia Reynolds529e3322017-02-06 08:33:01 -0500255 int channelLightColor = getChannel().getLightColor() != 0 ? getChannel().getLightColor()
256 : defaultLightColor;
257 Light light = getChannel().shouldShowLights() ? new Light(channelLightColor,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500258 defaultLightOn, defaultLightOff) : null;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000259 if (mPreChannelsNotification
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500260 && (getChannel().getUserLockedFields()
261 & NotificationChannel.USER_LOCKED_LIGHTS) == 0) {
262 final Notification notification = sbn.getNotification();
263 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
264 light = new Light(notification.ledARGB, notification.ledOnMS,
265 notification.ledOffMS);
266 if ((notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
267 light = new Light(defaultLightColor, defaultLightOn,
268 defaultLightOff);
269 }
270 } else {
271 light = null;
272 }
273 }
274 return light;
275 }
276
Julia Reynolds0c299d42016-11-15 14:37:04 -0500277 private long[] calculateVibration() {
278 long[] vibration;
279 final long[] defaultVibration = NotificationManagerService.getLongArray(
280 mContext.getResources(),
281 com.android.internal.R.array.config_defaultNotificationVibePattern,
282 NotificationManagerService.VIBRATE_PATTERN_MAXLEN,
283 NotificationManagerService.DEFAULT_VIBRATE_PATTERN);
284 if (getChannel().shouldVibrate()) {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500285 vibration = getChannel().getVibrationPattern() == null
286 ? defaultVibration : getChannel().getVibrationPattern();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500287 } else {
288 vibration = null;
289 }
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000290 if (mPreChannelsNotification
Julia Reynolds0c299d42016-11-15 14:37:04 -0500291 && (getChannel().getUserLockedFields()
292 & NotificationChannel.USER_LOCKED_VIBRATION) == 0) {
293 final Notification notification = sbn.getNotification();
294 final boolean useDefaultVibrate =
295 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
296 if (useDefaultVibrate) {
297 vibration = defaultVibration;
298 } else {
299 vibration = notification.vibrate;
300 }
301 }
302 return vibration;
303 }
304
305 private AudioAttributes calculateAttributes() {
306 final Notification n = sbn.getNotification();
Julia Reynolds619a69f2017-01-27 15:11:38 -0500307 AudioAttributes attributes = getChannel().getAudioAttributes();
308 if (attributes == null) {
309 attributes = Notification.AUDIO_ATTRIBUTES_DEFAULT;
310 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500311
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000312 if (mPreChannelsNotification
Julia Reynolds619a69f2017-01-27 15:11:38 -0500313 && (getChannel().getUserLockedFields()
314 & NotificationChannel.USER_LOCKED_SOUND) == 0) {
315 if (n.audioAttributes != null) {
316 // prefer audio attributes to stream type
317 attributes = n.audioAttributes;
318 } else if (n.audioStreamType >= 0
319 && n.audioStreamType < AudioSystem.getNumStreamTypes()) {
320 // the stream type is valid, use it
321 attributes = new AudioAttributes.Builder()
322 .setInternalLegacyStreamType(n.audioStreamType)
323 .build();
324 } else if (n.audioStreamType != AudioSystem.STREAM_DEFAULT) {
325 Log.w(TAG, String.format("Invalid stream type: %d", n.audioStreamType));
326 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500327 }
328 return attributes;
329 }
330
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400331 private int calculateInitialImportance() {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500332 final Notification n = sbn.getNotification();
333 int importance = getChannel().getImportance();
334 int requestedImportance = IMPORTANCE_DEFAULT;
Chris Wrenbdf33762015-12-04 15:50:51 -0500335
336 // Migrate notification flags to scores
337 if (0 != (n.flags & Notification.FLAG_HIGH_PRIORITY)) {
338 n.priority = Notification.PRIORITY_MAX;
339 }
340
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500341 n.priority = NotificationManagerService.clamp(n.priority, Notification.PRIORITY_MIN,
342 Notification.PRIORITY_MAX);
Chris Wrenbdf33762015-12-04 15:50:51 -0500343 switch (n.priority) {
344 case Notification.PRIORITY_MIN:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500345 requestedImportance = IMPORTANCE_MIN;
Julia Reynoldsf0f629f2016-02-25 09:34:04 -0500346 break;
Chris Wrenbdf33762015-12-04 15:50:51 -0500347 case Notification.PRIORITY_LOW:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500348 requestedImportance = IMPORTANCE_LOW;
Chris Wrenbdf33762015-12-04 15:50:51 -0500349 break;
350 case Notification.PRIORITY_DEFAULT:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500351 requestedImportance = IMPORTANCE_DEFAULT;
Chris Wrenbdf33762015-12-04 15:50:51 -0500352 break;
353 case Notification.PRIORITY_HIGH:
Chris Wrenbdf33762015-12-04 15:50:51 -0500354 case Notification.PRIORITY_MAX:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500355 requestedImportance = IMPORTANCE_HIGH;
Chris Wrenbdf33762015-12-04 15:50:51 -0500356 break;
357 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500358 stats.requestedImportance = requestedImportance;
359 stats.isNoisy = mSound != null || mVibration != null;
Chris Wrenbdf33762015-12-04 15:50:51 -0500360
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000361 if (mPreChannelsNotification
Julia Reynoldsa917a112017-03-21 11:09:14 -0400362 && (importance == IMPORTANCE_UNSPECIFIED
363 || (getChannel().getUserLockedFields()
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500364 & USER_LOCKED_IMPORTANCE) == 0)) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500365 if (!stats.isNoisy && requestedImportance > IMPORTANCE_LOW) {
366 requestedImportance = IMPORTANCE_LOW;
Julia Reynolds83fa1072016-02-17 09:10:19 -0500367 }
Julia Reynolds83fa1072016-02-17 09:10:19 -0500368
Julia Reynolds0c299d42016-11-15 14:37:04 -0500369 if (stats.isNoisy) {
370 if (requestedImportance < IMPORTANCE_DEFAULT) {
371 requestedImportance = IMPORTANCE_DEFAULT;
372 }
373 }
374
375 if (n.fullScreenIntent != null) {
376 requestedImportance = IMPORTANCE_HIGH;
377 }
378 importance = requestedImportance;
Chris Wrenbdf33762015-12-04 15:50:51 -0500379 }
380
Chris Wrencdee8cd2016-01-25 17:10:30 -0500381 stats.naturalImportance = importance;
Chris Wrenbdf33762015-12-04 15:50:51 -0500382 return importance;
Chris Wren333a61c2014-05-28 16:40:57 -0400383 }
384
385 // copy any notes that the ranking system may have made before the update
386 public void copyRankingInformation(NotificationRecord previous) {
387 mContactAffinity = previous.mContactAffinity;
388 mRecentlyIntrusive = previous.mRecentlyIntrusive;
Chris Wren54bbef42014-07-09 18:37:56 -0400389 mPackagePriority = previous.mPackagePriority;
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400390 mPackageVisibility = previous.mPackageVisibility;
Chris Wren333a61c2014-05-28 16:40:57 -0400391 mIntercept = previous.mIntercept;
Beverly5a20a5e2018-03-06 15:02:44 -0500392 mHidden = previous.mHidden;
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200393 mRankingTimeMs = calculateRankingTimeMs(previous.getRankingTimeMs());
Chris Wren640e3872015-04-21 13:23:18 -0400394 mCreationTimeMs = previous.mCreationTimeMs;
Chris Wren6650e572015-05-15 17:19:25 -0400395 mVisibleSinceMs = previous.mVisibleSinceMs;
Selim Cinek5b03ce92016-05-18 15:16:58 -0700396 if (previous.sbn.getOverrideGroupKey() != null && !sbn.isAppGroup()) {
Chris Wren8a1638f2016-05-02 16:19:14 -0400397 sbn.setOverrideGroupKey(previous.sbn.getOverrideGroupKey());
398 }
Chris Wren1f602dc2016-04-11 10:33:46 -0400399 // Don't copy importance information or mGlobalSortKey, recompute them.
Chris Wren333a61c2014-05-28 16:40:57 -0400400 }
401
402 public Notification getNotification() { return sbn.getNotification(); }
403 public int getFlags() { return sbn.getNotification().flags; }
Chris Wrenda4bd202014-09-04 15:53:52 -0400404 public UserHandle getUser() { return sbn.getUser(); }
Chris Wren333a61c2014-05-28 16:40:57 -0400405 public String getKey() { return sbn.getKey(); }
Chris Wrenda4bd202014-09-04 15:53:52 -0400406 /** @deprecated Use {@link #getUser()} instead. */
407 public int getUserId() { return sbn.getUserId(); }
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600408 public int getUid() { return sbn.getUid(); }
Chris Wren333a61c2014-05-28 16:40:57 -0400409
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800410 void dump(ProtoOutputStream proto, long fieldId, boolean redact, int state) {
411 final long token = proto.start(fieldId);
412
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500413 proto.write(NotificationRecordProto.KEY, sbn.getKey());
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800414 proto.write(NotificationRecordProto.STATE, state);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500415 if (getChannel() != null) {
416 proto.write(NotificationRecordProto.CHANNEL_ID, getChannel().getId());
417 }
418 proto.write(NotificationRecordProto.CAN_SHOW_LIGHT, getLight() != null);
419 proto.write(NotificationRecordProto.CAN_VIBRATE, getVibration() != null);
420 proto.write(NotificationRecordProto.FLAGS, sbn.getNotification().flags);
421 proto.write(NotificationRecordProto.GROUP_KEY, getGroupKey());
422 proto.write(NotificationRecordProto.IMPORTANCE, getImportance());
423 if (getSound() != null) {
424 proto.write(NotificationRecordProto.SOUND, getSound().toString());
425 }
426 if (getAudioAttributes() != null) {
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800427 getAudioAttributes().writeToProto(proto, NotificationRecordProto.AUDIO_ATTRIBUTES);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500428 }
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800429
430 proto.end(token);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500431 }
432
Dan Sandler0a2308e2017-05-30 19:50:42 -0400433 String formatRemoteViews(RemoteViews rv) {
434 if (rv == null) return "null";
435 return String.format("%s/0x%08x (%d bytes): %s",
436 rv.getPackage(), rv.getLayoutId(), rv.estimateMemoryUsage(), rv.toString());
437 }
438
Dan Sandlera1770312015-07-10 13:59:29 -0400439 void dump(PrintWriter pw, String prefix, Context baseContext, boolean redact) {
Chris Wren333a61c2014-05-28 16:40:57 -0400440 final Notification notification = sbn.getNotification();
Dan Sandlerd63f9322015-05-06 15:18:49 -0400441 final Icon icon = notification.getSmallIcon();
442 String iconStr = String.valueOf(icon);
443 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
444 iconStr += " / " + idDebugString(baseContext, icon.getResPackage(), icon.getResId());
445 }
Chris Wren333a61c2014-05-28 16:40:57 -0400446 pw.println(prefix + this);
Dan Sandler0a2308e2017-05-30 19:50:42 -0400447 prefix = prefix + " ";
Julia Reynoldsa917a112017-03-21 11:09:14 -0400448 pw.println(prefix + "uid=" + sbn.getUid() + " userId=" + sbn.getUserId());
449 pw.println(prefix + "icon=" + iconStr);
Julia Reynolds4db59552017-06-30 13:34:01 -0400450 pw.println(prefix + "flags=0x" + Integer.toHexString(notification.flags));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400451 pw.println(prefix + "pri=" + notification.priority);
452 pw.println(prefix + "key=" + sbn.getKey());
Julia Reynolds503ed942017-10-04 16:04:56 -0400453 pw.println(prefix + "seen=" + mStats.hasSeen());
Julia Reynoldsa917a112017-03-21 11:09:14 -0400454 pw.println(prefix + "groupKey=" + getGroupKey());
455 pw.println(prefix + "fullscreenIntent=" + notification.fullScreenIntent);
456 pw.println(prefix + "contentIntent=" + notification.contentIntent);
457 pw.println(prefix + "deleteIntent=" + notification.deleteIntent);
Aaron Heuckrothb4d6aa72018-07-02 14:58:33 -0400458 pw.println(prefix + "number=" + notification.number);
459 pw.println(prefix + "groupAlertBehavior=" + notification.getGroupAlertBehavior());
Dan Sandler0b4ceb32017-03-29 14:13:55 -0400460
461 pw.print(prefix + "tickerText=");
462 if (!TextUtils.isEmpty(notification.tickerText)) {
463 final String ticker = notification.tickerText.toString();
464 if (redact) {
465 // if the string is long enough, we allow ourselves a few bytes for debugging
466 pw.print(ticker.length() > 16 ? ticker.substring(0,8) : "");
467 pw.println("...");
468 } else {
469 pw.println(ticker);
470 }
471 } else {
472 pw.println("null");
473 }
Dan Sandler0a2308e2017-05-30 19:50:42 -0400474 pw.println(prefix + "contentView=" + formatRemoteViews(notification.contentView));
475 pw.println(prefix + "bigContentView=" + formatRemoteViews(notification.bigContentView));
476 pw.println(prefix + "headsUpContentView="
477 + formatRemoteViews(notification.headsUpContentView));
478 pw.print(prefix + String.format("color=0x%08x", notification.color));
Julia Reynoldsbad42972017-04-25 13:52:49 -0400479 pw.println(prefix + "timeout="
480 + TimeUtils.formatForLogging(notification.getTimeoutAfter()));
Chris Wren333a61c2014-05-28 16:40:57 -0400481 if (notification.actions != null && notification.actions.length > 0) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400482 pw.println(prefix + "actions={");
Chris Wren333a61c2014-05-28 16:40:57 -0400483 final int N = notification.actions.length;
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500484 for (int i = 0; i < N; i++) {
Chris Wren333a61c2014-05-28 16:40:57 -0400485 final Notification.Action action = notification.actions[i];
Chris Wren1ac52a92016-02-24 14:54:52 -0500486 if (action != null) {
487 pw.println(String.format("%s [%d] \"%s\" -> %s",
488 prefix,
489 i,
490 action.title,
491 action.actionIntent == null ? "null" : action.actionIntent.toString()
492 ));
493 }
Chris Wren333a61c2014-05-28 16:40:57 -0400494 }
495 pw.println(prefix + " }");
496 }
497 if (notification.extras != null && notification.extras.size() > 0) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400498 pw.println(prefix + "extras={");
Chris Wren333a61c2014-05-28 16:40:57 -0400499 for (String key : notification.extras.keySet()) {
500 pw.print(prefix + " " + key + "=");
501 Object val = notification.extras.get(key);
502 if (val == null) {
503 pw.println("null");
504 } else {
505 pw.print(val.getClass().getSimpleName());
Dan Sandlera1770312015-07-10 13:59:29 -0400506 if (redact && (val instanceof CharSequence || val instanceof String)) {
Chris Wren333a61c2014-05-28 16:40:57 -0400507 // redact contents from bugreports
508 } else if (val instanceof Bitmap) {
509 pw.print(String.format(" (%dx%d)",
510 ((Bitmap) val).getWidth(),
511 ((Bitmap) val).getHeight()));
512 } else if (val.getClass().isArray()) {
513 final int N = Array.getLength(val);
Dan Sandlera1770312015-07-10 13:59:29 -0400514 pw.print(" (" + N + ")");
515 if (!redact) {
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500516 for (int j = 0; j < N; j++) {
Dan Sandlera1770312015-07-10 13:59:29 -0400517 pw.println();
518 pw.print(String.format("%s [%d] %s",
519 prefix, j, String.valueOf(Array.get(val, j))));
520 }
521 }
Chris Wren333a61c2014-05-28 16:40:57 -0400522 } else {
523 pw.print(" (" + String.valueOf(val) + ")");
524 }
525 pw.println();
526 }
527 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400528 pw.println(prefix + "}");
Chris Wren333a61c2014-05-28 16:40:57 -0400529 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400530 pw.println(prefix + "stats=" + stats.toString());
531 pw.println(prefix + "mContactAffinity=" + mContactAffinity);
532 pw.println(prefix + "mRecentlyIntrusive=" + mRecentlyIntrusive);
533 pw.println(prefix + "mPackagePriority=" + mPackagePriority);
534 pw.println(prefix + "mPackageVisibility=" + mPackageVisibility);
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400535 pw.println(prefix + "mSystemImportance="
536 + NotificationListenerService.Ranking.importanceToString(mSystemImportance));
537 pw.println(prefix + "mAsstImportance="
538 + NotificationListenerService.Ranking.importanceToString(mAssistantImportance));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400539 pw.println(prefix + "mImportance="
Chris Wrenbdf33762015-12-04 15:50:51 -0500540 + NotificationListenerService.Ranking.importanceToString(mImportance));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400541 pw.println(prefix + "mImportanceExplanation=" + mImportanceExplanation);
Rohan Shah590e1b22018-04-10 23:48:47 -0400542 pw.println(prefix + "mIsAppImportanceLocked=" + mIsAppImportanceLocked);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400543 pw.println(prefix + "mIntercept=" + mIntercept);
Beverly5a20a5e2018-03-06 15:02:44 -0500544 pw.println(prefix + "mHidden==" + mHidden);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400545 pw.println(prefix + "mGlobalSortKey=" + mGlobalSortKey);
546 pw.println(prefix + "mRankingTimeMs=" + mRankingTimeMs);
547 pw.println(prefix + "mCreationTimeMs=" + mCreationTimeMs);
548 pw.println(prefix + "mVisibleSinceMs=" + mVisibleSinceMs);
549 pw.println(prefix + "mUpdateTimeMs=" + mUpdateTimeMs);
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400550 pw.println(prefix + "mInterruptionTimeMs=" + mInterruptionTimeMs);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400551 pw.println(prefix + "mSuppressedVisualEffects= " + mSuppressedVisualEffects);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000552 if (mPreChannelsNotification) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400553 pw.println(prefix + String.format("defaults=0x%08x flags=0x%08x",
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500554 notification.defaults, notification.flags));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400555 pw.println(prefix + "n.sound=" + notification.sound);
556 pw.println(prefix + "n.audioStreamType=" + notification.audioStreamType);
557 pw.println(prefix + "n.audioAttributes=" + notification.audioAttributes);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500558 pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d",
559 notification.ledARGB, notification.ledOnMS, notification.ledOffMS));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400560 pw.println(prefix + "vibrate=" + Arrays.toString(notification.vibrate));
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500561 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400562 pw.println(prefix + "mSound= " + mSound);
563 pw.println(prefix + "mVibration= " + mVibration);
564 pw.println(prefix + "mAttributes= " + mAttributes);
565 pw.println(prefix + "mLight= " + mLight);
566 pw.println(prefix + "mShowBadge=" + mShowBadge);
Julia Reynolds4db59552017-06-30 13:34:01 -0400567 pw.println(prefix + "mColorized=" + notification.isColorized());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500568 pw.println(prefix + "mIsInterruptive=" + mIsInterruptive);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400569 pw.println(prefix + "effectiveNotificationChannel=" + getChannel());
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500570 if (getPeopleOverride() != null) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400571 pw.println(prefix + "overridePeople= " + TextUtils.join(",", getPeopleOverride()));
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500572 }
573 if (getSnoozeCriteria() != null) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400574 pw.println(prefix + "snoozeCriteria=" + TextUtils.join(",", getSnoozeCriteria()));
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500575 }
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400576 pw.println(prefix + "mAdjustments=" + mAdjustments);
Chris Wren333a61c2014-05-28 16:40:57 -0400577 }
578
579
580 static String idDebugString(Context baseContext, String packageName, int id) {
581 Context c;
582
583 if (packageName != null) {
584 try {
585 c = baseContext.createPackageContext(packageName, 0);
586 } catch (NameNotFoundException e) {
587 c = baseContext;
588 }
589 } else {
590 c = baseContext;
591 }
592
593 Resources r = c.getResources();
594 try {
595 return r.getResourceName(id);
596 } catch (Resources.NotFoundException e) {
597 return "<name unknown>";
598 }
599 }
600
601 @Override
602 public final String toString() {
603 return String.format(
Julia Reynolds85769912016-10-25 09:08:57 -0400604 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s importance=%d key=%s" +
Rohan Shah590e1b22018-04-10 23:48:47 -0400605 "appImportanceLocked=%s: %s)",
Chris Wren333a61c2014-05-28 16:40:57 -0400606 System.identityHashCode(this),
607 this.sbn.getPackageName(), this.sbn.getUser(), this.sbn.getId(),
Julia Reynolds4db59552017-06-30 13:34:01 -0400608 this.sbn.getTag(), this.mImportance, this.sbn.getKey(),
Rohan Shah590e1b22018-04-10 23:48:47 -0400609 mIsAppImportanceLocked, this.sbn.getNotification());
Chris Wren333a61c2014-05-28 16:40:57 -0400610 }
611
Julia Reynolds27c0a962018-12-10 12:37:28 -0500612 public boolean hasAdjustment(String key) {
613 synchronized (mAdjustments) {
614 for (Adjustment adjustment : mAdjustments) {
615 if (adjustment.getSignals().containsKey(key)) {
616 return true;
617 }
618 }
619 }
620 return false;
621 }
622
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400623 public void addAdjustment(Adjustment adjustment) {
624 synchronized (mAdjustments) {
625 mAdjustments.add(adjustment);
626 }
627 }
628
629 public void applyAdjustments() {
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400630 long now = System.currentTimeMillis();
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400631 synchronized (mAdjustments) {
632 for (Adjustment adjustment: mAdjustments) {
633 Bundle signals = adjustment.getSignals();
634 if (signals.containsKey(Adjustment.KEY_PEOPLE)) {
635 final ArrayList<String> people =
636 adjustment.getSignals().getStringArrayList(Adjustment.KEY_PEOPLE);
637 setPeopleOverride(people);
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400638 MetricsLogger.action(getAdjustmentLogMaker()
639 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_PEOPLE, people.size()));
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400640 }
641 if (signals.containsKey(Adjustment.KEY_SNOOZE_CRITERIA)) {
642 final ArrayList<SnoozeCriterion> snoozeCriterionList =
643 adjustment.getSignals().getParcelableArrayList(
644 Adjustment.KEY_SNOOZE_CRITERIA);
645 setSnoozeCriteria(snoozeCriterionList);
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400646 MetricsLogger.action(getAdjustmentLogMaker()
647 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_SNOOZE_CRITERIA,
648 snoozeCriterionList.size()));
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400649 }
650 if (signals.containsKey(Adjustment.KEY_GROUP_KEY)) {
651 final String groupOverrideKey =
652 adjustment.getSignals().getString(Adjustment.KEY_GROUP_KEY);
653 setOverrideGroupKey(groupOverrideKey);
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400654 MetricsLogger.action(getAdjustmentLogMaker()
655 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_GROUP_KEY,
656 groupOverrideKey));
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400657 }
Julia Reynolds503ed942017-10-04 16:04:56 -0400658 if (signals.containsKey(Adjustment.KEY_USER_SENTIMENT)) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500659 // Only allow user sentiment update from assistant if user hasn't already
660 // expressed a preference for this channel
Rohan Shah590e1b22018-04-10 23:48:47 -0400661 if (!mIsAppImportanceLocked
662 && (getChannel().getUserLockedFields() & USER_LOCKED_IMPORTANCE) == 0) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500663 setUserSentiment(adjustment.getSignals().getInt(
664 Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEUTRAL));
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400665 MetricsLogger.action(getAdjustmentLogMaker()
666 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_USER_SENTIMENT,
667 getUserSentiment()));
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500668 }
Julia Reynolds503ed942017-10-04 16:04:56 -0400669 }
Tony Mak628cb932018-06-19 18:30:41 +0100670 if (signals.containsKey(Adjustment.KEY_SMART_ACTIONS)) {
Gustav Sennton1463d832018-11-06 16:12:48 +0000671 setSystemGeneratedSmartActions(
672 signals.getParcelableArrayList(Adjustment.KEY_SMART_ACTIONS));
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400673 MetricsLogger.action(getAdjustmentLogMaker()
674 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_SMART_ACTIONS,
Gustav Sennton1463d832018-11-06 16:12:48 +0000675 getSystemGeneratedSmartActions().size()));
Tony Mak628cb932018-06-19 18:30:41 +0100676 }
Tony Makc9acf672018-07-20 13:58:24 +0200677 if (signals.containsKey(Adjustment.KEY_SMART_REPLIES)) {
678 setSmartReplies(signals.getCharSequenceArrayList(Adjustment.KEY_SMART_REPLIES));
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400679 MetricsLogger.action(getAdjustmentLogMaker()
680 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_SMART_REPLIES,
681 getSmartReplies().size()));
Tony Makc9acf672018-07-20 13:58:24 +0200682 }
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400683 if (signals.containsKey(Adjustment.KEY_IMPORTANCE)) {
684 int importance = signals.getInt(Adjustment.KEY_IMPORTANCE);
685 importance = Math.max(IMPORTANCE_UNSPECIFIED, importance);
686 importance = Math.min(IMPORTANCE_HIGH, importance);
687 setAssistantImportance(importance);
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400688 MetricsLogger.action(getAdjustmentLogMaker()
689 .addTaggedData(MetricsEvent.ADJUSTMENT_KEY_IMPORTANCE,
690 importance));
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400691 }
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400692 }
693 }
694 }
695
Rohan Shah590e1b22018-04-10 23:48:47 -0400696 public void setIsAppImportanceLocked(boolean isAppImportanceLocked) {
697 mIsAppImportanceLocked = isAppImportanceLocked;
698 calculateUserSentiment();
699 }
700
Chris Wren333a61c2014-05-28 16:40:57 -0400701 public void setContactAffinity(float contactAffinity) {
702 mContactAffinity = contactAffinity;
703 }
704
705 public float getContactAffinity() {
706 return mContactAffinity;
707 }
708
John Spurlock1d881a12015-03-18 19:21:54 -0400709 public void setRecentlyIntrusive(boolean recentlyIntrusive) {
Chris Wren333a61c2014-05-28 16:40:57 -0400710 mRecentlyIntrusive = recentlyIntrusive;
Julia Reynolds309d1c82017-05-03 16:00:20 -0400711 if (recentlyIntrusive) {
712 mLastIntrusive = System.currentTimeMillis();
713 }
Chris Wren333a61c2014-05-28 16:40:57 -0400714 }
715
716 public boolean isRecentlyIntrusive() {
717 return mRecentlyIntrusive;
718 }
719
Julia Reynolds309d1c82017-05-03 16:00:20 -0400720 public long getLastIntrusive() {
721 return mLastIntrusive;
722 }
723
Chris Wren54bbef42014-07-09 18:37:56 -0400724 public void setPackagePriority(int packagePriority) {
John Spurlock6ac5f8d2014-07-18 11:27:54 -0400725 mPackagePriority = packagePriority;
Chris Wren54bbef42014-07-09 18:37:56 -0400726 }
727
728 public int getPackagePriority() {
729 return mPackagePriority;
730 }
731
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400732 public void setPackageVisibilityOverride(int packageVisibility) {
733 mPackageVisibility = packageVisibility;
734 }
735
736 public int getPackageVisibilityOverride() {
737 return mPackageVisibility;
738 }
739
Julia Reynoldsef37f282016-02-12 09:11:27 -0500740 private String getUserExplanation() {
741 if (mUserExplanation == null) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500742 mUserExplanation = mContext.getResources().getString(
743 com.android.internal.R.string.importance_from_user);
Chris Wrenbdf33762015-12-04 15:50:51 -0500744 }
Julia Reynoldsef37f282016-02-12 09:11:27 -0500745 return mUserExplanation;
Chris Wrenbdf33762015-12-04 15:50:51 -0500746 }
747
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400748 /**
749 * Sets the importance value the system thinks the record should have.
750 * e.g. bumping up foreground service notifications or people to people notifications.
751 */
752 public void setSystemImportance(int importance) {
753 mSystemImportance = importance;
Julia Reynolds27c0a962018-12-10 12:37:28 -0500754 // System importance is only changed in enqueue, so it's ok for us to calculate the
755 // importance directly instead of waiting for signal extractor.
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400756 calculateImportance();
Chris Wrenbdf33762015-12-04 15:50:51 -0500757 }
758
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400759 /**
760 * Sets the importance value the
761 * {@link android.service.notification.NotificationAssistantService} thinks the record should
762 * have.
763 */
764 public void setAssistantImportance(int importance) {
765 mAssistantImportance = importance;
Julia Reynolds27c0a962018-12-10 12:37:28 -0500766 // Unlike the system importance, the assistant importance can change on posted
767 // notifications, so don't calculateImportance() here, but wait for the signal extractors.
768 }
769
770 /**
771 * Returns the importance set by the assistant, or IMPORTANCE_UNSPECIFIED if the assistant
772 * hasn't set it.
773 */
774 public int getAssistantImportance() {
775 return mAssistantImportance;
Julia Reynolds5d25ee72015-11-20 15:38:20 -0500776 }
777
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400778 /**
779 * Recalculates the importance of the record after fields affecting importance have changed
780 */
781 protected void calculateImportance() {
782 mImportance = calculateInitialImportance();
783 mImportanceExplanation = "app";
Julia Reynolds48a6ed92018-10-22 12:52:03 -0400784 if (getChannel().hasUserSetImportance()) {
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400785 mImportanceExplanation = "user";
Chris Wrenbdf33762015-12-04 15:50:51 -0500786 }
Julia Reynolds27c0a962018-12-10 12:37:28 -0500787 if (!getChannel().hasUserSetImportance()
Julia Reynolds413ba842019-01-11 10:38:08 -0500788 && mAssistantImportance != IMPORTANCE_UNSPECIFIED
789 && !getChannel().isImportanceLockedByOEM()) {
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400790 mImportance = mAssistantImportance;
791 mImportanceExplanation = "asst";
792 }
793 if (mSystemImportance != IMPORTANCE_UNSPECIFIED) {
794 mImportance = mSystemImportance;
795 mImportanceExplanation = "system";
796 }
Chris Wrenbdf33762015-12-04 15:50:51 -0500797 }
798
799 public int getImportance() {
800 return mImportance;
801 }
802
803 public CharSequence getImportanceExplanation() {
804 return mImportanceExplanation;
805 }
806
Chris Wren333a61c2014-05-28 16:40:57 -0400807 public boolean setIntercepted(boolean intercept) {
808 mIntercept = intercept;
809 return mIntercept;
810 }
811
Brad Stenning9a8b2c82018-08-03 14:14:26 -0700812 /**
813 * Set to affect global sort key.
814 *
815 * @param criticality used in a string based sort thus 0 is the most critical
816 */
817 public void setCriticality(int criticality) {
818 mCriticality = criticality;
819 }
820
821 public int getCriticality() {
822 return mCriticality;
823 }
824
Chris Wren333a61c2014-05-28 16:40:57 -0400825 public boolean isIntercepted() {
826 return mIntercept;
827 }
828
Julia Reynoldsd6730072019-01-04 12:52:52 -0500829 public boolean isNewEnoughForAlerting(long now) {
830 return getFreshnessMs(now) <= MAX_SOUND_DELAY_MS;
831 }
832
Beverly5a20a5e2018-03-06 15:02:44 -0500833 public void setHidden(boolean hidden) {
834 mHidden = hidden;
835 }
836
837 public boolean isHidden() {
838 return mHidden;
839 }
840
841
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500842 public void setSuppressedVisualEffects(int effects) {
843 mSuppressedVisualEffects = effects;
844 }
845
846 public int getSuppressedVisualEffects() {
847 return mSuppressedVisualEffects;
848 }
849
John Spurlock312d1d02014-07-08 10:24:57 -0400850 public boolean isCategory(String category) {
John Spurlockbfa5dc42014-07-28 23:30:45 -0400851 return Objects.equals(getNotification().category, category);
852 }
853
John Spurlockbfa5dc42014-07-28 23:30:45 -0400854 public boolean isAudioAttributesUsage(int usage) {
Julia Reynolds51eb78f82018-03-07 07:35:21 -0500855 return mAttributes != null && mAttributes.getUsage() == usage;
John Spurlock312d1d02014-07-08 10:24:57 -0400856 }
857
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200858 /**
859 * Returns the timestamp to use for time-based sorting in the ranker.
860 */
861 public long getRankingTimeMs() {
862 return mRankingTimeMs;
863 }
864
865 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400866 * @param now this current time in milliseconds.
867 * @returns the number of milliseconds since the most recent update, or the post time if none.
Chris Wren6650e572015-05-15 17:19:25 -0400868 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400869 public int getFreshnessMs(long now) {
870 return (int) (now - mUpdateTimeMs);
Chris Wren6650e572015-05-15 17:19:25 -0400871 }
872
873 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400874 * @param now this current time in milliseconds.
875 * @returns the number of milliseconds since the the first post, ignoring updates.
Chris Wren640e3872015-04-21 13:23:18 -0400876 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400877 public int getLifespanMs(long now) {
878 return (int) (now - mCreationTimeMs);
Chris Wren640e3872015-04-21 13:23:18 -0400879 }
880
881 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400882 * @param now this current time in milliseconds.
883 * @returns the number of milliseconds since the most recent visibility event, or 0 if never.
Chris Wren6650e572015-05-15 17:19:25 -0400884 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400885 public int getExposureMs(long now) {
886 return mVisibleSinceMs == 0 ? 0 : (int) (now - mVisibleSinceMs);
Chris Wren6650e572015-05-15 17:19:25 -0400887 }
888
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400889 public int getInterruptionMs(long now) {
890 return (int) (now - mInterruptionTimeMs);
891 }
892
Chris Wren6650e572015-05-15 17:19:25 -0400893 /**
894 * Set the visibility of the notification.
895 */
Dieter Hsud39f0d52018-04-14 02:08:30 +0800896 public void setVisibility(boolean visible, int rank, int count) {
Chris Wren6650e572015-05-15 17:19:25 -0400897 final long now = System.currentTimeMillis();
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400898 mVisibleSinceMs = visible ? now : mVisibleSinceMs;
Chris Wren6650e572015-05-15 17:19:25 -0400899 stats.onVisibilityChanged(visible);
Chris Wren9eb5e102017-01-26 13:15:06 -0500900 MetricsLogger.action(getLogMaker(now)
901 .setCategory(MetricsEvent.NOTIFICATION_ITEM)
902 .setType(visible ? MetricsEvent.TYPE_OPEN : MetricsEvent.TYPE_CLOSE)
Dieter Hsud39f0d52018-04-14 02:08:30 +0800903 .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, rank)
904 .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, count));
Chris Wren9eb5e102017-01-26 13:15:06 -0500905 if (visible) {
Julia Reynolds503ed942017-10-04 16:04:56 -0400906 setSeen();
Chris Wren9eb5e102017-01-26 13:15:06 -0500907 MetricsLogger.histogram(mContext, "note_freshness", getFreshnessMs(now));
908 }
Chris Wren6650e572015-05-15 17:19:25 -0400909 EventLogTags.writeNotificationVisibility(getKey(), visible ? 1 : 0,
Chris Wren9eb5e102017-01-26 13:15:06 -0500910 getLifespanMs(now),
911 getFreshnessMs(now),
Chris Wrend1dbc922015-06-19 17:51:16 -0400912 0, // exposure time
913 rank);
Chris Wren6650e572015-05-15 17:19:25 -0400914 }
915
916 /**
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200917 * @param previousRankingTimeMs for updated notifications, {@link #getRankingTimeMs()}
918 * of the previous notification record, 0 otherwise
919 */
920 private long calculateRankingTimeMs(long previousRankingTimeMs) {
921 Notification n = getNotification();
922 // Take developer provided 'when', unless it's in the future.
923 if (n.when != 0 && n.when <= sbn.getPostTime()) {
924 return n.when;
925 }
926 // If we've ranked a previous instance with a timestamp, inherit it. This case is
927 // important in order to have ranking stability for updating notifications.
928 if (previousRankingTimeMs > 0) {
929 return previousRankingTimeMs;
930 }
931 return sbn.getPostTime();
932 }
Chris Wren1031c972014-07-23 13:11:45 +0000933
Christoph Studercd4adf82014-08-19 17:50:49 +0200934 public void setGlobalSortKey(String globalSortKey) {
935 mGlobalSortKey = globalSortKey;
Chris Wren1031c972014-07-23 13:11:45 +0000936 }
937
Christoph Studercd4adf82014-08-19 17:50:49 +0200938 public String getGlobalSortKey() {
939 return mGlobalSortKey;
Chris Wren1031c972014-07-23 13:11:45 +0000940 }
941
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700942 /** Check if any of the listeners have marked this notification as seen by the user. */
943 public boolean isSeen() {
Julia Reynolds503ed942017-10-04 16:04:56 -0400944 return mStats.hasSeen();
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700945 }
946
947 /** Mark the notification as seen by the user. */
948 public void setSeen() {
Julia Reynolds503ed942017-10-04 16:04:56 -0400949 mStats.setSeen();
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400950 if (mTextChanged) {
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400951 setInterruptive(true);
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400952 }
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700953 }
954
Chris Wren1031c972014-07-23 13:11:45 +0000955 public void setAuthoritativeRank(int authoritativeRank) {
956 mAuthoritativeRank = authoritativeRank;
957 }
958
959 public int getAuthoritativeRank() {
960 return mAuthoritativeRank;
961 }
962
963 public String getGroupKey() {
964 return sbn.getGroupKey();
965 }
Chris Wren47633422016-01-22 09:56:59 -0500966
Chris Wrenb3921792017-06-01 13:34:46 -0400967 public void setOverrideGroupKey(String overrideGroupKey) {
968 sbn.setOverrideGroupKey(overrideGroupKey);
Chris Wrenb3921792017-06-01 13:34:46 -0400969 }
970
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400971 public NotificationChannel getChannel() {
Julia Reynolds924eed12017-01-19 09:52:07 -0500972 return mChannel;
Julia Reynolds22f02b32016-12-01 15:05:13 -0500973 }
974
Rohan Shah590e1b22018-04-10 23:48:47 -0400975 /**
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400976 * @see PreferencesHelper#getIsAppImportanceLocked(String, int)
Rohan Shah590e1b22018-04-10 23:48:47 -0400977 */
978 public boolean getIsAppImportanceLocked() {
979 return mIsAppImportanceLocked;
980 }
981
Julia Reynolds924eed12017-01-19 09:52:07 -0500982 protected void updateNotificationChannel(NotificationChannel channel) {
983 if (channel != null) {
984 mChannel = channel;
Julia Reynolds22f02b32016-12-01 15:05:13 -0500985 calculateImportance();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500986 calculateUserSentiment();
Julia Reynolds22f02b32016-12-01 15:05:13 -0500987 }
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400988 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500989
Julia Reynolds924eed12017-01-19 09:52:07 -0500990 public void setShowBadge(boolean showBadge) {
991 mShowBadge = showBadge;
992 }
993
994 public boolean canShowBadge() {
995 return mShowBadge;
996 }
997
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500998 public Light getLight() {
999 return mLight;
1000 }
1001
Julia Reynolds0c299d42016-11-15 14:37:04 -05001002 public Uri getSound() {
1003 return mSound;
1004 }
1005
1006 public long[] getVibration() {
1007 return mVibration;
1008 }
1009
1010 public AudioAttributes getAudioAttributes() {
1011 return mAttributes;
1012 }
Julia Reynolds22f02b32016-12-01 15:05:13 -05001013
1014 public ArrayList<String> getPeopleOverride() {
1015 return mPeopleOverride;
1016 }
1017
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001018 public void setInterruptive(boolean interruptive) {
1019 mIsInterruptive = interruptive;
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -04001020 final long now = System.currentTimeMillis();
1021 mInterruptionTimeMs = interruptive ? now : mInterruptionTimeMs;
1022
1023 if (interruptive) {
1024 MetricsLogger.action(getLogMaker()
1025 .setCategory(MetricsEvent.NOTIFICATION_INTERRUPTION)
1026 .setType(MetricsEvent.TYPE_OPEN)
1027 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_INTERRUPTION_MILLIS,
1028 getInterruptionMs(now)));
1029 MetricsLogger.histogram(mContext, "note_interruptive", getInterruptionMs(now));
1030 }
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001031 }
1032
Gus Prevasa3226492018-10-23 11:10:09 -04001033 public void setAudiblyAlerted(boolean audiblyAlerted) {
Gus Prevas7306b902018-12-11 10:57:06 -05001034 mLastAudiblyAlertedMs = audiblyAlerted ? System.currentTimeMillis() : -1;
Gus Prevasa3226492018-10-23 11:10:09 -04001035 }
1036
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -04001037 public void setTextChanged(boolean textChanged) {
1038 mTextChanged = textChanged;
1039 }
1040
1041 public void setRecordedInterruption(boolean recorded) {
1042 mRecordedInterruption = recorded;
1043 }
1044
1045 public boolean hasRecordedInterruption() {
1046 return mRecordedInterruption;
1047 }
1048
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001049 public boolean isInterruptive() {
1050 return mIsInterruptive;
1051 }
1052
Gus Prevas7306b902018-12-11 10:57:06 -05001053 /** Returns the time the notification audibly alerted the user. */
1054 public long getLastAudiblyAlertedMs() {
1055 return mLastAudiblyAlertedMs;
Gus Prevasa3226492018-10-23 11:10:09 -04001056 }
1057
Julia Reynolds22f02b32016-12-01 15:05:13 -05001058 protected void setPeopleOverride(ArrayList<String> people) {
1059 mPeopleOverride = people;
1060 }
1061
1062 public ArrayList<SnoozeCriterion> getSnoozeCriteria() {
1063 return mSnoozeCriteria;
1064 }
1065
1066 protected void setSnoozeCriteria(ArrayList<SnoozeCriterion> snoozeCriteria) {
1067 mSnoozeCriteria = snoozeCriteria;
1068 }
Chris Wren9eb5e102017-01-26 13:15:06 -05001069
Julia Reynoldsc65656a2018-02-12 09:55:14 -05001070 private void calculateUserSentiment() {
Rohan Shah590e1b22018-04-10 23:48:47 -04001071 if ((getChannel().getUserLockedFields() & USER_LOCKED_IMPORTANCE) != 0
1072 || mIsAppImportanceLocked) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -05001073 mUserSentiment = USER_SENTIMENT_POSITIVE;
1074 }
1075 }
1076
Julia Reynolds503ed942017-10-04 16:04:56 -04001077 private void setUserSentiment(int userSentiment) {
1078 mUserSentiment = userSentiment;
1079 }
1080
1081 public int getUserSentiment() {
1082 return mUserSentiment;
1083 }
1084
1085 public NotificationStats getStats() {
1086 return mStats;
1087 }
1088
1089 public void recordExpanded() {
1090 mStats.setExpanded();
1091 }
1092
1093 public void recordDirectReplied() {
1094 mStats.setDirectReplied();
1095 }
1096
1097 public void recordDismissalSurface(@NotificationStats.DismissalSurface int surface) {
1098 mStats.setDismissalSurface(surface);
1099 }
1100
Julia Reynoldsfd4099d2018-08-21 11:06:06 -04001101 public void recordDismissalSentiment(@NotificationStats.DismissalSentiment int sentiment) {
1102 mStats.setDismissalSentiment(sentiment);
1103 }
1104
Julia Reynolds503ed942017-10-04 16:04:56 -04001105 public void recordSnoozed() {
1106 mStats.setSnoozed();
1107 }
1108
1109 public void recordViewedSettings() {
1110 mStats.setViewedSettings();
1111 }
1112
Kenny Guy23991102018-04-05 21:18:38 +01001113 public void setNumSmartRepliesAdded(int noReplies) {
1114 mNumberOfSmartRepliesAdded = noReplies;
1115 }
1116
1117 public int getNumSmartRepliesAdded() {
1118 return mNumberOfSmartRepliesAdded;
1119 }
1120
Gustav Senntond25a64d2018-12-07 10:58:39 +00001121 public void setNumSmartActionsAdded(int noActions) {
1122 mNumberOfSmartActionsAdded = noActions;
1123 }
1124
1125 public int getNumSmartActionsAdded() {
1126 return mNumberOfSmartActionsAdded;
1127 }
1128
1129 public void setSuggestionsGeneratedByAssistant(boolean generatedByAssistant) {
1130 mSuggestionsGeneratedByAssistant = generatedByAssistant;
1131 }
1132
1133 public boolean getSuggestionsGeneratedByAssistant() {
1134 return mSuggestionsGeneratedByAssistant;
1135 }
1136
Kenny Guy23991102018-04-05 21:18:38 +01001137 public boolean hasSeenSmartReplies() {
1138 return mHasSeenSmartReplies;
1139 }
1140
1141 public void setSeenSmartReplies(boolean hasSeenSmartReplies) {
1142 mHasSeenSmartReplies = hasSeenSmartReplies;
1143 }
1144
Gustav Sennton44dc5882018-12-13 14:38:50 +00001145 /**
1146 * Returns whether this notification has been visible and expanded at the same time.
1147 */
1148 public boolean hasBeenVisiblyExpanded() {
1149 return stats.hasBeenVisiblyExpanded();
1150 }
1151
Gustav Sennton1463d832018-11-06 16:12:48 +00001152 public void setSystemGeneratedSmartActions(
1153 ArrayList<Notification.Action> systemGeneratedSmartActions) {
1154 mSystemGeneratedSmartActions = systemGeneratedSmartActions;
Tony Mak628cb932018-06-19 18:30:41 +01001155 }
1156
Gustav Sennton1463d832018-11-06 16:12:48 +00001157 public ArrayList<Notification.Action> getSystemGeneratedSmartActions() {
1158 return mSystemGeneratedSmartActions;
Tony Mak628cb932018-06-19 18:30:41 +01001159 }
1160
Tony Makc9acf672018-07-20 13:58:24 +02001161 public void setSmartReplies(ArrayList<CharSequence> smartReplies) {
1162 mSmartReplies = smartReplies;
1163 }
1164
1165 public ArrayList<CharSequence> getSmartReplies() {
1166 return mSmartReplies;
1167 }
1168
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001169 /**
Julia Reynolds95334132018-12-19 11:15:35 -05001170 * Returns whether this notification was posted by a secondary app
1171 */
1172 public boolean isProxied() {
1173 return !Objects.equals(sbn.getPackageName(), sbn.getOpPkg());
1174 }
1175
1176 /**
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001177 * @return all {@link Uri} that should have permission granted to whoever
1178 * will be rendering it. This list has already been vetted to only
1179 * include {@link Uri} that the enqueuing app can grant.
1180 */
1181 public @Nullable ArraySet<Uri> getGrantableUris() {
1182 return mGrantableUris;
1183 }
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001184
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001185 /**
1186 * Collect all {@link Uri} that should have permission granted to whoever
1187 * will be rendering it.
1188 */
Julia Reynolds218871e2018-06-13 10:45:21 -04001189 protected void calculateGrantableUris() {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001190 final Notification notification = getNotification();
Jeff Sharkey23b31182018-04-18 21:32:12 -06001191 notification.visitUris((uri) -> {
Julia Reynolds218871e2018-06-13 10:45:21 -04001192 visitGrantableUri(uri, false);
Jeff Sharkey23b31182018-04-18 21:32:12 -06001193 });
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001194
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001195 if (notification.getChannelId() != null) {
1196 NotificationChannel channel = getChannel();
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001197 if (channel != null) {
Julia Reynolds218871e2018-06-13 10:45:21 -04001198 visitGrantableUri(channel.getSound(), (channel.getUserLockedFields()
1199 & NotificationChannel.USER_LOCKED_SOUND) != 0);
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001200 }
1201 }
1202 }
1203
1204 /**
1205 * Note the presence of a {@link Uri} that should have permission granted to
1206 * whoever will be rendering it.
1207 * <p>
1208 * If the enqueuing app has the ability to grant access, it will be added to
1209 * {@link #mGrantableUris}. Otherwise, this will either log or throw
1210 * {@link SecurityException} depending on target SDK of enqueuing app.
1211 */
Julia Reynolds218871e2018-06-13 10:45:21 -04001212 private void visitGrantableUri(Uri uri, boolean userOverriddenUri) {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001213 if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return;
1214
1215 // We can't grant Uri permissions from system
1216 final int sourceUid = sbn.getUid();
1217 if (sourceUid == android.os.Process.SYSTEM_UID) return;
1218
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001219 final long ident = Binder.clearCallingIdentity();
1220 try {
1221 // This will throw SecurityException if caller can't grant
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -07001222 mUgmInternal.checkGrantUriPermission(sourceUid, null,
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001223 ContentProvider.getUriWithoutUserId(uri),
1224 Intent.FLAG_GRANT_READ_URI_PERMISSION,
1225 ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid)));
1226
1227 if (mGrantableUris == null) {
1228 mGrantableUris = new ArraySet<>();
1229 }
1230 mGrantableUris.add(uri);
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001231 } catch (SecurityException e) {
Julia Reynolds218871e2018-06-13 10:45:21 -04001232 if (!userOverriddenUri) {
1233 if (mTargetSdkVersion >= Build.VERSION_CODES.P) {
1234 throw e;
1235 } else {
1236 Log.w(TAG, "Ignoring " + uri + " from " + sourceUid + ": " + e.getMessage());
1237 }
Jeff Sharkey6a97cc32018-04-17 12:16:20 -06001238 }
1239 } finally {
1240 Binder.restoreCallingIdentity(ident);
1241 }
Julia Reynoldse0d711f2017-09-01 08:50:47 -04001242 }
1243
Chris Wren9eb5e102017-01-26 13:15:06 -05001244 public LogMaker getLogMaker(long now) {
Eyal Posenera9cf9c72018-12-18 16:23:54 +02001245 return sbn.getLogMaker()
Chris Wren9eb5e102017-01-26 13:15:06 -05001246 .clearTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX)
Chris Wrenb3921792017-06-01 13:34:46 -04001247 .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE, mImportance)
Chris Wren9eb5e102017-01-26 13:15:06 -05001248 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_CREATE_MILLIS, getLifespanMs(now))
1249 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_UPDATE_MILLIS, getFreshnessMs(now))
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -04001250 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_VISIBLE_MILLIS, getExposureMs(now))
1251 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_INTERRUPTION_MILLIS,
1252 getInterruptionMs(now));
Chris Wren9eb5e102017-01-26 13:15:06 -05001253 }
1254
1255 public LogMaker getLogMaker() {
1256 return getLogMaker(System.currentTimeMillis());
1257 }
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05001258
Julia Reynolds3dfdde02018-10-08 09:17:56 -04001259 public LogMaker getItemLogMaker() {
1260 return getLogMaker().setCategory(MetricsEvent.NOTIFICATION_ITEM);
1261 }
1262
1263 public LogMaker getAdjustmentLogMaker() {
1264 return getLogMaker()
1265 .setCategory(MetricsEvent.NOTIFICATION_ITEM)
Will Brockman2a1e6b82019-01-15 11:36:06 -05001266 .setType(MetricsEvent.TYPE_NOTIFICATION_ASSISTANT_ADJUSTMENT);
Julia Reynolds3dfdde02018-10-08 09:17:56 -04001267 }
1268
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05001269 @VisibleForTesting
1270 static final class Light {
1271 public final int color;
1272 public final int onMs;
1273 public final int offMs;
1274
1275 public Light(int color, int onMs, int offMs) {
1276 this.color = color;
1277 this.onMs = onMs;
1278 this.offMs = offMs;
1279 }
1280
1281 @Override
1282 public boolean equals(Object o) {
1283 if (this == o) return true;
1284 if (o == null || getClass() != o.getClass()) return false;
1285
1286 Light light = (Light) o;
1287
1288 if (color != light.color) return false;
1289 if (onMs != light.onMs) return false;
1290 return offMs == light.offMs;
1291
1292 }
1293
1294 @Override
1295 public int hashCode() {
1296 int result = color;
1297 result = 31 * result + onMs;
1298 result = 31 * result + offMs;
1299 return result;
1300 }
1301
1302 @Override
1303 public String toString() {
1304 return "Light{" +
1305 "color=" + color +
1306 ", onMs=" + onMs +
1307 ", offMs=" + offMs +
1308 '}';
1309 }
1310 }
Chris Wren333a61c2014-05-28 16:40:57 -04001311}