use of com.google.i18n.phonenumbers.PhoneNumberUtil in project qksms by moezbhatti.
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.
*/
@SuppressLint("Override")
public static String formatNumberToE164(String phoneNumber, String defaultCountryIso) {
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
String result = null;
try {
Phonenumber.PhoneNumber pn = util.parse(phoneNumber, defaultCountryIso);
if (util.isValidNumber(pn)) {
result = util.format(pn, PhoneNumberUtil.PhoneNumberFormat.E164);
}
} catch (NumberParseException e) {
}
return result;
}
use of com.google.i18n.phonenumbers.PhoneNumberUtil in project cryptonomica by Cryptonomica.
the class TwilioUtils method checkPhoneNumber.
static void checkPhoneNumber(String phoneNumberStr) throws NumberParseException, IllegalArgumentException {
if (phoneNumberStr == null || phoneNumberStr.length() == 0) {
throw new IllegalArgumentException("Phone number is empty");
} else if (phoneNumberStr.charAt(0) != '+') {
throw new IllegalArgumentException("Phone number should start with '+'");
}
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber phoneNumber = phoneUtil.parse(phoneNumberStr, null);
if (!phoneUtil.isValidNumber(phoneNumber)) {
throw new IllegalArgumentException(phoneNumberStr + " is not valid phone number");
}
}
use of com.google.i18n.phonenumbers.PhoneNumberUtil in project android_packages_apps_Dialer by LineageOS.
the class MoreContactUtils method shouldCollapsePhoneNumbers.
// TODO: Move this to PhoneDataItem.shouldCollapse override
private static boolean shouldCollapsePhoneNumbers(String number1, String number2) {
// number. PhoneNumberUtil already distinguishes between 555 and 55#5.
if (number1.contains("#") != number2.contains("#") || number1.contains("*") != number2.contains("*")) {
return false;
}
// Now do the full phone number thing. split into parts, separated by waiting symbol
// and compare them individually
final String[] dataParts1 = number1.split(WAIT_SYMBOL_AS_STRING);
final String[] dataParts2 = number2.split(WAIT_SYMBOL_AS_STRING);
if (dataParts1.length != dataParts2.length) {
return false;
}
final PhoneNumberUtil util = PhoneNumberUtil.getInstance();
for (int i = 0; i < dataParts1.length; i++) {
// Match phone numbers represented by keypad letters, in which case prefer the
// phone number with letters.
final String dataPart1 = PhoneNumberUtils.convertKeypadLettersToDigits(dataParts1[i]);
final String dataPart2 = dataParts2[i];
// substrings equal? shortcut, don't parse
if (TextUtils.equals(dataPart1, dataPart2)) {
continue;
}
// do a full parse of the numbers
final PhoneNumberUtil.MatchType result = util.isNumberMatch(dataPart1, dataPart2);
switch(result) {
case NOT_A_NUMBER:
// don't understand the numbers? let's play it safe
return false;
case NO_MATCH:
return false;
case EXACT_MATCH:
break;
case NSN_MATCH:
try {
// In this case, prefer the +1 version.
if (util.parse(dataPart1, null).getCountryCode() == 1) {
// at the start of number 2.
if (dataPart2.trim().charAt(0) == '1') {
// case 1
return false;
}
break;
}
} catch (NumberParseException e) {
// existing unit test).
try {
util.parse(dataPart2, null);
} catch (NumberParseException e2) {
// Number 2 also does not have a country. Collapse.
break;
}
}
return false;
case SHORT_NSN_MATCH:
return false;
default:
throw new IllegalStateException("Unknown result value from phone number " + "library");
}
}
return true;
}
use of com.google.i18n.phonenumbers.PhoneNumberUtil in project android_packages_apps_Dialer by LineageOS.
the class GeoUtil method getGeocodedLocationFor.
public static String getGeocodedLocationFor(Context context, String phoneNumber) {
final PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
try {
final Phonenumber.PhoneNumber structuredPhoneNumber = phoneNumberUtil.parse(phoneNumber, getCurrentCountryIso(context));
final Locale locale = context.getResources().getConfiguration().locale;
return geocoder.getDescriptionForNumber(structuredPhoneNumber, locale);
} catch (NumberParseException e) {
return null;
}
}
use of com.google.i18n.phonenumbers.PhoneNumberUtil in project android_packages_apps_Dialer by LineageOS.
the class PhoneNumberHelper method getGeoDescription.
/**
* @return a geographical description string for the specified number.
* @see com.android.i18n.phonenumbers.PhoneNumberOfflineGeocoder
*/
public static String getGeoDescription(Context context, String number) {
LogUtil.v("PhoneNumberHelper.getGeoDescription", "" + LogUtil.sanitizePii(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.PhoneNumber pn = null;
try {
LogUtil.v("PhoneNumberHelper.getGeoDescription", "parsing '" + LogUtil.sanitizePii(number) + "' for countryIso '" + countryIso + "'...");
pn = util.parse(number, countryIso);
LogUtil.v("PhoneNumberHelper.getGeoDescription", "- parsed number: " + LogUtil.sanitizePii(pn));
} catch (NumberParseException e) {
LogUtil.e("PhoneNumberHelper.getGeoDescription", "getGeoDescription: NumberParseException for incoming number '" + LogUtil.sanitizePii(number) + "'");
}
if (pn != null) {
String description = geocoder.getDescriptionForNumber(pn, locale);
LogUtil.v("PhoneNumberHelper.getGeoDescription", "- got description: '" + description + "'");
return description;
}
return null;
}
Aggregations