blob: 7a3a344c0a7607b8126f2758f72aee0eade2b581 [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
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500104 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500105 private boolean mNotificationPulseEnabled;
106
Fred Quintana6ecaff12009-09-25 14:23:13 -0700107 private final ArrayList<NotificationRecord> mNotificationList =
108 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
110 private ArrayList<ToastRecord> mToastQueue;
111
112 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 private static String idDebugString(Context baseContext, String packageName, int id) {
116 Context c = null;
117
118 if (packageName != null) {
119 try {
120 c = baseContext.createPackageContext(packageName, 0);
121 } catch (NameNotFoundException e) {
122 c = baseContext;
123 }
124 } else {
125 c = baseContext;
126 }
127
128 String pkg;
129 String type;
130 String name;
131
132 Resources r = c.getResources();
133 try {
134 return r.getResourceName(id);
135 } catch (Resources.NotFoundException e) {
136 return "<name unknown>";
137 }
138 }
139
140 private static final class NotificationRecord
141 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700142 final String pkg;
143 final String tag;
144 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700145 final int uid;
146 final int initialPid;
Daniel Sandlere40451a2011-02-03 14:51:35 -0500147 final int priority;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700148 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 IBinder statusBarKey;
150
Daniel Sandlere40451a2011-02-03 14:51:35 -0500151 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid, int priority,
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700152 Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 {
154 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700155 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700157 this.uid = uid;
158 this.initialPid = initialPid;
Daniel Sandlere40451a2011-02-03 14:51:35 -0500159 this.priority = priority;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 this.notification = notification;
161 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 void dump(PrintWriter pw, String prefix, Context baseContext) {
164 pw.println(prefix + this);
165 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
166 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
167 pw.println(prefix + " contentIntent=" + notification.contentIntent);
168 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
169 pw.println(prefix + " tickerText=" + notification.tickerText);
170 pw.println(prefix + " contentView=" + notification.contentView);
171 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
172 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
173 pw.println(prefix + " sound=" + notification.sound);
174 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
175 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
176 + " ledOnMS=" + notification.ledOnMS
177 + " ledOffMS=" + notification.ledOffMS);
178 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 @Override
181 public final String toString()
182 {
183 return "NotificationRecord{"
184 + Integer.toHexString(System.identityHashCode(this))
185 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700186 + " id=" + Integer.toHexString(id)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500187 + " tag=" + tag
188 + " pri=" + priority
189 + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 }
191 }
192
193 private static final class ToastRecord
194 {
195 final int pid;
196 final String pkg;
197 final ITransientNotification callback;
198 int duration;
199
200 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
201 {
202 this.pid = pid;
203 this.pkg = pkg;
204 this.callback = callback;
205 this.duration = duration;
206 }
207
208 void update(int duration) {
209 this.duration = duration;
210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 void dump(PrintWriter pw, String prefix) {
213 pw.println(prefix + this);
214 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 @Override
217 public final String toString()
218 {
219 return "ToastRecord{"
220 + Integer.toHexString(System.identityHashCode(this))
221 + " pkg=" + pkg
222 + " callback=" + callback
223 + " duration=" + duration;
224 }
225 }
226
Joe Onorato089de882010-04-12 08:18:45 -0700227 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
228 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229
230 public void onSetDisabled(int status) {
231 synchronized (mNotificationList) {
232 mDisabledNotifications = status;
233 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
234 // cancel whatever's going on
235 long identity = Binder.clearCallingIdentity();
236 try {
237 mSound.stop();
238 }
239 finally {
240 Binder.restoreCallingIdentity(identity);
241 }
242
243 identity = Binder.clearCallingIdentity();
244 try {
245 mVibrator.cancel();
246 }
247 finally {
248 Binder.restoreCallingIdentity(identity);
249 }
250 }
251 }
252 }
253
254 public void onClearAll() {
255 cancelAll();
256 }
257
Fred Quintana6ecaff12009-09-25 14:23:13 -0700258 public void onNotificationClick(String pkg, String tag, int id) {
259 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
jhtop.kim2e448f72011-07-13 17:15:32 +0900260 Notification.FLAG_FOREGROUND_SERVICE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 }
262
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400263 public void onNotificationClear(String pkg, String tag, int id) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800264 cancelNotification(pkg, tag, id, 0,
265 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
266 true);
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400267 }
268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 public void onPanelRevealed() {
270 synchronized (mNotificationList) {
271 // sound
272 mSoundNotification = null;
273 long identity = Binder.clearCallingIdentity();
274 try {
275 mSound.stop();
276 }
277 finally {
278 Binder.restoreCallingIdentity(identity);
279 }
280
281 // vibrate
282 mVibrateNotification = null;
283 identity = Binder.clearCallingIdentity();
284 try {
285 mVibrator.cancel();
286 }
287 finally {
288 Binder.restoreCallingIdentity(identity);
289 }
290
291 // light
292 mLights.clear();
293 mLedNotification = null;
294 updateLightsLocked();
295 }
296 }
Joe Onorato005847b2010-06-04 16:08:02 -0400297
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700298 public void onNotificationError(String pkg, String tag, int id,
299 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400300 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
301 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Joe Onorato46439ce2010-11-19 13:56:21 -0800302 cancelNotification(pkg, tag, id, 0, 0, false);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700303 long ident = Binder.clearCallingIdentity();
304 try {
305 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
306 "Bad notification posted from package " + pkg
307 + ": " + message);
308 } catch (RemoteException e) {
309 }
310 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 };
313
314 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
315 @Override
316 public void onReceive(Context context, Intent intent) {
317 String action = intent.getAction();
318
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800319 boolean queryRestart = false;
320
Mike Lockwood541c9942011-06-12 19:35:45 -0400321 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800322 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400323 || action.equals(Intent.ACTION_PACKAGE_CHANGED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800324 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800325 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800326 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800327 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800328 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800329 } else if (queryRestart) {
330 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800331 } else {
332 Uri uri = intent.getData();
333 if (uri == null) {
334 return;
335 }
336 String pkgName = uri.getSchemeSpecificPart();
337 if (pkgName == null) {
338 return;
339 }
340 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800342 if (pkgList != null && (pkgList.length > 0)) {
343 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800344 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500347 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
348 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK));
349 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 }
351 }
352 };
353
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700354 class SettingsObserver extends ContentObserver {
355 SettingsObserver(Handler handler) {
356 super(handler);
357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700359 void observe() {
360 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500361 resolver.registerContentObserver(Settings.System.getUriFor(
362 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700363 update();
364 }
365
366 @Override public void onChange(boolean selfChange) {
367 update();
368 }
369
370 public void update() {
371 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500372 boolean pulseEnabled = Settings.System.getInt(resolver,
373 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
374 if (mNotificationPulseEnabled != pulseEnabled) {
375 mNotificationPulseEnabled = pulseEnabled;
376 updateNotificationPulse();
377 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700378 }
379 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500380
Joe Onorato089de882010-04-12 08:18:45 -0700381 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500382 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 {
384 super();
385 mContext = context;
386 mAm = ActivityManagerNative.getDefault();
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700387 mSound = new NotificationPlayer(TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 mSound.setUsesWakeLock(context);
389 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800391
Joe Onorato089de882010-04-12 08:18:45 -0700392 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 statusBar.setNotificationCallbacks(mNotificationCallbacks);
394
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500395 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
396 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
397
Mike Lockwood670f9322010-01-20 12:13:36 -0500398 Resources resources = mContext.getResources();
399 mDefaultNotificationColor = resources.getColor(
400 com.android.internal.R.color.config_defaultNotificationColor);
401 mDefaultNotificationLedOn = resources.getInteger(
402 com.android.internal.R.integer.config_defaultNotificationLedOn);
403 mDefaultNotificationLedOff = resources.getInteger(
404 com.android.internal.R.integer.config_defaultNotificationLedOff);
405
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400406 // Don't start allowing notifications until the setup wizard has run once.
407 // After that, including subsequent boots, init with notifications turned on.
408 // This works on the first boot because the setup wizard will toggle this
409 // flag at least once and we'll go back to 0 after that.
410 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
411 Settings.Secure.DEVICE_PROVISIONED, 0)) {
412 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
413 }
414
Mike Lockwood35e16bf2010-11-30 19:53:36 -0500415 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500417 filter.addAction(Intent.ACTION_SCREEN_ON);
418 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500419 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800421 IntentFilter pkgFilter = new IntentFilter();
422 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400423 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800424 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
425 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
426 pkgFilter.addDataScheme("package");
427 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800428 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800429 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800430
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500431 SettingsObserver observer = new SettingsObserver(mHandler);
432 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 }
434
Joe Onorato30275482009-07-08 17:09:14 -0700435 void systemReady() {
436 // no beeping until we're basically done booting
437 mSystemReady = true;
438 }
439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 // Toasts
441 // ============================================================================
442 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
443 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400444 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445
446 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800447 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 return ;
449 }
450
451 synchronized (mToastQueue) {
452 int callingPid = Binder.getCallingPid();
453 long callingId = Binder.clearCallingIdentity();
454 try {
455 ToastRecord record;
456 int index = indexOfToastLocked(pkg, callback);
457 // If it's already in the queue, we update it in place, we don't
458 // move it to the end of the queue.
459 if (index >= 0) {
460 record = mToastQueue.get(index);
461 record.update(duration);
462 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800463 // Limit the number of toasts that any given package except the android
464 // package can enqueue. Prevents DOS attacks and deals with leaks.
465 if (!"android".equals(pkg)) {
466 int count = 0;
467 final int N = mToastQueue.size();
468 for (int i=0; i<N; i++) {
469 final ToastRecord r = mToastQueue.get(i);
470 if (r.pkg.equals(pkg)) {
471 count++;
472 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
473 Slog.e(TAG, "Package has already posted " + count
474 + " toasts. Not showing more. Package=" + pkg);
475 return;
476 }
477 }
478 }
479 }
480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 record = new ToastRecord(callingPid, pkg, callback, duration);
482 mToastQueue.add(record);
483 index = mToastQueue.size() - 1;
484 keepProcessAliveLocked(callingPid);
485 }
486 // If it's at index 0, it's the current toast. It doesn't matter if it's
487 // new or just been updated. Call back and tell it to show itself.
488 // If the callback fails, this will remove it from the list, so don't
489 // assume that it's valid after this.
490 if (index == 0) {
491 showNextToastLocked();
492 }
493 } finally {
494 Binder.restoreCallingIdentity(callingId);
495 }
496 }
497 }
498
499 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800500 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501
502 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800503 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 return ;
505 }
506
507 synchronized (mToastQueue) {
508 long callingId = Binder.clearCallingIdentity();
509 try {
510 int index = indexOfToastLocked(pkg, callback);
511 if (index >= 0) {
512 cancelToastLocked(index);
513 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800514 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
516 } finally {
517 Binder.restoreCallingIdentity(callingId);
518 }
519 }
520 }
521
522 private void showNextToastLocked() {
523 ToastRecord record = mToastQueue.get(0);
524 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800525 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 try {
527 record.callback.show();
528 scheduleTimeoutLocked(record, false);
529 return;
530 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800531 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 + " in package " + record.pkg);
533 // remove it from the list and let the process die
534 int index = mToastQueue.indexOf(record);
535 if (index >= 0) {
536 mToastQueue.remove(index);
537 }
538 keepProcessAliveLocked(record.pid);
539 if (mToastQueue.size() > 0) {
540 record = mToastQueue.get(0);
541 } else {
542 record = null;
543 }
544 }
545 }
546 }
547
548 private void cancelToastLocked(int index) {
549 ToastRecord record = mToastQueue.get(index);
550 try {
551 record.callback.hide();
552 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800553 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 + " in package " + record.pkg);
555 // don't worry about this, we're about to remove it from
556 // the list anyway
557 }
558 mToastQueue.remove(index);
559 keepProcessAliveLocked(record.pid);
560 if (mToastQueue.size() > 0) {
561 // Show the next one. If the callback fails, this will remove
562 // it from the list, so don't assume that the list hasn't changed
563 // after this point.
564 showNextToastLocked();
565 }
566 }
567
568 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
569 {
570 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
571 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
572 mHandler.removeCallbacksAndMessages(r);
573 mHandler.sendMessageDelayed(m, delay);
574 }
575
576 private void handleTimeout(ToastRecord record)
577 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800578 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 synchronized (mToastQueue) {
580 int index = indexOfToastLocked(record.pkg, record.callback);
581 if (index >= 0) {
582 cancelToastLocked(index);
583 }
584 }
585 }
586
587 // lock on mToastQueue
588 private int indexOfToastLocked(String pkg, ITransientNotification callback)
589 {
590 IBinder cbak = callback.asBinder();
591 ArrayList<ToastRecord> list = mToastQueue;
592 int len = list.size();
593 for (int i=0; i<len; i++) {
594 ToastRecord r = list.get(i);
595 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
596 return i;
597 }
598 }
599 return -1;
600 }
601
602 // lock on mToastQueue
603 private void keepProcessAliveLocked(int pid)
604 {
605 int toastCount = 0; // toasts from this pid
606 ArrayList<ToastRecord> list = mToastQueue;
607 int N = list.size();
608 for (int i=0; i<N; i++) {
609 ToastRecord r = list.get(i);
610 if (r.pid == pid) {
611 toastCount++;
612 }
613 }
614 try {
615 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
616 } catch (RemoteException e) {
617 // Shouldn't happen.
618 }
619 }
620
621 private final class WorkerHandler extends Handler
622 {
623 @Override
624 public void handleMessage(Message msg)
625 {
626 switch (msg.what)
627 {
628 case MESSAGE_TIMEOUT:
629 handleTimeout((ToastRecord)msg.obj);
630 break;
631 }
632 }
633 }
634
635
636 // Notifications
637 // ============================================================================
Andy Stadler110988c2010-12-03 14:29:16 -0800638 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
640 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700641 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
642 }
643
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400644 public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,
645 int[] idOut)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700646 {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400647 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
648 tag, id, notification, idOut);
649 }
650
Daniel Sandlere40451a2011-02-03 14:51:35 -0500651 public void enqueueNotificationWithTagPriority(String pkg, String tag, int id, int priority,
652 Notification notification, int[] idOut)
653 {
654 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
655 tag, id, priority, notification, idOut);
656 }
657
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400658 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
659 // uid/pid of another application)
660 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
661 String tag, int id, Notification notification, int[] idOut)
662 {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500663 enqueueNotificationInternal(pkg, callingUid, callingPid, tag, id,
664 ((notification.flags & Notification.FLAG_ONGOING_EVENT) != 0)
665 ? StatusBarNotification.PRIORITY_ONGOING
666 : StatusBarNotification.PRIORITY_NORMAL,
667 notification, idOut);
668 }
669 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
670 String tag, int id, int priority, Notification notification, int[] idOut)
671 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700672 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800673
Joe Onoratobd73d012010-06-04 11:44:54 -0700674 // Limit the number of notifications that any given package except the android
675 // package can enqueue. Prevents DOS attacks and deals with leaks.
676 if (!"android".equals(pkg)) {
677 synchronized (mNotificationList) {
678 int count = 0;
679 final int N = mNotificationList.size();
680 for (int i=0; i<N; i++) {
681 final NotificationRecord r = mNotificationList.get(i);
682 if (r.pkg.equals(pkg)) {
683 count++;
684 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
685 Slog.e(TAG, "Package has already posted " + count
686 + " notifications. Not showing more. package=" + pkg);
687 return;
688 }
689 }
690 }
691 }
692 }
693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 // This conditional is a dirty hack to limit the logging done on
695 // behalf of the download manager without affecting other apps.
696 if (!pkg.equals("com.android.providers.downloads")
697 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800698 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 }
700
701 if (pkg == null || notification == null) {
702 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
703 + " id=" + id + " notification=" + notification);
704 }
705 if (notification.icon != 0) {
706 if (notification.contentView == null) {
707 throw new IllegalArgumentException("contentView required: pkg=" + pkg
708 + " id=" + id + " notification=" + notification);
709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
711
712 synchronized (mNotificationList) {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500713 NotificationRecord r = new NotificationRecord(pkg, tag, id,
714 callingUid, callingPid,
715 priority,
716 notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 NotificationRecord old = null;
718
Fred Quintana6ecaff12009-09-25 14:23:13 -0700719 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 if (index < 0) {
721 mNotificationList.add(r);
722 } else {
723 old = mNotificationList.remove(index);
724 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700725 // Make sure we don't lose the foreground service state.
726 if (old != null) {
727 notification.flags |=
728 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800731
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700732 // Ensure if this is a foreground service that the proper additional
733 // flags are set.
734 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
735 notification.flags |= Notification.FLAG_ONGOING_EVENT
736 | Notification.FLAG_NO_CLEAR;
737 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 if (notification.icon != 0) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700740 StatusBarNotification n = new StatusBarNotification(pkg, id, tag,
741 r.uid, r.initialPid, notification);
Daniel Sandlere40451a2011-02-03 14:51:35 -0500742 n.priority = r.priority;
743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 if (old != null && old.statusBarKey != null) {
745 r.statusBarKey = old.statusBarKey;
746 long identity = Binder.clearCallingIdentity();
747 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700748 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 }
750 finally {
751 Binder.restoreCallingIdentity(identity);
752 }
753 } else {
754 long identity = Binder.clearCallingIdentity();
755 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700756 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500757 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759 finally {
760 Binder.restoreCallingIdentity(identity);
761 }
762 }
Joe Onorato30275482009-07-08 17:09:14 -0700763 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500765 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 if (old != null && old.statusBarKey != null) {
767 long identity = Binder.clearCallingIdentity();
768 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700769 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
771 finally {
772 Binder.restoreCallingIdentity(identity);
773 }
774 }
775 }
776
777 // If we're not supposed to beep, vibrate, etc. then don't.
778 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
779 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700780 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
781 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800782
783 final AudioManager audioManager = (AudioManager) mContext
784 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 // sound
786 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800787 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 if (useDefaultSound || notification.sound != null) {
789 Uri uri;
790 if (useDefaultSound) {
791 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
792 } else {
793 uri = notification.sound;
794 }
795 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
796 int audioStreamType;
797 if (notification.audioStreamType >= 0) {
798 audioStreamType = notification.audioStreamType;
799 } else {
800 audioStreamType = DEFAULT_STREAM_TYPE;
801 }
802 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800803 // do not play notifications if stream volume is 0
804 // (typically because ringer mode is silent).
805 if (audioManager.getStreamVolume(audioStreamType) != 0) {
806 long identity = Binder.clearCallingIdentity();
807 try {
808 mSound.play(mContext, uri, looping, audioStreamType);
809 }
810 finally {
811 Binder.restoreCallingIdentity(identity);
812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 }
814 }
815
816 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800818 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 if ((useDefaultVibrate || notification.vibrate != null)
820 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
821 mVibrateNotification = r;
822
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800823 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 : notification.vibrate,
825 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
826 }
827 }
828
829 // this option doesn't shut off the lights
830
831 // light
832 // the most recent thing gets the light
833 mLights.remove(old);
834 if (mLedNotification == old) {
835 mLedNotification = null;
836 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800837 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
839 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
840 mLights.add(r);
841 updateLightsLocked();
842 } else {
843 if (old != null
844 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
845 updateLightsLocked();
846 }
847 }
848 }
849
850 idOut[0] = id;
851 }
852
Joe Onorato30275482009-07-08 17:09:14 -0700853 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700854 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
855 if (!manager.isEnabled()) {
856 return;
857 }
858
859 AccessibilityEvent event =
860 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
861 event.setPackageName(packageName);
862 event.setClassName(Notification.class.getName());
863 event.setParcelableData(notification);
864 CharSequence tickerText = notification.tickerText;
865 if (!TextUtils.isEmpty(tickerText)) {
866 event.getText().add(tickerText);
867 }
868
869 manager.sendAccessibilityEvent(event);
870 }
871
Joe Onorato46439ce2010-11-19 13:56:21 -0800872 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
873 // tell the app
874 if (sendDelete) {
875 if (r.notification.deleteIntent != null) {
876 try {
877 r.notification.deleteIntent.send();
878 } catch (PendingIntent.CanceledException ex) {
879 // do nothing - there's no relevant way to recover, and
880 // no reason to let this propagate
881 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
882 }
883 }
884 }
885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 // status bar
887 if (r.notification.icon != 0) {
888 long identity = Binder.clearCallingIdentity();
889 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700890 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 }
892 finally {
893 Binder.restoreCallingIdentity(identity);
894 }
895 r.statusBarKey = null;
896 }
897
898 // sound
899 if (mSoundNotification == r) {
900 mSoundNotification = null;
901 long identity = Binder.clearCallingIdentity();
902 try {
903 mSound.stop();
904 }
905 finally {
906 Binder.restoreCallingIdentity(identity);
907 }
908 }
909
910 // vibrate
911 if (mVibrateNotification == r) {
912 mVibrateNotification = null;
913 long identity = Binder.clearCallingIdentity();
914 try {
915 mVibrator.cancel();
916 }
917 finally {
918 Binder.restoreCallingIdentity(identity);
919 }
920 }
921
922 // light
923 mLights.remove(r);
924 if (mLedNotification == r) {
925 mLedNotification = null;
926 }
927 }
928
929 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700930 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800931 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700933 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Joe Onorato46439ce2010-11-19 13:56:21 -0800934 int mustNotHaveFlags, boolean sendDelete) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800935 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936
937 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700938 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700940 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
943 return;
944 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700945 if ((r.notification.flags & mustNotHaveFlags) != 0) {
946 return;
947 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 mNotificationList.remove(index);
950
Joe Onorato46439ce2010-11-19 13:56:21 -0800951 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 updateLightsLocked();
953 }
954 }
955 }
956
957 /**
958 * Cancels all notifications from a given package that have all of the
959 * {@code mustHaveFlags}.
960 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800961 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
962 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800963 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964
965 synchronized (mNotificationList) {
966 final int N = mNotificationList.size();
967 boolean canceledSomething = false;
968 for (int i = N-1; i >= 0; --i) {
969 NotificationRecord r = mNotificationList.get(i);
970 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
971 continue;
972 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700973 if ((r.notification.flags & mustNotHaveFlags) != 0) {
974 continue;
975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 if (!r.pkg.equals(pkg)) {
977 continue;
978 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800979 canceledSomething = true;
980 if (!doit) {
981 return true;
982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -0800984 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 }
986 if (canceledSomething) {
987 updateLightsLocked();
988 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800989 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
991 }
992
Andy Stadler110988c2010-12-03 14:29:16 -0800993 @Deprecated
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700994 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700995 cancelNotificationWithTag(pkg, null /* tag */, id);
996 }
997
998 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700999 checkIncomingCall(pkg);
1000 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001001 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001002 Binder.getCallingUid() == Process.SYSTEM_UID
Joe Onorato46439ce2010-11-19 13:56:21 -08001003 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 }
1005
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001006 public void cancelAllNotifications(String pkg) {
1007 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001008
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001009 // Calling from user space, don't allow the canceling of actively
1010 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001011 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 }
1013
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001014 void checkIncomingCall(String pkg) {
1015 int uid = Binder.getCallingUid();
1016 if (uid == Process.SYSTEM_UID || uid == 0) {
1017 return;
1018 }
1019 try {
1020 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
1021 pkg, 0);
1022 if (ai.uid != uid) {
1023 throw new SecurityException("Calling uid " + uid + " gave package"
1024 + pkg + " which is owned by uid " + ai.uid);
1025 }
1026 } catch (PackageManager.NameNotFoundException e) {
1027 throw new SecurityException("Unknown package " + pkg);
1028 }
1029 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001030
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001031 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 synchronized (mNotificationList) {
1033 final int N = mNotificationList.size();
1034 for (int i=N-1; i>=0; i--) {
1035 NotificationRecord r = mNotificationList.get(i);
1036
1037 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1038 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001040 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 }
1042 }
1043
1044 updateLightsLocked();
1045 }
1046 }
1047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 // lock on mNotificationList
1049 private void updateLightsLocked()
1050 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001051 // handle notification lights
1052 if (mLedNotification == null) {
1053 // get next notification, if any
1054 int n = mLights.size();
1055 if (n > 0) {
1056 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
1058 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001059
Mike Lockwoodb0626b52011-08-23 15:11:04 -04001060 // Don't flash while we are in a call
1061 if (mLedNotification == null || mInCall) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001062 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001063 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001064 int ledARGB = mLedNotification.notification.ledARGB;
1065 int ledOnMS = mLedNotification.notification.ledOnMS;
1066 int ledOffMS = mLedNotification.notification.ledOffMS;
1067 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1068 ledARGB = mDefaultNotificationColor;
1069 ledOnMS = mDefaultNotificationLedOn;
1070 ledOffMS = mDefaultNotificationLedOff;
1071 }
1072 if (mNotificationPulseEnabled) {
1073 // pulse repeatedly
1074 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1075 ledOnMS, ledOffMS);
1076 } else {
1077 // pulse only once
1078 mNotificationLight.pulse(ledARGB, ledOnMS);
1079 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
1082
1083 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001084 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 {
1086 ArrayList<NotificationRecord> list = mNotificationList;
1087 final int len = list.size();
1088 for (int i=0; i<len; i++) {
1089 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001090 if (tag == null) {
1091 if (r.tag != null) {
1092 continue;
1093 }
1094 } else {
1095 if (!tag.equals(r.tag)) {
1096 continue;
1097 }
1098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 if (r.id == id && r.pkg.equals(pkg)) {
1100 return i;
1101 }
1102 }
1103 return -1;
1104 }
1105
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001106 private void updateNotificationPulse() {
1107 synchronized (mNotificationList) {
1108 updateLightsLocked();
1109 }
1110 }
1111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 // ======================================================================
1113 @Override
1114 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1115 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1116 != PackageManager.PERMISSION_GRANTED) {
1117 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1118 + Binder.getCallingPid()
1119 + ", uid=" + Binder.getCallingUid());
1120 return;
1121 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 pw.println("Current Notification Manager state:");
1124
1125 int N;
1126
1127 synchronized (mToastQueue) {
1128 N = mToastQueue.size();
1129 if (N > 0) {
1130 pw.println(" Toast Queue:");
1131 for (int i=0; i<N; i++) {
1132 mToastQueue.get(i).dump(pw, " ");
1133 }
1134 pw.println(" ");
1135 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
1138
1139 synchronized (mNotificationList) {
1140 N = mNotificationList.size();
1141 if (N > 0) {
1142 pw.println(" Notification List:");
1143 for (int i=0; i<N; i++) {
1144 mNotificationList.get(i).dump(pw, " ", mContext);
1145 }
1146 pw.println(" ");
1147 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 N = mLights.size();
1150 if (N > 0) {
1151 pw.println(" Lights List:");
1152 for (int i=0; i<N; i++) {
1153 mLights.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 pw.println(" mSoundNotification=" + mSoundNotification);
1159 pw.println(" mSound=" + mSound);
1160 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001161 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1162 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 }
1164 }
1165}