blob: 4f11231d497880f897a4cd56da952faa13ee99d1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Joe Onorato18e69df2010-05-17 22:26:12 -070019import com.android.internal.statusbar.StatusBarNotification;
Joe Onorato7a0f36b2010-06-07 10:24:36 -070020import com.android.server.StatusBarManagerService;
svetoslavganov75986cf2009-05-14 22:28:01 -070021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.app.ActivityManagerNative;
23import android.app.IActivityManager;
24import android.app.INotificationManager;
25import android.app.ITransientNotification;
26import android.app.Notification;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070027import android.app.NotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.app.PendingIntent;
29import android.app.StatusBarManager;
30import android.content.BroadcastReceiver;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070031import android.content.ComponentName;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070032import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.Context;
34import android.content.Intent;
35import android.content.IntentFilter;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070036import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.PackageManager;
38import android.content.pm.PackageManager.NameNotFoundException;
39import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070040import android.database.ContentObserver;
Mike Lockwoodff2544c2010-06-28 09:17:50 -040041import android.hardware.Usb;
svetoslavganov75986cf2009-05-14 22:28:01 -070042import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.net.Uri;
44import android.os.BatteryManager;
Mike Lockwoodff2544c2010-06-28 09:17:50 -040045import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.IBinder;
49import android.os.Message;
50import android.os.Power;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070051import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070052import android.os.RemoteException;
Mike Lockwooded760372009-07-09 07:07:27 -040053import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Vibrator;
55import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050056import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080059import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.util.Log;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.widget.Toast;
Daniel Sandler0f0b11c2010-08-04 15:54:58 -040064import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import java.io.FileDescriptor;
67import java.io.PrintWriter;
68import java.util.ArrayList;
69import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
71class NotificationManagerService extends INotificationManager.Stub
72{
73 private static final String TAG = "NotificationService";
74 private static final boolean DBG = false;
75
Joe Onoratobd73d012010-06-04 11:44:54 -070076 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 // message codes
79 private static final int MESSAGE_TIMEOUT = 2;
80
81 private static final int LONG_DELAY = 3500; // 3.5 seconds
82 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -080083
84 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
86 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
87
88 final Context mContext;
89 final IActivityManager mAm;
90 final IBinder mForegroundToken = new Binder();
91
92 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -070093 private StatusBarManagerService mStatusBar;
Mike Lockwood3a322132009-11-24 00:30:52 -050094 private LightsService mLightsService;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050095 private LightsService.Light mBatteryLight;
96 private LightsService.Light mNotificationLight;
97 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
Mike Lockwood670f9322010-01-20 12:13:36 -050099 private int mDefaultNotificationColor;
100 private int mDefaultNotificationLedOn;
101 private int mDefaultNotificationLedOff;
102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 private NotificationRecord mSoundNotification;
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700104 private NotificationPlayer mSound;
Joe Onorato30275482009-07-08 17:09:14 -0700105 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400106 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 private NotificationRecord mVibrateNotification;
109 private Vibrator mVibrator = new Vibrator();
110
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500111 // for enabling and disabling notification pulse behavior
112 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500113 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500114 private boolean mNotificationPulseEnabled;
115
116 // for adb connected notifications
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700117 private boolean mAdbNotificationShown = false;
118 private Notification mAdbNotification;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800119
Fred Quintana6ecaff12009-09-25 14:23:13 -0700120 private final ArrayList<NotificationRecord> mNotificationList =
121 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
123 private ArrayList<ToastRecord> mToastQueue;
124
125 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
126
127 private boolean mBatteryCharging;
128 private boolean mBatteryLow;
129 private boolean mBatteryFull;
130 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700131
The Android Open Source Project10592532009-03-18 17:39:46 -0700132 private static final int BATTERY_LOW_ARGB = 0xFFFF0000; // Charging Low - red solid on
133 private static final int BATTERY_MEDIUM_ARGB = 0xFFFFFF00; // Charging - orange solid on
134 private static final int BATTERY_FULL_ARGB = 0xFF00FF00; // Charging Full - green solid on
135 private static final int BATTERY_BLINK_ON = 125;
136 private static final int BATTERY_BLINK_OFF = 2875;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 private static String idDebugString(Context baseContext, String packageName, int id) {
139 Context c = null;
140
141 if (packageName != null) {
142 try {
143 c = baseContext.createPackageContext(packageName, 0);
144 } catch (NameNotFoundException e) {
145 c = baseContext;
146 }
147 } else {
148 c = baseContext;
149 }
150
151 String pkg;
152 String type;
153 String name;
154
155 Resources r = c.getResources();
156 try {
157 return r.getResourceName(id);
158 } catch (Resources.NotFoundException e) {
159 return "<name unknown>";
160 }
161 }
162
163 private static final class NotificationRecord
164 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700165 final String pkg;
166 final String tag;
167 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700168 final int uid;
169 final int initialPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 ITransientNotification callback;
171 int duration;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700172 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 IBinder statusBarKey;
174
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700175 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid,
176 Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 {
178 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700179 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700181 this.uid = uid;
182 this.initialPid = initialPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 this.notification = notification;
184 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 void dump(PrintWriter pw, String prefix, Context baseContext) {
187 pw.println(prefix + this);
188 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
189 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
190 pw.println(prefix + " contentIntent=" + notification.contentIntent);
191 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
192 pw.println(prefix + " tickerText=" + notification.tickerText);
193 pw.println(prefix + " contentView=" + notification.contentView);
194 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
195 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
196 pw.println(prefix + " sound=" + notification.sound);
197 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
198 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
199 + " ledOnMS=" + notification.ledOnMS
200 + " ledOffMS=" + notification.ledOffMS);
201 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 @Override
204 public final String toString()
205 {
206 return "NotificationRecord{"
207 + Integer.toHexString(System.identityHashCode(this))
208 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700209 + " id=" + Integer.toHexString(id)
210 + " tag=" + tag + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 }
212 }
213
214 private static final class ToastRecord
215 {
216 final int pid;
217 final String pkg;
218 final ITransientNotification callback;
219 int duration;
220
221 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
222 {
223 this.pid = pid;
224 this.pkg = pkg;
225 this.callback = callback;
226 this.duration = duration;
227 }
228
229 void update(int duration) {
230 this.duration = duration;
231 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 void dump(PrintWriter pw, String prefix) {
234 pw.println(prefix + this);
235 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 @Override
238 public final String toString()
239 {
240 return "ToastRecord{"
241 + Integer.toHexString(System.identityHashCode(this))
242 + " pkg=" + pkg
243 + " callback=" + callback
244 + " duration=" + duration;
245 }
246 }
247
Joe Onorato089de882010-04-12 08:18:45 -0700248 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
249 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250
251 public void onSetDisabled(int status) {
252 synchronized (mNotificationList) {
253 mDisabledNotifications = status;
254 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
255 // cancel whatever's going on
256 long identity = Binder.clearCallingIdentity();
257 try {
258 mSound.stop();
259 }
260 finally {
261 Binder.restoreCallingIdentity(identity);
262 }
263
264 identity = Binder.clearCallingIdentity();
265 try {
266 mVibrator.cancel();
267 }
268 finally {
269 Binder.restoreCallingIdentity(identity);
270 }
271 }
272 }
273 }
274
275 public void onClearAll() {
276 cancelAll();
277 }
278
Fred Quintana6ecaff12009-09-25 14:23:13 -0700279 public void onNotificationClick(String pkg, String tag, int id) {
280 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700281 Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 }
283
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400284 public void onNotificationClear(String pkg, String tag, int id) {
285 cancelNotification(pkg, tag, id, 0, 0); // maybe add some flags?
286 }
287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 public void onPanelRevealed() {
289 synchronized (mNotificationList) {
290 // sound
291 mSoundNotification = null;
292 long identity = Binder.clearCallingIdentity();
293 try {
294 mSound.stop();
295 }
296 finally {
297 Binder.restoreCallingIdentity(identity);
298 }
299
300 // vibrate
301 mVibrateNotification = null;
302 identity = Binder.clearCallingIdentity();
303 try {
304 mVibrator.cancel();
305 }
306 finally {
307 Binder.restoreCallingIdentity(identity);
308 }
309
310 // light
311 mLights.clear();
312 mLedNotification = null;
313 updateLightsLocked();
314 }
315 }
Joe Onorato005847b2010-06-04 16:08:02 -0400316
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700317 public void onNotificationError(String pkg, String tag, int id,
318 int uid, int initialPid, String message) {
Joe Onorato005847b2010-06-04 16:08:02 -0400319 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id);
320 cancelNotification(pkg, tag, id, 0, 0);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700321 long ident = Binder.clearCallingIdentity();
322 try {
323 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
324 "Bad notification posted from package " + pkg
325 + ": " + message);
326 } catch (RemoteException e) {
327 }
328 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 };
331
332 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
333 @Override
334 public void onReceive(Context context, Intent intent) {
335 String action = intent.getAction();
336
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800337 boolean queryRestart = false;
338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
340 boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0);
341 int level = intent.getIntExtra("level", -1);
342 boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD);
343 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
344 boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90);
345
346 if (batteryCharging != mBatteryCharging ||
347 batteryLow != mBatteryLow ||
348 batteryFull != mBatteryFull) {
349 mBatteryCharging = batteryCharging;
350 mBatteryLow = batteryLow;
351 mBatteryFull = batteryFull;
352 updateLights();
353 }
Mike Lockwoodff2544c2010-06-28 09:17:50 -0400354 } else if (action.equals(Usb.ACTION_USB_STATE)) {
355 Bundle extras = intent.getExtras();
356 boolean usbConnected = extras.getBoolean(Usb.USB_CONNECTED);
357 boolean adbEnabled = (Usb.USB_FUNCTION_ENABLED.equals(
358 extras.getString(Usb.USB_FUNCTION_ADB)));
359 updateAdbNotification(usbConnected && adbEnabled);
360 } else if (action.equals(Usb.ACTION_USB_DISCONNECTED)) {
361 updateAdbNotification(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800363 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800364 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800365 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800366 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800367 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800368 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800369 } else if (queryRestart) {
370 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800371 } else {
372 Uri uri = intent.getData();
373 if (uri == null) {
374 return;
375 }
376 String pkgName = uri.getSchemeSpecificPart();
377 if (pkgName == null) {
378 return;
379 }
380 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800382 if (pkgList != null && (pkgList.length > 0)) {
383 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800384 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500387 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
388 mScreenOn = true;
389 updateNotificationPulse();
390 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
391 mScreenOn = false;
392 updateNotificationPulse();
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500393 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
394 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK));
395 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 }
397 }
398 };
399
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700400 class SettingsObserver extends ContentObserver {
401 SettingsObserver(Handler handler) {
402 super(handler);
403 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800404
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700405 void observe() {
406 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500407 resolver.registerContentObserver(Settings.System.getUriFor(
408 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700409 update();
410 }
411
412 @Override public void onChange(boolean selfChange) {
413 update();
414 }
415
416 public void update() {
417 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500418 boolean pulseEnabled = Settings.System.getInt(resolver,
419 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
420 if (mNotificationPulseEnabled != pulseEnabled) {
421 mNotificationPulseEnabled = pulseEnabled;
422 updateNotificationPulse();
423 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700424 }
425 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500426
Joe Onorato089de882010-04-12 08:18:45 -0700427 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500428 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 {
430 super();
431 mContext = context;
Mike Lockwood3a322132009-11-24 00:30:52 -0500432 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 mAm = ActivityManagerNative.getDefault();
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700434 mSound = new NotificationPlayer(TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 mSound.setUsesWakeLock(context);
436 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800438
Joe Onorato089de882010-04-12 08:18:45 -0700439 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 statusBar.setNotificationCallbacks(mNotificationCallbacks);
441
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500442 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
443 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
444 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
445
Mike Lockwood670f9322010-01-20 12:13:36 -0500446 Resources resources = mContext.getResources();
447 mDefaultNotificationColor = resources.getColor(
448 com.android.internal.R.color.config_defaultNotificationColor);
449 mDefaultNotificationLedOn = resources.getInteger(
450 com.android.internal.R.integer.config_defaultNotificationLedOn);
451 mDefaultNotificationLedOff = resources.getInteger(
452 com.android.internal.R.integer.config_defaultNotificationLedOff);
453
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400454 // Don't start allowing notifications until the setup wizard has run once.
455 // After that, including subsequent boots, init with notifications turned on.
456 // This works on the first boot because the setup wizard will toggle this
457 // flag at least once and we'll go back to 0 after that.
458 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
459 Settings.Secure.DEVICE_PROVISIONED, 0)) {
460 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
461 }
462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 // register for battery changed notifications
464 IntentFilter filter = new IntentFilter();
465 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Mike Lockwoodff2544c2010-06-28 09:17:50 -0400466 filter.addAction(Usb.ACTION_USB_STATE);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500467 filter.addAction(Intent.ACTION_SCREEN_ON);
468 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500469 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800471 IntentFilter pkgFilter = new IntentFilter();
472 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
473 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
474 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
475 pkgFilter.addDataScheme("package");
476 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800477 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800478 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800479
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500480 SettingsObserver observer = new SettingsObserver(mHandler);
481 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 }
483
Joe Onorato30275482009-07-08 17:09:14 -0700484 void systemReady() {
485 // no beeping until we're basically done booting
486 mSystemReady = true;
487 }
488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 // Toasts
490 // ============================================================================
491 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
492 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400493 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494
495 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800496 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 return ;
498 }
499
500 synchronized (mToastQueue) {
501 int callingPid = Binder.getCallingPid();
502 long callingId = Binder.clearCallingIdentity();
503 try {
504 ToastRecord record;
505 int index = indexOfToastLocked(pkg, callback);
506 // If it's already in the queue, we update it in place, we don't
507 // move it to the end of the queue.
508 if (index >= 0) {
509 record = mToastQueue.get(index);
510 record.update(duration);
511 } else {
512 record = new ToastRecord(callingPid, pkg, callback, duration);
513 mToastQueue.add(record);
514 index = mToastQueue.size() - 1;
515 keepProcessAliveLocked(callingPid);
516 }
517 // If it's at index 0, it's the current toast. It doesn't matter if it's
518 // new or just been updated. Call back and tell it to show itself.
519 // If the callback fails, this will remove it from the list, so don't
520 // assume that it's valid after this.
521 if (index == 0) {
522 showNextToastLocked();
523 }
524 } finally {
525 Binder.restoreCallingIdentity(callingId);
526 }
527 }
528 }
529
530 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800531 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532
533 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800534 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 return ;
536 }
537
538 synchronized (mToastQueue) {
539 long callingId = Binder.clearCallingIdentity();
540 try {
541 int index = indexOfToastLocked(pkg, callback);
542 if (index >= 0) {
543 cancelToastLocked(index);
544 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800545 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 }
547 } finally {
548 Binder.restoreCallingIdentity(callingId);
549 }
550 }
551 }
552
553 private void showNextToastLocked() {
554 ToastRecord record = mToastQueue.get(0);
555 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800556 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 try {
558 record.callback.show();
559 scheduleTimeoutLocked(record, false);
560 return;
561 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800562 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 + " in package " + record.pkg);
564 // remove it from the list and let the process die
565 int index = mToastQueue.indexOf(record);
566 if (index >= 0) {
567 mToastQueue.remove(index);
568 }
569 keepProcessAliveLocked(record.pid);
570 if (mToastQueue.size() > 0) {
571 record = mToastQueue.get(0);
572 } else {
573 record = null;
574 }
575 }
576 }
577 }
578
579 private void cancelToastLocked(int index) {
580 ToastRecord record = mToastQueue.get(index);
581 try {
582 record.callback.hide();
583 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800584 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 + " in package " + record.pkg);
586 // don't worry about this, we're about to remove it from
587 // the list anyway
588 }
589 mToastQueue.remove(index);
590 keepProcessAliveLocked(record.pid);
591 if (mToastQueue.size() > 0) {
592 // Show the next one. If the callback fails, this will remove
593 // it from the list, so don't assume that the list hasn't changed
594 // after this point.
595 showNextToastLocked();
596 }
597 }
598
599 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
600 {
601 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
602 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
603 mHandler.removeCallbacksAndMessages(r);
604 mHandler.sendMessageDelayed(m, delay);
605 }
606
607 private void handleTimeout(ToastRecord record)
608 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800609 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 synchronized (mToastQueue) {
611 int index = indexOfToastLocked(record.pkg, record.callback);
612 if (index >= 0) {
613 cancelToastLocked(index);
614 }
615 }
616 }
617
618 // lock on mToastQueue
619 private int indexOfToastLocked(String pkg, ITransientNotification callback)
620 {
621 IBinder cbak = callback.asBinder();
622 ArrayList<ToastRecord> list = mToastQueue;
623 int len = list.size();
624 for (int i=0; i<len; i++) {
625 ToastRecord r = list.get(i);
626 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
627 return i;
628 }
629 }
630 return -1;
631 }
632
633 // lock on mToastQueue
634 private void keepProcessAliveLocked(int pid)
635 {
636 int toastCount = 0; // toasts from this pid
637 ArrayList<ToastRecord> list = mToastQueue;
638 int N = list.size();
639 for (int i=0; i<N; i++) {
640 ToastRecord r = list.get(i);
641 if (r.pid == pid) {
642 toastCount++;
643 }
644 }
645 try {
646 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
647 } catch (RemoteException e) {
648 // Shouldn't happen.
649 }
650 }
651
652 private final class WorkerHandler extends Handler
653 {
654 @Override
655 public void handleMessage(Message msg)
656 {
657 switch (msg.what)
658 {
659 case MESSAGE_TIMEOUT:
660 handleTimeout((ToastRecord)msg.obj);
661 break;
662 }
663 }
664 }
665
666
667 // Notifications
668 // ============================================================================
669 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
670 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700671 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
672 }
673
674 public void enqueueNotificationWithTag(String pkg, String tag, int id,
675 Notification notification, int[] idOut)
676 {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700677 final int callingUid = Binder.getCallingUid();
678 final int callingPid = Binder.getCallingPid();
679
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700680 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800681
Joe Onoratobd73d012010-06-04 11:44:54 -0700682 // Limit the number of notifications that any given package except the android
683 // package can enqueue. Prevents DOS attacks and deals with leaks.
684 if (!"android".equals(pkg)) {
685 synchronized (mNotificationList) {
686 int count = 0;
687 final int N = mNotificationList.size();
688 for (int i=0; i<N; i++) {
689 final NotificationRecord r = mNotificationList.get(i);
690 if (r.pkg.equals(pkg)) {
691 count++;
692 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
693 Slog.e(TAG, "Package has already posted " + count
694 + " notifications. Not showing more. package=" + pkg);
695 return;
696 }
697 }
698 }
699 }
700 }
701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 // This conditional is a dirty hack to limit the logging done on
703 // behalf of the download manager without affecting other apps.
704 if (!pkg.equals("com.android.providers.downloads")
705 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800706 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 }
708
709 if (pkg == null || notification == null) {
710 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
711 + " id=" + id + " notification=" + notification);
712 }
713 if (notification.icon != 0) {
714 if (notification.contentView == null) {
715 throw new IllegalArgumentException("contentView required: pkg=" + pkg
716 + " id=" + id + " notification=" + notification);
717 }
718 if (notification.contentIntent == null) {
719 throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
720 + " id=" + id + " notification=" + notification);
721 }
722 }
723
724 synchronized (mNotificationList) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700725 NotificationRecord r = new NotificationRecord(pkg, tag, id,
726 callingUid, callingPid, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 NotificationRecord old = null;
728
Fred Quintana6ecaff12009-09-25 14:23:13 -0700729 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 if (index < 0) {
731 mNotificationList.add(r);
732 } else {
733 old = mNotificationList.remove(index);
734 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700735 // Make sure we don't lose the foreground service state.
736 if (old != null) {
737 notification.flags |=
738 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800741
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700742 // Ensure if this is a foreground service that the proper additional
743 // flags are set.
744 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
745 notification.flags |= Notification.FLAG_ONGOING_EVENT
746 | Notification.FLAG_NO_CLEAR;
747 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 if (notification.icon != 0) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700750 StatusBarNotification n = new StatusBarNotification(pkg, id, tag,
751 r.uid, r.initialPid, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 if (old != null && old.statusBarKey != null) {
753 r.statusBarKey = old.statusBarKey;
754 long identity = Binder.clearCallingIdentity();
755 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700756 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
758 finally {
759 Binder.restoreCallingIdentity(identity);
760 }
761 } else {
762 long identity = Binder.clearCallingIdentity();
763 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700764 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500765 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 }
767 finally {
768 Binder.restoreCallingIdentity(identity);
769 }
770 }
Joe Onorato30275482009-07-08 17:09:14 -0700771 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 } else {
773 if (old != null && old.statusBarKey != null) {
774 long identity = Binder.clearCallingIdentity();
775 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700776 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 }
778 finally {
779 Binder.restoreCallingIdentity(identity);
780 }
781 }
782 }
783
784 // If we're not supposed to beep, vibrate, etc. then don't.
785 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
786 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700787 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
788 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800789
790 final AudioManager audioManager = (AudioManager) mContext
791 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 // sound
793 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800794 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 if (useDefaultSound || notification.sound != null) {
796 Uri uri;
797 if (useDefaultSound) {
798 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
799 } else {
800 uri = notification.sound;
801 }
802 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
803 int audioStreamType;
804 if (notification.audioStreamType >= 0) {
805 audioStreamType = notification.audioStreamType;
806 } else {
807 audioStreamType = DEFAULT_STREAM_TYPE;
808 }
809 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800810 // do not play notifications if stream volume is 0
811 // (typically because ringer mode is silent).
812 if (audioManager.getStreamVolume(audioStreamType) != 0) {
813 long identity = Binder.clearCallingIdentity();
814 try {
815 mSound.play(mContext, uri, looping, audioStreamType);
816 }
817 finally {
818 Binder.restoreCallingIdentity(identity);
819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 }
821 }
822
823 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800825 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 if ((useDefaultVibrate || notification.vibrate != null)
827 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
828 mVibrateNotification = r;
829
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800830 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 : notification.vibrate,
832 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
833 }
834 }
835
836 // this option doesn't shut off the lights
837
838 // light
839 // the most recent thing gets the light
840 mLights.remove(old);
841 if (mLedNotification == old) {
842 mLedNotification = null;
843 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800844 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
846 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
847 mLights.add(r);
848 updateLightsLocked();
849 } else {
850 if (old != null
851 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
852 updateLightsLocked();
853 }
854 }
855 }
856
857 idOut[0] = id;
858 }
859
Joe Onorato30275482009-07-08 17:09:14 -0700860 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700861 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
862 if (!manager.isEnabled()) {
863 return;
864 }
865
866 AccessibilityEvent event =
867 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
868 event.setPackageName(packageName);
869 event.setClassName(Notification.class.getName());
870 event.setParcelableData(notification);
871 CharSequence tickerText = notification.tickerText;
872 if (!TextUtils.isEmpty(tickerText)) {
873 event.getText().add(tickerText);
874 }
875
876 manager.sendAccessibilityEvent(event);
877 }
878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 private void cancelNotificationLocked(NotificationRecord r) {
880 // status bar
881 if (r.notification.icon != 0) {
882 long identity = Binder.clearCallingIdentity();
883 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700884 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 }
886 finally {
887 Binder.restoreCallingIdentity(identity);
888 }
889 r.statusBarKey = null;
890 }
891
892 // sound
893 if (mSoundNotification == r) {
894 mSoundNotification = null;
895 long identity = Binder.clearCallingIdentity();
896 try {
897 mSound.stop();
898 }
899 finally {
900 Binder.restoreCallingIdentity(identity);
901 }
902 }
903
904 // vibrate
905 if (mVibrateNotification == r) {
906 mVibrateNotification = null;
907 long identity = Binder.clearCallingIdentity();
908 try {
909 mVibrator.cancel();
910 }
911 finally {
912 Binder.restoreCallingIdentity(identity);
913 }
914 }
915
916 // light
917 mLights.remove(r);
918 if (mLedNotification == r) {
919 mLedNotification = null;
920 }
921 }
922
923 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700924 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800925 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700927 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700928 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800929 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930
931 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700932 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700934 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
937 return;
938 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700939 if ((r.notification.flags & mustNotHaveFlags) != 0) {
940 return;
941 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 mNotificationList.remove(index);
944
945 cancelNotificationLocked(r);
946 updateLightsLocked();
947 }
948 }
949 }
950
951 /**
952 * Cancels all notifications from a given package that have all of the
953 * {@code mustHaveFlags}.
954 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800955 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
956 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800957 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958
959 synchronized (mNotificationList) {
960 final int N = mNotificationList.size();
961 boolean canceledSomething = false;
962 for (int i = N-1; i >= 0; --i) {
963 NotificationRecord r = mNotificationList.get(i);
964 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
965 continue;
966 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700967 if ((r.notification.flags & mustNotHaveFlags) != 0) {
968 continue;
969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 if (!r.pkg.equals(pkg)) {
971 continue;
972 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800973 canceledSomething = true;
974 if (!doit) {
975 return true;
976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 mNotificationList.remove(i);
978 cancelNotificationLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 }
980 if (canceledSomething) {
981 updateLightsLocked();
982 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800983 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
985 }
986
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800987
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700988 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700989 cancelNotificationWithTag(pkg, null /* tag */, id);
990 }
991
992 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700993 checkIncomingCall(pkg);
994 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700995 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700996 Binder.getCallingUid() == Process.SYSTEM_UID
997 ? 0 : Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 }
999
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001000 public void cancelAllNotifications(String pkg) {
1001 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001002
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001003 // Calling from user space, don't allow the canceling of actively
1004 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001005 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 }
1007
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001008 void checkIncomingCall(String pkg) {
1009 int uid = Binder.getCallingUid();
1010 if (uid == Process.SYSTEM_UID || uid == 0) {
1011 return;
1012 }
1013 try {
1014 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
1015 pkg, 0);
1016 if (ai.uid != uid) {
1017 throw new SecurityException("Calling uid " + uid + " gave package"
1018 + pkg + " which is owned by uid " + ai.uid);
1019 }
1020 } catch (PackageManager.NameNotFoundException e) {
1021 throw new SecurityException("Unknown package " + pkg);
1022 }
1023 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001024
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001025 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 synchronized (mNotificationList) {
1027 final int N = mNotificationList.size();
1028 for (int i=N-1; i>=0; i--) {
1029 NotificationRecord r = mNotificationList.get(i);
1030
1031 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1032 | Notification.FLAG_NO_CLEAR)) == 0) {
1033 if (r.notification.deleteIntent != null) {
1034 try {
1035 r.notification.deleteIntent.send();
1036 } catch (PendingIntent.CanceledException ex) {
1037 // do nothing - there's no relevant way to recover, and
1038 // no reason to let this propagate
Joe Onorato8a9b2202010-02-26 18:56:32 -08001039 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
1041 }
1042 mNotificationList.remove(i);
1043 cancelNotificationLocked(r);
1044 }
1045 }
1046
1047 updateLightsLocked();
1048 }
1049 }
1050
1051 private void updateLights() {
1052 synchronized (mNotificationList) {
1053 updateLightsLocked();
1054 }
1055 }
1056
1057 // lock on mNotificationList
1058 private void updateLightsLocked()
1059 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001060 // Battery low always shows, other states only show if charging.
1061 if (mBatteryLow) {
Mike Lockwood445f4302009-09-04 11:06:46 -04001062 if (mBatteryCharging) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001063 mBatteryLight.setColor(BATTERY_LOW_ARGB);
Mike Lockwood445f4302009-09-04 11:06:46 -04001064 } else {
1065 // Flash when battery is low and not charging
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001066 mBatteryLight.setFlashing(BATTERY_LOW_ARGB, LightsService.LIGHT_FLASH_TIMED,
1067 BATTERY_BLINK_ON, BATTERY_BLINK_OFF);
Mike Lockwood445f4302009-09-04 11:06:46 -04001068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001070 if (mBatteryFull) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001071 mBatteryLight.setColor(BATTERY_FULL_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001073 mBatteryLight.setColor(BATTERY_MEDIUM_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 }
1075 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001076 mBatteryLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078
The Android Open Source Project10592532009-03-18 17:39:46 -07001079 // handle notification lights
1080 if (mLedNotification == null) {
1081 // get next notification, if any
1082 int n = mLights.size();
1083 if (n > 0) {
1084 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 }
1086 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001087
1088 // we only flash if screen is off and persistent pulsing is enabled
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001089 // and we are not currently in a call
1090 if (mLedNotification == null || mScreenOn || mInCall) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001091 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001092 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001093 int ledARGB = mLedNotification.notification.ledARGB;
1094 int ledOnMS = mLedNotification.notification.ledOnMS;
1095 int ledOffMS = mLedNotification.notification.ledOffMS;
1096 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1097 ledARGB = mDefaultNotificationColor;
1098 ledOnMS = mDefaultNotificationLedOn;
1099 ledOffMS = mDefaultNotificationLedOff;
1100 }
1101 if (mNotificationPulseEnabled) {
1102 // pulse repeatedly
1103 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1104 ledOnMS, ledOffMS);
1105 } else {
1106 // pulse only once
1107 mNotificationLight.pulse(ledARGB, ledOnMS);
1108 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
1111
1112 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001113 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 {
1115 ArrayList<NotificationRecord> list = mNotificationList;
1116 final int len = list.size();
1117 for (int i=0; i<len; i++) {
1118 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001119 if (tag == null) {
1120 if (r.tag != null) {
1121 continue;
1122 }
1123 } else {
1124 if (!tag.equals(r.tag)) {
1125 continue;
1126 }
1127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 if (r.id == id && r.pkg.equals(pkg)) {
1129 return i;
1130 }
1131 }
1132 return -1;
1133 }
1134
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001135 // This is here instead of StatusBarPolicy because it is an important
1136 // security feature that we don't want people customizing the platform
1137 // to accidentally lose.
Mike Lockwoodff2544c2010-06-28 09:17:50 -04001138 private void updateAdbNotification(boolean adbEnabled) {
1139 if (adbEnabled) {
Mike Lockwooded760372009-07-09 07:07:27 -04001140 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1141 return;
1142 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001143 if (!mAdbNotificationShown) {
1144 NotificationManager notificationManager = (NotificationManager) mContext
1145 .getSystemService(Context.NOTIFICATION_SERVICE);
1146 if (notificationManager != null) {
1147 Resources r = mContext.getResources();
1148 CharSequence title = r.getText(
1149 com.android.internal.R.string.adb_active_notification_title);
1150 CharSequence message = r.getText(
1151 com.android.internal.R.string.adb_active_notification_message);
1152
1153 if (mAdbNotification == null) {
1154 mAdbNotification = new Notification();
Daniel Sandler39576c82010-03-25 16:02:33 -04001155 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001156 mAdbNotification.when = 0;
1157 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1158 mAdbNotification.tickerText = title;
Daniel Sandler39576c82010-03-25 16:02:33 -04001159 mAdbNotification.defaults = 0; // please be quiet
1160 mAdbNotification.sound = null;
1161 mAdbNotification.vibrate = null;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001162 }
1163
1164 Intent intent = new Intent(
1165 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1166 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1167 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1168 // Note: we are hard-coding the component because this is
1169 // an important security UI that we don't want anyone
1170 // intercepting.
1171 intent.setComponent(new ComponentName("com.android.settings",
1172 "com.android.settings.DevelopmentSettings"));
1173 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1174 intent, 0);
1175
1176 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001177
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001178 mAdbNotificationShown = true;
1179 notificationManager.notify(
1180 com.android.internal.R.string.adb_active_notification_title,
1181 mAdbNotification);
1182 }
1183 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001184
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001185 } else if (mAdbNotificationShown) {
1186 NotificationManager notificationManager = (NotificationManager) mContext
1187 .getSystemService(Context.NOTIFICATION_SERVICE);
1188 if (notificationManager != null) {
1189 mAdbNotificationShown = false;
1190 notificationManager.cancel(
1191 com.android.internal.R.string.adb_active_notification_title);
1192 }
1193 }
1194 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001195
1196 private void updateNotificationPulse() {
1197 synchronized (mNotificationList) {
1198 updateLightsLocked();
1199 }
1200 }
1201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 // ======================================================================
1203 @Override
1204 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1205 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1206 != PackageManager.PERMISSION_GRANTED) {
1207 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1208 + Binder.getCallingPid()
1209 + ", uid=" + Binder.getCallingUid());
1210 return;
1211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 pw.println("Current Notification Manager state:");
1214
1215 int N;
1216
1217 synchronized (mToastQueue) {
1218 N = mToastQueue.size();
1219 if (N > 0) {
1220 pw.println(" Toast Queue:");
1221 for (int i=0; i<N; i++) {
1222 mToastQueue.get(i).dump(pw, " ");
1223 }
1224 pw.println(" ");
1225 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 }
1228
1229 synchronized (mNotificationList) {
1230 N = mNotificationList.size();
1231 if (N > 0) {
1232 pw.println(" Notification List:");
1233 for (int i=0; i<N; i++) {
1234 mNotificationList.get(i).dump(pw, " ", mContext);
1235 }
1236 pw.println(" ");
1237 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 N = mLights.size();
1240 if (N > 0) {
1241 pw.println(" Lights List:");
1242 for (int i=0; i<N; i++) {
1243 mLights.get(i).dump(pw, " ", mContext);
1244 }
1245 pw.println(" ");
1246 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 pw.println(" mSoundNotification=" + mSoundNotification);
1249 pw.println(" mSound=" + mSound);
1250 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001251 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1252 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 }
1254 }
1255}