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