diff --git a/ModemConfig/AndroidManifest.xml b/ModemConfig/AndroidManifest.xml index 1d0f857..45fc428 100644 --- a/ModemConfig/AndroidManifest.xml +++ b/ModemConfig/AndroidManifest.xml @@ -5,14 +5,21 @@ android:versionCode="1" android:versionName="1.0"> - - + + - + + + + diff --git a/ModemConfig/src/com/sony/opentelephony/modemconfig/BootReceiver.kt b/ModemConfig/src/com/sony/opentelephony/modemconfig/BootReceiver.kt new file mode 100644 index 0000000..ea06c71 --- /dev/null +++ b/ModemConfig/src/com/sony/opentelephony/modemconfig/BootReceiver.kt @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2020 Marijn Suijten + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.sony.opentelephony.modemconfig + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent + +class BootReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent?) { + context.startService(Intent(context, ModemConfigService::class.java)) + } +} diff --git a/ModemConfig/src/com/sony/opentelephony/modemconfig/ModemConfigReceiver.kt b/ModemConfig/src/com/sony/opentelephony/modemconfig/ModemConfigService.kt similarity index 78% rename from ModemConfig/src/com/sony/opentelephony/modemconfig/ModemConfigReceiver.kt rename to ModemConfig/src/com/sony/opentelephony/modemconfig/ModemConfigService.kt index 2f86bcc..8958c38 100644 --- a/ModemConfig/src/com/sony/opentelephony/modemconfig/ModemConfigReceiver.kt +++ b/ModemConfig/src/com/sony/opentelephony/modemconfig/ModemConfigService.kt @@ -16,9 +16,11 @@ package com.sony.opentelephony.modemconfig -import android.content.BroadcastReceiver +import android.app.Service import android.content.Context import android.content.Intent +import android.os.IBinder +import android.telephony.SubscriptionInfo import android.telephony.SubscriptionManager import android.telephony.TelephonyManager import android.text.TextUtils @@ -26,9 +28,10 @@ import android.util.Log import org.xmlpull.v1.XmlPullParser private const val TAG = "ModemConfigReceiver" +private const val VERBOSE = false private val newlineMatch = Regex("[\n\r]") -class ModemConfigReceiver : BroadcastReceiver() { +class ModemConfigService : Service() { private class ProviderFilter(val sim_config_id: String, val mcc: String? = null, val mnc: String? = null, @@ -118,8 +121,8 @@ context.resources.getXml(R.xml.service_provider_sim_configs).use { val gid1 = tm.groupIdLevel1 val iccid2 = tm.simSerialNumber - Log.d(TAG, "Matching providers against: $sp $mcc/$mnc, imsi: $imsi" + - ", gid: $gid1, iccid2: $iccid2") + if (VERBOSE) Log.v(TAG, "Matching providers against: $sp $mcc/$mnc, imsi: $imsi" + + ", gid: $gid1, iccid2: $iccid2") val result = getProviders(context).filter f@{ info -> if (info.mcc != null && info.mcc != mcc) @@ -152,24 +155,30 @@ context.resources.getXml(R.xml.service_provider_sim_configs).use { } } - override fun onReceive(context: Context, intent: Intent) { - Log.d(TAG, "Received intent! $intent") - if (SubscriptionManager.ACTION_DEFAULT_SUBSCRIPTION_CHANGED != intent.action) - throw Exception("Expected ${SubscriptionManager.ACTION_DEFAULT_SUBSCRIPTION_CHANGED}" + - ", got ${intent.action}. Is the intent filter correct??") - val subId = intent.getIntExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, - SubscriptionManager.INVALID_SUBSCRIPTION_ID) - if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { - Log.d(TAG, "Received invalid subId. SIM removed?") - return - } - val slotIdx = SubscriptionManager.getSlotIndex(subId) // Can be INVALID_SIM_SLOT_INDEX - if (slotIdx == SubscriptionManager.INVALID_SIM_SLOT_INDEX) - throw Exception("Invalid slot index") - Log.d(TAG, "Finding configuration for subId $subId, slotIdx: $slotIdx") + private fun handleSubscription(sub: SubscriptionInfo) { + if (VERBOSE) Log.v(TAG, "Checking sub $sub") - val globalTm = context.getSystemService(TelephonyManager::class.java) - val tm = globalTm!!.createForSubscriptionId(subId) - val name = findConfigurationName(context, tm) + val globalTm = getSystemService(TelephonyManager::class.java) + val tm = globalTm!!.createForSubscriptionId(sub.subscriptionId) + val name = findConfigurationName(this, tm) } + + override fun onCreate() { + Log.e(TAG, "Starting") + val sm = getSystemService(SubscriptionManager::class.java) + ?: throw Exception("Expected SubscriptionManager, got null") + + // Our callback is invoked once on .add too; no need to run the contents manually at startup + sm.addOnSubscriptionsChangedListener( + object : SubscriptionManager.OnSubscriptionsChangedListener() { + // Even though this callback is invoked a lot (when sims are changed), apply no + // caching at all. The modem-switcher itself makes sure to not needlessly flash + // firmware - let it handle the validation. + override fun onSubscriptionsChanged() { + sm.activeSubscriptionInfoList?.forEach(::handleSubscription) + } + }) + } + + override fun onBind(intent: Intent?): IBinder? = null }