1 /* <lambda>null2 * 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.wifi.ui 18 19 import android.view.ViewGroup 20 import androidx.lifecycle.Lifecycle 21 import androidx.lifecycle.repeatOnLifecycle 22 import com.android.systemui.dagger.SysUISingleton 23 import com.android.systemui.lifecycle.repeatWhenAttached 24 import com.android.systemui.statusbar.phone.StatusBarIconController 25 import com.android.systemui.statusbar.phone.StatusBarLocation 26 import com.android.systemui.statusbar.pipeline.StatusBarPipelineFlags 27 import com.android.systemui.statusbar.pipeline.wifi.ui.model.WifiIcon 28 import com.android.systemui.statusbar.pipeline.wifi.ui.viewmodel.LocationBasedWifiViewModel 29 import com.android.systemui.statusbar.pipeline.wifi.ui.viewmodel.WifiViewModel 30 import javax.inject.Inject 31 import kotlinx.coroutines.flow.collect 32 import kotlinx.coroutines.launch 33 34 /** 35 * This class serves as a bridge between the old UI classes and the new data pipeline. 36 * 37 * Once the new pipeline notifies [wifiViewModel] that the wifi icon should be visible, this class 38 * notifies [iconController] to inflate the wifi icon (if needed). After that, the [wifiViewModel] 39 * has sole responsibility for updating the wifi icon drawable, visibility, etc. and the 40 * [iconController] will not do any updates to the icon. 41 */ 42 @SysUISingleton 43 class WifiUiAdapter 44 @Inject 45 constructor( 46 private val iconController: StatusBarIconController, 47 private val wifiViewModel: WifiViewModel, 48 private val statusBarPipelineFlags: StatusBarPipelineFlags, 49 ) { 50 /** 51 * Binds the container for all the status bar icons to a view model, so that we inflate the wifi 52 * view once we receive a valid icon from the data pipeline. 53 * 54 * NOTE: This should go away as we better integrate the data pipeline with the UI. 55 * 56 * @return the view model used for this particular group in the given [location]. 57 */ 58 fun bindGroup( 59 statusBarIconGroup: ViewGroup, 60 location: StatusBarLocation, 61 ): LocationBasedWifiViewModel { 62 val locationViewModel = 63 when (location) { 64 StatusBarLocation.HOME -> wifiViewModel.home 65 StatusBarLocation.KEYGUARD -> wifiViewModel.keyguard 66 StatusBarLocation.QS -> wifiViewModel.qs 67 } 68 69 statusBarIconGroup.repeatWhenAttached { 70 repeatOnLifecycle(Lifecycle.State.STARTED) { 71 launch { 72 locationViewModel.wifiIcon.collect { wifiIcon -> 73 // Only notify the icon controller if we want to *render* the new icon. 74 // Note that this flow may still run if 75 // [statusBarPipelineFlags.runNewWifiIconBackend] is true because we may 76 // want to get the logging data without rendering. 77 if ( 78 wifiIcon is WifiIcon.Visible && statusBarPipelineFlags.useNewWifiIcon() 79 ) { 80 iconController.setNewWifiIcon() 81 } 82 } 83 } 84 } 85 } 86 87 return locationViewModel 88 } 89 } 90