From 0d8dc0c5421ec37b2180d3ba7451892a7e44cc8f Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Fri, 5 Jun 2020 22:44:45 +0200 Subject: [PATCH] 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 --- .../modemconfig/ModemConfigService.kt | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/ModemConfig/src/com/sony/opentelephony/modemconfig/ModemConfigService.kt b/ModemConfig/src/com/sony/opentelephony/modemconfig/ModemConfigService.kt index c1dbe54..94860e3 100644 --- a/ModemConfig/src/com/sony/opentelephony/modemconfig/ModemConfigService.kt +++ b/ModemConfig/src/com/sony/opentelephony/modemconfig/ModemConfigService.kt @@ -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)