Merge "Use current county ISO for PhoneLookupHistoryRecorder"
diff --git a/java/com/android/dialer/location/CountryDetector.java b/java/com/android/dialer/location/CountryDetector.java
index 5c5ed9c..664bb50 100644
--- a/java/com/android/dialer/location/CountryDetector.java
+++ b/java/com/android/dialer/location/CountryDetector.java
@@ -16,10 +16,12 @@
 
 package com.android.dialer.location;
 
+import android.Manifest.permission;
 import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.PackageManager;
 import android.location.Address;
 import android.location.Geocoder;
 import android.location.Location;
@@ -35,7 +37,6 @@
 import com.android.dialer.common.LogUtil;
 import com.android.dialer.common.concurrent.DialerExecutor.Worker;
 import com.android.dialer.common.concurrent.DialerExecutorComponent;
-import com.android.dialer.util.PermissionsUtil;
 import java.util.List;
 import java.util.Locale;
 
@@ -98,8 +99,9 @@
     }
   }
 
+  @SuppressWarnings("missingPermission")
   private static void registerForLocationUpdates(Context context, LocationManager locationManager) {
-    if (!PermissionsUtil.hasLocationPermissions(context)) {
+    if (!hasLocationPermissions(context)) {
       LogUtil.w(
           "CountryDetector.registerForLocationUpdates",
           "no location permissions, not registering for location updates");
@@ -163,7 +165,7 @@
   @Nullable
   private String getLocationBasedCountryIso() {
     if (!Geocoder.isPresent()
-        || !PermissionsUtil.hasLocationPermissions(appContext)
+        || !hasLocationPermissions(appContext)
         || !UserManagerCompat.isUserUnlocked(appContext)) {
       return null;
     }
@@ -265,4 +267,9 @@
       return null;
     }
   }
+
+  private static boolean hasLocationPermissions(Context context) {
+    return context.checkSelfPermission(permission.ACCESS_FINE_LOCATION)
+        == PackageManager.PERMISSION_GRANTED;
+  }
 }
diff --git a/java/com/android/dialer/location/GeoUtil.java b/java/com/android/dialer/location/GeoUtil.java
index 27fbf23..8a875e8 100644
--- a/java/com/android/dialer/location/GeoUtil.java
+++ b/java/com/android/dialer/location/GeoUtil.java
@@ -22,7 +22,12 @@
 /** Static methods related to Geo. */
 public class GeoUtil {
 
-  /** @return the ISO 3166-1 two letters country code of the country the user is in. */
+  /**
+   * Return the ISO 3166-1 two letters country code of the country the user is in.
+   *
+   * <p>WARNING: {@link CountryDetector} caches TelephonyManager and other system services in a
+   * static. {@link CountryDetector#instance} must be reset in tests.
+   */
   public static String getCurrentCountryIso(Context context) {
     // The {@link CountryDetector} should never return null so this is safe to return as-is.
     Trace.beginSection("GeoUtil.getCurrentCountryIso");
diff --git a/java/com/android/dialer/telecom/TelecomCallUtil.java b/java/com/android/dialer/telecom/TelecomCallUtil.java
index 3ae9523..6b66d86 100644
--- a/java/com/android/dialer/telecom/TelecomCallUtil.java
+++ b/java/com/android/dialer/telecom/TelecomCallUtil.java
@@ -22,14 +22,11 @@
 import android.support.annotation.Nullable;
 import android.support.annotation.WorkerThread;
 import android.telecom.Call;
-import android.telecom.PhoneAccountHandle;
 import android.telephony.PhoneNumberUtils;
-import android.telephony.SubscriptionInfo;
 import android.text.TextUtils;
 import com.android.dialer.common.Assert;
-import com.android.dialer.common.LogUtil;
+import com.android.dialer.location.GeoUtil;
 import com.google.common.base.Optional;
-import java.util.Locale;
 
 /**
  * Class to provide a standard interface for obtaining information from the underlying
@@ -72,11 +69,12 @@
   }
 
   /**
-   * Normalizes the number of the {@code call} to E.164. The country of the SIM associated with the
-   * call is used to determine the country.
+   * Normalizes the number of the {@code call} to E.164. If the number for the call does not contain
+   * a country code, then the current location as defined by {@link
+   * GeoUtil#getCurrentCountryIso(Context)} is used.
    *
-   * <p>If the number cannot be formatted (because for example the country cannot be determined),
-   * returns the number with non-dialable digits removed.
+   * <p>If the number cannot be formatted (because for example number is invalid), returns the
+   * number with non-dialable digits removed.
    */
   @WorkerThread
   public static Optional<String> getNormalizedNumber(Context appContext, Call call) {
@@ -94,11 +92,12 @@
   }
 
   /**
-   * Formats the number of the {@code call} to E.164 if it is valid. The country of the SIM
-   * associated with the call is used to determine the country.
+   * Formats the number of the {@code call} to E.164 if it is valid. If the number for the call does
+   * not contain a country code, then the current location as defined by {@link
+   * GeoUtil#getCurrentCountryIso(Context)} is used.
    *
-   * <p>If the number cannot be formatted (because for example it is invalid or the country cannot
-   * be determined), returns {@link Optional#absent()}.
+   * <p>If the number cannot be formatted (because for example it is invalid), returns {@link
+   * Optional#absent()}.
    */
   @WorkerThread
   public static Optional<String> getValidE164Number(Context appContext, Call call) {
@@ -107,23 +106,7 @@
     if (TextUtils.isEmpty(rawNumber)) {
       return Optional.absent();
     }
-    Optional<String> countryCode = getCountryCode(appContext, call);
-    if (!countryCode.isPresent()) {
-      LogUtil.w("TelecomCallUtil.getValidE164Number", "couldn't find a country code for call");
-      return Optional.absent();
-    }
-    return Optional.fromNullable(PhoneNumberUtils.formatNumberToE164(rawNumber, countryCode.get()));
-  }
-
-  @WorkerThread
-  public static Optional<String> getCountryCode(Context appContext, Call call) {
-    Assert.isWorkerThread();
-    PhoneAccountHandle phoneAccountHandle = call.getDetails().getAccountHandle();
-    Optional<SubscriptionInfo> subscriptionInfo =
-        TelecomUtil.getSubscriptionInfo(appContext, phoneAccountHandle);
-    if (subscriptionInfo.isPresent() && subscriptionInfo.get().getCountryIso() != null) {
-      return Optional.of(subscriptionInfo.get().getCountryIso().toUpperCase(Locale.US));
-    }
-    return Optional.absent();
+    return Optional.fromNullable(
+        PhoneNumberUtils.formatNumberToE164(rawNumber, GeoUtil.getCurrentCountryIso(appContext)));
   }
 }
diff --git a/java/com/android/incallui/PhoneLookupHistoryRecorder.java b/java/com/android/incallui/PhoneLookupHistoryRecorder.java
index 802bf63..16d73ce 100644
--- a/java/com/android/incallui/PhoneLookupHistoryRecorder.java
+++ b/java/com/android/incallui/PhoneLookupHistoryRecorder.java
@@ -24,6 +24,7 @@
 import com.android.dialer.common.LogUtil;
 import com.android.dialer.common.concurrent.DialerExecutorComponent;
 import com.android.dialer.configprovider.ConfigProviderBindings;
+import com.android.dialer.location.GeoUtil;
 import com.android.dialer.phonelookup.PhoneLookupComponent;
 import com.android.dialer.phonelookup.PhoneLookupInfo;
 import com.android.dialer.phonelookup.database.contract.PhoneLookupHistoryContract.PhoneLookupHistory;
@@ -61,8 +62,7 @@
               DialerPhoneNumberUtil dialerPhoneNumberUtil =
                   new DialerPhoneNumberUtil(PhoneNumberUtil.getInstance());
               return dialerPhoneNumberUtil.parse(
-                  TelecomCallUtil.getNumber(call),
-                  TelecomCallUtil.getCountryCode(appContext, call).orNull());
+                  TelecomCallUtil.getNumber(call), GeoUtil.getCurrentCountryIso(appContext));
             });
 
     ListenableFuture<PhoneLookupInfo> infoFuture =