Java Code Examples for com.google.i18n.phonenumbers.PhoneNumberUtil#format()
The following examples show how to use
com.google.i18n.phonenumbers.PhoneNumberUtil#format() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PhoneNumberFormatter.java From mollyim-android with GNU General Public License v3.0 | 6 votes |
public static String formatE164(String countryCode, String number) { try { PhoneNumberUtil util = PhoneNumberUtil.getInstance(); int parsedCountryCode = Integer.parseInt(countryCode); PhoneNumber parsedNumber = util.parse(number, util.getRegionCodeForCountryCode(parsedCountryCode)); return util.format(parsedNumber, PhoneNumberUtil.PhoneNumberFormat.E164); } catch (NumberParseException | NumberFormatException npe) { Log.w(TAG, npe); } return "+" + countryCode.replaceAll("[^0-9]", "").replaceAll("^0*", "") + number.replaceAll("[^0-9]", ""); }
Example 2
Source File: PhoneNumberFormatter.java From libsignal-service-java with GNU General Public License v3.0 | 6 votes |
public static String formatE164(String countryCode, String number) { try { PhoneNumberUtil util = PhoneNumberUtil.getInstance(); int parsedCountryCode = Integer.parseInt(countryCode); PhoneNumber parsedNumber = util.parse(number, util.getRegionCodeForCountryCode(parsedCountryCode)); return util.format(parsedNumber, PhoneNumberUtil.PhoneNumberFormat.E164); } catch (NumberParseException | NumberFormatException npe) { Log.w(TAG, npe); } return "+" + countryCode.replaceAll("[^0-9]", "").replaceAll("^0*", "") + number.replaceAll("[^0-9]", ""); }
Example 3
Source File: Utilities.java From call_manage with MIT License | 6 votes |
/** * Format a given phone number to a readable string for the user * * @param phoneNumber the number to format * @return the formatted number */ public static String formatPhoneNumber(String phoneNumber) { if (phoneNumber == null) return null; phoneNumber = normalizePhoneNumber(phoneNumber); Phonenumber.PhoneNumber formattedNumber = null; PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); try { formattedNumber = phoneUtil.parse(phoneNumber, sLocale.getCountry()); } catch (NumberParseException e) { Timber.w(e); return phoneNumber; } PhoneNumberUtil.PhoneNumberFormat format; if (phoneUtil.getRegionCodeForCountryCode(formattedNumber.getCountryCode()).equals(sLocale.getCountry())) format = PhoneNumberUtil.PhoneNumberFormat.NATIONAL; else format = PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL; return phoneUtil.format(formattedNumber, format); }
Example 4
Source File: MessagesDatabase.java From weMessage with GNU Affero General Public License v3.0 | 6 votes |
public Handle getHandleByAccount(String account) throws SQLException { try { PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); Phonenumber.PhoneNumber numberOne = phoneNumberUtil.parse(account, Locale.getDefault().getCountry()); String numberStringOne = phoneNumberUtil.format(numberOne, PhoneNumberUtil.PhoneNumberFormat.E164); Handle handle = getHandleById(numberStringOne); if (handle == null){ Phonenumber.PhoneNumber numberTwo = numberOne.clearCountryCode(); String numberStringTwo = phoneNumberUtil.format(numberTwo, PhoneNumberUtil.PhoneNumberFormat.E164).substring(2); return getHandleById(numberStringTwo); }else { return handle; } }catch(Exception ex){ return getHandleById(account); } }
Example 5
Source File: Handle.java From weMessage with GNU Affero General Public License v3.0 | 6 votes |
public static String parseHandleId(String handleID){ PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); String locale = Resources.getSystem().getConfiguration().locale.getCountry(); String returnHandle; if (phoneNumberUtil.isPossibleNumber(handleID, locale)){ try { Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(handleID, locale); returnHandle = phoneNumberUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164); }catch (Exception ex){ returnHandle = handleID; } } else { returnHandle = handleID.trim().toLowerCase(); } return returnHandle; }
Example 6
Source File: Global.java From Faceless with GNU General Public License v3.0 | 6 votes |
public static String[] normalizeNumber(String phoneNumber, String defaultRegion) { PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); PhoneNumber input; try { input = phoneUtil.parse(phoneNumber, defaultRegion.trim().toUpperCase(Locale.US)); if (phoneUtil.isValidNumber(input)) { String parsedRegionCode = phoneUtil.getRegionCodeForNumber(input); if (parsedRegionCode != null) { return new String[] { phoneUtil.format(input, PhoneNumberUtil.PhoneNumberFormat.E164), parsedRegionCode }; } else { return null; } } else { return null; } } catch (Exception e) { return null; } }
Example 7
Source File: ExpandableAdvisoriesAdapter.java From AirMapSDK-Android with Apache License 2.0 | 6 votes |
private String formatPhoneNumber(Context context, String number) { if (TextUtils.isEmpty(number)) { return context.getString(R.string.no_known_number); } PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); Locale locale = Locale.getDefault(); String country = locale != null && locale.getCountry() != null && !TextUtils.isEmpty(locale.getCountry()) ? locale.getCountry() : "US"; try { Phonenumber.PhoneNumber phoneNumber = phoneUtil.parse(number, country); return phoneUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL); } catch (NumberParseException e) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return PhoneNumberUtils.formatNumber(number, country); } return PhoneNumberUtils.formatNumber(number); } }
Example 8
Source File: UiUtils.java From tindroid with Apache License 2.0 | 6 votes |
static Credential parseCredential(String cred) { final PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); final String country = Locale.getDefault().getCountry(); if (Patterns.PHONE.matcher(cred).matches()) { // Looks like a phone number. try { // Normalize phone number format cred = phoneUtil.format(phoneUtil.parse(cred, country), PhoneNumberUtil.PhoneNumberFormat.E164); // Exception not thrown, we have a phone number. return new Credential(Credential.METH_PHONE, cred); } catch (NumberParseException ignored) { return null; } } // Not a phone number. Try parsing as email. if (Patterns.EMAIL_ADDRESS.matcher(cred).matches()) { return new Credential(Credential.METH_EMAIL, cred); } return null; }
Example 9
Source File: MainPresenter.java From YaMvp with MIT License | 6 votes |
private String validateThenFormatMobileNumber(String phone) { try { PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(phone, "+86"); if (phoneNumberUtil.isValidNumber(phoneNumber) && ( phoneNumberUtil.getNumberType(phoneNumber) == PhoneNumberUtil.PhoneNumberType.MOBILE || phoneNumberUtil.getNumberType(phoneNumber) == PhoneNumberUtil.PhoneNumberType.FIXED_LINE_OR_MOBILE)) { return phoneNumberUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164); } else { return ""; } } catch (NumberParseException e) { return ""; } }
Example 10
Source File: FormatPhoneNumber.java From levelup-java-examples with Apache License 2.0 | 5 votes |
@Test public void format_phone_number_google_libphonenumber () throws NumberParseException { PhoneNumber phoneNumber = new PhoneNumber(); phoneNumber.setCountryCode(1); phoneNumber.setExtension("12345"); phoneNumber.setNationalNumber(1234567890); PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); String formattedPhoneNumber = phoneUtil.format(phoneNumber, PhoneNumberFormat.NATIONAL); assertEquals("(123) 456-7890 ext. 12345", formattedPhoneNumber); }
Example 11
Source File: PhoneNumberFormatter.java From Silence with GNU General Public License v3.0 | 5 votes |
public static String getInternationalFormatFromE164(String e164number) { try { PhoneNumberUtil util = PhoneNumberUtil.getInstance(); PhoneNumber parsedNumber = util.parse(e164number, null); return util.format(parsedNumber, PhoneNumberFormat.INTERNATIONAL); } catch (NumberParseException e) { Log.w(TAG, e); return e164number; } }
Example 12
Source File: PhoneNumberFormatter.java From Silence with GNU General Public License v3.0 | 5 votes |
public static String formatNumberInternational(String number) { try { PhoneNumberUtil util = PhoneNumberUtil.getInstance(); PhoneNumber parsedNumber = util.parse(number, null); return util.format(parsedNumber, PhoneNumberFormat.INTERNATIONAL); } catch (NumberParseException e) { Log.w(TAG, e); return number; } }
Example 13
Source File: PhoneNumberFormatter.java From libsignal-service-java with GNU General Public License v3.0 | 5 votes |
public static String getInternationalFormatFromE164(String e164number) { try { PhoneNumberUtil util = PhoneNumberUtil.getInstance(); PhoneNumber parsedNumber = util.parse(e164number, null); return util.format(parsedNumber, PhoneNumberFormat.INTERNATIONAL); } catch (NumberParseException e) { Log.w(TAG, e); return e164number; } }
Example 14
Source File: PhoneNumberFormatter.java From libsignal-service-java with GNU General Public License v3.0 | 5 votes |
public static String formatNumber(String number, String localNumber) throws InvalidNumberException { if (number == null) { throw new InvalidNumberException("Null String passed as number."); } if (number.contains("@")) { throw new InvalidNumberException("Possible attempt to use email address."); } number = number.replaceAll("[^0-9+]", ""); if (number.length() == 0) { throw new InvalidNumberException("No valid characters found."); } // if (number.charAt(0) == '+') // return number; try { PhoneNumberUtil util = PhoneNumberUtil.getInstance(); PhoneNumber localNumberObject = util.parse(localNumber, null); String localCountryCode = util.getRegionCodeForNumber(localNumberObject); Log.w(TAG, "Got local CC: " + localCountryCode); PhoneNumber numberObject = util.parse(number, localCountryCode); return util.format(numberObject, PhoneNumberFormat.E164); } catch (NumberParseException e) { Log.w(TAG, e); return impreciseFormatNumber(number, localNumber); } }
Example 15
Source File: PhoneNumberFormatter.java From libsignal-service-java with GNU General Public License v3.0 | 5 votes |
public static String formatNumberInternational(String number) { try { PhoneNumberUtil util = PhoneNumberUtil.getInstance(); PhoneNumber parsedNumber = util.parse(number, null); return util.format(parsedNumber, PhoneNumberFormat.INTERNATIONAL); } catch (NumberParseException e) { Log.w(TAG, e); return number; } }
Example 16
Source File: MessagesDatabase.java From weMessage with GNU Affero General Public License v3.0 | 5 votes |
public PeerChat getChatByAccount(String handle) throws SQLException { ChatBase chatBase; try { PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); Phonenumber.PhoneNumber phoneNumberOne = phoneNumberUtil.parse(handle, Locale.getDefault().getCountry()); String phoneNumberString = phoneNumberUtil.format(phoneNumberOne, PhoneNumberUtil.PhoneNumberFormat.E164); ChatBase attemptOne = getChatByIdentifier(phoneNumberString); if (attemptOne == null){ Phonenumber.PhoneNumber phoneNumberTwo = phoneNumberOne.clearCountryCode(); String phoneNumberStringTwo = phoneNumberUtil.format(phoneNumberTwo, PhoneNumberUtil.PhoneNumberFormat.E164).substring(2); ChatBase attemptTwo = getChatByIdentifier(phoneNumberStringTwo); if (attemptTwo == null){ return null; }else { chatBase = attemptTwo; } }else { chatBase = attemptOne; } }catch(Exception ex){ chatBase = getChatByIdentifier(handle); } if (chatBase == null) return null; if (chatBase instanceof GroupChat) return null; return (PeerChat) chatBase; }
Example 17
Source File: NumberViewState.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
private static String formatNumber(@NonNull PhoneNumberUtil util, @NonNull String e164Number) { try { Phonenumber.PhoneNumber number = getPhoneNumber(util, e164Number); return util.format(number, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL); } catch (NumberParseException e) { return e164Number; } }
Example 18
Source File: ContactUtil.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
private static @NonNull String getPrettyPhoneNumber(@NonNull String phoneNumber, @NonNull Locale fallbackLocale) { PhoneNumberUtil util = PhoneNumberUtil.getInstance(); try { PhoneNumber parsed = util.parse(phoneNumber, fallbackLocale.getCountry()); return util.format(parsed, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL); } catch (NumberParseException e) { return phoneNumber; } }
Example 19
Source File: PhoneNumberFormatter.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
public static String getInternationalFormatFromE164(String e164number) { try { PhoneNumberUtil util = PhoneNumberUtil.getInstance(); PhoneNumber parsedNumber = util.parse(e164number, null); return util.format(parsedNumber, PhoneNumberFormat.INTERNATIONAL); } catch (NumberParseException e) { Log.w(TAG, e); return e164number; } }
Example 20
Source File: KontalkIqRegister.java From tigase-extension with GNU General Public License v3.0 | 4 votes |
private String formatPhoneNumber(String phoneInput) throws NumberParseException { PhoneNumberUtil util = PhoneNumberUtil.getInstance(); Phonenumber.PhoneNumber phone = util.parse(phoneInput, null); return util.format(phone, PhoneNumberUtil.PhoneNumberFormat.E164); }