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.network.telephony.wificalling
18 
19 import android.content.Context
20 import android.telephony.AccessNetworkConstants
21 import android.telephony.CarrierConfigManager
22 import android.telephony.CarrierConfigManager.KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL
23 import android.telephony.SubscriptionManager
24 import android.telephony.TelephonyManager
25 import android.telephony.ims.ImsMmTelManager.WiFiCallingMode
26 import android.telephony.ims.feature.MmTelFeature
27 import android.telephony.ims.stub.ImsRegistrationImplBase
28 import com.android.settings.network.telephony.ims.ImsMmTelRepository
29 import com.android.settings.network.telephony.ims.ImsMmTelRepositoryImpl
30 import com.android.settings.network.telephony.ims.imsFeatureProvisionedFlow
31 import com.android.settings.network.telephony.subscriptionsChangedFlow
32 import kotlinx.coroutines.Dispatchers
33 import kotlinx.coroutines.ExperimentalCoroutinesApi
34 import kotlinx.coroutines.flow.Flow
35 import kotlinx.coroutines.flow.combine
36 import kotlinx.coroutines.flow.flatMapLatest
37 import kotlinx.coroutines.flow.flowOf
38 import kotlinx.coroutines.flow.map
39 import kotlinx.coroutines.withContext
40 
41 class WifiCallingRepository(
42     private val context: Context,
43     private val subId: Int,
44     private val imsMmTelRepository: ImsMmTelRepository = ImsMmTelRepositoryImpl(context, subId)
45 ) {
46     private val telephonyManager = context.getSystemService(TelephonyManager::class.java)!!
47         .createForSubscriptionId(subId)
48 
49     private val carrierConfigManager = context.getSystemService(CarrierConfigManager::class.java)!!
50 
51     @WiFiCallingMode
52     fun getWiFiCallingMode(): Int {
53         val useRoamingMode = telephonyManager.isNetworkRoaming && !useWfcHomeModeForRoaming()
54         return imsMmTelRepository.getWiFiCallingMode(useRoamingMode)
55     }
56 
57     private fun useWfcHomeModeForRoaming(): Boolean =
58         carrierConfigManager
59             .getConfigForSubId(subId, KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL)
60             .getBoolean(KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL)
61 
62     @OptIn(ExperimentalCoroutinesApi::class)
63     fun wifiCallingReadyFlow(): Flow<Boolean> {
64         if (!SubscriptionManager.isValidSubscriptionId(subId)) return flowOf(false)
65         return context.subscriptionsChangedFlow().flatMapLatest {
66             combine(
67                 imsFeatureProvisionedFlow(
68                     subId = subId,
69                     capability = MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE,
70                     tech = ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN,
71                 ),
72                 isWifiCallingSupportedFlow(),
73             ) { imsFeatureProvisioned, isWifiCallingSupported ->
74                 imsFeatureProvisioned && isWifiCallingSupported
75             }
76         }
77     }
78 
79     private fun isWifiCallingSupportedFlow(): Flow<Boolean> {
80         return imsMmTelRepository.imsReadyFlow().map { imsReady ->
81             imsReady && isWifiCallingSupported()
82         }
83     }
84 
85     suspend fun isWifiCallingSupported(): Boolean = withContext(Dispatchers.Default) {
86         imsMmTelRepository.isSupported(
87             capability = MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE,
88             transportType = AccessNetworkConstants.TRANSPORT_TYPE_WLAN,
89         )
90     }
91 }
92