From 39b25deb991bac80bfcbeb884912dd0cd2337e9e Mon Sep 17 00:00:00 2001 From: Karan Bedi Date: Fri, 18 Jan 2019 17:49:04 +0100 Subject: [PATCH] Detecting international extension from Locale, filtering invalid contacts --- .../rocket/android/contacts/models/Contact.kt | 6 +---- .../contacts/worker/ContactSyncWorker.kt | 25 ++++++++++++++++--- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/chat/rocket/android/contacts/models/Contact.kt b/app/src/main/java/chat/rocket/android/contacts/models/Contact.kt index e831192aab..e533a04b84 100644 --- a/app/src/main/java/chat/rocket/android/contacts/models/Contact.kt +++ b/app/src/main/java/chat/rocket/android/contacts/models/Contact.kt @@ -16,10 +16,6 @@ class Contact() : Parcelable { return username } - private fun formatPhoneNumber(phone: String): String { - return phone.replace("-|\\s|\\(|\\)".toRegex(), "") - } - fun getDetail(): String? { if(this.isPhone){ return getPhoneNumber() @@ -49,7 +45,7 @@ class Contact() : Parcelable { } fun setPhoneNumber(phoneNumber: String) { - this.phoneNumber = formatPhoneNumber(phoneNumber) + this.phoneNumber = phoneNumber } fun getEmailAddress(): String? { diff --git a/app/src/main/java/chat/rocket/android/contacts/worker/ContactSyncWorker.kt b/app/src/main/java/chat/rocket/android/contacts/worker/ContactSyncWorker.kt index a50bd18265..ec1581c80a 100644 --- a/app/src/main/java/chat/rocket/android/contacts/worker/ContactSyncWorker.kt +++ b/app/src/main/java/chat/rocket/android/contacts/worker/ContactSyncWorker.kt @@ -1,7 +1,9 @@ package chat.rocket.android.contacts.worker import android.content.Context +import android.os.Build import android.provider.ContactsContract +import android.telephony.PhoneNumberUtils import androidx.work.Worker import androidx.work.WorkerParameters import chat.rocket.android.contacts.models.Contact @@ -87,6 +89,13 @@ class ContactSyncWorker(context : Context, params : WorkerParameters) } private fun getContactList() { + var country = "" + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + country = applicationContext.getResources().getConfiguration().locales.get(0).country + } else{ + country = applicationContext.getResources().getConfiguration().locale.getCountry(); + } + val cr = applicationContext.contentResolver val cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null) @@ -108,10 +117,13 @@ class ContactSyncWorker(context : Context, params : WorkerParameters) while (pCur!!.moveToNext()) { val phoneNo = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)) - val contact = Contact() - contact.setName(name) - contact.setPhoneNumber(phoneNo) - contactArrayList.add(contact) + val formattedPhone = formatPhoneNumber(phoneNo, country) + if(formattedPhone!=null) { + val contact = Contact() + contact.setName(name) + contact.setPhoneNumber(formattedPhone) + contactArrayList.add(contact) + } } pCur.close() } @@ -156,4 +168,9 @@ class ContactSyncWorker(context : Context, params : WorkerParameters) return result.toString() } + + private fun formatPhoneNumber(phone: String, country:String): String? { + return PhoneNumberUtils.formatNumberToE164(phone.replace("-|\\s|\\(|\\)".toRegex(), ""), country) + } + }