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

Commit 5e92c6fd authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

BroadcastDispatcher can use Executors now

registerReceiver takes an optional Executor parameter. The old
registerReceiver that takes a Handler has been renamed and @Deprecated

Test: SystemUITests
Change-Id: I43b97f720b2b153d1019ed3cf19e1533558e380f
parent c1ee9c7c
Loading
Loading
Loading
Loading
+20 −15
Original line number Diff line number Diff line
@@ -48,18 +48,23 @@ Acquire the dispatcher by using `@Inject` to obtain a `BroadcastDispatcher`. The
 * @param filter A filter to determine what broadcasts should be dispatched to this receiver.
 *               It will only take into account actions and categories for filtering. It must
 *               have at least one action.
    * @param handler A handler to dispatch [BroadcastReceiver.onReceive]. By default, it is the
    *                main handler. Pass `null` to use the default.
 * @param executor An executor to dispatch [BroadcastReceiver.onReceive]. Pass null to use an
 *                 executor in the main thread (default).
 * @param user A user handle to determine which broadcast should be dispatched to this receiver.
 *             By default, it is the current user.
 * @throws IllegalArgumentException if the filter has other constraints that are not actions or
 *                                  categories or the filter has no actions.
 */
@JvmOverloads
fun registerReceiver(BroadcastReceiver, IntentFilter, Handler? = mainHandler, UserHandle = context.user)
fun registerReceiver(
        BroadcastReceiver, 
        IntentFilter, 
        Executor? = context.mainExecutor,
        UserHandle = context.user
) {
```

All subscriptions are done with the same overloaded method. As specified in the doc, in order to pass a `UserHandle` with the default `Handler`, pass `null` for the `Handler`.
All subscriptions are done with the same overloaded method. As specified in the doc, in order to pass a `UserHandle` with the default `Executor`, pass `null` for the `Executor`.

In the same way as with `Context`, subscribing the same `BroadcastReceiver` for the same user using different filters will result on two subscriptions, not in replacing the filter.

+3 −3
Original line number Diff line number Diff line
@@ -1638,7 +1638,7 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
        filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
        filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
        filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
        broadcastDispatcher.registerReceiver(mBroadcastReceiver, filter, mHandler);
        broadcastDispatcher.registerReceiverWithHandler(mBroadcastReceiver, filter, mHandler);

        final IntentFilter allUserFilter = new IntentFilter();
        allUserFilter.addAction(Intent.ACTION_USER_INFO_CHANGED);
@@ -1649,8 +1649,8 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
        allUserFilter.addAction(ACTION_USER_UNLOCKED);
        allUserFilter.addAction(ACTION_USER_STOPPED);
        allUserFilter.addAction(ACTION_USER_REMOVED);
        broadcastDispatcher.registerReceiver(mBroadcastAllReceiver, allUserFilter, mHandler,
                UserHandle.ALL);
        broadcastDispatcher.registerReceiverWithHandler(mBroadcastAllReceiver, allUserFilter,
                mHandler, UserHandle.ALL);

        mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
        try {
+1 −1
Original line number Diff line number Diff line
@@ -266,7 +266,7 @@ public class ScreenDecorations extends SystemUI implements Tunable {

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_USER_SWITCHED);
        mBroadcastDispatcher.registerReceiver(mIntentReceiver, filter, mHandler);
        mBroadcastDispatcher.registerReceiverWithHandler(mIntentReceiver, filter, mHandler);

        mOverlay.addOnLayoutChangeListener(new OnLayoutChangeListener() {
            @Override
+33 −6
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.IntentFilter
import android.os.Handler
import android.os.HandlerExecutor
import android.os.Looper
import android.os.Message
import android.os.UserHandle
@@ -32,13 +33,14 @@ import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.dagger.qualifiers.Main
import java.io.FileDescriptor
import java.io.PrintWriter
import java.util.concurrent.Executor
import javax.inject.Inject
import javax.inject.Singleton

data class ReceiverData(
    val receiver: BroadcastReceiver,
    val filter: IntentFilter,
    val handler: Handler,
    val executor: Executor,
    val user: UserHandle
)

@@ -76,8 +78,33 @@ open class BroadcastDispatcher @Inject constructor (
     * @param filter A filter to determine what broadcasts should be dispatched to this receiver.
     *               It will only take into account actions and categories for filtering. It must
     *               have at least one action.
     * @param handler A handler to dispatch [BroadcastReceiver.onReceive]. By default, it is the
     *                main handler. Pass `null` to use the default.
     * @param handler A handler to dispatch [BroadcastReceiver.onReceive].
     * @param user A user handle to determine which broadcast should be dispatched to this receiver.
     *             By default, it is the current user.
     * @throws IllegalArgumentException if the filter has other constraints that are not actions or
     *                                  categories or the filter has no actions.
     */
    @Deprecated(message = "Replacing Handler for Executor in SystemUI",
            replaceWith = ReplaceWith("registerReceiver(receiver, filter, executor, user)"))
    @JvmOverloads
    fun registerReceiverWithHandler(
        receiver: BroadcastReceiver,
        filter: IntentFilter,
        handler: Handler,
        user: UserHandle = context.user
    ) {
        registerReceiver(receiver, filter, HandlerExecutor(handler), user)
    }

    /**
     * Register a receiver for broadcast with the dispatcher
     *
     * @param receiver A receiver to dispatch the [Intent]
     * @param filter A filter to determine what broadcasts should be dispatched to this receiver.
     *               It will only take into account actions and categories for filtering. It must
     *               have at least one action.
     * @param executor An executor to dispatch [BroadcastReceiver.onReceive]. Pass null to use an
     *                 executor in the main thread (default).
     * @param user A user handle to determine which broadcast should be dispatched to this receiver.
     *             By default, it is the current user.
     * @throws IllegalArgumentException if the filter has other constraints that are not actions or
@@ -87,13 +114,13 @@ open class BroadcastDispatcher @Inject constructor (
    fun registerReceiver(
            receiver: BroadcastReceiver,
            filter: IntentFilter,
        handler: Handler? = mainHandler,
            executor: Executor? = context.mainExecutor,
            user: UserHandle = context.user
    ) {
        checkFilter(filter)
        this.handler
                .obtainMessage(MSG_ADD_RECEIVER,
                ReceiverData(receiver, filter, handler ?: mainHandler, user))
                        ReceiverData(receiver, filter, executor ?: context.mainExecutor, user))
                .sendToTarget()
    }

+1 −1
Original line number Diff line number Diff line
@@ -193,7 +193,7 @@ class UserBroadcastDispatcher(
                        it.filter.hasAction(intent.action) &&
                            it.filter.matchCategories(intent.categories) == null }
                    ?.forEach {
                        it.handler.post {
                        it.executor.execute {
                            if (DEBUG) Log.w(TAG,
                                    "[$index] Dispatching ${intent.action} to ${it.receiver}")
                            it.receiver.pendingResult = pendingResult
Loading