blob: b3eceb10864c337d8466657178a1959c38e26a1a [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 Lockwoodece18ef2012-02-13 20:42:19 -0800771 if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
772 mAttentionLight.pulse();
773 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 }
775 finally {
776 Binder.restoreCallingIdentity(identity);
777 }
778 }
Joe Onorato30275482009-07-08 17:09:14 -0700779 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500781 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 if (old != null && old.statusBarKey != null) {
783 long identity = Binder.clearCallingIdentity();
784 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700785 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 }
787 finally {
788 Binder.restoreCallingIdentity(identity);
789 }
790 }
791 }
792
793 // If we're not supposed to beep, vibrate, etc. then don't.
794 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
795 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700796 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
797 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800798
799 final AudioManager audioManager = (AudioManager) mContext
800 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 // sound
802 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800803 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 if (useDefaultSound || notification.sound != null) {
805 Uri uri;
806 if (useDefaultSound) {
807 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
808 } else {
809 uri = notification.sound;
810 }
811 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
812 int audioStreamType;
813 if (notification.audioStreamType >= 0) {
814 audioStreamType = notification.audioStreamType;
815 } else {
816 audioStreamType = DEFAULT_STREAM_TYPE;
817 }
818 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800819 // do not play notifications if stream volume is 0
820 // (typically because ringer mode is silent).
821 if (audioManager.getStreamVolume(audioStreamType) != 0) {
822 long identity = Binder.clearCallingIdentity();
823 try {
824 mSound.play(mContext, uri, looping, audioStreamType);
825 }
826 finally {
827 Binder.restoreCallingIdentity(identity);
828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 }
830 }
831
832 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800834 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 if ((useDefaultVibrate || notification.vibrate != null)
836 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
837 mVibrateNotification = r;
838
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800839 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 : notification.vibrate,
841 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
842 }
843 }
844
845 // this option doesn't shut off the lights
846
847 // light
848 // the most recent thing gets the light
849 mLights.remove(old);
850 if (mLedNotification == old) {
851 mLedNotification = null;
852 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800853 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
855 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
856 mLights.add(r);
857 updateLightsLocked();
858 } else {
859 if (old != null
860 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
861 updateLightsLocked();
862 }
863 }
864 }
865
866 idOut[0] = id;
867 }
868
Joe Onorato30275482009-07-08 17:09:14 -0700869 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700870 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
871 if (!manager.isEnabled()) {
872 return;
873 }
874
875 AccessibilityEvent event =
876 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
877 event.setPackageName(packageName);
878 event.setClassName(Notification.class.getName());
879 event.setParcelableData(notification);
880 CharSequence tickerText = notification.tickerText;
881 if (!TextUtils.isEmpty(tickerText)) {
882 event.getText().add(tickerText);
883 }
884
885 manager.sendAccessibilityEvent(event);
886 }
887
Joe Onorato46439ce2010-11-19 13:56:21 -0800888 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
889 // tell the app
890 if (sendDelete) {
891 if (r.notification.deleteIntent != null) {
892 try {
893 r.notification.deleteIntent.send();
894 } catch (PendingIntent.CanceledException ex) {
895 // do nothing - there's no relevant way to recover, and
896 // no reason to let this propagate
897 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
898 }
899 }
900 }
901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 // status bar
903 if (r.notification.icon != 0) {
904 long identity = Binder.clearCallingIdentity();
905 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700906 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 }
908 finally {
909 Binder.restoreCallingIdentity(identity);
910 }
911 r.statusBarKey = null;
912 }
913
914 // sound
915 if (mSoundNotification == r) {
916 mSoundNotification = null;
917 long identity = Binder.clearCallingIdentity();
918 try {
919 mSound.stop();
920 }
921 finally {
922 Binder.restoreCallingIdentity(identity);
923 }
924 }
925
926 // vibrate
927 if (mVibrateNotification == r) {
928 mVibrateNotification = null;
929 long identity = Binder.clearCallingIdentity();
930 try {
931 mVibrator.cancel();
932 }
933 finally {
934 Binder.restoreCallingIdentity(identity);
935 }
936 }
937
938 // light
939 mLights.remove(r);
940 if (mLedNotification == r) {
941 mLedNotification = null;
942 }
943 }
944
945 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700946 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800947 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700949 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Joe Onorato46439ce2010-11-19 13:56:21 -0800950 int mustNotHaveFlags, boolean sendDelete) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -0500951 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag,
952 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953
954 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700955 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700957 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
960 return;
961 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700962 if ((r.notification.flags & mustNotHaveFlags) != 0) {
963 return;
964 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 mNotificationList.remove(index);
967
Joe Onorato46439ce2010-11-19 13:56:21 -0800968 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 updateLightsLocked();
970 }
971 }
972 }
973
974 /**
975 * Cancels all notifications from a given package that have all of the
976 * {@code mustHaveFlags}.
977 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800978 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
979 int mustNotHaveFlags, boolean doit) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -0500980 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags,
981 mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982
983 synchronized (mNotificationList) {
984 final int N = mNotificationList.size();
985 boolean canceledSomething = false;
986 for (int i = N-1; i >= 0; --i) {
987 NotificationRecord r = mNotificationList.get(i);
988 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
989 continue;
990 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700991 if ((r.notification.flags & mustNotHaveFlags) != 0) {
992 continue;
993 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 if (!r.pkg.equals(pkg)) {
995 continue;
996 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800997 canceledSomething = true;
998 if (!doit) {
999 return true;
1000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001002 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 }
1004 if (canceledSomething) {
1005 updateLightsLocked();
1006 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001007 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
1009 }
1010
Andy Stadler110988c2010-12-03 14:29:16 -08001011 @Deprecated
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001012 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001013 cancelNotificationWithTag(pkg, null /* tag */, id);
1014 }
1015
1016 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001017 checkIncomingCall(pkg);
1018 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001019 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001020 Binder.getCallingUid() == Process.SYSTEM_UID
Joe Onorato46439ce2010-11-19 13:56:21 -08001021 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 }
1023
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001024 public void cancelAllNotifications(String pkg) {
1025 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001026
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001027 // Calling from user space, don't allow the canceling of actively
1028 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001029 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 }
1031
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001032 void checkIncomingCall(String pkg) {
1033 int uid = Binder.getCallingUid();
1034 if (uid == Process.SYSTEM_UID || uid == 0) {
1035 return;
1036 }
1037 try {
1038 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
1039 pkg, 0);
Amith Yamasani742a6712011-05-04 14:49:28 -07001040 if (!UserId.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001041 throw new SecurityException("Calling uid " + uid + " gave package"
1042 + pkg + " which is owned by uid " + ai.uid);
1043 }
1044 } catch (PackageManager.NameNotFoundException e) {
1045 throw new SecurityException("Unknown package " + pkg);
1046 }
1047 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001048
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001049 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 synchronized (mNotificationList) {
1051 final int N = mNotificationList.size();
1052 for (int i=N-1; i>=0; i--) {
1053 NotificationRecord r = mNotificationList.get(i);
1054
1055 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1056 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001058 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 }
1060 }
1061
1062 updateLightsLocked();
1063 }
1064 }
1065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 // lock on mNotificationList
1067 private void updateLightsLocked()
1068 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001069 // handle notification lights
1070 if (mLedNotification == null) {
1071 // get next notification, if any
1072 int n = mLights.size();
1073 if (n > 0) {
1074 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001077
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001078 // Don't flash while we are in a call or screen is on
1079 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001080 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001081 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001082 int ledARGB = mLedNotification.notification.ledARGB;
1083 int ledOnMS = mLedNotification.notification.ledOnMS;
1084 int ledOffMS = mLedNotification.notification.ledOffMS;
1085 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1086 ledARGB = mDefaultNotificationColor;
1087 ledOnMS = mDefaultNotificationLedOn;
1088 ledOffMS = mDefaultNotificationLedOff;
1089 }
1090 if (mNotificationPulseEnabled) {
1091 // pulse repeatedly
1092 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1093 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05001094 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
1097
1098 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001099 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 {
1101 ArrayList<NotificationRecord> list = mNotificationList;
1102 final int len = list.size();
1103 for (int i=0; i<len; i++) {
1104 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001105 if (tag == null) {
1106 if (r.tag != null) {
1107 continue;
1108 }
1109 } else {
1110 if (!tag.equals(r.tag)) {
1111 continue;
1112 }
1113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 if (r.id == id && r.pkg.equals(pkg)) {
1115 return i;
1116 }
1117 }
1118 return -1;
1119 }
1120
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001121 private void updateNotificationPulse() {
1122 synchronized (mNotificationList) {
1123 updateLightsLocked();
1124 }
1125 }
1126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 // ======================================================================
1128 @Override
1129 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1130 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1131 != PackageManager.PERMISSION_GRANTED) {
1132 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1133 + Binder.getCallingPid()
1134 + ", uid=" + Binder.getCallingUid());
1135 return;
1136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 pw.println("Current Notification Manager state:");
1139
1140 int N;
1141
1142 synchronized (mToastQueue) {
1143 N = mToastQueue.size();
1144 if (N > 0) {
1145 pw.println(" Toast Queue:");
1146 for (int i=0; i<N; i++) {
1147 mToastQueue.get(i).dump(pw, " ");
1148 }
1149 pw.println(" ");
1150 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
1153
1154 synchronized (mNotificationList) {
1155 N = mNotificationList.size();
1156 if (N > 0) {
1157 pw.println(" Notification List:");
1158 for (int i=0; i<N; i++) {
1159 mNotificationList.get(i).dump(pw, " ", mContext);
1160 }
1161 pw.println(" ");
1162 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 N = mLights.size();
1165 if (N > 0) {
1166 pw.println(" Lights List:");
1167 for (int i=0; i<N; i++) {
1168 mLights.get(i).dump(pw, " ", mContext);
1169 }
1170 pw.println(" ");
1171 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 pw.println(" mSoundNotification=" + mSoundNotification);
1174 pw.println(" mSound=" + mSound);
1175 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001176 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1177 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 }
1179 }
1180}