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