com.google.i18n.phonenumbers.Phonenumber Java Examples
The following examples show how to use
com.google.i18n.phonenumbers.Phonenumber.
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: 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 #2
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 #3
Source File: ProjectUtil.java From sunbird-lms-service with MIT License | 6 votes |
public static boolean validatePhone(String phNumber, String countryCode) { PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); String contryCode = countryCode; if (!StringUtils.isBlank(countryCode) && (countryCode.charAt(0) != '+')) { contryCode = "+" + countryCode; } Phonenumber.PhoneNumber phoneNumber = null; try { if (StringUtils.isBlank(countryCode)) { contryCode = PropertiesCache.getInstance().getProperty("sunbird_default_country_code"); } String isoCode = phoneNumberUtil.getRegionCodeForCountryCode(Integer.parseInt(contryCode)); phoneNumber = phoneNumberUtil.parse(phNumber, isoCode); return phoneNumberUtil.isValidNumber(phoneNumber); } catch (NumberParseException e) { ProjectLogger.log("Exception occurred while validating phone number : ", e); ProjectLogger.log(phNumber + "this phone no. is not a valid one."); } return false; }
Example #4
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 #5
Source File: ContactFragment.java From customview-samples with Apache License 2.0 | 6 votes |
private void displayContactData(){ List<PhoneInfo> phoneInfoList = MobileContactSingleton.getInstance().getMobileContact(); PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); for(PhoneInfo phoneInfo: phoneInfoList) { try { String phoneNum = phoneInfo.getPhoneNum(); Phonenumber.PhoneNumber phoneNumProto = phoneUtil.parse(phoneNum, "CN"); boolean isValid = phoneUtil.isValidNumber(phoneNumProto); Log.d("hyh", "ContactFragment: displayContactData: isValid="+isValid+" ,phoneNum="+phoneNum); if (isValid) { String formatPhoneNum = phoneUtil.format(phoneNumProto, PhoneNumberUtil.PhoneNumberFormat.E164); Log.d("hyh", "ContactMgr: run: formatPhoneNum=" + formatPhoneNum); } } catch (NumberParseException e) { Log.d("hyh", "ContactFragment: displayContactData: phone="+phoneInfo.getPhoneNum()); e.printStackTrace(); } } ContactListAdapter contactListAdapter = new ContactListAdapter(getContext(),phoneInfoList); mRvContactList.setLayoutManager(new LinearLayoutManager(getContext())); mRvContactList.setAdapter(contactListAdapter); }
Example #6
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 #7
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 #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: 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 #10
Source File: MobileValidation.java From keycloak-extension-playground with Apache License 2.0 | 6 votes |
static boolean isPhoneNumberValid(String phoneNumber) { String formattedPhoneNumber = convertInternationalPrefix(phoneNumber); String region; if (isPossibleNationalNumber(formattedPhoneNumber)) { region = "DE"; } else if (isInternationalNumber(formattedPhoneNumber)) { region = null; } else { return true; // If the number cannot be interpreted as an international or possible DE phone number, do not attempt to validate it. } try { Phonenumber.PhoneNumber parsedPhoneNumber = PhoneNumberUtil.getInstance().parse(formattedPhoneNumber, region); return PhoneNumberUtil.getInstance().isValidNumber(parsedPhoneNumber); } catch (NumberParseException e) { return false; } }
Example #11
Source File: Util.java From mollyim-android with GNU General Public License v3.0 | 6 votes |
@RequiresPermission(anyOf = { android.Manifest.permission.READ_PHONE_STATE, android.Manifest.permission.READ_SMS, android.Manifest.permission.READ_PHONE_NUMBERS }) @SuppressLint("MissingPermission") public static Optional<Phonenumber.PhoneNumber> getDeviceNumber(Context context) { try { final String localNumber = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number(); final Optional<String> countryIso = getSimCountryIso(context); if (TextUtils.isEmpty(localNumber)) return Optional.absent(); if (!countryIso.isPresent()) return Optional.absent(); return Optional.fromNullable(PhoneNumberUtil.getInstance().parse(localNumber, countryIso.get())); } catch (NumberParseException e) { Log.w(TAG, e); return Optional.absent(); } }
Example #12
Source File: ShortCodeUtil.java From mollyim-android 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 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 #13
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 #14
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 #15
Source File: BaseFlagFragment.java From android-phone-number-with-flags with Apache License 2.0 | 6 votes |
protected String validate() { String region = null; String phone = null; if (mLastEnteredPhone != null) { try { Phonenumber.PhoneNumber p = mPhoneNumberUtil.parse(mLastEnteredPhone, null); StringBuilder sb = new StringBuilder(16); sb.append('+').append(p.getCountryCode()).append(p.getNationalNumber()); phone = sb.toString(); region = mPhoneNumberUtil.getRegionCodeForNumber(p); } catch (NumberParseException ignore) { } } if (region != null) { return phone; } else { return null; } }
Example #16
Source File: WelcomeFragment.java From mollyim-android with GNU General Public License v3.0 | 6 votes |
@SuppressLint("MissingPermission") private void initializeNumber() { Optional<Phonenumber.PhoneNumber> localNumber = Optional.absent(); if (Permissions.hasAll(requireContext(), Manifest.permission.READ_PHONE_STATE)) { localNumber = Util.getDeviceNumber(requireContext()); } if (localNumber.isPresent()) { getModel().onNumberDetected(localNumber.get().getCountryCode(), localNumber.get().getNationalNumber()); } else { Optional<String> simCountryIso = Util.getSimCountryIso(requireContext()); if (simCountryIso.isPresent() && !TextUtils.isEmpty(simCountryIso.get())) { getModel().onNumberDetected(PhoneNumberUtil.getInstance().getCountryCodeForRegion(simCountryIso.get()), 0); } } }
Example #17
Source File: E164PhoneNumberWithExtension.java From jadira with Apache License 2.0 | 6 votes |
/** * Creates a instance from the given PhoneNumber * @param prototype The PhoneNumber to construct the instance from */ public E164PhoneNumberWithExtension(Phonenumber.PhoneNumber prototype) { StringBuilder e164Builder = new StringBuilder(); PHONE_NUMBER_UTIL.format(prototype, PhoneNumberFormat.E164, e164Builder); try { this.number = PHONE_NUMBER_UTIL.parse(e164Builder.toString(), "GB"); } catch (NumberParseException e) { throw new PhoneNumberParseException( EX_PARSE_MSG_PREFIX + prototype.getNationalNumber() +"}", e); } if (prototype.hasExtension()) { this.number.setExtension(prototype.getExtension()); } }
Example #18
Source File: E164PhoneNumberWithExtension.java From jadira with Apache License 2.0 | 6 votes |
/** * Returns the underlying LibPhoneNumber {@link Phonenumber.PhoneNumber} instance. * To preserve the immutability of E164PhoneNumber, a copy is made. * @return The underlying PhoneNumber instance */ public Phonenumber.PhoneNumber getUnderlyingPhoneNumber() { Phonenumber.PhoneNumber copy; StringBuilder e164Builder = new StringBuilder(); PHONE_NUMBER_UTIL.format(number, PhoneNumberFormat.E164, e164Builder); try { copy = PHONE_NUMBER_UTIL.parse(e164Builder.toString(), "GB"); } catch (NumberParseException e) { throw new PhoneNumberParseException( EX_PARSE_MSG_PREFIX + number.getNationalNumber() +"}", e); } if (number.hasExtension()) { copy.setExtension(number.getExtension()); } return copy; }
Example #19
Source File: LinkChangePhoneActivity.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())){ nextBtn.setEnabled(true); }else{ nextBtn.setEnabled(false); } } catch (NumberParseException e) { e.printStackTrace(); nextBtn.setEnabled(false); } }
Example #20
Source File: PhoneField.java From android-phone-field with Apache License 2.0 | 5 votes |
/** * Sets phone number. * * @param rawNumber the raw number */ public void setPhoneNumber(String rawNumber) { try { Phonenumber.PhoneNumber number = parsePhoneNumber(rawNumber); if (mCountry == null || mCountry.getDialCode() != number.getCountryCode()) { selectCountry(number.getCountryCode()); } mEditText.setText(mPhoneUtil.format(number, PhoneNumberUtil.PhoneNumberFormat.NATIONAL)); } catch (NumberParseException ignored) { } }
Example #21
Source File: PhoneField.java From android-phone-field with Apache License 2.0 | 5 votes |
/** * Gets phone number. * * @return the phone number */ public String getPhoneNumber() { try { Phonenumber.PhoneNumber number = parsePhoneNumber(getRawInput()); return mPhoneUtil.format(number, PhoneNumberUtil.PhoneNumberFormat.E164); } catch (NumberParseException ignored) { } return getRawInput(); }
Example #22
Source File: SessionPhoneTokenRequestJson.java From mxisd with GNU Affero General Public License v3.0 | 5 votes |
public String getValue() { try { Phonenumber.PhoneNumber num = phoneUtil.parse(phone_number, country); return phoneUtil.format(num, PhoneNumberUtil.PhoneNumberFormat.E164).replace("+", ""); } catch (NumberParseException e) { throw new IllegalArgumentException("Invalid phone number"); } }
Example #23
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 #24
Source File: PhoneNumberFormatter.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
PhoneNumberFormatter(@NonNull String localNumberString) { try { Phonenumber.PhoneNumber libNumber = phoneNumberUtil.parse(localNumberString, null); int countryCode = libNumber.getCountryCode(); this.localNumber = Optional.of(new PhoneNumber(localNumberString, countryCode, parseAreaCode(localNumberString, countryCode))); this.localCountryCode = phoneNumberUtil.getRegionCodeForNumber(libNumber); } catch (NumberParseException e) { throw new AssertionError(e); } }
Example #25
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 #26
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 #27
Source File: NumberViewState.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
/** * Finds actual name of region from a valid number. So for example +1 might map to US or Canada or other territories. */ private static @Nullable String getActualCountry(@NonNull PhoneNumberUtil util, @NonNull String e164Number) { try { Phonenumber.PhoneNumber phoneNumber = getPhoneNumber(util, e164Number); String regionCode = util.getRegionCodeForNumber(phoneNumber); if (regionCode != null) { return PhoneNumberFormatter.getRegionDisplayName(regionCode); } } catch (NumberParseException e) { return null; } return null; }
Example #28
Source File: PhoneNumberFormatter.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
private boolean isShortCode(@NonNull String bareNumber, String localCountryCode) { try { Phonenumber.PhoneNumber parsedNumber = phoneNumberUtil.parse(bareNumber, localCountryCode); return ShortNumberInfo.getInstance().isPossibleShortNumberForRegion(parsedNumber, localCountryCode); } catch (NumberParseException e) { return false; } }
Example #29
Source File: PhoneField.java From android-phone-field with Apache License 2.0 | 4 votes |
private Phonenumber.PhoneNumber parsePhoneNumber(String number) throws NumberParseException { String defaultRegion = mCountry != null ? mCountry.getCode().toUpperCase() : ""; return mPhoneUtil.parseAndKeepRawInput(number, defaultRegion); }
Example #30
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); }