getExpectedCostForRegion method

ShortNumberCost getExpectedCostForRegion(
  1. PhoneNumber number,
  2. 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. 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.instance;
if (shortInfo.isValidShortNumberForRegion(shortNumber, regionCode)) {
  ShortNumberCost cost = shortInfo.getExpectedCostForRegion(number, regionCode);
  // Do something with the cost information here.

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 ShortNumberCost.unknownCost if the number does not match a cost category. Note that an invalid number may match any cost category.

Implementation

ShortNumberCost getExpectedCostForRegion(
    PhoneNumber number, String regionDialingFrom) {
  if (!_regionDialingFromMatchesNumber(number, regionDialingFrom)) {
    return ShortNumberCost.unknownCost;
  }
  // Note that regionDialingFrom may be null, in which case phoneMetadata will also be null.
  PhoneMetadata? phoneMetadata =
      _getShortNumberMetadataForRegion(regionDialingFrom);
  if (phoneMetadata == null) {
    return ShortNumberCost.unknownCost;
  }

  String shortNumber = _getNationalSignificantNumber(number);

  // The possible lengths are not present for a particular sub-type if they match the general
  // description; for this reason, we check the possible lengths against the general description
  // first to allow an early exit if possible.
  if (!phoneMetadata.generalDesc.possibleLength
      .contains(shortNumber.length)) {
    return ShortNumberCost.unknownCost;
  }

  // The cost categories are tested in order of decreasing expense, since if for some reason the
  // patterns overlap the most expensive matching cost category should be returned.
  if (_matchesPossibleNumberAndNationalNumber(
      shortNumber, phoneMetadata.premiumRate)) {
    return ShortNumberCost.premiumRate;
  }
  if (_matchesPossibleNumberAndNationalNumber(
      shortNumber, phoneMetadata.standardRate)) {
    return ShortNumberCost.standardRate;
  }
  if (_matchesPossibleNumberAndNationalNumber(
      shortNumber, phoneMetadata.tollFree)) {
    return ShortNumberCost.tollFree;
  }
  if (isEmergencyNumber(shortNumber, regionDialingFrom)) {
    // Emergency numbers are implicitly toll-free.
    return ShortNumberCost.tollFree;
  }
  return ShortNumberCost.unknownCost;
}