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