Create WifiCallingRepository

Move Wi-Fi calling related feature into it.

Which simplifies the ImsMmTelRepository, so we can put more
ImsMmTelManager related features into ImsMmTelRepository in the future
cl.

Bug: 325414275
Test: manual - on Mobile Settings
Test: unit tests
Change-Id: I391f43cfa3a79e0c44865050c357ac54346c2fb1
diff --git a/src/com/android/settings/network/telephony/WifiCallingPreferenceController.kt b/src/com/android/settings/network/telephony/WifiCallingPreferenceController.kt
index e7b8318..698341c 100644
--- a/src/com/android/settings/network/telephony/WifiCallingPreferenceController.kt
+++ b/src/com/android/settings/network/telephony/WifiCallingPreferenceController.kt
@@ -29,8 +29,7 @@
 import androidx.preference.Preference
 import androidx.preference.PreferenceScreen
 import com.android.settings.R
-import com.android.settings.network.telephony.ims.ImsMmTelRepository
-import com.android.settings.network.telephony.ims.ImsMmTelRepositoryImpl
+import com.android.settings.network.telephony.wificalling.WifiCallingRepository
 import com.android.settingslib.spa.framework.util.collectLatestWithLifecycle
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.flow.Flow
@@ -46,8 +45,8 @@
     context: Context,
     key: String,
     private val callStateFlowFactory: (subId: Int) -> Flow<Int> = context::callStateFlow,
-    private val imsMmTelRepositoryFactory: (subId: Int) -> ImsMmTelRepository = { subId ->
-        ImsMmTelRepositoryImpl(context, subId)
+    private val wifiCallingRepository: (subId: Int) -> WifiCallingRepository = { subId ->
+        WifiCallingRepository(context, subId)
     },
 ) : TelephonyBasePreferenceController(context, key) {
 
@@ -123,7 +122,7 @@
     }
 
     private fun getSummaryForWfcMode(): String {
-        val resId = when (imsMmTelRepositoryFactory(mSubId).getWiFiCallingMode()) {
+        val resId = when (wifiCallingRepository(mSubId).getWiFiCallingMode()) {
             ImsMmTelManager.WIFI_MODE_WIFI_ONLY ->
                 com.android.internal.R.string.wfc_mode_wifi_only_summary
 
diff --git a/src/com/android/settings/network/telephony/ims/ImsMmTelRepository.kt b/src/com/android/settings/network/telephony/ims/ImsMmTelRepository.kt
index 3408eb7..1d288d4 100644
--- a/src/com/android/settings/network/telephony/ims/ImsMmTelRepository.kt
+++ b/src/com/android/settings/network/telephony/ims/ImsMmTelRepository.kt
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2023 The Android Open Source Project
+ * 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.
@@ -17,9 +17,6 @@
 package com.android.settings.network.telephony.ims
 
 import android.content.Context
-import android.telephony.CarrierConfigManager
-import android.telephony.CarrierConfigManager.KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL
-import android.telephony.TelephonyManager
 import android.telephony.ims.ImsManager
 import android.telephony.ims.ImsMmTelManager
 import android.telephony.ims.ImsMmTelManager.WiFiCallingMode
@@ -27,7 +24,7 @@
 
 interface ImsMmTelRepository {
     @WiFiCallingMode
-    fun getWiFiCallingMode(): Int
+    fun getWiFiCallingMode(useRoamingMode: Boolean): Int
 }
 
 class ImsMmTelRepositoryImpl(
@@ -36,31 +33,18 @@
     private val imsMmTelManager: ImsMmTelManager = ImsManager(context).getImsMmTelManager(subId),
 ) : ImsMmTelRepository {
 
-    private val telephonyManager = context.getSystemService(TelephonyManager::class.java)!!
-        .createForSubscriptionId(subId)
-
-    private val carrierConfigManager = context.getSystemService(CarrierConfigManager::class.java)!!
-
     @WiFiCallingMode
-    override fun getWiFiCallingMode(): Int = try {
+    override fun getWiFiCallingMode(useRoamingMode: Boolean): Int = try {
         when {
             !imsMmTelManager.isVoWiFiSettingEnabled -> ImsMmTelManager.WIFI_MODE_UNKNOWN
-
-            telephonyManager.isNetworkRoaming && !useWfcHomeModeForRoaming() ->
-                imsMmTelManager.getVoWiFiRoamingModeSetting()
-
+            useRoamingMode -> imsMmTelManager.getVoWiFiRoamingModeSetting()
             else -> imsMmTelManager.getVoWiFiModeSetting()
         }
     } catch (e: IllegalArgumentException) {
-        Log.w(TAG, "getWiFiCallingMode failed subId=$subId", e)
+        Log.w(TAG, "[$subId] getWiFiCallingMode failed useRoamingMode=$useRoamingMode", e)
         ImsMmTelManager.WIFI_MODE_UNKNOWN
     }
 
-    private fun useWfcHomeModeForRoaming(): Boolean =
-        carrierConfigManager
-            .getConfigForSubId(subId, KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL)
-            .getBoolean(KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL)
-
     private companion object {
         private const val TAG = "ImsMmTelRepository"
     }
diff --git a/src/com/android/settings/network/telephony/wificalling/WifiCallingRepository.kt b/src/com/android/settings/network/telephony/wificalling/WifiCallingRepository.kt
new file mode 100644
index 0000000..3d841d5
--- /dev/null
+++ b/src/com/android/settings/network/telephony/wificalling/WifiCallingRepository.kt
@@ -0,0 +1,47 @@
+/*
+ * 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.network.telephony.wificalling
+
+import android.content.Context
+import android.telephony.CarrierConfigManager
+import android.telephony.CarrierConfigManager.KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL
+import android.telephony.TelephonyManager
+import android.telephony.ims.ImsMmTelManager.WiFiCallingMode
+import com.android.settings.network.telephony.ims.ImsMmTelRepository
+import com.android.settings.network.telephony.ims.ImsMmTelRepositoryImpl
+
+class WifiCallingRepository(
+    private val context: Context,
+    private val subId: Int,
+    private val imsMmTelRepository : ImsMmTelRepository = ImsMmTelRepositoryImpl(context, subId)
+) {
+    private val telephonyManager = context.getSystemService(TelephonyManager::class.java)!!
+        .createForSubscriptionId(subId)
+
+    private val carrierConfigManager = context.getSystemService(CarrierConfigManager::class.java)!!
+
+    @WiFiCallingMode
+    fun getWiFiCallingMode(): Int {
+        val useRoamingMode = telephonyManager.isNetworkRoaming && !useWfcHomeModeForRoaming()
+        return imsMmTelRepository.getWiFiCallingMode(useRoamingMode)
+    }
+
+    private fun useWfcHomeModeForRoaming(): Boolean =
+        carrierConfigManager
+            .getConfigForSubId(subId, KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL)
+            .getBoolean(KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL)
+}
diff --git a/tests/spa_unit/src/com/android/settings/network/telephony/WifiCallingPreferenceControllerTest.kt b/tests/spa_unit/src/com/android/settings/network/telephony/WifiCallingPreferenceControllerTest.kt
index fc53049..f947f81 100644
--- a/tests/spa_unit/src/com/android/settings/network/telephony/WifiCallingPreferenceControllerTest.kt
+++ b/tests/spa_unit/src/com/android/settings/network/telephony/WifiCallingPreferenceControllerTest.kt
@@ -29,7 +29,7 @@
 import androidx.preference.PreferenceManager
 import androidx.test.core.app.ApplicationProvider
 import androidx.test.ext.junit.runners.AndroidJUnit4
-import com.android.settings.network.telephony.ims.ImsMmTelRepository
+import com.android.settings.network.telephony.wificalling.WifiCallingRepository
 import com.google.common.truth.Truth.assertThat
 import kotlinx.coroutines.delay
 import kotlinx.coroutines.flow.flowOf
@@ -60,9 +60,8 @@
 
     private var callState = TelephonyManager.CALL_STATE_IDLE
 
-    private object FakeImsMmTelRepository : ImsMmTelRepository {
-        var wiFiMode = ImsMmTelManager.WIFI_MODE_UNKNOWN
-        override fun getWiFiCallingMode() = wiFiMode
+    private val mockWifiCallingRepository = mock<WifiCallingRepository> {
+        on { getWiFiCallingMode() } doReturn ImsMmTelManager.WIFI_MODE_UNKNOWN
     }
 
     private val callingPreferenceCategoryController =
@@ -72,7 +71,7 @@
         context = context,
         key = TEST_KEY,
         callStateFlowFactory = { flowOf(callState) },
-        imsMmTelRepositoryFactory = { FakeImsMmTelRepository },
+        wifiCallingRepository = { mockWifiCallingRepository },
     ).init(subId = SUB_ID, callingPreferenceCategoryController)
 
     @Before
@@ -86,7 +85,9 @@
         mockTelecomManager.stub {
             on { getSimCallManagerForSubscription(SUB_ID) } doReturn null
         }
-        FakeImsMmTelRepository.wiFiMode = ImsMmTelManager.WIFI_MODE_WIFI_ONLY
+        mockWifiCallingRepository.stub {
+            on { getWiFiCallingMode() } doReturn ImsMmTelManager.WIFI_MODE_WIFI_ONLY
+        }
 
         controller.onViewCreated(TestLifecycleOwner())
         delay(100)
diff --git a/tests/spa_unit/src/com/android/settings/network/telephony/ims/ImsMmTelRepositoryTest.kt b/tests/spa_unit/src/com/android/settings/network/telephony/ims/ImsMmTelRepositoryTest.kt
index d5142fa..106a82f 100644
--- a/tests/spa_unit/src/com/android/settings/network/telephony/ims/ImsMmTelRepositoryTest.kt
+++ b/tests/spa_unit/src/com/android/settings/network/telephony/ims/ImsMmTelRepositoryTest.kt
@@ -17,11 +17,7 @@
 package com.android.settings.network.telephony.ims
 
 import android.content.Context
-import android.telephony.CarrierConfigManager
-import android.telephony.CarrierConfigManager.KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL
-import android.telephony.TelephonyManager
 import android.telephony.ims.ImsMmTelManager
-import androidx.core.os.persistableBundleOf
 import androidx.test.core.app.ApplicationProvider
 import androidx.test.ext.junit.runners.AndroidJUnit4
 import com.google.common.truth.Truth.assertThat
@@ -30,21 +26,11 @@
 import org.mockito.kotlin.doReturn
 import org.mockito.kotlin.doThrow
 import org.mockito.kotlin.mock
-import org.mockito.kotlin.spy
 import org.mockito.kotlin.stub
 
 @RunWith(AndroidJUnit4::class)
 class ImsMmTelRepositoryTest {
-    private val mockTelephonyManager = mock<TelephonyManager> {
-        on { createForSubscriptionId(SUB_ID) } doReturn mock
-    }
-
-    private val mockCarrierConfigManager = mock<CarrierConfigManager>()
-
-    private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
-        on { getSystemService(TelephonyManager::class.java) } doReturn mockTelephonyManager
-        on { getSystemService(CarrierConfigManager::class.java) } doReturn mockCarrierConfigManager
-    }
+    private val context: Context = ApplicationProvider.getApplicationContext()
 
     private val mockImsMmTelManager = mock<ImsMmTelManager> {
         on { isVoWiFiSettingEnabled } doReturn true
@@ -60,42 +46,21 @@
             on { isVoWiFiSettingEnabled } doReturn false
         }
 
-        val wiFiCallingMode = repository.getWiFiCallingMode()
+        val wiFiCallingMode = repository.getWiFiCallingMode(false)
 
         assertThat(wiFiCallingMode).isEqualTo(ImsMmTelManager.WIFI_MODE_UNKNOWN)
     }
 
     @Test
-    fun getWiFiCallingMode_roamingAndNotUseWfcHomeModeForRoaming_returnRoamingSetting() {
-        mockTelephonyManager.stub {
-            on { isNetworkRoaming } doReturn true
-        }
-        mockUseWfcHomeModeForRoaming(false)
-
-        val wiFiCallingMode = repository.getWiFiCallingMode()
+    fun getWiFiCallingMode_useRoamingMode_returnRoamingSetting() {
+        val wiFiCallingMode = repository.getWiFiCallingMode(true)
 
         assertThat(wiFiCallingMode).isEqualTo(mockImsMmTelManager.getVoWiFiRoamingModeSetting())
     }
 
     @Test
-    fun getWiFiCallingMode_roamingAndUseWfcHomeModeForRoaming_returnHomeSetting() {
-        mockTelephonyManager.stub {
-            on { isNetworkRoaming } doReturn true
-        }
-        mockUseWfcHomeModeForRoaming(true)
-
-        val wiFiCallingMode = repository.getWiFiCallingMode()
-
-        assertThat(wiFiCallingMode).isEqualTo(mockImsMmTelManager.getVoWiFiModeSetting())
-    }
-
-    @Test
-    fun getWiFiCallingMode_notRoaming_returnHomeSetting() {
-        mockTelephonyManager.stub {
-            on { isNetworkRoaming } doReturn false
-        }
-
-        val wiFiCallingMode = repository.getWiFiCallingMode()
+    fun getWiFiCallingMode_notSseRoamingMode_returnHomeSetting() {
+        val wiFiCallingMode = repository.getWiFiCallingMode(false)
 
         assertThat(wiFiCallingMode).isEqualTo(mockImsMmTelManager.getVoWiFiModeSetting())
     }
@@ -106,21 +71,11 @@
             on { isVoWiFiSettingEnabled } doThrow IllegalArgumentException()
         }
 
-        val wiFiCallingMode = repository.getWiFiCallingMode()
+        val wiFiCallingMode = repository.getWiFiCallingMode(false)
 
         assertThat(wiFiCallingMode).isEqualTo(ImsMmTelManager.WIFI_MODE_UNKNOWN)
     }
 
-    private fun mockUseWfcHomeModeForRoaming(config: Boolean) {
-        mockCarrierConfigManager.stub {
-            on {
-                getConfigForSubId(SUB_ID, KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL)
-            } doReturn persistableBundleOf(
-                KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL to config,
-            )
-        }
-    }
-
     private companion object {
         const val SUB_ID = 1
     }
diff --git a/tests/spa_unit/src/com/android/settings/network/telephony/wificalling/WifiCallingRepositoryTest.kt b/tests/spa_unit/src/com/android/settings/network/telephony/wificalling/WifiCallingRepositoryTest.kt
new file mode 100644
index 0000000..1f3acc2
--- /dev/null
+++ b/tests/spa_unit/src/com/android/settings/network/telephony/wificalling/WifiCallingRepositoryTest.kt
@@ -0,0 +1,114 @@
+/*
+ * 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.network.telephony.wificalling
+
+import android.content.Context
+import android.telephony.CarrierConfigManager
+import android.telephony.CarrierConfigManager.KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL
+import android.telephony.TelephonyManager
+import android.telephony.ims.ImsMmTelManager
+import androidx.core.os.persistableBundleOf
+import androidx.test.core.app.ApplicationProvider
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import com.android.settings.network.telephony.ims.ImsMmTelRepository
+import com.google.common.truth.Truth.assertThat
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.kotlin.any
+import org.mockito.kotlin.doReturn
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.spy
+import org.mockito.kotlin.stub
+
+@RunWith(AndroidJUnit4::class)
+class WifiCallingRepositoryTest {
+
+    private val mockTelephonyManager = mock<TelephonyManager> {
+        on { createForSubscriptionId(SUB_ID) } doReturn mock
+    }
+
+    private val mockCarrierConfigManager = mock<CarrierConfigManager>()
+
+    private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
+        on { getSystemService(TelephonyManager::class.java) } doReturn mockTelephonyManager
+        on { getSystemService(CarrierConfigManager::class.java) } doReturn mockCarrierConfigManager
+    }
+
+    private val mockImsMmTelRepository = mock<ImsMmTelRepository> {
+        on { getWiFiCallingMode(any()) } doReturn ImsMmTelManager.WIFI_MODE_UNKNOWN
+    }
+
+    private val repository = WifiCallingRepository(context, SUB_ID, mockImsMmTelRepository)
+
+    @Test
+    fun getWiFiCallingMode_roamingAndNotUseWfcHomeModeForRoaming_returnRoamingSetting() {
+        mockTelephonyManager.stub {
+            on { isNetworkRoaming } doReturn true
+        }
+        mockUseWfcHomeModeForRoaming(false)
+        mockImsMmTelRepository.stub {
+            on { getWiFiCallingMode(true) } doReturn ImsMmTelManager.WIFI_MODE_WIFI_PREFERRED
+        }
+
+        val wiFiCallingMode = repository.getWiFiCallingMode()
+
+        assertThat(wiFiCallingMode).isEqualTo(ImsMmTelManager.WIFI_MODE_WIFI_PREFERRED)
+    }
+
+    @Test
+    fun getWiFiCallingMode_roamingAndUseWfcHomeModeForRoaming_returnHomeSetting() {
+        mockTelephonyManager.stub {
+            on { isNetworkRoaming } doReturn true
+        }
+        mockUseWfcHomeModeForRoaming(true)
+        mockImsMmTelRepository.stub {
+            on { getWiFiCallingMode(false) } doReturn ImsMmTelManager.WIFI_MODE_WIFI_PREFERRED
+        }
+
+        val wiFiCallingMode = repository.getWiFiCallingMode()
+
+        assertThat(wiFiCallingMode).isEqualTo(ImsMmTelManager.WIFI_MODE_WIFI_PREFERRED)
+    }
+
+    @Test
+    fun getWiFiCallingMode_notRoaming_returnHomeSetting() {
+        mockTelephonyManager.stub {
+            on { isNetworkRoaming } doReturn false
+        }
+        mockImsMmTelRepository.stub {
+            on { getWiFiCallingMode(false) } doReturn ImsMmTelManager.WIFI_MODE_WIFI_PREFERRED
+        }
+
+        val wiFiCallingMode = repository.getWiFiCallingMode()
+
+        assertThat(wiFiCallingMode).isEqualTo(ImsMmTelManager.WIFI_MODE_WIFI_PREFERRED)
+    }
+
+    private fun mockUseWfcHomeModeForRoaming(config: Boolean) {
+        mockCarrierConfigManager.stub {
+            on {
+                getConfigForSubId(SUB_ID, KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL)
+            } doReturn persistableBundleOf(
+                KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL to config,
+            )
+        }
+    }
+
+    private companion object {
+        const val SUB_ID = 1
+    }
+}