diff --git a/packages/SystemUI/docs/broadcasts.md b/packages/SystemUI/docs/broadcasts.md index 56a637fe588c1..28657f28e53bd 100644 --- a/packages/SystemUI/docs/broadcasts.md +++ b/packages/SystemUI/docs/broadcasts.md @@ -42,24 +42,29 @@ Acquire the dispatcher by using `@Inject` to obtain a `BroadcastDispatcher`. The ```kotlin /** - * 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 handler A handler to dispatch [BroadcastReceiver.onReceive]. By default, it is the - * main handler. Pass `null` to use the 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. - */ + * 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 + * 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. diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java b/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java index 694c623ac11fb..0faadf7e4fabd 100644 --- a/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java +++ b/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java @@ -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 { diff --git a/packages/SystemUI/src/com/android/systemui/ScreenDecorations.java b/packages/SystemUI/src/com/android/systemui/ScreenDecorations.java index 0e736dcd11a85..c533755c76da9 100644 --- a/packages/SystemUI/src/com/android/systemui/ScreenDecorations.java +++ b/packages/SystemUI/src/com/android/systemui/ScreenDecorations.java @@ -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 diff --git a/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastDispatcher.kt b/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastDispatcher.kt index 8cb0cc5db1d3b..cedf7c354ccc9 100644 --- a/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastDispatcher.kt +++ b/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastDispatcher.kt @@ -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 @@ -85,15 +112,15 @@ open class BroadcastDispatcher @Inject constructor ( */ @JvmOverloads fun registerReceiver( - receiver: BroadcastReceiver, - filter: IntentFilter, - handler: Handler? = mainHandler, - user: UserHandle = context.user + receiver: BroadcastReceiver, + filter: IntentFilter, + 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() } diff --git a/packages/SystemUI/src/com/android/systemui/broadcast/UserBroadcastDispatcher.kt b/packages/SystemUI/src/com/android/systemui/broadcast/UserBroadcastDispatcher.kt index b2942bb14c6b3..0c631aacab825 100644 --- a/packages/SystemUI/src/com/android/systemui/broadcast/UserBroadcastDispatcher.kt +++ b/packages/SystemUI/src/com/android/systemui/broadcast/UserBroadcastDispatcher.kt @@ -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 diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeScreenBrightness.java b/packages/SystemUI/src/com/android/systemui/doze/DozeScreenBrightness.java index c9faf69cfd6f0..4e887262659ef 100644 --- a/packages/SystemUI/src/com/android/systemui/doze/DozeScreenBrightness.java +++ b/packages/SystemUI/src/com/android/systemui/doze/DozeScreenBrightness.java @@ -91,7 +91,7 @@ public class DozeScreenBrightness extends BroadcastReceiver implements DozeMachi if (mDebuggable) { IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_AOD_BRIGHTNESS); - mBroadcastDispatcher.registerReceiver(this, filter, handler, UserHandle.ALL); + mBroadcastDispatcher.registerReceiverWithHandler(this, filter, handler, UserHandle.ALL); } } diff --git a/packages/SystemUI/src/com/android/systemui/power/PowerUI.java b/packages/SystemUI/src/com/android/systemui/power/PowerUI.java index 59ac329e19838..48750fa5e7693 100644 --- a/packages/SystemUI/src/com/android/systemui/power/PowerUI.java +++ b/packages/SystemUI/src/com/android/systemui/power/PowerUI.java @@ -233,7 +233,7 @@ public class PowerUI extends SystemUI implements CommandQueue.Callbacks { filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_USER_SWITCHED); - mBroadcastDispatcher.registerReceiver(this, filter, mHandler); + mBroadcastDispatcher.registerReceiverWithHandler(this, filter, mHandler); } @Override diff --git a/packages/SystemUI/src/com/android/systemui/qs/external/TileLifecycleManager.java b/packages/SystemUI/src/com/android/systemui/qs/external/TileLifecycleManager.java index bbff117c6f814..ad79cadcc12d6 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/external/TileLifecycleManager.java +++ b/packages/SystemUI/src/com/android/systemui/qs/external/TileLifecycleManager.java @@ -322,7 +322,7 @@ public class TileLifecycleManager extends BroadcastReceiver implements filter = new IntentFilter(Intent.ACTION_USER_UNLOCKED); try { mUserReceiverRegistered.set(true); - mBroadcastDispatcher.registerReceiver(this, filter, mHandler, mUser); + mBroadcastDispatcher.registerReceiverWithHandler(this, filter, mHandler, mUser); } catch (Exception ex) { mUserReceiverRegistered.set(false); Log.e(TAG, "Could not register unlock receiver", ex); diff --git a/packages/SystemUI/src/com/android/systemui/settings/CurrentUserTracker.java b/packages/SystemUI/src/com/android/systemui/settings/CurrentUserTracker.java index 077d2602e43fa..9599d77bf65a8 100644 --- a/packages/SystemUI/src/com/android/systemui/settings/CurrentUserTracker.java +++ b/packages/SystemUI/src/com/android/systemui/settings/CurrentUserTracker.java @@ -98,7 +98,8 @@ public abstract class CurrentUserTracker { if (!mReceiverRegistered) { mCurrentUserId = ActivityManager.getCurrentUser(); IntentFilter filter = new IntentFilter(Intent.ACTION_USER_SWITCHED); - mBroadcastDispatcher.registerReceiver(this, filter, null, UserHandle.ALL); + mBroadcastDispatcher.registerReceiver(this, filter, null, + UserHandle.ALL); mReceiverRegistered = true; } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java index d4cf272e80778..08e1319a275a9 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java @@ -366,8 +366,8 @@ public class NavigationBarFragment extends LifecycleFragment implements Callback IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_USER_SWITCHED); - mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter, Handler.getMain(), - UserHandle.ALL); + mBroadcastDispatcher.registerReceiverWithHandler(mBroadcastReceiver, filter, + Handler.getMain(), UserHandle.ALL); notifyNavigationBarScreenOn(); mOverviewProxyService.addCallback(mOverviewProxyListener); diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java index 5b34aa7781c12..00e38f814dff6 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java @@ -179,7 +179,7 @@ public class PhoneStatusBarPolicy filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE); filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); - broadcastDispatcher.registerReceiver(mIntentReceiver, filter, mHandler); + broadcastDispatcher.registerReceiverWithHandler(mIntentReceiver, filter, mHandler); // listen for user / profile change. try { diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/Clock.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/Clock.java index ddacc3aedce0b..4f0af9e166c97 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/Clock.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/Clock.java @@ -188,7 +188,7 @@ public class Clock extends TextView implements DemoMode, Tunable, CommandQueue.C // NOTE: This receiver could run before this method returns, as it's not dispatching // on the main thread and BroadcastDispatcher may not need to register with Context. // The receiver will return immediately if the view does not have a Handler yet. - mBroadcastDispatcher.registerReceiver(mIntentReceiver, filter, + mBroadcastDispatcher.registerReceiverWithHandler(mIntentReceiver, filter, Dependency.get(Dependency.TIME_TICK_HANDLER), UserHandle.ALL); Dependency.get(TunerService.class).addTunable(this, CLOCK_SECONDS, StatusBarIconController.ICON_BLACKLIST); diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/DateView.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/DateView.java index 2e26711a35782..b4c154aa28cbe 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/DateView.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/DateView.java @@ -96,7 +96,7 @@ public class DateView extends TextView { filter.addAction(Intent.ACTION_TIME_CHANGED); filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); filter.addAction(Intent.ACTION_LOCALE_CHANGED); - mBroadcastDispatcher.registerReceiver(mIntentReceiver, filter, + mBroadcastDispatcher.registerReceiverWithHandler(mIntentReceiver, filter, Dependency.get(Dependency.TIME_TICK_HANDLER)); updateClock(); diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/LocationControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/LocationControllerImpl.java index 570f153a62c02..cb40d7752f535 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/LocationControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/LocationControllerImpl.java @@ -79,7 +79,8 @@ public class LocationControllerImpl extends BroadcastReceiver implements Locatio IntentFilter filter = new IntentFilter(); filter.addAction(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION); filter.addAction(LocationManager.MODE_CHANGED_ACTION); - mBroadcastDispatcher.registerReceiver(this, filter, new Handler(bgLooper), UserHandle.ALL); + mBroadcastDispatcher.registerReceiverWithHandler(this, filter, + new Handler(bgLooper), UserHandle.ALL); mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); mStatusBarManager diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java index f20a47babe5d4..32bbf4783c6cf 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java @@ -326,7 +326,7 @@ public class NetworkControllerImpl extends BroadcastReceiver filter.addAction(ConnectivityManager.INET_CONDITION_ACTION); filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); filter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED); - mBroadcastDispatcher.registerReceiver(this, filter, mReceiverHandler); + mBroadcastDispatcher.registerReceiverWithHandler(this, filter, mReceiverHandler); mListening = true; updateMobileControllers(); diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/SecurityControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/SecurityControllerImpl.java index 019ef3bca709e..312c4ac75bfaa 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/SecurityControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/SecurityControllerImpl.java @@ -126,7 +126,8 @@ public class SecurityControllerImpl extends CurrentUserTracker implements Securi IntentFilter filter = new IntentFilter(); filter.addAction(KeyChain.ACTION_TRUST_STORE_CHANGED); filter.addAction(Intent.ACTION_USER_UNLOCKED); - broadcastDispatcher.registerReceiver(mBroadcastReceiver, filter, bgHandler, UserHandle.ALL); + broadcastDispatcher.registerReceiverWithHandler(mBroadcastReceiver, filter, bgHandler, + UserHandle.ALL); // TODO: re-register network callback on user change. mConnectivityManager.registerNetworkCallback(REQUEST, mNetworkCallback); diff --git a/packages/SystemUI/src/com/android/systemui/theme/ThemeOverlayController.java b/packages/SystemUI/src/com/android/systemui/theme/ThemeOverlayController.java index 7758aba529189..31b9952afe941 100644 --- a/packages/SystemUI/src/com/android/systemui/theme/ThemeOverlayController.java +++ b/packages/SystemUI/src/com/android/systemui/theme/ThemeOverlayController.java @@ -88,7 +88,7 @@ public class ThemeOverlayController extends SystemUI { final IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_USER_SWITCHED); filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED); - mBroadcastDispatcher.registerReceiver(new BroadcastReceiver() { + mBroadcastDispatcher.registerReceiverWithHandler(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (DEBUG) Log.d(TAG, "Updating overlays for user switch / profile added."); diff --git a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java index a4ed31d95b2b7..112ae6f3758a2 100644 --- a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java @@ -1008,7 +1008,7 @@ public class VolumeDialogControllerImpl implements VolumeDialogController, Dumpa filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); - mBroadcastDispatcher.registerReceiver(this, filter, mWorker); + mBroadcastDispatcher.registerReceiverWithHandler(this, filter, mWorker); } public void destroy() { diff --git a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardUpdateMonitorTest.java b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardUpdateMonitorTest.java index 4bf1e1c0920d5..ef1e8172a4642 100644 --- a/packages/SystemUI/tests/src/com/android/keyguard/KeyguardUpdateMonitorTest.java +++ b/packages/SystemUI/tests/src/com/android/keyguard/KeyguardUpdateMonitorTest.java @@ -150,10 +150,10 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase { @Test public void testReceiversRegistered() { - verify(mBroadcastDispatcher, atLeastOnce()).registerReceiver( + verify(mBroadcastDispatcher, atLeastOnce()).registerReceiverWithHandler( eq(mKeyguardUpdateMonitor.mBroadcastReceiver), any(IntentFilter.class), any(Handler.class)); - verify(mBroadcastDispatcher, atLeastOnce()).registerReceiver( + verify(mBroadcastDispatcher, atLeastOnce()).registerReceiverWithHandler( eq(mKeyguardUpdateMonitor.mBroadcastAllReceiver), any(IntentFilter.class), any(Handler.class), eq(UserHandle.ALL)); } diff --git a/packages/SystemUI/tests/src/com/android/systemui/broadcast/BroadcastDispatcherTest.kt b/packages/SystemUI/tests/src/com/android/systemui/broadcast/BroadcastDispatcherTest.kt index 42fbf59fef44a..22b18373e81d3 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/broadcast/BroadcastDispatcherTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/broadcast/BroadcastDispatcherTest.kt @@ -27,6 +27,8 @@ import android.test.suitebuilder.annotation.SmallTest import android.testing.AndroidTestingRunner import android.testing.TestableLooper import com.android.systemui.SysuiTestCase +import com.android.systemui.util.concurrency.FakeExecutor +import com.android.systemui.util.time.FakeSystemClock import junit.framework.Assert.assertSame import org.junit.Before import org.junit.Test @@ -39,6 +41,7 @@ import org.mockito.Mockito.mock import org.mockito.Mockito.never import org.mockito.Mockito.verify import org.mockito.MockitoAnnotations +import java.util.concurrent.Executor @RunWith(AndroidTestingRunner::class) @TestableLooper.RunWithLooper @@ -73,6 +76,8 @@ class BroadcastDispatcherTest : SysuiTestCase() { @Mock private lateinit var mockHandler: Handler + private lateinit var executor: Executor + @Captor private lateinit var argumentCaptor: ArgumentCaptor @@ -83,6 +88,7 @@ class BroadcastDispatcherTest : SysuiTestCase() { fun setUp() { MockitoAnnotations.initMocks(this) testableLooper = TestableLooper.get(this) + executor = FakeExecutor(FakeSystemClock()) broadcastDispatcher = TestBroadcastDispatcher( mockContext, @@ -98,8 +104,9 @@ class BroadcastDispatcherTest : SysuiTestCase() { @Test fun testAddingReceiverToCorrectUBR() { - broadcastDispatcher.registerReceiver(broadcastReceiver, intentFilter, mockHandler, user0) - broadcastDispatcher.registerReceiver( + broadcastDispatcher.registerReceiverWithHandler(broadcastReceiver, intentFilter, + mockHandler, user0) + broadcastDispatcher.registerReceiverWithHandler( broadcastReceiverOther, intentFilterOther, mockHandler, user1) testableLooper.processAllMessages() @@ -114,10 +121,30 @@ class BroadcastDispatcherTest : SysuiTestCase() { assertSame(intentFilterOther, argumentCaptor.value.filter) } + @Test + fun testAddingReceiverToCorrectUBR_executor() { + broadcastDispatcher.registerReceiver(broadcastReceiver, intentFilter, executor, user0) + broadcastDispatcher.registerReceiver( + broadcastReceiverOther, intentFilterOther, executor, user1) + + testableLooper.processAllMessages() + + verify(mockUBRUser0).registerReceiver(capture(argumentCaptor)) + + assertSame(broadcastReceiver, argumentCaptor.value.receiver) + assertSame(intentFilter, argumentCaptor.value.filter) + + verify(mockUBRUser1).registerReceiver(capture(argumentCaptor)) + assertSame(broadcastReceiverOther, argumentCaptor.value.receiver) + assertSame(intentFilterOther, argumentCaptor.value.filter) + } + @Test fun testRemovingReceiversRemovesFromAllUBR() { - broadcastDispatcher.registerReceiver(broadcastReceiver, intentFilter, mockHandler, user0) - broadcastDispatcher.registerReceiver(broadcastReceiver, intentFilter, mockHandler, user1) + broadcastDispatcher.registerReceiverWithHandler(broadcastReceiver, intentFilter, + mockHandler, user0) + broadcastDispatcher.registerReceiverWithHandler(broadcastReceiver, intentFilter, + mockHandler, user1) broadcastDispatcher.unregisterReceiver(broadcastReceiver) @@ -129,8 +156,10 @@ class BroadcastDispatcherTest : SysuiTestCase() { @Test fun testRemoveReceiverFromUser() { - broadcastDispatcher.registerReceiver(broadcastReceiver, intentFilter, mockHandler, user0) - broadcastDispatcher.registerReceiver(broadcastReceiver, intentFilter, mockHandler, user1) + broadcastDispatcher.registerReceiverWithHandler(broadcastReceiver, intentFilter, + mockHandler, user0) + broadcastDispatcher.registerReceiverWithHandler(broadcastReceiver, intentFilter, + mockHandler, user1) broadcastDispatcher.unregisterReceiverForUser(broadcastReceiver, user0) @@ -143,8 +172,8 @@ class BroadcastDispatcherTest : SysuiTestCase() { @Test fun testRegisterCurrentAsActualUser() { setUserMock(mockContext, user1) - broadcastDispatcher.registerReceiver(broadcastReceiver, intentFilter, mockHandler, - UserHandle.CURRENT) + broadcastDispatcher.registerReceiverWithHandler(broadcastReceiver, intentFilter, + mockHandler, UserHandle.CURRENT) testableLooper.processAllMessages() diff --git a/packages/SystemUI/tests/src/com/android/systemui/broadcast/UserBroadcastDispatcherTest.kt b/packages/SystemUI/tests/src/com/android/systemui/broadcast/UserBroadcastDispatcherTest.kt index 21ed15517752f..7821ae29592eb 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/broadcast/UserBroadcastDispatcherTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/broadcast/UserBroadcastDispatcherTest.kt @@ -26,6 +26,8 @@ import android.test.suitebuilder.annotation.SmallTest import android.testing.AndroidTestingRunner import android.testing.TestableLooper import com.android.systemui.SysuiTestCase +import com.android.systemui.util.concurrency.FakeExecutor +import com.android.systemui.util.time.FakeSystemClock import junit.framework.Assert.assertEquals import junit.framework.Assert.assertFalse import junit.framework.Assert.assertTrue @@ -69,8 +71,6 @@ class UserBroadcastDispatcherTest : SysuiTestCase() { @Mock private lateinit var mockContext: Context @Mock - private lateinit var mockHandler: Handler - @Mock private lateinit var mPendingResult: BroadcastReceiver.PendingResult @Captor @@ -81,12 +81,14 @@ class UserBroadcastDispatcherTest : SysuiTestCase() { private lateinit var intentFilter: IntentFilter private lateinit var intentFilterOther: IntentFilter private lateinit var handler: Handler + private lateinit var fakeExecutor: FakeExecutor @Before fun setUp() { MockitoAnnotations.initMocks(this) testableLooper = TestableLooper.get(this) handler = Handler(testableLooper.looper) + fakeExecutor = FakeExecutor(FakeSystemClock()) userBroadcastDispatcher = UserBroadcastDispatcher( mockContext, USER_ID, handler, testableLooper.looper) @@ -108,7 +110,7 @@ class UserBroadcastDispatcherTest : SysuiTestCase() { intentFilter = IntentFilter(ACTION_1) userBroadcastDispatcher.registerReceiver( - ReceiverData(broadcastReceiver, intentFilter, mockHandler, USER_HANDLE)) + ReceiverData(broadcastReceiver, intentFilter, fakeExecutor, USER_HANDLE)) testableLooper.processAllMessages() assertTrue(userBroadcastDispatcher.isRegistered()) @@ -128,7 +130,7 @@ class UserBroadcastDispatcherTest : SysuiTestCase() { intentFilter = IntentFilter(ACTION_1) userBroadcastDispatcher.registerReceiver( - ReceiverData(broadcastReceiver, intentFilter, mockHandler, USER_HANDLE)) + ReceiverData(broadcastReceiver, intentFilter, fakeExecutor, USER_HANDLE)) testableLooper.processAllMessages() reset(mockContext) @@ -151,9 +153,9 @@ class UserBroadcastDispatcherTest : SysuiTestCase() { } userBroadcastDispatcher.registerReceiver( - ReceiverData(broadcastReceiver, intentFilter, mockHandler, USER_HANDLE)) + ReceiverData(broadcastReceiver, intentFilter, fakeExecutor, USER_HANDLE)) userBroadcastDispatcher.registerReceiver( - ReceiverData(broadcastReceiverOther, intentFilterOther, mockHandler, USER_HANDLE)) + ReceiverData(broadcastReceiverOther, intentFilterOther, fakeExecutor, USER_HANDLE)) testableLooper.processAllMessages() assertTrue(userBroadcastDispatcher.isRegistered()) @@ -179,14 +181,15 @@ class UserBroadcastDispatcherTest : SysuiTestCase() { intentFilterOther = IntentFilter(ACTION_2) userBroadcastDispatcher.registerReceiver( - ReceiverData(broadcastReceiver, intentFilter, handler, USER_HANDLE)) + ReceiverData(broadcastReceiver, intentFilter, fakeExecutor, USER_HANDLE)) userBroadcastDispatcher.registerReceiver( - ReceiverData(broadcastReceiverOther, intentFilterOther, handler, USER_HANDLE)) + ReceiverData(broadcastReceiverOther, intentFilterOther, fakeExecutor, USER_HANDLE)) val intent = Intent(ACTION_2) userBroadcastDispatcher.onReceive(mockContext, intent) testableLooper.processAllMessages() + fakeExecutor.runAllReady() verify(broadcastReceiver, never()).onReceive(any(), any()) verify(broadcastReceiverOther).onReceive(mockContext, intent) @@ -198,14 +201,15 @@ class UserBroadcastDispatcherTest : SysuiTestCase() { intentFilterOther = IntentFilter(ACTION_2) userBroadcastDispatcher.registerReceiver( - ReceiverData(broadcastReceiver, intentFilter, handler, USER_HANDLE)) + ReceiverData(broadcastReceiver, intentFilter, fakeExecutor, USER_HANDLE)) userBroadcastDispatcher.registerReceiver( - ReceiverData(broadcastReceiver, intentFilterOther, handler, USER_HANDLE)) + ReceiverData(broadcastReceiver, intentFilterOther, fakeExecutor, USER_HANDLE)) val intent = Intent(ACTION_2) userBroadcastDispatcher.onReceive(mockContext, intent) testableLooper.processAllMessages() + fakeExecutor.runAllReady() verify(broadcastReceiver).onReceive(mockContext, intent) } @@ -218,14 +222,15 @@ class UserBroadcastDispatcherTest : SysuiTestCase() { intentFilterOther.addCategory(CATEGORY_2) userBroadcastDispatcher.registerReceiver( - ReceiverData(broadcastReceiver, intentFilter, handler, USER_HANDLE)) + ReceiverData(broadcastReceiver, intentFilter, fakeExecutor, USER_HANDLE)) userBroadcastDispatcher.registerReceiver( - ReceiverData(broadcastReceiverOther, intentFilterOther, handler, USER_HANDLE)) + ReceiverData(broadcastReceiverOther, intentFilterOther, fakeExecutor, USER_HANDLE)) val intent = Intent(ACTION_1) userBroadcastDispatcher.onReceive(mockContext, intent) testableLooper.processAllMessages() + fakeExecutor.runAllReady() verify(broadcastReceiver).onReceive(mockContext, intent) verify(broadcastReceiverOther).onReceive(mockContext, intent) @@ -235,12 +240,13 @@ class UserBroadcastDispatcherTest : SysuiTestCase() { fun testPendingResult() { intentFilter = IntentFilter(ACTION_1) userBroadcastDispatcher.registerReceiver( - ReceiverData(broadcastReceiver, intentFilter, handler, USER_HANDLE)) + ReceiverData(broadcastReceiver, intentFilter, fakeExecutor, USER_HANDLE)) val intent = Intent(ACTION_1) userBroadcastDispatcher.onReceive(mockContext, intent) testableLooper.processAllMessages() + fakeExecutor.runAllReady() verify(broadcastReceiver).onReceive(mockContext, intent) verify(broadcastReceiver).pendingResult = mPendingResult @@ -250,15 +256,16 @@ class UserBroadcastDispatcherTest : SysuiTestCase() { fun testRemoveReceiverReferences() { intentFilter = IntentFilter(ACTION_1) userBroadcastDispatcher.registerReceiver( - ReceiverData(broadcastReceiver, intentFilter, handler, USER_HANDLE)) + ReceiverData(broadcastReceiver, intentFilter, fakeExecutor, USER_HANDLE)) intentFilterOther = IntentFilter(ACTION_1) intentFilterOther.addAction(ACTION_2) userBroadcastDispatcher.registerReceiver( - ReceiverData(broadcastReceiverOther, intentFilterOther, handler, USER_HANDLE)) + ReceiverData(broadcastReceiverOther, intentFilterOther, fakeExecutor, USER_HANDLE)) userBroadcastDispatcher.unregisterReceiver(broadcastReceiver) testableLooper.processAllMessages() + fakeExecutor.runAllReady() assertFalse(userBroadcastDispatcher.isReceiverReferenceHeld(broadcastReceiver)) } diff --git a/packages/SystemUI/tests/src/com/android/systemui/power/PowerUITest.java b/packages/SystemUI/tests/src/com/android/systemui/power/PowerUITest.java index 167f361b341a4..548da8e1f2aa1 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/power/PowerUITest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/power/PowerUITest.java @@ -110,7 +110,7 @@ public class PowerUITest extends SysuiTestCase { @Test public void testReceiverIsRegisteredToDispatcherOnStart() { mPowerUI.start(); - verify(mBroadcastDispatcher).registerReceiver( + verify(mBroadcastDispatcher).registerReceiverWithHandler( any(BroadcastReceiver.class), any(IntentFilter.class), any(Handler.class)); //PowerUI does not call with User diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NavigationBarFragmentTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NavigationBarFragmentTest.java index 39afbe0a1a233..8f645b6bd3c8b 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NavigationBarFragmentTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NavigationBarFragmentTest.java @@ -190,7 +190,7 @@ public class NavigationBarFragmentTest extends SysuiBaseFragmentTest { mFragments.dispatchResume(); processAllMessages(); - verify(mBroadcastDispatcher).registerReceiver( + verify(mBroadcastDispatcher).registerReceiverWithHandler( any(BroadcastReceiver.class), any(IntentFilter.class), any(Handler.class), diff --git a/packages/SystemUI/tests/src/com/android/systemui/volume/VolumeDialogControllerImplTest.java b/packages/SystemUI/tests/src/com/android/systemui/volume/VolumeDialogControllerImplTest.java index 2854665aedb11..80aa6f6c49bc1 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/volume/VolumeDialogControllerImplTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/volume/VolumeDialogControllerImplTest.java @@ -69,7 +69,7 @@ public class VolumeDialogControllerImplTest extends SysuiTestCase { @Test public void testRegisteredWithDispatcher() { - verify(mBroadcastDispatcher).registerReceiver(any(BroadcastReceiver.class), + verify(mBroadcastDispatcher).registerReceiverWithHandler(any(BroadcastReceiver.class), any(IntentFilter.class), any(Handler.class)); // VolumeDialogControllerImpl does not call with user }