@Test public void testAddressString() throws Exception { PhoneNumberFormatter formatter = new PhoneNumberFormatter("+14152222222"); assertEquals(formatter.format("bonbon"), "bonbon"); }
public String format(@Nullable String number) { if (number == null) return "Unknown"; if (GroupId.isEncodedGroup(number)) return number; if (ALPHA_PATTERN.matcher(number).find()) return number.trim(); String bareNumber = number.replaceAll("[^0-9+]", ""); if (bareNumber.length() == 0) { if (number.trim().length() == 0) return "Unknown"; else return number.trim(); } if (bareNumber.length() <= 6 && ("DE".equals(localCountryCode) || "FI".equals(localCountryCode) || "SK".equals(localCountryCode))) { return bareNumber; } if (bareNumber.length() <= 4 && !SHORT_COUNTRIES.contains(localCountryCode)) { return bareNumber; } if (isShortCode(bareNumber, localCountryCode)) { return bareNumber; } String processedNumber = applyAreaCodeRules(localNumber, bareNumber); try { Phonenumber.PhoneNumber parsedNumber = phoneNumberUtil.parse(processedNumber, localCountryCode); return phoneNumberUtil.format(parsedNumber, PhoneNumberUtil.PhoneNumberFormat.E164); } catch (NumberParseException e) { Log.w(TAG, e); if (bareNumber.charAt(0) == '+') return bareNumber; String localNumberImprecise = localNumber.isPresent() ? localNumber.get().getE164Number() : ""; if (localNumberImprecise.charAt(0) == '+') localNumberImprecise = localNumberImprecise.substring(1); if (localNumberImprecise.length() == bareNumber.length() || bareNumber.length() > localNumberImprecise.length()) return "+" + number; int difference = localNumberImprecise.length() - bareNumber.length(); return "+" + localNumberImprecise.substring(0, difference) + bareNumber; } }
PhoneNumberFormatter { public String format(@Nullable String number) { if (number == null) return "Unknown"; if (GroupId.isEncodedGroup(number)) return number; if (ALPHA_PATTERN.matcher(number).find()) return number.trim(); String bareNumber = number.replaceAll("[^0-9+]", ""); if (bareNumber.length() == 0) { if (number.trim().length() == 0) return "Unknown"; else return number.trim(); } if (bareNumber.length() <= 6 && ("DE".equals(localCountryCode) || "FI".equals(localCountryCode) || "SK".equals(localCountryCode))) { return bareNumber; } if (bareNumber.length() <= 4 && !SHORT_COUNTRIES.contains(localCountryCode)) { return bareNumber; } if (isShortCode(bareNumber, localCountryCode)) { return bareNumber; } String processedNumber = applyAreaCodeRules(localNumber, bareNumber); try { Phonenumber.PhoneNumber parsedNumber = phoneNumberUtil.parse(processedNumber, localCountryCode); return phoneNumberUtil.format(parsedNumber, PhoneNumberUtil.PhoneNumberFormat.E164); } catch (NumberParseException e) { Log.w(TAG, e); if (bareNumber.charAt(0) == '+') return bareNumber; String localNumberImprecise = localNumber.isPresent() ? localNumber.get().getE164Number() : ""; if (localNumberImprecise.charAt(0) == '+') localNumberImprecise = localNumberImprecise.substring(1); if (localNumberImprecise.length() == bareNumber.length() || bareNumber.length() > localNumberImprecise.length()) return "+" + number; int difference = localNumberImprecise.length() - bareNumber.length(); return "+" + localNumberImprecise.substring(0, difference) + bareNumber; } } }
PhoneNumberFormatter { public String format(@Nullable String number) { if (number == null) return "Unknown"; if (GroupId.isEncodedGroup(number)) return number; if (ALPHA_PATTERN.matcher(number).find()) return number.trim(); String bareNumber = number.replaceAll("[^0-9+]", ""); if (bareNumber.length() == 0) { if (number.trim().length() == 0) return "Unknown"; else return number.trim(); } if (bareNumber.length() <= 6 && ("DE".equals(localCountryCode) || "FI".equals(localCountryCode) || "SK".equals(localCountryCode))) { return bareNumber; } if (bareNumber.length() <= 4 && !SHORT_COUNTRIES.contains(localCountryCode)) { return bareNumber; } if (isShortCode(bareNumber, localCountryCode)) { return bareNumber; } String processedNumber = applyAreaCodeRules(localNumber, bareNumber); try { Phonenumber.PhoneNumber parsedNumber = phoneNumberUtil.parse(processedNumber, localCountryCode); return phoneNumberUtil.format(parsedNumber, PhoneNumberUtil.PhoneNumberFormat.E164); } catch (NumberParseException e) { Log.w(TAG, e); if (bareNumber.charAt(0) == '+') return bareNumber; String localNumberImprecise = localNumber.isPresent() ? localNumber.get().getE164Number() : ""; if (localNumberImprecise.charAt(0) == '+') localNumberImprecise = localNumberImprecise.substring(1); if (localNumberImprecise.length() == bareNumber.length() || bareNumber.length() > localNumberImprecise.length()) return "+" + number; int difference = localNumberImprecise.length() - bareNumber.length(); return "+" + localNumberImprecise.substring(0, difference) + bareNumber; } } PhoneNumberFormatter(@NonNull String localNumberString);   PhoneNumberFormatter(@NonNull String localCountryCode, boolean countryCode); }
PhoneNumberFormatter { public String format(@Nullable String number) { if (number == null) return "Unknown"; if (GroupId.isEncodedGroup(number)) return number; if (ALPHA_PATTERN.matcher(number).find()) return number.trim(); String bareNumber = number.replaceAll("[^0-9+]", ""); if (bareNumber.length() == 0) { if (number.trim().length() == 0) return "Unknown"; else return number.trim(); } if (bareNumber.length() <= 6 && ("DE".equals(localCountryCode) || "FI".equals(localCountryCode) || "SK".equals(localCountryCode))) { return bareNumber; } if (bareNumber.length() <= 4 && !SHORT_COUNTRIES.contains(localCountryCode)) { return bareNumber; } if (isShortCode(bareNumber, localCountryCode)) { return bareNumber; } String processedNumber = applyAreaCodeRules(localNumber, bareNumber); try { Phonenumber.PhoneNumber parsedNumber = phoneNumberUtil.parse(processedNumber, localCountryCode); return phoneNumberUtil.format(parsedNumber, PhoneNumberUtil.PhoneNumberFormat.E164); } catch (NumberParseException e) { Log.w(TAG, e); if (bareNumber.charAt(0) == '+') return bareNumber; String localNumberImprecise = localNumber.isPresent() ? localNumber.get().getE164Number() : ""; if (localNumberImprecise.charAt(0) == '+') localNumberImprecise = localNumberImprecise.substring(1); if (localNumberImprecise.length() == bareNumber.length() || bareNumber.length() > localNumberImprecise.length()) return "+" + number; int difference = localNumberImprecise.length() - bareNumber.length(); return "+" + localNumberImprecise.substring(0, difference) + bareNumber; } } PhoneNumberFormatter(@NonNull String localNumberString);   PhoneNumberFormatter(@NonNull String localCountryCode, boolean countryCode); static @NonNull PhoneNumberFormatter get(Context context); String format(@Nullable String number); }
PhoneNumberFormatter { public String format(@Nullable String number) { if (number == null) return "Unknown"; if (GroupId.isEncodedGroup(number)) return number; if (ALPHA_PATTERN.matcher(number).find()) return number.trim(); String bareNumber = number.replaceAll("[^0-9+]", ""); if (bareNumber.length() == 0) { if (number.trim().length() == 0) return "Unknown"; else return number.trim(); } if (bareNumber.length() <= 6 && ("DE".equals(localCountryCode) || "FI".equals(localCountryCode) || "SK".equals(localCountryCode))) { return bareNumber; } if (bareNumber.length() <= 4 && !SHORT_COUNTRIES.contains(localCountryCode)) { return bareNumber; } if (isShortCode(bareNumber, localCountryCode)) { return bareNumber; } String processedNumber = applyAreaCodeRules(localNumber, bareNumber); try { Phonenumber.PhoneNumber parsedNumber = phoneNumberUtil.parse(processedNumber, localCountryCode); return phoneNumberUtil.format(parsedNumber, PhoneNumberUtil.PhoneNumberFormat.E164); } catch (NumberParseException e) { Log.w(TAG, e); if (bareNumber.charAt(0) == '+') return bareNumber; String localNumberImprecise = localNumber.isPresent() ? localNumber.get().getE164Number() : ""; if (localNumberImprecise.charAt(0) == '+') localNumberImprecise = localNumberImprecise.substring(1); if (localNumberImprecise.length() == bareNumber.length() || bareNumber.length() > localNumberImprecise.length()) return "+" + number; int difference = localNumberImprecise.length() - bareNumber.length(); return "+" + localNumberImprecise.substring(0, difference) + bareNumber; } } PhoneNumberFormatter(@NonNull String localNumberString);   PhoneNumberFormatter(@NonNull String localCountryCode, boolean countryCode); static @NonNull PhoneNumberFormatter get(Context context); String format(@Nullable String number);  }