com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat Java Examples

The following examples show how to use com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat. 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 mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
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: E164PhoneNumberWithExtension.java    From jadira with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #3
Source File: E164PhoneNumberWithExtension.java    From jadira with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #4
Source File: PhoneNumberFormatter.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
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 #5
Source File: PhoneNumberParser.java    From endpoints-codelab-android with GNU General Public License v3.0 6 votes vote down vote up
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 #6
Source File: PhoneNumberFormatter.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
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: PhoneNumberFormatter.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
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 #8
Source File: FormatPhoneNumber.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void format_phone_number_google_libphonenumber () throws NumberParseException {
	
	PhoneNumber phoneNumber = new PhoneNumber();
	phoneNumber.setCountryCode(1);
	phoneNumber.setExtension("12345");
	phoneNumber.setNationalNumber(1234567890);

	PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
	String formattedPhoneNumber = phoneUtil.format(phoneNumber, PhoneNumberFormat.NATIONAL);

	assertEquals("(123) 456-7890 ext. 12345", formattedPhoneNumber);
}
 
Example #9
Source File: E164PhoneNumberWithExtension.java    From jadira with Apache License 2.0 5 votes vote down vote up
/**
* {@inheritDoc}
*/
  public String toE164NumberString() {
      StringBuilder result = new StringBuilder();
      PHONE_NUMBER_UTIL.format(number, PhoneNumberFormat.E164, result);
      
      return result.toString();
  }
 
Example #10
Source File: ContactPhoneAdapter.java    From Contacts with MIT License 5 votes vote down vote up
@Override
public void bindData(View view, BaseType data, int position)
{
	final ContactPhoneHolder holder = (ContactPhoneHolder) getHolder(view);

	final ContactPhone phone = ((ContactPhone) data);

	PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
	
	try
	{
		PhoneNumber numberProto = phoneUtil.parse(phone.phoneNumber, "");
		holder.primaryText.setText(phoneUtil.format(numberProto, PhoneNumberFormat.NATIONAL));
	}
	catch (NumberParseException e)
	{
		holder.primaryText.setText(phone.phoneNumber);
	}

	holder.secondaryText.setText(Phone.getTypeLabel(holder.secondaryText.getResources(), phone.type, phone.label));
	
	holder.messageButton.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v)
		{
			PhoneUtil.sendSMS(v.getContext(), phone.phoneNumber);
		}
	});
}
 
Example #11
Source File: PhoneNumberFormatter.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
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 #12
Source File: PhoneNumberFormatter.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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 #13
Source File: PhoneNumberFormatter.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
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 #14
Source File: PhoneNumberFormatter.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
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: PhoneNumberFormatter.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
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 #16
Source File: PhoneNumberFormatter.java    From libsignal-service-java with GNU General Public License v3.0 5 votes vote down vote up
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 vote down vote up
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 #18
Source File: PhoneNumberFormatter.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
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 #19
Source File: PhoneUtil.java    From fenixedu-academic with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public String getInternationalFormatNumber(String numberText) {
    final PhoneNumber phoneNumber = parsePhoneNumber(numberText);
    return phoneNumber != null ? PHONE_UTIL.format(phoneNumber, PhoneNumberFormat.E164) : null;
}