Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1600f2a8 authored by Jeff Sharkey's avatar Jeff Sharkey Committed by Android (Google) Code Review
Browse files

Merge changes from topic "aug24b"

* changes:
  [2/?] Reduce BroadcastQueue interface complexity.
  [1/?] Reduce BroadcastQueue interface complexity.
  Factor out broadcast "skip" policy.
parents b61f0a74 6c3670e1
Loading
Loading
Loading
Loading
+19 −46
Original line number Diff line number Diff line
@@ -10626,8 +10626,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        if (!onlyHistory && !onlyReceivers && dumpAll) {
            pw.println();
            for (BroadcastQueue queue : mBroadcastQueues) {
                pw.println("  mBroadcastsScheduled [" + queue.mQueueName + "]="
                        + queue.hasBroadcastsScheduled());
                pw.println("  Queue " + queue.toString() + ": " + queue.describeState());
            }
            pw.println("  mHandler:");
            mHandler.dump(new PrintWriterPrinter(pw), "    ");
@@ -13082,17 +13081,23 @@ public class ActivityManagerService extends IActivityManager.Stub
    }
    boolean isPendingBroadcastProcessLocked(int pid) {
        return mFgBroadcastQueue.isPendingBroadcastProcessLocked(pid)
                || mBgBroadcastQueue.isPendingBroadcastProcessLocked(pid)
                || mBgOffloadBroadcastQueue.isPendingBroadcastProcessLocked(pid)
                || mFgOffloadBroadcastQueue.isPendingBroadcastProcessLocked(pid);
        for (BroadcastQueue queue : mBroadcastQueues) {
            BroadcastRecord r = queue.getPendingBroadcastLocked();
            if (r != null && r.curApp.getPid() == pid) {
                return true;
            }
        }
        return false;
    }
    boolean isPendingBroadcastProcessLocked(ProcessRecord app) {
        return mFgBroadcastQueue.isPendingBroadcastProcessLocked(app)
                || mBgBroadcastQueue.isPendingBroadcastProcessLocked(app)
                || mBgOffloadBroadcastQueue.isPendingBroadcastProcessLocked(app)
                || mFgOffloadBroadcastQueue.isPendingBroadcastProcessLocked(app);
        for (BroadcastQueue queue : mBroadcastQueues) {
            BroadcastRecord r = queue.getPendingBroadcastLocked();
            if (r != null && r.curApp == app) {
                return true;
            }
        }
        return false;
    }
    void skipPendingBroadcastLocked(int pid) {
@@ -13114,7 +13119,6 @@ public class ActivityManagerService extends IActivityManager.Stub
    void updateUidReadyForBootCompletedBroadcastLocked(int uid) {
        for (BroadcastQueue queue : mBroadcastQueues) {
            queue.updateUidReadyForBootCompletedBroadcastLocked(uid);
            queue.scheduleBroadcastsLocked();
        }
    }
@@ -13364,8 +13368,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                            receivers, null, 0, null, null, false, true, true, -1, false, null,
                            false /* only PRE_BOOT_COMPLETED should be exempt, no stickies */,
                            null /* filterExtrasForReceiver */);
                    queue.enqueueParallelBroadcastLocked(r);
                    queue.scheduleBroadcastsLocked();
                    queue.enqueueBroadcastLocked(r);
                }
            }
@@ -14243,13 +14246,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                    sticky, false, userId, allowBackgroundActivityStarts,
                    backgroundActivityStartsToken, timeoutExempt, filterExtrasForReceiver);
            if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Enqueueing parallel broadcast " + r);
            final boolean replaced = replacePending
                    && (queue.replaceParallelBroadcastLocked(r) != null);
            // Note: We assume resultTo is null for non-ordered broadcasts.
            if (!replaced) {
                queue.enqueueParallelBroadcastLocked(r);
                queue.scheduleBroadcastsLocked();
            }
            queue.enqueueBroadcastLocked(r);
            registeredReceivers = null;
            NR = 0;
        }
@@ -14342,31 +14339,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                    backgroundActivityStartsToken, timeoutExempt, filterExtrasForReceiver);
            if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Enqueueing ordered broadcast " + r);
            final BroadcastRecord oldRecord =
                    replacePending ? queue.replaceOrderedBroadcastLocked(r) : null;
            if (oldRecord != null) {
                // Replaced, fire the result-to receiver.
                if (oldRecord.resultTo != null) {
                    final BroadcastQueue oldQueue = broadcastQueueForIntent(oldRecord.intent);
                    try {
                        oldRecord.mIsReceiverAppRunning = true;
                        oldQueue.performReceiveLocked(oldRecord.callerApp, oldRecord.resultTo,
                                oldRecord.intent,
                                Activity.RESULT_CANCELED, null, null,
                                false, false, oldRecord.userId, oldRecord.callingUid, callingUid,
                                SystemClock.uptimeMillis() - oldRecord.enqueueTime, 0);
                    } catch (RemoteException e) {
                        Slog.w(TAG, "Failure ["
                                + queue.mQueueName + "] sending broadcast result of "
                                + intent, e);
                    }
                }
            } else {
                queue.enqueueOrderedBroadcastLocked(r);
                queue.scheduleBroadcastsLocked();
            }
            queue.enqueueBroadcastLocked(r);
        } else {
            // There was nobody interested in the broadcast, but we still want to record
            // that it happened.
@@ -17890,7 +17863,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                            pw.flush();
                        }
                        Slog.v(TAG, msg);
                        queue.cancelDeferrals();
                        queue.flush();
                        idle = false;
                    }
                }
+2 −2
Original line number Diff line number Diff line
@@ -162,7 +162,7 @@ public class BroadcastDispatcher {
    }

    private final Object mLock;
    private final BroadcastQueue mQueue;
    private final BroadcastQueueImpl mQueue;
    private final BroadcastConstants mConstants;
    private final Handler mHandler;
    private AlarmManagerInternal mAlarm;
@@ -489,7 +489,7 @@ public class BroadcastDispatcher {
    /**
     * Constructed & sharing a lock with its associated BroadcastQueue instance
     */
    public BroadcastDispatcher(BroadcastQueue queue, BroadcastConstants constants,
    public BroadcastDispatcher(BroadcastQueueImpl queue, BroadcastConstants constants,
            Handler handler, Object lock) {
        mQueue = queue;
        mConstants = constants;
+45 −26
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 The Android Open Source Project
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -16,13 +16,12 @@

package com.android.server.am;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.IIntentReceiver;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.proto.ProtoOutputStream;

import java.io.FileDescriptor;
@@ -33,9 +32,12 @@ import java.util.Set;
 * Queue of broadcast intents and associated bookkeeping.
 */
public abstract class BroadcastQueue {
    public static final String TAG = "BroadcastQueue";

    final ActivityManagerService mService;
    final Handler mHandler;
    final BroadcastConstants mConstants;
    final BroadcastSkipPolicy mSkipPolicy;
    final String mQueueName;

    BroadcastQueue(ActivityManagerService service, Handler handler,
@@ -44,6 +46,7 @@ public abstract class BroadcastQueue {
        mHandler = handler;
        mQueueName = name;
        mConstants = constants;
        mSkipPolicy = new BroadcastSkipPolicy(service);
    }

    void start(ContentResolver resolver) {
@@ -57,23 +60,20 @@ public abstract class BroadcastQueue {

    public abstract boolean isDelayBehindServices();

    public abstract boolean hasBroadcastsScheduled();

    public abstract BroadcastRecord getPendingBroadcastLocked();

    public abstract BroadcastRecord getActiveBroadcastLocked();

    public abstract boolean isPendingBroadcastProcessLocked(int pid);

    public abstract boolean isPendingBroadcastProcessLocked(ProcessRecord app);

    public abstract void enqueueParallelBroadcastLocked(BroadcastRecord r);

    public abstract void enqueueOrderedBroadcastLocked(BroadcastRecord r);

    public abstract BroadcastRecord replaceParallelBroadcastLocked(BroadcastRecord r);

    public abstract BroadcastRecord replaceOrderedBroadcastLocked(BroadcastRecord r);
    /**
     * Enqueue the given broadcast to be eventually dispatched.
     * <p>
     * Callers must populate {@link BroadcastRecord#receivers} with the relevant
     * targets before invoking this method.
     * <p>
     * When {@link Intent#FLAG_RECEIVER_REPLACE_PENDING} is set, this method
     * internally handles replacement of any matching broadcasts.
     */
    public abstract void enqueueBroadcastLocked(BroadcastRecord r);

    public abstract void updateUidReadyForBootCompletedBroadcastLocked(int uid);

@@ -83,32 +83,51 @@ public abstract class BroadcastQueue {

    public abstract void skipCurrentReceiverLocked(ProcessRecord app);

    public abstract void scheduleBroadcastsLocked();

    public abstract BroadcastRecord getMatchingOrderedReceiver(IBinder receiver);

    /**
     * Signal delivered back from a {@link BroadcastReceiver} to indicate that
     * it's finished processing the current broadcast being dispatched to it.
     * <p>
     * If this signal isn't delivered back in a timely fashion, we assume the
     * receiver has somehow wedged and we trigger an ANR.
     */
    public abstract boolean finishReceiverLocked(BroadcastRecord r, int resultCode,
            String resultData, Bundle resultExtras, boolean resultAbort, boolean waitForServices);

    public abstract void backgroundServicesFinishedLocked(int userId);

    public abstract void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,
            Intent intent, int resultCode, String data, Bundle extras,
            boolean ordered, boolean sticky, int sendingUser,
            int receiverUid, int callingUid, long dispatchDelay,
            long receiveDelay) throws RemoteException;

    public abstract void processNextBroadcastLocked(boolean fromMsg, boolean skipOomAdj);

    /**
     * Signal from OS internals that the given package (or some subset of that
     * package) has been disabled or uninstalled, and that any pending
     * broadcasts should be cleaned up.
     */
    public abstract boolean cleanupDisabledPackageReceiversLocked(
            String packageName, Set<String> filterByClasses, int userId, boolean doit);

    /**
     * Quickly determine if this queue has broadcasts that are still waiting to
     * be delivered at some point in the future.
     *
     * @see #flush()
     */
    public abstract boolean isIdle();

    public abstract void cancelDeferrals();

    /**
     * Brief summary of internal state, useful for debugging purposes.
     */
    public abstract String describeState();

    /**
     * Flush any broadcasts still waiting to be delivered, causing them to be
     * delivered as soon as possible.
     *
     * @see #isIdle()
     */
    public abstract void flush();

    public abstract void dumpDebug(ProtoOutputStream proto, long fieldId);

    public abstract boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args,
+63 −678

File changed.

Preview size limit exceeded, changes collapsed.

+715 −0

File added.

Preview size limit exceeded, changes collapsed.