Fix: WalletContextualLocationsService issuing binder calls on main thread

Bug: b/322506838
Test: Manual test
Flag: NA

Change-Id: I3904019a26c4bbca11543b27a3b8557639400fa0
diff --git a/packages/SystemUI/aconfig/systemui.aconfig b/packages/SystemUI/aconfig/systemui.aconfig
index 407873e..6ba138f 100644
--- a/packages/SystemUI/aconfig/systemui.aconfig
+++ b/packages/SystemUI/aconfig/systemui.aconfig
@@ -426,6 +426,16 @@
    description: "Decide whether to fetch the connected bluetooth device name outside a synchronized block."
    bug: "323995015"
    metadata {
-       purpose: PURPOSE_BUGFIX
-     }
+        purpose: PURPOSE_BUGFIX
+   }
+}
+
+flag {
+   name: "register_new_wallet_card_in_background"
+   namespace: "systemui"
+   description: "Decide whether the call to registerNewWalletCards method should be issued on background thread."
+   bug: "322506838"
+   metadata {
+        purpose: PURPOSE_BUGFIX
+   }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/wallet/controller/WalletContextualLocationsService.kt b/packages/SystemUI/src/com/android/systemui/wallet/controller/WalletContextualLocationsService.kt
index bb4a2c4..3c50c7b 100644
--- a/packages/SystemUI/src/com/android/systemui/wallet/controller/WalletContextualLocationsService.kt
+++ b/packages/SystemUI/src/com/android/systemui/wallet/controller/WalletContextualLocationsService.kt
@@ -6,9 +6,12 @@
 import androidx.annotation.VisibleForTesting
 import androidx.lifecycle.LifecycleService
 import androidx.lifecycle.lifecycleScope
+import com.android.systemui.Flags.registerNewWalletCardInBackground
+import com.android.systemui.dagger.qualifiers.Background
 import com.android.systemui.flags.FeatureFlags
 import com.android.systemui.flags.Flags
 import javax.inject.Inject
+import kotlinx.coroutines.CoroutineDispatcher
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.launch
 
@@ -21,6 +24,7 @@
 class WalletContextualLocationsService
 @Inject
 constructor(
+    @Background private val backgroundDispatcher: CoroutineDispatcher,
     private val controller: WalletContextualSuggestionsController,
     private val featureFlags: FeatureFlags,
 ) : LifecycleService() {
@@ -29,20 +33,30 @@
 
     @VisibleForTesting
     constructor(
+        dispatcher: CoroutineDispatcher,
         controller: WalletContextualSuggestionsController,
         featureFlags: FeatureFlags,
         scope: CoroutineScope,
-    ) : this(controller, featureFlags) {
+    ) : this(dispatcher, controller, featureFlags) {
         this.scope = scope
     }
-
     override fun onBind(intent: Intent): IBinder {
         super.onBind(intent)
-        scope.launch {
-            controller.allWalletCards.collect { cards ->
-                val cardsSize = cards.size
-                Log.i(TAG, "Number of cards registered $cardsSize")
-                listener?.registerNewWalletCards(cards)
+        if (registerNewWalletCardInBackground()) {
+            scope.launch(backgroundDispatcher) {
+                controller.allWalletCards.collect { cards ->
+                    val cardsSize = cards.size
+                    Log.i(TAG, "Number of cards registered $cardsSize")
+                    listener?.registerNewWalletCards(cards)
+                }
+            }
+        } else {
+            scope.launch {
+                controller.allWalletCards.collect { cards ->
+                    val cardsSize = cards.size
+                    Log.i(TAG, "Number of cards registered $cardsSize")
+                    listener?.registerNewWalletCards(cards)
+                }
             }
         }
         return binder
diff --git a/packages/SystemUI/tests/src/com/android/systemui/wallet/controller/WalletContextualLocationsServiceTest.kt b/packages/SystemUI/tests/src/com/android/systemui/wallet/controller/WalletContextualLocationsServiceTest.kt
index e6b4d06..8c5df6e 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/wallet/controller/WalletContextualLocationsServiceTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/wallet/controller/WalletContextualLocationsServiceTest.kt
@@ -14,6 +14,7 @@
 import com.google.common.truth.Truth.assertThat
 import kotlinx.coroutines.flow.MutableStateFlow
 import kotlinx.coroutines.flow.update
+import kotlinx.coroutines.test.StandardTestDispatcher
 import kotlinx.coroutines.test.TestScope
 import kotlinx.coroutines.test.runCurrent
 import kotlinx.coroutines.test.runTest
@@ -53,13 +54,17 @@
         doNothing().whenever(controller).setSuggestionCardIds(anySet())
 
         if (Looper.myLooper() == null) Looper.prepare()
-
+        val testDispatcher = StandardTestDispatcher()
         testScope = TestScope()
         featureFlags.set(Flags.ENABLE_WALLET_CONTEXTUAL_LOYALTY_CARDS, true)
         listenerRegisteredCount = 0
-
         underTest =
-            WalletContextualLocationsService(controller, featureFlags, testScope.backgroundScope)
+            WalletContextualLocationsService(
+                testDispatcher,
+                controller,
+                featureFlags,
+                testScope.backgroundScope
+            )
     }
 
     @Test