FileDocCategorySizeDatePackage
PasswordEntryKeyboardHelper.javaAPI DocAndroid 5.1 API13092Thu Mar 12 22:22:10 GMT 2015com.android.internal.widget

PasswordEntryKeyboardHelper

public class PasswordEntryKeyboardHelper extends Object implements android.inputmethodservice.KeyboardView.OnKeyboardActionListener

Fields Summary
public static final int
KEYBOARD_MODE_ALPHA
public static final int
KEYBOARD_MODE_NUMERIC
private static final int
KEYBOARD_STATE_NORMAL
private static final int
KEYBOARD_STATE_SHIFTED
private static final int
KEYBOARD_STATE_CAPSLOCK
private static final String
TAG
private int
mKeyboardMode
private int
mKeyboardState
private PasswordEntryKeyboard
mQwertyKeyboard
private PasswordEntryKeyboard
mQwertyKeyboardShifted
private PasswordEntryKeyboard
mSymbolsKeyboard
private PasswordEntryKeyboard
mSymbolsKeyboardShifted
private PasswordEntryKeyboard
mNumericKeyboard
private final android.content.Context
mContext
private final android.view.View
mTargetView
private final android.inputmethodservice.KeyboardView
mKeyboardView
private long[]
mVibratePattern
private boolean
mEnableHaptics
private static final int
NUMERIC
private static final int
QWERTY
private static final int
QWERTY_SHIFTED
private static final int
SYMBOLS
private static final int
SYMBOLS_SHIFTED
int[]
mLayouts
private boolean
mUsingScreenWidth
Constructors Summary
public PasswordEntryKeyboardHelper(android.content.Context context, android.inputmethodservice.KeyboardView keyboardView, android.view.View targetView)


           
        this(context, keyboardView, targetView, true, null);
    
public PasswordEntryKeyboardHelper(android.content.Context context, android.inputmethodservice.KeyboardView keyboardView, android.view.View targetView, boolean useFullScreenWidth)

        this(context, keyboardView, targetView, useFullScreenWidth, null);
    
public PasswordEntryKeyboardHelper(android.content.Context context, android.inputmethodservice.KeyboardView keyboardView, android.view.View targetView, boolean useFullScreenWidth, int[] layouts)

        mContext = context;
        mTargetView = targetView;
        mKeyboardView = keyboardView;
        mKeyboardView.setOnKeyboardActionListener(this);
        mUsingScreenWidth = useFullScreenWidth;
        if (layouts != null) {
            if (layouts.length != mLayouts.length) {
                throw new RuntimeException("Wrong number of layouts");
            }
            for (int i = 0; i < mLayouts.length; i++) {
                mLayouts[i] = layouts[i];
            }
        }
        createKeyboards();
    
Methods Summary
public voidcreateKeyboards()

        LayoutParams lp = mKeyboardView.getLayoutParams();
        if (mUsingScreenWidth || lp.width == ViewGroup.LayoutParams.MATCH_PARENT) {
            createKeyboardsWithDefaultWidth();
        } else {
            createKeyboardsWithSpecificSize(lp.width, lp.height);
        }
    
private voidcreateKeyboardsWithDefaultWidth()

        mNumericKeyboard = new PasswordEntryKeyboard(mContext, mLayouts[NUMERIC]);
        mQwertyKeyboard = new PasswordEntryKeyboard(mContext, mLayouts[QWERTY], R.id.mode_normal);
        mQwertyKeyboard.enableShiftLock();

        mQwertyKeyboardShifted = new PasswordEntryKeyboard(mContext, mLayouts[QWERTY_SHIFTED],
                R.id.mode_normal);
        mQwertyKeyboardShifted.enableShiftLock();
        mQwertyKeyboardShifted.setShifted(true); // always shifted.

        mSymbolsKeyboard = new PasswordEntryKeyboard(mContext, mLayouts[SYMBOLS]);
        mSymbolsKeyboard.enableShiftLock();

        mSymbolsKeyboardShifted = new PasswordEntryKeyboard(mContext, mLayouts[SYMBOLS_SHIFTED]);
        mSymbolsKeyboardShifted.enableShiftLock();
        mSymbolsKeyboardShifted.setShifted(true); // always shifted
    
private voidcreateKeyboardsWithSpecificSize(int width, int height)

        mNumericKeyboard = new PasswordEntryKeyboard(mContext, mLayouts[NUMERIC], width, height);
        mQwertyKeyboard = new PasswordEntryKeyboard(mContext, mLayouts[QWERTY], R.id.mode_normal,
                width, height);
        mQwertyKeyboard.enableShiftLock();

        mQwertyKeyboardShifted = new PasswordEntryKeyboard(mContext, mLayouts[QWERTY_SHIFTED],
                R.id.mode_normal, width, height);
        mQwertyKeyboardShifted.enableShiftLock();
        mQwertyKeyboardShifted.setShifted(true); // always shifted.

        mSymbolsKeyboard = new PasswordEntryKeyboard(mContext, mLayouts[SYMBOLS], width, height);
        mSymbolsKeyboard.enableShiftLock();

        mSymbolsKeyboardShifted = new PasswordEntryKeyboard(mContext, mLayouts[SYMBOLS_SHIFTED],
                width, height);
        mSymbolsKeyboardShifted.enableShiftLock();
        mSymbolsKeyboardShifted.setShifted(true); // always shifted
    
public voidhandleBackspace()

        sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
        performHapticFeedback();
    
private voidhandleCharacter(int primaryCode, int[] keyCodes)

        // Maybe turn off shift if not in capslock mode.
        if (mKeyboardView.isShifted() && primaryCode != ' " && primaryCode != '\n") {
            primaryCode = Character.toUpperCase(primaryCode);
        }
        sendKeyEventsToTarget(primaryCode);
    
private voidhandleClose()


    
private voidhandleModeChange()

        final Keyboard current = mKeyboardView.getKeyboard();
        Keyboard next = null;
        if (current == mQwertyKeyboard || current == mQwertyKeyboardShifted) {
            next = mSymbolsKeyboard;
        } else if (current == mSymbolsKeyboard || current == mSymbolsKeyboardShifted) {
            next = mQwertyKeyboard;
        }
        if (next != null) {
            mKeyboardView.setKeyboard(next);
            mKeyboardState = KEYBOARD_STATE_NORMAL;
        }
    
private voidhandleShift()

        if (mKeyboardView == null) {
            return;
        }
        Keyboard current = mKeyboardView.getKeyboard();
        PasswordEntryKeyboard next = null;
        final boolean isAlphaMode = current == mQwertyKeyboard
                || current == mQwertyKeyboardShifted;
        if (mKeyboardState == KEYBOARD_STATE_NORMAL) {
            mKeyboardState = isAlphaMode ? KEYBOARD_STATE_SHIFTED : KEYBOARD_STATE_CAPSLOCK;
            next = isAlphaMode ? mQwertyKeyboardShifted : mSymbolsKeyboardShifted;
        } else if (mKeyboardState == KEYBOARD_STATE_SHIFTED) {
            mKeyboardState = KEYBOARD_STATE_CAPSLOCK;
            next = isAlphaMode ? mQwertyKeyboardShifted : mSymbolsKeyboardShifted;
        } else if (mKeyboardState == KEYBOARD_STATE_CAPSLOCK) {
            mKeyboardState = KEYBOARD_STATE_NORMAL;
            next = isAlphaMode ? mQwertyKeyboard : mSymbolsKeyboard;
        }
        if (next != null) {
            if (next != current) {
                mKeyboardView.setKeyboard(next);
            }
            next.setShiftLocked(mKeyboardState == KEYBOARD_STATE_CAPSLOCK);
            mKeyboardView.setShifted(mKeyboardState != KEYBOARD_STATE_NORMAL);
        }
    
public booleanisAlpha()

        return mKeyboardMode == KEYBOARD_MODE_ALPHA;
    
public voidonKey(int primaryCode, int[] keyCodes)

        if (primaryCode == Keyboard.KEYCODE_DELETE) {
            handleBackspace();
        } else if (primaryCode == Keyboard.KEYCODE_SHIFT) {
            handleShift();
        } else if (primaryCode == Keyboard.KEYCODE_CANCEL) {
            handleClose();
            return;
        } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE && mKeyboardView != null) {
            handleModeChange();
        } else {
            handleCharacter(primaryCode, keyCodes);
            // Switch back to old keyboard if we're not in capslock mode
            if (mKeyboardState == KEYBOARD_STATE_SHIFTED) {
                // skip to the unlocked state
                mKeyboardState = KEYBOARD_STATE_CAPSLOCK;
                handleShift();
            }
        }
    
public voidonPress(int primaryCode)

        performHapticFeedback();
    
public voidonRelease(int primaryCode)


    
public voidonText(java.lang.CharSequence text)


    
private voidperformHapticFeedback()

        if (mEnableHaptics) {
            mKeyboardView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY,
                    HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING
                    | HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
        }
    
public voidsendDownUpKeyEvents(int keyEventCode)

        long eventTime = SystemClock.uptimeMillis();
        ViewRootImpl viewRootImpl = mTargetView.getViewRootImpl();
        viewRootImpl.dispatchKeyFromIme(
                new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, keyEventCode, 0, 0,
                        KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
                    KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));
        viewRootImpl.dispatchKeyFromIme(
                new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP, keyEventCode, 0, 0,
                        KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
                        KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));
    
private voidsendKeyEventsToTarget(int character)

        ViewRootImpl viewRootImpl = mTargetView.getViewRootImpl();
        KeyEvent[] events = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD).getEvents(
                new char[] { (char) character });
        if (events != null) {
            final int N = events.length;
            for (int i=0; i<N; i++) {
                KeyEvent event = events[i];
                event = KeyEvent.changeFlags(event, event.getFlags()
                        | KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE);
                viewRootImpl.dispatchInputEvent(event);
            }
        }
    
public voidsetEnableHaptics(boolean enabled)

        mEnableHaptics = enabled;
    
public voidsetKeyboardMode(int mode)

        switch (mode) {
            case KEYBOARD_MODE_ALPHA:
                mKeyboardView.setKeyboard(mQwertyKeyboard);
                mKeyboardState = KEYBOARD_STATE_NORMAL;
                final boolean visiblePassword = Settings.System.getInt(
                        mContext.getContentResolver(),
                        Settings.System.TEXT_SHOW_PASSWORD, 1) != 0;
                final boolean enablePreview = false; // TODO: grab from configuration
                mKeyboardView.setPreviewEnabled(visiblePassword && enablePreview);
                break;
            case KEYBOARD_MODE_NUMERIC:
                mKeyboardView.setKeyboard(mNumericKeyboard);
                mKeyboardState = KEYBOARD_STATE_NORMAL;
                mKeyboardView.setPreviewEnabled(false); // never show popup for numeric keypad
                break;
        }
        mKeyboardMode = mode;
    
public voidsetVibratePattern(int id)
Sets and enables vibrate pattern. If id is 0 (or can't be loaded), vibrate is disabled.

param
id resource id for array containing vibrate pattern.

        int[] tmpArray = null;
        try {
            tmpArray = mContext.getResources().getIntArray(id);
        } catch (Resources.NotFoundException e) {
            if (id != 0) {
                Log.e(TAG, "Vibrate pattern missing", e);
            }
        }
        if (tmpArray == null) {
            mVibratePattern = null;
            return;
        }
        mVibratePattern = new long[tmpArray.length];
        for (int i = 0; i < tmpArray.length; i++) {
            mVibratePattern[i] = tmpArray[i];
        }
    
public voidswipeDown()


    
public voidswipeLeft()


    
public voidswipeRight()


    
public voidswipeUp()