From 5cca5b51a4c17177590cd2fec7e6464ce5b5fb23 Mon Sep 17 00:00:00 2001 From: "am.yazdanmanesh" Date: Fri, 23 Dec 2022 17:26:54 +0330 Subject: [PATCH] Fix dual simcard with same label bug --- .../dialer/dialogs/SelectSIMDialog.kt | 6 ++-- .../dialer/extensions/Activity.kt | 8 ++--- .../dialer/extensions/SharedPreferences.kt | 32 +++++++++++++++++++ .../dialer/helpers/Config.kt | 21 ++++++++++-- .../dialer/models/PhoneAccountHandleModel.kt | 7 ++++ build.gradle | 3 ++ 6 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/dialer/extensions/SharedPreferences.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/dialer/models/PhoneAccountHandleModel.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/dialogs/SelectSIMDialog.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/dialogs/SelectSIMDialog.kt index 34bbdb2c..8cd75af6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/dialogs/SelectSIMDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/dialogs/SelectSIMDialog.kt @@ -31,7 +31,7 @@ class SelectSIMDialog(val activity: BaseSimpleActivity, val phoneNumber: String, val radioButton = (activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton).apply { text = "${index + 1} - ${SIMAccount.label}" id = index - setOnClickListener { selectedSIM(SIMAccount.handle, SIMAccount.label) } + setOnClickListener { selectedSIM(SIMAccount.handle) } } radioGroup!!.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) } @@ -44,9 +44,9 @@ class SelectSIMDialog(val activity: BaseSimpleActivity, val phoneNumber: String, } } - private fun selectedSIM(handle: PhoneAccountHandle, label: String) { + private fun selectedSIM(handle: PhoneAccountHandle) { if (view.select_sim_remember.isChecked) { - activity.config.saveCustomSIM(phoneNumber, label) + activity.config.saveCustomSIM(phoneNumber, handle) } callback(handle) diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Activity.kt index acfd694d..e59a784b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Activity.kt @@ -75,12 +75,10 @@ fun SimpleActivity.getHandleToUse(intent: Intent?, phoneNumber: String, callback val defaultHandle = telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL) when { intent?.hasExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE) == true -> callback(intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE)!!) - config.getCustomSIM(phoneNumber)?.isNotEmpty() == true -> { - val storedLabel = Uri.decode(config.getCustomSIM(phoneNumber)) - val availableSIMs = getAvailableSIMCardLabels() - val firstOrNull = availableSIMs.firstOrNull { it.label == storedLabel }?.handle ?: availableSIMs.first().handle - callback(firstOrNull) + config.getCustomSIM() != null -> { + callback(config.getCustomSIM()) } + defaultHandle != null -> callback(defaultHandle) else -> { SelectSIMDialog(this, phoneNumber) { handle -> diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/SharedPreferences.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/SharedPreferences.kt new file mode 100644 index 00000000..138a4a1e --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/SharedPreferences.kt @@ -0,0 +1,32 @@ +package com.simplemobiletools.dialer.extensions + +import android.content.SharedPreferences +import android.telecom.PhoneAccountHandle +import com.simplemobiletools.dialer.models.PhoneAccountHandleModel +import com.google.gson.Gson + +fun SharedPreferences.Editor.putPhoneAccountHandle( + key: String, + parcelable: PhoneAccountHandle +): SharedPreferences.Editor { + val componentName = parcelable.componentName + val myPhoneAccountHandleModel = PhoneAccountHandleModel( + componentName.packageName, componentName.className, parcelable.id + ) + val json = Gson().toJson(myPhoneAccountHandleModel) + return putString(key, json) +} + +inline fun SharedPreferences.getPhoneAccountHandleModel( + key: String, + default: T +): T { + val json = getString(key, null) + return try { + if (json != null) + Gson().fromJson(json, T::class.java) + else default + } catch (_: JsonSyntaxException) { + default + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/Config.kt index 6f31cc0c..351fbd4d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/Config.kt @@ -1,7 +1,9 @@ package com.simplemobiletools.dialer.helpers +import android.content.ComponentName import android.content.Context import android.net.Uri +import android.telecom.PhoneAccountHandle import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.simplemobiletools.commons.helpers.BaseConfig @@ -30,11 +32,24 @@ class Config(context: Context) : BaseConfig(context) { return speedDialValues } - fun saveCustomSIM(number: String, SIMlabel: String) { - prefs.edit().putString(REMEMBER_SIM_PREFIX + number, Uri.encode(SIMlabel)).apply() + fun saveCustomSIM(number: String, handle: PhoneAccountHandle) { + prefs.edit().putPhoneAccountHandle(REMEMBER_SIM_PREFIX + number,handle).apply() } - fun getCustomSIM(number: String) = prefs.getString(REMEMBER_SIM_PREFIX + number, "") + fun getCustomSIM(number: String): PhoneAccountHandle? { + val myPhoneAccountHandle = + prefs.getPhoneAccountHandleModel(REMEMBER_SIM_PREFIX + number, null) + return if (myPhoneAccountHandle != null) { + val packageName = myPhoneAccountHandle.packageName + val className = myPhoneAccountHandle.className + val componentName = ComponentName(packageName, className) + val id = myPhoneAccountHandle.id + + PhoneAccountHandle(componentName, id) + } else { + null + } + } fun removeCustomSIM(number: String) { prefs.edit().remove(REMEMBER_SIM_PREFIX + number).apply() diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/models/PhoneAccountHandleModel.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/models/PhoneAccountHandleModel.kt new file mode 100644 index 00000000..b95bf181 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/models/PhoneAccountHandleModel.kt @@ -0,0 +1,7 @@ +package com.simplemobiletools.dialer.models + +data class PhoneAccountHandleModel( + val packageName: String, + val className: String, + val id: String +) diff --git a/build.gradle b/build.gradle index 09fe7980..ad5f0288 100644 --- a/build.gradle +++ b/build.gradle @@ -5,6 +5,8 @@ buildscript { repositories { google() jcenter() + mavenCentral() + maven { url 'https://jitpack.io' } } dependencies { classpath 'com.android.tools.build:gradle:7.3.1' @@ -19,6 +21,7 @@ allprojects { repositories { google() jcenter() + mavenCentral() maven { url 'https://jitpack.io' } } }