Formatting the phone number with user's region

Example:
(US) 5554567890 -> (555)456-7890
(France) 0781635535 -> 07 81 63 55 35

Original PR: https://github.com/FossifyOrg/Contacts/pull/72
---------

Co-authored-by: Naveen Singh <36371707+naveensingh@users.noreply.github.com>
Co-authored-by: Naveen <snaveen935@gmail.com>
This commit is contained in:
theoUniv 2024-03-26 06:40:04 +01:00 committed by GitHub
parent ca64735c34
commit b545a0dd9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 62 additions and 8 deletions

View file

@ -52,6 +52,7 @@ class SettingsActivity : SimpleActivity() {
setupLanguage() setupLanguage()
setupShowContactThumbnails() setupShowContactThumbnails()
setupShowPhoneNumbers() setupShowPhoneNumbers()
setupEnableNumberFormatting()
setupShowContactsWithNumbers() setupShowContactsWithNumbers()
setupStartNameWithSurname() setupStartNameWithSurname()
setupMergeDuplicateContacts() setupMergeDuplicateContacts()
@ -165,6 +166,14 @@ class SettingsActivity : SimpleActivity() {
} }
} }
private fun setupEnableNumberFormatting(){
binding.settingsFormatPhoneNumbers.isChecked = config.formatPhoneNumbers
binding.settingsFormatPhoneNumbersHolder.setOnClickListener{
binding.settingsFormatPhoneNumbers.toggle()
config.formatPhoneNumbers = binding.settingsFormatPhoneNumbers.isChecked
}
}
private fun setupShowPhoneNumbers() { private fun setupShowPhoneNumbers() {
binding.settingsShowPhoneNumbers.isChecked = config.showPhoneNumbers binding.settingsShowPhoneNumbers.isChecked = config.showPhoneNumbers
binding.settingsShowPhoneNumbersHolder.setOnClickListener { binding.settingsShowPhoneNumbersHolder.setOnClickListener {

View file

@ -2,12 +2,14 @@ package org.fossify.contacts.activities
import android.content.ActivityNotFoundException import android.content.ActivityNotFoundException
import android.content.ContentUris import android.content.ContentUris
import android.content.Context
import android.content.Intent import android.content.Intent
import android.media.AudioManager import android.media.AudioManager
import android.media.RingtoneManager import android.media.RingtoneManager
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.ContactsContract import android.provider.ContactsContract
import android.telephony.PhoneNumberUtils
import android.view.View import android.view.View
import android.view.WindowManager import android.view.WindowManager
import android.widget.RelativeLayout import android.widget.RelativeLayout
@ -27,10 +29,7 @@ import org.fossify.contacts.R
import org.fossify.contacts.databinding.* import org.fossify.contacts.databinding.*
import org.fossify.contacts.dialogs.ChooseSocialDialog import org.fossify.contacts.dialogs.ChooseSocialDialog
import org.fossify.contacts.dialogs.ManageVisibleFieldsDialog import org.fossify.contacts.dialogs.ManageVisibleFieldsDialog
import org.fossify.contacts.extensions.config import org.fossify.contacts.extensions.*
import org.fossify.contacts.extensions.editContact
import org.fossify.contacts.extensions.getPackageDrawable
import org.fossify.contacts.extensions.startCallIntent
import org.fossify.contacts.helpers.* import org.fossify.contacts.helpers.*
import java.util.Locale import java.util.Locale
@ -383,7 +382,11 @@ class ViewContactActivity : ContactActivity() {
phoneNumbers.forEach { phoneNumber -> phoneNumbers.forEach { phoneNumber ->
ItemViewPhoneNumberBinding.inflate(layoutInflater, binding.contactNumbersHolder, false).apply { ItemViewPhoneNumberBinding.inflate(layoutInflater, binding.contactNumbersHolder, false).apply {
binding.contactNumbersHolder.addView(root) binding.contactNumbersHolder.addView(root)
contactNumber.text = phoneNumber.value if (config.formatPhoneNumbers) {
contactNumber.text = phoneNumber.value.formatPhoneNumber()
} else {
contactNumber.text = phoneNumber.value
}
contactNumberType.text = getPhoneNumberTypeText(phoneNumber.type, phoneNumber.label) contactNumberType.text = getPhoneNumberTypeText(phoneNumber.type, phoneNumber.label)
root.copyOnLongClick(phoneNumber.value) root.copyOnLongClick(phoneNumber.value)

View file

@ -1,6 +1,7 @@
package org.fossify.contacts.adapters package org.fossify.contacts.adapters
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.ShortcutInfo import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager import android.content.pm.ShortcutManager
@ -8,6 +9,7 @@ import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.graphics.drawable.Icon import android.graphics.drawable.Icon
import android.graphics.drawable.LayerDrawable import android.graphics.drawable.LayerDrawable
import android.telephony.PhoneNumberUtils
import android.util.TypedValue import android.util.TypedValue
import android.view.Menu import android.view.Menu
import android.view.MotionEvent import android.view.MotionEvent
@ -40,11 +42,13 @@ import org.fossify.contacts.activities.ViewContactActivity
import org.fossify.contacts.dialogs.CreateNewGroupDialog import org.fossify.contacts.dialogs.CreateNewGroupDialog
import org.fossify.contacts.extensions.config import org.fossify.contacts.extensions.config
import org.fossify.contacts.extensions.editContact import org.fossify.contacts.extensions.editContact
import org.fossify.contacts.extensions.formatPhoneNumber
import org.fossify.contacts.extensions.shareContacts import org.fossify.contacts.extensions.shareContacts
import org.fossify.contacts.helpers.* import org.fossify.contacts.helpers.*
import org.fossify.contacts.interfaces.RefreshContactsListener import org.fossify.contacts.interfaces.RefreshContactsListener
import org.fossify.contacts.interfaces.RemoveFromGroupListener import org.fossify.contacts.interfaces.RemoveFromGroupListener
import java.util.Collections import java.util.Collections
import java.util.Locale
class ContactsAdapter( class ContactsAdapter(
activity: SimpleActivity, activity: SimpleActivity,
@ -155,6 +159,7 @@ class ContactsAdapter(
if (showPhoneNumbers) org.fossify.commons.R.layout.item_contact_with_number else org.fossify.commons.R.layout.item_contact_without_number if (showPhoneNumbers) org.fossify.commons.R.layout.item_contact_with_number else org.fossify.commons.R.layout.item_contact_without_number
} }
} }
return createViewHolder(layout, parent) return createViewHolder(layout, parent)
} }
@ -408,8 +413,12 @@ class ContactsAdapter(
} else { } else {
contact.phoneNumbers.firstOrNull { it.value.contains(textToHighlight) } ?: contact.phoneNumbers.firstOrNull() contact.phoneNumbers.firstOrNull { it.value.contains(textToHighlight) } ?: contact.phoneNumbers.firstOrNull()
} }
val phoneNumberToFormat = phoneNumberToUse?.value ?: ""
val numberText = phoneNumberToUse?.value ?: "" val numberText = if (config.formatPhoneNumbers) {
phoneNumberToFormat.formatPhoneNumber()
} else {
phoneNumberToUse?.value ?: ""
}
findViewById<TextView>(org.fossify.commons.R.id.item_contact_number).apply { findViewById<TextView>(org.fossify.commons.R.id.item_contact_number).apply {
text = if (textToHighlight.isEmpty()) numberText else numberText.highlightTextPart(textToHighlight, properPrimaryColor, false, true) text = if (textToHighlight.isEmpty()) numberText else numberText.highlightTextPart(textToHighlight, properPrimaryColor, false, true)
setTextColor(textColor) setTextColor(textColor)

View file

@ -140,4 +140,3 @@ fun SimpleActivity.tryImportContactsFromFile(uri: Uri, callback: (Boolean) -> Un
fun SimpleActivity.showImportContactsDialog(path: String, callback: (Boolean) -> Unit) { fun SimpleActivity.showImportContactsDialog(path: String, callback: (Boolean) -> Unit) {
ImportContactsDialog(this, path, callback) ImportContactsDialog(this, path, callback)
} }

View file

@ -0,0 +1,13 @@
package org.fossify.contacts.extensions
import android.telephony.PhoneNumberUtils
import java.util.Locale
fun String.formatPhoneNumber(minimumLength: Int = 4): String {
val country = Locale.getDefault().country
return if (this.length >= minimumLength) {
PhoneNumberUtils.formatNumber(this, country).toString()
} else {
this
}
}

View file

@ -9,6 +9,10 @@ class Config(context: Context) : BaseConfig(context) {
fun newInstance(context: Context) = Config(context) fun newInstance(context: Context) = Config(context)
} }
var formatPhoneNumbers: Boolean
get() = prefs.getBoolean(FORMAT_PHONE_NUMBERS, true)
set(formatPhoneNumbers) = prefs.edit().putBoolean(FORMAT_PHONE_NUMBERS, formatPhoneNumbers).apply()
var showTabs: Int var showTabs: Int
get() = prefs.getInt(SHOW_TABS, ALL_TABS_MASK) get() = prefs.getInt(SHOW_TABS, ALL_TABS_MASK)
set(showTabs) = prefs.edit().putInt(SHOW_TABS, showTabs).apply() set(showTabs) = prefs.edit().putInt(SHOW_TABS, showTabs).apply()

View file

@ -11,6 +11,7 @@ const val ADD_NEW_CONTACT_NUMBER = "add_new_contact_number"
const val DEFAULT_FILE_NAME = "contacts.vcf" const val DEFAULT_FILE_NAME = "contacts.vcf"
const val AVOID_CHANGING_TEXT_TAG = "avoid_changing_text_tag" const val AVOID_CHANGING_TEXT_TAG = "avoid_changing_text_tag"
const val AVOID_CHANGING_VISIBILITY_TAG = "avoid_changing_visibility_tag" const val AVOID_CHANGING_VISIBILITY_TAG = "avoid_changing_visibility_tag"
const val FORMAT_PHONE_NUMBERS = "format_phone_numbers"
const val AUTOMATIC_BACKUP_REQUEST_CODE = 10001 const val AUTOMATIC_BACKUP_REQUEST_CODE = 10001
const val AUTO_BACKUP_INTERVAL_IN_DAYS = 1 const val AUTO_BACKUP_INTERVAL_IN_DAYS = 1

View file

@ -327,6 +327,21 @@
</RelativeLayout> </RelativeLayout>
<RelativeLayout
android:id="@+id/settings_format_phone_numbers_holder"
style="@style/SettingsHolderCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<org.fossify.commons.views.MyAppCompatCheckbox
android:id="@+id/settings_format_phone_numbers"
style="@style/SettingsCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/format_phone_numbers" />
</RelativeLayout>
<RelativeLayout <RelativeLayout
android:id="@+id/settings_start_name_with_surname_holder" android:id="@+id/settings_start_name_with_surname_holder"
style="@style/SettingsHolderCheckboxStyle" style="@style/SettingsHolderCheckboxStyle"

View file

@ -42,6 +42,7 @@
<!-- Settings --> <!-- Settings -->
<string name="show_phone_numbers">Show phone numbers</string> <string name="show_phone_numbers">Show phone numbers</string>
<string name="show_contact_thumbnails">Show contact thumbnails</string> <string name="show_contact_thumbnails">Show contact thumbnails</string>
<string name="format_phone_numbers">Format phone numbers</string>
<string name="show_dialpad_button">Show a dialpad button on the main screen</string> <string name="show_dialpad_button">Show a dialpad button on the main screen</string>
<string name="on_contact_click">On contact click</string> <string name="on_contact_click">On contact click</string>
<string name="call_contact">Call contact</string> <string name="call_contact">Call contact</string>