1 /*
2  * Copyright (C) 2022 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.server.appsearch.contactsindexer;
18 
19 import android.util.ArrayMap;
20 
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 
26 public class CountryCodeUtils {
CountryCodeUtils()27     private CountryCodeUtils() {}
28 
29     // Maps dialing code starting with "+" to its primary corresponding ISO 3166-1 alpha-2
30     // country code.
31     // E.g. "+1" -> "US".
32     public static final Map<String, String> COUNTRY_TO_REGIONAL_CODE;
33 
34     // A set of country dialing code. E.g. "+1", "+86" etc.
35     public static final Set<String> COUNTRY_DIALING_CODE;
36 
37     // Totally 240 entries.
38     static {
39         Map<Integer, List<String>> countryCodeToRegionCodeMap =
40                 CountryCodeToRegionCodeMap.getCountryCodeToRegionCodeMap();
41         Map<String, String> transformedCountryCodeToRegionCode = new ArrayMap<>();
42         for (Map.Entry<Integer, List<String>> entry : countryCodeToRegionCodeMap.entrySet()) {
43             Integer dialingCode = entry.getKey();
44             List<String> regionCodes = entry.getValue();
45             String dialingCodeString = "+" + dialingCode;
46             if (!regionCodes.isEmpty()) {
47                 // If we have a list of regional codes, just get the 1st one since it is the
48                 // primary country for the same country code.
transformedCountryCodeToRegionCode.put(dialingCodeString, regionCodes.get(0))49                 transformedCountryCodeToRegionCode.put(dialingCodeString, regionCodes.get(0));
50             }
51         }
52         COUNTRY_TO_REGIONAL_CODE = Collections.unmodifiableMap(transformedCountryCodeToRegionCode);
53         COUNTRY_DIALING_CODE = Collections.unmodifiableSet(COUNTRY_TO_REGIONAL_CODE.keySet());
54     }
55 }
56