• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Libphonenumber Authors
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.google.i18n.phonenumbers;
18 
19 import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberType;
20 import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
21 import com.google.i18n.phonenumbers.metadata.DefaultMetadataDependenciesProvider;
22 import com.google.i18n.phonenumbers.prefixmapper.PrefixFileReader;
23 import java.util.Locale;
24 
25 /**
26  * A phone prefix mapper which provides carrier information related to a phone number.
27  *
28  * @author Cecilia Roes
29  */
30 public class PhoneNumberToCarrierMapper {
31   private static PhoneNumberToCarrierMapper instance = null;
32   private final PrefixFileReader prefixFileReader;
33 
34   private final PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
35 
36   // @VisibleForTesting
PhoneNumberToCarrierMapper(String phonePrefixDataDirectory)37   PhoneNumberToCarrierMapper(String phonePrefixDataDirectory) {
38     prefixFileReader = new PrefixFileReader(phonePrefixDataDirectory);
39   }
40 
41   /**
42    * Gets a {@link PhoneNumberToCarrierMapper} instance to carry out international carrier lookup.
43    *
44    * <p> The {@link PhoneNumberToCarrierMapper} is implemented as a singleton. Therefore, calling
45    * this method multiple times will only result in one instance being created.
46    *
47    * @return  a {@link PhoneNumberToCarrierMapper} instance
48    */
getInstance()49   public static synchronized PhoneNumberToCarrierMapper getInstance() {
50     if (instance == null) {
51       instance = new PhoneNumberToCarrierMapper(DefaultMetadataDependenciesProvider.getInstance()
52           .getCarrierDataDirectory());
53     }
54     return instance;
55   }
56 
57   /**
58    * Returns a carrier name for the given phone number, in the language provided. The carrier name
59    * is the one the number was originally allocated to, however if the country supports mobile
60    * number portability the number might not belong to the returned carrier anymore. If no mapping
61    * is found an empty string is returned.
62    *
63    * <p>This method assumes the validity of the number passed in has already been checked, and that
64    * the number is suitable for carrier lookup. We consider mobile and pager numbers possible
65    * candidates for carrier lookup.
66    *
67    * @param number  a valid phone number for which we want to get a carrier name
68    * @param languageCode  the language code in which the name should be written
69    * @return  a carrier name for the given phone number
70    */
getNameForValidNumber(PhoneNumber number, Locale languageCode)71   public String getNameForValidNumber(PhoneNumber number, Locale languageCode) {
72     String langStr = languageCode.getLanguage();
73     String scriptStr = "";  // No script is specified
74     String regionStr = languageCode.getCountry();
75 
76     return prefixFileReader.getDescriptionForNumber(number, langStr, scriptStr, regionStr);
77   }
78 
79   /**
80    * Gets the name of the carrier for the given phone number, in the language provided. As per
81    * {@link #getNameForValidNumber(PhoneNumber, Locale)} but explicitly checks the validity of
82    * the number passed in.
83    *
84    * @param number  the phone number for which we want to get a carrier name
85    * @param languageCode  the language code in which the name should be written
86    * @return  a carrier name for the given phone number, or empty string if the number passed in is
87    *     invalid
88    */
getNameForNumber(PhoneNumber number, Locale languageCode)89   public String getNameForNumber(PhoneNumber number, Locale languageCode) {
90     PhoneNumberType numberType = phoneUtil.getNumberType(number);
91     if (isMobile(numberType)) {
92       return getNameForValidNumber(number, languageCode);
93     }
94     return "";
95   }
96 
97   /**
98    * Gets the name of the carrier for the given phone number only when it is 'safe' to display to
99    * users. A carrier name is considered safe if the number is valid and for a region that doesn't
100    * support
101    * <a href="http://en.wikipedia.org/wiki/Mobile_number_portability">mobile number portability</a>.
102    *
103    * @param number  the phone number for which we want to get a carrier name
104    * @param languageCode  the language code in which the name should be written
105    * @return  a carrier name that is safe to display to users, or the empty string
106    */
getSafeDisplayName(PhoneNumber number, Locale languageCode)107   public String getSafeDisplayName(PhoneNumber number, Locale languageCode) {
108     if (phoneUtil.isMobileNumberPortableRegion(phoneUtil.getRegionCodeForNumber(number))) {
109       return "";
110     }
111     return getNameForNumber(number, languageCode);
112   }
113 
114   /**
115    * Checks if the supplied number type supports carrier lookup.
116    */
isMobile(PhoneNumberType numberType)117   private boolean isMobile(PhoneNumberType numberType) {
118     return (numberType == PhoneNumberType.MOBILE
119         || numberType == PhoneNumberType.FIXED_LINE_OR_MOBILE
120         || numberType == PhoneNumberType.PAGER);
121   }
122 }
123