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.support.annotation.VisibleForTesting; 20 import android.text.TextUtils; 21 import android.util.ArraySet; 22 import com.android.dialer.common.LogUtil; 23 import com.android.dialer.configprovider.ConfigProvider; 24 import java.util.ArrayList; 25 import java.util.Arrays; 26 import java.util.List; 27 import java.util.Locale; 28 import java.util.Set; 29 import java.util.StringTokenizer; 30 import java.util.stream.Collectors; 31 32 /** A class to provide the appropriate country codes related to assisted dialing. */ 33 public final class CountryCodeProvider { 34 35 // TODO(erfanian): Ensure the below standard is consistent between libphonenumber and the 36 // platform. 37 // ISO 3166-1 alpha-2 Country Codes that are eligible for assisted dialing. 38 @VisibleForTesting 39 static final List<String> DEFAULT_COUNTRY_CODES = 40 Arrays.asList( 41 "CA" /* Canada */, 42 "GB" /* United Kingdom */, 43 "JP" /* Japan */, 44 "MX" /* Mexico */, 45 "US" /* United States */); 46 47 private final Set<String> supportedCountryCodes; 48 CountryCodeProvider(ConfigProvider configProvider)49 CountryCodeProvider(ConfigProvider configProvider) { 50 supportedCountryCodes = 51 parseConfigProviderCountryCodes( 52 configProvider.getString("assisted_dialing_csv_country_codes", "")) 53 .stream() 54 .map(v -> v.toUpperCase(Locale.US)) 55 .collect(Collectors.toCollection(ArraySet::new)); 56 LogUtil.i( 57 "CountryCodeProvider.CountryCodeProvider", "Using country codes: " + supportedCountryCodes); 58 } 59 60 /** Checks whether a supplied country code is supported. */ isSupportedCountryCode(String countryCode)61 public boolean isSupportedCountryCode(String countryCode) { 62 return supportedCountryCodes.contains(countryCode); 63 } 64 parseConfigProviderCountryCodes(String configProviderCountryCodes)65 private List<String> parseConfigProviderCountryCodes(String configProviderCountryCodes) { 66 if (TextUtils.isEmpty(configProviderCountryCodes)) { 67 LogUtil.i( 68 "Constraints.parseConfigProviderCountryCodes", 69 "configProviderCountryCodes was empty, returning default"); 70 return DEFAULT_COUNTRY_CODES; 71 } 72 73 StringTokenizer tokenizer = new StringTokenizer(configProviderCountryCodes, ","); 74 75 if (tokenizer.countTokens() < 1) { 76 LogUtil.i( 77 "Constraints.parseConfigProviderCountryCodes", "insufficient provided country codes"); 78 return DEFAULT_COUNTRY_CODES; 79 } 80 81 List<String> parsedCountryCodes = new ArrayList<>(); 82 while (tokenizer.hasMoreTokens()) { 83 String foundLocale = tokenizer.nextToken(); 84 if (foundLocale == null) { 85 LogUtil.i( 86 "Constraints.parseConfigProviderCountryCodes", 87 "Unexpected empty value, returning default."); 88 return DEFAULT_COUNTRY_CODES; 89 } 90 91 if (foundLocale.length() != 2) { 92 LogUtil.i( 93 "Constraints.parseConfigProviderCountryCodes", 94 "Unexpected locale %s, returning default", 95 foundLocale); 96 return DEFAULT_COUNTRY_CODES; 97 } 98 99 parsedCountryCodes.add(foundLocale); 100 } 101 return parsedCountryCodes; 102 } 103 } 104