Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.server.notification; |
| 17 | |
Julia Reynolds | f0f629f | 2016-02-25 09:34:04 -0500 | [diff] [blame] | 18 | import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_MIN; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 19 | import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_UNSPECIFIED; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 20 | import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_DEFAULT; |
| 21 | import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_HIGH; |
| 22 | import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_LOW; |
| 23 | import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_MAX; |
| 24 | |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 25 | import android.app.Notification; |
| 26 | import android.content.Context; |
| 27 | import android.content.pm.PackageManager.NameNotFoundException; |
| 28 | import android.content.res.Resources; |
| 29 | import android.graphics.Bitmap; |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 30 | import android.graphics.drawable.Icon; |
John Spurlock | bfa5dc4 | 2014-07-28 23:30:45 -0400 | [diff] [blame] | 31 | import android.media.AudioAttributes; |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 32 | import android.os.UserHandle; |
Julia Reynolds | 5d25ee7 | 2015-11-20 15:38:20 -0500 | [diff] [blame] | 33 | import android.service.notification.NotificationListenerService; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 34 | import android.service.notification.StatusBarNotification; |
Julia Reynolds | f0f629f | 2016-02-25 09:34:04 -0500 | [diff] [blame] | 35 | import android.util.Log; |
John Spurlock | bfa5dc4 | 2014-07-28 23:30:45 -0400 | [diff] [blame] | 36 | |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 37 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 38 | import com.android.server.EventLogTags; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 39 | |
| 40 | import java.io.PrintWriter; |
| 41 | import java.lang.reflect.Array; |
| 42 | import java.util.Arrays; |
John Spurlock | 312d1d0 | 2014-07-08 10:24:57 -0400 | [diff] [blame] | 43 | import java.util.Objects; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Holds data about notifications that should not be shared with the |
| 47 | * {@link android.service.notification.NotificationListenerService}s. |
| 48 | * |
| 49 | * <p>These objects should not be mutated unless the code is synchronized |
| 50 | * on {@link NotificationManagerService#mNotificationList}, and any |
| 51 | * modification should be followed by a sorting of that list.</p> |
| 52 | * |
| 53 | * <p>Is sortable by {@link NotificationComparator}.</p> |
| 54 | * |
| 55 | * {@hide} |
| 56 | */ |
| 57 | public final class NotificationRecord { |
Julia Reynolds | f0f629f | 2016-02-25 09:34:04 -0500 | [diff] [blame] | 58 | static final String TAG = "NotificationRecord"; |
| 59 | static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 60 | final StatusBarNotification sbn; |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 61 | final int mOriginalFlags; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 62 | private final Context mContext; |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 63 | |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 64 | NotificationUsageStats.SingleNotificationStats stats; |
| 65 | boolean isCanceled; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 66 | /** Whether the notification was seen by the user via one of the notification listeners. */ |
| 67 | boolean mIsSeen; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 68 | |
| 69 | // These members are used by NotificationSignalExtractors |
| 70 | // to communicate with the ranking module. |
| 71 | private float mContactAffinity; |
| 72 | private boolean mRecentlyIntrusive; |
| 73 | |
| 74 | // is this notification currently being intercepted by Zen Mode? |
| 75 | private boolean mIntercept; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 76 | |
Christoph Studer | 52b7a5a | 2014-06-06 16:09:15 +0200 | [diff] [blame] | 77 | // The timestamp used for ranking. |
| 78 | private long mRankingTimeMs; |
| 79 | |
Chris Wren | 640e387 | 2015-04-21 13:23:18 -0400 | [diff] [blame] | 80 | // The first post time, stable across updates. |
| 81 | private long mCreationTimeMs; |
| 82 | |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 83 | // The most recent visibility event. |
| 84 | private long mVisibleSinceMs; |
| 85 | |
| 86 | // The most recent update time, or the creation time if no updates. |
| 87 | private long mUpdateTimeMs; |
| 88 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 89 | // Is this record an update of an old record? |
| 90 | public boolean isUpdate; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 91 | private int mPackagePriority; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 92 | |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 93 | private int mAuthoritativeRank; |
Christoph Studer | cd4adf8 | 2014-08-19 17:50:49 +0200 | [diff] [blame] | 94 | private String mGlobalSortKey; |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 95 | private int mPackageVisibility; |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 96 | private int mUserImportance = IMPORTANCE_UNSPECIFIED; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 97 | private int mImportance = IMPORTANCE_UNSPECIFIED; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 98 | private CharSequence mImportanceExplanation = null; |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 99 | |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 100 | private int mSuppressedVisualEffects = 0; |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 101 | private String mUserExplanation; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 102 | private String mPeopleExplanation; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 103 | |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 104 | @VisibleForTesting |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 105 | public NotificationRecord(Context context, StatusBarNotification sbn) |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 106 | { |
| 107 | this.sbn = sbn; |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 108 | mOriginalFlags = sbn.getNotification().flags; |
Christoph Studer | 52b7a5a | 2014-06-06 16:09:15 +0200 | [diff] [blame] | 109 | mRankingTimeMs = calculateRankingTimeMs(0L); |
Chris Wren | 640e387 | 2015-04-21 13:23:18 -0400 | [diff] [blame] | 110 | mCreationTimeMs = sbn.getPostTime(); |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 111 | mUpdateTimeMs = mCreationTimeMs; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 112 | mContext = context; |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 113 | stats = new NotificationUsageStats.SingleNotificationStats(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 114 | mImportance = defaultImportance(); |
| 115 | } |
| 116 | |
| 117 | private int defaultImportance() { |
| 118 | final Notification n = sbn.getNotification(); |
| 119 | int importance = IMPORTANCE_DEFAULT; |
| 120 | |
| 121 | // Migrate notification flags to scores |
| 122 | if (0 != (n.flags & Notification.FLAG_HIGH_PRIORITY)) { |
| 123 | n.priority = Notification.PRIORITY_MAX; |
| 124 | } |
| 125 | |
| 126 | switch (n.priority) { |
| 127 | case Notification.PRIORITY_MIN: |
Julia Reynolds | f0f629f | 2016-02-25 09:34:04 -0500 | [diff] [blame] | 128 | importance = IMPORTANCE_MIN; |
| 129 | break; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 130 | case Notification.PRIORITY_LOW: |
| 131 | importance = IMPORTANCE_LOW; |
| 132 | break; |
| 133 | case Notification.PRIORITY_DEFAULT: |
| 134 | importance = IMPORTANCE_DEFAULT; |
| 135 | break; |
| 136 | case Notification.PRIORITY_HIGH: |
| 137 | importance = IMPORTANCE_HIGH; |
| 138 | break; |
| 139 | case Notification.PRIORITY_MAX: |
| 140 | importance = IMPORTANCE_MAX; |
| 141 | break; |
| 142 | } |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 143 | stats.requestedImportance = importance; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 144 | |
| 145 | boolean isNoisy = (n.defaults & Notification.DEFAULT_SOUND) != 0 |
| 146 | || (n.defaults & Notification.DEFAULT_VIBRATE) != 0 |
| 147 | || n.sound != null |
| 148 | || n.vibrate != null; |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 149 | stats.isNoisy = isNoisy; |
Julia Reynolds | f0f629f | 2016-02-25 09:34:04 -0500 | [diff] [blame] | 150 | |
| 151 | if (!isNoisy && importance > IMPORTANCE_LOW) { |
| 152 | importance = IMPORTANCE_LOW; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 153 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 154 | |
Julia Reynolds | f0f629f | 2016-02-25 09:34:04 -0500 | [diff] [blame] | 155 | if (isNoisy) { |
| 156 | if (importance < IMPORTANCE_DEFAULT) { |
| 157 | importance = IMPORTANCE_DEFAULT; |
Julia Reynolds | 83fa107 | 2016-02-17 09:10:19 -0500 | [diff] [blame] | 158 | } |
Julia Reynolds | 83fa107 | 2016-02-17 09:10:19 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 161 | if (n.fullScreenIntent != null) { |
| 162 | importance = IMPORTANCE_MAX; |
| 163 | } |
| 164 | |
Chris Wren | cdee8cd | 2016-01-25 17:10:30 -0500 | [diff] [blame] | 165 | stats.naturalImportance = importance; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 166 | return importance; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // copy any notes that the ranking system may have made before the update |
| 170 | public void copyRankingInformation(NotificationRecord previous) { |
| 171 | mContactAffinity = previous.mContactAffinity; |
| 172 | mRecentlyIntrusive = previous.mRecentlyIntrusive; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 173 | mPackagePriority = previous.mPackagePriority; |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 174 | mPackageVisibility = previous.mPackageVisibility; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 175 | mIntercept = previous.mIntercept; |
Christoph Studer | 52b7a5a | 2014-06-06 16:09:15 +0200 | [diff] [blame] | 176 | mRankingTimeMs = calculateRankingTimeMs(previous.getRankingTimeMs()); |
Chris Wren | 640e387 | 2015-04-21 13:23:18 -0400 | [diff] [blame] | 177 | mCreationTimeMs = previous.mCreationTimeMs; |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 178 | mVisibleSinceMs = previous.mVisibleSinceMs; |
Selim Cinek | 5b03ce9 | 2016-05-18 15:16:58 -0700 | [diff] [blame] | 179 | if (previous.sbn.getOverrideGroupKey() != null && !sbn.isAppGroup()) { |
Chris Wren | 8a1638f | 2016-05-02 16:19:14 -0400 | [diff] [blame] | 180 | sbn.setOverrideGroupKey(previous.sbn.getOverrideGroupKey()); |
| 181 | } |
Chris Wren | 1f602dc | 2016-04-11 10:33:46 -0400 | [diff] [blame] | 182 | // Don't copy importance information or mGlobalSortKey, recompute them. |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | public Notification getNotification() { return sbn.getNotification(); } |
| 186 | public int getFlags() { return sbn.getNotification().flags; } |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 187 | public UserHandle getUser() { return sbn.getUser(); } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 188 | public String getKey() { return sbn.getKey(); } |
Chris Wren | da4bd20 | 2014-09-04 15:53:52 -0400 | [diff] [blame] | 189 | /** @deprecated Use {@link #getUser()} instead. */ |
| 190 | public int getUserId() { return sbn.getUserId(); } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 191 | |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 192 | void dump(PrintWriter pw, String prefix, Context baseContext, boolean redact) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 193 | final Notification notification = sbn.getNotification(); |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 194 | final Icon icon = notification.getSmallIcon(); |
| 195 | String iconStr = String.valueOf(icon); |
| 196 | if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) { |
| 197 | iconStr += " / " + idDebugString(baseContext, icon.getResPackage(), icon.getResId()); |
| 198 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 199 | pw.println(prefix + this); |
| 200 | pw.println(prefix + " uid=" + sbn.getUid() + " userId=" + sbn.getUserId()); |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 201 | pw.println(prefix + " icon=" + iconStr); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 202 | pw.println(prefix + " pri=" + notification.priority); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 203 | pw.println(prefix + " key=" + sbn.getKey()); |
Amith Yamasani | 24bd0be | 2015-04-28 10:28:09 -0700 | [diff] [blame] | 204 | pw.println(prefix + " seen=" + mIsSeen); |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 205 | pw.println(prefix + " groupKey=" + getGroupKey()); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 206 | pw.println(prefix + " contentIntent=" + notification.contentIntent); |
| 207 | pw.println(prefix + " deleteIntent=" + notification.deleteIntent); |
| 208 | pw.println(prefix + " tickerText=" + notification.tickerText); |
| 209 | pw.println(prefix + " contentView=" + notification.contentView); |
| 210 | pw.println(prefix + String.format(" defaults=0x%08x flags=0x%08x", |
| 211 | notification.defaults, notification.flags)); |
| 212 | pw.println(prefix + " sound=" + notification.sound); |
John Spurlock | bfa5dc4 | 2014-07-28 23:30:45 -0400 | [diff] [blame] | 213 | pw.println(prefix + " audioStreamType=" + notification.audioStreamType); |
| 214 | pw.println(prefix + " audioAttributes=" + notification.audioAttributes); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 215 | pw.println(prefix + String.format(" color=0x%08x", notification.color)); |
| 216 | pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate)); |
| 217 | pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d", |
| 218 | notification.ledARGB, notification.ledOnMS, notification.ledOffMS)); |
| 219 | if (notification.actions != null && notification.actions.length > 0) { |
| 220 | pw.println(prefix + " actions={"); |
| 221 | final int N = notification.actions.length; |
| 222 | for (int i=0; i<N; i++) { |
| 223 | final Notification.Action action = notification.actions[i]; |
Chris Wren | 1ac52a9 | 2016-02-24 14:54:52 -0500 | [diff] [blame] | 224 | if (action != null) { |
| 225 | pw.println(String.format("%s [%d] \"%s\" -> %s", |
| 226 | prefix, |
| 227 | i, |
| 228 | action.title, |
| 229 | action.actionIntent == null ? "null" : action.actionIntent.toString() |
| 230 | )); |
| 231 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 232 | } |
| 233 | pw.println(prefix + " }"); |
| 234 | } |
| 235 | if (notification.extras != null && notification.extras.size() > 0) { |
| 236 | pw.println(prefix + " extras={"); |
| 237 | for (String key : notification.extras.keySet()) { |
| 238 | pw.print(prefix + " " + key + "="); |
| 239 | Object val = notification.extras.get(key); |
| 240 | if (val == null) { |
| 241 | pw.println("null"); |
| 242 | } else { |
| 243 | pw.print(val.getClass().getSimpleName()); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 244 | if (redact && (val instanceof CharSequence || val instanceof String)) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 245 | // redact contents from bugreports |
| 246 | } else if (val instanceof Bitmap) { |
| 247 | pw.print(String.format(" (%dx%d)", |
| 248 | ((Bitmap) val).getWidth(), |
| 249 | ((Bitmap) val).getHeight())); |
| 250 | } else if (val.getClass().isArray()) { |
| 251 | final int N = Array.getLength(val); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 252 | pw.print(" (" + N + ")"); |
| 253 | if (!redact) { |
| 254 | for (int j=0; j<N; j++) { |
| 255 | pw.println(); |
| 256 | pw.print(String.format("%s [%d] %s", |
| 257 | prefix, j, String.valueOf(Array.get(val, j)))); |
| 258 | } |
| 259 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 260 | } else { |
| 261 | pw.print(" (" + String.valueOf(val) + ")"); |
| 262 | } |
| 263 | pw.println(); |
| 264 | } |
| 265 | } |
| 266 | pw.println(prefix + " }"); |
| 267 | } |
| 268 | pw.println(prefix + " stats=" + stats.toString()); |
| 269 | pw.println(prefix + " mContactAffinity=" + mContactAffinity); |
| 270 | pw.println(prefix + " mRecentlyIntrusive=" + mRecentlyIntrusive); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 271 | pw.println(prefix + " mPackagePriority=" + mPackagePriority); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 272 | pw.println(prefix + " mPackageVisibility=" + mPackageVisibility); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 273 | pw.println(prefix + " mUserImportance=" |
| 274 | + NotificationListenerService.Ranking.importanceToString(mUserImportance)); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 275 | pw.println(prefix + " mImportance=" |
| 276 | + NotificationListenerService.Ranking.importanceToString(mImportance)); |
| 277 | pw.println(prefix + " mImportanceExplanation=" + mImportanceExplanation); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 278 | pw.println(prefix + " mIntercept=" + mIntercept); |
Christoph Studer | cd4adf8 | 2014-08-19 17:50:49 +0200 | [diff] [blame] | 279 | pw.println(prefix + " mGlobalSortKey=" + mGlobalSortKey); |
Christoph Studer | 52b7a5a | 2014-06-06 16:09:15 +0200 | [diff] [blame] | 280 | pw.println(prefix + " mRankingTimeMs=" + mRankingTimeMs); |
Chris Wren | 640e387 | 2015-04-21 13:23:18 -0400 | [diff] [blame] | 281 | pw.println(prefix + " mCreationTimeMs=" + mCreationTimeMs); |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 282 | pw.println(prefix + " mVisibleSinceMs=" + mVisibleSinceMs); |
| 283 | pw.println(prefix + " mUpdateTimeMs=" + mUpdateTimeMs); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 284 | pw.println(prefix + " mSuppressedVisualEffects= " + mSuppressedVisualEffects); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
| 288 | static String idDebugString(Context baseContext, String packageName, int id) { |
| 289 | Context c; |
| 290 | |
| 291 | if (packageName != null) { |
| 292 | try { |
| 293 | c = baseContext.createPackageContext(packageName, 0); |
| 294 | } catch (NameNotFoundException e) { |
| 295 | c = baseContext; |
| 296 | } |
| 297 | } else { |
| 298 | c = baseContext; |
| 299 | } |
| 300 | |
| 301 | Resources r = c.getResources(); |
| 302 | try { |
| 303 | return r.getResourceName(id); |
| 304 | } catch (Resources.NotFoundException e) { |
| 305 | return "<name unknown>"; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public final String toString() { |
| 311 | return String.format( |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 312 | "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s importance=%d key=%s: %s)", |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 313 | System.identityHashCode(this), |
| 314 | this.sbn.getPackageName(), this.sbn.getUser(), this.sbn.getId(), |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 315 | this.sbn.getTag(), this.mImportance, this.sbn.getKey(), |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 316 | this.sbn.getNotification()); |
| 317 | } |
| 318 | |
| 319 | public void setContactAffinity(float contactAffinity) { |
| 320 | mContactAffinity = contactAffinity; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 321 | if (mImportance < IMPORTANCE_DEFAULT && |
| 322 | mContactAffinity > ValidateNotificationPeople.VALID_CONTACT) { |
| 323 | setImportance(IMPORTANCE_DEFAULT, getPeopleExplanation()); |
| 324 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | public float getContactAffinity() { |
| 328 | return mContactAffinity; |
| 329 | } |
| 330 | |
John Spurlock | 1d881a1 | 2015-03-18 19:21:54 -0400 | [diff] [blame] | 331 | public void setRecentlyIntrusive(boolean recentlyIntrusive) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 332 | mRecentlyIntrusive = recentlyIntrusive; |
| 333 | } |
| 334 | |
| 335 | public boolean isRecentlyIntrusive() { |
| 336 | return mRecentlyIntrusive; |
| 337 | } |
| 338 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 339 | public void setPackagePriority(int packagePriority) { |
John Spurlock | 6ac5f8d | 2014-07-18 11:27:54 -0400 | [diff] [blame] | 340 | mPackagePriority = packagePriority; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | public int getPackagePriority() { |
| 344 | return mPackagePriority; |
| 345 | } |
| 346 | |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 347 | public void setPackageVisibilityOverride(int packageVisibility) { |
| 348 | mPackageVisibility = packageVisibility; |
| 349 | } |
| 350 | |
| 351 | public int getPackageVisibilityOverride() { |
| 352 | return mPackageVisibility; |
| 353 | } |
| 354 | |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 355 | public void setUserImportance(int importance) { |
| 356 | mUserImportance = importance; |
| 357 | applyUserImportance(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 358 | } |
| 359 | |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 360 | private String getUserExplanation() { |
| 361 | if (mUserExplanation == null) { |
| 362 | mUserExplanation = |
| 363 | mContext.getString(com.android.internal.R.string.importance_from_user); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 364 | } |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 365 | return mUserExplanation; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | private String getPeopleExplanation() { |
| 369 | if (mPeopleExplanation == null) { |
| 370 | mPeopleExplanation = |
| 371 | mContext.getString(com.android.internal.R.string.importance_from_person); |
| 372 | } |
| 373 | return mPeopleExplanation; |
| 374 | } |
| 375 | |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 376 | private void applyUserImportance() { |
| 377 | if (mUserImportance != NotificationListenerService.Ranking.IMPORTANCE_UNSPECIFIED) { |
| 378 | mImportance = mUserImportance; |
| 379 | mImportanceExplanation = getUserExplanation(); |
Julia Reynolds | 5d25ee7 | 2015-11-20 15:38:20 -0500 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 383 | public int getUserImportance() { |
| 384 | return mUserImportance; |
Julia Reynolds | 5d25ee7 | 2015-11-20 15:38:20 -0500 | [diff] [blame] | 385 | } |
| 386 | |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 387 | public void setImportance(int importance, CharSequence explanation) { |
| 388 | if (importance != NotificationListenerService.Ranking.IMPORTANCE_UNSPECIFIED) { |
| 389 | mImportance = importance; |
| 390 | mImportanceExplanation = explanation; |
| 391 | } |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 392 | applyUserImportance(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | public int getImportance() { |
| 396 | return mImportance; |
| 397 | } |
| 398 | |
| 399 | public CharSequence getImportanceExplanation() { |
| 400 | return mImportanceExplanation; |
| 401 | } |
| 402 | |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 403 | public boolean setIntercepted(boolean intercept) { |
| 404 | mIntercept = intercept; |
| 405 | return mIntercept; |
| 406 | } |
| 407 | |
| 408 | public boolean isIntercepted() { |
| 409 | return mIntercept; |
| 410 | } |
| 411 | |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 412 | public void setSuppressedVisualEffects(int effects) { |
| 413 | mSuppressedVisualEffects = effects; |
| 414 | } |
| 415 | |
| 416 | public int getSuppressedVisualEffects() { |
| 417 | return mSuppressedVisualEffects; |
| 418 | } |
| 419 | |
John Spurlock | 312d1d0 | 2014-07-08 10:24:57 -0400 | [diff] [blame] | 420 | public boolean isCategory(String category) { |
John Spurlock | bfa5dc4 | 2014-07-28 23:30:45 -0400 | [diff] [blame] | 421 | return Objects.equals(getNotification().category, category); |
| 422 | } |
| 423 | |
| 424 | public boolean isAudioStream(int stream) { |
| 425 | return getNotification().audioStreamType == stream; |
| 426 | } |
| 427 | |
| 428 | public boolean isAudioAttributesUsage(int usage) { |
| 429 | final AudioAttributes attributes = getNotification().audioAttributes; |
| 430 | return attributes != null && attributes.getUsage() == usage; |
John Spurlock | 312d1d0 | 2014-07-08 10:24:57 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Christoph Studer | 52b7a5a | 2014-06-06 16:09:15 +0200 | [diff] [blame] | 433 | /** |
| 434 | * Returns the timestamp to use for time-based sorting in the ranker. |
| 435 | */ |
| 436 | public long getRankingTimeMs() { |
| 437 | return mRankingTimeMs; |
| 438 | } |
| 439 | |
| 440 | /** |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 441 | * @param now this current time in milliseconds. |
| 442 | * @returns the number of milliseconds since the most recent update, or the post time if none. |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 443 | */ |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 444 | public int getFreshnessMs(long now) { |
| 445 | return (int) (now - mUpdateTimeMs); |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /** |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 449 | * @param now this current time in milliseconds. |
| 450 | * @returns the number of milliseconds since the the first post, ignoring updates. |
Chris Wren | 640e387 | 2015-04-21 13:23:18 -0400 | [diff] [blame] | 451 | */ |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 452 | public int getLifespanMs(long now) { |
| 453 | return (int) (now - mCreationTimeMs); |
Chris Wren | 640e387 | 2015-04-21 13:23:18 -0400 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /** |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 457 | * @param now this current time in milliseconds. |
| 458 | * @returns the number of milliseconds since the most recent visibility event, or 0 if never. |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 459 | */ |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 460 | public int getExposureMs(long now) { |
| 461 | return mVisibleSinceMs == 0 ? 0 : (int) (now - mVisibleSinceMs); |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Set the visibility of the notification. |
| 466 | */ |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 467 | public void setVisibility(boolean visible, int rank) { |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 468 | final long now = System.currentTimeMillis(); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 469 | mVisibleSinceMs = visible ? now : mVisibleSinceMs; |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 470 | stats.onVisibilityChanged(visible); |
| 471 | EventLogTags.writeNotificationVisibility(getKey(), visible ? 1 : 0, |
| 472 | (int) (now - mCreationTimeMs), |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 473 | (int) (now - mUpdateTimeMs), |
| 474 | 0, // exposure time |
| 475 | rank); |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /** |
Christoph Studer | 52b7a5a | 2014-06-06 16:09:15 +0200 | [diff] [blame] | 479 | * @param previousRankingTimeMs for updated notifications, {@link #getRankingTimeMs()} |
| 480 | * of the previous notification record, 0 otherwise |
| 481 | */ |
| 482 | private long calculateRankingTimeMs(long previousRankingTimeMs) { |
| 483 | Notification n = getNotification(); |
| 484 | // Take developer provided 'when', unless it's in the future. |
| 485 | if (n.when != 0 && n.when <= sbn.getPostTime()) { |
| 486 | return n.when; |
| 487 | } |
| 488 | // If we've ranked a previous instance with a timestamp, inherit it. This case is |
| 489 | // important in order to have ranking stability for updating notifications. |
| 490 | if (previousRankingTimeMs > 0) { |
| 491 | return previousRankingTimeMs; |
| 492 | } |
| 493 | return sbn.getPostTime(); |
| 494 | } |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 495 | |
Christoph Studer | cd4adf8 | 2014-08-19 17:50:49 +0200 | [diff] [blame] | 496 | public void setGlobalSortKey(String globalSortKey) { |
| 497 | mGlobalSortKey = globalSortKey; |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Christoph Studer | cd4adf8 | 2014-08-19 17:50:49 +0200 | [diff] [blame] | 500 | public String getGlobalSortKey() { |
| 501 | return mGlobalSortKey; |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 504 | /** Check if any of the listeners have marked this notification as seen by the user. */ |
| 505 | public boolean isSeen() { |
| 506 | return mIsSeen; |
| 507 | } |
| 508 | |
| 509 | /** Mark the notification as seen by the user. */ |
| 510 | public void setSeen() { |
| 511 | mIsSeen = true; |
| 512 | } |
| 513 | |
Chris Wren | 1031c97 | 2014-07-23 13:11:45 +0000 | [diff] [blame] | 514 | public void setAuthoritativeRank(int authoritativeRank) { |
| 515 | mAuthoritativeRank = authoritativeRank; |
| 516 | } |
| 517 | |
| 518 | public int getAuthoritativeRank() { |
| 519 | return mAuthoritativeRank; |
| 520 | } |
| 521 | |
| 522 | public String getGroupKey() { |
| 523 | return sbn.getGroupKey(); |
| 524 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 525 | |
| 526 | public boolean isImportanceFromUser() { |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 527 | return mImportance == mUserImportance; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 528 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 529 | } |