aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/LatinKeyboard.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin/LatinKeyboard.java')
-rw-r--r--java/src/com/android/inputmethod/latin/LatinKeyboard.java369
1 files changed, 114 insertions, 255 deletions
diff --git a/java/src/com/android/inputmethod/latin/LatinKeyboard.java b/java/src/com/android/inputmethod/latin/LatinKeyboard.java
index d04fc586a..3ca85c5d0 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 final 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;
@@ -74,7 +69,6 @@ public class LatinKeyboard extends Keyboard {
private LanguageSwitcher mLanguageSwitcher;
private final Resources mRes;
private final Context mContext;
- private int mMode;
// Whether this keyboard has voice icon on it
private boolean mHasVoiceButton;
// Whether voice icon is enabled at all
@@ -89,13 +83,15 @@ public class LatinKeyboard extends Keyboard {
private int mPrefLetterY;
private int mPrefDistance;
+ // Default Enter key attributes
+ private final Drawable mDefaultEnterIcon;
+ private final Drawable mDefaultEnterPreview;
+ private final CharSequence mDefaultEnterLabel;
+ private final CharSequence mDefaultEnterText;
+
// 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;
@@ -123,7 +119,6 @@ public class LatinKeyboard extends Keyboard {
super(context, xmlLayoutResId, mode);
final Resources res = context.getResources();
mContext = context;
- mMode = mode;
mRes = res;
mShiftLockIcon = res.getDrawable(R.drawable.sym_keyboard_shift_locked);
mShiftLockPreviewIcon = res.getDrawable(R.drawable.sym_keyboard_feedback_shift_locked);
@@ -138,30 +133,22 @@ 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);
+ if (mEnterKey != null) {
+ mDefaultEnterIcon = mEnterKey.icon;
+ mDefaultEnterPreview = mEnterKey.iconPreview;
+ mDefaultEnterLabel = mEnterKey.label;
+ mDefaultEnterText = mEnterKey.text;
+ } else {
+ mDefaultEnterIcon = mDefaultEnterPreview = null;
+ mDefaultEnterLabel = mDefaultEnterText = null;
+ }
}
@Override
@@ -184,145 +171,111 @@ 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) {
- mMode = mode;
- // TODO should clean up this method
- 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)) {
- case EditorInfo.IME_ACTION_GO:
- mEnterKey.iconPreview = null;
- mEnterKey.icon = null;
- mEnterKey.label = 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);
- break;
- case EditorInfo.IME_ACTION_DONE:
- mEnterKey.iconPreview = null;
- mEnterKey.icon = null;
- mEnterKey.label = res.getText(R.string.label_done_key);
- break;
- case EditorInfo.IME_ACTION_SEARCH:
- 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;
- }
- break;
- }
- // Set the initial size of the preview icon
- if (mEnterKey.iconPreview != null) {
- setDefaultBounds(mEnterKey.iconPreview);
- }
+ 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)
+ return;
+ switch (options & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
+ case EditorInfo.IME_ACTION_GO:
+ resetKeyAttributes(mEnterKey, res.getText(R.string.label_go_key));
+ break;
+ case EditorInfo.IME_ACTION_NEXT:
+ resetKeyAttributes(mEnterKey, res.getText(R.string.label_next_key));
+ break;
+ case EditorInfo.IME_ACTION_DONE:
+ 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);
+ break;
+ case EditorInfo.IME_ACTION_SEND:
+ resetKeyAttributes(mEnterKey, res.getText(R.string.label_send_key));
+ break;
+ default:
+ resetKeyAttributes(mEnterKey, mDefaultEnterLabel);
+ mEnterKey.text = mDefaultEnterText;
+ mEnterKey.icon = mDefaultEnterIcon;
+ mEnterKey.iconPreview = mDefaultEnterPreview;
+ break;
}
+ // Set the initial size of the preview icon
+ 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();
}
}
- /* package */ boolean isAlphaKeyboard() {
+ public boolean isTemporaryUpperCase() {
+ return mIsAlphaKeyboard && isShifted() && !isShiftLocked();
+ }
+
+ public boolean isAlphaKeyboard() {
return mIsAlphaKeyboard;
}
@@ -339,30 +292,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();
- }
-
- private void updateDynamicKeys() {
- update123Key();
updateF1Key();
}
- private void update123Key() {
- // 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;
@@ -374,58 +317,22 @@ public class LatinKeyboard extends Keyboard {
m123Key.label = m123Label;
}
}
- }
-
- private void updateF1Key() {
- // Update KEYCODE_F1 key. Please note that some keyboard layouts have no F1 key.
- if (mF1Key == null)
- return;
- if (mIsAlphaKeyboard) {
- if (mMode == KeyboardSwitcher.MODE_URL) {
- setNonMicF1Key(mF1Key, "/", R.xml.popup_slash);
- } else if (mMode == KeyboardSwitcher.MODE_EMAIL) {
- setNonMicF1Key(mF1Key, "@", R.xml.popup_at);
- } else {
- if (mVoiceEnabled && mHasVoiceButton) {
- setMicF1Key(mF1Key);
- } else {
- setNonMicF1Key(mF1Key, ",", R.xml.popup_comma);
- }
- }
- } else { // Symbols keyboard
- if (mVoiceEnabled && mHasVoiceButton) {
- setMicF1Key(mF1Key);
- } else {
- setNonMicF1Key(mF1Key, ",", 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;
}
}
- private void setMicF1Key(Key key) {
- // HACK: draw mMicIcon and mF1HintIcon at the same time
- final Drawable micWithSettingsHintDrawable = new BitmapDrawable(mRes,
- drawSynthesizedSettingsHintImage(key.width, key.height, mMicIcon, mF1HintIcon));
-
- key.label = null;
- key.codes = new int[] { LatinKeyboardView.KEYCODE_VOICE };
- key.popupResId = R.xml.popup_mic;
- key.icon = micWithSettingsHintDrawable;
- key.iconPreview = mMicPreviewIcon;
- }
-
- private void setNonMicF1Key(Key key, String label, int popupResId) {
- key.label = label;
- key.codes = new int[] { label.charAt(0) };
- key.popupResId = popupResId;
- key.icon = mF1HintIcon;
- key.iconPreview = null;
- }
-
- public boolean isF1Key(Key key) {
- return key == mF1Key;
- }
-
/**
* @return a key which should be invalidated.
*/
@@ -434,14 +341,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;
}
@@ -470,34 +369,6 @@ public class LatinKeyboard extends Keyboard {
return bounds.width();
}
- // Overlay two images: mainIcon and hintIcon.
- private Bitmap drawSynthesizedSettingsHintImage(
- int width, int height, Drawable mainIcon, Drawable hintIcon) {
- if (mainIcon == null || hintIcon == null)
- return null;
- Rect hintIconPadding = new Rect(0, 0, 0, 0);
- hintIcon.getPadding(hintIconPadding);
- 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 the center of the key visual
- // Assuming the hintIcon shares the same padding with the key's background drawable
- final int drawableX = (width + hintIconPadding.left - hintIconPadding.right
- - mainIcon.getIntrinsicWidth()) / 2;
- final int drawableY = (height + hintIconPadding.top - hintIconPadding.bottom
- - mainIcon.getIntrinsicHeight()) / 2;
- setDefaultBounds(mainIcon);
- 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,
@@ -645,12 +516,12 @@ public class LatinKeyboard extends Keyboard {
return mCurrentlyInSpace;
}
- void setPreferredLetters(int[] frequencies) {
+ public void setPreferredLetters(int[] frequencies) {
mPrefLetterFrequencies = frequencies;
mPrefLetter = 0;
}
- void keyReleased() {
+ public void keyReleased() {
mCurrentlyInSpace = false;
mSpaceDragLastDiff = 0;
mPrefLetter = 0;
@@ -666,10 +537,9 @@ public class LatinKeyboard extends Keyboard {
* Does the magic of locking the touch gesture into the spacebar when
* switching input languages.
*/
- boolean isInside(LatinKey key, int x, int y) {
+ public boolean isInside(LatinKey key, int x, int y) {
final int code = key.codes[0];
- if (code == KEYCODE_SHIFT ||
- code == KEYCODE_DELETE) {
+ if (code == KEYCODE_SHIFT || code == KEYCODE_DELETE) {
y -= key.height / 10;
if (code == KEYCODE_SHIFT) x += key.width / 6;
if (code == KEYCODE_DELETE) x -= key.width / 6;
@@ -813,8 +683,7 @@ public class LatinKeyboard extends Keyboard {
return textSize;
}
- // TODO LatinKey could be static class
- class LatinKey extends Keyboard.Key {
+ public static class LatinKey extends BaseKeyboard.Key {
// functional normal state (with properties)
private final int[] KEY_STATE_FUNCTIONAL_NORMAL = {
@@ -829,7 +698,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) {
@@ -862,13 +731,12 @@ 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);
+ boolean result = (keyboard instanceof LatinKeyboard)
+ && ((LatinKeyboard)keyboard).isInside(this, x, y);
return result;
}
- boolean isInsideSuper(int x, int y) {
+ private boolean isInsideSuper(int x, int y) {
return super.isInside(x, y);
}
@@ -883,15 +751,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;
- }
}
/**
@@ -899,7 +758,7 @@ public class LatinKeyboard extends Keyboard {
* languages by swiping the spacebar. It draws the current, previous and
* next languages and moves them by the delta of touch movement on the spacebar.
*/
- class SlidingLocaleDrawable extends Drawable {
+ private class SlidingLocaleDrawable extends Drawable {
private final int mWidth;
private final int mHeight;