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