Fix dual simcard with same label bug

This commit is contained in:
am.yazdanmanesh 2022-12-23 17:26:54 +03:30
parent 362757fa42
commit 5cca5b51a4
6 changed files with 66 additions and 11 deletions

View File

@ -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)

View File

@ -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 ->

View File

@ -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 <reified T : PhoneAccountHandleModel?> 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
}
}

View File

@ -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()

View File

@ -0,0 +1,7 @@
package com.simplemobiletools.dialer.models
data class PhoneAccountHandleModel(
val packageName: String,
val className: String,
val id: String
)

View File

@ -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' }
}
}