blob: 25de8b0c49b821b1c74e7b696b6e2a3c087e39fe [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;
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import java.io.FileDescriptor;
66import java.io.PrintWriter;
67import java.util.ArrayList;
68import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
70class NotificationManagerService extends INotificationManager.Stub
71{
72 private static final String TAG = "NotificationService";
73 private static final boolean DBG = false;
74
Joe Onoratobd73d012010-06-04 11:44:54 -070075 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 // message codes
78 private static final int MESSAGE_TIMEOUT = 2;
79
80 private static final int LONG_DELAY = 3500; // 3.5 seconds
81 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -080082
83 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
85 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
86
87 final Context mContext;
88 final IActivityManager mAm;
89 final IBinder mForegroundToken = new Binder();
90
91 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -070092 private StatusBarManagerService mStatusBar;
Mike Lockwood3a322132009-11-24 00:30:52 -050093 private LightsService mLightsService;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050094 private LightsService.Light mBatteryLight;
95 private LightsService.Light mNotificationLight;
96 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
Mike Lockwood670f9322010-01-20 12:13:36 -050098 private int mDefaultNotificationColor;
99 private int mDefaultNotificationLedOn;
100 private int mDefaultNotificationLedOff;
101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 private NotificationRecord mSoundNotification;
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700103 private NotificationPlayer mSound;
Joe Onorato30275482009-07-08 17:09:14 -0700104 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400105 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
107 private NotificationRecord mVibrateNotification;
108 private Vibrator mVibrator = new Vibrator();
109
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500110 // for enabling and disabling notification pulse behavior
111 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500112 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500113 private boolean mNotificationPulseEnabled;
114
115 // for adb connected notifications
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700116 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;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700167 final int uid;
168 final int initialPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 ITransientNotification callback;
170 int duration;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700171 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 IBinder statusBarKey;
173
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700174 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid,
175 Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 {
177 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700178 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700180 this.uid = uid;
181 this.initialPid = initialPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 this.notification = notification;
183 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 void dump(PrintWriter pw, String prefix, Context baseContext) {
186 pw.println(prefix + this);
187 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
188 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
189 pw.println(prefix + " contentIntent=" + notification.contentIntent);
190 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
191 pw.println(prefix + " tickerText=" + notification.tickerText);
192 pw.println(prefix + " contentView=" + notification.contentView);
193 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
194 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
195 pw.println(prefix + " sound=" + notification.sound);
196 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
197 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
198 + " ledOnMS=" + notification.ledOnMS
199 + " ledOffMS=" + notification.ledOffMS);
200 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 @Override
203 public final String toString()
204 {
205 return "NotificationRecord{"
206 + Integer.toHexString(System.identityHashCode(this))
207 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700208 + " id=" + Integer.toHexString(id)
209 + " tag=" + tag + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 }
211 }
212
213 private static final class ToastRecord
214 {
215 final int pid;
216 final String pkg;
217 final ITransientNotification callback;
218 int duration;
219
220 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
221 {
222 this.pid = pid;
223 this.pkg = pkg;
224 this.callback = callback;
225 this.duration = duration;
226 }
227
228 void update(int duration) {
229 this.duration = duration;
230 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 void dump(PrintWriter pw, String prefix) {
233 pw.println(prefix + this);
234 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 @Override
237 public final String toString()
238 {
239 return "ToastRecord{"
240 + Integer.toHexString(System.identityHashCode(this))
241 + " pkg=" + pkg
242 + " callback=" + callback
243 + " duration=" + duration;
244 }
245 }
246
Joe Onorato089de882010-04-12 08:18:45 -0700247 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
248 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249
250 public void onSetDisabled(int status) {
251 synchronized (mNotificationList) {
252 mDisabledNotifications = status;
253 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
254 // cancel whatever's going on
255 long identity = Binder.clearCallingIdentity();
256 try {
257 mSound.stop();
258 }
259 finally {
260 Binder.restoreCallingIdentity(identity);
261 }
262
263 identity = Binder.clearCallingIdentity();
264 try {
265 mVibrator.cancel();
266 }
267 finally {
268 Binder.restoreCallingIdentity(identity);
269 }
270 }
271 }
272 }
273
274 public void onClearAll() {
275 cancelAll();
276 }
277
Fred Quintana6ecaff12009-09-25 14:23:13 -0700278 public void onNotificationClick(String pkg, String tag, int id) {
279 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700280 Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 }
282
283 public void onPanelRevealed() {
284 synchronized (mNotificationList) {
285 // sound
286 mSoundNotification = null;
287 long identity = Binder.clearCallingIdentity();
288 try {
289 mSound.stop();
290 }
291 finally {
292 Binder.restoreCallingIdentity(identity);
293 }
294
295 // vibrate
296 mVibrateNotification = null;
297 identity = Binder.clearCallingIdentity();
298 try {
299 mVibrator.cancel();
300 }
301 finally {
302 Binder.restoreCallingIdentity(identity);
303 }
304
305 // light
306 mLights.clear();
307 mLedNotification = null;
308 updateLightsLocked();
309 }
310 }
Joe Onorato005847b2010-06-04 16:08:02 -0400311
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700312 public void onNotificationError(String pkg, String tag, int id,
313 int uid, int initialPid, String message) {
Joe Onorato005847b2010-06-04 16:08:02 -0400314 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id);
315 cancelNotification(pkg, tag, id, 0, 0);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700316 long ident = Binder.clearCallingIdentity();
317 try {
318 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
319 "Bad notification posted from package " + pkg
320 + ": " + message);
321 } catch (RemoteException e) {
322 }
323 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 };
326
327 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
328 @Override
329 public void onReceive(Context context, Intent intent) {
330 String action = intent.getAction();
331
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800332 boolean queryRestart = false;
333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
335 boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0);
336 int level = intent.getIntExtra("level", -1);
337 boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD);
338 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
339 boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90);
340
341 if (batteryCharging != mBatteryCharging ||
342 batteryLow != mBatteryLow ||
343 batteryFull != mBatteryFull) {
344 mBatteryCharging = batteryCharging;
345 mBatteryLow = batteryLow;
346 mBatteryFull = batteryFull;
347 updateLights();
348 }
Mike Lockwoodff2544c2010-06-28 09:17:50 -0400349 } else if (action.equals(Usb.ACTION_USB_STATE)) {
350 Bundle extras = intent.getExtras();
351 boolean usbConnected = extras.getBoolean(Usb.USB_CONNECTED);
352 boolean adbEnabled = (Usb.USB_FUNCTION_ENABLED.equals(
353 extras.getString(Usb.USB_FUNCTION_ADB)));
354 updateAdbNotification(usbConnected && adbEnabled);
355 } else if (action.equals(Usb.ACTION_USB_DISCONNECTED)) {
356 updateAdbNotification(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800358 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800359 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800360 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800361 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800362 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800363 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800364 } else if (queryRestart) {
365 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800366 } else {
367 Uri uri = intent.getData();
368 if (uri == null) {
369 return;
370 }
371 String pkgName = uri.getSchemeSpecificPart();
372 if (pkgName == null) {
373 return;
374 }
375 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800377 if (pkgList != null && (pkgList.length > 0)) {
378 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800379 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500382 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
383 mScreenOn = true;
384 updateNotificationPulse();
385 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
386 mScreenOn = false;
387 updateNotificationPulse();
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500388 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
389 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK));
390 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 }
392 }
393 };
394
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700395 class SettingsObserver extends ContentObserver {
396 SettingsObserver(Handler handler) {
397 super(handler);
398 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800399
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700400 void observe() {
401 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500402 resolver.registerContentObserver(Settings.System.getUriFor(
403 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700404 update();
405 }
406
407 @Override public void onChange(boolean selfChange) {
408 update();
409 }
410
411 public void update() {
412 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500413 boolean pulseEnabled = Settings.System.getInt(resolver,
414 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
415 if (mNotificationPulseEnabled != pulseEnabled) {
416 mNotificationPulseEnabled = pulseEnabled;
417 updateNotificationPulse();
418 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700419 }
420 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500421
Joe Onorato089de882010-04-12 08:18:45 -0700422 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500423 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 {
425 super();
426 mContext = context;
Mike Lockwood3a322132009-11-24 00:30:52 -0500427 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 mAm = ActivityManagerNative.getDefault();
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700429 mSound = new NotificationPlayer(TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 mSound.setUsesWakeLock(context);
431 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800433
Joe Onorato089de882010-04-12 08:18:45 -0700434 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 statusBar.setNotificationCallbacks(mNotificationCallbacks);
436
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500437 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
438 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
439 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
440
Mike Lockwood670f9322010-01-20 12:13:36 -0500441 Resources resources = mContext.getResources();
442 mDefaultNotificationColor = resources.getColor(
443 com.android.internal.R.color.config_defaultNotificationColor);
444 mDefaultNotificationLedOn = resources.getInteger(
445 com.android.internal.R.integer.config_defaultNotificationLedOn);
446 mDefaultNotificationLedOff = resources.getInteger(
447 com.android.internal.R.integer.config_defaultNotificationLedOff);
448
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400449 // Don't start allowing notifications until the setup wizard has run once.
450 // After that, including subsequent boots, init with notifications turned on.
451 // This works on the first boot because the setup wizard will toggle this
452 // flag at least once and we'll go back to 0 after that.
453 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
454 Settings.Secure.DEVICE_PROVISIONED, 0)) {
455 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
456 }
457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 // register for battery changed notifications
459 IntentFilter filter = new IntentFilter();
460 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Mike Lockwoodff2544c2010-06-28 09:17:50 -0400461 filter.addAction(Usb.ACTION_USB_STATE);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500462 filter.addAction(Intent.ACTION_SCREEN_ON);
463 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500464 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800466 IntentFilter pkgFilter = new IntentFilter();
467 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
468 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
469 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
470 pkgFilter.addDataScheme("package");
471 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800472 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800473 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800474
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500475 SettingsObserver observer = new SettingsObserver(mHandler);
476 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 }
478
Joe Onorato30275482009-07-08 17:09:14 -0700479 void systemReady() {
480 // no beeping until we're basically done booting
481 mSystemReady = true;
482 }
483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 // Toasts
485 // ============================================================================
486 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
487 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400488 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489
490 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800491 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 return ;
493 }
494
495 synchronized (mToastQueue) {
496 int callingPid = Binder.getCallingPid();
497 long callingId = Binder.clearCallingIdentity();
498 try {
499 ToastRecord record;
500 int index = indexOfToastLocked(pkg, callback);
501 // If it's already in the queue, we update it in place, we don't
502 // move it to the end of the queue.
503 if (index >= 0) {
504 record = mToastQueue.get(index);
505 record.update(duration);
506 } else {
507 record = new ToastRecord(callingPid, pkg, callback, duration);
508 mToastQueue.add(record);
509 index = mToastQueue.size() - 1;
510 keepProcessAliveLocked(callingPid);
511 }
512 // If it's at index 0, it's the current toast. It doesn't matter if it's
513 // new or just been updated. Call back and tell it to show itself.
514 // If the callback fails, this will remove it from the list, so don't
515 // assume that it's valid after this.
516 if (index == 0) {
517 showNextToastLocked();
518 }
519 } finally {
520 Binder.restoreCallingIdentity(callingId);
521 }
522 }
523 }
524
525 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800526 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527
528 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800529 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 return ;
531 }
532
533 synchronized (mToastQueue) {
534 long callingId = Binder.clearCallingIdentity();
535 try {
536 int index = indexOfToastLocked(pkg, callback);
537 if (index >= 0) {
538 cancelToastLocked(index);
539 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800540 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 }
542 } finally {
543 Binder.restoreCallingIdentity(callingId);
544 }
545 }
546 }
547
548 private void showNextToastLocked() {
549 ToastRecord record = mToastQueue.get(0);
550 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800551 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 try {
553 record.callback.show();
554 scheduleTimeoutLocked(record, false);
555 return;
556 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800557 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 + " in package " + record.pkg);
559 // remove it from the list and let the process die
560 int index = mToastQueue.indexOf(record);
561 if (index >= 0) {
562 mToastQueue.remove(index);
563 }
564 keepProcessAliveLocked(record.pid);
565 if (mToastQueue.size() > 0) {
566 record = mToastQueue.get(0);
567 } else {
568 record = null;
569 }
570 }
571 }
572 }
573
574 private void cancelToastLocked(int index) {
575 ToastRecord record = mToastQueue.get(index);
576 try {
577 record.callback.hide();
578 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800579 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 + " in package " + record.pkg);
581 // don't worry about this, we're about to remove it from
582 // the list anyway
583 }
584 mToastQueue.remove(index);
585 keepProcessAliveLocked(record.pid);
586 if (mToastQueue.size() > 0) {
587 // Show the next one. If the callback fails, this will remove
588 // it from the list, so don't assume that the list hasn't changed
589 // after this point.
590 showNextToastLocked();
591 }
592 }
593
594 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
595 {
596 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
597 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
598 mHandler.removeCallbacksAndMessages(r);
599 mHandler.sendMessageDelayed(m, delay);
600 }
601
602 private void handleTimeout(ToastRecord record)
603 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800604 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 synchronized (mToastQueue) {
606 int index = indexOfToastLocked(record.pkg, record.callback);
607 if (index >= 0) {
608 cancelToastLocked(index);
609 }
610 }
611 }
612
613 // lock on mToastQueue
614 private int indexOfToastLocked(String pkg, ITransientNotification callback)
615 {
616 IBinder cbak = callback.asBinder();
617 ArrayList<ToastRecord> list = mToastQueue;
618 int len = list.size();
619 for (int i=0; i<len; i++) {
620 ToastRecord r = list.get(i);
621 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
622 return i;
623 }
624 }
625 return -1;
626 }
627
628 // lock on mToastQueue
629 private void keepProcessAliveLocked(int pid)
630 {
631 int toastCount = 0; // toasts from this pid
632 ArrayList<ToastRecord> list = mToastQueue;
633 int N = list.size();
634 for (int i=0; i<N; i++) {
635 ToastRecord r = list.get(i);
636 if (r.pid == pid) {
637 toastCount++;
638 }
639 }
640 try {
641 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
642 } catch (RemoteException e) {
643 // Shouldn't happen.
644 }
645 }
646
647 private final class WorkerHandler extends Handler
648 {
649 @Override
650 public void handleMessage(Message msg)
651 {
652 switch (msg.what)
653 {
654 case MESSAGE_TIMEOUT:
655 handleTimeout((ToastRecord)msg.obj);
656 break;
657 }
658 }
659 }
660
661
662 // Notifications
663 // ============================================================================
664 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
665 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700666 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
667 }
668
669 public void enqueueNotificationWithTag(String pkg, String tag, int id,
670 Notification notification, int[] idOut)
671 {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700672 final int callingUid = Binder.getCallingUid();
673 final int callingPid = Binder.getCallingPid();
674
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700675 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800676
Joe Onoratobd73d012010-06-04 11:44:54 -0700677 // Limit the number of notifications that any given package except the android
678 // package can enqueue. Prevents DOS attacks and deals with leaks.
679 if (!"android".equals(pkg)) {
680 synchronized (mNotificationList) {
681 int count = 0;
682 final int N = mNotificationList.size();
683 for (int i=0; i<N; i++) {
684 final NotificationRecord r = mNotificationList.get(i);
685 if (r.pkg.equals(pkg)) {
686 count++;
687 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
688 Slog.e(TAG, "Package has already posted " + count
689 + " notifications. Not showing more. package=" + pkg);
690 return;
691 }
692 }
693 }
694 }
695 }
696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 // This conditional is a dirty hack to limit the logging done on
698 // behalf of the download manager without affecting other apps.
699 if (!pkg.equals("com.android.providers.downloads")
700 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800701 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
703
704 if (pkg == null || notification == null) {
705 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
706 + " id=" + id + " notification=" + notification);
707 }
708 if (notification.icon != 0) {
709 if (notification.contentView == null) {
710 throw new IllegalArgumentException("contentView required: pkg=" + pkg
711 + " id=" + id + " notification=" + notification);
712 }
713 if (notification.contentIntent == null) {
714 throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
715 + " id=" + id + " notification=" + notification);
716 }
717 }
718
719 synchronized (mNotificationList) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700720 NotificationRecord r = new NotificationRecord(pkg, tag, id,
721 callingUid, callingPid, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 NotificationRecord old = null;
723
Fred Quintana6ecaff12009-09-25 14:23:13 -0700724 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 if (index < 0) {
726 mNotificationList.add(r);
727 } else {
728 old = mNotificationList.remove(index);
729 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700730 // Make sure we don't lose the foreground service state.
731 if (old != null) {
732 notification.flags |=
733 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
734 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800736
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700737 // Ensure if this is a foreground service that the proper additional
738 // flags are set.
739 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
740 notification.flags |= Notification.FLAG_ONGOING_EVENT
741 | Notification.FLAG_NO_CLEAR;
742 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 if (notification.icon != 0) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700745 StatusBarNotification n = new StatusBarNotification(pkg, id, tag,
746 r.uid, r.initialPid, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 if (old != null && old.statusBarKey != null) {
748 r.statusBarKey = old.statusBarKey;
749 long identity = Binder.clearCallingIdentity();
750 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700751 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 }
753 finally {
754 Binder.restoreCallingIdentity(identity);
755 }
756 } else {
757 long identity = Binder.clearCallingIdentity();
758 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700759 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500760 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 }
762 finally {
763 Binder.restoreCallingIdentity(identity);
764 }
765 }
Joe Onorato30275482009-07-08 17:09:14 -0700766 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 } else {
768 if (old != null && old.statusBarKey != null) {
769 long identity = Binder.clearCallingIdentity();
770 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700771 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
773 finally {
774 Binder.restoreCallingIdentity(identity);
775 }
776 }
777 }
778
779 // If we're not supposed to beep, vibrate, etc. then don't.
780 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
781 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700782 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
783 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800784
785 final AudioManager audioManager = (AudioManager) mContext
786 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 // sound
788 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800789 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 if (useDefaultSound || notification.sound != null) {
791 Uri uri;
792 if (useDefaultSound) {
793 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
794 } else {
795 uri = notification.sound;
796 }
797 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
798 int audioStreamType;
799 if (notification.audioStreamType >= 0) {
800 audioStreamType = notification.audioStreamType;
801 } else {
802 audioStreamType = DEFAULT_STREAM_TYPE;
803 }
804 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800805 // do not play notifications if stream volume is 0
806 // (typically because ringer mode is silent).
807 if (audioManager.getStreamVolume(audioStreamType) != 0) {
808 long identity = Binder.clearCallingIdentity();
809 try {
810 mSound.play(mContext, uri, looping, audioStreamType);
811 }
812 finally {
813 Binder.restoreCallingIdentity(identity);
814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 }
816 }
817
818 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800820 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 if ((useDefaultVibrate || notification.vibrate != null)
822 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
823 mVibrateNotification = r;
824
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800825 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 : notification.vibrate,
827 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
828 }
829 }
830
831 // this option doesn't shut off the lights
832
833 // light
834 // the most recent thing gets the light
835 mLights.remove(old);
836 if (mLedNotification == old) {
837 mLedNotification = null;
838 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800839 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
841 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
842 mLights.add(r);
843 updateLightsLocked();
844 } else {
845 if (old != null
846 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
847 updateLightsLocked();
848 }
849 }
850 }
851
852 idOut[0] = id;
853 }
854
Joe Onorato30275482009-07-08 17:09:14 -0700855 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700856 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
857 if (!manager.isEnabled()) {
858 return;
859 }
860
861 AccessibilityEvent event =
862 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
863 event.setPackageName(packageName);
864 event.setClassName(Notification.class.getName());
865 event.setParcelableData(notification);
866 CharSequence tickerText = notification.tickerText;
867 if (!TextUtils.isEmpty(tickerText)) {
868 event.getText().add(tickerText);
869 }
870
871 manager.sendAccessibilityEvent(event);
872 }
873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 private void cancelNotificationLocked(NotificationRecord r) {
875 // status bar
876 if (r.notification.icon != 0) {
877 long identity = Binder.clearCallingIdentity();
878 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700879 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 }
881 finally {
882 Binder.restoreCallingIdentity(identity);
883 }
884 r.statusBarKey = null;
885 }
886
887 // sound
888 if (mSoundNotification == r) {
889 mSoundNotification = null;
890 long identity = Binder.clearCallingIdentity();
891 try {
892 mSound.stop();
893 }
894 finally {
895 Binder.restoreCallingIdentity(identity);
896 }
897 }
898
899 // vibrate
900 if (mVibrateNotification == r) {
901 mVibrateNotification = null;
902 long identity = Binder.clearCallingIdentity();
903 try {
904 mVibrator.cancel();
905 }
906 finally {
907 Binder.restoreCallingIdentity(identity);
908 }
909 }
910
911 // light
912 mLights.remove(r);
913 if (mLedNotification == r) {
914 mLedNotification = null;
915 }
916 }
917
918 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700919 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800920 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700922 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700923 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800924 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925
926 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700927 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700929 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
932 return;
933 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700934 if ((r.notification.flags & mustNotHaveFlags) != 0) {
935 return;
936 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 mNotificationList.remove(index);
939
940 cancelNotificationLocked(r);
941 updateLightsLocked();
942 }
943 }
944 }
945
946 /**
947 * Cancels all notifications from a given package that have all of the
948 * {@code mustHaveFlags}.
949 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800950 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
951 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800952 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953
954 synchronized (mNotificationList) {
955 final int N = mNotificationList.size();
956 boolean canceledSomething = false;
957 for (int i = N-1; i >= 0; --i) {
958 NotificationRecord r = mNotificationList.get(i);
959 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
960 continue;
961 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700962 if ((r.notification.flags & mustNotHaveFlags) != 0) {
963 continue;
964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 if (!r.pkg.equals(pkg)) {
966 continue;
967 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800968 canceledSomething = true;
969 if (!doit) {
970 return true;
971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 mNotificationList.remove(i);
973 cancelNotificationLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 }
975 if (canceledSomething) {
976 updateLightsLocked();
977 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800978 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 }
980 }
981
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800982
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700983 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700984 cancelNotificationWithTag(pkg, null /* tag */, id);
985 }
986
987 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700988 checkIncomingCall(pkg);
989 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700990 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700991 Binder.getCallingUid() == Process.SYSTEM_UID
992 ? 0 : Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 }
994
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700995 public void cancelAllNotifications(String pkg) {
996 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800997
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700998 // Calling from user space, don't allow the canceling of actively
999 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001000 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 }
1002
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001003 void checkIncomingCall(String pkg) {
1004 int uid = Binder.getCallingUid();
1005 if (uid == Process.SYSTEM_UID || uid == 0) {
1006 return;
1007 }
1008 try {
1009 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
1010 pkg, 0);
1011 if (ai.uid != uid) {
1012 throw new SecurityException("Calling uid " + uid + " gave package"
1013 + pkg + " which is owned by uid " + ai.uid);
1014 }
1015 } catch (PackageManager.NameNotFoundException e) {
1016 throw new SecurityException("Unknown package " + pkg);
1017 }
1018 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001019
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001020 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 synchronized (mNotificationList) {
1022 final int N = mNotificationList.size();
1023 for (int i=N-1; i>=0; i--) {
1024 NotificationRecord r = mNotificationList.get(i);
1025
1026 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1027 | Notification.FLAG_NO_CLEAR)) == 0) {
1028 if (r.notification.deleteIntent != null) {
1029 try {
1030 r.notification.deleteIntent.send();
1031 } catch (PendingIntent.CanceledException ex) {
1032 // do nothing - there's no relevant way to recover, and
1033 // no reason to let this propagate
Joe Onorato8a9b2202010-02-26 18:56:32 -08001034 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 }
1036 }
1037 mNotificationList.remove(i);
1038 cancelNotificationLocked(r);
1039 }
1040 }
1041
1042 updateLightsLocked();
1043 }
1044 }
1045
1046 private void updateLights() {
1047 synchronized (mNotificationList) {
1048 updateLightsLocked();
1049 }
1050 }
1051
1052 // lock on mNotificationList
1053 private void updateLightsLocked()
1054 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001055 // Battery low always shows, other states only show if charging.
1056 if (mBatteryLow) {
Mike Lockwood445f4302009-09-04 11:06:46 -04001057 if (mBatteryCharging) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001058 mBatteryLight.setColor(BATTERY_LOW_ARGB);
Mike Lockwood445f4302009-09-04 11:06:46 -04001059 } else {
1060 // Flash when battery is low and not charging
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001061 mBatteryLight.setFlashing(BATTERY_LOW_ARGB, LightsService.LIGHT_FLASH_TIMED,
1062 BATTERY_BLINK_ON, BATTERY_BLINK_OFF);
Mike Lockwood445f4302009-09-04 11:06:46 -04001063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001065 if (mBatteryFull) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001066 mBatteryLight.setColor(BATTERY_FULL_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001068 mBatteryLight.setColor(BATTERY_MEDIUM_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
1070 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001071 mBatteryLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073
The Android Open Source Project10592532009-03-18 17:39:46 -07001074 // handle notification lights
1075 if (mLedNotification == null) {
1076 // get next notification, if any
1077 int n = mLights.size();
1078 if (n > 0) {
1079 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
1081 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001082
1083 // we only flash if screen is off and persistent pulsing is enabled
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001084 // and we are not currently in a call
1085 if (mLedNotification == null || mScreenOn || mInCall) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001086 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001087 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001088 int ledARGB = mLedNotification.notification.ledARGB;
1089 int ledOnMS = mLedNotification.notification.ledOnMS;
1090 int ledOffMS = mLedNotification.notification.ledOffMS;
1091 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1092 ledARGB = mDefaultNotificationColor;
1093 ledOnMS = mDefaultNotificationLedOn;
1094 ledOffMS = mDefaultNotificationLedOff;
1095 }
1096 if (mNotificationPulseEnabled) {
1097 // pulse repeatedly
1098 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1099 ledOnMS, ledOffMS);
1100 } else {
1101 // pulse only once
1102 mNotificationLight.pulse(ledARGB, ledOnMS);
1103 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 }
1106
1107 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001108 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 {
1110 ArrayList<NotificationRecord> list = mNotificationList;
1111 final int len = list.size();
1112 for (int i=0; i<len; i++) {
1113 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001114 if (tag == null) {
1115 if (r.tag != null) {
1116 continue;
1117 }
1118 } else {
1119 if (!tag.equals(r.tag)) {
1120 continue;
1121 }
1122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 if (r.id == id && r.pkg.equals(pkg)) {
1124 return i;
1125 }
1126 }
1127 return -1;
1128 }
1129
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001130 // This is here instead of StatusBarPolicy because it is an important
1131 // security feature that we don't want people customizing the platform
1132 // to accidentally lose.
Mike Lockwoodff2544c2010-06-28 09:17:50 -04001133 private void updateAdbNotification(boolean adbEnabled) {
1134 if (adbEnabled) {
Mike Lockwooded760372009-07-09 07:07:27 -04001135 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1136 return;
1137 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001138 if (!mAdbNotificationShown) {
1139 NotificationManager notificationManager = (NotificationManager) mContext
1140 .getSystemService(Context.NOTIFICATION_SERVICE);
1141 if (notificationManager != null) {
1142 Resources r = mContext.getResources();
1143 CharSequence title = r.getText(
1144 com.android.internal.R.string.adb_active_notification_title);
1145 CharSequence message = r.getText(
1146 com.android.internal.R.string.adb_active_notification_message);
1147
1148 if (mAdbNotification == null) {
1149 mAdbNotification = new Notification();
Daniel Sandler39576c82010-03-25 16:02:33 -04001150 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001151 mAdbNotification.when = 0;
1152 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1153 mAdbNotification.tickerText = title;
Daniel Sandler39576c82010-03-25 16:02:33 -04001154 mAdbNotification.defaults = 0; // please be quiet
1155 mAdbNotification.sound = null;
1156 mAdbNotification.vibrate = null;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001157 }
1158
1159 Intent intent = new Intent(
1160 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1161 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1162 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1163 // Note: we are hard-coding the component because this is
1164 // an important security UI that we don't want anyone
1165 // intercepting.
1166 intent.setComponent(new ComponentName("com.android.settings",
1167 "com.android.settings.DevelopmentSettings"));
1168 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1169 intent, 0);
1170
1171 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001172
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001173 mAdbNotificationShown = true;
1174 notificationManager.notify(
1175 com.android.internal.R.string.adb_active_notification_title,
1176 mAdbNotification);
1177 }
1178 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001179
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001180 } else if (mAdbNotificationShown) {
1181 NotificationManager notificationManager = (NotificationManager) mContext
1182 .getSystemService(Context.NOTIFICATION_SERVICE);
1183 if (notificationManager != null) {
1184 mAdbNotificationShown = false;
1185 notificationManager.cancel(
1186 com.android.internal.R.string.adb_active_notification_title);
1187 }
1188 }
1189 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001190
1191 private void updateNotificationPulse() {
1192 synchronized (mNotificationList) {
1193 updateLightsLocked();
1194 }
1195 }
1196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 // ======================================================================
1198 @Override
1199 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1200 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1201 != PackageManager.PERMISSION_GRANTED) {
1202 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1203 + Binder.getCallingPid()
1204 + ", uid=" + Binder.getCallingUid());
1205 return;
1206 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 pw.println("Current Notification Manager state:");
1209
1210 int N;
1211
1212 synchronized (mToastQueue) {
1213 N = mToastQueue.size();
1214 if (N > 0) {
1215 pw.println(" Toast Queue:");
1216 for (int i=0; i<N; i++) {
1217 mToastQueue.get(i).dump(pw, " ");
1218 }
1219 pw.println(" ");
1220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
1223
1224 synchronized (mNotificationList) {
1225 N = mNotificationList.size();
1226 if (N > 0) {
1227 pw.println(" Notification List:");
1228 for (int i=0; i<N; i++) {
1229 mNotificationList.get(i).dump(pw, " ", mContext);
1230 }
1231 pw.println(" ");
1232 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 N = mLights.size();
1235 if (N > 0) {
1236 pw.println(" Lights List:");
1237 for (int i=0; i<N; i++) {
1238 mLights.get(i).dump(pw, " ", mContext);
1239 }
1240 pw.println(" ");
1241 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 pw.println(" mSoundNotification=" + mSoundNotification);
1244 pw.println(" mSound=" + mSound);
1245 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001246 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1247 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 }
1249 }
1250}