@Test public void testUsNumbers() { PhoneNumberFormatter formatter = new PhoneNumberFormatter("+16105880522"); assertEquals("+551234567890", formatter.format("+551234567890")); assertEquals("+11234567890", formatter.format("(123) 456-7890")); assertEquals("+11234567890", formatter.format("1234567890")); assertEquals("+16104567890", formatter.format("456-7890")); assertEquals("+16104567890", formatter.format("4567890")); assertEquals("+11234567890", formatter.format("011 1 123 456 7890")); assertEquals("+5511912345678", formatter.format("0115511912345678")); assertEquals("+16105880522", formatter.format("+16105880522")); }
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);  }