| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 19 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
| 20 | import static org.xmlpull.v1.XmlPullParser.END_TAG; |
| 21 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 22 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.app.ActivityManagerNative; |
| Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 25 | import android.app.AppGlobals; |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 26 | import android.app.AppOpsManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.app.IActivityManager; |
| 28 | import android.app.INotificationManager; |
| 29 | import android.app.ITransientNotification; |
| 30 | import android.app.Notification; |
| 31 | import android.app.PendingIntent; |
| 32 | import android.app.StatusBarManager; |
| 33 | import android.content.BroadcastReceiver; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 34 | import android.content.ComponentName; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 35 | import android.content.ContentResolver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.Context; |
| 37 | import android.content.Intent; |
| 38 | import android.content.IntentFilter; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 39 | import android.content.ServiceConnection; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 40 | import android.content.pm.ApplicationInfo; |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 41 | import android.content.pm.PackageInfo; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.content.pm.PackageManager; |
| 43 | import android.content.pm.PackageManager.NameNotFoundException; |
| 44 | import android.content.res.Resources; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 45 | import android.database.ContentObserver; |
| Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 46 | import android.graphics.Bitmap; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 47 | import android.media.AudioManager; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 48 | import android.media.IAudioService; |
| 49 | import android.media.IRingtonePlayer; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.net.Uri; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.os.Binder; |
| Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 52 | import android.os.Bundle; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.os.Handler; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.os.IBinder; |
| 55 | import android.os.Message; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 56 | import android.os.Process; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 57 | import android.os.RemoteException; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 58 | import android.os.ServiceManager; |
| Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 59 | import android.os.UserHandle; |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 60 | import android.os.UserManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.os.Vibrator; |
| 62 | import android.provider.Settings; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 63 | import android.service.notification.INotificationListener; |
| 64 | import android.service.notification.NotificationListenerService; |
| 65 | import android.service.notification.StatusBarNotification; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 66 | import android.telephony.TelephonyManager; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 67 | import android.text.TextUtils; |
| Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 68 | import android.util.AtomicFile; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | import android.util.EventLog; |
| 70 | import android.util.Log; |
| Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 71 | import android.util.Slog; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 72 | import android.util.Xml; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 73 | import android.view.accessibility.AccessibilityEvent; |
| 74 | import android.view.accessibility.AccessibilityManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import android.widget.Toast; |
| 76 | |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 77 | import org.xmlpull.v1.XmlPullParser; |
| 78 | import org.xmlpull.v1.XmlPullParserException; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 79 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 80 | import java.io.File; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | import java.io.FileDescriptor; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 82 | import java.io.FileInputStream; |
| 83 | import java.io.FileNotFoundException; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 84 | import java.io.IOException; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | import java.io.PrintWriter; |
| Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 86 | import java.lang.reflect.Array; |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 87 | import java.util.ArrayDeque; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | import java.util.ArrayList; |
| 89 | import java.util.Arrays; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 90 | import java.util.HashSet; |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 91 | import java.util.Iterator; |
| 92 | import java.util.NoSuchElementException; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 93 | |
| 94 | import libcore.io.IoUtils; |
| 95 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 97 | /** {@hide} */ |
| 98 | public class NotificationManagerService extends INotificationManager.Stub |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | { |
| 100 | private static final String TAG = "NotificationService"; |
| 101 | private static final boolean DBG = false; |
| 102 | |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 103 | private static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
| 104 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | // message codes |
| 106 | private static final int MESSAGE_TIMEOUT = 2; |
| 107 | |
| 108 | private static final int LONG_DELAY = 3500; // 3.5 seconds |
| 109 | private static final int SHORT_DELAY = 2000; // 2 seconds |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 110 | |
| 111 | private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 112 | private static final int VIBRATE_PATTERN_MAXLEN = 8 * 2 + 1; // up to eight bumps |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | |
| 114 | private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
| Daniel Sandler | 49a2ad1 | 2012-03-28 15:46:39 -0400 | [diff] [blame] | 115 | private static final boolean SCORE_ONGOING_HIGHER = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 117 | private static final int JUNK_SCORE = -1000; |
| 118 | private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10; |
| 119 | private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER; |
| 120 | |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 121 | // Notifications with scores below this will not interrupt the user, either via LED or |
| 122 | // sound or vibration |
| 123 | private static final int SCORE_INTERRUPTION_THRESHOLD = |
| 124 | Notification.PRIORITY_LOW * NOTIFICATION_PRIORITY_MULTIPLIER; |
| 125 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 126 | private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true; |
| 127 | private static final boolean ENABLE_BLOCKED_TOASTS = true; |
| 128 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 129 | private static final String ENABLED_NOTIFICATION_LISTENERS_SEPARATOR = ":"; |
| 130 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | final Context mContext; |
| 132 | final IActivityManager mAm; |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 133 | final UserManager mUserManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | final IBinder mForegroundToken = new Binder(); |
| 135 | |
| 136 | private WorkerHandler mHandler; |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 137 | private StatusBarManagerService mStatusBar; |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 138 | private LightsService.Light mNotificationLight; |
| 139 | private LightsService.Light mAttentionLight; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 141 | private int mDefaultNotificationColor; |
| 142 | private int mDefaultNotificationLedOn; |
| 143 | private int mDefaultNotificationLedOff; |
| 144 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 145 | private long[] mDefaultVibrationPattern; |
| 146 | private long[] mFallbackVibrationPattern; |
| 147 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 148 | private boolean mSystemReady; |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 149 | private int mDisabledNotifications; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 151 | private NotificationRecord mSoundNotification; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | private NotificationRecord mVibrateNotification; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 153 | |
| 154 | private IAudioService mAudioService; |
| Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 155 | private Vibrator mVibrator; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 157 | // for enabling and disabling notification pulse behavior |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 158 | private boolean mScreenOn = true; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 159 | private boolean mInCall = false; |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 160 | private boolean mNotificationPulseEnabled; |
| 161 | |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 162 | // used as a mutex for access to all active notifications & listeners |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 163 | private final ArrayList<NotificationRecord> mNotificationList = |
| 164 | new ArrayList<NotificationRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | |
| 166 | private ArrayList<ToastRecord> mToastQueue; |
| 167 | |
| 168 | private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | private NotificationRecord mLedNotification; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 170 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 171 | private final AppOpsManager mAppOps; |
| 172 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 173 | // contains connections to all connected listeners, including app services |
| 174 | // and system listeners |
| 175 | private ArrayList<NotificationListenerInfo> mListeners |
| 176 | = new ArrayList<NotificationListenerInfo>(); |
| 177 | // things that will be put into mListeners as soon as they're ready |
| 178 | private ArrayList<String> mServicesBinding = new ArrayList<String>(); |
| 179 | // lists the component names of all enabled (and therefore connected) listener |
| 180 | // app services for the current user only |
| 181 | private HashSet<ComponentName> mEnabledListenersForCurrentUser |
| 182 | = new HashSet<ComponentName>(); |
| 183 | // Just the packages from mEnabledListenersForCurrentUser |
| 184 | private HashSet<String> mEnabledListenerPackageNames = new HashSet<String>(); |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 185 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 186 | // Notification control database. For now just contains disabled packages. |
| 187 | private AtomicFile mPolicyFile; |
| 188 | private HashSet<String> mBlockedPackages = new HashSet<String>(); |
| 189 | |
| 190 | private static final int DB_VERSION = 1; |
| 191 | |
| 192 | private static final String TAG_BODY = "notification-policy"; |
| 193 | private static final String ATTR_VERSION = "version"; |
| 194 | |
| 195 | private static final String TAG_BLOCKED_PKGS = "blocked-packages"; |
| 196 | private static final String TAG_PACKAGE = "package"; |
| 197 | private static final String ATTR_NAME = "name"; |
| 198 | |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 199 | private class NotificationListenerInfo implements DeathRecipient { |
| 200 | INotificationListener listener; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 201 | ComponentName component; |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 202 | int userid; |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 203 | boolean isSystem; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 204 | ServiceConnection connection; |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 205 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 206 | public NotificationListenerInfo(INotificationListener listener, ComponentName component, |
| 207 | int userid, boolean isSystem) { |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 208 | this.listener = listener; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 209 | this.component = component; |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 210 | this.userid = userid; |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 211 | this.isSystem = isSystem; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 212 | this.connection = null; |
| 213 | } |
| 214 | |
| 215 | public NotificationListenerInfo(INotificationListener listener, ComponentName component, |
| 216 | int userid, ServiceConnection connection) { |
| 217 | this.listener = listener; |
| 218 | this.component = component; |
| 219 | this.userid = userid; |
| 220 | this.isSystem = false; |
| 221 | this.connection = connection; |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 222 | } |
| 223 | |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 224 | boolean enabledAndUserMatches(StatusBarNotification sbn) { |
| 225 | final int nid = sbn.getUserId(); |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 226 | if (!isEnabledForCurrentUser()) { |
| 227 | return false; |
| 228 | } |
| Daniel Sandler | 21ca44d | 2013-03-07 13:58:00 -0500 | [diff] [blame] | 229 | if (this.userid == UserHandle.USER_ALL) return true; |
| Daniel Sandler | 21ca44d | 2013-03-07 13:58:00 -0500 | [diff] [blame] | 230 | return (nid == UserHandle.USER_ALL || nid == this.userid); |
| 231 | } |
| 232 | |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 233 | public void notifyPostedIfUserMatch(StatusBarNotification sbn) { |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 234 | if (!enabledAndUserMatches(sbn)) { |
| 235 | return; |
| 236 | } |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 237 | try { |
| 238 | listener.onNotificationPosted(sbn); |
| 239 | } catch (RemoteException ex) { |
| 240 | // not there? |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | public void notifyRemovedIfUserMatch(StatusBarNotification sbn) { |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 245 | if (!enabledAndUserMatches(sbn)) return; |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 246 | try { |
| 247 | listener.onNotificationRemoved(sbn); |
| 248 | } catch (RemoteException ex) { |
| 249 | // not there? |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public void binderDied() { |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 255 | if (connection == null) { |
| 256 | // This is not a service; it won't be recreated. We can give up this connection. |
| 257 | unregisterListener(this.listener, this.userid); |
| 258 | } |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 259 | } |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 260 | |
| 261 | /** convenience method for looking in mEnabledListenersForCurrentUser */ |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 262 | public boolean isEnabledForCurrentUser() { |
| 263 | if (this.isSystem) return true; |
| 264 | if (this.connection == null) return false; |
| 265 | return mEnabledListenersForCurrentUser.contains(this.component); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 266 | } |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 267 | } |
| 268 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 269 | private static class Archive { |
| Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 270 | static final int BUFFER_SIZE = 250; |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 271 | ArrayDeque<StatusBarNotification> mBuffer = new ArrayDeque<StatusBarNotification>(BUFFER_SIZE); |
| 272 | |
| 273 | public Archive() { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 274 | } |
| Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 275 | |
| Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 276 | public String toString() { |
| 277 | final StringBuilder sb = new StringBuilder(); |
| 278 | final int N = mBuffer.size(); |
| 279 | sb.append("Archive ("); |
| 280 | sb.append(N); |
| 281 | sb.append(" notification"); |
| 282 | sb.append((N==1)?")":"s)"); |
| 283 | return sb.toString(); |
| 284 | } |
| 285 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 286 | public void record(StatusBarNotification nr) { |
| Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 287 | // Nuke heavy parts of notification before storing in archive |
| 288 | nr.notification.tickerView = null; |
| 289 | nr.notification.contentView = null; |
| 290 | nr.notification.bigContentView = null; |
| 291 | nr.notification.largeIcon = null; |
| Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 292 | final Bundle extras = nr.notification.extras; |
| 293 | extras.remove(Notification.EXTRA_LARGE_ICON); |
| 294 | extras.remove(Notification.EXTRA_LARGE_ICON_BIG); |
| 295 | extras.remove(Notification.EXTRA_PICTURE); |
| Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 296 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 297 | if (mBuffer.size() == BUFFER_SIZE) { |
| 298 | mBuffer.removeFirst(); |
| 299 | } |
| 300 | mBuffer.addLast(nr); |
| 301 | } |
| 302 | |
| Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 303 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 304 | public void clear() { |
| 305 | mBuffer.clear(); |
| 306 | } |
| 307 | |
| 308 | public Iterator<StatusBarNotification> descendingIterator() { |
| 309 | return mBuffer.descendingIterator(); |
| 310 | } |
| 311 | public Iterator<StatusBarNotification> ascendingIterator() { |
| 312 | return mBuffer.iterator(); |
| 313 | } |
| 314 | public Iterator<StatusBarNotification> filter( |
| 315 | final Iterator<StatusBarNotification> iter, final String pkg, final int userId) { |
| 316 | return new Iterator<StatusBarNotification>() { |
| 317 | StatusBarNotification mNext = findNext(); |
| 318 | |
| 319 | private StatusBarNotification findNext() { |
| 320 | while (iter.hasNext()) { |
| 321 | StatusBarNotification nr = iter.next(); |
| 322 | if ((pkg == null || nr.pkg == pkg) |
| 323 | && (userId == UserHandle.USER_ALL || nr.getUserId() == userId)) { |
| 324 | return nr; |
| 325 | } |
| 326 | } |
| 327 | return null; |
| 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public boolean hasNext() { |
| 332 | return mNext == null; |
| 333 | } |
| 334 | |
| 335 | @Override |
| 336 | public StatusBarNotification next() { |
| 337 | StatusBarNotification next = mNext; |
| 338 | if (next == null) { |
| 339 | throw new NoSuchElementException(); |
| 340 | } |
| 341 | mNext = findNext(); |
| 342 | return next; |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public void remove() { |
| 347 | iter.remove(); |
| 348 | } |
| 349 | }; |
| 350 | } |
| Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 351 | |
| 352 | public StatusBarNotification[] getArray(int count) { |
| 353 | if (count == 0) count = Archive.BUFFER_SIZE; |
| 354 | final StatusBarNotification[] a |
| 355 | = new StatusBarNotification[Math.min(count, mBuffer.size())]; |
| 356 | Iterator<StatusBarNotification> iter = descendingIterator(); |
| 357 | int i=0; |
| 358 | while (iter.hasNext() && i < count) { |
| 359 | a[i++] = iter.next(); |
| 360 | } |
| 361 | return a; |
| 362 | } |
| 363 | |
| 364 | public StatusBarNotification[] getArray(int count, String pkg, int userId) { |
| 365 | if (count == 0) count = Archive.BUFFER_SIZE; |
| 366 | final StatusBarNotification[] a |
| 367 | = new StatusBarNotification[Math.min(count, mBuffer.size())]; |
| 368 | Iterator<StatusBarNotification> iter = filter(descendingIterator(), pkg, userId); |
| 369 | int i=0; |
| 370 | while (iter.hasNext() && i < count) { |
| 371 | a[i++] = iter.next(); |
| 372 | } |
| 373 | return a; |
| 374 | } |
| 375 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | Archive mArchive = new Archive(); |
| 379 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 380 | private void loadBlockDb() { |
| 381 | synchronized(mBlockedPackages) { |
| 382 | if (mPolicyFile == null) { |
| 383 | File dir = new File("/data/system"); |
| 384 | mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml")); |
| 385 | |
| 386 | mBlockedPackages.clear(); |
| 387 | |
| 388 | FileInputStream infile = null; |
| 389 | try { |
| 390 | infile = mPolicyFile.openRead(); |
| 391 | final XmlPullParser parser = Xml.newPullParser(); |
| 392 | parser.setInput(infile, null); |
| 393 | |
| 394 | int type; |
| 395 | String tag; |
| 396 | int version = DB_VERSION; |
| 397 | while ((type = parser.next()) != END_DOCUMENT) { |
| 398 | tag = parser.getName(); |
| 399 | if (type == START_TAG) { |
| 400 | if (TAG_BODY.equals(tag)) { |
| 401 | version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION)); |
| 402 | } else if (TAG_BLOCKED_PKGS.equals(tag)) { |
| 403 | while ((type = parser.next()) != END_DOCUMENT) { |
| 404 | tag = parser.getName(); |
| 405 | if (TAG_PACKAGE.equals(tag)) { |
| 406 | mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME)); |
| 407 | } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) { |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | } catch (FileNotFoundException e) { |
| 415 | // No data yet |
| 416 | } catch (IOException e) { |
| 417 | Log.wtf(TAG, "Unable to read blocked notifications database", e); |
| 418 | } catch (NumberFormatException e) { |
| 419 | Log.wtf(TAG, "Unable to parse blocked notifications database", e); |
| 420 | } catch (XmlPullParserException e) { |
| 421 | Log.wtf(TAG, "Unable to parse blocked notifications database", e); |
| 422 | } finally { |
| 423 | IoUtils.closeQuietly(infile); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 429 | /** |
| 430 | * Use this when you just want to know if notifications are OK for this package. |
| 431 | */ |
| 432 | public boolean areNotificationsEnabledForPackage(String pkg, int uid) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 433 | checkCallerIsSystem(); |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 434 | return (mAppOps.checkOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg) |
| 435 | == AppOpsManager.MODE_ALLOWED); |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 436 | } |
| 437 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 438 | /** Use this when you actually want to post a notification or toast. |
| 439 | * |
| 440 | * Unchecked. Not exposed via Binder, but can be called in the course of enqueue*(). |
| 441 | */ |
| 442 | private boolean noteNotificationOp(String pkg, int uid) { |
| 443 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg) |
| 444 | != AppOpsManager.MODE_ALLOWED) { |
| 445 | Slog.v(TAG, "notifications are disabled by AppOps for " + pkg); |
| 446 | return false; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 447 | } |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 448 | return true; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 449 | } |
| 450 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 451 | public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 452 | checkCallerIsSystem(); |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 453 | if (true||DBG) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 454 | Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg); |
| 455 | } |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 456 | mAppOps.setMode(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg, |
| 457 | enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED); |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | private static String idDebugString(Context baseContext, String packageName, int id) { |
| 462 | Context c = null; |
| 463 | |
| 464 | if (packageName != null) { |
| 465 | try { |
| 466 | c = baseContext.createPackageContext(packageName, 0); |
| 467 | } catch (NameNotFoundException e) { |
| 468 | c = baseContext; |
| 469 | } |
| 470 | } else { |
| 471 | c = baseContext; |
| 472 | } |
| 473 | |
| 474 | String pkg; |
| 475 | String type; |
| 476 | String name; |
| 477 | |
| 478 | Resources r = c.getResources(); |
| 479 | try { |
| 480 | return r.getResourceName(id); |
| 481 | } catch (Resources.NotFoundException e) { |
| 482 | return "<name unknown>"; |
| 483 | } |
| 484 | } |
| 485 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 486 | /** |
| 487 | * System-only API for getting a list of current (i.e. not cleared) notifications. |
| 488 | * |
| 489 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
| 490 | */ |
| 491 | @Override |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 492 | public StatusBarNotification[] getActiveNotifications(String callingPkg) { |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 493 | // enforce() will ensure the calling uid has the correct permission |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 494 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 495 | "NotificationManagerService.getActiveNotifications"); |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 496 | |
| 497 | StatusBarNotification[] tmp = null; |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 498 | int uid = Binder.getCallingUid(); |
| 499 | |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 500 | // noteOp will check to make sure the callingPkg matches the uid |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 501 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 502 | == AppOpsManager.MODE_ALLOWED) { |
| 503 | synchronized (mNotificationList) { |
| 504 | tmp = new StatusBarNotification[mNotificationList.size()]; |
| 505 | final int N = mNotificationList.size(); |
| 506 | for (int i=0; i<N; i++) { |
| 507 | tmp[i] = mNotificationList.get(i).sbn; |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | return tmp; |
| 512 | } |
| 513 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 514 | /** |
| 515 | * System-only API for getting a list of recent (cleared, no longer shown) notifications. |
| 516 | * |
| 517 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
| 518 | */ |
| 519 | @Override |
| Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 520 | public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) { |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 521 | // enforce() will ensure the calling uid has the correct permission |
| Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 522 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 523 | "NotificationManagerService.getHistoricalNotifications"); |
| 524 | |
| 525 | StatusBarNotification[] tmp = null; |
| 526 | int uid = Binder.getCallingUid(); |
| 527 | |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 528 | // noteOp will check to make sure the callingPkg matches the uid |
| Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 529 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 530 | == AppOpsManager.MODE_ALLOWED) { |
| 531 | synchronized (mArchive) { |
| 532 | tmp = mArchive.getArray(count); |
| 533 | } |
| 534 | } |
| 535 | return tmp; |
| 536 | } |
| 537 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 538 | /** |
| 539 | * Called whenever packages change, the user switches, or ENABLED_NOTIFICATION_LISTENERS |
| 540 | * is altered. (For example in response to USER_SWITCHED in our broadcast receiver) |
| 541 | */ |
| 542 | void rebindListenerServices() { |
| 543 | String flat = Settings.Secure.getString( |
| 544 | mContext.getContentResolver(), |
| 545 | Settings.Secure.ENABLED_NOTIFICATION_LISTENERS); |
| 546 | |
| 547 | NotificationListenerInfo[] toRemove = new NotificationListenerInfo[mListeners.size()]; |
| 548 | final ArrayList<ComponentName> toAdd; |
| 549 | final int currentUser = ActivityManager.getCurrentUser(); |
| 550 | |
| 551 | synchronized (mNotificationList) { |
| 552 | // unbind and remove all existing listeners |
| 553 | toRemove = mListeners.toArray(toRemove); |
| 554 | |
| 555 | toAdd = new ArrayList<ComponentName>(); |
| 556 | final HashSet<ComponentName> newEnabled = new HashSet<ComponentName>(); |
| 557 | final HashSet<String> newPackages = new HashSet<String>(); |
| 558 | |
| 559 | // decode the list of components |
| 560 | if (flat != null) { |
| 561 | String[] components = flat.split(ENABLED_NOTIFICATION_LISTENERS_SEPARATOR); |
| 562 | for (int i=0; i<components.length; i++) { |
| 563 | final ComponentName component |
| 564 | = ComponentName.unflattenFromString(components[i]); |
| 565 | if (component != null) { |
| 566 | newEnabled.add(component); |
| 567 | toAdd.add(component); |
| 568 | newPackages.add(component.getPackageName()); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | mEnabledListenersForCurrentUser = newEnabled; |
| 573 | mEnabledListenerPackageNames = newPackages; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | for (NotificationListenerInfo info : toRemove) { |
| 578 | final ComponentName component = info.component; |
| 579 | final int oldUser = info.userid; |
| 580 | Slog.v(TAG, "disabling notification listener for user " + oldUser + ": " + component); |
| 581 | unregisterListenerService(component, info.userid); |
| 582 | } |
| 583 | |
| 584 | final int N = toAdd.size(); |
| 585 | for (int i=0; i<N; i++) { |
| 586 | final ComponentName component = toAdd.get(i); |
| 587 | Slog.v(TAG, "enabling notification listener for user " + currentUser + ": " |
| 588 | + component); |
| 589 | registerListenerService(component, currentUser); |
| 590 | } |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 591 | } |
| 592 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 593 | /** |
| 594 | * Register a listener binder directly with the notification manager. |
| 595 | * |
| 596 | * Only works with system callers. Apps should extend |
| 597 | * {@link android.service.notification.NotificationListenerService}. |
| 598 | */ |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 599 | @Override |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 600 | public void registerListener(final INotificationListener listener, |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 601 | final ComponentName component, final int userid) { |
| 602 | checkCallerIsSystem(); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 603 | |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 604 | synchronized (mNotificationList) { |
| 605 | try { |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 606 | NotificationListenerInfo info |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 607 | = new NotificationListenerInfo(listener, component, userid, true); |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 608 | listener.asBinder().linkToDeath(info, 0); |
| 609 | mListeners.add(info); |
| 610 | } catch (RemoteException e) { |
| 611 | // already dead |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 616 | /** |
| 617 | * Version of registerListener that takes the name of a |
| 618 | * {@link android.service.notification.NotificationListenerService} to bind to. |
| 619 | * |
| 620 | * This is the mechanism by which third parties may subscribe to notifications. |
| 621 | */ |
| 622 | private void registerListenerService(final ComponentName name, final int userid) { |
| 623 | checkCallerIsSystem(); |
| 624 | |
| 625 | if (DBG) Slog.v(TAG, "registerListenerService: " + name + " u=" + userid); |
| 626 | |
| 627 | synchronized (mNotificationList) { |
| 628 | final String servicesBindingTag = name.toString() + "/" + userid; |
| 629 | if (mServicesBinding.contains(servicesBindingTag)) { |
| 630 | // stop registering this thing already! we're working on it |
| 631 | return; |
| 632 | } |
| 633 | mServicesBinding.add(servicesBindingTag); |
| 634 | |
| 635 | final int N = mListeners.size(); |
| 636 | for (int i=N-1; i>=0; i--) { |
| 637 | final NotificationListenerInfo info = mListeners.get(i); |
| 638 | if (name.equals(info.component) |
| 639 | && info.userid == userid) { |
| 640 | // cut old connections |
| 641 | if (DBG) Slog.v(TAG, " disconnecting old listener: " + info.listener); |
| 642 | mListeners.remove(i); |
| 643 | if (info.connection != null) { |
| 644 | mContext.unbindService(info.connection); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | Intent intent = new Intent(NotificationListenerService.SERVICE_INTERFACE); |
| 650 | intent.setComponent(name); |
| 651 | |
| 652 | intent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 653 | com.android.internal.R.string.notification_listener_binding_label); |
| 654 | intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity( |
| 655 | mContext, 0, new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS), 0)); |
| 656 | |
| 657 | try { |
| 658 | if (DBG) Slog.v(TAG, "binding: " + intent); |
| 659 | if (!mContext.bindServiceAsUser(intent, |
| 660 | new ServiceConnection() { |
| 661 | INotificationListener mListener; |
| 662 | @Override |
| 663 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 664 | synchronized (mNotificationList) { |
| 665 | mServicesBinding.remove(servicesBindingTag); |
| 666 | try { |
| 667 | mListener = INotificationListener.Stub.asInterface(service); |
| 668 | NotificationListenerInfo info = new NotificationListenerInfo( |
| 669 | mListener, name, userid, this); |
| 670 | service.linkToDeath(info, 0); |
| 671 | mListeners.add(info); |
| 672 | } catch (RemoteException e) { |
| 673 | // already dead |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | @Override |
| 679 | public void onServiceDisconnected(ComponentName name) { |
| 680 | Slog.v(TAG, "notification listener connection lost: " + name); |
| 681 | } |
| 682 | }, |
| 683 | Context.BIND_AUTO_CREATE, |
| 684 | new UserHandle(userid))) |
| 685 | { |
| 686 | mServicesBinding.remove(servicesBindingTag); |
| 687 | Slog.w(TAG, "Unable to bind listener service: " + intent); |
| 688 | return; |
| 689 | } |
| 690 | } catch (SecurityException ex) { |
| 691 | Slog.e(TAG, "Unable to bind listener service: " + intent, ex); |
| 692 | return; |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * Remove a listener binder directly |
| 699 | */ |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 700 | @Override |
| 701 | public void unregisterListener(INotificationListener listener, int userid) { |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 702 | // no need to check permissions; if your listener binder is in the list, |
| 703 | // that's proof that you had permission to add it in the first place |
| 704 | |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 705 | synchronized (mNotificationList) { |
| 706 | final int N = mListeners.size(); |
| 707 | for (int i=N-1; i>=0; i--) { |
| 708 | final NotificationListenerInfo info = mListeners.get(i); |
| 709 | if (info.listener == listener && info.userid == userid) { |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 710 | mListeners.remove(i); |
| 711 | if (info.connection != null) { |
| 712 | mContext.unbindService(info.connection); |
| 713 | } |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 719 | /** |
| 720 | * Remove a listener service for the given user by ComponentName |
| 721 | */ |
| 722 | private void unregisterListenerService(ComponentName name, int userid) { |
| 723 | checkCallerIsSystem(); |
| 724 | |
| 725 | synchronized (mNotificationList) { |
| 726 | final int N = mListeners.size(); |
| 727 | for (int i=N-1; i>=0; i--) { |
| 728 | final NotificationListenerInfo info = mListeners.get(i); |
| 729 | if (name.equals(info.component) |
| 730 | && info.userid == userid) { |
| 731 | mListeners.remove(i); |
| 732 | if (info.connection != null) { |
| 733 | mContext.unbindService(info.connection); |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * asynchronously notify all listeners about a new notification |
| 742 | */ |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 743 | private void notifyPostedLocked(NotificationRecord n) { |
| 744 | final StatusBarNotification sbn = n.sbn; |
| 745 | for (final NotificationListenerInfo info : mListeners) { |
| 746 | mHandler.post(new Runnable() { |
| 747 | @Override |
| 748 | public void run() { |
| 749 | info.notifyPostedIfUserMatch(sbn); |
| 750 | }}); |
| 751 | } |
| 752 | } |
| 753 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 754 | /** |
| 755 | * asynchronously notify all listeners about a removed notification |
| 756 | */ |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 757 | private void notifyRemovedLocked(NotificationRecord n) { |
| 758 | final StatusBarNotification sbn = n.sbn; |
| 759 | for (final NotificationListenerInfo info : mListeners) { |
| 760 | mHandler.post(new Runnable() { |
| 761 | @Override |
| 762 | public void run() { |
| 763 | info.notifyRemovedIfUserMatch(sbn); |
| 764 | }}); |
| 765 | } |
| 766 | } |
| 767 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 768 | // -- APIs to support listeners clicking/clearing notifications -- |
| 769 | |
| 770 | private NotificationListenerInfo checkListenerToken(INotificationListener listener) { |
| 771 | final IBinder token = listener.asBinder(); |
| 772 | final int N = mListeners.size(); |
| 773 | for (int i=0; i<N; i++) { |
| 774 | final NotificationListenerInfo info = mListeners.get(i); |
| 775 | if (info.listener.asBinder() == token) return info; |
| 776 | } |
| 777 | throw new SecurityException("Disallowed call from unknown listener: " + listener); |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Allow an INotificationListener to simulate a "clear all" operation. |
| 782 | * |
| 783 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications} |
| 784 | * |
| 785 | * @param token The binder for the listener, to check that the caller is allowed |
| 786 | */ |
| 787 | public void clearAllNotificationsFromListener(INotificationListener token) { |
| 788 | NotificationListenerInfo info = checkListenerToken(token); |
| 789 | long identity = Binder.clearCallingIdentity(); |
| 790 | try { |
| 791 | cancelAll(info.userid); |
| 792 | } finally { |
| 793 | Binder.restoreCallingIdentity(identity); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 799 | * |
| 800 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 801 | * |
| 802 | * @param token The binder for the listener, to check that the caller is allowed |
| 803 | */ |
| 804 | public void clearNotificationFromListener(INotificationListener token, String pkg, String tag, int id) { |
| 805 | NotificationListenerInfo info = checkListenerToken(token); |
| 806 | long identity = Binder.clearCallingIdentity(); |
| 807 | try { |
| 808 | cancelNotification(pkg, tag, id, 0, |
| 809 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
| 810 | true, |
| 811 | info.userid); |
| 812 | } finally { |
| 813 | Binder.restoreCallingIdentity(identity); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | // -- end of listener APIs -- |
| 818 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 819 | public static final class NotificationRecord |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 821 | final StatusBarNotification sbn; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | IBinder statusBarKey; |
| 823 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 824 | NotificationRecord(StatusBarNotification sbn) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 826 | this.sbn = sbn; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 827 | } |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 828 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 829 | public Notification getNotification() { return sbn.notification; } |
| 830 | public int getFlags() { return sbn.notification.flags; } |
| 831 | public int getUserId() { return sbn.getUserId(); } |
| 832 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | void dump(PrintWriter pw, String prefix, Context baseContext) { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 834 | final Notification notification = sbn.notification; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | pw.println(prefix + this); |
| Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 836 | pw.println(prefix + " uid=" + sbn.uid + " userId=" + sbn.getUserId()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon) |
| Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 838 | + " / " + idDebugString(baseContext, sbn.pkg, notification.icon)); |
| 839 | pw.println(prefix + " pri=" + notification.priority + " score=" + sbn.score); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 840 | pw.println(prefix + " contentIntent=" + notification.contentIntent); |
| 841 | pw.println(prefix + " deleteIntent=" + notification.deleteIntent); |
| 842 | pw.println(prefix + " tickerText=" + notification.tickerText); |
| 843 | pw.println(prefix + " contentView=" + notification.contentView); |
| Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 844 | pw.println(prefix + String.format(" defaults=0x%08x flags=0x%08x", |
| 845 | notification.defaults, notification.flags)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | pw.println(prefix + " sound=" + notification.sound); |
| 847 | pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate)); |
| Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 848 | pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d", |
| 849 | notification.ledARGB, notification.ledOnMS, notification.ledOffMS)); |
| 850 | if (notification.actions != null && notification.actions.length > 0) { |
| 851 | pw.println(prefix + " actions={"); |
| 852 | final int N = notification.actions.length; |
| 853 | for (int i=0; i<N; i++) { |
| 854 | final Notification.Action action = notification.actions[i]; |
| 855 | pw.println(String.format("%s [%d] \"%s\" -> %s", |
| 856 | prefix, |
| 857 | i, |
| 858 | action.title, |
| 859 | action.actionIntent.toString() |
| 860 | )); |
| 861 | } |
| 862 | pw.println(prefix + " }"); |
| 863 | } |
| 864 | if (notification.extras != null && notification.extras.size() > 0) { |
| 865 | pw.println(prefix + " extras={"); |
| 866 | for (String key : notification.extras.keySet()) { |
| 867 | pw.print(prefix + " " + key + "="); |
| 868 | Object val = notification.extras.get(key); |
| 869 | if (val == null) { |
| 870 | pw.println("null"); |
| 871 | } else { |
| 872 | pw.print(val.toString()); |
| 873 | if (val instanceof Bitmap) { |
| 874 | pw.print(String.format(" (%dx%d)", |
| 875 | ((Bitmap) val).getWidth(), |
| 876 | ((Bitmap) val).getHeight())); |
| 877 | } else if (val.getClass().isArray()) { |
| 878 | pw.println(" {"); |
| 879 | final int N = Array.getLength(val); |
| 880 | for (int i=0; i<N; i++) { |
| 881 | if (i > 0) pw.println(","); |
| 882 | pw.print(prefix + " " + Array.get(val, i)); |
| 883 | } |
| 884 | pw.print("\n" + prefix + " }"); |
| 885 | } |
| 886 | pw.println(); |
| 887 | } |
| 888 | } |
| 889 | pw.println(prefix + " }"); |
| 890 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 892 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | @Override |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 894 | public final String toString() { |
| 895 | return String.format( |
| 896 | "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s score=%d: %s)", |
| 897 | System.identityHashCode(this), |
| 898 | this.sbn.pkg, this.sbn.user, this.sbn.id, this.sbn.tag, |
| 899 | this.sbn.score, this.sbn.notification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
| 903 | private static final class ToastRecord |
| 904 | { |
| 905 | final int pid; |
| 906 | final String pkg; |
| 907 | final ITransientNotification callback; |
| 908 | int duration; |
| 909 | |
| 910 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration) |
| 911 | { |
| 912 | this.pid = pid; |
| 913 | this.pkg = pkg; |
| 914 | this.callback = callback; |
| 915 | this.duration = duration; |
| 916 | } |
| 917 | |
| 918 | void update(int duration) { |
| 919 | this.duration = duration; |
| 920 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 921 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | void dump(PrintWriter pw, String prefix) { |
| 923 | pw.println(prefix + this); |
| 924 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 925 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | @Override |
| 927 | public final String toString() |
| 928 | { |
| 929 | return "ToastRecord{" |
| 930 | + Integer.toHexString(System.identityHashCode(this)) |
| 931 | + " pkg=" + pkg |
| 932 | + " callback=" + callback |
| 933 | + " duration=" + duration; |
| 934 | } |
| 935 | } |
| 936 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 937 | private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks |
| 938 | = new StatusBarManagerService.NotificationCallbacks() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | |
| 940 | public void onSetDisabled(int status) { |
| 941 | synchronized (mNotificationList) { |
| 942 | mDisabledNotifications = status; |
| 943 | if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) { |
| 944 | // cancel whatever's going on |
| 945 | long identity = Binder.clearCallingIdentity(); |
| 946 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 947 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 948 | if (player != null) { |
| 949 | player.stopAsync(); |
| 950 | } |
| 951 | } catch (RemoteException e) { |
| 952 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 953 | Binder.restoreCallingIdentity(identity); |
| 954 | } |
| 955 | |
| 956 | identity = Binder.clearCallingIdentity(); |
| 957 | try { |
| 958 | mVibrator.cancel(); |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 959 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | Binder.restoreCallingIdentity(identity); |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | public void onClearAll() { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 967 | // XXX to be totally correct, the caller should tell us which user |
| 968 | // this is for. |
| 969 | cancelAll(ActivityManager.getCurrentUser()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 970 | } |
| 971 | |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 972 | public void onNotificationClick(String pkg, String tag, int id) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 973 | // XXX to be totally correct, the caller should tell us which user |
| 974 | // this is for. |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 975 | cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 976 | Notification.FLAG_FOREGROUND_SERVICE, false, |
| 977 | ActivityManager.getCurrentUser()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | } |
| 979 | |
| Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 980 | public void onNotificationClear(String pkg, String tag, int id) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 981 | // XXX to be totally correct, the caller should tell us which user |
| 982 | // this is for. |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 983 | cancelNotification(pkg, tag, id, 0, |
| 984 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 985 | true, ActivityManager.getCurrentUser()); |
| Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 986 | } |
| 987 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | public void onPanelRevealed() { |
| 989 | synchronized (mNotificationList) { |
| 990 | // sound |
| 991 | mSoundNotification = null; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 992 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | long identity = Binder.clearCallingIdentity(); |
| 994 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 995 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 996 | if (player != null) { |
| 997 | player.stopAsync(); |
| 998 | } |
| 999 | } catch (RemoteException e) { |
| 1000 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | Binder.restoreCallingIdentity(identity); |
| 1002 | } |
| 1003 | |
| 1004 | // vibrate |
| 1005 | mVibrateNotification = null; |
| 1006 | identity = Binder.clearCallingIdentity(); |
| 1007 | try { |
| 1008 | mVibrator.cancel(); |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1009 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | Binder.restoreCallingIdentity(identity); |
| 1011 | } |
| 1012 | |
| 1013 | // light |
| 1014 | mLights.clear(); |
| 1015 | mLedNotification = null; |
| 1016 | updateLightsLocked(); |
| 1017 | } |
| 1018 | } |
| Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 1019 | |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 1020 | public void onNotificationError(String pkg, String tag, int id, |
| 1021 | int uid, int initialPid, String message) { |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 1022 | Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id |
| 1023 | + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")"); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1024 | // XXX to be totally correct, the caller should tell us which user |
| 1025 | // this is for. |
| 1026 | cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid)); |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 1027 | long ident = Binder.clearCallingIdentity(); |
| 1028 | try { |
| 1029 | ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg, |
| 1030 | "Bad notification posted from package " + pkg |
| 1031 | + ": " + message); |
| 1032 | } catch (RemoteException e) { |
| 1033 | } |
| 1034 | Binder.restoreCallingIdentity(ident); |
| Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 1035 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1036 | }; |
| 1037 | |
| 1038 | private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 1039 | @Override |
| 1040 | public void onReceive(Context context, Intent intent) { |
| 1041 | String action = intent.getAction(); |
| 1042 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1043 | boolean queryRestart = false; |
| Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1044 | boolean packageChanged = false; |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1045 | |
| Mike Lockwood | 541c994 | 2011-06-12 19:35:45 -0400 | [diff] [blame] | 1046 | if (action.equals(Intent.ACTION_PACKAGE_REMOVED) |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1047 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
| Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1048 | || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED)) |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1049 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1050 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1051 | String pkgList[] = null; |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1052 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1053 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1054 | } else if (queryRestart) { |
| 1055 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1056 | } else { |
| 1057 | Uri uri = intent.getData(); |
| 1058 | if (uri == null) { |
| 1059 | return; |
| 1060 | } |
| 1061 | String pkgName = uri.getSchemeSpecificPart(); |
| 1062 | if (pkgName == null) { |
| 1063 | return; |
| 1064 | } |
| Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1065 | if (packageChanged) { |
| 1066 | // We cancel notifications for packages which have just been disabled |
| 1067 | final int enabled = mContext.getPackageManager() |
| 1068 | .getApplicationEnabledSetting(pkgName); |
| 1069 | if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 1070 | || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 1071 | return; |
| 1072 | } |
| 1073 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1074 | pkgList = new String[]{pkgName}; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1075 | } |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1076 | |
| 1077 | boolean anyListenersInvolved = false; |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1078 | if (pkgList != null && (pkgList.length > 0)) { |
| 1079 | for (String pkgName : pkgList) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1080 | cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart, |
| 1081 | UserHandle.USER_ALL); |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1082 | if (mEnabledListenerPackageNames.contains(pkgName)) { |
| 1083 | anyListenersInvolved = true; |
| 1084 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1085 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1086 | } |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1087 | |
| 1088 | if (anyListenersInvolved) { |
| 1089 | // make sure we're still bound to any of our |
| 1090 | // listeners who may have just upgraded |
| 1091 | rebindListenerServices(); |
| 1092 | } |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1093 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
| 1094 | // Keep track of screen on/off state, but do not turn off the notification light |
| 1095 | // until user passes through the lock screen or views the notification. |
| 1096 | mScreenOn = true; |
| 1097 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 1098 | mScreenOn = false; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1099 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1100 | mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals( |
| 1101 | TelephonyManager.EXTRA_STATE_OFFHOOK)); |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1102 | updateNotificationPulse(); |
| Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1103 | } else if (action.equals(Intent.ACTION_USER_STOPPED)) { |
| 1104 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 1105 | if (userHandle >= 0) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1106 | cancelAllNotificationsInt(null, 0, 0, true, userHandle); |
| Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1107 | } |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1108 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 1109 | // turn off LED when user passes through lock screen |
| 1110 | mNotificationLight.turnOff(); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1111 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
| 1112 | // reload per-user settings |
| 1113 | mSettingsObserver.update(null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | }; |
| 1117 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1118 | class SettingsObserver extends ContentObserver { |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1119 | private final Uri NOTIFICATION_LIGHT_PULSE_URI |
| 1120 | = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); |
| 1121 | |
| 1122 | private final Uri ENABLED_NOTIFICATION_LISTENERS_URI |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1123 | = Settings.Secure.getUriFor(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1124 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1125 | SettingsObserver(Handler handler) { |
| 1126 | super(handler); |
| 1127 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1128 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1129 | void observe() { |
| 1130 | ContentResolver resolver = mContext.getContentResolver(); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1131 | resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1132 | false, this, UserHandle.USER_ALL); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1133 | resolver.registerContentObserver(ENABLED_NOTIFICATION_LISTENERS_URI, |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1134 | false, this, UserHandle.USER_ALL); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1135 | update(null); |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1138 | @Override public void onChange(boolean selfChange, Uri uri) { |
| 1139 | update(uri); |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1142 | public void update(Uri uri) { |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1143 | ContentResolver resolver = mContext.getContentResolver(); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1144 | if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { |
| 1145 | boolean pulseEnabled = Settings.System.getInt(resolver, |
| 1146 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0; |
| 1147 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 1148 | mNotificationPulseEnabled = pulseEnabled; |
| 1149 | updateNotificationPulse(); |
| 1150 | } |
| 1151 | } |
| 1152 | if (uri == null || ENABLED_NOTIFICATION_LISTENERS_URI.equals(uri)) { |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1153 | rebindListenerServices(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1154 | } |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1155 | } |
| 1156 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1157 | |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1158 | private SettingsObserver mSettingsObserver; |
| 1159 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1160 | static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) { |
| 1161 | int[] ar = r.getIntArray(resid); |
| 1162 | if (ar == null) { |
| 1163 | return def; |
| 1164 | } |
| 1165 | final int len = ar.length > maxlen ? maxlen : ar.length; |
| 1166 | long[] out = new long[len]; |
| 1167 | for (int i=0; i<len; i++) { |
| 1168 | out[i] = ar[i]; |
| 1169 | } |
| 1170 | return out; |
| 1171 | } |
| 1172 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 1173 | NotificationManagerService(Context context, StatusBarManagerService statusBar, |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 1174 | LightsService lights) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1175 | { |
| 1176 | super(); |
| 1177 | mContext = context; |
| Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 1178 | mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1179 | mAm = ActivityManagerNative.getDefault(); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1180 | mUserManager = (UserManager)context.getSystemService(Context.USER_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1181 | mToastQueue = new ArrayList<ToastRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | mHandler = new WorkerHandler(); |
| San Mehat | 3ee1317 | 2010-02-04 20:54:43 -0800 | [diff] [blame] | 1183 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1184 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
| 1185 | |
| 1186 | importOldBlockDb(); |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1187 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 1188 | mStatusBar = statusBar; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | statusBar.setNotificationCallbacks(mNotificationCallbacks); |
| 1190 | |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1191 | mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS); |
| 1192 | mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION); |
| 1193 | |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 1194 | Resources resources = mContext.getResources(); |
| 1195 | mDefaultNotificationColor = resources.getColor( |
| 1196 | com.android.internal.R.color.config_defaultNotificationColor); |
| 1197 | mDefaultNotificationLedOn = resources.getInteger( |
| 1198 | com.android.internal.R.integer.config_defaultNotificationLedOn); |
| 1199 | mDefaultNotificationLedOff = resources.getInteger( |
| 1200 | com.android.internal.R.integer.config_defaultNotificationLedOff); |
| 1201 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1202 | mDefaultVibrationPattern = getLongArray(resources, |
| 1203 | com.android.internal.R.array.config_defaultNotificationVibePattern, |
| 1204 | VIBRATE_PATTERN_MAXLEN, |
| 1205 | DEFAULT_VIBRATE_PATTERN); |
| 1206 | |
| 1207 | mFallbackVibrationPattern = getLongArray(resources, |
| 1208 | com.android.internal.R.array.config_notificationFallbackVibePattern, |
| 1209 | VIBRATE_PATTERN_MAXLEN, |
| 1210 | DEFAULT_VIBRATE_PATTERN); |
| 1211 | |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1212 | // Don't start allowing notifications until the setup wizard has run once. |
| 1213 | // After that, including subsequent boots, init with notifications turned on. |
| 1214 | // This works on the first boot because the setup wizard will toggle this |
| 1215 | // flag at least once and we'll go back to 0 after that. |
| Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1216 | if (0 == Settings.Global.getInt(mContext.getContentResolver(), |
| 1217 | Settings.Global.DEVICE_PROVISIONED, 0)) { |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1218 | mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS; |
| 1219 | } |
| 1220 | |
| Mike Lockwood | 35e16bf | 2010-11-30 19:53:36 -0500 | [diff] [blame] | 1221 | // register for various Intents |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1222 | IntentFilter filter = new IntentFilter(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1223 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1224 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1225 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1226 | filter.addAction(Intent.ACTION_USER_PRESENT); |
| Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1227 | filter.addAction(Intent.ACTION_USER_STOPPED); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1228 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1229 | mContext.registerReceiver(mIntentReceiver, filter); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1230 | IntentFilter pkgFilter = new IntentFilter(); |
| 1231 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| Daniel Sandler | aac0eb0 | 2011-08-06 22:51:56 -0400 | [diff] [blame] | 1232 | pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1233 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 1234 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 1235 | pkgFilter.addDataScheme("package"); |
| 1236 | mContext.registerReceiver(mIntentReceiver, pkgFilter); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1237 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1238 | mContext.registerReceiver(mIntentReceiver, sdFilter); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1239 | |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1240 | mSettingsObserver = new SettingsObserver(mHandler); |
| 1241 | mSettingsObserver.observe(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1244 | /** |
| 1245 | * Read the old XML-based app block database and import those blockages into the AppOps system. |
| 1246 | */ |
| 1247 | private void importOldBlockDb() { |
| 1248 | loadBlockDb(); |
| 1249 | |
| 1250 | PackageManager pm = mContext.getPackageManager(); |
| 1251 | for (String pkg : mBlockedPackages) { |
| 1252 | PackageInfo info = null; |
| 1253 | try { |
| 1254 | info = pm.getPackageInfo(pkg, 0); |
| 1255 | setNotificationsEnabledForPackage(pkg, info.applicationInfo.uid, false); |
| 1256 | } catch (NameNotFoundException e) { |
| 1257 | // forget you |
| 1258 | } |
| 1259 | } |
| 1260 | mBlockedPackages.clear(); |
| 1261 | if (mPolicyFile != null) { |
| 1262 | mPolicyFile.delete(); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1266 | void systemReady() { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1267 | mAudioService = IAudioService.Stub.asInterface( |
| 1268 | ServiceManager.getService(Context.AUDIO_SERVICE)); |
| 1269 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1270 | // no beeping until we're basically done booting |
| 1271 | mSystemReady = true; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1272 | |
| 1273 | // make sure our listener services are properly bound |
| 1274 | rebindListenerServices(); |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | // Toasts |
| 1278 | // ============================================================================ |
| 1279 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
| 1280 | { |
| Daniel Sandler | a703590 | 2010-03-30 15:45:31 -0400 | [diff] [blame] | 1281 | if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1282 | |
| 1283 | if (pkg == null || callback == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1284 | Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1285 | return ; |
| 1286 | } |
| 1287 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1288 | final boolean isSystemToast = ("android".equals(pkg)); |
| 1289 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1290 | if (ENABLE_BLOCKED_TOASTS && !noteNotificationOp(pkg, Binder.getCallingUid())) { |
| 1291 | if (!isSystemToast) { |
| 1292 | Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request."); |
| 1293 | return; |
| 1294 | } |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1295 | } |
| 1296 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1297 | synchronized (mToastQueue) { |
| 1298 | int callingPid = Binder.getCallingPid(); |
| 1299 | long callingId = Binder.clearCallingIdentity(); |
| 1300 | try { |
| 1301 | ToastRecord record; |
| 1302 | int index = indexOfToastLocked(pkg, callback); |
| 1303 | // If it's already in the queue, we update it in place, we don't |
| 1304 | // move it to the end of the queue. |
| 1305 | if (index >= 0) { |
| 1306 | record = mToastQueue.get(index); |
| 1307 | record.update(duration); |
| 1308 | } else { |
| Vairavan Srinivasan | f9eb06c | 2011-01-21 18:08:36 -0800 | [diff] [blame] | 1309 | // Limit the number of toasts that any given package except the android |
| 1310 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1311 | if (!isSystemToast) { |
| Vairavan Srinivasan | f9eb06c | 2011-01-21 18:08:36 -0800 | [diff] [blame] | 1312 | int count = 0; |
| 1313 | final int N = mToastQueue.size(); |
| 1314 | for (int i=0; i<N; i++) { |
| 1315 | final ToastRecord r = mToastQueue.get(i); |
| 1316 | if (r.pkg.equals(pkg)) { |
| 1317 | count++; |
| 1318 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 1319 | Slog.e(TAG, "Package has already posted " + count |
| 1320 | + " toasts. Not showing more. Package=" + pkg); |
| 1321 | return; |
| 1322 | } |
| 1323 | } |
| 1324 | } |
| 1325 | } |
| 1326 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1327 | record = new ToastRecord(callingPid, pkg, callback, duration); |
| 1328 | mToastQueue.add(record); |
| 1329 | index = mToastQueue.size() - 1; |
| 1330 | keepProcessAliveLocked(callingPid); |
| 1331 | } |
| 1332 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 1333 | // new or just been updated. Call back and tell it to show itself. |
| 1334 | // If the callback fails, this will remove it from the list, so don't |
| 1335 | // assume that it's valid after this. |
| 1336 | if (index == 0) { |
| 1337 | showNextToastLocked(); |
| 1338 | } |
| 1339 | } finally { |
| 1340 | Binder.restoreCallingIdentity(callingId); |
| 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | public void cancelToast(String pkg, ITransientNotification callback) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1346 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1347 | |
| 1348 | if (pkg == null || callback == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1349 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1350 | return ; |
| 1351 | } |
| 1352 | |
| 1353 | synchronized (mToastQueue) { |
| 1354 | long callingId = Binder.clearCallingIdentity(); |
| 1355 | try { |
| 1356 | int index = indexOfToastLocked(pkg, callback); |
| 1357 | if (index >= 0) { |
| 1358 | cancelToastLocked(index); |
| 1359 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1360 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1361 | } |
| 1362 | } finally { |
| 1363 | Binder.restoreCallingIdentity(callingId); |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | private void showNextToastLocked() { |
| 1369 | ToastRecord record = mToastQueue.get(0); |
| 1370 | while (record != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1371 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1372 | try { |
| 1373 | record.callback.show(); |
| 1374 | scheduleTimeoutLocked(record, false); |
| 1375 | return; |
| 1376 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1377 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1378 | + " in package " + record.pkg); |
| 1379 | // remove it from the list and let the process die |
| 1380 | int index = mToastQueue.indexOf(record); |
| 1381 | if (index >= 0) { |
| 1382 | mToastQueue.remove(index); |
| 1383 | } |
| 1384 | keepProcessAliveLocked(record.pid); |
| 1385 | if (mToastQueue.size() > 0) { |
| 1386 | record = mToastQueue.get(0); |
| 1387 | } else { |
| 1388 | record = null; |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | private void cancelToastLocked(int index) { |
| 1395 | ToastRecord record = mToastQueue.get(index); |
| 1396 | try { |
| 1397 | record.callback.hide(); |
| 1398 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1399 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1400 | + " in package " + record.pkg); |
| 1401 | // don't worry about this, we're about to remove it from |
| 1402 | // the list anyway |
| 1403 | } |
| 1404 | mToastQueue.remove(index); |
| 1405 | keepProcessAliveLocked(record.pid); |
| 1406 | if (mToastQueue.size() > 0) { |
| 1407 | // Show the next one. If the callback fails, this will remove |
| 1408 | // it from the list, so don't assume that the list hasn't changed |
| 1409 | // after this point. |
| 1410 | showNextToastLocked(); |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | private void scheduleTimeoutLocked(ToastRecord r, boolean immediate) |
| 1415 | { |
| 1416 | Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r); |
| 1417 | long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY); |
| 1418 | mHandler.removeCallbacksAndMessages(r); |
| 1419 | mHandler.sendMessageDelayed(m, delay); |
| 1420 | } |
| 1421 | |
| 1422 | private void handleTimeout(ToastRecord record) |
| 1423 | { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1424 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1425 | synchronized (mToastQueue) { |
| 1426 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 1427 | if (index >= 0) { |
| 1428 | cancelToastLocked(index); |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | // lock on mToastQueue |
| 1434 | private int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 1435 | { |
| 1436 | IBinder cbak = callback.asBinder(); |
| 1437 | ArrayList<ToastRecord> list = mToastQueue; |
| 1438 | int len = list.size(); |
| 1439 | for (int i=0; i<len; i++) { |
| 1440 | ToastRecord r = list.get(i); |
| 1441 | if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) { |
| 1442 | return i; |
| 1443 | } |
| 1444 | } |
| 1445 | return -1; |
| 1446 | } |
| 1447 | |
| 1448 | // lock on mToastQueue |
| 1449 | private void keepProcessAliveLocked(int pid) |
| 1450 | { |
| 1451 | int toastCount = 0; // toasts from this pid |
| 1452 | ArrayList<ToastRecord> list = mToastQueue; |
| 1453 | int N = list.size(); |
| 1454 | for (int i=0; i<N; i++) { |
| 1455 | ToastRecord r = list.get(i); |
| 1456 | if (r.pid == pid) { |
| 1457 | toastCount++; |
| 1458 | } |
| 1459 | } |
| 1460 | try { |
| 1461 | mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0); |
| 1462 | } catch (RemoteException e) { |
| 1463 | // Shouldn't happen. |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | private final class WorkerHandler extends Handler |
| 1468 | { |
| 1469 | @Override |
| 1470 | public void handleMessage(Message msg) |
| 1471 | { |
| 1472 | switch (msg.what) |
| 1473 | { |
| 1474 | case MESSAGE_TIMEOUT: |
| 1475 | handleTimeout((ToastRecord)msg.obj); |
| 1476 | break; |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | |
| 1482 | // Notifications |
| 1483 | // ============================================================================ |
| Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1484 | public void enqueueNotificationWithTag(String pkg, String basePkg, String tag, int id, |
| 1485 | Notification notification, int[] idOut, int userId) |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1486 | { |
| Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1487 | enqueueNotificationInternal(pkg, basePkg, Binder.getCallingUid(), Binder.getCallingPid(), |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1488 | tag, id, notification, idOut, userId); |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 1489 | } |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1490 | |
| 1491 | private final static int clamp(int x, int low, int high) { |
| 1492 | return (x < low) ? low : ((x > high) ? high : x); |
| Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 1493 | } |
| 1494 | |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 1495 | // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the |
| 1496 | // uid/pid of another application) |
| Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1497 | public void enqueueNotificationInternal(String pkg, String basePkg, int callingUid, |
| 1498 | int callingPid, String tag, int id, Notification notification, int[] idOut, int userId) |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 1499 | { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1500 | if (DBG) { |
| 1501 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification); |
| 1502 | } |
| 1503 | checkCallerIsSystemOrSameApp(pkg); |
| 1504 | final boolean isSystemNotification = ("android".equals(pkg)); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1505 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1506 | userId = ActivityManager.handleIncomingUser(callingPid, |
| Amith Yamasani | 2c7ebea | 2012-10-30 15:28:27 -0700 | [diff] [blame] | 1507 | callingUid, userId, true, false, "enqueueNotification", pkg); |
| Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 1508 | final UserHandle user = new UserHandle(userId); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1509 | |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 1510 | // Limit the number of notifications that any given package except the android |
| 1511 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1512 | if (!isSystemNotification) { |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 1513 | synchronized (mNotificationList) { |
| 1514 | int count = 0; |
| 1515 | final int N = mNotificationList.size(); |
| 1516 | for (int i=0; i<N; i++) { |
| 1517 | final NotificationRecord r = mNotificationList.get(i); |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1518 | if (r.sbn.pkg.equals(pkg) && r.sbn.getUserId() == userId) { |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 1519 | count++; |
| 1520 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 1521 | Slog.e(TAG, "Package has already posted " + count |
| 1522 | + " notifications. Not showing more. package=" + pkg); |
| 1523 | return; |
| 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1530 | // This conditional is a dirty hack to limit the logging done on |
| 1531 | // behalf of the download manager without affecting other apps. |
| 1532 | if (!pkg.equals("com.android.providers.downloads") |
| 1533 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1534 | EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId, |
| Daniel Sandler | b64cb88 | 2011-11-29 23:48:29 -0500 | [diff] [blame] | 1535 | notification.toString()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | if (pkg == null || notification == null) { |
| 1539 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 1540 | + " id=" + id + " notification=" + notification); |
| 1541 | } |
| 1542 | if (notification.icon != 0) { |
| 1543 | if (notification.contentView == null) { |
| 1544 | throw new IllegalArgumentException("contentView required: pkg=" + pkg |
| 1545 | + " id=" + id + " notification=" + notification); |
| 1546 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1547 | } |
| 1548 | |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1549 | // === Scoring === |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1550 | |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1551 | // 0. Sanitize inputs |
| 1552 | notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX); |
| 1553 | // Migrate notification flags to scores |
| 1554 | if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) { |
| 1555 | if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX; |
| Daniel Sandler | 49a2ad1 | 2012-03-28 15:46:39 -0400 | [diff] [blame] | 1556 | } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) { |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1557 | if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH; |
| 1558 | } |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1559 | |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1560 | // 1. initial score: buckets of 10, around the app |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1561 | int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20] |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1562 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1563 | // 2. Consult external heuristics (TBD) |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1564 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1565 | // 3. Apply local rules |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1566 | |
| 1567 | // blocked apps |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1568 | if (ENABLE_BLOCKED_NOTIFICATIONS && !noteNotificationOp(pkg, callingUid)) { |
| 1569 | if (!isSystemNotification) { |
| 1570 | score = JUNK_SCORE; |
| 1571 | Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request."); |
| 1572 | } |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | if (DBG) { |
| 1576 | Slog.v(TAG, "Assigned score=" + score + " to " + notification); |
| 1577 | } |
| 1578 | |
| 1579 | if (score < SCORE_DISPLAY_THRESHOLD) { |
| 1580 | // Notification will be blocked because the score is too low. |
| 1581 | return; |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1582 | } |
| 1583 | |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 1584 | // Should this notification make noise, vibe, or use the LED? |
| 1585 | final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD); |
| 1586 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | synchronized (mNotificationList) { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1588 | final StatusBarNotification n = new StatusBarNotification( |
| 1589 | pkg, id, tag, callingUid, callingPid, score, notification, user); |
| 1590 | NotificationRecord r = new NotificationRecord(n); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1591 | NotificationRecord old = null; |
| 1592 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1593 | int index = indexOfNotificationLocked(pkg, tag, id, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1594 | if (index < 0) { |
| 1595 | mNotificationList.add(r); |
| 1596 | } else { |
| 1597 | old = mNotificationList.remove(index); |
| 1598 | mNotificationList.add(index, r); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1599 | // Make sure we don't lose the foreground service state. |
| 1600 | if (old != null) { |
| 1601 | notification.flags |= |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1602 | old.getNotification().flags&Notification.FLAG_FOREGROUND_SERVICE; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1603 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1604 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1605 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1606 | // Ensure if this is a foreground service that the proper additional |
| 1607 | // flags are set. |
| 1608 | if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 1609 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 1610 | | Notification.FLAG_NO_CLEAR; |
| 1611 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1612 | |
| Svetoslav Ganov | c31ed39 | 2012-10-10 14:58:28 -0700 | [diff] [blame] | 1613 | final int currentUser; |
| 1614 | final long token = Binder.clearCallingIdentity(); |
| 1615 | try { |
| 1616 | currentUser = ActivityManager.getCurrentUser(); |
| 1617 | } finally { |
| 1618 | Binder.restoreCallingIdentity(token); |
| 1619 | } |
| 1620 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1621 | if (notification.icon != 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1622 | if (old != null && old.statusBarKey != null) { |
| 1623 | r.statusBarKey = old.statusBarKey; |
| 1624 | long identity = Binder.clearCallingIdentity(); |
| 1625 | try { |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1626 | mStatusBar.updateNotification(r.statusBarKey, n); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1627 | } |
| 1628 | finally { |
| 1629 | Binder.restoreCallingIdentity(identity); |
| 1630 | } |
| 1631 | } else { |
| 1632 | long identity = Binder.clearCallingIdentity(); |
| 1633 | try { |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1634 | r.statusBarKey = mStatusBar.addNotification(n); |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 1635 | if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0 |
| 1636 | && canInterrupt) { |
| Mike Lockwood | ece18ef | 2012-02-13 20:42:19 -0800 | [diff] [blame] | 1637 | mAttentionLight.pulse(); |
| 1638 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1639 | } |
| 1640 | finally { |
| 1641 | Binder.restoreCallingIdentity(identity); |
| 1642 | } |
| 1643 | } |
| Svetoslav Ganov | c31ed39 | 2012-10-10 14:58:28 -0700 | [diff] [blame] | 1644 | // Send accessibility events only for the current user. |
| 1645 | if (currentUser == userId) { |
| 1646 | sendAccessibilityEvent(notification, pkg); |
| 1647 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1648 | |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 1649 | notifyPostedLocked(r); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1650 | } else { |
| Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 1651 | Slog.e(TAG, "Ignoring notification with icon==0: " + notification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1652 | if (old != null && old.statusBarKey != null) { |
| 1653 | long identity = Binder.clearCallingIdentity(); |
| 1654 | try { |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 1655 | mStatusBar.removeNotification(old.statusBarKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1656 | } |
| 1657 | finally { |
| 1658 | Binder.restoreCallingIdentity(identity); |
| 1659 | } |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 1660 | |
| 1661 | notifyRemovedLocked(r); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1662 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1663 | return; // do not play sounds, show lights, etc. for invalid notifications |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | // If we're not supposed to beep, vibrate, etc. then don't. |
| 1667 | if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0) |
| 1668 | && (!(old != null |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1669 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 )) |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1670 | && (r.getUserId() == UserHandle.USER_ALL || |
| 1671 | (r.getUserId() == userId && r.getUserId() == currentUser)) |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 1672 | && canInterrupt |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1673 | && mSystemReady) { |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1674 | |
| 1675 | final AudioManager audioManager = (AudioManager) mContext |
| 1676 | .getSystemService(Context.AUDIO_SERVICE); |
| Daniel Sandler | d4d2de2 | 2012-11-14 11:25:46 -0800 | [diff] [blame] | 1677 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1678 | // sound |
| Daniel Sandler | 3147523 | 2013-04-17 00:17:22 -0400 | [diff] [blame] | 1679 | |
| 1680 | // should we use the default notification sound? (indicated either by DEFAULT_SOUND |
| 1681 | // or because notification.sound is pointing at Settings.System.NOTIFICATION_SOUND) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | final boolean useDefaultSound = |
| Daniel Sandler | 3147523 | 2013-04-17 00:17:22 -0400 | [diff] [blame] | 1683 | (notification.defaults & Notification.DEFAULT_SOUND) != 0 |
| 1684 | || Settings.System.DEFAULT_NOTIFICATION_URI.equals(notification.sound); |
| Daniel Sandler | d4d2de2 | 2012-11-14 11:25:46 -0800 | [diff] [blame] | 1685 | |
| 1686 | Uri soundUri = null; |
| 1687 | boolean hasValidSound = false; |
| 1688 | |
| 1689 | if (useDefaultSound) { |
| 1690 | soundUri = Settings.System.DEFAULT_NOTIFICATION_URI; |
| 1691 | |
| 1692 | // check to see if the default notification sound is silent |
| 1693 | ContentResolver resolver = mContext.getContentResolver(); |
| 1694 | hasValidSound = Settings.System.getString(resolver, |
| 1695 | Settings.System.NOTIFICATION_SOUND) != null; |
| 1696 | } else if (notification.sound != null) { |
| 1697 | soundUri = notification.sound; |
| 1698 | hasValidSound = (soundUri != null); |
| 1699 | } |
| 1700 | |
| 1701 | if (hasValidSound) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1702 | boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0; |
| 1703 | int audioStreamType; |
| 1704 | if (notification.audioStreamType >= 0) { |
| 1705 | audioStreamType = notification.audioStreamType; |
| 1706 | } else { |
| 1707 | audioStreamType = DEFAULT_STREAM_TYPE; |
| 1708 | } |
| 1709 | mSoundNotification = r; |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1710 | // do not play notifications if stream volume is 0 |
| Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 1711 | // (typically because ringer mode is silent) or if speech recognition is active. |
| 1712 | if ((audioManager.getStreamVolume(audioStreamType) != 0) |
| 1713 | && !audioManager.isSpeechRecognitionActive()) { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1714 | final long identity = Binder.clearCallingIdentity(); |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1715 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1716 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 1717 | if (player != null) { |
| Daniel Sandler | d4d2de2 | 2012-11-14 11:25:46 -0800 | [diff] [blame] | 1718 | player.playAsync(soundUri, user, looping, audioStreamType); |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1719 | } |
| 1720 | } catch (RemoteException e) { |
| 1721 | } finally { |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1722 | Binder.restoreCallingIdentity(identity); |
| 1723 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | // vibrate |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1728 | // Does the notification want to specify its own vibration? |
| 1729 | final boolean hasCustomVibrate = notification.vibrate != null; |
| 1730 | |
| David Agnew | 71789e1 | 2012-11-09 23:03:26 -0500 | [diff] [blame] | 1731 | // new in 4.2: if there was supposed to be a sound and we're in vibrate mode, |
| Daniel Sandler | 4a7a9b9 | 2012-11-20 12:59:41 -0500 | [diff] [blame] | 1732 | // and no other vibration is specified, we fall back to vibration |
| David Agnew | 71789e1 | 2012-11-09 23:03:26 -0500 | [diff] [blame] | 1733 | final boolean convertSoundToVibration = |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1734 | !hasCustomVibrate |
| Daniel Sandler | d4d2de2 | 2012-11-14 11:25:46 -0800 | [diff] [blame] | 1735 | && hasValidSound |
| David Agnew | 71789e1 | 2012-11-09 23:03:26 -0500 | [diff] [blame] | 1736 | && (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE); |
| 1737 | |
| Daniel Sandler | 4a7a9b9 | 2012-11-20 12:59:41 -0500 | [diff] [blame] | 1738 | // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1739 | final boolean useDefaultVibrate = |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1740 | (notification.defaults & Notification.DEFAULT_VIBRATE) != 0; |
| David Agnew | 71789e1 | 2012-11-09 23:03:26 -0500 | [diff] [blame] | 1741 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1742 | if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate) |
| Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1743 | && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1744 | mVibrateNotification = r; |
| 1745 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1746 | if (useDefaultVibrate || convertSoundToVibration) { |
| 1747 | // Escalate privileges so we can use the vibrator even if the notifying app |
| 1748 | // does not have the VIBRATE permission. |
| 1749 | long identity = Binder.clearCallingIdentity(); |
| 1750 | try { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1751 | mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg, |
| Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1752 | useDefaultVibrate ? mDefaultVibrationPattern |
| 1753 | : mFallbackVibrationPattern, |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1754 | ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1); |
| 1755 | } finally { |
| 1756 | Binder.restoreCallingIdentity(identity); |
| 1757 | } |
| 1758 | } else if (notification.vibrate.length > 1) { |
| 1759 | // If you want your own vibration pattern, you need the VIBRATE permission |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1760 | mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg, notification.vibrate, |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1761 | ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1); |
| 1762 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1763 | } |
| 1764 | } |
| 1765 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1766 | // light |
| 1767 | // the most recent thing gets the light |
| 1768 | mLights.remove(old); |
| 1769 | if (mLedNotification == old) { |
| 1770 | mLedNotification = null; |
| 1771 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1772 | //Slog.i(TAG, "notification.lights=" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1773 | // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0)); |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 1774 | if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0 |
| 1775 | && canInterrupt) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1776 | mLights.add(r); |
| 1777 | updateLightsLocked(); |
| 1778 | } else { |
| 1779 | if (old != null |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1780 | && ((old.getFlags() & Notification.FLAG_SHOW_LIGHTS) != 0)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1781 | updateLightsLocked(); |
| 1782 | } |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | idOut[0] = id; |
| 1787 | } |
| 1788 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1789 | private void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1790 | AccessibilityManager manager = AccessibilityManager.getInstance(mContext); |
| 1791 | if (!manager.isEnabled()) { |
| 1792 | return; |
| 1793 | } |
| 1794 | |
| 1795 | AccessibilityEvent event = |
| 1796 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 1797 | event.setPackageName(packageName); |
| 1798 | event.setClassName(Notification.class.getName()); |
| 1799 | event.setParcelableData(notification); |
| 1800 | CharSequence tickerText = notification.tickerText; |
| 1801 | if (!TextUtils.isEmpty(tickerText)) { |
| 1802 | event.getText().add(tickerText); |
| 1803 | } |
| 1804 | |
| 1805 | manager.sendAccessibilityEvent(event); |
| 1806 | } |
| 1807 | |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1808 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) { |
| 1809 | // tell the app |
| 1810 | if (sendDelete) { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1811 | if (r.getNotification().deleteIntent != null) { |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1812 | try { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1813 | r.getNotification().deleteIntent.send(); |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1814 | } catch (PendingIntent.CanceledException ex) { |
| 1815 | // do nothing - there's no relevant way to recover, and |
| 1816 | // no reason to let this propagate |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1817 | Slog.w(TAG, "canceled PendingIntent for " + r.sbn.pkg, ex); |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1818 | } |
| 1819 | } |
| 1820 | } |
| 1821 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1822 | // status bar |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1823 | if (r.getNotification().icon != 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1824 | long identity = Binder.clearCallingIdentity(); |
| 1825 | try { |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 1826 | mStatusBar.removeNotification(r.statusBarKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1827 | } |
| 1828 | finally { |
| 1829 | Binder.restoreCallingIdentity(identity); |
| 1830 | } |
| 1831 | r.statusBarKey = null; |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 1832 | notifyRemovedLocked(r); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | // sound |
| 1836 | if (mSoundNotification == r) { |
| 1837 | mSoundNotification = null; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1838 | final long identity = Binder.clearCallingIdentity(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1839 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1840 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 1841 | if (player != null) { |
| 1842 | player.stopAsync(); |
| 1843 | } |
| 1844 | } catch (RemoteException e) { |
| 1845 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1846 | Binder.restoreCallingIdentity(identity); |
| 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | // vibrate |
| 1851 | if (mVibrateNotification == r) { |
| 1852 | mVibrateNotification = null; |
| 1853 | long identity = Binder.clearCallingIdentity(); |
| 1854 | try { |
| 1855 | mVibrator.cancel(); |
| 1856 | } |
| 1857 | finally { |
| 1858 | Binder.restoreCallingIdentity(identity); |
| 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | // light |
| 1863 | mLights.remove(r); |
| 1864 | if (mLedNotification == r) { |
| 1865 | mLedNotification = null; |
| 1866 | } |
| Daniel Sandler | 23d7c70 | 2013-03-07 16:32:06 -0500 | [diff] [blame] | 1867 | |
| 1868 | // Save it for users of getHistoricalNotifications() |
| 1869 | mArchive.record(r.sbn); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | /** |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1873 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1874 | * and none of the {@code mustNotHaveFlags}. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1875 | */ |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1876 | private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1877 | int mustNotHaveFlags, boolean sendDelete, int userId) { |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1878 | EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId, |
| Daniel Sandler | b64cb88 | 2011-11-29 23:48:29 -0500 | [diff] [blame] | 1879 | mustHaveFlags, mustNotHaveFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1880 | |
| 1881 | synchronized (mNotificationList) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1882 | int index = indexOfNotificationLocked(pkg, tag, id, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1883 | if (index >= 0) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1884 | NotificationRecord r = mNotificationList.get(index); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1885 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1886 | if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1887 | return; |
| 1888 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1889 | if ((r.getNotification().flags & mustNotHaveFlags) != 0) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1890 | return; |
| 1891 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1892 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1893 | mNotificationList.remove(index); |
| 1894 | |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1895 | cancelNotificationLocked(r, sendDelete); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1896 | updateLightsLocked(); |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | /** |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1902 | * Determine whether the userId applies to the notification in question, either because |
| 1903 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard). |
| 1904 | */ |
| 1905 | private boolean notificationMatchesUserId(NotificationRecord r, int userId) { |
| 1906 | return |
| 1907 | // looking for USER_ALL notifications? match everything |
| 1908 | userId == UserHandle.USER_ALL |
| 1909 | // a notification sent to USER_ALL matches any query |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1910 | || r.getUserId() == UserHandle.USER_ALL |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1911 | // an exact user match |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1912 | || r.getUserId() == userId; |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | /** |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1916 | * Cancels all notifications from a given package that have all of the |
| 1917 | * {@code mustHaveFlags}. |
| 1918 | */ |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1919 | boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1920 | int mustNotHaveFlags, boolean doit, int userId) { |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1921 | EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId, |
| 1922 | mustHaveFlags, mustNotHaveFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1923 | |
| 1924 | synchronized (mNotificationList) { |
| 1925 | final int N = mNotificationList.size(); |
| 1926 | boolean canceledSomething = false; |
| 1927 | for (int i = N-1; i >= 0; --i) { |
| 1928 | NotificationRecord r = mNotificationList.get(i); |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1929 | if (!notificationMatchesUserId(r, userId)) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1930 | continue; |
| 1931 | } |
| Amith Yamasani | 5ec00e9 | 2012-11-07 16:58:30 -0800 | [diff] [blame] | 1932 | // Don't remove notifications to all, if there's no package name specified |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1933 | if (r.getUserId() == UserHandle.USER_ALL && pkg == null) { |
| Amith Yamasani | 5ec00e9 | 2012-11-07 16:58:30 -0800 | [diff] [blame] | 1934 | continue; |
| 1935 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1936 | if ((r.getFlags() & mustHaveFlags) != mustHaveFlags) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1937 | continue; |
| 1938 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1939 | if ((r.getFlags() & mustNotHaveFlags) != 0) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1940 | continue; |
| 1941 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1942 | if (pkg != null && !r.sbn.pkg.equals(pkg)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1943 | continue; |
| 1944 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1945 | canceledSomething = true; |
| 1946 | if (!doit) { |
| 1947 | return true; |
| 1948 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1949 | mNotificationList.remove(i); |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1950 | cancelNotificationLocked(r, false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1951 | } |
| 1952 | if (canceledSomething) { |
| 1953 | updateLightsLocked(); |
| 1954 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1955 | return canceledSomething; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1956 | } |
| 1957 | } |
| 1958 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1959 | public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1960 | checkCallerIsSystemOrSameApp(pkg); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1961 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| Amith Yamasani | 2c7ebea | 2012-10-30 15:28:27 -0700 | [diff] [blame] | 1962 | Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1963 | // Don't allow client applications to cancel foreground service notis. |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1964 | cancelNotification(pkg, tag, id, 0, |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1965 | Binder.getCallingUid() == Process.SYSTEM_UID |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1966 | ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1967 | } |
| 1968 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1969 | public void cancelAllNotifications(String pkg, int userId) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1970 | checkCallerIsSystemOrSameApp(pkg); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1971 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1972 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| Amith Yamasani | 2c7ebea | 2012-10-30 15:28:27 -0700 | [diff] [blame] | 1973 | Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1974 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1975 | // Calling from user space, don't allow the canceling of actively |
| 1976 | // running foreground services. |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1977 | cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1978 | } |
| 1979 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1980 | void checkCallerIsSystem() { |
| 1981 | int uid = Binder.getCallingUid(); |
| Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 1982 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1983 | return; |
| 1984 | } |
| 1985 | throw new SecurityException("Disallowed call for uid " + uid); |
| 1986 | } |
| 1987 | |
| 1988 | void checkCallerIsSystemOrSameApp(String pkg) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1989 | int uid = Binder.getCallingUid(); |
| Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 1990 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1991 | return; |
| 1992 | } |
| 1993 | try { |
| Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 1994 | ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo( |
| 1995 | pkg, 0, UserHandle.getCallingUserId()); |
| Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1996 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1997 | throw new SecurityException("Calling uid " + uid + " gave package" |
| 1998 | + pkg + " which is owned by uid " + ai.uid); |
| 1999 | } |
| Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 2000 | } catch (RemoteException re) { |
| 2001 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 2002 | } |
| 2003 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2004 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2005 | void cancelAll(int userId) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2006 | synchronized (mNotificationList) { |
| 2007 | final int N = mNotificationList.size(); |
| 2008 | for (int i=N-1; i>=0; i--) { |
| 2009 | NotificationRecord r = mNotificationList.get(i); |
| 2010 | |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 2011 | if (!notificationMatchesUserId(r, userId)) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2012 | continue; |
| 2013 | } |
| 2014 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 2015 | if ((r.getFlags() & (Notification.FLAG_ONGOING_EVENT |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2016 | | Notification.FLAG_NO_CLEAR)) == 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2017 | mNotificationList.remove(i); |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2018 | cancelNotificationLocked(r, true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | updateLightsLocked(); |
| 2023 | } |
| 2024 | } |
| 2025 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2026 | // lock on mNotificationList |
| 2027 | private void updateLightsLocked() |
| 2028 | { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2029 | // handle notification lights |
| 2030 | if (mLedNotification == null) { |
| 2031 | // get next notification, if any |
| 2032 | int n = mLights.size(); |
| 2033 | if (n > 0) { |
| 2034 | mLedNotification = mLights.get(n-1); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2035 | } |
| 2036 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 2037 | |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 2038 | // Don't flash while we are in a call or screen is on |
| 2039 | if (mLedNotification == null || mInCall || mScreenOn) { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 2040 | mNotificationLight.turnOff(); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2041 | } else { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 2042 | final Notification ledno = mLedNotification.sbn.notification; |
| 2043 | int ledARGB = ledno.ledARGB; |
| 2044 | int ledOnMS = ledno.ledOnMS; |
| 2045 | int ledOffMS = ledno.ledOffMS; |
| 2046 | if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) { |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 2047 | ledARGB = mDefaultNotificationColor; |
| 2048 | ledOnMS = mDefaultNotificationLedOn; |
| 2049 | ledOffMS = mDefaultNotificationLedOff; |
| 2050 | } |
| 2051 | if (mNotificationPulseEnabled) { |
| 2052 | // pulse repeatedly |
| 2053 | mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED, |
| 2054 | ledOnMS, ledOffMS); |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 2055 | } |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2056 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | // lock on mNotificationList |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2060 | private int indexOfNotificationLocked(String pkg, String tag, int id, int userId) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2061 | { |
| 2062 | ArrayList<NotificationRecord> list = mNotificationList; |
| 2063 | final int len = list.size(); |
| 2064 | for (int i=0; i<len; i++) { |
| 2065 | NotificationRecord r = list.get(i); |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 2066 | if (!notificationMatchesUserId(r, userId) || r.sbn.id != id) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2067 | continue; |
| 2068 | } |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 2069 | if (tag == null) { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 2070 | if (r.sbn.tag != null) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 2071 | continue; |
| 2072 | } |
| 2073 | } else { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 2074 | if (!tag.equals(r.sbn.tag)) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 2075 | continue; |
| 2076 | } |
| 2077 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 2078 | if (r.sbn.pkg.equals(pkg)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2079 | return i; |
| 2080 | } |
| 2081 | } |
| 2082 | return -1; |
| 2083 | } |
| 2084 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 2085 | private void updateNotificationPulse() { |
| 2086 | synchronized (mNotificationList) { |
| 2087 | updateLightsLocked(); |
| 2088 | } |
| 2089 | } |
| 2090 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2091 | // ====================================================================== |
| 2092 | @Override |
| 2093 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 2094 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 2095 | != PackageManager.PERMISSION_GRANTED) { |
| 2096 | pw.println("Permission Denial: can't dump NotificationManager from from pid=" |
| 2097 | + Binder.getCallingPid() |
| 2098 | + ", uid=" + Binder.getCallingUid()); |
| 2099 | return; |
| 2100 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2101 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2102 | pw.println("Current Notification Manager state:"); |
| 2103 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 2104 | pw.println(" Listeners (" + mEnabledListenersForCurrentUser.size() |
| 2105 | + ") enabled for current user:"); |
| 2106 | for (ComponentName cmpt : mEnabledListenersForCurrentUser) { |
| 2107 | pw.println(" " + cmpt); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 2108 | } |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 2109 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 2110 | pw.println(" Live listeners (" + mListeners.size() + "):"); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 2111 | for (NotificationListenerInfo info : mListeners) { |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 2112 | pw.println(" " + info.component |
| 2113 | + " (user " + info.userid + "): " + info.listener |
| 2114 | + (info.isSystem?" SYSTEM":"")); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 2115 | } |
| 2116 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2117 | int N; |
| 2118 | |
| 2119 | synchronized (mToastQueue) { |
| 2120 | N = mToastQueue.size(); |
| 2121 | if (N > 0) { |
| 2122 | pw.println(" Toast Queue:"); |
| 2123 | for (int i=0; i<N; i++) { |
| 2124 | mToastQueue.get(i).dump(pw, " "); |
| 2125 | } |
| 2126 | pw.println(" "); |
| 2127 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2128 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | synchronized (mNotificationList) { |
| 2132 | N = mNotificationList.size(); |
| 2133 | if (N > 0) { |
| 2134 | pw.println(" Notification List:"); |
| 2135 | for (int i=0; i<N; i++) { |
| 2136 | mNotificationList.get(i).dump(pw, " ", mContext); |
| 2137 | } |
| 2138 | pw.println(" "); |
| 2139 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2140 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2141 | N = mLights.size(); |
| 2142 | if (N > 0) { |
| 2143 | pw.println(" Lights List:"); |
| 2144 | for (int i=0; i<N; i++) { |
| Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 2145 | pw.println(" " + mLights.get(i)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2146 | } |
| 2147 | pw.println(" "); |
| 2148 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2149 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2150 | pw.println(" mSoundNotification=" + mSoundNotification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2151 | pw.println(" mVibrateNotification=" + mVibrateNotification); |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 2152 | pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications)); |
| 2153 | pw.println(" mSystemReady=" + mSystemReady); |
| Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 2154 | pw.println(" mArchive=" + mArchive.toString()); |
| 2155 | Iterator<StatusBarNotification> iter = mArchive.descendingIterator(); |
| 2156 | int i=0; |
| 2157 | while (iter.hasNext()) { |
| 2158 | pw.println(" " + iter.next()); |
| 2159 | if (++i >= 5) { |
| 2160 | if (iter.hasNext()) pw.println(" ..."); |
| 2161 | break; |
| 2162 | } |
| 2163 | } |
| 2164 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2165 | } |
| 2166 | } |
| 2167 | } |