aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java')
-rw-r--r--java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java172
1 files changed, 68 insertions, 104 deletions
diff --git a/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java b/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java
index c1b1008b0..83b761904 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;
@@ -47,7 +47,6 @@ import android.widget.PopupWindow;
import android.widget.TextView;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.WeakHashMap;
@@ -159,7 +158,6 @@ 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;
// XML attribute
private int mKeyTextSize;
@@ -178,10 +176,8 @@ 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 TextView mPreviewText;
@@ -244,11 +240,6 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx
private final Paint mPaint;
private final Rect mPadding;
private final Rect mClipRegion = new Rect(0, 0, 0, 0);
- // This map caches key label text height in pixel as value and key label text size as map key.
- private final HashMap<Integer, Integer> mTextHeightCache = new HashMap<Integer, Integer>();
- // Distance from horizontal center of the key, proportional to key label text height.
- private final float KEY_LABEL_VERTICAL_ADJUSTMENT_FACTOR = 0.55f;
- private final String KEY_LABEL_HEIGHT_REFERENCE_CHAR = "H";
private final UIHandler mHandler = new UIHandler();
@@ -477,7 +468,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx
mPreviewPopup = new PopupWindow(context);
if (previewLayout != 0) {
mPreviewText = (TextView) inflate.inflate(previewLayout, null);
- mPreviewTextSizeLarge = (int) res.getDimension(R.dimen.key_preview_text_size_large);
+ mPreviewTextSizeLarge = (int) mPreviewText.getTextSize();
mPreviewPopup.setContentView(mPreviewText);
mPreviewPopup.setBackgroundDrawable(null);
} else {
@@ -577,7 +568,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx
* @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();
}
@@ -588,9 +579,8 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx
LatinImeLogger.onSetKeyboard(keyboard);
mKeys = mKeyDetector.setKeyboard(keyboard, -getPaddingLeft(),
-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,7 +595,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx
* @return the currently attached keyboard
* @see #setKeyboard(Keyboard)
*/
- public Keyboard getKeyboard() {
+ public BaseKeyboard getKeyboard() {
return mKeyboard;
}
@@ -725,7 +715,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;
@@ -733,7 +723,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx
int dimensionSum = 0;
for (int i = 0; i < length; i++) {
Key key = keys[i];
- dimensionSum += Math.min(key.width, key.height + mKeyboardVerticalGap) + key.gap;
+ dimensionSum += Math.min(key.width, key.height) + key.gap;
}
if (dimensionSum < 0 || length == 0) return;
mKeyDetector.setProximityThreshold((int) (dimensionSum * 1.4f / length));
@@ -785,14 +775,13 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx
paint.setColor(mKeyTextColor);
boolean drawSingleKey = false;
if (invalidKey != null && canvas.getClipBounds(clipRegion)) {
- // TODO we should use Rect.inset and Rect.contains here.
- // Is clipRegion completely contained within the invalidated key?
- if (invalidKey.x + kbdPaddingLeft - 1 <= clipRegion.left &&
- invalidKey.y + kbdPaddingTop - 1 <= clipRegion.top &&
- invalidKey.x + invalidKey.width + kbdPaddingLeft + 1 >= clipRegion.right &&
- invalidKey.y + invalidKey.height + kbdPaddingTop + 1 >= clipRegion.bottom) {
- drawSingleKey = true;
- }
+ // Is clipRegion completely contained within the invalidated key?
+ if (invalidKey.x + kbdPaddingLeft - 1 <= clipRegion.left &&
+ invalidKey.y + kbdPaddingTop - 1 <= clipRegion.top &&
+ invalidKey.x + invalidKey.width + kbdPaddingLeft + 1 >= clipRegion.right &&
+ invalidKey.y + invalidKey.height + kbdPaddingTop + 1 >= clipRegion.bottom) {
+ drawSingleKey = true;
+ }
}
canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
final int keyCount = keys.length;
@@ -808,72 +797,65 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx
String label = key.label == null? null : adjustCase(key.label).toString();
final Rect bounds = keyBackground.getBounds();
- if (key.width != bounds.right || key.height != bounds.bottom) {
+ if (key.width != bounds.right ||
+ key.height != bounds.bottom) {
keyBackground.setBounds(0, 0, key.width, key.height);
}
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) {
- labelSize = mLabelTextSize;
+ paint.setTextSize(mLabelTextSize);
paint.setTypeface(Typeface.DEFAULT_BOLD);
} else {
- labelSize = mKeyTextSize;
+ paint.setTextSize(mKeyTextSize);
paint.setTypeface(mKeyTextStyle);
}
- paint.setTextSize(labelSize);
-
- Integer labelHeightValue = mTextHeightCache.get(labelSize);
- final int labelHeight;
- if (labelHeightValue != null) {
- labelHeight = labelHeightValue;
- } else {
- Rect textBounds = new Rect();
- paint.getTextBounds(KEY_LABEL_HEIGHT_REFERENCE_CHAR, 0, 1, textBounds);
- labelHeight = textBounds.height();
- mTextHeightCache.put(labelSize, labelHeight);
- }
-
// Draw a drop shadow for the text
paint.setShadowLayer(mShadowRadius, 0, 0, mShadowColor);
- final int centerX = (key.width + padding.left - padding.right) / 2;
- final int centerY = (key.height + padding.top - padding.bottom) / 2;
- final float baseline = centerY
- + labelHeight * KEY_LABEL_VERTICAL_ADJUSTMENT_FACTOR;
- canvas.drawText(label, centerX, baseline, paint);
+ // Draw the text
+ canvas.drawText(label,
+ (key.width - padding.left - padding.right) / 2
+ + padding.left,
+ (key.height - padding.top - padding.bottom) / 2
+ + (paint.getTextSize() - paint.descent()) / 2 + padding.top,
+ paint);
// Turn off drop shadow
paint.setShadowLayer(0, 0, 0, 0);
-
- // Usually don't draw icon if label is not null, but we draw icon for the number
- // hint.
- shouldDrawIcon = isCommaKeyLabelOrNumberAtEdgeOfPopupChars(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 (isNumberAtEdgeOfPopupChars(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 + padding.left;
- drawableY = (key.height - padding.top - padding.bottom - drawableHeight)
- / 2 + padding.top;
- }
+ Drawable icon = null;
+ if (key.label == null && key.icon != null)
+ icon = key.icon;
+ if (icon == null && key.hintIcon != null && drawHintIcon)
+ icon = key.hintIcon;
+ if (icon != null) {
+ // Hack for key hint icon displaying at the top right corner of the key.
+ final int drawableWidth = icon == key.hintIcon
+ ? key.width : icon.getIntrinsicWidth();
+ final int drawableHeight = icon == key.hintIcon
+ ? key.height : icon.getIntrinsicHeight();
+
+ final int drawableX = (key.width - padding.left - padding.right
+ - drawableWidth) / 2 + padding.left;
+ final int drawableY = (key.height - padding.top - padding.bottom
+ - drawableHeight) / 2 + padding.top;
canvas.translate(drawableX, drawableY);
- key.icon.setBounds(0, 0, drawableWidth, drawableHeight);
- key.icon.draw(canvas);
+ icon.setBounds(0, 0, drawableWidth, drawableHeight);
+ icon.draw(canvas);
canvas.translate(-drawableX, -drawableY);
}
canvas.translate(-key.x - kbdPaddingLeft, -key.y - kbdPaddingTop);
@@ -938,16 +920,18 @@ 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)
return;
// Should not draw number hint icons
- if (key.icon != null && !isCommaKeyLabelOrNumberAtEdgeOfPopupChars(key)) {
+ if (key.icon != null && key.label == 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) {
@@ -1029,14 +1013,13 @@ 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) {
if (key == null)
return;
mInvalidatedKey = key;
- // TODO we should clean up this and record key's region to use in onBufferDraw.
mDirtyRect.union(key.x + getPaddingLeft(), key.y + getPaddingTop(),
key.x + key.width + getPaddingLeft(), key.y + key.height + getPaddingTop());
onBufferDraw();
@@ -1107,12 +1090,12 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx
// Override default ProximityKeyDetector.
miniKeyboard.mKeyDetector = new MiniKeyboardKeyDetector(mMiniKeyboardSlideAllowance);
- 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 +1115,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,19 +1210,7 @@ public class LatinKeyboardBaseView extends View implements PointerTracker.UIProx
return false;
}
- private static boolean isCommaKeyLabelOrNumberAtEdgeOfPopupChars(Key key) {
- return isNumberAtEdgeOfPopupChars(key) || isCommaKeyLabel(key);
- }
-
- private static boolean isCommaKeyLabel(Key key) {
- return ",".equals(key.label);
- }
-
- 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;
@@ -1246,14 +1218,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);
}
@@ -1273,7 +1237,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);