1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.statusbar.pipeline.mobile.ui
18 
19 import com.android.systemui.CoreStartable
20 import com.android.systemui.dagger.SysUISingleton
21 import com.android.systemui.dagger.qualifiers.Application
22 import com.android.systemui.shade.carrier.ShadeCarrierGroupController
23 import com.android.systemui.statusbar.phone.ui.StatusBarIconController
24 import com.android.systemui.statusbar.pipeline.mobile.domain.interactor.MobileIconsInteractor
25 import com.android.systemui.statusbar.pipeline.mobile.ui.viewmodel.MobileIconsViewModel
26 import java.io.PrintWriter
27 import javax.inject.Inject
28 import kotlinx.coroutines.CoroutineScope
29 import kotlinx.coroutines.flow.collectLatest
30 import kotlinx.coroutines.launch
31 
32 /**
33  * This class is intended to provide a context to collect on the
34  * [MobileIconsInteractor.filteredSubscriptions] data source and supply a state flow that can
35  * control [StatusBarIconController] to keep the old UI in sync with the new data source.
36  *
37  * It also provides a mechanism to create a top-level view model for each IconManager to know about
38  * the list of available mobile lines of service for which we want to show icons.
39  */
40 @Suppress("EXPERIMENTAL_IS_NOT_ENABLED")
41 @SysUISingleton
42 class MobileUiAdapter
43 @Inject
44 constructor(
45     private val iconController: StatusBarIconController,
46     val mobileIconsViewModel: MobileIconsViewModel,
47     private val logger: MobileViewLogger,
48     @Application private val scope: CoroutineScope,
49 ) : CoreStartable {
50     private var isCollecting: Boolean = false
51     private var lastValue: List<Int>? = null
52 
53     private var shadeCarrierGroupController: ShadeCarrierGroupController? = null
54 
startnull55     override fun start() {
56         // Start notifying the icon controller of subscriptions
57         scope.launch {
58             isCollecting = true
59             mobileIconsViewModel.subscriptionIdsFlow.collectLatest {
60                 logger.logUiAdapterSubIdsSentToIconController(it)
61                 lastValue = it
62                 iconController.setNewMobileIconSubIds(it)
63                 shadeCarrierGroupController?.updateModernMobileIcons(it)
64             }
65         }
66     }
67 
68     /** Set the [ShadeCarrierGroupController] to notify of subscription updates */
setShadeCarrierGroupControllernull69     fun setShadeCarrierGroupController(controller: ShadeCarrierGroupController) {
70         shadeCarrierGroupController = controller
71     }
72 
dumpnull73     override fun dump(pw: PrintWriter, args: Array<out String>) {
74         pw.println("isCollecting=$isCollecting")
75         pw.println("Last values sent to icon controller: $lastValue")
76     }
77 }
78