NotificationManager.java revision f3a6b901917b1609c6f1901bb36deb07a58fd20d
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
17package android.app;
18
19import android.content.Context;
20import android.os.Binder;
21import android.os.RemoteException;
22import android.os.Handler;
23import android.os.IBinder;
24import android.os.ServiceManager;
25import 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 and optionally a
43 * {@link String} tag parameter, which may be {@code null}.  These parameters
44 * are used to form a pair (tag, id), or ({@code null}, id) if tag is
45 * unspecified.  This pair identifies this notification from your app to the
46 * system, so that pair should be unique within your app.  If you call one
47 * of the notify methods with a (tag, id) pair that is currently active and
48 * a new set of notification parameters, it will be updated.  For example,
49 * if you pass a new status bar icon, the old icon in the status bar will
50 * be replaced with the new one.  This is also the same tag and id you pass
51 * to the {@link #cancel(int)} or {@link #cancel(String, int)} method to clear
52 * this notification.
53 *
54 * <p>
55 * You do not instantiate this class directly; instead, retrieve it through
56 * {@link android.content.Context#getSystemService}.
57 *
58 * @see android.app.Notification
59 * @see android.content.Context#getSystemService
60 */
61public class NotificationManager
62{
63    private static String TAG = "NotificationManager";
64    private static boolean DEBUG = false;
65    private static boolean localLOGV = DEBUG || android.util.Config.LOGV;
66
67    private static INotificationManager sService;
68
69    /** @hide */
70    static public INotificationManager getService()
71    {
72        if (sService != null) {
73            return sService;
74        }
75        IBinder b = ServiceManager.getService("notification");
76        sService = INotificationManager.Stub.asInterface(b);
77        return sService;
78    }
79
80    /*package*/ NotificationManager(Context context, Handler handler)
81    {
82        mContext = context;
83    }
84
85    /**
86     * Persistent notification on the status bar,
87     *
88     * @param id An identifier for this notification unique within your
89     *        application.
90     * @param notification A {@link Notification} object describing how to
91     *        notify the user, other than the view you're providing. Must not be null.
92     */
93    public void notify(int id, Notification notification)
94    {
95        notify(null, id, notification);
96    }
97
98    /**
99     * Persistent notification on the status bar,
100     *
101     * @param tag A string identifier for this notification.  May be {@code null}.
102     * @param id An identifier for this notification.  The pair (tag, id) must be unique
103     *        within your application.
104     * @param notification A {@link Notification} object describing how to
105     *        notify the user, other than the view you're providing. Must not be null.
106     */
107    public void notify(String tag, int id, Notification notification)
108    {
109        int[] idOut = new int[1];
110        INotificationManager service = getService();
111        String pkg = mContext.getPackageName();
112        if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
113        try {
114            service.enqueueNotificationWithTag(pkg, tag, id, notification, idOut);
115            if (id != idOut[0]) {
116                Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
117            }
118        } catch (RemoteException e) {
119        }
120    }
121
122    /**
123     * Cancel a previously shown notification.  If it's transient, the view
124     * will be hidden.  If it's persistent, it will be removed from the status
125     * bar.
126     */
127    public void cancel(int id)
128    {
129        cancel(null, id);
130    }
131
132    /**
133     * Cancel a previously shown notification.  If it's transient, the view
134     * will be hidden.  If it's persistent, it will be removed from the status
135     * bar.
136     */
137    public void cancel(String tag, int id)
138    {
139        INotificationManager service = getService();
140        String pkg = mContext.getPackageName();
141        if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")");
142        try {
143            service.cancelNotificationWithTag(pkg, tag, id);
144        } catch (RemoteException e) {
145        }
146    }
147
148    /**
149     * Cancel all previously shown notifications. See {@link #cancel} for the
150     * detailed behavior.
151     */
152    public void cancelAll()
153    {
154        INotificationManager service = getService();
155        String pkg = mContext.getPackageName();
156        if (localLOGV) Log.v(TAG, pkg + ": cancelAll()");
157        try {
158            service.cancelAllNotifications(pkg);
159        } catch (RemoteException e) {
160        }
161    }
162
163    private Context mContext;
164}
165