blob: 5cf1c285f2db7fe11095609214364e0278b613ab [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;
Daniel Sandler09a247e2013-02-14 10:24:17 -050029import android.app.INotificationListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.app.ITransientNotification;
31import android.app.Notification;
32import android.app.PendingIntent;
33import android.app.StatusBarManager;
34import android.content.BroadcastReceiver;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070035import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.Context;
37import android.content.Intent;
38import android.content.IntentFilter;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070039import android.content.pm.ApplicationInfo;
Daniel Sandler4a900acd2013-01-30 14:04:10 -050040import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.pm.PackageManager;
42import android.content.pm.PackageManager.NameNotFoundException;
43import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070044import android.database.ContentObserver;
svetoslavganov75986cf2009-05-14 22:28:01 -070045import android.media.AudioManager;
Jeff Sharkey098d5802012-04-26 17:30:34 -070046import android.media.IAudioService;
47import android.media.IRingtonePlayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.IBinder;
52import android.os.Message;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070053import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070054import android.os.RemoteException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070055import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070056import android.os.UserHandle;
Daniel Sandler4b749ef2013-03-18 21:53:04 -040057import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.Vibrator;
59import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050060import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.text.TextUtils;
Dianne Hackborn39606a02012-07-31 17:54:35 -070062import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.util.EventLog;
64import android.util.Log;
Andy Stadler110988c2010-12-03 14:29:16 -080065import android.util.Slog;
Daniel Sandler0da673f2012-04-11 12:33:16 -040066import android.util.Xml;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.accessibility.AccessibilityEvent;
68import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.widget.Toast;
70
Jeff Sharkey098d5802012-04-26 17:30:34 -070071import com.android.internal.statusbar.StatusBarNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -070072
73import org.xmlpull.v1.XmlPullParser;
74import org.xmlpull.v1.XmlPullParserException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070075
Daniel Sandler0da673f2012-04-11 12:33:16 -040076import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.io.FileDescriptor;
Daniel Sandler0da673f2012-04-11 12:33:16 -040078import java.io.FileInputStream;
79import java.io.FileNotFoundException;
Daniel Sandler0da673f2012-04-11 12:33:16 -040080import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import java.io.PrintWriter;
Daniel Sandlerfde19b12013-01-17 00:21:05 -050082import java.util.ArrayDeque;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import java.util.ArrayList;
84import java.util.Arrays;
Daniel Sandler0da673f2012-04-11 12:33:16 -040085import java.util.HashSet;
Daniel Sandlerfde19b12013-01-17 00:21:05 -050086import java.util.Iterator;
87import java.util.NoSuchElementException;
Daniel Sandler0da673f2012-04-11 12:33:16 -040088
89import libcore.io.IoUtils;
90
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040092/** {@hide} */
93public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094{
95 private static final String TAG = "NotificationService";
96 private static final boolean DBG = false;
97
Joe Onoratobd73d012010-06-04 11:44:54 -070098 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 // message codes
101 private static final int MESSAGE_TIMEOUT = 2;
102
103 private static final int LONG_DELAY = 3500; // 3.5 seconds
104 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800105
106 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
Daniel Sandleredbb3802012-11-13 20:49:47 -0800107 private static final int VIBRATE_PATTERN_MAXLEN = 8 * 2 + 1; // up to eight bumps
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
109 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400110 private static final boolean SCORE_ONGOING_HIGHER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111
Daniel Sandler0da673f2012-04-11 12:33:16 -0400112 private static final int JUNK_SCORE = -1000;
113 private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10;
114 private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER;
115
Daniel Sandler526fa0e2012-12-04 14:51:50 -0500116 // Notifications with scores below this will not interrupt the user, either via LED or
117 // sound or vibration
118 private static final int SCORE_INTERRUPTION_THRESHOLD =
119 Notification.PRIORITY_LOW * NOTIFICATION_PRIORITY_MULTIPLIER;
120
Daniel Sandler0da673f2012-04-11 12:33:16 -0400121 private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true;
122 private static final boolean ENABLE_BLOCKED_TOASTS = true;
123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 final Context mContext;
125 final IActivityManager mAm;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400126 final UserManager mUserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 final IBinder mForegroundToken = new Binder();
128
129 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -0700130 private StatusBarManagerService mStatusBar;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500131 private LightsService.Light mNotificationLight;
132 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
Mike Lockwood670f9322010-01-20 12:13:36 -0500134 private int mDefaultNotificationColor;
135 private int mDefaultNotificationLedOn;
136 private int mDefaultNotificationLedOff;
137
Daniel Sandleredbb3802012-11-13 20:49:47 -0800138 private long[] mDefaultVibrationPattern;
139 private long[] mFallbackVibrationPattern;
140
Joe Onorato30275482009-07-08 17:09:14 -0700141 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400142 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
Jeff Sharkey098d5802012-04-26 17:30:34 -0700144 private NotificationRecord mSoundNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 private NotificationRecord mVibrateNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700146
147 private IAudioService mAudioService;
Jeff Brownc2346132012-04-13 01:55:38 -0700148 private Vibrator mVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500150 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400151 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500152 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500153 private boolean mNotificationPulseEnabled;
154
Daniel Sandler09a247e2013-02-14 10:24:17 -0500155 // used as a mutex for access to all active notifications & listeners
Fred Quintana6ecaff12009-09-25 14:23:13 -0700156 private final ArrayList<NotificationRecord> mNotificationList =
157 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158
159 private ArrayList<ToastRecord> mToastQueue;
160
161 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700163
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500164 private final AppOpsManager mAppOps;
165
Daniel Sandler09a247e2013-02-14 10:24:17 -0500166 private ArrayList<NotificationListenerInfo> mListeners = new ArrayList<NotificationListenerInfo>();
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400167 private ArrayList<String> mEnabledListenersForCurrentUser = new ArrayList<String>();
Daniel Sandler09a247e2013-02-14 10:24:17 -0500168
Daniel Sandler0da673f2012-04-11 12:33:16 -0400169 // Notification control database. For now just contains disabled packages.
170 private AtomicFile mPolicyFile;
171 private HashSet<String> mBlockedPackages = new HashSet<String>();
172
173 private static final int DB_VERSION = 1;
174
175 private static final String TAG_BODY = "notification-policy";
176 private static final String ATTR_VERSION = "version";
177
178 private static final String TAG_BLOCKED_PKGS = "blocked-packages";
179 private static final String TAG_PACKAGE = "package";
180 private static final String ATTR_NAME = "name";
181
Daniel Sandler09a247e2013-02-14 10:24:17 -0500182 private class NotificationListenerInfo implements DeathRecipient {
183 INotificationListener listener;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400184 String pkg;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500185 int userid;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400186 boolean isSystem;
187
188 public NotificationListenerInfo(INotificationListener listener, String pkg, int userid,
189 boolean isSystem) {
Daniel Sandler09a247e2013-02-14 10:24:17 -0500190 this.listener = listener;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400191 this.pkg = pkg;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500192 this.userid = userid;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400193 this.isSystem = isSystem;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500194 }
195
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400196 boolean enabledAndUserMatches(StatusBarNotification sbn) {
197 final int nid = sbn.getUserId();
198 if (!(isSystem || isEnabledForUser(nid))) return false;
Daniel Sandler21ca44d2013-03-07 13:58:00 -0500199 if (this.userid == UserHandle.USER_ALL) return true;
Daniel Sandler21ca44d2013-03-07 13:58:00 -0500200 return (nid == UserHandle.USER_ALL || nid == this.userid);
201 }
202
Daniel Sandler09a247e2013-02-14 10:24:17 -0500203 public void notifyPostedIfUserMatch(StatusBarNotification sbn) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400204 if (!enabledAndUserMatches(sbn)) return;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500205 try {
206 listener.onNotificationPosted(sbn);
207 } catch (RemoteException ex) {
208 // not there?
209 }
210 }
211
212 public void notifyRemovedIfUserMatch(StatusBarNotification sbn) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400213 if (!enabledAndUserMatches(sbn)) return;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500214 try {
215 listener.onNotificationRemoved(sbn);
216 } catch (RemoteException ex) {
217 // not there?
218 }
219 }
220
221 @Override
222 public void binderDied() {
223 unregisterListener(this.listener, this.userid);
224 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400225
226 /** convenience method for looking in mEnabledListenersForCurrentUser */
227 public boolean isEnabledForUser(int userid) {
228 for (int i=0; i<mEnabledListenersForCurrentUser.size(); i++) {
229 if (this.pkg.equals(mEnabledListenersForCurrentUser.get(i))) return true;
230 }
231 return false;
232 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500233 }
234
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500235 private static class Archive {
236 static final int BUFFER_SIZE = 1000;
237 ArrayDeque<StatusBarNotification> mBuffer = new ArrayDeque<StatusBarNotification>(BUFFER_SIZE);
238
239 public Archive() {
240
241 }
242 public void record(StatusBarNotification nr) {
243 if (mBuffer.size() == BUFFER_SIZE) {
244 mBuffer.removeFirst();
245 }
246 mBuffer.addLast(nr);
247 }
248
249 public void clear() {
250 mBuffer.clear();
251 }
252
253 public Iterator<StatusBarNotification> descendingIterator() {
254 return mBuffer.descendingIterator();
255 }
256 public Iterator<StatusBarNotification> ascendingIterator() {
257 return mBuffer.iterator();
258 }
259 public Iterator<StatusBarNotification> filter(
260 final Iterator<StatusBarNotification> iter, final String pkg, final int userId) {
261 return new Iterator<StatusBarNotification>() {
262 StatusBarNotification mNext = findNext();
263
264 private StatusBarNotification findNext() {
265 while (iter.hasNext()) {
266 StatusBarNotification nr = iter.next();
267 if ((pkg == null || nr.pkg == pkg)
268 && (userId == UserHandle.USER_ALL || nr.getUserId() == userId)) {
269 return nr;
270 }
271 }
272 return null;
273 }
274
275 @Override
276 public boolean hasNext() {
277 return mNext == null;
278 }
279
280 @Override
281 public StatusBarNotification next() {
282 StatusBarNotification next = mNext;
283 if (next == null) {
284 throw new NoSuchElementException();
285 }
286 mNext = findNext();
287 return next;
288 }
289
290 @Override
291 public void remove() {
292 iter.remove();
293 }
294 };
295 }
Daniel Sandler78d0d252013-02-12 08:14:52 -0500296
297 public StatusBarNotification[] getArray(int count) {
298 if (count == 0) count = Archive.BUFFER_SIZE;
299 final StatusBarNotification[] a
300 = new StatusBarNotification[Math.min(count, mBuffer.size())];
301 Iterator<StatusBarNotification> iter = descendingIterator();
302 int i=0;
303 while (iter.hasNext() && i < count) {
304 a[i++] = iter.next();
305 }
306 return a;
307 }
308
309 public StatusBarNotification[] getArray(int count, String pkg, int userId) {
310 if (count == 0) count = Archive.BUFFER_SIZE;
311 final StatusBarNotification[] a
312 = new StatusBarNotification[Math.min(count, mBuffer.size())];
313 Iterator<StatusBarNotification> iter = filter(descendingIterator(), pkg, userId);
314 int i=0;
315 while (iter.hasNext() && i < count) {
316 a[i++] = iter.next();
317 }
318 return a;
319 }
320
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500321 }
322
323 Archive mArchive = new Archive();
324
Daniel Sandler0da673f2012-04-11 12:33:16 -0400325 private void loadBlockDb() {
326 synchronized(mBlockedPackages) {
327 if (mPolicyFile == null) {
328 File dir = new File("/data/system");
329 mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
330
331 mBlockedPackages.clear();
332
333 FileInputStream infile = null;
334 try {
335 infile = mPolicyFile.openRead();
336 final XmlPullParser parser = Xml.newPullParser();
337 parser.setInput(infile, null);
338
339 int type;
340 String tag;
341 int version = DB_VERSION;
342 while ((type = parser.next()) != END_DOCUMENT) {
343 tag = parser.getName();
344 if (type == START_TAG) {
345 if (TAG_BODY.equals(tag)) {
346 version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
347 } else if (TAG_BLOCKED_PKGS.equals(tag)) {
348 while ((type = parser.next()) != END_DOCUMENT) {
349 tag = parser.getName();
350 if (TAG_PACKAGE.equals(tag)) {
351 mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
352 } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
353 break;
354 }
355 }
356 }
357 }
358 }
359 } catch (FileNotFoundException e) {
360 // No data yet
361 } catch (IOException e) {
362 Log.wtf(TAG, "Unable to read blocked notifications database", e);
363 } catch (NumberFormatException e) {
364 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
365 } catch (XmlPullParserException e) {
366 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
367 } finally {
368 IoUtils.closeQuietly(infile);
369 }
370 }
371 }
372 }
373
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500374 /**
375 * Use this when you just want to know if notifications are OK for this package.
376 */
377 public boolean areNotificationsEnabledForPackage(String pkg, int uid) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400378 checkCallerIsSystem();
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500379 return (mAppOps.checkOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
380 == AppOpsManager.MODE_ALLOWED);
Daniel Sandler0da673f2012-04-11 12:33:16 -0400381 }
382
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500383 /** Use this when you actually want to post a notification or toast.
384 *
385 * Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
386 */
387 private boolean noteNotificationOp(String pkg, int uid) {
388 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
389 != AppOpsManager.MODE_ALLOWED) {
390 Slog.v(TAG, "notifications are disabled by AppOps for " + pkg);
391 return false;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400392 }
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500393 return true;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400394 }
395
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500396 public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400397 checkCallerIsSystem();
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500398 if (true||DBG) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400399 Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
400 }
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500401 mAppOps.setMode(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg,
402 enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED);
Daniel Sandler0da673f2012-04-11 12:33:16 -0400403 }
404
405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 private static String idDebugString(Context baseContext, String packageName, int id) {
407 Context c = null;
408
409 if (packageName != null) {
410 try {
411 c = baseContext.createPackageContext(packageName, 0);
412 } catch (NameNotFoundException e) {
413 c = baseContext;
414 }
415 } else {
416 c = baseContext;
417 }
418
419 String pkg;
420 String type;
421 String name;
422
423 Resources r = c.getResources();
424 try {
425 return r.getResourceName(id);
426 } catch (Resources.NotFoundException e) {
427 return "<name unknown>";
428 }
429 }
430
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500431 public StatusBarNotification[] getActiveNotifications(String callingPkg) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400432 // enforce() will ensure the calling uid has the correct permission
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500433 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
Daniel Sandler78d0d252013-02-12 08:14:52 -0500434 "NotificationManagerService.getActiveNotifications");
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500435
436 StatusBarNotification[] tmp = null;
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500437 int uid = Binder.getCallingUid();
438
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400439 // noteOp will check to make sure the callingPkg matches the uid
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500440 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
441 == AppOpsManager.MODE_ALLOWED) {
442 synchronized (mNotificationList) {
443 tmp = new StatusBarNotification[mNotificationList.size()];
444 final int N = mNotificationList.size();
445 for (int i=0; i<N; i++) {
446 tmp[i] = mNotificationList.get(i).sbn;
447 }
448 }
449 }
450 return tmp;
451 }
452
Daniel Sandler78d0d252013-02-12 08:14:52 -0500453 public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400454 // enforce() will ensure the calling uid has the correct permission
Daniel Sandler78d0d252013-02-12 08:14:52 -0500455 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
456 "NotificationManagerService.getHistoricalNotifications");
457
458 StatusBarNotification[] tmp = null;
459 int uid = Binder.getCallingUid();
460
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400461 // noteOp will check to make sure the callingPkg matches the uid
Daniel Sandler78d0d252013-02-12 08:14:52 -0500462 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
463 == AppOpsManager.MODE_ALLOWED) {
464 synchronized (mArchive) {
465 tmp = mArchive.getArray(count);
466 }
467 }
468 return tmp;
469 }
470
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400471 boolean packageCanTapNotificationsForUser(final int uid, final String pkg) {
472 // Make sure the package and uid match, and that the package is allowed access
473 return (AppOpsManager.MODE_ALLOWED
474 == mAppOps.checkOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, pkg));
475 }
476
Daniel Sandler09a247e2013-02-14 10:24:17 -0500477 @Override
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400478 public void registerListener(final INotificationListener listener,
479 final String pkg, final int userid) {
480 // ensure system or allowed pkg
481 int uid = Binder.getCallingUid();
482 boolean isSystem = (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0);
483 if (!(isSystem || packageCanTapNotificationsForUser(uid, pkg))) {
484 throw new SecurityException("Package " + pkg
485 + " may not listen for notifications");
486 }
487
Daniel Sandler09a247e2013-02-14 10:24:17 -0500488 synchronized (mNotificationList) {
489 try {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400490 NotificationListenerInfo info
491 = new NotificationListenerInfo(listener, pkg, userid, isSystem);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500492 listener.asBinder().linkToDeath(info, 0);
493 mListeners.add(info);
494 } catch (RemoteException e) {
495 // already dead
496 }
497 }
498 }
499
500 @Override
501 public void unregisterListener(INotificationListener listener, int userid) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400502 // no need to check permissions; if your listener binder is in the list,
503 // that's proof that you had permission to add it in the first place
504
Daniel Sandler09a247e2013-02-14 10:24:17 -0500505 synchronized (mNotificationList) {
506 final int N = mListeners.size();
507 for (int i=N-1; i>=0; i--) {
508 final NotificationListenerInfo info = mListeners.get(i);
509 if (info.listener == listener && info.userid == userid) {
510 mListeners.remove(listener);
511 }
512 }
513 }
514 }
515
516 private void notifyPostedLocked(NotificationRecord n) {
517 final StatusBarNotification sbn = n.sbn;
518 for (final NotificationListenerInfo info : mListeners) {
519 mHandler.post(new Runnable() {
520 @Override
521 public void run() {
522 info.notifyPostedIfUserMatch(sbn);
523 }});
524 }
525 }
526
527 private void notifyRemovedLocked(NotificationRecord n) {
528 final StatusBarNotification sbn = n.sbn;
529 for (final NotificationListenerInfo info : mListeners) {
530 mHandler.post(new Runnable() {
531 @Override
532 public void run() {
533 info.notifyRemovedIfUserMatch(sbn);
534 }});
535 }
536 }
537
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500538 public static final class NotificationRecord
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500540 final StatusBarNotification sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 IBinder statusBarKey;
542
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500543 NotificationRecord(StatusBarNotification sbn)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500545 this.sbn = sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700547
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500548 public Notification getNotification() { return sbn.notification; }
549 public int getFlags() { return sbn.notification.flags; }
550 public int getUserId() { return sbn.getUserId(); }
551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 void dump(PrintWriter pw, String prefix, Context baseContext) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500553 final Notification notification = sbn.notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 pw.println(prefix + this);
555 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500556 + " / " + idDebugString(baseContext, this.sbn.pkg, notification.icon));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500557 pw.println(prefix + " pri=" + notification.priority);
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500558 pw.println(prefix + " score=" + this.sbn.score);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 pw.println(prefix + " contentIntent=" + notification.contentIntent);
560 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
561 pw.println(prefix + " tickerText=" + notification.tickerText);
562 pw.println(prefix + " contentView=" + notification.contentView);
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500563 pw.println(prefix + " uid=" + this.sbn.uid + " userId=" + this.sbn.getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
565 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
566 pw.println(prefix + " sound=" + notification.sound);
567 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
568 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
569 + " ledOnMS=" + notification.ledOnMS
570 + " ledOffMS=" + notification.ledOffMS);
571 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 @Override
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500574 public final String toString() {
575 return String.format(
576 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s score=%d: %s)",
577 System.identityHashCode(this),
578 this.sbn.pkg, this.sbn.user, this.sbn.id, this.sbn.tag,
579 this.sbn.score, this.sbn.notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
581 }
582
583 private static final class ToastRecord
584 {
585 final int pid;
586 final String pkg;
587 final ITransientNotification callback;
588 int duration;
589
590 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
591 {
592 this.pid = pid;
593 this.pkg = pkg;
594 this.callback = callback;
595 this.duration = duration;
596 }
597
598 void update(int duration) {
599 this.duration = duration;
600 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 void dump(PrintWriter pw, String prefix) {
603 pw.println(prefix + this);
604 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 @Override
607 public final String toString()
608 {
609 return "ToastRecord{"
610 + Integer.toHexString(System.identityHashCode(this))
611 + " pkg=" + pkg
612 + " callback=" + callback
613 + " duration=" + duration;
614 }
615 }
616
Joe Onorato089de882010-04-12 08:18:45 -0700617 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
618 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619
620 public void onSetDisabled(int status) {
621 synchronized (mNotificationList) {
622 mDisabledNotifications = status;
623 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
624 // cancel whatever's going on
625 long identity = Binder.clearCallingIdentity();
626 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700627 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
628 if (player != null) {
629 player.stopAsync();
630 }
631 } catch (RemoteException e) {
632 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 Binder.restoreCallingIdentity(identity);
634 }
635
636 identity = Binder.clearCallingIdentity();
637 try {
638 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700639 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 Binder.restoreCallingIdentity(identity);
641 }
642 }
643 }
644 }
645
646 public void onClearAll() {
Dianne Hackborn41203752012-08-31 14:05:51 -0700647 // XXX to be totally correct, the caller should tell us which user
648 // this is for.
649 cancelAll(ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 }
651
Fred Quintana6ecaff12009-09-25 14:23:13 -0700652 public void onNotificationClick(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700653 // XXX to be totally correct, the caller should tell us which user
654 // this is for.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700655 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackborn41203752012-08-31 14:05:51 -0700656 Notification.FLAG_FOREGROUND_SERVICE, false,
657 ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 }
659
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400660 public void onNotificationClear(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700661 // XXX to be totally correct, the caller should tell us which user
662 // this is for.
Joe Onorato46439ce2010-11-19 13:56:21 -0800663 cancelNotification(pkg, tag, id, 0,
664 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
Dianne Hackborn41203752012-08-31 14:05:51 -0700665 true, ActivityManager.getCurrentUser());
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400666 }
667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 public void onPanelRevealed() {
669 synchronized (mNotificationList) {
670 // sound
671 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 long identity = Binder.clearCallingIdentity();
674 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700675 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
676 if (player != null) {
677 player.stopAsync();
678 }
679 } catch (RemoteException e) {
680 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 Binder.restoreCallingIdentity(identity);
682 }
683
684 // vibrate
685 mVibrateNotification = null;
686 identity = Binder.clearCallingIdentity();
687 try {
688 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700689 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 Binder.restoreCallingIdentity(identity);
691 }
692
693 // light
694 mLights.clear();
695 mLedNotification = null;
696 updateLightsLocked();
697 }
698 }
Joe Onorato005847b2010-06-04 16:08:02 -0400699
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700700 public void onNotificationError(String pkg, String tag, int id,
701 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400702 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
703 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Dianne Hackborn41203752012-08-31 14:05:51 -0700704 // XXX to be totally correct, the caller should tell us which user
705 // this is for.
706 cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid));
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700707 long ident = Binder.clearCallingIdentity();
708 try {
709 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
710 "Bad notification posted from package " + pkg
711 + ": " + message);
712 } catch (RemoteException e) {
713 }
714 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 };
717
718 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
719 @Override
720 public void onReceive(Context context, Intent intent) {
721 String action = intent.getAction();
722
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800723 boolean queryRestart = false;
Daniel Sandler26ece572012-06-01 15:38:46 -0400724 boolean packageChanged = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800725
Mike Lockwood541c9942011-06-12 19:35:45 -0400726 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800727 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandler26ece572012-06-01 15:38:46 -0400728 || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800729 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800730 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800731 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800732 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800733 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800734 } else if (queryRestart) {
735 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800736 } else {
737 Uri uri = intent.getData();
738 if (uri == null) {
739 return;
740 }
741 String pkgName = uri.getSchemeSpecificPart();
742 if (pkgName == null) {
743 return;
744 }
Daniel Sandler26ece572012-06-01 15:38:46 -0400745 if (packageChanged) {
746 // We cancel notifications for packages which have just been disabled
747 final int enabled = mContext.getPackageManager()
748 .getApplicationEnabledSetting(pkgName);
749 if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
750 || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
751 return;
752 }
753 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800754 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800756 if (pkgList != null && (pkgList.length > 0)) {
757 for (String pkgName : pkgList) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700758 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart,
759 UserHandle.USER_ALL);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400762 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
763 // Keep track of screen on/off state, but do not turn off the notification light
764 // until user passes through the lock screen or views the notification.
765 mScreenOn = true;
766 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
767 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500768 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400769 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
770 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500771 updateNotificationPulse();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700772 } else if (action.equals(Intent.ACTION_USER_STOPPED)) {
773 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
774 if (userHandle >= 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700775 cancelAllNotificationsInt(null, 0, 0, true, userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700776 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400777 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
778 // turn off LED when user passes through lock screen
779 mNotificationLight.turnOff();
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400780 } else if (action.equals(Intent.ACTION_USER_SWITCHED)) {
781 // reload per-user settings
782 mSettingsObserver.update(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
784 }
785 };
786
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700787 class SettingsObserver extends ContentObserver {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400788 private final Uri NOTIFICATION_LIGHT_PULSE_URI
789 = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE);
790
791 private final Uri ENABLED_NOTIFICATION_LISTENERS_URI
792 = Settings.System.getUriFor(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
793
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700794 SettingsObserver(Handler handler) {
795 super(handler);
796 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800797
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700798 void observe() {
799 ContentResolver resolver = mContext.getContentResolver();
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400800 resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI,
801 false, this);
802 resolver.registerContentObserver(ENABLED_NOTIFICATION_LISTENERS_URI,
803 false, this);
804 update(null);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700805 }
806
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400807 @Override public void onChange(boolean selfChange, Uri uri) {
808 update(uri);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700809 }
810
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400811 public void update(Uri uri) {
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700812 ContentResolver resolver = mContext.getContentResolver();
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400813 if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) {
814 boolean pulseEnabled = Settings.System.getInt(resolver,
815 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
816 if (mNotificationPulseEnabled != pulseEnabled) {
817 mNotificationPulseEnabled = pulseEnabled;
818 updateNotificationPulse();
819 }
820 }
821 if (uri == null || ENABLED_NOTIFICATION_LISTENERS_URI.equals(uri)) {
822 String pkglist = Settings.Secure.getString(
823 mContext.getContentResolver(),
824 Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
825 mEnabledListenersForCurrentUser.clear();
826 if (pkglist != null) {
827 String[] pkgs = pkglist.split(";");
828 for (int i=0; i<pkgs.length; i++) {
829 final String pkg = pkgs[i];
830 if (pkg != null && ! "".equals(pkg)) {
831 mEnabledListenersForCurrentUser.add(pkgs[i]);
832 }
833 }
834 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500835 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700836 }
837 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500838
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400839 private SettingsObserver mSettingsObserver;
840
Daniel Sandleredbb3802012-11-13 20:49:47 -0800841 static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) {
842 int[] ar = r.getIntArray(resid);
843 if (ar == null) {
844 return def;
845 }
846 final int len = ar.length > maxlen ? maxlen : ar.length;
847 long[] out = new long[len];
848 for (int i=0; i<len; i++) {
849 out[i] = ar[i];
850 }
851 return out;
852 }
853
Joe Onorato089de882010-04-12 08:18:45 -0700854 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500855 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 {
857 super();
858 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -0700859 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 mAm = ActivityManagerNative.getDefault();
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400861 mUserManager = (UserManager)context.getSystemService(Context.USER_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800864
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500865 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
866
867 importOldBlockDb();
Daniel Sandler0da673f2012-04-11 12:33:16 -0400868
Joe Onorato089de882010-04-12 08:18:45 -0700869 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 statusBar.setNotificationCallbacks(mNotificationCallbacks);
871
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500872 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
873 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
874
Mike Lockwood670f9322010-01-20 12:13:36 -0500875 Resources resources = mContext.getResources();
876 mDefaultNotificationColor = resources.getColor(
877 com.android.internal.R.color.config_defaultNotificationColor);
878 mDefaultNotificationLedOn = resources.getInteger(
879 com.android.internal.R.integer.config_defaultNotificationLedOn);
880 mDefaultNotificationLedOff = resources.getInteger(
881 com.android.internal.R.integer.config_defaultNotificationLedOff);
882
Daniel Sandleredbb3802012-11-13 20:49:47 -0800883 mDefaultVibrationPattern = getLongArray(resources,
884 com.android.internal.R.array.config_defaultNotificationVibePattern,
885 VIBRATE_PATTERN_MAXLEN,
886 DEFAULT_VIBRATE_PATTERN);
887
888 mFallbackVibrationPattern = getLongArray(resources,
889 com.android.internal.R.array.config_notificationFallbackVibePattern,
890 VIBRATE_PATTERN_MAXLEN,
891 DEFAULT_VIBRATE_PATTERN);
892
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400893 // Don't start allowing notifications until the setup wizard has run once.
894 // After that, including subsequent boots, init with notifications turned on.
895 // This works on the first boot because the setup wizard will toggle this
896 // flag at least once and we'll go back to 0 after that.
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700897 if (0 == Settings.Global.getInt(mContext.getContentResolver(),
898 Settings.Global.DEVICE_PROVISIONED, 0)) {
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400899 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
900 }
901
Mike Lockwood35e16bf2010-11-30 19:53:36 -0500902 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500904 filter.addAction(Intent.ACTION_SCREEN_ON);
905 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500906 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400907 filter.addAction(Intent.ACTION_USER_PRESENT);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700908 filter.addAction(Intent.ACTION_USER_STOPPED);
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400909 filter.addAction(Intent.ACTION_USER_SWITCHED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800911 IntentFilter pkgFilter = new IntentFilter();
912 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400913 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800914 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
915 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
916 pkgFilter.addDataScheme("package");
917 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800918 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800919 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800920
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400921 mSettingsObserver = new SettingsObserver(mHandler);
922 mSettingsObserver.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 }
924
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500925 /**
926 * Read the old XML-based app block database and import those blockages into the AppOps system.
927 */
928 private void importOldBlockDb() {
929 loadBlockDb();
930
931 PackageManager pm = mContext.getPackageManager();
932 for (String pkg : mBlockedPackages) {
933 PackageInfo info = null;
934 try {
935 info = pm.getPackageInfo(pkg, 0);
936 setNotificationsEnabledForPackage(pkg, info.applicationInfo.uid, false);
937 } catch (NameNotFoundException e) {
938 // forget you
939 }
940 }
941 mBlockedPackages.clear();
942 if (mPolicyFile != null) {
943 mPolicyFile.delete();
944 }
945 }
946
Joe Onorato30275482009-07-08 17:09:14 -0700947 void systemReady() {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700948 mAudioService = IAudioService.Stub.asInterface(
949 ServiceManager.getService(Context.AUDIO_SERVICE));
950
Joe Onorato30275482009-07-08 17:09:14 -0700951 // no beeping until we're basically done booting
952 mSystemReady = true;
953 }
954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 // Toasts
956 // ============================================================================
957 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
958 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400959 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960
961 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800962 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 return ;
964 }
965
Daniel Sandler0da673f2012-04-11 12:33:16 -0400966 final boolean isSystemToast = ("android".equals(pkg));
967
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500968 if (ENABLE_BLOCKED_TOASTS && !noteNotificationOp(pkg, Binder.getCallingUid())) {
969 if (!isSystemToast) {
970 Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
971 return;
972 }
Daniel Sandler0da673f2012-04-11 12:33:16 -0400973 }
974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 synchronized (mToastQueue) {
976 int callingPid = Binder.getCallingPid();
977 long callingId = Binder.clearCallingIdentity();
978 try {
979 ToastRecord record;
980 int index = indexOfToastLocked(pkg, callback);
981 // If it's already in the queue, we update it in place, we don't
982 // move it to the end of the queue.
983 if (index >= 0) {
984 record = mToastQueue.get(index);
985 record.update(duration);
986 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800987 // Limit the number of toasts that any given package except the android
988 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -0400989 if (!isSystemToast) {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800990 int count = 0;
991 final int N = mToastQueue.size();
992 for (int i=0; i<N; i++) {
993 final ToastRecord r = mToastQueue.get(i);
994 if (r.pkg.equals(pkg)) {
995 count++;
996 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
997 Slog.e(TAG, "Package has already posted " + count
998 + " toasts. Not showing more. Package=" + pkg);
999 return;
1000 }
1001 }
1002 }
1003 }
1004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 record = new ToastRecord(callingPid, pkg, callback, duration);
1006 mToastQueue.add(record);
1007 index = mToastQueue.size() - 1;
1008 keepProcessAliveLocked(callingPid);
1009 }
1010 // If it's at index 0, it's the current toast. It doesn't matter if it's
1011 // new or just been updated. Call back and tell it to show itself.
1012 // If the callback fails, this will remove it from the list, so don't
1013 // assume that it's valid after this.
1014 if (index == 0) {
1015 showNextToastLocked();
1016 }
1017 } finally {
1018 Binder.restoreCallingIdentity(callingId);
1019 }
1020 }
1021 }
1022
1023 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001024 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025
1026 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001027 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 return ;
1029 }
1030
1031 synchronized (mToastQueue) {
1032 long callingId = Binder.clearCallingIdentity();
1033 try {
1034 int index = indexOfToastLocked(pkg, callback);
1035 if (index >= 0) {
1036 cancelToastLocked(index);
1037 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001038 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 }
1040 } finally {
1041 Binder.restoreCallingIdentity(callingId);
1042 }
1043 }
1044 }
1045
1046 private void showNextToastLocked() {
1047 ToastRecord record = mToastQueue.get(0);
1048 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001049 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 try {
1051 record.callback.show();
1052 scheduleTimeoutLocked(record, false);
1053 return;
1054 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001055 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 + " in package " + record.pkg);
1057 // remove it from the list and let the process die
1058 int index = mToastQueue.indexOf(record);
1059 if (index >= 0) {
1060 mToastQueue.remove(index);
1061 }
1062 keepProcessAliveLocked(record.pid);
1063 if (mToastQueue.size() > 0) {
1064 record = mToastQueue.get(0);
1065 } else {
1066 record = null;
1067 }
1068 }
1069 }
1070 }
1071
1072 private void cancelToastLocked(int index) {
1073 ToastRecord record = mToastQueue.get(index);
1074 try {
1075 record.callback.hide();
1076 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001077 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 + " in package " + record.pkg);
1079 // don't worry about this, we're about to remove it from
1080 // the list anyway
1081 }
1082 mToastQueue.remove(index);
1083 keepProcessAliveLocked(record.pid);
1084 if (mToastQueue.size() > 0) {
1085 // Show the next one. If the callback fails, this will remove
1086 // it from the list, so don't assume that the list hasn't changed
1087 // after this point.
1088 showNextToastLocked();
1089 }
1090 }
1091
1092 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
1093 {
1094 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
1095 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
1096 mHandler.removeCallbacksAndMessages(r);
1097 mHandler.sendMessageDelayed(m, delay);
1098 }
1099
1100 private void handleTimeout(ToastRecord record)
1101 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001102 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 synchronized (mToastQueue) {
1104 int index = indexOfToastLocked(record.pkg, record.callback);
1105 if (index >= 0) {
1106 cancelToastLocked(index);
1107 }
1108 }
1109 }
1110
1111 // lock on mToastQueue
1112 private int indexOfToastLocked(String pkg, ITransientNotification callback)
1113 {
1114 IBinder cbak = callback.asBinder();
1115 ArrayList<ToastRecord> list = mToastQueue;
1116 int len = list.size();
1117 for (int i=0; i<len; i++) {
1118 ToastRecord r = list.get(i);
1119 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
1120 return i;
1121 }
1122 }
1123 return -1;
1124 }
1125
1126 // lock on mToastQueue
1127 private void keepProcessAliveLocked(int pid)
1128 {
1129 int toastCount = 0; // toasts from this pid
1130 ArrayList<ToastRecord> list = mToastQueue;
1131 int N = list.size();
1132 for (int i=0; i<N; i++) {
1133 ToastRecord r = list.get(i);
1134 if (r.pid == pid) {
1135 toastCount++;
1136 }
1137 }
1138 try {
1139 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
1140 } catch (RemoteException e) {
1141 // Shouldn't happen.
1142 }
1143 }
1144
1145 private final class WorkerHandler extends Handler
1146 {
1147 @Override
1148 public void handleMessage(Message msg)
1149 {
1150 switch (msg.what)
1151 {
1152 case MESSAGE_TIMEOUT:
1153 handleTimeout((ToastRecord)msg.obj);
1154 break;
1155 }
1156 }
1157 }
1158
1159
1160 // Notifications
1161 // ============================================================================
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001162 public void enqueueNotificationWithTag(String pkg, String basePkg, String tag, int id,
1163 Notification notification, int[] idOut, int userId)
Fred Quintana6ecaff12009-09-25 14:23:13 -07001164 {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001165 enqueueNotificationInternal(pkg, basePkg, Binder.getCallingUid(), Binder.getCallingPid(),
Dianne Hackborn41203752012-08-31 14:05:51 -07001166 tag, id, notification, idOut, userId);
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001167 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001168
1169 private final static int clamp(int x, int low, int high) {
1170 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -05001171 }
1172
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001173 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
1174 // uid/pid of another application)
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001175 public void enqueueNotificationInternal(String pkg, String basePkg, int callingUid,
1176 int callingPid, String tag, int id, Notification notification, int[] idOut, int userId)
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001177 {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001178 if (DBG) {
1179 Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
1180 }
1181 checkCallerIsSystemOrSameApp(pkg);
1182 final boolean isSystemNotification = ("android".equals(pkg));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001183
Dianne Hackborn41203752012-08-31 14:05:51 -07001184 userId = ActivityManager.handleIncomingUser(callingPid,
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001185 callingUid, userId, true, false, "enqueueNotification", pkg);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -07001186 final UserHandle user = new UserHandle(userId);
Dianne Hackborn41203752012-08-31 14:05:51 -07001187
Joe Onoratobd73d012010-06-04 11:44:54 -07001188 // Limit the number of notifications that any given package except the android
1189 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -04001190 if (!isSystemNotification) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001191 synchronized (mNotificationList) {
1192 int count = 0;
1193 final int N = mNotificationList.size();
1194 for (int i=0; i<N; i++) {
1195 final NotificationRecord r = mNotificationList.get(i);
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001196 if (r.sbn.pkg.equals(pkg) && r.sbn.getUserId() == userId) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001197 count++;
1198 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
1199 Slog.e(TAG, "Package has already posted " + count
1200 + " notifications. Not showing more. package=" + pkg);
1201 return;
1202 }
1203 }
1204 }
1205 }
1206 }
1207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 // This conditional is a dirty hack to limit the logging done on
1209 // behalf of the download manager without affecting other apps.
1210 if (!pkg.equals("com.android.providers.downloads")
1211 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001212 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001213 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 }
1215
1216 if (pkg == null || notification == null) {
1217 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
1218 + " id=" + id + " notification=" + notification);
1219 }
1220 if (notification.icon != 0) {
1221 if (notification.contentView == null) {
1222 throw new IllegalArgumentException("contentView required: pkg=" + pkg
1223 + " id=" + id + " notification=" + notification);
1224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 }
1226
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001227 // === Scoring ===
Daniel Sandler0da673f2012-04-11 12:33:16 -04001228
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001229 // 0. Sanitize inputs
1230 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
1231 // Migrate notification flags to scores
1232 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
1233 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -04001234 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001235 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
1236 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001237
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001238 // 1. initial score: buckets of 10, around the app
Daniel Sandler0da673f2012-04-11 12:33:16 -04001239 int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001240
Daniel Sandler0da673f2012-04-11 12:33:16 -04001241 // 2. Consult external heuristics (TBD)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001242
Daniel Sandler0da673f2012-04-11 12:33:16 -04001243 // 3. Apply local rules
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001244
1245 // blocked apps
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001246 if (ENABLE_BLOCKED_NOTIFICATIONS && !noteNotificationOp(pkg, callingUid)) {
1247 if (!isSystemNotification) {
1248 score = JUNK_SCORE;
1249 Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
1250 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001251 }
1252
1253 if (DBG) {
1254 Slog.v(TAG, "Assigned score=" + score + " to " + notification);
1255 }
1256
1257 if (score < SCORE_DISPLAY_THRESHOLD) {
1258 // Notification will be blocked because the score is too low.
1259 return;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001260 }
1261
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001262 // Should this notification make noise, vibe, or use the LED?
1263 final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD);
1264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 synchronized (mNotificationList) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001266 final StatusBarNotification n = new StatusBarNotification(
1267 pkg, id, tag, callingUid, callingPid, score, notification, user);
1268 NotificationRecord r = new NotificationRecord(n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 NotificationRecord old = null;
1270
Dianne Hackborn41203752012-08-31 14:05:51 -07001271 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 if (index < 0) {
1273 mNotificationList.add(r);
1274 } else {
1275 old = mNotificationList.remove(index);
1276 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001277 // Make sure we don't lose the foreground service state.
1278 if (old != null) {
1279 notification.flags |=
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001280 old.getNotification().flags&Notification.FLAG_FOREGROUND_SERVICE;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001283
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001284 // Ensure if this is a foreground service that the proper additional
1285 // flags are set.
1286 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
1287 notification.flags |= Notification.FLAG_ONGOING_EVENT
1288 | Notification.FLAG_NO_CLEAR;
1289 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001290
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001291 final int currentUser;
1292 final long token = Binder.clearCallingIdentity();
1293 try {
1294 currentUser = ActivityManager.getCurrentUser();
1295 } finally {
1296 Binder.restoreCallingIdentity(token);
1297 }
1298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 if (notification.icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 if (old != null && old.statusBarKey != null) {
1301 r.statusBarKey = old.statusBarKey;
1302 long identity = Binder.clearCallingIdentity();
1303 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001304 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 }
1306 finally {
1307 Binder.restoreCallingIdentity(identity);
1308 }
1309 } else {
1310 long identity = Binder.clearCallingIdentity();
1311 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001312 r.statusBarKey = mStatusBar.addNotification(n);
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001313 if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1314 && canInterrupt) {
Mike Lockwoodece18ef2012-02-13 20:42:19 -08001315 mAttentionLight.pulse();
1316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 }
1318 finally {
1319 Binder.restoreCallingIdentity(identity);
1320 }
1321 }
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001322 // Send accessibility events only for the current user.
1323 if (currentUser == userId) {
1324 sendAccessibilityEvent(notification, pkg);
1325 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001326
Daniel Sandler09a247e2013-02-14 10:24:17 -05001327 notifyPostedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001329 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 if (old != null && old.statusBarKey != null) {
1331 long identity = Binder.clearCallingIdentity();
1332 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001333 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 }
1335 finally {
1336 Binder.restoreCallingIdentity(identity);
1337 }
Daniel Sandler09a247e2013-02-14 10:24:17 -05001338
1339 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001341 return; // do not play sounds, show lights, etc. for invalid notifications
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 }
1343
1344 // If we're not supposed to beep, vibrate, etc. then don't.
1345 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
1346 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -07001347 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001348 && (r.getUserId() == UserHandle.USER_ALL ||
1349 (r.getUserId() == userId && r.getUserId() == currentUser))
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001350 && canInterrupt
Joe Onorato30275482009-07-08 17:09:14 -07001351 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -08001352
1353 final AudioManager audioManager = (AudioManager) mContext
1354 .getSystemService(Context.AUDIO_SERVICE);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 // sound
1357 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001358 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001359
1360 Uri soundUri = null;
1361 boolean hasValidSound = false;
1362
1363 if (useDefaultSound) {
1364 soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
1365
1366 // check to see if the default notification sound is silent
1367 ContentResolver resolver = mContext.getContentResolver();
1368 hasValidSound = Settings.System.getString(resolver,
1369 Settings.System.NOTIFICATION_SOUND) != null;
1370 } else if (notification.sound != null) {
1371 soundUri = notification.sound;
1372 hasValidSound = (soundUri != null);
1373 }
1374
1375 if (hasValidSound) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
1377 int audioStreamType;
1378 if (notification.audioStreamType >= 0) {
1379 audioStreamType = notification.audioStreamType;
1380 } else {
1381 audioStreamType = DEFAULT_STREAM_TYPE;
1382 }
1383 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -08001384 // do not play notifications if stream volume is 0
Jean-Michel Trivid6770542012-10-10 12:03:41 -07001385 // (typically because ringer mode is silent) or if speech recognition is active.
1386 if ((audioManager.getStreamVolume(audioStreamType) != 0)
1387 && !audioManager.isSpeechRecognitionActive()) {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001388 final long identity = Binder.clearCallingIdentity();
Eric Laurent524dc042009-11-27 05:07:55 -08001389 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001390 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1391 if (player != null) {
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001392 player.playAsync(soundUri, user, looping, audioStreamType);
Jeff Sharkey098d5802012-04-26 17:30:34 -07001393 }
1394 } catch (RemoteException e) {
1395 } finally {
Eric Laurent524dc042009-11-27 05:07:55 -08001396 Binder.restoreCallingIdentity(identity);
1397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 }
1399 }
1400
1401 // vibrate
Daniel Sandleredbb3802012-11-13 20:49:47 -08001402 // Does the notification want to specify its own vibration?
1403 final boolean hasCustomVibrate = notification.vibrate != null;
1404
David Agnew71789e12012-11-09 23:03:26 -05001405 // 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 -05001406 // and no other vibration is specified, we fall back to vibration
David Agnew71789e12012-11-09 23:03:26 -05001407 final boolean convertSoundToVibration =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001408 !hasCustomVibrate
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001409 && hasValidSound
David Agnew71789e12012-11-09 23:03:26 -05001410 && (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
1411
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001412 // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 final boolean useDefaultVibrate =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001414 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
David Agnew71789e12012-11-09 23:03:26 -05001415
Daniel Sandleredbb3802012-11-13 20:49:47 -08001416 if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
Eric Laurentbffc3d12012-05-07 17:43:49 -07001417 && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 mVibrateNotification = r;
1419
Daniel Sandleredbb3802012-11-13 20:49:47 -08001420 if (useDefaultVibrate || convertSoundToVibration) {
1421 // Escalate privileges so we can use the vibrator even if the notifying app
1422 // does not have the VIBRATE permission.
1423 long identity = Binder.clearCallingIdentity();
1424 try {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001425 mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg,
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001426 useDefaultVibrate ? mDefaultVibrationPattern
1427 : mFallbackVibrationPattern,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001428 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1429 } finally {
1430 Binder.restoreCallingIdentity(identity);
1431 }
1432 } else if (notification.vibrate.length > 1) {
1433 // If you want your own vibration pattern, you need the VIBRATE permission
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001434 mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg, notification.vibrate,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001435 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 }
1438 }
1439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 // light
1441 // the most recent thing gets the light
1442 mLights.remove(old);
1443 if (mLedNotification == old) {
1444 mLedNotification = null;
1445 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001446 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001448 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1449 && canInterrupt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 mLights.add(r);
1451 updateLightsLocked();
1452 } else {
1453 if (old != null
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001454 && ((old.getFlags() & Notification.FLAG_SHOW_LIGHTS) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 updateLightsLocked();
1456 }
1457 }
1458 }
1459
1460 idOut[0] = id;
1461 }
1462
Joe Onorato30275482009-07-08 17:09:14 -07001463 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001464 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
1465 if (!manager.isEnabled()) {
1466 return;
1467 }
1468
1469 AccessibilityEvent event =
1470 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
1471 event.setPackageName(packageName);
1472 event.setClassName(Notification.class.getName());
1473 event.setParcelableData(notification);
1474 CharSequence tickerText = notification.tickerText;
1475 if (!TextUtils.isEmpty(tickerText)) {
1476 event.getText().add(tickerText);
1477 }
1478
1479 manager.sendAccessibilityEvent(event);
1480 }
1481
Joe Onorato46439ce2010-11-19 13:56:21 -08001482 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
1483 // tell the app
1484 if (sendDelete) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001485 if (r.getNotification().deleteIntent != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001486 try {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001487 r.getNotification().deleteIntent.send();
Joe Onorato46439ce2010-11-19 13:56:21 -08001488 } catch (PendingIntent.CanceledException ex) {
1489 // do nothing - there's no relevant way to recover, and
1490 // no reason to let this propagate
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001491 Slog.w(TAG, "canceled PendingIntent for " + r.sbn.pkg, ex);
Joe Onorato46439ce2010-11-19 13:56:21 -08001492 }
1493 }
1494 }
1495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 // status bar
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001497 if (r.getNotification().icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 long identity = Binder.clearCallingIdentity();
1499 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001500 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 }
1502 finally {
1503 Binder.restoreCallingIdentity(identity);
1504 }
1505 r.statusBarKey = null;
Daniel Sandler09a247e2013-02-14 10:24:17 -05001506 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 }
1508
1509 // sound
1510 if (mSoundNotification == r) {
1511 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001512 final long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001514 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1515 if (player != null) {
1516 player.stopAsync();
1517 }
1518 } catch (RemoteException e) {
1519 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 Binder.restoreCallingIdentity(identity);
1521 }
1522 }
1523
1524 // vibrate
1525 if (mVibrateNotification == r) {
1526 mVibrateNotification = null;
1527 long identity = Binder.clearCallingIdentity();
1528 try {
1529 mVibrator.cancel();
1530 }
1531 finally {
1532 Binder.restoreCallingIdentity(identity);
1533 }
1534 }
1535
1536 // light
1537 mLights.remove(r);
1538 if (mLedNotification == r) {
1539 mLedNotification = null;
1540 }
Daniel Sandler23d7c702013-03-07 16:32:06 -05001541
1542 // Save it for users of getHistoricalNotifications()
1543 mArchive.record(r.sbn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 }
1545
1546 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001547 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001548 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 */
Fred Quintana6ecaff12009-09-25 14:23:13 -07001550 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001551 int mustNotHaveFlags, boolean sendDelete, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001552 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001553 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554
1555 synchronized (mNotificationList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001556 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001558 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001559
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001560 if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 return;
1562 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001563 if ((r.getNotification().flags & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001564 return;
1565 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 mNotificationList.remove(index);
1568
Joe Onorato46439ce2010-11-19 13:56:21 -08001569 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 updateLightsLocked();
1571 }
1572 }
1573 }
1574
1575 /**
Daniel Sandler321e9c52012-10-12 10:59:26 -07001576 * Determine whether the userId applies to the notification in question, either because
1577 * they match exactly, or one of them is USER_ALL (which is treated as a wildcard).
1578 */
1579 private boolean notificationMatchesUserId(NotificationRecord r, int userId) {
1580 return
1581 // looking for USER_ALL notifications? match everything
1582 userId == UserHandle.USER_ALL
1583 // a notification sent to USER_ALL matches any query
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001584 || r.getUserId() == UserHandle.USER_ALL
Daniel Sandler321e9c52012-10-12 10:59:26 -07001585 // an exact user match
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001586 || r.getUserId() == userId;
Daniel Sandler321e9c52012-10-12 10:59:26 -07001587 }
1588
1589 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 * Cancels all notifications from a given package that have all of the
1591 * {@code mustHaveFlags}.
1592 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001593 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001594 int mustNotHaveFlags, boolean doit, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001595 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId,
1596 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597
1598 synchronized (mNotificationList) {
1599 final int N = mNotificationList.size();
1600 boolean canceledSomething = false;
1601 for (int i = N-1; i >= 0; --i) {
1602 NotificationRecord r = mNotificationList.get(i);
Daniel Sandler321e9c52012-10-12 10:59:26 -07001603 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001604 continue;
1605 }
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001606 // Don't remove notifications to all, if there's no package name specified
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001607 if (r.getUserId() == UserHandle.USER_ALL && pkg == null) {
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001608 continue;
1609 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001610 if ((r.getFlags() & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 continue;
1612 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001613 if ((r.getFlags() & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001614 continue;
1615 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001616 if (pkg != null && !r.sbn.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 continue;
1618 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001619 canceledSomething = true;
1620 if (!doit) {
1621 return true;
1622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001624 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 }
1626 if (canceledSomething) {
1627 updateLightsLocked();
1628 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001629 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 }
1631 }
1632
Dianne Hackborn41203752012-08-31 14:05:51 -07001633 public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001634 checkCallerIsSystemOrSameApp(pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001635 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001636 Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001637 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001638 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001639 Binder.getCallingUid() == Process.SYSTEM_UID
Dianne Hackborn41203752012-08-31 14:05:51 -07001640 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 }
1642
Dianne Hackborn41203752012-08-31 14:05:51 -07001643 public void cancelAllNotifications(String pkg, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001644 checkCallerIsSystemOrSameApp(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001645
Dianne Hackborn41203752012-08-31 14:05:51 -07001646 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001647 Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001648
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001649 // Calling from user space, don't allow the canceling of actively
1650 // running foreground services.
Dianne Hackborn41203752012-08-31 14:05:51 -07001651 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 }
1653
Daniel Sandler0da673f2012-04-11 12:33:16 -04001654 void checkCallerIsSystem() {
1655 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001656 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001657 return;
1658 }
1659 throw new SecurityException("Disallowed call for uid " + uid);
1660 }
1661
1662 void checkCallerIsSystemOrSameApp(String pkg) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001663 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001664 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001665 return;
1666 }
1667 try {
Amith Yamasanif203aee2012-08-29 18:41:53 -07001668 ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
1669 pkg, 0, UserHandle.getCallingUserId());
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001670 if (!UserHandle.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001671 throw new SecurityException("Calling uid " + uid + " gave package"
1672 + pkg + " which is owned by uid " + ai.uid);
1673 }
Amith Yamasanif203aee2012-08-29 18:41:53 -07001674 } catch (RemoteException re) {
1675 throw new SecurityException("Unknown package " + pkg + "\n" + re);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001676 }
1677 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001678
Dianne Hackborn41203752012-08-31 14:05:51 -07001679 void cancelAll(int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 synchronized (mNotificationList) {
1681 final int N = mNotificationList.size();
1682 for (int i=N-1; i>=0; i--) {
1683 NotificationRecord r = mNotificationList.get(i);
1684
Daniel Sandler321e9c52012-10-12 10:59:26 -07001685 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001686 continue;
1687 }
1688
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001689 if ((r.getFlags() & (Notification.FLAG_ONGOING_EVENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001692 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 }
1694 }
1695
1696 updateLightsLocked();
1697 }
1698 }
1699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 // lock on mNotificationList
1701 private void updateLightsLocked()
1702 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001703 // handle notification lights
1704 if (mLedNotification == null) {
1705 // get next notification, if any
1706 int n = mLights.size();
1707 if (n > 0) {
1708 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 }
1710 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001711
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001712 // Don't flash while we are in a call or screen is on
1713 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001714 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001715 } else {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001716 final Notification ledno = mLedNotification.sbn.notification;
1717 int ledARGB = ledno.ledARGB;
1718 int ledOnMS = ledno.ledOnMS;
1719 int ledOffMS = ledno.ledOffMS;
1720 if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) {
Mike Lockwood670f9322010-01-20 12:13:36 -05001721 ledARGB = mDefaultNotificationColor;
1722 ledOnMS = mDefaultNotificationLedOn;
1723 ledOffMS = mDefaultNotificationLedOff;
1724 }
1725 if (mNotificationPulseEnabled) {
1726 // pulse repeatedly
1727 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1728 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05001729 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 }
1732
1733 // lock on mNotificationList
Dianne Hackborn41203752012-08-31 14:05:51 -07001734 private int indexOfNotificationLocked(String pkg, String tag, int id, int userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 {
1736 ArrayList<NotificationRecord> list = mNotificationList;
1737 final int len = list.size();
1738 for (int i=0; i<len; i++) {
1739 NotificationRecord r = list.get(i);
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001740 if (!notificationMatchesUserId(r, userId) || r.sbn.id != id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001741 continue;
1742 }
Fred Quintana6ecaff12009-09-25 14:23:13 -07001743 if (tag == null) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001744 if (r.sbn.tag != null) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001745 continue;
1746 }
1747 } else {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001748 if (!tag.equals(r.sbn.tag)) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001749 continue;
1750 }
1751 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001752 if (r.sbn.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 return i;
1754 }
1755 }
1756 return -1;
1757 }
1758
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001759 private void updateNotificationPulse() {
1760 synchronized (mNotificationList) {
1761 updateLightsLocked();
1762 }
1763 }
1764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 // ======================================================================
1766 @Override
1767 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1768 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1769 != PackageManager.PERMISSION_GRANTED) {
1770 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1771 + Binder.getCallingPid()
1772 + ", uid=" + Binder.getCallingUid());
1773 return;
1774 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 pw.println("Current Notification Manager state:");
1777
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001778 pw.print(" Enabled listeners: [");
1779 for (String pkg : mEnabledListenersForCurrentUser) {
1780 pw.print(" " + pkg);
1781 }
1782 pw.println(" ]");
1783
1784 pw.println(" Live listeners:");
1785 for (NotificationListenerInfo info : mListeners) {
1786 pw.println(" " + info.pkg + " (user " + info.userid + "): " + info.listener
1787 + (info.isSystem?" SYSTEM":""));
1788 }
1789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 int N;
1791
1792 synchronized (mToastQueue) {
1793 N = mToastQueue.size();
1794 if (N > 0) {
1795 pw.println(" Toast Queue:");
1796 for (int i=0; i<N; i++) {
1797 mToastQueue.get(i).dump(pw, " ");
1798 }
1799 pw.println(" ");
1800 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 }
1803
1804 synchronized (mNotificationList) {
1805 N = mNotificationList.size();
1806 if (N > 0) {
1807 pw.println(" Notification List:");
1808 for (int i=0; i<N; i++) {
1809 mNotificationList.get(i).dump(pw, " ", mContext);
1810 }
1811 pw.println(" ");
1812 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 N = mLights.size();
1815 if (N > 0) {
1816 pw.println(" Lights List:");
1817 for (int i=0; i<N; i++) {
1818 mLights.get(i).dump(pw, " ", mContext);
1819 }
1820 pw.println(" ");
1821 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 pw.println(" mSoundNotification=" + mSoundNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001825 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1826 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 }
1828 }
1829}