<lambda>null1 package com.android.systemui.wallet.controller
2 
3 import android.content.Intent
4 import android.os.IBinder
5 import android.util.Log
6 import androidx.annotation.VisibleForTesting
7 import androidx.lifecycle.LifecycleService
8 import androidx.lifecycle.lifecycleScope
9 import com.android.systemui.Flags.registerNewWalletCardInBackground
10 import com.android.systemui.dagger.qualifiers.Background
11 import com.android.systemui.flags.FeatureFlags
12 import com.android.systemui.flags.Flags
13 import javax.inject.Inject
14 import kotlinx.coroutines.CoroutineDispatcher
15 import kotlinx.coroutines.CoroutineScope
16 import kotlinx.coroutines.launch
17 
18 /**
19  * Serves as an intermediary between QuickAccessWalletService and ContextualCardManager (in PCC).
20  * When QuickAccessWalletService has a list of store locations, WalletContextualLocationsService
21  * will send them to ContextualCardManager. When the user enters a store location, this Service
22  * class will be notified, and WalletContextualSuggestionsController will be updated.
23  */
24 class WalletContextualLocationsService
25 @Inject
26 constructor(
27     @Background private val backgroundDispatcher: CoroutineDispatcher,
28     private val controller: WalletContextualSuggestionsController,
29     private val featureFlags: FeatureFlags,
30 ) : LifecycleService() {
31     private var listener: IWalletCardsUpdatedListener? = null
32     private var scope: CoroutineScope = this.lifecycleScope
33 
34     @VisibleForTesting
35     constructor(
36         dispatcher: CoroutineDispatcher,
37         controller: WalletContextualSuggestionsController,
38         featureFlags: FeatureFlags,
39         scope: CoroutineScope,
40     ) : this(dispatcher, controller, featureFlags) {
41         this.scope = scope
42     }
43     override fun onBind(intent: Intent): IBinder {
44         super.onBind(intent)
45         if (registerNewWalletCardInBackground()) {
46             scope.launch(backgroundDispatcher) {
47                 controller.allWalletCards.collect { cards ->
48                     val cardsSize = cards.size
49                     Log.i(TAG, "Number of cards registered $cardsSize")
50                     listener?.registerNewWalletCards(cards)
51                 }
52             }
53         } else {
54             scope.launch {
55                 controller.allWalletCards.collect { cards ->
56                     val cardsSize = cards.size
57                     Log.i(TAG, "Number of cards registered $cardsSize")
58                     listener?.registerNewWalletCards(cards)
59                 }
60             }
61         }
62         return binder
63     }
64 
65     override fun onDestroy() {
66         super.onDestroy()
67         listener = null
68     }
69 
70     @VisibleForTesting
71     fun addWalletCardsUpdatedListenerInternal(listener: IWalletCardsUpdatedListener) {
72         if (!featureFlags.isEnabled(Flags.ENABLE_WALLET_CONTEXTUAL_LOYALTY_CARDS)) {
73             return
74         }
75         this.listener = listener // Currently, only one listener at a time is supported
76         // Sends WalletCard objects from QuickAccessWalletService to the listener
77         val cards = controller.allWalletCards.value
78         if (!cards.isEmpty()) {
79             val cardsSize = cards.size
80             Log.i(TAG, "Number of cards registered $cardsSize")
81             listener.registerNewWalletCards(cards)
82         }
83     }
84 
85     @VisibleForTesting
86     fun onWalletContextualLocationsStateUpdatedInternal(storeLocations: List<String>) {
87         if (!featureFlags.isEnabled(Flags.ENABLE_WALLET_CONTEXTUAL_LOYALTY_CARDS)) {
88             return
89         }
90         Log.i(TAG, "Entered store $storeLocations")
91         controller.setSuggestionCardIds(storeLocations.toSet())
92     }
93 
94     private val binder: IWalletContextualLocationsService.Stub =
95         object : IWalletContextualLocationsService.Stub() {
96             override fun addWalletCardsUpdatedListener(listener: IWalletCardsUpdatedListener) {
97                 addWalletCardsUpdatedListenerInternal(listener)
98             }
99             override fun onWalletContextualLocationsStateUpdated(storeLocations: List<String>) {
100                 onWalletContextualLocationsStateUpdatedInternal(storeLocations)
101             }
102         }
103 
104     companion object {
105         private const val TAG = "WalletContextualLocationsService"
106     }
107 }
108