diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-contacts.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-contacts.kt index fc31ff92c..3e057fcf2 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-contacts.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Context-contacts.kt @@ -8,10 +8,14 @@ import android.os.Handler import android.os.Looper import android.provider.ContactsContract import com.simplemobiletools.commons.R +import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.databases.ContactsDatabase +import com.simplemobiletools.commons.dialogs.CallConfirmationDialog +import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.interfaces.ContactsDao import com.simplemobiletools.commons.interfaces.GroupsDao +import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.contacts.Contact import com.simplemobiletools.commons.models.contacts.ContactSource import com.simplemobiletools.commons.models.contacts.Organization @@ -321,3 +325,34 @@ fun Context.getSocialActions(id: Int): ArrayList { } return socialActions } + +fun BaseSimpleActivity.initiateCall(contact: Contact, onStartCallIntent: (phoneNumber: String) -> Unit) { + val numbers = contact.phoneNumbers + if (numbers.size == 1) { + onStartCallIntent(numbers.first().value) + } else if (numbers.size > 1) { + val primaryNumber = contact.phoneNumbers.find { it.isPrimary } + if (primaryNumber != null) { + onStartCallIntent(primaryNumber.value) + } else { + val items = ArrayList() + numbers.forEachIndexed { index, phoneNumber -> + items.add(RadioItem(index, "${phoneNumber.value} (${getPhoneNumberTypeText(phoneNumber.type, phoneNumber.label)})", phoneNumber.value)) + } + + RadioGroupDialog(this, items) { + onStartCallIntent(it as String) + } + } + } +} + +fun BaseSimpleActivity.tryInitiateCall(contact: Contact, onStartCallIntent: (phoneNumber: String) -> Unit) { + if (baseConfig.showCallConfirmation) { + CallConfirmationDialog(this, contact.getNameToDisplay()) { + initiateCall(contact, onStartCallIntent) + } + } else { + initiateCall(contact, onStartCallIntent) + } +}