default number

This commit is contained in:
Pavel Poley 2022-05-18 13:48:38 +03:00
parent 6f6d03080f
commit 6566fadf85
6 changed files with 104 additions and 20 deletions

View File

@ -63,7 +63,7 @@ android {
}
dependencies {
implementation 'com.github.SimpleMobileTools:Simple-Commons:34fdfce71c'
implementation 'com.github.pavelpoley:Simple-Commons:1c1017d62a'
implementation 'com.googlecode.ez-vcard:ez-vcard:0.11.3'
implementation 'com.github.tibbi:IndicatorFastScroll:4524cd0b61'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'

View File

@ -31,7 +31,6 @@ import com.simplemobiletools.contacts.pro.extensions.sendEmailIntent
import com.simplemobiletools.contacts.pro.extensions.shareContacts
import com.simplemobiletools.contacts.pro.helpers.ContactsHelper
import com.simplemobiletools.contacts.pro.models.Contact
import java.util.*
abstract class ContactActivity : SimpleActivity() {
protected val PICK_RINGTONE_INTENT_ID = 1500
@ -128,6 +127,10 @@ abstract class ContactActivity : SimpleActivity() {
if (numbers.size == 1) {
launchSendSMSIntent(numbers.first().value)
} else if (numbers.size > 1) {
val primaryNumber = numbers.find { it.isPrimary }
if (primaryNumber != null) {
launchSendSMSIntent(primaryNumber.value)
} else {
val items = ArrayList<RadioItem>()
numbers.forEachIndexed { index, phoneNumber ->
items.add(RadioItem(index, phoneNumber.value, phoneNumber.value))
@ -138,6 +141,7 @@ abstract class ContactActivity : SimpleActivity() {
}
}
}
}
fun trySendEmail() {
val emails = contact!!.emails

View File

@ -22,6 +22,7 @@ import android.widget.EditText
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.dialogs.SelectAlarmSoundDialog
@ -470,7 +471,9 @@ class EditContactActivity : ContactActivity() {
}
private fun setupPhoneNumbers() {
contact!!.phoneNumbers.forEachIndexed { index, number ->
val phoneNumbers = contact!!.phoneNumbers
phoneNumbers.forEachIndexed { index, number ->
var numberHolder = contact_numbers_holder.getChildAt(index)
if (numberHolder == null) {
numberHolder = layoutInflater.inflate(R.layout.item_edit_phone_number, contact_numbers_holder, false)
@ -481,9 +484,58 @@ class EditContactActivity : ContactActivity() {
contact_number.setText(number.value)
contact_number.tag = number.normalizedNumber
setupPhoneNumberTypePicker(contact_number_type, number.type, number.label)
if (highlightLastPhoneNumber && index == contact!!.phoneNumbers.size - 1) {
if (highlightLastPhoneNumber && index == phoneNumbers.size - 1) {
numberViewToColor = contact_number
}
default_toggle_icon.tag = if (number.isPrimary) 1 else 0
}
}
initNumberHolders()
}
private fun setDefaultNumber(selected: ImageView) {
val numbersCount = contact_numbers_holder.childCount
for (i in 0 until numbersCount) {
val toggleIcon = contact_numbers_holder.getChildAt(i).default_toggle_icon
if (toggleIcon != selected) {
toggleIcon.tag = 0
}
}
selected.tag = if (selected.tag == 1) 0 else 1
initNumberHolders()
}
private fun initNumberHolders() {
val numbersCount = contact_numbers_holder.childCount
if (numbersCount == 1) {
contact_numbers_holder.getChildAt(0).default_toggle_icon.beGone()
return
}
for (i in 0 until numbersCount) {
val toggleIcon = contact_numbers_holder.getChildAt(i).default_toggle_icon
val isPrimary = toggleIcon.tag == 1
val drawable = if (isPrimary) {
ContextCompat.getDrawable(this@EditContactActivity, R.drawable.ic_star_vector)
} else {
ContextCompat.getDrawable(this@EditContactActivity, R.drawable.ic_star_outline_vector)
}
drawable?.apply {
mutate()
setTint(getProperTextColor())
}
toggleIcon.setImageDrawable(drawable)
toggleIcon.beVisible()
toggleIcon.setOnClickListener {
setDefaultNumber(toggleIcon)
}
}
}
@ -1037,7 +1089,9 @@ class EditContactActivity : ContactActivity() {
if (PhoneNumberUtils.compare(number.normalizePhoneNumber(), fetchedNormalizedNumber)) {
normalizedNumber = fetchedNormalizedNumber
}
phoneNumbers.add(PhoneNumber(number, numberType, numberLabel, normalizedNumber))
val isPrimary = numberHolder.default_toggle_icon.tag == 1
phoneNumbers.add(PhoneNumber(number, numberType, numberLabel, normalizedNumber, isPrimary))
}
}
return phoneNumbers
@ -1176,6 +1230,10 @@ class EditContactActivity : ContactActivity() {
numberHolder.contact_number.requestFocus()
showKeyboard(numberHolder.contact_number)
}
numberHolder.default_toggle_icon.apply {
tag = 0
}
initNumberHolders()
}
private fun addNewEmailField() {

View File

@ -44,6 +44,10 @@ fun SimpleActivity.startCall(contact: Contact) {
if (numbers.size == 1) {
startCallIntent(numbers.first().value)
} else if (numbers.size > 1) {
val primaryNumber = contact.phoneNumbers.find { it.isPrimary }
if (primaryNumber != null) {
startCallIntent(primaryNumber.value)
} else {
val items = ArrayList<RadioItem>()
numbers.forEachIndexed { index, phoneNumber ->
items.add(RadioItem(index, "${phoneNumber.value} (${getPhoneNumberTypeText(phoneNumber.type, phoneNumber.label)})", phoneNumber.value))
@ -53,6 +57,7 @@ fun SimpleActivity.startCall(contact: Contact) {
startCallIntent(it as String)
}
}
}
}
fun SimpleActivity.showContactSourcePicker(currentSource: String, callback: (newSource: String) -> Unit) {

View File

@ -281,7 +281,8 @@ class ContactsHelper(val context: Context) {
Phone.NUMBER,
Phone.NORMALIZED_NUMBER,
Phone.TYPE,
Phone.LABEL
Phone.LABEL,
Phone.IS_PRIMARY
)
val selection = if (contactId == null) getSourcesSelection() else "${Data.RAW_CONTACT_ID} = ?"
@ -293,12 +294,13 @@ class ContactsHelper(val context: Context) {
val normalizedNumber = cursor.getStringValue(Phone.NORMALIZED_NUMBER) ?: number.normalizePhoneNumber()
val type = cursor.getIntValue(Phone.TYPE)
val label = cursor.getStringValue(Phone.LABEL) ?: ""
val isPrimary = cursor.getIntValue(Phone.IS_PRIMARY) != 0
if (phoneNumbers[id] == null) {
phoneNumbers.put(id, ArrayList())
}
val phoneNumber = PhoneNumber(number, type, label, normalizedNumber)
val phoneNumber = PhoneNumber(number, type, label, normalizedNumber, isPrimary)
phoneNumbers[id].add(phoneNumber)
}
@ -944,6 +946,7 @@ class ContactsHelper(val context: Context) {
withValue(Phone.NORMALIZED_NUMBER, it.normalizedNumber)
withValue(Phone.TYPE, it.type)
withValue(Phone.LABEL, it.label)
withValue(Phone.IS_PRIMARY, it.isPrimary)
operations.add(build())
}
}
@ -1253,6 +1256,7 @@ class ContactsHelper(val context: Context) {
withValue(Phone.NORMALIZED_NUMBER, it.normalizedNumber)
withValue(Phone.TYPE, it.type)
withValue(Phone.LABEL, it.label)
withValue(Phone.IS_PRIMARY, it.isPrimary)
operations.add(build())
}
}

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/contact_number_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -10,7 +11,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="@+id/contact_number_type"
android:layout_marginEnd="0dp"
android:layout_toStartOf="@+id/default_toggle_icon"
android:hint="@string/number"
android:inputType="phone"
android:lines="1"
@ -29,9 +31,20 @@
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:minWidth="70dp"
android:paddingStart="@dimen/medium_margin"
android:paddingEnd="@dimen/medium_margin"
android:text="@string/mobile"
android:textSize="@dimen/bigger_text_size" />
<ImageView
android:id="@+id/default_toggle_icon"
android:layout_width="@dimen/contact_icons_size"
android:layout_height="@dimen/contact_icons_size"
android:layout_centerInParent="true"
android:layout_marginEnd="0dp"
android:layout_toStartOf="@+id/contact_number_type"
android:visibility="gone"
app:srcCompat="@drawable/ic_star_outline_vector" />
</RelativeLayout>