1 /* <lambda>null2 * Copyright (C) 2024 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.settings.deviceinfo.simstatus 18 19 import android.content.Context 20 import android.telephony.CarrierConfigManager 21 import android.telephony.SubscriptionManager 22 import androidx.lifecycle.Lifecycle 23 import androidx.lifecycle.LifecycleOwner 24 import androidx.lifecycle.lifecycleScope 25 import androidx.lifecycle.repeatOnLifecycle 26 import com.android.settings.network.telephony.SimSlotRepository 27 import com.android.settings.network.telephony.ims.ImsMmTelRepository 28 import com.android.settings.network.telephony.ims.ImsMmTelRepositoryImpl 29 import com.android.settings.network.telephony.safeGetConfig 30 import kotlinx.coroutines.Dispatchers 31 import kotlinx.coroutines.ExperimentalCoroutinesApi 32 import kotlinx.coroutines.flow.Flow 33 import kotlinx.coroutines.flow.combine 34 import kotlinx.coroutines.flow.conflate 35 import kotlinx.coroutines.flow.flatMapLatest 36 import kotlinx.coroutines.flow.flow 37 import kotlinx.coroutines.flow.flowOf 38 import kotlinx.coroutines.flow.flowOn 39 import kotlinx.coroutines.launch 40 41 @OptIn(ExperimentalCoroutinesApi::class) 42 class SimStatusDialogRepository @JvmOverloads constructor( 43 private val context: Context, 44 private val simSlotRepository: SimSlotRepository = SimSlotRepository(context), 45 private val signalStrengthRepository: SignalStrengthRepository = 46 SignalStrengthRepository(context), 47 private val imsMmTelRepositoryFactory: (subId: Int) -> ImsMmTelRepository = { subId -> 48 ImsMmTelRepositoryImpl(context, subId) 49 }, 50 ) { 51 private val carrierConfigManager = context.getSystemService(CarrierConfigManager::class.java)!! 52 53 data class SimStatusDialogInfo( 54 val signalStrength: String? = null, 55 val imsRegistered: Boolean? = null, 56 ) 57 58 private data class SimStatusDialogVisibility( 59 val signalStrengthShowUp: Boolean, 60 val imsRegisteredShowUp: Boolean, 61 ) 62 collectSimStatusDialogInfonull63 fun collectSimStatusDialogInfo( 64 lifecycleOwner: LifecycleOwner, 65 simSlotIndex: Int, 66 action: (info: SimStatusDialogInfo) -> Unit, 67 ) { 68 lifecycleOwner.lifecycleScope.launch { 69 lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { 70 simStatusDialogInfoBySlotFlow(simSlotIndex).collect(action) 71 } 72 } 73 } 74 simStatusDialogInfoBySlotFlownull75 private fun simStatusDialogInfoBySlotFlow(simSlotIndex: Int): Flow<SimStatusDialogInfo> = 76 simSlotRepository.subIdInSimSlotFlow(simSlotIndex) 77 .flatMapLatest { subId -> 78 if (SubscriptionManager.isValidSubscriptionId(subId)) { 79 simStatusDialogInfoFlow(subId) 80 } else { 81 flowOf(SimStatusDialogInfo()) 82 } 83 } 84 .conflate() 85 .flowOn(Dispatchers.Default) 86 simStatusDialogInfoFlownull87 private fun simStatusDialogInfoFlow(subId: Int): Flow<SimStatusDialogInfo> = 88 showUpFlow(subId).flatMapLatest { visibility -> 89 combine( 90 if (visibility.signalStrengthShowUp) { 91 signalStrengthRepository.signalStrengthDisplayFlow(subId) 92 } else flowOf(null), 93 if (visibility.imsRegisteredShowUp) { 94 imsMmTelRepositoryFactory(subId).imsRegisteredFlow() 95 } else flowOf(null), 96 ) { signalStrength, imsRegistered -> 97 SimStatusDialogInfo(signalStrength = signalStrength, imsRegistered = imsRegistered) 98 } 99 } 100 <lambda>null101 private fun showUpFlow(subId: Int) = flow { 102 val config = carrierConfigManager.safeGetConfig( 103 keys = listOf( 104 CarrierConfigManager.KEY_SHOW_SIGNAL_STRENGTH_IN_SIM_STATUS_BOOL, 105 CarrierConfigManager.KEY_SHOW_IMS_REGISTRATION_STATUS_BOOL, 106 ), 107 subId = subId, 108 ) 109 val visibility = SimStatusDialogVisibility( 110 signalStrengthShowUp = config.getBoolean( 111 CarrierConfigManager.KEY_SHOW_SIGNAL_STRENGTH_IN_SIM_STATUS_BOOL, 112 true, // by default we show the signal strength in sim status 113 ), 114 imsRegisteredShowUp = config.getBoolean( 115 CarrierConfigManager.KEY_SHOW_IMS_REGISTRATION_STATUS_BOOL 116 ), 117 ) 118 emit(visibility) 119 } 120 } 121