• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.CarrierConfigRepository
27 import com.android.settings.network.telephony.SimSlotRepository
28 import com.android.settings.network.telephony.ims.ImsMmTelRepository
29 import com.android.settings.network.telephony.ims.ImsMmTelRepositoryImpl
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
43 @JvmOverloads
44 constructor(
45     private val context: Context,
46     private val simSlotRepository: SimSlotRepository = SimSlotRepository(context),
47     private val signalStrengthRepository: SignalStrengthRepository =
48         SignalStrengthRepository(context),
49     private val imsMmTelRepositoryFactory: (subId: Int) -> ImsMmTelRepository = { subId ->
50         ImsMmTelRepositoryImpl(context, subId)
51     },
52 ) {
53     private val carrierConfigRepository = CarrierConfigRepository(context)
54 
55     data class SimStatusDialogInfo(
56         val signalStrength: String? = null,
57         val imsRegistered: Boolean? = null,
58     )
59 
60     private data class SimStatusDialogVisibility(
61         val signalStrengthShowUp: Boolean,
62         val imsRegisteredShowUp: Boolean,
63     )
64 
collectSimStatusDialogInfonull65     fun collectSimStatusDialogInfo(
66         lifecycleOwner: LifecycleOwner,
67         simSlotIndex: Int,
68         action: (info: SimStatusDialogInfo) -> Unit,
69     ) {
70         lifecycleOwner.lifecycleScope.launch {
71             lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
72                 simStatusDialogInfoBySlotFlow(simSlotIndex).collect(action)
73             }
74         }
75     }
76 
simStatusDialogInfoBySlotFlownull77     private fun simStatusDialogInfoBySlotFlow(simSlotIndex: Int): Flow<SimStatusDialogInfo> =
78         simSlotRepository
79             .subIdInSimSlotFlow(simSlotIndex)
80             .flatMapLatest { subId ->
81                 if (SubscriptionManager.isValidSubscriptionId(subId)) {
82                     simStatusDialogInfoFlow(subId)
83                 } else {
84                     flowOf(SimStatusDialogInfo())
85                 }
86             }
87             .conflate()
88             .flowOn(Dispatchers.Default)
89 
simStatusDialogInfoFlownull90     private fun simStatusDialogInfoFlow(subId: Int): Flow<SimStatusDialogInfo> =
91         showUpFlow(subId).flatMapLatest { visibility ->
92             combine(
93                 if (visibility.signalStrengthShowUp) {
94                     signalStrengthRepository.signalStrengthDisplayFlow(subId)
95                 } else flowOf(null),
96                 if (visibility.imsRegisteredShowUp) {
97                     imsMmTelRepositoryFactory(subId).imsRegisteredFlow()
98                 } else flowOf(null),
99             ) { signalStrength, imsRegistered ->
100                 SimStatusDialogInfo(signalStrength = signalStrength, imsRegistered = imsRegistered)
101             }
102         }
103 
<lambda>null104     private fun showUpFlow(subId: Int) = flow {
105         val visibility =
106             carrierConfigRepository.transformConfig(subId) {
107                 SimStatusDialogVisibility(
108                     signalStrengthShowUp =
109                         getBoolean(
110                             CarrierConfigManager.KEY_SHOW_SIGNAL_STRENGTH_IN_SIM_STATUS_BOOL),
111                     imsRegisteredShowUp =
112                         getBoolean(CarrierConfigManager.KEY_SHOW_IMS_REGISTRATION_STATUS_BOOL),
113                 )
114             }
115         emit(visibility)
116     }
117 }
118