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

Commit dc8f3ef5 authored by Roozbeh Pournader's avatar Roozbeh Pournader
Browse files

Make Linkify use the SIM card's country for phone numbers

Previously, it was using the locale's country code, which doesn't
make sense, since the locale's country code means which dialect of
the language the system should function in.  We should use the SIM
card's country code instead.

Change-Id: Ib942f92f3218c943669fc6150d6cc6b4e340b451
Fixes: 30000652
Test: cts-tradefed run cts-dev --module CtsTextTestCases
Test: adb shell am instrument -w -e package android.text com.android.frameworks.coretests/android.support.test.runner.AndroidJUnitRunner
Test: Manual
parent 1b581e46
Loading
Loading
Loading
Loading
+19 −7
Original line number Diff line number Diff line
@@ -19,7 +19,9 @@ package android.text.util;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.telephony.PhoneNumberUtils;
import android.telephony.TelephonyManager;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
@@ -221,6 +223,11 @@ public class Linkify {
     *  @return True if at least one link is found and applied.
     */
    public static final boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask) {
        return addLinks(text, mask, null);
    }

    private static boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask,
            @Nullable Context context) {
        if (mask == 0) {
            return false;
        }
@@ -246,7 +253,7 @@ public class Linkify {
        }

        if ((mask & PHONE_NUMBERS) != 0) {
            gatherTelLinks(links, text);
            gatherTelLinks(links, text, context);
        }

        if ((mask & MAP_ADDRESSES) != 0) {
@@ -282,10 +289,10 @@ public class Linkify {
            return false;
        }

        CharSequence t = text.getText();

        final Context context = text.getContext();
        final CharSequence t = text.getText();
        if (t instanceof Spannable) {
            if (addLinks((Spannable) t, mask)) {
            if (addLinks((Spannable) t, mask, context)) {
                addLinkMovementMethod(text);
                return true;
            }
@@ -294,7 +301,7 @@ public class Linkify {
        } else {
            SpannableString s = SpannableString.valueOf(t);

            if (addLinks(s, mask)) {
            if (addLinks(s, mask, context)) {
                addLinkMovementMethod(text);
                text.setText(s);

@@ -528,10 +535,15 @@ public class Linkify {
        }
    }

    private static final void gatherTelLinks(ArrayList<LinkSpec> links, Spannable s) {
    private static void gatherTelLinks(ArrayList<LinkSpec> links, Spannable s,
            @Nullable Context context) {
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        final TelephonyManager tm = (context == null)
                ? TelephonyManager.getDefault()
                : TelephonyManager.from(context);
        Iterable<PhoneNumberMatch> matches = phoneUtil.findNumbers(s.toString(),
                Locale.getDefault().getCountry(), Leniency.POSSIBLE, Long.MAX_VALUE);
                tm.getSimCountryIso().toUpperCase(Locale.US),
                Leniency.POSSIBLE, Long.MAX_VALUE);
        for (PhoneNumberMatch match : matches) {
            LinkSpec spec = new LinkSpec();
            spec.url = "tel:" + PhoneNumberUtils.normalizeNumber(match.rawString());
+2 −2
Original line number Diff line number Diff line
@@ -80,7 +80,7 @@ public class LinkifyTest {

        tv = new TextView(createUsEnglishContext());
        tv.setAutoLinkMask(Linkify.ALL);
        tv.setText("Hey, foo@google.com, call 415-555-1212.");
        tv.setText("Hey, foo@google.com, call +1-415-555-1212.");

        assertTrue(tv.getMovementMethod() instanceof LinkMovementMethod);
        assertTrue(tv.getUrls().length == 2);
@@ -93,7 +93,7 @@ public class LinkifyTest {
        tv = new TextView(createUsEnglishContext());
        tv.setAutoLinkMask(Linkify.ALL);
        tv.setLinksClickable(false);
        tv.setText("Hey, foo@google.com, call 415-555-1212.");
        tv.setText("Hey, foo@google.com, call +1-415-555-1212.");

        assertFalse(tv.getMovementMethod() instanceof LinkMovementMethod);
        assertTrue(tv.getUrls().length == 2);