Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 57f8f0d7 authored by Brian Attwell's avatar Brian Attwell Committed by Etan Cohen
Browse files

Unhide and rename PhoneNumberUtils tts methods

Renamed ttsSpanAsPhoneNumber #1 -> getPhoneTtsSpan
Renamed ttsSpanAsPhoneNumber #2 -> addPhoneTtsSpan

Instead of removing the original hidden methods, I deprecated them.
Since Telephony changes initially get checked into wireless-dev
branch and dialer/contacts changes get checked into master branch,
immediately deleting the ttsSpanAsPhoneNumber methods would break
builds.

In ContactsCommon, I plan on removing
ContactDisplayUtils#getTelephoneTtsSpannable and instead using
these Telephony APIs. Numbers I tested worked in ToT master talkback:
911, (650) 215 3111, 22222222222, 333-3333, 013334612234, +1 905 471 8484,
001 39 02 444 4444. The changes in this diff cause the numbers
to be talked-back at least as well as before.

Some context: go/question-about-telephonys-ttsspan

Bug: 18777272
Change-Id: I3a6ead3ee9c030761bde903cf88f4aa4fc7a3460
parent 48d8442e
Loading
Loading
Loading
Loading
+3 −0
Original line number Original line Diff line number Diff line
@@ -28466,6 +28466,7 @@ package android.telephony {
  public class PhoneNumberUtils {
  public class PhoneNumberUtils {
    ctor public PhoneNumberUtils();
    ctor public PhoneNumberUtils();
    method public static void addPhoneTtsSpan(android.text.Spannable, int, int);
    method public static java.lang.String calledPartyBCDFragmentToString(byte[], int, int);
    method public static java.lang.String calledPartyBCDFragmentToString(byte[], int, int);
    method public static java.lang.String calledPartyBCDToString(byte[], int, int);
    method public static java.lang.String calledPartyBCDToString(byte[], int, int);
    method public static boolean compare(java.lang.String, java.lang.String);
    method public static boolean compare(java.lang.String, java.lang.String);
@@ -28482,6 +28483,8 @@ package android.telephony {
    method public static java.lang.String formatNumberToE164(java.lang.String, java.lang.String);
    method public static java.lang.String formatNumberToE164(java.lang.String, java.lang.String);
    method public static deprecated int getFormatTypeForLocale(java.util.Locale);
    method public static deprecated int getFormatTypeForLocale(java.util.Locale);
    method public static java.lang.String getNumberFromIntent(android.content.Intent, android.content.Context);
    method public static java.lang.String getNumberFromIntent(android.content.Intent, android.content.Context);
    method public static android.text.style.TtsSpan getPhoneTtsSpan(java.lang.String);
    method public static java.lang.CharSequence getPhoneTtsSpannable(java.lang.CharSequence);
    method public static java.lang.String getStrippedReversed(java.lang.String);
    method public static java.lang.String getStrippedReversed(java.lang.String);
    method public static final boolean is12Key(char);
    method public static final boolean is12Key(char);
    method public static final boolean isDialable(char);
    method public static final boolean isDialable(char);
+75 −13
Original line number Original line Diff line number Diff line
@@ -34,11 +34,11 @@ import android.text.Editable;
import android.text.Spannable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextUtils;
import android.telephony.Rlog;
import android.text.style.TtsSpan;
import android.text.style.TtsSpan;
import android.util.SparseIntArray;
import android.util.SparseIntArray;


import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
import static com.android.internal.telephony.TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY;
import static com.android.internal.telephony.TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY;
import static com.android.internal.telephony.TelephonyProperties.PROPERTY_OPERATOR_IDP_STRING;
import static com.android.internal.telephony.TelephonyProperties.PROPERTY_OPERATOR_IDP_STRING;


import java.util.Locale;
import java.util.Locale;
@@ -2311,15 +2311,13 @@ public class PhoneNumberUtils
     *
     *
     * @param phoneNumber A {@code CharSequence} the entirety of which represents a phone number.
     * @param phoneNumber A {@code CharSequence} the entirety of which represents a phone number.
     * @return A {@code CharSequence} with appropriate annotations.
     * @return A {@code CharSequence} with appropriate annotations.
     *
     * @hide
     */
     */
    public static CharSequence ttsSpanAsPhoneNumber(CharSequence phoneNumber) {
    public static CharSequence getPhoneTtsSpannable(CharSequence phoneNumber) {
        if (phoneNumber == null) {
        if (phoneNumber == null) {
            return null;
            return null;
        }
        }
        Spannable spannable = Spannable.Factory.getInstance().newSpannable(phoneNumber);
        Spannable spannable = Spannable.Factory.getInstance().newSpannable(phoneNumber);
        PhoneNumberUtils.ttsSpanAsPhoneNumber(spannable, 0, spannable.length());
        PhoneNumberUtils.addPhoneTtsSpan(spannable, 0, spannable.length());
        return spannable;
        return spannable;
    }
    }


@@ -2330,19 +2328,83 @@ public class PhoneNumberUtils
     * @param s A {@code Spannable} to annotate.
     * @param s A {@code Spannable} to annotate.
     * @param start The starting character position of the phone number in {@code s}.
     * @param start The starting character position of the phone number in {@code s}.
     * @param end The ending character position of the phone number in {@code s}.
     * @param end The ending character position of the phone number in {@code s}.
     *
     * @hide
     */
     */
    public static void ttsSpanAsPhoneNumber(Spannable s, int start, int end) {
    public static void addPhoneTtsSpan(Spannable s, int start, int end) {
        s.setSpan(
        s.setSpan(getPhoneTtsSpan(s.subSequence(start, end).toString()),
                new TtsSpan.TelephoneBuilder()
                        .setNumberParts(splitAtNonNumerics(s.subSequence(start, end)))
                        .build(),
                start,
                start,
                end,
                end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    }


    /**
     * Wrap the supplied {@code CharSequence} with a {@code TtsSpan}, annotating it as
     * containing a phone number in its entirety.
     *
     * @param phoneNumber A {@code CharSequence} the entirety of which represents a phone number.
     * @return A {@code CharSequence} with appropriate annotations.
     * @deprecated Renamed {@link #getPhoneTtsSpannable}.
     *
     * @hide
     */
    @Deprecated
    public static CharSequence ttsSpanAsPhoneNumber(CharSequence phoneNumber) {
        return getPhoneTtsSpannable(phoneNumber);
    }

    /**
     * Attach a {@link TtsSpan} to the supplied {@code Spannable} at the indicated location,
     * annotating that location as containing a phone number.
     *
     * @param s A {@code Spannable} to annotate.
     * @param start The starting character position of the phone number in {@code s}.
     * @param end The ending character position of the phone number in {@code s}.
     *
     * @deprecated Renamed {@link #addPhoneTtsSpan}.
     *
     * @hide
     */
    @Deprecated
    public static void ttsSpanAsPhoneNumber(Spannable s, int start, int end) {
        addPhoneTtsSpan(s, start, end);
    }

    /**
     * Create a {@code TtsSpan} for the supplied {@code String}.
     *
     * @param phoneNumberString A {@code String} the entirety of which represents a phone number.
     * @return A {@code TtsSpan} for {@param phoneNumberString}.
     */
    public static TtsSpan getPhoneTtsSpan(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 like "+20(123)-456#" using spaces, ignoring anything that is not
    // Split a phone number like "+20(123)-456#" using spaces, ignoring anything that is not
    // a digit, to produce a result like "20 123 456".
    // a digit, to produce a result like "20 123 456".
    private static String splitAtNonNumerics(CharSequence number) {
    private static String splitAtNonNumerics(CharSequence number) {