FileDocCategorySizeDatePackage
DialerFilter.javaAPI DocAndroid 5.1 API13590Thu Mar 12 22:22:10 GMT 2015android.widget

DialerFilter

public class DialerFilter extends RelativeLayout

Fields Summary
public static final int
DIGITS_AND_LETTERS
This mode has both lines
public static final int
DIGITS_AND_LETTERS_NO_DIGITS
This mode is when after starting in {@link #DIGITS_AND_LETTERS} mode the filter has removed all possibility of the digits matching, leaving only the letters line
public static final int
DIGITS_AND_LETTERS_NO_LETTERS
This mode is when after starting in {@link #DIGITS_AND_LETTERS} mode the filter has removed all possibility of the letters matching, leaving only the digits line
public static final int
DIGITS_ONLY
This mode has only the digits line
public static final int
LETTERS_ONLY
This mode has only the letters line
EditText
mLetters
EditText
mDigits
EditText
mPrimary
EditText
mHint
android.text.InputFilter[]
mInputFilters
ImageView
mIcon
int
mMode
private boolean
mIsQwerty
Constructors Summary
public DialerFilter(android.content.Context context)

        super(context);
    
public DialerFilter(android.content.Context context, android.util.AttributeSet attrs)

        super(context, attrs);
    
Methods Summary
public voidappend(java.lang.String text)

        switch (mMode) {
            case DIGITS_AND_LETTERS:
                mDigits.getText().append(text);
                mLetters.getText().append(text);
                break;

            case DIGITS_AND_LETTERS_NO_LETTERS:
            case DIGITS_ONLY:
                mDigits.getText().append(text);
                break;

            case DIGITS_AND_LETTERS_NO_DIGITS:
            case LETTERS_ONLY:
                mLetters.getText().append(text);
                break;
        }
    
public voidclearText()
Clears both the digits and the filter text.

        Editable text;

        text = mLetters.getText();
        text.clear();

        text = mDigits.getText();
        text.clear();

        // Reset the mode based on the hardware type
        if (mIsQwerty) {
            setMode(DIGITS_AND_LETTERS);
        } else {
            setMode(DIGITS_ONLY);
        }
    
public java.lang.CharSequencegetDigits()

        if (mDigits.getVisibility() == View.VISIBLE) {
            return mDigits.getText();
        } else {
            return "";
        }
    
public java.lang.CharSequencegetFilterText()

        if (mMode != DIGITS_ONLY) {
            return getLetters();
        } else {
            return getDigits();
        }
    
public java.lang.CharSequencegetLetters()

        if (mLetters.getVisibility() == View.VISIBLE) {
            return mLetters.getText();
        } else {
            return "";
        }
    
public intgetMode()

        return mMode;
    
public booleanisQwertyKeyboard()

        return mIsQwerty;
    
private voidmakeDigitsPrimary()

        if (mPrimary == mLetters) {
            swapPrimaryAndHint(false);
        }
    
private voidmakeLettersPrimary()

        if (mPrimary == mDigits) {
            swapPrimaryAndHint(true);
        }
    
protected voidonFinishInflate()

        super.onFinishInflate();

        // Setup the filter view
        mInputFilters = new InputFilter[] { new InputFilter.AllCaps() };

        mHint = (EditText) findViewById(com.android.internal.R.id.hint);
        if (mHint == null) {
            throw new IllegalStateException("DialerFilter must have a child EditText named hint");
        }
        mHint.setFilters(mInputFilters);

        mLetters = mHint;
        mLetters.setKeyListener(TextKeyListener.getInstance());
        mLetters.setMovementMethod(null);
        mLetters.setFocusable(false);

        // Setup the digits view
        mPrimary = (EditText) findViewById(com.android.internal.R.id.primary);
        if (mPrimary == null) {
            throw new IllegalStateException("DialerFilter must have a child EditText named primary");
        }
        mPrimary.setFilters(mInputFilters);

        mDigits = mPrimary;
        mDigits.setKeyListener(DialerKeyListener.getInstance());
        mDigits.setMovementMethod(null);
        mDigits.setFocusable(false);

        // Look for an icon
        mIcon = (ImageView) findViewById(com.android.internal.R.id.icon);

        // Setup focus & highlight for this view
        setFocusable(true);

        // XXX Force the mode to QWERTY for now, since 12-key isn't supported
        mIsQwerty = true;
        setMode(DIGITS_AND_LETTERS);
    
protected voidonFocusChanged(boolean focused, int direction, android.graphics.Rect previouslyFocusedRect)
Only show the icon view when focused, if there is one.

        super.onFocusChanged(focused, direction, previouslyFocusedRect);

        if (mIcon != null) {
            mIcon.setVisibility(focused ? View.VISIBLE : View.GONE);
        }
    
public booleanonKeyDown(int keyCode, android.view.KeyEvent event)

        boolean handled = false;

        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_UP:
            case KeyEvent.KEYCODE_DPAD_DOWN:
            case KeyEvent.KEYCODE_DPAD_LEFT:
            case KeyEvent.KEYCODE_DPAD_RIGHT:
            case KeyEvent.KEYCODE_ENTER:
            case KeyEvent.KEYCODE_DPAD_CENTER:
                break;

            case KeyEvent.KEYCODE_DEL:
                switch (mMode) {
                    case DIGITS_AND_LETTERS:
                        handled = mDigits.onKeyDown(keyCode, event);
                        handled &= mLetters.onKeyDown(keyCode, event);
                        break;

                    case DIGITS_AND_LETTERS_NO_DIGITS:
                        handled = mLetters.onKeyDown(keyCode, event);
                        if (mLetters.getText().length() == mDigits.getText().length()) {
                            setMode(DIGITS_AND_LETTERS);
                        }
                        break;

                    case DIGITS_AND_LETTERS_NO_LETTERS:
                        if (mDigits.getText().length() == mLetters.getText().length()) {
                            mLetters.onKeyDown(keyCode, event);
                            setMode(DIGITS_AND_LETTERS);
                        }
                        handled = mDigits.onKeyDown(keyCode, event);
                        break;

                    case DIGITS_ONLY:
                        handled = mDigits.onKeyDown(keyCode, event);
                        break;

                    case LETTERS_ONLY:
                        handled = mLetters.onKeyDown(keyCode, event);
                        break;
                }
                break;

            default:
                //mIsQwerty = msg.getKeyIsQwertyKeyboard();

                switch (mMode) {
                    case DIGITS_AND_LETTERS:
                        handled = mLetters.onKeyDown(keyCode, event);

                        // pass this throw so the shift state is correct (for example,
                        // on a standard QWERTY keyboard, * and 8 are on the same key)
                        if (KeyEvent.isModifierKey(keyCode)) {
                            mDigits.onKeyDown(keyCode, event);
                            handled = true;
                            break;
                        }

                        // Only check to see if the digit is valid if the key is a printing key
                        // in the TextKeyListener. This prevents us from hiding the digits
                        // line when keys like UP and DOWN are hit.
                        // XXX note that KEYCODE_TAB is special-cased here for 
                        // devices that share tab and 0 on a single key.
                        boolean isPrint = event.isPrintingKey();
                        if (isPrint || keyCode == KeyEvent.KEYCODE_SPACE
                                || keyCode == KeyEvent.KEYCODE_TAB) {
                            char c = event.getMatch(DialerKeyListener.CHARACTERS);
                            if (c != 0) {
                                handled &= mDigits.onKeyDown(keyCode, event);
                            } else {
                                setMode(DIGITS_AND_LETTERS_NO_DIGITS);
                            }
                        }
                        break;

                    case DIGITS_AND_LETTERS_NO_LETTERS:
                    case DIGITS_ONLY:
                        handled = mDigits.onKeyDown(keyCode, event);
                        break;

                    case DIGITS_AND_LETTERS_NO_DIGITS:
                    case LETTERS_ONLY:
                        handled = mLetters.onKeyDown(keyCode, event);
                        break;
                }
        }

        if (!handled) {
            return super.onKeyDown(keyCode, event);
        } else {
            return true;
        }
    
public booleanonKeyUp(int keyCode, android.view.KeyEvent event)

        boolean a = mLetters.onKeyUp(keyCode, event);
        boolean b = mDigits.onKeyUp(keyCode, event);
        return a || b;
    
protected voidonModeChange(int oldMode, int newMode)
Called right after the mode changes to give subclasses the option to restyle, etc.

    
public voidremoveFilterWatcher(android.text.TextWatcher watcher)

        Spannable text;
        if (mMode != DIGITS_ONLY) {
            text = mLetters.getText();
        } else {
            text = mDigits.getText();
        }
        text.removeSpan(watcher);
    
public voidsetDigitsWatcher(android.text.TextWatcher watcher)

        CharSequence text = mDigits.getText();
        Spannable span = (Spannable)text;
        span.setSpan(watcher, 0, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    
public voidsetFilterWatcher(android.text.TextWatcher watcher)

        if (mMode != DIGITS_ONLY) {
            setLettersWatcher(watcher);
        } else {
            setDigitsWatcher(watcher);
        }
    
public voidsetLettersWatcher(android.text.TextWatcher watcher)

        CharSequence text = mLetters.getText();
        Spannable span = (Spannable)text;
        span.setSpan(watcher, 0, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    
public voidsetMode(int newMode)
Change the mode of the widget.

param
newMode The mode to switch to.

        switch (newMode) {
            case DIGITS_AND_LETTERS:
                makeDigitsPrimary();
                mLetters.setVisibility(View.VISIBLE);
                mDigits.setVisibility(View.VISIBLE);
                break;

            case DIGITS_ONLY:
                makeDigitsPrimary();
                mLetters.setVisibility(View.GONE);
                mDigits.setVisibility(View.VISIBLE);
                break;

            case LETTERS_ONLY:
                makeLettersPrimary();
                mLetters.setVisibility(View.VISIBLE);
                mDigits.setVisibility(View.GONE);
                break;

            case DIGITS_AND_LETTERS_NO_LETTERS:
                makeDigitsPrimary();
                mLetters.setVisibility(View.INVISIBLE);
                mDigits.setVisibility(View.VISIBLE);
                break;

            case DIGITS_AND_LETTERS_NO_DIGITS:
                makeLettersPrimary();
                mLetters.setVisibility(View.VISIBLE);
                mDigits.setVisibility(View.INVISIBLE);
                break;

        }
        int oldMode = mMode;
        mMode = newMode;
        onModeChange(oldMode, newMode);
    
private voidswapPrimaryAndHint(boolean makeLettersPrimary)

        Editable lettersText = mLetters.getText();
        Editable digitsText = mDigits.getText();
        KeyListener lettersInput = mLetters.getKeyListener();
        KeyListener digitsInput = mDigits.getKeyListener();

        if (makeLettersPrimary) {
            mLetters = mPrimary;
            mDigits = mHint;
        } else {
            mLetters = mHint;
            mDigits = mPrimary;
        }

        mLetters.setKeyListener(lettersInput);
        mLetters.setText(lettersText);
        lettersText = mLetters.getText();
        Selection.setSelection(lettersText, lettersText.length());

        mDigits.setKeyListener(digitsInput);
        mDigits.setText(digitsText);
        digitsText = mDigits.getText();
        Selection.setSelection(digitsText, digitsText.length());

        // Reset the filters
        mPrimary.setFilters(mInputFilters);
        mHint.setFilters(mInputFilters);