Example usage for android.telephony PhoneNumberUtils formatNumberToE164

List of usage examples for android.telephony PhoneNumberUtils formatNumberToE164

Introduction

In this page you can find the example usage for android.telephony PhoneNumberUtils formatNumberToE164.

Prototype

public static String formatNumberToE164(String phoneNumber, String defaultCountryIso) 

Source Link

Document

Formats the specified phoneNumber to the E.164 representation.

Usage

From source file:com.geniusgithub.contact.contact.calllog.ContactInfoHelper.java

/**
 * Returns the contact information for the given number.
 * <p>//  www .  ja v  a 2  s. co  m
 * If the number does not match any contact, returns a contact info containing only the number
 * and the formatted number.
 * <p>
 * If an error occurs during the lookup, it returns null.
 *
 * @param number the number to look up
 * @param countryIso the country associated with this number
 */
public ContactInfo lookupNumber(String number, String countryIso) {
    final ContactInfo info;

    // Determine the contact info.
    if (PhoneNumberHelper.isUriNumber(number)) {
        // This "number" is really a SIP address.
        ContactInfo sipInfo = queryContactInfoForSipAddress(number);
        if (sipInfo == null || sipInfo == ContactInfo.EMPTY) {
            // Check whether the "username" part of the SIP address is
            // actually the phone number of a contact.
            String username = PhoneNumberHelper.getUsernameFromUriNumber(number);
            if (PhoneNumberUtils.isGlobalPhoneNumber(username)) {
                sipInfo = queryContactInfoForPhoneNumber(username, countryIso);
            }
        }
        info = sipInfo;
    } else {
        // Look for a contact that has the given phone number.
        ContactInfo phoneInfo = queryContactInfoForPhoneNumber(number, countryIso);

        if (phoneInfo == null || phoneInfo == ContactInfo.EMPTY) {
            // Check whether the phone number has been saved as an "Internet call" number.
            phoneInfo = queryContactInfoForSipAddress(number);
        }
        info = phoneInfo;
    }

    final ContactInfo updatedInfo;
    if (info == null) {
        // The lookup failed.
        updatedInfo = null;
    } else {
        // If we did not find a matching contact, generate an empty contact info for the number.
        if (info == ContactInfo.EMPTY) {
            // Did not find a matching contact.
            updatedInfo = new ContactInfo();
            updatedInfo.number = number;
            updatedInfo.formattedNumber = formatPhoneNumber(number, null, countryIso);
            updatedInfo.normalizedNumber = PhoneNumberUtils.formatNumberToE164(number, countryIso);
            updatedInfo.lookupUri = createTemporaryContactUri(updatedInfo.formattedNumber);
        } else {
            updatedInfo = info;
        }
    }
    return updatedInfo;
}

From source file:com.android.dialer.calllog.ContactInfoHelper.java

/**
 * Returns the contact information for the given number.
 * <p>/*from  www  .j a  va2  s .c o m*/
 * If the number does not match any contact, returns a contact info containing only the number
 * and the formatted number.
 * <p>
 * If an error occurs during the lookup, it returns null.
 *
 * @param number the number to look up
 * @param countryIso the country associated with this number
 */
public ContactInfo lookupNumber(String number, String countryIso) {
    if (TextUtils.isEmpty(number)) {
        return null;
    }
    final ContactInfo info;

    // Determine the contact info.
    if (PhoneNumberHelper.isUriNumber(number)) {
        // This "number" is really a SIP address.
        ContactInfo sipInfo = queryContactInfoForSipAddress(number);
        if (sipInfo == null || sipInfo == ContactInfo.EMPTY) {
            // Check whether the "username" part of the SIP address is
            // actually the phone number of a contact.
            String username = PhoneNumberHelper.getUsernameFromUriNumber(number);
            if (PhoneNumberUtils.isGlobalPhoneNumber(username)) {
                sipInfo = queryContactInfoForPhoneNumber(username, countryIso);
            }
        }
        info = sipInfo;
    } else {
        // Look for a contact that has the given phone number.
        ContactInfo phoneInfo = queryContactInfoForPhoneNumber(number, countryIso);

        if (phoneInfo == null || phoneInfo == ContactInfo.EMPTY) {
            // Check whether the phone number has been saved as an "Internet call" number.
            phoneInfo = queryContactInfoForSipAddress(number);
        }
        info = phoneInfo;
    }

    final ContactInfo updatedInfo;
    if (info == null) {
        // The lookup failed.
        updatedInfo = null;
    } else {
        // If we did not find a matching contact, generate an empty contact info for the number.
        if (info == ContactInfo.EMPTY) {
            // Did not find a matching contact.
            updatedInfo = new ContactInfo();
            updatedInfo.number = number;
            updatedInfo.formattedNumber = formatPhoneNumber(number, null, countryIso);
            updatedInfo.normalizedNumber = PhoneNumberUtils.formatNumberToE164(number, countryIso);
            updatedInfo.lookupUri = createTemporaryContactUri(updatedInfo.formattedNumber);
        } else {
            updatedInfo = info;
        }
    }
    return updatedInfo;
}

From source file:im.delight.android.commons.Social.java

/**
 * Normalizes the specified phone number to its E.164 representation, if supported by the platform
 *
 * @param context a context reference/*from   w  w  w  .  j a  va2 s .  co  m*/
 * @param phoneNumber the phone number to normalize
 * @return the normalized phone number
 */
@SuppressLint("NewApi")
public static String normalizePhoneNumber(final Context context, final String phoneNumber) {
    if (Build.VERSION.SDK_INT >= 21) {
        final String countryIso2 = DeviceInfo.getCountryISO2(context);

        if (countryIso2 != null) {
            final String formatted = PhoneNumberUtils.formatNumberToE164(phoneNumber,
                    countryIso2.toUpperCase(Locale.US));

            if (formatted != null) {
                return formatted;
            }
        }
    }

    return phoneNumber;
}

From source file:com.geniusgithub.contact.contact.calllog.ContactInfoHelper.java

/**
 * Determines the contact information for the given phone number.
 * <p>// w w w.j a v a 2 s . c o  m
 * It returns the contact info if found.
 * <p>
 * If no contact corresponds to the given phone number, returns {@link ContactInfo#EMPTY}.
 * <p>
 * If the lookup fails for some other reason, it returns null.
 */
private ContactInfo queryContactInfoForPhoneNumber(String number, String countryIso) {
    String contactNumber = number;
    if (!TextUtils.isEmpty(countryIso)) {
        // Normalize the number: this is needed because the PhoneLookup query below does not
        // accept a country code as an input.
        String numberE164 = PhoneNumberUtils.formatNumberToE164(number, countryIso);
        if (!TextUtils.isEmpty(numberE164)) {
            // Only use it if the number could be formatted to E164.
            contactNumber = numberE164;
        }
    }

    // The "contactNumber" is a regular phone number, so use the PhoneLookup table.
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(contactNumber));
    ContactInfo info = lookupContactFromUri(uri);
    if (info != null && info != ContactInfo.EMPTY) {
        info.formattedNumber = formatPhoneNumber(number, null, countryIso);
    } else if (mCachedNumberLookupService != null) {
        CachedContactInfo cacheInfo = mCachedNumberLookupService.lookupCachedContactFromNumber(mContext,
                number);
        if (cacheInfo != null) {
            info = cacheInfo.getContactInfo().isBadData ? null : cacheInfo.getContactInfo();
        } else {
            info = null;
        }
    }
    return info;
}

From source file:com.android.dialer.calllog.ContactInfoHelper.java

/**
 * Determines the contact information for the given phone number.
 * <p>//from w  ww .j ava2 s  .co m
 * It returns the contact info if found.
 * <p>
 * If no contact corresponds to the given phone number, returns {@link ContactInfo#EMPTY}.
 * <p>
 * If the lookup fails for some other reason, it returns null.
 */
private ContactInfo queryContactInfoForPhoneNumber(String number, String countryIso) {
    if (TextUtils.isEmpty(number)) {
        return null;
    }
    String contactNumber = number;
    if (!TextUtils.isEmpty(countryIso)) {
        // Normalize the number: this is needed because the PhoneLookup query below does not
        // accept a country code as an input.
        String numberE164 = PhoneNumberUtils.formatNumberToE164(number, countryIso);
        if (!TextUtils.isEmpty(numberE164)) {
            // Only use it if the number could be formatted to E164.
            contactNumber = numberE164;
        }
    }

    // The "contactNumber" is a regular phone number, so use the PhoneLookup table.
    Uri uri = Uri.withAppendedPath(PhoneLookup.ENTERPRISE_CONTENT_FILTER_URI, Uri.encode(contactNumber));
    ContactInfo info = lookupContactFromUri(uri);
    if (info != null && info != ContactInfo.EMPTY) {
        info.formattedNumber = formatPhoneNumber(number, null, countryIso);
    } else if (mCachedNumberLookupService != null) {
        CachedContactInfo cacheInfo = mCachedNumberLookupService.lookupCachedContactFromNumber(mContext,
                number);
        if (cacheInfo != null) {
            info = cacheInfo.getContactInfo().isBadData ? null : cacheInfo.getContactInfo();
        } else {
            info = null;
        }
    }
    return info;
}