Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.am; |
| 18 | |
| 19 | import java.io.FileDescriptor; |
| 20 | import java.io.PrintWriter; |
| 21 | import java.util.ArrayList; |
| 22 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 24 | import android.app.AppGlobals; |
| 25 | import android.content.ComponentName; |
| 26 | import android.content.IIntentReceiver; |
| 27 | import android.content.Intent; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 28 | import android.content.pm.ActivityInfo; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.pm.ResolveInfo; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 31 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 32 | import android.os.Bundle; |
| 33 | import android.os.Handler; |
| 34 | import android.os.IBinder; |
| 35 | import android.os.Message; |
| 36 | import android.os.Process; |
| 37 | import android.os.RemoteException; |
| 38 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 39 | import android.os.UserHandle; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 40 | import android.util.EventLog; |
| 41 | import android.util.Slog; |
| 42 | |
| 43 | /** |
| 44 | * BROADCASTS |
| 45 | * |
| 46 | * We keep two broadcast queues and associated bookkeeping, one for those at |
| 47 | * foreground priority, and one for normal (background-priority) broadcasts. |
| 48 | */ |
| 49 | public class BroadcastQueue { |
| 50 | static final String TAG = "BroadcastQueue"; |
| 51 | static final String TAG_MU = ActivityManagerService.TAG_MU; |
| 52 | static final boolean DEBUG_BROADCAST = ActivityManagerService.DEBUG_BROADCAST; |
| 53 | static final boolean DEBUG_BROADCAST_LIGHT = ActivityManagerService.DEBUG_BROADCAST_LIGHT; |
| 54 | static final boolean DEBUG_MU = ActivityManagerService.DEBUG_MU; |
| 55 | |
| 56 | static final int MAX_BROADCAST_HISTORY = 25; |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 57 | static final int MAX_BROADCAST_SUMMARY_HISTORY = 100; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 58 | |
| 59 | final ActivityManagerService mService; |
| 60 | |
| 61 | /** |
| 62 | * Recognizable moniker for this queue |
| 63 | */ |
| 64 | final String mQueueName; |
| 65 | |
| 66 | /** |
| 67 | * Timeout period for this queue's broadcasts |
| 68 | */ |
| 69 | final long mTimeoutPeriod; |
| 70 | |
| 71 | /** |
| 72 | * Lists of all active broadcasts that are to be executed immediately |
| 73 | * (without waiting for another broadcast to finish). Currently this only |
| 74 | * contains broadcasts to registered receivers, to avoid spinning up |
| 75 | * a bunch of processes to execute IntentReceiver components. Background- |
| 76 | * and foreground-priority broadcasts are queued separately. |
| 77 | */ |
| 78 | final ArrayList<BroadcastRecord> mParallelBroadcasts |
| 79 | = new ArrayList<BroadcastRecord>(); |
| 80 | /** |
| 81 | * List of all active broadcasts that are to be executed one at a time. |
| 82 | * The object at the top of the list is the currently activity broadcasts; |
| 83 | * those after it are waiting for the top to finish. As with parallel |
| 84 | * broadcasts, separate background- and foreground-priority queues are |
| 85 | * maintained. |
| 86 | */ |
| 87 | final ArrayList<BroadcastRecord> mOrderedBroadcasts |
| 88 | = new ArrayList<BroadcastRecord>(); |
| 89 | |
| 90 | /** |
| 91 | * Historical data of past broadcasts, for debugging. |
| 92 | */ |
| 93 | final BroadcastRecord[] mBroadcastHistory |
| 94 | = new BroadcastRecord[MAX_BROADCAST_HISTORY]; |
| 95 | |
| 96 | /** |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 97 | * Summary of historical data of past broadcasts, for debugging. |
| 98 | */ |
| 99 | final Intent[] mBroadcastSummaryHistory |
| 100 | = new Intent[MAX_BROADCAST_SUMMARY_HISTORY]; |
| 101 | |
| 102 | /** |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 103 | * Set when we current have a BROADCAST_INTENT_MSG in flight. |
| 104 | */ |
| 105 | boolean mBroadcastsScheduled = false; |
| 106 | |
| 107 | /** |
| 108 | * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler. |
| 109 | */ |
| 110 | boolean mPendingBroadcastTimeoutMessage; |
| 111 | |
| 112 | /** |
| 113 | * Intent broadcasts that we have tried to start, but are |
| 114 | * waiting for the application's process to be created. We only |
| 115 | * need one per scheduling class (instead of a list) because we always |
| 116 | * process broadcasts one at a time, so no others can be started while |
| 117 | * waiting for this one. |
| 118 | */ |
| 119 | BroadcastRecord mPendingBroadcast = null; |
| 120 | |
| 121 | /** |
| 122 | * The receiver index that is pending, to restart the broadcast if needed. |
| 123 | */ |
| 124 | int mPendingBroadcastRecvIndex; |
| 125 | |
| 126 | static final int BROADCAST_INTENT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG; |
| 127 | static final int BROADCAST_TIMEOUT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG + 1; |
| 128 | |
| 129 | final Handler mHandler = new Handler() { |
| 130 | //public Handler() { |
| 131 | // if (localLOGV) Slog.v(TAG, "Handler started!"); |
| 132 | //} |
| 133 | |
| 134 | public void handleMessage(Message msg) { |
| 135 | switch (msg.what) { |
| 136 | case BROADCAST_INTENT_MSG: { |
| 137 | if (DEBUG_BROADCAST) Slog.v( |
| 138 | TAG, "Received BROADCAST_INTENT_MSG"); |
| 139 | processNextBroadcast(true); |
| 140 | } break; |
| 141 | case BROADCAST_TIMEOUT_MSG: { |
| 142 | synchronized (mService) { |
| 143 | broadcastTimeoutLocked(true); |
| 144 | } |
| 145 | } break; |
| 146 | } |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | private final class AppNotResponding implements Runnable { |
| 151 | private final ProcessRecord mApp; |
| 152 | private final String mAnnotation; |
| 153 | |
| 154 | public AppNotResponding(ProcessRecord app, String annotation) { |
| 155 | mApp = app; |
| 156 | mAnnotation = annotation; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void run() { |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 161 | mService.appNotResponding(mApp, null, null, false, mAnnotation); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | BroadcastQueue(ActivityManagerService service, String name, long timeoutPeriod) { |
| 166 | mService = service; |
| 167 | mQueueName = name; |
| 168 | mTimeoutPeriod = timeoutPeriod; |
| 169 | } |
| 170 | |
| 171 | public boolean isPendingBroadcastProcessLocked(int pid) { |
| 172 | return mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid; |
| 173 | } |
| 174 | |
| 175 | public void enqueueParallelBroadcastLocked(BroadcastRecord r) { |
| 176 | mParallelBroadcasts.add(r); |
| 177 | } |
| 178 | |
| 179 | public void enqueueOrderedBroadcastLocked(BroadcastRecord r) { |
| 180 | mOrderedBroadcasts.add(r); |
| 181 | } |
| 182 | |
| 183 | public final boolean replaceParallelBroadcastLocked(BroadcastRecord r) { |
| 184 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
| 185 | if (r.intent.filterEquals(mParallelBroadcasts.get(i).intent)) { |
| 186 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 187 | "***** DROPPING PARALLEL [" |
| 188 | + mQueueName + "]: " + r.intent); |
| 189 | mParallelBroadcasts.set(i, r); |
| 190 | return true; |
| 191 | } |
| 192 | } |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | public final boolean replaceOrderedBroadcastLocked(BroadcastRecord r) { |
| 197 | for (int i=mOrderedBroadcasts.size()-1; i>0; i--) { |
| 198 | if (r.intent.filterEquals(mOrderedBroadcasts.get(i).intent)) { |
| 199 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 200 | "***** DROPPING ORDERED [" |
| 201 | + mQueueName + "]: " + r.intent); |
| 202 | mOrderedBroadcasts.set(i, r); |
| 203 | return true; |
| 204 | } |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | private final void processCurBroadcastLocked(BroadcastRecord r, |
| 210 | ProcessRecord app) throws RemoteException { |
| 211 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 212 | "Process cur broadcast " + r + " for app " + app); |
| 213 | if (app.thread == null) { |
| 214 | throw new RemoteException(); |
| 215 | } |
| 216 | r.receiver = app.thread.asBinder(); |
| 217 | r.curApp = app; |
| 218 | app.curReceiver = r; |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 219 | mService.updateLruProcessLocked(app, true); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 220 | |
| 221 | // Tell the application to launch this receiver. |
| 222 | r.intent.setComponent(r.curComponent); |
| 223 | |
| 224 | boolean started = false; |
| 225 | try { |
| 226 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, |
| 227 | "Delivering to component " + r.curComponent |
| 228 | + ": " + r); |
| 229 | mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName()); |
| 230 | app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver, |
| 231 | mService.compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo), |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 232 | r.resultCode, r.resultData, r.resultExtras, r.ordered, r.userId); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 233 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 234 | "Process cur broadcast " + r + " DELIVERED for app " + app); |
| 235 | started = true; |
| 236 | } finally { |
| 237 | if (!started) { |
| 238 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 239 | "Process cur broadcast " + r + ": NOT STARTED!"); |
| 240 | r.receiver = null; |
| 241 | r.curApp = null; |
| 242 | app.curReceiver = null; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | public boolean sendPendingBroadcastsLocked(ProcessRecord app) { |
| 248 | boolean didSomething = false; |
| 249 | final BroadcastRecord br = mPendingBroadcast; |
| 250 | if (br != null && br.curApp.pid == app.pid) { |
| 251 | try { |
| 252 | mPendingBroadcast = null; |
| 253 | processCurBroadcastLocked(br, app); |
| 254 | didSomething = true; |
| 255 | } catch (Exception e) { |
| 256 | Slog.w(TAG, "Exception in new application when starting receiver " |
| 257 | + br.curComponent.flattenToShortString(), e); |
| 258 | logBroadcastReceiverDiscardLocked(br); |
| 259 | finishReceiverLocked(br, br.resultCode, br.resultData, |
| 260 | br.resultExtras, br.resultAbort, true); |
| 261 | scheduleBroadcastsLocked(); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 262 | // We need to reset the state if we failed to start the receiver. |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 263 | br.state = BroadcastRecord.IDLE; |
| 264 | throw new RuntimeException(e.getMessage()); |
| 265 | } |
| 266 | } |
| 267 | return didSomething; |
| 268 | } |
| 269 | |
| 270 | public void skipPendingBroadcastLocked(int pid) { |
| 271 | final BroadcastRecord br = mPendingBroadcast; |
| 272 | if (br != null && br.curApp.pid == pid) { |
| 273 | br.state = BroadcastRecord.IDLE; |
| 274 | br.nextReceiver = mPendingBroadcastRecvIndex; |
| 275 | mPendingBroadcast = null; |
| 276 | scheduleBroadcastsLocked(); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | public void skipCurrentReceiverLocked(ProcessRecord app) { |
| 281 | boolean reschedule = false; |
| 282 | BroadcastRecord r = app.curReceiver; |
| 283 | if (r != null) { |
| 284 | // The current broadcast is waiting for this app's receiver |
| 285 | // to be finished. Looks like that's not going to happen, so |
| 286 | // let the broadcast continue. |
| 287 | logBroadcastReceiverDiscardLocked(r); |
| 288 | finishReceiverLocked(r, r.resultCode, r.resultData, |
| 289 | r.resultExtras, r.resultAbort, true); |
| 290 | reschedule = true; |
| 291 | } |
| 292 | |
| 293 | r = mPendingBroadcast; |
| 294 | if (r != null && r.curApp == app) { |
| 295 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 296 | "[" + mQueueName + "] skip & discard pending app " + r); |
| 297 | logBroadcastReceiverDiscardLocked(r); |
| 298 | finishReceiverLocked(r, r.resultCode, r.resultData, |
| 299 | r.resultExtras, r.resultAbort, true); |
| 300 | reschedule = true; |
| 301 | } |
| 302 | if (reschedule) { |
| 303 | scheduleBroadcastsLocked(); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | public void scheduleBroadcastsLocked() { |
| 308 | if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts [" |
| 309 | + mQueueName + "]: current=" |
| 310 | + mBroadcastsScheduled); |
| 311 | |
| 312 | if (mBroadcastsScheduled) { |
| 313 | return; |
| 314 | } |
| 315 | mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this)); |
| 316 | mBroadcastsScheduled = true; |
| 317 | } |
| 318 | |
| 319 | public BroadcastRecord getMatchingOrderedReceiver(IBinder receiver) { |
| 320 | if (mOrderedBroadcasts.size() > 0) { |
| 321 | final BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 322 | if (r != null && r.receiver == receiver) { |
| 323 | return r; |
| 324 | } |
| 325 | } |
| 326 | return null; |
| 327 | } |
| 328 | |
| 329 | public boolean finishReceiverLocked(BroadcastRecord r, int resultCode, |
| 330 | String resultData, Bundle resultExtras, boolean resultAbort, |
| 331 | boolean explicit) { |
| 332 | int state = r.state; |
| 333 | r.state = BroadcastRecord.IDLE; |
| 334 | if (state == BroadcastRecord.IDLE) { |
| 335 | if (explicit) { |
| 336 | Slog.w(TAG, "finishReceiver [" + mQueueName + "] called but state is IDLE"); |
| 337 | } |
| 338 | } |
| 339 | r.receiver = null; |
| 340 | r.intent.setComponent(null); |
| 341 | if (r.curApp != null) { |
| 342 | r.curApp.curReceiver = null; |
| 343 | } |
| 344 | if (r.curFilter != null) { |
| 345 | r.curFilter.receiverList.curBroadcast = null; |
| 346 | } |
| 347 | r.curFilter = null; |
| 348 | r.curApp = null; |
| 349 | r.curComponent = null; |
| 350 | r.curReceiver = null; |
| 351 | mPendingBroadcast = null; |
| 352 | |
| 353 | r.resultCode = resultCode; |
| 354 | r.resultData = resultData; |
| 355 | r.resultExtras = resultExtras; |
| 356 | r.resultAbort = resultAbort; |
| 357 | |
| 358 | // We will process the next receiver right now if this is finishing |
| 359 | // an app receiver (which is always asynchronous) or after we have |
| 360 | // come back from calling a receiver. |
| 361 | return state == BroadcastRecord.APP_RECEIVE |
| 362 | || state == BroadcastRecord.CALL_DONE_RECEIVE; |
| 363 | } |
| 364 | |
| 365 | private static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver, |
| 366 | Intent intent, int resultCode, String data, Bundle extras, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 367 | boolean ordered, boolean sticky, int sendingUser) throws RemoteException { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 368 | // Send the intent to the receiver asynchronously using one-way binder calls. |
| 369 | if (app != null && app.thread != null) { |
| 370 | // If we have an app thread, do the call through that so it is |
| 371 | // correctly ordered with other one-way calls. |
| 372 | app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 373 | data, extras, ordered, sticky, sendingUser); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 374 | } else { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 375 | receiver.performReceive(intent, resultCode, data, extras, ordered, |
| 376 | sticky, sendingUser); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | private final void deliverToRegisteredReceiverLocked(BroadcastRecord r, |
| 381 | BroadcastFilter filter, boolean ordered) { |
| 382 | boolean skip = false; |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 383 | if (filter.requiredPermission != null) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 384 | int perm = mService.checkComponentPermission(filter.requiredPermission, |
| 385 | r.callingPid, r.callingUid, -1, true); |
| 386 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 387 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 388 | + r.intent.toString() |
| 389 | + " from " + r.callerPackage + " (pid=" |
| 390 | + r.callingPid + ", uid=" + r.callingUid + ")" |
| 391 | + " requires " + filter.requiredPermission |
| 392 | + " due to registered receiver " + filter); |
| 393 | skip = true; |
| 394 | } |
| 395 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 396 | if (!skip && r.requiredPermission != null) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 397 | int perm = mService.checkComponentPermission(r.requiredPermission, |
| 398 | filter.receiverList.pid, filter.receiverList.uid, -1, true); |
| 399 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 400 | Slog.w(TAG, "Permission Denial: receiving " |
| 401 | + r.intent.toString() |
| 402 | + " to " + filter.receiverList.app |
| 403 | + " (pid=" + filter.receiverList.pid |
| 404 | + ", uid=" + filter.receiverList.uid + ")" |
| 405 | + " requires " + r.requiredPermission |
| 406 | + " due to sender " + r.callerPackage |
| 407 | + " (uid " + r.callingUid + ")"); |
| 408 | skip = true; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | if (!skip) { |
| 413 | // If this is not being sent as an ordered broadcast, then we |
| 414 | // don't want to touch the fields that keep track of the current |
| 415 | // state of ordered broadcasts. |
| 416 | if (ordered) { |
| 417 | r.receiver = filter.receiverList.receiver.asBinder(); |
| 418 | r.curFilter = filter; |
| 419 | filter.receiverList.curBroadcast = r; |
| 420 | r.state = BroadcastRecord.CALL_IN_RECEIVE; |
| 421 | if (filter.receiverList.app != null) { |
| 422 | // Bump hosting application to no longer be in background |
| 423 | // scheduling class. Note that we can't do that if there |
| 424 | // isn't an app... but we can only be in that case for |
| 425 | // things that directly call the IActivityManager API, which |
| 426 | // are already core system stuff so don't matter for this. |
| 427 | r.curApp = filter.receiverList.app; |
| 428 | filter.receiverList.app.curReceiver = r; |
| 429 | mService.updateOomAdjLocked(); |
| 430 | } |
| 431 | } |
| 432 | try { |
| 433 | if (DEBUG_BROADCAST_LIGHT) { |
| 434 | int seq = r.intent.getIntExtra("seq", -1); |
| 435 | Slog.i(TAG, "Delivering to " + filter |
| 436 | + " (seq=" + seq + "): " + r); |
| 437 | } |
| 438 | performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 439 | new Intent(r.intent), r.resultCode, r.resultData, |
| 440 | r.resultExtras, r.ordered, r.initialSticky, r.userId); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 441 | if (ordered) { |
| 442 | r.state = BroadcastRecord.CALL_DONE_RECEIVE; |
| 443 | } |
| 444 | } catch (RemoteException e) { |
| 445 | Slog.w(TAG, "Failure sending broadcast " + r.intent, e); |
| 446 | if (ordered) { |
| 447 | r.receiver = null; |
| 448 | r.curFilter = null; |
| 449 | filter.receiverList.curBroadcast = null; |
| 450 | if (filter.receiverList.app != null) { |
| 451 | filter.receiverList.app.curReceiver = null; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | final void processNextBroadcast(boolean fromMsg) { |
| 459 | synchronized(mService) { |
| 460 | BroadcastRecord r; |
| 461 | |
| 462 | if (DEBUG_BROADCAST) Slog.v(TAG, "processNextBroadcast [" |
| 463 | + mQueueName + "]: " |
| 464 | + mParallelBroadcasts.size() + " broadcasts, " |
| 465 | + mOrderedBroadcasts.size() + " ordered broadcasts"); |
| 466 | |
| 467 | mService.updateCpuStats(); |
| 468 | |
| 469 | if (fromMsg) { |
| 470 | mBroadcastsScheduled = false; |
| 471 | } |
| 472 | |
| 473 | // First, deliver any non-serialized broadcasts right away. |
| 474 | while (mParallelBroadcasts.size() > 0) { |
| 475 | r = mParallelBroadcasts.remove(0); |
| 476 | r.dispatchTime = SystemClock.uptimeMillis(); |
| 477 | r.dispatchClockTime = System.currentTimeMillis(); |
| 478 | final int N = r.receivers.size(); |
| 479 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast [" |
| 480 | + mQueueName + "] " + r); |
| 481 | for (int i=0; i<N; i++) { |
| 482 | Object target = r.receivers.get(i); |
| 483 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 484 | "Delivering non-ordered on [" + mQueueName + "] to registered " |
| 485 | + target + ": " + r); |
| 486 | deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false); |
| 487 | } |
| 488 | addBroadcastToHistoryLocked(r); |
| 489 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast [" |
| 490 | + mQueueName + "] " + r); |
| 491 | } |
| 492 | |
| 493 | // Now take care of the next serialized one... |
| 494 | |
| 495 | // If we are waiting for a process to come up to handle the next |
| 496 | // broadcast, then do nothing at this point. Just in case, we |
| 497 | // check that the process we're waiting for still exists. |
| 498 | if (mPendingBroadcast != null) { |
| 499 | if (DEBUG_BROADCAST_LIGHT) { |
| 500 | Slog.v(TAG, "processNextBroadcast [" |
| 501 | + mQueueName + "]: waiting for " |
| 502 | + mPendingBroadcast.curApp); |
| 503 | } |
| 504 | |
| 505 | boolean isDead; |
| 506 | synchronized (mService.mPidsSelfLocked) { |
| 507 | isDead = (mService.mPidsSelfLocked.get( |
| 508 | mPendingBroadcast.curApp.pid) == null); |
| 509 | } |
| 510 | if (!isDead) { |
| 511 | // It's still alive, so keep waiting |
| 512 | return; |
| 513 | } else { |
| 514 | Slog.w(TAG, "pending app [" |
| 515 | + mQueueName + "]" + mPendingBroadcast.curApp |
| 516 | + " died before responding to broadcast"); |
| 517 | mPendingBroadcast.state = BroadcastRecord.IDLE; |
| 518 | mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; |
| 519 | mPendingBroadcast = null; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | boolean looped = false; |
| 524 | |
| 525 | do { |
| 526 | if (mOrderedBroadcasts.size() == 0) { |
| 527 | // No more broadcasts pending, so all done! |
| 528 | mService.scheduleAppGcsLocked(); |
| 529 | if (looped) { |
| 530 | // If we had finished the last ordered broadcast, then |
| 531 | // make sure all processes have correct oom and sched |
| 532 | // adjustments. |
| 533 | mService.updateOomAdjLocked(); |
| 534 | } |
| 535 | return; |
| 536 | } |
| 537 | r = mOrderedBroadcasts.get(0); |
| 538 | boolean forceReceive = false; |
| 539 | |
| 540 | // Ensure that even if something goes awry with the timeout |
| 541 | // detection, we catch "hung" broadcasts here, discard them, |
| 542 | // and continue to make progress. |
| 543 | // |
| 544 | // This is only done if the system is ready so that PRE_BOOT_COMPLETED |
| 545 | // receivers don't get executed with timeouts. They're intended for |
| 546 | // one time heavy lifting after system upgrades and can take |
| 547 | // significant amounts of time. |
| 548 | int numReceivers = (r.receivers != null) ? r.receivers.size() : 0; |
| 549 | if (mService.mProcessesReady && r.dispatchTime > 0) { |
| 550 | long now = SystemClock.uptimeMillis(); |
| 551 | if ((numReceivers > 0) && |
| 552 | (now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) { |
| 553 | Slog.w(TAG, "Hung broadcast [" |
| 554 | + mQueueName + "] discarded after timeout failure:" |
| 555 | + " now=" + now |
| 556 | + " dispatchTime=" + r.dispatchTime |
| 557 | + " startTime=" + r.receiverTime |
| 558 | + " intent=" + r.intent |
| 559 | + " numReceivers=" + numReceivers |
| 560 | + " nextReceiver=" + r.nextReceiver |
| 561 | + " state=" + r.state); |
| 562 | broadcastTimeoutLocked(false); // forcibly finish this broadcast |
| 563 | forceReceive = true; |
| 564 | r.state = BroadcastRecord.IDLE; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | if (r.state != BroadcastRecord.IDLE) { |
| 569 | if (DEBUG_BROADCAST) Slog.d(TAG, |
| 570 | "processNextBroadcast(" |
| 571 | + mQueueName + ") called when not idle (state=" |
| 572 | + r.state + ")"); |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | if (r.receivers == null || r.nextReceiver >= numReceivers |
| 577 | || r.resultAbort || forceReceive) { |
| 578 | // No more receivers for this broadcast! Send the final |
| 579 | // result if requested... |
| 580 | if (r.resultTo != null) { |
| 581 | try { |
| 582 | if (DEBUG_BROADCAST) { |
| 583 | int seq = r.intent.getIntExtra("seq", -1); |
| 584 | Slog.i(TAG, "Finishing broadcast [" |
| 585 | + mQueueName + "] " + r.intent.getAction() |
| 586 | + " seq=" + seq + " app=" + r.callerApp); |
| 587 | } |
| 588 | performReceiveLocked(r.callerApp, r.resultTo, |
| 589 | new Intent(r.intent), r.resultCode, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 590 | r.resultData, r.resultExtras, false, false, r.userId); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 591 | // Set this to null so that the reference |
| 592 | // (local and remote) isnt kept in the mBroadcastHistory. |
| 593 | r.resultTo = null; |
| 594 | } catch (RemoteException e) { |
| 595 | Slog.w(TAG, "Failure [" |
| 596 | + mQueueName + "] sending broadcast result of " |
| 597 | + r.intent, e); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG"); |
| 602 | cancelBroadcastTimeoutLocked(); |
| 603 | |
| 604 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast " |
| 605 | + r); |
| 606 | |
| 607 | // ... and on to the next... |
| 608 | addBroadcastToHistoryLocked(r); |
| 609 | mOrderedBroadcasts.remove(0); |
| 610 | r = null; |
| 611 | looped = true; |
| 612 | continue; |
| 613 | } |
| 614 | } while (r == null); |
| 615 | |
| 616 | // Get the next receiver... |
| 617 | int recIdx = r.nextReceiver++; |
| 618 | |
| 619 | // Keep track of when this receiver started, and make sure there |
| 620 | // is a timeout message pending to kill it if need be. |
| 621 | r.receiverTime = SystemClock.uptimeMillis(); |
| 622 | if (recIdx == 0) { |
| 623 | r.dispatchTime = r.receiverTime; |
| 624 | r.dispatchClockTime = System.currentTimeMillis(); |
| 625 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast [" |
| 626 | + mQueueName + "] " + r); |
| 627 | } |
| 628 | if (! mPendingBroadcastTimeoutMessage) { |
| 629 | long timeoutTime = r.receiverTime + mTimeoutPeriod; |
| 630 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 631 | "Submitting BROADCAST_TIMEOUT_MSG [" |
| 632 | + mQueueName + "] for " + r + " at " + timeoutTime); |
| 633 | setBroadcastTimeoutLocked(timeoutTime); |
| 634 | } |
| 635 | |
| 636 | Object nextReceiver = r.receivers.get(recIdx); |
| 637 | if (nextReceiver instanceof BroadcastFilter) { |
| 638 | // Simple case: this is a registered receiver who gets |
| 639 | // a direct call. |
| 640 | BroadcastFilter filter = (BroadcastFilter)nextReceiver; |
| 641 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 642 | "Delivering ordered [" |
| 643 | + mQueueName + "] to registered " |
| 644 | + filter + ": " + r); |
| 645 | deliverToRegisteredReceiverLocked(r, filter, r.ordered); |
| 646 | if (r.receiver == null || !r.ordered) { |
| 647 | // The receiver has already finished, so schedule to |
| 648 | // process the next one. |
| 649 | if (DEBUG_BROADCAST) Slog.v(TAG, "Quick finishing [" |
| 650 | + mQueueName + "]: ordered=" |
| 651 | + r.ordered + " receiver=" + r.receiver); |
| 652 | r.state = BroadcastRecord.IDLE; |
| 653 | scheduleBroadcastsLocked(); |
| 654 | } |
| 655 | return; |
| 656 | } |
| 657 | |
| 658 | // Hard case: need to instantiate the receiver, possibly |
| 659 | // starting its application process to host it. |
| 660 | |
| 661 | ResolveInfo info = |
| 662 | (ResolveInfo)nextReceiver; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 663 | ComponentName component = new ComponentName( |
| 664 | info.activityInfo.applicationInfo.packageName, |
| 665 | info.activityInfo.name); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 666 | |
| 667 | boolean skip = false; |
| 668 | int perm = mService.checkComponentPermission(info.activityInfo.permission, |
| 669 | r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid, |
| 670 | info.activityInfo.exported); |
| 671 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 672 | if (!info.activityInfo.exported) { |
| 673 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 674 | + r.intent.toString() |
| 675 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 676 | + ", uid=" + r.callingUid + ")" |
| 677 | + " is not exported from uid " + info.activityInfo.applicationInfo.uid |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 678 | + " due to receiver " + component.flattenToShortString()); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 679 | } else { |
| 680 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 681 | + r.intent.toString() |
| 682 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 683 | + ", uid=" + r.callingUid + ")" |
| 684 | + " requires " + info.activityInfo.permission |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 685 | + " due to receiver " + component.flattenToShortString()); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 686 | } |
| 687 | skip = true; |
| 688 | } |
| 689 | if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID && |
| 690 | r.requiredPermission != null) { |
| 691 | try { |
| 692 | perm = AppGlobals.getPackageManager(). |
| 693 | checkPermission(r.requiredPermission, |
| 694 | info.activityInfo.applicationInfo.packageName); |
| 695 | } catch (RemoteException e) { |
| 696 | perm = PackageManager.PERMISSION_DENIED; |
| 697 | } |
| 698 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 699 | Slog.w(TAG, "Permission Denial: receiving " |
| 700 | + r.intent + " to " |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 701 | + component.flattenToShortString() |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 702 | + " requires " + r.requiredPermission |
| 703 | + " due to sender " + r.callerPackage |
| 704 | + " (uid " + r.callingUid + ")"); |
| 705 | skip = true; |
| 706 | } |
| 707 | } |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 708 | boolean isSingleton = false; |
| 709 | try { |
| 710 | isSingleton = mService.isSingleton(info.activityInfo.processName, |
| 711 | info.activityInfo.applicationInfo, |
| 712 | info.activityInfo.name, info.activityInfo.flags); |
| 713 | } catch (SecurityException e) { |
| 714 | Slog.w(TAG, e.getMessage()); |
| 715 | skip = true; |
| 716 | } |
| 717 | if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 718 | if (ActivityManager.checkUidPermission( |
| 719 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 720 | info.activityInfo.applicationInfo.uid) |
| 721 | != PackageManager.PERMISSION_GRANTED) { |
| 722 | Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString() |
| 723 | + " requests FLAG_SINGLE_USER, but app does not hold " |
| 724 | + android.Manifest.permission.INTERACT_ACROSS_USERS); |
| 725 | skip = true; |
| 726 | } |
| 727 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 728 | if (r.curApp != null && r.curApp.crashing) { |
| 729 | // If the target process is crashing, just skip it. |
| 730 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 731 | "Skipping deliver ordered [" |
| 732 | + mQueueName + "] " + r + " to " + r.curApp |
| 733 | + ": process crashing"); |
| 734 | skip = true; |
| 735 | } |
| 736 | |
| 737 | if (skip) { |
| 738 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 739 | "Skipping delivery of ordered [" |
| 740 | + mQueueName + "] " + r + " for whatever reason"); |
| 741 | r.receiver = null; |
| 742 | r.curFilter = null; |
| 743 | r.state = BroadcastRecord.IDLE; |
| 744 | scheduleBroadcastsLocked(); |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | r.state = BroadcastRecord.APP_RECEIVE; |
| 749 | String targetProcess = info.activityInfo.processName; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 750 | r.curComponent = component; |
| 751 | if (r.callingUid != Process.SYSTEM_UID && isSingleton) { |
| 752 | info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 753 | } |
| 754 | r.curReceiver = info.activityInfo; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 755 | if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 756 | Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, " |
| 757 | + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = " |
| 758 | + info.activityInfo.applicationInfo.uid); |
| 759 | } |
| 760 | |
| 761 | // Broadcast is being executed, its package can't be stopped. |
| 762 | try { |
| 763 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 764 | r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid)); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 765 | } catch (RemoteException e) { |
| 766 | } catch (IllegalArgumentException e) { |
| 767 | Slog.w(TAG, "Failed trying to unstop package " |
| 768 | + r.curComponent.getPackageName() + ": " + e); |
| 769 | } |
| 770 | |
| 771 | // Is this receiver's application already running? |
| 772 | ProcessRecord app = mService.getProcessRecordLocked(targetProcess, |
| 773 | info.activityInfo.applicationInfo.uid); |
| 774 | if (app != null && app.thread != null) { |
| 775 | try { |
| 776 | app.addPackage(info.activityInfo.packageName); |
| 777 | processCurBroadcastLocked(r, app); |
| 778 | return; |
| 779 | } catch (RemoteException e) { |
| 780 | Slog.w(TAG, "Exception when sending broadcast to " |
| 781 | + r.curComponent, e); |
| 782 | } |
| 783 | |
| 784 | // If a dead object exception was thrown -- fall through to |
| 785 | // restart the application. |
| 786 | } |
| 787 | |
| 788 | // Not running -- get it started, to be executed when the app comes up. |
| 789 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 790 | "Need to start app [" |
| 791 | + mQueueName + "] " + targetProcess + " for broadcast " + r); |
| 792 | if ((r.curApp=mService.startProcessLocked(targetProcess, |
| 793 | info.activityInfo.applicationInfo, true, |
| 794 | r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND, |
| 795 | "broadcast", r.curComponent, |
| 796 | (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false)) |
| 797 | == null) { |
| 798 | // Ah, this recipient is unavailable. Finish it if necessary, |
| 799 | // and mark the broadcast record as ready for the next. |
| 800 | Slog.w(TAG, "Unable to launch app " |
| 801 | + info.activityInfo.applicationInfo.packageName + "/" |
| 802 | + info.activityInfo.applicationInfo.uid + " for broadcast " |
| 803 | + r.intent + ": process is bad"); |
| 804 | logBroadcastReceiverDiscardLocked(r); |
| 805 | finishReceiverLocked(r, r.resultCode, r.resultData, |
| 806 | r.resultExtras, r.resultAbort, true); |
| 807 | scheduleBroadcastsLocked(); |
| 808 | r.state = BroadcastRecord.IDLE; |
| 809 | return; |
| 810 | } |
| 811 | |
| 812 | mPendingBroadcast = r; |
| 813 | mPendingBroadcastRecvIndex = recIdx; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | final void setBroadcastTimeoutLocked(long timeoutTime) { |
| 818 | if (! mPendingBroadcastTimeoutMessage) { |
| 819 | Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this); |
| 820 | mHandler.sendMessageAtTime(msg, timeoutTime); |
| 821 | mPendingBroadcastTimeoutMessage = true; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | final void cancelBroadcastTimeoutLocked() { |
| 826 | if (mPendingBroadcastTimeoutMessage) { |
| 827 | mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this); |
| 828 | mPendingBroadcastTimeoutMessage = false; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | final void broadcastTimeoutLocked(boolean fromMsg) { |
| 833 | if (fromMsg) { |
| 834 | mPendingBroadcastTimeoutMessage = false; |
| 835 | } |
| 836 | |
| 837 | if (mOrderedBroadcasts.size() == 0) { |
| 838 | return; |
| 839 | } |
| 840 | |
| 841 | long now = SystemClock.uptimeMillis(); |
| 842 | BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 843 | if (fromMsg) { |
| 844 | if (mService.mDidDexOpt) { |
| 845 | // Delay timeouts until dexopt finishes. |
| 846 | mService.mDidDexOpt = false; |
| 847 | long timeoutTime = SystemClock.uptimeMillis() + mTimeoutPeriod; |
| 848 | setBroadcastTimeoutLocked(timeoutTime); |
| 849 | return; |
| 850 | } |
| 851 | if (!mService.mProcessesReady) { |
| 852 | // Only process broadcast timeouts if the system is ready. That way |
| 853 | // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended |
| 854 | // to do heavy lifting for system up. |
| 855 | return; |
| 856 | } |
| 857 | |
| 858 | long timeoutTime = r.receiverTime + mTimeoutPeriod; |
| 859 | if (timeoutTime > now) { |
| 860 | // We can observe premature timeouts because we do not cancel and reset the |
| 861 | // broadcast timeout message after each receiver finishes. Instead, we set up |
| 862 | // an initial timeout then kick it down the road a little further as needed |
| 863 | // when it expires. |
| 864 | if (DEBUG_BROADCAST) Slog.v(TAG, |
| 865 | "Premature timeout [" |
| 866 | + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for " |
| 867 | + timeoutTime); |
| 868 | setBroadcastTimeoutLocked(timeoutTime); |
| 869 | return; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r.receiver |
| 874 | + ", started " + (now - r.receiverTime) + "ms ago"); |
| 875 | r.receiverTime = now; |
| 876 | r.anrCount++; |
| 877 | |
| 878 | // Current receiver has passed its expiration date. |
| 879 | if (r.nextReceiver <= 0) { |
| 880 | Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0"); |
| 881 | return; |
| 882 | } |
| 883 | |
| 884 | ProcessRecord app = null; |
| 885 | String anrMessage = null; |
| 886 | |
| 887 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 888 | Slog.w(TAG, "Receiver during timeout: " + curReceiver); |
| 889 | logBroadcastReceiverDiscardLocked(r); |
| 890 | if (curReceiver instanceof BroadcastFilter) { |
| 891 | BroadcastFilter bf = (BroadcastFilter)curReceiver; |
| 892 | if (bf.receiverList.pid != 0 |
| 893 | && bf.receiverList.pid != ActivityManagerService.MY_PID) { |
| 894 | synchronized (mService.mPidsSelfLocked) { |
| 895 | app = mService.mPidsSelfLocked.get( |
| 896 | bf.receiverList.pid); |
| 897 | } |
| 898 | } |
| 899 | } else { |
| 900 | app = r.curApp; |
| 901 | } |
| 902 | |
| 903 | if (app != null) { |
| 904 | anrMessage = "Broadcast of " + r.intent.toString(); |
| 905 | } |
| 906 | |
| 907 | if (mPendingBroadcast == r) { |
| 908 | mPendingBroadcast = null; |
| 909 | } |
| 910 | |
| 911 | // Move on to the next receiver. |
| 912 | finishReceiverLocked(r, r.resultCode, r.resultData, |
| 913 | r.resultExtras, r.resultAbort, true); |
| 914 | scheduleBroadcastsLocked(); |
| 915 | |
| 916 | if (anrMessage != null) { |
| 917 | // Post the ANR to the handler since we do not want to process ANRs while |
| 918 | // potentially holding our lock. |
| 919 | mHandler.post(new AppNotResponding(app, anrMessage)); |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | private final void addBroadcastToHistoryLocked(BroadcastRecord r) { |
| 924 | if (r.callingUid < 0) { |
| 925 | // This was from a registerReceiver() call; ignore it. |
| 926 | return; |
| 927 | } |
| 928 | System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1, |
| 929 | MAX_BROADCAST_HISTORY-1); |
| 930 | r.finishTime = SystemClock.uptimeMillis(); |
| 931 | mBroadcastHistory[0] = r; |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 932 | System.arraycopy(mBroadcastSummaryHistory, 0, mBroadcastSummaryHistory, 1, |
| 933 | MAX_BROADCAST_SUMMARY_HISTORY-1); |
| 934 | mBroadcastSummaryHistory[0] = r.intent; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) { |
| 938 | if (r.nextReceiver > 0) { |
| 939 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 940 | if (curReceiver instanceof BroadcastFilter) { |
| 941 | BroadcastFilter bf = (BroadcastFilter) curReceiver; |
| 942 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 943 | bf.owningUserId, System.identityHashCode(r), |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 944 | r.intent.getAction(), |
| 945 | r.nextReceiver - 1, |
| 946 | System.identityHashCode(bf)); |
| 947 | } else { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 948 | ResolveInfo ri = (ResolveInfo)curReceiver; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 949 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 950 | UserHandle.getUserId(ri.activityInfo.applicationInfo.uid), |
| 951 | System.identityHashCode(r), r.intent.getAction(), |
| 952 | r.nextReceiver - 1, ri.toString()); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 953 | } |
| 954 | } else { |
| 955 | Slog.w(TAG, "Discarding broadcast before first receiver is invoked: " |
| 956 | + r); |
| 957 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 958 | -1, System.identityHashCode(r), |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 959 | r.intent.getAction(), |
| 960 | r.nextReceiver, |
| 961 | "NONE"); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 966 | int opti, boolean dumpAll, String dumpPackage, boolean needSep) { |
| 967 | if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0 |
| 968 | || mPendingBroadcast != null) { |
| 969 | boolean printed = false; |
| 970 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
| 971 | BroadcastRecord br = mParallelBroadcasts.get(i); |
| 972 | if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) { |
| 973 | continue; |
| 974 | } |
| 975 | if (!printed) { |
| 976 | if (needSep) { |
| 977 | pw.println(); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 978 | } |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 979 | needSep = true; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 980 | printed = true; |
| 981 | pw.println(" Active broadcasts [" + mQueueName + "]:"); |
| 982 | } |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 983 | pw.println(" Active Broadcast " + mQueueName + " #" + i + ":"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 984 | br.dump(pw, " "); |
| 985 | } |
| 986 | printed = false; |
| 987 | needSep = true; |
| 988 | for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) { |
| 989 | BroadcastRecord br = mOrderedBroadcasts.get(i); |
| 990 | if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) { |
| 991 | continue; |
| 992 | } |
| 993 | if (!printed) { |
| 994 | if (needSep) { |
| 995 | pw.println(); |
| 996 | } |
| 997 | needSep = true; |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 998 | printed = true; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 999 | pw.println(" Active ordered broadcasts [" + mQueueName + "]:"); |
| 1000 | } |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1001 | pw.println(" Active Ordered Broadcast " + mQueueName + " #" + i + ":"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1002 | mOrderedBroadcasts.get(i).dump(pw, " "); |
| 1003 | } |
| 1004 | if (dumpPackage == null || (mPendingBroadcast != null |
| 1005 | && dumpPackage.equals(mPendingBroadcast.callerPackage))) { |
| 1006 | if (needSep) { |
| 1007 | pw.println(); |
| 1008 | } |
| 1009 | pw.println(" Pending broadcast [" + mQueueName + "]:"); |
| 1010 | if (mPendingBroadcast != null) { |
| 1011 | mPendingBroadcast.dump(pw, " "); |
| 1012 | } else { |
| 1013 | pw.println(" (null)"); |
| 1014 | } |
| 1015 | needSep = true; |
| 1016 | } |
| 1017 | } |
| 1018 | |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1019 | int i; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1020 | boolean printed = false; |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1021 | for (i=0; i<MAX_BROADCAST_HISTORY; i++) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1022 | BroadcastRecord r = mBroadcastHistory[i]; |
| 1023 | if (r == null) { |
| 1024 | break; |
| 1025 | } |
| 1026 | if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) { |
| 1027 | continue; |
| 1028 | } |
| 1029 | if (!printed) { |
| 1030 | if (needSep) { |
| 1031 | pw.println(); |
| 1032 | } |
| 1033 | needSep = true; |
| 1034 | pw.println(" Historical broadcasts [" + mQueueName + "]:"); |
| 1035 | printed = true; |
| 1036 | } |
| 1037 | if (dumpAll) { |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1038 | pw.print(" Historical Broadcast " + mQueueName + " #"); |
| 1039 | pw.print(i); pw.println(":"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1040 | r.dump(pw, " "); |
| 1041 | } else { |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1042 | pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r); |
| 1043 | pw.print(" "); |
| 1044 | pw.println(r.intent.toShortString(false, true, true, false)); |
| 1045 | Bundle bundle = r.intent.getExtras(); |
| 1046 | if (bundle != null) { |
| 1047 | pw.print(" extras: "); pw.println(bundle.toString()); |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | if (dumpPackage == null) { |
| 1053 | if (dumpAll) { |
| 1054 | i = 0; |
| 1055 | printed = false; |
| 1056 | } |
| 1057 | for (; i<MAX_BROADCAST_SUMMARY_HISTORY; i++) { |
| 1058 | Intent intent = mBroadcastSummaryHistory[i]; |
| 1059 | if (intent == null) { |
| 1060 | break; |
| 1061 | } |
| 1062 | if (!printed) { |
| 1063 | if (needSep) { |
| 1064 | pw.println(); |
| 1065 | } |
| 1066 | needSep = true; |
| 1067 | pw.println(" Historical broadcasts summary [" + mQueueName + "]:"); |
| 1068 | printed = true; |
| 1069 | } |
| 1070 | if (!dumpAll && i >= 50) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1071 | pw.println(" ..."); |
| 1072 | break; |
| 1073 | } |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1074 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 1075 | pw.println(intent.toShortString(false, true, true, false)); |
| 1076 | Bundle bundle = intent.getExtras(); |
| 1077 | if (bundle != null) { |
| 1078 | pw.print(" extras: "); pw.println(bundle.toString()); |
| 1079 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | return needSep; |
| 1084 | } |
| 1085 | } |