Search in sources :

Example 1 with PhoneNumber

use of com.android.i18n.phonenumbers.Phonenumber.PhoneNumber in project platform_frameworks_base by android.

the class CallerInfo method getGeoDescription.

/**
     * @return a geographical description string for the specified number.
     * @see com.android.i18n.phonenumbers.PhoneNumberOfflineGeocoder
     */
private static String getGeoDescription(Context context, String number) {
    if (VDBG)
        Rlog.v(TAG, "getGeoDescription('" + number + "')...");
    if (TextUtils.isEmpty(number)) {
        return null;
    }
    PhoneNumberUtil util = PhoneNumberUtil.getInstance();
    PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
    Locale locale = context.getResources().getConfiguration().locale;
    String countryIso = getCurrentCountryIso(context, locale);
    PhoneNumber pn = null;
    try {
        if (VDBG)
            Rlog.v(TAG, "parsing '" + number + "' for countryIso '" + countryIso + "'...");
        pn = util.parse(number, countryIso);
        if (VDBG)
            Rlog.v(TAG, "- parsed number: " + pn);
    } catch (NumberParseException e) {
        Rlog.w(TAG, "getGeoDescription: NumberParseException for incoming number '" + Rlog.pii(TAG, number) + "'");
    }
    if (pn != null) {
        String description = geocoder.getDescriptionForNumber(pn, locale);
        if (VDBG)
            Rlog.v(TAG, "- got description: '" + description + "'");
        return description;
    } else {
        return null;
    }
}
Also used : Locale(java.util.Locale) PhoneNumberUtil(com.android.i18n.phonenumbers.PhoneNumberUtil) PhoneNumberOfflineGeocoder(com.android.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder) PhoneNumber(com.android.i18n.phonenumbers.Phonenumber.PhoneNumber) NumberParseException(com.android.i18n.phonenumbers.NumberParseException)

Example 2 with PhoneNumber

use of com.android.i18n.phonenumbers.Phonenumber.PhoneNumber in project XobotOS by xamarin.

the class PhoneNumberUtil method parse.

/**
   * Parses a string and returns it in proto buffer format. This method will throw a
   * {@link com.android.i18n.phonenumbers.NumberParseException} if the number is not considered to be
   * a possible number. Note that validation of whether the number is actually a valid number for a
   * particular region is not performed. This can be done separately with {@link #isValidNumber}.
   *
   * @param numberToParse     number that we are attempting to parse. This can contain formatting
   *                          such as +, ( and -, as well as a phone number extension.
   * @param defaultRegion     region that we are expecting the number to be from. This is only used
   *                          if the number being parsed is not written in international format.
   *                          The country_code for the number in this case would be stored as that
   *                          of the default region supplied. If the number is guaranteed to
   *                          start with a '+' followed by the country calling code, then
   *                          "ZZ" or null can be supplied.
   * @return                  a phone number proto buffer filled with the parsed number
   * @throws NumberParseException  if the string is not considered to be a viable phone number or if
   *                               no default region was supplied and the number is not in
   *                               international format (does not start with +)
   */
public PhoneNumber parse(String numberToParse, String defaultRegion) throws NumberParseException {
    PhoneNumber phoneNumber = new PhoneNumber();
    parse(numberToParse, defaultRegion, phoneNumber);
    return phoneNumber;
}
Also used : PhoneNumber(com.android.i18n.phonenumbers.Phonenumber.PhoneNumber)

Example 3 with PhoneNumber

use of com.android.i18n.phonenumbers.Phonenumber.PhoneNumber in project XobotOS by xamarin.

the class PhoneNumberUtil method getLengthOfNationalDestinationCode.

/**
   * Gets the length of the national destination code (NDC) from the PhoneNumber object passed in,
   * so that clients could use it to split a national significant number into NDC and subscriber
   * number. The NDC of a phone number is normally the first group of digit(s) right after the
   * country calling code when the number is formatted in the international format, if there is a
   * subscriber number part that follows. An example of how this could be used:
   *
   * <pre>
   * PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
   * PhoneNumber number = phoneUtil.parse("18002530000", "US");
   * String nationalSignificantNumber = phoneUtil.getNationalSignificantNumber(number);
   * String nationalDestinationCode;
   * String subscriberNumber;
   *
   * int nationalDestinationCodeLength = phoneUtil.getLengthOfNationalDestinationCode(number);
   * if (nationalDestinationCodeLength > 0) {
   *   nationalDestinationCode = nationalSignificantNumber.substring(0,
   *       nationalDestinationCodeLength);
   *   subscriberNumber = nationalSignificantNumber.substring(nationalDestinationCodeLength);
   * } else {
   *   nationalDestinationCode = "";
   *   subscriberNumber = nationalSignificantNumber;
   * }
   * </pre>
   *
   * Refer to the unittests to see the difference between this function and
   * {@link #getLengthOfGeographicalAreaCode}.
   *
   * @param number  the PhoneNumber object for which clients want to know the length of the NDC.
   * @return  the length of NDC of the PhoneNumber object passed in.
   */
public int getLengthOfNationalDestinationCode(PhoneNumber number) {
    PhoneNumber copiedProto;
    if (number.hasExtension()) {
        // We don't want to alter the proto given to us, but we don't want to include the extension
        // when we format it, so we copy it and clear the extension here.
        copiedProto = new PhoneNumber();
        copiedProto.mergeFrom(number);
        copiedProto.clearExtension();
    } else {
        copiedProto = number;
    }
    String nationalSignificantNumber = format(copiedProto, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
    String[] numberGroups = NON_DIGITS_PATTERN.split(nationalSignificantNumber);
    // group will be area code if it is not the last group.
    if (numberGroups.length <= 3) {
        return 0;
    }
    if (getRegionCodeForNumber(number).equals("AR") && getNumberType(number) == PhoneNumberType.MOBILE) {
        // easier to obtain the NDC.
        return numberGroups[3].length() + 1;
    }
    return numberGroups[2].length();
}
Also used : PhoneNumber(com.android.i18n.phonenumbers.Phonenumber.PhoneNumber)

Example 4 with PhoneNumber

use of com.android.i18n.phonenumbers.Phonenumber.PhoneNumber in project XobotOS by xamarin.

the class PhoneNumberUtil method isNumberMatch.

/**
   * Takes two phone numbers as strings and compares them for equality. This is a convenience
   * wrapper for {@link #isNumberMatch(Phonenumber.PhoneNumber, Phonenumber.PhoneNumber)}. No
   * default region is known.
   *
   * @param firstNumber  first number to compare. Can contain formatting, and can have country
   *     calling code specified with + at the start.
   * @param secondNumber  second number to compare. Can contain formatting, and can have country
   *     calling code specified with + at the start.
   * @return  NOT_A_NUMBER, NO_MATCH, SHORT_NSN_MATCH, NSN_MATCH, EXACT_MATCH. See
   *     {@link #isNumberMatch(Phonenumber.PhoneNumber, Phonenumber.PhoneNumber)} for more details.
   */
public MatchType isNumberMatch(String firstNumber, String secondNumber) {
    try {
        PhoneNumber firstNumberAsProto = parse(firstNumber, UNKNOWN_REGION);
        return isNumberMatch(firstNumberAsProto, secondNumber);
    } catch (NumberParseException e) {
        if (e.getErrorType() == NumberParseException.ErrorType.INVALID_COUNTRY_CODE) {
            try {
                PhoneNumber secondNumberAsProto = parse(secondNumber, UNKNOWN_REGION);
                return isNumberMatch(secondNumberAsProto, firstNumber);
            } catch (NumberParseException e2) {
                if (e2.getErrorType() == NumberParseException.ErrorType.INVALID_COUNTRY_CODE) {
                    try {
                        PhoneNumber firstNumberProto = new PhoneNumber();
                        PhoneNumber secondNumberProto = new PhoneNumber();
                        parseHelper(firstNumber, null, false, false, firstNumberProto);
                        parseHelper(secondNumber, null, false, false, secondNumberProto);
                        return isNumberMatch(firstNumberProto, secondNumberProto);
                    } catch (NumberParseException e3) {
                    // Fall through and return MatchType.NOT_A_NUMBER.
                    }
                }
            }
        }
    }
    // One or more of the phone numbers we are trying to match is not a viable phone number.
    return MatchType.NOT_A_NUMBER;
}
Also used : PhoneNumber(com.android.i18n.phonenumbers.Phonenumber.PhoneNumber)

Example 5 with PhoneNumber

use of com.android.i18n.phonenumbers.Phonenumber.PhoneNumber in project XobotOS by xamarin.

the class PhoneNumberUtils method formatNumberToE164.

/**
     * Format the given phoneNumber to the E.164 representation.
     * <p>
     * The given phone number must have an area code and could have a country
     * code.
     * <p>
     * The defaultCountryIso is used to validate the given number and generate
     * the E.164 phone number if the given number doesn't have a country code.
     *
     * @param phoneNumber
     *            the phone number to format
     * @param defaultCountryIso
     *            the ISO 3166-1 two letters country code
     * @return the E.164 representation, or null if the given phone number is
     *         not valid.
     *
     * @hide
     */
public static String formatNumberToE164(String phoneNumber, String defaultCountryIso) {
    PhoneNumberUtil util = PhoneNumberUtil.getInstance();
    String result = null;
    try {
        PhoneNumber pn = util.parse(phoneNumber, defaultCountryIso);
        if (util.isValidNumber(pn)) {
            result = util.format(pn, PhoneNumberFormat.E164);
        }
    } catch (NumberParseException e) {
    }
    return result;
}
Also used : PhoneNumberUtil(com.android.i18n.phonenumbers.PhoneNumberUtil) PhoneNumber(com.android.i18n.phonenumbers.Phonenumber.PhoneNumber) NumberParseException(com.android.i18n.phonenumbers.NumberParseException)

Aggregations

PhoneNumber (com.android.i18n.phonenumbers.Phonenumber.PhoneNumber)22 NumberParseException (com.android.i18n.phonenumbers.NumberParseException)16 PhoneNumberUtil (com.android.i18n.phonenumbers.PhoneNumberUtil)16 PhoneNumberOfflineGeocoder (com.android.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder)6 Locale (java.util.Locale)6 TtsSpan (android.text.style.TtsSpan)4