forked from upstream-mirrors/quik
78 lines
2.5 KiB
Kotlin
78 lines
2.5 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 {
|
|
if (first.equals(second, true)) {
|
|
return true
|
|
}
|
|
|
|
if (PhoneNumberUtils.compare(first, second)) {
|
|
val matchType = phoneNumberUtil.isNumberMatch(first, second)
|
|
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) }
|
|
}
|
|
|
|
}
|