blob: 34a8a027673e0b95dbb7bdd4080ee09248d8d187 [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;
Amith Yamasani742a6712011-05-04 14:49:28 -070048import android.os.UserId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.Vibrator;
50import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050051import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070052import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.EventLog;
54import android.util.Log;
Andy Stadler110988c2010-12-03 14:29:16 -080055import android.util.Slog;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.view.accessibility.AccessibilityEvent;
57import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.widget.Toast;
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import java.io.FileDescriptor;
61import java.io.PrintWriter;
62import java.util.ArrayList;
63import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040065/** {@hide} */
66public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067{
68 private static final String TAG = "NotificationService";
69 private static final boolean DBG = false;
70
Joe Onoratobd73d012010-06-04 11:44:54 -070071 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
72
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 // message codes
74 private static final int MESSAGE_TIMEOUT = 2;
75
76 private static final int LONG_DELAY = 3500; // 3.5 seconds
77 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -080078
79 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
82
83 final Context mContext;
84 final IActivityManager mAm;
85 final IBinder mForegroundToken = new Binder();
86
87 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -070088 private StatusBarManagerService mStatusBar;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050089 private LightsService.Light mNotificationLight;
90 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
Mike Lockwood670f9322010-01-20 12:13:36 -050092 private int mDefaultNotificationColor;
93 private int mDefaultNotificationLedOn;
94 private int mDefaultNotificationLedOff;
95
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 private NotificationRecord mSoundNotification;
Jean-Michel Trivi211957f2010-03-26 18:19:33 -070097 private NotificationPlayer mSound;
Joe Onorato30275482009-07-08 17:09:14 -070098 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -040099 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
101 private NotificationRecord mVibrateNotification;
102 private Vibrator mVibrator = new Vibrator();
103
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500104 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400105 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500106 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500107 private boolean mNotificationPulseEnabled;
108
Fred Quintana6ecaff12009-09-25 14:23:13 -0700109 private final ArrayList<NotificationRecord> mNotificationList =
110 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111
112 private ArrayList<ToastRecord> mToastQueue;
113
114 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 private static String idDebugString(Context baseContext, String packageName, int id) {
118 Context c = null;
119
120 if (packageName != null) {
121 try {
122 c = baseContext.createPackageContext(packageName, 0);
123 } catch (NameNotFoundException e) {
124 c = baseContext;
125 }
126 } else {
127 c = baseContext;
128 }
129
130 String pkg;
131 String type;
132 String name;
133
134 Resources r = c.getResources();
135 try {
136 return r.getResourceName(id);
137 } catch (Resources.NotFoundException e) {
138 return "<name unknown>";
139 }
140 }
141
142 private static final class NotificationRecord
143 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700144 final String pkg;
145 final String tag;
146 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700147 final int uid;
148 final int initialPid;
Daniel Sandlere40451a2011-02-03 14:51:35 -0500149 final int priority;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700150 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 IBinder statusBarKey;
152
Daniel Sandlere40451a2011-02-03 14:51:35 -0500153 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid, int priority,
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700154 Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 {
156 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700157 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700159 this.uid = uid;
160 this.initialPid = initialPid;
Daniel Sandlere40451a2011-02-03 14:51:35 -0500161 this.priority = priority;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 this.notification = notification;
163 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 void dump(PrintWriter pw, String prefix, Context baseContext) {
166 pw.println(prefix + this);
167 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
168 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
169 pw.println(prefix + " contentIntent=" + notification.contentIntent);
170 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
171 pw.println(prefix + " tickerText=" + notification.tickerText);
172 pw.println(prefix + " contentView=" + notification.contentView);
173 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
174 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
175 pw.println(prefix + " sound=" + notification.sound);
176 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
177 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
178 + " ledOnMS=" + notification.ledOnMS
179 + " ledOffMS=" + notification.ledOffMS);
180 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 @Override
183 public final String toString()
184 {
185 return "NotificationRecord{"
186 + Integer.toHexString(System.identityHashCode(this))
187 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700188 + " id=" + Integer.toHexString(id)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500189 + " tag=" + tag
190 + " pri=" + priority
191 + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 }
193 }
194
195 private static final class ToastRecord
196 {
197 final int pid;
198 final String pkg;
199 final ITransientNotification callback;
200 int duration;
201
202 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
203 {
204 this.pid = pid;
205 this.pkg = pkg;
206 this.callback = callback;
207 this.duration = duration;
208 }
209
210 void update(int duration) {
211 this.duration = duration;
212 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 void dump(PrintWriter pw, String prefix) {
215 pw.println(prefix + this);
216 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 @Override
219 public final String toString()
220 {
221 return "ToastRecord{"
222 + Integer.toHexString(System.identityHashCode(this))
223 + " pkg=" + pkg
224 + " callback=" + callback
225 + " duration=" + duration;
226 }
227 }
228
Joe Onorato089de882010-04-12 08:18:45 -0700229 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
230 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231
232 public void onSetDisabled(int status) {
233 synchronized (mNotificationList) {
234 mDisabledNotifications = status;
235 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
236 // cancel whatever's going on
237 long identity = Binder.clearCallingIdentity();
238 try {
239 mSound.stop();
240 }
241 finally {
242 Binder.restoreCallingIdentity(identity);
243 }
244
245 identity = Binder.clearCallingIdentity();
246 try {
247 mVibrator.cancel();
248 }
249 finally {
250 Binder.restoreCallingIdentity(identity);
251 }
252 }
253 }
254 }
255
256 public void onClearAll() {
257 cancelAll();
258 }
259
Fred Quintana6ecaff12009-09-25 14:23:13 -0700260 public void onNotificationClick(String pkg, String tag, int id) {
261 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
jhtop.kim2e448f72011-07-13 17:15:32 +0900262 Notification.FLAG_FOREGROUND_SERVICE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 }
264
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400265 public void onNotificationClear(String pkg, String tag, int id) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800266 cancelNotification(pkg, tag, id, 0,
267 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
268 true);
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400269 }
270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 public void onPanelRevealed() {
272 synchronized (mNotificationList) {
273 // sound
274 mSoundNotification = null;
275 long identity = Binder.clearCallingIdentity();
276 try {
277 mSound.stop();
278 }
279 finally {
280 Binder.restoreCallingIdentity(identity);
281 }
282
283 // vibrate
284 mVibrateNotification = null;
285 identity = Binder.clearCallingIdentity();
286 try {
287 mVibrator.cancel();
288 }
289 finally {
290 Binder.restoreCallingIdentity(identity);
291 }
292
293 // light
294 mLights.clear();
295 mLedNotification = null;
296 updateLightsLocked();
297 }
298 }
Joe Onorato005847b2010-06-04 16:08:02 -0400299
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700300 public void onNotificationError(String pkg, String tag, int id,
301 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400302 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
303 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Joe Onorato46439ce2010-11-19 13:56:21 -0800304 cancelNotification(pkg, tag, id, 0, 0, false);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700305 long ident = Binder.clearCallingIdentity();
306 try {
307 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
308 "Bad notification posted from package " + pkg
309 + ": " + message);
310 } catch (RemoteException e) {
311 }
312 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 };
315
316 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
317 @Override
318 public void onReceive(Context context, Intent intent) {
319 String action = intent.getAction();
320
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800321 boolean queryRestart = false;
322
Mike Lockwood541c9942011-06-12 19:35:45 -0400323 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800324 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400325 || action.equals(Intent.ACTION_PACKAGE_CHANGED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800326 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800327 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800328 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800329 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800330 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800331 } else if (queryRestart) {
332 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800333 } else {
334 Uri uri = intent.getData();
335 if (uri == null) {
336 return;
337 }
338 String pkgName = uri.getSchemeSpecificPart();
339 if (pkgName == null) {
340 return;
341 }
342 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800344 if (pkgList != null && (pkgList.length > 0)) {
345 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800346 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400349 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
350 // Keep track of screen on/off state, but do not turn off the notification light
351 // until user passes through the lock screen or views the notification.
352 mScreenOn = true;
353 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
354 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500355 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400356 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
357 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500358 updateNotificationPulse();
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400359 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
360 // turn off LED when user passes through lock screen
361 mNotificationLight.turnOff();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 }
363 }
364 };
365
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700366 class SettingsObserver extends ContentObserver {
367 SettingsObserver(Handler handler) {
368 super(handler);
369 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800370
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700371 void observe() {
372 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500373 resolver.registerContentObserver(Settings.System.getUriFor(
374 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700375 update();
376 }
377
378 @Override public void onChange(boolean selfChange) {
379 update();
380 }
381
382 public void update() {
383 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500384 boolean pulseEnabled = Settings.System.getInt(resolver,
385 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
386 if (mNotificationPulseEnabled != pulseEnabled) {
387 mNotificationPulseEnabled = pulseEnabled;
388 updateNotificationPulse();
389 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700390 }
391 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500392
Joe Onorato089de882010-04-12 08:18:45 -0700393 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500394 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 {
396 super();
397 mContext = context;
398 mAm = ActivityManagerNative.getDefault();
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700399 mSound = new NotificationPlayer(TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 mSound.setUsesWakeLock(context);
401 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800403
Joe Onorato089de882010-04-12 08:18:45 -0700404 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 statusBar.setNotificationCallbacks(mNotificationCallbacks);
406
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500407 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
408 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
409
Mike Lockwood670f9322010-01-20 12:13:36 -0500410 Resources resources = mContext.getResources();
411 mDefaultNotificationColor = resources.getColor(
412 com.android.internal.R.color.config_defaultNotificationColor);
413 mDefaultNotificationLedOn = resources.getInteger(
414 com.android.internal.R.integer.config_defaultNotificationLedOn);
415 mDefaultNotificationLedOff = resources.getInteger(
416 com.android.internal.R.integer.config_defaultNotificationLedOff);
417
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400418 // Don't start allowing notifications until the setup wizard has run once.
419 // After that, including subsequent boots, init with notifications turned on.
420 // This works on the first boot because the setup wizard will toggle this
421 // flag at least once and we'll go back to 0 after that.
422 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
423 Settings.Secure.DEVICE_PROVISIONED, 0)) {
424 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
425 }
426
Mike Lockwood35e16bf2010-11-30 19:53:36 -0500427 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500429 filter.addAction(Intent.ACTION_SCREEN_ON);
430 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500431 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400432 filter.addAction(Intent.ACTION_USER_PRESENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800434 IntentFilter pkgFilter = new IntentFilter();
435 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400436 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800437 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
438 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
439 pkgFilter.addDataScheme("package");
440 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800441 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800442 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800443
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500444 SettingsObserver observer = new SettingsObserver(mHandler);
445 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 }
447
Joe Onorato30275482009-07-08 17:09:14 -0700448 void systemReady() {
449 // no beeping until we're basically done booting
450 mSystemReady = true;
451 }
452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 // Toasts
454 // ============================================================================
455 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
456 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400457 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458
459 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800460 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 return ;
462 }
463
464 synchronized (mToastQueue) {
465 int callingPid = Binder.getCallingPid();
466 long callingId = Binder.clearCallingIdentity();
467 try {
468 ToastRecord record;
469 int index = indexOfToastLocked(pkg, callback);
470 // If it's already in the queue, we update it in place, we don't
471 // move it to the end of the queue.
472 if (index >= 0) {
473 record = mToastQueue.get(index);
474 record.update(duration);
475 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800476 // Limit the number of toasts that any given package except the android
477 // package can enqueue. Prevents DOS attacks and deals with leaks.
478 if (!"android".equals(pkg)) {
479 int count = 0;
480 final int N = mToastQueue.size();
481 for (int i=0; i<N; i++) {
482 final ToastRecord r = mToastQueue.get(i);
483 if (r.pkg.equals(pkg)) {
484 count++;
485 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
486 Slog.e(TAG, "Package has already posted " + count
487 + " toasts. Not showing more. Package=" + pkg);
488 return;
489 }
490 }
491 }
492 }
493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 record = new ToastRecord(callingPid, pkg, callback, duration);
495 mToastQueue.add(record);
496 index = mToastQueue.size() - 1;
497 keepProcessAliveLocked(callingPid);
498 }
499 // If it's at index 0, it's the current toast. It doesn't matter if it's
500 // new or just been updated. Call back and tell it to show itself.
501 // If the callback fails, this will remove it from the list, so don't
502 // assume that it's valid after this.
503 if (index == 0) {
504 showNextToastLocked();
505 }
506 } finally {
507 Binder.restoreCallingIdentity(callingId);
508 }
509 }
510 }
511
512 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800513 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514
515 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800516 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 return ;
518 }
519
520 synchronized (mToastQueue) {
521 long callingId = Binder.clearCallingIdentity();
522 try {
523 int index = indexOfToastLocked(pkg, callback);
524 if (index >= 0) {
525 cancelToastLocked(index);
526 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800527 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
529 } finally {
530 Binder.restoreCallingIdentity(callingId);
531 }
532 }
533 }
534
535 private void showNextToastLocked() {
536 ToastRecord record = mToastQueue.get(0);
537 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800538 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 try {
540 record.callback.show();
541 scheduleTimeoutLocked(record, false);
542 return;
543 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800544 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 + " in package " + record.pkg);
546 // remove it from the list and let the process die
547 int index = mToastQueue.indexOf(record);
548 if (index >= 0) {
549 mToastQueue.remove(index);
550 }
551 keepProcessAliveLocked(record.pid);
552 if (mToastQueue.size() > 0) {
553 record = mToastQueue.get(0);
554 } else {
555 record = null;
556 }
557 }
558 }
559 }
560
561 private void cancelToastLocked(int index) {
562 ToastRecord record = mToastQueue.get(index);
563 try {
564 record.callback.hide();
565 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800566 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 + " in package " + record.pkg);
568 // don't worry about this, we're about to remove it from
569 // the list anyway
570 }
571 mToastQueue.remove(index);
572 keepProcessAliveLocked(record.pid);
573 if (mToastQueue.size() > 0) {
574 // Show the next one. If the callback fails, this will remove
575 // it from the list, so don't assume that the list hasn't changed
576 // after this point.
577 showNextToastLocked();
578 }
579 }
580
581 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
582 {
583 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
584 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
585 mHandler.removeCallbacksAndMessages(r);
586 mHandler.sendMessageDelayed(m, delay);
587 }
588
589 private void handleTimeout(ToastRecord record)
590 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800591 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 synchronized (mToastQueue) {
593 int index = indexOfToastLocked(record.pkg, record.callback);
594 if (index >= 0) {
595 cancelToastLocked(index);
596 }
597 }
598 }
599
600 // lock on mToastQueue
601 private int indexOfToastLocked(String pkg, ITransientNotification callback)
602 {
603 IBinder cbak = callback.asBinder();
604 ArrayList<ToastRecord> list = mToastQueue;
605 int len = list.size();
606 for (int i=0; i<len; i++) {
607 ToastRecord r = list.get(i);
608 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
609 return i;
610 }
611 }
612 return -1;
613 }
614
615 // lock on mToastQueue
616 private void keepProcessAliveLocked(int pid)
617 {
618 int toastCount = 0; // toasts from this pid
619 ArrayList<ToastRecord> list = mToastQueue;
620 int N = list.size();
621 for (int i=0; i<N; i++) {
622 ToastRecord r = list.get(i);
623 if (r.pid == pid) {
624 toastCount++;
625 }
626 }
627 try {
628 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
629 } catch (RemoteException e) {
630 // Shouldn't happen.
631 }
632 }
633
634 private final class WorkerHandler extends Handler
635 {
636 @Override
637 public void handleMessage(Message msg)
638 {
639 switch (msg.what)
640 {
641 case MESSAGE_TIMEOUT:
642 handleTimeout((ToastRecord)msg.obj);
643 break;
644 }
645 }
646 }
647
648
649 // Notifications
650 // ============================================================================
Andy Stadler110988c2010-12-03 14:29:16 -0800651 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
653 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700654 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
655 }
656
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400657 public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,
658 int[] idOut)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700659 {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400660 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
661 tag, id, notification, idOut);
662 }
663
Daniel Sandlere40451a2011-02-03 14:51:35 -0500664 public void enqueueNotificationWithTagPriority(String pkg, String tag, int id, int priority,
665 Notification notification, int[] idOut)
666 {
667 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
668 tag, id, priority, notification, idOut);
669 }
670
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400671 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
672 // uid/pid of another application)
673 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
674 String tag, int id, Notification notification, int[] idOut)
675 {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500676 enqueueNotificationInternal(pkg, callingUid, callingPid, tag, id,
677 ((notification.flags & Notification.FLAG_ONGOING_EVENT) != 0)
678 ? StatusBarNotification.PRIORITY_ONGOING
679 : StatusBarNotification.PRIORITY_NORMAL,
680 notification, idOut);
681 }
682 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
683 String tag, int id, int priority, Notification notification, int[] idOut)
684 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700685 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800686
Joe Onoratobd73d012010-06-04 11:44:54 -0700687 // Limit the number of notifications that any given package except the android
688 // package can enqueue. Prevents DOS attacks and deals with leaks.
689 if (!"android".equals(pkg)) {
690 synchronized (mNotificationList) {
691 int count = 0;
692 final int N = mNotificationList.size();
693 for (int i=0; i<N; i++) {
694 final NotificationRecord r = mNotificationList.get(i);
695 if (r.pkg.equals(pkg)) {
696 count++;
697 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
698 Slog.e(TAG, "Package has already posted " + count
699 + " notifications. Not showing more. package=" + pkg);
700 return;
701 }
702 }
703 }
704 }
705 }
706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 // This conditional is a dirty hack to limit the logging done on
708 // behalf of the download manager without affecting other apps.
709 if (!pkg.equals("com.android.providers.downloads")
710 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -0500711 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag,
712 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714
715 if (pkg == null || notification == null) {
716 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
717 + " id=" + id + " notification=" + notification);
718 }
719 if (notification.icon != 0) {
720 if (notification.contentView == null) {
721 throw new IllegalArgumentException("contentView required: pkg=" + pkg
722 + " id=" + id + " notification=" + notification);
723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 }
725
726 synchronized (mNotificationList) {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500727 NotificationRecord r = new NotificationRecord(pkg, tag, id,
728 callingUid, callingPid,
729 priority,
730 notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 NotificationRecord old = null;
732
Fred Quintana6ecaff12009-09-25 14:23:13 -0700733 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 if (index < 0) {
735 mNotificationList.add(r);
736 } else {
737 old = mNotificationList.remove(index);
738 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700739 // Make sure we don't lose the foreground service state.
740 if (old != null) {
741 notification.flags |=
742 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800745
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700746 // Ensure if this is a foreground service that the proper additional
747 // flags are set.
748 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
749 notification.flags |= Notification.FLAG_ONGOING_EVENT
750 | Notification.FLAG_NO_CLEAR;
751 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 if (notification.icon != 0) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700754 StatusBarNotification n = new StatusBarNotification(pkg, id, tag,
755 r.uid, r.initialPid, notification);
Daniel Sandlere40451a2011-02-03 14:51:35 -0500756 n.priority = r.priority;
757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 if (old != null && old.statusBarKey != null) {
759 r.statusBarKey = old.statusBarKey;
760 long identity = Binder.clearCallingIdentity();
761 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700762 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764 finally {
765 Binder.restoreCallingIdentity(identity);
766 }
767 } else {
768 long identity = Binder.clearCallingIdentity();
769 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700770 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500771 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
773 finally {
774 Binder.restoreCallingIdentity(identity);
775 }
776 }
Joe Onorato30275482009-07-08 17:09:14 -0700777 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500779 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 if (old != null && old.statusBarKey != null) {
781 long identity = Binder.clearCallingIdentity();
782 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700783 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 }
785 finally {
786 Binder.restoreCallingIdentity(identity);
787 }
788 }
789 }
790
791 // If we're not supposed to beep, vibrate, etc. then don't.
792 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
793 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700794 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
795 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800796
797 final AudioManager audioManager = (AudioManager) mContext
798 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 // sound
800 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800801 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 if (useDefaultSound || notification.sound != null) {
803 Uri uri;
804 if (useDefaultSound) {
805 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
806 } else {
807 uri = notification.sound;
808 }
809 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
810 int audioStreamType;
811 if (notification.audioStreamType >= 0) {
812 audioStreamType = notification.audioStreamType;
813 } else {
814 audioStreamType = DEFAULT_STREAM_TYPE;
815 }
816 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800817 // do not play notifications if stream volume is 0
818 // (typically because ringer mode is silent).
819 if (audioManager.getStreamVolume(audioStreamType) != 0) {
820 long identity = Binder.clearCallingIdentity();
821 try {
822 mSound.play(mContext, uri, looping, audioStreamType);
823 }
824 finally {
825 Binder.restoreCallingIdentity(identity);
826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 }
828 }
829
830 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800832 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 if ((useDefaultVibrate || notification.vibrate != null)
834 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
835 mVibrateNotification = r;
836
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800837 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 : notification.vibrate,
839 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
840 }
841 }
842
843 // this option doesn't shut off the lights
844
845 // light
846 // the most recent thing gets the light
847 mLights.remove(old);
848 if (mLedNotification == old) {
849 mLedNotification = null;
850 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800851 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
853 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
854 mLights.add(r);
855 updateLightsLocked();
856 } else {
857 if (old != null
858 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
859 updateLightsLocked();
860 }
861 }
862 }
863
864 idOut[0] = id;
865 }
866
Joe Onorato30275482009-07-08 17:09:14 -0700867 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700868 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
869 if (!manager.isEnabled()) {
870 return;
871 }
872
873 AccessibilityEvent event =
874 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
875 event.setPackageName(packageName);
876 event.setClassName(Notification.class.getName());
877 event.setParcelableData(notification);
878 CharSequence tickerText = notification.tickerText;
879 if (!TextUtils.isEmpty(tickerText)) {
880 event.getText().add(tickerText);
881 }
882
883 manager.sendAccessibilityEvent(event);
884 }
885
Joe Onorato46439ce2010-11-19 13:56:21 -0800886 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
887 // tell the app
888 if (sendDelete) {
889 if (r.notification.deleteIntent != null) {
890 try {
891 r.notification.deleteIntent.send();
892 } catch (PendingIntent.CanceledException ex) {
893 // do nothing - there's no relevant way to recover, and
894 // no reason to let this propagate
895 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
896 }
897 }
898 }
899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 // status bar
901 if (r.notification.icon != 0) {
902 long identity = Binder.clearCallingIdentity();
903 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700904 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 }
906 finally {
907 Binder.restoreCallingIdentity(identity);
908 }
909 r.statusBarKey = null;
910 }
911
912 // sound
913 if (mSoundNotification == r) {
914 mSoundNotification = null;
915 long identity = Binder.clearCallingIdentity();
916 try {
917 mSound.stop();
918 }
919 finally {
920 Binder.restoreCallingIdentity(identity);
921 }
922 }
923
924 // vibrate
925 if (mVibrateNotification == r) {
926 mVibrateNotification = null;
927 long identity = Binder.clearCallingIdentity();
928 try {
929 mVibrator.cancel();
930 }
931 finally {
932 Binder.restoreCallingIdentity(identity);
933 }
934 }
935
936 // light
937 mLights.remove(r);
938 if (mLedNotification == r) {
939 mLedNotification = null;
940 }
941 }
942
943 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700944 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800945 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700947 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Joe Onorato46439ce2010-11-19 13:56:21 -0800948 int mustNotHaveFlags, boolean sendDelete) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -0500949 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag,
950 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951
952 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700953 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700955 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
958 return;
959 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700960 if ((r.notification.flags & mustNotHaveFlags) != 0) {
961 return;
962 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 mNotificationList.remove(index);
965
Joe Onorato46439ce2010-11-19 13:56:21 -0800966 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 updateLightsLocked();
968 }
969 }
970 }
971
972 /**
973 * Cancels all notifications from a given package that have all of the
974 * {@code mustHaveFlags}.
975 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800976 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
977 int mustNotHaveFlags, boolean doit) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -0500978 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags,
979 mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980
981 synchronized (mNotificationList) {
982 final int N = mNotificationList.size();
983 boolean canceledSomething = false;
984 for (int i = N-1; i >= 0; --i) {
985 NotificationRecord r = mNotificationList.get(i);
986 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
987 continue;
988 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700989 if ((r.notification.flags & mustNotHaveFlags) != 0) {
990 continue;
991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 if (!r.pkg.equals(pkg)) {
993 continue;
994 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800995 canceledSomething = true;
996 if (!doit) {
997 return true;
998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001000 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 }
1002 if (canceledSomething) {
1003 updateLightsLocked();
1004 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001005 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 }
1007 }
1008
Andy Stadler110988c2010-12-03 14:29:16 -08001009 @Deprecated
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001010 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001011 cancelNotificationWithTag(pkg, null /* tag */, id);
1012 }
1013
1014 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001015 checkIncomingCall(pkg);
1016 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001017 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001018 Binder.getCallingUid() == Process.SYSTEM_UID
Joe Onorato46439ce2010-11-19 13:56:21 -08001019 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 }
1021
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001022 public void cancelAllNotifications(String pkg) {
1023 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001024
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001025 // Calling from user space, don't allow the canceling of actively
1026 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001027 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 }
1029
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001030 void checkIncomingCall(String pkg) {
1031 int uid = Binder.getCallingUid();
1032 if (uid == Process.SYSTEM_UID || uid == 0) {
1033 return;
1034 }
1035 try {
1036 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
1037 pkg, 0);
Amith Yamasani742a6712011-05-04 14:49:28 -07001038 if (!UserId.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001039 throw new SecurityException("Calling uid " + uid + " gave package"
1040 + pkg + " which is owned by uid " + ai.uid);
1041 }
1042 } catch (PackageManager.NameNotFoundException e) {
1043 throw new SecurityException("Unknown package " + pkg);
1044 }
1045 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001046
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001047 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 synchronized (mNotificationList) {
1049 final int N = mNotificationList.size();
1050 for (int i=N-1; i>=0; i--) {
1051 NotificationRecord r = mNotificationList.get(i);
1052
1053 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1054 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001056 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
1058 }
1059
1060 updateLightsLocked();
1061 }
1062 }
1063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 // lock on mNotificationList
1065 private void updateLightsLocked()
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 }
1074 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001075
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001076 // Don't flash while we are in a call or screen is on
1077 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001078 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001079 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001080 int ledARGB = mLedNotification.notification.ledARGB;
1081 int ledOnMS = mLedNotification.notification.ledOnMS;
1082 int ledOffMS = mLedNotification.notification.ledOffMS;
1083 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1084 ledARGB = mDefaultNotificationColor;
1085 ledOnMS = mDefaultNotificationLedOn;
1086 ledOffMS = mDefaultNotificationLedOff;
1087 }
1088 if (mNotificationPulseEnabled) {
1089 // pulse repeatedly
1090 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1091 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05001092 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
1095
1096 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001097 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 {
1099 ArrayList<NotificationRecord> list = mNotificationList;
1100 final int len = list.size();
1101 for (int i=0; i<len; i++) {
1102 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001103 if (tag == null) {
1104 if (r.tag != null) {
1105 continue;
1106 }
1107 } else {
1108 if (!tag.equals(r.tag)) {
1109 continue;
1110 }
1111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 if (r.id == id && r.pkg.equals(pkg)) {
1113 return i;
1114 }
1115 }
1116 return -1;
1117 }
1118
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001119 private void updateNotificationPulse() {
1120 synchronized (mNotificationList) {
1121 updateLightsLocked();
1122 }
1123 }
1124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 // ======================================================================
1126 @Override
1127 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1128 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1129 != PackageManager.PERMISSION_GRANTED) {
1130 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1131 + Binder.getCallingPid()
1132 + ", uid=" + Binder.getCallingUid());
1133 return;
1134 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 pw.println("Current Notification Manager state:");
1137
1138 int N;
1139
1140 synchronized (mToastQueue) {
1141 N = mToastQueue.size();
1142 if (N > 0) {
1143 pw.println(" Toast Queue:");
1144 for (int i=0; i<N; i++) {
1145 mToastQueue.get(i).dump(pw, " ");
1146 }
1147 pw.println(" ");
1148 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 }
1151
1152 synchronized (mNotificationList) {
1153 N = mNotificationList.size();
1154 if (N > 0) {
1155 pw.println(" Notification List:");
1156 for (int i=0; i<N; i++) {
1157 mNotificationList.get(i).dump(pw, " ", mContext);
1158 }
1159 pw.println(" ");
1160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 N = mLights.size();
1163 if (N > 0) {
1164 pw.println(" Lights List:");
1165 for (int i=0; i<N; i++) {
1166 mLights.get(i).dump(pw, " ", mContext);
1167 }
1168 pw.println(" ");
1169 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 pw.println(" mSoundNotification=" + mSoundNotification);
1172 pw.println(" mSound=" + mSound);
1173 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001174 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1175 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
1177 }
1178}