blob: 7d1d9765eb9d73f15749a541827ea92e39876e9c [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 com.android.server;
18
Joe Onorato18e69df2010-05-17 22:26:12 -070019import com.android.internal.statusbar.StatusBarNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -070020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.app.ActivityManagerNative;
22import android.app.IActivityManager;
23import android.app.INotificationManager;
24import android.app.ITransientNotification;
25import android.app.Notification;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070026import android.app.NotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.app.PendingIntent;
28import android.app.StatusBarManager;
29import android.content.BroadcastReceiver;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070030import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.content.Context;
32import android.content.Intent;
33import android.content.IntentFilter;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070034import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.PackageManager;
36import android.content.pm.PackageManager.NameNotFoundException;
37import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070038import android.database.ContentObserver;
svetoslavganov75986cf2009-05-14 22:28:01 -070039import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.Binder;
Andy Stadler110988c2010-12-03 14:29:16 -080042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.IBinder;
45import android.os.Message;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070046import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070047import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.Vibrator;
49import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050050import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070051import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.EventLog;
53import android.util.Log;
Andy Stadler110988c2010-12-03 14:29:16 -080054import android.util.Slog;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.view.accessibility.AccessibilityEvent;
56import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.widget.Toast;
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import java.io.FileDescriptor;
60import java.io.PrintWriter;
61import java.util.ArrayList;
62import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040064/** {@hide} */
65public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066{
67 private static final String TAG = "NotificationService";
68 private static final boolean DBG = false;
69
Joe Onoratobd73d012010-06-04 11:44:54 -070070 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
71
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 // message codes
73 private static final int MESSAGE_TIMEOUT = 2;
74
75 private static final int LONG_DELAY = 3500; // 3.5 seconds
76 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -080077
78 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
81
82 final Context mContext;
83 final IActivityManager mAm;
84 final IBinder mForegroundToken = new Binder();
85
86 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -070087 private StatusBarManagerService mStatusBar;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050088 private LightsService.Light mNotificationLight;
89 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
Mike Lockwood670f9322010-01-20 12:13:36 -050091 private int mDefaultNotificationColor;
92 private int mDefaultNotificationLedOn;
93 private int mDefaultNotificationLedOff;
94
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 private NotificationRecord mSoundNotification;
Jean-Michel Trivi211957f2010-03-26 18:19:33 -070096 private NotificationPlayer mSound;
Joe Onorato30275482009-07-08 17:09:14 -070097 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -040098 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099
100 private NotificationRecord mVibrateNotification;
101 private Vibrator mVibrator = new Vibrator();
102
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500103 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400104 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500105 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500106 private boolean mNotificationPulseEnabled;
107
Fred Quintana6ecaff12009-09-25 14:23:13 -0700108 private final ArrayList<NotificationRecord> mNotificationList =
109 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
111 private ArrayList<ToastRecord> mToastQueue;
112
113 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 private static String idDebugString(Context baseContext, String packageName, int id) {
117 Context c = null;
118
119 if (packageName != null) {
120 try {
121 c = baseContext.createPackageContext(packageName, 0);
122 } catch (NameNotFoundException e) {
123 c = baseContext;
124 }
125 } else {
126 c = baseContext;
127 }
128
129 String pkg;
130 String type;
131 String name;
132
133 Resources r = c.getResources();
134 try {
135 return r.getResourceName(id);
136 } catch (Resources.NotFoundException e) {
137 return "<name unknown>";
138 }
139 }
140
141 private static final class NotificationRecord
142 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700143 final String pkg;
144 final String tag;
145 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700146 final int uid;
147 final int initialPid;
Daniel Sandlere40451a2011-02-03 14:51:35 -0500148 final int priority;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700149 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 IBinder statusBarKey;
151
Daniel Sandlere40451a2011-02-03 14:51:35 -0500152 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid, int priority,
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700153 Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 {
155 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700156 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700158 this.uid = uid;
159 this.initialPid = initialPid;
Daniel Sandlere40451a2011-02-03 14:51:35 -0500160 this.priority = priority;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 this.notification = notification;
162 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 void dump(PrintWriter pw, String prefix, Context baseContext) {
165 pw.println(prefix + this);
166 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
167 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
168 pw.println(prefix + " contentIntent=" + notification.contentIntent);
169 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
170 pw.println(prefix + " tickerText=" + notification.tickerText);
171 pw.println(prefix + " contentView=" + notification.contentView);
172 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
173 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
174 pw.println(prefix + " sound=" + notification.sound);
175 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
176 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
177 + " ledOnMS=" + notification.ledOnMS
178 + " ledOffMS=" + notification.ledOffMS);
179 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 @Override
182 public final String toString()
183 {
184 return "NotificationRecord{"
185 + Integer.toHexString(System.identityHashCode(this))
186 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700187 + " id=" + Integer.toHexString(id)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500188 + " tag=" + tag
189 + " pri=" + priority
190 + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 }
192 }
193
194 private static final class ToastRecord
195 {
196 final int pid;
197 final String pkg;
198 final ITransientNotification callback;
199 int duration;
200
201 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
202 {
203 this.pid = pid;
204 this.pkg = pkg;
205 this.callback = callback;
206 this.duration = duration;
207 }
208
209 void update(int duration) {
210 this.duration = duration;
211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 void dump(PrintWriter pw, String prefix) {
214 pw.println(prefix + this);
215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 @Override
218 public final String toString()
219 {
220 return "ToastRecord{"
221 + Integer.toHexString(System.identityHashCode(this))
222 + " pkg=" + pkg
223 + " callback=" + callback
224 + " duration=" + duration;
225 }
226 }
227
Joe Onorato089de882010-04-12 08:18:45 -0700228 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
229 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
231 public void onSetDisabled(int status) {
232 synchronized (mNotificationList) {
233 mDisabledNotifications = status;
234 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
235 // cancel whatever's going on
236 long identity = Binder.clearCallingIdentity();
237 try {
238 mSound.stop();
239 }
240 finally {
241 Binder.restoreCallingIdentity(identity);
242 }
243
244 identity = Binder.clearCallingIdentity();
245 try {
246 mVibrator.cancel();
247 }
248 finally {
249 Binder.restoreCallingIdentity(identity);
250 }
251 }
252 }
253 }
254
255 public void onClearAll() {
256 cancelAll();
257 }
258
Fred Quintana6ecaff12009-09-25 14:23:13 -0700259 public void onNotificationClick(String pkg, String tag, int id) {
260 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
jhtop.kim2e448f72011-07-13 17:15:32 +0900261 Notification.FLAG_FOREGROUND_SERVICE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 }
263
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400264 public void onNotificationClear(String pkg, String tag, int id) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800265 cancelNotification(pkg, tag, id, 0,
266 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
267 true);
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400268 }
269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 public void onPanelRevealed() {
271 synchronized (mNotificationList) {
272 // sound
273 mSoundNotification = null;
274 long identity = Binder.clearCallingIdentity();
275 try {
276 mSound.stop();
277 }
278 finally {
279 Binder.restoreCallingIdentity(identity);
280 }
281
282 // vibrate
283 mVibrateNotification = null;
284 identity = Binder.clearCallingIdentity();
285 try {
286 mVibrator.cancel();
287 }
288 finally {
289 Binder.restoreCallingIdentity(identity);
290 }
291
292 // light
293 mLights.clear();
294 mLedNotification = null;
295 updateLightsLocked();
296 }
297 }
Joe Onorato005847b2010-06-04 16:08:02 -0400298
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700299 public void onNotificationError(String pkg, String tag, int id,
300 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400301 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
302 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Joe Onorato46439ce2010-11-19 13:56:21 -0800303 cancelNotification(pkg, tag, id, 0, 0, false);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700304 long ident = Binder.clearCallingIdentity();
305 try {
306 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
307 "Bad notification posted from package " + pkg
308 + ": " + message);
309 } catch (RemoteException e) {
310 }
311 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 };
314
315 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
316 @Override
317 public void onReceive(Context context, Intent intent) {
318 String action = intent.getAction();
319
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800320 boolean queryRestart = false;
321
Mike Lockwood541c9942011-06-12 19:35:45 -0400322 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800323 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400324 || action.equals(Intent.ACTION_PACKAGE_CHANGED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800325 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800326 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800327 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800328 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800329 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800330 } else if (queryRestart) {
331 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800332 } else {
333 Uri uri = intent.getData();
334 if (uri == null) {
335 return;
336 }
337 String pkgName = uri.getSchemeSpecificPart();
338 if (pkgName == null) {
339 return;
340 }
341 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800343 if (pkgList != null && (pkgList.length > 0)) {
344 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800345 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400348 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
349 // Keep track of screen on/off state, but do not turn off the notification light
350 // until user passes through the lock screen or views the notification.
351 mScreenOn = true;
352 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
353 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500354 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400355 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
356 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500357 updateNotificationPulse();
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400358 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
359 // turn off LED when user passes through lock screen
360 mNotificationLight.turnOff();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
362 }
363 };
364
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700365 class SettingsObserver extends ContentObserver {
366 SettingsObserver(Handler handler) {
367 super(handler);
368 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800369
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700370 void observe() {
371 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500372 resolver.registerContentObserver(Settings.System.getUriFor(
373 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700374 update();
375 }
376
377 @Override public void onChange(boolean selfChange) {
378 update();
379 }
380
381 public void update() {
382 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500383 boolean pulseEnabled = Settings.System.getInt(resolver,
384 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
385 if (mNotificationPulseEnabled != pulseEnabled) {
386 mNotificationPulseEnabled = pulseEnabled;
387 updateNotificationPulse();
388 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700389 }
390 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500391
Joe Onorato089de882010-04-12 08:18:45 -0700392 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500393 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 {
395 super();
396 mContext = context;
397 mAm = ActivityManagerNative.getDefault();
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700398 mSound = new NotificationPlayer(TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 mSound.setUsesWakeLock(context);
400 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800402
Joe Onorato089de882010-04-12 08:18:45 -0700403 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 statusBar.setNotificationCallbacks(mNotificationCallbacks);
405
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500406 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
407 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
408
Mike Lockwood670f9322010-01-20 12:13:36 -0500409 Resources resources = mContext.getResources();
410 mDefaultNotificationColor = resources.getColor(
411 com.android.internal.R.color.config_defaultNotificationColor);
412 mDefaultNotificationLedOn = resources.getInteger(
413 com.android.internal.R.integer.config_defaultNotificationLedOn);
414 mDefaultNotificationLedOff = resources.getInteger(
415 com.android.internal.R.integer.config_defaultNotificationLedOff);
416
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400417 // Don't start allowing notifications until the setup wizard has run once.
418 // After that, including subsequent boots, init with notifications turned on.
419 // This works on the first boot because the setup wizard will toggle this
420 // flag at least once and we'll go back to 0 after that.
421 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
422 Settings.Secure.DEVICE_PROVISIONED, 0)) {
423 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
424 }
425
Mike Lockwood35e16bf2010-11-30 19:53:36 -0500426 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500428 filter.addAction(Intent.ACTION_SCREEN_ON);
429 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500430 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400431 filter.addAction(Intent.ACTION_USER_PRESENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800433 IntentFilter pkgFilter = new IntentFilter();
434 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400435 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800436 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
437 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
438 pkgFilter.addDataScheme("package");
439 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800440 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800441 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800442
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500443 SettingsObserver observer = new SettingsObserver(mHandler);
444 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 }
446
Joe Onorato30275482009-07-08 17:09:14 -0700447 void systemReady() {
448 // no beeping until we're basically done booting
449 mSystemReady = true;
450 }
451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 // Toasts
453 // ============================================================================
454 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
455 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400456 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457
458 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800459 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 return ;
461 }
462
463 synchronized (mToastQueue) {
464 int callingPid = Binder.getCallingPid();
465 long callingId = Binder.clearCallingIdentity();
466 try {
467 ToastRecord record;
468 int index = indexOfToastLocked(pkg, callback);
469 // If it's already in the queue, we update it in place, we don't
470 // move it to the end of the queue.
471 if (index >= 0) {
472 record = mToastQueue.get(index);
473 record.update(duration);
474 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800475 // Limit the number of toasts that any given package except the android
476 // package can enqueue. Prevents DOS attacks and deals with leaks.
477 if (!"android".equals(pkg)) {
478 int count = 0;
479 final int N = mToastQueue.size();
480 for (int i=0; i<N; i++) {
481 final ToastRecord r = mToastQueue.get(i);
482 if (r.pkg.equals(pkg)) {
483 count++;
484 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
485 Slog.e(TAG, "Package has already posted " + count
486 + " toasts. Not showing more. Package=" + pkg);
487 return;
488 }
489 }
490 }
491 }
492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 record = new ToastRecord(callingPid, pkg, callback, duration);
494 mToastQueue.add(record);
495 index = mToastQueue.size() - 1;
496 keepProcessAliveLocked(callingPid);
497 }
498 // If it's at index 0, it's the current toast. It doesn't matter if it's
499 // new or just been updated. Call back and tell it to show itself.
500 // If the callback fails, this will remove it from the list, so don't
501 // assume that it's valid after this.
502 if (index == 0) {
503 showNextToastLocked();
504 }
505 } finally {
506 Binder.restoreCallingIdentity(callingId);
507 }
508 }
509 }
510
511 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800512 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513
514 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800515 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 return ;
517 }
518
519 synchronized (mToastQueue) {
520 long callingId = Binder.clearCallingIdentity();
521 try {
522 int index = indexOfToastLocked(pkg, callback);
523 if (index >= 0) {
524 cancelToastLocked(index);
525 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800526 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
528 } finally {
529 Binder.restoreCallingIdentity(callingId);
530 }
531 }
532 }
533
534 private void showNextToastLocked() {
535 ToastRecord record = mToastQueue.get(0);
536 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800537 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 try {
539 record.callback.show();
540 scheduleTimeoutLocked(record, false);
541 return;
542 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800543 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 + " in package " + record.pkg);
545 // remove it from the list and let the process die
546 int index = mToastQueue.indexOf(record);
547 if (index >= 0) {
548 mToastQueue.remove(index);
549 }
550 keepProcessAliveLocked(record.pid);
551 if (mToastQueue.size() > 0) {
552 record = mToastQueue.get(0);
553 } else {
554 record = null;
555 }
556 }
557 }
558 }
559
560 private void cancelToastLocked(int index) {
561 ToastRecord record = mToastQueue.get(index);
562 try {
563 record.callback.hide();
564 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800565 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 + " in package " + record.pkg);
567 // don't worry about this, we're about to remove it from
568 // the list anyway
569 }
570 mToastQueue.remove(index);
571 keepProcessAliveLocked(record.pid);
572 if (mToastQueue.size() > 0) {
573 // Show the next one. If the callback fails, this will remove
574 // it from the list, so don't assume that the list hasn't changed
575 // after this point.
576 showNextToastLocked();
577 }
578 }
579
580 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
581 {
582 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
583 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
584 mHandler.removeCallbacksAndMessages(r);
585 mHandler.sendMessageDelayed(m, delay);
586 }
587
588 private void handleTimeout(ToastRecord record)
589 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800590 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 synchronized (mToastQueue) {
592 int index = indexOfToastLocked(record.pkg, record.callback);
593 if (index >= 0) {
594 cancelToastLocked(index);
595 }
596 }
597 }
598
599 // lock on mToastQueue
600 private int indexOfToastLocked(String pkg, ITransientNotification callback)
601 {
602 IBinder cbak = callback.asBinder();
603 ArrayList<ToastRecord> list = mToastQueue;
604 int len = list.size();
605 for (int i=0; i<len; i++) {
606 ToastRecord r = list.get(i);
607 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
608 return i;
609 }
610 }
611 return -1;
612 }
613
614 // lock on mToastQueue
615 private void keepProcessAliveLocked(int pid)
616 {
617 int toastCount = 0; // toasts from this pid
618 ArrayList<ToastRecord> list = mToastQueue;
619 int N = list.size();
620 for (int i=0; i<N; i++) {
621 ToastRecord r = list.get(i);
622 if (r.pid == pid) {
623 toastCount++;
624 }
625 }
626 try {
627 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
628 } catch (RemoteException e) {
629 // Shouldn't happen.
630 }
631 }
632
633 private final class WorkerHandler extends Handler
634 {
635 @Override
636 public void handleMessage(Message msg)
637 {
638 switch (msg.what)
639 {
640 case MESSAGE_TIMEOUT:
641 handleTimeout((ToastRecord)msg.obj);
642 break;
643 }
644 }
645 }
646
647
648 // Notifications
649 // ============================================================================
Andy Stadler110988c2010-12-03 14:29:16 -0800650 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
652 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700653 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
654 }
655
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400656 public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,
657 int[] idOut)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700658 {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400659 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
660 tag, id, notification, idOut);
661 }
662
Daniel Sandlere40451a2011-02-03 14:51:35 -0500663 public void enqueueNotificationWithTagPriority(String pkg, String tag, int id, int priority,
664 Notification notification, int[] idOut)
665 {
666 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
667 tag, id, priority, notification, idOut);
668 }
669
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400670 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
671 // uid/pid of another application)
672 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
673 String tag, int id, Notification notification, int[] idOut)
674 {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500675 enqueueNotificationInternal(pkg, callingUid, callingPid, tag, id,
676 ((notification.flags & Notification.FLAG_ONGOING_EVENT) != 0)
677 ? StatusBarNotification.PRIORITY_ONGOING
678 : StatusBarNotification.PRIORITY_NORMAL,
679 notification, idOut);
680 }
681 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
682 String tag, int id, int priority, Notification notification, int[] idOut)
683 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700684 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800685
Joe Onoratobd73d012010-06-04 11:44:54 -0700686 // Limit the number of notifications that any given package except the android
687 // package can enqueue. Prevents DOS attacks and deals with leaks.
688 if (!"android".equals(pkg)) {
689 synchronized (mNotificationList) {
690 int count = 0;
691 final int N = mNotificationList.size();
692 for (int i=0; i<N; i++) {
693 final NotificationRecord r = mNotificationList.get(i);
694 if (r.pkg.equals(pkg)) {
695 count++;
696 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
697 Slog.e(TAG, "Package has already posted " + count
698 + " notifications. Not showing more. package=" + pkg);
699 return;
700 }
701 }
702 }
703 }
704 }
705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 // This conditional is a dirty hack to limit the logging done on
707 // behalf of the download manager without affecting other apps.
708 if (!pkg.equals("com.android.providers.downloads")
709 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800710 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712
713 if (pkg == null || notification == null) {
714 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
715 + " id=" + id + " notification=" + notification);
716 }
717 if (notification.icon != 0) {
718 if (notification.contentView == null) {
719 throw new IllegalArgumentException("contentView required: pkg=" + pkg
720 + " id=" + id + " notification=" + notification);
721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 }
723
724 synchronized (mNotificationList) {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500725 NotificationRecord r = new NotificationRecord(pkg, tag, id,
726 callingUid, callingPid,
727 priority,
728 notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 NotificationRecord old = null;
730
Fred Quintana6ecaff12009-09-25 14:23:13 -0700731 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 if (index < 0) {
733 mNotificationList.add(r);
734 } else {
735 old = mNotificationList.remove(index);
736 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700737 // Make sure we don't lose the foreground service state.
738 if (old != null) {
739 notification.flags |=
740 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800743
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700744 // Ensure if this is a foreground service that the proper additional
745 // flags are set.
746 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
747 notification.flags |= Notification.FLAG_ONGOING_EVENT
748 | Notification.FLAG_NO_CLEAR;
749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 if (notification.icon != 0) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700752 StatusBarNotification n = new StatusBarNotification(pkg, id, tag,
753 r.uid, r.initialPid, notification);
Daniel Sandlere40451a2011-02-03 14:51:35 -0500754 n.priority = r.priority;
755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 if (old != null && old.statusBarKey != null) {
757 r.statusBarKey = old.statusBarKey;
758 long identity = Binder.clearCallingIdentity();
759 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700760 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 }
762 finally {
763 Binder.restoreCallingIdentity(identity);
764 }
765 } else {
766 long identity = Binder.clearCallingIdentity();
767 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700768 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500769 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
771 finally {
772 Binder.restoreCallingIdentity(identity);
773 }
774 }
Joe Onorato30275482009-07-08 17:09:14 -0700775 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500777 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 if (old != null && old.statusBarKey != null) {
779 long identity = Binder.clearCallingIdentity();
780 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700781 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 }
783 finally {
784 Binder.restoreCallingIdentity(identity);
785 }
786 }
787 }
788
789 // If we're not supposed to beep, vibrate, etc. then don't.
790 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
791 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700792 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
793 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800794
795 final AudioManager audioManager = (AudioManager) mContext
796 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 // sound
798 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800799 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 if (useDefaultSound || notification.sound != null) {
801 Uri uri;
802 if (useDefaultSound) {
803 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
804 } else {
805 uri = notification.sound;
806 }
807 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
808 int audioStreamType;
809 if (notification.audioStreamType >= 0) {
810 audioStreamType = notification.audioStreamType;
811 } else {
812 audioStreamType = DEFAULT_STREAM_TYPE;
813 }
814 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800815 // do not play notifications if stream volume is 0
816 // (typically because ringer mode is silent).
817 if (audioManager.getStreamVolume(audioStreamType) != 0) {
818 long identity = Binder.clearCallingIdentity();
819 try {
820 mSound.play(mContext, uri, looping, audioStreamType);
821 }
822 finally {
823 Binder.restoreCallingIdentity(identity);
824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 }
826 }
827
828 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800830 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 if ((useDefaultVibrate || notification.vibrate != null)
832 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
833 mVibrateNotification = r;
834
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800835 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 : notification.vibrate,
837 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
838 }
839 }
840
841 // this option doesn't shut off the lights
842
843 // light
844 // the most recent thing gets the light
845 mLights.remove(old);
846 if (mLedNotification == old) {
847 mLedNotification = null;
848 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800849 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
851 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
852 mLights.add(r);
853 updateLightsLocked();
854 } else {
855 if (old != null
856 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
857 updateLightsLocked();
858 }
859 }
860 }
861
862 idOut[0] = id;
863 }
864
Joe Onorato30275482009-07-08 17:09:14 -0700865 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700866 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
867 if (!manager.isEnabled()) {
868 return;
869 }
870
871 AccessibilityEvent event =
872 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
873 event.setPackageName(packageName);
874 event.setClassName(Notification.class.getName());
875 event.setParcelableData(notification);
876 CharSequence tickerText = notification.tickerText;
877 if (!TextUtils.isEmpty(tickerText)) {
878 event.getText().add(tickerText);
879 }
880
881 manager.sendAccessibilityEvent(event);
882 }
883
Joe Onorato46439ce2010-11-19 13:56:21 -0800884 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
885 // tell the app
886 if (sendDelete) {
887 if (r.notification.deleteIntent != null) {
888 try {
889 r.notification.deleteIntent.send();
890 } catch (PendingIntent.CanceledException ex) {
891 // do nothing - there's no relevant way to recover, and
892 // no reason to let this propagate
893 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
894 }
895 }
896 }
897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 // status bar
899 if (r.notification.icon != 0) {
900 long identity = Binder.clearCallingIdentity();
901 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700902 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
904 finally {
905 Binder.restoreCallingIdentity(identity);
906 }
907 r.statusBarKey = null;
908 }
909
910 // sound
911 if (mSoundNotification == r) {
912 mSoundNotification = null;
913 long identity = Binder.clearCallingIdentity();
914 try {
915 mSound.stop();
916 }
917 finally {
918 Binder.restoreCallingIdentity(identity);
919 }
920 }
921
922 // vibrate
923 if (mVibrateNotification == r) {
924 mVibrateNotification = null;
925 long identity = Binder.clearCallingIdentity();
926 try {
927 mVibrator.cancel();
928 }
929 finally {
930 Binder.restoreCallingIdentity(identity);
931 }
932 }
933
934 // light
935 mLights.remove(r);
936 if (mLedNotification == r) {
937 mLedNotification = null;
938 }
939 }
940
941 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700942 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800943 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700945 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Joe Onorato46439ce2010-11-19 13:56:21 -0800946 int mustNotHaveFlags, boolean sendDelete) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800947 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948
949 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700950 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700952 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
955 return;
956 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700957 if ((r.notification.flags & mustNotHaveFlags) != 0) {
958 return;
959 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 mNotificationList.remove(index);
962
Joe Onorato46439ce2010-11-19 13:56:21 -0800963 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 updateLightsLocked();
965 }
966 }
967 }
968
969 /**
970 * Cancels all notifications from a given package that have all of the
971 * {@code mustHaveFlags}.
972 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800973 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
974 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800975 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976
977 synchronized (mNotificationList) {
978 final int N = mNotificationList.size();
979 boolean canceledSomething = false;
980 for (int i = N-1; i >= 0; --i) {
981 NotificationRecord r = mNotificationList.get(i);
982 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
983 continue;
984 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700985 if ((r.notification.flags & mustNotHaveFlags) != 0) {
986 continue;
987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 if (!r.pkg.equals(pkg)) {
989 continue;
990 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800991 canceledSomething = true;
992 if (!doit) {
993 return true;
994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -0800996 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 }
998 if (canceledSomething) {
999 updateLightsLocked();
1000 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001001 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
1003 }
1004
Andy Stadler110988c2010-12-03 14:29:16 -08001005 @Deprecated
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001006 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001007 cancelNotificationWithTag(pkg, null /* tag */, id);
1008 }
1009
1010 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001011 checkIncomingCall(pkg);
1012 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001013 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001014 Binder.getCallingUid() == Process.SYSTEM_UID
Joe Onorato46439ce2010-11-19 13:56:21 -08001015 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 }
1017
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001018 public void cancelAllNotifications(String pkg) {
1019 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001020
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001021 // Calling from user space, don't allow the canceling of actively
1022 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001023 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 }
1025
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001026 void checkIncomingCall(String pkg) {
1027 int uid = Binder.getCallingUid();
1028 if (uid == Process.SYSTEM_UID || uid == 0) {
1029 return;
1030 }
1031 try {
1032 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
1033 pkg, 0);
1034 if (ai.uid != uid) {
1035 throw new SecurityException("Calling uid " + uid + " gave package"
1036 + pkg + " which is owned by uid " + ai.uid);
1037 }
1038 } catch (PackageManager.NameNotFoundException e) {
1039 throw new SecurityException("Unknown package " + pkg);
1040 }
1041 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001042
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001043 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 synchronized (mNotificationList) {
1045 final int N = mNotificationList.size();
1046 for (int i=N-1; i>=0; i--) {
1047 NotificationRecord r = mNotificationList.get(i);
1048
1049 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1050 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001052 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 }
1054 }
1055
1056 updateLightsLocked();
1057 }
1058 }
1059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 // lock on mNotificationList
1061 private void updateLightsLocked()
1062 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001063 // handle notification lights
1064 if (mLedNotification == null) {
1065 // get next notification, if any
1066 int n = mLights.size();
1067 if (n > 0) {
1068 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
1070 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001071
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001072 // Don't flash while we are in a call or screen is on
1073 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001074 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001075 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001076 int ledARGB = mLedNotification.notification.ledARGB;
1077 int ledOnMS = mLedNotification.notification.ledOnMS;
1078 int ledOffMS = mLedNotification.notification.ledOffMS;
1079 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1080 ledARGB = mDefaultNotificationColor;
1081 ledOnMS = mDefaultNotificationLedOn;
1082 ledOffMS = mDefaultNotificationLedOff;
1083 }
1084 if (mNotificationPulseEnabled) {
1085 // pulse repeatedly
1086 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1087 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05001088 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 }
1091
1092 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001093 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 {
1095 ArrayList<NotificationRecord> list = mNotificationList;
1096 final int len = list.size();
1097 for (int i=0; i<len; i++) {
1098 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001099 if (tag == null) {
1100 if (r.tag != null) {
1101 continue;
1102 }
1103 } else {
1104 if (!tag.equals(r.tag)) {
1105 continue;
1106 }
1107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 if (r.id == id && r.pkg.equals(pkg)) {
1109 return i;
1110 }
1111 }
1112 return -1;
1113 }
1114
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001115 private void updateNotificationPulse() {
1116 synchronized (mNotificationList) {
1117 updateLightsLocked();
1118 }
1119 }
1120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 // ======================================================================
1122 @Override
1123 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1124 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1125 != PackageManager.PERMISSION_GRANTED) {
1126 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1127 + Binder.getCallingPid()
1128 + ", uid=" + Binder.getCallingUid());
1129 return;
1130 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 pw.println("Current Notification Manager state:");
1133
1134 int N;
1135
1136 synchronized (mToastQueue) {
1137 N = mToastQueue.size();
1138 if (N > 0) {
1139 pw.println(" Toast Queue:");
1140 for (int i=0; i<N; i++) {
1141 mToastQueue.get(i).dump(pw, " ");
1142 }
1143 pw.println(" ");
1144 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 }
1147
1148 synchronized (mNotificationList) {
1149 N = mNotificationList.size();
1150 if (N > 0) {
1151 pw.println(" Notification List:");
1152 for (int i=0; i<N; i++) {
1153 mNotificationList.get(i).dump(pw, " ", mContext);
1154 }
1155 pw.println(" ");
1156 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 N = mLights.size();
1159 if (N > 0) {
1160 pw.println(" Lights List:");
1161 for (int i=0; i<N; i++) {
1162 mLights.get(i).dump(pw, " ", mContext);
1163 }
1164 pw.println(" ");
1165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 pw.println(" mSoundNotification=" + mSoundNotification);
1168 pw.println(" mSound=" + mSound);
1169 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001170 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1171 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 }
1173 }
1174}