StatusBarNotification.java revision a263e4e438746f91fb78857bd569ba4f796a346d
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    /**
179     * Returns a userHandle for the instance of the app that posted this notification.
180     *
181     * @deprecated Use {@link #getUser()} instead.
182     */
183    public int getUserId() {
184        return this.user.getIdentifier();
185    }
186
187    /** The package of the app that posted the notification. */
188    public String getPackageName() {
189        return pkg;
190    }
191
192    /** The id supplied to {@link android.app.NotificationManager#notify(int,Notification)}. */
193    public int getId() {
194        return id;
195    }
196
197    /** The tag supplied to {@link android.app.NotificationManager#notify(int,Notification)},
198     * or null if no tag was specified. */
199    public String getTag() {
200        return tag;
201    }
202
203    /** The notifying app's calling uid. @hide */
204    public int getUid() {
205        return uid;
206    }
207
208    /** The notifying app's base package. @hide */
209    public String getBasePkg() {
210        return basePkg;
211    }
212
213    /** @hide */
214    public int getInitialPid() {
215        return initialPid;
216    }
217
218    /** The {@link android.app.Notification} supplied to
219     * {@link android.app.NotificationManager#notify(int,Notification)}. */
220    public Notification getNotification() {
221        return notification;
222    }
223
224    /**
225     * The {@link android.os.UserHandle} for whom this notification is intended.
226     */
227    public UserHandle getUser() {
228        return user;
229    }
230
231    /** The time (in {@link System#currentTimeMillis} time) the notification was posted,
232     * which may be different than {@link android.app.Notification#when}.
233     */
234    public long getPostTime() {
235        return postTime;
236    }
237
238    /** @hide */
239    public int getScore() {
240        return score;
241    }
242
243    /**
244     * A unique instance key for this notification record.
245     */
246    public String getKey() {
247        return key;
248    }
249}
250