blob: 46ce4bf59a50d01cbbf5cb7a9309423a57b1c360 [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 Reynolds4509ce72019-01-31 13:12:43 -0500164 private boolean mAllowBubble;
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500165 private Light mLight;
Gustav Sennton1463d832018-11-06 16:12:48 +0000166 /**
167 * This list contains system generated smart actions from NAS, app-generated smart actions are
Gustav Sennton005d7a02019-01-04 13:41:32 +0000168 * stored in Notification.actions with isContextual() set to true.
Gustav Sennton1463d832018-11-06 16:12:48 +0000169 */
170 private ArrayList<Notification.Action> mSystemGeneratedSmartActions;
Tony Makc9acf672018-07-20 13:58:24 +0200171 private ArrayList<CharSequence> mSmartReplies;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500172
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400173 private final List<Adjustment> mAdjustments;
Julia Reynolds503ed942017-10-04 16:04:56 -0400174 private final NotificationStats mStats;
175 private int mUserSentiment;
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500176 private boolean mIsInterruptive;
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400177 private boolean mTextChanged;
178 private boolean mRecordedInterruption;
Kenny Guy23991102018-04-05 21:18:38 +0100179 private int mNumberOfSmartRepliesAdded;
Gustav Senntond25a64d2018-12-07 10:58:39 +0000180 private int mNumberOfSmartActionsAdded;
181 private boolean mSuggestionsGeneratedByAssistant;
Milo Sredkov13d88112019-02-01 12:23:24 +0000182 private boolean mEditChoicesBeforeSending;
Kenny Guy23991102018-04-05 21:18:38 +0100183 private boolean mHasSeenSmartReplies;
Rohan Shah590e1b22018-04-10 23:48:47 -0400184 /**
185 * Whether this notification (and its channels) should be considered user locked. Used in
186 * conjunction with user sentiment calculation.
187 */
188 private boolean mIsAppImportanceLocked;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600189 private ArraySet<Uri> mGrantableUris;
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400190
Julia Reynolds924eed12017-01-19 09:52:07 -0500191 public NotificationRecord(Context context, StatusBarNotification sbn,
Rohan Shah590e1b22018-04-10 23:48:47 -0400192 NotificationChannel channel) {
Chris Wren333a61c2014-05-28 16:40:57 -0400193 this.sbn = sbn;
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600194 mTargetSdkVersion = LocalServices.getService(PackageManagerInternal.class)
195 .getPackageTargetSdkVersion(sbn.getPackageName());
Julia Reynolds218871e2018-06-13 10:45:21 -0400196 mAm = ActivityManager.getService();
Wale Ogunwale6d50dcc2018-07-21 23:00:40 -0700197 mUgmInternal = LocalServices.getService(UriGrantsManagerInternal.class);
Christoph Studer365e4c32014-09-18 20:35:36 +0200198 mOriginalFlags = sbn.getNotification().flags;
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200199 mRankingTimeMs = calculateRankingTimeMs(0L);
Chris Wren640e3872015-04-21 13:23:18 -0400200 mCreationTimeMs = sbn.getPostTime();
Chris Wren6650e572015-05-15 17:19:25 -0400201 mUpdateTimeMs = mCreationTimeMs;
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400202 mInterruptionTimeMs = mCreationTimeMs;
Chris Wrenbdf33762015-12-04 15:50:51 -0500203 mContext = context;
Chris Wrencdee8cd2016-01-25 17:10:30 -0500204 stats = new NotificationUsageStats.SingleNotificationStats();
Julia Reynolds924eed12017-01-19 09:52:07 -0500205 mChannel = channel;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000206 mPreChannelsNotification = isPreChannelsNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500207 mSound = calculateSound();
208 mVibration = calculateVibration();
209 mAttributes = calculateAttributes();
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400210 mImportance = calculateInitialImportance();
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500211 mLight = calculateLights();
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400212 mAdjustments = new ArrayList<>();
Julia Reynolds503ed942017-10-04 16:04:56 -0400213 mStats = new NotificationStats();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500214 calculateUserSentiment();
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600215 calculateGrantableUris();
Chris Wrenbdf33762015-12-04 15:50:51 -0500216 }
217
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000218 private boolean isPreChannelsNotification() {
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600219 if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(getChannel().getId())) {
220 if (mTargetSdkVersion < Build.VERSION_CODES.O) {
221 return true;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000222 }
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000223 }
224 return false;
225 }
226
Julia Reynolds0c299d42016-11-15 14:37:04 -0500227 private Uri calculateSound() {
Chris Wrenbdf33762015-12-04 15:50:51 -0500228 final Notification n = sbn.getNotification();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500229
Tony Mantlerab55f0f2017-06-16 10:50:00 -0700230 // No notification sounds on tv
231 if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
232 return null;
233 }
234
Julia Reynolds924eed12017-01-19 09:52:07 -0500235 Uri sound = mChannel.getSound();
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000236 if (mPreChannelsNotification && (getChannel().getUserLockedFields()
Julia Reynolds0c299d42016-11-15 14:37:04 -0500237 & NotificationChannel.USER_LOCKED_SOUND) == 0) {
238
239 final boolean useDefaultSound = (n.defaults & Notification.DEFAULT_SOUND) != 0;
240 if (useDefaultSound) {
241 sound = Settings.System.DEFAULT_NOTIFICATION_URI;
Julia Reynoldsb9e712e2017-04-17 10:31:03 -0400242 } else {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500243 sound = n.sound;
244 }
245 }
246 return sound;
247 }
248
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500249 private Light calculateLights() {
250 int defaultLightColor = mContext.getResources().getColor(
251 com.android.internal.R.color.config_defaultNotificationColor);
252 int defaultLightOn = mContext.getResources().getInteger(
253 com.android.internal.R.integer.config_defaultNotificationLedOn);
254 int defaultLightOff = mContext.getResources().getInteger(
255 com.android.internal.R.integer.config_defaultNotificationLedOff);
256
Julia Reynolds529e3322017-02-06 08:33:01 -0500257 int channelLightColor = getChannel().getLightColor() != 0 ? getChannel().getLightColor()
258 : defaultLightColor;
259 Light light = getChannel().shouldShowLights() ? new Light(channelLightColor,
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500260 defaultLightOn, defaultLightOff) : null;
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000261 if (mPreChannelsNotification
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500262 && (getChannel().getUserLockedFields()
263 & NotificationChannel.USER_LOCKED_LIGHTS) == 0) {
264 final Notification notification = sbn.getNotification();
265 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
266 light = new Light(notification.ledARGB, notification.ledOnMS,
267 notification.ledOffMS);
268 if ((notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
269 light = new Light(defaultLightColor, defaultLightOn,
270 defaultLightOff);
271 }
272 } else {
273 light = null;
274 }
275 }
276 return light;
277 }
278
Julia Reynolds0c299d42016-11-15 14:37:04 -0500279 private long[] calculateVibration() {
280 long[] vibration;
281 final long[] defaultVibration = NotificationManagerService.getLongArray(
282 mContext.getResources(),
283 com.android.internal.R.array.config_defaultNotificationVibePattern,
284 NotificationManagerService.VIBRATE_PATTERN_MAXLEN,
285 NotificationManagerService.DEFAULT_VIBRATE_PATTERN);
286 if (getChannel().shouldVibrate()) {
Julia Reynoldsf57de462016-11-23 11:31:46 -0500287 vibration = getChannel().getVibrationPattern() == null
288 ? defaultVibration : getChannel().getVibrationPattern();
Julia Reynolds0c299d42016-11-15 14:37:04 -0500289 } else {
290 vibration = null;
291 }
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000292 if (mPreChannelsNotification
Julia Reynolds0c299d42016-11-15 14:37:04 -0500293 && (getChannel().getUserLockedFields()
294 & NotificationChannel.USER_LOCKED_VIBRATION) == 0) {
295 final Notification notification = sbn.getNotification();
296 final boolean useDefaultVibrate =
297 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
298 if (useDefaultVibrate) {
299 vibration = defaultVibration;
300 } else {
301 vibration = notification.vibrate;
302 }
303 }
304 return vibration;
305 }
306
307 private AudioAttributes calculateAttributes() {
308 final Notification n = sbn.getNotification();
Julia Reynolds619a69f2017-01-27 15:11:38 -0500309 AudioAttributes attributes = getChannel().getAudioAttributes();
310 if (attributes == null) {
311 attributes = Notification.AUDIO_ATTRIBUTES_DEFAULT;
312 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500313
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000314 if (mPreChannelsNotification
Julia Reynolds619a69f2017-01-27 15:11:38 -0500315 && (getChannel().getUserLockedFields()
316 & NotificationChannel.USER_LOCKED_SOUND) == 0) {
317 if (n.audioAttributes != null) {
318 // prefer audio attributes to stream type
319 attributes = n.audioAttributes;
320 } else if (n.audioStreamType >= 0
321 && n.audioStreamType < AudioSystem.getNumStreamTypes()) {
322 // the stream type is valid, use it
323 attributes = new AudioAttributes.Builder()
324 .setInternalLegacyStreamType(n.audioStreamType)
325 .build();
326 } else if (n.audioStreamType != AudioSystem.STREAM_DEFAULT) {
327 Log.w(TAG, String.format("Invalid stream type: %d", n.audioStreamType));
328 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500329 }
330 return attributes;
331 }
332
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400333 private int calculateInitialImportance() {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500334 final Notification n = sbn.getNotification();
335 int importance = getChannel().getImportance();
336 int requestedImportance = IMPORTANCE_DEFAULT;
Chris Wrenbdf33762015-12-04 15:50:51 -0500337
338 // Migrate notification flags to scores
339 if (0 != (n.flags & Notification.FLAG_HIGH_PRIORITY)) {
340 n.priority = Notification.PRIORITY_MAX;
341 }
342
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500343 n.priority = NotificationManagerService.clamp(n.priority, Notification.PRIORITY_MIN,
344 Notification.PRIORITY_MAX);
Chris Wrenbdf33762015-12-04 15:50:51 -0500345 switch (n.priority) {
346 case Notification.PRIORITY_MIN:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500347 requestedImportance = IMPORTANCE_MIN;
Julia Reynoldsf0f629f2016-02-25 09:34:04 -0500348 break;
Chris Wrenbdf33762015-12-04 15:50:51 -0500349 case Notification.PRIORITY_LOW:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500350 requestedImportance = IMPORTANCE_LOW;
Chris Wrenbdf33762015-12-04 15:50:51 -0500351 break;
352 case Notification.PRIORITY_DEFAULT:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500353 requestedImportance = IMPORTANCE_DEFAULT;
Chris Wrenbdf33762015-12-04 15:50:51 -0500354 break;
355 case Notification.PRIORITY_HIGH:
Chris Wrenbdf33762015-12-04 15:50:51 -0500356 case Notification.PRIORITY_MAX:
Julia Reynolds0c299d42016-11-15 14:37:04 -0500357 requestedImportance = IMPORTANCE_HIGH;
Chris Wrenbdf33762015-12-04 15:50:51 -0500358 break;
359 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500360 stats.requestedImportance = requestedImportance;
361 stats.isNoisy = mSound != null || mVibration != null;
Chris Wrenbdf33762015-12-04 15:50:51 -0500362
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000363 if (mPreChannelsNotification
Julia Reynoldsa917a112017-03-21 11:09:14 -0400364 && (importance == IMPORTANCE_UNSPECIFIED
365 || (getChannel().getUserLockedFields()
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500366 & USER_LOCKED_IMPORTANCE) == 0)) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500367 if (!stats.isNoisy && requestedImportance > IMPORTANCE_LOW) {
368 requestedImportance = IMPORTANCE_LOW;
Julia Reynolds83fa1072016-02-17 09:10:19 -0500369 }
Julia Reynolds83fa1072016-02-17 09:10:19 -0500370
Julia Reynolds0c299d42016-11-15 14:37:04 -0500371 if (stats.isNoisy) {
372 if (requestedImportance < IMPORTANCE_DEFAULT) {
373 requestedImportance = IMPORTANCE_DEFAULT;
374 }
375 }
376
377 if (n.fullScreenIntent != null) {
378 requestedImportance = IMPORTANCE_HIGH;
379 }
380 importance = requestedImportance;
Chris Wrenbdf33762015-12-04 15:50:51 -0500381 }
382
Chris Wrencdee8cd2016-01-25 17:10:30 -0500383 stats.naturalImportance = importance;
Chris Wrenbdf33762015-12-04 15:50:51 -0500384 return importance;
Chris Wren333a61c2014-05-28 16:40:57 -0400385 }
386
387 // copy any notes that the ranking system may have made before the update
388 public void copyRankingInformation(NotificationRecord previous) {
389 mContactAffinity = previous.mContactAffinity;
390 mRecentlyIntrusive = previous.mRecentlyIntrusive;
Chris Wren54bbef42014-07-09 18:37:56 -0400391 mPackagePriority = previous.mPackagePriority;
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400392 mPackageVisibility = previous.mPackageVisibility;
Chris Wren333a61c2014-05-28 16:40:57 -0400393 mIntercept = previous.mIntercept;
Beverly5a20a5e2018-03-06 15:02:44 -0500394 mHidden = previous.mHidden;
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200395 mRankingTimeMs = calculateRankingTimeMs(previous.getRankingTimeMs());
Chris Wren640e3872015-04-21 13:23:18 -0400396 mCreationTimeMs = previous.mCreationTimeMs;
Chris Wren6650e572015-05-15 17:19:25 -0400397 mVisibleSinceMs = previous.mVisibleSinceMs;
Selim Cinek5b03ce92016-05-18 15:16:58 -0700398 if (previous.sbn.getOverrideGroupKey() != null && !sbn.isAppGroup()) {
Chris Wren8a1638f2016-05-02 16:19:14 -0400399 sbn.setOverrideGroupKey(previous.sbn.getOverrideGroupKey());
400 }
Chris Wren1f602dc2016-04-11 10:33:46 -0400401 // Don't copy importance information or mGlobalSortKey, recompute them.
Chris Wren333a61c2014-05-28 16:40:57 -0400402 }
403
404 public Notification getNotification() { return sbn.getNotification(); }
405 public int getFlags() { return sbn.getNotification().flags; }
Chris Wrenda4bd202014-09-04 15:53:52 -0400406 public UserHandle getUser() { return sbn.getUser(); }
Chris Wren333a61c2014-05-28 16:40:57 -0400407 public String getKey() { return sbn.getKey(); }
Chris Wrenda4bd202014-09-04 15:53:52 -0400408 /** @deprecated Use {@link #getUser()} instead. */
409 public int getUserId() { return sbn.getUserId(); }
Jeff Sharkey6a97cc32018-04-17 12:16:20 -0600410 public int getUid() { return sbn.getUid(); }
Chris Wren333a61c2014-05-28 16:40:57 -0400411
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800412 void dump(ProtoOutputStream proto, long fieldId, boolean redact, int state) {
413 final long token = proto.start(fieldId);
414
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500415 proto.write(NotificationRecordProto.KEY, sbn.getKey());
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800416 proto.write(NotificationRecordProto.STATE, state);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500417 if (getChannel() != null) {
418 proto.write(NotificationRecordProto.CHANNEL_ID, getChannel().getId());
419 }
420 proto.write(NotificationRecordProto.CAN_SHOW_LIGHT, getLight() != null);
421 proto.write(NotificationRecordProto.CAN_VIBRATE, getVibration() != null);
422 proto.write(NotificationRecordProto.FLAGS, sbn.getNotification().flags);
423 proto.write(NotificationRecordProto.GROUP_KEY, getGroupKey());
424 proto.write(NotificationRecordProto.IMPORTANCE, getImportance());
425 if (getSound() != null) {
426 proto.write(NotificationRecordProto.SOUND, getSound().toString());
427 }
428 if (getAudioAttributes() != null) {
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800429 getAudioAttributes().writeToProto(proto, NotificationRecordProto.AUDIO_ATTRIBUTES);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500430 }
Kweku Adamsbc84aec2018-01-23 13:33:12 -0800431
432 proto.end(token);
Julia Reynoldsc9842c12017-02-07 12:46:41 -0500433 }
434
Dan Sandler0a2308e2017-05-30 19:50:42 -0400435 String formatRemoteViews(RemoteViews rv) {
436 if (rv == null) return "null";
437 return String.format("%s/0x%08x (%d bytes): %s",
438 rv.getPackage(), rv.getLayoutId(), rv.estimateMemoryUsage(), rv.toString());
439 }
440
Dan Sandlera1770312015-07-10 13:59:29 -0400441 void dump(PrintWriter pw, String prefix, Context baseContext, boolean redact) {
Chris Wren333a61c2014-05-28 16:40:57 -0400442 final Notification notification = sbn.getNotification();
Dan Sandlerd63f9322015-05-06 15:18:49 -0400443 final Icon icon = notification.getSmallIcon();
444 String iconStr = String.valueOf(icon);
445 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
446 iconStr += " / " + idDebugString(baseContext, icon.getResPackage(), icon.getResId());
447 }
Chris Wren333a61c2014-05-28 16:40:57 -0400448 pw.println(prefix + this);
Dan Sandler0a2308e2017-05-30 19:50:42 -0400449 prefix = prefix + " ";
Julia Reynoldsa917a112017-03-21 11:09:14 -0400450 pw.println(prefix + "uid=" + sbn.getUid() + " userId=" + sbn.getUserId());
451 pw.println(prefix + "icon=" + iconStr);
Julia Reynolds4db59552017-06-30 13:34:01 -0400452 pw.println(prefix + "flags=0x" + Integer.toHexString(notification.flags));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400453 pw.println(prefix + "pri=" + notification.priority);
454 pw.println(prefix + "key=" + sbn.getKey());
Julia Reynolds503ed942017-10-04 16:04:56 -0400455 pw.println(prefix + "seen=" + mStats.hasSeen());
Julia Reynoldsa917a112017-03-21 11:09:14 -0400456 pw.println(prefix + "groupKey=" + getGroupKey());
457 pw.println(prefix + "fullscreenIntent=" + notification.fullScreenIntent);
458 pw.println(prefix + "contentIntent=" + notification.contentIntent);
459 pw.println(prefix + "deleteIntent=" + notification.deleteIntent);
Aaron Heuckrothb4d6aa72018-07-02 14:58:33 -0400460 pw.println(prefix + "number=" + notification.number);
461 pw.println(prefix + "groupAlertBehavior=" + notification.getGroupAlertBehavior());
Dan Sandler0b4ceb32017-03-29 14:13:55 -0400462
463 pw.print(prefix + "tickerText=");
464 if (!TextUtils.isEmpty(notification.tickerText)) {
465 final String ticker = notification.tickerText.toString();
466 if (redact) {
467 // if the string is long enough, we allow ourselves a few bytes for debugging
468 pw.print(ticker.length() > 16 ? ticker.substring(0,8) : "");
469 pw.println("...");
470 } else {
471 pw.println(ticker);
472 }
473 } else {
474 pw.println("null");
475 }
Dan Sandler0a2308e2017-05-30 19:50:42 -0400476 pw.println(prefix + "contentView=" + formatRemoteViews(notification.contentView));
477 pw.println(prefix + "bigContentView=" + formatRemoteViews(notification.bigContentView));
478 pw.println(prefix + "headsUpContentView="
479 + formatRemoteViews(notification.headsUpContentView));
480 pw.print(prefix + String.format("color=0x%08x", notification.color));
Julia Reynoldsbad42972017-04-25 13:52:49 -0400481 pw.println(prefix + "timeout="
482 + TimeUtils.formatForLogging(notification.getTimeoutAfter()));
Chris Wren333a61c2014-05-28 16:40:57 -0400483 if (notification.actions != null && notification.actions.length > 0) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400484 pw.println(prefix + "actions={");
Chris Wren333a61c2014-05-28 16:40:57 -0400485 final int N = notification.actions.length;
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500486 for (int i = 0; i < N; i++) {
Chris Wren333a61c2014-05-28 16:40:57 -0400487 final Notification.Action action = notification.actions[i];
Chris Wren1ac52a92016-02-24 14:54:52 -0500488 if (action != null) {
489 pw.println(String.format("%s [%d] \"%s\" -> %s",
490 prefix,
491 i,
492 action.title,
493 action.actionIntent == null ? "null" : action.actionIntent.toString()
494 ));
495 }
Chris Wren333a61c2014-05-28 16:40:57 -0400496 }
497 pw.println(prefix + " }");
498 }
499 if (notification.extras != null && notification.extras.size() > 0) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400500 pw.println(prefix + "extras={");
Chris Wren333a61c2014-05-28 16:40:57 -0400501 for (String key : notification.extras.keySet()) {
502 pw.print(prefix + " " + key + "=");
503 Object val = notification.extras.get(key);
504 if (val == null) {
505 pw.println("null");
506 } else {
507 pw.print(val.getClass().getSimpleName());
Dan Sandlera1770312015-07-10 13:59:29 -0400508 if (redact && (val instanceof CharSequence || val instanceof String)) {
Chris Wren333a61c2014-05-28 16:40:57 -0400509 // redact contents from bugreports
510 } else if (val instanceof Bitmap) {
511 pw.print(String.format(" (%dx%d)",
512 ((Bitmap) val).getWidth(),
513 ((Bitmap) val).getHeight()));
514 } else if (val.getClass().isArray()) {
515 final int N = Array.getLength(val);
Dan Sandlera1770312015-07-10 13:59:29 -0400516 pw.print(" (" + N + ")");
517 if (!redact) {
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500518 for (int j = 0; j < N; j++) {
Dan Sandlera1770312015-07-10 13:59:29 -0400519 pw.println();
520 pw.print(String.format("%s [%d] %s",
521 prefix, j, String.valueOf(Array.get(val, j))));
522 }
523 }
Chris Wren333a61c2014-05-28 16:40:57 -0400524 } else {
525 pw.print(" (" + String.valueOf(val) + ")");
526 }
527 pw.println();
528 }
529 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400530 pw.println(prefix + "}");
Chris Wren333a61c2014-05-28 16:40:57 -0400531 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400532 pw.println(prefix + "stats=" + stats.toString());
533 pw.println(prefix + "mContactAffinity=" + mContactAffinity);
534 pw.println(prefix + "mRecentlyIntrusive=" + mRecentlyIntrusive);
535 pw.println(prefix + "mPackagePriority=" + mPackagePriority);
536 pw.println(prefix + "mPackageVisibility=" + mPackageVisibility);
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400537 pw.println(prefix + "mSystemImportance="
538 + NotificationListenerService.Ranking.importanceToString(mSystemImportance));
539 pw.println(prefix + "mAsstImportance="
540 + NotificationListenerService.Ranking.importanceToString(mAssistantImportance));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400541 pw.println(prefix + "mImportance="
Chris Wrenbdf33762015-12-04 15:50:51 -0500542 + NotificationListenerService.Ranking.importanceToString(mImportance));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400543 pw.println(prefix + "mImportanceExplanation=" + mImportanceExplanation);
Rohan Shah590e1b22018-04-10 23:48:47 -0400544 pw.println(prefix + "mIsAppImportanceLocked=" + mIsAppImportanceLocked);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400545 pw.println(prefix + "mIntercept=" + mIntercept);
Beverly5a20a5e2018-03-06 15:02:44 -0500546 pw.println(prefix + "mHidden==" + mHidden);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400547 pw.println(prefix + "mGlobalSortKey=" + mGlobalSortKey);
548 pw.println(prefix + "mRankingTimeMs=" + mRankingTimeMs);
549 pw.println(prefix + "mCreationTimeMs=" + mCreationTimeMs);
550 pw.println(prefix + "mVisibleSinceMs=" + mVisibleSinceMs);
551 pw.println(prefix + "mUpdateTimeMs=" + mUpdateTimeMs);
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400552 pw.println(prefix + "mInterruptionTimeMs=" + mInterruptionTimeMs);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400553 pw.println(prefix + "mSuppressedVisualEffects= " + mSuppressedVisualEffects);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000554 if (mPreChannelsNotification) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400555 pw.println(prefix + String.format("defaults=0x%08x flags=0x%08x",
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500556 notification.defaults, notification.flags));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400557 pw.println(prefix + "n.sound=" + notification.sound);
558 pw.println(prefix + "n.audioStreamType=" + notification.audioStreamType);
559 pw.println(prefix + "n.audioAttributes=" + notification.audioAttributes);
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500560 pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d",
561 notification.ledARGB, notification.ledOnMS, notification.ledOffMS));
Julia Reynoldsa917a112017-03-21 11:09:14 -0400562 pw.println(prefix + "vibrate=" + Arrays.toString(notification.vibrate));
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500563 }
Julia Reynoldsa917a112017-03-21 11:09:14 -0400564 pw.println(prefix + "mSound= " + mSound);
565 pw.println(prefix + "mVibration= " + mVibration);
566 pw.println(prefix + "mAttributes= " + mAttributes);
567 pw.println(prefix + "mLight= " + mLight);
568 pw.println(prefix + "mShowBadge=" + mShowBadge);
Julia Reynolds4db59552017-06-30 13:34:01 -0400569 pw.println(prefix + "mColorized=" + notification.isColorized());
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500570 pw.println(prefix + "mIsInterruptive=" + mIsInterruptive);
Julia Reynoldsa917a112017-03-21 11:09:14 -0400571 pw.println(prefix + "effectiveNotificationChannel=" + getChannel());
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500572 if (getPeopleOverride() != null) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400573 pw.println(prefix + "overridePeople= " + TextUtils.join(",", getPeopleOverride()));
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500574 }
575 if (getSnoozeCriteria() != null) {
Julia Reynoldsa917a112017-03-21 11:09:14 -0400576 pw.println(prefix + "snoozeCriteria=" + TextUtils.join(",", getSnoozeCriteria()));
Julia Reynolds4b82f6d2017-01-04 10:47:41 -0500577 }
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400578 pw.println(prefix + "mAdjustments=" + mAdjustments);
Chris Wren333a61c2014-05-28 16:40:57 -0400579 }
580
581
582 static String idDebugString(Context baseContext, String packageName, int id) {
583 Context c;
584
585 if (packageName != null) {
586 try {
587 c = baseContext.createPackageContext(packageName, 0);
588 } catch (NameNotFoundException e) {
589 c = baseContext;
590 }
591 } else {
592 c = baseContext;
593 }
594
595 Resources r = c.getResources();
596 try {
597 return r.getResourceName(id);
598 } catch (Resources.NotFoundException e) {
599 return "<name unknown>";
600 }
601 }
602
603 @Override
604 public final String toString() {
605 return String.format(
Julia Reynolds85769912016-10-25 09:08:57 -0400606 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s importance=%d key=%s" +
Rohan Shah590e1b22018-04-10 23:48:47 -0400607 "appImportanceLocked=%s: %s)",
Chris Wren333a61c2014-05-28 16:40:57 -0400608 System.identityHashCode(this),
609 this.sbn.getPackageName(), this.sbn.getUser(), this.sbn.getId(),
Julia Reynolds4db59552017-06-30 13:34:01 -0400610 this.sbn.getTag(), this.mImportance, this.sbn.getKey(),
Rohan Shah590e1b22018-04-10 23:48:47 -0400611 mIsAppImportanceLocked, this.sbn.getNotification());
Chris Wren333a61c2014-05-28 16:40:57 -0400612 }
613
Julia Reynolds27c0a962018-12-10 12:37:28 -0500614 public boolean hasAdjustment(String key) {
615 synchronized (mAdjustments) {
616 for (Adjustment adjustment : mAdjustments) {
617 if (adjustment.getSignals().containsKey(key)) {
618 return true;
619 }
620 }
621 }
622 return false;
623 }
624
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400625 public void addAdjustment(Adjustment adjustment) {
626 synchronized (mAdjustments) {
627 mAdjustments.add(adjustment);
628 }
629 }
630
631 public void applyAdjustments() {
Julia Reynolds3dfdde02018-10-08 09:17:56 -0400632 long now = System.currentTimeMillis();
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400633 synchronized (mAdjustments) {
634 for (Adjustment adjustment: mAdjustments) {
635 Bundle signals = adjustment.getSignals();
636 if (signals.containsKey(Adjustment.KEY_PEOPLE)) {
637 final ArrayList<String> people =
638 adjustment.getSignals().getStringArrayList(Adjustment.KEY_PEOPLE);
639 setPeopleOverride(people);
640 }
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);
646 }
647 if (signals.containsKey(Adjustment.KEY_GROUP_KEY)) {
648 final String groupOverrideKey =
649 adjustment.getSignals().getString(Adjustment.KEY_GROUP_KEY);
650 setOverrideGroupKey(groupOverrideKey);
651 }
Julia Reynolds503ed942017-10-04 16:04:56 -0400652 if (signals.containsKey(Adjustment.KEY_USER_SENTIMENT)) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500653 // Only allow user sentiment update from assistant if user hasn't already
654 // expressed a preference for this channel
Rohan Shah590e1b22018-04-10 23:48:47 -0400655 if (!mIsAppImportanceLocked
656 && (getChannel().getUserLockedFields() & USER_LOCKED_IMPORTANCE) == 0) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500657 setUserSentiment(adjustment.getSignals().getInt(
658 Adjustment.KEY_USER_SENTIMENT, USER_SENTIMENT_NEUTRAL));
659 }
Julia Reynolds503ed942017-10-04 16:04:56 -0400660 }
Tony Maka4716992019-01-24 15:41:59 +0000661 if (signals.containsKey(Adjustment.KEY_CONTEXTUAL_ACTIONS)) {
Gustav Sennton1463d832018-11-06 16:12:48 +0000662 setSystemGeneratedSmartActions(
Tony Maka4716992019-01-24 15:41:59 +0000663 signals.getParcelableArrayList(Adjustment.KEY_CONTEXTUAL_ACTIONS));
Tony Mak628cb932018-06-19 18:30:41 +0100664 }
Tony Maka4716992019-01-24 15:41:59 +0000665 if (signals.containsKey(Adjustment.KEY_TEXT_REPLIES)) {
666 setSmartReplies(signals.getCharSequenceArrayList(Adjustment.KEY_TEXT_REPLIES));
Tony Makc9acf672018-07-20 13:58:24 +0200667 }
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400668 if (signals.containsKey(Adjustment.KEY_IMPORTANCE)) {
669 int importance = signals.getInt(Adjustment.KEY_IMPORTANCE);
670 importance = Math.max(IMPORTANCE_UNSPECIFIED, importance);
671 importance = Math.min(IMPORTANCE_HIGH, importance);
672 setAssistantImportance(importance);
673 }
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400674 }
Will Brockmanb020b5e2019-01-25 10:37:30 -0500675 // We have now gotten all the information out of the adjustments and can forget them.
676 mAdjustments.clear();
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400677 }
678 }
679
Rohan Shah590e1b22018-04-10 23:48:47 -0400680 public void setIsAppImportanceLocked(boolean isAppImportanceLocked) {
681 mIsAppImportanceLocked = isAppImportanceLocked;
682 calculateUserSentiment();
683 }
684
Chris Wren333a61c2014-05-28 16:40:57 -0400685 public void setContactAffinity(float contactAffinity) {
686 mContactAffinity = contactAffinity;
687 }
688
689 public float getContactAffinity() {
690 return mContactAffinity;
691 }
692
John Spurlock1d881a12015-03-18 19:21:54 -0400693 public void setRecentlyIntrusive(boolean recentlyIntrusive) {
Chris Wren333a61c2014-05-28 16:40:57 -0400694 mRecentlyIntrusive = recentlyIntrusive;
Julia Reynolds309d1c82017-05-03 16:00:20 -0400695 if (recentlyIntrusive) {
696 mLastIntrusive = System.currentTimeMillis();
697 }
Chris Wren333a61c2014-05-28 16:40:57 -0400698 }
699
700 public boolean isRecentlyIntrusive() {
701 return mRecentlyIntrusive;
702 }
703
Julia Reynolds309d1c82017-05-03 16:00:20 -0400704 public long getLastIntrusive() {
705 return mLastIntrusive;
706 }
707
Chris Wren54bbef42014-07-09 18:37:56 -0400708 public void setPackagePriority(int packagePriority) {
John Spurlock6ac5f8d2014-07-18 11:27:54 -0400709 mPackagePriority = packagePriority;
Chris Wren54bbef42014-07-09 18:37:56 -0400710 }
711
712 public int getPackagePriority() {
713 return mPackagePriority;
714 }
715
Chris Wren3ad4e3a2014-09-02 17:23:51 -0400716 public void setPackageVisibilityOverride(int packageVisibility) {
717 mPackageVisibility = packageVisibility;
718 }
719
720 public int getPackageVisibilityOverride() {
721 return mPackageVisibility;
722 }
723
Julia Reynoldsef37f282016-02-12 09:11:27 -0500724 private String getUserExplanation() {
725 if (mUserExplanation == null) {
Julia Reynolds0c299d42016-11-15 14:37:04 -0500726 mUserExplanation = mContext.getResources().getString(
727 com.android.internal.R.string.importance_from_user);
Chris Wrenbdf33762015-12-04 15:50:51 -0500728 }
Julia Reynoldsef37f282016-02-12 09:11:27 -0500729 return mUserExplanation;
Chris Wrenbdf33762015-12-04 15:50:51 -0500730 }
731
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400732 /**
733 * Sets the importance value the system thinks the record should have.
734 * e.g. bumping up foreground service notifications or people to people notifications.
735 */
736 public void setSystemImportance(int importance) {
737 mSystemImportance = importance;
Julia Reynolds27c0a962018-12-10 12:37:28 -0500738 // System importance is only changed in enqueue, so it's ok for us to calculate the
739 // importance directly instead of waiting for signal extractor.
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400740 calculateImportance();
Chris Wrenbdf33762015-12-04 15:50:51 -0500741 }
742
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400743 /**
744 * Sets the importance value the
745 * {@link android.service.notification.NotificationAssistantService} thinks the record should
746 * have.
747 */
748 public void setAssistantImportance(int importance) {
749 mAssistantImportance = importance;
Julia Reynolds27c0a962018-12-10 12:37:28 -0500750 // Unlike the system importance, the assistant importance can change on posted
751 // notifications, so don't calculateImportance() here, but wait for the signal extractors.
752 }
753
754 /**
755 * Returns the importance set by the assistant, or IMPORTANCE_UNSPECIFIED if the assistant
756 * hasn't set it.
757 */
758 public int getAssistantImportance() {
759 return mAssistantImportance;
Julia Reynolds5d25ee72015-11-20 15:38:20 -0500760 }
761
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400762 /**
763 * Recalculates the importance of the record after fields affecting importance have changed
764 */
765 protected void calculateImportance() {
766 mImportance = calculateInitialImportance();
767 mImportanceExplanation = "app";
Julia Reynolds48a6ed92018-10-22 12:52:03 -0400768 if (getChannel().hasUserSetImportance()) {
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400769 mImportanceExplanation = "user";
Chris Wrenbdf33762015-12-04 15:50:51 -0500770 }
Julia Reynolds27c0a962018-12-10 12:37:28 -0500771 if (!getChannel().hasUserSetImportance()
Julia Reynolds413ba842019-01-11 10:38:08 -0500772 && mAssistantImportance != IMPORTANCE_UNSPECIFIED
773 && !getChannel().isImportanceLockedByOEM()) {
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400774 mImportance = mAssistantImportance;
775 mImportanceExplanation = "asst";
776 }
777 if (mSystemImportance != IMPORTANCE_UNSPECIFIED) {
778 mImportance = mSystemImportance;
779 mImportanceExplanation = "system";
780 }
Chris Wrenbdf33762015-12-04 15:50:51 -0500781 }
782
783 public int getImportance() {
784 return mImportance;
785 }
786
787 public CharSequence getImportanceExplanation() {
788 return mImportanceExplanation;
789 }
790
Chris Wren333a61c2014-05-28 16:40:57 -0400791 public boolean setIntercepted(boolean intercept) {
792 mIntercept = intercept;
793 return mIntercept;
794 }
795
Brad Stenning9a8b2c82018-08-03 14:14:26 -0700796 /**
797 * Set to affect global sort key.
798 *
799 * @param criticality used in a string based sort thus 0 is the most critical
800 */
801 public void setCriticality(int criticality) {
802 mCriticality = criticality;
803 }
804
805 public int getCriticality() {
806 return mCriticality;
807 }
808
Chris Wren333a61c2014-05-28 16:40:57 -0400809 public boolean isIntercepted() {
810 return mIntercept;
811 }
812
Julia Reynoldsd6730072019-01-04 12:52:52 -0500813 public boolean isNewEnoughForAlerting(long now) {
814 return getFreshnessMs(now) <= MAX_SOUND_DELAY_MS;
815 }
816
Beverly5a20a5e2018-03-06 15:02:44 -0500817 public void setHidden(boolean hidden) {
818 mHidden = hidden;
819 }
820
821 public boolean isHidden() {
822 return mHidden;
823 }
824
825
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500826 public void setSuppressedVisualEffects(int effects) {
827 mSuppressedVisualEffects = effects;
828 }
829
830 public int getSuppressedVisualEffects() {
831 return mSuppressedVisualEffects;
832 }
833
John Spurlock312d1d02014-07-08 10:24:57 -0400834 public boolean isCategory(String category) {
John Spurlockbfa5dc42014-07-28 23:30:45 -0400835 return Objects.equals(getNotification().category, category);
836 }
837
John Spurlockbfa5dc42014-07-28 23:30:45 -0400838 public boolean isAudioAttributesUsage(int usage) {
Julia Reynolds51eb78f82018-03-07 07:35:21 -0500839 return mAttributes != null && mAttributes.getUsage() == usage;
John Spurlock312d1d02014-07-08 10:24:57 -0400840 }
841
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200842 /**
843 * Returns the timestamp to use for time-based sorting in the ranker.
844 */
845 public long getRankingTimeMs() {
846 return mRankingTimeMs;
847 }
848
849 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400850 * @param now this current time in milliseconds.
851 * @returns the number of milliseconds since the most recent update, or the post time if none.
Chris Wren6650e572015-05-15 17:19:25 -0400852 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400853 public int getFreshnessMs(long now) {
854 return (int) (now - mUpdateTimeMs);
Chris Wren6650e572015-05-15 17:19:25 -0400855 }
856
857 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400858 * @param now this current time in milliseconds.
859 * @returns the number of milliseconds since the the first post, ignoring updates.
Chris Wren640e3872015-04-21 13:23:18 -0400860 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400861 public int getLifespanMs(long now) {
862 return (int) (now - mCreationTimeMs);
Chris Wren640e3872015-04-21 13:23:18 -0400863 }
864
865 /**
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400866 * @param now this current time in milliseconds.
867 * @returns the number of milliseconds since the most recent visibility event, or 0 if never.
Chris Wren6650e572015-05-15 17:19:25 -0400868 */
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400869 public int getExposureMs(long now) {
870 return mVisibleSinceMs == 0 ? 0 : (int) (now - mVisibleSinceMs);
Chris Wren6650e572015-05-15 17:19:25 -0400871 }
872
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400873 public int getInterruptionMs(long now) {
874 return (int) (now - mInterruptionTimeMs);
875 }
876
Chris Wren6650e572015-05-15 17:19:25 -0400877 /**
878 * Set the visibility of the notification.
879 */
Dieter Hsud39f0d52018-04-14 02:08:30 +0800880 public void setVisibility(boolean visible, int rank, int count) {
Chris Wren6650e572015-05-15 17:19:25 -0400881 final long now = System.currentTimeMillis();
Chris Wrene6ddb8a2015-05-27 15:21:00 -0400882 mVisibleSinceMs = visible ? now : mVisibleSinceMs;
Chris Wren6650e572015-05-15 17:19:25 -0400883 stats.onVisibilityChanged(visible);
Chris Wren9eb5e102017-01-26 13:15:06 -0500884 MetricsLogger.action(getLogMaker(now)
885 .setCategory(MetricsEvent.NOTIFICATION_ITEM)
886 .setType(visible ? MetricsEvent.TYPE_OPEN : MetricsEvent.TYPE_CLOSE)
Dieter Hsud39f0d52018-04-14 02:08:30 +0800887 .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, rank)
888 .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, count));
Chris Wren9eb5e102017-01-26 13:15:06 -0500889 if (visible) {
Julia Reynolds503ed942017-10-04 16:04:56 -0400890 setSeen();
Chris Wren9eb5e102017-01-26 13:15:06 -0500891 MetricsLogger.histogram(mContext, "note_freshness", getFreshnessMs(now));
892 }
Chris Wren6650e572015-05-15 17:19:25 -0400893 EventLogTags.writeNotificationVisibility(getKey(), visible ? 1 : 0,
Chris Wren9eb5e102017-01-26 13:15:06 -0500894 getLifespanMs(now),
895 getFreshnessMs(now),
Chris Wrend1dbc922015-06-19 17:51:16 -0400896 0, // exposure time
897 rank);
Chris Wren6650e572015-05-15 17:19:25 -0400898 }
899
900 /**
Christoph Studer52b7a5a2014-06-06 16:09:15 +0200901 * @param previousRankingTimeMs for updated notifications, {@link #getRankingTimeMs()}
902 * of the previous notification record, 0 otherwise
903 */
904 private long calculateRankingTimeMs(long previousRankingTimeMs) {
905 Notification n = getNotification();
906 // Take developer provided 'when', unless it's in the future.
907 if (n.when != 0 && n.when <= sbn.getPostTime()) {
908 return n.when;
909 }
910 // If we've ranked a previous instance with a timestamp, inherit it. This case is
911 // important in order to have ranking stability for updating notifications.
912 if (previousRankingTimeMs > 0) {
913 return previousRankingTimeMs;
914 }
915 return sbn.getPostTime();
916 }
Chris Wren1031c972014-07-23 13:11:45 +0000917
Christoph Studercd4adf82014-08-19 17:50:49 +0200918 public void setGlobalSortKey(String globalSortKey) {
919 mGlobalSortKey = globalSortKey;
Chris Wren1031c972014-07-23 13:11:45 +0000920 }
921
Christoph Studercd4adf82014-08-19 17:50:49 +0200922 public String getGlobalSortKey() {
923 return mGlobalSortKey;
Chris Wren1031c972014-07-23 13:11:45 +0000924 }
925
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700926 /** Check if any of the listeners have marked this notification as seen by the user. */
927 public boolean isSeen() {
Julia Reynolds503ed942017-10-04 16:04:56 -0400928 return mStats.hasSeen();
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700929 }
930
931 /** Mark the notification as seen by the user. */
932 public void setSeen() {
Julia Reynolds503ed942017-10-04 16:04:56 -0400933 mStats.setSeen();
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400934 if (mTextChanged) {
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -0400935 setInterruptive(true);
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -0400936 }
Amith Yamasanif47e51e2015-04-17 10:02:15 -0700937 }
938
Chris Wren1031c972014-07-23 13:11:45 +0000939 public void setAuthoritativeRank(int authoritativeRank) {
940 mAuthoritativeRank = authoritativeRank;
941 }
942
943 public int getAuthoritativeRank() {
944 return mAuthoritativeRank;
945 }
946
947 public String getGroupKey() {
948 return sbn.getGroupKey();
949 }
Chris Wren47633422016-01-22 09:56:59 -0500950
Chris Wrenb3921792017-06-01 13:34:46 -0400951 public void setOverrideGroupKey(String overrideGroupKey) {
952 sbn.setOverrideGroupKey(overrideGroupKey);
Chris Wrenb3921792017-06-01 13:34:46 -0400953 }
954
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400955 public NotificationChannel getChannel() {
Julia Reynolds924eed12017-01-19 09:52:07 -0500956 return mChannel;
Julia Reynolds22f02b32016-12-01 15:05:13 -0500957 }
958
Rohan Shah590e1b22018-04-10 23:48:47 -0400959 /**
Julia Reynoldsefcdff42018-08-09 09:42:56 -0400960 * @see PreferencesHelper#getIsAppImportanceLocked(String, int)
Rohan Shah590e1b22018-04-10 23:48:47 -0400961 */
962 public boolean getIsAppImportanceLocked() {
963 return mIsAppImportanceLocked;
964 }
965
Julia Reynolds924eed12017-01-19 09:52:07 -0500966 protected void updateNotificationChannel(NotificationChannel channel) {
967 if (channel != null) {
968 mChannel = channel;
Julia Reynolds22f02b32016-12-01 15:05:13 -0500969 calculateImportance();
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500970 calculateUserSentiment();
Julia Reynolds22f02b32016-12-01 15:05:13 -0500971 }
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400972 }
Julia Reynolds0c299d42016-11-15 14:37:04 -0500973
Julia Reynolds924eed12017-01-19 09:52:07 -0500974 public void setShowBadge(boolean showBadge) {
975 mShowBadge = showBadge;
976 }
977
Julia Reynolds4509ce72019-01-31 13:12:43 -0500978 public boolean canBubble() {
979 return mAllowBubble;
980 }
981
982 public void setAllowBubble(boolean allow) {
983 mAllowBubble = allow;
984 }
985
Julia Reynolds924eed12017-01-19 09:52:07 -0500986 public boolean canShowBadge() {
987 return mShowBadge;
988 }
989
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500990 public Light getLight() {
991 return mLight;
992 }
993
Julia Reynolds0c299d42016-11-15 14:37:04 -0500994 public Uri getSound() {
995 return mSound;
996 }
997
998 public long[] getVibration() {
999 return mVibration;
1000 }
1001
1002 public AudioAttributes getAudioAttributes() {
1003 return mAttributes;
1004 }
Julia Reynolds22f02b32016-12-01 15:05:13 -05001005
1006 public ArrayList<String> getPeopleOverride() {
1007 return mPeopleOverride;
1008 }
1009
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001010 public void setInterruptive(boolean interruptive) {
1011 mIsInterruptive = interruptive;
Julia Reynoldsd6ba35c2018-06-18 09:29:19 -04001012 final long now = System.currentTimeMillis();
1013 mInterruptionTimeMs = interruptive ? now : mInterruptionTimeMs;
1014
1015 if (interruptive) {
1016 MetricsLogger.action(getLogMaker()
1017 .setCategory(MetricsEvent.NOTIFICATION_INTERRUPTION)
1018 .setType(MetricsEvent.TYPE_OPEN)
1019 .addTaggedData(MetricsEvent.NOTIFICATION_SINCE_INTERRUPTION_MILLIS,
1020 getInterruptionMs(now)));
1021 MetricsLogger.histogram(mContext, "note_interruptive", getInterruptionMs(now));
1022 }
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001023 }
1024
Gus Prevasa3226492018-10-23 11:10:09 -04001025 public void setAudiblyAlerted(boolean audiblyAlerted) {
Gus Prevas7306b902018-12-11 10:57:06 -05001026 mLastAudiblyAlertedMs = audiblyAlerted ? System.currentTimeMillis() : -1;
Gus Prevasa3226492018-10-23 11:10:09 -04001027 }
1028
Julia Reynoldsb3c68ff2018-05-22 14:58:39 -04001029 public void setTextChanged(boolean textChanged) {
1030 mTextChanged = textChanged;
1031 }
1032
1033 public void setRecordedInterruption(boolean recorded) {
1034 mRecordedInterruption = recorded;
1035 }
1036
1037 public boolean hasRecordedInterruption() {
1038 return mRecordedInterruption;
1039 }
1040
Julia Reynolds1fac86e2018-03-07 08:30:37 -05001041 public boolean isInterruptive() {
1042 return mIsInterruptive;
1043 }
1044
Gus Prevas7306b902018-12-11 10:57:06 -05001045 /** Returns the time the notification audibly alerted the user. */
1046 public long getLastAudiblyAlertedMs() {
1047 return mLastAudiblyAlertedMs;
Gus Prevasa3226492018-10-23 11:10:09 -04001048 }
1049
Julia Reynolds22f02b32016-12-01 15:05:13 -05001050 protected void setPeopleOverride(ArrayList<String> people) {
1051 mPeopleOverride = people;
1052 }
1053
1054 public ArrayList<SnoozeCriterion> getSnoozeCriteria() {
1055 return mSnoozeCriteria;
1056 }
1057
1058 protected void setSnoozeCriteria(ArrayList<SnoozeCriterion> snoozeCriteria) {
1059 mSnoozeCriteria = snoozeCriteria;
1060 }
Chris Wren9eb5e102017-01-26 13:15:06 -05001061
Julia Reynoldsc65656a2018-02-12 09:55:14 -05001062 private void calculateUserSentiment() {
Rohan Shah590e1b22018-04-10 23:48:47 -04001063 if ((getChannel().getUserLockedFields() & USER_LOCKED_IMPORTANCE) != 0
1064 || mIsAppImportanceLocked) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -05001065 mUserSentiment = USER_SENTIMENT_POSITIVE;
1066 }
1067 }
1068
Julia Reynolds503ed942017-10-04 16:04:56 -04001069 private void setUserSentiment(int userSentiment) {
1070 mUserSentiment = userSentiment;
1071 }
1072
1073 public int getUserSentiment() {
1074 return mUserSentiment;
1075 }
1076
1077 public NotificationStats getStats() {
1078 return mStats;
1079 }
1080
1081 public void recordExpanded() {
1082 mStats.setExpanded();
1083 }
1084
1085 public void recordDirectReplied() {
1086 mStats.setDirectReplied();
1087 }
1088
1089 public void recordDismissalSurface(@NotificationStats.DismissalSurface int surface) {
1090 mStats.setDismissalSurface(surface);
1091 }
1092
Julia Reynoldsfd4099d2018-08-21 11:06:06 -04001093 public void recordDismissalSentiment(@NotificationStats.DismissalSentiment int sentiment) {
1094 mStats.setDismissalSentiment(sentiment);
1095 }
1096
Julia Reynolds503ed942017-10-04 16:04:56 -04001097 public void recordSnoozed() {
1098 mStats.setSnoozed();
1099 }
1100
1101 public void recordViewedSettings() {
1102 mStats.setViewedSettings();
1103 }
1104
Kenny Guy23991102018-04-05 21:18:38 +01001105 public void setNumSmartRepliesAdded(int noReplies) {
1106 mNumberOfSmartRepliesAdded = noReplies;
1107 }
1108
1109 public int getNumSmartRepliesAdded() {
1110 return mNumberOfSmartRepliesAdded;
1111 }
1112
Gustav Senntond25a64d2018-12-07 10:58:39 +00001113 public void setNumSmartActionsAdded(int noActions) {
1114 mNumberOfSmartActionsAdded = noActions;
1115 }
1116
1117 public int getNumSmartActionsAdded() {
1118 return mNumberOfSmartActionsAdded;
1119 }
1120
1121 public void setSuggestionsGeneratedByAssistant(boolean generatedByAssistant) {
1122 mSuggestionsGeneratedByAssistant = generatedByAssistant;
1123 }
1124
1125 public boolean getSuggestionsGeneratedByAssistant() {
1126 return mSuggestionsGeneratedByAssistant;
1127 }
1128
Milo Sredkov13d88112019-02-01 12:23:24 +00001129 public boolean getEditChoicesBeforeSending() {
1130 return mEditChoicesBeforeSending;
1131 }
1132
1133 public void setEditChoicesBeforeSending(boolean editChoicesBeforeSending) {
1134 mEditChoicesBeforeSending = editChoicesBeforeSending;
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
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05001263 @VisibleForTesting
1264 static final class Light {
1265 public final int color;
1266 public final int onMs;
1267 public final int offMs;
1268
1269 public Light(int color, int onMs, int offMs) {
1270 this.color = color;
1271 this.onMs = onMs;
1272 this.offMs = offMs;
1273 }
1274
1275 @Override
1276 public boolean equals(Object o) {
1277 if (this == o) return true;
1278 if (o == null || getClass() != o.getClass()) return false;
1279
1280 Light light = (Light) o;
1281
1282 if (color != light.color) return false;
1283 if (onMs != light.onMs) return false;
1284 return offMs == light.offMs;
1285
1286 }
1287
1288 @Override
1289 public int hashCode() {
1290 int result = color;
1291 result = 31 * result + onMs;
1292 result = 31 * result + offMs;
1293 return result;
1294 }
1295
1296 @Override
1297 public String toString() {
1298 return "Light{" +
1299 "color=" + color +
1300 ", onMs=" + onMs +
1301 ", offMs=" + offMs +
1302 '}';
1303 }
1304 }
Chris Wren333a61c2014-05-28 16:40:57 -04001305}