blob: d121653e4e257c7997ff9010577561bd861de78c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jeff Sharkey098d5802012-04-26 17:30:34 -070019import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
20import static org.xmlpull.v1.XmlPullParser.END_TAG;
21import static org.xmlpull.v1.XmlPullParser.START_TAG;
svetoslavganov75986cf2009-05-14 22:28:01 -070022
Dianne Hackborn41203752012-08-31 14:05:51 -070023import android.app.ActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.app.ActivityManagerNative;
Amith Yamasanif203aee2012-08-29 18:41:53 -070025import android.app.AppGlobals;
Daniel Sandler4a900acd2013-01-30 14:04:10 -050026import android.app.AppOpsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.app.IActivityManager;
28import android.app.INotificationManager;
29import android.app.ITransientNotification;
30import android.app.Notification;
31import android.app.PendingIntent;
32import android.app.StatusBarManager;
33import android.content.BroadcastReceiver;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070034import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.Context;
36import android.content.Intent;
37import android.content.IntentFilter;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070038import android.content.pm.ApplicationInfo;
Daniel Sandler4a900acd2013-01-30 14:04:10 -050039import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.pm.PackageManager;
41import android.content.pm.PackageManager.NameNotFoundException;
42import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070043import android.database.ContentObserver;
svetoslavganov75986cf2009-05-14 22:28:01 -070044import android.media.AudioManager;
Jeff Sharkey098d5802012-04-26 17:30:34 -070045import android.media.IAudioService;
46import android.media.IRingtonePlayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.os.IBinder;
51import android.os.Message;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070052import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070053import android.os.RemoteException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070054import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070055import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.Vibrator;
57import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050058import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.text.TextUtils;
Dianne Hackborn39606a02012-07-31 17:54:35 -070060import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.util.EventLog;
62import android.util.Log;
Andy Stadler110988c2010-12-03 14:29:16 -080063import android.util.Slog;
Daniel Sandler0da673f2012-04-11 12:33:16 -040064import android.util.Xml;
svetoslavganov75986cf2009-05-14 22:28:01 -070065import android.view.accessibility.AccessibilityEvent;
66import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.widget.Toast;
68
Jeff Sharkey098d5802012-04-26 17:30:34 -070069import com.android.internal.statusbar.StatusBarNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -070070
71import org.xmlpull.v1.XmlPullParser;
72import org.xmlpull.v1.XmlPullParserException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070073
Daniel Sandler0da673f2012-04-11 12:33:16 -040074import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import java.io.FileDescriptor;
Daniel Sandler0da673f2012-04-11 12:33:16 -040076import java.io.FileInputStream;
77import java.io.FileNotFoundException;
Daniel Sandler0da673f2012-04-11 12:33:16 -040078import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import java.io.PrintWriter;
80import java.util.ArrayList;
81import java.util.Arrays;
Daniel Sandler0da673f2012-04-11 12:33:16 -040082import java.util.HashSet;
83
84import libcore.io.IoUtils;
85
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040087/** {@hide} */
88public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089{
90 private static final String TAG = "NotificationService";
91 private static final boolean DBG = false;
92
Joe Onoratobd73d012010-06-04 11:44:54 -070093 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
94
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 // message codes
96 private static final int MESSAGE_TIMEOUT = 2;
97
98 private static final int LONG_DELAY = 3500; // 3.5 seconds
99 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800100
101 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
Daniel Sandleredbb3802012-11-13 20:49:47 -0800102 private static final int VIBRATE_PATTERN_MAXLEN = 8 * 2 + 1; // up to eight bumps
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
104 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400105 private static final boolean SCORE_ONGOING_HIGHER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
Daniel Sandler0da673f2012-04-11 12:33:16 -0400107 private static final int JUNK_SCORE = -1000;
108 private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10;
109 private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER;
110
Daniel Sandler526fa0e2012-12-04 14:51:50 -0500111 // Notifications with scores below this will not interrupt the user, either via LED or
112 // sound or vibration
113 private static final int SCORE_INTERRUPTION_THRESHOLD =
114 Notification.PRIORITY_LOW * NOTIFICATION_PRIORITY_MULTIPLIER;
115
Daniel Sandler0da673f2012-04-11 12:33:16 -0400116 private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true;
117 private static final boolean ENABLE_BLOCKED_TOASTS = true;
118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 final Context mContext;
120 final IActivityManager mAm;
121 final IBinder mForegroundToken = new Binder();
122
123 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -0700124 private StatusBarManagerService mStatusBar;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500125 private LightsService.Light mNotificationLight;
126 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
Mike Lockwood670f9322010-01-20 12:13:36 -0500128 private int mDefaultNotificationColor;
129 private int mDefaultNotificationLedOn;
130 private int mDefaultNotificationLedOff;
131
Daniel Sandleredbb3802012-11-13 20:49:47 -0800132 private long[] mDefaultVibrationPattern;
133 private long[] mFallbackVibrationPattern;
134
Joe Onorato30275482009-07-08 17:09:14 -0700135 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400136 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
Jeff Sharkey098d5802012-04-26 17:30:34 -0700138 private NotificationRecord mSoundNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 private NotificationRecord mVibrateNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700140
141 private IAudioService mAudioService;
Jeff Brownc2346132012-04-13 01:55:38 -0700142 private Vibrator mVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500144 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400145 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500146 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500147 private boolean mNotificationPulseEnabled;
148
Fred Quintana6ecaff12009-09-25 14:23:13 -0700149 private final ArrayList<NotificationRecord> mNotificationList =
150 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151
152 private ArrayList<ToastRecord> mToastQueue;
153
154 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700156
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500157 private final AppOpsManager mAppOps;
158
Daniel Sandler0da673f2012-04-11 12:33:16 -0400159 // Notification control database. For now just contains disabled packages.
160 private AtomicFile mPolicyFile;
161 private HashSet<String> mBlockedPackages = new HashSet<String>();
162
163 private static final int DB_VERSION = 1;
164
165 private static final String TAG_BODY = "notification-policy";
166 private static final String ATTR_VERSION = "version";
167
168 private static final String TAG_BLOCKED_PKGS = "blocked-packages";
169 private static final String TAG_PACKAGE = "package";
170 private static final String ATTR_NAME = "name";
171
172 private void loadBlockDb() {
173 synchronized(mBlockedPackages) {
174 if (mPolicyFile == null) {
175 File dir = new File("/data/system");
176 mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
177
178 mBlockedPackages.clear();
179
180 FileInputStream infile = null;
181 try {
182 infile = mPolicyFile.openRead();
183 final XmlPullParser parser = Xml.newPullParser();
184 parser.setInput(infile, null);
185
186 int type;
187 String tag;
188 int version = DB_VERSION;
189 while ((type = parser.next()) != END_DOCUMENT) {
190 tag = parser.getName();
191 if (type == START_TAG) {
192 if (TAG_BODY.equals(tag)) {
193 version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
194 } else if (TAG_BLOCKED_PKGS.equals(tag)) {
195 while ((type = parser.next()) != END_DOCUMENT) {
196 tag = parser.getName();
197 if (TAG_PACKAGE.equals(tag)) {
198 mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
199 } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
200 break;
201 }
202 }
203 }
204 }
205 }
206 } catch (FileNotFoundException e) {
207 // No data yet
208 } catch (IOException e) {
209 Log.wtf(TAG, "Unable to read blocked notifications database", e);
210 } catch (NumberFormatException e) {
211 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
212 } catch (XmlPullParserException e) {
213 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
214 } finally {
215 IoUtils.closeQuietly(infile);
216 }
217 }
218 }
219 }
220
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500221 /**
222 * Use this when you just want to know if notifications are OK for this package.
223 */
224 public boolean areNotificationsEnabledForPackage(String pkg, int uid) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400225 checkCallerIsSystem();
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500226 return (mAppOps.checkOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
227 == AppOpsManager.MODE_ALLOWED);
Daniel Sandler0da673f2012-04-11 12:33:16 -0400228 }
229
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500230 /** Use this when you actually want to post a notification or toast.
231 *
232 * Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
233 */
234 private boolean noteNotificationOp(String pkg, int uid) {
235 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
236 != AppOpsManager.MODE_ALLOWED) {
237 Slog.v(TAG, "notifications are disabled by AppOps for " + pkg);
238 return false;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400239 }
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500240 return true;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400241 }
242
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500243 public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400244 checkCallerIsSystem();
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500245 if (true||DBG) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400246 Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
247 }
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500248 mAppOps.setMode(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg,
249 enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED);
Daniel Sandler0da673f2012-04-11 12:33:16 -0400250 }
251
252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 private static String idDebugString(Context baseContext, String packageName, int id) {
254 Context c = null;
255
256 if (packageName != null) {
257 try {
258 c = baseContext.createPackageContext(packageName, 0);
259 } catch (NameNotFoundException e) {
260 c = baseContext;
261 }
262 } else {
263 c = baseContext;
264 }
265
266 String pkg;
267 String type;
268 String name;
269
270 Resources r = c.getResources();
271 try {
272 return r.getResourceName(id);
273 } catch (Resources.NotFoundException e) {
274 return "<name unknown>";
275 }
276 }
277
278 private static final class NotificationRecord
279 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700280 final String pkg;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800281 final String basePkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700282 final String tag;
283 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700284 final int uid;
285 final int initialPid;
Dianne Hackborn41203752012-08-31 14:05:51 -0700286 final int userId;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700287 final Notification notification;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500288 final int score;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 IBinder statusBarKey;
290
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800291 NotificationRecord(String pkg, String basePkg, String tag, int id, int uid, int initialPid,
Dianne Hackborn41203752012-08-31 14:05:51 -0700292 int userId, int score, Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 {
294 this.pkg = pkg;
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800295 this.basePkg = basePkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700296 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700298 this.uid = uid;
299 this.initialPid = initialPid;
Dianne Hackborn41203752012-08-31 14:05:51 -0700300 this.userId = userId;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500301 this.score = score;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 this.notification = notification;
303 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 void dump(PrintWriter pw, String prefix, Context baseContext) {
306 pw.println(prefix + this);
307 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
308 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500309 pw.println(prefix + " pri=" + notification.priority);
310 pw.println(prefix + " score=" + this.score);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 pw.println(prefix + " contentIntent=" + notification.contentIntent);
312 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
313 pw.println(prefix + " tickerText=" + notification.tickerText);
314 pw.println(prefix + " contentView=" + notification.contentView);
Dianne Hackborn41203752012-08-31 14:05:51 -0700315 pw.println(prefix + " uid=" + uid + " userId=" + userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
317 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
318 pw.println(prefix + " sound=" + notification.sound);
319 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
320 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
321 + " ledOnMS=" + notification.ledOnMS
322 + " ledOffMS=" + notification.ledOffMS);
323 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 @Override
326 public final String toString()
327 {
328 return "NotificationRecord{"
329 + Integer.toHexString(System.identityHashCode(this))
330 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700331 + " id=" + Integer.toHexString(id)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500332 + " tag=" + tag
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500333 + " score=" + score
Daniel Sandlere40451a2011-02-03 14:51:35 -0500334 + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 }
336 }
337
338 private static final class ToastRecord
339 {
340 final int pid;
341 final String pkg;
342 final ITransientNotification callback;
343 int duration;
344
345 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
346 {
347 this.pid = pid;
348 this.pkg = pkg;
349 this.callback = callback;
350 this.duration = duration;
351 }
352
353 void update(int duration) {
354 this.duration = duration;
355 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 void dump(PrintWriter pw, String prefix) {
358 pw.println(prefix + this);
359 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 @Override
362 public final String toString()
363 {
364 return "ToastRecord{"
365 + Integer.toHexString(System.identityHashCode(this))
366 + " pkg=" + pkg
367 + " callback=" + callback
368 + " duration=" + duration;
369 }
370 }
371
Joe Onorato089de882010-04-12 08:18:45 -0700372 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
373 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374
375 public void onSetDisabled(int status) {
376 synchronized (mNotificationList) {
377 mDisabledNotifications = status;
378 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
379 // cancel whatever's going on
380 long identity = Binder.clearCallingIdentity();
381 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700382 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
383 if (player != null) {
384 player.stopAsync();
385 }
386 } catch (RemoteException e) {
387 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 Binder.restoreCallingIdentity(identity);
389 }
390
391 identity = Binder.clearCallingIdentity();
392 try {
393 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700394 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 Binder.restoreCallingIdentity(identity);
396 }
397 }
398 }
399 }
400
401 public void onClearAll() {
Dianne Hackborn41203752012-08-31 14:05:51 -0700402 // XXX to be totally correct, the caller should tell us which user
403 // this is for.
404 cancelAll(ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 }
406
Fred Quintana6ecaff12009-09-25 14:23:13 -0700407 public void onNotificationClick(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700408 // XXX to be totally correct, the caller should tell us which user
409 // this is for.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700410 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackborn41203752012-08-31 14:05:51 -0700411 Notification.FLAG_FOREGROUND_SERVICE, false,
412 ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 }
414
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400415 public void onNotificationClear(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700416 // XXX to be totally correct, the caller should tell us which user
417 // this is for.
Joe Onorato46439ce2010-11-19 13:56:21 -0800418 cancelNotification(pkg, tag, id, 0,
419 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
Dianne Hackborn41203752012-08-31 14:05:51 -0700420 true, ActivityManager.getCurrentUser());
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400421 }
422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 public void onPanelRevealed() {
424 synchronized (mNotificationList) {
425 // sound
426 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 long identity = Binder.clearCallingIdentity();
429 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700430 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
431 if (player != null) {
432 player.stopAsync();
433 }
434 } catch (RemoteException e) {
435 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 Binder.restoreCallingIdentity(identity);
437 }
438
439 // vibrate
440 mVibrateNotification = null;
441 identity = Binder.clearCallingIdentity();
442 try {
443 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700444 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 Binder.restoreCallingIdentity(identity);
446 }
447
448 // light
449 mLights.clear();
450 mLedNotification = null;
451 updateLightsLocked();
452 }
453 }
Joe Onorato005847b2010-06-04 16:08:02 -0400454
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700455 public void onNotificationError(String pkg, String tag, int id,
456 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400457 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
458 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Dianne Hackborn41203752012-08-31 14:05:51 -0700459 // XXX to be totally correct, the caller should tell us which user
460 // this is for.
461 cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid));
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700462 long ident = Binder.clearCallingIdentity();
463 try {
464 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
465 "Bad notification posted from package " + pkg
466 + ": " + message);
467 } catch (RemoteException e) {
468 }
469 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 };
472
473 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
474 @Override
475 public void onReceive(Context context, Intent intent) {
476 String action = intent.getAction();
477
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800478 boolean queryRestart = false;
Daniel Sandler26ece572012-06-01 15:38:46 -0400479 boolean packageChanged = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800480
Mike Lockwood541c9942011-06-12 19:35:45 -0400481 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800482 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandler26ece572012-06-01 15:38:46 -0400483 || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800484 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800485 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800486 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800487 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800488 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800489 } else if (queryRestart) {
490 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800491 } else {
492 Uri uri = intent.getData();
493 if (uri == null) {
494 return;
495 }
496 String pkgName = uri.getSchemeSpecificPart();
497 if (pkgName == null) {
498 return;
499 }
Daniel Sandler26ece572012-06-01 15:38:46 -0400500 if (packageChanged) {
501 // We cancel notifications for packages which have just been disabled
502 final int enabled = mContext.getPackageManager()
503 .getApplicationEnabledSetting(pkgName);
504 if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
505 || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
506 return;
507 }
508 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800509 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800511 if (pkgList != null && (pkgList.length > 0)) {
512 for (String pkgName : pkgList) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700513 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart,
514 UserHandle.USER_ALL);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400517 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
518 // Keep track of screen on/off state, but do not turn off the notification light
519 // until user passes through the lock screen or views the notification.
520 mScreenOn = true;
521 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
522 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500523 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400524 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
525 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500526 updateNotificationPulse();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700527 } else if (action.equals(Intent.ACTION_USER_STOPPED)) {
528 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
529 if (userHandle >= 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700530 cancelAllNotificationsInt(null, 0, 0, true, userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700531 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400532 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
533 // turn off LED when user passes through lock screen
534 mNotificationLight.turnOff();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
536 }
537 };
538
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700539 class SettingsObserver extends ContentObserver {
540 SettingsObserver(Handler handler) {
541 super(handler);
542 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800543
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700544 void observe() {
545 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500546 resolver.registerContentObserver(Settings.System.getUriFor(
547 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700548 update();
549 }
550
551 @Override public void onChange(boolean selfChange) {
552 update();
553 }
554
555 public void update() {
556 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500557 boolean pulseEnabled = Settings.System.getInt(resolver,
558 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
559 if (mNotificationPulseEnabled != pulseEnabled) {
560 mNotificationPulseEnabled = pulseEnabled;
561 updateNotificationPulse();
562 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700563 }
564 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500565
Daniel Sandleredbb3802012-11-13 20:49:47 -0800566 static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) {
567 int[] ar = r.getIntArray(resid);
568 if (ar == null) {
569 return def;
570 }
571 final int len = ar.length > maxlen ? maxlen : ar.length;
572 long[] out = new long[len];
573 for (int i=0; i<len; i++) {
574 out[i] = ar[i];
575 }
576 return out;
577 }
578
Joe Onorato089de882010-04-12 08:18:45 -0700579 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500580 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 {
582 super();
583 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -0700584 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 mAm = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800588
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500589 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
590
591 importOldBlockDb();
Daniel Sandler0da673f2012-04-11 12:33:16 -0400592
Joe Onorato089de882010-04-12 08:18:45 -0700593 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 statusBar.setNotificationCallbacks(mNotificationCallbacks);
595
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500596 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
597 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
598
Mike Lockwood670f9322010-01-20 12:13:36 -0500599 Resources resources = mContext.getResources();
600 mDefaultNotificationColor = resources.getColor(
601 com.android.internal.R.color.config_defaultNotificationColor);
602 mDefaultNotificationLedOn = resources.getInteger(
603 com.android.internal.R.integer.config_defaultNotificationLedOn);
604 mDefaultNotificationLedOff = resources.getInteger(
605 com.android.internal.R.integer.config_defaultNotificationLedOff);
606
Daniel Sandleredbb3802012-11-13 20:49:47 -0800607 mDefaultVibrationPattern = getLongArray(resources,
608 com.android.internal.R.array.config_defaultNotificationVibePattern,
609 VIBRATE_PATTERN_MAXLEN,
610 DEFAULT_VIBRATE_PATTERN);
611
612 mFallbackVibrationPattern = getLongArray(resources,
613 com.android.internal.R.array.config_notificationFallbackVibePattern,
614 VIBRATE_PATTERN_MAXLEN,
615 DEFAULT_VIBRATE_PATTERN);
616
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400617 // Don't start allowing notifications until the setup wizard has run once.
618 // After that, including subsequent boots, init with notifications turned on.
619 // This works on the first boot because the setup wizard will toggle this
620 // flag at least once and we'll go back to 0 after that.
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700621 if (0 == Settings.Global.getInt(mContext.getContentResolver(),
622 Settings.Global.DEVICE_PROVISIONED, 0)) {
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400623 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
624 }
625
Mike Lockwood35e16bf2010-11-30 19:53:36 -0500626 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500628 filter.addAction(Intent.ACTION_SCREEN_ON);
629 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500630 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400631 filter.addAction(Intent.ACTION_USER_PRESENT);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700632 filter.addAction(Intent.ACTION_USER_STOPPED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800634 IntentFilter pkgFilter = new IntentFilter();
635 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400636 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800637 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
638 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
639 pkgFilter.addDataScheme("package");
640 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800641 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800642 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800643
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500644 SettingsObserver observer = new SettingsObserver(mHandler);
645 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 }
647
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500648 /**
649 * Read the old XML-based app block database and import those blockages into the AppOps system.
650 */
651 private void importOldBlockDb() {
652 loadBlockDb();
653
654 PackageManager pm = mContext.getPackageManager();
655 for (String pkg : mBlockedPackages) {
656 PackageInfo info = null;
657 try {
658 info = pm.getPackageInfo(pkg, 0);
659 setNotificationsEnabledForPackage(pkg, info.applicationInfo.uid, false);
660 } catch (NameNotFoundException e) {
661 // forget you
662 }
663 }
664 mBlockedPackages.clear();
665 if (mPolicyFile != null) {
666 mPolicyFile.delete();
667 }
668 }
669
Joe Onorato30275482009-07-08 17:09:14 -0700670 void systemReady() {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700671 mAudioService = IAudioService.Stub.asInterface(
672 ServiceManager.getService(Context.AUDIO_SERVICE));
673
Joe Onorato30275482009-07-08 17:09:14 -0700674 // no beeping until we're basically done booting
675 mSystemReady = true;
676 }
677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 // Toasts
679 // ============================================================================
680 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
681 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400682 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683
684 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800685 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 return ;
687 }
688
Daniel Sandler0da673f2012-04-11 12:33:16 -0400689 final boolean isSystemToast = ("android".equals(pkg));
690
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500691 if (ENABLE_BLOCKED_TOASTS && !noteNotificationOp(pkg, Binder.getCallingUid())) {
692 if (!isSystemToast) {
693 Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
694 return;
695 }
Daniel Sandler0da673f2012-04-11 12:33:16 -0400696 }
697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 synchronized (mToastQueue) {
699 int callingPid = Binder.getCallingPid();
700 long callingId = Binder.clearCallingIdentity();
701 try {
702 ToastRecord record;
703 int index = indexOfToastLocked(pkg, callback);
704 // If it's already in the queue, we update it in place, we don't
705 // move it to the end of the queue.
706 if (index >= 0) {
707 record = mToastQueue.get(index);
708 record.update(duration);
709 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800710 // Limit the number of toasts that any given package except the android
711 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -0400712 if (!isSystemToast) {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800713 int count = 0;
714 final int N = mToastQueue.size();
715 for (int i=0; i<N; i++) {
716 final ToastRecord r = mToastQueue.get(i);
717 if (r.pkg.equals(pkg)) {
718 count++;
719 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
720 Slog.e(TAG, "Package has already posted " + count
721 + " toasts. Not showing more. Package=" + pkg);
722 return;
723 }
724 }
725 }
726 }
727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 record = new ToastRecord(callingPid, pkg, callback, duration);
729 mToastQueue.add(record);
730 index = mToastQueue.size() - 1;
731 keepProcessAliveLocked(callingPid);
732 }
733 // If it's at index 0, it's the current toast. It doesn't matter if it's
734 // new or just been updated. Call back and tell it to show itself.
735 // If the callback fails, this will remove it from the list, so don't
736 // assume that it's valid after this.
737 if (index == 0) {
738 showNextToastLocked();
739 }
740 } finally {
741 Binder.restoreCallingIdentity(callingId);
742 }
743 }
744 }
745
746 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800747 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748
749 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800750 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 return ;
752 }
753
754 synchronized (mToastQueue) {
755 long callingId = Binder.clearCallingIdentity();
756 try {
757 int index = indexOfToastLocked(pkg, callback);
758 if (index >= 0) {
759 cancelToastLocked(index);
760 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800761 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
763 } finally {
764 Binder.restoreCallingIdentity(callingId);
765 }
766 }
767 }
768
769 private void showNextToastLocked() {
770 ToastRecord record = mToastQueue.get(0);
771 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800772 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 try {
774 record.callback.show();
775 scheduleTimeoutLocked(record, false);
776 return;
777 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800778 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 + " in package " + record.pkg);
780 // remove it from the list and let the process die
781 int index = mToastQueue.indexOf(record);
782 if (index >= 0) {
783 mToastQueue.remove(index);
784 }
785 keepProcessAliveLocked(record.pid);
786 if (mToastQueue.size() > 0) {
787 record = mToastQueue.get(0);
788 } else {
789 record = null;
790 }
791 }
792 }
793 }
794
795 private void cancelToastLocked(int index) {
796 ToastRecord record = mToastQueue.get(index);
797 try {
798 record.callback.hide();
799 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800800 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 + " in package " + record.pkg);
802 // don't worry about this, we're about to remove it from
803 // the list anyway
804 }
805 mToastQueue.remove(index);
806 keepProcessAliveLocked(record.pid);
807 if (mToastQueue.size() > 0) {
808 // Show the next one. If the callback fails, this will remove
809 // it from the list, so don't assume that the list hasn't changed
810 // after this point.
811 showNextToastLocked();
812 }
813 }
814
815 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
816 {
817 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
818 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
819 mHandler.removeCallbacksAndMessages(r);
820 mHandler.sendMessageDelayed(m, delay);
821 }
822
823 private void handleTimeout(ToastRecord record)
824 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800825 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 synchronized (mToastQueue) {
827 int index = indexOfToastLocked(record.pkg, record.callback);
828 if (index >= 0) {
829 cancelToastLocked(index);
830 }
831 }
832 }
833
834 // lock on mToastQueue
835 private int indexOfToastLocked(String pkg, ITransientNotification callback)
836 {
837 IBinder cbak = callback.asBinder();
838 ArrayList<ToastRecord> list = mToastQueue;
839 int len = list.size();
840 for (int i=0; i<len; i++) {
841 ToastRecord r = list.get(i);
842 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
843 return i;
844 }
845 }
846 return -1;
847 }
848
849 // lock on mToastQueue
850 private void keepProcessAliveLocked(int pid)
851 {
852 int toastCount = 0; // toasts from this pid
853 ArrayList<ToastRecord> list = mToastQueue;
854 int N = list.size();
855 for (int i=0; i<N; i++) {
856 ToastRecord r = list.get(i);
857 if (r.pid == pid) {
858 toastCount++;
859 }
860 }
861 try {
862 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
863 } catch (RemoteException e) {
864 // Shouldn't happen.
865 }
866 }
867
868 private final class WorkerHandler extends Handler
869 {
870 @Override
871 public void handleMessage(Message msg)
872 {
873 switch (msg.what)
874 {
875 case MESSAGE_TIMEOUT:
876 handleTimeout((ToastRecord)msg.obj);
877 break;
878 }
879 }
880 }
881
882
883 // Notifications
884 // ============================================================================
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800885 public void enqueueNotificationWithTag(String pkg, String basePkg, String tag, int id,
886 Notification notification, int[] idOut, int userId)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700887 {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800888 enqueueNotificationInternal(pkg, basePkg, Binder.getCallingUid(), Binder.getCallingPid(),
Dianne Hackborn41203752012-08-31 14:05:51 -0700889 tag, id, notification, idOut, userId);
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400890 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500891
892 private final static int clamp(int x, int low, int high) {
893 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -0500894 }
895
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400896 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
897 // uid/pid of another application)
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800898 public void enqueueNotificationInternal(String pkg, String basePkg, int callingUid,
899 int callingPid, String tag, int id, Notification notification, int[] idOut, int userId)
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400900 {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400901 if (DBG) {
902 Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
903 }
904 checkCallerIsSystemOrSameApp(pkg);
905 final boolean isSystemNotification = ("android".equals(pkg));
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800906
Dianne Hackborn41203752012-08-31 14:05:51 -0700907 userId = ActivityManager.handleIncomingUser(callingPid,
Amith Yamasani2c7ebea2012-10-30 15:28:27 -0700908 callingUid, userId, true, false, "enqueueNotification", pkg);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700909 final UserHandle user = new UserHandle(userId);
Dianne Hackborn41203752012-08-31 14:05:51 -0700910
Joe Onoratobd73d012010-06-04 11:44:54 -0700911 // Limit the number of notifications that any given package except the android
912 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -0400913 if (!isSystemNotification) {
Joe Onoratobd73d012010-06-04 11:44:54 -0700914 synchronized (mNotificationList) {
915 int count = 0;
916 final int N = mNotificationList.size();
917 for (int i=0; i<N; i++) {
918 final NotificationRecord r = mNotificationList.get(i);
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700919 if (r.pkg.equals(pkg) && r.userId == userId) {
Joe Onoratobd73d012010-06-04 11:44:54 -0700920 count++;
921 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
922 Slog.e(TAG, "Package has already posted " + count
923 + " notifications. Not showing more. package=" + pkg);
924 return;
925 }
926 }
927 }
928 }
929 }
930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 // This conditional is a dirty hack to limit the logging done on
932 // behalf of the download manager without affecting other apps.
933 if (!pkg.equals("com.android.providers.downloads")
934 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandler321e9c52012-10-12 10:59:26 -0700935 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -0500936 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 }
938
939 if (pkg == null || notification == null) {
940 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
941 + " id=" + id + " notification=" + notification);
942 }
943 if (notification.icon != 0) {
944 if (notification.contentView == null) {
945 throw new IllegalArgumentException("contentView required: pkg=" + pkg
946 + " id=" + id + " notification=" + notification);
947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
949
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500950 // === Scoring ===
Daniel Sandler0da673f2012-04-11 12:33:16 -0400951
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500952 // 0. Sanitize inputs
953 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
954 // Migrate notification flags to scores
955 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
956 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400957 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500958 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
959 }
Daniel Sandler0da673f2012-04-11 12:33:16 -0400960
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500961 // 1. initial score: buckets of 10, around the app
Daniel Sandler0da673f2012-04-11 12:33:16 -0400962 int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500963
Daniel Sandler0da673f2012-04-11 12:33:16 -0400964 // 2. Consult external heuristics (TBD)
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500965
Daniel Sandler0da673f2012-04-11 12:33:16 -0400966 // 3. Apply local rules
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500967
968 // blocked apps
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500969 if (ENABLE_BLOCKED_NOTIFICATIONS && !noteNotificationOp(pkg, callingUid)) {
970 if (!isSystemNotification) {
971 score = JUNK_SCORE;
972 Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
973 }
Daniel Sandler0da673f2012-04-11 12:33:16 -0400974 }
975
976 if (DBG) {
977 Slog.v(TAG, "Assigned score=" + score + " to " + notification);
978 }
979
980 if (score < SCORE_DISPLAY_THRESHOLD) {
981 // Notification will be blocked because the score is too low.
982 return;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500983 }
984
Daniel Sandler526fa0e2012-12-04 14:51:50 -0500985 // Should this notification make noise, vibe, or use the LED?
986 final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD);
987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 synchronized (mNotificationList) {
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800989 NotificationRecord r = new NotificationRecord(pkg, basePkg, tag, id,
Dianne Hackborn41203752012-08-31 14:05:51 -0700990 callingUid, callingPid, userId,
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500991 score,
Daniel Sandlere40451a2011-02-03 14:51:35 -0500992 notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 NotificationRecord old = null;
994
Dianne Hackborn41203752012-08-31 14:05:51 -0700995 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 if (index < 0) {
997 mNotificationList.add(r);
998 } else {
999 old = mNotificationList.remove(index);
1000 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001001 // Make sure we don't lose the foreground service state.
1002 if (old != null) {
1003 notification.flags |=
1004 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
1005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001007
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001008 // Ensure if this is a foreground service that the proper additional
1009 // flags are set.
1010 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
1011 notification.flags |= Notification.FLAG_ONGOING_EVENT
1012 | Notification.FLAG_NO_CLEAR;
1013 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001014
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001015 final int currentUser;
1016 final long token = Binder.clearCallingIdentity();
1017 try {
1018 currentUser = ActivityManager.getCurrentUser();
1019 } finally {
1020 Binder.restoreCallingIdentity(token);
1021 }
1022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 if (notification.icon != 0) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07001024 final StatusBarNotification n = new StatusBarNotification(
1025 pkg, id, tag, r.uid, r.initialPid, score, notification, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 if (old != null && old.statusBarKey != null) {
1027 r.statusBarKey = old.statusBarKey;
1028 long identity = Binder.clearCallingIdentity();
1029 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001030 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 }
1032 finally {
1033 Binder.restoreCallingIdentity(identity);
1034 }
1035 } else {
1036 long identity = Binder.clearCallingIdentity();
1037 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001038 r.statusBarKey = mStatusBar.addNotification(n);
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001039 if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1040 && canInterrupt) {
Mike Lockwoodece18ef2012-02-13 20:42:19 -08001041 mAttentionLight.pulse();
1042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 }
1044 finally {
1045 Binder.restoreCallingIdentity(identity);
1046 }
1047 }
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001048 // Send accessibility events only for the current user.
1049 if (currentUser == userId) {
1050 sendAccessibilityEvent(notification, pkg);
1051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001053 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 if (old != null && old.statusBarKey != null) {
1055 long identity = Binder.clearCallingIdentity();
1056 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001057 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 }
1059 finally {
1060 Binder.restoreCallingIdentity(identity);
1061 }
1062 }
1063 }
1064
1065 // If we're not supposed to beep, vibrate, etc. then don't.
1066 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
1067 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -07001068 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
Jeff Sharkey8637bd92012-10-05 15:25:17 -07001069 && (r.userId == UserHandle.USER_ALL ||
Jeff Sharkeyb78738f2012-10-05 16:03:01 -07001070 (r.userId == userId && r.userId == currentUser))
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001071 && canInterrupt
Joe Onorato30275482009-07-08 17:09:14 -07001072 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -08001073
1074 final AudioManager audioManager = (AudioManager) mContext
1075 .getSystemService(Context.AUDIO_SERVICE);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 // sound
1078 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001079 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001080
1081 Uri soundUri = null;
1082 boolean hasValidSound = false;
1083
1084 if (useDefaultSound) {
1085 soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
1086
1087 // check to see if the default notification sound is silent
1088 ContentResolver resolver = mContext.getContentResolver();
1089 hasValidSound = Settings.System.getString(resolver,
1090 Settings.System.NOTIFICATION_SOUND) != null;
1091 } else if (notification.sound != null) {
1092 soundUri = notification.sound;
1093 hasValidSound = (soundUri != null);
1094 }
1095
1096 if (hasValidSound) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
1098 int audioStreamType;
1099 if (notification.audioStreamType >= 0) {
1100 audioStreamType = notification.audioStreamType;
1101 } else {
1102 audioStreamType = DEFAULT_STREAM_TYPE;
1103 }
1104 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -08001105 // do not play notifications if stream volume is 0
Jean-Michel Trivid6770542012-10-10 12:03:41 -07001106 // (typically because ringer mode is silent) or if speech recognition is active.
1107 if ((audioManager.getStreamVolume(audioStreamType) != 0)
1108 && !audioManager.isSpeechRecognitionActive()) {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001109 final long identity = Binder.clearCallingIdentity();
Eric Laurent524dc042009-11-27 05:07:55 -08001110 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001111 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1112 if (player != null) {
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001113 player.playAsync(soundUri, user, looping, audioStreamType);
Jeff Sharkey098d5802012-04-26 17:30:34 -07001114 }
1115 } catch (RemoteException e) {
1116 } finally {
Eric Laurent524dc042009-11-27 05:07:55 -08001117 Binder.restoreCallingIdentity(identity);
1118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
1120 }
1121
1122 // vibrate
Daniel Sandleredbb3802012-11-13 20:49:47 -08001123 // Does the notification want to specify its own vibration?
1124 final boolean hasCustomVibrate = notification.vibrate != null;
1125
David Agnew71789e12012-11-09 23:03:26 -05001126 // new in 4.2: if there was supposed to be a sound and we're in vibrate mode,
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001127 // and no other vibration is specified, we fall back to vibration
David Agnew71789e12012-11-09 23:03:26 -05001128 final boolean convertSoundToVibration =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001129 !hasCustomVibrate
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001130 && hasValidSound
David Agnew71789e12012-11-09 23:03:26 -05001131 && (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
1132
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001133 // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 final boolean useDefaultVibrate =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001135 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
David Agnew71789e12012-11-09 23:03:26 -05001136
Daniel Sandleredbb3802012-11-13 20:49:47 -08001137 if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
Eric Laurentbffc3d12012-05-07 17:43:49 -07001138 && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 mVibrateNotification = r;
1140
Daniel Sandleredbb3802012-11-13 20:49:47 -08001141 if (useDefaultVibrate || convertSoundToVibration) {
1142 // Escalate privileges so we can use the vibrator even if the notifying app
1143 // does not have the VIBRATE permission.
1144 long identity = Binder.clearCallingIdentity();
1145 try {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001146 mVibrator.vibrate(r.uid, r.basePkg,
1147 useDefaultVibrate ? mDefaultVibrationPattern
1148 : mFallbackVibrationPattern,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001149 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1150 } finally {
1151 Binder.restoreCallingIdentity(identity);
1152 }
1153 } else if (notification.vibrate.length > 1) {
1154 // If you want your own vibration pattern, you need the VIBRATE permission
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001155 mVibrator.vibrate(r.uid, r.basePkg, notification.vibrate,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001156 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 }
1159 }
1160
1161 // this option doesn't shut off the lights
1162
1163 // light
1164 // the most recent thing gets the light
1165 mLights.remove(old);
1166 if (mLedNotification == old) {
1167 mLedNotification = null;
1168 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001169 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001171 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1172 && canInterrupt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 mLights.add(r);
1174 updateLightsLocked();
1175 } else {
1176 if (old != null
1177 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
1178 updateLightsLocked();
1179 }
1180 }
1181 }
1182
1183 idOut[0] = id;
1184 }
1185
Joe Onorato30275482009-07-08 17:09:14 -07001186 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001187 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
1188 if (!manager.isEnabled()) {
1189 return;
1190 }
1191
1192 AccessibilityEvent event =
1193 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
1194 event.setPackageName(packageName);
1195 event.setClassName(Notification.class.getName());
1196 event.setParcelableData(notification);
1197 CharSequence tickerText = notification.tickerText;
1198 if (!TextUtils.isEmpty(tickerText)) {
1199 event.getText().add(tickerText);
1200 }
1201
1202 manager.sendAccessibilityEvent(event);
1203 }
1204
Joe Onorato46439ce2010-11-19 13:56:21 -08001205 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
1206 // tell the app
1207 if (sendDelete) {
1208 if (r.notification.deleteIntent != null) {
1209 try {
1210 r.notification.deleteIntent.send();
1211 } catch (PendingIntent.CanceledException ex) {
1212 // do nothing - there's no relevant way to recover, and
1213 // no reason to let this propagate
1214 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
1215 }
1216 }
1217 }
1218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 // status bar
1220 if (r.notification.icon != 0) {
1221 long identity = Binder.clearCallingIdentity();
1222 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001223 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 }
1225 finally {
1226 Binder.restoreCallingIdentity(identity);
1227 }
1228 r.statusBarKey = null;
1229 }
1230
1231 // sound
1232 if (mSoundNotification == r) {
1233 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001234 final long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001236 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1237 if (player != null) {
1238 player.stopAsync();
1239 }
1240 } catch (RemoteException e) {
1241 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 Binder.restoreCallingIdentity(identity);
1243 }
1244 }
1245
1246 // vibrate
1247 if (mVibrateNotification == r) {
1248 mVibrateNotification = null;
1249 long identity = Binder.clearCallingIdentity();
1250 try {
1251 mVibrator.cancel();
1252 }
1253 finally {
1254 Binder.restoreCallingIdentity(identity);
1255 }
1256 }
1257
1258 // light
1259 mLights.remove(r);
1260 if (mLedNotification == r) {
1261 mLedNotification = null;
1262 }
1263 }
1264
1265 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001266 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001267 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 */
Fred Quintana6ecaff12009-09-25 14:23:13 -07001269 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001270 int mustNotHaveFlags, boolean sendDelete, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001271 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001272 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273
1274 synchronized (mNotificationList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001275 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001277 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
1280 return;
1281 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001282 if ((r.notification.flags & mustNotHaveFlags) != 0) {
1283 return;
1284 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 mNotificationList.remove(index);
1287
Joe Onorato46439ce2010-11-19 13:56:21 -08001288 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 updateLightsLocked();
1290 }
1291 }
1292 }
1293
1294 /**
Daniel Sandler321e9c52012-10-12 10:59:26 -07001295 * Determine whether the userId applies to the notification in question, either because
1296 * they match exactly, or one of them is USER_ALL (which is treated as a wildcard).
1297 */
1298 private boolean notificationMatchesUserId(NotificationRecord r, int userId) {
1299 return
1300 // looking for USER_ALL notifications? match everything
1301 userId == UserHandle.USER_ALL
1302 // a notification sent to USER_ALL matches any query
1303 || r.userId == UserHandle.USER_ALL
1304 // an exact user match
1305 || r.userId == userId;
1306 }
1307
1308 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 * Cancels all notifications from a given package that have all of the
1310 * {@code mustHaveFlags}.
1311 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001312 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001313 int mustNotHaveFlags, boolean doit, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001314 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId,
1315 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316
1317 synchronized (mNotificationList) {
1318 final int N = mNotificationList.size();
1319 boolean canceledSomething = false;
1320 for (int i = N-1; i >= 0; --i) {
1321 NotificationRecord r = mNotificationList.get(i);
Daniel Sandler321e9c52012-10-12 10:59:26 -07001322 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001323 continue;
1324 }
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001325 // Don't remove notifications to all, if there's no package name specified
1326 if (r.userId == UserHandle.USER_ALL && pkg == null) {
1327 continue;
1328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
1330 continue;
1331 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001332 if ((r.notification.flags & mustNotHaveFlags) != 0) {
1333 continue;
1334 }
John Spurlock078a4902012-10-04 12:00:53 -04001335 if (pkg != null && !r.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 continue;
1337 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001338 canceledSomething = true;
1339 if (!doit) {
1340 return true;
1341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001343 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 }
1345 if (canceledSomething) {
1346 updateLightsLocked();
1347 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001348 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 }
1350 }
1351
Dianne Hackborn41203752012-08-31 14:05:51 -07001352 public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001353 checkCallerIsSystemOrSameApp(pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001354 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001355 Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001356 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001357 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001358 Binder.getCallingUid() == Process.SYSTEM_UID
Dianne Hackborn41203752012-08-31 14:05:51 -07001359 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 }
1361
Dianne Hackborn41203752012-08-31 14:05:51 -07001362 public void cancelAllNotifications(String pkg, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001363 checkCallerIsSystemOrSameApp(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001364
Dianne Hackborn41203752012-08-31 14:05:51 -07001365 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001366 Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001367
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001368 // Calling from user space, don't allow the canceling of actively
1369 // running foreground services.
Dianne Hackborn41203752012-08-31 14:05:51 -07001370 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 }
1372
Daniel Sandler0da673f2012-04-11 12:33:16 -04001373 void checkCallerIsSystem() {
1374 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001375 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001376 return;
1377 }
1378 throw new SecurityException("Disallowed call for uid " + uid);
1379 }
1380
1381 void checkCallerIsSystemOrSameApp(String pkg) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001382 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001383 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001384 return;
1385 }
1386 try {
Amith Yamasanif203aee2012-08-29 18:41:53 -07001387 ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
1388 pkg, 0, UserHandle.getCallingUserId());
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001389 if (!UserHandle.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001390 throw new SecurityException("Calling uid " + uid + " gave package"
1391 + pkg + " which is owned by uid " + ai.uid);
1392 }
Amith Yamasanif203aee2012-08-29 18:41:53 -07001393 } catch (RemoteException re) {
1394 throw new SecurityException("Unknown package " + pkg + "\n" + re);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001395 }
1396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001397
Dianne Hackborn41203752012-08-31 14:05:51 -07001398 void cancelAll(int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 synchronized (mNotificationList) {
1400 final int N = mNotificationList.size();
1401 for (int i=N-1; i>=0; i--) {
1402 NotificationRecord r = mNotificationList.get(i);
1403
Daniel Sandler321e9c52012-10-12 10:59:26 -07001404 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001405 continue;
1406 }
1407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1409 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001411 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 }
1413 }
1414
1415 updateLightsLocked();
1416 }
1417 }
1418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 // lock on mNotificationList
1420 private void updateLightsLocked()
1421 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001422 // handle notification lights
1423 if (mLedNotification == null) {
1424 // get next notification, if any
1425 int n = mLights.size();
1426 if (n > 0) {
1427 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 }
1429 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001430
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001431 // Don't flash while we are in a call or screen is on
1432 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001433 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001434 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001435 int ledARGB = mLedNotification.notification.ledARGB;
1436 int ledOnMS = mLedNotification.notification.ledOnMS;
1437 int ledOffMS = mLedNotification.notification.ledOffMS;
1438 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1439 ledARGB = mDefaultNotificationColor;
1440 ledOnMS = mDefaultNotificationLedOn;
1441 ledOffMS = mDefaultNotificationLedOff;
1442 }
1443 if (mNotificationPulseEnabled) {
1444 // pulse repeatedly
1445 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1446 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05001447 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 }
1450
1451 // lock on mNotificationList
Dianne Hackborn41203752012-08-31 14:05:51 -07001452 private int indexOfNotificationLocked(String pkg, String tag, int id, int userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 {
1454 ArrayList<NotificationRecord> list = mNotificationList;
1455 final int len = list.size();
1456 for (int i=0; i<len; i++) {
1457 NotificationRecord r = list.get(i);
Daniel Sandler321e9c52012-10-12 10:59:26 -07001458 if (!notificationMatchesUserId(r, userId) || r.id != id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001459 continue;
1460 }
Fred Quintana6ecaff12009-09-25 14:23:13 -07001461 if (tag == null) {
1462 if (r.tag != null) {
1463 continue;
1464 }
1465 } else {
1466 if (!tag.equals(r.tag)) {
1467 continue;
1468 }
1469 }
Dianne Hackborn41203752012-08-31 14:05:51 -07001470 if (r.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 return i;
1472 }
1473 }
1474 return -1;
1475 }
1476
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001477 private void updateNotificationPulse() {
1478 synchronized (mNotificationList) {
1479 updateLightsLocked();
1480 }
1481 }
1482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 // ======================================================================
1484 @Override
1485 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1486 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1487 != PackageManager.PERMISSION_GRANTED) {
1488 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1489 + Binder.getCallingPid()
1490 + ", uid=" + Binder.getCallingUid());
1491 return;
1492 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 pw.println("Current Notification Manager state:");
1495
1496 int N;
1497
1498 synchronized (mToastQueue) {
1499 N = mToastQueue.size();
1500 if (N > 0) {
1501 pw.println(" Toast Queue:");
1502 for (int i=0; i<N; i++) {
1503 mToastQueue.get(i).dump(pw, " ");
1504 }
1505 pw.println(" ");
1506 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
1509
1510 synchronized (mNotificationList) {
1511 N = mNotificationList.size();
1512 if (N > 0) {
1513 pw.println(" Notification List:");
1514 for (int i=0; i<N; i++) {
1515 mNotificationList.get(i).dump(pw, " ", mContext);
1516 }
1517 pw.println(" ");
1518 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 N = mLights.size();
1521 if (N > 0) {
1522 pw.println(" Lights List:");
1523 for (int i=0; i<N; i++) {
1524 mLights.get(i).dump(pw, " ", mContext);
1525 }
1526 pw.println(" ");
1527 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 pw.println(" mSoundNotification=" + mSoundNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001531 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1532 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 }
1534 }
1535}