Java Code Examples for com.google.i18n.phonenumbers.PhoneNumberUtil#parse()
The following examples show how to use
com.google.i18n.phonenumbers.PhoneNumberUtil#parse() .
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: 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 2
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 3
Source File: AutoInitiate.java From Silence with GNU General Public License v3.0 | 6 votes |
public static boolean isTaggableDestination(Recipients recipients){ // Be safe - err on the side of not tagging if (recipients.isGroupRecipient()) return false; PhoneNumberUtil util = PhoneNumberUtil.getInstance(); try { PhoneNumber num = util.parse(recipients.getPrimaryRecipient().getNumber(), Locale.getDefault().getCountry()); PhoneNumberType type = util.getNumberType(num); Log.d(TAG, "Number type: " + type.toString()); return type == PhoneNumberType.FIXED_LINE || type == PhoneNumberType.MOBILE || type == PhoneNumberType.FIXED_LINE_OR_MOBILE; } catch (NumberParseException e){ Log.w(TAG, "Couldn't get number type (country: " + Locale.getDefault().getCountry() + ")"); return false; } }
Example 4
Source File: PhoneUtils.java From android-phone-number-with-flags with Apache License 2.0 | 6 votes |
private static String parseNumber(String paramString) { if (paramString == null) { return null; } PhoneNumberUtil numberUtil = PhoneNumberUtil.getInstance(); String result; try { Phonenumber.PhoneNumber localPhoneNumber = numberUtil.parse(paramString, null); result = numberUtil.getRegionCodeForNumber(localPhoneNumber); if (result == null) { return null; } } catch (NumberParseException localNumberParseException) { return null; } return result; }
Example 5
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 6
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 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: 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 9
Source File: MainActivity.java From Android with MIT License | 6 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); Phonenumber.PhoneNumber indianPhoneNumber = null; try { indianPhoneNumber = phoneNumberUtil.parse(phoneNumber, "IN"); boolean val = phoneNumberUtil.isValidNumber(indianPhoneNumber); Log.d("Parzival", "Data: "+indianPhoneNumber.getCountryCode()+" >> "+indianPhoneNumber.getNationalNumber()); Log.d("Parzival", "Val: "+val); } catch (NumberParseException e) { e.printStackTrace(); } }
Example 10
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 11
Source File: ShortCodeUtil.java From Silence with GNU General Public License v3.0 | 6 votes |
public static boolean isShortCode(@NonNull String localNumber, @NonNull String number) { try { PhoneNumberUtil util = PhoneNumberUtil.getInstance(); Phonenumber.PhoneNumber localNumberObject = util.parse(localNumber, null); String localCountryCode = util.getRegionCodeForNumber(localNumberObject); String bareNumber = number.replaceAll("[^0-9+]", ""); // libphonenumber doesn't seem to be correct for Germany and Finland if (bareNumber.length() <= 6 && ("DE".equals(localCountryCode) || "FI".equals(localCountryCode) || "SK".equals(localCountryCode))) { return true; } // libphonenumber seems incorrect for Russia and a few other countries with 4 digit short codes. if (bareNumber.length() <= 4 && !SHORT_COUNTRIES.contains(localCountryCode)) { return true; } Phonenumber.PhoneNumber shortCode = util.parse(number, localCountryCode); return ShortNumberInfo.getInstance().isPossibleShortNumberForRegion(shortCode, localCountryCode); } catch (NumberParseException e) { Log.w(TAG, e); return false; } }
Example 12
Source File: AddressUtil.java From scipio-erp with Apache License 2.0 | 5 votes |
/** * Splits phone number (using external library). * <p> * SCIPIO: 2018-08-30: Stock method moved here from {@link org.ofbiz.base.util.UtilMisc} * due to dependency issues (and really did not belong there). */ public static Map<String, String> splitPhoneNumber(String phoneNumber, Delegator delegator) { Map<String, String> result = new HashMap<>(); try { PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); String defaultCountry = EntityUtilProperties.getPropertyValue("general", "country.geo.id.default", delegator); GenericValue defaultGeo = EntityQuery.use(delegator).from("Geo").where("geoId", defaultCountry).cache().queryOne(); String defaultGeoCode = defaultGeo != null ? defaultGeo.getString("geoCode") : "US"; PhoneNumber phNumber = phoneUtil.parse(phoneNumber, defaultGeoCode); if (phoneUtil.isValidNumber(phNumber) || phoneUtil.isPossibleNumber(phNumber)) { String nationalSignificantNumber = phoneUtil.getNationalSignificantNumber(phNumber); int areaCodeLength = phoneUtil.getLengthOfGeographicalAreaCode(phNumber); result.put("countryCode", Integer.toString(phNumber.getCountryCode())); if (areaCodeLength > 0) { result.put("areaCode", nationalSignificantNumber.substring(0, areaCodeLength)); result.put("contactNumber", nationalSignificantNumber.substring(areaCodeLength)); } else { result.put("areaCode", ""); result.put("contactNumber", nationalSignificantNumber); } } else { Debug.logError("Invalid phone number " + phoneNumber, module); result.put(ModelService.ERROR_MESSAGE, "Invalid phone number"); } } catch (GenericEntityException | NumberParseException ex) { Debug.logError(ex, module); result.put(ModelService.ERROR_MESSAGE, ex.getMessage()); } return result; }
Example 13
Source File: PhoneNumberFormatter.java From Silence with GNU General Public License v3.0 | 5 votes |
public static String formatNumber(String number, String localNumber) throws InvalidNumberException { 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 14
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 15
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 16
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 17
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 18
Source File: ParsePhoneNumber.java From levelup-java-examples with Apache License 2.0 | 5 votes |
@Test public void parse_phone_number_google_libphonenumber () throws NumberParseException { String formattedPhoneNumber = "(123) 456-7890 x 1234"; PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); PhoneNumber phonenumber = phoneUtil.parse(formattedPhoneNumber, "US"); assertEquals(1, phonenumber.getCountryCode()); assertEquals("1234", phonenumber.getExtension()); assertEquals(1234567890, phonenumber.getNationalNumber()); }
Example 19
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); }
Example 20
Source File: NumberViewState.java From mollyim-android with GNU General Public License v3.0 | 4 votes |
private static Phonenumber.PhoneNumber getPhoneNumber(@NonNull PhoneNumberUtil util, @NonNull String e164Number) throws NumberParseException { return util.parse(e164Number, null); }