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 com.android.internal.statusbar;
18
19import android.app.Notification;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.os.UserHandle;
23
24/*
25boolean clearable = !n.ongoingEvent && ((notification.flags & Notification.FLAG_NO_CLEAR) == 0);
26
27
28// TODO: make this restriction do something smarter like never fill
29// more than two screens.  "Why would anyone need more than 80 characters." :-/
30final int maxTickerLen = 80;
31if (truncatedTicker != null && truncatedTicker.length() > maxTickerLen) {
32    truncatedTicker = truncatedTicker.subSequence(0, maxTickerLen);
33}
34*/
35
36/**
37 * Class encapsulating a Notification. Sent by the NotificationManagerService to the IStatusBar (in System UI).
38 */
39public class StatusBarNotification implements Parcelable {
40    public final String pkg;
41    public final int id;
42    public final String tag;
43    public final int uid;
44    public final int initialPid;
45    // TODO: make this field private and move callers to an accessor that
46    // ensures sourceUser is applied.
47    public final Notification notification;
48    public final int score;
49    public final UserHandle user;
50
51    /** This is temporarily needed for the JB MR1 PDK. */
52    @Deprecated
53    public StatusBarNotification(String pkg, int id, String tag, int uid, int initialPid, int score,
54            Notification notification) {
55        this(pkg, id, tag, uid, initialPid, score, notification, UserHandle.OWNER);
56    }
57
58    public StatusBarNotification(String pkg, int id, String tag, int uid, int initialPid, int score,
59            Notification notification, UserHandle user) {
60        if (pkg == null) throw new NullPointerException();
61        if (notification == null) throw new NullPointerException();
62
63        this.pkg = 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    }
73
74    public StatusBarNotification(Parcel in) {
75        this.pkg = 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.score = in.readInt();
85        this.notification = new Notification(in);
86        this.user = UserHandle.readFromParcel(in);
87        this.notification.setUser(user);
88    }
89
90    public void writeToParcel(Parcel out, int flags) {
91        out.writeString(this.pkg);
92        out.writeInt(this.id);
93        if (this.tag != null) {
94            out.writeInt(1);
95            out.writeString(this.tag);
96        } else {
97            out.writeInt(0);
98        }
99        out.writeInt(this.uid);
100        out.writeInt(this.initialPid);
101        out.writeInt(this.score);
102        this.notification.writeToParcel(out, flags);
103        user.writeToParcel(out, flags);
104    }
105
106    public int describeContents() {
107        return 0;
108    }
109
110    public static final Parcelable.Creator<StatusBarNotification> CREATOR
111            = new Parcelable.Creator<StatusBarNotification>()
112    {
113        public StatusBarNotification createFromParcel(Parcel parcel)
114        {
115            return new StatusBarNotification(parcel);
116        }
117
118        public StatusBarNotification[] newArray(int size)
119        {
120            return new StatusBarNotification[size];
121        }
122    };
123
124    @Override
125    public StatusBarNotification clone() {
126        return new StatusBarNotification(this.pkg, this.id, this.tag, this.uid, this.initialPid,
127                this.score, this.notification.clone(), this.user);
128    }
129
130    @Override
131    public String toString() {
132        return "StatusBarNotification(pkg=" + pkg + " id=" + id + " tag=" + tag + " score=" + score
133                + " notn=" + notification + " user=" + user + ")";
134    }
135
136    public boolean isOngoing() {
137        return (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0;
138    }
139
140    public boolean isClearable() {
141        return ((notification.flags & Notification.FLAG_ONGOING_EVENT) == 0)
142                && ((notification.flags & Notification.FLAG_NO_CLEAR) == 0);
143    }
144
145    /** Returns a userHandle for the instance of the app that posted this notification. */
146    public int getUserId() {
147        return this.user.getIdentifier();
148    }
149}
150