Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 635e5a99 authored by erfanian's avatar erfanian Committed by android-build-merger
Browse files

Merge "Move country constraint logic into standalone class." am: b33463c7

am: c0547e20

Change-Id: I9bfc027452209d68ee0c1655fb3a2c115b924c6a
parents 21fa17bf c0547e20
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -86,9 +86,7 @@ public final class ConcreteCreator {
      return new AssistedDialingMediatorStub();
    }

    Constraints constraints =
        new Constraints(
            context, configProvider.getString("assisted_dialing_csv_country_codes", ""));
    Constraints constraints = new Constraints(context, getCountryCodeProvider(configProvider));
    return new AssistedDialingMediatorImpl(
        new LocationDetector(
            telephonyManager,
@@ -108,4 +106,16 @@ public final class ConcreteCreator {
            && Build.VERSION.SDK_INT <= BUILD_CODE_CEILING)
        && configProvider.getBoolean("assisted_dialing_enabled", false);
  }

  /**
   * Returns a CountryCodeProvider responsible for providing countries eligible for assisted Dialing
   */
  public static CountryCodeProvider getCountryCodeProvider(ConfigProvider configProvider) {
    if (configProvider == null) {
      LogUtil.i("ConcreteCreator.getCountryCodeProvider", "provided configProvider was null");
      throw new NullPointerException("Provided configProvider was null");
    }

    return new CountryCodeProvider(configProvider);
  }
}
+7 −72
Original line number Diff line number Diff line
@@ -20,10 +20,8 @@ import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build.VERSION_CODES;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import android.util.ArraySet;
import com.android.dialer.common.LogUtil;
import com.android.dialer.logging.DialerImpression;
import com.android.dialer.logging.Logger;
@@ -31,14 +29,8 @@ 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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

/** Ensures that a number is eligible for Assisted Dialing */
@TargetApi(VERSION_CODES.N)
@@ -46,19 +38,7 @@ import java.util.stream.Collectors;
final class Constraints {
  private final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
  private final 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 static final List<String> DEFAULT_COUNTRY_CODES =
      Arrays.asList(
          "CA" /* Canada */,
          "GB" /* United Kingdom */,
          "JP" /* Japan */,
          "MX" /* Mexico */,
          "US" /* United States */);

  @VisibleForTesting final Set<String> supportedCountryCodes;
  private final CountryCodeProvider countryCodeProvider;

  /**
   * Create a new instance of Constraints.
@@ -66,65 +46,20 @@ final class Constraints {
   * @param context The context used to determine whether or not a number is an emergency number.
   * @param configProviderCountryCodes A csv of supported country codes, e.g. "US,CA"
   */
  public Constraints(@NonNull Context context, @NonNull String configProviderCountryCodes) {
  public Constraints(@NonNull Context context, @NonNull CountryCodeProvider countryCodeProvider) {
    if (context == null) {
      throw new NullPointerException("Provided context cannot be null");
    }
    this.context = context;

    if (configProviderCountryCodes == null) {
    if (countryCodeProvider == null) {
      throw new NullPointerException("Provided configProviderCountryCodes cannot be null");
    }

    // We allow dynamic country support only in Dialer; this should be removed in the framework
    // implementation.
    // TODO(erfanian): Remove in the framework implementation, or add a service to provide these
    // values to the framework.
    supportedCountryCodes =
        parseConfigProviderCountryCodes(configProviderCountryCodes)
            .stream()
            .map(v -> v.toUpperCase(Locale.US))
            .collect(Collectors.toCollection(ArraySet::new));
    LogUtil.i("Constraints.Constraints", "Using country codes: " + supportedCountryCodes);
  }

  private List<String> parseConfigProviderCountryCodes(String configProviderCountryCodes) {
    if (TextUtils.isEmpty(configProviderCountryCodes)) {
      LogUtil.i(
          "Constraints.parseConfigProviderCountryCodes",
          "configProviderCountryCodes was empty, returning default");
      return DEFAULT_COUNTRY_CODES;
    }

    StringTokenizer tokenizer = new StringTokenizer(configProviderCountryCodes, ",");

    if (tokenizer.countTokens() < 1) {
      LogUtil.i(
          "Constraints.parseConfigProviderCountryCodes", "insufficient provided country codes");
      return DEFAULT_COUNTRY_CODES;
    }

    List<String> parsedCountryCodes = new ArrayList<>();
    while (tokenizer.hasMoreTokens()) {
      String foundLocale = tokenizer.nextToken();
      if (foundLocale == null) {
        LogUtil.i(
            "Constraints.parseConfigProviderCountryCodes",
            "Unexpected empty value, returning default.");
        return DEFAULT_COUNTRY_CODES;
      }

      if (foundLocale.length() != 2) {
        LogUtil.i(
            "Constraints.parseConfigProviderCountryCodes",
            "Unexpected locale %s, returning default",
            foundLocale);
        return DEFAULT_COUNTRY_CODES;
      }

      parsedCountryCodes.add(foundLocale);
    }
    return parsedCountryCodes;
    // TODO(erfanian): Remove in the framework implementation.
    this.countryCodeProvider = countryCodeProvider;
  }

  /**
@@ -201,8 +136,8 @@ final class Constraints {
    }

    boolean result =
        supportedCountryCodes.contains(userHomeCountryCode)
            && supportedCountryCodes.contains(userRoamingCountryCode);
        countryCodeProvider.isSupportedCountryCode(userHomeCountryCode)
            && countryCodeProvider.isSupportedCountryCode(userRoamingCountryCode);
    LogUtil.i("Constraints.areSupportedCountryCodes", String.valueOf(result));
    return result;
  }
+107 −0
Original line number Diff line number Diff line
/*
 * 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.VisibleForTesting;
import android.text.TextUtils;
import android.util.ArraySet;
import com.android.dialer.common.LogUtil;
import com.android.dialer.configprovider.ConfigProvider;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

/** A class to provide the appropriate country codes related to assisted dialing. */
@TargetApi(VERSION_CODES.N)
@SuppressWarnings("AndroidApiChecker") // Java 8 APIs
final class CountryCodeProvider {

  // 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.
  @VisibleForTesting
  static final List<String> DEFAULT_COUNTRY_CODES =
      Arrays.asList(
          "CA" /* Canada */,
          "GB" /* United Kingdom */,
          "JP" /* Japan */,
          "MX" /* Mexico */,
          "US" /* United States */);

  private final Set<String> supportedCountryCodes;

  CountryCodeProvider(ConfigProvider configProvider) {
    supportedCountryCodes =
        parseConfigProviderCountryCodes(
                configProvider.getString("assisted_dialing_csv_country_codes", ""))
            .stream()
            .map(v -> v.toUpperCase(Locale.US))
            .collect(Collectors.toCollection(ArraySet::new));
    LogUtil.i(
        "CountryCodeProvider.CountryCodeProvider", "Using country codes: " + supportedCountryCodes);
  }

  /** Checks whether a supplied country code is supported. */
  public boolean isSupportedCountryCode(String countryCode) {
    return supportedCountryCodes.contains(countryCode);
  }

  private List<String> parseConfigProviderCountryCodes(String configProviderCountryCodes) {
    if (TextUtils.isEmpty(configProviderCountryCodes)) {
      LogUtil.i(
          "Constraints.parseConfigProviderCountryCodes",
          "configProviderCountryCodes was empty, returning default");
      return DEFAULT_COUNTRY_CODES;
    }

    StringTokenizer tokenizer = new StringTokenizer(configProviderCountryCodes, ",");

    if (tokenizer.countTokens() < 1) {
      LogUtil.i(
          "Constraints.parseConfigProviderCountryCodes", "insufficient provided country codes");
      return DEFAULT_COUNTRY_CODES;
    }

    List<String> parsedCountryCodes = new ArrayList<>();
    while (tokenizer.hasMoreTokens()) {
      String foundLocale = tokenizer.nextToken();
      if (foundLocale == null) {
        LogUtil.i(
            "Constraints.parseConfigProviderCountryCodes",
            "Unexpected empty value, returning default.");
        return DEFAULT_COUNTRY_CODES;
      }

      if (foundLocale.length() != 2) {
        LogUtil.i(
            "Constraints.parseConfigProviderCountryCodes",
            "Unexpected locale %s, returning default",
            foundLocale);
        return DEFAULT_COUNTRY_CODES;
      }

      parsedCountryCodes.add(foundLocale);
    }
    return parsedCountryCodes;
  }
}