| 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; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 34 | import android.content.ContentResolver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.content.Context; |
| 36 | import android.content.Intent; |
| 37 | import android.content.IntentFilter; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 38 | import android.content.pm.ApplicationInfo; |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 39 | import android.content.pm.PackageInfo; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.content.pm.PackageManager; |
| 41 | import android.content.pm.PackageManager.NameNotFoundException; |
| 42 | import android.content.res.Resources; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 43 | import android.database.ContentObserver; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 44 | import android.media.AudioManager; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 45 | import android.media.IAudioService; |
| 46 | import android.media.IRingtonePlayer; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.net.Uri; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.os.Binder; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.os.Handler; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.os.IBinder; |
| 51 | import android.os.Message; |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 52 | import android.os.Parcel; |
| 53 | import android.os.Parcelable; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 54 | import android.os.Process; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 55 | import android.os.RemoteException; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 56 | import android.os.ServiceManager; |
| Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 57 | import android.os.UserHandle; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.os.Vibrator; |
| 59 | import android.provider.Settings; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 60 | import android.telephony.TelephonyManager; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 61 | import android.text.TextUtils; |
| Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 62 | import android.util.AtomicFile; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.util.EventLog; |
| 64 | import android.util.Log; |
| Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 65 | import android.util.Slog; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 66 | import android.util.Xml; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 67 | import android.view.accessibility.AccessibilityEvent; |
| 68 | import android.view.accessibility.AccessibilityManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | import android.widget.Toast; |
| 70 | |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 71 | import com.android.internal.statusbar.StatusBarNotification; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 72 | |
| 73 | import org.xmlpull.v1.XmlPullParser; |
| 74 | import org.xmlpull.v1.XmlPullParserException; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 75 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 76 | import java.io.File; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | import java.io.FileDescriptor; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 78 | import java.io.FileInputStream; |
| 79 | import java.io.FileNotFoundException; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 80 | import java.io.IOException; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | import java.io.PrintWriter; |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 82 | import java.util.ArrayDeque; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | import java.util.ArrayList; |
| 84 | import java.util.Arrays; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 85 | import java.util.HashSet; |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 86 | import java.util.Iterator; |
| 87 | import java.util.NoSuchElementException; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 88 | |
| 89 | import libcore.io.IoUtils; |
| 90 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 92 | /** {@hide} */ |
| 93 | public class NotificationManagerService extends INotificationManager.Stub |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | { |
| 95 | private static final String TAG = "NotificationService"; |
| 96 | private static final boolean DBG = false; |
| 97 | |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 98 | private static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
| 99 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | // message codes |
| 101 | private static final int MESSAGE_TIMEOUT = 2; |
| 102 | |
| 103 | private static final int LONG_DELAY = 3500; // 3.5 seconds |
| 104 | private static final int SHORT_DELAY = 2000; // 2 seconds |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 105 | |
| 106 | private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 107 | 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] | 108 | |
| 109 | private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
| Daniel Sandler | 49a2ad1 | 2012-03-28 15:46:39 -0400 | [diff] [blame] | 110 | private static final boolean SCORE_ONGOING_HIGHER = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 112 | private static final int JUNK_SCORE = -1000; |
| 113 | private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10; |
| 114 | private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER; |
| 115 | |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 116 | // Notifications with scores below this will not interrupt the user, either via LED or |
| 117 | // sound or vibration |
| 118 | private static final int SCORE_INTERRUPTION_THRESHOLD = |
| 119 | Notification.PRIORITY_LOW * NOTIFICATION_PRIORITY_MULTIPLIER; |
| 120 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 121 | private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true; |
| 122 | private static final boolean ENABLE_BLOCKED_TOASTS = true; |
| 123 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | final Context mContext; |
| 125 | final IActivityManager mAm; |
| 126 | final IBinder mForegroundToken = new Binder(); |
| 127 | |
| 128 | private WorkerHandler mHandler; |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 129 | private StatusBarManagerService mStatusBar; |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 130 | private LightsService.Light mNotificationLight; |
| 131 | private LightsService.Light mAttentionLight; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 133 | private int mDefaultNotificationColor; |
| 134 | private int mDefaultNotificationLedOn; |
| 135 | private int mDefaultNotificationLedOff; |
| 136 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 137 | private long[] mDefaultVibrationPattern; |
| 138 | private long[] mFallbackVibrationPattern; |
| 139 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 140 | private boolean mSystemReady; |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 141 | private int mDisabledNotifications; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 143 | private NotificationRecord mSoundNotification; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | private NotificationRecord mVibrateNotification; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 145 | |
| 146 | private IAudioService mAudioService; |
| Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 147 | private Vibrator mVibrator; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 149 | // for enabling and disabling notification pulse behavior |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 150 | private boolean mScreenOn = true; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 151 | private boolean mInCall = false; |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 152 | private boolean mNotificationPulseEnabled; |
| 153 | |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 154 | private final ArrayList<NotificationRecord> mNotificationList = |
| 155 | new ArrayList<NotificationRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | |
| 157 | private ArrayList<ToastRecord> mToastQueue; |
| 158 | |
| 159 | private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | private NotificationRecord mLedNotification; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 161 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 162 | private final AppOpsManager mAppOps; |
| 163 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 164 | // Notification control database. For now just contains disabled packages. |
| 165 | private AtomicFile mPolicyFile; |
| 166 | private HashSet<String> mBlockedPackages = new HashSet<String>(); |
| 167 | |
| 168 | private static final int DB_VERSION = 1; |
| 169 | |
| 170 | private static final String TAG_BODY = "notification-policy"; |
| 171 | private static final String ATTR_VERSION = "version"; |
| 172 | |
| 173 | private static final String TAG_BLOCKED_PKGS = "blocked-packages"; |
| 174 | private static final String TAG_PACKAGE = "package"; |
| 175 | private static final String ATTR_NAME = "name"; |
| 176 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 177 | private static class Archive { |
| 178 | static final int BUFFER_SIZE = 1000; |
| 179 | ArrayDeque<StatusBarNotification> mBuffer = new ArrayDeque<StatusBarNotification>(BUFFER_SIZE); |
| 180 | |
| 181 | public Archive() { |
| 182 | |
| 183 | } |
| 184 | public void record(StatusBarNotification nr) { |
| 185 | if (mBuffer.size() == BUFFER_SIZE) { |
| 186 | mBuffer.removeFirst(); |
| 187 | } |
| 188 | mBuffer.addLast(nr); |
| 189 | } |
| 190 | |
| 191 | public void clear() { |
| 192 | mBuffer.clear(); |
| 193 | } |
| 194 | |
| 195 | public Iterator<StatusBarNotification> descendingIterator() { |
| 196 | return mBuffer.descendingIterator(); |
| 197 | } |
| 198 | public Iterator<StatusBarNotification> ascendingIterator() { |
| 199 | return mBuffer.iterator(); |
| 200 | } |
| 201 | public Iterator<StatusBarNotification> filter( |
| 202 | final Iterator<StatusBarNotification> iter, final String pkg, final int userId) { |
| 203 | return new Iterator<StatusBarNotification>() { |
| 204 | StatusBarNotification mNext = findNext(); |
| 205 | |
| 206 | private StatusBarNotification findNext() { |
| 207 | while (iter.hasNext()) { |
| 208 | StatusBarNotification nr = iter.next(); |
| 209 | if ((pkg == null || nr.pkg == pkg) |
| 210 | && (userId == UserHandle.USER_ALL || nr.getUserId() == userId)) { |
| 211 | return nr; |
| 212 | } |
| 213 | } |
| 214 | return null; |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public boolean hasNext() { |
| 219 | return mNext == null; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public StatusBarNotification next() { |
| 224 | StatusBarNotification next = mNext; |
| 225 | if (next == null) { |
| 226 | throw new NoSuchElementException(); |
| 227 | } |
| 228 | mNext = findNext(); |
| 229 | return next; |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public void remove() { |
| 234 | iter.remove(); |
| 235 | } |
| 236 | }; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | Archive mArchive = new Archive(); |
| 241 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 242 | private void loadBlockDb() { |
| 243 | synchronized(mBlockedPackages) { |
| 244 | if (mPolicyFile == null) { |
| 245 | File dir = new File("/data/system"); |
| 246 | mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml")); |
| 247 | |
| 248 | mBlockedPackages.clear(); |
| 249 | |
| 250 | FileInputStream infile = null; |
| 251 | try { |
| 252 | infile = mPolicyFile.openRead(); |
| 253 | final XmlPullParser parser = Xml.newPullParser(); |
| 254 | parser.setInput(infile, null); |
| 255 | |
| 256 | int type; |
| 257 | String tag; |
| 258 | int version = DB_VERSION; |
| 259 | while ((type = parser.next()) != END_DOCUMENT) { |
| 260 | tag = parser.getName(); |
| 261 | if (type == START_TAG) { |
| 262 | if (TAG_BODY.equals(tag)) { |
| 263 | version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION)); |
| 264 | } else if (TAG_BLOCKED_PKGS.equals(tag)) { |
| 265 | while ((type = parser.next()) != END_DOCUMENT) { |
| 266 | tag = parser.getName(); |
| 267 | if (TAG_PACKAGE.equals(tag)) { |
| 268 | mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME)); |
| 269 | } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) { |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | } catch (FileNotFoundException e) { |
| 277 | // No data yet |
| 278 | } catch (IOException e) { |
| 279 | Log.wtf(TAG, "Unable to read blocked notifications database", e); |
| 280 | } catch (NumberFormatException e) { |
| 281 | Log.wtf(TAG, "Unable to parse blocked notifications database", e); |
| 282 | } catch (XmlPullParserException e) { |
| 283 | Log.wtf(TAG, "Unable to parse blocked notifications database", e); |
| 284 | } finally { |
| 285 | IoUtils.closeQuietly(infile); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 291 | /** |
| 292 | * Use this when you just want to know if notifications are OK for this package. |
| 293 | */ |
| 294 | public boolean areNotificationsEnabledForPackage(String pkg, int uid) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 295 | checkCallerIsSystem(); |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 296 | return (mAppOps.checkOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg) |
| 297 | == AppOpsManager.MODE_ALLOWED); |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 298 | } |
| 299 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 300 | /** Use this when you actually want to post a notification or toast. |
| 301 | * |
| 302 | * Unchecked. Not exposed via Binder, but can be called in the course of enqueue*(). |
| 303 | */ |
| 304 | private boolean noteNotificationOp(String pkg, int uid) { |
| 305 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg) |
| 306 | != AppOpsManager.MODE_ALLOWED) { |
| 307 | Slog.v(TAG, "notifications are disabled by AppOps for " + pkg); |
| 308 | return false; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 309 | } |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 310 | return true; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 311 | } |
| 312 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 313 | public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 314 | checkCallerIsSystem(); |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 315 | if (true||DBG) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 316 | Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg); |
| 317 | } |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 318 | mAppOps.setMode(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg, |
| 319 | enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED); |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | private static String idDebugString(Context baseContext, String packageName, int id) { |
| 324 | Context c = null; |
| 325 | |
| 326 | if (packageName != null) { |
| 327 | try { |
| 328 | c = baseContext.createPackageContext(packageName, 0); |
| 329 | } catch (NameNotFoundException e) { |
| 330 | c = baseContext; |
| 331 | } |
| 332 | } else { |
| 333 | c = baseContext; |
| 334 | } |
| 335 | |
| 336 | String pkg; |
| 337 | String type; |
| 338 | String name; |
| 339 | |
| 340 | Resources r = c.getResources(); |
| 341 | try { |
| 342 | return r.getResourceName(id); |
| 343 | } catch (Resources.NotFoundException e) { |
| 344 | return "<name unknown>"; |
| 345 | } |
| 346 | } |
| 347 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 348 | public StatusBarNotification[] getActiveNotifications(String callingPkg) { |
| 349 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 350 | "NotificationManagerService"); |
| 351 | |
| 352 | StatusBarNotification[] tmp = null; |
| 353 | int userId = UserHandle.getCallingUserId(); |
| 354 | int uid = Binder.getCallingUid(); |
| 355 | |
| 356 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 357 | == AppOpsManager.MODE_ALLOWED) { |
| 358 | synchronized (mNotificationList) { |
| 359 | tmp = new StatusBarNotification[mNotificationList.size()]; |
| 360 | final int N = mNotificationList.size(); |
| 361 | for (int i=0; i<N; i++) { |
| 362 | tmp[i] = mNotificationList.get(i).sbn; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | return tmp; |
| 367 | } |
| 368 | |
| 369 | public static final class NotificationRecord |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 371 | final StatusBarNotification sbn; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | IBinder statusBarKey; |
| 373 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 374 | NotificationRecord(StatusBarNotification sbn) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 376 | this.sbn = sbn; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | } |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 378 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 379 | public Notification getNotification() { return sbn.notification; } |
| 380 | public int getFlags() { return sbn.notification.flags; } |
| 381 | public int getUserId() { return sbn.getUserId(); } |
| 382 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | void dump(PrintWriter pw, String prefix, Context baseContext) { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 384 | final Notification notification = sbn.notification; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | pw.println(prefix + this); |
| 386 | pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon) |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 387 | + " / " + idDebugString(baseContext, this.sbn.pkg, notification.icon)); |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 388 | pw.println(prefix + " pri=" + notification.priority); |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 389 | pw.println(prefix + " score=" + this.sbn.score); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | pw.println(prefix + " contentIntent=" + notification.contentIntent); |
| 391 | pw.println(prefix + " deleteIntent=" + notification.deleteIntent); |
| 392 | pw.println(prefix + " tickerText=" + notification.tickerText); |
| 393 | pw.println(prefix + " contentView=" + notification.contentView); |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 394 | pw.println(prefix + " uid=" + this.sbn.uid + " userId=" + this.sbn.getUserId()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults)); |
| 396 | pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags)); |
| 397 | pw.println(prefix + " sound=" + notification.sound); |
| 398 | pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate)); |
| 399 | pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB) |
| 400 | + " ledOnMS=" + notification.ledOnMS |
| 401 | + " ledOffMS=" + notification.ledOffMS); |
| 402 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 403 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | @Override |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 405 | public final String toString() { |
| 406 | return String.format( |
| 407 | "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s score=%d: %s)", |
| 408 | System.identityHashCode(this), |
| 409 | this.sbn.pkg, this.sbn.user, this.sbn.id, this.sbn.tag, |
| 410 | this.sbn.score, this.sbn.notification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | private static final class ToastRecord |
| 415 | { |
| 416 | final int pid; |
| 417 | final String pkg; |
| 418 | final ITransientNotification callback; |
| 419 | int duration; |
| 420 | |
| 421 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration) |
| 422 | { |
| 423 | this.pid = pid; |
| 424 | this.pkg = pkg; |
| 425 | this.callback = callback; |
| 426 | this.duration = duration; |
| 427 | } |
| 428 | |
| 429 | void update(int duration) { |
| 430 | this.duration = duration; |
| 431 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 432 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | void dump(PrintWriter pw, String prefix) { |
| 434 | pw.println(prefix + this); |
| 435 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 436 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | @Override |
| 438 | public final String toString() |
| 439 | { |
| 440 | return "ToastRecord{" |
| 441 | + Integer.toHexString(System.identityHashCode(this)) |
| 442 | + " pkg=" + pkg |
| 443 | + " callback=" + callback |
| 444 | + " duration=" + duration; |
| 445 | } |
| 446 | } |
| 447 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 448 | private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks |
| 449 | = new StatusBarManagerService.NotificationCallbacks() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | |
| 451 | public void onSetDisabled(int status) { |
| 452 | synchronized (mNotificationList) { |
| 453 | mDisabledNotifications = status; |
| 454 | if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) { |
| 455 | // cancel whatever's going on |
| 456 | long identity = Binder.clearCallingIdentity(); |
| 457 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 458 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 459 | if (player != null) { |
| 460 | player.stopAsync(); |
| 461 | } |
| 462 | } catch (RemoteException e) { |
| 463 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | Binder.restoreCallingIdentity(identity); |
| 465 | } |
| 466 | |
| 467 | identity = Binder.clearCallingIdentity(); |
| 468 | try { |
| 469 | mVibrator.cancel(); |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 470 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | Binder.restoreCallingIdentity(identity); |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | public void onClearAll() { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 478 | // XXX to be totally correct, the caller should tell us which user |
| 479 | // this is for. |
| 480 | cancelAll(ActivityManager.getCurrentUser()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | } |
| 482 | |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 483 | public void onNotificationClick(String pkg, String tag, int id) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 484 | // XXX to be totally correct, the caller should tell us which user |
| 485 | // this is for. |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 486 | cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 487 | Notification.FLAG_FOREGROUND_SERVICE, false, |
| 488 | ActivityManager.getCurrentUser()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | } |
| 490 | |
| Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 491 | public void onNotificationClear(String pkg, String tag, int id) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 492 | // XXX to be totally correct, the caller should tell us which user |
| 493 | // this is for. |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 494 | cancelNotification(pkg, tag, id, 0, |
| 495 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 496 | true, ActivityManager.getCurrentUser()); |
| Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 497 | } |
| 498 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | public void onPanelRevealed() { |
| 500 | synchronized (mNotificationList) { |
| 501 | // sound |
| 502 | mSoundNotification = null; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 503 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | long identity = Binder.clearCallingIdentity(); |
| 505 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 506 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 507 | if (player != null) { |
| 508 | player.stopAsync(); |
| 509 | } |
| 510 | } catch (RemoteException e) { |
| 511 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | Binder.restoreCallingIdentity(identity); |
| 513 | } |
| 514 | |
| 515 | // vibrate |
| 516 | mVibrateNotification = null; |
| 517 | identity = Binder.clearCallingIdentity(); |
| 518 | try { |
| 519 | mVibrator.cancel(); |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 520 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | Binder.restoreCallingIdentity(identity); |
| 522 | } |
| 523 | |
| 524 | // light |
| 525 | mLights.clear(); |
| 526 | mLedNotification = null; |
| 527 | updateLightsLocked(); |
| 528 | } |
| 529 | } |
| Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 530 | |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 531 | public void onNotificationError(String pkg, String tag, int id, |
| 532 | int uid, int initialPid, String message) { |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 533 | Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id |
| 534 | + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")"); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 535 | // XXX to be totally correct, the caller should tell us which user |
| 536 | // this is for. |
| 537 | cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid)); |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 538 | long ident = Binder.clearCallingIdentity(); |
| 539 | try { |
| 540 | ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg, |
| 541 | "Bad notification posted from package " + pkg |
| 542 | + ": " + message); |
| 543 | } catch (RemoteException e) { |
| 544 | } |
| 545 | Binder.restoreCallingIdentity(ident); |
| Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 546 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | }; |
| 548 | |
| 549 | private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 550 | @Override |
| 551 | public void onReceive(Context context, Intent intent) { |
| 552 | String action = intent.getAction(); |
| 553 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 554 | boolean queryRestart = false; |
| Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 555 | boolean packageChanged = false; |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 556 | |
| Mike Lockwood | 541c994 | 2011-06-12 19:35:45 -0400 | [diff] [blame] | 557 | if (action.equals(Intent.ACTION_PACKAGE_REMOVED) |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 558 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
| Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 559 | || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED)) |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 560 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 561 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 562 | String pkgList[] = null; |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 563 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 564 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 565 | } else if (queryRestart) { |
| 566 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 567 | } else { |
| 568 | Uri uri = intent.getData(); |
| 569 | if (uri == null) { |
| 570 | return; |
| 571 | } |
| 572 | String pkgName = uri.getSchemeSpecificPart(); |
| 573 | if (pkgName == null) { |
| 574 | return; |
| 575 | } |
| Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 576 | if (packageChanged) { |
| 577 | // We cancel notifications for packages which have just been disabled |
| 578 | final int enabled = mContext.getPackageManager() |
| 579 | .getApplicationEnabledSetting(pkgName); |
| 580 | if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 581 | || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 582 | return; |
| 583 | } |
| 584 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 585 | pkgList = new String[]{pkgName}; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 587 | if (pkgList != null && (pkgList.length > 0)) { |
| 588 | for (String pkgName : pkgList) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 589 | cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart, |
| 590 | UserHandle.USER_ALL); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 591 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | } |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 593 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
| 594 | // Keep track of screen on/off state, but do not turn off the notification light |
| 595 | // until user passes through the lock screen or views the notification. |
| 596 | mScreenOn = true; |
| 597 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 598 | mScreenOn = false; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 599 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 600 | mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals( |
| 601 | TelephonyManager.EXTRA_STATE_OFFHOOK)); |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 602 | updateNotificationPulse(); |
| Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 603 | } else if (action.equals(Intent.ACTION_USER_STOPPED)) { |
| 604 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 605 | if (userHandle >= 0) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 606 | cancelAllNotificationsInt(null, 0, 0, true, userHandle); |
| Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 607 | } |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 608 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 609 | // turn off LED when user passes through lock screen |
| 610 | mNotificationLight.turnOff(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | }; |
| 614 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 615 | class SettingsObserver extends ContentObserver { |
| 616 | SettingsObserver(Handler handler) { |
| 617 | super(handler); |
| 618 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 619 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 620 | void observe() { |
| 621 | ContentResolver resolver = mContext.getContentResolver(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 622 | resolver.registerContentObserver(Settings.System.getUriFor( |
| 623 | Settings.System.NOTIFICATION_LIGHT_PULSE), false, this); |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 624 | update(); |
| 625 | } |
| 626 | |
| 627 | @Override public void onChange(boolean selfChange) { |
| 628 | update(); |
| 629 | } |
| 630 | |
| 631 | public void update() { |
| 632 | ContentResolver resolver = mContext.getContentResolver(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 633 | boolean pulseEnabled = Settings.System.getInt(resolver, |
| 634 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0; |
| 635 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 636 | mNotificationPulseEnabled = pulseEnabled; |
| 637 | updateNotificationPulse(); |
| 638 | } |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 639 | } |
| 640 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 641 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 642 | static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) { |
| 643 | int[] ar = r.getIntArray(resid); |
| 644 | if (ar == null) { |
| 645 | return def; |
| 646 | } |
| 647 | final int len = ar.length > maxlen ? maxlen : ar.length; |
| 648 | long[] out = new long[len]; |
| 649 | for (int i=0; i<len; i++) { |
| 650 | out[i] = ar[i]; |
| 651 | } |
| 652 | return out; |
| 653 | } |
| 654 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 655 | NotificationManagerService(Context context, StatusBarManagerService statusBar, |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 656 | LightsService lights) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | { |
| 658 | super(); |
| 659 | mContext = context; |
| Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 660 | mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | mAm = ActivityManagerNative.getDefault(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | mToastQueue = new ArrayList<ToastRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | mHandler = new WorkerHandler(); |
| San Mehat | 3ee1317 | 2010-02-04 20:54:43 -0800 | [diff] [blame] | 664 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 665 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
| 666 | |
| 667 | importOldBlockDb(); |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 668 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 669 | mStatusBar = statusBar; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | statusBar.setNotificationCallbacks(mNotificationCallbacks); |
| 671 | |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 672 | mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS); |
| 673 | mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION); |
| 674 | |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 675 | Resources resources = mContext.getResources(); |
| 676 | mDefaultNotificationColor = resources.getColor( |
| 677 | com.android.internal.R.color.config_defaultNotificationColor); |
| 678 | mDefaultNotificationLedOn = resources.getInteger( |
| 679 | com.android.internal.R.integer.config_defaultNotificationLedOn); |
| 680 | mDefaultNotificationLedOff = resources.getInteger( |
| 681 | com.android.internal.R.integer.config_defaultNotificationLedOff); |
| 682 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 683 | mDefaultVibrationPattern = getLongArray(resources, |
| 684 | com.android.internal.R.array.config_defaultNotificationVibePattern, |
| 685 | VIBRATE_PATTERN_MAXLEN, |
| 686 | DEFAULT_VIBRATE_PATTERN); |
| 687 | |
| 688 | mFallbackVibrationPattern = getLongArray(resources, |
| 689 | com.android.internal.R.array.config_notificationFallbackVibePattern, |
| 690 | VIBRATE_PATTERN_MAXLEN, |
| 691 | DEFAULT_VIBRATE_PATTERN); |
| 692 | |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 693 | // Don't start allowing notifications until the setup wizard has run once. |
| 694 | // After that, including subsequent boots, init with notifications turned on. |
| 695 | // This works on the first boot because the setup wizard will toggle this |
| 696 | // 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] | 697 | if (0 == Settings.Global.getInt(mContext.getContentResolver(), |
| 698 | Settings.Global.DEVICE_PROVISIONED, 0)) { |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 699 | mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS; |
| 700 | } |
| 701 | |
| Mike Lockwood | 35e16bf | 2010-11-30 19:53:36 -0500 | [diff] [blame] | 702 | // register for various Intents |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | IntentFilter filter = new IntentFilter(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 704 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 705 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 706 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 707 | filter.addAction(Intent.ACTION_USER_PRESENT); |
| Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 708 | filter.addAction(Intent.ACTION_USER_STOPPED); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 709 | mContext.registerReceiver(mIntentReceiver, filter); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 710 | IntentFilter pkgFilter = new IntentFilter(); |
| 711 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| Daniel Sandler | aac0eb0 | 2011-08-06 22:51:56 -0400 | [diff] [blame] | 712 | pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 713 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 714 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 715 | pkgFilter.addDataScheme("package"); |
| 716 | mContext.registerReceiver(mIntentReceiver, pkgFilter); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 717 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 718 | mContext.registerReceiver(mIntentReceiver, sdFilter); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 719 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 720 | SettingsObserver observer = new SettingsObserver(mHandler); |
| 721 | observer.observe(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | } |
| 723 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 724 | /** |
| 725 | * Read the old XML-based app block database and import those blockages into the AppOps system. |
| 726 | */ |
| 727 | private void importOldBlockDb() { |
| 728 | loadBlockDb(); |
| 729 | |
| 730 | PackageManager pm = mContext.getPackageManager(); |
| 731 | for (String pkg : mBlockedPackages) { |
| 732 | PackageInfo info = null; |
| 733 | try { |
| 734 | info = pm.getPackageInfo(pkg, 0); |
| 735 | setNotificationsEnabledForPackage(pkg, info.applicationInfo.uid, false); |
| 736 | } catch (NameNotFoundException e) { |
| 737 | // forget you |
| 738 | } |
| 739 | } |
| 740 | mBlockedPackages.clear(); |
| 741 | if (mPolicyFile != null) { |
| 742 | mPolicyFile.delete(); |
| 743 | } |
| 744 | } |
| 745 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 746 | void systemReady() { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 747 | mAudioService = IAudioService.Stub.asInterface( |
| 748 | ServiceManager.getService(Context.AUDIO_SERVICE)); |
| 749 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 750 | // no beeping until we're basically done booting |
| 751 | mSystemReady = true; |
| 752 | } |
| 753 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 754 | // Toasts |
| 755 | // ============================================================================ |
| 756 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
| 757 | { |
| Daniel Sandler | a703590 | 2010-03-30 15:45:31 -0400 | [diff] [blame] | 758 | 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] | 759 | |
| 760 | if (pkg == null || callback == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 761 | 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] | 762 | return ; |
| 763 | } |
| 764 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 765 | final boolean isSystemToast = ("android".equals(pkg)); |
| 766 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 767 | if (ENABLE_BLOCKED_TOASTS && !noteNotificationOp(pkg, Binder.getCallingUid())) { |
| 768 | if (!isSystemToast) { |
| 769 | Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request."); |
| 770 | return; |
| 771 | } |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 772 | } |
| 773 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 774 | synchronized (mToastQueue) { |
| 775 | int callingPid = Binder.getCallingPid(); |
| 776 | long callingId = Binder.clearCallingIdentity(); |
| 777 | try { |
| 778 | ToastRecord record; |
| 779 | int index = indexOfToastLocked(pkg, callback); |
| 780 | // If it's already in the queue, we update it in place, we don't |
| 781 | // move it to the end of the queue. |
| 782 | if (index >= 0) { |
| 783 | record = mToastQueue.get(index); |
| 784 | record.update(duration); |
| 785 | } else { |
| Vairavan Srinivasan | f9eb06c | 2011-01-21 18:08:36 -0800 | [diff] [blame] | 786 | // Limit the number of toasts that any given package except the android |
| 787 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 788 | if (!isSystemToast) { |
| Vairavan Srinivasan | f9eb06c | 2011-01-21 18:08:36 -0800 | [diff] [blame] | 789 | int count = 0; |
| 790 | final int N = mToastQueue.size(); |
| 791 | for (int i=0; i<N; i++) { |
| 792 | final ToastRecord r = mToastQueue.get(i); |
| 793 | if (r.pkg.equals(pkg)) { |
| 794 | count++; |
| 795 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 796 | Slog.e(TAG, "Package has already posted " + count |
| 797 | + " toasts. Not showing more. Package=" + pkg); |
| 798 | return; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | record = new ToastRecord(callingPid, pkg, callback, duration); |
| 805 | mToastQueue.add(record); |
| 806 | index = mToastQueue.size() - 1; |
| 807 | keepProcessAliveLocked(callingPid); |
| 808 | } |
| 809 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 810 | // new or just been updated. Call back and tell it to show itself. |
| 811 | // If the callback fails, this will remove it from the list, so don't |
| 812 | // assume that it's valid after this. |
| 813 | if (index == 0) { |
| 814 | showNextToastLocked(); |
| 815 | } |
| 816 | } finally { |
| 817 | Binder.restoreCallingIdentity(callingId); |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | public void cancelToast(String pkg, ITransientNotification callback) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 823 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | |
| 825 | if (pkg == null || callback == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 826 | 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] | 827 | return ; |
| 828 | } |
| 829 | |
| 830 | synchronized (mToastQueue) { |
| 831 | long callingId = Binder.clearCallingIdentity(); |
| 832 | try { |
| 833 | int index = indexOfToastLocked(pkg, callback); |
| 834 | if (index >= 0) { |
| 835 | cancelToastLocked(index); |
| 836 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 837 | 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] | 838 | } |
| 839 | } finally { |
| 840 | Binder.restoreCallingIdentity(callingId); |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | private void showNextToastLocked() { |
| 846 | ToastRecord record = mToastQueue.get(0); |
| 847 | while (record != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 848 | 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] | 849 | try { |
| 850 | record.callback.show(); |
| 851 | scheduleTimeoutLocked(record, false); |
| 852 | return; |
| 853 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 854 | 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] | 855 | + " in package " + record.pkg); |
| 856 | // remove it from the list and let the process die |
| 857 | int index = mToastQueue.indexOf(record); |
| 858 | if (index >= 0) { |
| 859 | mToastQueue.remove(index); |
| 860 | } |
| 861 | keepProcessAliveLocked(record.pid); |
| 862 | if (mToastQueue.size() > 0) { |
| 863 | record = mToastQueue.get(0); |
| 864 | } else { |
| 865 | record = null; |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | private void cancelToastLocked(int index) { |
| 872 | ToastRecord record = mToastQueue.get(index); |
| 873 | try { |
| 874 | record.callback.hide(); |
| 875 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 876 | 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] | 877 | + " in package " + record.pkg); |
| 878 | // don't worry about this, we're about to remove it from |
| 879 | // the list anyway |
| 880 | } |
| 881 | mToastQueue.remove(index); |
| 882 | keepProcessAliveLocked(record.pid); |
| 883 | if (mToastQueue.size() > 0) { |
| 884 | // Show the next one. If the callback fails, this will remove |
| 885 | // it from the list, so don't assume that the list hasn't changed |
| 886 | // after this point. |
| 887 | showNextToastLocked(); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | private void scheduleTimeoutLocked(ToastRecord r, boolean immediate) |
| 892 | { |
| 893 | Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r); |
| 894 | long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY); |
| 895 | mHandler.removeCallbacksAndMessages(r); |
| 896 | mHandler.sendMessageDelayed(m, delay); |
| 897 | } |
| 898 | |
| 899 | private void handleTimeout(ToastRecord record) |
| 900 | { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 901 | 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] | 902 | synchronized (mToastQueue) { |
| 903 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 904 | if (index >= 0) { |
| 905 | cancelToastLocked(index); |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | // lock on mToastQueue |
| 911 | private int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 912 | { |
| 913 | IBinder cbak = callback.asBinder(); |
| 914 | ArrayList<ToastRecord> list = mToastQueue; |
| 915 | int len = list.size(); |
| 916 | for (int i=0; i<len; i++) { |
| 917 | ToastRecord r = list.get(i); |
| 918 | if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) { |
| 919 | return i; |
| 920 | } |
| 921 | } |
| 922 | return -1; |
| 923 | } |
| 924 | |
| 925 | // lock on mToastQueue |
| 926 | private void keepProcessAliveLocked(int pid) |
| 927 | { |
| 928 | int toastCount = 0; // toasts from this pid |
| 929 | ArrayList<ToastRecord> list = mToastQueue; |
| 930 | int N = list.size(); |
| 931 | for (int i=0; i<N; i++) { |
| 932 | ToastRecord r = list.get(i); |
| 933 | if (r.pid == pid) { |
| 934 | toastCount++; |
| 935 | } |
| 936 | } |
| 937 | try { |
| 938 | mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0); |
| 939 | } catch (RemoteException e) { |
| 940 | // Shouldn't happen. |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | private final class WorkerHandler extends Handler |
| 945 | { |
| 946 | @Override |
| 947 | public void handleMessage(Message msg) |
| 948 | { |
| 949 | switch (msg.what) |
| 950 | { |
| 951 | case MESSAGE_TIMEOUT: |
| 952 | handleTimeout((ToastRecord)msg.obj); |
| 953 | break; |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | |
| 959 | // Notifications |
| 960 | // ============================================================================ |
| Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 961 | public void enqueueNotificationWithTag(String pkg, String basePkg, String tag, int id, |
| 962 | Notification notification, int[] idOut, int userId) |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 963 | { |
| Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 964 | enqueueNotificationInternal(pkg, basePkg, Binder.getCallingUid(), Binder.getCallingPid(), |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 965 | tag, id, notification, idOut, userId); |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 966 | } |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 967 | |
| 968 | private final static int clamp(int x, int low, int high) { |
| 969 | return (x < low) ? low : ((x > high) ? high : x); |
| Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 970 | } |
| 971 | |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 972 | // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the |
| 973 | // uid/pid of another application) |
| Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 974 | public void enqueueNotificationInternal(String pkg, String basePkg, int callingUid, |
| 975 | int callingPid, String tag, int id, Notification notification, int[] idOut, int userId) |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 976 | { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 977 | if (DBG) { |
| 978 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification); |
| 979 | } |
| 980 | checkCallerIsSystemOrSameApp(pkg); |
| 981 | final boolean isSystemNotification = ("android".equals(pkg)); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 982 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 983 | userId = ActivityManager.handleIncomingUser(callingPid, |
| Amith Yamasani | 2c7ebea | 2012-10-30 15:28:27 -0700 | [diff] [blame] | 984 | callingUid, userId, true, false, "enqueueNotification", pkg); |
| Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 985 | final UserHandle user = new UserHandle(userId); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 986 | |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 987 | // Limit the number of notifications that any given package except the android |
| 988 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 989 | if (!isSystemNotification) { |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 990 | synchronized (mNotificationList) { |
| 991 | int count = 0; |
| 992 | final int N = mNotificationList.size(); |
| 993 | for (int i=0; i<N; i++) { |
| 994 | final NotificationRecord r = mNotificationList.get(i); |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 995 | if (r.sbn.pkg.equals(pkg) && r.sbn.getUserId() == userId) { |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 996 | count++; |
| 997 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 998 | Slog.e(TAG, "Package has already posted " + count |
| 999 | + " notifications. Not showing more. package=" + pkg); |
| 1000 | return; |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | // This conditional is a dirty hack to limit the logging done on |
| 1008 | // behalf of the download manager without affecting other apps. |
| 1009 | if (!pkg.equals("com.android.providers.downloads") |
| 1010 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1011 | EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId, |
| Daniel Sandler | b64cb88 | 2011-11-29 23:48:29 -0500 | [diff] [blame] | 1012 | notification.toString()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | if (pkg == null || notification == null) { |
| 1016 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 1017 | + " id=" + id + " notification=" + notification); |
| 1018 | } |
| 1019 | if (notification.icon != 0) { |
| 1020 | if (notification.contentView == null) { |
| 1021 | throw new IllegalArgumentException("contentView required: pkg=" + pkg |
| 1022 | + " id=" + id + " notification=" + notification); |
| 1023 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1026 | // === Scoring === |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1027 | |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1028 | // 0. Sanitize inputs |
| 1029 | notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX); |
| 1030 | // Migrate notification flags to scores |
| 1031 | if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) { |
| 1032 | if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX; |
| Daniel Sandler | 49a2ad1 | 2012-03-28 15:46:39 -0400 | [diff] [blame] | 1033 | } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) { |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1034 | if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH; |
| 1035 | } |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1036 | |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1037 | // 1. initial score: buckets of 10, around the app |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1038 | int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20] |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1039 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1040 | // 2. Consult external heuristics (TBD) |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1041 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1042 | // 3. Apply local rules |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1043 | |
| 1044 | // blocked apps |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1045 | if (ENABLE_BLOCKED_NOTIFICATIONS && !noteNotificationOp(pkg, callingUid)) { |
| 1046 | if (!isSystemNotification) { |
| 1047 | score = JUNK_SCORE; |
| 1048 | Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request."); |
| 1049 | } |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | if (DBG) { |
| 1053 | Slog.v(TAG, "Assigned score=" + score + " to " + notification); |
| 1054 | } |
| 1055 | |
| 1056 | if (score < SCORE_DISPLAY_THRESHOLD) { |
| 1057 | // Notification will be blocked because the score is too low. |
| 1058 | return; |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1059 | } |
| 1060 | |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 1061 | // Should this notification make noise, vibe, or use the LED? |
| 1062 | final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD); |
| 1063 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | synchronized (mNotificationList) { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1065 | final StatusBarNotification n = new StatusBarNotification( |
| 1066 | pkg, id, tag, callingUid, callingPid, score, notification, user); |
| 1067 | NotificationRecord r = new NotificationRecord(n); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | NotificationRecord old = null; |
| 1069 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1070 | int index = indexOfNotificationLocked(pkg, tag, id, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | if (index < 0) { |
| 1072 | mNotificationList.add(r); |
| 1073 | } else { |
| 1074 | old = mNotificationList.remove(index); |
| 1075 | mNotificationList.add(index, r); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1076 | // Make sure we don't lose the foreground service state. |
| 1077 | if (old != null) { |
| 1078 | notification.flags |= |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1079 | old.getNotification().flags&Notification.FLAG_FOREGROUND_SERVICE; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1080 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1082 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1083 | // Ensure if this is a foreground service that the proper additional |
| 1084 | // flags are set. |
| 1085 | if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 1086 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 1087 | | Notification.FLAG_NO_CLEAR; |
| 1088 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1089 | |
| Svetoslav Ganov | c31ed39 | 2012-10-10 14:58:28 -0700 | [diff] [blame] | 1090 | final int currentUser; |
| 1091 | final long token = Binder.clearCallingIdentity(); |
| 1092 | try { |
| 1093 | currentUser = ActivityManager.getCurrentUser(); |
| 1094 | } finally { |
| 1095 | Binder.restoreCallingIdentity(token); |
| 1096 | } |
| 1097 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1098 | if (notification.icon != 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | if (old != null && old.statusBarKey != null) { |
| 1100 | r.statusBarKey = old.statusBarKey; |
| 1101 | long identity = Binder.clearCallingIdentity(); |
| 1102 | try { |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1103 | mStatusBar.updateNotification(r.statusBarKey, n); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1104 | } |
| 1105 | finally { |
| 1106 | Binder.restoreCallingIdentity(identity); |
| 1107 | } |
| 1108 | } else { |
| 1109 | long identity = Binder.clearCallingIdentity(); |
| 1110 | try { |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1111 | r.statusBarKey = mStatusBar.addNotification(n); |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 1112 | if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0 |
| 1113 | && canInterrupt) { |
| Mike Lockwood | ece18ef | 2012-02-13 20:42:19 -0800 | [diff] [blame] | 1114 | mAttentionLight.pulse(); |
| 1115 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1116 | } |
| 1117 | finally { |
| 1118 | Binder.restoreCallingIdentity(identity); |
| 1119 | } |
| 1120 | } |
| Svetoslav Ganov | c31ed39 | 2012-10-10 14:58:28 -0700 | [diff] [blame] | 1121 | // Send accessibility events only for the current user. |
| 1122 | if (currentUser == userId) { |
| 1123 | sendAccessibilityEvent(notification, pkg); |
| 1124 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1125 | |
| 1126 | // finally, keep some of this information around for later use |
| 1127 | mArchive.record(n); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1128 | } else { |
| Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 1129 | Slog.e(TAG, "Ignoring notification with icon==0: " + notification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1130 | if (old != null && old.statusBarKey != null) { |
| 1131 | long identity = Binder.clearCallingIdentity(); |
| 1132 | try { |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 1133 | mStatusBar.removeNotification(old.statusBarKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1134 | } |
| 1135 | finally { |
| 1136 | Binder.restoreCallingIdentity(identity); |
| 1137 | } |
| 1138 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1139 | 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] | 1140 | } |
| 1141 | |
| 1142 | // If we're not supposed to beep, vibrate, etc. then don't. |
| 1143 | if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0) |
| 1144 | && (!(old != null |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1145 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 )) |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1146 | && (r.getUserId() == UserHandle.USER_ALL || |
| 1147 | (r.getUserId() == userId && r.getUserId() == currentUser)) |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 1148 | && canInterrupt |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1149 | && mSystemReady) { |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1150 | |
| 1151 | final AudioManager audioManager = (AudioManager) mContext |
| 1152 | .getSystemService(Context.AUDIO_SERVICE); |
| Daniel Sandler | d4d2de2 | 2012-11-14 11:25:46 -0800 | [diff] [blame] | 1153 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1154 | // sound |
| 1155 | final boolean useDefaultSound = |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1156 | (notification.defaults & Notification.DEFAULT_SOUND) != 0; |
| Daniel Sandler | d4d2de2 | 2012-11-14 11:25:46 -0800 | [diff] [blame] | 1157 | |
| 1158 | Uri soundUri = null; |
| 1159 | boolean hasValidSound = false; |
| 1160 | |
| 1161 | if (useDefaultSound) { |
| 1162 | soundUri = Settings.System.DEFAULT_NOTIFICATION_URI; |
| 1163 | |
| 1164 | // check to see if the default notification sound is silent |
| 1165 | ContentResolver resolver = mContext.getContentResolver(); |
| 1166 | hasValidSound = Settings.System.getString(resolver, |
| 1167 | Settings.System.NOTIFICATION_SOUND) != null; |
| 1168 | } else if (notification.sound != null) { |
| 1169 | soundUri = notification.sound; |
| 1170 | hasValidSound = (soundUri != null); |
| 1171 | } |
| 1172 | |
| 1173 | if (hasValidSound) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1174 | boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0; |
| 1175 | int audioStreamType; |
| 1176 | if (notification.audioStreamType >= 0) { |
| 1177 | audioStreamType = notification.audioStreamType; |
| 1178 | } else { |
| 1179 | audioStreamType = DEFAULT_STREAM_TYPE; |
| 1180 | } |
| 1181 | mSoundNotification = r; |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1182 | // do not play notifications if stream volume is 0 |
| Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 1183 | // (typically because ringer mode is silent) or if speech recognition is active. |
| 1184 | if ((audioManager.getStreamVolume(audioStreamType) != 0) |
| 1185 | && !audioManager.isSpeechRecognitionActive()) { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1186 | final long identity = Binder.clearCallingIdentity(); |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1187 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1188 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 1189 | if (player != null) { |
| Daniel Sandler | d4d2de2 | 2012-11-14 11:25:46 -0800 | [diff] [blame] | 1190 | player.playAsync(soundUri, user, looping, audioStreamType); |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1191 | } |
| 1192 | } catch (RemoteException e) { |
| 1193 | } finally { |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1194 | Binder.restoreCallingIdentity(identity); |
| 1195 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | // vibrate |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1200 | // Does the notification want to specify its own vibration? |
| 1201 | final boolean hasCustomVibrate = notification.vibrate != null; |
| 1202 | |
| David Agnew | 71789e1 | 2012-11-09 23:03:26 -0500 | [diff] [blame] | 1203 | // 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] | 1204 | // and no other vibration is specified, we fall back to vibration |
| David Agnew | 71789e1 | 2012-11-09 23:03:26 -0500 | [diff] [blame] | 1205 | final boolean convertSoundToVibration = |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1206 | !hasCustomVibrate |
| Daniel Sandler | d4d2de2 | 2012-11-14 11:25:46 -0800 | [diff] [blame] | 1207 | && hasValidSound |
| David Agnew | 71789e1 | 2012-11-09 23:03:26 -0500 | [diff] [blame] | 1208 | && (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE); |
| 1209 | |
| Daniel Sandler | 4a7a9b9 | 2012-11-20 12:59:41 -0500 | [diff] [blame] | 1210 | // 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] | 1211 | final boolean useDefaultVibrate = |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1212 | (notification.defaults & Notification.DEFAULT_VIBRATE) != 0; |
| David Agnew | 71789e1 | 2012-11-09 23:03:26 -0500 | [diff] [blame] | 1213 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1214 | if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate) |
| Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1215 | && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1216 | mVibrateNotification = r; |
| 1217 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1218 | if (useDefaultVibrate || convertSoundToVibration) { |
| 1219 | // Escalate privileges so we can use the vibrator even if the notifying app |
| 1220 | // does not have the VIBRATE permission. |
| 1221 | long identity = Binder.clearCallingIdentity(); |
| 1222 | try { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1223 | mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg, |
| Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1224 | useDefaultVibrate ? mDefaultVibrationPattern |
| 1225 | : mFallbackVibrationPattern, |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1226 | ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1); |
| 1227 | } finally { |
| 1228 | Binder.restoreCallingIdentity(identity); |
| 1229 | } |
| 1230 | } else if (notification.vibrate.length > 1) { |
| 1231 | // If you want your own vibration pattern, you need the VIBRATE permission |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1232 | mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg, notification.vibrate, |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1233 | ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1); |
| 1234 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1238 | // light |
| 1239 | // the most recent thing gets the light |
| 1240 | mLights.remove(old); |
| 1241 | if (mLedNotification == old) { |
| 1242 | mLedNotification = null; |
| 1243 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1244 | //Slog.i(TAG, "notification.lights=" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0)); |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 1246 | if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0 |
| 1247 | && canInterrupt) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | mLights.add(r); |
| 1249 | updateLightsLocked(); |
| 1250 | } else { |
| 1251 | if (old != null |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1252 | && ((old.getFlags() & Notification.FLAG_SHOW_LIGHTS) != 0)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1253 | updateLightsLocked(); |
| 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | idOut[0] = id; |
| 1259 | } |
| 1260 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1261 | private void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1262 | AccessibilityManager manager = AccessibilityManager.getInstance(mContext); |
| 1263 | if (!manager.isEnabled()) { |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | AccessibilityEvent event = |
| 1268 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 1269 | event.setPackageName(packageName); |
| 1270 | event.setClassName(Notification.class.getName()); |
| 1271 | event.setParcelableData(notification); |
| 1272 | CharSequence tickerText = notification.tickerText; |
| 1273 | if (!TextUtils.isEmpty(tickerText)) { |
| 1274 | event.getText().add(tickerText); |
| 1275 | } |
| 1276 | |
| 1277 | manager.sendAccessibilityEvent(event); |
| 1278 | } |
| 1279 | |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1280 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) { |
| 1281 | // tell the app |
| 1282 | if (sendDelete) { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1283 | if (r.getNotification().deleteIntent != null) { |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1284 | try { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1285 | r.getNotification().deleteIntent.send(); |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1286 | } catch (PendingIntent.CanceledException ex) { |
| 1287 | // do nothing - there's no relevant way to recover, and |
| 1288 | // no reason to let this propagate |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1289 | Slog.w(TAG, "canceled PendingIntent for " + r.sbn.pkg, ex); |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | } |
| 1293 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1294 | // status bar |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1295 | if (r.getNotification().icon != 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | long identity = Binder.clearCallingIdentity(); |
| 1297 | try { |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 1298 | mStatusBar.removeNotification(r.statusBarKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1299 | } |
| 1300 | finally { |
| 1301 | Binder.restoreCallingIdentity(identity); |
| 1302 | } |
| 1303 | r.statusBarKey = null; |
| 1304 | } |
| 1305 | |
| 1306 | // sound |
| 1307 | if (mSoundNotification == r) { |
| 1308 | mSoundNotification = null; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1309 | final long identity = Binder.clearCallingIdentity(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1310 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1311 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 1312 | if (player != null) { |
| 1313 | player.stopAsync(); |
| 1314 | } |
| 1315 | } catch (RemoteException e) { |
| 1316 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1317 | Binder.restoreCallingIdentity(identity); |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | // vibrate |
| 1322 | if (mVibrateNotification == r) { |
| 1323 | mVibrateNotification = null; |
| 1324 | long identity = Binder.clearCallingIdentity(); |
| 1325 | try { |
| 1326 | mVibrator.cancel(); |
| 1327 | } |
| 1328 | finally { |
| 1329 | Binder.restoreCallingIdentity(identity); |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | // light |
| 1334 | mLights.remove(r); |
| 1335 | if (mLedNotification == r) { |
| 1336 | mLedNotification = null; |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | /** |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1341 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1342 | * and none of the {@code mustNotHaveFlags}. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1343 | */ |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1344 | private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1345 | int mustNotHaveFlags, boolean sendDelete, int userId) { |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1346 | EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId, |
| Daniel Sandler | b64cb88 | 2011-11-29 23:48:29 -0500 | [diff] [blame] | 1347 | mustHaveFlags, mustNotHaveFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1348 | |
| 1349 | synchronized (mNotificationList) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1350 | int index = indexOfNotificationLocked(pkg, tag, id, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1351 | if (index >= 0) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1352 | NotificationRecord r = mNotificationList.get(index); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1353 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1354 | if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1355 | return; |
| 1356 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1357 | if ((r.getNotification().flags & mustNotHaveFlags) != 0) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1358 | return; |
| 1359 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1360 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1361 | mNotificationList.remove(index); |
| 1362 | |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1363 | cancelNotificationLocked(r, sendDelete); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1364 | updateLightsLocked(); |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | /** |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1370 | * Determine whether the userId applies to the notification in question, either because |
| 1371 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard). |
| 1372 | */ |
| 1373 | private boolean notificationMatchesUserId(NotificationRecord r, int userId) { |
| 1374 | return |
| 1375 | // looking for USER_ALL notifications? match everything |
| 1376 | userId == UserHandle.USER_ALL |
| 1377 | // a notification sent to USER_ALL matches any query |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1378 | || r.getUserId() == UserHandle.USER_ALL |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1379 | // an exact user match |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1380 | || r.getUserId() == userId; |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | /** |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1384 | * Cancels all notifications from a given package that have all of the |
| 1385 | * {@code mustHaveFlags}. |
| 1386 | */ |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1387 | boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1388 | int mustNotHaveFlags, boolean doit, int userId) { |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1389 | EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId, |
| 1390 | mustHaveFlags, mustNotHaveFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1391 | |
| 1392 | synchronized (mNotificationList) { |
| 1393 | final int N = mNotificationList.size(); |
| 1394 | boolean canceledSomething = false; |
| 1395 | for (int i = N-1; i >= 0; --i) { |
| 1396 | NotificationRecord r = mNotificationList.get(i); |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1397 | if (!notificationMatchesUserId(r, userId)) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1398 | continue; |
| 1399 | } |
| Amith Yamasani | 5ec00e9 | 2012-11-07 16:58:30 -0800 | [diff] [blame] | 1400 | // 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^] | 1401 | if (r.getUserId() == UserHandle.USER_ALL && pkg == null) { |
| Amith Yamasani | 5ec00e9 | 2012-11-07 16:58:30 -0800 | [diff] [blame] | 1402 | continue; |
| 1403 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1404 | if ((r.getFlags() & mustHaveFlags) != mustHaveFlags) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | continue; |
| 1406 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1407 | if ((r.getFlags() & mustNotHaveFlags) != 0) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1408 | continue; |
| 1409 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1410 | if (pkg != null && !r.sbn.pkg.equals(pkg)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1411 | continue; |
| 1412 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1413 | canceledSomething = true; |
| 1414 | if (!doit) { |
| 1415 | return true; |
| 1416 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1417 | mNotificationList.remove(i); |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1418 | cancelNotificationLocked(r, false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1419 | } |
| 1420 | if (canceledSomething) { |
| 1421 | updateLightsLocked(); |
| 1422 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1423 | return canceledSomething; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1424 | } |
| 1425 | } |
| 1426 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1427 | public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1428 | checkCallerIsSystemOrSameApp(pkg); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1429 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| Amith Yamasani | 2c7ebea | 2012-10-30 15:28:27 -0700 | [diff] [blame] | 1430 | Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1431 | // Don't allow client applications to cancel foreground service notis. |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1432 | cancelNotification(pkg, tag, id, 0, |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1433 | Binder.getCallingUid() == Process.SYSTEM_UID |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1434 | ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | } |
| 1436 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1437 | public void cancelAllNotifications(String pkg, int userId) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1438 | checkCallerIsSystemOrSameApp(pkg); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1439 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1440 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| Amith Yamasani | 2c7ebea | 2012-10-30 15:28:27 -0700 | [diff] [blame] | 1441 | Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1442 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1443 | // Calling from user space, don't allow the canceling of actively |
| 1444 | // running foreground services. |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1445 | cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1446 | } |
| 1447 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1448 | void checkCallerIsSystem() { |
| 1449 | int uid = Binder.getCallingUid(); |
| Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 1450 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1451 | return; |
| 1452 | } |
| 1453 | throw new SecurityException("Disallowed call for uid " + uid); |
| 1454 | } |
| 1455 | |
| 1456 | void checkCallerIsSystemOrSameApp(String pkg) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1457 | int uid = Binder.getCallingUid(); |
| Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 1458 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1459 | return; |
| 1460 | } |
| 1461 | try { |
| Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 1462 | ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo( |
| 1463 | pkg, 0, UserHandle.getCallingUserId()); |
| Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1464 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1465 | throw new SecurityException("Calling uid " + uid + " gave package" |
| 1466 | + pkg + " which is owned by uid " + ai.uid); |
| 1467 | } |
| Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 1468 | } catch (RemoteException re) { |
| 1469 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1470 | } |
| 1471 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1472 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1473 | void cancelAll(int userId) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1474 | synchronized (mNotificationList) { |
| 1475 | final int N = mNotificationList.size(); |
| 1476 | for (int i=N-1; i>=0; i--) { |
| 1477 | NotificationRecord r = mNotificationList.get(i); |
| 1478 | |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1479 | if (!notificationMatchesUserId(r, userId)) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1480 | continue; |
| 1481 | } |
| 1482 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1483 | if ((r.getFlags() & (Notification.FLAG_ONGOING_EVENT |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1484 | | Notification.FLAG_NO_CLEAR)) == 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1485 | mNotificationList.remove(i); |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1486 | cancelNotificationLocked(r, true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | updateLightsLocked(); |
| 1491 | } |
| 1492 | } |
| 1493 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1494 | // lock on mNotificationList |
| 1495 | private void updateLightsLocked() |
| 1496 | { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1497 | // handle notification lights |
| 1498 | if (mLedNotification == null) { |
| 1499 | // get next notification, if any |
| 1500 | int n = mLights.size(); |
| 1501 | if (n > 0) { |
| 1502 | mLedNotification = mLights.get(n-1); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1503 | } |
| 1504 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1505 | |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1506 | // Don't flash while we are in a call or screen is on |
| 1507 | if (mLedNotification == null || mInCall || mScreenOn) { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1508 | mNotificationLight.turnOff(); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1509 | } else { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1510 | final Notification ledno = mLedNotification.sbn.notification; |
| 1511 | int ledARGB = ledno.ledARGB; |
| 1512 | int ledOnMS = ledno.ledOnMS; |
| 1513 | int ledOffMS = ledno.ledOffMS; |
| 1514 | if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) { |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 1515 | ledARGB = mDefaultNotificationColor; |
| 1516 | ledOnMS = mDefaultNotificationLedOn; |
| 1517 | ledOffMS = mDefaultNotificationLedOff; |
| 1518 | } |
| 1519 | if (mNotificationPulseEnabled) { |
| 1520 | // pulse repeatedly |
| 1521 | mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED, |
| 1522 | ledOnMS, ledOffMS); |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 1523 | } |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1524 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | // lock on mNotificationList |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1528 | 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] | 1529 | { |
| 1530 | ArrayList<NotificationRecord> list = mNotificationList; |
| 1531 | final int len = list.size(); |
| 1532 | for (int i=0; i<len; i++) { |
| 1533 | NotificationRecord r = list.get(i); |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1534 | if (!notificationMatchesUserId(r, userId) || r.sbn.id != id) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1535 | continue; |
| 1536 | } |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1537 | if (tag == null) { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1538 | if (r.sbn.tag != null) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1539 | continue; |
| 1540 | } |
| 1541 | } else { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1542 | if (!tag.equals(r.sbn.tag)) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1543 | continue; |
| 1544 | } |
| 1545 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame^] | 1546 | if (r.sbn.pkg.equals(pkg)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1547 | return i; |
| 1548 | } |
| 1549 | } |
| 1550 | return -1; |
| 1551 | } |
| 1552 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1553 | private void updateNotificationPulse() { |
| 1554 | synchronized (mNotificationList) { |
| 1555 | updateLightsLocked(); |
| 1556 | } |
| 1557 | } |
| 1558 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1559 | // ====================================================================== |
| 1560 | @Override |
| 1561 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1562 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1563 | != PackageManager.PERMISSION_GRANTED) { |
| 1564 | pw.println("Permission Denial: can't dump NotificationManager from from pid=" |
| 1565 | + Binder.getCallingPid() |
| 1566 | + ", uid=" + Binder.getCallingUid()); |
| 1567 | return; |
| 1568 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1569 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1570 | pw.println("Current Notification Manager state:"); |
| 1571 | |
| 1572 | int N; |
| 1573 | |
| 1574 | synchronized (mToastQueue) { |
| 1575 | N = mToastQueue.size(); |
| 1576 | if (N > 0) { |
| 1577 | pw.println(" Toast Queue:"); |
| 1578 | for (int i=0; i<N; i++) { |
| 1579 | mToastQueue.get(i).dump(pw, " "); |
| 1580 | } |
| 1581 | pw.println(" "); |
| 1582 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1583 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | synchronized (mNotificationList) { |
| 1587 | N = mNotificationList.size(); |
| 1588 | if (N > 0) { |
| 1589 | pw.println(" Notification List:"); |
| 1590 | for (int i=0; i<N; i++) { |
| 1591 | mNotificationList.get(i).dump(pw, " ", mContext); |
| 1592 | } |
| 1593 | pw.println(" "); |
| 1594 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1595 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1596 | N = mLights.size(); |
| 1597 | if (N > 0) { |
| 1598 | pw.println(" Lights List:"); |
| 1599 | for (int i=0; i<N; i++) { |
| 1600 | mLights.get(i).dump(pw, " ", mContext); |
| 1601 | } |
| 1602 | pw.println(" "); |
| 1603 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1604 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1605 | pw.println(" mSoundNotification=" + mSoundNotification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1606 | pw.println(" mVibrateNotification=" + mVibrateNotification); |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1607 | pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications)); |
| 1608 | pw.println(" mSystemReady=" + mSystemReady); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | } |
| 1610 | } |
| 1611 | } |