NotificationRecord.java revision bfa5dc4c6c486bdabadb5ea2e356a7d348e3b975
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 */ 16package com.android.server.notification; 17 18import android.app.Notification; 19import android.content.Context; 20import android.content.pm.PackageManager.NameNotFoundException; 21import android.content.res.Resources; 22import android.graphics.Bitmap; 23import android.media.AudioAttributes; 24import android.service.notification.StatusBarNotification; 25 26import com.android.internal.annotations.VisibleForTesting; 27 28import java.io.PrintWriter; 29import java.lang.reflect.Array; 30import java.util.Arrays; 31import java.util.Objects; 32 33/** 34 * Holds data about notifications that should not be shared with the 35 * {@link android.service.notification.NotificationListenerService}s. 36 * 37 * <p>These objects should not be mutated unless the code is synchronized 38 * on {@link NotificationManagerService#mNotificationList}, and any 39 * modification should be followed by a sorting of that list.</p> 40 * 41 * <p>Is sortable by {@link NotificationComparator}.</p> 42 * 43 * {@hide} 44 */ 45public final class NotificationRecord { 46 final StatusBarNotification sbn; 47 NotificationUsageStats.SingleNotificationStats stats; 48 boolean isCanceled; 49 int score; 50 51 // These members are used by NotificationSignalExtractors 52 // to communicate with the ranking module. 53 private float mContactAffinity; 54 private boolean mRecentlyIntrusive; 55 56 // is this notification currently being intercepted by Zen Mode? 57 private boolean mIntercept; 58 59 // The timestamp used for ranking. 60 private long mRankingTimeMs; 61 62 // Is this record an update of an old record? 63 public boolean isUpdate; 64 private int mPackagePriority; 65 66 // The record that ranking should use for comparisons outside the group. 67 private NotificationRecord mRankingProxy; 68 private int mAuthoritativeRank; 69 70 @VisibleForTesting 71 public NotificationRecord(StatusBarNotification sbn, int score) 72 { 73 this.sbn = sbn; 74 this.score = score; 75 mRankingTimeMs = calculateRankingTimeMs(0L); 76 } 77 78 // copy any notes that the ranking system may have made before the update 79 public void copyRankingInformation(NotificationRecord previous) { 80 mContactAffinity = previous.mContactAffinity; 81 mRecentlyIntrusive = previous.mRecentlyIntrusive; 82 mPackagePriority = previous.mPackagePriority; 83 mIntercept = previous.mIntercept; 84 mRankingProxy = previous.mRankingProxy; 85 mRankingTimeMs = calculateRankingTimeMs(previous.getRankingTimeMs()); 86 // Don't copy mGroupKey, recompute it, in case it has changed 87 } 88 89 public Notification getNotification() { return sbn.getNotification(); } 90 public int getFlags() { return sbn.getNotification().flags; } 91 public int getUserId() { return sbn.getUserId(); } 92 public String getKey() { return sbn.getKey(); } 93 94 void dump(PrintWriter pw, String prefix, Context baseContext) { 95 final Notification notification = sbn.getNotification(); 96 pw.println(prefix + this); 97 pw.println(prefix + " uid=" + sbn.getUid() + " userId=" + sbn.getUserId()); 98 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon) 99 + " / " + idDebugString(baseContext, sbn.getPackageName(), notification.icon)); 100 pw.println(prefix + " pri=" + notification.priority + " score=" + sbn.getScore()); 101 pw.println(prefix + " key=" + sbn.getKey()); 102 pw.println(prefix + " groupKey=" + getGroupKey()); 103 pw.println(prefix + " contentIntent=" + notification.contentIntent); 104 pw.println(prefix + " deleteIntent=" + notification.deleteIntent); 105 pw.println(prefix + " tickerText=" + notification.tickerText); 106 pw.println(prefix + " contentView=" + notification.contentView); 107 pw.println(prefix + String.format(" defaults=0x%08x flags=0x%08x", 108 notification.defaults, notification.flags)); 109 pw.println(prefix + " sound=" + notification.sound); 110 pw.println(prefix + " audioStreamType=" + notification.audioStreamType); 111 pw.println(prefix + " audioAttributes=" + notification.audioAttributes); 112 pw.println(prefix + String.format(" color=0x%08x", notification.color)); 113 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate)); 114 pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d", 115 notification.ledARGB, notification.ledOnMS, notification.ledOffMS)); 116 if (notification.actions != null && notification.actions.length > 0) { 117 pw.println(prefix + " actions={"); 118 final int N = notification.actions.length; 119 for (int i=0; i<N; i++) { 120 final Notification.Action action = notification.actions[i]; 121 pw.println(String.format("%s [%d] \"%s\" -> %s", 122 prefix, 123 i, 124 action.title, 125 action.actionIntent.toString() 126 )); 127 } 128 pw.println(prefix + " }"); 129 } 130 if (notification.extras != null && notification.extras.size() > 0) { 131 pw.println(prefix + " extras={"); 132 for (String key : notification.extras.keySet()) { 133 pw.print(prefix + " " + key + "="); 134 Object val = notification.extras.get(key); 135 if (val == null) { 136 pw.println("null"); 137 } else { 138 pw.print(val.getClass().getSimpleName()); 139 if (val instanceof CharSequence || val instanceof String) { 140 // redact contents from bugreports 141 } else if (val instanceof Bitmap) { 142 pw.print(String.format(" (%dx%d)", 143 ((Bitmap) val).getWidth(), 144 ((Bitmap) val).getHeight())); 145 } else if (val.getClass().isArray()) { 146 final int N = Array.getLength(val); 147 pw.println(" (" + N + ")"); 148 } else { 149 pw.print(" (" + String.valueOf(val) + ")"); 150 } 151 pw.println(); 152 } 153 } 154 pw.println(prefix + " }"); 155 } 156 pw.println(prefix + " stats=" + stats.toString()); 157 pw.println(prefix + " mContactAffinity=" + mContactAffinity); 158 pw.println(prefix + " mRecentlyIntrusive=" + mRecentlyIntrusive); 159 pw.println(prefix + " mPackagePriority=" + mPackagePriority); 160 pw.println(prefix + " mIntercept=" + mIntercept); 161 pw.println(prefix + " mRankingProxy=" + getRankingProxy().getKey()); 162 pw.println(prefix + " mRankingTimeMs=" + mRankingTimeMs); 163 } 164 165 166 static String idDebugString(Context baseContext, String packageName, int id) { 167 Context c; 168 169 if (packageName != null) { 170 try { 171 c = baseContext.createPackageContext(packageName, 0); 172 } catch (NameNotFoundException e) { 173 c = baseContext; 174 } 175 } else { 176 c = baseContext; 177 } 178 179 Resources r = c.getResources(); 180 try { 181 return r.getResourceName(id); 182 } catch (Resources.NotFoundException e) { 183 return "<name unknown>"; 184 } 185 } 186 187 @Override 188 public final String toString() { 189 return String.format( 190 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s score=%d key=%s: %s)", 191 System.identityHashCode(this), 192 this.sbn.getPackageName(), this.sbn.getUser(), this.sbn.getId(), 193 this.sbn.getTag(), this.sbn.getScore(), this.sbn.getKey(), 194 this.sbn.getNotification()); 195 } 196 197 public void setContactAffinity(float contactAffinity) { 198 mContactAffinity = contactAffinity; 199 } 200 201 public float getContactAffinity() { 202 return mContactAffinity; 203 } 204 205 public void setRecentlyIntusive(boolean recentlyIntrusive) { 206 mRecentlyIntrusive = recentlyIntrusive; 207 } 208 209 public boolean isRecentlyIntrusive() { 210 return mRecentlyIntrusive; 211 } 212 213 public void setPackagePriority(int packagePriority) { 214 mPackagePriority = packagePriority; 215 } 216 217 public int getPackagePriority() { 218 return mPackagePriority; 219 } 220 221 public boolean setIntercepted(boolean intercept) { 222 mIntercept = intercept; 223 return mIntercept; 224 } 225 226 public boolean isIntercepted() { 227 return mIntercept; 228 } 229 230 public boolean isCategory(String category) { 231 return Objects.equals(getNotification().category, category); 232 } 233 234 public boolean isAudioStream(int stream) { 235 return getNotification().audioStreamType == stream; 236 } 237 238 public boolean isAudioAttributesUsage(int usage) { 239 final AudioAttributes attributes = getNotification().audioAttributes; 240 return attributes != null && attributes.getUsage() == usage; 241 } 242 243 /** 244 * Returns the timestamp to use for time-based sorting in the ranker. 245 */ 246 public long getRankingTimeMs() { 247 return mRankingTimeMs; 248 } 249 250 /** 251 * @param previousRankingTimeMs for updated notifications, {@link #getRankingTimeMs()} 252 * of the previous notification record, 0 otherwise 253 */ 254 private long calculateRankingTimeMs(long previousRankingTimeMs) { 255 Notification n = getNotification(); 256 // Take developer provided 'when', unless it's in the future. 257 if (n.when != 0 && n.when <= sbn.getPostTime()) { 258 return n.when; 259 } 260 // If we've ranked a previous instance with a timestamp, inherit it. This case is 261 // important in order to have ranking stability for updating notifications. 262 if (previousRankingTimeMs > 0) { 263 return previousRankingTimeMs; 264 } 265 return sbn.getPostTime(); 266 } 267 268 public NotificationRecord getRankingProxy() { 269 return mRankingProxy; 270 } 271 272 public void setRankingProxy(NotificationRecord proxy) { 273 mRankingProxy = proxy; 274 } 275 276 public void setAuthoritativeRank(int authoritativeRank) { 277 mAuthoritativeRank = authoritativeRank; 278 } 279 280 public int getAuthoritativeRank() { 281 return mAuthoritativeRank; 282 } 283 284 public String getGroupKey() { 285 return sbn.getGroupKey(); 286 } 287} 288