blob: 6edb132eb73c96917e3272a272ac367de69e8d65 [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
104 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;
Mike Lockwood2117f6f2010-09-09 09:48:08 -0400107 // This is true if we have received a new notification while the screen is off
108 // (that is, if mLedNotification was set while the screen was off)
109 // This is reset to false when the screen is turned on.
110 private boolean mPendingPulseNotification;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500111
Fred Quintana6ecaff12009-09-25 14:23:13 -0700112 private final ArrayList<NotificationRecord> mNotificationList =
113 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114
115 private ArrayList<ToastRecord> mToastQueue;
116
117 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 private static String idDebugString(Context baseContext, String packageName, int id) {
121 Context c = null;
122
123 if (packageName != null) {
124 try {
125 c = baseContext.createPackageContext(packageName, 0);
126 } catch (NameNotFoundException e) {
127 c = baseContext;
128 }
129 } else {
130 c = baseContext;
131 }
132
133 String pkg;
134 String type;
135 String name;
136
137 Resources r = c.getResources();
138 try {
139 return r.getResourceName(id);
140 } catch (Resources.NotFoundException e) {
141 return "<name unknown>";
142 }
143 }
144
145 private static final class NotificationRecord
146 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700147 final String pkg;
148 final String tag;
149 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700150 final int uid;
151 final int initialPid;
Daniel Sandlere40451a2011-02-03 14:51:35 -0500152 final int priority;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700153 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 IBinder statusBarKey;
155
Daniel Sandlere40451a2011-02-03 14:51:35 -0500156 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid, int priority,
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700157 Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 {
159 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700160 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700162 this.uid = uid;
163 this.initialPid = initialPid;
Daniel Sandlere40451a2011-02-03 14:51:35 -0500164 this.priority = priority;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 this.notification = notification;
166 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 void dump(PrintWriter pw, String prefix, Context baseContext) {
169 pw.println(prefix + this);
170 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
171 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
172 pw.println(prefix + " contentIntent=" + notification.contentIntent);
173 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
174 pw.println(prefix + " tickerText=" + notification.tickerText);
175 pw.println(prefix + " contentView=" + notification.contentView);
176 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
177 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
178 pw.println(prefix + " sound=" + notification.sound);
179 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
180 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
181 + " ledOnMS=" + notification.ledOnMS
182 + " ledOffMS=" + notification.ledOffMS);
183 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 @Override
186 public final String toString()
187 {
188 return "NotificationRecord{"
189 + Integer.toHexString(System.identityHashCode(this))
190 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700191 + " id=" + Integer.toHexString(id)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500192 + " tag=" + tag
193 + " pri=" + priority
194 + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 }
196 }
197
198 private static final class ToastRecord
199 {
200 final int pid;
201 final String pkg;
202 final ITransientNotification callback;
203 int duration;
204
205 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
206 {
207 this.pid = pid;
208 this.pkg = pkg;
209 this.callback = callback;
210 this.duration = duration;
211 }
212
213 void update(int duration) {
214 this.duration = duration;
215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 void dump(PrintWriter pw, String prefix) {
218 pw.println(prefix + this);
219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 @Override
222 public final String toString()
223 {
224 return "ToastRecord{"
225 + Integer.toHexString(System.identityHashCode(this))
226 + " pkg=" + pkg
227 + " callback=" + callback
228 + " duration=" + duration;
229 }
230 }
231
Joe Onorato089de882010-04-12 08:18:45 -0700232 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
233 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234
235 public void onSetDisabled(int status) {
236 synchronized (mNotificationList) {
237 mDisabledNotifications = status;
238 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
239 // cancel whatever's going on
240 long identity = Binder.clearCallingIdentity();
241 try {
242 mSound.stop();
243 }
244 finally {
245 Binder.restoreCallingIdentity(identity);
246 }
247
248 identity = Binder.clearCallingIdentity();
249 try {
250 mVibrator.cancel();
251 }
252 finally {
253 Binder.restoreCallingIdentity(identity);
254 }
255 }
256 }
257 }
258
259 public void onClearAll() {
260 cancelAll();
261 }
262
Fred Quintana6ecaff12009-09-25 14:23:13 -0700263 public void onNotificationClick(String pkg, String tag, int id) {
264 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
jhtop.kim2e448f72011-07-13 17:15:32 +0900265 Notification.FLAG_FOREGROUND_SERVICE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 }
267
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400268 public void onNotificationClear(String pkg, String tag, int id) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800269 cancelNotification(pkg, tag, id, 0,
270 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
271 true);
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400272 }
273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 public void onPanelRevealed() {
275 synchronized (mNotificationList) {
276 // sound
277 mSoundNotification = null;
278 long identity = Binder.clearCallingIdentity();
279 try {
280 mSound.stop();
281 }
282 finally {
283 Binder.restoreCallingIdentity(identity);
284 }
285
286 // vibrate
287 mVibrateNotification = null;
288 identity = Binder.clearCallingIdentity();
289 try {
290 mVibrator.cancel();
291 }
292 finally {
293 Binder.restoreCallingIdentity(identity);
294 }
295
296 // light
297 mLights.clear();
298 mLedNotification = null;
299 updateLightsLocked();
300 }
301 }
Joe Onorato005847b2010-06-04 16:08:02 -0400302
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700303 public void onNotificationError(String pkg, String tag, int id,
304 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400305 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
306 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Joe Onorato46439ce2010-11-19 13:56:21 -0800307 cancelNotification(pkg, tag, id, 0, 0, false);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700308 long ident = Binder.clearCallingIdentity();
309 try {
310 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
311 "Bad notification posted from package " + pkg
312 + ": " + message);
313 } catch (RemoteException e) {
314 }
315 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 };
318
319 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
320 @Override
321 public void onReceive(Context context, Intent intent) {
322 String action = intent.getAction();
323
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800324 boolean queryRestart = false;
325
Mike Lockwood541c9942011-06-12 19:35:45 -0400326 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800327 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400328 || action.equals(Intent.ACTION_PACKAGE_CHANGED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800329 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800330 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800331 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800332 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800333 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800334 } else if (queryRestart) {
335 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800336 } else {
337 Uri uri = intent.getData();
338 if (uri == null) {
339 return;
340 }
341 String pkgName = uri.getSchemeSpecificPart();
342 if (pkgName == null) {
343 return;
344 }
345 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800347 if (pkgList != null && (pkgList.length > 0)) {
348 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800349 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500352 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
353 mScreenOn = true;
354 updateNotificationPulse();
355 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
356 mScreenOn = false;
357 updateNotificationPulse();
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500358 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
359 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK));
360 updateNotificationPulse();
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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800432 IntentFilter pkgFilter = new IntentFilter();
433 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400434 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800435 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
436 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
437 pkgFilter.addDataScheme("package");
438 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800439 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800440 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800441
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500442 SettingsObserver observer = new SettingsObserver(mHandler);
443 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445
Joe Onorato30275482009-07-08 17:09:14 -0700446 void systemReady() {
447 // no beeping until we're basically done booting
448 mSystemReady = true;
449 }
450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 // Toasts
452 // ============================================================================
453 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
454 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400455 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456
457 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800458 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 return ;
460 }
461
462 synchronized (mToastQueue) {
463 int callingPid = Binder.getCallingPid();
464 long callingId = Binder.clearCallingIdentity();
465 try {
466 ToastRecord record;
467 int index = indexOfToastLocked(pkg, callback);
468 // If it's already in the queue, we update it in place, we don't
469 // move it to the end of the queue.
470 if (index >= 0) {
471 record = mToastQueue.get(index);
472 record.update(duration);
473 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800474 // Limit the number of toasts that any given package except the android
475 // package can enqueue. Prevents DOS attacks and deals with leaks.
476 if (!"android".equals(pkg)) {
477 int count = 0;
478 final int N = mToastQueue.size();
479 for (int i=0; i<N; i++) {
480 final ToastRecord r = mToastQueue.get(i);
481 if (r.pkg.equals(pkg)) {
482 count++;
483 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
484 Slog.e(TAG, "Package has already posted " + count
485 + " toasts. Not showing more. Package=" + pkg);
486 return;
487 }
488 }
489 }
490 }
491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 record = new ToastRecord(callingPid, pkg, callback, duration);
493 mToastQueue.add(record);
494 index = mToastQueue.size() - 1;
495 keepProcessAliveLocked(callingPid);
496 }
497 // If it's at index 0, it's the current toast. It doesn't matter if it's
498 // new or just been updated. Call back and tell it to show itself.
499 // If the callback fails, this will remove it from the list, so don't
500 // assume that it's valid after this.
501 if (index == 0) {
502 showNextToastLocked();
503 }
504 } finally {
505 Binder.restoreCallingIdentity(callingId);
506 }
507 }
508 }
509
510 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800511 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512
513 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800514 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 return ;
516 }
517
518 synchronized (mToastQueue) {
519 long callingId = Binder.clearCallingIdentity();
520 try {
521 int index = indexOfToastLocked(pkg, callback);
522 if (index >= 0) {
523 cancelToastLocked(index);
524 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800525 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527 } finally {
528 Binder.restoreCallingIdentity(callingId);
529 }
530 }
531 }
532
533 private void showNextToastLocked() {
534 ToastRecord record = mToastQueue.get(0);
535 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800536 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 try {
538 record.callback.show();
539 scheduleTimeoutLocked(record, false);
540 return;
541 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800542 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 + " in package " + record.pkg);
544 // remove it from the list and let the process die
545 int index = mToastQueue.indexOf(record);
546 if (index >= 0) {
547 mToastQueue.remove(index);
548 }
549 keepProcessAliveLocked(record.pid);
550 if (mToastQueue.size() > 0) {
551 record = mToastQueue.get(0);
552 } else {
553 record = null;
554 }
555 }
556 }
557 }
558
559 private void cancelToastLocked(int index) {
560 ToastRecord record = mToastQueue.get(index);
561 try {
562 record.callback.hide();
563 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800564 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 + " in package " + record.pkg);
566 // don't worry about this, we're about to remove it from
567 // the list anyway
568 }
569 mToastQueue.remove(index);
570 keepProcessAliveLocked(record.pid);
571 if (mToastQueue.size() > 0) {
572 // Show the next one. If the callback fails, this will remove
573 // it from the list, so don't assume that the list hasn't changed
574 // after this point.
575 showNextToastLocked();
576 }
577 }
578
579 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
580 {
581 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
582 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
583 mHandler.removeCallbacksAndMessages(r);
584 mHandler.sendMessageDelayed(m, delay);
585 }
586
587 private void handleTimeout(ToastRecord record)
588 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800589 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 synchronized (mToastQueue) {
591 int index = indexOfToastLocked(record.pkg, record.callback);
592 if (index >= 0) {
593 cancelToastLocked(index);
594 }
595 }
596 }
597
598 // lock on mToastQueue
599 private int indexOfToastLocked(String pkg, ITransientNotification callback)
600 {
601 IBinder cbak = callback.asBinder();
602 ArrayList<ToastRecord> list = mToastQueue;
603 int len = list.size();
604 for (int i=0; i<len; i++) {
605 ToastRecord r = list.get(i);
606 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
607 return i;
608 }
609 }
610 return -1;
611 }
612
613 // lock on mToastQueue
614 private void keepProcessAliveLocked(int pid)
615 {
616 int toastCount = 0; // toasts from this pid
617 ArrayList<ToastRecord> list = mToastQueue;
618 int N = list.size();
619 for (int i=0; i<N; i++) {
620 ToastRecord r = list.get(i);
621 if (r.pid == pid) {
622 toastCount++;
623 }
624 }
625 try {
626 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
627 } catch (RemoteException e) {
628 // Shouldn't happen.
629 }
630 }
631
632 private final class WorkerHandler extends Handler
633 {
634 @Override
635 public void handleMessage(Message msg)
636 {
637 switch (msg.what)
638 {
639 case MESSAGE_TIMEOUT:
640 handleTimeout((ToastRecord)msg.obj);
641 break;
642 }
643 }
644 }
645
646
647 // Notifications
648 // ============================================================================
Andy Stadler110988c2010-12-03 14:29:16 -0800649 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
651 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700652 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
653 }
654
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400655 public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,
656 int[] idOut)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700657 {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400658 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
659 tag, id, notification, idOut);
660 }
661
Daniel Sandlere40451a2011-02-03 14:51:35 -0500662 public void enqueueNotificationWithTagPriority(String pkg, String tag, int id, int priority,
663 Notification notification, int[] idOut)
664 {
665 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
666 tag, id, priority, notification, idOut);
667 }
668
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400669 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
670 // uid/pid of another application)
671 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
672 String tag, int id, Notification notification, int[] idOut)
673 {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500674 enqueueNotificationInternal(pkg, callingUid, callingPid, tag, id,
675 ((notification.flags & Notification.FLAG_ONGOING_EVENT) != 0)
676 ? StatusBarNotification.PRIORITY_ONGOING
677 : StatusBarNotification.PRIORITY_NORMAL,
678 notification, idOut);
679 }
680 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
681 String tag, int id, int priority, Notification notification, int[] idOut)
682 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700683 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800684
Joe Onoratobd73d012010-06-04 11:44:54 -0700685 // Limit the number of notifications that any given package except the android
686 // package can enqueue. Prevents DOS attacks and deals with leaks.
687 if (!"android".equals(pkg)) {
688 synchronized (mNotificationList) {
689 int count = 0;
690 final int N = mNotificationList.size();
691 for (int i=0; i<N; i++) {
692 final NotificationRecord r = mNotificationList.get(i);
693 if (r.pkg.equals(pkg)) {
694 count++;
695 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
696 Slog.e(TAG, "Package has already posted " + count
697 + " notifications. Not showing more. package=" + pkg);
698 return;
699 }
700 }
701 }
702 }
703 }
704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 // This conditional is a dirty hack to limit the logging done on
706 // behalf of the download manager without affecting other apps.
707 if (!pkg.equals("com.android.providers.downloads")
708 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800709 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
711
712 if (pkg == null || notification == null) {
713 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
714 + " id=" + id + " notification=" + notification);
715 }
716 if (notification.icon != 0) {
717 if (notification.contentView == null) {
718 throw new IllegalArgumentException("contentView required: pkg=" + pkg
719 + " id=" + id + " notification=" + notification);
720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 }
722
723 synchronized (mNotificationList) {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500724 NotificationRecord r = new NotificationRecord(pkg, tag, id,
725 callingUid, callingPid,
726 priority,
727 notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 NotificationRecord old = null;
729
Fred Quintana6ecaff12009-09-25 14:23:13 -0700730 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 if (index < 0) {
732 mNotificationList.add(r);
733 } else {
734 old = mNotificationList.remove(index);
735 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700736 // Make sure we don't lose the foreground service state.
737 if (old != null) {
738 notification.flags |=
739 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800742
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700743 // Ensure if this is a foreground service that the proper additional
744 // flags are set.
745 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
746 notification.flags |= Notification.FLAG_ONGOING_EVENT
747 | Notification.FLAG_NO_CLEAR;
748 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 if (notification.icon != 0) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700751 StatusBarNotification n = new StatusBarNotification(pkg, id, tag,
752 r.uid, r.initialPid, notification);
Daniel Sandlere40451a2011-02-03 14:51:35 -0500753 n.priority = r.priority;
754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 if (old != null && old.statusBarKey != null) {
756 r.statusBarKey = old.statusBarKey;
757 long identity = Binder.clearCallingIdentity();
758 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700759 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 }
761 finally {
762 Binder.restoreCallingIdentity(identity);
763 }
764 } else {
765 long identity = Binder.clearCallingIdentity();
766 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700767 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500768 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 }
770 finally {
771 Binder.restoreCallingIdentity(identity);
772 }
773 }
Joe Onorato30275482009-07-08 17:09:14 -0700774 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500776 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 if (old != null && old.statusBarKey != null) {
778 long identity = Binder.clearCallingIdentity();
779 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700780 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 }
782 finally {
783 Binder.restoreCallingIdentity(identity);
784 }
785 }
786 }
787
788 // If we're not supposed to beep, vibrate, etc. then don't.
789 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
790 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700791 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
792 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800793
794 final AudioManager audioManager = (AudioManager) mContext
795 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 // sound
797 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800798 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 if (useDefaultSound || notification.sound != null) {
800 Uri uri;
801 if (useDefaultSound) {
802 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
803 } else {
804 uri = notification.sound;
805 }
806 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
807 int audioStreamType;
808 if (notification.audioStreamType >= 0) {
809 audioStreamType = notification.audioStreamType;
810 } else {
811 audioStreamType = DEFAULT_STREAM_TYPE;
812 }
813 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800814 // do not play notifications if stream volume is 0
815 // (typically because ringer mode is silent).
816 if (audioManager.getStreamVolume(audioStreamType) != 0) {
817 long identity = Binder.clearCallingIdentity();
818 try {
819 mSound.play(mContext, uri, looping, audioStreamType);
820 }
821 finally {
822 Binder.restoreCallingIdentity(identity);
823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 }
825 }
826
827 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800829 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 if ((useDefaultVibrate || notification.vibrate != null)
831 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
832 mVibrateNotification = r;
833
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800834 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 : notification.vibrate,
836 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
837 }
838 }
839
840 // this option doesn't shut off the lights
841
842 // light
843 // the most recent thing gets the light
844 mLights.remove(old);
845 if (mLedNotification == old) {
846 mLedNotification = null;
847 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800848 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
850 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
851 mLights.add(r);
852 updateLightsLocked();
853 } else {
854 if (old != null
855 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
856 updateLightsLocked();
857 }
858 }
859 }
860
861 idOut[0] = id;
862 }
863
Joe Onorato30275482009-07-08 17:09:14 -0700864 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700865 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
866 if (!manager.isEnabled()) {
867 return;
868 }
869
870 AccessibilityEvent event =
871 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
872 event.setPackageName(packageName);
873 event.setClassName(Notification.class.getName());
874 event.setParcelableData(notification);
875 CharSequence tickerText = notification.tickerText;
876 if (!TextUtils.isEmpty(tickerText)) {
877 event.getText().add(tickerText);
878 }
879
880 manager.sendAccessibilityEvent(event);
881 }
882
Joe Onorato46439ce2010-11-19 13:56:21 -0800883 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
884 // tell the app
885 if (sendDelete) {
886 if (r.notification.deleteIntent != null) {
887 try {
888 r.notification.deleteIntent.send();
889 } catch (PendingIntent.CanceledException ex) {
890 // do nothing - there's no relevant way to recover, and
891 // no reason to let this propagate
892 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
893 }
894 }
895 }
896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 // status bar
898 if (r.notification.icon != 0) {
899 long identity = Binder.clearCallingIdentity();
900 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700901 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 }
903 finally {
904 Binder.restoreCallingIdentity(identity);
905 }
906 r.statusBarKey = null;
907 }
908
909 // sound
910 if (mSoundNotification == r) {
911 mSoundNotification = null;
912 long identity = Binder.clearCallingIdentity();
913 try {
914 mSound.stop();
915 }
916 finally {
917 Binder.restoreCallingIdentity(identity);
918 }
919 }
920
921 // vibrate
922 if (mVibrateNotification == r) {
923 mVibrateNotification = null;
924 long identity = Binder.clearCallingIdentity();
925 try {
926 mVibrator.cancel();
927 }
928 finally {
929 Binder.restoreCallingIdentity(identity);
930 }
931 }
932
933 // light
934 mLights.remove(r);
935 if (mLedNotification == r) {
936 mLedNotification = null;
937 }
938 }
939
940 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700941 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800942 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700944 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Joe Onorato46439ce2010-11-19 13:56:21 -0800945 int mustNotHaveFlags, boolean sendDelete) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800946 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947
948 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700949 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700951 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
954 return;
955 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700956 if ((r.notification.flags & mustNotHaveFlags) != 0) {
957 return;
958 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 mNotificationList.remove(index);
961
Joe Onorato46439ce2010-11-19 13:56:21 -0800962 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 updateLightsLocked();
964 }
965 }
966 }
967
968 /**
969 * Cancels all notifications from a given package that have all of the
970 * {@code mustHaveFlags}.
971 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800972 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
973 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800974 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975
976 synchronized (mNotificationList) {
977 final int N = mNotificationList.size();
978 boolean canceledSomething = false;
979 for (int i = N-1; i >= 0; --i) {
980 NotificationRecord r = mNotificationList.get(i);
981 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
982 continue;
983 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700984 if ((r.notification.flags & mustNotHaveFlags) != 0) {
985 continue;
986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 if (!r.pkg.equals(pkg)) {
988 continue;
989 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800990 canceledSomething = true;
991 if (!doit) {
992 return true;
993 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -0800995 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 }
997 if (canceledSomething) {
998 updateLightsLocked();
999 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001000 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 }
1002 }
1003
Andy Stadler110988c2010-12-03 14:29:16 -08001004 @Deprecated
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001005 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001006 cancelNotificationWithTag(pkg, null /* tag */, id);
1007 }
1008
1009 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001010 checkIncomingCall(pkg);
1011 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001012 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001013 Binder.getCallingUid() == Process.SYSTEM_UID
Joe Onorato46439ce2010-11-19 13:56:21 -08001014 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 }
1016
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001017 public void cancelAllNotifications(String pkg) {
1018 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001019
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001020 // Calling from user space, don't allow the canceling of actively
1021 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001022 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 }
1024
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001025 void checkIncomingCall(String pkg) {
1026 int uid = Binder.getCallingUid();
1027 if (uid == Process.SYSTEM_UID || uid == 0) {
1028 return;
1029 }
1030 try {
1031 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
1032 pkg, 0);
1033 if (ai.uid != uid) {
1034 throw new SecurityException("Calling uid " + uid + " gave package"
1035 + pkg + " which is owned by uid " + ai.uid);
1036 }
1037 } catch (PackageManager.NameNotFoundException e) {
1038 throw new SecurityException("Unknown package " + pkg);
1039 }
1040 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001041
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001042 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 synchronized (mNotificationList) {
1044 final int N = mNotificationList.size();
1045 for (int i=N-1; i>=0; i--) {
1046 NotificationRecord r = mNotificationList.get(i);
1047
1048 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1049 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001051 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 }
1053 }
1054
1055 updateLightsLocked();
1056 }
1057 }
1058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 // lock on mNotificationList
1060 private void updateLightsLocked()
1061 {
Mike Lockwood2117f6f2010-09-09 09:48:08 -04001062 // clear pending pulse notification if screen is on
1063 if (mScreenOn || mLedNotification == null) {
1064 mPendingPulseNotification = false;
1065 }
1066
The Android Open Source Project10592532009-03-18 17:39:46 -07001067 // handle notification lights
1068 if (mLedNotification == null) {
1069 // get next notification, if any
1070 int n = mLights.size();
1071 if (n > 0) {
1072 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
Mike Lockwood2117f6f2010-09-09 09:48:08 -04001074 if (mLedNotification != null && !mScreenOn) {
1075 mPendingPulseNotification = true;
1076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001078
1079 // we only flash if screen is off and persistent pulsing is enabled
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001080 // and we are not currently in a call
Mike Lockwood2117f6f2010-09-09 09:48:08 -04001081 if (!mPendingPulseNotification || mScreenOn || mInCall) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001082 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001083 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001084 int ledARGB = mLedNotification.notification.ledARGB;
1085 int ledOnMS = mLedNotification.notification.ledOnMS;
1086 int ledOffMS = mLedNotification.notification.ledOffMS;
1087 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1088 ledARGB = mDefaultNotificationColor;
1089 ledOnMS = mDefaultNotificationLedOn;
1090 ledOffMS = mDefaultNotificationLedOff;
1091 }
1092 if (mNotificationPulseEnabled) {
1093 // pulse repeatedly
1094 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1095 ledOnMS, ledOffMS);
1096 } else {
1097 // pulse only once
1098 mNotificationLight.pulse(ledARGB, ledOnMS);
1099 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 }
1102
1103 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001104 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 {
1106 ArrayList<NotificationRecord> list = mNotificationList;
1107 final int len = list.size();
1108 for (int i=0; i<len; i++) {
1109 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001110 if (tag == null) {
1111 if (r.tag != null) {
1112 continue;
1113 }
1114 } else {
1115 if (!tag.equals(r.tag)) {
1116 continue;
1117 }
1118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 if (r.id == id && r.pkg.equals(pkg)) {
1120 return i;
1121 }
1122 }
1123 return -1;
1124 }
1125
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001126 private void updateNotificationPulse() {
1127 synchronized (mNotificationList) {
1128 updateLightsLocked();
1129 }
1130 }
1131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 // ======================================================================
1133 @Override
1134 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1135 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1136 != PackageManager.PERMISSION_GRANTED) {
1137 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1138 + Binder.getCallingPid()
1139 + ", uid=" + Binder.getCallingUid());
1140 return;
1141 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 pw.println("Current Notification Manager state:");
1144
1145 int N;
1146
1147 synchronized (mToastQueue) {
1148 N = mToastQueue.size();
1149 if (N > 0) {
1150 pw.println(" Toast Queue:");
1151 for (int i=0; i<N; i++) {
1152 mToastQueue.get(i).dump(pw, " ");
1153 }
1154 pw.println(" ");
1155 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
1158
1159 synchronized (mNotificationList) {
1160 N = mNotificationList.size();
1161 if (N > 0) {
1162 pw.println(" Notification List:");
1163 for (int i=0; i<N; i++) {
1164 mNotificationList.get(i).dump(pw, " ", mContext);
1165 }
1166 pw.println(" ");
1167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 N = mLights.size();
1170 if (N > 0) {
1171 pw.println(" Lights List:");
1172 for (int i=0; i<N; i++) {
1173 mLights.get(i).dump(pw, " ", mContext);
1174 }
1175 pw.println(" ");
1176 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 pw.println(" mSoundNotification=" + mSoundNotification);
1179 pw.println(" mSound=" + mSound);
1180 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001181 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1182 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 }
1184 }
1185}