From 56e8e4744e23fa10e1959010eb6bad4e2ae02195 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 2 May 2020 20:48:00 +0200 Subject: [PATCH] adding a checkbox for remembering SIM at specific numbers --- .../contacts/pro/activities/DialerActivity.kt | 14 ++++++++-- .../contacts/pro/dialogs/SelectSIMDialog.kt | 28 ++++++++----------- .../contacts/pro/extensions/Context.kt | 20 +++++++++++++ .../contacts/pro/helpers/Config.kt | 7 +++++ .../contacts/pro/helpers/Constants.kt | 5 ++-- .../contacts/pro/models/SIMAccount.kt | 5 ++++ app/src/main/res/layout/dialog_select_sim.xml | 14 +++++++++- 7 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/SIMAccount.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/DialerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/DialerActivity.kt index a2d29519..c020692f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/DialerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/DialerActivity.kt @@ -16,6 +16,8 @@ import com.simplemobiletools.commons.helpers.PERMISSION_READ_PHONE_STATE import com.simplemobiletools.commons.helpers.REQUEST_CODE_SET_DEFAULT_DIALER import com.simplemobiletools.contacts.pro.R import com.simplemobiletools.contacts.pro.dialogs.SelectSIMDialog +import com.simplemobiletools.contacts.pro.extensions.config +import com.simplemobiletools.contacts.pro.extensions.getAvailableSIMCardLabels class DialerActivity : SimpleActivity() { private var callNumber: Uri? = null @@ -46,7 +48,7 @@ class DialerActivity : SimpleActivity() { @SuppressLint("MissingPermission") private fun initOutgoingCall() { try { - getHandleToUse { handle -> + getHandleToUse(callNumber.toString()) { handle -> Bundle().apply { putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, handle) putBoolean(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, false) @@ -62,15 +64,21 @@ class DialerActivity : SimpleActivity() { } @SuppressLint("MissingPermission") - private fun getHandleToUse(callback: (PhoneAccountHandle) -> Unit) { + private fun getHandleToUse(phoneNumber: String, callback: (PhoneAccountHandle) -> Unit) { val defaultHandle = telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL) when { intent.hasExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE) -> 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) + } defaultHandle != null -> callback(defaultHandle) else -> { handlePermission(PERMISSION_READ_PHONE_STATE) { if (it) { - SelectSIMDialog(this) { handle -> + SelectSIMDialog(this, phoneNumber) { handle -> callback(handle) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/SelectSIMDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/SelectSIMDialog.kt index c3cb2e47..c621ceae 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/SelectSIMDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/SelectSIMDialog.kt @@ -1,7 +1,6 @@ package com.simplemobiletools.contacts.pro.dialogs import android.annotation.SuppressLint -import android.net.Uri import android.telecom.PhoneAccountHandle import android.view.ViewGroup import android.widget.RadioButton @@ -9,31 +8,24 @@ import android.widget.RadioGroup import androidx.appcompat.app.AlertDialog import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.setupDialogStuff -import com.simplemobiletools.commons.extensions.telecomManager import com.simplemobiletools.contacts.pro.R +import com.simplemobiletools.contacts.pro.extensions.config +import com.simplemobiletools.contacts.pro.extensions.getAvailableSIMCardLabels import kotlinx.android.synthetic.main.dialog_select_sim.view.* @SuppressLint("MissingPermission") -class SelectSIMDialog(val activity: BaseSimpleActivity, val callback: (handle: PhoneAccountHandle) -> Unit) { +class SelectSIMDialog(val activity: BaseSimpleActivity, val phoneNumber: String, val callback: (handle: PhoneAccountHandle) -> Unit) { private var dialog: AlertDialog? = null + private val view = activity.layoutInflater.inflate(R.layout.dialog_select_sim, null) init { - val view = activity.layoutInflater.inflate(R.layout.dialog_select_sim, null) val radioGroup = view.select_sim_radio_group - activity.telecomManager.callCapablePhoneAccounts.forEachIndexed { index, account -> - val phoneAccount = activity.telecomManager.getPhoneAccount(account) - var label = phoneAccount.label.toString() - var address = phoneAccount.address.toString() - if (address.startsWith("tel:") && address.substringAfter("tel:").isNotEmpty()) { - address = Uri.decode(address.substringAfter("tel:")) - label += " ($address)" - } - + activity.getAvailableSIMCardLabels().forEachIndexed { index, SIMAccount -> val radioButton = (activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton).apply { - text = label + text = SIMAccount.label id = index - setOnClickListener { selectedSIM(phoneAccount.accountHandle) } + setOnClickListener { selectedSIM(SIMAccount.handle, SIMAccount.label) } } radioGroup!!.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)) } @@ -44,7 +36,11 @@ class SelectSIMDialog(val activity: BaseSimpleActivity, val callback: (handle: P } } - private fun selectedSIM(handle: PhoneAccountHandle) { + private fun selectedSIM(handle: PhoneAccountHandle, label: String) { + if (view.select_sim_remember.isChecked) { + activity.config.saveCustomSIM(phoneNumber, label) + } + callback(handle) dialog?.dismiss() } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt index e9cafbc2..ca8dd026 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt @@ -1,5 +1,6 @@ package com.simplemobiletools.contacts.pro.extensions +import android.annotation.SuppressLint import android.content.Context import android.content.Context.AUDIO_SERVICE import android.content.Intent @@ -12,6 +13,7 @@ import android.provider.ContactsContract import androidx.core.content.FileProvider import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.hasPermission +import com.simplemobiletools.commons.extensions.telecomManager import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS @@ -26,6 +28,7 @@ import com.simplemobiletools.contacts.pro.interfaces.GroupsDao import com.simplemobiletools.contacts.pro.models.Contact import com.simplemobiletools.contacts.pro.models.ContactSource import com.simplemobiletools.contacts.pro.models.Organization +import com.simplemobiletools.contacts.pro.models.SIMAccount import java.io.File val Context.config: Config get() = Config.newInstance(applicationContext) @@ -326,3 +329,20 @@ fun Context.getAllContactSources(): ArrayList { } fun Context.getPrivateContactSource() = ContactSource(SMT_PRIVATE, SMT_PRIVATE, getString(R.string.phone_storage_hidden)) + +@SuppressLint("MissingPermission") +fun Context.getAvailableSIMCardLabels(): ArrayList { + val SIMAccounts = ArrayList() + telecomManager.callCapablePhoneAccounts.forEach { account -> + val phoneAccount = telecomManager.getPhoneAccount(account) + var label = phoneAccount.label.toString() + var address = phoneAccount.address.toString() + if (address.startsWith("tel:") && address.substringAfter("tel:").isNotEmpty()) { + address = Uri.decode(address.substringAfter("tel:")) + label += " ($address)" + } + val SIM = SIMAccount(phoneAccount.accountHandle, label) + SIMAccounts.add(SIM) + } + return SIMAccounts +} diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Config.kt index 76b047a7..b931a9ee 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Config.kt @@ -1,6 +1,7 @@ package com.simplemobiletools.contacts.pro.helpers import android.content.Context +import android.net.Uri import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.simplemobiletools.commons.helpers.BaseConfig @@ -72,6 +73,12 @@ class Config(context: Context) : BaseConfig(context) { get() = prefs.getString(SPEED_DIAL, "")!! set(speedDial) = prefs.edit().putString(SPEED_DIAL, speedDial).apply() + fun saveCustomSIM(number: String, SIMlabel: String) { + prefs.edit().putString(REMEMBER_SIM_PREFIX + number, Uri.encode(SIMlabel)).apply() + } + + fun getCustomSIM(number: String) = prefs.getString(REMEMBER_SIM_PREFIX + number, "") + fun getSpeedDialValues(): ArrayList { val speedDialType = object : TypeToken>() {}.type val speedDialValues = Gson().fromJson>(speedDial, speedDialType) ?: ArrayList(1) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt index 5de1e921..dedf47b3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt @@ -24,6 +24,7 @@ const val SHOW_DIALPAD_LETTERS = "show_dialpad_letters" const val SPEED_DIAL = "speed_dial" const val LAST_EXPORT_PATH = "last_export_path" const val WAS_LOCAL_ACCOUNT_INITIALIZED = "was_local_account_initialized" +const val REMEMBER_SIM_PREFIX = "remember_sim_" const val CONTACT_ID = "contact_id" const val SMT_PRIVATE = "smt_private" // used at the contact source of local contacts hidden from other apps @@ -56,8 +57,8 @@ const val GROUPS_TAB_MASK = 8 const val ALL_TABS_MASK = CONTACTS_TAB_MASK or FAVORITES_TAB_MASK or GROUPS_TAB_MASK val tabsList = arrayListOf(CONTACTS_TAB_MASK, - FAVORITES_TAB_MASK, - GROUPS_TAB_MASK + FAVORITES_TAB_MASK, + GROUPS_TAB_MASK ) // contact photo changes diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/SIMAccount.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/SIMAccount.kt new file mode 100644 index 00000000..aca810f4 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/SIMAccount.kt @@ -0,0 +1,5 @@ +package com.simplemobiletools.contacts.pro.models + +import android.telecom.PhoneAccountHandle + +data class SIMAccount(val handle: PhoneAccountHandle, val label: String) diff --git a/app/src/main/res/layout/dialog_select_sim.xml b/app/src/main/res/layout/dialog_select_sim.xml index 7a8f7b14..6c51bb4b 100644 --- a/app/src/main/res/layout/dialog_select_sim.xml +++ b/app/src/main/res/layout/dialog_select_sim.xml @@ -18,6 +18,18 @@ + android:layout_height="wrap_content" + android:layout_marginBottom="@dimen/small_margin" /> + + + +