StatusBarNotification.java revision a4294297d46cc0b9f45897bc688c267502cce3ef
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.os.Parcel; 21import android.os.Parcelable; 22import android.os.UserHandle; 23 24/** 25 * Class encapsulating a Notification. Sent by the NotificationManagerService to clients including 26 * the status bar and any {@link android.service.notification.NotificationListenerService}s. 27 */ 28public class StatusBarNotification implements Parcelable { 29 private final String pkg; 30 private final int id; 31 private final String tag; 32 private final String key; 33 34 private final int uid; 35 private final String basePkg; 36 private final int initialPid; 37 private final Notification notification; 38 private final UserHandle user; 39 private final long postTime; 40 41 private final int score; 42 43 /** @hide */ 44 public StatusBarNotification(String pkg, int id, String tag, int uid, int initialPid, int score, 45 Notification notification, UserHandle user) { 46 this(pkg, null, id, tag, uid, initialPid, score, notification, user); 47 } 48 49 /** @hide */ 50 public StatusBarNotification(String pkg, String basePkg, int id, String tag, int uid, 51 int initialPid, int score, Notification notification, UserHandle user) { 52 this(pkg, basePkg, id, tag, uid, initialPid, score, notification, user, 53 System.currentTimeMillis()); 54 } 55 56 public StatusBarNotification(String pkg, String basePkg, int id, String tag, int uid, 57 int initialPid, int score, Notification notification, UserHandle user, 58 long postTime) { 59 if (pkg == null) throw new NullPointerException(); 60 if (notification == null) throw new NullPointerException(); 61 62 this.pkg = pkg; 63 this.basePkg = pkg; 64 this.id = id; 65 this.tag = tag; 66 this.uid = uid; 67 this.initialPid = initialPid; 68 this.score = score; 69 this.notification = notification; 70 this.user = user; 71 this.notification.setUser(user); 72 this.postTime = postTime; 73 this.key = key(); 74 } 75 76 public StatusBarNotification(Parcel in) { 77 this.pkg = in.readString(); 78 this.basePkg = in.readString(); 79 this.id = in.readInt(); 80 if (in.readInt() != 0) { 81 this.tag = in.readString(); 82 } else { 83 this.tag = null; 84 } 85 this.uid = in.readInt(); 86 this.initialPid = in.readInt(); 87 this.score = in.readInt(); 88 this.notification = new Notification(in); 89 this.user = UserHandle.readFromParcel(in); 90 this.notification.setUser(this.user); 91 this.postTime = in.readLong(); 92 this.key = key(); 93 } 94 95 private String key() { 96 return pkg + '|' + basePkg + '|' + id + '|' + tag + '|' + uid; 97 } 98 99 public void writeToParcel(Parcel out, int flags) { 100 out.writeString(this.pkg); 101 out.writeString(this.basePkg); 102 out.writeInt(this.id); 103 if (this.tag != null) { 104 out.writeInt(1); 105 out.writeString(this.tag); 106 } else { 107 out.writeInt(0); 108 } 109 out.writeInt(this.uid); 110 out.writeInt(this.initialPid); 111 out.writeInt(this.score); 112 this.notification.writeToParcel(out, flags); 113 user.writeToParcel(out, flags); 114 115 out.writeLong(this.postTime); 116 } 117 118 public int describeContents() { 119 return 0; 120 } 121 122 public static final Parcelable.Creator<StatusBarNotification> CREATOR 123 = new Parcelable.Creator<StatusBarNotification>() 124 { 125 public StatusBarNotification createFromParcel(Parcel parcel) 126 { 127 return new StatusBarNotification(parcel); 128 } 129 130 public StatusBarNotification[] newArray(int size) 131 { 132 return new StatusBarNotification[size]; 133 } 134 }; 135 136 /** 137 * @hide 138 */ 139 public StatusBarNotification cloneLight() { 140 final Notification no = new Notification(); 141 this.notification.cloneInto(no, false); // light copy 142 return new StatusBarNotification(this.pkg, this.basePkg, 143 this.id, this.tag, this.uid, this.initialPid, 144 this.score, no, this.user, this.postTime); 145 } 146 147 @Override 148 public StatusBarNotification clone() { 149 return new StatusBarNotification(this.pkg, this.basePkg, 150 this.id, this.tag, this.uid, this.initialPid, 151 this.score, this.notification.clone(), this.user, this.postTime); 152 } 153 154 @Override 155 public String toString() { 156 return String.format( 157 "StatusBarNotification(pkg=%s user=%s id=%d tag=%s score=%d key=%s: %s)", 158 this.pkg, this.user, this.id, this.tag, 159 this.score, this.key, this.notification); 160 } 161 162 /** Convenience method to check the notification's flags for 163 * {@link Notification#FLAG_ONGOING_EVENT}. 164 */ 165 public boolean isOngoing() { 166 return (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0; 167 } 168 169 /** Convenience method to check the notification's flags for 170 * either {@link Notification#FLAG_ONGOING_EVENT} or 171 * {@link Notification#FLAG_NO_CLEAR}. 172 */ 173 public boolean isClearable() { 174 return ((notification.flags & Notification.FLAG_ONGOING_EVENT) == 0) 175 && ((notification.flags & Notification.FLAG_NO_CLEAR) == 0); 176 } 177 178 /** Returns a userHandle for the instance of the app that posted this notification. */ 179 public int getUserId() { 180 return this.user.getIdentifier(); 181 } 182 183 /** The package of the app that posted the notification. */ 184 public String getPackageName() { 185 return pkg; 186 } 187 188 /** The id supplied to {@link android.app.NotificationManager#notify(int,Notification)}. */ 189 public int getId() { 190 return id; 191 } 192 193 /** The tag supplied to {@link android.app.NotificationManager#notify(int,Notification)}, 194 * or null if no tag was specified. */ 195 public String getTag() { 196 return tag; 197 } 198 199 /** The notifying app's calling uid. @hide */ 200 public int getUid() { 201 return uid; 202 } 203 204 /** The notifying app's base package. @hide */ 205 public String getBasePkg() { 206 return basePkg; 207 } 208 209 /** @hide */ 210 public int getInitialPid() { 211 return initialPid; 212 } 213 214 /** The {@link android.app.Notification} supplied to 215 * {@link android.app.NotificationManager#notify(int,Notification)}. */ 216 public Notification getNotification() { 217 return notification; 218 } 219 220 /** 221 * The {@link android.os.UserHandle} for whom this notification is intended. 222 * @hide 223 */ 224 public UserHandle getUser() { 225 return user; 226 } 227 228 /** The time (in {@link System#currentTimeMillis} time) the notification was posted, 229 * which may be different than {@link android.app.Notification#when}. 230 */ 231 public long getPostTime() { 232 return postTime; 233 } 234 235 /** @hide */ 236 public int getScore() { 237 return score; 238 } 239 240 /** 241 * A unique instance key for this notification record. 242 */ 243 public String getKey() { 244 return key; 245 } 246} 247