blob: cf54107848f855009cd49a2081c91f4c256c2f83 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
John Spurlockb4782522014-08-22 14:54:46 -040019import android.annotation.SdkConstant;
Christoph Studer4600f9b2014-07-22 22:44:43 +020020import android.app.Notification.Builder;
John Spurlockb4782522014-08-22 14:54:46 -040021import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
John Spurlock2b122f42014-08-27 16:29:47 -040023import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.os.Handler;
25import android.os.IBinder;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070026import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.os.ServiceManager;
Jeff Sharkeya14acd22013-04-02 18:27:45 -070028import android.os.StrictMode;
Dianne Hackborn41203752012-08-31 14:05:51 -070029import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.util.Log;
31
32/**
33 * Class to notify the user of events that happen. This is how you tell
34 * the user that something has happened in the background. {@more}
35 *
36 * Notifications can take different forms:
37 * <ul>
38 * <li>A persistent icon that goes in the status bar and is accessible
39 * through the launcher, (when the user selects it, a designated Intent
40 * can be launched),</li>
41 * <li>Turning on or flashing LEDs on the device, or</li>
42 * <li>Alerting the user by flashing the backlight, playing a sound,
43 * or vibrating.</li>
44 * </ul>
45 *
46 * <p>
Peter Collingbourneb97c3492010-10-13 20:04:52 +010047 * Each of the notify methods takes an int id parameter and optionally a
48 * {@link String} tag parameter, which may be {@code null}. These parameters
49 * are used to form a pair (tag, id), or ({@code null}, id) if tag is
50 * unspecified. This pair identifies this notification from your app to the
51 * system, so that pair should be unique within your app. If you call one
52 * of the notify methods with a (tag, id) pair that is currently active and
53 * a new set of notification parameters, it will be updated. For example,
54 * if you pass a new status bar icon, the old icon in the status bar will
55 * be replaced with the new one. This is also the same tag and id you pass
56 * to the {@link #cancel(int)} or {@link #cancel(String, int)} method to clear
57 * this notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 *
59 * <p>
60 * You do not instantiate this class directly; instead, retrieve it through
61 * {@link android.content.Context#getSystemService}.
62 *
Joe Fernandez558459f2011-10-13 16:47:36 -070063 * <div class="special reference">
64 * <h3>Developer Guides</h3>
65 * <p>For a guide to creating notifications, read the
66 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
67 * developer guide.</p>
68 * </div>
69 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 * @see android.app.Notification
71 * @see android.content.Context#getSystemService
72 */
73public class NotificationManager
74{
75 private static String TAG = "NotificationManager";
Joe Onorato43a17652011-04-06 19:22:23 -070076 private static boolean localLOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
John Spurlockb4782522014-08-22 14:54:46 -040078 /**
79 * Intent that is broadcast when the state of {@link #getEffectsSuppressor()} changes.
80 * This broadcast is only sent to registered receivers.
81 *
82 * @hide
83 */
84 @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
85 public static final String ACTION_EFFECTS_SUPPRESSOR_CHANGED
86 = "android.os.action.ACTION_EFFECTS_SUPPRESSOR_CHANGED";
87
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 private static INotificationManager sService;
89
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070090 /** @hide */
91 static public INotificationManager getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 {
93 if (sService != null) {
94 return sService;
95 }
96 IBinder b = ServiceManager.getService("notification");
97 sService = INotificationManager.Stub.asInterface(b);
98 return sService;
99 }
100
101 /*package*/ NotificationManager(Context context, Handler handler)
102 {
103 mContext = context;
104 }
105
Jeff Sharkey69ddab42012-08-25 00:05:46 -0700106 /** {@hide} */
107 public static NotificationManager from(Context context) {
108 return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
109 }
110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 /**
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500112 * Post a notification to be shown in the status bar. If a notification with
113 * the same id has already been posted by your application and has not yet been canceled, it
114 * will be replaced by the updated information.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 *
116 * @param id An identifier for this notification unique within your
117 * application.
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500118 * @param notification A {@link Notification} object describing what to show the user. Must not
119 * be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 */
121 public void notify(int id, Notification notification)
122 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700123 notify(null, id, notification);
124 }
125
126 /**
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500127 * Post a notification to be shown in the status bar. If a notification with
128 * the same tag and id has already been posted by your application and has not yet been
129 * canceled, it will be replaced by the updated information.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700130 *
Peter Collingbourneb97c3492010-10-13 20:04:52 +0100131 * @param tag A string identifier for this notification. May be {@code null}.
132 * @param id An identifier for this notification. The pair (tag, id) must be unique
133 * within your application.
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500134 * @param notification A {@link Notification} object describing what to
135 * show the user. Must not be null.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700136 */
137 public void notify(String tag, int id, Notification notification)
138 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 int[] idOut = new int[1];
140 INotificationManager service = getService();
141 String pkg = mContext.getPackageName();
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700142 if (notification.sound != null) {
143 notification.sound = notification.sound.getCanonicalUri();
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700144 if (StrictMode.vmFileUriExposureEnabled()) {
145 notification.sound.checkFileUriExposed("Notification.sound");
146 }
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
Christoph Studer4600f9b2014-07-22 22:44:43 +0200149 Notification stripped = notification.clone();
150 Builder.stripForDelivery(stripped);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 try {
Dianne Hackborn95d78532013-09-11 09:51:14 -0700152 service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,
Christoph Studer4600f9b2014-07-22 22:44:43 +0200153 stripped, idOut, UserHandle.myUserId());
Dianne Hackborn41203752012-08-31 14:05:51 -0700154 if (id != idOut[0]) {
155 Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
156 }
157 } catch (RemoteException e) {
158 }
159 }
160
161 /**
162 * @hide
163 */
164 public void notifyAsUser(String tag, int id, Notification notification, UserHandle user)
165 {
166 int[] idOut = new int[1];
167 INotificationManager service = getService();
168 String pkg = mContext.getPackageName();
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700169 if (notification.sound != null) {
170 notification.sound = notification.sound.getCanonicalUri();
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700171 if (StrictMode.vmFileUriExposureEnabled()) {
172 notification.sound.checkFileUriExposed("Notification.sound");
173 }
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700174 }
Dianne Hackborn41203752012-08-31 14:05:51 -0700175 if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
Christoph Studer4600f9b2014-07-22 22:44:43 +0200176 Notification stripped = notification.clone();
177 Builder.stripForDelivery(stripped);
Dianne Hackborn41203752012-08-31 14:05:51 -0700178 try {
Dianne Hackborn95d78532013-09-11 09:51:14 -0700179 service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,
Christoph Studer4600f9b2014-07-22 22:44:43 +0200180 stripped, idOut, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 if (id != idOut[0]) {
182 Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
183 }
184 } catch (RemoteException e) {
185 }
186 }
187
188 /**
189 * Cancel a previously shown notification. If it's transient, the view
190 * will be hidden. If it's persistent, it will be removed from the status
191 * bar.
192 */
193 public void cancel(int id)
194 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700195 cancel(null, id);
196 }
197
198 /**
199 * Cancel a previously shown notification. If it's transient, the view
200 * will be hidden. If it's persistent, it will be removed from the status
201 * bar.
202 */
203 public void cancel(String tag, int id)
204 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 INotificationManager service = getService();
206 String pkg = mContext.getPackageName();
207 if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")");
208 try {
Dianne Hackborn41203752012-08-31 14:05:51 -0700209 service.cancelNotificationWithTag(pkg, tag, id, UserHandle.myUserId());
210 } catch (RemoteException e) {
211 }
212 }
213
214 /**
215 * @hide
216 */
217 public void cancelAsUser(String tag, int id, UserHandle user)
218 {
219 INotificationManager service = getService();
220 String pkg = mContext.getPackageName();
221 if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")");
222 try {
223 service.cancelNotificationWithTag(pkg, tag, id, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 } catch (RemoteException e) {
225 }
226 }
227
228 /**
229 * Cancel all previously shown notifications. See {@link #cancel} for the
230 * detailed behavior.
231 */
232 public void cancelAll()
233 {
234 INotificationManager service = getService();
235 String pkg = mContext.getPackageName();
236 if (localLOGV) Log.v(TAG, pkg + ": cancelAll()");
237 try {
Dianne Hackborn41203752012-08-31 14:05:51 -0700238 service.cancelAllNotifications(pkg, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 } catch (RemoteException e) {
240 }
241 }
242
John Spurlockb4782522014-08-22 14:54:46 -0400243 /**
244 * @hide
245 */
246 public ComponentName getEffectsSuppressor() {
247 INotificationManager service = getService();
248 try {
249 return service.getEffectsSuppressor();
250 } catch (RemoteException e) {
251 return null;
252 }
253 }
254
John Spurlock2b122f42014-08-27 16:29:47 -0400255 /**
256 * @hide
257 */
258 public boolean matchesCallFilter(Bundle extras) {
259 INotificationManager service = getService();
260 try {
261 return service.matchesCallFilter(extras);
262 } catch (RemoteException e) {
263 return false;
264 }
265 }
266
John Spurlock530052a2014-11-30 16:26:19 -0500267 /**
268 * @hide
269 */
270 public boolean isSystemConditionProviderEnabled(String path) {
271 INotificationManager service = getService();
272 try {
273 return service.isSystemConditionProviderEnabled(path);
274 } catch (RemoteException e) {
275 return false;
276 }
277 }
278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 private Context mContext;
280}