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 <marijns95@gmail.com>
This commit is contained in:
Marijn Suijten 2020-10-16 12:16:48 +02:00 • committed by Alin Jerpelea
commit c0b6d4306c

View file

@ -55,7 +55,11 @@ class ModemConfigService : Service() {
fun specificity() = listOfNotNull(mcc, mnc, gid1, imsi, sp).count()
}
private lateinit var providers: List<ProviderFilter>
private val providers: List<ProviderFilter> 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")