StatusBarNotification.java revision e6345b5194b89b46ba28dfcea9e2e0699c0705ba
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 opPkg;
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, String opPkg, int id, String tag, int uid,
45            int initialPid, int score, Notification notification, UserHandle user) {
46        this(pkg, opPkg, id, tag, uid, initialPid, score, notification, user,
47                System.currentTimeMillis());
48    }
49
50    public StatusBarNotification(String pkg, String opPkg, int id, String tag, int uid,
51            int initialPid, int score, Notification notification, UserHandle user,
52            long postTime) {
53        if (pkg == null) throw new NullPointerException();
54        if (notification == null) throw new NullPointerException();
55
56        this.pkg = pkg;
57        this.opPkg = opPkg;
58        this.id = id;
59        this.tag = tag;
60        this.uid = uid;
61        this.initialPid = initialPid;
62        this.score = score;
63        this.notification = notification;
64        this.user = user;
65        this.notification.setUser(user);
66        this.postTime = postTime;
67        this.key = key();
68    }
69
70    public StatusBarNotification(Parcel in) {
71        this.pkg = in.readString();
72        this.opPkg = in.readString();
73        this.id = in.readInt();
74        if (in.readInt() != 0) {
75            this.tag = in.readString();
76        } else {
77            this.tag = null;
78        }
79        this.uid = in.readInt();
80        this.initialPid = in.readInt();
81        this.score = in.readInt();
82        this.notification = new Notification(in);
83        this.user = UserHandle.readFromParcel(in);
84        this.notification.setUser(this.user);
85        this.postTime = in.readLong();
86        this.key = key();
87    }
88
89    private String key() {
90        return pkg + '|' + id + '|' + tag + '|' + uid;
91    }
92
93    public void writeToParcel(Parcel out, int flags) {
94        out.writeString(this.pkg);
95        out.writeString(this.opPkg);
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.opPkg,
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.opPkg,
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 key=%s: %s)",
152                this.pkg, this.user, this.id, this.tag,
153                this.score, this.key, 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    /**
173     * Returns a userHandle for the instance of the app that posted this notification.
174     *
175     * @deprecated Use {@link #getUser()} instead.
176     */
177    public int getUserId() {
178        return this.user.getIdentifier();
179    }
180
181    /** The package of the app that posted the notification. */
182    public String getPackageName() {
183        return pkg;
184    }
185
186    /** The id supplied to {@link android.app.NotificationManager#notify(int,Notification)}. */
187    public int getId() {
188        return id;
189    }
190
191    /** The tag supplied to {@link android.app.NotificationManager#notify(int,Notification)},
192     * or null if no tag was specified. */
193    public String getTag() {
194        return tag;
195    }
196
197    /** The notifying app's calling uid. @hide */
198    public int getUid() {
199        return uid;
200    }
201
202    /** The package used for AppOps tracking. @hide */
203    public String getOpPkg() {
204        return opPkg;
205    }
206
207    /** @hide */
208    public int getInitialPid() {
209        return initialPid;
210    }
211
212    /** The {@link android.app.Notification} supplied to
213     * {@link android.app.NotificationManager#notify(int,Notification)}. */
214    public Notification getNotification() {
215        return notification;
216    }
217
218    /**
219     * The {@link android.os.UserHandle} for whom this notification is intended.
220     */
221    public UserHandle getUser() {
222        return user;
223    }
224
225    /** The time (in {@link System#currentTimeMillis} time) the notification was posted,
226     * which may be different than {@link android.app.Notification#when}.
227     */
228    public long getPostTime() {
229        return postTime;
230    }
231
232    /** @hide */
233    public int getScore() {
234        return score;
235    }
236
237    /**
238     * A unique instance key for this notification record.
239     */
240    public String getKey() {
241        return key;
242    }
243}
244