2010-08-20 05:35:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
package com.android.inputmethod.keyboard;
|
2010-08-20 05:35:02 +00:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.Canvas;
|
2011-02-20 05:44:59 +00:00
|
|
|
import android.graphics.Color;
|
2010-08-20 05:35:02 +00:00
|
|
|
import android.graphics.Paint;
|
2010-08-24 13:46:36 +00:00
|
|
|
import android.graphics.Paint.Align;
|
2010-08-20 05:35:02 +00:00
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
import android.graphics.Rect;
|
2012-08-02 06:10:59 +00:00
|
|
|
import android.graphics.Region;
|
2010-08-24 13:46:36 +00:00
|
|
|
import android.graphics.Typeface;
|
2010-08-20 05:35:02 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.Message;
|
|
|
|
import android.util.AttributeSet;
|
2012-10-02 01:30:03 +00:00
|
|
|
import android.util.DisplayMetrics;
|
2012-08-21 01:42:49 +00:00
|
|
|
import android.util.Log;
|
2012-06-29 07:32:45 +00:00
|
|
|
import android.util.SparseArray;
|
2010-08-20 05:35:02 +00:00
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
2011-04-13 05:12:24 +00:00
|
|
|
import android.view.ViewGroup;
|
2010-08-20 05:35:02 +00:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
2012-08-30 07:18:53 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyDrawParams;
|
|
|
|
import com.android.inputmethod.keyboard.internal.KeyPreviewDrawParams;
|
2012-08-30 08:42:49 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.KeyVisualAttributes;
|
2012-07-20 12:01:44 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.PreviewPlacerView;
|
2012-08-21 07:34:55 +00:00
|
|
|
import com.android.inputmethod.latin.CollectionUtils;
|
2012-07-19 08:20:54 +00:00
|
|
|
import com.android.inputmethod.latin.Constants;
|
2012-11-28 08:25:23 +00:00
|
|
|
import com.android.inputmethod.latin.CoordinateUtils;
|
2011-06-15 04:38:58 +00:00
|
|
|
import com.android.inputmethod.latin.LatinImeLogger;
|
|
|
|
import com.android.inputmethod.latin.R;
|
2011-06-24 18:54:11 +00:00
|
|
|
import com.android.inputmethod.latin.StaticInnerHandlerWrapper;
|
2012-03-08 08:07:02 +00:00
|
|
|
import com.android.inputmethod.latin.StringUtils;
|
2012-07-19 02:21:54 +00:00
|
|
|
import com.android.inputmethod.latin.define.ProductionFlag;
|
|
|
|
import com.android.inputmethod.research.ResearchLogger;
|
2011-06-15 04:38:58 +00:00
|
|
|
|
2012-03-13 08:28:52 +00:00
|
|
|
import java.util.HashSet;
|
2010-08-20 05:35:02 +00:00
|
|
|
|
|
|
|
/**
|
2011-07-01 04:07:59 +00:00
|
|
|
* A view that renders a virtual {@link Keyboard}.
|
2010-08-20 05:35:02 +00:00
|
|
|
*
|
2010-12-02 09:46:21 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyBackground
|
2012-08-31 01:09:43 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_moreKeysLayout
|
|
|
|
* @attr ref R.styleable#KeyboardView_keyPreviewLayout
|
|
|
|
* @attr ref R.styleable#KeyboardView_keyPreviewOffset
|
|
|
|
* @attr ref R.styleable#KeyboardView_keyPreviewHeight
|
|
|
|
* @attr ref R.styleable#KeyboardView_keyPreviewLingerTimeout
|
2011-06-28 07:32:39 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyLabelHorizontalPadding
|
|
|
|
* @attr ref R.styleable#KeyboardView_keyHintLetterPadding
|
2011-09-07 12:56:42 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyPopupHintLetterPadding
|
2012-01-26 09:03:30 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyShiftedLetterHintPadding
|
2012-08-30 08:42:49 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyTextShadowRadius
|
2012-08-31 01:09:43 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_backgroundDimAlpha
|
|
|
|
* @attr ref R.styleable#KeyboardView_gestureFloatingPreviewTextSize
|
|
|
|
* @attr ref R.styleable#KeyboardView_gestureFloatingPreviewTextColor
|
|
|
|
* @attr ref R.styleable#KeyboardView_gestureFloatingPreviewTextOffset
|
2012-09-05 10:03:02 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_gestureFloatingPreviewColor
|
|
|
|
* @attr ref R.styleable#KeyboardView_gestureFloatingPreviewHorizontalPadding
|
|
|
|
* @attr ref R.styleable#KeyboardView_gestureFloatingPreviewVerticalPadding
|
|
|
|
* @attr ref R.styleable#KeyboardView_gestureFloatingPreviewRoundRadius
|
2012-08-31 01:09:43 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_gestureFloatingPreviewTextLingerTimeout
|
|
|
|
* @attr ref R.styleable#KeyboardView_gesturePreviewTrailFadeoutStartDelay
|
|
|
|
* @attr ref R.styleable#KeyboardView_gesturePreviewTrailFadeoutDuration
|
|
|
|
* @attr ref R.styleable#KeyboardView_gesturePreviewTrailUpdateInterval
|
|
|
|
* @attr ref R.styleable#KeyboardView_gesturePreviewTrailColor
|
|
|
|
* @attr ref R.styleable#KeyboardView_gesturePreviewTrailWidth
|
|
|
|
* @attr ref R.styleable#KeyboardView_verticalCorrection
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyTypeface
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyLetterSize
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyLabelSize
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyLargeLetterRatio
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyLargeLabelRatio
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyHintLetterRatio
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyShiftedLetterHintRatio
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyHintLabelRatio
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyPreviewTextRatio
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyTextColor
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyTextColorDisabled
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyTextShadowColor
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyHintLetterColor
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyHintLabelColor
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyShiftedLetterHintInactivatedColor
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyShiftedLetterHintActivatedColor
|
|
|
|
* @attr ref R.styleable#Keyboard_Key_keyPreviewTextColor
|
2010-08-20 05:35:02 +00:00
|
|
|
*/
|
2011-07-11 02:31:02 +00:00
|
|
|
public class KeyboardView extends View implements PointerTracker.DrawingProxy {
|
2012-08-21 01:42:49 +00:00
|
|
|
private static final String TAG = KeyboardView.class.getSimpleName();
|
|
|
|
|
2011-08-31 05:22:53 +00:00
|
|
|
// XML attributes
|
2012-10-16 20:26:45 +00:00
|
|
|
protected final KeyVisualAttributes mKeyVisualAttributes;
|
2012-08-31 01:09:43 +00:00
|
|
|
private final int mKeyLabelHorizontalPadding;
|
|
|
|
private final float mKeyHintLetterPadding;
|
|
|
|
private final float mKeyPopupHintLetterPadding;
|
|
|
|
private final float mKeyShiftedLetterHintPadding;
|
2012-08-30 08:42:49 +00:00
|
|
|
private final float mKeyTextShadowRadius;
|
2011-08-31 05:22:53 +00:00
|
|
|
protected final float mVerticalCorrection;
|
2011-08-31 06:26:32 +00:00
|
|
|
protected final int mMoreKeysLayout;
|
2012-08-30 08:42:49 +00:00
|
|
|
protected final Drawable mKeyBackground;
|
|
|
|
protected final Rect mKeyBackgroundPadding = new Rect();
|
2012-03-13 08:28:52 +00:00
|
|
|
private final int mBackgroundDimAlpha;
|
2010-08-27 12:23:51 +00:00
|
|
|
|
2011-06-27 15:54:14 +00:00
|
|
|
// HORIZONTAL ELLIPSIS "...", character for popup hint.
|
2011-09-23 21:59:33 +00:00
|
|
|
private static final String POPUP_HINT_CHAR = "\u2026";
|
2011-06-27 15:54:14 +00:00
|
|
|
|
2011-08-15 11:43:00 +00:00
|
|
|
// Margin between the label and the icon on a key that has both of them.
|
|
|
|
// Specified by the fraction of the key width.
|
|
|
|
// TODO: Use resource parameter for this value.
|
|
|
|
private static final float LABEL_ICON_MARGIN = 0.05f;
|
|
|
|
|
2011-09-12 07:29:24 +00:00
|
|
|
// The maximum key label width in the proportion to the key width.
|
|
|
|
private static final float MAX_LABEL_RATIO = 0.90f;
|
|
|
|
|
2010-08-27 12:23:51 +00:00
|
|
|
// Main keyboard
|
2010-12-02 09:46:21 +00:00
|
|
|
private Keyboard mKeyboard;
|
2012-08-30 08:42:49 +00:00
|
|
|
protected final KeyDrawParams mKeyDrawParams = new KeyDrawParams();
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2012-09-05 06:34:51 +00:00
|
|
|
// Preview placer view
|
|
|
|
private final PreviewPlacerView mPreviewPlacerView;
|
2012-11-28 06:24:13 +00:00
|
|
|
private final int[] mOriginCoords = CoordinateUtils.newInstance();
|
2012-09-05 06:34:51 +00:00
|
|
|
|
2011-04-19 06:18:20 +00:00
|
|
|
// Key preview
|
2012-08-30 08:42:49 +00:00
|
|
|
private static final int PREVIEW_ALPHA = 240;
|
2011-07-25 21:05:02 +00:00
|
|
|
private final int mKeyPreviewLayoutId;
|
2012-08-30 08:42:49 +00:00
|
|
|
private final int mPreviewOffset;
|
|
|
|
private final int mPreviewHeight;
|
|
|
|
private final int mPreviewLingerTimeout;
|
2012-08-22 05:15:56 +00:00
|
|
|
private final SparseArray<TextView> mKeyPreviewTexts = CollectionUtils.newSparseArray();
|
2012-08-30 08:42:49 +00:00
|
|
|
protected final KeyPreviewDrawParams mKeyPreviewDrawParams = new KeyPreviewDrawParams();
|
2011-05-11 11:51:07 +00:00
|
|
|
private boolean mShowKeyPreviewPopup = true;
|
|
|
|
private int mDelayAfterPreview;
|
2012-09-05 06:34:51 +00:00
|
|
|
// 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];
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2010-08-27 12:23:51 +00:00
|
|
|
// Drawing
|
2011-09-01 08:50:51 +00:00
|
|
|
/** True if the entire keyboard needs to be dimmed. */
|
2012-04-18 05:54:40 +00:00
|
|
|
private boolean mNeedsToDimEntireKeyboard;
|
2012-03-13 08:28:52 +00:00
|
|
|
/** True if all keys should be drawn */
|
|
|
|
private boolean mInvalidateAllKeys;
|
|
|
|
/** The keys that should be drawn */
|
2012-08-21 07:34:55 +00:00
|
|
|
private final HashSet<Key> mInvalidatedKeys = CollectionUtils.newHashSet();
|
2012-08-02 06:10:59 +00:00
|
|
|
/** The working rectangle variable */
|
|
|
|
private final Rect mWorkingRect = new Rect();
|
2011-07-21 06:57:00 +00:00
|
|
|
/** The keyboard bitmap buffer for faster updates */
|
2012-08-02 07:39:27 +00:00
|
|
|
/** The clip region to draw keys */
|
|
|
|
private final Region mClipRegion = new Region();
|
2012-08-02 06:10:59 +00:00
|
|
|
private Bitmap mOffscreenBuffer;
|
2010-08-20 05:35:02 +00:00
|
|
|
/** The canvas for the above mutable keyboard bitmap */
|
2012-09-12 07:28:54 +00:00
|
|
|
private final Canvas mOffscreenCanvas = new Canvas();
|
2011-06-15 03:36:53 +00:00
|
|
|
private final Paint mPaint = new Paint();
|
2012-03-27 08:47:33 +00:00
|
|
|
private final Paint.FontMetrics mFontMetrics = new Paint.FontMetrics();
|
2012-06-29 07:32:45 +00:00
|
|
|
// This sparse array caches key label text height in pixel indexed by key label text size.
|
2012-08-22 05:22:20 +00:00
|
|
|
private static final SparseArray<Float> sTextHeightCache = CollectionUtils.newSparseArray();
|
2012-06-29 07:32:45 +00:00
|
|
|
// This sparse array caches key label text width in pixel indexed by key label text size.
|
2012-08-22 05:22:20 +00:00
|
|
|
private static final SparseArray<Float> sTextWidthCache = CollectionUtils.newSparseArray();
|
2011-09-07 03:07:56 +00:00
|
|
|
private static final char[] KEY_LABEL_REFERENCE_CHAR = { 'M' };
|
|
|
|
private static final char[] KEY_NUMERIC_HINT_LABEL_REFERENCE_CHAR = { '8' };
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2011-07-04 11:58:58 +00:00
|
|
|
private final DrawingHandler mDrawingHandler = new DrawingHandler(this);
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2011-07-04 11:58:58 +00:00
|
|
|
public static class DrawingHandler extends StaticInnerHandlerWrapper<KeyboardView> {
|
2012-08-07 02:48:10 +00:00
|
|
|
private static final int MSG_DISMISS_KEY_PREVIEW = 0;
|
2010-08-24 13:46:36 +00:00
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public DrawingHandler(final KeyboardView outerInstance) {
|
2011-06-24 18:54:11 +00:00
|
|
|
super(outerInstance);
|
|
|
|
}
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
@Override
|
2012-08-30 08:42:49 +00:00
|
|
|
public void handleMessage(final Message msg) {
|
2011-06-24 18:54:11 +00:00
|
|
|
final KeyboardView keyboardView = getOuterInstance();
|
2011-07-21 21:14:22 +00:00
|
|
|
if (keyboardView == null) return;
|
2011-04-27 05:14:45 +00:00
|
|
|
final PointerTracker tracker = (PointerTracker) msg.obj;
|
2010-08-20 05:35:02 +00:00
|
|
|
switch (msg.what) {
|
2011-04-27 05:14:45 +00:00
|
|
|
case MSG_DISMISS_KEY_PREVIEW:
|
2012-08-22 05:15:56 +00:00
|
|
|
final TextView previewText = keyboardView.mKeyPreviewTexts.get(tracker.mPointerId);
|
|
|
|
if (previewText != null) {
|
|
|
|
previewText.setVisibility(INVISIBLE);
|
|
|
|
}
|
2011-04-27 05:14:45 +00:00
|
|
|
break;
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public void dismissKeyPreview(final long delay, final PointerTracker tracker) {
|
2011-04-19 06:18:20 +00:00
|
|
|
sendMessageDelayed(obtainMessage(MSG_DISMISS_KEY_PREVIEW, tracker), delay);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public void cancelDismissKeyPreview(final PointerTracker tracker) {
|
2011-07-25 21:05:02 +00:00
|
|
|
removeMessages(MSG_DISMISS_KEY_PREVIEW, tracker);
|
|
|
|
}
|
|
|
|
|
2012-08-22 05:15:56 +00:00
|
|
|
private void cancelAllDismissKeyPreviews() {
|
2011-04-19 06:18:20 +00:00
|
|
|
removeMessages(MSG_DISMISS_KEY_PREVIEW);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void cancelAllMessages() {
|
2011-04-19 06:18:20 +00:00
|
|
|
cancelAllDismissKeyPreviews();
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
2010-12-10 06:24:28 +00:00
|
|
|
}
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public KeyboardView(final Context context, final AttributeSet attrs) {
|
2010-08-20 05:35:02 +00:00
|
|
|
this(context, attrs, R.attr.keyboardViewStyle);
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public KeyboardView(final Context context, final AttributeSet attrs, final int defStyle) {
|
2010-08-20 05:35:02 +00:00
|
|
|
super(context, attrs, defStyle);
|
|
|
|
|
2012-08-30 07:05:40 +00:00
|
|
|
final TypedArray keyboardViewAttr = context.obtainStyledAttributes(attrs,
|
|
|
|
R.styleable.KeyboardView, defStyle, R.style.KeyboardView);
|
2012-08-30 08:42:49 +00:00
|
|
|
mKeyBackground = keyboardViewAttr.getDrawable(R.styleable.KeyboardView_keyBackground);
|
|
|
|
mKeyBackground.getPadding(mKeyBackgroundPadding);
|
|
|
|
mPreviewOffset = keyboardViewAttr.getDimensionPixelOffset(
|
|
|
|
R.styleable.KeyboardView_keyPreviewOffset, 0);
|
|
|
|
mPreviewHeight = keyboardViewAttr.getDimensionPixelSize(
|
|
|
|
R.styleable.KeyboardView_keyPreviewHeight, 80);
|
|
|
|
mPreviewLingerTimeout = keyboardViewAttr.getInt(
|
|
|
|
R.styleable.KeyboardView_keyPreviewLingerTimeout, 0);
|
|
|
|
mDelayAfterPreview = mPreviewLingerTimeout;
|
2012-08-31 10:57:25 +00:00
|
|
|
mKeyLabelHorizontalPadding = keyboardViewAttr.getDimensionPixelOffset(
|
2012-08-31 01:09:43 +00:00
|
|
|
R.styleable.KeyboardView_keyLabelHorizontalPadding, 0);
|
2012-08-31 10:57:25 +00:00
|
|
|
mKeyHintLetterPadding = keyboardViewAttr.getDimension(
|
2012-08-31 01:09:43 +00:00
|
|
|
R.styleable.KeyboardView_keyHintLetterPadding, 0);
|
2012-08-31 10:57:25 +00:00
|
|
|
mKeyPopupHintLetterPadding = keyboardViewAttr.getDimension(
|
2012-08-31 01:09:43 +00:00
|
|
|
R.styleable.KeyboardView_keyPopupHintLetterPadding, 0);
|
2012-08-31 10:57:25 +00:00
|
|
|
mKeyShiftedLetterHintPadding = keyboardViewAttr.getDimension(
|
2012-08-31 01:09:43 +00:00
|
|
|
R.styleable.KeyboardView_keyShiftedLetterHintPadding, 0);
|
2012-08-30 08:42:49 +00:00
|
|
|
mKeyTextShadowRadius = keyboardViewAttr.getFloat(
|
|
|
|
R.styleable.KeyboardView_keyTextShadowRadius, 0.0f);
|
2012-08-30 07:05:40 +00:00
|
|
|
mKeyPreviewLayoutId = keyboardViewAttr.getResourceId(
|
|
|
|
R.styleable.KeyboardView_keyPreviewLayout, 0);
|
2011-07-25 21:05:02 +00:00
|
|
|
if (mKeyPreviewLayoutId == 0) {
|
2011-06-27 08:51:36 +00:00
|
|
|
mShowKeyPreviewPopup = false;
|
|
|
|
}
|
2012-08-30 08:42:49 +00:00
|
|
|
mVerticalCorrection = keyboardViewAttr.getDimension(
|
2011-08-31 05:22:53 +00:00
|
|
|
R.styleable.KeyboardView_verticalCorrection, 0);
|
2012-08-30 07:05:40 +00:00
|
|
|
mMoreKeysLayout = keyboardViewAttr.getResourceId(
|
|
|
|
R.styleable.KeyboardView_moreKeysLayout, 0);
|
|
|
|
mBackgroundDimAlpha = keyboardViewAttr.getInt(
|
|
|
|
R.styleable.KeyboardView_backgroundDimAlpha, 0);
|
|
|
|
keyboardViewAttr.recycle();
|
2012-08-30 08:42:49 +00:00
|
|
|
|
|
|
|
final TypedArray keyAttr = context.obtainStyledAttributes(attrs,
|
|
|
|
R.styleable.Keyboard_Key, defStyle, R.style.KeyboardView);
|
|
|
|
mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr);
|
2012-08-30 07:05:40 +00:00
|
|
|
keyAttr.recycle();
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2012-08-20 02:24:16 +00:00
|
|
|
mPreviewPlacerView = new PreviewPlacerView(context, attrs);
|
2010-08-20 05:35:02 +00:00
|
|
|
mPaint.setAntiAlias(true);
|
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
private static void blendAlpha(final Paint paint, final int alpha) {
|
|
|
|
final int color = paint.getColor();
|
|
|
|
paint.setARGB((paint.getAlpha() * alpha) / Constants.Color.ALPHA_OPAQUE,
|
|
|
|
Color.red(color), Color.green(color), Color.blue(color));
|
|
|
|
}
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2010-12-02 09:46:21 +00:00
|
|
|
* @see Keyboard
|
2010-08-20 05:35:02 +00:00
|
|
|
* @see #getKeyboard()
|
|
|
|
* @param keyboard the keyboard to display in this view
|
|
|
|
*/
|
2012-08-30 08:42:49 +00:00
|
|
|
public void setKeyboard(final Keyboard keyboard) {
|
2010-08-20 05:35:02 +00:00
|
|
|
mKeyboard = keyboard;
|
2010-09-01 14:18:39 +00:00
|
|
|
LatinImeLogger.onSetKeyboard(keyboard);
|
2010-08-20 05:35:02 +00:00
|
|
|
requestLayout();
|
|
|
|
invalidateAllKeys();
|
2012-08-30 08:42:49 +00:00
|
|
|
final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap;
|
|
|
|
mKeyDrawParams.updateParams(keyHeight, mKeyVisualAttributes);
|
|
|
|
mKeyDrawParams.updateParams(keyHeight, keyboard.mKeyVisualAttributes);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current keyboard being displayed by this view.
|
|
|
|
* @return the currently attached keyboard
|
2010-12-02 09:46:21 +00:00
|
|
|
* @see #setKeyboard(Keyboard)
|
2010-08-20 05:35:02 +00:00
|
|
|
*/
|
2010-12-02 09:46:21 +00:00
|
|
|
public Keyboard getKeyboard() {
|
2010-08-20 05:35:02 +00:00
|
|
|
return mKeyboard;
|
|
|
|
}
|
|
|
|
|
2011-01-14 18:49:21 +00:00
|
|
|
/**
|
2011-05-11 11:48:20 +00:00
|
|
|
* Enables or disables the key feedback popup. This is a popup that shows a magnified
|
2010-08-20 05:35:02 +00:00
|
|
|
* version of the depressed key. By default the preview is enabled.
|
2011-04-19 06:18:20 +00:00
|
|
|
* @param previewEnabled whether or not to enable the key feedback preview
|
2011-05-11 11:51:07 +00:00
|
|
|
* @param delay the delay after which the preview is dismissed
|
|
|
|
* @see #isKeyPreviewPopupEnabled()
|
2010-08-20 05:35:02 +00:00
|
|
|
*/
|
2012-08-30 08:42:49 +00:00
|
|
|
public void setKeyPreviewPopupEnabled(final boolean previewEnabled, final int delay) {
|
2011-05-11 11:51:07 +00:00
|
|
|
mShowKeyPreviewPopup = previewEnabled;
|
|
|
|
mDelayAfterPreview = delay;
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-19 06:18:20 +00:00
|
|
|
* Returns the enabled state of the key feedback preview
|
|
|
|
* @return whether or not the key feedback preview is enabled
|
2011-05-11 11:51:07 +00:00
|
|
|
* @see #setKeyPreviewPopupEnabled(boolean, int)
|
2010-08-20 05:35:02 +00:00
|
|
|
*/
|
2011-05-11 11:51:07 +00:00
|
|
|
public boolean isKeyPreviewPopupEnabled() {
|
|
|
|
return mShowKeyPreviewPopup;
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public void setGesturePreviewMode(final boolean drawsGesturePreviewTrail,
|
|
|
|
final boolean drawsGestureFloatingPreviewText) {
|
2012-07-27 06:32:29 +00:00
|
|
|
mPreviewPlacerView.setGesturePreviewMode(
|
|
|
|
drawsGesturePreviewTrail, drawsGestureFloatingPreviewText);
|
2012-07-20 08:49:06 +00:00
|
|
|
}
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
@Override
|
2012-08-30 08:42:49 +00:00
|
|
|
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
|
2011-07-21 06:57:00 +00:00
|
|
|
if (mKeyboard != null) {
|
|
|
|
// The main keyboard expands to the display width.
|
2011-07-29 00:05:40 +00:00
|
|
|
final int height = mKeyboard.mOccupiedHeight + getPaddingTop() + getPaddingBottom();
|
2011-07-21 06:57:00 +00:00
|
|
|
setMeasuredDimension(widthMeasureSpec, height);
|
2010-08-20 05:35:02 +00:00
|
|
|
} else {
|
2011-07-21 06:57:00 +00:00
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-08-30 08:42:49 +00:00
|
|
|
public void onDraw(final Canvas canvas) {
|
2010-08-20 05:35:02 +00:00
|
|
|
super.onDraw(canvas);
|
2012-08-02 07:39:27 +00:00
|
|
|
if (canvas.isHardwareAccelerated()) {
|
|
|
|
onDrawKeyboard(canvas);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-09 06:25:01 +00:00
|
|
|
|
|
|
|
final boolean bufferNeedsUpdates = mInvalidateAllKeys || !mInvalidatedKeys.isEmpty();
|
|
|
|
if (bufferNeedsUpdates || mOffscreenBuffer == null) {
|
2012-08-02 06:10:59 +00:00
|
|
|
if (maybeAllocateOffscreenBuffer()) {
|
|
|
|
mInvalidateAllKeys = true;
|
2012-09-12 07:28:54 +00:00
|
|
|
// TODO: Stop using the offscreen canvas even when in software rendering
|
|
|
|
mOffscreenCanvas.setBitmap(mOffscreenBuffer);
|
2012-08-02 06:10:59 +00:00
|
|
|
}
|
|
|
|
onDrawKeyboard(mOffscreenCanvas);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
2012-08-02 06:10:59 +00:00
|
|
|
canvas.drawBitmap(mOffscreenBuffer, 0, 0, null);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2012-08-02 06:10:59 +00:00
|
|
|
private boolean maybeAllocateOffscreenBuffer() {
|
2011-02-01 05:02:22 +00:00
|
|
|
final int width = getWidth();
|
|
|
|
final int height = getHeight();
|
2012-08-02 06:10:59 +00:00
|
|
|
if (width == 0 || height == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mOffscreenBuffer != null && mOffscreenBuffer.getWidth() == width
|
|
|
|
&& mOffscreenBuffer.getHeight() == height) {
|
|
|
|
return false;
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
2012-08-02 06:10:59 +00:00
|
|
|
freeOffscreenBuffer();
|
|
|
|
mOffscreenBuffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
|
|
|
return true;
|
|
|
|
}
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2012-08-02 06:10:59 +00:00
|
|
|
private void freeOffscreenBuffer() {
|
|
|
|
if (mOffscreenBuffer != null) {
|
|
|
|
mOffscreenBuffer.recycle();
|
|
|
|
mOffscreenBuffer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onDrawKeyboard(final Canvas canvas) {
|
2010-08-20 05:35:02 +00:00
|
|
|
if (mKeyboard == null) return;
|
|
|
|
|
2012-08-02 06:10:59 +00:00
|
|
|
final int width = getWidth();
|
|
|
|
final int height = getHeight();
|
2012-03-13 08:28:52 +00:00
|
|
|
final Paint paint = mPaint;
|
|
|
|
|
2012-08-02 07:39:27 +00:00
|
|
|
// Calculate clip region and set.
|
|
|
|
final boolean drawAllKeys = mInvalidateAllKeys || mInvalidatedKeys.isEmpty();
|
|
|
|
final boolean isHardwareAccelerated = canvas.isHardwareAccelerated();
|
|
|
|
// TODO: Confirm if it's really required to draw all keys when hardware acceleration is on.
|
|
|
|
if (drawAllKeys || isHardwareAccelerated) {
|
|
|
|
mClipRegion.set(0, 0, width, height);
|
|
|
|
} else {
|
|
|
|
mClipRegion.setEmpty();
|
|
|
|
for (final Key key : mInvalidatedKeys) {
|
|
|
|
if (mKeyboard.hasKey(key)) {
|
|
|
|
final int x = key.mX + getPaddingLeft();
|
|
|
|
final int y = key.mY + getPaddingTop();
|
|
|
|
mWorkingRect.set(x, y, x + key.mWidth, y + key.mHeight);
|
|
|
|
mClipRegion.union(mWorkingRect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isHardwareAccelerated) {
|
|
|
|
canvas.clipRegion(mClipRegion, Region.Op.REPLACE);
|
2012-08-09 06:25:01 +00:00
|
|
|
// Draw keyboard background.
|
|
|
|
canvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR);
|
|
|
|
final Drawable background = getBackground();
|
|
|
|
if (background != null) {
|
|
|
|
background.draw(canvas);
|
|
|
|
}
|
2012-08-02 07:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Confirm if it's really required to draw all keys when hardware acceleration is on.
|
|
|
|
if (drawAllKeys || isHardwareAccelerated) {
|
2011-04-15 04:05:58 +00:00
|
|
|
// Draw all keys.
|
2011-08-02 21:35:18 +00:00
|
|
|
for (final Key key : mKeyboard.mKeys) {
|
2012-08-30 08:42:49 +00:00
|
|
|
onDrawKey(key, canvas, paint);
|
2012-03-13 08:28:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Draw invalidated keys.
|
|
|
|
for (final Key key : mInvalidatedKeys) {
|
2012-08-02 07:39:27 +00:00
|
|
|
if (mKeyboard.hasKey(key)) {
|
2012-08-30 08:42:49 +00:00
|
|
|
onDrawKey(key, canvas, paint);
|
2012-04-18 05:54:40 +00:00
|
|
|
}
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-05 09:41:12 +00:00
|
|
|
|
2012-08-02 07:39:27 +00:00
|
|
|
// Overlay a dark rectangle to dim.
|
|
|
|
if (mNeedsToDimEntireKeyboard) {
|
|
|
|
paint.setColor(Color.BLACK);
|
|
|
|
paint.setAlpha(mBackgroundDimAlpha);
|
|
|
|
// Note: clipRegion() above is in effect if it was called.
|
|
|
|
canvas.drawRect(0, 0, width, height, paint);
|
|
|
|
}
|
|
|
|
|
2012-07-19 02:21:54 +00:00
|
|
|
// ResearchLogging indicator.
|
|
|
|
// TODO: Reimplement using a keyboard background image specific to the ResearchLogger,
|
|
|
|
// and remove this call.
|
|
|
|
if (ProductionFlag.IS_EXPERIMENTAL) {
|
|
|
|
ResearchLogger.getInstance().paintIndicator(this, paint, canvas, width, height);
|
|
|
|
}
|
|
|
|
|
2012-03-13 08:28:52 +00:00
|
|
|
mInvalidatedKeys.clear();
|
|
|
|
mInvalidateAllKeys = false;
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
public void dimEntireKeyboard(final boolean dimmed) {
|
2012-04-18 05:54:40 +00:00
|
|
|
final boolean needsRedrawing = mNeedsToDimEntireKeyboard != dimmed;
|
|
|
|
mNeedsToDimEntireKeyboard = dimmed;
|
2011-09-01 08:50:51 +00:00
|
|
|
if (needsRedrawing) {
|
|
|
|
invalidateAllKeys();
|
|
|
|
}
|
2011-07-01 04:07:59 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
private void onDrawKey(final Key key, final Canvas canvas, final Paint paint) {
|
2012-08-28 08:19:49 +00:00
|
|
|
final int keyDrawX = key.getDrawX() + getPaddingLeft();
|
2012-03-13 08:28:52 +00:00
|
|
|
final int keyDrawY = key.mY + getPaddingTop();
|
|
|
|
canvas.translate(keyDrawX, keyDrawY);
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
final int keyHeight = mKeyboard.mMostCommonKeyHeight - mKeyboard.mVerticalGap;
|
|
|
|
final KeyVisualAttributes attr = key.mKeyVisualAttributes;
|
|
|
|
final KeyDrawParams params = mKeyDrawParams.mayCloneAndUpdateParams(keyHeight, attr);
|
2012-07-19 08:20:54 +00:00
|
|
|
params.mAnimAlpha = Constants.Color.ALPHA_OPAQUE;
|
2012-08-30 08:42:49 +00:00
|
|
|
|
2012-02-02 15:18:47 +00:00
|
|
|
if (!key.isSpacer()) {
|
2012-08-30 08:42:49 +00:00
|
|
|
onDrawKeyBackground(key, canvas);
|
2012-02-02 15:18:47 +00:00
|
|
|
}
|
2011-12-17 17:58:13 +00:00
|
|
|
onDrawKeyTopVisuals(key, canvas, paint, params);
|
2012-03-13 08:28:52 +00:00
|
|
|
|
|
|
|
canvas.translate(-keyDrawX, -keyDrawY);
|
2011-12-17 17:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw key background.
|
2012-09-05 06:34:51 +00:00
|
|
|
protected void onDrawKeyBackground(final Key key, final Canvas canvas) {
|
2012-08-30 08:42:49 +00:00
|
|
|
final Rect padding = mKeyBackgroundPadding;
|
|
|
|
final int bgWidth = key.getDrawWidth() + padding.left + padding.right;
|
|
|
|
final int bgHeight = key.mHeight + padding.top + padding.bottom;
|
|
|
|
final int bgX = -padding.left;
|
|
|
|
final int bgY = -padding.top;
|
2011-12-17 17:58:13 +00:00
|
|
|
final int[] drawableState = key.getCurrentDrawableState();
|
2012-08-30 08:42:49 +00:00
|
|
|
final Drawable background = mKeyBackground;
|
2011-12-17 17:58:13 +00:00
|
|
|
background.setState(drawableState);
|
|
|
|
final Rect bounds = background.getBounds();
|
|
|
|
if (bgWidth != bounds.right || bgHeight != bounds.bottom) {
|
|
|
|
background.setBounds(0, 0, bgWidth, bgHeight);
|
2011-06-28 07:32:39 +00:00
|
|
|
}
|
2011-12-17 17:58:13 +00:00
|
|
|
canvas.translate(bgX, bgY);
|
|
|
|
background.draw(canvas);
|
|
|
|
if (LatinImeLogger.sVISUALDEBUG) {
|
|
|
|
drawRectangle(canvas, 0, 0, bgWidth, bgHeight, 0x80c00000, new Paint());
|
|
|
|
}
|
|
|
|
canvas.translate(-bgX, -bgY);
|
|
|
|
}
|
2011-06-28 07:32:39 +00:00
|
|
|
|
2011-12-17 17:58:13 +00:00
|
|
|
// Draw key top visuals.
|
2012-09-05 06:34:51 +00:00
|
|
|
protected void onDrawKeyTopVisuals(final Key key, final Canvas canvas, final Paint paint,
|
|
|
|
final KeyDrawParams params) {
|
2012-08-28 08:19:49 +00:00
|
|
|
final int keyWidth = key.getDrawWidth();
|
2011-06-28 07:32:39 +00:00
|
|
|
final int keyHeight = key.mHeight;
|
|
|
|
final float centerX = keyWidth * 0.5f;
|
|
|
|
final float centerY = keyHeight * 0.5f;
|
|
|
|
|
2011-12-17 17:58:13 +00:00
|
|
|
if (LatinImeLogger.sVISUALDEBUG) {
|
2011-06-28 07:32:39 +00:00
|
|
|
drawRectangle(canvas, 0, 0, keyWidth, keyHeight, 0x800000c0, new Paint());
|
2011-04-15 04:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw key label.
|
2012-05-16 08:41:56 +00:00
|
|
|
final Drawable icon = key.getIcon(mKeyboard.mIconsSet, params.mAnimAlpha);
|
2011-06-28 07:32:39 +00:00
|
|
|
float positionX = centerX;
|
2011-04-15 04:05:58 +00:00
|
|
|
if (key.mLabel != null) {
|
2012-01-26 09:03:30 +00:00
|
|
|
final String label = key.mLabel;
|
2012-08-30 08:42:49 +00:00
|
|
|
paint.setTypeface(key.selectTypeface(params));
|
|
|
|
paint.setTextSize(key.selectTextSize(params));
|
2011-09-07 03:07:56 +00:00
|
|
|
final float labelCharHeight = getCharHeight(KEY_LABEL_REFERENCE_CHAR, paint);
|
|
|
|
final float labelCharWidth = getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint);
|
2011-04-15 04:05:58 +00:00
|
|
|
|
|
|
|
// Vertical label text alignment.
|
2011-06-28 07:32:39 +00:00
|
|
|
final float baseline = centerY + labelCharHeight / 2;
|
2011-06-25 10:38:55 +00:00
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
// Horizontal label text alignment
|
2011-08-03 04:29:24 +00:00
|
|
|
float labelWidth = 0;
|
|
|
|
if (key.isAlignLeft()) {
|
2012-08-31 01:09:43 +00:00
|
|
|
positionX = mKeyLabelHorizontalPadding;
|
2011-04-15 04:05:58 +00:00
|
|
|
paint.setTextAlign(Align.LEFT);
|
2011-08-03 04:29:24 +00:00
|
|
|
} else if (key.isAlignRight()) {
|
2012-08-31 01:09:43 +00:00
|
|
|
positionX = keyWidth - mKeyLabelHorizontalPadding;
|
2011-04-15 04:05:58 +00:00
|
|
|
paint.setTextAlign(Align.RIGHT);
|
2011-08-03 04:29:24 +00:00
|
|
|
} else if (key.isAlignLeftOfCenter()) {
|
2011-06-25 10:38:55 +00:00
|
|
|
// TODO: Parameterise this?
|
|
|
|
positionX = centerX - labelCharWidth * 7 / 4;
|
|
|
|
paint.setTextAlign(Align.LEFT);
|
2011-08-03 04:29:24 +00:00
|
|
|
} else if (key.hasLabelWithIconLeft() && icon != null) {
|
2011-08-15 11:43:00 +00:00
|
|
|
labelWidth = getLabelWidth(label, paint) + icon.getIntrinsicWidth()
|
2011-09-12 07:29:24 +00:00
|
|
|
+ LABEL_ICON_MARGIN * keyWidth;
|
2011-08-03 04:29:24 +00:00
|
|
|
positionX = centerX + labelWidth / 2;
|
|
|
|
paint.setTextAlign(Align.RIGHT);
|
|
|
|
} else if (key.hasLabelWithIconRight() && icon != null) {
|
2011-08-15 11:43:00 +00:00
|
|
|
labelWidth = getLabelWidth(label, paint) + icon.getIntrinsicWidth()
|
2011-09-12 07:29:24 +00:00
|
|
|
+ LABEL_ICON_MARGIN * keyWidth;
|
2011-08-03 04:29:24 +00:00
|
|
|
positionX = centerX - labelWidth / 2;
|
|
|
|
paint.setTextAlign(Align.LEFT);
|
2011-04-15 04:05:58 +00:00
|
|
|
} else {
|
2011-06-25 10:38:55 +00:00
|
|
|
positionX = centerX;
|
2011-04-15 04:05:58 +00:00
|
|
|
paint.setTextAlign(Align.CENTER);
|
|
|
|
}
|
2011-09-12 07:29:24 +00:00
|
|
|
if (key.needsXScale()) {
|
|
|
|
paint.setTextScaleX(
|
|
|
|
Math.min(1.0f, (keyWidth * MAX_LABEL_RATIO) / getLabelWidth(label, paint)));
|
|
|
|
}
|
2011-06-25 10:38:55 +00:00
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
paint.setColor(key.selectTextColor(params));
|
2011-06-23 12:23:44 +00:00
|
|
|
if (key.isEnabled()) {
|
2011-04-15 04:05:58 +00:00
|
|
|
// Set a drop shadow for the text
|
2012-08-30 08:42:49 +00:00
|
|
|
paint.setShadowLayer(mKeyTextShadowRadius, 0, 0, params.mTextShadowColor);
|
2011-04-15 04:05:58 +00:00
|
|
|
} else {
|
|
|
|
// Make label invisible
|
|
|
|
paint.setColor(Color.TRANSPARENT);
|
|
|
|
}
|
2012-08-30 08:42:49 +00:00
|
|
|
blendAlpha(paint, params.mAnimAlpha);
|
2011-06-25 10:38:55 +00:00
|
|
|
canvas.drawText(label, 0, label.length(), positionX, baseline, paint);
|
2011-09-12 07:29:24 +00:00
|
|
|
// Turn off drop shadow and reset x-scale.
|
2011-04-15 04:05:58 +00:00
|
|
|
paint.setShadowLayer(0, 0, 0, 0);
|
2011-09-12 07:29:24 +00:00
|
|
|
paint.setTextScaleX(1.0f);
|
2011-06-25 10:38:55 +00:00
|
|
|
|
2011-08-03 04:29:24 +00:00
|
|
|
if (icon != null) {
|
|
|
|
final int iconWidth = icon.getIntrinsicWidth();
|
|
|
|
final int iconHeight = icon.getIntrinsicHeight();
|
|
|
|
final int iconY = (keyHeight - iconHeight) / 2;
|
|
|
|
if (key.hasLabelWithIconLeft()) {
|
|
|
|
final int iconX = (int)(centerX - labelWidth / 2);
|
|
|
|
drawIcon(canvas, icon, iconX, iconY, iconWidth, iconHeight);
|
|
|
|
} else if (key.hasLabelWithIconRight()) {
|
|
|
|
final int iconX = (int)(centerX + labelWidth / 2 - iconWidth);
|
|
|
|
drawIcon(canvas, icon, iconX, iconY, iconWidth, iconHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-17 17:58:13 +00:00
|
|
|
if (LatinImeLogger.sVISUALDEBUG) {
|
2011-06-28 07:32:39 +00:00
|
|
|
final Paint line = new Paint();
|
|
|
|
drawHorizontalLine(canvas, baseline, keyWidth, 0xc0008000, line);
|
|
|
|
drawVerticalLine(canvas, positionX, keyHeight, 0xc0800080, line);
|
|
|
|
}
|
2011-04-15 04:05:58 +00:00
|
|
|
}
|
|
|
|
|
2011-06-25 10:38:55 +00:00
|
|
|
// Draw hint label.
|
|
|
|
if (key.mHintLabel != null) {
|
2012-08-30 08:42:49 +00:00
|
|
|
final String hintLabel = key.mHintLabel;
|
|
|
|
paint.setTextSize(key.selectHintTextSize(params));
|
|
|
|
paint.setColor(key.selectHintTextColor(params));
|
|
|
|
blendAlpha(paint, params.mAnimAlpha);
|
2011-06-25 10:38:55 +00:00
|
|
|
final float hintX, hintY;
|
|
|
|
if (key.hasHintLabel()) {
|
2011-09-07 03:07:56 +00:00
|
|
|
// The hint label is placed just right of the key label. Used mainly on
|
|
|
|
// "phone number" layout.
|
2011-06-25 10:38:55 +00:00
|
|
|
// TODO: Generalize the following calculations.
|
2011-09-07 03:07:56 +00:00
|
|
|
hintX = positionX + getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint) * 2;
|
|
|
|
hintY = centerY + getCharHeight(KEY_LABEL_REFERENCE_CHAR, paint) / 2;
|
2011-06-28 07:32:39 +00:00
|
|
|
paint.setTextAlign(Align.LEFT);
|
2012-01-26 09:03:30 +00:00
|
|
|
} else if (key.hasShiftedLetterHint()) {
|
2011-09-07 03:07:56 +00:00
|
|
|
// The hint label is placed at top-right corner of the key. Used mainly on tablet.
|
2012-08-31 01:09:43 +00:00
|
|
|
hintX = keyWidth - mKeyShiftedLetterHintPadding
|
2011-09-07 03:07:56 +00:00
|
|
|
- getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint) / 2;
|
2012-03-27 08:47:33 +00:00
|
|
|
paint.getFontMetrics(mFontMetrics);
|
2012-05-01 10:33:22 +00:00
|
|
|
hintY = -mFontMetrics.top;
|
2011-06-28 07:32:39 +00:00
|
|
|
paint.setTextAlign(Align.CENTER);
|
|
|
|
} else { // key.hasHintLetter()
|
2012-04-24 03:59:33 +00:00
|
|
|
// The hint letter is placed at top-right corner of the key. Used mainly on phone.
|
2012-08-31 01:09:43 +00:00
|
|
|
hintX = keyWidth - mKeyHintLetterPadding
|
2011-09-07 03:07:56 +00:00
|
|
|
- getCharWidth(KEY_NUMERIC_HINT_LABEL_REFERENCE_CHAR, paint) / 2;
|
2012-04-24 03:59:33 +00:00
|
|
|
hintY = -paint.ascent();
|
2011-06-28 07:32:39 +00:00
|
|
|
paint.setTextAlign(Align.CENTER);
|
2011-06-25 10:38:55 +00:00
|
|
|
}
|
2012-08-30 08:42:49 +00:00
|
|
|
canvas.drawText(hintLabel, 0, hintLabel.length(), hintX, hintY, paint);
|
2011-06-28 07:32:39 +00:00
|
|
|
|
2011-12-17 17:58:13 +00:00
|
|
|
if (LatinImeLogger.sVISUALDEBUG) {
|
2011-06-28 07:32:39 +00:00
|
|
|
final Paint line = new Paint();
|
|
|
|
drawHorizontalLine(canvas, (int)hintY, keyWidth, 0xc0808000, line);
|
|
|
|
drawVerticalLine(canvas, (int)hintX, keyHeight, 0xc0808000, line);
|
|
|
|
}
|
2011-06-15 03:36:53 +00:00
|
|
|
}
|
|
|
|
|
2011-04-15 04:05:58 +00:00
|
|
|
// Draw key icon.
|
|
|
|
if (key.mLabel == null && icon != null) {
|
2012-09-03 02:26:45 +00:00
|
|
|
final int iconWidth = Math.min(icon.getIntrinsicWidth(), keyWidth);
|
2011-06-25 10:38:55 +00:00
|
|
|
final int iconHeight = icon.getIntrinsicHeight();
|
|
|
|
final int iconX, alignX;
|
2011-06-28 07:32:39 +00:00
|
|
|
final int iconY = (keyHeight - iconHeight) / 2;
|
2011-08-03 04:29:24 +00:00
|
|
|
if (key.isAlignLeft()) {
|
2012-08-31 01:09:43 +00:00
|
|
|
iconX = mKeyLabelHorizontalPadding;
|
2011-06-25 10:38:55 +00:00
|
|
|
alignX = iconX;
|
2011-08-03 04:29:24 +00:00
|
|
|
} else if (key.isAlignRight()) {
|
2012-08-31 01:09:43 +00:00
|
|
|
iconX = keyWidth - mKeyLabelHorizontalPadding - iconWidth;
|
2011-06-25 10:38:55 +00:00
|
|
|
alignX = iconX + iconWidth;
|
2011-04-15 04:05:58 +00:00
|
|
|
} else { // Align center
|
2011-06-28 07:32:39 +00:00
|
|
|
iconX = (keyWidth - iconWidth) / 2;
|
2011-06-25 10:38:55 +00:00
|
|
|
alignX = iconX + iconWidth / 2;
|
|
|
|
}
|
|
|
|
drawIcon(canvas, icon, iconX, iconY, iconWidth, iconHeight);
|
2011-06-28 07:32:39 +00:00
|
|
|
|
2011-12-17 17:58:13 +00:00
|
|
|
if (LatinImeLogger.sVISUALDEBUG) {
|
2011-06-25 10:38:55 +00:00
|
|
|
final Paint line = new Paint();
|
2011-06-28 07:32:39 +00:00
|
|
|
drawVerticalLine(canvas, alignX, keyHeight, 0xc0800080, line);
|
2011-06-25 10:38:55 +00:00
|
|
|
drawRectangle(canvas, iconX, iconY, iconWidth, iconHeight, 0x80c00000, line);
|
2011-04-15 04:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-17 18:06:20 +00:00
|
|
|
if (key.hasPopupHint() && key.mMoreKeys != null && key.mMoreKeys.length > 0) {
|
2011-12-17 17:58:13 +00:00
|
|
|
drawKeyPopupHint(key, canvas, paint, params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw popup hint "..." at the bottom right corner of the key.
|
2012-09-05 06:34:51 +00:00
|
|
|
protected void drawKeyPopupHint(final Key key, final Canvas canvas, final Paint paint,
|
|
|
|
final KeyDrawParams params) {
|
2012-08-28 08:19:49 +00:00
|
|
|
final int keyWidth = key.getDrawWidth();
|
2011-12-17 17:58:13 +00:00
|
|
|
final int keyHeight = key.mHeight;
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
paint.setTypeface(params.mTypeface);
|
|
|
|
paint.setTextSize(params.mHintLetterSize);
|
|
|
|
paint.setColor(params.mHintLabelColor);
|
2011-12-17 17:58:13 +00:00
|
|
|
paint.setTextAlign(Align.CENTER);
|
2012-08-31 01:09:43 +00:00
|
|
|
final float hintX = keyWidth - mKeyHintLetterPadding
|
2011-12-17 17:58:13 +00:00
|
|
|
- getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint) / 2;
|
2012-08-31 01:09:43 +00:00
|
|
|
final float hintY = keyHeight - mKeyPopupHintLetterPadding;
|
2011-12-17 17:58:13 +00:00
|
|
|
canvas.drawText(POPUP_HINT_CHAR, hintX, hintY, paint);
|
|
|
|
|
|
|
|
if (LatinImeLogger.sVISUALDEBUG) {
|
|
|
|
final Paint line = new Paint();
|
|
|
|
drawHorizontalLine(canvas, (int)hintY, keyWidth, 0xc0808000, line);
|
|
|
|
drawVerticalLine(canvas, (int)hintX, keyHeight, 0xc0808000, line);
|
2011-06-28 07:32:39 +00:00
|
|
|
}
|
2011-04-15 04:05:58 +00:00
|
|
|
}
|
|
|
|
|
2012-09-05 06:34:51 +00:00
|
|
|
private static int getCharGeometryCacheKey(final char referenceChar, final Paint paint) {
|
2011-06-25 10:38:55 +00:00
|
|
|
final int labelSize = (int)paint.getTextSize();
|
|
|
|
final Typeface face = paint.getTypeface();
|
2012-03-16 07:41:18 +00:00
|
|
|
final int codePointOffset = referenceChar << 15;
|
2011-06-25 10:38:55 +00:00
|
|
|
if (face == Typeface.DEFAULT) {
|
2011-09-07 03:07:56 +00:00
|
|
|
return codePointOffset + labelSize;
|
2011-06-25 10:38:55 +00:00
|
|
|
} else if (face == Typeface.DEFAULT_BOLD) {
|
2011-09-07 03:07:56 +00:00
|
|
|
return codePointOffset + labelSize + 0x1000;
|
2011-06-25 10:38:55 +00:00
|
|
|
} else if (face == Typeface.MONOSPACE) {
|
2011-09-07 03:07:56 +00:00
|
|
|
return codePointOffset + labelSize + 0x2000;
|
2011-06-15 03:36:53 +00:00
|
|
|
} else {
|
2011-09-07 03:07:56 +00:00
|
|
|
return codePointOffset + labelSize;
|
2011-06-15 03:36:53 +00:00
|
|
|
}
|
2011-09-07 03:07:56 +00:00
|
|
|
}
|
2011-06-25 10:38:55 +00:00
|
|
|
|
2012-01-25 02:12:48 +00:00
|
|
|
// Working variable for the following methods.
|
|
|
|
private final Rect mTextBounds = new Rect();
|
|
|
|
|
2012-09-05 06:34:51 +00:00
|
|
|
private float getCharHeight(final char[] referenceChar, final Paint paint) {
|
2012-06-29 07:32:45 +00:00
|
|
|
final int key = getCharGeometryCacheKey(referenceChar[0], paint);
|
2011-09-07 03:07:56 +00:00
|
|
|
final Float cachedValue = sTextHeightCache.get(key);
|
|
|
|
if (cachedValue != null)
|
|
|
|
return cachedValue;
|
|
|
|
|
2012-03-16 07:41:18 +00:00
|
|
|
paint.getTextBounds(referenceChar, 0, 1, mTextBounds);
|
2012-01-25 02:12:48 +00:00
|
|
|
final float height = mTextBounds.height();
|
2011-09-07 03:07:56 +00:00
|
|
|
sTextHeightCache.put(key, height);
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
|
2012-09-05 06:34:51 +00:00
|
|
|
private float getCharWidth(final char[] referenceChar, final Paint paint) {
|
2012-06-29 07:32:45 +00:00
|
|
|
final int key = getCharGeometryCacheKey(referenceChar[0], paint);
|
2011-09-07 03:07:56 +00:00
|
|
|
final Float cachedValue = sTextWidthCache.get(key);
|
|
|
|
if (cachedValue != null)
|
|
|
|
return cachedValue;
|
2011-06-25 10:38:55 +00:00
|
|
|
|
2012-03-16 07:41:18 +00:00
|
|
|
paint.getTextBounds(referenceChar, 0, 1, mTextBounds);
|
2012-01-25 02:12:48 +00:00
|
|
|
final float width = mTextBounds.width();
|
2011-06-28 07:32:39 +00:00
|
|
|
sTextWidthCache.put(key, width);
|
2011-06-25 10:38:55 +00:00
|
|
|
return width;
|
2011-06-15 03:36:53 +00:00
|
|
|
}
|
|
|
|
|
2012-10-16 20:26:45 +00:00
|
|
|
// TODO: Remove this method.
|
2012-09-05 06:34:51 +00:00
|
|
|
public float getLabelWidth(final String label, final Paint paint) {
|
|
|
|
paint.getTextBounds(label, 0, label.length(), mTextBounds);
|
2012-01-25 02:12:48 +00:00
|
|
|
return mTextBounds.width();
|
2011-08-03 04:29:24 +00:00
|
|
|
}
|
|
|
|
|
2012-09-05 06:34:51 +00:00
|
|
|
protected static void drawIcon(final Canvas canvas, final Drawable icon, final int x,
|
|
|
|
final int y, final int width, final int height) {
|
2010-10-19 15:18:50 +00:00
|
|
|
canvas.translate(x, y);
|
|
|
|
icon.setBounds(0, 0, width, height);
|
|
|
|
icon.draw(canvas);
|
|
|
|
canvas.translate(-x, -y);
|
|
|
|
}
|
|
|
|
|
2012-09-05 06:34:51 +00:00
|
|
|
private static void drawHorizontalLine(final Canvas canvas, final float y, final float w,
|
|
|
|
final int color, final Paint paint) {
|
2010-11-24 07:59:56 +00:00
|
|
|
paint.setStyle(Paint.Style.STROKE);
|
|
|
|
paint.setStrokeWidth(1.0f);
|
|
|
|
paint.setColor(color);
|
|
|
|
canvas.drawLine(0, y, w, y, paint);
|
|
|
|
}
|
|
|
|
|
2012-09-05 06:34:51 +00:00
|
|
|
private static void drawVerticalLine(final Canvas canvas, final float x, final float h,
|
|
|
|
final int color, final Paint paint) {
|
2010-11-24 07:59:56 +00:00
|
|
|
paint.setStyle(Paint.Style.STROKE);
|
|
|
|
paint.setStrokeWidth(1.0f);
|
|
|
|
paint.setColor(color);
|
|
|
|
canvas.drawLine(x, 0, x, h, paint);
|
|
|
|
}
|
|
|
|
|
2012-09-05 06:34:51 +00:00
|
|
|
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) {
|
2010-11-24 07:59:56 +00:00
|
|
|
paint.setStyle(Paint.Style.STROKE);
|
|
|
|
paint.setStrokeWidth(1.0f);
|
|
|
|
paint.setColor(color);
|
|
|
|
canvas.translate(x, y);
|
|
|
|
canvas.drawRect(0, 0, w, h, paint);
|
|
|
|
canvas.translate(-x, -y);
|
|
|
|
}
|
|
|
|
|
2012-03-16 07:41:18 +00:00
|
|
|
public Paint newDefaultLabelPaint() {
|
|
|
|
final Paint paint = new Paint();
|
|
|
|
paint.setAntiAlias(true);
|
2012-08-30 08:42:49 +00:00
|
|
|
paint.setTypeface(mKeyDrawParams.mTypeface);
|
|
|
|
paint.setTextSize(mKeyDrawParams.mLabelSize);
|
2012-03-16 07:41:18 +00:00
|
|
|
return paint;
|
|
|
|
}
|
|
|
|
|
2011-07-01 04:07:59 +00:00
|
|
|
public void cancelAllMessages() {
|
2011-07-04 11:58:58 +00:00
|
|
|
mDrawingHandler.cancelAllMessages();
|
2011-06-27 15:28:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 05:15:56 +00:00
|
|
|
private TextView getKeyPreviewText(final int pointerId) {
|
|
|
|
TextView previewText = mKeyPreviewTexts.get(pointerId);
|
|
|
|
if (previewText != null) {
|
|
|
|
return previewText;
|
|
|
|
}
|
2011-07-25 21:05:02 +00:00
|
|
|
final Context context = getContext();
|
|
|
|
if (mKeyPreviewLayoutId != 0) {
|
2012-08-22 05:15:56 +00:00
|
|
|
previewText = (TextView)LayoutInflater.from(context).inflate(mKeyPreviewLayoutId, null);
|
2011-07-25 21:05:02 +00:00
|
|
|
} else {
|
2012-08-22 05:15:56 +00:00
|
|
|
previewText = new TextView(context);
|
|
|
|
}
|
|
|
|
mKeyPreviewTexts.put(pointerId, previewText);
|
|
|
|
return previewText;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void dismissAllKeyPreviews() {
|
|
|
|
final int pointerCount = mKeyPreviewTexts.size();
|
|
|
|
for (int id = 0; id < pointerCount; id++) {
|
|
|
|
final TextView previewText = mKeyPreviewTexts.get(id);
|
|
|
|
if (previewText != null) {
|
|
|
|
previewText.setVisibility(INVISIBLE);
|
|
|
|
}
|
2011-07-25 21:05:02 +00:00
|
|
|
}
|
2012-08-22 05:15:56 +00:00
|
|
|
PointerTracker.setReleasedKeyGraphicsToAllKeys();
|
2011-07-25 21:05:02 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 06:18:20 +00:00
|
|
|
@Override
|
2012-08-30 08:42:49 +00:00
|
|
|
public void dismissKeyPreview(final PointerTracker tracker) {
|
2011-07-30 02:52:49 +00:00
|
|
|
mDrawingHandler.dismissKeyPreview(mDelayAfterPreview, tracker);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
private void addKeyPreview(final TextView keyPreview) {
|
2012-07-27 10:32:47 +00:00
|
|
|
locatePreviewPlacerView();
|
|
|
|
mPreviewPlacerView.addView(
|
|
|
|
keyPreview, ViewLayoutUtils.newLayoutParam(mPreviewPlacerView, 0, 0));
|
2011-04-18 08:00:58 +00:00
|
|
|
}
|
|
|
|
|
2012-07-27 10:32:47 +00:00
|
|
|
private void locatePreviewPlacerView() {
|
|
|
|
if (mPreviewPlacerView.getParent() != null) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-02 01:30:03 +00:00
|
|
|
final int width = getWidth();
|
|
|
|
final int height = getHeight();
|
|
|
|
if (width == 0 || height == 0) {
|
|
|
|
// In transient state.
|
|
|
|
return;
|
|
|
|
}
|
2012-11-28 06:24:13 +00:00
|
|
|
getLocationInWindow(mOriginCoords);
|
2012-10-02 01:30:03 +00:00
|
|
|
final DisplayMetrics dm = getResources().getDisplayMetrics();
|
2012-11-28 06:24:13 +00:00
|
|
|
if (CoordinateUtils.y(mOriginCoords) < dm.heightPixels / 4) {
|
2012-10-02 01:30:03 +00:00
|
|
|
// In transient state.
|
|
|
|
return;
|
|
|
|
}
|
2012-08-21 01:42:49 +00:00
|
|
|
final View rootView = getRootView();
|
|
|
|
if (rootView == null) {
|
|
|
|
Log.w(TAG, "Cannot find root view");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final ViewGroup windowContentView = (ViewGroup)rootView.findViewById(android.R.id.content);
|
|
|
|
// Note: It'd be very weird if we get null by android.R.id.content.
|
|
|
|
if (windowContentView == null) {
|
|
|
|
Log.w(TAG, "Cannot find android.R.id.content view to add PreviewPlacerView");
|
|
|
|
} else {
|
|
|
|
windowContentView.addView(mPreviewPlacerView);
|
2012-11-28 06:24:13 +00:00
|
|
|
mPreviewPlacerView.setKeyboardViewGeometry(mOriginCoords, width, height);
|
2012-08-21 01:42:49 +00:00
|
|
|
}
|
2012-07-19 08:20:54 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 06:24:13 +00:00
|
|
|
@Override
|
|
|
|
public void showSlidingKeyInputPreview(final PointerTracker tracker) {
|
|
|
|
locatePreviewPlacerView();
|
|
|
|
mPreviewPlacerView.showSlidingKeyInputPreview(tracker);
|
|
|
|
}
|
|
|
|
|
2012-09-05 06:34:51 +00:00
|
|
|
public void showGestureFloatingPreviewText(final String gestureFloatingPreviewText) {
|
2012-07-27 10:32:47 +00:00
|
|
|
locatePreviewPlacerView();
|
2012-07-30 07:06:19 +00:00
|
|
|
mPreviewPlacerView.setGestureFloatingPreviewText(gestureFloatingPreviewText);
|
2012-07-20 12:01:44 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 02:48:10 +00:00
|
|
|
public void dismissGestureFloatingPreviewText() {
|
|
|
|
locatePreviewPlacerView();
|
|
|
|
mPreviewPlacerView.dismissGestureFloatingPreviewText();
|
|
|
|
}
|
|
|
|
|
2012-07-19 08:20:54 +00:00
|
|
|
@Override
|
2012-09-13 03:38:08 +00:00
|
|
|
public void showGesturePreviewTrail(final PointerTracker tracker,
|
|
|
|
final boolean isOldestTracker) {
|
2012-07-27 10:32:47 +00:00
|
|
|
locatePreviewPlacerView();
|
2012-09-13 03:38:08 +00:00
|
|
|
mPreviewPlacerView.invalidatePointer(tracker, isOldestTracker);
|
2012-07-19 08:20:54 +00:00
|
|
|
}
|
|
|
|
|
2012-04-27 14:44:28 +00:00
|
|
|
@Override
|
2012-08-30 08:42:49 +00:00
|
|
|
public void showKeyPreview(final PointerTracker tracker) {
|
|
|
|
final KeyPreviewDrawParams previewParams = mKeyPreviewDrawParams;
|
2012-08-28 08:19:49 +00:00
|
|
|
if (!mShowKeyPreviewPopup) {
|
2012-08-30 08:42:49 +00:00
|
|
|
previewParams.mPreviewVisibleOffset = -mKeyboard.mVerticalGap;
|
2012-08-28 08:19:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-04-27 14:44:28 +00:00
|
|
|
|
2012-08-22 05:15:56 +00:00
|
|
|
final TextView previewText = getKeyPreviewText(tracker.mPointerId);
|
2011-04-19 06:18:20 +00:00
|
|
|
// If the key preview has no parent view yet, add it to the ViewGroup which can place
|
2011-04-18 08:00:58 +00:00
|
|
|
// key preview absolutely in SoftInputWindow.
|
2011-04-27 05:14:45 +00:00
|
|
|
if (previewText.getParent() == null) {
|
|
|
|
addKeyPreview(previewText);
|
2011-04-13 05:12:24 +00:00
|
|
|
}
|
|
|
|
|
2011-07-25 21:05:02 +00:00
|
|
|
mDrawingHandler.cancelDismissKeyPreview(tracker);
|
2011-11-29 07:56:27 +00:00
|
|
|
final Key key = tracker.getKey();
|
|
|
|
// If key is invalid or IME is already closed, we must not show key preview.
|
2011-04-19 06:18:20 +00:00
|
|
|
// Trying to show key preview while root window is closed causes
|
2010-10-17 10:10:46 +00:00
|
|
|
// WindowManager.BadTokenException.
|
2012-09-05 06:34:51 +00:00
|
|
|
if (key == null) {
|
2010-08-31 11:21:45 +00:00
|
|
|
return;
|
2012-09-05 06:34:51 +00:00
|
|
|
}
|
2011-04-27 05:14:45 +00:00
|
|
|
|
2012-08-30 08:42:49 +00:00
|
|
|
final KeyDrawParams drawParams = mKeyDrawParams;
|
2012-09-05 06:34:51 +00:00
|
|
|
previewText.setTextColor(drawParams.mPreviewTextColor);
|
|
|
|
final Drawable background = previewText.getBackground();
|
|
|
|
if (background != null) {
|
|
|
|
background.setState(KEY_PREVIEW_BACKGROUND_DEFAULT_STATE);
|
|
|
|
background.setAlpha(PREVIEW_ALPHA);
|
|
|
|
}
|
2012-04-23 09:50:07 +00:00
|
|
|
final String label = key.isShiftedLetterActivated() ? key.mHintLabel : key.mLabel;
|
2012-08-28 02:28:42 +00:00
|
|
|
// What we show as preview should match what we show on a key top in onDraw().
|
2012-04-23 09:50:07 +00:00
|
|
|
if (label != null) {
|
2010-10-02 06:17:27 +00:00
|
|
|
// TODO Should take care of temporaryShiftLabel here.
|
2011-04-27 05:14:45 +00:00
|
|
|
previewText.setCompoundDrawables(null, null, null, null);
|
2012-04-23 09:50:07 +00:00
|
|
|
if (StringUtils.codePointCount(label) > 1) {
|
2012-08-30 08:42:49 +00:00
|
|
|
previewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, drawParams.mLetterSize);
|
2011-04-27 05:14:45 +00:00
|
|
|
previewText.setTypeface(Typeface.DEFAULT_BOLD);
|
2010-08-20 05:35:02 +00:00
|
|
|
} else {
|
2012-08-30 08:42:49 +00:00
|
|
|
previewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, drawParams.mPreviewTextSize);
|
|
|
|
previewText.setTypeface(key.selectTypeface(drawParams));
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
2012-04-23 09:50:07 +00:00
|
|
|
previewText.setText(label);
|
2010-10-22 10:35:23 +00:00
|
|
|
} else {
|
2011-04-27 05:14:45 +00:00
|
|
|
previewText.setCompoundDrawables(null, null, null,
|
2012-02-02 15:18:47 +00:00
|
|
|
key.getPreviewIcon(mKeyboard.mIconsSet));
|
2011-04-27 05:14:45 +00:00
|
|
|
previewText.setText(null);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
2011-04-19 06:18:20 +00:00
|
|
|
|
2011-08-31 04:00:32 +00:00
|
|
|
previewText.measure(
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
2012-08-28 08:19:49 +00:00
|
|
|
final int keyDrawWidth = key.getDrawWidth();
|
2012-04-27 14:44:28 +00:00
|
|
|
final int previewWidth = previewText.getMeasuredWidth();
|
2012-08-30 08:42:49 +00:00
|
|
|
final int previewHeight = mPreviewHeight;
|
2012-04-27 14:44:28 +00:00
|
|
|
// The width and height of visible part of the key preview background. The content marker
|
|
|
|
// of the background 9-patch have to cover the visible part of the background.
|
2012-08-30 08:42:49 +00:00
|
|
|
previewParams.mPreviewVisibleWidth = previewWidth - previewText.getPaddingLeft()
|
2012-04-27 14:44:28 +00:00
|
|
|
- previewText.getPaddingRight();
|
2012-08-30 08:42:49 +00:00
|
|
|
previewParams.mPreviewVisibleHeight = previewHeight - previewText.getPaddingTop()
|
2012-04-27 14:44:28 +00:00
|
|
|
- previewText.getPaddingBottom();
|
|
|
|
// The distance between the top edge of the parent key and the bottom of the visible part
|
|
|
|
// of the key preview background.
|
2012-08-30 08:42:49 +00:00
|
|
|
previewParams.mPreviewVisibleOffset = mPreviewOffset - previewText.getPaddingBottom();
|
2012-11-28 06:24:13 +00:00
|
|
|
getLocationInWindow(mOriginCoords);
|
2012-04-27 14:44:28 +00:00
|
|
|
// 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.
|
2012-09-05 06:34:51 +00:00
|
|
|
final int statePosition;
|
2012-11-28 08:25:23 +00:00
|
|
|
int previewX = key.getDrawX() - (previewWidth - keyDrawWidth) / 2
|
2012-11-28 06:24:13 +00:00
|
|
|
+ CoordinateUtils.x(mOriginCoords);
|
2011-11-02 02:00:45 +00:00
|
|
|
if (previewX < 0) {
|
2011-06-27 18:01:35 +00:00
|
|
|
previewX = 0;
|
2012-09-05 06:34:51 +00:00
|
|
|
statePosition = STATE_LEFT;
|
2011-11-02 02:00:45 +00:00
|
|
|
} else if (previewX > getWidth() - previewWidth) {
|
2011-06-27 18:01:35 +00:00
|
|
|
previewX = getWidth() - previewWidth;
|
2012-09-05 06:34:51 +00:00
|
|
|
statePosition = STATE_RIGHT;
|
|
|
|
} else {
|
|
|
|
statePosition = STATE_MIDDLE;
|
2011-06-27 18:01:35 +00:00
|
|
|
}
|
2012-04-27 14:44:28 +00:00
|
|
|
// The key preview is placed vertically above the top edge of the parent key with an
|
|
|
|
// arbitrary offset.
|
2012-11-28 08:25:23 +00:00
|
|
|
final int previewY = key.mY - previewHeight + mPreviewOffset
|
2012-11-28 06:24:13 +00:00
|
|
|
+ CoordinateUtils.y(mOriginCoords);
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2012-09-05 06:34:51 +00:00
|
|
|
if (background != null) {
|
|
|
|
final int hasMoreKeys = (key.mMoreKeys != null) ? STATE_HAS_MOREKEYS : STATE_NORMAL;
|
|
|
|
background.setState(KEY_PREVIEW_BACKGROUND_STATE_TABLE[statePosition][hasMoreKeys]);
|
|
|
|
}
|
2012-04-02 13:55:12 +00:00
|
|
|
ViewLayoutUtils.placeViewAt(
|
2011-06-24 05:19:59 +00:00
|
|
|
previewText, previewX, previewY, previewWidth, previewHeight);
|
2011-04-27 05:14:45 +00:00
|
|
|
previewText.setVisibility(VISIBLE);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Requests a redraw of the entire keyboard. Calling {@link #invalidate} is not sufficient
|
|
|
|
* because the keyboard renders the keys to an off-screen buffer and an invalidate() only
|
|
|
|
* draws the cached buffer.
|
2010-08-31 11:21:45 +00:00
|
|
|
* @see #invalidateKey(Key)
|
2010-08-20 05:35:02 +00:00
|
|
|
*/
|
|
|
|
public void invalidateAllKeys() {
|
2012-03-13 08:28:52 +00:00
|
|
|
mInvalidatedKeys.clear();
|
|
|
|
mInvalidateAllKeys = true;
|
2010-08-20 05:35:02 +00:00
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2010-12-02 09:46:21 +00:00
|
|
|
* @param key key in the attached {@link Keyboard}.
|
2010-08-20 05:35:02 +00:00
|
|
|
* @see #invalidateAllKeys
|
|
|
|
*/
|
2010-12-02 09:46:21 +00:00
|
|
|
@Override
|
2012-09-05 06:34:51 +00:00
|
|
|
public void invalidateKey(final Key key) {
|
2012-03-13 08:28:52 +00:00
|
|
|
if (mInvalidateAllKeys) return;
|
|
|
|
if (key == null) return;
|
|
|
|
mInvalidatedKeys.add(key);
|
2011-04-27 05:14:45 +00:00
|
|
|
final int x = key.mX + getPaddingLeft();
|
|
|
|
final int y = key.mY + getPaddingTop();
|
2012-08-09 06:25:01 +00:00
|
|
|
invalidate(x, y, x + key.mWidth, y + key.mHeight);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void closing() {
|
2012-08-22 05:15:56 +00:00
|
|
|
dismissAllKeyPreviews();
|
2011-07-01 04:07:59 +00:00
|
|
|
cancelAllMessages();
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2012-03-13 08:28:52 +00:00
|
|
|
mInvalidateAllKeys = true;
|
2011-01-14 23:44:25 +00:00
|
|
|
requestLayout();
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2011-07-23 08:16:56 +00:00
|
|
|
@Override
|
2011-08-31 06:26:32 +00:00
|
|
|
public boolean dismissMoreKeysPanel() {
|
2011-07-23 08:16:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-17 14:47:52 +00:00
|
|
|
public void purgeKeyboardAndClosing() {
|
|
|
|
mKeyboard = null;
|
|
|
|
closing();
|
|
|
|
}
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
@Override
|
2011-09-27 06:18:02 +00:00
|
|
|
protected void onDetachedFromWindow() {
|
2010-08-20 05:35:02 +00:00
|
|
|
super.onDetachedFromWindow();
|
|
|
|
closing();
|
2012-07-27 10:32:47 +00:00
|
|
|
mPreviewPlacerView.removeAllViews();
|
2012-08-02 06:10:59 +00:00
|
|
|
freeOffscreenBuffer();
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
}
|