ModemConfig: Deal with null|empty strings from TelephonyManager

Some of these strings are (temporarily) empty, eg. while the SIM is
locked. Make sure we do not crash in these instances, and rather wait
for the time these become available.

Signed-off-by: Marijn Suijten <marijns95@gmail.com>
This commit is contained in:
Marijn Suijten 2020-06-05 22:44:45 +02:00
commit 0d8dc0c542

View file

@ -109,7 +109,7 @@ context.resources.getXml(R.xml.service_provider_sim_configs).use {
} else if (simConfigId != null) {
// If not in a service_provider_sim_config element, and a valid simConfigId
// /exists parse all members/subelements:
val text = cleanString(xml.nextText())
val text = xml.nextText().cleanString()
if (currentMap.containsKey(xml.name))
throw Exception("Already parsed ${xml.name}!")
currentMap[xml.name] = text
@ -125,21 +125,27 @@ context.resources.getXml(R.xml.service_provider_sim_configs).use {
return list
}
private fun cleanString(str: String) = str.replace(newlineMatch, "").trim()
private fun String.cleanString() = replace(newlineMatch, "").trim()
private fun findConfigurationName(context: Context, tm: TelephonyManager): String? {
val operator = tm.simOperator
val mcc = operator.substring(0, 3)
val mnc = operator.substring(3)
val sp = cleanString(tm.simOperatorName)
if (operator.isNullOrEmpty()) {
Log.d(TAG, "Operator is null or empty")
return null
}
val mcc = operator.take(3)
val mnc = operator.drop(3)
val sp = tm.simOperatorName?.cleanString() ?: ""
val gid1 = tm.groupIdLevel1 ?: ""
val imsi = tm.subscriberId
val gid1 = tm.groupIdLevel1
val iccid2 = tm.simSerialNumber
if (imsi.isNullOrEmpty()) {
Log.d(TAG, "Imsi is null or empty")
return null
}
if (VERBOSE) Log.v(TAG, "Matching providers against: $sp $mcc/$mnc, imsi: $imsi" +
", gid: $gid1, iccid2: $iccid2")
", gid: $gid1")
val result = getProviders(context).firstOrNull f@{ info ->
if (info.mcc != null && info.mcc != mcc)