Add the assisted dialing logic module.

This implements the core assisted dialing logic specified in
go/assisted-dialing-dd-v1

Bug: 36414469,63995261
Test: new unit tests
PiperOrigin-RevId: 170232634
Change-Id: I3b668c3a0e9fb5398eca4614548c8141b200e70e
diff --git a/java/com/android/dialer/assisteddialing/AssistedDialingMediator.java b/java/com/android/dialer/assisteddialing/AssistedDialingMediator.java
new file mode 100644
index 0000000..2613d07
--- /dev/null
+++ b/java/com/android/dialer/assisteddialing/AssistedDialingMediator.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.dialer.assisteddialing;
+
+import android.annotation.TargetApi;
+import android.os.Build.VERSION_CODES;
+import android.support.annotation.NonNull;
+import com.android.dialer.common.LogUtil;
+import java.util.Optional;
+
+/**
+ * The Mediator for Assisted Dialing.
+ *
+ * <p>This class is responsible for mediating location discovery of the user, determining if the
+ * call is eligible for assisted dialing, and performing the transformation of numbers eligible for
+ * assisted dialing.
+ */
+public final class AssistedDialingMediator {
+
+  private final LocationDetector locationDetector;
+  private final NumberTransformer numberTransformer;
+
+  protected AssistedDialingMediator(
+      @NonNull LocationDetector locationDetector, @NonNull NumberTransformer numberTransformer) {
+    if (locationDetector == null) {
+      throw new NullPointerException("locationDetector was null");
+    }
+
+    if (numberTransformer == null) {
+      throw new NullPointerException("numberTransformer was null");
+    }
+    this.locationDetector = locationDetector;
+    this.numberTransformer = numberTransformer;
+  }
+
+  /**
+   * Returns a boolean for callers to quickly determine whether or not the AssistedDialingMediator
+   * thinks an attempt at assisted dialing is likely to succeed.
+   */
+  public boolean conditionsEligibleForAssistedDialing(
+      @NonNull String numberToCheck,
+      @NonNull String userHomeCountryCode,
+      @NonNull String userRoamingCountryCode) {
+    return numberTransformer.canDoAssistedDialingTransformation(
+        numberToCheck, userHomeCountryCode, userRoamingCountryCode);
+  }
+
+  /**
+   * Returns an Optional of type String containing the transformed number that was provided. The
+   * transformed number should be capable of dialing out of the User's current country and
+   * successfully connecting with a contact in the User's home country.
+   */
+  @SuppressWarnings("AndroidApiChecker") // Use of optional
+  @TargetApi(VERSION_CODES.N)
+  public Optional<String> attemptAssistedDial(@NonNull String numberToTransform) {
+    Optional<String> userHomeCountryCode = locationDetector.getUpperCaseUserHomeCountry();
+    Optional<String> userRoamingCountryCode = locationDetector.getUpperCaseUserRoamingCountry();
+
+    if (!userHomeCountryCode.isPresent() || !userRoamingCountryCode.isPresent()) {
+      LogUtil.i("AssistedDialingMediator.attemptAssistedDial", "Unable to determine country codes");
+      return Optional.empty();
+    }
+
+    return numberTransformer.doAssistedDialingTransformation(
+        numberToTransform, userHomeCountryCode.get(), userRoamingCountryCode.get());
+  }
+}
diff --git a/java/com/android/dialer/assisteddialing/ConcreteCreator.java b/java/com/android/dialer/assisteddialing/ConcreteCreator.java
new file mode 100644
index 0000000..f51216a
--- /dev/null
+++ b/java/com/android/dialer/assisteddialing/ConcreteCreator.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.dialer.assisteddialing;
+
+import android.annotation.TargetApi;
+import android.content.Context;
+import android.os.Build.VERSION_CODES;
+import android.support.annotation.NonNull;
+import android.telephony.TelephonyManager;
+import com.android.dialer.common.LogUtil;
+
+/**
+ * A Creator for AssistedDialingMediators.
+ *
+ * <p>This helps keep the dependencies required by AssistedDialingMediator for assisted dialing
+ * explicit.
+ */
+@TargetApi(VERSION_CODES.N)
+public final class ConcreteCreator {
+
+  /**
+   * Creates a new AssistedDialingMediator
+   *
+   * @param telephonyManager The telephony manager used to determine user location.
+   * @param context The context used to determine whether or not a provided number is an emergency
+   *     number.
+   * @return An AssistedDialingMediator
+   */
+  public static AssistedDialingMediator createNewAssistedDialingMediator(
+      @NonNull TelephonyManager telephonyManager, @NonNull Context context) {
+    if (telephonyManager == null) {
+      LogUtil.i(
+          "ConcreteCreator.createNewAssistedDialingMediator", "provided TelephonyManager was null");
+      throw new NullPointerException("Provided TelephonyManager was null");
+    }
+    if (context == null) {
+      LogUtil.i("ConcreteCreator.createNewAssistedDialingMediator", "provided context was null");
+      throw new NullPointerException("Provided context was null");
+    }
+    Constraints constraints = new Constraints(context);
+    return new AssistedDialingMediator(
+        new LocationDetector(telephonyManager), new NumberTransformer(constraints));
+  }
+}
diff --git a/java/com/android/dialer/assisteddialing/Constraints.java b/java/com/android/dialer/assisteddialing/Constraints.java
new file mode 100644
index 0000000..6bcab99
--- /dev/null
+++ b/java/com/android/dialer/assisteddialing/Constraints.java
@@ -0,0 +1,202 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.dialer.assisteddialing;
+
+import android.annotation.TargetApi;
+import android.content.Context;
+import android.os.Build.VERSION_CODES;
+import android.support.annotation.NonNull;
+import android.telephony.PhoneNumberUtils;
+import android.text.TextUtils;
+import android.util.ArraySet;
+import com.android.dialer.common.LogUtil;
+import com.google.i18n.phonenumbers.NumberParseException;
+import com.google.i18n.phonenumbers.PhoneNumberUtil;
+import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
+import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber.CountryCodeSource;
+import java.util.Arrays;
+import java.util.Locale;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/** Ensures that a number is eligible for Assisted Dialing */
+@TargetApi(VERSION_CODES.N)
+@SuppressWarnings("AndroidApiChecker") // Use of optional
+final class Constraints {
+  private final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
+  private final Context context;
+
+  /**
+   * Create a new instance of Constraints.
+   *
+   * @param context The context used to determine whether or not a number is an emergency number.
+   */
+  public Constraints(@NonNull Context context) {
+    if (context == null) {
+      throw new NullPointerException("Provided context cannot be null");
+    }
+    this.context = context;
+  }
+
+  // TODO(erfanian): Ensure the below standard is consistent between libphonenumber and the
+  // platform.
+  // ISO 3166-1 alpha-2 Country Codes that are eligible for assisted dialing.
+  private final String[] supportedCountryCodeValues =
+      new String[] {
+        "CA" /* Canada */,
+        "GB" /* United Kingdom */,
+        "JP" /* Japan */,
+        "MX" /* Mexico */,
+        "US" /* United States*/,
+      };
+
+  private final Set<String> supportedCountryCodes =
+      Arrays.stream(supportedCountryCodeValues)
+          .map(v -> v.toUpperCase(Locale.US))
+          .collect(Collectors.toCollection(ArraySet::new));
+
+  /**
+   * Determines whether or not we think Assisted Dialing is possible given the provided parameters.
+   *
+   * @param numberToCheck A string containing the phone number.
+   * @param userHomeCountryCode A string containing an ISO 3166-1 alpha-2 country code representing
+   *     the user's home country.
+   * @param userRoamingCountryCode A string containing an ISO 3166-1 alpha-2 country code
+   *     representing the user's roaming country.
+   * @return A boolean indicating whether or not the provided values are eligible for assisted
+   *     dialing.
+   */
+  public boolean meetsPreconditions(
+      @NonNull String numberToCheck,
+      @NonNull String userHomeCountryCode,
+      @NonNull String userRoamingCountryCode) {
+
+    if (TextUtils.isEmpty(numberToCheck)) {
+      LogUtil.i("Constraints.meetsPreconditions", "numberToCheck was empty");
+      return false;
+    }
+
+    if (TextUtils.isEmpty(userHomeCountryCode)) {
+      LogUtil.i("Constraints.meetsPreconditions", "userHomeCountryCode was empty");
+      return false;
+    }
+
+    if (TextUtils.isEmpty(userRoamingCountryCode)) {
+      LogUtil.i("Constraints.meetsPreconditions", "userRoamingCountryCode was empty");
+      return false;
+    }
+
+    userHomeCountryCode = userHomeCountryCode.toUpperCase(Locale.US);
+    userRoamingCountryCode = userRoamingCountryCode.toUpperCase(Locale.US);
+
+    Optional<PhoneNumber> parsedPhoneNumber = parsePhoneNumber(numberToCheck, userHomeCountryCode);
+
+    if (!parsedPhoneNumber.isPresent()) {
+      LogUtil.i("Constraints.meetsPreconditions", "parsedPhoneNumber was empty");
+      return false;
+    }
+
+    return areSupportedCountryCodes(userHomeCountryCode, userRoamingCountryCode)
+        && isUserRoaming(userHomeCountryCode, userRoamingCountryCode)
+        && isNotInternationalNumber(parsedPhoneNumber)
+        && isNotEmergencyNumber(numberToCheck, context)
+        && isValidNumber(parsedPhoneNumber);
+  }
+
+  /** Returns a boolean indicating the value equivalence of the provided country codes. */
+  private boolean isUserRoaming(
+      @NonNull String userHomeCountryCode, @NonNull String userRoamingCountryCode) {
+    boolean result = !userHomeCountryCode.equals(userRoamingCountryCode);
+    LogUtil.i("Constraints.isUserRoaming", String.valueOf(result));
+    return result;
+  }
+
+  /**
+   * Returns a boolean indicating the support of both provided country codes for assisted dialing.
+   * Both country codes must be allowed for the return value to be true.
+   */
+  private boolean areSupportedCountryCodes(
+      @NonNull String userHomeCountryCode, @NonNull String userRoamingCountryCode) {
+    if (TextUtils.isEmpty(userHomeCountryCode)) {
+      LogUtil.i("Constraints.areSupportedCountryCodes", "userHomeCountryCode was empty");
+      return false;
+    }
+
+    if (TextUtils.isEmpty(userRoamingCountryCode)) {
+      LogUtil.i("Constraints.areSupportedCountryCodes", "userRoamingCountryCode was empty");
+      return false;
+    }
+
+    boolean result =
+        supportedCountryCodes.contains(userHomeCountryCode)
+            && supportedCountryCodes.contains(userRoamingCountryCode);
+    LogUtil.i("Constraints.areSupportedCountryCodes", String.valueOf(result));
+    return result;
+  }
+
+  /**
+   * A convenience method to take a number as a String and a specified country code, and return a
+   * PhoneNumber object.
+   */
+  private Optional<PhoneNumber> parsePhoneNumber(
+      @NonNull String numberToParse, @NonNull String userHomeCountryCode) {
+    try {
+      // TODO(erfanian): confirm behavior of blocking the foreground thread when moving to the
+      // framework
+      return Optional.of(phoneNumberUtil.parseAndKeepRawInput(numberToParse, userHomeCountryCode));
+    } catch (NumberParseException e) {
+      LogUtil.i("Constraints.parsePhoneNumber", "could not parse the number");
+      return Optional.empty();
+    }
+  }
+
+  /**
+   * Returns a boolean indicating if the provided number and home country code are already
+   * internationally formatted.
+   */
+  private boolean isNotInternationalNumber(@NonNull Optional<PhoneNumber> parsedPhoneNumber) {
+
+    if (parsedPhoneNumber.get().hasCountryCode()
+        && parsedPhoneNumber.get().getCountryCodeSource()
+            != CountryCodeSource.FROM_DEFAULT_COUNTRY) {
+      LogUtil.i(
+          "Constraints.isNotInternationalNumber", "phone number already provided the country code");
+      return false;
+    }
+    return true;
+  }
+
+  /** Returns a boolean indicating if the provided number is considered to be a valid number. */
+  private boolean isValidNumber(@NonNull Optional<PhoneNumber> parsedPhoneNumber) {
+    boolean result = PhoneNumberUtil.getInstance().isValidNumber(parsedPhoneNumber.get());
+    LogUtil.i("Constraints.isValidNumber", String.valueOf(result));
+
+    return result;
+  }
+
+  /** Returns a boolean indicating if the provided number is an emergency number. */
+  private boolean isNotEmergencyNumber(@NonNull String numberToCheck, @NonNull Context context) {
+    // isEmergencyNumber may depend on network state, so also use isLocalEmergencyNumber when
+    // roaming and out of service.
+    boolean result =
+        !PhoneNumberUtils.isEmergencyNumber(numberToCheck)
+            && !PhoneNumberUtils.isLocalEmergencyNumber(context, numberToCheck);
+    LogUtil.i("Constraints.isNotEmergencyNumber", String.valueOf(result));
+    return result;
+  }
+}
diff --git a/java/com/android/dialer/assisteddialing/LocationDetector.java b/java/com/android/dialer/assisteddialing/LocationDetector.java
new file mode 100644
index 0000000..6840689
--- /dev/null
+++ b/java/com/android/dialer/assisteddialing/LocationDetector.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.dialer.assisteddialing;
+
+import android.annotation.TargetApi;
+import android.os.Build.VERSION_CODES;
+import android.support.annotation.NonNull;
+import android.telephony.TelephonyManager;
+import com.android.dialer.common.LogUtil;
+import java.util.Locale;
+import java.util.Optional;
+
+// TODO(erfanian): Improve definition of roaming and home country in finalized API.
+/**
+ * LocationDetector is responsible for determining the Roaming location of the User, in addition to
+ * User's home country.
+ */
+final class LocationDetector {
+
+  private final TelephonyManager telephonyManager;
+
+  public LocationDetector(@NonNull TelephonyManager telephonyManager) {
+    if (telephonyManager == null) {
+      throw new NullPointerException("Provided TelephonyManager was null");
+    }
+    this.telephonyManager = telephonyManager;
+  }
+
+  // TODO(erfanian):  confirm this is based on ISO 3166-1 alpha-2. libphonenumber expects Unicode's
+  // CLDR
+  // TODO(erfanian):  confirm these are still valid in a multi-sim environment.
+  /**
+   * Returns what we believe to be the User's home country. This should resolve to
+   * PROPERTY_ICC_OPERATOR_ISO_COUNTRY
+   */
+  @SuppressWarnings("AndroidApiChecker") // Use of optional
+  @TargetApi(VERSION_CODES.N)
+  public Optional<String> getUpperCaseUserHomeCountry() {
+    String simCountryIso = telephonyManager.getSimCountryIso();
+    if (simCountryIso != null) {
+      return Optional.of(telephonyManager.getSimCountryIso().toUpperCase(Locale.US));
+    }
+    LogUtil.i("LocationDetector.getUpperCaseUserHomeCountry", "user home country was null");
+    return Optional.empty();
+  }
+
+  /** Returns what we believe to be the User's current (roaming) country */
+  @SuppressWarnings("AndroidApiChecker") // Use of optional
+  @TargetApi(VERSION_CODES.N)
+  public Optional<String> getUpperCaseUserRoamingCountry() {
+    // TODO Increase coverage of location resolution??
+    String networkCountryIso = telephonyManager.getNetworkCountryIso();
+    if (networkCountryIso != null) {
+      return Optional.of(telephonyManager.getNetworkCountryIso().toUpperCase(Locale.US));
+    }
+    LogUtil.i("LocationDetector.getUpperCaseUserRoamingCountry", "user roaming country was null");
+    return Optional.empty();
+  }
+}
diff --git a/java/com/android/dialer/assisteddialing/NumberTransformer.java b/java/com/android/dialer/assisteddialing/NumberTransformer.java
new file mode 100644
index 0000000..f01d1a0
--- /dev/null
+++ b/java/com/android/dialer/assisteddialing/NumberTransformer.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.dialer.assisteddialing;
+
+import android.annotation.TargetApi;
+import android.os.Build.VERSION_CODES;
+import android.support.annotation.NonNull;
+import android.text.TextUtils;
+import com.android.dialer.common.LogUtil;
+import com.google.i18n.phonenumbers.NumberParseException;
+import com.google.i18n.phonenumbers.PhoneNumberUtil;
+import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
+import java.util.Optional;
+
+/** Responsible for transforming numbers to make them dialable and valid when roaming. */
+final class NumberTransformer {
+
+  private final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
+  private final Constraints constraints;
+
+  public NumberTransformer(Constraints constraints) {
+    this.constraints = constraints;
+  }
+
+  /**
+   * Returns a boolean for callers to quickly determine whether or not the AssistedDialingMediator
+   * thinks an attempt at assisted dialing is likely to succeed.
+   */
+  public boolean canDoAssistedDialingTransformation(
+      @NonNull String numberToCheck,
+      @NonNull String userHomeCountryCode,
+      @NonNull String userRoamingCountryCode) {
+    return constraints.meetsPreconditions(
+        numberToCheck, userHomeCountryCode, userRoamingCountryCode);
+  }
+
+  /**
+   * A method to do assisted dialing transformations.
+   *
+   * <p>The library will do its best to attempt a transformation, but, if for any reason the
+   * transformation fails, we return an empty optional. The operation can be considered a success
+   * when the Optional we return has a value set.
+   */
+  @SuppressWarnings("AndroidApiChecker") // Use of optional
+  @TargetApi(VERSION_CODES.N)
+  public Optional<String> doAssistedDialingTransformation(
+      String numbertoTransform, String userHomeCountryCode, String userRoamingCountryCode) {
+
+    if (!constraints.meetsPreconditions(
+        numbertoTransform, userHomeCountryCode, userRoamingCountryCode)) {
+      LogUtil.i(
+          "NumberTransformer.doAssistedDialingTransformation",
+          "assisted dialing failed preconditions");
+      return Optional.empty();
+    }
+
+    PhoneNumber phoneNumber;
+    try {
+      phoneNumber = phoneNumberUtil.parse(numbertoTransform, userHomeCountryCode);
+    } catch (NumberParseException e) {
+      LogUtil.i("NumberTransformer.doAssistedDialingTransformation", "number failed to parse");
+      return Optional.empty();
+    }
+
+    String transformedNumber =
+        phoneNumberUtil.formatNumberForMobileDialing(phoneNumber, userRoamingCountryCode, true);
+
+    // formatNumberForMobileDialing may return an empty String.
+    if (TextUtils.isEmpty(transformedNumber)) {
+      LogUtil.i(
+          "NumberTransformer.doAssistedDialingTransformation",
+          "formatNumberForMobileDialing returned an empty string");
+      return Optional.empty();
+    }
+
+    // TODO Verify the transformed number is still valid?
+    return Optional.of(transformedNumber);
+  }
+}