io.michaelrocks.libphonenumber.android.NumberParseException Java Examples

The following examples show how to use io.michaelrocks.libphonenumber.android.NumberParseException. 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: CountryCodePicker.java    From CountryCodePicker with Apache License 2.0 6 votes vote down vote up
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
  super.onTextChanged(s, start, before, count);
  try {
    String iso = null;
    if (mSelectedCountry != null) iso = mSelectedCountry.getPhoneCode().toUpperCase();
    Phonenumber.PhoneNumber phoneNumber = mPhoneUtil.parse(s.toString(), iso);
    iso = mPhoneUtil.getRegionCodeForNumber(phoneNumber);
    if (iso != null) {
      //int countryIdx = mCountries.indexOfIso(iso);
      //mCountrySpinner.setSelection(countryIdx);
    }
  } catch (NumberParseException ignored) {
  }

  if (mPhoneNumberInputValidityListener != null) {
    boolean validity = isValid();
    if (validity != lastValidity) {
      mPhoneNumberInputValidityListener.onFinish(CountryCodePicker.this, validity);
    }
    lastValidity = validity;
  }
}
 
Example #2
Source File: CountryCodePicker.java    From CountryCodePickerProject with Apache License 2.0 6 votes vote down vote up
/**
 * This function will check the validity of entered number.
 * It will use PhoneNumberUtil to check validity
 *
 * @return true if entered carrier number is valid else false
 */
public boolean isValidFullNumber() {
    try {
        if (getEditText_registeredCarrierNumber() != null && getEditText_registeredCarrierNumber().getText().length() != 0) {
            Phonenumber.PhoneNumber phoneNumber = getPhoneUtil().parse("+" + selectedCCPCountry.getPhoneCode() + getEditText_registeredCarrierNumber().getText().toString(), selectedCCPCountry.getNameCode());
            return getPhoneUtil().isValidNumber(phoneNumber);
        } else if (getEditText_registeredCarrierNumber() == null) {
            Toast.makeText(context, "No editText for Carrier number found.", Toast.LENGTH_SHORT).show();
            return false;
        } else {
            return false;
        }
    } catch (NumberParseException e) {
        //            when number could not be parsed, its not valid
        return false;
    }
}
 
Example #3
Source File: MainActivity.java    From libphonenumber-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  final TextView textView = findViewById(R.id.textView);
  findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
    private PhoneNumberUtil util = null;

    @Override
    public void onClick(final View v) {
      if (util == null) {
        util = PhoneNumberUtil.createInstance(getApplicationContext());
      }

      try {
        final Phonenumber.PhoneNumber phoneNumber = util.parse("8005551212", "US");
        textView.setText(util.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL));
      } catch (NumberParseException e) {
        e.printStackTrace();
      }
    }
  });
}
 
Example #4
Source File: EnterPhoneNumberActivity.java    From Conversations with GNU General Public License v3.0 6 votes vote down vote up
private void onNextClick(View v) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    try {
        final Editable number = this.binding.number.getText();
        final String input = number.toString();
        final Phonenumber.PhoneNumber phoneNumber = PhoneNumberUtilWrapper.getInstance(this).parse(input, region);
        this.binding.countryCode.setText(String.valueOf(phoneNumber.getCountryCode()));
        number.clear();
        number.append(String.valueOf(phoneNumber.getNationalNumber()));
        final String formattedPhoneNumber = PhoneNumberUtilWrapper.getInstance(this).format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL).replace(' ','\u202F');

        if (PhoneNumberUtilWrapper.getInstance(this).isValidNumber(phoneNumber)) {
            builder.setMessage(Html.fromHtml(getString(R.string.we_will_be_verifying, formattedPhoneNumber)));
            builder.setNegativeButton(R.string.edit, null);
            builder.setPositiveButton(R.string.ok, (dialog, which) -> onPhoneNumberEntered(phoneNumber));
        } else {
            builder.setMessage(getString(R.string.not_a_valid_phone_number, formattedPhoneNumber));
            builder.setPositiveButton(R.string.ok, null);
        }
        Log.d(Config.LOGTAG, phoneNumber.toString());
    } catch (NumberParseException e) {
        builder.setMessage(R.string.please_enter_your_phone_number);
        builder.setPositiveButton(R.string.ok, null);
    }
    builder.create().show();
}
 
Example #5
Source File: CountryCodePicker.java    From CountryCodePicker with Apache License 2.0 5 votes vote down vote up
/**
 * Get PhoneNumber object
 *
 * @return Phone Number | null on error
 */
@SuppressWarnings("unused") public Phonenumber.PhoneNumber getPhoneNumber() {
  try {
    String iso = null;
    if (mSelectedCountry != null) iso = mSelectedCountry.getIso().toUpperCase();
    if (mRegisteredPhoneNumberTextView == null) {
      Log.w(TAG, getContext().getString(R.string.error_unregister_carrier_number));
      return null;
    }
    return mPhoneUtil.parse(mRegisteredPhoneNumberTextView.getText().toString(), iso);
  } catch (NumberParseException ignored) {
    return null;
  }
}
 
Example #6
Source File: CountryCodePicker.java    From CountryCodePickerProject with Apache License 2.0 5 votes vote down vote up
private Phonenumber.PhoneNumber getEnteredPhoneNumber() throws NumberParseException {
    String carrierNumber = "";
    if (editText_registeredCarrierNumber != null) {
        carrierNumber = PhoneNumberUtil.normalizeDigitsOnly(editText_registeredCarrierNumber.getText().toString());
    }
    return getPhoneUtil().parse(carrierNumber, getSelectedCountryNameCode());
}
 
Example #7
Source File: CountryCodePicker.java    From CountryCodePickerProject with Apache License 2.0 5 votes vote down vote up
/**
 * This function combines selected country code from CCP and carrier number from @param editTextCarrierNumber
 *
 * @return Full number is countryCode + carrierNumber i.e countryCode= 91 and carrier number= 8866667722, this will return "918866667722"
 */
public String getFullNumber() {
    try {
        Phonenumber.PhoneNumber phoneNumber = getEnteredPhoneNumber();
        return getPhoneUtil().format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164).substring(1);
    } catch (NumberParseException e) {
        Log.e(TAG, "getFullNumber: Could not parse number");
        return getSelectedCountryCode() + PhoneNumberUtil.normalizeDigitsOnly(editText_registeredCarrierNumber.getText().toString());
    }
}
 
Example #8
Source File: CountryCodePicker.java    From CountryCodePickerProject with Apache License 2.0 5 votes vote down vote up
/**
 * This function combines selected country code from CCP and carrier number from @param editTextCarrierNumber
 * This will return formatted number.
 *
 * @return Full number is countryCode + carrierNumber i.e countryCode= 91 and carrier number= 8866667722, this will return "918866667722"
 */
public String getFormattedFullNumber() {
    try {
        Phonenumber.PhoneNumber phoneNumber = getEnteredPhoneNumber();
        return "+" + getPhoneUtil().format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL).substring(1);
    } catch (NumberParseException e) {
        Log.e(TAG, "getFullNumber: Could not parse number");
        return getFullNumberWithPlus();
    }
}
 
Example #9
Source File: PhoneNumberUtilWrapper.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
public static String normalize(Context context, String input) throws IllegalArgumentException, NumberParseException {
    final Phonenumber.PhoneNumber number = getInstance(context).parse(input, LocationProvider.getUserCountry(context));
    if (!getInstance(context).isValidNumber(number)) {
        throw new IllegalArgumentException("Not a valid phone number");
    }
    return normalize(context, number);
}
 
Example #10
Source File: VerifyActivity.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
private void onResendSmsButton(View view) {
    try {
        xmppConnectionService.getQuickConversationsService().requestVerification(PhoneNumberUtilWrapper.toPhoneNumber(this, account.getJid()));
        setRequestingVerificationState(true);
    } catch (NumberParseException e) {

    }
}
 
Example #11
Source File: PhoneNumberContact.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
private PhoneNumberContact(Context context, Cursor cursor) throws IllegalArgumentException {
    super(cursor);
    try {
        this.phoneNumber = PhoneNumberUtilWrapper.normalize(context,cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
    } catch (NumberParseException | NullPointerException e) {
        throw new IllegalArgumentException(e);
    }
}
 
Example #12
Source File: PhoneNumberUtilWrapper.java    From Conversations with GNU General Public License v3.0 4 votes vote down vote up
public static Phonenumber.PhoneNumber toPhoneNumber(Context context, Jid jid) throws NumberParseException {
    return getInstance(context).parse(jid.getEscapedLocal(), "de");
}