blob: 5e69128aafb12766124e5959d3c69390a47e8d7f [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
19import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.os.Handler;
21import android.os.IBinder;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070022import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.os.ServiceManager;
Dianne Hackborn41203752012-08-31 14:05:51 -070024import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import 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 Collingbourneb97c3492010-10-13 20:04:52 +010042 * 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 Project9066cfe2009-03-03 19:31:44 -080053 *
54 * <p>
55 * You do not instantiate this class directly; instead, retrieve it through
56 * {@link android.content.Context#getSystemService}.
57 *
Joe Fernandez558459f2011-10-13 16:47:36 -070058 * <div class="special reference">
59 * <h3>Developer Guides</h3>
60 * <p>For a guide to creating notifications, read the
61 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
62 * developer guide.</p>
63 * </div>
64 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 * @see android.app.Notification
66 * @see android.content.Context#getSystemService
67 */
68public class NotificationManager
69{
70 private static String TAG = "NotificationManager";
Joe Onorato43a17652011-04-06 19:22:23 -070071 private static boolean localLOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
73 private static INotificationManager sService;
74
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070075 /** @hide */
76 static public INotificationManager getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 {
78 if (sService != null) {
79 return sService;
80 }
81 IBinder b = ServiceManager.getService("notification");
82 sService = INotificationManager.Stub.asInterface(b);
83 return sService;
84 }
85
86 /*package*/ NotificationManager(Context context, Handler handler)
87 {
88 mContext = context;
89 }
90
Jeff Sharkey69ddab42012-08-25 00:05:46 -070091 /** {@hide} */
92 public static NotificationManager from(Context context) {
93 return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
94 }
95
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 /**
Daniel Sandlere97a3bc2011-02-07 16:47:07 -050097 * Post a notification to be shown in the status bar. If a notification with
98 * the same id has already been posted by your application and has not yet been canceled, it
99 * will be replaced by the updated information.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 *
101 * @param id An identifier for this notification unique within your
102 * application.
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500103 * @param notification A {@link Notification} object describing what to show the user. Must not
104 * be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 */
106 public void notify(int id, Notification notification)
107 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700108 notify(null, id, notification);
109 }
110
111 /**
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 tag and id has already been posted by your application and has not yet been
114 * canceled, it will be replaced by the updated information.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700115 *
Peter Collingbourneb97c3492010-10-13 20:04:52 +0100116 * @param tag A string identifier for this notification. May be {@code null}.
117 * @param id An identifier for this notification. The pair (tag, id) must be unique
118 * within your application.
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500119 * @param notification A {@link Notification} object describing what to
120 * show the user. Must not be null.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700121 */
122 public void notify(String tag, int id, Notification notification)
123 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 int[] idOut = new int[1];
125 INotificationManager service = getService();
126 String pkg = mContext.getPackageName();
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700127 if (notification.sound != null) {
128 notification.sound = notification.sound.getCanonicalUri();
129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
131 try {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800132 service.enqueueNotificationWithTag(pkg, mContext.getBasePackageName(), tag, id,
133 notification, idOut, UserHandle.myUserId());
Dianne Hackborn41203752012-08-31 14:05:51 -0700134 if (id != idOut[0]) {
135 Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
136 }
137 } catch (RemoteException e) {
138 }
139 }
140
141 /**
142 * @hide
143 */
144 public void notifyAsUser(String tag, int id, Notification notification, UserHandle user)
145 {
146 int[] idOut = new int[1];
147 INotificationManager service = getService();
148 String pkg = mContext.getPackageName();
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700149 if (notification.sound != null) {
150 notification.sound = notification.sound.getCanonicalUri();
151 }
Dianne Hackborn41203752012-08-31 14:05:51 -0700152 if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
153 try {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800154 service.enqueueNotificationWithTag(pkg, mContext.getBasePackageName(), tag, id,
155 notification, idOut, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 if (id != idOut[0]) {
157 Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
158 }
159 } catch (RemoteException e) {
160 }
161 }
162
163 /**
164 * Cancel a previously shown notification. If it's transient, the view
165 * will be hidden. If it's persistent, it will be removed from the status
166 * bar.
167 */
168 public void cancel(int id)
169 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700170 cancel(null, id);
171 }
172
173 /**
174 * Cancel a previously shown notification. If it's transient, the view
175 * will be hidden. If it's persistent, it will be removed from the status
176 * bar.
177 */
178 public void cancel(String tag, int id)
179 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 INotificationManager service = getService();
181 String pkg = mContext.getPackageName();
182 if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")");
183 try {
Dianne Hackborn41203752012-08-31 14:05:51 -0700184 service.cancelNotificationWithTag(pkg, tag, id, UserHandle.myUserId());
185 } catch (RemoteException e) {
186 }
187 }
188
189 /**
190 * @hide
191 */
192 public void cancelAsUser(String tag, int id, UserHandle user)
193 {
194 INotificationManager service = getService();
195 String pkg = mContext.getPackageName();
196 if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")");
197 try {
198 service.cancelNotificationWithTag(pkg, tag, id, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 } catch (RemoteException e) {
200 }
201 }
202
203 /**
204 * Cancel all previously shown notifications. See {@link #cancel} for the
205 * detailed behavior.
206 */
207 public void cancelAll()
208 {
209 INotificationManager service = getService();
210 String pkg = mContext.getPackageName();
211 if (localLOGV) Log.v(TAG, pkg + ": cancelAll()");
212 try {
Dianne Hackborn41203752012-08-31 14:05:51 -0700213 service.cancelAllNotifications(pkg, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 } catch (RemoteException e) {
215 }
216 }
217
218 private Context mContext;
219}