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