Refactor SimStatusDialogRepository

- Move data logic into repository for better testing
- Check carrier config first, if not shows some items, we don't need to
  load data
- Tests in SimStatusDialogControllerTest will be fixed in later cls

Bug: 337417520
Test: manual - on SIM status
Test: unit test
Change-Id: Ia0c32882f0b35ec9154b3da58ac6a7b98c879efc
This commit is contained in:
Chaohui Wang
2024-05-27 18:24:52 +08:00
parent 659e512b1c
commit 3b925a0cfe
5 changed files with 113 additions and 64 deletions

View File

@@ -0,0 +1,107 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.deviceinfo.simstatus
import android.content.Context
import android.telephony.CarrierConfigManager
import android.telephony.SubscriptionManager
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.android.settings.network.telephony.SimSlotRepository
import com.android.settings.network.telephony.ims.ImsMmTelRepository
import com.android.settings.network.telephony.ims.ImsMmTelRepositoryImpl
import com.android.settings.network.telephony.safeGetConfig
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
@OptIn(ExperimentalCoroutinesApi::class)
class SimStatusDialogRepository @JvmOverloads constructor(
private val context: Context,
private val simSlotRepository: SimSlotRepository = SimSlotRepository(context),
private val imsMmTelRepositoryFactory: (subId: Int) -> ImsMmTelRepository = { subId ->
ImsMmTelRepositoryImpl(context, subId)
},
) {
private val carrierConfigManager = context.getSystemService(CarrierConfigManager::class.java)!!
data class SimStatusDialogInfo(
val imsRegistered: Boolean? = null,
)
private data class SimStatusDialogVisibility(
val imsRegisteredShowUp: Boolean,
)
fun collectSimStatusDialogInfo(
lifecycleOwner: LifecycleOwner,
simSlotIndex: Int,
action: (info: SimStatusDialogInfo) -> Unit,
) {
lifecycleOwner.lifecycleScope.launch {
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
simStatusDialogInfoBySlotFlow(simSlotIndex).collect(action)
}
}
}
private fun simStatusDialogInfoBySlotFlow(simSlotIndex: Int): Flow<SimStatusDialogInfo> =
simSlotRepository.subIdInSimSlotFlow(simSlotIndex)
.flatMapLatest { subId ->
if (SubscriptionManager.isValidSubscriptionId(subId)) {
simStatusDialogInfoFlow(subId)
} else {
flowOf(SimStatusDialogInfo())
}
}
.conflate()
.flowOn(Dispatchers.Default)
private fun simStatusDialogInfoFlow(subId: Int): Flow<SimStatusDialogInfo> =
showUpFlow(subId).flatMapLatest { visibility ->
combine(
if (visibility.imsRegisteredShowUp) {
imsMmTelRepositoryFactory(subId).imsRegisteredFlow()
} else flowOf(null),
) { (imsRegistered) ->
SimStatusDialogInfo(imsRegistered = imsRegistered)
}
}
private fun showUpFlow(subId: Int) = flow {
val config = carrierConfigManager.safeGetConfig(
keys = listOf(CarrierConfigManager.KEY_SHOW_IMS_REGISTRATION_STATUS_BOOL),
subId = subId,
)
emit(
SimStatusDialogVisibility(
imsRegisteredShowUp = config.getBoolean(
CarrierConfigManager.KEY_SHOW_IMS_REGISTRATION_STATUS_BOOL
),
)
)
}
}