From e8c6d5a480987a1b5d8c3455512f7d75c8fe1de4 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Wed, 5 Sep 2012 15:34:51 +0900 Subject: [PATCH] Utilize StateListDrawable for key preview background Change-Id: I268550a185701caecefc27ebaec40fb2c940d883 --- java/res/drawable/keyboard_key_feedback.xml | 10 +- .../drawable/keyboard_key_feedback_ics.xml | 21 ++- .../keyboard_key_feedback_left_ics.xml | 21 --- .../keyboard_key_feedback_right_ics.xml | 21 --- java/res/layout/key_preview.xml | 6 +- java/res/layout/key_preview_ics.xml | 27 ++++ java/res/values/attrs.xml | 10 +- java/res/values/styles.xml | 7 +- .../inputmethod/keyboard/KeyboardView.java | 125 +++++++++--------- .../keyboard/internal/KeyDrawParams.java | 2 +- .../internal/KeyPreviewDrawParams.java | 4 +- 11 files changed, 127 insertions(+), 127 deletions(-) delete mode 100644 java/res/drawable/keyboard_key_feedback_left_ics.xml delete mode 100644 java/res/drawable/keyboard_key_feedback_right_ics.xml create mode 100644 java/res/layout/key_preview_ics.xml diff --git a/java/res/drawable/keyboard_key_feedback.xml b/java/res/drawable/keyboard_key_feedback.xml index 159ba8686..397e948d8 100644 --- a/java/res/drawable/keyboard_key_feedback.xml +++ b/java/res/drawable/keyboard_key_feedback.xml @@ -14,9 +14,11 @@ limitations under the License. --> - - - + + diff --git a/java/res/drawable/keyboard_key_feedback_ics.xml b/java/res/drawable/keyboard_key_feedback_ics.xml index 04c86794f..3c8850e6c 100644 --- a/java/res/drawable/keyboard_key_feedback_ics.xml +++ b/java/res/drawable/keyboard_key_feedback_ics.xml @@ -14,8 +14,23 @@ limitations under the License. --> - - + + + + + + + + + + diff --git a/java/res/drawable/keyboard_key_feedback_left_ics.xml b/java/res/drawable/keyboard_key_feedback_left_ics.xml deleted file mode 100644 index b68b37f6b..000000000 --- a/java/res/drawable/keyboard_key_feedback_left_ics.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - diff --git a/java/res/drawable/keyboard_key_feedback_right_ics.xml b/java/res/drawable/keyboard_key_feedback_right_ics.xml deleted file mode 100644 index 830678a5a..000000000 --- a/java/res/drawable/keyboard_key_feedback_right_ics.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - diff --git a/java/res/layout/key_preview.xml b/java/res/layout/key_preview.xml index 6ed892e28..2fcd0c4dd 100644 --- a/java/res/layout/key_preview.xml +++ b/java/res/layout/key_preview.xml @@ -20,8 +20,8 @@ +/> diff --git a/java/res/layout/key_preview_ics.xml b/java/res/layout/key_preview_ics.xml new file mode 100644 index 000000000..222e8846c --- /dev/null +++ b/java/res/layout/key_preview_ics.xml @@ -0,0 +1,27 @@ + + + + diff --git a/java/res/values/attrs.xml b/java/res/values/attrs.xml index c8f64351c..05c53a40e 100644 --- a/java/res/values/attrs.xml +++ b/java/res/values/attrs.xml @@ -54,12 +54,10 @@ - - - - - - + + + + diff --git a/java/res/values/styles.xml b/java/res/values/styles.xml index 634b32a78..40f1ff3f4 100644 --- a/java/res/values/styles.xml +++ b/java/res/values/styles.xml @@ -54,9 +54,6 @@ @dimen/key_popup_hint_letter_padding @dimen/key_uppercase_letter_padding @layout/key_preview - @drawable/keyboard_key_feedback - @null - @null #FFFFFFFF @dimen/key_preview_offset @dimen/key_preview_height @@ -323,9 +320,7 @@ #A0FFFFFF #66E0E4E5 #FFFFFFFF - @drawable/keyboard_key_feedback_ics - @drawable/keyboard_key_feedback_left_ics - @drawable/keyboard_key_feedback_right_ics + @layout/key_preview_ics #FFFFFFFF @dimen/key_preview_offset_ics #00000000 diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java index 5b02f9f5a..ce7d79778 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java @@ -59,9 +59,6 @@ import java.util.HashSet; * @attr ref R.styleable#KeyboardView_keyBackground * @attr ref R.styleable#KeyboardView_moreKeysLayout * @attr ref R.styleable#KeyboardView_keyPreviewLayout - * @attr ref R.styleable#KeyboardView_keyPreviewBackground - * @attr ref R.styleable#KeyboardView_keyPreviewLeftBackground - * @attr ref R.styleable#KeyboardView_keyPreviewRightBackground * @attr ref R.styleable#KeyboardView_keyPreviewOffset * @attr ref R.styleable#KeyboardView_keyPreviewHeight * @attr ref R.styleable#KeyboardView_keyPreviewLingerTimeout @@ -108,9 +105,6 @@ import java.util.HashSet; public class KeyboardView extends View implements PointerTracker.DrawingProxy { private static final String TAG = KeyboardView.class.getSimpleName(); - // Miscellaneous constants - private static final int[] LONG_PRESSABLE_STATE_SET = { android.R.attr.state_long_pressable }; - // XML attributes private final KeyVisualAttributes mKeyVisualAttributes; private final int mKeyLabelHorizontalPadding; @@ -139,12 +133,13 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { private Keyboard mKeyboard; protected final KeyDrawParams mKeyDrawParams = new KeyDrawParams(); + // Preview placer view + private final PreviewPlacerView mPreviewPlacerView; + private final int[] mCoordinates = new int[2]; + // Key preview private static final int PREVIEW_ALPHA = 240; private final int mKeyPreviewLayoutId; - private final Drawable mPreviewBackground; - private final Drawable mPreviewLeftBackground; - private final Drawable mPreviewRightBackground; private final int mPreviewOffset; private final int mPreviewHeight; private final int mPreviewLingerTimeout; @@ -152,7 +147,28 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { protected final KeyPreviewDrawParams mKeyPreviewDrawParams = new KeyPreviewDrawParams(); private boolean mShowKeyPreviewPopup = true; private int mDelayAfterPreview; - private final PreviewPlacerView mPreviewPlacerView; + // Background state set + private static final int[][][] KEY_PREVIEW_BACKGROUND_STATE_TABLE = { + { // STATE_MIDDLE + EMPTY_STATE_SET, + { R.attr.state_has_morekeys } + }, + { // STATE_LEFT + { R.attr.state_left_edge }, + { R.attr.state_left_edge, R.attr.state_has_morekeys } + }, + { // STATE_RIGHT + { R.attr.state_right_edge }, + { R.attr.state_right_edge, R.attr.state_has_morekeys } + } + }; + private static final int STATE_MIDDLE = 0; + private static final int STATE_LEFT = 1; + private static final int STATE_RIGHT = 2; + private static final int STATE_NORMAL = 0; + private static final int STATE_HAS_MOREKEYS = 1; + private static final int[] KEY_PREVIEW_BACKGROUND_DEFAULT_STATE = + KEY_PREVIEW_BACKGROUND_STATE_TABLE[STATE_MIDDLE][STATE_NORMAL]; // Drawing /** True if the entire keyboard needs to be dimmed. */ @@ -230,15 +246,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { R.styleable.KeyboardView, defStyle, R.style.KeyboardView); mKeyBackground = keyboardViewAttr.getDrawable(R.styleable.KeyboardView_keyBackground); mKeyBackground.getPadding(mKeyBackgroundPadding); - mPreviewBackground = keyboardViewAttr.getDrawable( - R.styleable.KeyboardView_keyPreviewBackground); - mPreviewLeftBackground = keyboardViewAttr.getDrawable( - R.styleable.KeyboardView_keyPreviewLeftBackground); - mPreviewRightBackground = keyboardViewAttr.getDrawable( - R.styleable.KeyboardView_keyPreviewRightBackground); - setAlpha(mPreviewBackground, PREVIEW_ALPHA); - setAlpha(mPreviewLeftBackground, PREVIEW_ALPHA); - setAlpha(mPreviewRightBackground, PREVIEW_ALPHA); mPreviewOffset = keyboardViewAttr.getDimensionPixelOffset( R.styleable.KeyboardView_keyPreviewOffset, 0); mPreviewHeight = keyboardViewAttr.getDimensionPixelSize( @@ -278,11 +285,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { mPaint.setAntiAlias(true); } - private static void setAlpha(final Drawable drawable, final int alpha) { - if (drawable == null) return; - drawable.setAlpha(alpha); - } - private static void blendAlpha(final Paint paint, final int alpha) { final int color = paint.getColor(); paint.setARGB((paint.getAlpha() * alpha) / Constants.Color.ALPHA_OPAQUE, @@ -498,7 +500,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { } // Draw key background. - protected void onDrawKeyBackground(Key key, Canvas canvas) { + protected void onDrawKeyBackground(final Key key, final Canvas canvas) { final Rect padding = mKeyBackgroundPadding; final int bgWidth = key.getDrawWidth() + padding.left + padding.right; final int bgHeight = key.mHeight + padding.top + padding.bottom; @@ -520,7 +522,8 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { } // Draw key top visuals. - protected void onDrawKeyTopVisuals(Key key, Canvas canvas, Paint paint, KeyDrawParams params) { + protected void onDrawKeyTopVisuals(final Key key, final Canvas canvas, final Paint paint, + final KeyDrawParams params) { final int keyWidth = key.getDrawWidth(); final int keyHeight = key.mHeight; final float centerX = keyWidth * 0.5f; @@ -676,7 +679,8 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { } // Draw popup hint "..." at the bottom right corner of the key. - protected void drawKeyPopupHint(Key key, Canvas canvas, Paint paint, KeyDrawParams params) { + protected void drawKeyPopupHint(final Key key, final Canvas canvas, final Paint paint, + final KeyDrawParams params) { final int keyWidth = key.getDrawWidth(); final int keyHeight = key.mHeight; @@ -696,7 +700,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { } } - private static int getCharGeometryCacheKey(char referenceChar, Paint paint) { + private static int getCharGeometryCacheKey(final char referenceChar, final Paint paint) { final int labelSize = (int)paint.getTextSize(); final Typeface face = paint.getTypeface(); final int codePointOffset = referenceChar << 15; @@ -714,7 +718,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { // Working variable for the following methods. private final Rect mTextBounds = new Rect(); - private float getCharHeight(char[] referenceChar, Paint paint) { + private float getCharHeight(final char[] referenceChar, final Paint paint) { final int key = getCharGeometryCacheKey(referenceChar[0], paint); final Float cachedValue = sTextHeightCache.get(key); if (cachedValue != null) @@ -726,7 +730,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { return height; } - private float getCharWidth(char[] referenceChar, Paint paint) { + private float getCharWidth(final char[] referenceChar, final Paint paint) { final int key = getCharGeometryCacheKey(referenceChar[0], paint); final Float cachedValue = sTextWidthCache.get(key); if (cachedValue != null) @@ -738,36 +742,37 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { return width; } - public float getLabelWidth(String label, Paint paint) { - paint.getTextBounds(label.toString(), 0, label.length(), mTextBounds); + public float getLabelWidth(final String label, final Paint paint) { + paint.getTextBounds(label, 0, label.length(), mTextBounds); return mTextBounds.width(); } - protected static void drawIcon(Canvas canvas, Drawable icon, int x, int y, int width, - int height) { + protected static void drawIcon(final Canvas canvas, final Drawable icon, final int x, + final int y, final int width, final int height) { canvas.translate(x, y); icon.setBounds(0, 0, width, height); icon.draw(canvas); canvas.translate(-x, -y); } - private static void drawHorizontalLine(Canvas canvas, float y, float w, int color, - Paint paint) { + private static void drawHorizontalLine(final Canvas canvas, final float y, final float w, + final int color, final Paint paint) { paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(1.0f); paint.setColor(color); canvas.drawLine(0, y, w, y, paint); } - private static void drawVerticalLine(Canvas canvas, float x, float h, int color, Paint paint) { + private static void drawVerticalLine(final Canvas canvas, final float x, final float h, + final int color, final Paint paint) { paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(1.0f); paint.setColor(color); canvas.drawLine(x, 0, x, h, paint); } - private static void drawRectangle(Canvas canvas, float x, float y, float w, float h, int color, - Paint paint) { + private static void drawRectangle(final Canvas canvas, final float x, final float y, + final float w, final float h, final int color, final Paint paint) { paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(1.0f); paint.setColor(color); @@ -849,7 +854,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { } } - public void showGestureFloatingPreviewText(String gestureFloatingPreviewText) { + public void showGestureFloatingPreviewText(final String gestureFloatingPreviewText) { locatePreviewPlacerView(); mPreviewPlacerView.setGestureFloatingPreviewText(gestureFloatingPreviewText); } @@ -865,7 +870,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { mPreviewPlacerView.invalidatePointer(tracker); } - @SuppressWarnings("deprecation") // setBackgroundDrawable is replaced by setBackground in API16 @Override public void showKeyPreview(final PointerTracker tracker) { final KeyPreviewDrawParams previewParams = mKeyPreviewDrawParams; @@ -886,10 +890,17 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { // If key is invalid or IME is already closed, we must not show key preview. // Trying to show key preview while root window is closed causes // WindowManager.BadTokenException. - if (key == null) + if (key == null) { return; + } final KeyDrawParams drawParams = mKeyDrawParams; + previewText.setTextColor(drawParams.mPreviewTextColor); + final Drawable background = previewText.getBackground(); + if (background != null) { + background.setState(KEY_PREVIEW_BACKGROUND_DEFAULT_STATE); + background.setAlpha(PREVIEW_ALPHA); + } final String label = key.isShiftedLetterActivated() ? key.mHintLabel : key.mLabel; // What we show as preview should match what we show on a key top in onDraw(). if (label != null) { @@ -908,7 +919,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { key.getPreviewIcon(mKeyboard.mIconsSet)); previewText.setText(null); } - previewText.setBackgroundDrawable(mPreviewBackground); previewText.measure( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); @@ -924,32 +934,29 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { // The distance between the top edge of the parent key and the bottom of the visible part // of the key preview background. previewParams.mPreviewVisibleOffset = mPreviewOffset - previewText.getPaddingBottom(); - getLocationInWindow(previewParams.mCoordinates); + getLocationInWindow(mCoordinates); // The key preview is horizontally aligned with the center of the visible part of the // parent key. If it doesn't fit in this {@link KeyboardView}, it is moved inward to fit and // the left/right background is used if such background is specified. - int previewX = key.getDrawX() - (previewWidth - keyDrawWidth) / 2 - + previewParams.mCoordinates[0]; + final int statePosition; + int previewX = key.getDrawX() - (previewWidth - keyDrawWidth) / 2 + mCoordinates[0]; if (previewX < 0) { previewX = 0; - if (mPreviewLeftBackground != null) { - previewText.setBackgroundDrawable(mPreviewLeftBackground); - } + statePosition = STATE_LEFT; } else if (previewX > getWidth() - previewWidth) { previewX = getWidth() - previewWidth; - if (mPreviewRightBackground != null) { - previewText.setBackgroundDrawable(mPreviewRightBackground); - } + statePosition = STATE_RIGHT; + } else { + statePosition = STATE_MIDDLE; } // The key preview is placed vertically above the top edge of the parent key with an // arbitrary offset. - final int previewY = key.mY - previewHeight + mPreviewOffset - + previewParams.mCoordinates[1]; + final int previewY = key.mY - previewHeight + mPreviewOffset + mCoordinates[1]; - // Set the preview background state - previewText.getBackground().setState( - key.mMoreKeys != null ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET); - previewText.setTextColor(drawParams.mPreviewTextColor); + if (background != null) { + final int hasMoreKeys = (key.mMoreKeys != null) ? STATE_HAS_MOREKEYS : STATE_NORMAL; + background.setState(KEY_PREVIEW_BACKGROUND_STATE_TABLE[statePosition][hasMoreKeys]); + } ViewLayoutUtils.placeViewAt( previewText, previewX, previewY, previewWidth, previewHeight); previewText.setVisibility(VISIBLE); @@ -975,7 +982,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { * @see #invalidateAllKeys */ @Override - public void invalidateKey(Key key) { + public void invalidateKey(final Key key) { if (mInvalidateAllKeys) return; if (key == null) return; mInvalidatedKeys.add(key); diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java b/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java index 203bab6ff..5dcd842f7 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java @@ -20,7 +20,7 @@ import android.graphics.Typeface; import com.android.inputmethod.latin.ResourceUtils; -public class KeyDrawParams { +public final class KeyDrawParams { public Typeface mTypeface; public int mLetterSize; diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyPreviewDrawParams.java b/java/src/com/android/inputmethod/keyboard/internal/KeyPreviewDrawParams.java index 996a722c0..609d1a57f 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyPreviewDrawParams.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyPreviewDrawParams.java @@ -16,7 +16,7 @@ package com.android.inputmethod.keyboard.internal; -public class KeyPreviewDrawParams { +public final class KeyPreviewDrawParams { // The graphical geometry of the key preview. // <-width-> // +-------+ ^ @@ -41,6 +41,4 @@ public class KeyPreviewDrawParams { // offset between the top edge of parent key and the bottom of the visible part of key // preview background. public int mPreviewVisibleOffset; - - public final int[] mCoordinates = new int[2]; }