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