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