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