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