Skip to content

Commit 7b4f476

Browse files
[feat]
1. 한국 전화 번호 지역 번호 형태로 format되어 표시되도록 수정
1 parent 839bf6d commit 7b4f476

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

app/src/main/java/ch/lock/mobile/android/deviceinfo/di/UtilsModule.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ object UtilsModule {
1313
get()
1414
)
1515
}
16+
single {
17+
FormatUtils()
18+
}
1619
single {
1720
PermissionUtils()
1821
}
@@ -23,6 +26,7 @@ object UtilsModule {
2326
} bind ResourceProvider::class
2427
single {
2528
SimUtils(
29+
get(),
2630
get()
2731
)
2832
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package ch.lock.mobile.android.deviceinfo.utils
2+
3+
import java.util.regex.Pattern
4+
5+
open class FormatUtils {
6+
7+
companion object {
8+
const val TAG: String = "FormatUtils"
9+
}
10+
11+
/**
12+
* 국제 번호를 지역 번호 형태로 format
13+
*/
14+
open fun toLocalPhoneNumber(
15+
internationalNumber: String?,
16+
includeHyphen: Boolean = false
17+
): String {
18+
if (internationalNumber.isNullOrEmpty()) return ""
19+
20+
var toLocalPhoneNumber: String = Pattern.compile("(^[+]82)|(^82)")
21+
.matcher(internationalNumber)
22+
.replaceAll("0")
23+
24+
try {
25+
if (includeHyphen) {
26+
toLocalPhoneNumber = when (toLocalPhoneNumber.length) {
27+
8 -> {
28+
toLocalPhoneNumber.replaceFirst("^([0-9]{4})([0-9]{4})$".toRegex(), "$1-$2")
29+
}
30+
12 -> {
31+
toLocalPhoneNumber.replaceFirst(
32+
"(^[0-9]{4})([0-9]{4})([0-9]{4})$".toRegex(),
33+
"$1-$2-$3"
34+
)
35+
}
36+
else -> {
37+
toLocalPhoneNumber.replaceFirst(
38+
"(^02|[0-9]{3})([0-9]{3,4})([0-9]{4})$".toRegex(),
39+
"$1-$2-$3"
40+
)
41+
}
42+
}
43+
}
44+
return toLocalPhoneNumber
45+
} catch (e: Exception) {
46+
return toLocalPhoneNumber
47+
}
48+
}
49+
50+
}

app/src/main/java/ch/lock/mobile/android/deviceinfo/utils/SimUtils.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import androidx.annotation.RequiresApi
99

1010
@SuppressLint("MissingPermission", "HardwareIds")
1111
open class SimUtils(
12-
private val applicationContext: Context
12+
private val applicationContext: Context,
13+
private val formatUtils: FormatUtils
1314
) {
1415

1516
companion object {
@@ -31,7 +32,7 @@ open class SimUtils(
3132
get() = try {
3233
when {
3334
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 ->
34-
subscriptionManager.activeSubscriptionInfoList?.firstOrNull()?.iccId ?: "유심이 장착되어 있지 않습니다."
35+
subscriptionManager.activeSubscriptionInfoList?.firstOrNull()?.iccId ?: ""
3536
else -> telephonyManager.simSerialNumber ?: ""
3637
}
3738
} catch (e: Exception) {
@@ -49,9 +50,23 @@ open class SimUtils(
4950
}
5051

5152
/**
53+
* 지역 번호
54+
*
5255
* @return 디바이스 전화번호
5356
*/
5457
open val phoneNumber: String
58+
get() = try {
59+
formatUtils.toLocalPhoneNumber(globalPhoneNumber)
60+
} catch (e: Exception) {
61+
""
62+
}
63+
64+
/**
65+
* 국제 번호
66+
*
67+
* @return 디바이스 전화번호
68+
*/
69+
open val globalPhoneNumber: String
5570
get() = try {
5671
telephonyManager.line1Number ?: ""
5772
} catch (e: Exception) {

0 commit comments

Comments
 (0)