Stricter check for phone number equality

This commit is contained in:
moezbhatti
2019-10-05 19:42:05 -04:00
parent c2bcf56925
commit 56d13dfe3e
19 changed files with 151 additions and 62 deletions
@@ -20,7 +20,6 @@ package com.moez.QKSMS.util
import android.content.Context
import android.provider.ContactsContract
import android.telephony.PhoneNumberUtils
import com.bumptech.glide.Priority
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.Key
@@ -38,11 +37,12 @@ import java.security.MessageDigest
class ContactImageLoader(
private val context: Context,
private val contactRepo: ContactRepository
private val contactRepo: ContactRepository,
private val phoneNumberUtils: PhoneNumberUtils
) : ModelLoader<String, InputStream> {
override fun handles(model: String): Boolean {
return PhoneNumberUtils.isGlobalPhoneNumber(model)
return phoneNumberUtils.isPossibleNumber(model)
}
override fun buildLoadData(
@@ -51,12 +51,18 @@ class ContactImageLoader(
height: Int,
options: Options
): ModelLoader.LoadData<InputStream>? {
return ModelLoader.LoadData(ContactImageKey(model), ContactImageFetcher(context, contactRepo, model))
return ModelLoader.LoadData(
ContactImageKey(phoneNumberUtils.normalizeNumber(model)),
ContactImageFetcher(context, contactRepo, model))
}
class Factory(val context: Context, val prefs: Preferences) : ModelLoaderFactory<String, InputStream> {
class Factory(
private val context: Context,
private val prefs: Preferences
) : ModelLoaderFactory<String, InputStream> {
override fun build(multiFactory: MultiModelLoaderFactory): ContactImageLoader {
return ContactImageLoader(context, ContactRepositoryImpl(context, prefs))
return ContactImageLoader(context, ContactRepositoryImpl(context, prefs), PhoneNumberUtils(context))
}
override fun teardown() {} // nothing to do here
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2019 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.util
import android.content.Context
import android.telephony.PhoneNumberUtils
import io.michaelrocks.libphonenumber.android.PhoneNumberUtil
import io.michaelrocks.libphonenumber.android.Phonenumber
import java.util.*
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class PhoneNumberUtils @Inject constructor(context: Context) {
private val countryCode = Locale.getDefault().country
private val phoneNumberUtil = PhoneNumberUtil.createInstance(context)
/**
* Android's implementation is too loose and causes false positives
* libphonenumber is stricter but too slow
*
* This method will run successfully stricter checks without compromising much speed
*/
fun compare(first: String, second: String): Boolean {
if (first.equals(second, true)) {
return true
}
if (PhoneNumberUtils.compare(first, second)) {
if (phoneNumberUtil.isNumberMatch(first, second) >= PhoneNumberUtil.MatchType.NSN_MATCH) {
return true
}
}
return false
}
fun isPossibleNumber(number: CharSequence): Boolean {
val phoneNumber = parse(number) ?: return false
return phoneNumberUtil.isPossibleNumber(phoneNumber)
}
fun isReallyDialable(digit: Char): Boolean {
return PhoneNumberUtils.isReallyDialable(digit)
}
fun formatNumber(number: CharSequence): String {
// PhoneNumberUtil doesn't maintain country code input
return PhoneNumberUtils.formatNumber(number.toString(), countryCode) ?: number.toString()
}
fun normalizeNumber(number: String): String {
return PhoneNumberUtils.stripSeparators(number)
}
private fun parse(number: CharSequence): Phonenumber.PhoneNumber? {
return tryOrNull(false) { phoneNumberUtil.parse(number, countryCode) }
}
}