Home | History | Annotate | Download | only in assisteddialing
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.dialer.assisteddialing;
     18 
     19 import android.annotation.TargetApi;
     20 import android.os.Build.VERSION_CODES;
     21 import android.support.annotation.VisibleForTesting;
     22 import android.text.TextUtils;
     23 import android.util.ArraySet;
     24 import com.android.dialer.common.LogUtil;
     25 import com.android.dialer.configprovider.ConfigProvider;
     26 import java.util.ArrayList;
     27 import java.util.Arrays;
     28 import java.util.List;
     29 import java.util.Locale;
     30 import java.util.Set;
     31 import java.util.StringTokenizer;
     32 import java.util.stream.Collectors;
     33 
     34 /** A class to provide the appropriate country codes related to assisted dialing. */
     35 @TargetApi(VERSION_CODES.N)
     36 @SuppressWarnings("AndroidApiChecker") // Java 8 APIs
     37 public final class CountryCodeProvider {
     38 
     39   // TODO(erfanian): Ensure the below standard is consistent between libphonenumber and the
     40   // platform.
     41   // ISO 3166-1 alpha-2 Country Codes that are eligible for assisted dialing.
     42   @VisibleForTesting
     43   static final List<String> DEFAULT_COUNTRY_CODES =
     44       Arrays.asList(
     45           "CA" /* Canada */,
     46           "GB" /* United Kingdom */,
     47           "JP" /* Japan */,
     48           "MX" /* Mexico */,
     49           "US" /* United States */);
     50 
     51   private final Set<String> supportedCountryCodes;
     52 
     53   CountryCodeProvider(ConfigProvider configProvider) {
     54     supportedCountryCodes =
     55         parseConfigProviderCountryCodes(
     56                 configProvider.getString("assisted_dialing_csv_country_codes", ""))
     57             .stream()
     58             .map(v -> v.toUpperCase(Locale.US))
     59             .collect(Collectors.toCollection(ArraySet::new));
     60     LogUtil.i(
     61         "CountryCodeProvider.CountryCodeProvider", "Using country codes: " + supportedCountryCodes);
     62   }
     63 
     64   /** Checks whether a supplied country code is supported. */
     65   public boolean isSupportedCountryCode(String countryCode) {
     66     return supportedCountryCodes.contains(countryCode);
     67   }
     68 
     69   private List<String> parseConfigProviderCountryCodes(String configProviderCountryCodes) {
     70     if (TextUtils.isEmpty(configProviderCountryCodes)) {
     71       LogUtil.i(
     72           "Constraints.parseConfigProviderCountryCodes",
     73           "configProviderCountryCodes was empty, returning default");
     74       return DEFAULT_COUNTRY_CODES;
     75     }
     76 
     77     StringTokenizer tokenizer = new StringTokenizer(configProviderCountryCodes, ",");
     78 
     79     if (tokenizer.countTokens() < 1) {
     80       LogUtil.i(
     81           "Constraints.parseConfigProviderCountryCodes", "insufficient provided country codes");
     82       return DEFAULT_COUNTRY_CODES;
     83     }
     84 
     85     List<String> parsedCountryCodes = new ArrayList<>();
     86     while (tokenizer.hasMoreTokens()) {
     87       String foundLocale = tokenizer.nextToken();
     88       if (foundLocale == null) {
     89         LogUtil.i(
     90             "Constraints.parseConfigProviderCountryCodes",
     91             "Unexpected empty value, returning default.");
     92         return DEFAULT_COUNTRY_CODES;
     93       }
     94 
     95       if (foundLocale.length() != 2) {
     96         LogUtil.i(
     97             "Constraints.parseConfigProviderCountryCodes",
     98             "Unexpected locale %s, returning default",
     99             foundLocale);
    100         return DEFAULT_COUNTRY_CODES;
    101       }
    102 
    103       parsedCountryCodes.add(foundLocale);
    104     }
    105     return parsedCountryCodes;
    106   }
    107 }
    108