Better handling country iso when getting geo location of phone number.
1. Use existing country iso info instead of guessing current country iso from sim and locale for blocked number.
2. Use country iso of current sim instead of default sim for incall location info. This fixes bug in multi sim device when default sim is out of service and making call with the other sim.
Bug: 65399976
Test: manual
PiperOrigin-RevId: 169554641
Change-Id: I416d7e2d6bc3c872bfec3eda4005a5a1684f0e40
diff --git a/java/com/android/contacts/common/list/PhoneNumberListAdapter.java b/java/com/android/contacts/common/list/PhoneNumberListAdapter.java
index 71d9dad..741e606 100644
--- a/java/com/android/contacts/common/list/PhoneNumberListAdapter.java
+++ b/java/com/android/contacts/common/list/PhoneNumberListAdapter.java
@@ -381,7 +381,11 @@
text = phoneLabel;
} else {
final String phoneNumber = cursor.getString(PhoneQuery.PHONE_NUMBER);
- text = PhoneNumberHelper.getGeoDescription(mContext, phoneNumber);
+ text =
+ PhoneNumberHelper.getGeoDescription(
+ mContext,
+ phoneNumber,
+ PhoneNumberHelper.getCurrentCountryIso(mContext, null /* PhoneAccountHandle */));
}
}
view.setPhoneNumber(text);
diff --git a/java/com/android/dialer/app/filterednumber/NumbersAdapter.java b/java/com/android/dialer/app/filterednumber/NumbersAdapter.java
index 938a784..bdd6dce 100644
--- a/java/com/android/dialer/app/filterednumber/NumbersAdapter.java
+++ b/java/com/android/dialer/app/filterednumber/NumbersAdapter.java
@@ -72,7 +72,7 @@
info = new ContactInfo();
info.number = number;
}
- final CharSequence locationOrType = getNumberTypeOrLocation(info);
+ final CharSequence locationOrType = getNumberTypeOrLocation(info, countryIso);
final String displayNumber = getDisplayNumber(info);
final String displayNumberStr =
mBidiFormatter.unicodeWrap(displayNumber, TextDirectionHeuristics.LTR);
@@ -121,12 +121,12 @@
}
}
- private CharSequence getNumberTypeOrLocation(ContactInfo info) {
+ private CharSequence getNumberTypeOrLocation(ContactInfo info, String countryIso) {
if (!TextUtils.isEmpty(info.name)) {
return ContactsContract.CommonDataKinds.Phone.getTypeLabel(
mContext.getResources(), info.type, info.label);
} else {
- return PhoneNumberHelper.getGeoDescription(mContext, info.number);
+ return PhoneNumberHelper.getGeoDescription(mContext, info.number, countryIso);
}
}
diff --git a/java/com/android/dialer/compat/telephony/TelephonyManagerCompat.java b/java/com/android/dialer/compat/telephony/TelephonyManagerCompat.java
index db1dd4a..ecd36d3 100644
--- a/java/com/android/dialer/compat/telephony/TelephonyManagerCompat.java
+++ b/java/com/android/dialer/compat/telephony/TelephonyManagerCompat.java
@@ -198,4 +198,34 @@
context.sendBroadcast(intent);
}
}
+
+ /**
+ * Returns network country iso for given {@code PhoneAccountHandle} for O+ devices and country iso
+ * for default sim for pre-O devices.
+ */
+ public static String getNetworkCountryIsoForPhoneAccountHandle(
+ Context context, @Nullable PhoneAccountHandle phoneAccountHandle) {
+ return getTelephonyManagerForPhoneAccountHandle(context, phoneAccountHandle)
+ .getNetworkCountryIso();
+ }
+
+ /**
+ * Returns TelephonyManager for given {@code PhoneAccountHandle} for O+ devices and default {@code
+ * TelephonyManager} for pre-O devices.
+ */
+ public static TelephonyManager getTelephonyManagerForPhoneAccountHandle(
+ Context context, @Nullable PhoneAccountHandle phoneAccountHandle) {
+ TelephonyManager telephonyManager = context.getSystemService(TelephonyManager.class);
+ if (phoneAccountHandle == null) {
+ return telephonyManager;
+ }
+ if (VERSION.SDK_INT >= VERSION_CODES.O) {
+ TelephonyManager telephonyManagerForPhoneAccount =
+ telephonyManager.createForPhoneAccountHandle(phoneAccountHandle);
+ if (telephonyManagerForPhoneAccount != null) {
+ return telephonyManagerForPhoneAccount;
+ }
+ }
+ return telephonyManager;
+ }
}
diff --git a/java/com/android/dialer/phonenumbergeoutil/PhoneNumberGeoUtil.java b/java/com/android/dialer/phonenumbergeoutil/PhoneNumberGeoUtil.java
index 2005abc..7e45598 100644
--- a/java/com/android/dialer/phonenumbergeoutil/PhoneNumberGeoUtil.java
+++ b/java/com/android/dialer/phonenumbergeoutil/PhoneNumberGeoUtil.java
@@ -20,5 +20,6 @@
/** Interface for getting geo information for phone number. */
public interface PhoneNumberGeoUtil {
- String getGeoDescription(Context context, String number);
+ /** Returns geo location information for a phone number, e.g. California. */
+ String getGeoDescription(Context context, String number, String countryIso);
}
diff --git a/java/com/android/dialer/phonenumbergeoutil/impl/PhoneNumberGeoUtilImpl.java b/java/com/android/dialer/phonenumbergeoutil/impl/PhoneNumberGeoUtilImpl.java
index 32f6592..fb28ed5 100644
--- a/java/com/android/dialer/phonenumbergeoutil/impl/PhoneNumberGeoUtilImpl.java
+++ b/java/com/android/dialer/phonenumbergeoutil/impl/PhoneNumberGeoUtilImpl.java
@@ -21,7 +21,6 @@
import com.android.dialer.common.LogUtil;
import com.android.dialer.compat.CompatUtils;
import com.android.dialer.phonenumbergeoutil.PhoneNumberGeoUtil;
-import com.android.dialer.phonenumberutil.PhoneNumberHelper;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
@@ -36,7 +35,7 @@
public PhoneNumberGeoUtilImpl() {}
@Override
- public String getGeoDescription(Context context, String number) {
+ public String getGeoDescription(Context context, String number, String countryIso) {
LogUtil.v("PhoneNumberGeoUtilImpl.getGeoDescription", "" + LogUtil.sanitizePii(number));
if (TextUtils.isEmpty(number)) {
@@ -47,7 +46,6 @@
PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
Locale locale = CompatUtils.getLocale(context);
- String countryIso = PhoneNumberHelper.getCurrentCountryIso(context, locale);
Phonenumber.PhoneNumber pn = null;
try {
LogUtil.v(
diff --git a/java/com/android/dialer/phonenumbergeoutil/stub/PhoneNumberGeoUtilStub.java b/java/com/android/dialer/phonenumbergeoutil/stub/PhoneNumberGeoUtilStub.java
index 4c5b3b0..06cd137 100644
--- a/java/com/android/dialer/phonenumbergeoutil/stub/PhoneNumberGeoUtilStub.java
+++ b/java/com/android/dialer/phonenumbergeoutil/stub/PhoneNumberGeoUtilStub.java
@@ -26,7 +26,7 @@
public PhoneNumberGeoUtilStub() {}
@Override
- public String getGeoDescription(Context context, String number) {
+ public String getGeoDescription(Context context, String number, String countryIso) {
return null;
}
}
diff --git a/java/com/android/dialer/phonenumberutil/PhoneNumberHelper.java b/java/com/android/dialer/phonenumberutil/PhoneNumberHelper.java
index 1189a9b..b25e4d7 100644
--- a/java/com/android/dialer/phonenumberutil/PhoneNumberHelper.java
+++ b/java/com/android/dialer/phonenumberutil/PhoneNumberHelper.java
@@ -24,11 +24,12 @@
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.android.dialer.common.LogUtil;
+import com.android.dialer.compat.CompatUtils;
+import com.android.dialer.compat.telephony.TelephonyManagerCompat;
import com.android.dialer.phonenumbergeoutil.PhoneNumberGeoUtilComponent;
import com.android.dialer.telecom.TelecomUtil;
import java.util.Arrays;
import java.util.HashSet;
-import java.util.Locale;
import java.util.Set;
public class PhoneNumberHelper {
@@ -92,27 +93,33 @@
}
/**
+ * @param countryIso Country ISO used if there is no country code in the number, may be null
+ * otherwise.
* @return a geographical description string for the specified number.
*/
- public static String getGeoDescription(Context context, String number) {
+ public static String getGeoDescription(
+ Context context, String number, @Nullable String countryIso) {
return PhoneNumberGeoUtilComponent.get(context)
.getPhoneNumberGeoUtil()
- .getGeoDescription(context, number);
+ .getGeoDescription(context, number, countryIso);
}
/**
+ * @param phoneAccountHandle {@code PhonAccountHandle} used to get current network country ISO.
+ * May be null if no account is in use or selected, in which case default account will be
+ * used.
* @return The ISO 3166-1 two letters country code of the country the user is in based on the
* network location. If the network location does not exist, fall back to the locale setting.
*/
- public static String getCurrentCountryIso(Context context, Locale locale) {
+ public static String getCurrentCountryIso(
+ Context context, @Nullable PhoneAccountHandle phoneAccountHandle) {
// Without framework function calls, this seems to be the most accurate location service
// we can rely on.
- final TelephonyManager telephonyManager =
- (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
-
- String countryIso = telephonyManager.getNetworkCountryIso();
+ String countryIso =
+ TelephonyManagerCompat.getNetworkCountryIsoForPhoneAccountHandle(
+ context, phoneAccountHandle);
if (TextUtils.isEmpty(countryIso)) {
- countryIso = locale.getCountry();
+ countryIso = CompatUtils.getLocale(context).getCountry();
LogUtil.i(
"PhoneNumberHelper.getCurrentCountryIso",
"No CountryDetector; falling back to countryIso based on locale: " + countryIso);
@@ -126,14 +133,12 @@
* @return Formatted phone number. e.g. 1-123-456-7890. Returns the original number if formatting
* failed.
*/
- public static String formatNumber(@Nullable String number, Context context) {
+ public static String formatNumber(@Nullable String number, String countryIso) {
// The number can be null e.g. schema is voicemail and uri content is empty.
if (number == null) {
return null;
}
- String formattedNumber =
- PhoneNumberUtils.formatNumber(
- number, PhoneNumberHelper.getCurrentCountryIso(context, Locale.getDefault()));
+ String formattedNumber = PhoneNumberUtils.formatNumber(number, countryIso);
return formattedNumber != null ? formattedNumber : number;
}
diff --git a/java/com/android/incallui/CallerInfo.java b/java/com/android/incallui/CallerInfo.java
index e552d7c..cc1a60a 100644
--- a/java/com/android/incallui/CallerInfo.java
+++ b/java/com/android/incallui/CallerInfo.java
@@ -162,6 +162,8 @@
*/
public String callSubject;
+ public String countryIso;
+
private boolean mIsEmergency;
private boolean mIsVoiceMail;
@@ -522,7 +524,7 @@
*/
public void updateGeoDescription(Context context, String fallbackNumber) {
String number = TextUtils.isEmpty(phoneNumber) ? fallbackNumber : phoneNumber;
- geoDescription = PhoneNumberHelper.getGeoDescription(context, number);
+ geoDescription = PhoneNumberHelper.getGeoDescription(context, number, countryIso);
}
/** @return a string debug representation of this instance. */
diff --git a/java/com/android/incallui/CallerInfoAsyncQuery.java b/java/com/android/incallui/CallerInfoAsyncQuery.java
index 858d0f4..86b1b7f 100644
--- a/java/com/android/incallui/CallerInfoAsyncQuery.java
+++ b/java/com/android/incallui/CallerInfoAsyncQuery.java
@@ -162,6 +162,7 @@
cw.listener = listener;
cw.cookie = cookie;
cw.number = info.phoneNumber;
+ cw.countryIso = info.countryIso;
// check to see if these are recognized numbers, and use shortcuts if we can.
if (PhoneNumberUtils.isLocalEmergencyNumber(context, info.phoneNumber)) {
@@ -268,6 +269,7 @@
public Object cookie;
public int event;
public String number;
+ public String countryIso;
}
/* Directory lookup related code - END */
@@ -493,6 +495,7 @@
mCallerInfo = newCallerInfo;
Log.d(this, "#####async contact look up with numeric username" + mCallerInfo);
}
+ mCallerInfo.countryIso = cw.countryIso;
// Final step: look up the geocoded description.
if (ENABLE_UNKNOWN_NUMBER_GEO_DESCRIPTION) {
diff --git a/java/com/android/incallui/CallerInfoUtils.java b/java/com/android/incallui/CallerInfoUtils.java
index 8f23107..bf586f5 100644
--- a/java/com/android/incallui/CallerInfoUtils.java
+++ b/java/com/android/incallui/CallerInfoUtils.java
@@ -70,7 +70,7 @@
"CallerInfoUtils.getCallerInfoForCall",
"Actually starting CallerInfoAsyncQuery.startQuery()...");
- //noinspection MissingPermission
+ // noinspection MissingPermission
CallerInfoAsyncQuery.startQuery(QUERY_TOKEN, context, info, listener, cookie);
} else {
LogUtil.w(
@@ -93,6 +93,7 @@
info.namePresentation = call.getCnapNamePresentation();
info.callSubject = call.getCallSubject();
info.contactExists = false;
+ info.countryIso = PhoneNumberHelper.getCurrentCountryIso(context, call.getAccountHandle());
String number = call.getNumber();
if (!TextUtils.isEmpty(number)) {
diff --git a/java/com/android/incallui/ContactInfoCache.java b/java/com/android/incallui/ContactInfoCache.java
index 7bac6d3..39c4c2f 100644
--- a/java/com/android/incallui/ContactInfoCache.java
+++ b/java/com/android/incallui/ContactInfoCache.java
@@ -211,7 +211,7 @@
// No name, but we do have a valid CNAP name, so use that.
displayName = info.cnapName;
info.name = info.cnapName;
- displayNumber = PhoneNumberHelper.formatNumber(number, context);
+ displayNumber = PhoneNumberHelper.formatNumber(number, info.countryIso);
Log.d(
TAG,
" ==> cnapName available: displayName '"
@@ -224,7 +224,7 @@
// case when an incoming call doesn't match any contact,
// or if you manually dial an outgoing number using the
// dialpad.
- displayNumber = PhoneNumberHelper.formatNumber(number, context);
+ displayNumber = PhoneNumberHelper.formatNumber(number, info.countryIso);
Log.d(
TAG,
@@ -249,7 +249,7 @@
// later determine whether to use the name or nameAlternative when presenting
displayName = info.name;
cce.nameAlternative = info.nameAlternative;
- displayNumber = PhoneNumberHelper.formatNumber(number, context);
+ displayNumber = PhoneNumberHelper.formatNumber(number, info.countryIso);
label = info.phoneLabel;
Log.d(
TAG,