From bf0d04e4de1253057c993501301f69466d1773e2 Mon Sep 17 00:00:00 2001 From: Michael W Date: Wed, 11 Sep 2019 13:59:30 +0200 Subject: [PATCH] SensitivePn: Also hide international numbers * When calling the number with intl. prefix it is currently not hidden * Remove the international prefix before checking th number Test: Before: 116006 - hidden +49116006 - not hidden 004911606 - not hidden After: 116006 - hidden +49116006 - hidden 0049116006 - hidden Change-Id: I72ec2c9a4da87ef243c59c8c4bab33585fdbd854 --- Android.bp | 6 ++-- .../internal/phone/SensitivePhoneNumbers.java | 30 +++++++++++++++++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/Android.bp b/Android.bp index ac1e6531..84831378 100644 --- a/Android.bp +++ b/Android.bp @@ -78,7 +78,8 @@ java_library { name: "org.lineageos.platform", installable: true, static_libs: [ - "telephony-ext" + "libphonenumber", + "telephony-ext", ] + lineage_sdk_LOCAL_STATIC_ANDROID_LIBRARIES + lineage_sdk_LOCAL_STATIC_JAVA_LIBRARIES, libs: [ @@ -106,7 +107,8 @@ java_library { name: "org.lineageos.platform.internal", required: ["services"], static_libs: [ - "telephony-ext" + "libphonenumber", + "telephony-ext", ] + lineage_sdk_LOCAL_STATIC_ANDROID_LIBRARIES + lineage_sdk_LOCAL_STATIC_JAVA_LIBRARIES, srcs: [ diff --git a/sdk/src/java/org/lineageos/internal/phone/SensitivePhoneNumbers.java b/sdk/src/java/org/lineageos/internal/phone/SensitivePhoneNumbers.java index a87e2b09..8d917600 100644 --- a/sdk/src/java/org/lineageos/internal/phone/SensitivePhoneNumbers.java +++ b/sdk/src/java/org/lineageos/internal/phone/SensitivePhoneNumbers.java @@ -27,6 +27,11 @@ import android.text.TextUtils; import android.util.Log; import android.util.Xml; +import com.google.i18n.phonenumbers.NumberParseException; +import com.google.i18n.phonenumbers.Phonenumber; +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; + import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -37,6 +42,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Locale; public class SensitivePhoneNumbers { private final String LOG_TAG = this.getClass().getSimpleName(); @@ -98,14 +104,15 @@ public class SensitivePhoneNumbers { } public boolean isSensitiveNumber(Context context, String numberToCheck, int subId) { - SubscriptionManager subManager = context.getSystemService(SubscriptionManager.class); + String nationalNumber = formatNumberToNational(context, numberToCheck); + SubscriptionManager subManager = context.getSystemService(SubscriptionManager.class); List list = subManager.getActiveSubscriptionInfoList(); if (list != null) { // Test all subscriptions so an accidential use of a wrong sim also hides the number for (SubscriptionInfo subInfo : list) { String mcc = String.valueOf(subInfo.getMcc()); - if (isSensitiveNumber(numberToCheck, mcc)) { + if (isSensitiveNumber(nationalNumber, mcc)) { return true; } } @@ -118,7 +125,7 @@ public class SensitivePhoneNumbers { String networkUsed = telephonyManager.getNetworkOperator(subId); if (!TextUtils.isEmpty(networkUsed)) { String networkMCC = networkUsed.substring(0, 3); - return isSensitiveNumber(numberToCheck, networkMCC); + return isSensitiveNumber(nationalNumber, networkMCC); } } @@ -137,4 +144,21 @@ public class SensitivePhoneNumbers { } return false; } + + private String formatNumberToNational(Context context, String number) { + PhoneNumberUtil util = PhoneNumberUtil.getInstance(); + String countryIso = context.getResources().getConfiguration().locale.getCountry(); + + Phonenumber.PhoneNumber pn = null; + try { + pn = util.parse(number, countryIso); + } catch (NumberParseException e) { + } + + if (pn != null) { + return util.format(pn, PhoneNumberFormat.NATIONAL); + } else { + return number; + } + } }