StatusBarNotification.java revision 5feceebb892d4cb5777cea3c6174b206705d456b
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 /** The package of the app that posted the notification. */ 30 public final String pkg; 31 /** The id supplied to {@link android.app.NotificationManager#notify}. */ 32 public final int id; 33 /** The tag supplied to {@link android.app.NotificationManager#notify}, or null if no tag 34 * was specified. */ 35 public final String tag; 36 37 /** The notifying app's calling uid. @hide */ 38 public final int uid; 39 /** The notifying app's base package. @hide */ 40 public final String basePkg; 41 /** @hide */ 42 public final int initialPid; 43 // TODO: make this field private and move callers to an accessor that 44 // ensures sourceUser is applied. 45 46 /** The {@link android.app.Notification} supplied to 47 * {@link android.app.NotificationManager#notify}. */ 48 public final Notification notification; 49 /** The {@link android.os.UserHandle} for whom this notification is intended. */ 50 public final UserHandle user; 51 /** The time (in {@link System#currentTimeMillis} time) the notification was posted, 52 * which may be different than {@link android.app.Notification#when}. 53 */ 54 public final long postTime; 55 56 /** @hide */ 57 public final int score; 58 59 /** This is temporarily needed for the JB MR1 PDK. 60 * @hide */ 61 @Deprecated 62 public StatusBarNotification(String pkg, int id, String tag, int uid, int initialPid, int score, 63 Notification notification) { 64 this(pkg, id, tag, uid, initialPid, score, notification, UserHandle.OWNER); 65 } 66 67 /** @hide */ 68 public StatusBarNotification(String pkg, int id, String tag, int uid, int initialPid, int score, 69 Notification notification, UserHandle user) { 70 this(pkg, null, id, tag, uid, initialPid, score, notification, user); 71 } 72 73 /** @hide */ 74 public StatusBarNotification(String pkg, String basePkg, int id, String tag, int uid, 75 int initialPid, int score, Notification notification, UserHandle user) { 76 this(pkg, basePkg, id, tag, uid, initialPid, score, notification, user, 77 System.currentTimeMillis()); 78 } 79 80 public StatusBarNotification(String pkg, String basePkg, int id, String tag, int uid, 81 int initialPid, int score, Notification notification, UserHandle user, 82 long postTime) { 83 if (pkg == null) throw new NullPointerException(); 84 if (notification == null) throw new NullPointerException(); 85 86 this.pkg = pkg; 87 this.basePkg = pkg; 88 this.id = id; 89 this.tag = tag; 90 this.uid = uid; 91 this.initialPid = initialPid; 92 this.score = score; 93 this.notification = notification; 94 this.user = user; 95 this.notification.setUser(user); 96 97 this.postTime = postTime; 98 } 99 100 public StatusBarNotification(Parcel in) { 101 this.pkg = in.readString(); 102 this.basePkg = in.readString(); 103 this.id = in.readInt(); 104 if (in.readInt() != 0) { 105 this.tag = in.readString(); 106 } else { 107 this.tag = null; 108 } 109 this.uid = in.readInt(); 110 this.initialPid = in.readInt(); 111 this.score = in.readInt(); 112 this.notification = new Notification(in); 113 this.user = UserHandle.readFromParcel(in); 114 this.notification.setUser(this.user); 115 this.postTime = in.readLong(); 116 } 117 118 public void writeToParcel(Parcel out, int flags) { 119 out.writeString(this.pkg); 120 out.writeString(this.basePkg); 121 out.writeInt(this.id); 122 if (this.tag != null) { 123 out.writeInt(1); 124 out.writeString(this.tag); 125 } else { 126 out.writeInt(0); 127 } 128 out.writeInt(this.uid); 129 out.writeInt(this.initialPid); 130 out.writeInt(this.score); 131 this.notification.writeToParcel(out, flags); 132 user.writeToParcel(out, flags); 133 134 out.writeLong(this.postTime); 135 } 136 137 public int describeContents() { 138 return 0; 139 } 140 141 public static final Parcelable.Creator<StatusBarNotification> CREATOR 142 = new Parcelable.Creator<StatusBarNotification>() 143 { 144 public StatusBarNotification createFromParcel(Parcel parcel) 145 { 146 return new StatusBarNotification(parcel); 147 } 148 149 public StatusBarNotification[] newArray(int size) 150 { 151 return new StatusBarNotification[size]; 152 } 153 }; 154 155 @Override 156 public StatusBarNotification clone() { 157 return new StatusBarNotification(this.pkg, this.basePkg, 158 this.id, this.tag, this.uid, this.initialPid, 159 this.score, this.notification.clone(), this.user, this.postTime); 160 } 161 162 @Override 163 public String toString() { 164 return String.format( 165 "StatusBarNotification(pkg=%s user=%s id=%d tag=%s score=%d: %s)", 166 this.pkg, this.user, this.id, this.tag, 167 this.score, this.notification); 168 } 169 170 /** Convenience method to check the notification's flags for 171 * {@link Notification#FLAG_ONGOING_EVENT}. 172 */ 173 public boolean isOngoing() { 174 return (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0; 175 } 176 177 /** Convenience method to check the notification's flags for 178 * either {@link Notification#FLAG_ONGOING_EVENT} or 179 * {@link Notification#FLAG_NO_CLEAR}. 180 */ 181 public boolean isClearable() { 182 return ((notification.flags & Notification.FLAG_ONGOING_EVENT) == 0) 183 && ((notification.flags & Notification.FLAG_NO_CLEAR) == 0); 184 } 185 186 /** Returns a userHandle for the instance of the app that posted this notification. */ 187 public int getUserId() { 188 return this.user.getIdentifier(); 189 } 190} 191