The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 20 | import android.os.Binder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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> |
Peter Collingbourne | b97c349 | 2010-10-13 20:04:52 +0100 | [diff] [blame] | 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. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 61 | public class NotificationManager |
| 62 | { |
| 63 | private static String TAG = "NotificationManager"; |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 64 | private static boolean localLOGV = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | |
| 66 | private static INotificationManager sService; |
| 67 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 68 | /** @hide */ |
| 69 | static public INotificationManager getService() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | { |
| 71 | if (sService != null) { |
| 72 | return sService; |
| 73 | } |
| 74 | IBinder b = ServiceManager.getService("notification"); |
| 75 | sService = INotificationManager.Stub.asInterface(b); |
| 76 | return sService; |
| 77 | } |
| 78 | |
| 79 | /*package*/ NotificationManager(Context context, Handler handler) |
| 80 | { |
| 81 | mContext = context; |
| 82 | } |
| 83 | |
| 84 | /** |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 85 | * Post a notification to be shown in the status bar. If a notification with |
| 86 | * the same id has already been posted by your application and has not yet been canceled, it |
| 87 | * will be replaced by the updated information. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | * |
| 89 | * @param id An identifier for this notification unique within your |
| 90 | * application. |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 91 | * @param notification A {@link Notification} object describing what to show the user. Must not |
| 92 | * be null. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | */ |
| 94 | public void notify(int id, Notification notification) |
| 95 | { |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 96 | notify(null, id, notification); |
| 97 | } |
| 98 | |
| 99 | /** |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 100 | * Post a notification to be shown in the status bar. If a notification with |
| 101 | * the same tag and id has already been posted by your application and has not yet been |
| 102 | * canceled, it will be replaced by the updated information. |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 103 | * |
Peter Collingbourne | b97c349 | 2010-10-13 20:04:52 +0100 | [diff] [blame] | 104 | * @param tag A string identifier for this notification. May be {@code null}. |
| 105 | * @param id An identifier for this notification. The pair (tag, id) must be unique |
| 106 | * within your application. |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 107 | * @param notification A {@link Notification} object describing what to |
| 108 | * show the user. Must not be null. |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 109 | */ |
| 110 | public void notify(String tag, int id, Notification notification) |
| 111 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | int[] idOut = new int[1]; |
| 113 | INotificationManager service = getService(); |
| 114 | String pkg = mContext.getPackageName(); |
| 115 | if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")"); |
| 116 | try { |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 117 | service.enqueueNotificationWithTag(pkg, tag, id, notification, idOut); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | if (id != idOut[0]) { |
| 119 | Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]); |
| 120 | } |
| 121 | } catch (RemoteException e) { |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Cancel a previously shown notification. If it's transient, the view |
| 127 | * will be hidden. If it's persistent, it will be removed from the status |
| 128 | * bar. |
| 129 | */ |
| 130 | public void cancel(int id) |
| 131 | { |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 132 | cancel(null, id); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Cancel a previously shown notification. If it's transient, the view |
| 137 | * will be hidden. If it's persistent, it will be removed from the status |
| 138 | * bar. |
| 139 | */ |
| 140 | public void cancel(String tag, int id) |
| 141 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | INotificationManager service = getService(); |
| 143 | String pkg = mContext.getPackageName(); |
| 144 | if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")"); |
| 145 | try { |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 146 | service.cancelNotificationWithTag(pkg, tag, id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | } catch (RemoteException e) { |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Cancel all previously shown notifications. See {@link #cancel} for the |
| 153 | * detailed behavior. |
| 154 | */ |
| 155 | public void cancelAll() |
| 156 | { |
| 157 | INotificationManager service = getService(); |
| 158 | String pkg = mContext.getPackageName(); |
| 159 | if (localLOGV) Log.v(TAG, pkg + ": cancelAll()"); |
| 160 | try { |
| 161 | service.cancelAllNotifications(pkg); |
| 162 | } catch (RemoteException e) { |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private Context mContext; |
| 167 | } |