Skip to content

Commit

Permalink
Merge pull request #198 from WideChat/karan_phone_extension
Browse files Browse the repository at this point in the history
 Detecting international extension from Locale, filtering invalid contacts
  • Loading branch information
ear-dev authored Jan 21, 2019
2 parents 15b2dd7 + 39b25de commit 54a1ea8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -49,7 +45,7 @@ class Contact() : Parcelable {
}

fun setPhoneNumber(phoneNumber: String) {
this.phoneNumber = formatPhoneNumber(phoneNumber)
this.phoneNumber = phoneNumber
}

fun getEmailAddress(): String? {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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()
}
Expand Down Expand Up @@ -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)
}

}

0 comments on commit 54a1ea8

Please sign in to comment.