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.viewmodel 18 19 import androidx.annotation.VisibleForTesting 20 import com.android.systemui.dagger.SysUISingleton 21 import com.android.systemui.dagger.qualifiers.Application 22 import com.android.systemui.statusbar.phone.StatusBarLocation 23 import com.android.systemui.statusbar.pipeline.StatusBarPipelineFlags 24 import com.android.systemui.statusbar.pipeline.airplane.domain.interactor.AirplaneModeInteractor 25 import com.android.systemui.statusbar.pipeline.mobile.domain.interactor.MobileIconsInteractor 26 import com.android.systemui.statusbar.pipeline.mobile.ui.MobileViewLogger 27 import com.android.systemui.statusbar.pipeline.mobile.ui.VerboseMobileViewLogger 28 import com.android.systemui.statusbar.pipeline.mobile.ui.view.ModernStatusBarMobileView 29 import com.android.systemui.statusbar.pipeline.shared.ConnectivityConstants 30 import javax.inject.Inject 31 import kotlinx.coroutines.CoroutineScope 32 import kotlinx.coroutines.flow.StateFlow 33 import kotlinx.coroutines.launch 34 35 /** 36 * View model for describing the system's current mobile cellular connections. The result is a list 37 * of [MobileIconViewModel]s which describe the individual icons and can be bound to 38 * [ModernStatusBarMobileView] 39 */ 40 class MobileIconsViewModel 41 @Inject 42 constructor( 43 val subscriptionIdsFlow: StateFlow<List<Int>>, 44 val logger: MobileViewLogger, 45 private val verboseLogger: VerboseMobileViewLogger, 46 private val interactor: MobileIconsInteractor, 47 private val airplaneModeInteractor: AirplaneModeInteractor, 48 private val constants: ConnectivityConstants, 49 @Application private val scope: CoroutineScope, 50 private val statusBarPipelineFlags: StatusBarPipelineFlags, 51 ) { 52 @VisibleForTesting val mobileIconSubIdCache = mutableMapOf<Int, MobileIconViewModel>() 53 54 init { <lambda>null55 scope.launch { subscriptionIdsFlow.collect { removeInvalidModelsFromCache(it) } } 56 } 57 viewModelForSubnull58 fun viewModelForSub(subId: Int, location: StatusBarLocation): LocationBasedMobileViewModel { 59 val common = 60 mobileIconSubIdCache[subId] 61 ?: MobileIconViewModel( 62 subId, 63 interactor.createMobileConnectionInteractorForSubId(subId), 64 airplaneModeInteractor, 65 constants, 66 scope, 67 ) 68 .also { mobileIconSubIdCache[subId] = it } 69 70 return LocationBasedMobileViewModel.viewModelForLocation( 71 common, 72 statusBarPipelineFlags, 73 verboseLogger, 74 location, 75 ) 76 } 77 removeInvalidModelsFromCachenull78 private fun removeInvalidModelsFromCache(subIds: List<Int>) { 79 val subIdsToRemove = mobileIconSubIdCache.keys.filter { !subIds.contains(it) } 80 subIdsToRemove.forEach { mobileIconSubIdCache.remove(it) } 81 } 82 83 @SysUISingleton 84 class Factory 85 @Inject 86 constructor( 87 private val logger: MobileViewLogger, 88 private val verboseLogger: VerboseMobileViewLogger, 89 private val interactor: MobileIconsInteractor, 90 private val airplaneModeInteractor: AirplaneModeInteractor, 91 private val constants: ConnectivityConstants, 92 @Application private val scope: CoroutineScope, 93 private val statusBarPipelineFlags: StatusBarPipelineFlags, 94 ) { createnull95 fun create(subscriptionIdsFlow: StateFlow<List<Int>>): MobileIconsViewModel { 96 return MobileIconsViewModel( 97 subscriptionIdsFlow, 98 logger, 99 verboseLogger, 100 interactor, 101 airplaneModeInteractor, 102 constants, 103 scope, 104 statusBarPipelineFlags, 105 ) 106 } 107 } 108 } 109