diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java | 166 |
1 files changed, 81 insertions, 85 deletions
diff --git a/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java b/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java index 5a015e93b..4e264e853 100644 --- a/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java +++ b/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java @@ -16,6 +16,8 @@ package com.android.inputmethod.latin; +import com.android.inputmethod.latin.BaseKeyboard.Key; + import android.content.Context; import android.content.pm.PackageManager; import android.content.res.Resources; @@ -29,8 +31,6 @@ import android.graphics.Rect; import android.graphics.Region.Op; import android.graphics.Typeface; import android.graphics.drawable.Drawable; -import android.inputmethodservice.Keyboard; -import android.inputmethodservice.Keyboard.Key; import android.os.Handler; import android.os.Message; import android.os.SystemClock; @@ -43,6 +43,7 @@ import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup.LayoutParams; +import android.view.WindowManager; import android.widget.PopupWindow; import android.widget.TextView; @@ -161,7 +162,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx // Miscellaneous constants /* package */ static final int NOT_A_KEY = -1; private static final int[] LONG_PRESSABLE_STATE_SET = { android.R.attr.state_long_pressable }; - private static final int NUMBER_HINT_VERTICAL_ADJUSTMENT_PIXEL = -1; + private static final int HINT_ICON_VERTICAL_ADJUSTMENT_PIXEL = -1; // XML attribute private int mKeyTextSize; @@ -180,12 +181,13 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx private int mPopupLayout; // Main keyboard - private Keyboard mKeyboard; + private BaseKeyboard mKeyboard; private Key[] mKeys; // TODO this attribute should be gotten from Keyboard. private int mKeyboardVerticalGap; // Key preview popup + private boolean mInForeground; private TextView mPreviewText; private PopupWindow mPreviewPopup; private int mPreviewTextSizeLarge; @@ -226,7 +228,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx protected KeyDetector mKeyDetector = new ProximityKeyDetector(); // Swipe gesture detector - private final GestureDetector mGestureDetector; + private GestureDetector mGestureDetector; private final SwipeTracker mSwipeTracker = new SwipeTracker(); private final int mSwipeThreshold; private final boolean mDisambiguateSwipe; @@ -575,11 +577,11 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx /** * Attaches a keyboard to this view. The keyboard can be switched at any time and the * view will re-layout itself to accommodate the keyboard. - * @see Keyboard + * @see BaseKeyboard * @see #getKeyboard() * @param keyboard the keyboard to display in this view */ - public void setKeyboard(Keyboard keyboard) { + public void setKeyboard(BaseKeyboard keyboard) { if (mKeyboard != null) { dismissKeyPreview(); } @@ -592,7 +594,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx -getPaddingTop() + mVerticalCorrection); mKeyboardVerticalGap = (int)getResources().getDimension(R.dimen.key_bottom_gap); for (PointerTracker tracker : mPointerTrackers) { - tracker.setKeyboard(mKeys, mKeyHysteresisDistance); + tracker.setKeyboard(keyboard, mKeys, mKeyHysteresisDistance); } requestLayout(); // Hint to reallocate the buffer if the size changed @@ -605,9 +607,9 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx /** * Returns the current keyboard being displayed by this view. * @return the currently attached keyboard - * @see #setKeyboard(Keyboard) + * @see #setKeyboard(BaseKeyboard) */ - public Keyboard getKeyboard() { + public BaseKeyboard getKeyboard() { return mKeyboard; } @@ -727,7 +729,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx * the touch distance from a key's center to avoid taking a square root. * @param keyboard */ - private void computeProximityThreshold(Keyboard keyboard) { + private void computeProximityThreshold(BaseKeyboard keyboard) { if (keyboard == null) return; final Key[] keys = mKeys; if (keys == null) return; @@ -816,8 +818,19 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx canvas.translate(key.x + kbdPaddingLeft, key.y + kbdPaddingTop); keyBackground.draw(canvas); - boolean shouldDrawIcon = true; + boolean drawHintIcon = true; if (label != null) { + // If keyboard is multi-touch capable and in temporary upper case state and key has + // tempoarary shift label, label should be hint character and hint icon should not + // be drawn. + if (mHasDistinctMultitouch + && mKeyboard instanceof LatinKeyboard + && ((LatinKeyboard)mKeyboard).isTemporaryUpperCase() + && key.temporaryShiftLabel != null) { + label = key.temporaryShiftLabel.toString(); + drawHintIcon = false; + } + // For characters, use large font. For labels like "Done", use small font. final int labelSize; if (label.length() > 1 && key.codes.length < 2) { @@ -849,32 +862,21 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx canvas.drawText(label, centerX, baseline, paint); // Turn off drop shadow paint.setShadowLayer(0, 0, 0, 0); + } + if (key.label == null && key.icon != null) { + int drawableWidth = key.icon.getIntrinsicWidth(); + int drawableHeight = key.icon.getIntrinsicHeight(); + int drawableX = (key.width + padding.left - padding.right - drawableWidth) / 2; + int drawableY = (key.height + padding.top - padding.bottom - drawableHeight) / 2; + drawIcon(canvas, key.icon, drawableX, drawableY, drawableWidth, drawableHeight); - // Usually don't draw icon if label is not null, but we draw icon for the number - // hint and popup hint. - shouldDrawIcon = shouldDrawLabelAndIcon(key); } - if (key.icon != null && shouldDrawIcon) { - // Special handing for the upper-right number hint icons - final int drawableWidth; - final int drawableHeight; - final int drawableX; - final int drawableY; - if (shouldDrawIconFully(key)) { - drawableWidth = key.width; - drawableHeight = key.height; - drawableX = 0; - drawableY = NUMBER_HINT_VERTICAL_ADJUSTMENT_PIXEL; - } else { - drawableWidth = key.icon.getIntrinsicWidth(); - drawableHeight = key.icon.getIntrinsicHeight(); - drawableX = (key.width + padding.left - padding.right - drawableWidth) / 2; - drawableY = (key.height + padding.top - padding.bottom - drawableHeight) / 2; - } - canvas.translate(drawableX, drawableY); - key.icon.setBounds(0, 0, drawableWidth, drawableHeight); - key.icon.draw(canvas); - canvas.translate(-drawableX, -drawableY); + if (key.hintIcon != null && drawHintIcon) { + int drawableWidth = key.width; + int drawableHeight = key.height; + int drawableX = 0; + int drawableY = HINT_ICON_VERTICAL_ADJUSTMENT_PIXEL; + drawIcon(canvas, key.hintIcon, drawableX, drawableY, drawableWidth, drawableHeight); } canvas.translate(-key.x - kbdPaddingLeft, -key.y - kbdPaddingTop); } @@ -908,6 +910,17 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx mDirtyRect.setEmpty(); } + private void drawIcon(Canvas canvas, Drawable icon, int x, int y, int width, int height) { + canvas.translate(x, y); + icon.setBounds(0, 0, width, height); + icon.draw(canvas); + canvas.translate(-x, -y); + } + + public void setForeground(boolean foreground) { + mInForeground = foreground; + } + // TODO: clean up this method. private void dismissKeyPreview() { for (PointerTracker tracker : mPointerTrackers) @@ -938,16 +951,20 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx } } + // TODO Must fix popup preview on xlarge layout private void showKey(final int keyIndex, PointerTracker tracker) { Key key = tracker.getKey(keyIndex); - if (key == null) + // If keyIndex is invalid or IME is already closed, we must not show key preview. + // Trying to show preview PopupWindow while root window is closed causes + // WindowManager.BadTokenException. + if (key == null || !mInForeground) return; - // Should not draw hint icon in key preview - if (key.icon != null && !shouldDrawLabelAndIcon(key)) { + if (key.icon != null) { mPreviewText.setCompoundDrawables(null, null, null, key.iconPreview != null ? key.iconPreview : key.icon); mPreviewText.setText(null); } else { + // TODO Should take care of temporaryShiftLabel here. mPreviewText.setCompoundDrawables(null, null, null, null); mPreviewText.setText(adjustCase(tracker.getPreviewText(key))); if (key.label.length() > 1 && key.codes.length < 2) { @@ -1000,13 +1017,18 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx popupPreviewY += popupHeight; } - if (mPreviewPopup.isShowing()) { - mPreviewPopup.update(popupPreviewX, popupPreviewY, popupWidth, popupHeight); - } else { - mPreviewPopup.setWidth(popupWidth); - mPreviewPopup.setHeight(popupHeight); - mPreviewPopup.showAtLocation(mMiniKeyboardParent, Gravity.NO_GRAVITY, - popupPreviewX, popupPreviewY); + try { + if (mPreviewPopup.isShowing()) { + mPreviewPopup.update(popupPreviewX, popupPreviewY, popupWidth, popupHeight); + } else { + mPreviewPopup.setWidth(popupWidth); + mPreviewPopup.setHeight(popupHeight); + mPreviewPopup.showAtLocation(mMiniKeyboardParent, Gravity.NO_GRAVITY, + popupPreviewX, popupPreviewY); + } + } catch (WindowManager.BadTokenException e) { + // Swallow the exception which will be happened when IME is already closed. + Log.w(TAG, "LatinIME is already closed when tried showing key preview."); } // Record popup preview position to display mini-keyboard later at the same positon mPopupPreviewDisplayedY = popupPreviewY; @@ -1029,7 +1051,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx * Invalidates a key so that it will be redrawn on the next repaint. Use this method if only * one key is changing it's content. Any changes that affect the position or size of the key * may not be honored. - * @param key key in the attached {@link Keyboard}. + * @param key key in the attached {@link BaseKeyboard}. * @see #invalidateAllKeys */ public void invalidateKey(Key key) { @@ -1106,13 +1128,15 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx }); // Override default ProximityKeyDetector. miniKeyboard.mKeyDetector = new MiniKeyboardKeyDetector(mMiniKeyboardSlideAllowance); + // Remove gesture detector on mini-keyboard + miniKeyboard.mGestureDetector = null; - Keyboard keyboard; + BaseKeyboard keyboard; if (popupKey.popupCharacters != null) { - keyboard = new Keyboard(getContext(), popupKeyboardId, popupKey.popupCharacters, + keyboard = new BaseKeyboard(getContext(), popupKeyboardId, popupKey.popupCharacters, -1, getPaddingLeft() + getPaddingRight()); } else { - keyboard = new Keyboard(getContext(), popupKeyboardId); + keyboard = new BaseKeyboard(getContext(), popupKeyboardId); } miniKeyboard.setKeyboard(keyboard); miniKeyboard.setPopupParent(this); @@ -1132,7 +1156,8 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx // and bottom edge flags on. // When you want to use one row mini-keyboard from xml file, make sure that the row has // both top and bottom edge flags set. - return (edgeFlags & Keyboard.EDGE_TOP) != 0 && (edgeFlags & Keyboard.EDGE_BOTTOM) != 0; + return (edgeFlags & BaseKeyboard.EDGE_TOP) != 0 + && (edgeFlags & BaseKeyboard.EDGE_BOTTOM) != 0; } /** @@ -1226,29 +1251,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx return false; } - private boolean shouldDrawIconFully(Key key) { - return isNumberAtEdgeOfPopupChars(key) || isLatinF1Key(key) - || LatinKeyboard.hasPuncOrSmileysPopup(key); - } - - private boolean shouldDrawLabelAndIcon(Key key) { - return isNumberAtEdgeOfPopupChars(key) || isNonMicLatinF1Key(key) - || LatinKeyboard.hasPuncOrSmileysPopup(key); - } - - private boolean isLatinF1Key(Key key) { - return (mKeyboard instanceof LatinKeyboard) && ((LatinKeyboard)mKeyboard).isF1Key(key); - } - - private boolean isNonMicLatinF1Key(Key key) { - return isLatinF1Key(key) && key.label != null; - } - - private static boolean isNumberAtEdgeOfPopupChars(Key key) { - return isNumberAtLeftmostPopupChar(key) || isNumberAtRightmostPopupChar(key); - } - - /* package */ static boolean isNumberAtLeftmostPopupChar(Key key) { + private static boolean isNumberAtLeftmostPopupChar(Key key) { if (key.popupCharacters != null && key.popupCharacters.length() > 0 && isAsciiDigit(key.popupCharacters.charAt(0))) { return true; @@ -1256,14 +1259,6 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx return false; } - /* package */ static boolean isNumberAtRightmostPopupChar(Key key) { - if (key.popupCharacters != null && key.popupCharacters.length() > 0 - && isAsciiDigit(key.popupCharacters.charAt(key.popupCharacters.length() - 1))) { - return true; - } - return false; - } - private static boolean isAsciiDigit(char c) { return (c < 0x80) && Character.isDigit(c); } @@ -1283,7 +1278,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx final PointerTracker tracker = new PointerTracker(i, mHandler, mKeyDetector, this, getResources()); if (keys != null) - tracker.setKeyboard(keys, mKeyHysteresisDistance); + tracker.setKeyboard(mKeyboard, keys, mKeyHysteresisDistance); if (listener != null) tracker.setOnKeyboardActionListener(listener); pointers.add(tracker); @@ -1307,8 +1302,9 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx // Track the last few movements to look for spurious swipes. mSwipeTracker.addMovement(me); - // We must disable gesture detector while mini-keyboard is on the screen. - if (mMiniKeyboard == null && mGestureDetector.onTouchEvent(me)) { + // Gesture detector must be enabled only when mini-keyboard is not on the screen. + if (mMiniKeyboard == null + && mGestureDetector != null && mGestureDetector.onTouchEvent(me)) { dismissKeyPreview(); mHandler.cancelKeyTimers(); return true; |