diff options
| author | 2020-03-27 13:29:15 -0400 | |
|---|---|---|
| committer | 2020-03-27 13:32:49 -0400 | |
| commit | aaf4c5e47b0876f46b5d3151b4068a32d0bfdf27 (patch) | |
| tree | 9a85966d39b425075a49ea2723352fad33afe95d | |
| parent | 5cb264d9df806f3433d4065d8d9d7ac8c4e96269 (diff) | |
Use bg thread for BroadcastDispatcher registering
There is no need for the broadcastdispatcher to register in the main
thread. Moving to the bg thread reduces the chance of our main thread
being busy when many classes subscribe in a short period of time.
Test: manual (logs show BroadcastDispatcher is receiving broadcasts)
Test: atest UserBroadcastDispatcherTest
Fixes: 151684085
Change-Id: Iac6fd9e3b9f7bed3879620b4f262cc68f8ffdcd3
3 files changed, 6 insertions, 8 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastDispatcher.kt b/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastDispatcher.kt index e89c66e0137c..74b94e76dfc1 100644 --- a/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastDispatcher.kt +++ b/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastDispatcher.kt @@ -166,7 +166,7 @@ open class BroadcastDispatcher @Inject constructor ( @VisibleForTesting protected open fun createUBRForUser(userId: Int) = - UserBroadcastDispatcher(context, userId, mainHandler, bgLooper) + UserBroadcastDispatcher(context, userId, bgLooper) override fun dump(fd: FileDescriptor, pw: PrintWriter, args: Array<out String>) { pw.println("Broadcast dispatcher:") diff --git a/packages/SystemUI/src/com/android/systemui/broadcast/UserBroadcastDispatcher.kt b/packages/SystemUI/src/com/android/systemui/broadcast/UserBroadcastDispatcher.kt index 0c631aacab82..4e84f06f51a7 100644 --- a/packages/SystemUI/src/com/android/systemui/broadcast/UserBroadcastDispatcher.kt +++ b/packages/SystemUI/src/com/android/systemui/broadcast/UserBroadcastDispatcher.kt @@ -27,7 +27,6 @@ import android.os.UserHandle import android.util.ArrayMap import android.util.ArraySet import android.util.Log -import androidx.annotation.MainThread import androidx.annotation.VisibleForTesting import com.android.internal.util.Preconditions import com.android.systemui.Dumpable @@ -46,11 +45,13 @@ private const val DEBUG = false * * Created by [BroadcastDispatcher] as needed by users. The value of [userId] can be * [UserHandle.USER_ALL]. + * + * Each instance of this class will register itself exactly once with [Context]. Updates to the + * [IntentFilter] will be done in the background thread. */ class UserBroadcastDispatcher( private val context: Context, private val userId: Int, - private val mainHandler: Handler, private val bgLooper: Looper ) : BroadcastReceiver(), Dumpable { @@ -168,7 +169,7 @@ class UserBroadcastDispatcher( // Only call this from a BG thread private fun createFilterAndRegisterReceiverBG() { val intentFilter = createFilter() - mainHandler.post(RegisterReceiverRunnable(intentFilter)) + bgHandler.post(RegisterReceiverRunnable(intentFilter)) } override fun dump(fd: FileDescriptor, pw: PrintWriter, args: Array<out String>) { @@ -207,10 +208,7 @@ class UserBroadcastDispatcher( /* * Registers and unregisters the BroadcastReceiver - * - * Must be called from Main Thread */ - @MainThread override fun run() { if (registered.get()) { context.unregisterReceiver(this@UserBroadcastDispatcher) 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 7821ae29592e..847e442f1a49 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/broadcast/UserBroadcastDispatcherTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/broadcast/UserBroadcastDispatcherTest.kt @@ -91,7 +91,7 @@ class UserBroadcastDispatcherTest : SysuiTestCase() { fakeExecutor = FakeExecutor(FakeSystemClock()) userBroadcastDispatcher = UserBroadcastDispatcher( - mockContext, USER_ID, handler, testableLooper.looper) + mockContext, USER_ID, testableLooper.looper) userBroadcastDispatcher.pendingResult = mPendingResult } |