mirror of
				https://github.com/SimpleMobileTools/Simple-Contacts.git
				synced 2025-06-05 21:59:27 +02:00 
			
		
		
		
	adding a checkbox for remembering SIM at specific numbers
This commit is contained in:
		| @@ -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) | ||||
|                         } | ||||
|                     } | ||||
|   | ||||
| @@ -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() | ||||
|     } | ||||
|   | ||||
| @@ -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<ContactSource> { | ||||
| } | ||||
|  | ||||
| fun Context.getPrivateContactSource() = ContactSource(SMT_PRIVATE, SMT_PRIVATE, getString(R.string.phone_storage_hidden)) | ||||
|  | ||||
| @SuppressLint("MissingPermission") | ||||
| fun Context.getAvailableSIMCardLabels(): ArrayList<SIMAccount> { | ||||
|     val SIMAccounts = ArrayList<SIMAccount>() | ||||
|     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 | ||||
| } | ||||
|   | ||||
| @@ -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<SpeedDial> { | ||||
|         val speedDialType = object : TypeToken<List<SpeedDial>>() {}.type | ||||
|         val speedDialValues = Gson().fromJson<ArrayList<SpeedDial>>(speedDial, speedDialType) ?: ArrayList(1) | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -0,0 +1,5 @@ | ||||
| package com.simplemobiletools.contacts.pro.models | ||||
|  | ||||
| import android.telecom.PhoneAccountHandle | ||||
|  | ||||
| data class SIMAccount(val handle: PhoneAccountHandle, val label: String) | ||||
| @@ -18,6 +18,18 @@ | ||||
|     <RadioGroup | ||||
|         android:id="@+id/select_sim_radio_group" | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="wrap_content" /> | ||||
|         android:layout_height="wrap_content" | ||||
|         android:layout_marginBottom="@dimen/small_margin" /> | ||||
|  | ||||
|     <include layout="@layout/divider" /> | ||||
|  | ||||
|     <com.simplemobiletools.commons.views.MyAppCompatCheckbox | ||||
|         android:id="@+id/select_sim_remember" | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:layout_marginTop="@dimen/small_margin" | ||||
|         android:paddingTop="@dimen/activity_margin" | ||||
|         android:paddingBottom="@dimen/activity_margin" | ||||
|         android:text="@string/always_use_this_sim" /> | ||||
|  | ||||
| </LinearLayout> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user