Skip to content

Commit

Permalink
fix libphonenubmer checkings
Browse files Browse the repository at this point in the history
  • Loading branch information
aj3423 committed Dec 16, 2024
1 parent 6774b48 commit 6dd31ea
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 18 additions & 17 deletions app/src/main/java/spam/blocker/service/bot/Actions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" }
Expand Down

0 comments on commit 6dd31ea

Please sign in to comment.