blob: 8066fa70991f5e168a3b3073fbaddc3f2b65de5e [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;
Joe Onorato7a0f36b2010-06-07 10:24:36 -070020import com.android.server.StatusBarManagerService;
svetoslavganov75986cf2009-05-14 22:28:01 -070021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.app.ActivityManagerNative;
23import android.app.IActivityManager;
24import android.app.INotificationManager;
25import android.app.ITransientNotification;
26import android.app.Notification;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070027import android.app.NotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.app.PendingIntent;
29import android.app.StatusBarManager;
30import android.content.BroadcastReceiver;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070031import android.content.ComponentName;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070032import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.Context;
34import android.content.Intent;
35import android.content.IntentFilter;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070036import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.PackageManager;
38import android.content.pm.PackageManager.NameNotFoundException;
39import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070040import android.database.ContentObserver;
Mike Lockwoodff2544c2010-06-28 09:17:50 -040041import android.hardware.Usb;
svetoslavganov75986cf2009-05-14 22:28:01 -070042import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.net.Uri;
44import android.os.BatteryManager;
Mike Lockwoodff2544c2010-06-28 09:17:50 -040045import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.IBinder;
49import android.os.Message;
50import android.os.Power;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070051import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070052import android.os.RemoteException;
Mike Lockwooded760372009-07-09 07:07:27 -040053import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Vibrator;
55import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050056import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080059import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.util.Log;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.widget.Toast;
Daniel Sandler0f0b11c2010-08-04 15:54:58 -040064import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import java.io.FileDescriptor;
67import java.io.PrintWriter;
68import java.util.ArrayList;
69import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040071/** {@hide} */
72public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073{
74 private static final String TAG = "NotificationService";
75 private static final boolean DBG = false;
76
Joe Onoratobd73d012010-06-04 11:44:54 -070077 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
78
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 // message codes
80 private static final int MESSAGE_TIMEOUT = 2;
81
82 private static final int LONG_DELAY = 3500; // 3.5 seconds
83 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -080084
85 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
88
89 final Context mContext;
90 final IActivityManager mAm;
91 final IBinder mForegroundToken = new Binder();
92
93 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -070094 private StatusBarManagerService mStatusBar;
Mike Lockwood3a322132009-11-24 00:30:52 -050095 private LightsService mLightsService;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050096 private LightsService.Light mBatteryLight;
97 private LightsService.Light mNotificationLight;
98 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099
Mike Lockwood670f9322010-01-20 12:13:36 -0500100 private int mDefaultNotificationColor;
101 private int mDefaultNotificationLedOn;
102 private int mDefaultNotificationLedOff;
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 private NotificationRecord mSoundNotification;
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700105 private NotificationPlayer mSound;
Joe Onorato30275482009-07-08 17:09:14 -0700106 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400107 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
109 private NotificationRecord mVibrateNotification;
110 private Vibrator mVibrator = new Vibrator();
111
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500112 // for enabling and disabling notification pulse behavior
113 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500114 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500115 private boolean mNotificationPulseEnabled;
Mike Lockwood2117f6f2010-09-09 09:48:08 -0400116 // This is true if we have received a new notification while the screen is off
117 // (that is, if mLedNotification was set while the screen was off)
118 // This is reset to false when the screen is turned on.
119 private boolean mPendingPulseNotification;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500120
121 // for adb connected notifications
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700122 private boolean mAdbNotificationShown = false;
123 private Notification mAdbNotification;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800124
Fred Quintana6ecaff12009-09-25 14:23:13 -0700125 private final ArrayList<NotificationRecord> mNotificationList =
126 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
128 private ArrayList<ToastRecord> mToastQueue;
129
130 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
131
132 private boolean mBatteryCharging;
133 private boolean mBatteryLow;
134 private boolean mBatteryFull;
135 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700136
Kenneth Anderssond5d87b22010-08-03 13:30:18 +0200137 private static int mBatteryLowARGB;
138 private static int mBatteryMediumARGB;
139 private static int mBatteryFullARGB;
140 private static int mBatteryLedOn;
141 private static int mBatteryLedOff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 private static String idDebugString(Context baseContext, String packageName, int id) {
144 Context c = null;
145
146 if (packageName != null) {
147 try {
148 c = baseContext.createPackageContext(packageName, 0);
149 } catch (NameNotFoundException e) {
150 c = baseContext;
151 }
152 } else {
153 c = baseContext;
154 }
155
156 String pkg;
157 String type;
158 String name;
159
160 Resources r = c.getResources();
161 try {
162 return r.getResourceName(id);
163 } catch (Resources.NotFoundException e) {
164 return "<name unknown>";
165 }
166 }
167
168 private static final class NotificationRecord
169 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700170 final String pkg;
171 final String tag;
172 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700173 final int uid;
174 final int initialPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 ITransientNotification callback;
176 int duration;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700177 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 IBinder statusBarKey;
179
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700180 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid,
181 Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 {
183 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700184 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700186 this.uid = uid;
187 this.initialPid = initialPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 this.notification = notification;
189 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 void dump(PrintWriter pw, String prefix, Context baseContext) {
192 pw.println(prefix + this);
193 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
194 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
195 pw.println(prefix + " contentIntent=" + notification.contentIntent);
196 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
197 pw.println(prefix + " tickerText=" + notification.tickerText);
198 pw.println(prefix + " contentView=" + notification.contentView);
199 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
200 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
201 pw.println(prefix + " sound=" + notification.sound);
202 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
203 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
204 + " ledOnMS=" + notification.ledOnMS
205 + " ledOffMS=" + notification.ledOffMS);
206 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 @Override
209 public final String toString()
210 {
211 return "NotificationRecord{"
212 + Integer.toHexString(System.identityHashCode(this))
213 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700214 + " id=" + Integer.toHexString(id)
215 + " tag=" + tag + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 }
217 }
218
219 private static final class ToastRecord
220 {
221 final int pid;
222 final String pkg;
223 final ITransientNotification callback;
224 int duration;
225
226 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
227 {
228 this.pid = pid;
229 this.pkg = pkg;
230 this.callback = callback;
231 this.duration = duration;
232 }
233
234 void update(int duration) {
235 this.duration = duration;
236 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 void dump(PrintWriter pw, String prefix) {
239 pw.println(prefix + this);
240 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 @Override
243 public final String toString()
244 {
245 return "ToastRecord{"
246 + Integer.toHexString(System.identityHashCode(this))
247 + " pkg=" + pkg
248 + " callback=" + callback
249 + " duration=" + duration;
250 }
251 }
252
Joe Onorato089de882010-04-12 08:18:45 -0700253 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
254 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255
256 public void onSetDisabled(int status) {
257 synchronized (mNotificationList) {
258 mDisabledNotifications = status;
259 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
260 // cancel whatever's going on
261 long identity = Binder.clearCallingIdentity();
262 try {
263 mSound.stop();
264 }
265 finally {
266 Binder.restoreCallingIdentity(identity);
267 }
268
269 identity = Binder.clearCallingIdentity();
270 try {
271 mVibrator.cancel();
272 }
273 finally {
274 Binder.restoreCallingIdentity(identity);
275 }
276 }
277 }
278 }
279
280 public void onClearAll() {
281 cancelAll();
282 }
283
Fred Quintana6ecaff12009-09-25 14:23:13 -0700284 public void onNotificationClick(String pkg, String tag, int id) {
285 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700286 Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 }
288
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400289 public void onNotificationClear(String pkg, String tag, int id) {
290 cancelNotification(pkg, tag, id, 0, 0); // maybe add some flags?
291 }
292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 public void onPanelRevealed() {
294 synchronized (mNotificationList) {
295 // sound
296 mSoundNotification = null;
297 long identity = Binder.clearCallingIdentity();
298 try {
299 mSound.stop();
300 }
301 finally {
302 Binder.restoreCallingIdentity(identity);
303 }
304
305 // vibrate
306 mVibrateNotification = null;
307 identity = Binder.clearCallingIdentity();
308 try {
309 mVibrator.cancel();
310 }
311 finally {
312 Binder.restoreCallingIdentity(identity);
313 }
314
315 // light
316 mLights.clear();
317 mLedNotification = null;
318 updateLightsLocked();
319 }
320 }
Joe Onorato005847b2010-06-04 16:08:02 -0400321
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700322 public void onNotificationError(String pkg, String tag, int id,
323 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400324 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
325 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Joe Onorato005847b2010-06-04 16:08:02 -0400326 cancelNotification(pkg, tag, id, 0, 0);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700327 long ident = Binder.clearCallingIdentity();
328 try {
329 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
330 "Bad notification posted from package " + pkg
331 + ": " + message);
332 } catch (RemoteException e) {
333 }
334 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 };
337
338 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
339 @Override
340 public void onReceive(Context context, Intent intent) {
341 String action = intent.getAction();
342
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800343 boolean queryRestart = false;
344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
346 boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0);
347 int level = intent.getIntExtra("level", -1);
348 boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD);
349 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
350 boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90);
351
352 if (batteryCharging != mBatteryCharging ||
353 batteryLow != mBatteryLow ||
354 batteryFull != mBatteryFull) {
355 mBatteryCharging = batteryCharging;
356 mBatteryLow = batteryLow;
357 mBatteryFull = batteryFull;
358 updateLights();
359 }
Mike Lockwoodff2544c2010-06-28 09:17:50 -0400360 } else if (action.equals(Usb.ACTION_USB_STATE)) {
361 Bundle extras = intent.getExtras();
362 boolean usbConnected = extras.getBoolean(Usb.USB_CONNECTED);
363 boolean adbEnabled = (Usb.USB_FUNCTION_ENABLED.equals(
364 extras.getString(Usb.USB_FUNCTION_ADB)));
365 updateAdbNotification(usbConnected && adbEnabled);
366 } else if (action.equals(Usb.ACTION_USB_DISCONNECTED)) {
367 updateAdbNotification(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800369 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800370 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800371 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800372 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800373 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800374 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800375 } else if (queryRestart) {
376 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800377 } else {
378 Uri uri = intent.getData();
379 if (uri == null) {
380 return;
381 }
382 String pkgName = uri.getSchemeSpecificPart();
383 if (pkgName == null) {
384 return;
385 }
386 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800388 if (pkgList != null && (pkgList.length > 0)) {
389 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800390 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500393 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
394 mScreenOn = true;
395 updateNotificationPulse();
396 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
397 mScreenOn = false;
398 updateNotificationPulse();
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500399 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
400 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK));
401 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 }
403 }
404 };
405
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700406 class SettingsObserver extends ContentObserver {
407 SettingsObserver(Handler handler) {
408 super(handler);
409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800410
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700411 void observe() {
412 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500413 resolver.registerContentObserver(Settings.System.getUriFor(
414 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700415 update();
416 }
417
418 @Override public void onChange(boolean selfChange) {
419 update();
420 }
421
422 public void update() {
423 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500424 boolean pulseEnabled = Settings.System.getInt(resolver,
425 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
426 if (mNotificationPulseEnabled != pulseEnabled) {
427 mNotificationPulseEnabled = pulseEnabled;
428 updateNotificationPulse();
429 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700430 }
431 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500432
Joe Onorato089de882010-04-12 08:18:45 -0700433 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500434 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 {
436 super();
437 mContext = context;
Mike Lockwood3a322132009-11-24 00:30:52 -0500438 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 mAm = ActivityManagerNative.getDefault();
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700440 mSound = new NotificationPlayer(TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 mSound.setUsesWakeLock(context);
442 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800444
Joe Onorato089de882010-04-12 08:18:45 -0700445 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 statusBar.setNotificationCallbacks(mNotificationCallbacks);
447
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500448 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
449 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
450 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
451
Mike Lockwood670f9322010-01-20 12:13:36 -0500452 Resources resources = mContext.getResources();
453 mDefaultNotificationColor = resources.getColor(
454 com.android.internal.R.color.config_defaultNotificationColor);
455 mDefaultNotificationLedOn = resources.getInteger(
456 com.android.internal.R.integer.config_defaultNotificationLedOn);
457 mDefaultNotificationLedOff = resources.getInteger(
458 com.android.internal.R.integer.config_defaultNotificationLedOff);
459
Kenneth Anderssond5d87b22010-08-03 13:30:18 +0200460 mBatteryLowARGB = mContext.getResources().getInteger(
461 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
462 mBatteryMediumARGB = mContext.getResources().getInteger(
463 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
464 mBatteryFullARGB = mContext.getResources().getInteger(
465 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
466 mBatteryLedOn = mContext.getResources().getInteger(
467 com.android.internal.R.integer.config_notificationsBatteryLedOn);
468 mBatteryLedOff = mContext.getResources().getInteger(
469 com.android.internal.R.integer.config_notificationsBatteryLedOff);
470
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400471 // Don't start allowing notifications until the setup wizard has run once.
472 // After that, including subsequent boots, init with notifications turned on.
473 // This works on the first boot because the setup wizard will toggle this
474 // flag at least once and we'll go back to 0 after that.
475 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
476 Settings.Secure.DEVICE_PROVISIONED, 0)) {
477 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
478 }
479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 // register for battery changed notifications
481 IntentFilter filter = new IntentFilter();
482 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Mike Lockwoodff2544c2010-06-28 09:17:50 -0400483 filter.addAction(Usb.ACTION_USB_STATE);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500484 filter.addAction(Intent.ACTION_SCREEN_ON);
485 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500486 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800488 IntentFilter pkgFilter = new IntentFilter();
489 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
490 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
491 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
492 pkgFilter.addDataScheme("package");
493 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800494 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800495 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800496
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500497 SettingsObserver observer = new SettingsObserver(mHandler);
498 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 }
500
Joe Onorato30275482009-07-08 17:09:14 -0700501 void systemReady() {
502 // no beeping until we're basically done booting
503 mSystemReady = true;
504 }
505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 // Toasts
507 // ============================================================================
508 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
509 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400510 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511
512 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800513 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 return ;
515 }
516
517 synchronized (mToastQueue) {
518 int callingPid = Binder.getCallingPid();
519 long callingId = Binder.clearCallingIdentity();
520 try {
521 ToastRecord record;
522 int index = indexOfToastLocked(pkg, callback);
523 // If it's already in the queue, we update it in place, we don't
524 // move it to the end of the queue.
525 if (index >= 0) {
526 record = mToastQueue.get(index);
527 record.update(duration);
528 } else {
529 record = new ToastRecord(callingPid, pkg, callback, duration);
530 mToastQueue.add(record);
531 index = mToastQueue.size() - 1;
532 keepProcessAliveLocked(callingPid);
533 }
534 // If it's at index 0, it's the current toast. It doesn't matter if it's
535 // new or just been updated. Call back and tell it to show itself.
536 // If the callback fails, this will remove it from the list, so don't
537 // assume that it's valid after this.
538 if (index == 0) {
539 showNextToastLocked();
540 }
541 } finally {
542 Binder.restoreCallingIdentity(callingId);
543 }
544 }
545 }
546
547 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800548 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549
550 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800551 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 return ;
553 }
554
555 synchronized (mToastQueue) {
556 long callingId = Binder.clearCallingIdentity();
557 try {
558 int index = indexOfToastLocked(pkg, callback);
559 if (index >= 0) {
560 cancelToastLocked(index);
561 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800562 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
564 } finally {
565 Binder.restoreCallingIdentity(callingId);
566 }
567 }
568 }
569
570 private void showNextToastLocked() {
571 ToastRecord record = mToastQueue.get(0);
572 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800573 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 try {
575 record.callback.show();
576 scheduleTimeoutLocked(record, false);
577 return;
578 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800579 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 + " in package " + record.pkg);
581 // remove it from the list and let the process die
582 int index = mToastQueue.indexOf(record);
583 if (index >= 0) {
584 mToastQueue.remove(index);
585 }
586 keepProcessAliveLocked(record.pid);
587 if (mToastQueue.size() > 0) {
588 record = mToastQueue.get(0);
589 } else {
590 record = null;
591 }
592 }
593 }
594 }
595
596 private void cancelToastLocked(int index) {
597 ToastRecord record = mToastQueue.get(index);
598 try {
599 record.callback.hide();
600 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800601 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 + " in package " + record.pkg);
603 // don't worry about this, we're about to remove it from
604 // the list anyway
605 }
606 mToastQueue.remove(index);
607 keepProcessAliveLocked(record.pid);
608 if (mToastQueue.size() > 0) {
609 // Show the next one. If the callback fails, this will remove
610 // it from the list, so don't assume that the list hasn't changed
611 // after this point.
612 showNextToastLocked();
613 }
614 }
615
616 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
617 {
618 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
619 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
620 mHandler.removeCallbacksAndMessages(r);
621 mHandler.sendMessageDelayed(m, delay);
622 }
623
624 private void handleTimeout(ToastRecord record)
625 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800626 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 synchronized (mToastQueue) {
628 int index = indexOfToastLocked(record.pkg, record.callback);
629 if (index >= 0) {
630 cancelToastLocked(index);
631 }
632 }
633 }
634
635 // lock on mToastQueue
636 private int indexOfToastLocked(String pkg, ITransientNotification callback)
637 {
638 IBinder cbak = callback.asBinder();
639 ArrayList<ToastRecord> list = mToastQueue;
640 int len = list.size();
641 for (int i=0; i<len; i++) {
642 ToastRecord r = list.get(i);
643 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
644 return i;
645 }
646 }
647 return -1;
648 }
649
650 // lock on mToastQueue
651 private void keepProcessAliveLocked(int pid)
652 {
653 int toastCount = 0; // toasts from this pid
654 ArrayList<ToastRecord> list = mToastQueue;
655 int N = list.size();
656 for (int i=0; i<N; i++) {
657 ToastRecord r = list.get(i);
658 if (r.pid == pid) {
659 toastCount++;
660 }
661 }
662 try {
663 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
664 } catch (RemoteException e) {
665 // Shouldn't happen.
666 }
667 }
668
669 private final class WorkerHandler extends Handler
670 {
671 @Override
672 public void handleMessage(Message msg)
673 {
674 switch (msg.what)
675 {
676 case MESSAGE_TIMEOUT:
677 handleTimeout((ToastRecord)msg.obj);
678 break;
679 }
680 }
681 }
682
683
684 // Notifications
685 // ============================================================================
686 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
687 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700688 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
689 }
690
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400691 public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,
692 int[] idOut)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700693 {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400694 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
695 tag, id, notification, idOut);
696 }
697
698 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
699 // uid/pid of another application)
700 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
701 String tag, int id, Notification notification, int[] idOut)
702 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700703 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800704
Joe Onoratobd73d012010-06-04 11:44:54 -0700705 // Limit the number of notifications that any given package except the android
706 // package can enqueue. Prevents DOS attacks and deals with leaks.
707 if (!"android".equals(pkg)) {
708 synchronized (mNotificationList) {
709 int count = 0;
710 final int N = mNotificationList.size();
711 for (int i=0; i<N; i++) {
712 final NotificationRecord r = mNotificationList.get(i);
713 if (r.pkg.equals(pkg)) {
714 count++;
715 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
716 Slog.e(TAG, "Package has already posted " + count
717 + " notifications. Not showing more. package=" + pkg);
718 return;
719 }
720 }
721 }
722 }
723 }
724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 // This conditional is a dirty hack to limit the logging done on
726 // behalf of the download manager without affecting other apps.
727 if (!pkg.equals("com.android.providers.downloads")
728 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800729 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 }
731
732 if (pkg == null || notification == null) {
733 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
734 + " id=" + id + " notification=" + notification);
735 }
736 if (notification.icon != 0) {
737 if (notification.contentView == null) {
738 throw new IllegalArgumentException("contentView required: pkg=" + pkg
739 + " id=" + id + " notification=" + notification);
740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742
743 synchronized (mNotificationList) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700744 NotificationRecord r = new NotificationRecord(pkg, tag, id,
745 callingUid, callingPid, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 NotificationRecord old = null;
747
Fred Quintana6ecaff12009-09-25 14:23:13 -0700748 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 if (index < 0) {
750 mNotificationList.add(r);
751 } else {
752 old = mNotificationList.remove(index);
753 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700754 // Make sure we don't lose the foreground service state.
755 if (old != null) {
756 notification.flags |=
757 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800760
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700761 // Ensure if this is a foreground service that the proper additional
762 // flags are set.
763 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
764 notification.flags |= Notification.FLAG_ONGOING_EVENT
765 | Notification.FLAG_NO_CLEAR;
766 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 if (notification.icon != 0) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700769 StatusBarNotification n = new StatusBarNotification(pkg, id, tag,
770 r.uid, r.initialPid, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 if (old != null && old.statusBarKey != null) {
772 r.statusBarKey = old.statusBarKey;
773 long identity = Binder.clearCallingIdentity();
774 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700775 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 }
777 finally {
778 Binder.restoreCallingIdentity(identity);
779 }
780 } else {
781 long identity = Binder.clearCallingIdentity();
782 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700783 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500784 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 }
786 finally {
787 Binder.restoreCallingIdentity(identity);
788 }
789 }
Joe Onorato30275482009-07-08 17:09:14 -0700790 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 } else {
792 if (old != null && old.statusBarKey != null) {
793 long identity = Binder.clearCallingIdentity();
794 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700795 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 }
797 finally {
798 Binder.restoreCallingIdentity(identity);
799 }
800 }
801 }
802
803 // If we're not supposed to beep, vibrate, etc. then don't.
804 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
805 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700806 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
807 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800808
809 final AudioManager audioManager = (AudioManager) mContext
810 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 // sound
812 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800813 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 if (useDefaultSound || notification.sound != null) {
815 Uri uri;
816 if (useDefaultSound) {
817 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
818 } else {
819 uri = notification.sound;
820 }
821 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
822 int audioStreamType;
823 if (notification.audioStreamType >= 0) {
824 audioStreamType = notification.audioStreamType;
825 } else {
826 audioStreamType = DEFAULT_STREAM_TYPE;
827 }
828 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800829 // do not play notifications if stream volume is 0
830 // (typically because ringer mode is silent).
831 if (audioManager.getStreamVolume(audioStreamType) != 0) {
832 long identity = Binder.clearCallingIdentity();
833 try {
834 mSound.play(mContext, uri, looping, audioStreamType);
835 }
836 finally {
837 Binder.restoreCallingIdentity(identity);
838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 }
840 }
841
842 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800844 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 if ((useDefaultVibrate || notification.vibrate != null)
846 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
847 mVibrateNotification = r;
848
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800849 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 : notification.vibrate,
851 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
852 }
853 }
854
855 // this option doesn't shut off the lights
856
857 // light
858 // the most recent thing gets the light
859 mLights.remove(old);
860 if (mLedNotification == old) {
861 mLedNotification = null;
862 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800863 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
865 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
866 mLights.add(r);
867 updateLightsLocked();
868 } else {
869 if (old != null
870 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
871 updateLightsLocked();
872 }
873 }
874 }
875
876 idOut[0] = id;
877 }
878
Joe Onorato30275482009-07-08 17:09:14 -0700879 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700880 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
881 if (!manager.isEnabled()) {
882 return;
883 }
884
885 AccessibilityEvent event =
886 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
887 event.setPackageName(packageName);
888 event.setClassName(Notification.class.getName());
889 event.setParcelableData(notification);
890 CharSequence tickerText = notification.tickerText;
891 if (!TextUtils.isEmpty(tickerText)) {
892 event.getText().add(tickerText);
893 }
894
895 manager.sendAccessibilityEvent(event);
896 }
897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 private void cancelNotificationLocked(NotificationRecord r) {
899 // status bar
900 if (r.notification.icon != 0) {
901 long identity = Binder.clearCallingIdentity();
902 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700903 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 }
905 finally {
906 Binder.restoreCallingIdentity(identity);
907 }
908 r.statusBarKey = null;
909 }
910
911 // sound
912 if (mSoundNotification == r) {
913 mSoundNotification = null;
914 long identity = Binder.clearCallingIdentity();
915 try {
916 mSound.stop();
917 }
918 finally {
919 Binder.restoreCallingIdentity(identity);
920 }
921 }
922
923 // vibrate
924 if (mVibrateNotification == r) {
925 mVibrateNotification = null;
926 long identity = Binder.clearCallingIdentity();
927 try {
928 mVibrator.cancel();
929 }
930 finally {
931 Binder.restoreCallingIdentity(identity);
932 }
933 }
934
935 // light
936 mLights.remove(r);
937 if (mLedNotification == r) {
938 mLedNotification = null;
939 }
940 }
941
942 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700943 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800944 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700946 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700947 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800948 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949
950 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700951 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700953 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
956 return;
957 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700958 if ((r.notification.flags & mustNotHaveFlags) != 0) {
959 return;
960 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 mNotificationList.remove(index);
963
964 cancelNotificationLocked(r);
965 updateLightsLocked();
966 }
967 }
968 }
969
970 /**
971 * Cancels all notifications from a given package that have all of the
972 * {@code mustHaveFlags}.
973 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800974 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
975 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800976 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977
978 synchronized (mNotificationList) {
979 final int N = mNotificationList.size();
980 boolean canceledSomething = false;
981 for (int i = N-1; i >= 0; --i) {
982 NotificationRecord r = mNotificationList.get(i);
983 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
984 continue;
985 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700986 if ((r.notification.flags & mustNotHaveFlags) != 0) {
987 continue;
988 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 if (!r.pkg.equals(pkg)) {
990 continue;
991 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800992 canceledSomething = true;
993 if (!doit) {
994 return true;
995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 mNotificationList.remove(i);
997 cancelNotificationLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 }
999 if (canceledSomething) {
1000 updateLightsLocked();
1001 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001002 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 }
1004 }
1005
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001006
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001007 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001008 cancelNotificationWithTag(pkg, null /* tag */, id);
1009 }
1010
1011 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001012 checkIncomingCall(pkg);
1013 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001014 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001015 Binder.getCallingUid() == Process.SYSTEM_UID
1016 ? 0 : Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 }
1018
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001019 public void cancelAllNotifications(String pkg) {
1020 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001021
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001022 // Calling from user space, don't allow the canceling of actively
1023 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001024 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001027 void checkIncomingCall(String pkg) {
1028 int uid = Binder.getCallingUid();
1029 if (uid == Process.SYSTEM_UID || uid == 0) {
1030 return;
1031 }
1032 try {
1033 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
1034 pkg, 0);
1035 if (ai.uid != uid) {
1036 throw new SecurityException("Calling uid " + uid + " gave package"
1037 + pkg + " which is owned by uid " + ai.uid);
1038 }
1039 } catch (PackageManager.NameNotFoundException e) {
1040 throw new SecurityException("Unknown package " + pkg);
1041 }
1042 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001043
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001044 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 synchronized (mNotificationList) {
1046 final int N = mNotificationList.size();
1047 for (int i=N-1; i>=0; i--) {
1048 NotificationRecord r = mNotificationList.get(i);
1049
1050 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1051 | Notification.FLAG_NO_CLEAR)) == 0) {
1052 if (r.notification.deleteIntent != null) {
1053 try {
1054 r.notification.deleteIntent.send();
1055 } catch (PendingIntent.CanceledException ex) {
1056 // do nothing - there's no relevant way to recover, and
1057 // no reason to let this propagate
Joe Onorato8a9b2202010-02-26 18:56:32 -08001058 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 }
1060 }
1061 mNotificationList.remove(i);
1062 cancelNotificationLocked(r);
1063 }
1064 }
1065
1066 updateLightsLocked();
1067 }
1068 }
1069
1070 private void updateLights() {
1071 synchronized (mNotificationList) {
1072 updateLightsLocked();
1073 }
1074 }
1075
1076 // lock on mNotificationList
1077 private void updateLightsLocked()
1078 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001079 // Battery low always shows, other states only show if charging.
1080 if (mBatteryLow) {
Mike Lockwood445f4302009-09-04 11:06:46 -04001081 if (mBatteryCharging) {
Kenneth Anderssond5d87b22010-08-03 13:30:18 +02001082 mBatteryLight.setColor(mBatteryLowARGB);
Mike Lockwood445f4302009-09-04 11:06:46 -04001083 } else {
1084 // Flash when battery is low and not charging
Kenneth Anderssond5d87b22010-08-03 13:30:18 +02001085 mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED,
1086 mBatteryLedOn, mBatteryLedOff);
Mike Lockwood445f4302009-09-04 11:06:46 -04001087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001089 if (mBatteryFull) {
Kenneth Anderssond5d87b22010-08-03 13:30:18 +02001090 mBatteryLight.setColor(mBatteryFullARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 } else {
Kenneth Anderssond5d87b22010-08-03 13:30:18 +02001092 mBatteryLight.setColor(mBatteryMediumARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 }
1094 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001095 mBatteryLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097
Mike Lockwood2117f6f2010-09-09 09:48:08 -04001098 // clear pending pulse notification if screen is on
1099 if (mScreenOn || mLedNotification == null) {
1100 mPendingPulseNotification = false;
1101 }
1102
The Android Open Source Project10592532009-03-18 17:39:46 -07001103 // handle notification lights
1104 if (mLedNotification == null) {
1105 // get next notification, if any
1106 int n = mLights.size();
1107 if (n > 0) {
1108 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 }
Mike Lockwood2117f6f2010-09-09 09:48:08 -04001110 if (mLedNotification != null && !mScreenOn) {
1111 mPendingPulseNotification = true;
1112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001114
1115 // we only flash if screen is off and persistent pulsing is enabled
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001116 // and we are not currently in a call
Mike Lockwood2117f6f2010-09-09 09:48:08 -04001117 if (!mPendingPulseNotification || mScreenOn || mInCall) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001118 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001119 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001120 int ledARGB = mLedNotification.notification.ledARGB;
1121 int ledOnMS = mLedNotification.notification.ledOnMS;
1122 int ledOffMS = mLedNotification.notification.ledOffMS;
1123 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1124 ledARGB = mDefaultNotificationColor;
1125 ledOnMS = mDefaultNotificationLedOn;
1126 ledOffMS = mDefaultNotificationLedOff;
1127 }
1128 if (mNotificationPulseEnabled) {
1129 // pulse repeatedly
1130 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1131 ledOnMS, ledOffMS);
1132 } else {
1133 // pulse only once
1134 mNotificationLight.pulse(ledARGB, ledOnMS);
1135 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 }
1138
1139 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001140 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 {
1142 ArrayList<NotificationRecord> list = mNotificationList;
1143 final int len = list.size();
1144 for (int i=0; i<len; i++) {
1145 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001146 if (tag == null) {
1147 if (r.tag != null) {
1148 continue;
1149 }
1150 } else {
1151 if (!tag.equals(r.tag)) {
1152 continue;
1153 }
1154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 if (r.id == id && r.pkg.equals(pkg)) {
1156 return i;
1157 }
1158 }
1159 return -1;
1160 }
1161
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001162 // This is here instead of StatusBarPolicy because it is an important
1163 // security feature that we don't want people customizing the platform
1164 // to accidentally lose.
Mike Lockwoodff2544c2010-06-28 09:17:50 -04001165 private void updateAdbNotification(boolean adbEnabled) {
1166 if (adbEnabled) {
Mike Lockwooded760372009-07-09 07:07:27 -04001167 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1168 return;
1169 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001170 if (!mAdbNotificationShown) {
1171 NotificationManager notificationManager = (NotificationManager) mContext
1172 .getSystemService(Context.NOTIFICATION_SERVICE);
1173 if (notificationManager != null) {
1174 Resources r = mContext.getResources();
1175 CharSequence title = r.getText(
1176 com.android.internal.R.string.adb_active_notification_title);
1177 CharSequence message = r.getText(
1178 com.android.internal.R.string.adb_active_notification_message);
1179
1180 if (mAdbNotification == null) {
1181 mAdbNotification = new Notification();
Daniel Sandler39576c82010-03-25 16:02:33 -04001182 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001183 mAdbNotification.when = 0;
1184 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1185 mAdbNotification.tickerText = title;
Daniel Sandler39576c82010-03-25 16:02:33 -04001186 mAdbNotification.defaults = 0; // please be quiet
1187 mAdbNotification.sound = null;
1188 mAdbNotification.vibrate = null;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001189 }
1190
1191 Intent intent = new Intent(
1192 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1193 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1194 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1195 // Note: we are hard-coding the component because this is
1196 // an important security UI that we don't want anyone
1197 // intercepting.
1198 intent.setComponent(new ComponentName("com.android.settings",
1199 "com.android.settings.DevelopmentSettings"));
1200 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1201 intent, 0);
1202
1203 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001204
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001205 mAdbNotificationShown = true;
1206 notificationManager.notify(
1207 com.android.internal.R.string.adb_active_notification_title,
1208 mAdbNotification);
1209 }
1210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001211
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001212 } else if (mAdbNotificationShown) {
1213 NotificationManager notificationManager = (NotificationManager) mContext
1214 .getSystemService(Context.NOTIFICATION_SERVICE);
1215 if (notificationManager != null) {
1216 mAdbNotificationShown = false;
1217 notificationManager.cancel(
1218 com.android.internal.R.string.adb_active_notification_title);
1219 }
1220 }
1221 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001222
1223 private void updateNotificationPulse() {
1224 synchronized (mNotificationList) {
1225 updateLightsLocked();
1226 }
1227 }
1228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 // ======================================================================
1230 @Override
1231 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1232 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1233 != PackageManager.PERMISSION_GRANTED) {
1234 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1235 + Binder.getCallingPid()
1236 + ", uid=" + Binder.getCallingUid());
1237 return;
1238 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 pw.println("Current Notification Manager state:");
1241
1242 int N;
1243
1244 synchronized (mToastQueue) {
1245 N = mToastQueue.size();
1246 if (N > 0) {
1247 pw.println(" Toast Queue:");
1248 for (int i=0; i<N; i++) {
1249 mToastQueue.get(i).dump(pw, " ");
1250 }
1251 pw.println(" ");
1252 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 }
1255
1256 synchronized (mNotificationList) {
1257 N = mNotificationList.size();
1258 if (N > 0) {
1259 pw.println(" Notification List:");
1260 for (int i=0; i<N; i++) {
1261 mNotificationList.get(i).dump(pw, " ", mContext);
1262 }
1263 pw.println(" ");
1264 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 N = mLights.size();
1267 if (N > 0) {
1268 pw.println(" Lights List:");
1269 for (int i=0; i<N; i++) {
1270 mLights.get(i).dump(pw, " ", mContext);
1271 }
1272 pw.println(" ");
1273 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 pw.println(" mSoundNotification=" + mSoundNotification);
1276 pw.println(" mSound=" + mSound);
1277 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001278 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1279 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 }
1281 }
1282}