FileDocCategorySizeDatePackage
SoftKeyboard.javaAPI DocAndroid 1.5 API17111Wed May 06 22:42:48 BST 2009com.android.inputmethod.pinyin

SoftKeyboard

public class SoftKeyboard extends Object
Class used to represent a soft keyboard definition, including the height, the background image, the image for high light, the keys, etc.

Fields Summary
private int
mSkbXmlId
The XML resource id for this soft keyboard.
private boolean
mCacheFlag
Do we need to cache this soft keyboard?
private boolean
mStickyFlag
After user switches to this soft keyboard, if this flag is true, this soft keyboard will be kept unless explicit switching operation is performed, otherwise IME will switch back to the previous keyboard layout whenever user clicks on any none-function key.
private int
mCacheId
The cache id for this soft keyboard. It is used to identify it in the soft keyboard pool.
private boolean
mNewlyLoadedFlag
Used to indicate whether this soft keyboard is newly loaded from an XML file or is just gotten from the soft keyboard pool.
private int
mSkbCoreWidth
The width of the soft keyboard.
private int
mSkbCoreHeight
The height of the soft keyboard.
private SkbTemplate
mSkbTemplate
The soft keyboard template for this soft keyboard.
private boolean
mIsQwerty
Used to indicate whether this soft keyboard is a QWERTY keyboard.
private boolean
mIsQwertyUpperCase
When {@link #mIsQwerty} is true, this member is Used to indicate that the soft keyboard should be displayed in uppercase.
private int
mEnabledRowId
The id of the rows which are enabled. Rows with id {@link KeyRow#ALWAYS_SHOW_ROW_ID} are always enabled.
private List
mKeyRows
Rows in this soft keyboard. Each row has a id. Only matched rows will be enabled.
public android.graphics.drawable.Drawable
mSkbBg
Background of the soft keyboard. If it is null, the one in the soft keyboard template will be used.
private android.graphics.drawable.Drawable
mBalloonBg
Background for key balloon. If it is null, the one in the soft keyboard template will be used.
private android.graphics.drawable.Drawable
mPopupBg
Background for popup mini soft keyboard. If it is null, the one in the soft keyboard template will be used.
private float
mKeyXMargin
The left and right margin of a key.
private float
mKeyYMargin
The top and bottom margin of a key.
private android.graphics.Rect
mTmpRect
Constructors Summary
public SoftKeyboard(int skbXmlId, SkbTemplate skbTemplate, int skbWidth, int skbHeight)


          
              
        mSkbXmlId = skbXmlId;
        mSkbTemplate = skbTemplate;
        mSkbCoreWidth = skbWidth;
        mSkbCoreHeight = skbHeight;
    
Methods Summary
public booleanaddSoftKey(SoftKey softKey)

        if (mKeyRows.size() == 0) return false;
        KeyRow keyRow = mKeyRows.get(mKeyRows.size() - 1);
        if (null == keyRow) return false;
        List<SoftKey> softKeys = keyRow.mSoftKeys;

        softKey.setSkbCoreSize(mSkbCoreWidth, mSkbCoreHeight);
        softKeys.add(softKey);
        if (softKey.mTopF < keyRow.mTopF) {
            keyRow.mTopF = softKey.mTopF;
        }
        if (softKey.mBottomF > keyRow.mBottomF) {
            keyRow.mBottomF = softKey.mBottomF;
        }
        return true;
    
public voidbeginNewRow(int rowId, float yStartingPos)

        if (null == mKeyRows) mKeyRows = new ArrayList<KeyRow>();
        KeyRow keyRow = new KeyRow();
        keyRow.mRowId = rowId;
        keyRow.mTopF = yStartingPos;
        keyRow.mBottomF = yStartingPos;
        keyRow.mSoftKeys = new ArrayList<SoftKey>();
        mKeyRows.add(keyRow);
    
public voiddisableToggleState(int toggleStateId, boolean resetIfNotFound)

        int rowNum = mKeyRows.size();
        for (int row = 0; row < rowNum; row++) {
            KeyRow keyRow = mKeyRows.get(row);
            List<SoftKey> softKeys = keyRow.mSoftKeys;
            int keyNum = softKeys.size();
            for (int i = 0; i < keyNum; i++) {
                SoftKey sKey = softKeys.get(i);
                if (sKey instanceof SoftKeyToggle) {
                    ((SoftKeyToggle) sKey).disableToggleState(toggleStateId,
                            resetIfNotFound);
                }
            }
        }
    
private booleanenableRow(int rowId)
Enable a row with the give toggle Id. Rows with other toggle ids (except the id {@link KeyRow#ALWAYS_SHOW_ROW_ID}) will be disabled.

param
rowId The row id to enable.
return
True if the soft keyboard requires redrawing.

        if (KeyRow.ALWAYS_SHOW_ROW_ID == rowId) return false;

        boolean enabled = false;
        int rowNum = mKeyRows.size();
        for (int row = rowNum - 1; row >= 0; row--) {
            if (mKeyRows.get(row).mRowId == rowId) {
                enabled = true;
                break;
            }
        }
        if (enabled) {
            mEnabledRowId = rowId;
        }
        return enabled;
    
public voidenableToggleState(int toggleStateId, boolean resetIfNotFound)

        int rowNum = mKeyRows.size();
        for (int row = 0; row < rowNum; row++) {
            KeyRow keyRow = mKeyRows.get(row);
            List<SoftKey> softKeys = keyRow.mSoftKeys;
            int keyNum = softKeys.size();
            for (int i = 0; i < keyNum; i++) {
                SoftKey sKey = softKeys.get(i);
                if (sKey instanceof SoftKeyToggle) {
                    ((SoftKeyToggle) sKey).enableToggleState(toggleStateId,
                            resetIfNotFound);
                }
            }
        }
    
public voidenableToggleStates(com.android.inputmethod.pinyin.InputModeSwitcher.ToggleStates toggleStates)

        if (null == toggleStates) return;

        enableRow(toggleStates.mRowIdToEnable);

        boolean isQwerty = toggleStates.mQwerty;
        boolean isQwertyUpperCase = toggleStates.mQwertyUpperCase;
        boolean needUpdateQwerty = (isQwerty && mIsQwerty && (mIsQwertyUpperCase != isQwertyUpperCase));
        int states[] = toggleStates.mKeyStates;
        int statesNum = toggleStates.mKeyStatesNum;

        int rowNum = mKeyRows.size();
        for (int row = 0; row < rowNum; row++) {
            KeyRow keyRow = mKeyRows.get(row);
            if (KeyRow.ALWAYS_SHOW_ROW_ID != keyRow.mRowId
                    && keyRow.mRowId != mEnabledRowId) {
                continue;
            }
            List<SoftKey> softKeys = keyRow.mSoftKeys;
            int keyNum = softKeys.size();
            for (int keyPos = 0; keyPos < keyNum; keyPos++) {
                SoftKey sKey = softKeys.get(keyPos);
                if (sKey instanceof SoftKeyToggle) {
                    for (int statePos = 0; statePos < statesNum; statePos++) {
                        ((SoftKeyToggle) sKey).enableToggleState(
                                states[statePos], statePos == 0);
                    }
                    if (0 == statesNum) {
                        ((SoftKeyToggle) sKey).disableAllToggleStates();
                    }
                }
                if (needUpdateQwerty) {
                    if (sKey.mKeyCode >= KeyEvent.KEYCODE_A
                            && sKey.mKeyCode <= KeyEvent.KEYCODE_Z) {
                        sKey.changeCase(isQwertyUpperCase);
                    }
                }
            }
        }
        mIsQwertyUpperCase = isQwertyUpperCase;
    
public android.graphics.drawable.DrawablegetBalloonBackground()

        if (null != mBalloonBg) return mBalloonBg;
        return mSkbTemplate.getBalloonBackground();
    
public booleangetCacheFlag()

        return mCacheFlag;
    
public intgetCacheId()

        return mCacheId;
    
public SoftKeygetKey(int row, int location)

        if (null != mKeyRows && mKeyRows.size() > row) {
            List<SoftKey> softKeys = mKeyRows.get(row).mSoftKeys;
            if (softKeys.size() > location) {
                return softKeys.get(location);
            }
        }
        return null;
    
public com.android.inputmethod.pinyin.SoftKeyboard$KeyRowgetKeyRowForDisplay(int row)

        if (null != mKeyRows && mKeyRows.size() > row) {
            KeyRow keyRow = mKeyRows.get(row);
            if (KeyRow.ALWAYS_SHOW_ROW_ID == keyRow.mRowId
                    || keyRow.mRowId == mEnabledRowId) {
                return keyRow;
            }
        }
        return null;
    
public intgetKeyXMargin()

        Environment env = Environment.getInstance();
        return (int) (mKeyXMargin * mSkbCoreWidth * env.getKeyXMarginFactor());
    
public intgetKeyYMargin()

        Environment env = Environment.getInstance();
        return (int) (mKeyYMargin * mSkbCoreHeight * env.getKeyYMarginFactor());
    
public booleangetNewlyLoadedFlag()

        return mNewlyLoadedFlag;
    
private android.graphics.RectgetPadding()

        mTmpRect.set(0, 0, 0, 0);
        Drawable skbBg = getSkbBackground();
        if (null == skbBg) return mTmpRect;
        skbBg.getPadding(mTmpRect);
        return mTmpRect;
    
public android.graphics.drawable.DrawablegetPopupBackground()

        if (null != mPopupBg) return mPopupBg;
        return mSkbTemplate.getPopupBackground();
    
public intgetRowNum()

        if (null != mKeyRows) {
            return mKeyRows.size();
        }
        return 0;
    
public android.graphics.drawable.DrawablegetSkbBackground()

        if (null != mSkbBg) return mSkbBg;
        return mSkbTemplate.getSkbBackground();
    
public intgetSkbCoreHeight()

        return mSkbCoreHeight;
    
public intgetSkbCoreWidth()

        return mSkbCoreWidth;
    
public intgetSkbTotalHeight()

        Rect padding = getPadding();
        return mSkbCoreHeight + padding.top + padding.bottom;
    
public intgetSkbTotalWidth()

        Rect padding = getPadding();
        return mSkbCoreWidth + padding.left + padding.right;
    
public intgetSkbXmlId()

        return mSkbXmlId;
    
public booleangetStickyFlag()

        return mStickyFlag;
    
public SoftKeymapToKey(int x, int y)

        if (null == mKeyRows) {
            return null;
        }
        // If the position is inside the rectangle of a certain key, return that
        // key.
        int rowNum = mKeyRows.size();
        for (int row = 0; row < rowNum; row++) {
            KeyRow keyRow = mKeyRows.get(row);
            if (KeyRow.ALWAYS_SHOW_ROW_ID != keyRow.mRowId
                    && keyRow.mRowId != mEnabledRowId) continue;
            if (keyRow.mTop > y && keyRow.mBottom <= y) continue;

            List<SoftKey> softKeys = keyRow.mSoftKeys;
            int keyNum = softKeys.size();
            for (int i = 0; i < keyNum; i++) {
                SoftKey sKey = softKeys.get(i);
                if (sKey.mLeft <= x && sKey.mTop <= y && sKey.mRight > x
                        && sKey.mBottom > y) {
                    return sKey;
                }
            }
        }

        // If the position is outside the rectangles of all keys, find the
        // nearest one.
        SoftKey nearestKey = null;
        float nearestDis = Float.MAX_VALUE;
        for (int row = 0; row < rowNum; row++) {
            KeyRow keyRow = mKeyRows.get(row);
            if (KeyRow.ALWAYS_SHOW_ROW_ID != keyRow.mRowId
                    && keyRow.mRowId != mEnabledRowId) continue;
            if (keyRow.mTop > y && keyRow.mBottom <= y) continue;

            List<SoftKey> softKeys = keyRow.mSoftKeys;
            int keyNum = softKeys.size();
            for (int i = 0; i < keyNum; i++) {
                SoftKey sKey = softKeys.get(i);
                int disx = (sKey.mLeft + sKey.mRight) / 2 - x;
                int disy = (sKey.mTop + sKey.mBottom) / 2 - y;
                float dis = disx * disx + disy * disy;
                if (dis < nearestDis) {
                    nearestDis = dis;
                    nearestKey = sKey;
                }
            }
        }
        return nearestKey;
    
public voidreset()

        if (null != mKeyRows) mKeyRows.clear();
    
public voidsetCacheId(int cacheId)

        mCacheId = cacheId;
    
public voidsetFlags(boolean cacheFlag, boolean stickyFlag, boolean isQwerty, boolean isQwertyUpperCase)

        mCacheFlag = cacheFlag;
        mStickyFlag = stickyFlag;
        mIsQwerty = isQwerty;
        mIsQwertyUpperCase = isQwertyUpperCase;
    
public voidsetKeyBalloonBackground(android.graphics.drawable.Drawable balloonBg)

        mBalloonBg = balloonBg;
    
public voidsetKeyMargins(float xMargin, float yMargin)

        mKeyXMargin = xMargin;
        mKeyYMargin = yMargin;
    
public voidsetNewlyLoadedFlag(boolean newlyLoadedFlag)

        mNewlyLoadedFlag = newlyLoadedFlag;
    
public voidsetPopupBackground(android.graphics.drawable.Drawable popupBg)

        mPopupBg = popupBg;
    
public voidsetSkbBackground(android.graphics.drawable.Drawable skbBg)

        mSkbBg = skbBg;
    
public voidsetSkbCoreSize(int skbCoreWidth, int skbCoreHeight)

        if (null == mKeyRows
                || (skbCoreWidth == mSkbCoreWidth && skbCoreHeight == mSkbCoreHeight)) {
            return;
        }
        for (int row = 0; row < mKeyRows.size(); row++) {
            KeyRow keyRow = mKeyRows.get(row);
            keyRow.mBottom = (int) (skbCoreHeight * keyRow.mBottomF);
            keyRow.mTop = (int) (skbCoreHeight * keyRow.mTopF);

            List<SoftKey> softKeys = keyRow.mSoftKeys;
            for (int i = 0; i < softKeys.size(); i++) {
                SoftKey softKey = softKeys.get(i);
                softKey.setSkbCoreSize(skbCoreWidth, skbCoreHeight);
            }
        }
        mSkbCoreWidth = skbCoreWidth;
        mSkbCoreHeight = skbCoreHeight;
    
public voidswitchQwertyMode(int toggle_state_id, boolean upperCase)

        if (!mIsQwerty) return;

        int rowNum = mKeyRows.size();
        for (int row = 0; row < rowNum; row++) {
            KeyRow keyRow = mKeyRows.get(row);
            List<SoftKey> softKeys = keyRow.mSoftKeys;
            int keyNum = softKeys.size();
            for (int i = 0; i < keyNum; i++) {
                SoftKey sKey = softKeys.get(i);
                if (sKey instanceof SoftKeyToggle) {
                    ((SoftKeyToggle) sKey).enableToggleState(toggle_state_id,
                            true);
                }
                if (sKey.mKeyCode >= KeyEvent.KEYCODE_A
                        && sKey.mKeyCode <= KeyEvent.KEYCODE_Z) {
                    sKey.changeCase(upperCase);
                }
            }
        }
    
public java.lang.StringtoShortString()

        return super.toString();
    
public java.lang.StringtoString()

        String str = "------------------SkbInfo----------------------\n";
        String endStr = "-----------------------------------------------\n";
        str += "Width: " + String.valueOf(mSkbCoreWidth) + "\n";
        str += "Height: " + String.valueOf(mSkbCoreHeight) + "\n";
        str += "KeyRowNum: " + mKeyRows == null ? "0" : String.valueOf(mKeyRows
                .size())
                + "\n";
        if (null == mKeyRows) return str + endStr;
        int rowNum = mKeyRows.size();
        for (int row = 0; row < rowNum; row++) {
            KeyRow keyRow = mKeyRows.get(row);
            List<SoftKey> softKeys = keyRow.mSoftKeys;
            int keyNum = softKeys.size();
            for (int i = 0; i < softKeys.size(); i++) {
                str += "-key " + String.valueOf(i) + ":"
                        + softKeys.get(i).toString();
            }
        }
        return str + endStr;