diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 1a155a30..7cbbf297 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -81,7 +81,7 @@ dependencies { // third-party implementation(libs.lazycolumnscrollbar) // for scroll bar implementation(libs.reorderable) // for reordering Action items with drag & drop -// implementation(libs.libphonenumber) // for checking whether 33123 and +33123 are the same number, 600kb.. + implementation(libs.libphonenumber) // for checking whether 33123 and +33123 are the same number // jetbrains kotlinx implementation(libs.serialization.json) // for backup/restore json serialization diff --git a/app/src/main/java/spam/blocker/service/bot/Actions.kt b/app/src/main/java/spam/blocker/service/bot/Actions.kt index b2c97d0e..fd543a6a 100644 --- a/app/src/main/java/spam/blocker/service/bot/Actions.kt +++ b/app/src/main/java/spam/blocker/service/bot/Actions.kt @@ -12,6 +12,8 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.platform.LocalContext +import com.google.i18n.phonenumbers.PhoneNumberUtil +import com.google.i18n.phonenumbers.Phonenumber import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.DelicateCoroutinesApi @@ -1647,30 +1649,29 @@ class ParseIncomingNumber( return false } - // Use libphonenumber to check if the number has no leading + but is actually started with the CC. - // e.g.: 3312345, it's actually +3312345 but no leading + - // I'm trying to avoid using this library, because it increases the apk by 600kb. - // Here, the code assumes all international calls to start with leading +. - // If anyone has received number like 3312345, please report on github. - // Not sure if it's possible to have a number start with CC but has no leading `+`, // for instance: 33xxxxxxxx // Check the part xxxxxxxx, if it's still a valid number for cc==33, // then the xxxxxxxx is the domestic part. // Otherwise, the entire 33xxxxxxxx is a domestic number -// val rest = clearedNumber.substring(cc.toString().length) -// val pnUtil = PhoneNumberUtil.getInstance() -// val n = Phonenumber.PhoneNumber().apply { -// countryCode = cc -// nationalNumber = rest.toLong() -// } -// if (pnUtil.isValidNumber(n)) { // the number start with CC -// aCtx.cc = cc.toString() -// aCtx.domestic = rest -// } else { // it's simply a domestic number + if (clearedNumber.startsWith(cc.toString())) { + val rest = clearedNumber.substring(cc.toString().length) + val pnUtil = PhoneNumberUtil.getInstance() + val n = Phonenumber.PhoneNumber().apply { + countryCode = cc + nationalNumber = rest.toLong() + } + if (pnUtil.isValidNumber(n)) { // the number start with CC + aCtx.cc = cc.toString() + aCtx.domestic = rest + } else { // it's simply a domestic number + aCtx.cc = cc.toString() + aCtx.domestic = clearedNumber + } + } else { // it's simply a domestic number aCtx.cc = cc.toString() aCtx.domestic = clearedNumber -// } + } } return true diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index c6b840f0..788551bf 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,7 +5,7 @@ junitVersion = "1.2.1" kotlin = "2.0.0" activityCompose = "1.9.3" composeBom = "2024.12.01" -#libphonenumber = "8.13.52" +libphonenumber = "8.13.52" reorderable = "2.3.3" serialization = "1.7.1" mockk = "1.13.10" @@ -17,7 +17,7 @@ junitJupiter = "5.8.2" androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espressoCore" } androidx-junit = { module = "androidx.test.ext:junit", version.ref = "junitVersion" } androidx-work-runtime-ktx = { module = "androidx.work:work-runtime-ktx", version.ref = "workRuntimeKtx" } -#libphonenumber = { module = "com.googlecode.libphonenumber:libphonenumber", version.ref = "libphonenumber" } +libphonenumber = { module = "com.googlecode.libphonenumber:libphonenumber", version.ref = "libphonenumber" } mockk = { group = "io.mockk", name = "mockk-android", version.ref = "mockk" } reorderable = { module = "sh.calvin.reorderable:reorderable", version.ref = "reorderable" } serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "serialization" }