From 62dbf274279754d030b2296de6928a273e2a1675 Mon Sep 17 00:00:00 2001 From: Aishwarya Mallampati Date: Fri, 26 May 2023 20:58:39 +0000 Subject: [PATCH] Change Linkify to use defaultCountry if simCountryIso is empty. If device has no sim cards, then simCountryIso is empty. If simCountryIso is empty, then we should fallback to default country. Bug: 261576074 Test: atest com.android.frameworks.coretests Change-Id: I2e83ffa1aaaff413d3dbe6dc49b81c2ee134b338 --- core/java/android/text/util/Linkify.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/core/java/android/text/util/Linkify.java b/core/java/android/text/util/Linkify.java index 946fc30eecb36..fb6de56b09523 100644 --- a/core/java/android/text/util/Linkify.java +++ b/core/java/android/text/util/Linkify.java @@ -31,6 +31,7 @@ import android.text.Spanned; import android.text.method.LinkMovementMethod; import android.text.method.MovementMethod; import android.text.style.URLSpan; +import android.text.TextUtils; import android.util.Log; import android.util.Patterns; import android.webkit.WebView; @@ -672,8 +673,15 @@ public class Linkify { @Nullable Context context) { PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); final Context ctx = (context != null) ? context : ActivityThread.currentApplication(); - final String regionCode = (ctx != null) ? ctx.getSystemService(TelephonyManager.class). - getSimCountryIso().toUpperCase(Locale.US) : Locale.getDefault().getCountry(); + String regionCode = Locale.getDefault().getCountry(); + if (ctx != null) { + String simCountryIso = ctx.getSystemService(TelephonyManager.class).getSimCountryIso(); + if (!TextUtils.isEmpty(simCountryIso)) { + // Assign simCountryIso if it is available + regionCode = simCountryIso.toUpperCase(Locale.US); + } + } + Iterable matches = phoneUtil.findNumbers(s.toString(), regionCode, Leniency.POSSIBLE, Long.MAX_VALUE); for (PhoneNumberMatch match : matches) {