From c0b6d4306cd69764de63d2064c99c1224b840430 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Fri, 16 Oct 2020 12:16:48 +0200 Subject: [PATCH] ModemConfig: Convert providers list into readonly, lazy-initialized val Assuming the value is used after onCreate has been called (ie. not in a constructor) parseProviders can safely run and retrieve resources. This solves poor (basically inexistent) use-before-initialization checks of a lateinit value as well; the value could have been freely used without any initialization at all or reassigned from anywhere because it's mutable, without the compiler complaining. Signed-off-by: Marijn Suijten --- .../sony/opentelephony/modemconfig/ModemConfigService.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ModemConfig/src/com/sony/opentelephony/modemconfig/ModemConfigService.kt b/ModemConfig/src/com/sony/opentelephony/modemconfig/ModemConfigService.kt index 036ee8d..eef2ef5 100644 --- a/ModemConfig/src/com/sony/opentelephony/modemconfig/ModemConfigService.kt +++ b/ModemConfig/src/com/sony/opentelephony/modemconfig/ModemConfigService.kt @@ -55,7 +55,11 @@ class ModemConfigService : Service() { fun specificity() = listOfNotNull(mcc, mnc, gid1, imsi, sp).count() } - private lateinit var providers: List + private val providers: List by lazy { + // WARNING: This lazy assumes `providers` is used after the service has started, + // parseProviders requires a valid Context to call `getResources()` on. + parseProviders() + } private val notificationManager: NotificationManager by lazy { getSystemService(NotificationManager::class.java) @@ -217,9 +221,6 @@ class ModemConfigService : Service() { override fun onCreate() { Log.e(TAG, "Starting") - - providers = parseProviders() - val sm = getSystemService(SubscriptionManager::class.java) ?: throw Exception("Expected SubscriptionManager, got null")