StatusBarNotification.java revision e46bb37acf6d3cfb9974672ace93f5381f70ad99
1/* 2 * Copyright (C) 2008 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 17package android.service.notification; 18 19import android.app.Notification; 20import android.content.Context; 21import android.content.pm.ApplicationInfo; 22import android.content.pm.PackageManager; 23import android.os.Parcel; 24import android.os.Parcelable; 25import android.os.UserHandle; 26 27/** 28 * Class encapsulating a Notification. Sent by the NotificationManagerService to clients including 29 * the status bar and any {@link android.service.notification.NotificationListenerService}s. 30 */ 31public class StatusBarNotification implements Parcelable { 32 private final String pkg; 33 private final int id; 34 private final String tag; 35 private final String key; 36 private String groupKey; 37 private String overrideGroupKey; 38 39 private final int uid; 40 private final String opPkg; 41 private final int initialPid; 42 private final Notification notification; 43 private final UserHandle user; 44 private final long postTime; 45 46 private Context mContext; // used for inflation & icon expansion 47 48 /** @hide */ 49 public StatusBarNotification(String pkg, String opPkg, int id, String tag, int uid, 50 int initialPid, int score, Notification notification, UserHandle user) { 51 this(pkg, opPkg, id, tag, uid, initialPid, score, notification, user, 52 System.currentTimeMillis()); 53 } 54 55 /** @hide */ 56 public StatusBarNotification(String pkg, String opPkg, int id, String tag, int uid, 57 int initialPid, Notification notification, UserHandle user, String overrideGroupKey, 58 long postTime) { 59 if (pkg == null) throw new NullPointerException(); 60 if (notification == null) throw new NullPointerException(); 61 62 this.pkg = pkg; 63 this.opPkg = opPkg; 64 this.id = id; 65 this.tag = tag; 66 this.uid = uid; 67 this.initialPid = initialPid; 68 this.notification = notification; 69 this.user = user; 70 this.postTime = postTime; 71 this.overrideGroupKey = overrideGroupKey; 72 this.key = key(); 73 this.groupKey = groupKey(); 74 } 75 76 public StatusBarNotification(String pkg, String opPkg, int id, String tag, int uid, 77 int initialPid, int score, Notification notification, UserHandle user, 78 long postTime) { 79 if (pkg == null) throw new NullPointerException(); 80 if (notification == null) throw new NullPointerException(); 81 82 this.pkg = pkg; 83 this.opPkg = opPkg; 84 this.id = id; 85 this.tag = tag; 86 this.uid = uid; 87 this.initialPid = initialPid; 88 this.notification = notification; 89 this.user = user; 90 this.postTime = postTime; 91 this.key = key(); 92 this.groupKey = groupKey(); 93 } 94 95 public StatusBarNotification(Parcel in) { 96 this.pkg = in.readString(); 97 this.opPkg = in.readString(); 98 this.id = in.readInt(); 99 if (in.readInt() != 0) { 100 this.tag = in.readString(); 101 } else { 102 this.tag = null; 103 } 104 this.uid = in.readInt(); 105 this.initialPid = in.readInt(); 106 this.notification = new Notification(in); 107 this.user = UserHandle.readFromParcel(in); 108 this.postTime = in.readLong(); 109 if (in.readInt() != 0) { 110 this.overrideGroupKey = in.readString(); 111 } else { 112 this.overrideGroupKey = null; 113 } 114 this.key = key(); 115 this.groupKey = groupKey(); 116 } 117 118 private String key() { 119 String sbnKey = user.getIdentifier() + "|" + pkg + "|" + id + "|" + tag + "|" + uid; 120 if (overrideGroupKey != null && getNotification().isGroupSummary()) { 121 sbnKey = sbnKey + "|" + overrideGroupKey; 122 } 123 return sbnKey; 124 } 125 126 private String groupKey() { 127 if (overrideGroupKey != null) { 128 return user.getIdentifier() + "|" + pkg + "|" + "g:" + overrideGroupKey; 129 } 130 final String group = getNotification().getGroup(); 131 final String sortKey = getNotification().getSortKey(); 132 if (group == null && sortKey == null) { 133 // a group of one 134 return key; 135 } 136 return user.getIdentifier() + "|" + pkg + "|" + 137 (group == null 138 ? "p:" + notification.priority 139 : "g:" + group); 140 } 141 142 /** 143 * Returns true if this notification is part of a group. 144 */ 145 public boolean isGroup() { 146 if (overrideGroupKey != null || getNotification().getGroup() != null 147 || getNotification().getSortKey() != null) { 148 return true; 149 } 150 return false; 151 } 152 153 public void writeToParcel(Parcel out, int flags) { 154 out.writeString(this.pkg); 155 out.writeString(this.opPkg); 156 out.writeInt(this.id); 157 if (this.tag != null) { 158 out.writeInt(1); 159 out.writeString(this.tag); 160 } else { 161 out.writeInt(0); 162 } 163 out.writeInt(this.uid); 164 out.writeInt(this.initialPid); 165 this.notification.writeToParcel(out, flags); 166 user.writeToParcel(out, flags); 167 168 out.writeLong(this.postTime); 169 if (this.overrideGroupKey != null) { 170 out.writeInt(1); 171 out.writeString(this.overrideGroupKey); 172 } else { 173 out.writeInt(0); 174 } 175 } 176 177 public int describeContents() { 178 return 0; 179 } 180 181 public static final Parcelable.Creator<StatusBarNotification> CREATOR 182 = new Parcelable.Creator<StatusBarNotification>() 183 { 184 public StatusBarNotification createFromParcel(Parcel parcel) 185 { 186 return new StatusBarNotification(parcel); 187 } 188 189 public StatusBarNotification[] newArray(int size) 190 { 191 return new StatusBarNotification[size]; 192 } 193 }; 194 195 /** 196 * @hide 197 */ 198 public StatusBarNotification cloneLight() { 199 final Notification no = new Notification(); 200 this.notification.cloneInto(no, false); // light copy 201 return new StatusBarNotification(this.pkg, this.opPkg, 202 this.id, this.tag, this.uid, this.initialPid, 203 no, this.user, this.overrideGroupKey, this.postTime); 204 } 205 206 @Override 207 public StatusBarNotification clone() { 208 return new StatusBarNotification(this.pkg, this.opPkg, 209 this.id, this.tag, this.uid, this.initialPid, 210 this.notification.clone(), this.user, this.overrideGroupKey, this.postTime); 211 } 212 213 @Override 214 public String toString() { 215 return String.format( 216 "StatusBarNotification(pkg=%s user=%s id=%d tag=%s key=%s: %s)", 217 this.pkg, this.user, this.id, this.tag, 218 this.key, this.notification); 219 } 220 221 /** Convenience method to check the notification's flags for 222 * {@link Notification#FLAG_ONGOING_EVENT}. 223 */ 224 public boolean isOngoing() { 225 return (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0; 226 } 227 228 /** Convenience method to check the notification's flags for 229 * either {@link Notification#FLAG_ONGOING_EVENT} or 230 * {@link Notification#FLAG_NO_CLEAR}. 231 */ 232 public boolean isClearable() { 233 return ((notification.flags & Notification.FLAG_ONGOING_EVENT) == 0) 234 && ((notification.flags & Notification.FLAG_NO_CLEAR) == 0); 235 } 236 237 /** 238 * Returns a userHandle for the instance of the app that posted this notification. 239 * 240 * @deprecated Use {@link #getUser()} instead. 241 */ 242 public int getUserId() { 243 return this.user.getIdentifier(); 244 } 245 246 /** The package of the app that posted the notification. */ 247 public String getPackageName() { 248 return pkg; 249 } 250 251 /** The id supplied to {@link android.app.NotificationManager#notify(int,Notification)}. */ 252 public int getId() { 253 return id; 254 } 255 256 /** The tag supplied to {@link android.app.NotificationManager#notify(int,Notification)}, 257 * or null if no tag was specified. */ 258 public String getTag() { 259 return tag; 260 } 261 262 /** The notifying app's calling uid. @hide */ 263 public int getUid() { 264 return uid; 265 } 266 267 /** The package used for AppOps tracking. @hide */ 268 public String getOpPkg() { 269 return opPkg; 270 } 271 272 /** @hide */ 273 public int getInitialPid() { 274 return initialPid; 275 } 276 277 /** The {@link android.app.Notification} supplied to 278 * {@link android.app.NotificationManager#notify(int,Notification)}. */ 279 public Notification getNotification() { 280 return notification; 281 } 282 283 /** 284 * The {@link android.os.UserHandle} for whom this notification is intended. 285 */ 286 public UserHandle getUser() { 287 return user; 288 } 289 290 /** The time (in {@link System#currentTimeMillis} time) the notification was posted, 291 * which may be different than {@link android.app.Notification#when}. 292 */ 293 public long getPostTime() { 294 return postTime; 295 } 296 297 /** 298 * A unique instance key for this notification record. 299 */ 300 public String getKey() { 301 return key; 302 } 303 304 /** 305 * A key that indicates the group with which this message ranks. 306 */ 307 public String getGroupKey() { 308 return groupKey; 309 } 310 311 /** 312 * Sets the override group key. 313 */ 314 public void setOverrideGroupKey(String overrideGroupKey) { 315 this.overrideGroupKey = overrideGroupKey; 316 groupKey = groupKey(); 317 } 318 319 /** 320 * Returns the override group key. 321 */ 322 public String getOverrideGroupKey() { 323 return overrideGroupKey; 324 } 325 326 /** 327 * @hide 328 */ 329 public Context getPackageContext(Context context) { 330 if (mContext == null) { 331 try { 332 ApplicationInfo ai = context.getPackageManager() 333 .getApplicationInfo(pkg, PackageManager.GET_UNINSTALLED_PACKAGES); 334 mContext = context.createApplicationContext(ai, 335 Context.CONTEXT_RESTRICTED); 336 } catch (PackageManager.NameNotFoundException e) { 337 mContext = null; 338 } 339 } 340 if (mContext == null) { 341 mContext = context; 342 } 343 return mContext; 344 } 345} 346