summaryrefslogtreecommitdiff
path: root/java/com/android/contacts/common/compat/PhoneNumberUtilsCompat.java
diff options
context:
space:
mode:
authorEric Erfanian <erfanian@google.com>2017-06-05 13:35:02 -0700
committerEric Erfanian <erfanian@google.com>2017-06-07 20:44:54 +0000
commit91ce7d2a476bd04fe525049a37a2f8b2824e9724 (patch)
treeb9bbc285430ffb5363a70eb27e382c38f5a85b7a /java/com/android/contacts/common/compat/PhoneNumberUtilsCompat.java
parent75233ff03785f24789b32039ac2c208805b7e506 (diff)
Update AOSP Dialer source from internal google3 repository at
cl/158012278. Test: make, treehugger This CL updates the AOSP Dialer source with all the changes that have gone into the private google3 repository. This includes all the changes from cl/152373142 (4/06/2017) to cl/158012278 (6/05/2017). This goal of these drops is to keep the AOSP source in sync with the internal google3 repository. Currently these sync are done by hand with very minor modifications to the internal source code. See the Android.mk file for list of modifications. Our current goal is to do frequent drops (daily if possible) and eventually switched to an automated process. Change-Id: I4d3f14b5140e2e51bead9497bc118a205b3ebe76
Diffstat (limited to 'java/com/android/contacts/common/compat/PhoneNumberUtilsCompat.java')
-rw-r--r--java/com/android/contacts/common/compat/PhoneNumberUtilsCompat.java132
1 files changed, 2 insertions, 130 deletions
diff --git a/java/com/android/contacts/common/compat/PhoneNumberUtilsCompat.java b/java/com/android/contacts/common/compat/PhoneNumberUtilsCompat.java
index 960b340d8..a6cfe07cd 100644
--- a/java/com/android/contacts/common/compat/PhoneNumberUtilsCompat.java
+++ b/java/com/android/contacts/common/compat/PhoneNumberUtilsCompat.java
@@ -17,13 +17,7 @@
package com.android.contacts.common.compat;
import android.telephony.PhoneNumberUtils;
-import android.text.Spannable;
-import android.text.TextUtils;
import android.text.style.TtsSpan;
-import com.android.dialer.compat.CompatUtils;
-import com.google.i18n.phonenumbers.NumberParseException;
-import com.google.i18n.phonenumbers.PhoneNumberUtil;
-import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
/**
* This class contains static utility methods extracted from PhoneNumberUtils, and the methods were
@@ -37,138 +31,16 @@ public class PhoneNumberUtilsCompat {
/** Not instantiable. */
private PhoneNumberUtilsCompat() {}
- public static String normalizeNumber(String phoneNumber) {
- if (CompatUtils.isLollipopCompatible()) {
- return PhoneNumberUtils.normalizeNumber(phoneNumber);
- } else {
- return normalizeNumberInternal(phoneNumber);
- }
- }
-
- /** Implementation copied from {@link PhoneNumberUtils#normalizeNumber} */
- private static String normalizeNumberInternal(String phoneNumber) {
- if (TextUtils.isEmpty(phoneNumber)) {
- return "";
- }
- StringBuilder sb = new StringBuilder();
- int len = phoneNumber.length();
- for (int i = 0; i < len; i++) {
- char c = phoneNumber.charAt(i);
- // Character.digit() supports ASCII and Unicode digits (fullwidth, Arabic-Indic, etc.)
- int digit = Character.digit(c, 10);
- if (digit != -1) {
- sb.append(digit);
- } else if (sb.length() == 0 && c == '+') {
- sb.append(c);
- } else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
- return normalizeNumber(PhoneNumberUtils.convertKeypadLettersToDigits(phoneNumber));
- }
- }
- return sb.toString();
- }
-
public static String formatNumber(
String phoneNumber, String phoneNumberE164, String defaultCountryIso) {
- if (CompatUtils.isLollipopCompatible()) {
return PhoneNumberUtils.formatNumber(phoneNumber, phoneNumberE164, defaultCountryIso);
- } else {
- // This method was deprecated in API level 21, so it's only used on pre-L SDKs.
- return PhoneNumberUtils.formatNumber(phoneNumber);
- }
}
public static CharSequence createTtsSpannable(CharSequence phoneNumber) {
- if (CompatUtils.isMarshmallowCompatible()) {
- return PhoneNumberUtils.createTtsSpannable(phoneNumber);
- } else {
- return createTtsSpannableInternal(phoneNumber);
- }
+ return PhoneNumberUtils.createTtsSpannable(phoneNumber);
}
public static TtsSpan createTtsSpan(String phoneNumber) {
- if (CompatUtils.isMarshmallowCompatible()) {
- return PhoneNumberUtils.createTtsSpan(phoneNumber);
- } else if (CompatUtils.isLollipopCompatible()) {
- return createTtsSpanLollipop(phoneNumber);
- } else {
- return null;
- }
- }
-
- /** Copied from {@link PhoneNumberUtils#createTtsSpannable} */
- private static CharSequence createTtsSpannableInternal(CharSequence phoneNumber) {
- if (phoneNumber == null) {
- return null;
- }
- Spannable spannable = Spannable.Factory.getInstance().newSpannable(phoneNumber);
- addTtsSpanInternal(spannable, 0, spannable.length());
- return spannable;
- }
-
- /** Compat method for addTtsSpan, see {@link PhoneNumberUtils#addTtsSpan} */
- public static void addTtsSpan(Spannable s, int start, int endExclusive) {
- if (CompatUtils.isMarshmallowCompatible()) {
- PhoneNumberUtils.addTtsSpan(s, start, endExclusive);
- } else {
- addTtsSpanInternal(s, start, endExclusive);
- }
- }
-
- /** Copied from {@link PhoneNumberUtils#addTtsSpan} */
- private static void addTtsSpanInternal(Spannable s, int start, int endExclusive) {
- s.setSpan(
- createTtsSpan(s.subSequence(start, endExclusive).toString()),
- start,
- endExclusive,
- Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- }
-
- /** Copied from {@link PhoneNumberUtils#createTtsSpan} */
- private static TtsSpan createTtsSpanLollipop(String phoneNumberString) {
- if (phoneNumberString == null) {
- return null;
- }
-
- // Parse the phone number
- final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
- PhoneNumber phoneNumber = null;
- try {
- // Don't supply a defaultRegion so this fails for non-international numbers because
- // we don't want to TalkBalk to read a country code (e.g. +1) if it is not already
- // present
- phoneNumber = phoneNumberUtil.parse(phoneNumberString, /* defaultRegion */ null);
- } catch (NumberParseException ignored) {
- }
-
- // Build a telephone tts span
- final TtsSpan.TelephoneBuilder builder = new TtsSpan.TelephoneBuilder();
- if (phoneNumber == null) {
- // Strip separators otherwise TalkBack will be silent
- // (this behavior was observed with TalkBalk 4.0.2 from their alpha channel)
- builder.setNumberParts(splitAtNonNumerics(phoneNumberString));
- } else {
- if (phoneNumber.hasCountryCode()) {
- builder.setCountryCode(Integer.toString(phoneNumber.getCountryCode()));
- }
- builder.setNumberParts(Long.toString(phoneNumber.getNationalNumber()));
- }
- return builder.build();
- }
-
- /**
- * Split a phone number using spaces, ignoring anything that is not a digit
- *
- * @param number A {@code CharSequence} before splitting, e.g., "+20(123)-456#"
- * @return A {@code String} after splitting, e.g., "20 123 456".
- */
- private static String splitAtNonNumerics(CharSequence number) {
- StringBuilder sb = new StringBuilder(number.length());
- for (int i = 0; i < number.length(); i++) {
- sb.append(PhoneNumberUtils.isISODigit(number.charAt(i)) ? number.charAt(i) : " ");
- }
- // It is very important to remove extra spaces. At time of writing, any leading or trailing
- // spaces, or any sequence of more than one space, will confuse TalkBack and cause the TTS
- // span to be non-functional!
- return sb.toString().replaceAll(" +", " ").trim();
+ return PhoneNumberUtils.createTtsSpan(phoneNumber);
}
}