blob: b5c2b1bea32b2ed00d50279cda1f908923d752df [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 Onorato089de882010-04-12 08:18:45 -070020import com.android.server.status.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;
svetoslavganov75986cf2009-05-14 22:28:01 -070041import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.net.Uri;
43import android.os.BatteryManager;
44import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.IBinder;
47import android.os.Message;
48import android.os.Power;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070049import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070050import android.os.RemoteException;
Mike Lockwooded760372009-07-09 07:07:27 -040051import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Vibrator;
53import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050054import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080057import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.util.Log;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.view.accessibility.AccessibilityEvent;
60import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.widget.Toast;
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import java.io.FileDescriptor;
64import java.io.PrintWriter;
65import java.util.ArrayList;
66import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
68class NotificationManagerService extends INotificationManager.Stub
69{
70 private static final String TAG = "NotificationService";
71 private static final boolean DBG = false;
72
Joe Onoratobd73d012010-06-04 11:44:54 -070073 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
74
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 // message codes
76 private static final int MESSAGE_TIMEOUT = 2;
77
78 private static final int LONG_DELAY = 3500; // 3.5 seconds
79 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -080080
81 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
84
85 final Context mContext;
86 final IActivityManager mAm;
87 final IBinder mForegroundToken = new Binder();
88
89 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -070090 private StatusBarManagerService mStatusBar;
Mike Lockwood3a322132009-11-24 00:30:52 -050091 private LightsService mLightsService;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050092 private LightsService.Light mBatteryLight;
93 private LightsService.Light mNotificationLight;
94 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Mike Lockwood670f9322010-01-20 12:13:36 -050096 private int mDefaultNotificationColor;
97 private int mDefaultNotificationLedOn;
98 private int mDefaultNotificationLedOff;
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 private NotificationRecord mSoundNotification;
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700101 private NotificationPlayer mSound;
Joe Onorato30275482009-07-08 17:09:14 -0700102 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400103 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
105 private NotificationRecord mVibrateNotification;
106 private Vibrator mVibrator = new Vibrator();
107
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500108 // for enabling and disabling notification pulse behavior
109 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500110 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500111 private boolean mNotificationPulseEnabled;
112
113 // for adb connected notifications
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400114 private boolean mUsbConnected;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700115 private boolean mAdbEnabled = false;
116 private boolean mAdbNotificationShown = false;
117 private Notification mAdbNotification;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800118
Fred Quintana6ecaff12009-09-25 14:23:13 -0700119 private final ArrayList<NotificationRecord> mNotificationList =
120 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
122 private ArrayList<ToastRecord> mToastQueue;
123
124 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
125
126 private boolean mBatteryCharging;
127 private boolean mBatteryLow;
128 private boolean mBatteryFull;
129 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700130
The Android Open Source Project10592532009-03-18 17:39:46 -0700131 private static final int BATTERY_LOW_ARGB = 0xFFFF0000; // Charging Low - red solid on
132 private static final int BATTERY_MEDIUM_ARGB = 0xFFFFFF00; // Charging - orange solid on
133 private static final int BATTERY_FULL_ARGB = 0xFF00FF00; // Charging Full - green solid on
134 private static final int BATTERY_BLINK_ON = 125;
135 private static final int BATTERY_BLINK_OFF = 2875;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 private static String idDebugString(Context baseContext, String packageName, int id) {
138 Context c = null;
139
140 if (packageName != null) {
141 try {
142 c = baseContext.createPackageContext(packageName, 0);
143 } catch (NameNotFoundException e) {
144 c = baseContext;
145 }
146 } else {
147 c = baseContext;
148 }
149
150 String pkg;
151 String type;
152 String name;
153
154 Resources r = c.getResources();
155 try {
156 return r.getResourceName(id);
157 } catch (Resources.NotFoundException e) {
158 return "<name unknown>";
159 }
160 }
161
162 private static final class NotificationRecord
163 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700164 final String pkg;
165 final String tag;
166 final int id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 ITransientNotification callback;
168 int duration;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700169 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 IBinder statusBarKey;
171
Fred Quintana6ecaff12009-09-25 14:23:13 -0700172 NotificationRecord(String pkg, String tag, int id, Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 {
174 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700175 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 this.id = id;
177 this.notification = notification;
178 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 void dump(PrintWriter pw, String prefix, Context baseContext) {
181 pw.println(prefix + this);
182 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
183 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
184 pw.println(prefix + " contentIntent=" + notification.contentIntent);
185 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
186 pw.println(prefix + " tickerText=" + notification.tickerText);
187 pw.println(prefix + " contentView=" + notification.contentView);
188 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
189 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
190 pw.println(prefix + " sound=" + notification.sound);
191 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
192 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
193 + " ledOnMS=" + notification.ledOnMS
194 + " ledOffMS=" + notification.ledOffMS);
195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 @Override
198 public final String toString()
199 {
200 return "NotificationRecord{"
201 + Integer.toHexString(System.identityHashCode(this))
202 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700203 + " id=" + Integer.toHexString(id)
204 + " tag=" + tag + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 }
206 }
207
208 private static final class ToastRecord
209 {
210 final int pid;
211 final String pkg;
212 final ITransientNotification callback;
213 int duration;
214
215 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
216 {
217 this.pid = pid;
218 this.pkg = pkg;
219 this.callback = callback;
220 this.duration = duration;
221 }
222
223 void update(int duration) {
224 this.duration = duration;
225 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 void dump(PrintWriter pw, String prefix) {
228 pw.println(prefix + this);
229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 @Override
232 public final String toString()
233 {
234 return "ToastRecord{"
235 + Integer.toHexString(System.identityHashCode(this))
236 + " pkg=" + pkg
237 + " callback=" + callback
238 + " duration=" + duration;
239 }
240 }
241
Joe Onorato089de882010-04-12 08:18:45 -0700242 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
243 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244
245 public void onSetDisabled(int status) {
246 synchronized (mNotificationList) {
247 mDisabledNotifications = status;
248 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
249 // cancel whatever's going on
250 long identity = Binder.clearCallingIdentity();
251 try {
252 mSound.stop();
253 }
254 finally {
255 Binder.restoreCallingIdentity(identity);
256 }
257
258 identity = Binder.clearCallingIdentity();
259 try {
260 mVibrator.cancel();
261 }
262 finally {
263 Binder.restoreCallingIdentity(identity);
264 }
265 }
266 }
267 }
268
269 public void onClearAll() {
270 cancelAll();
271 }
272
Fred Quintana6ecaff12009-09-25 14:23:13 -0700273 public void onNotificationClick(String pkg, String tag, int id) {
274 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700275 Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 }
277
278 public void onPanelRevealed() {
279 synchronized (mNotificationList) {
280 // sound
281 mSoundNotification = null;
282 long identity = Binder.clearCallingIdentity();
283 try {
284 mSound.stop();
285 }
286 finally {
287 Binder.restoreCallingIdentity(identity);
288 }
289
290 // vibrate
291 mVibrateNotification = null;
292 identity = Binder.clearCallingIdentity();
293 try {
294 mVibrator.cancel();
295 }
296 finally {
297 Binder.restoreCallingIdentity(identity);
298 }
299
300 // light
301 mLights.clear();
302 mLedNotification = null;
303 updateLightsLocked();
304 }
305 }
306 };
307
308 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
309 @Override
310 public void onReceive(Context context, Intent intent) {
311 String action = intent.getAction();
312
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800313 boolean queryRestart = false;
314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
316 boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0);
317 int level = intent.getIntExtra("level", -1);
318 boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD);
319 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
320 boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90);
321
322 if (batteryCharging != mBatteryCharging ||
323 batteryLow != mBatteryLow ||
324 batteryFull != mBatteryFull) {
325 mBatteryCharging = batteryCharging;
326 mBatteryLow = batteryLow;
327 mBatteryFull = batteryFull;
328 updateLights();
329 }
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400330 } else if (action.equals(Intent.ACTION_UMS_CONNECTED)) {
331 mUsbConnected = true;
332 updateAdbNotification();
333 } else if (action.equals(Intent.ACTION_UMS_DISCONNECTED)) {
334 mUsbConnected = false;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700335 updateAdbNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800337 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800338 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800339 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800340 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800341 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800342 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800343 } else if (queryRestart) {
344 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800345 } else {
346 Uri uri = intent.getData();
347 if (uri == null) {
348 return;
349 }
350 String pkgName = uri.getSchemeSpecificPart();
351 if (pkgName == null) {
352 return;
353 }
354 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800356 if (pkgList != null && (pkgList.length > 0)) {
357 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800358 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500361 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
362 mScreenOn = true;
363 updateNotificationPulse();
364 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
365 mScreenOn = false;
366 updateNotificationPulse();
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500367 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
368 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK));
369 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 }
371 }
372 };
373
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700374 class SettingsObserver extends ContentObserver {
375 SettingsObserver(Handler handler) {
376 super(handler);
377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800378
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700379 void observe() {
380 ContentResolver resolver = mContext.getContentResolver();
381 resolver.registerContentObserver(Settings.Secure.getUriFor(
382 Settings.Secure.ADB_ENABLED), false, this);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500383 resolver.registerContentObserver(Settings.System.getUriFor(
384 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700385 update();
386 }
387
388 @Override public void onChange(boolean selfChange) {
389 update();
390 }
391
392 public void update() {
393 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500394 boolean adbEnabled = Settings.Secure.getInt(resolver,
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700395 Settings.Secure.ADB_ENABLED, 0) != 0;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500396 if (mAdbEnabled != adbEnabled) {
397 mAdbEnabled = adbEnabled;
398 updateAdbNotification();
399 }
400 boolean pulseEnabled = Settings.System.getInt(resolver,
401 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
402 if (mNotificationPulseEnabled != pulseEnabled) {
403 mNotificationPulseEnabled = pulseEnabled;
404 updateNotificationPulse();
405 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700406 }
407 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500408
Joe Onorato089de882010-04-12 08:18:45 -0700409 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500410 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 {
412 super();
413 mContext = context;
Mike Lockwood3a322132009-11-24 00:30:52 -0500414 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 mAm = ActivityManagerNative.getDefault();
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700416 mSound = new NotificationPlayer(TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 mSound.setUsesWakeLock(context);
418 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800420
Joe Onorato089de882010-04-12 08:18:45 -0700421 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 statusBar.setNotificationCallbacks(mNotificationCallbacks);
423
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500424 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
425 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
426 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
427
Mike Lockwood670f9322010-01-20 12:13:36 -0500428 Resources resources = mContext.getResources();
429 mDefaultNotificationColor = resources.getColor(
430 com.android.internal.R.color.config_defaultNotificationColor);
431 mDefaultNotificationLedOn = resources.getInteger(
432 com.android.internal.R.integer.config_defaultNotificationLedOn);
433 mDefaultNotificationLedOff = resources.getInteger(
434 com.android.internal.R.integer.config_defaultNotificationLedOff);
435
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400436 // Don't start allowing notifications until the setup wizard has run once.
437 // After that, including subsequent boots, init with notifications turned on.
438 // This works on the first boot because the setup wizard will toggle this
439 // flag at least once and we'll go back to 0 after that.
440 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
441 Settings.Secure.DEVICE_PROVISIONED, 0)) {
442 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
443 }
444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 // register for battery changed notifications
446 IntentFilter filter = new IntentFilter();
447 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400448 filter.addAction(Intent.ACTION_UMS_CONNECTED);
449 filter.addAction(Intent.ACTION_UMS_DISCONNECTED);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500450 filter.addAction(Intent.ACTION_SCREEN_ON);
451 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500452 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800454 IntentFilter pkgFilter = new IntentFilter();
455 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
456 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
457 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
458 pkgFilter.addDataScheme("package");
459 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800460 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800461 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800462
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500463 SettingsObserver observer = new SettingsObserver(mHandler);
464 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466
Joe Onorato30275482009-07-08 17:09:14 -0700467 void systemReady() {
468 // no beeping until we're basically done booting
469 mSystemReady = true;
470 }
471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 // Toasts
473 // ============================================================================
474 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
475 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400476 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477
478 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800479 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 return ;
481 }
482
483 synchronized (mToastQueue) {
484 int callingPid = Binder.getCallingPid();
485 long callingId = Binder.clearCallingIdentity();
486 try {
487 ToastRecord record;
488 int index = indexOfToastLocked(pkg, callback);
489 // If it's already in the queue, we update it in place, we don't
490 // move it to the end of the queue.
491 if (index >= 0) {
492 record = mToastQueue.get(index);
493 record.update(duration);
494 } else {
495 record = new ToastRecord(callingPid, pkg, callback, duration);
496 mToastQueue.add(record);
497 index = mToastQueue.size() - 1;
498 keepProcessAliveLocked(callingPid);
499 }
500 // If it's at index 0, it's the current toast. It doesn't matter if it's
501 // new or just been updated. Call back and tell it to show itself.
502 // If the callback fails, this will remove it from the list, so don't
503 // assume that it's valid after this.
504 if (index == 0) {
505 showNextToastLocked();
506 }
507 } finally {
508 Binder.restoreCallingIdentity(callingId);
509 }
510 }
511 }
512
513 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800514 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515
516 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800517 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 return ;
519 }
520
521 synchronized (mToastQueue) {
522 long callingId = Binder.clearCallingIdentity();
523 try {
524 int index = indexOfToastLocked(pkg, callback);
525 if (index >= 0) {
526 cancelToastLocked(index);
527 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800528 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 }
530 } finally {
531 Binder.restoreCallingIdentity(callingId);
532 }
533 }
534 }
535
536 private void showNextToastLocked() {
537 ToastRecord record = mToastQueue.get(0);
538 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800539 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 try {
541 record.callback.show();
542 scheduleTimeoutLocked(record, false);
543 return;
544 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800545 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 + " in package " + record.pkg);
547 // remove it from the list and let the process die
548 int index = mToastQueue.indexOf(record);
549 if (index >= 0) {
550 mToastQueue.remove(index);
551 }
552 keepProcessAliveLocked(record.pid);
553 if (mToastQueue.size() > 0) {
554 record = mToastQueue.get(0);
555 } else {
556 record = null;
557 }
558 }
559 }
560 }
561
562 private void cancelToastLocked(int index) {
563 ToastRecord record = mToastQueue.get(index);
564 try {
565 record.callback.hide();
566 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800567 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 + " in package " + record.pkg);
569 // don't worry about this, we're about to remove it from
570 // the list anyway
571 }
572 mToastQueue.remove(index);
573 keepProcessAliveLocked(record.pid);
574 if (mToastQueue.size() > 0) {
575 // Show the next one. If the callback fails, this will remove
576 // it from the list, so don't assume that the list hasn't changed
577 // after this point.
578 showNextToastLocked();
579 }
580 }
581
582 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
583 {
584 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
585 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
586 mHandler.removeCallbacksAndMessages(r);
587 mHandler.sendMessageDelayed(m, delay);
588 }
589
590 private void handleTimeout(ToastRecord record)
591 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800592 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 synchronized (mToastQueue) {
594 int index = indexOfToastLocked(record.pkg, record.callback);
595 if (index >= 0) {
596 cancelToastLocked(index);
597 }
598 }
599 }
600
601 // lock on mToastQueue
602 private int indexOfToastLocked(String pkg, ITransientNotification callback)
603 {
604 IBinder cbak = callback.asBinder();
605 ArrayList<ToastRecord> list = mToastQueue;
606 int len = list.size();
607 for (int i=0; i<len; i++) {
608 ToastRecord r = list.get(i);
609 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
610 return i;
611 }
612 }
613 return -1;
614 }
615
616 // lock on mToastQueue
617 private void keepProcessAliveLocked(int pid)
618 {
619 int toastCount = 0; // toasts from this pid
620 ArrayList<ToastRecord> list = mToastQueue;
621 int N = list.size();
622 for (int i=0; i<N; i++) {
623 ToastRecord r = list.get(i);
624 if (r.pid == pid) {
625 toastCount++;
626 }
627 }
628 try {
629 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
630 } catch (RemoteException e) {
631 // Shouldn't happen.
632 }
633 }
634
635 private final class WorkerHandler extends Handler
636 {
637 @Override
638 public void handleMessage(Message msg)
639 {
640 switch (msg.what)
641 {
642 case MESSAGE_TIMEOUT:
643 handleTimeout((ToastRecord)msg.obj);
644 break;
645 }
646 }
647 }
648
649
650 // Notifications
651 // ============================================================================
652 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
657 public void enqueueNotificationWithTag(String pkg, String tag, int id,
658 Notification notification, int[] idOut)
659 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700660 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800661
Joe Onoratobd73d012010-06-04 11:44:54 -0700662 // Limit the number of notifications that any given package except the android
663 // package can enqueue. Prevents DOS attacks and deals with leaks.
664 if (!"android".equals(pkg)) {
665 synchronized (mNotificationList) {
666 int count = 0;
667 final int N = mNotificationList.size();
668 for (int i=0; i<N; i++) {
669 final NotificationRecord r = mNotificationList.get(i);
670 if (r.pkg.equals(pkg)) {
671 count++;
672 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
673 Slog.e(TAG, "Package has already posted " + count
674 + " notifications. Not showing more. package=" + pkg);
675 return;
676 }
677 }
678 }
679 }
680 }
681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 // This conditional is a dirty hack to limit the logging done on
683 // behalf of the download manager without affecting other apps.
684 if (!pkg.equals("com.android.providers.downloads")
685 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800686 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
688
689 if (pkg == null || notification == null) {
690 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
691 + " id=" + id + " notification=" + notification);
692 }
693 if (notification.icon != 0) {
694 if (notification.contentView == null) {
695 throw new IllegalArgumentException("contentView required: pkg=" + pkg
696 + " id=" + id + " notification=" + notification);
697 }
698 if (notification.contentIntent == null) {
699 throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
700 + " id=" + id + " notification=" + notification);
701 }
702 }
703
704 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700705 NotificationRecord r = new NotificationRecord(pkg, tag, id, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 NotificationRecord old = null;
707
Fred Quintana6ecaff12009-09-25 14:23:13 -0700708 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 if (index < 0) {
710 mNotificationList.add(r);
711 } else {
712 old = mNotificationList.remove(index);
713 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700714 // Make sure we don't lose the foreground service state.
715 if (old != null) {
716 notification.flags |=
717 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800720
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700721 // Ensure if this is a foreground service that the proper additional
722 // flags are set.
723 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
724 notification.flags |= Notification.FLAG_ONGOING_EVENT
725 | Notification.FLAG_NO_CLEAR;
726 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 if (notification.icon != 0) {
Joe Onorato18e69df2010-05-17 22:26:12 -0700729 StatusBarNotification n = new StatusBarNotification(pkg, id, tag, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 if (old != null && old.statusBarKey != null) {
731 r.statusBarKey = old.statusBarKey;
732 long identity = Binder.clearCallingIdentity();
733 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700734 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
736 finally {
737 Binder.restoreCallingIdentity(identity);
738 }
739 } else {
740 long identity = Binder.clearCallingIdentity();
741 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700742 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500743 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
745 finally {
746 Binder.restoreCallingIdentity(identity);
747 }
748 }
Joe Onorato30275482009-07-08 17:09:14 -0700749 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 } else {
751 if (old != null && old.statusBarKey != null) {
752 long identity = Binder.clearCallingIdentity();
753 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700754 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756 finally {
757 Binder.restoreCallingIdentity(identity);
758 }
759 }
760 }
761
762 // If we're not supposed to beep, vibrate, etc. then don't.
763 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
764 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700765 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
766 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800767
768 final AudioManager audioManager = (AudioManager) mContext
769 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 // sound
771 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800772 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 if (useDefaultSound || notification.sound != null) {
774 Uri uri;
775 if (useDefaultSound) {
776 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
777 } else {
778 uri = notification.sound;
779 }
780 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
781 int audioStreamType;
782 if (notification.audioStreamType >= 0) {
783 audioStreamType = notification.audioStreamType;
784 } else {
785 audioStreamType = DEFAULT_STREAM_TYPE;
786 }
787 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800788 // do not play notifications if stream volume is 0
789 // (typically because ringer mode is silent).
790 if (audioManager.getStreamVolume(audioStreamType) != 0) {
791 long identity = Binder.clearCallingIdentity();
792 try {
793 mSound.play(mContext, uri, looping, audioStreamType);
794 }
795 finally {
796 Binder.restoreCallingIdentity(identity);
797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
799 }
800
801 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800803 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 if ((useDefaultVibrate || notification.vibrate != null)
805 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
806 mVibrateNotification = r;
807
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800808 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 : notification.vibrate,
810 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
811 }
812 }
813
814 // this option doesn't shut off the lights
815
816 // light
817 // the most recent thing gets the light
818 mLights.remove(old);
819 if (mLedNotification == old) {
820 mLedNotification = null;
821 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800822 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
824 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
825 mLights.add(r);
826 updateLightsLocked();
827 } else {
828 if (old != null
829 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
830 updateLightsLocked();
831 }
832 }
833 }
834
835 idOut[0] = id;
836 }
837
Joe Onorato30275482009-07-08 17:09:14 -0700838 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700839 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
840 if (!manager.isEnabled()) {
841 return;
842 }
843
844 AccessibilityEvent event =
845 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
846 event.setPackageName(packageName);
847 event.setClassName(Notification.class.getName());
848 event.setParcelableData(notification);
849 CharSequence tickerText = notification.tickerText;
850 if (!TextUtils.isEmpty(tickerText)) {
851 event.getText().add(tickerText);
852 }
853
854 manager.sendAccessibilityEvent(event);
855 }
856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 private void cancelNotificationLocked(NotificationRecord r) {
858 // status bar
859 if (r.notification.icon != 0) {
860 long identity = Binder.clearCallingIdentity();
861 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700862 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
864 finally {
865 Binder.restoreCallingIdentity(identity);
866 }
867 r.statusBarKey = null;
868 }
869
870 // sound
871 if (mSoundNotification == r) {
872 mSoundNotification = null;
873 long identity = Binder.clearCallingIdentity();
874 try {
875 mSound.stop();
876 }
877 finally {
878 Binder.restoreCallingIdentity(identity);
879 }
880 }
881
882 // vibrate
883 if (mVibrateNotification == r) {
884 mVibrateNotification = null;
885 long identity = Binder.clearCallingIdentity();
886 try {
887 mVibrator.cancel();
888 }
889 finally {
890 Binder.restoreCallingIdentity(identity);
891 }
892 }
893
894 // light
895 mLights.remove(r);
896 if (mLedNotification == r) {
897 mLedNotification = null;
898 }
899 }
900
901 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700902 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800903 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700905 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700906 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800907 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908
909 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700910 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700912 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
915 return;
916 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700917 if ((r.notification.flags & mustNotHaveFlags) != 0) {
918 return;
919 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 mNotificationList.remove(index);
922
923 cancelNotificationLocked(r);
924 updateLightsLocked();
925 }
926 }
927 }
928
929 /**
930 * Cancels all notifications from a given package that have all of the
931 * {@code mustHaveFlags}.
932 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800933 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
934 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800935 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936
937 synchronized (mNotificationList) {
938 final int N = mNotificationList.size();
939 boolean canceledSomething = false;
940 for (int i = N-1; i >= 0; --i) {
941 NotificationRecord r = mNotificationList.get(i);
942 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
943 continue;
944 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700945 if ((r.notification.flags & mustNotHaveFlags) != 0) {
946 continue;
947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 if (!r.pkg.equals(pkg)) {
949 continue;
950 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800951 canceledSomething = true;
952 if (!doit) {
953 return true;
954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 mNotificationList.remove(i);
956 cancelNotificationLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 }
958 if (canceledSomething) {
959 updateLightsLocked();
960 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800961 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
963 }
964
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800965
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700966 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700967 cancelNotificationWithTag(pkg, null /* tag */, id);
968 }
969
970 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700971 checkIncomingCall(pkg);
972 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700973 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700974 Binder.getCallingUid() == Process.SYSTEM_UID
975 ? 0 : Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 }
977
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700978 public void cancelAllNotifications(String pkg) {
979 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800980
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700981 // Calling from user space, don't allow the canceling of actively
982 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800983 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
985
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700986 void checkIncomingCall(String pkg) {
987 int uid = Binder.getCallingUid();
988 if (uid == Process.SYSTEM_UID || uid == 0) {
989 return;
990 }
991 try {
992 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
993 pkg, 0);
994 if (ai.uid != uid) {
995 throw new SecurityException("Calling uid " + uid + " gave package"
996 + pkg + " which is owned by uid " + ai.uid);
997 }
998 } catch (PackageManager.NameNotFoundException e) {
999 throw new SecurityException("Unknown package " + pkg);
1000 }
1001 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001002
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001003 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 synchronized (mNotificationList) {
1005 final int N = mNotificationList.size();
1006 for (int i=N-1; i>=0; i--) {
1007 NotificationRecord r = mNotificationList.get(i);
1008
1009 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1010 | Notification.FLAG_NO_CLEAR)) == 0) {
1011 if (r.notification.deleteIntent != null) {
1012 try {
1013 r.notification.deleteIntent.send();
1014 } catch (PendingIntent.CanceledException ex) {
1015 // do nothing - there's no relevant way to recover, and
1016 // no reason to let this propagate
Joe Onorato8a9b2202010-02-26 18:56:32 -08001017 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 }
1019 }
1020 mNotificationList.remove(i);
1021 cancelNotificationLocked(r);
1022 }
1023 }
1024
1025 updateLightsLocked();
1026 }
1027 }
1028
1029 private void updateLights() {
1030 synchronized (mNotificationList) {
1031 updateLightsLocked();
1032 }
1033 }
1034
1035 // lock on mNotificationList
1036 private void updateLightsLocked()
1037 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001038 // Battery low always shows, other states only show if charging.
1039 if (mBatteryLow) {
Mike Lockwood445f4302009-09-04 11:06:46 -04001040 if (mBatteryCharging) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001041 mBatteryLight.setColor(BATTERY_LOW_ARGB);
Mike Lockwood445f4302009-09-04 11:06:46 -04001042 } else {
1043 // Flash when battery is low and not charging
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001044 mBatteryLight.setFlashing(BATTERY_LOW_ARGB, LightsService.LIGHT_FLASH_TIMED,
1045 BATTERY_BLINK_ON, BATTERY_BLINK_OFF);
Mike Lockwood445f4302009-09-04 11:06:46 -04001046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001048 if (mBatteryFull) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001049 mBatteryLight.setColor(BATTERY_FULL_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001051 mBatteryLight.setColor(BATTERY_MEDIUM_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 }
1053 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001054 mBatteryLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056
The Android Open Source Project10592532009-03-18 17:39:46 -07001057 // handle notification lights
1058 if (mLedNotification == null) {
1059 // get next notification, if any
1060 int n = mLights.size();
1061 if (n > 0) {
1062 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
1064 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001065
1066 // we only flash if screen is off and persistent pulsing is enabled
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001067 // and we are not currently in a call
1068 if (mLedNotification == null || mScreenOn || mInCall) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001069 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001070 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001071 int ledARGB = mLedNotification.notification.ledARGB;
1072 int ledOnMS = mLedNotification.notification.ledOnMS;
1073 int ledOffMS = mLedNotification.notification.ledOffMS;
1074 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1075 ledARGB = mDefaultNotificationColor;
1076 ledOnMS = mDefaultNotificationLedOn;
1077 ledOffMS = mDefaultNotificationLedOff;
1078 }
1079 if (mNotificationPulseEnabled) {
1080 // pulse repeatedly
1081 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1082 ledOnMS, ledOffMS);
1083 } else {
1084 // pulse only once
1085 mNotificationLight.pulse(ledARGB, ledOnMS);
1086 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 }
1089
1090 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001091 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 {
1093 ArrayList<NotificationRecord> list = mNotificationList;
1094 final int len = list.size();
1095 for (int i=0; i<len; i++) {
1096 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001097 if (tag == null) {
1098 if (r.tag != null) {
1099 continue;
1100 }
1101 } else {
1102 if (!tag.equals(r.tag)) {
1103 continue;
1104 }
1105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 if (r.id == id && r.pkg.equals(pkg)) {
1107 return i;
1108 }
1109 }
1110 return -1;
1111 }
1112
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001113 // This is here instead of StatusBarPolicy because it is an important
1114 // security feature that we don't want people customizing the platform
1115 // to accidentally lose.
1116 private void updateAdbNotification() {
Mike Lockwoodea8b7d52009-08-04 17:03:15 -04001117 if (mAdbEnabled && mUsbConnected) {
Mike Lockwooded760372009-07-09 07:07:27 -04001118 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1119 return;
1120 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001121 if (!mAdbNotificationShown) {
1122 NotificationManager notificationManager = (NotificationManager) mContext
1123 .getSystemService(Context.NOTIFICATION_SERVICE);
1124 if (notificationManager != null) {
1125 Resources r = mContext.getResources();
1126 CharSequence title = r.getText(
1127 com.android.internal.R.string.adb_active_notification_title);
1128 CharSequence message = r.getText(
1129 com.android.internal.R.string.adb_active_notification_message);
1130
1131 if (mAdbNotification == null) {
1132 mAdbNotification = new Notification();
Daniel Sandler39576c82010-03-25 16:02:33 -04001133 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001134 mAdbNotification.when = 0;
1135 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1136 mAdbNotification.tickerText = title;
Daniel Sandler39576c82010-03-25 16:02:33 -04001137 mAdbNotification.defaults = 0; // please be quiet
1138 mAdbNotification.sound = null;
1139 mAdbNotification.vibrate = null;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001140 }
1141
1142 Intent intent = new Intent(
1143 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1144 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1145 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1146 // Note: we are hard-coding the component because this is
1147 // an important security UI that we don't want anyone
1148 // intercepting.
1149 intent.setComponent(new ComponentName("com.android.settings",
1150 "com.android.settings.DevelopmentSettings"));
1151 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1152 intent, 0);
1153
1154 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001155
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001156 mAdbNotificationShown = true;
1157 notificationManager.notify(
1158 com.android.internal.R.string.adb_active_notification_title,
1159 mAdbNotification);
1160 }
1161 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001162
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001163 } else if (mAdbNotificationShown) {
1164 NotificationManager notificationManager = (NotificationManager) mContext
1165 .getSystemService(Context.NOTIFICATION_SERVICE);
1166 if (notificationManager != null) {
1167 mAdbNotificationShown = false;
1168 notificationManager.cancel(
1169 com.android.internal.R.string.adb_active_notification_title);
1170 }
1171 }
1172 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001173
1174 private void updateNotificationPulse() {
1175 synchronized (mNotificationList) {
1176 updateLightsLocked();
1177 }
1178 }
1179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 // ======================================================================
1181 @Override
1182 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1183 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1184 != PackageManager.PERMISSION_GRANTED) {
1185 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1186 + Binder.getCallingPid()
1187 + ", uid=" + Binder.getCallingUid());
1188 return;
1189 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 pw.println("Current Notification Manager state:");
1192
1193 int N;
1194
1195 synchronized (mToastQueue) {
1196 N = mToastQueue.size();
1197 if (N > 0) {
1198 pw.println(" Toast Queue:");
1199 for (int i=0; i<N; i++) {
1200 mToastQueue.get(i).dump(pw, " ");
1201 }
1202 pw.println(" ");
1203 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
1206
1207 synchronized (mNotificationList) {
1208 N = mNotificationList.size();
1209 if (N > 0) {
1210 pw.println(" Notification List:");
1211 for (int i=0; i<N; i++) {
1212 mNotificationList.get(i).dump(pw, " ", mContext);
1213 }
1214 pw.println(" ");
1215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 N = mLights.size();
1218 if (N > 0) {
1219 pw.println(" Lights List:");
1220 for (int i=0; i<N; i++) {
1221 mLights.get(i).dump(pw, " ", mContext);
1222 }
1223 pw.println(" ");
1224 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 pw.println(" mSoundNotification=" + mSoundNotification);
1227 pw.println(" mSound=" + mSound);
1228 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001229 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1230 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 }
1232 }
1233}