Fix crash when phone process has problem.
- Phone process may not exist in some situation. It shall catch the exception to avoid Settings crash. Flag: EXEMPT bugfix fix: 365669913 Test: build pass Change-Id: I2b95f46b8887a9c8416fb4a5724537ad27e86850
This commit is contained in:
@@ -19,6 +19,7 @@ package com.android.settings.network.telephony
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.telephony.CarrierConfigManager
|
import android.telephony.CarrierConfigManager
|
||||||
import android.telephony.SubscriptionManager
|
import android.telephony.SubscriptionManager
|
||||||
|
import android.telephony.TelephonyManager
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
@@ -37,12 +38,13 @@ class VoNrRepository(
|
|||||||
fun isVoNrAvailable(subId: Int): Boolean {
|
fun isVoNrAvailable(subId: Int): Boolean {
|
||||||
if (!nrRepository.isNrAvailable(subId)) return false
|
if (!nrRepository.isNrAvailable(subId)) return false
|
||||||
data class Config(val isVoNrEnabled: Boolean, val isVoNrSettingVisibility: Boolean)
|
data class Config(val isVoNrEnabled: Boolean, val isVoNrSettingVisibility: Boolean)
|
||||||
|
|
||||||
val carrierConfig =
|
val carrierConfig =
|
||||||
carrierConfigRepository.transformConfig(subId) {
|
carrierConfigRepository.transformConfig(subId) {
|
||||||
Config(
|
Config(
|
||||||
isVoNrEnabled = getBoolean(CarrierConfigManager.KEY_VONR_ENABLED_BOOL),
|
isVoNrEnabled = getBoolean(CarrierConfigManager.KEY_VONR_ENABLED_BOOL),
|
||||||
isVoNrSettingVisibility =
|
isVoNrSettingVisibility =
|
||||||
getBoolean(CarrierConfigManager.KEY_VONR_SETTING_VISIBILITY_BOOL),
|
getBoolean(CarrierConfigManager.KEY_VONR_SETTING_VISIBILITY_BOOL),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return carrierConfig.isVoNrEnabled && carrierConfig.isVoNrSettingVisibility
|
return carrierConfig.isVoNrEnabled && carrierConfig.isVoNrSettingVisibility
|
||||||
@@ -52,7 +54,14 @@ class VoNrRepository(
|
|||||||
val telephonyManager = context.telephonyManager(subId)
|
val telephonyManager = context.telephonyManager(subId)
|
||||||
return context
|
return context
|
||||||
.subscriptionsChangedFlow()
|
.subscriptionsChangedFlow()
|
||||||
.map { telephonyManager.isVoNrEnabled }
|
.map {
|
||||||
|
try {
|
||||||
|
telephonyManager.isVoNrEnabled
|
||||||
|
} catch (e: IllegalStateException) {
|
||||||
|
Log.e(TAG, "IllegalStateException - isVoNrEnabled : $e")
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
.conflate()
|
.conflate()
|
||||||
.onEach { Log.d(TAG, "[$subId] isVoNrEnabled: $it") }
|
.onEach { Log.d(TAG, "[$subId] isVoNrEnabled: $it") }
|
||||||
.flowOn(Dispatchers.Default)
|
.flowOn(Dispatchers.Default)
|
||||||
@@ -61,11 +70,17 @@ class VoNrRepository(
|
|||||||
suspend fun setVoNrEnabled(subId: Int, enabled: Boolean) =
|
suspend fun setVoNrEnabled(subId: Int, enabled: Boolean) =
|
||||||
withContext(Dispatchers.Default) {
|
withContext(Dispatchers.Default) {
|
||||||
if (!SubscriptionManager.isValidSubscriptionId(subId)) return@withContext
|
if (!SubscriptionManager.isValidSubscriptionId(subId)) return@withContext
|
||||||
val result = context.telephonyManager(subId).setVoNrEnabled(enabled)
|
var result = TelephonyManager.ENABLE_VONR_RADIO_INVALID_STATE
|
||||||
Log.d(TAG, "[$subId] setVoNrEnabled: $enabled, result: $result")
|
try {
|
||||||
|
result = context.telephonyManager(subId).setVoNrEnabled(enabled)
|
||||||
|
} catch (e: IllegalStateException) {
|
||||||
|
Log.e(TAG, "IllegalStateException - setVoNrEnabled : $e")
|
||||||
|
} finally {
|
||||||
|
Log.d(TAG, "[$subId] setVoNrEnabled: $enabled, result: $result")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
private const val TAG = "VoNrRepository"
|
private const val TAG = "VoNrRepository"
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -27,7 +27,9 @@ import kotlinx.coroutines.runBlocking
|
|||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
|
import org.mockito.kotlin.any
|
||||||
import org.mockito.kotlin.doReturn
|
import org.mockito.kotlin.doReturn
|
||||||
|
import org.mockito.kotlin.doThrow
|
||||||
import org.mockito.kotlin.mock
|
import org.mockito.kotlin.mock
|
||||||
import org.mockito.kotlin.spy
|
import org.mockito.kotlin.spy
|
||||||
import org.mockito.kotlin.stub
|
import org.mockito.kotlin.stub
|
||||||
@@ -134,6 +136,15 @@ class VoNrRepositoryTest {
|
|||||||
assertThat(isVoNrEnabled).isTrue()
|
assertThat(isVoNrEnabled).isTrue()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun isVoNrEnabledFlow_noPhoneProcess_noCrash() = runBlocking {
|
||||||
|
mockTelephonyManager.stub { on { isVoNrEnabled } doThrow IllegalStateException("no Phone") }
|
||||||
|
|
||||||
|
val isVoNrEnabled = repository.isVoNrEnabledFlow(SUB_ID).firstWithTimeoutOrNull()
|
||||||
|
|
||||||
|
assertThat(isVoNrEnabled).isFalse()
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun setVoNrEnabled(): Unit = runBlocking {
|
fun setVoNrEnabled(): Unit = runBlocking {
|
||||||
repository.setVoNrEnabled(SUB_ID, true)
|
repository.setVoNrEnabled(SUB_ID, true)
|
||||||
@@ -141,7 +152,20 @@ class VoNrRepositoryTest {
|
|||||||
verify(mockTelephonyManager).setVoNrEnabled(true)
|
verify(mockTelephonyManager).setVoNrEnabled(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun setVoNrEnabled_noPhoneProcess_noCrash(): Unit = runBlocking {
|
||||||
|
mockTelephonyManager.stub {
|
||||||
|
on {
|
||||||
|
setVoNrEnabled(any())
|
||||||
|
} doThrow IllegalStateException("no Phone")
|
||||||
|
}
|
||||||
|
|
||||||
|
repository.setVoNrEnabled(SUB_ID, true)
|
||||||
|
|
||||||
|
verify(mockTelephonyManager).setVoNrEnabled(true)
|
||||||
|
}
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
const val SUB_ID = 1
|
const val SUB_ID = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user