| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 19 | import com.android.internal.statusbar.StatusBarNotification; |
| Joe Onorato | 7a0f36b | 2010-06-07 10:24:36 -0700 | [diff] [blame] | 20 | import com.android.server.StatusBarManagerService; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 21 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.app.ActivityManagerNative; |
| 23 | import android.app.IActivityManager; |
| 24 | import android.app.INotificationManager; |
| 25 | import android.app.ITransientNotification; |
| 26 | import android.app.Notification; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 27 | import android.app.NotificationManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.app.PendingIntent; |
| 29 | import android.app.StatusBarManager; |
| 30 | import android.content.BroadcastReceiver; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 31 | import android.content.ComponentName; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 32 | import android.content.ContentResolver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.content.Context; |
| 34 | import android.content.Intent; |
| 35 | import android.content.IntentFilter; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 36 | import android.content.pm.ApplicationInfo; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.content.pm.PackageManager; |
| 38 | import android.content.pm.PackageManager.NameNotFoundException; |
| 39 | import android.content.res.Resources; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 40 | import android.database.ContentObserver; |
| Mike Lockwood | 40bbf92 | 2011-03-01 10:23:48 -0800 | [diff] [blame] | 41 | import android.hardware.usb.UsbManager; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 42 | import android.media.AudioManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.net.Uri; |
| 44 | import android.os.BatteryManager; |
| Mike Lockwood | ff2544c | 2010-06-28 09:17:50 -0400 | [diff] [blame] | 45 | import android.os.Bundle; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.os.Binder; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.os.Handler; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.os.IBinder; |
| 49 | import android.os.Message; |
| 50 | import android.os.Power; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 51 | import android.os.Process; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 52 | import android.os.RemoteException; |
| Mike Lockwood | ed76037 | 2009-07-09 07:07:27 -0400 | [diff] [blame] | 53 | import android.os.SystemProperties; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.os.Vibrator; |
| 55 | import android.provider.Settings; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 56 | import android.telephony.TelephonyManager; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 57 | import android.text.TextUtils; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.util.EventLog; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 59 | import android.util.Slog; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.util.Log; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 61 | import android.view.accessibility.AccessibilityEvent; |
| 62 | import android.view.accessibility.AccessibilityManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.widget.Toast; |
| 64 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import java.io.FileDescriptor; |
| 66 | import java.io.PrintWriter; |
| 67 | import java.util.ArrayList; |
| 68 | import java.util.Arrays; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 70 | /** {@hide} */ |
| 71 | public class NotificationManagerService extends INotificationManager.Stub |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | { |
| 73 | private static final String TAG = "NotificationService"; |
| 74 | private static final boolean DBG = false; |
| 75 | |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 76 | private static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
| 77 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | // 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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 83 | |
| 84 | private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | |
| 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 Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 93 | private StatusBarManagerService mStatusBar; |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 94 | private LightsService mLightsService; |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 95 | private LightsService.Light mBatteryLight; |
| 96 | private LightsService.Light mNotificationLight; |
| 97 | private LightsService.Light mAttentionLight; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 99 | private int mDefaultNotificationColor; |
| 100 | private int mDefaultNotificationLedOn; |
| 101 | private int mDefaultNotificationLedOff; |
| 102 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | private NotificationRecord mSoundNotification; |
| Jean-Michel Trivi | 211957f | 2010-03-26 18:19:33 -0700 | [diff] [blame] | 104 | private NotificationPlayer mSound; |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 105 | private boolean mSystemReady; |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 106 | private int mDisabledNotifications; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | |
| 108 | private NotificationRecord mVibrateNotification; |
| 109 | private Vibrator mVibrator = new Vibrator(); |
| 110 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 111 | // for enabling and disabling notification pulse behavior |
| 112 | private boolean mScreenOn = true; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 113 | private boolean mInCall = false; |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 114 | private boolean mNotificationPulseEnabled; |
| Mike Lockwood | 2117f6f | 2010-09-09 09:48:08 -0400 | [diff] [blame] | 115 | // This is true if we have received a new notification while the screen is off |
| 116 | // (that is, if mLedNotification was set while the screen was off) |
| 117 | // This is reset to false when the screen is turned on. |
| 118 | private boolean mPendingPulseNotification; |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 119 | |
| 120 | // for adb connected notifications |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 121 | private boolean mAdbNotificationShown = false; |
| 122 | private Notification mAdbNotification; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 123 | |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 124 | private final ArrayList<NotificationRecord> mNotificationList = |
| 125 | new ArrayList<NotificationRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | |
| 127 | private ArrayList<ToastRecord> mToastQueue; |
| 128 | |
| 129 | private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>(); |
| 130 | |
| 131 | private boolean mBatteryCharging; |
| 132 | private boolean mBatteryLow; |
| 133 | private boolean mBatteryFull; |
| 134 | private NotificationRecord mLedNotification; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 135 | |
| Kenneth Andersson | d5d87b2 | 2010-08-03 13:30:18 +0200 | [diff] [blame] | 136 | private static int mBatteryLowARGB; |
| 137 | private static int mBatteryMediumARGB; |
| 138 | private static int mBatteryFullARGB; |
| 139 | private static int mBatteryLedOn; |
| 140 | private static int mBatteryLedOff; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | private static String idDebugString(Context baseContext, String packageName, int id) { |
| 143 | Context c = null; |
| 144 | |
| 145 | if (packageName != null) { |
| 146 | try { |
| 147 | c = baseContext.createPackageContext(packageName, 0); |
| 148 | } catch (NameNotFoundException e) { |
| 149 | c = baseContext; |
| 150 | } |
| 151 | } else { |
| 152 | c = baseContext; |
| 153 | } |
| 154 | |
| 155 | String pkg; |
| 156 | String type; |
| 157 | String name; |
| 158 | |
| 159 | Resources r = c.getResources(); |
| 160 | try { |
| 161 | return r.getResourceName(id); |
| 162 | } catch (Resources.NotFoundException e) { |
| 163 | return "<name unknown>"; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | private static final class NotificationRecord |
| 168 | { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 169 | final String pkg; |
| 170 | final String tag; |
| 171 | final int id; |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 172 | final int uid; |
| 173 | final int initialPid; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | ITransientNotification callback; |
| 175 | int duration; |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 176 | final Notification notification; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | IBinder statusBarKey; |
| 178 | |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 179 | NotificationRecord(String pkg, String tag, int id, int uid, int initialPid, |
| 180 | Notification notification) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | { |
| 182 | this.pkg = pkg; |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 183 | this.tag = tag; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | this.id = id; |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 185 | this.uid = uid; |
| 186 | this.initialPid = initialPid; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | this.notification = notification; |
| 188 | } |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 189 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | void dump(PrintWriter pw, String prefix, Context baseContext) { |
| 191 | pw.println(prefix + this); |
| 192 | pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon) |
| 193 | + " / " + idDebugString(baseContext, this.pkg, notification.icon)); |
| 194 | pw.println(prefix + " contentIntent=" + notification.contentIntent); |
| 195 | pw.println(prefix + " deleteIntent=" + notification.deleteIntent); |
| 196 | pw.println(prefix + " tickerText=" + notification.tickerText); |
| 197 | pw.println(prefix + " contentView=" + notification.contentView); |
| 198 | pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults)); |
| 199 | pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags)); |
| 200 | pw.println(prefix + " sound=" + notification.sound); |
| 201 | pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate)); |
| 202 | pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB) |
| 203 | + " ledOnMS=" + notification.ledOnMS |
| 204 | + " ledOffMS=" + notification.ledOffMS); |
| 205 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 206 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | @Override |
| 208 | public final String toString() |
| 209 | { |
| 210 | return "NotificationRecord{" |
| 211 | + Integer.toHexString(System.identityHashCode(this)) |
| 212 | + " pkg=" + pkg |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 213 | + " id=" + Integer.toHexString(id) |
| 214 | + " tag=" + tag + "}"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | private static final class ToastRecord |
| 219 | { |
| 220 | final int pid; |
| 221 | final String pkg; |
| 222 | final ITransientNotification callback; |
| 223 | int duration; |
| 224 | |
| 225 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration) |
| 226 | { |
| 227 | this.pid = pid; |
| 228 | this.pkg = pkg; |
| 229 | this.callback = callback; |
| 230 | this.duration = duration; |
| 231 | } |
| 232 | |
| 233 | void update(int duration) { |
| 234 | this.duration = duration; |
| 235 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 236 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | void dump(PrintWriter pw, String prefix) { |
| 238 | pw.println(prefix + this); |
| 239 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 240 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | @Override |
| 242 | public final String toString() |
| 243 | { |
| 244 | return "ToastRecord{" |
| 245 | + Integer.toHexString(System.identityHashCode(this)) |
| 246 | + " pkg=" + pkg |
| 247 | + " callback=" + callback |
| 248 | + " duration=" + duration; |
| 249 | } |
| 250 | } |
| 251 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 252 | private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks |
| 253 | = new StatusBarManagerService.NotificationCallbacks() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | |
| 255 | public void onSetDisabled(int status) { |
| 256 | synchronized (mNotificationList) { |
| 257 | mDisabledNotifications = status; |
| 258 | if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) { |
| 259 | // cancel whatever's going on |
| 260 | long identity = Binder.clearCallingIdentity(); |
| 261 | try { |
| 262 | mSound.stop(); |
| 263 | } |
| 264 | finally { |
| 265 | Binder.restoreCallingIdentity(identity); |
| 266 | } |
| 267 | |
| 268 | identity = Binder.clearCallingIdentity(); |
| 269 | try { |
| 270 | mVibrator.cancel(); |
| 271 | } |
| 272 | finally { |
| 273 | Binder.restoreCallingIdentity(identity); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | public void onClearAll() { |
| 280 | cancelAll(); |
| 281 | } |
| 282 | |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 283 | public void onNotificationClick(String pkg, String tag, int id) { |
| 284 | cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL, |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 285 | Notification.FLAG_FOREGROUND_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | 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 Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 316 | |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 317 | public void onNotificationError(String pkg, String tag, int id, |
| 318 | int uid, int initialPid, String message) { |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 319 | Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id |
| 320 | + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")"); |
| Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 321 | cancelNotification(pkg, tag, id, 0, 0); |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 322 | long ident = Binder.clearCallingIdentity(); |
| 323 | try { |
| 324 | ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg, |
| 325 | "Bad notification posted from package " + pkg |
| 326 | + ": " + message); |
| 327 | } catch (RemoteException e) { |
| 328 | } |
| 329 | Binder.restoreCallingIdentity(ident); |
| Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 330 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 334 | @Override |
| 335 | public void onReceive(Context context, Intent intent) { |
| 336 | String action = intent.getAction(); |
| 337 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 338 | boolean queryRestart = false; |
| 339 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { |
| 341 | boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0); |
| 342 | int level = intent.getIntExtra("level", -1); |
| 343 | boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD); |
| 344 | int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN); |
| 345 | boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90); |
| 346 | |
| 347 | if (batteryCharging != mBatteryCharging || |
| 348 | batteryLow != mBatteryLow || |
| 349 | batteryFull != mBatteryFull) { |
| 350 | mBatteryCharging = batteryCharging; |
| 351 | mBatteryLow = batteryLow; |
| 352 | mBatteryFull = batteryFull; |
| 353 | updateLights(); |
| 354 | } |
| Mike Lockwood | 7916432 | 2010-07-27 18:44:30 -0400 | [diff] [blame] | 355 | } else if (action.equals(UsbManager.ACTION_USB_STATE)) { |
| Mike Lockwood | ff2544c | 2010-06-28 09:17:50 -0400 | [diff] [blame] | 356 | Bundle extras = intent.getExtras(); |
| Mike Lockwood | 7916432 | 2010-07-27 18:44:30 -0400 | [diff] [blame] | 357 | boolean usbConnected = extras.getBoolean(UsbManager.USB_CONNECTED); |
| 358 | boolean adbEnabled = (UsbManager.USB_FUNCTION_ENABLED.equals( |
| 359 | extras.getString(UsbManager.USB_FUNCTION_ADB))); |
| Mike Lockwood | ff2544c | 2010-06-28 09:17:50 -0400 | [diff] [blame] | 360 | updateAdbNotification(usbConnected && adbEnabled); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED) |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 362 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 363 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 364 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 365 | String pkgList[] = null; |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 366 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 367 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 368 | } else if (queryRestart) { |
| 369 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 370 | } else { |
| 371 | Uri uri = intent.getData(); |
| 372 | if (uri == null) { |
| 373 | return; |
| 374 | } |
| 375 | String pkgName = uri.getSchemeSpecificPart(); |
| 376 | if (pkgName == null) { |
| 377 | return; |
| 378 | } |
| 379 | pkgList = new String[]{pkgName}; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 381 | if (pkgList != null && (pkgList.length > 0)) { |
| 382 | for (String pkgName : pkgList) { |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 383 | cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 384 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 386 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
| 387 | mScreenOn = true; |
| 388 | updateNotificationPulse(); |
| 389 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 390 | mScreenOn = false; |
| 391 | updateNotificationPulse(); |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 392 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
| 393 | mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)); |
| 394 | updateNotificationPulse(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | }; |
| 398 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 399 | class SettingsObserver extends ContentObserver { |
| 400 | SettingsObserver(Handler handler) { |
| 401 | super(handler); |
| 402 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 403 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 404 | void observe() { |
| 405 | ContentResolver resolver = mContext.getContentResolver(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 406 | resolver.registerContentObserver(Settings.System.getUriFor( |
| 407 | Settings.System.NOTIFICATION_LIGHT_PULSE), false, this); |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 408 | update(); |
| 409 | } |
| 410 | |
| 411 | @Override public void onChange(boolean selfChange) { |
| 412 | update(); |
| 413 | } |
| 414 | |
| 415 | public void update() { |
| 416 | ContentResolver resolver = mContext.getContentResolver(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 417 | boolean pulseEnabled = Settings.System.getInt(resolver, |
| 418 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0; |
| 419 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 420 | mNotificationPulseEnabled = pulseEnabled; |
| 421 | updateNotificationPulse(); |
| 422 | } |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 425 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 426 | NotificationManagerService(Context context, StatusBarManagerService statusBar, |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 427 | LightsService lights) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | { |
| 429 | super(); |
| 430 | mContext = context; |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 431 | mLightsService = lights; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | mAm = ActivityManagerNative.getDefault(); |
| Jean-Michel Trivi | 211957f | 2010-03-26 18:19:33 -0700 | [diff] [blame] | 433 | mSound = new NotificationPlayer(TAG); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | mSound.setUsesWakeLock(context); |
| 435 | mToastQueue = new ArrayList<ToastRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | mHandler = new WorkerHandler(); |
| San Mehat | 3ee1317 | 2010-02-04 20:54:43 -0800 | [diff] [blame] | 437 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 438 | mStatusBar = statusBar; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | statusBar.setNotificationCallbacks(mNotificationCallbacks); |
| 440 | |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 441 | mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY); |
| 442 | mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS); |
| 443 | mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION); |
| 444 | |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 445 | Resources resources = mContext.getResources(); |
| 446 | mDefaultNotificationColor = resources.getColor( |
| 447 | com.android.internal.R.color.config_defaultNotificationColor); |
| 448 | mDefaultNotificationLedOn = resources.getInteger( |
| 449 | com.android.internal.R.integer.config_defaultNotificationLedOn); |
| 450 | mDefaultNotificationLedOff = resources.getInteger( |
| 451 | com.android.internal.R.integer.config_defaultNotificationLedOff); |
| 452 | |
| Kenneth Andersson | d5d87b2 | 2010-08-03 13:30:18 +0200 | [diff] [blame] | 453 | mBatteryLowARGB = mContext.getResources().getInteger( |
| 454 | com.android.internal.R.integer.config_notificationsBatteryLowARGB); |
| 455 | mBatteryMediumARGB = mContext.getResources().getInteger( |
| 456 | com.android.internal.R.integer.config_notificationsBatteryMediumARGB); |
| 457 | mBatteryFullARGB = mContext.getResources().getInteger( |
| 458 | com.android.internal.R.integer.config_notificationsBatteryFullARGB); |
| 459 | mBatteryLedOn = mContext.getResources().getInteger( |
| 460 | com.android.internal.R.integer.config_notificationsBatteryLedOn); |
| 461 | mBatteryLedOff = mContext.getResources().getInteger( |
| 462 | com.android.internal.R.integer.config_notificationsBatteryLedOff); |
| 463 | |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 464 | // Don't start allowing notifications until the setup wizard has run once. |
| 465 | // After that, including subsequent boots, init with notifications turned on. |
| 466 | // This works on the first boot because the setup wizard will toggle this |
| 467 | // flag at least once and we'll go back to 0 after that. |
| 468 | if (0 == Settings.Secure.getInt(mContext.getContentResolver(), |
| 469 | Settings.Secure.DEVICE_PROVISIONED, 0)) { |
| 470 | mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS; |
| 471 | } |
| 472 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | // register for battery changed notifications |
| 474 | IntentFilter filter = new IntentFilter(); |
| 475 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
| Mike Lockwood | 7916432 | 2010-07-27 18:44:30 -0400 | [diff] [blame] | 476 | filter.addAction(UsbManager.ACTION_USB_STATE); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 477 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 478 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 479 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | mContext.registerReceiver(mIntentReceiver, filter); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 481 | IntentFilter pkgFilter = new IntentFilter(); |
| 482 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 483 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 484 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 485 | pkgFilter.addDataScheme("package"); |
| 486 | mContext.registerReceiver(mIntentReceiver, pkgFilter); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 487 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 488 | mContext.registerReceiver(mIntentReceiver, sdFilter); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 489 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 490 | SettingsObserver observer = new SettingsObserver(mHandler); |
| 491 | observer.observe(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 494 | void systemReady() { |
| 495 | // no beeping until we're basically done booting |
| 496 | mSystemReady = true; |
| 497 | } |
| 498 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | // Toasts |
| 500 | // ============================================================================ |
| 501 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
| 502 | { |
| Daniel Sandler | a703590 | 2010-03-30 15:45:31 -0400 | [diff] [blame] | 503 | if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | |
| 505 | if (pkg == null || callback == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 506 | Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | return ; |
| 508 | } |
| 509 | |
| 510 | synchronized (mToastQueue) { |
| 511 | int callingPid = Binder.getCallingPid(); |
| 512 | long callingId = Binder.clearCallingIdentity(); |
| 513 | try { |
| 514 | ToastRecord record; |
| 515 | int index = indexOfToastLocked(pkg, callback); |
| 516 | // If it's already in the queue, we update it in place, we don't |
| 517 | // move it to the end of the queue. |
| 518 | if (index >= 0) { |
| 519 | record = mToastQueue.get(index); |
| 520 | record.update(duration); |
| 521 | } else { |
| Vairavan Srinivasan | f9eb06c | 2011-01-21 18:08:36 -0800 | [diff] [blame^] | 522 | // Limit the number of toasts that any given package except the android |
| 523 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| 524 | if (!"android".equals(pkg)) { |
| 525 | int count = 0; |
| 526 | final int N = mToastQueue.size(); |
| 527 | for (int i=0; i<N; i++) { |
| 528 | final ToastRecord r = mToastQueue.get(i); |
| 529 | if (r.pkg.equals(pkg)) { |
| 530 | count++; |
| 531 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 532 | Slog.e(TAG, "Package has already posted " + count |
| 533 | + " toasts. Not showing more. Package=" + pkg); |
| 534 | return; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | record = new ToastRecord(callingPid, pkg, callback, duration); |
| 541 | mToastQueue.add(record); |
| 542 | index = mToastQueue.size() - 1; |
| 543 | keepProcessAliveLocked(callingPid); |
| 544 | } |
| 545 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 546 | // new or just been updated. Call back and tell it to show itself. |
| 547 | // If the callback fails, this will remove it from the list, so don't |
| 548 | // assume that it's valid after this. |
| 549 | if (index == 0) { |
| 550 | showNextToastLocked(); |
| 551 | } |
| 552 | } finally { |
| 553 | Binder.restoreCallingIdentity(callingId); |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | public void cancelToast(String pkg, ITransientNotification callback) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 559 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | |
| 561 | if (pkg == null || callback == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 562 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | return ; |
| 564 | } |
| 565 | |
| 566 | synchronized (mToastQueue) { |
| 567 | long callingId = Binder.clearCallingIdentity(); |
| 568 | try { |
| 569 | int index = indexOfToastLocked(pkg, callback); |
| 570 | if (index >= 0) { |
| 571 | cancelToastLocked(index); |
| 572 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 573 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | } |
| 575 | } finally { |
| 576 | Binder.restoreCallingIdentity(callingId); |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | private void showNextToastLocked() { |
| 582 | ToastRecord record = mToastQueue.get(0); |
| 583 | while (record != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 584 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | try { |
| 586 | record.callback.show(); |
| 587 | scheduleTimeoutLocked(record, false); |
| 588 | return; |
| 589 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 590 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | + " in package " + record.pkg); |
| 592 | // remove it from the list and let the process die |
| 593 | int index = mToastQueue.indexOf(record); |
| 594 | if (index >= 0) { |
| 595 | mToastQueue.remove(index); |
| 596 | } |
| 597 | keepProcessAliveLocked(record.pid); |
| 598 | if (mToastQueue.size() > 0) { |
| 599 | record = mToastQueue.get(0); |
| 600 | } else { |
| 601 | record = null; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | private void cancelToastLocked(int index) { |
| 608 | ToastRecord record = mToastQueue.get(index); |
| 609 | try { |
| 610 | record.callback.hide(); |
| 611 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 612 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | + " in package " + record.pkg); |
| 614 | // don't worry about this, we're about to remove it from |
| 615 | // the list anyway |
| 616 | } |
| 617 | mToastQueue.remove(index); |
| 618 | keepProcessAliveLocked(record.pid); |
| 619 | if (mToastQueue.size() > 0) { |
| 620 | // Show the next one. If the callback fails, this will remove |
| 621 | // it from the list, so don't assume that the list hasn't changed |
| 622 | // after this point. |
| 623 | showNextToastLocked(); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | private void scheduleTimeoutLocked(ToastRecord r, boolean immediate) |
| 628 | { |
| 629 | Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r); |
| 630 | long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY); |
| 631 | mHandler.removeCallbacksAndMessages(r); |
| 632 | mHandler.sendMessageDelayed(m, delay); |
| 633 | } |
| 634 | |
| 635 | private void handleTimeout(ToastRecord record) |
| 636 | { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 637 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | synchronized (mToastQueue) { |
| 639 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 640 | if (index >= 0) { |
| 641 | cancelToastLocked(index); |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | // lock on mToastQueue |
| 647 | private int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 648 | { |
| 649 | IBinder cbak = callback.asBinder(); |
| 650 | ArrayList<ToastRecord> list = mToastQueue; |
| 651 | int len = list.size(); |
| 652 | for (int i=0; i<len; i++) { |
| 653 | ToastRecord r = list.get(i); |
| 654 | if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) { |
| 655 | return i; |
| 656 | } |
| 657 | } |
| 658 | return -1; |
| 659 | } |
| 660 | |
| 661 | // lock on mToastQueue |
| 662 | private void keepProcessAliveLocked(int pid) |
| 663 | { |
| 664 | int toastCount = 0; // toasts from this pid |
| 665 | ArrayList<ToastRecord> list = mToastQueue; |
| 666 | int N = list.size(); |
| 667 | for (int i=0; i<N; i++) { |
| 668 | ToastRecord r = list.get(i); |
| 669 | if (r.pid == pid) { |
| 670 | toastCount++; |
| 671 | } |
| 672 | } |
| 673 | try { |
| 674 | mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0); |
| 675 | } catch (RemoteException e) { |
| 676 | // Shouldn't happen. |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | private final class WorkerHandler extends Handler |
| 681 | { |
| 682 | @Override |
| 683 | public void handleMessage(Message msg) |
| 684 | { |
| 685 | switch (msg.what) |
| 686 | { |
| 687 | case MESSAGE_TIMEOUT: |
| 688 | handleTimeout((ToastRecord)msg.obj); |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | |
| 695 | // Notifications |
| 696 | // ============================================================================ |
| 697 | public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut) |
| 698 | { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 699 | enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut); |
| 700 | } |
| 701 | |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 702 | public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification, |
| 703 | int[] idOut) |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 704 | { |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 705 | enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(), |
| 706 | tag, id, notification, idOut); |
| 707 | } |
| 708 | |
| 709 | // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the |
| 710 | // uid/pid of another application) |
| 711 | public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid, |
| 712 | String tag, int id, Notification notification, int[] idOut) |
| 713 | { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 714 | checkIncomingCall(pkg); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 715 | |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 716 | // Limit the number of notifications that any given package except the android |
| 717 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| 718 | if (!"android".equals(pkg)) { |
| 719 | synchronized (mNotificationList) { |
| 720 | int count = 0; |
| 721 | final int N = mNotificationList.size(); |
| 722 | for (int i=0; i<N; i++) { |
| 723 | final NotificationRecord r = mNotificationList.get(i); |
| 724 | if (r.pkg.equals(pkg)) { |
| 725 | count++; |
| 726 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 727 | Slog.e(TAG, "Package has already posted " + count |
| 728 | + " notifications. Not showing more. package=" + pkg); |
| 729 | return; |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 736 | // This conditional is a dirty hack to limit the logging done on |
| 737 | // behalf of the download manager without affecting other apps. |
| 738 | if (!pkg.equals("com.android.providers.downloads") |
| 739 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 740 | EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | if (pkg == null || notification == null) { |
| 744 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 745 | + " id=" + id + " notification=" + notification); |
| 746 | } |
| 747 | if (notification.icon != 0) { |
| 748 | if (notification.contentView == null) { |
| 749 | throw new IllegalArgumentException("contentView required: pkg=" + pkg |
| 750 | + " id=" + id + " notification=" + notification); |
| 751 | } |
| 752 | if (notification.contentIntent == null) { |
| 753 | throw new IllegalArgumentException("contentIntent required: pkg=" + pkg |
| 754 | + " id=" + id + " notification=" + notification); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | synchronized (mNotificationList) { |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 759 | NotificationRecord r = new NotificationRecord(pkg, tag, id, |
| 760 | callingUid, callingPid, notification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 761 | NotificationRecord old = null; |
| 762 | |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 763 | int index = indexOfNotificationLocked(pkg, tag, id); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | if (index < 0) { |
| 765 | mNotificationList.add(r); |
| 766 | } else { |
| 767 | old = mNotificationList.remove(index); |
| 768 | mNotificationList.add(index, r); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 769 | // Make sure we don't lose the foreground service state. |
| 770 | if (old != null) { |
| 771 | notification.flags |= |
| 772 | old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE; |
| 773 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 774 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 775 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 776 | // Ensure if this is a foreground service that the proper additional |
| 777 | // flags are set. |
| 778 | if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 779 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 780 | | Notification.FLAG_NO_CLEAR; |
| 781 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 782 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 783 | if (notification.icon != 0) { |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 784 | StatusBarNotification n = new StatusBarNotification(pkg, id, tag, |
| 785 | r.uid, r.initialPid, notification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 786 | if (old != null && old.statusBarKey != null) { |
| 787 | r.statusBarKey = old.statusBarKey; |
| 788 | long identity = Binder.clearCallingIdentity(); |
| 789 | try { |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 790 | mStatusBar.updateNotification(r.statusBarKey, n); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | } |
| 792 | finally { |
| 793 | Binder.restoreCallingIdentity(identity); |
| 794 | } |
| 795 | } else { |
| 796 | long identity = Binder.clearCallingIdentity(); |
| 797 | try { |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 798 | r.statusBarKey = mStatusBar.addNotification(n); |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 799 | mAttentionLight.pulse(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | } |
| 801 | finally { |
| 802 | Binder.restoreCallingIdentity(identity); |
| 803 | } |
| 804 | } |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 805 | sendAccessibilityEvent(notification, pkg); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 806 | } else { |
| 807 | if (old != null && old.statusBarKey != null) { |
| 808 | long identity = Binder.clearCallingIdentity(); |
| 809 | try { |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 810 | mStatusBar.removeNotification(old.statusBarKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | } |
| 812 | finally { |
| 813 | Binder.restoreCallingIdentity(identity); |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | // If we're not supposed to beep, vibrate, etc. then don't. |
| 819 | if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0) |
| 820 | && (!(old != null |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 821 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 )) |
| 822 | && mSystemReady) { |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 823 | |
| 824 | final AudioManager audioManager = (AudioManager) mContext |
| 825 | .getSystemService(Context.AUDIO_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | // sound |
| 827 | final boolean useDefaultSound = |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 828 | (notification.defaults & Notification.DEFAULT_SOUND) != 0; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | if (useDefaultSound || notification.sound != null) { |
| 830 | Uri uri; |
| 831 | if (useDefaultSound) { |
| 832 | uri = Settings.System.DEFAULT_NOTIFICATION_URI; |
| 833 | } else { |
| 834 | uri = notification.sound; |
| 835 | } |
| 836 | boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0; |
| 837 | int audioStreamType; |
| 838 | if (notification.audioStreamType >= 0) { |
| 839 | audioStreamType = notification.audioStreamType; |
| 840 | } else { |
| 841 | audioStreamType = DEFAULT_STREAM_TYPE; |
| 842 | } |
| 843 | mSoundNotification = r; |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 844 | // do not play notifications if stream volume is 0 |
| 845 | // (typically because ringer mode is silent). |
| 846 | if (audioManager.getStreamVolume(audioStreamType) != 0) { |
| 847 | long identity = Binder.clearCallingIdentity(); |
| 848 | try { |
| 849 | mSound.play(mContext, uri, looping, audioStreamType); |
| 850 | } |
| 851 | finally { |
| 852 | Binder.restoreCallingIdentity(identity); |
| 853 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | |
| 857 | // vibrate |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | final boolean useDefaultVibrate = |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 859 | (notification.defaults & Notification.DEFAULT_VIBRATE) != 0; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 860 | if ((useDefaultVibrate || notification.vibrate != null) |
| 861 | && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) { |
| 862 | mVibrateNotification = r; |
| 863 | |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 864 | mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 865 | : notification.vibrate, |
| 866 | ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | // this option doesn't shut off the lights |
| 871 | |
| 872 | // light |
| 873 | // the most recent thing gets the light |
| 874 | mLights.remove(old); |
| 875 | if (mLedNotification == old) { |
| 876 | mLedNotification = null; |
| 877 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 878 | //Slog.i(TAG, "notification.lights=" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0)); |
| 880 | if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) { |
| 881 | mLights.add(r); |
| 882 | updateLightsLocked(); |
| 883 | } else { |
| 884 | if (old != null |
| 885 | && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) { |
| 886 | updateLightsLocked(); |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | idOut[0] = id; |
| 892 | } |
| 893 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 894 | private void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 895 | AccessibilityManager manager = AccessibilityManager.getInstance(mContext); |
| 896 | if (!manager.isEnabled()) { |
| 897 | return; |
| 898 | } |
| 899 | |
| 900 | AccessibilityEvent event = |
| 901 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 902 | event.setPackageName(packageName); |
| 903 | event.setClassName(Notification.class.getName()); |
| 904 | event.setParcelableData(notification); |
| 905 | CharSequence tickerText = notification.tickerText; |
| 906 | if (!TextUtils.isEmpty(tickerText)) { |
| 907 | event.getText().add(tickerText); |
| 908 | } |
| 909 | |
| 910 | manager.sendAccessibilityEvent(event); |
| 911 | } |
| 912 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | private void cancelNotificationLocked(NotificationRecord r) { |
| 914 | // status bar |
| 915 | if (r.notification.icon != 0) { |
| 916 | long identity = Binder.clearCallingIdentity(); |
| 917 | try { |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 918 | mStatusBar.removeNotification(r.statusBarKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | } |
| 920 | finally { |
| 921 | Binder.restoreCallingIdentity(identity); |
| 922 | } |
| 923 | r.statusBarKey = null; |
| 924 | } |
| 925 | |
| 926 | // sound |
| 927 | if (mSoundNotification == r) { |
| 928 | mSoundNotification = null; |
| 929 | long identity = Binder.clearCallingIdentity(); |
| 930 | try { |
| 931 | mSound.stop(); |
| 932 | } |
| 933 | finally { |
| 934 | Binder.restoreCallingIdentity(identity); |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | // vibrate |
| 939 | if (mVibrateNotification == r) { |
| 940 | mVibrateNotification = null; |
| 941 | long identity = Binder.clearCallingIdentity(); |
| 942 | try { |
| 943 | mVibrator.cancel(); |
| 944 | } |
| 945 | finally { |
| 946 | Binder.restoreCallingIdentity(identity); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | // light |
| 951 | mLights.remove(r); |
| 952 | if (mLedNotification == r) { |
| 953 | mLedNotification = null; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | /** |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 958 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 959 | * and none of the {@code mustNotHaveFlags}. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | */ |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 961 | private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags, |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 962 | int mustNotHaveFlags) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 963 | EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | |
| 965 | synchronized (mNotificationList) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 966 | int index = indexOfNotificationLocked(pkg, tag, id); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | if (index >= 0) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 968 | NotificationRecord r = mNotificationList.get(index); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 969 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 970 | if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) { |
| 971 | return; |
| 972 | } |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 973 | if ((r.notification.flags & mustNotHaveFlags) != 0) { |
| 974 | return; |
| 975 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 976 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | mNotificationList.remove(index); |
| 978 | |
| 979 | cancelNotificationLocked(r); |
| 980 | updateLightsLocked(); |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | /** |
| 986 | * Cancels all notifications from a given package that have all of the |
| 987 | * {@code mustHaveFlags}. |
| 988 | */ |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 989 | boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags, |
| 990 | int mustNotHaveFlags, boolean doit) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 991 | EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | |
| 993 | synchronized (mNotificationList) { |
| 994 | final int N = mNotificationList.size(); |
| 995 | boolean canceledSomething = false; |
| 996 | for (int i = N-1; i >= 0; --i) { |
| 997 | NotificationRecord r = mNotificationList.get(i); |
| 998 | if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) { |
| 999 | continue; |
| 1000 | } |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1001 | if ((r.notification.flags & mustNotHaveFlags) != 0) { |
| 1002 | continue; |
| 1003 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | if (!r.pkg.equals(pkg)) { |
| 1005 | continue; |
| 1006 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1007 | canceledSomething = true; |
| 1008 | if (!doit) { |
| 1009 | return true; |
| 1010 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | mNotificationList.remove(i); |
| 1012 | cancelNotificationLocked(r); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | } |
| 1014 | if (canceledSomething) { |
| 1015 | updateLightsLocked(); |
| 1016 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1017 | return canceledSomething; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1021 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1022 | public void cancelNotification(String pkg, int id) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1023 | cancelNotificationWithTag(pkg, null /* tag */, id); |
| 1024 | } |
| 1025 | |
| 1026 | public void cancelNotificationWithTag(String pkg, String tag, int id) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1027 | checkIncomingCall(pkg); |
| 1028 | // Don't allow client applications to cancel foreground service notis. |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1029 | cancelNotification(pkg, tag, id, 0, |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1030 | Binder.getCallingUid() == Process.SYSTEM_UID |
| 1031 | ? 0 : Notification.FLAG_FOREGROUND_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1034 | public void cancelAllNotifications(String pkg) { |
| 1035 | checkIncomingCall(pkg); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1036 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1037 | // Calling from user space, don't allow the canceling of actively |
| 1038 | // running foreground services. |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1039 | cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1042 | void checkIncomingCall(String pkg) { |
| 1043 | int uid = Binder.getCallingUid(); |
| 1044 | if (uid == Process.SYSTEM_UID || uid == 0) { |
| 1045 | return; |
| 1046 | } |
| 1047 | try { |
| 1048 | ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo( |
| 1049 | pkg, 0); |
| 1050 | if (ai.uid != uid) { |
| 1051 | throw new SecurityException("Calling uid " + uid + " gave package" |
| 1052 | + pkg + " which is owned by uid " + ai.uid); |
| 1053 | } |
| 1054 | } catch (PackageManager.NameNotFoundException e) { |
| 1055 | throw new SecurityException("Unknown package " + pkg); |
| 1056 | } |
| 1057 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1058 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1059 | void cancelAll() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1060 | synchronized (mNotificationList) { |
| 1061 | final int N = mNotificationList.size(); |
| 1062 | for (int i=N-1; i>=0; i--) { |
| 1063 | NotificationRecord r = mNotificationList.get(i); |
| 1064 | |
| 1065 | if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT |
| 1066 | | Notification.FLAG_NO_CLEAR)) == 0) { |
| 1067 | if (r.notification.deleteIntent != null) { |
| 1068 | try { |
| 1069 | r.notification.deleteIntent.send(); |
| 1070 | } catch (PendingIntent.CanceledException ex) { |
| 1071 | // do nothing - there's no relevant way to recover, and |
| 1072 | // no reason to let this propagate |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1073 | Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | mNotificationList.remove(i); |
| 1077 | cancelNotificationLocked(r); |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | updateLightsLocked(); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | private void updateLights() { |
| 1086 | synchronized (mNotificationList) { |
| 1087 | updateLightsLocked(); |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | // lock on mNotificationList |
| 1092 | private void updateLightsLocked() |
| 1093 | { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1094 | // Battery low always shows, other states only show if charging. |
| 1095 | if (mBatteryLow) { |
| Mike Lockwood | 445f430 | 2009-09-04 11:06:46 -0400 | [diff] [blame] | 1096 | if (mBatteryCharging) { |
| Kenneth Andersson | d5d87b2 | 2010-08-03 13:30:18 +0200 | [diff] [blame] | 1097 | mBatteryLight.setColor(mBatteryLowARGB); |
| Mike Lockwood | 445f430 | 2009-09-04 11:06:46 -0400 | [diff] [blame] | 1098 | } else { |
| 1099 | // Flash when battery is low and not charging |
| Kenneth Andersson | d5d87b2 | 2010-08-03 13:30:18 +0200 | [diff] [blame] | 1100 | mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED, |
| 1101 | mBatteryLedOn, mBatteryLedOff); |
| Mike Lockwood | 445f430 | 2009-09-04 11:06:46 -0400 | [diff] [blame] | 1102 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1103 | } else if (mBatteryCharging) { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1104 | if (mBatteryFull) { |
| Kenneth Andersson | d5d87b2 | 2010-08-03 13:30:18 +0200 | [diff] [blame] | 1105 | mBatteryLight.setColor(mBatteryFullARGB); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1106 | } else { |
| Kenneth Andersson | d5d87b2 | 2010-08-03 13:30:18 +0200 | [diff] [blame] | 1107 | mBatteryLight.setColor(mBatteryMediumARGB); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | } |
| 1109 | } else { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1110 | mBatteryLight.turnOff(); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1111 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | |
| Mike Lockwood | 2117f6f | 2010-09-09 09:48:08 -0400 | [diff] [blame] | 1113 | // clear pending pulse notification if screen is on |
| 1114 | if (mScreenOn || mLedNotification == null) { |
| 1115 | mPendingPulseNotification = false; |
| 1116 | } |
| 1117 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1118 | // handle notification lights |
| 1119 | if (mLedNotification == null) { |
| 1120 | // get next notification, if any |
| 1121 | int n = mLights.size(); |
| 1122 | if (n > 0) { |
| 1123 | mLedNotification = mLights.get(n-1); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1124 | } |
| Mike Lockwood | 2117f6f | 2010-09-09 09:48:08 -0400 | [diff] [blame] | 1125 | if (mLedNotification != null && !mScreenOn) { |
| 1126 | mPendingPulseNotification = true; |
| 1127 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1128 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1129 | |
| 1130 | // we only flash if screen is off and persistent pulsing is enabled |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1131 | // and we are not currently in a call |
| Mike Lockwood | 2117f6f | 2010-09-09 09:48:08 -0400 | [diff] [blame] | 1132 | if (!mPendingPulseNotification || mScreenOn || mInCall) { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1133 | mNotificationLight.turnOff(); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1134 | } else { |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 1135 | int ledARGB = mLedNotification.notification.ledARGB; |
| 1136 | int ledOnMS = mLedNotification.notification.ledOnMS; |
| 1137 | int ledOffMS = mLedNotification.notification.ledOffMS; |
| 1138 | if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) { |
| 1139 | ledARGB = mDefaultNotificationColor; |
| 1140 | ledOnMS = mDefaultNotificationLedOn; |
| 1141 | ledOffMS = mDefaultNotificationLedOff; |
| 1142 | } |
| 1143 | if (mNotificationPulseEnabled) { |
| 1144 | // pulse repeatedly |
| 1145 | mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED, |
| 1146 | ledOnMS, ledOffMS); |
| 1147 | } else { |
| 1148 | // pulse only once |
| 1149 | mNotificationLight.pulse(ledARGB, ledOnMS); |
| 1150 | } |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1151 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | // lock on mNotificationList |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1155 | private int indexOfNotificationLocked(String pkg, String tag, int id) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1156 | { |
| 1157 | ArrayList<NotificationRecord> list = mNotificationList; |
| 1158 | final int len = list.size(); |
| 1159 | for (int i=0; i<len; i++) { |
| 1160 | NotificationRecord r = list.get(i); |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1161 | if (tag == null) { |
| 1162 | if (r.tag != null) { |
| 1163 | continue; |
| 1164 | } |
| 1165 | } else { |
| 1166 | if (!tag.equals(r.tag)) { |
| 1167 | continue; |
| 1168 | } |
| 1169 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1170 | if (r.id == id && r.pkg.equals(pkg)) { |
| 1171 | return i; |
| 1172 | } |
| 1173 | } |
| 1174 | return -1; |
| 1175 | } |
| 1176 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1177 | // This is here instead of StatusBarPolicy because it is an important |
| 1178 | // security feature that we don't want people customizing the platform |
| 1179 | // to accidentally lose. |
| Mike Lockwood | ff2544c | 2010-06-28 09:17:50 -0400 | [diff] [blame] | 1180 | private void updateAdbNotification(boolean adbEnabled) { |
| 1181 | if (adbEnabled) { |
| Mike Lockwood | ed76037 | 2009-07-09 07:07:27 -0400 | [diff] [blame] | 1182 | if ("0".equals(SystemProperties.get("persist.adb.notify"))) { |
| 1183 | return; |
| 1184 | } |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1185 | if (!mAdbNotificationShown) { |
| 1186 | NotificationManager notificationManager = (NotificationManager) mContext |
| 1187 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 1188 | if (notificationManager != null) { |
| 1189 | Resources r = mContext.getResources(); |
| 1190 | CharSequence title = r.getText( |
| 1191 | com.android.internal.R.string.adb_active_notification_title); |
| 1192 | CharSequence message = r.getText( |
| 1193 | com.android.internal.R.string.adb_active_notification_message); |
| 1194 | |
| 1195 | if (mAdbNotification == null) { |
| 1196 | mAdbNotification = new Notification(); |
| Daniel Sandler | 39576c8 | 2010-03-25 16:02:33 -0400 | [diff] [blame] | 1197 | mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1198 | mAdbNotification.when = 0; |
| 1199 | mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1200 | mAdbNotification.tickerText = title; |
| Daniel Sandler | 39576c8 | 2010-03-25 16:02:33 -0400 | [diff] [blame] | 1201 | mAdbNotification.defaults = 0; // please be quiet |
| 1202 | mAdbNotification.sound = null; |
| 1203 | mAdbNotification.vibrate = null; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | Intent intent = new Intent( |
| 1207 | Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS); |
| 1208 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | |
| 1209 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 1210 | // Note: we are hard-coding the component because this is |
| 1211 | // an important security UI that we don't want anyone |
| 1212 | // intercepting. |
| 1213 | intent.setComponent(new ComponentName("com.android.settings", |
| 1214 | "com.android.settings.DevelopmentSettings")); |
| 1215 | PendingIntent pi = PendingIntent.getActivity(mContext, 0, |
| 1216 | intent, 0); |
| 1217 | |
| 1218 | mAdbNotification.setLatestEventInfo(mContext, title, message, pi); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1219 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1220 | mAdbNotificationShown = true; |
| 1221 | notificationManager.notify( |
| 1222 | com.android.internal.R.string.adb_active_notification_title, |
| 1223 | mAdbNotification); |
| 1224 | } |
| 1225 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1226 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1227 | } else if (mAdbNotificationShown) { |
| 1228 | NotificationManager notificationManager = (NotificationManager) mContext |
| 1229 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 1230 | if (notificationManager != null) { |
| 1231 | mAdbNotificationShown = false; |
| 1232 | notificationManager.cancel( |
| 1233 | com.android.internal.R.string.adb_active_notification_title); |
| 1234 | } |
| 1235 | } |
| 1236 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1237 | |
| 1238 | private void updateNotificationPulse() { |
| 1239 | synchronized (mNotificationList) { |
| 1240 | updateLightsLocked(); |
| 1241 | } |
| 1242 | } |
| 1243 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1244 | // ====================================================================== |
| 1245 | @Override |
| 1246 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1247 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1248 | != PackageManager.PERMISSION_GRANTED) { |
| 1249 | pw.println("Permission Denial: can't dump NotificationManager from from pid=" |
| 1250 | + Binder.getCallingPid() |
| 1251 | + ", uid=" + Binder.getCallingUid()); |
| 1252 | return; |
| 1253 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1254 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | pw.println("Current Notification Manager state:"); |
| 1256 | |
| 1257 | int N; |
| 1258 | |
| 1259 | synchronized (mToastQueue) { |
| 1260 | N = mToastQueue.size(); |
| 1261 | if (N > 0) { |
| 1262 | pw.println(" Toast Queue:"); |
| 1263 | for (int i=0; i<N; i++) { |
| 1264 | mToastQueue.get(i).dump(pw, " "); |
| 1265 | } |
| 1266 | pw.println(" "); |
| 1267 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1268 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | synchronized (mNotificationList) { |
| 1272 | N = mNotificationList.size(); |
| 1273 | if (N > 0) { |
| 1274 | pw.println(" Notification List:"); |
| 1275 | for (int i=0; i<N; i++) { |
| 1276 | mNotificationList.get(i).dump(pw, " ", mContext); |
| 1277 | } |
| 1278 | pw.println(" "); |
| 1279 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1280 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1281 | N = mLights.size(); |
| 1282 | if (N > 0) { |
| 1283 | pw.println(" Lights List:"); |
| 1284 | for (int i=0; i<N; i++) { |
| 1285 | mLights.get(i).dump(pw, " ", mContext); |
| 1286 | } |
| 1287 | pw.println(" "); |
| 1288 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1289 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1290 | pw.println(" mSoundNotification=" + mSoundNotification); |
| 1291 | pw.println(" mSound=" + mSound); |
| 1292 | pw.println(" mVibrateNotification=" + mVibrateNotification); |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1293 | pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications)); |
| 1294 | pw.println(" mSystemReady=" + mSystemReady); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | } |
| 1296 | } |
| 1297 | } |