Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007 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.app;
     18 
     19 import android.content.Context;
     20 import android.os.Binder;
     21 import android.os.RemoteException;
     22 import android.os.Handler;
     23 import android.os.IBinder;
     24 import android.os.ServiceManager;
     25 import android.util.Log;
     26 
     27 /**
     28  * Class to notify the user of events that happen.  This is how you tell
     29  * the user that something has happened in the background. {@more}
     30  *
     31  * Notifications can take different forms:
     32  * <ul>
     33  *      <li>A persistent icon that goes in the status bar and is accessible
     34  *          through the launcher, (when the user selects it, a designated Intent
     35  *          can be launched),</li>
     36  *      <li>Turning on or flashing LEDs on the device, or</li>
     37  *      <li>Alerting the user by flashing the backlight, playing a sound,
     38  *          or vibrating.</li>
     39  * </ul>
     40  *
     41  * <p>
     42  * Each of the notify methods takes an int id parameter.  This id identifies
     43  * this notification from your app to the system, so that id should be unique
     44  * within your app.  If you call one of the notify methods with an id that is
     45  * currently active and a new set of notification parameters, it will be
     46  * updated.  For example, if you pass a new status bar icon, the old icon in
     47  * the status bar will be replaced with the new one.  This is also the same
     48  * id you pass to the {@link #cancel} method to clear this notification.
     49  *
     50  * <p>
     51  * You do not instantiate this class directly; instead, retrieve it through
     52  * {@link android.content.Context#getSystemService}.
     53  *
     54  * @see android.app.Notification
     55  * @see android.content.Context#getSystemService
     56  */
     57 public class NotificationManager
     58 {
     59     private static String TAG = "NotificationManager";
     60     private static boolean DEBUG = false;
     61     private static boolean localLOGV = DEBUG || android.util.Config.LOGV;
     62 
     63     private static INotificationManager sService;
     64 
     65     /** @hide */
     66     static public INotificationManager getService()
     67     {
     68         if (sService != null) {
     69             return sService;
     70         }
     71         IBinder b = ServiceManager.getService("notification");
     72         sService = INotificationManager.Stub.asInterface(b);
     73         return sService;
     74     }
     75 
     76     /*package*/ NotificationManager(Context context, Handler handler)
     77     {
     78         mContext = context;
     79     }
     80 
     81     /**
     82      * Persistent notification on the status bar,
     83      *
     84      * @param id An identifier for this notification unique within your
     85      *        application.
     86      * @param notification A {@link Notification} object describing how to
     87      *        notify the user, other than the view you're providing. Must not be null.
     88      */
     89     public void notify(int id, Notification notification)
     90     {
     91         notify(null, id, notification);
     92     }
     93 
     94     /**
     95      * Persistent notification on the status bar,
     96      *
     97      * @param tag An string identifier for this notification unique within your
     98      *        application.
     99      * @param notification A {@link Notification} object describing how to
    100      *        notify the user, other than the view you're providing. Must not be null.
    101      * @return the id of the notification that is associated with the string identifier that
    102      * can be used to cancel the notification
    103      */
    104     public void notify(String tag, int id, Notification notification)
    105     {
    106         int[] idOut = new int[1];
    107         INotificationManager service = getService();
    108         String pkg = mContext.getPackageName();
    109         if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
    110         try {
    111             service.enqueueNotificationWithTag(pkg, tag, id, notification, idOut);
    112             if (id != idOut[0]) {
    113                 Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
    114             }
    115         } catch (RemoteException e) {
    116         }
    117     }
    118 
    119     /**
    120      * Cancel a previously shown notification.  If it's transient, the view
    121      * will be hidden.  If it's persistent, it will be removed from the status
    122      * bar.
    123      */
    124     public void cancel(int id)
    125     {
    126         cancel(null, id);
    127     }
    128 
    129     /**
    130      * Cancel a previously shown notification.  If it's transient, the view
    131      * will be hidden.  If it's persistent, it will be removed from the status
    132      * bar.
    133      */
    134     public void cancel(String tag, int id)
    135     {
    136         INotificationManager service = getService();
    137         String pkg = mContext.getPackageName();
    138         if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")");
    139         try {
    140             service.cancelNotificationWithTag(pkg, tag, id);
    141         } catch (RemoteException e) {
    142         }
    143     }
    144 
    145     /**
    146      * Cancel all previously shown notifications. See {@link #cancel} for the
    147      * detailed behavior.
    148      */
    149     public void cancelAll()
    150     {
    151         INotificationManager service = getService();
    152         String pkg = mContext.getPackageName();
    153         if (localLOGV) Log.v(TAG, pkg + ": cancelAll()");
    154         try {
    155             service.cancelAllNotifications(pkg);
    156         } catch (RemoteException e) {
    157         }
    158     }
    159 
    160     private Context mContext;
    161 }
    162