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