Merge "Make Linkify use the SIM card's country for phone numbers"
diff --git a/core/java/android/text/util/Linkify.java b/core/java/android/text/util/Linkify.java
index 7e6eb49..768aee9 100644
--- a/core/java/android/text/util/Linkify.java
+++ b/core/java/android/text/util/Linkify.java
@@ -19,7 +19,9 @@
 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 @@
      *  @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 @@
         }
 
         if ((mask & PHONE_NUMBERS) != 0) {
-            gatherTelLinks(links, text);
+            gatherTelLinks(links, text, context);
         }
 
         if ((mask & MAP_ADDRESSES) != 0) {
@@ -282,10 +289,10 @@
             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 @@
         } else {
             SpannableString s = SpannableString.valueOf(t);
 
-            if (addLinks(s, mask)) {
+            if (addLinks(s, mask, context)) {
                 addLinkMovementMethod(text);
                 text.setText(s);
 
@@ -528,10 +535,15 @@
         }
     }
 
-    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());
diff --git a/core/tests/coretests/src/android/text/util/LinkifyTest.java b/core/tests/coretests/src/android/text/util/LinkifyTest.java
index 73ff046..be3a0be 100644
--- a/core/tests/coretests/src/android/text/util/LinkifyTest.java
+++ b/core/tests/coretests/src/android/text/util/LinkifyTest.java
@@ -80,7 +80,7 @@
 
         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 @@
         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);