forked from upstream-mirrors/quik
6029f56d04
When QUIK receives a message it compares to see if it goes into a new conversation or an existing. However, if the numbers are formatted differently, it will think that they are separate convos. Hopefully, normalizing before comparing will fix that.
80 lines
2.7 KiB
Kotlin
80 lines
2.7 KiB
Kotlin
/*
|
|
* 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 dev.octoshrimpy.quik.util
|
|
|
|
import android.content.Context
|
|
import android.telephony.PhoneNumberUtils
|
|
import io.michaelrocks.libphonenumber.android.PhoneNumberUtil
|
|
import io.michaelrocks.libphonenumber.android.Phonenumber
|
|
import java.util.Locale
|
|
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 {
|
|
val normalizedFirst = normalizeNumber(first)
|
|
val normalizedSecond = normalizeNumber(second)
|
|
if (normalizedFirst.equals(normalizedSecond, true)) {
|
|
return true
|
|
}
|
|
|
|
if (PhoneNumberUtils.compare(first, second)) {
|
|
val matchType = phoneNumberUtil.isNumberMatch(normalizedFirst, normalizedSecond)
|
|
if (matchType >= PhoneNumberUtil.MatchType.SHORT_NSN_MATCH) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
fun isPossibleNumber(number: CharSequence): Boolean {
|
|
return parse(number) != null
|
|
}
|
|
|
|
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) }
|
|
}
|
|
|
|
}
|