diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin/LatinKeyboard.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/LatinKeyboard.java | 259 |
1 files changed, 69 insertions, 190 deletions
diff --git a/java/src/com/android/inputmethod/latin/LatinKeyboard.java b/java/src/com/android/inputmethod/latin/LatinKeyboard.java index e33ae145b..e10346570 100644 --- a/java/src/com/android/inputmethod/latin/LatinKeyboard.java +++ b/java/src/com/android/inputmethod/latin/LatinKeyboard.java @@ -30,16 +30,16 @@ import android.graphics.PorterDuff; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; -import android.inputmethodservice.Keyboard; import android.text.TextPaint; import android.util.Log; import android.view.ViewConfiguration; import android.view.inputmethod.EditorInfo; +import java.util.HashMap; import java.util.List; import java.util.Locale; -public class LatinKeyboard extends Keyboard { +public class LatinKeyboard extends BaseKeyboard { private static final boolean DEBUG_PREFERRED_LETTER = false; private static final String TAG = "LatinKeyboard"; @@ -48,7 +48,7 @@ public class LatinKeyboard extends Keyboard { private Drawable mShiftLockIcon; private Drawable mShiftLockPreviewIcon; - private Drawable mOldShiftIcon; + private final HashMap<Key, Drawable> mOldShiftIcons = new HashMap<Key, Drawable>(); private Drawable mSpaceIcon; private Drawable mSpaceAutoCompletionIndicator; private Drawable mSpacePreviewIcon; @@ -58,15 +58,10 @@ public class LatinKeyboard extends Keyboard { private Drawable m123MicPreviewIcon; private final Drawable mButtonArrowLeftIcon; private final Drawable mButtonArrowRightIcon; - private Key mShiftKey; private Key mEnterKey; private Key mF1Key; - private Drawable mF1HintIcon; private Key mSpaceKey; private Key m123Key; - private final int NUMBER_HINT_COUNT = 10; - private Key[] mNumberHintKeys; - private Drawable[] mNumberHintIcons = new Drawable[NUMBER_HINT_COUNT]; private int mSpaceKeyIndex = -1; private int mSpaceDragStartX; private int mSpaceDragLastDiff; @@ -91,10 +86,6 @@ public class LatinKeyboard extends Keyboard { // TODO: generalize for any keyboardId private boolean mIsBlackSym; - // TODO: remove this attribute when either Keyboard.mDefaultVerticalGap or Key.parent becomes - // non-private. - private final int mVerticalGap; - private static final int SHIFT_OFF = 0; private static final int SHIFT_ON = 1; private static final int SHIFT_LOCKED = 2; @@ -136,30 +127,12 @@ public class LatinKeyboard extends Keyboard { mButtonArrowRightIcon = res.getDrawable(R.drawable.sym_keyboard_language_arrows_right); m123MicIcon = res.getDrawable(R.drawable.sym_keyboard_123_mic); m123MicPreviewIcon = res.getDrawable(R.drawable.sym_keyboard_feedback_123_mic); - mF1HintIcon = res.getDrawable(R.drawable.hint_settings); setDefaultBounds(m123MicPreviewIcon); sSpacebarVerticalCorrection = res.getDimensionPixelOffset( R.dimen.spacebar_vertical_correction); mIsAlphaKeyboard = xmlLayoutResId == R.xml.kbd_qwerty || xmlLayoutResId == R.xml.kbd_qwerty_black; mSpaceKeyIndex = indexOf(LatinIME.KEYCODE_SPACE); - initializeNumberHintResources(context); - // TODO remove this initialization after cleanup - mVerticalGap = super.getVerticalGap(); - } - - private void initializeNumberHintResources(Context context) { - final Resources res = context.getResources(); - mNumberHintIcons[0] = res.getDrawable(R.drawable.keyboard_hint_0); - mNumberHintIcons[1] = res.getDrawable(R.drawable.keyboard_hint_1); - mNumberHintIcons[2] = res.getDrawable(R.drawable.keyboard_hint_2); - mNumberHintIcons[3] = res.getDrawable(R.drawable.keyboard_hint_3); - mNumberHintIcons[4] = res.getDrawable(R.drawable.keyboard_hint_4); - mNumberHintIcons[5] = res.getDrawable(R.drawable.keyboard_hint_5); - mNumberHintIcons[6] = res.getDrawable(R.drawable.keyboard_hint_6); - mNumberHintIcons[7] = res.getDrawable(R.drawable.keyboard_hint_7); - mNumberHintIcons[8] = res.getDrawable(R.drawable.keyboard_hint_8); - mNumberHintIcons[9] = res.getDrawable(R.drawable.keyboard_hint_9); } @Override @@ -182,143 +155,105 @@ public class LatinKeyboard extends Keyboard { break; } - // For number hints on the upper-right corner of key - if (mNumberHintKeys == null) { - // NOTE: This protected method is being called from the base class constructor before - // mNumberHintKeys gets initialized. - mNumberHintKeys = new Key[NUMBER_HINT_COUNT]; - } - int hintNumber = -1; - if (LatinKeyboardBaseView.isNumberAtLeftmostPopupChar(key)) { - hintNumber = key.popupCharacters.charAt(0) - '0'; - } else if (LatinKeyboardBaseView.isNumberAtRightmostPopupChar(key)) { - hintNumber = key.popupCharacters.charAt(key.popupCharacters.length() - 1) - '0'; - } - if (hintNumber >= 0 && hintNumber <= 9) { - mNumberHintKeys[hintNumber] = key; - } - return key; } - void setImeOptions(Resources res, int mode, int options) { - // TODO should clean up this method + private static void resetKeyAttributes(Key key, CharSequence label) { + key.popupCharacters = null; + key.popupResId = 0; + key.text = null; + key.iconPreview = null; + key.icon = null; + key.label = label; + } + + public void setImeOptions(Resources res, int mode, int options) { if (mEnterKey != null) { - // Reset some of the rarely used attributes. - mEnterKey.popupCharacters = null; - mEnterKey.popupResId = 0; - mEnterKey.text = null; - switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) { + switch (options & (EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) { case EditorInfo.IME_ACTION_GO: - mEnterKey.iconPreview = null; - mEnterKey.icon = null; - mEnterKey.label = res.getText(R.string.label_go_key); + resetKeyAttributes(mEnterKey, res.getText(R.string.label_go_key)); break; case EditorInfo.IME_ACTION_NEXT: - mEnterKey.iconPreview = null; - mEnterKey.icon = null; - mEnterKey.label = res.getText(R.string.label_next_key); + resetKeyAttributes(mEnterKey, res.getText(R.string.label_next_key)); break; case EditorInfo.IME_ACTION_DONE: - mEnterKey.iconPreview = null; - mEnterKey.icon = null; - mEnterKey.label = res.getText(R.string.label_done_key); + resetKeyAttributes(mEnterKey, res.getText(R.string.label_done_key)); break; case EditorInfo.IME_ACTION_SEARCH: + resetKeyAttributes(mEnterKey, null); mEnterKey.iconPreview = res.getDrawable( R.drawable.sym_keyboard_feedback_search); mEnterKey.icon = res.getDrawable(mIsBlackSym ? R.drawable.sym_bkeyboard_search : R.drawable.sym_keyboard_search); - mEnterKey.label = null; break; case EditorInfo.IME_ACTION_SEND: - mEnterKey.iconPreview = null; - mEnterKey.icon = null; - mEnterKey.label = res.getText(R.string.label_send_key); - break; - default: - if (mode == KeyboardSwitcher.MODE_IM) { - mEnterKey.icon = null; - mEnterKey.iconPreview = null; - mEnterKey.label = ":-)"; - mEnterKey.text = ":-) "; - mEnterKey.popupResId = R.xml.popup_smileys; - } else { - mEnterKey.iconPreview = res.getDrawable( - R.drawable.sym_keyboard_feedback_return); - mEnterKey.icon = res.getDrawable(mIsBlackSym ? - R.drawable.sym_bkeyboard_return : R.drawable.sym_keyboard_return); - mEnterKey.label = null; - } + resetKeyAttributes(mEnterKey, res.getText(R.string.label_send_key)); break; } // Set the initial size of the preview icon - if (mEnterKey.iconPreview != null) { - setDefaultBounds(mEnterKey.iconPreview); - } + setDefaultBounds(mEnterKey.iconPreview); } } - - void enableShiftLock() { - int index = getShiftKeyIndex(); - if (index >= 0) { - mShiftKey = getKeys().get(index); - if (mShiftKey instanceof LatinKey) { - ((LatinKey)mShiftKey).enableShiftLock(); + + public void enableShiftLock() { + for (final Key key : getShiftKeys()) { + if (key instanceof LatinKey) { + ((LatinKey)key).enableShiftLock(); } - mOldShiftIcon = mShiftKey.icon; + mOldShiftIcons.put(key, key.icon); } } - void setShiftLocked(boolean shiftLocked) { - if (mShiftKey != null) { - if (shiftLocked) { - mShiftKey.on = true; - mShiftKey.icon = mShiftLockIcon; - mShiftState = SHIFT_LOCKED; - } else { - mShiftKey.on = false; - mShiftKey.icon = mShiftLockIcon; - mShiftState = SHIFT_ON; - } + public void setShiftLocked(boolean shiftLocked) { + for (final Key key : getShiftKeys()) { + key.on = shiftLocked; + key.icon = mShiftLockIcon; } + mShiftState = shiftLocked ? SHIFT_LOCKED : SHIFT_ON; } - boolean isShiftLocked() { + public boolean isShiftLocked() { return mShiftState == SHIFT_LOCKED; } - + @Override public boolean setShifted(boolean shiftState) { boolean shiftChanged = false; - if (mShiftKey != null) { + if (getShiftKeys().size() > 0) { + for (final Key key : getShiftKeys()) { + if (shiftState == false) { + key.on = false; + key.icon = mOldShiftIcons.get(key); + } else if (mShiftState == SHIFT_OFF) { + key.icon = mShiftLockIcon; + } + } if (shiftState == false) { shiftChanged = mShiftState != SHIFT_OFF; mShiftState = SHIFT_OFF; - mShiftKey.on = false; - mShiftKey.icon = mOldShiftIcon; - } else { - if (mShiftState == SHIFT_OFF) { - shiftChanged = mShiftState == SHIFT_OFF; - mShiftState = SHIFT_ON; - mShiftKey.icon = mShiftLockIcon; - } + } else if (mShiftState == SHIFT_OFF) { + shiftChanged = mShiftState == SHIFT_OFF; + mShiftState = SHIFT_ON; } + return shiftChanged; } else { return super.setShifted(shiftState); } - return shiftChanged; } @Override public boolean isShifted() { - if (mShiftKey != null) { + if (getShiftKeys().size() > 0) { return mShiftState != SHIFT_OFF; } else { return super.isShifted(); } } + public boolean isTemporaryUpperCase() { + return mIsAlphaKeyboard && isShifted() && !isShiftLocked(); + } + /* package */ boolean isAlphaKeyboard() { return mIsAlphaKeyboard; } @@ -336,25 +271,20 @@ public class LatinKeyboard extends Keyboard { mMicIcon = mRes.getDrawable(R.drawable.sym_keyboard_mic); m123MicIcon = mRes.getDrawable(R.drawable.sym_keyboard_123_mic); } - updateDynamicKeys(); + updateF1Key(); if (mSpaceKey != null) { updateSpaceBarForLocale(isAutoCompletion, isBlack); } - updateNumberHintKeys(); - } - - private void setDefaultBounds(Drawable drawable) { - drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); } public void setVoiceMode(boolean hasVoiceButton, boolean hasVoice) { mHasVoiceButton = hasVoiceButton; mVoiceEnabled = hasVoice; - updateDynamicKeys(); + updateF1Key(); } - private void updateDynamicKeys() { - // Update KEYCODE_MODE_CHANGE key only on alphabet mode, not on symbol mode. + private void updateF1Key() { + if (mF1Key == null) return; if (m123Key != null && mIsAlphaKeyboard) { if (mVoiceEnabled && !mHasVoiceButton) { m123Key.icon = m123MicIcon; @@ -367,26 +297,18 @@ public class LatinKeyboard extends Keyboard { } } - // Update KEYCODE_F1 key. Please note that some keyboard layout has no F1 key. - if (mF1Key != null) { - if (mHasVoiceButton && mVoiceEnabled) { - mF1Key.codes = new int[] { LatinKeyboardView.KEYCODE_VOICE }; - mF1Key.label = null; - // HACK: draw mMicIcon and mF1HintIcon at the same time - mF1Key.icon = new BitmapDrawable(mRes, drawSynthesizedSettingsHintImage( - mF1Key.width, mF1Key.height + mVerticalGap, mMicIcon, mF1HintIcon)); - mF1Key.iconPreview = mMicPreviewIcon; - mF1Key.popupResId = R.xml.popup_mic; - } else { - mF1Key.label = ","; - mF1Key.codes = new int[] { ',' }; - // HACK: draw only mF1HintIcon on offscreen buffer to adjust position of '...' to - // the above synthesized icon - mF1Key.icon = new BitmapDrawable(mRes, drawSynthesizedSettingsHintImage( - mF1Key.width, mF1Key.height + mVerticalGap, null, mF1HintIcon)); - mF1Key.iconPreview = null; - mF1Key.popupResId = R.xml.popup_comma; - } + if (mHasVoiceButton && mVoiceEnabled) { + mF1Key.codes = new int[] { LatinKeyboardView.KEYCODE_VOICE }; + mF1Key.label = null; + mF1Key.icon = mMicIcon; + mF1Key.iconPreview = mMicPreviewIcon; + mF1Key.popupResId = R.xml.popup_mic; + } else { + mF1Key.label = ","; + mF1Key.codes = new int[] { ',' }; + mF1Key.icon = null; + mF1Key.iconPreview = null; + mF1Key.popupResId = R.xml.popup_comma; } } @@ -398,14 +320,6 @@ public class LatinKeyboard extends Keyboard { return mSpaceKey; } - private void updateNumberHintKeys() { - for (int i = 0; i < mNumberHintKeys.length; ++i) { - if (mNumberHintKeys[i] != null) { - mNumberHintKeys[i].icon = mNumberHintIcons[i]; - } - } - } - public boolean isLanguageSwitchEnabled() { return mLocale != null; } @@ -434,29 +348,6 @@ public class LatinKeyboard extends Keyboard { return bounds.width(); } - // Overlay two images. Note that mainIcon can be null. - private Bitmap drawSynthesizedSettingsHintImage( - int width, int height, Drawable mainIcon, Drawable hintIcon) { - if (hintIcon == null) - return null; - final Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); - final Canvas canvas = new Canvas(buffer); - canvas.drawColor(mRes.getColor(R.color.latinkeyboard_transparent), PorterDuff.Mode.CLEAR); - // draw main icon at centered position - if (mainIcon != null) { - setDefaultBounds(mainIcon); - final int drawableX = (width - mainIcon.getIntrinsicWidth()) / 2; - final int drawableY = (height - mainIcon.getIntrinsicHeight()) / 2; - canvas.translate(drawableX, drawableY); - mainIcon.draw(canvas); - canvas.translate(-drawableX, -drawableY); - } - // draw hint icon fully in the key - hintIcon.setBounds(0, 0, width, height); - hintIcon.draw(canvas); - return buffer; - } - // Layout local language name and left and right arrow on space bar. private static String layoutSpaceBar(Paint paint, Locale locale, Drawable lArrow, Drawable rArrow, int width, int height, float origTextSize, @@ -772,8 +663,7 @@ public class LatinKeyboard extends Keyboard { return textSize; } - // TODO LatinKey could be static class - class LatinKey extends Keyboard.Key { + class LatinKey extends BaseKeyboard.Key { // functional normal state (with properties) private final int[] KEY_STATE_FUNCTIONAL_NORMAL = { @@ -788,7 +678,7 @@ public class LatinKeyboard extends Keyboard { private boolean mShiftLockEnabled; - public LatinKey(Resources res, Keyboard.Row parent, int x, int y, + public LatinKey(Resources res, BaseKeyboard.Row parent, int x, int y, XmlResourceParser parser) { super(res, parent, x, y, parser); if (popupCharacters != null && popupCharacters.length() == 0) { @@ -821,8 +711,6 @@ public class LatinKeyboard extends Keyboard { */ @Override public boolean isInside(int x, int y) { - // TODO This should be done by parent.isInside(this, x, y) - // if Key.parent were protected. boolean result = LatinKeyboard.this.isInside(this, x, y); return result; } @@ -842,15 +730,6 @@ public class LatinKeyboard extends Keyboard { } return super.getCurrentDrawableState(); } - - @Override - public int squaredDistanceFrom(int x, int y) { - // We should count vertical gap between rows to calculate the center of this Key. - final int verticalGap = LatinKeyboard.this.mVerticalGap; - final int xDist = this.x + width / 2 - x; - final int yDist = this.y + (height + verticalGap) / 2 - y; - return xDist * xDist + yDist * yDist; - } } /** |