Java Code Examples for com.google.i18n.phonenumbers.PhoneNumberUtil#getInstance()
The following examples show how to use
com.google.i18n.phonenumbers.PhoneNumberUtil#getInstance() .
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 Silence 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: SignIn.java From XERUNG with Apache License 2.0 | 6 votes |
private void sendOTPNumber(){ if(checkNull()){ PhoneNumberUtil util = PhoneNumberUtil.getInstance(); // assuming you only a button in your layout... boolean isAuthentic = false; try { PhoneNumber number = util.parse(countryPrefix + edtMobile.getText().toString().trim(), countryIso); isAuthentic = true; } catch (NumberParseException e) { e.printStackTrace(); } if (isAuthentic) { comman.hideSoftKeyBoard(context, edtMobile); createJson(edtMobile.getText().toString().trim(), countryPrefix, countryName); } } }
Example 3
Source File: PhoneValidator.java From sunbird-lms-service with MIT License | 6 votes |
public static boolean validatePhone(String phone, String countryCode) { PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); String code = countryCode; if (StringUtils.isNotBlank(countryCode) && (countryCode.charAt(0) != '+')) { code = "+" + countryCode; } Phonenumber.PhoneNumber phoneNumber = null; try { if (StringUtils.isBlank(countryCode)) { code = PropertiesCache.getInstance().getProperty("sunbird_default_country_code"); } String isoCode = phoneNumberUtil.getRegionCodeForCountryCode(Integer.parseInt(code)); phoneNumber = phoneNumberUtil.parse(phone, isoCode); return phoneNumberUtil.isValidNumber(phoneNumber); } catch (NumberParseException e) { ProjectLogger.log( "PhoneValidator:validatePhone: Exception occurred while validating phone number = ", e); } return false; }
Example 4
Source File: NumberViewState.java From mollyim-android with GNU General Public License v3.0 | 6 votes |
public String getCountryDisplayName() { if (selectedCountryName != null) { return selectedCountryName; } PhoneNumberUtil util = PhoneNumberUtil.getInstance(); if (isValid()) { String actualCountry = getActualCountry(util, getE164Number()); if (actualCountry != null) { return actualCountry; } } String regionCode = util.getRegionCodeForCountryCode(countryCode); return PhoneNumberFormatter.getRegionDisplayName(regionCode); }
Example 5
Source File: XMPPUtils.java From desktopclient-java with GNU General Public License v3.0 | 6 votes |
public static String phoneNumberToKontalkLocal(String number) { PhoneNumberUtil pnUtil = PhoneNumberUtil.getInstance(); PhoneNumber n; try { n = pnUtil.parse(number, null); } catch (NumberParseException ex) { return ""; } if (!pnUtil.isValidNumber(n)) return ""; return DigestUtils.sha1Hex( PhoneNumberUtil.getInstance().format(n, PhoneNumberUtil.PhoneNumberFormat.E164)); }
Example 6
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 7
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 8
Source File: AddressUtil.java From scipio-erp with Apache License 2.0 | 6 votes |
/** * Validates phone numbers (using external library). * <p> * SCIPIO: 2018-08-30: Stock method moved here from {@link org.ofbiz.base.util.UtilValidate} * due to dependency issues. */ public static boolean isValidPhoneNumber(String phoneNumber, String geoId, Delegator delegator) { boolean isValid = false; try { GenericValue geo = EntityQuery.use(delegator).from("Geo").where("geoId", geoId).cache().queryOne(); PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); String geoCode = geo != null ? geo.getString("geoCode") : "US"; PhoneNumber phNumber = phoneUtil.parse(phoneNumber, geoCode); if (phoneUtil.isValidNumber(phNumber) || phoneUtil.isPossibleNumber(phNumber)) { isValid = true; } } catch (GenericEntityException | NumberParseException ex) { Debug.logError(ex, module); } return isValid; }
Example 9
Source File: PhoneNumberParser.java From endpoints-codelab-android with GNU General Public License v3.0 | 6 votes |
public List<String> parse(String inputText) { if (inputText == null) { return Collections.emptyList(); } // Only run the phone number parser if Android version is not Honeycomb // API level 11 - 13 int sdk = Build.VERSION.SDK_INT; if (sdk >= 11 && sdk <= 13) { return Collections.emptyList(); } PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); Iterable<PhoneNumberMatch> numbersMatch = phoneUtil.findNumbers( inputText, Locale.getDefault().getCountry()); ArrayList<String> numbers = new ArrayList<String>(); for (PhoneNumberMatch number : numbersMatch) { numbers.add(phoneUtil.format(number.number(), PhoneNumberFormat.NATIONAL)); } return numbers; }
Example 10
Source File: PhoneNumberTest.java From card-reader with MIT License | 5 votes |
@Test public void phoneNumberParsingTest(){ String text = "Hank Schrader Program Manager Org Activities Multi Studios 555 Horton Street, " + "P.0. Box 143 London, ON N6A 4H6 519 661 9000 Ext. 5014 Cell: 519 456 5463 Powering London. " + "[email protected] Empowering You. Fax: 519 611 5841 :"; String text2 = "onlinestudiomarketing Darth Vader R Web Developer/ Designer/ Wordpress " + "Consultant 519-333-541 [email protected] London, ON Canada www.onlinestudiomarketing.ca"; PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); Iterable<PhoneNumberMatch> numberMatches = phoneNumberUtil.findNumbers(text2, Locale.US.getCountry()); for(PhoneNumberMatch number : numberMatches){ String s = number.rawString(); System.out.println(s); } }
Example 11
Source File: LoginForPhoneActivity.java From Android with MIT License | 5 votes |
@Override public void afterTextChanged(Editable s) { try { PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); Phonenumber.PhoneNumber swissNumberProto = phoneUtil.parse(s.toString(), countryBean.getCountryCode()); if(phoneUtil.isValidNumberForRegion(swissNumberProto, countryBean.getCountryCode())){ setBtnEnabled(true); }else{ setBtnEnabled(false); } } catch (Exception e) { e.printStackTrace(); setBtnEnabled(false); } }
Example 12
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 13
Source File: MainActivity.java From card-reader with MIT License | 5 votes |
/** * Parses phoneNumbers from a string using Google's libphonenumber library * * @param bCardText, The text obtained from the vision API processing * @return ArrayList of parsed phone numbers from the vision API processed text string */ private ArrayList<String> parseResults(String bCardText) { PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); Iterable<PhoneNumberMatch> numberMatches = phoneNumberUtil.findNumbers(bCardText, Locale.US.getCountry()); ArrayList<String> data = new ArrayList<>(); for(PhoneNumberMatch number : numberMatches){ String s = number.rawString(); data.add(s); } return data; }
Example 14
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 15
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 16
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 17
Source File: PhoneNumberFormatter.java From mollyim-android 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 18
Source File: SmppAddressing.java From smsrouter with GNU General Public License v3.0 | 4 votes |
public SmppAddressing(String smscCountry, String originHeaderName, String destinationHeaderName) { this.smscCountry = smscCountry; this.originHeaderName = originHeaderName; this.destinationHeaderName = destinationHeaderName; pnu = PhoneNumberUtil.getInstance(); }
Example 19
Source File: SettingsFragment.java From haven with GNU General Public License v3.0 | 4 votes |
String getCountryCode() { PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); return "+" + String.valueOf(phoneUtil.getCountryCodeForRegion(Locale.getDefault().getCountry())); }
Example 20
Source File: EnterPhoneNumberFragment.java From mollyim-android with GNU General Public License v3.0 | 3 votes |
private void setCountryFormatter(@Nullable String regionCode) { PhoneNumberUtil util = PhoneNumberUtil.getInstance(); countryFormatter = regionCode != null ? util.getAsYouTypeFormatter(regionCode) : null; reformatText(number.getText()); }