com.google.i18n.phonenumbers.Phonenumber.PhoneNumber Java Examples

The following examples show how to use com.google.i18n.phonenumbers.Phonenumber.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: SignIn.java    From XERUNG with Apache License 2.0 6 votes vote down vote up
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 #2
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 #3
Source File: AutoInitiate.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
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: 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: Global.java    From Faceless with GNU General Public License v3.0 6 votes vote down vote up
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 #6
Source File: XMPPUtils.java    From desktopclient-java with GNU General Public License v3.0 6 votes vote down vote up
public static String phoneNumberToKontalkLocal(String number) {
    PhoneNumberUtil pnUtil = PhoneNumberUtil.getInstance();
    PhoneNumber n;
    try {
        n = pnUtil.parse(number, null);
    } catch (NumberParseException ex) {
        return "";
    }

    if (!pnUtil.isValidNumber(n))
        return "";

    return DigestUtils.sha1Hex(
            PhoneNumberUtil.getInstance().format(n,
            PhoneNumberUtil.PhoneNumberFormat.E164));
}
 
Example #7
Source File: AddressUtil.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
/**
 * Validates phone numbers (using external library).
 * <p>
 * SCIPIO: 2018-08-30: Stock method moved here from {@link org.ofbiz.base.util.UtilValidate}
 * due to dependency issues.
 */
public static boolean isValidPhoneNumber(String phoneNumber, String geoId, Delegator delegator) {
    boolean isValid = false;
    try {
        GenericValue geo = EntityQuery.use(delegator).from("Geo").where("geoId", geoId).cache().queryOne();
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        String geoCode = geo != null ? geo.getString("geoCode") : "US";
        PhoneNumber phNumber = phoneUtil.parse(phoneNumber, geoCode);
        if (phoneUtil.isValidNumber(phNumber) || phoneUtil.isPossibleNumber(phNumber)) {
            isValid = true;
        }
    } catch (GenericEntityException | NumberParseException ex) {
        Debug.logError(ex, module);
    }
    return isValid;
}
 
Example #8
Source File: PhoneUtil.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public PhoneNumber parsePhoneNumber(String numberText) {
    if (!StringUtils.isEmpty(numberText)) {

        if (numberText.startsWith("00")) {
            numberText = numberText.replaceFirst("00", "+");
        }
        
        numberText = numberText.replaceAll("[^0-9+]","");

        try {
            final PhoneNumber phoneNumber = PHONE_UTIL.parse(numberText, null);
            if (PHONE_UTIL.isValidNumber(phoneNumber)) {
                return phoneNumber;
            }
        } catch (NumberParseException e) {
            return null;
        }
    }
    return null;
}
 
Example #9
Source File: SignIn.java    From XERUNG with Apache License 2.0 6 votes vote down vote up
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 #10
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 #11
Source File: LookupAsyncTask.java    From callerid-for-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onPreExecute() throws Exception {
	super.onPreExecute();
	address.setVisibility(View.GONE);
	if(layout.findViewById(R.id.map_view)!=null) layout.findViewById(R.id.map_view).setVisibility(View.GONE);
	image.setVisibility(View.VISIBLE);
	text.setVisibility(View.VISIBLE);
	
	final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
	try{
		final PhoneNumber phoneNumberPhoneNumber = phoneNumberUtil.parse(phoneNumber.toString(), countryDetector.getCountry());
		final PhoneNumberOfflineGeocoder phoneNumberOfflineGeocoder = PhoneNumberOfflineGeocoder.getInstance();
		offlineGeocoderResult = phoneNumberOfflineGeocoder.getDescriptionForNumber(phoneNumberPhoneNumber, Locale.getDefault());
	}catch(NumberParseException e){
		//ignore this exception
	}
	if("".equals(offlineGeocoderResult)) offlineGeocoderResult = null;
	if(offlineGeocoderResult == null)
		text.setText(lookupInProgress);
	else
		text.setText(offlineGeocoderResult);
	image.spin();
}
 
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 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 #13
Source File: PhoneUtil.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * This function is never used, delete on next major
 */
@Deprecated
public static int getCountry(String numberText) {
    final PhoneNumber phoneNumber = getPhoneNumber(numberText);
    if (phoneNumber != null) {
        return phoneNumber.getCountryCode();
    }
    return -1;
}
 
Example #14
Source File: ParsePhoneNumber.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
Source File: ContactUtil.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private static @NonNull String getPrettyPhoneNumber(@NonNull String phoneNumber, @NonNull Locale fallbackLocale) {
  PhoneNumberUtil util = PhoneNumberUtil.getInstance();
  try {
    PhoneNumber parsed = util.parse(phoneNumber, fallbackLocale.getCountry());
    return util.format(parsed, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
  } catch (NumberParseException e) {
    return phoneNumber;
  }
}
 
Example #22
Source File: CountryMaster.java    From XERUNG with Apache License 2.0 5 votes vote down vote up
/**
 * Uses libphonenumber library to fetch a {@link PhoneNumber} object, from
 * a full phone number inclusive of country telephone prefix. Works best
 * by supplying a string without plus and hyphen characters in my experience.
 * 
 * @param phoneNumber	String
 * @return number		{@link PhoneNumber}
 * 
 * @see <a href="https://github.com/googlei18n/libphonenumber">https://github.com/googlei18n/libphonenumber</a>
 */
public PhoneNumber getPhoneNumber(String phoneNumber) {
	PhoneNumberUtil util = PhoneNumberUtil.getInstance();
	PhoneNumber number = null;
	try {
		number = util.parse(phoneNumber, getDefaultCountryIso());
	} catch (NumberParseException e) {
		e.printStackTrace();
	}
	return number;
}
 
Example #23
Source File: CountryMaster.java    From XERUNG with Apache License 2.0 5 votes vote down vote up
/**
 * Uses libphonenumber library to fetch a {@link PhoneNumber} object, from
 * a full phone number inclusive of country telephone prefix. Works best
 * by supplying a string without plus and hyphen characters in my experience.
 * 
 * @param phoneNumber	String
 * @return number		{@link PhoneNumber}
 * 
 * @see <a href="https://github.com/googlei18n/libphonenumber">https://github.com/googlei18n/libphonenumber</a>
 */
public PhoneNumber getPhoneNumber(String phoneNumber) {
	PhoneNumberUtil util = PhoneNumberUtil.getInstance();
	PhoneNumber number = null;
	try {
		number = util.parse(phoneNumber, getDefaultCountryIso());
	} catch (NumberParseException e) {
		e.printStackTrace();
	}
	return number;
}
 
Example #24
Source File: AddressUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #25
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 #26
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 #27
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 #28
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 #29
Source File: PhoneUtil.java    From fenixedu-academic with GNU Lesser General Public License v3.0 4 votes vote down vote up
private PhoneNumberType getPhoneNumberType(PhoneNumber phoneNumber) {
    return phoneNumber != null ? PHONE_UTIL.getNumberType(phoneNumber) : null;
}
 
Example #30
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;
}