Java Code Examples for android.telephony.PhoneNumberUtils#isNonSeparator()

The following examples show how to use android.telephony.PhoneNumberUtils#isNonSeparator() . 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: InternationalPhoneTextWatcher.java    From CountryCodePickerProject with Apache License 2.0 5 votes vote down vote up
/**
 * this will format the number in international format (only).
 */
private String reformat(CharSequence s) {

    String internationalFormatted = "";
    mFormatter.clear();
    char lastNonSeparator = 0;

    String countryCallingCode = "+" + countryPhoneCode;

    if (internationalOnly || (s.length() > 0 && s.charAt(0) != '0'))
        //to have number formatted as international format, add country code before that
        s = countryCallingCode + s;
    int len = s.length();

    for (int i = 0; i < len; i++) {
        char c = s.charAt(i);
        if (PhoneNumberUtils.isNonSeparator(c)) {
            if (lastNonSeparator != 0) {
                internationalFormatted = mFormatter.inputDigit(lastNonSeparator);
            }
            lastNonSeparator = c;
        }
    }
    if (lastNonSeparator != 0) {
        internationalFormatted = mFormatter.inputDigit(lastNonSeparator);
    }

    internationalFormatted = internationalFormatted.trim();
    if (internationalOnly || (s.length() == 0 || s.charAt(0) != '0')) {
        if (internationalFormatted.length() > countryCallingCode.length()) {
            if (internationalFormatted.charAt(countryCallingCode.length()) == ' ')
                internationalFormatted = internationalFormatted.substring(countryCallingCode.length() + 1);
            else
                internationalFormatted = internationalFormatted.substring(countryCallingCode.length());
        } else {
            internationalFormatted = "";
        }
    }
    return TextUtils.isEmpty(internationalFormatted) ? "" : internationalFormatted;
}
 
Example 2
Source File: InternationalPhoneTextWatcher.java    From CountryCodePickerProject with Apache License 2.0 5 votes vote down vote up
private boolean hasSeparator(final CharSequence s, final int start, final int count) {
    for (int i = start; i < start + count; i++) {
        char c = s.charAt(i);
        if (!PhoneNumberUtils.isNonSeparator(c)) {
            return true;
        }
    }
    return false;
}
 
Example 3
Source File: CustomPhoneNumberFormattingTextWatcher.java    From android-phone-number-with-flags with Apache License 2.0 5 votes vote down vote up
/**
 * Generate the formatted number by ignoring all non-dialable chars and stick the cursor to the
 * nearest dialable char to the left. For instance, if the number is  (650) 123-45678 and '4' is
 * removed then the cursor should be behind '3' instead of '-'.
 */
private String reformat(CharSequence s, int cursor) {
    // The index of char to the leftward of the cursor.
    int curIndex = cursor - 1;
    String formatted = null;
    mFormatter.clear();
    char lastNonSeparator = 0;
    boolean hasCursor = false;
    int len = s.length();
    for (int i = 0; i < len; i++) {
        char c = s.charAt(i);
        if (PhoneNumberUtils.isNonSeparator(c)) {
            if (lastNonSeparator != 0) {
                formatted = getFormattedNumber(lastNonSeparator, hasCursor);
                hasCursor = false;
            }
            lastNonSeparator = c;
        }
        if (i == curIndex) {
            hasCursor = true;
        }
    }
    if (lastNonSeparator != 0) {
        formatted = getFormattedNumber(lastNonSeparator, hasCursor);
    }
    return formatted;
}
 
Example 4
Source File: CustomPhoneNumberFormattingTextWatcher.java    From android-phone-number-with-flags with Apache License 2.0 5 votes vote down vote up
private boolean hasSeparator(final CharSequence s, final int start, final int count) {
    for (int i = start; i < start + count; i++) {
        char c = s.charAt(i);
        if (!PhoneNumberUtils.isNonSeparator(c)) {
            return true;
        }
    }
    return false;
}
 
Example 5
Source File: InternationalPhoneTextWatcher.java    From CountryCodePickerProject with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void afterTextChanged(Editable s) {
    if (mStopFormatting) {
        // Restart the formatting when all texts were clear.
        mStopFormatting = !(s.length() == 0);
        return;
    }
    if (mSelfChange) {
        // Ignore the change caused by s.replace().
        return;
    }

    //calculate few things that will be helpful later
    int selectionEnd = Selection.getSelectionEnd(s);
    boolean isCursorAtEnd = (selectionEnd == s.length());

    //get formatted text for this number
    String formatted = reformat(s);

    //now calculate cursor position in formatted text
    int finalCursorPosition = 0;
    if (formatted.equals(s.toString())) {
        //means there is no change while formatting don't move cursor
        finalCursorPosition = selectionEnd;
    } else if (isCursorAtEnd) {
        //if cursor was already at the end, put it at the end.
        finalCursorPosition = formatted.length();
    } else {

        // if no earlier case matched, we will use "digitBeforeCursor" way to figure out the cursor position
        int digitsBeforeCursor = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i >= selectionEnd) {
                break;
            }
            if (PhoneNumberUtils.isNonSeparator(s.charAt(i))) {
                digitsBeforeCursor++;
            }
        }

        //at this point we will have digitsBeforeCursor calculated.
        // now find this position in formatted text
        for (int i = 0, digitPassed = 0; i < formatted.length(); i++) {
            if (digitPassed == digitsBeforeCursor) {
                finalCursorPosition = i;
                break;
            }
            if (PhoneNumberUtils.isNonSeparator(formatted.charAt(i))) {
                digitPassed++;
            }
        }
    }

    //if this ends right before separator, we might wish to move it further so user do not delete separator by mistake.
    // because deletion of separator will cause stop formatting that should not happen by mistake
    if (!isCursorAtEnd) {
        while (0 < finalCursorPosition - 1 && !PhoneNumberUtils.isNonSeparator(formatted.charAt(finalCursorPosition - 1))) {
            finalCursorPosition--;
        }
    }

    //Now we have everything calculated, set this values in
    try {
        if (formatted != null) {
            mSelfChange = true;
            s.replace(0, s.length(), formatted, 0, formatted.length());
            mSelfChange = false;
            lastFormatted = s;
            Selection.setSelection(s, finalCursorPosition);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}