Class ShortNumberInfo

java.lang.Object
com.google.i18n.phonenumbers.ShortNumberInfo

public class ShortNumberInfo extends Object
Methods for getting information about short phone numbers, such as short codes and emergency numbers. Note that most commercial short numbers are not handled here, but by the PhoneNumberUtil.
Author:
Shaopeng Jia, David Yonge-Mallo
  • Method Details

    • getInstance

      public static ShortNumberInfo getInstance()
      Returns the singleton instance of the ShortNumberInfo.
    • isPossibleShortNumberForRegion

      public boolean isPossibleShortNumberForRegion(Phonenumber.PhoneNumber number, String regionDialingFrom)
      Check whether a short number is a possible number when dialed from the given region. This provides a more lenient check than isValidShortNumberForRegion(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber, java.lang.String).
      Parameters:
      number - the short number to check
      regionDialingFrom - the region from which the number is dialed
      Returns:
      whether the number is a possible short number
    • isPossibleShortNumber

      public boolean isPossibleShortNumber(Phonenumber.PhoneNumber number)
      Check whether a short number is a possible number. If a country calling code is shared by multiple regions, this returns true if it's possible in any of them. This provides a more lenient check than isValidShortNumber(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber). See isPossibleShortNumberForRegion(PhoneNumber, String) for details.
      Parameters:
      number - the short number to check
      Returns:
      whether the number is a possible short number
    • isValidShortNumberForRegion

      public boolean isValidShortNumberForRegion(Phonenumber.PhoneNumber number, String regionDialingFrom)
      Tests whether a short number matches a valid pattern in a region. Note that this doesn't verify the number is actually in use, which is impossible to tell by just looking at the number itself.
      Parameters:
      number - the short number for which we want to test the validity
      regionDialingFrom - the region from which the number is dialed
      Returns:
      whether the short number matches a valid pattern
    • isValidShortNumber

      public boolean isValidShortNumber(Phonenumber.PhoneNumber number)
      Tests whether a short number matches a valid pattern. If a country calling code is shared by multiple regions, this returns true if it's valid in any of them. Note that this doesn't verify the number is actually in use, which is impossible to tell by just looking at the number itself. See isValidShortNumberForRegion(PhoneNumber, String) for details.
      Parameters:
      number - the short number for which we want to test the validity
      Returns:
      whether the short number matches a valid pattern
    • getExpectedCostForRegion

      public ShortNumberInfo.ShortNumberCost getExpectedCostForRegion(Phonenumber.PhoneNumber number, String regionDialingFrom)
      Gets the expected cost category of a short number when dialed from a region (however, nothing is implied about its validity). If it is important that the number is valid, then its validity must first be checked using isValidShortNumberForRegion(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber, java.lang.String). Note that emergency numbers are always considered toll-free. Example usage:
      
       // The region for which the number was parsed and the region we subsequently check against
       // need not be the same. Here we parse the number in the US and check it for Canada.
       PhoneNumber number = phoneUtil.parse("110", "US");
       ...
       String regionCode = "CA";
       ShortNumberInfo shortInfo = ShortNumberInfo.getInstance();
       if (shortInfo.isValidShortNumberForRegion(shortNumber, regionCode)) {
         ShortNumberCost cost = shortInfo.getExpectedCostForRegion(number, regionCode);
         // Do something with the cost information here.
       }
      Parameters:
      number - the short number for which we want to know the expected cost category
      regionDialingFrom - the region from which the number is dialed
      Returns:
      the expected cost category for that region of the short number. Returns UNKNOWN_COST if the number does not match a cost category. Note that an invalid number may match any cost category.
    • getExpectedCost

      Gets the expected cost category of a short number (however, nothing is implied about its validity). If the country calling code is unique to a region, this method behaves exactly the same as getExpectedCostForRegion(PhoneNumber, String). However, if the country calling code is shared by multiple regions, then it returns the highest cost in the sequence PREMIUM_RATE, UNKNOWN_COST, STANDARD_RATE, TOLL_FREE. The reason for the position of UNKNOWN_COST in this order is that if a number is UNKNOWN_COST in one region but STANDARD_RATE or TOLL_FREE in another, its expected cost cannot be estimated as one of the latter since it might be a PREMIUM_RATE number.

      For example, if a number is STANDARD_RATE in the US, but TOLL_FREE in Canada, the expected cost returned by this method will be STANDARD_RATE, since the NANPA countries share the same country calling code.

      Note: If the region from which the number is dialed is known, it is highly preferable to call getExpectedCostForRegion(PhoneNumber, String) instead.

      Parameters:
      number - the short number for which we want to know the expected cost category
      Returns:
      the highest expected cost category of the short number in the region(s) with the given country calling code
    • connectsToEmergencyNumber

      public boolean connectsToEmergencyNumber(String number, String regionCode)
      Returns true if the given number, exactly as dialed, might be used to connect to an emergency service in the given region.

      This method accepts a string, rather than a PhoneNumber, because it needs to distinguish cases such as "+1 911" and "911", where the former may not connect to an emergency service in all cases but the latter would. This method takes into account cases where the number might contain formatting, or might have additional digits appended (when it is okay to do that in the specified region).

      Parameters:
      number - the phone number to test
      regionCode - the region where the phone number is being dialed
      Returns:
      whether the number might be used to connect to an emergency service in the given region
    • isEmergencyNumber

      public boolean isEmergencyNumber(CharSequence number, String regionCode)
      Returns true if the given number exactly matches an emergency service number in the given region.

      This method takes into account cases where the number might contain formatting, but doesn't allow additional digits to be appended. Note that isEmergencyNumber(number, region) implies connectsToEmergencyNumber(number, region).

      Parameters:
      number - the phone number to test
      regionCode - the region where the phone number is being dialed
      Returns:
      whether the number exactly matches an emergency services number in the given region
    • isCarrierSpecific

      public boolean isCarrierSpecific(Phonenumber.PhoneNumber number)
      Given a valid short number, determines whether it is carrier-specific (however, nothing is implied about its validity). Carrier-specific numbers may connect to a different end-point, or not connect at all, depending on the user's carrier. If it is important that the number is valid, then its validity must first be checked using isValidShortNumber(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber) or isValidShortNumberForRegion(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber, java.lang.String).
      Parameters:
      number - the valid short number to check
      Returns:
      whether the short number is carrier-specific, assuming the input was a valid short number
    • isCarrierSpecificForRegion

      public boolean isCarrierSpecificForRegion(Phonenumber.PhoneNumber number, String regionDialingFrom)
      Given a valid short number, determines whether it is carrier-specific when dialed from the given region (however, nothing is implied about its validity). Carrier-specific numbers may connect to a different end-point, or not connect at all, depending on the user's carrier. If it is important that the number is valid, then its validity must first be checked using isValidShortNumber(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber) or isValidShortNumberForRegion(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber, java.lang.String). Returns false if the number doesn't match the region provided.
      Parameters:
      number - the valid short number to check
      regionDialingFrom - the region from which the number is dialed
      Returns:
      whether the short number is carrier-specific in the provided region, assuming the input was a valid short number
    • isSmsServiceForRegion

      public boolean isSmsServiceForRegion(Phonenumber.PhoneNumber number, String regionDialingFrom)
      Given a valid short number, determines whether it is an SMS service (however, nothing is implied about its validity). An SMS service is where the primary or only intended usage is to receive and/or send text messages (SMSs). This includes MMS as MMS numbers downgrade to SMS if the other party isn't MMS-capable. If it is important that the number is valid, then its validity must first be checked using isValidShortNumber(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber) or isValidShortNumberForRegion(com.google.i18n.phonenumbers.Phonenumber.PhoneNumber, java.lang.String). Returns false if the number doesn't match the region provided.
      Parameters:
      number - the valid short number to check
      regionDialingFrom - the region from which the number is dialed
      Returns:
      whether the short number is an SMS service in the provided region, assuming the input was a valid short number