summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrace Jia <xiaotonj@google.com>2019-10-17 17:47:12 -0700
committerandroid-build-merger <android-build-merger@google.com>2019-10-17 17:47:12 -0700
commit03ebff76fda70ac1de4e1b679f94cd6483b26c00 (patch)
tree33d151b914cdb28e17e3febf0a284f22f04ed95e
parent9f955679260791993d93d7cbe1ca9054e1dc7e2a (diff)
parent28d36664b80e5ba01ca0f7e8b98865c0704d5e15 (diff)
downloadBlockedNumberProvider-03ebff76fda70ac1de4e1b679f94cd6483b26c00.tar.gz
Implemented piiHandle to avoid importing it from Telecomm package. am: 0edf1e3bc0 am: 81c0c27570
am: 28d36664b8 Change-Id: I8a80c29e6e2d25a97bc8a83f6f4d37c69a25fa57
-rw-r--r--src/com/android/providers/blockednumber/BlockedNumberProvider.java2
-rw-r--r--src/com/android/providers/blockednumber/Utils.java100
2 files changed, 101 insertions, 1 deletions
diff --git a/src/com/android/providers/blockednumber/BlockedNumberProvider.java b/src/com/android/providers/blockednumber/BlockedNumberProvider.java
index 2e029d8..4d6a284 100644
--- a/src/com/android/providers/blockednumber/BlockedNumberProvider.java
+++ b/src/com/android/providers/blockednumber/BlockedNumberProvider.java
@@ -15,7 +15,7 @@
*/
package com.android.providers.blockednumber;
-import static android.telecom.Log.piiHandle;
+import static com.android.providers.blockednumber.Utils.piiHandle;
import android.Manifest;
import android.annotation.NonNull;
diff --git a/src/com/android/providers/blockednumber/Utils.java b/src/com/android/providers/blockednumber/Utils.java
index 6aaf178..66f0fcb 100644
--- a/src/com/android/providers/blockednumber/Utils.java
+++ b/src/com/android/providers/blockednumber/Utils.java
@@ -15,21 +15,33 @@
*/
package com.android.providers.blockednumber;
+import static android.util.Log.isLoggable;
+
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.location.Country;
import android.location.CountryDetector;
+import android.net.Uri;
+import android.os.Build;
+import android.telecom.PhoneAccount;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import java.util.Locale;
public class Utils {
+ /**
+ * When generating a bug report, include the last X dialable digits when logging phone numbers.
+ */
+ private static final int NUM_DIALABLE_DIGITS_TO_LOG = Build.IS_USER ? 0 : 2;
+
private Utils() {
}
public static final int MIN_INDEX_LEN = 8;
+ public static String TAG = "BlockedNumberProvider";
+ public static boolean VERBOSE = isLoggable(TAG, android.util.Log.VERBOSE);
/**
* @return The current country code.
@@ -74,4 +86,92 @@ public class Utils {
public static @Nullable String wrapSelectionWithParens(@Nullable String selection) {
return TextUtils.isEmpty(selection) ? null : "(" + selection + ")";
}
+
+ /**
+ * Generates an obfuscated string for a calling handle in {@link Uri} format, or a raw phone
+ * phone number in {@link String} format.
+ * @param pii The information to obfuscate.
+ * @return The obfuscated string.
+ */
+ public static String piiHandle(Object pii) {
+ if (pii == null || VERBOSE) {
+ return String.valueOf(pii);
+ }
+
+ StringBuilder sb = new StringBuilder();
+ if (pii instanceof Uri) {
+ Uri uri = (Uri) pii;
+ String scheme = uri.getScheme();
+
+ if (!TextUtils.isEmpty(scheme)) {
+ sb.append(scheme).append(":");
+ }
+
+ String textToObfuscate = uri.getSchemeSpecificPart();
+ if (PhoneAccount.SCHEME_TEL.equals(scheme)) {
+ obfuscatePhoneNumber(sb, textToObfuscate);
+ } else if (PhoneAccount.SCHEME_SIP.equals(scheme)) {
+ for (int i = 0; i < textToObfuscate.length(); i++) {
+ char c = textToObfuscate.charAt(i);
+ if (c != '@' && c != '.') {
+ c = '*';
+ }
+ sb.append(c);
+ }
+ } else {
+ sb.append(pii(pii));
+ }
+ } else if (pii instanceof String) {
+ String number = (String) pii;
+ obfuscatePhoneNumber(sb, number);
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Obfuscates a phone number, allowing NUM_DIALABLE_DIGITS_TO_LOG digits to be exposed for the
+ * phone number.
+ * @param sb String buffer to write obfuscated number to.
+ * @param phoneNumber The number to obfuscate.
+ */
+ private static void obfuscatePhoneNumber(StringBuilder sb, String phoneNumber) {
+ int numDigitsToObfuscate = getDialableCount(phoneNumber)
+ - NUM_DIALABLE_DIGITS_TO_LOG;
+ for (int i = 0; i < phoneNumber.length(); i++) {
+ char c = phoneNumber.charAt(i);
+ boolean isDialable = PhoneNumberUtils.isDialable(c);
+ if (isDialable) {
+ numDigitsToObfuscate--;
+ }
+ sb.append(isDialable && numDigitsToObfuscate >= 0 ? "*" : c);
+ }
+ }
+
+ /**
+ * Redact personally identifiable information for production users.
+ * If we are running in verbose mode, return the original string,
+ * and return "***" otherwise.
+ */
+ public static String pii(Object pii) {
+ if (pii == null || VERBOSE) {
+ return String.valueOf(pii);
+ }
+ return "***";
+ }
+
+ /**
+ * Determines the number of dialable characters in a string.
+ * @param toCount The string to count dialable characters in.
+ * @return The count of dialable characters.
+ */
+ private static int getDialableCount(String toCount) {
+ int numDialable = 0;
+ for (char c : toCount.toCharArray()) {
+ if (PhoneNumberUtils.isDialable(c)) {
+ numDialable++;
+ }
+ }
+ return numDialable;
+ }
}