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;
|
|
|
|
import android.graphics.Region.Op;
|
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;
|
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
2011-04-13 05:12:24 +00:00
|
|
|
import android.view.ViewGroup;
|
2011-08-06 06:33:51 +00:00
|
|
|
import android.widget.RelativeLayout;
|
2010-08-20 05:35:02 +00:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
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;
|
2011-06-15 04:38:58 +00:00
|
|
|
|
2010-10-12 06:02:44 +00:00
|
|
|
import java.util.HashMap;
|
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
|
|
|
*
|
2012-03-13 08:28:52 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_backgroundDimAlpha
|
2010-12-02 09:46:21 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyBackground
|
2011-05-13 10:29:25 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyLetterRatio
|
2011-06-25 10:38:55 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyLargeLetterRatio
|
2011-06-15 03:36:53 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyLabelRatio
|
|
|
|
* @attr ref R.styleable#KeyboardView_keyHintLetterRatio
|
2012-01-26 09:03:30 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyShiftedLetterHintRatio
|
2011-06-25 10:38:55 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyHintLabelRatio
|
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
|
2011-06-15 03:36:53 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyTextStyle
|
2010-12-02 09:46:21 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyPreviewLayout
|
2011-06-25 10:38:55 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyPreviewTextRatio
|
2010-12-02 09:46:21 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyPreviewOffset
|
2011-05-13 10:29:25 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyPreviewHeight
|
2010-12-02 09:46:21 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyTextColor
|
2011-05-13 10:29:25 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyTextColorDisabled
|
2011-06-15 03:36:53 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyHintLetterColor
|
2011-06-25 10:38:55 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyHintLabelColor
|
2012-01-26 09:03:30 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyShiftedLetterHintInactivatedColor
|
|
|
|
* @attr ref R.styleable#KeyboardView_keyShiftedLetterHintActivatedColor
|
2011-05-13 10:29:25 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_shadowColor
|
|
|
|
* @attr ref R.styleable#KeyboardView_shadowRadius
|
2010-08-20 05:35:02 +00:00
|
|
|
*/
|
2011-07-11 02:31:02 +00:00
|
|
|
public class KeyboardView extends View implements PointerTracker.DrawingProxy {
|
2010-08-27 12:23:51 +00:00
|
|
|
// Miscellaneous constants
|
2010-08-20 05:35:02 +00:00
|
|
|
private static final int[] LONG_PRESSABLE_STATE_SET = { android.R.attr.state_long_pressable };
|
|
|
|
|
2011-08-31 05:22:53 +00:00
|
|
|
// XML attributes
|
|
|
|
protected final float mVerticalCorrection;
|
2011-08-31 06:26:32 +00:00
|
|
|
protected final int mMoreKeysLayout;
|
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;
|
|
|
|
|
2012-03-14 05:46:22 +00:00
|
|
|
private final static int ALPHA_OPAQUE = 255;
|
|
|
|
|
2010-08-27 12:23:51 +00:00
|
|
|
// Main keyboard
|
2010-12-02 09:46:21 +00:00
|
|
|
private Keyboard mKeyboard;
|
2012-03-16 07:41:18 +00:00
|
|
|
protected final KeyDrawParams mKeyDrawParams;
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2011-04-19 06:18:20 +00:00
|
|
|
// Key preview
|
2011-07-25 21:05:02 +00:00
|
|
|
private final int mKeyPreviewLayoutId;
|
2011-10-12 09:50:04 +00:00
|
|
|
protected final KeyPreviewDrawParams mKeyPreviewDrawParams;
|
2011-05-11 11:51:07 +00:00
|
|
|
private boolean mShowKeyPreviewPopup = true;
|
|
|
|
private int mDelayAfterPreview;
|
2011-04-18 08:00:58 +00:00
|
|
|
private ViewGroup mPreviewPlacer;
|
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;
|
2011-07-21 06:57:00 +00:00
|
|
|
/** Whether the keyboard bitmap buffer needs to be redrawn before it's blitted. **/
|
|
|
|
private boolean mBufferNeedsUpdate;
|
2012-03-13 08:28:52 +00:00
|
|
|
/** True if all keys should be drawn */
|
|
|
|
private boolean mInvalidateAllKeys;
|
|
|
|
/** The keys that should be drawn */
|
|
|
|
private final HashSet<Key> mInvalidatedKeys = new HashSet<Key>();
|
|
|
|
/** The region of invalidated keys */
|
|
|
|
private final Rect mInvalidatedKeysRect = new Rect();
|
2011-07-21 06:57:00 +00:00
|
|
|
/** The keyboard bitmap buffer for faster updates */
|
2010-08-20 05:35:02 +00:00
|
|
|
private Bitmap mBuffer;
|
|
|
|
/** The canvas for the above mutable keyboard bitmap */
|
|
|
|
private Canvas mCanvas;
|
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();
|
2010-10-12 06:02:44 +00:00
|
|
|
// This map caches key label text height in pixel as value and key label text size as map key.
|
2011-06-28 07:32:39 +00:00
|
|
|
private static final HashMap<Integer, Float> sTextHeightCache =
|
|
|
|
new HashMap<Integer, Float>();
|
2011-06-15 03:36:53 +00:00
|
|
|
// This map caches key label text width in pixel as value and key label text size as map key.
|
2011-06-28 07:32:39 +00:00
|
|
|
private static final HashMap<Integer, Float> sTextWidthCache =
|
|
|
|
new HashMap<Integer, Float>();
|
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-01-25 11:14:39 +00:00
|
|
|
private static final int MSG_DISMISS_KEY_PREVIEW = 1;
|
2010-08-24 13:46:36 +00:00
|
|
|
|
2011-07-04 11:58:58 +00:00
|
|
|
public DrawingHandler(KeyboardView outerInstance) {
|
2011-06-24 18:54:11 +00:00
|
|
|
super(outerInstance);
|
|
|
|
}
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
@Override
|
|
|
|
public void handleMessage(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:
|
2011-07-25 21:05:02 +00:00
|
|
|
tracker.getKeyPreviewText().setVisibility(View.INVISIBLE);
|
2011-04-27 05:14:45 +00:00
|
|
|
break;
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-19 06:18:20 +00:00
|
|
|
public void dismissKeyPreview(long delay, PointerTracker tracker) {
|
|
|
|
sendMessageDelayed(obtainMessage(MSG_DISMISS_KEY_PREVIEW, tracker), delay);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2011-07-25 21:05:02 +00:00
|
|
|
public void cancelDismissKeyPreview(PointerTracker tracker) {
|
|
|
|
removeMessages(MSG_DISMISS_KEY_PREVIEW, tracker);
|
|
|
|
}
|
|
|
|
|
2011-04-19 06:18:20 +00:00
|
|
|
public void cancelAllDismissKeyPreviews() {
|
|
|
|
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-03-16 07:41:18 +00:00
|
|
|
protected static class KeyDrawParams {
|
2011-06-28 07:32:39 +00:00
|
|
|
// XML attributes
|
|
|
|
public final int mKeyTextColor;
|
|
|
|
public final int mKeyTextInactivatedColor;
|
|
|
|
public final Typeface mKeyTextStyle;
|
|
|
|
public final float mKeyLabelHorizontalPadding;
|
|
|
|
public final float mKeyHintLetterPadding;
|
2011-09-07 12:56:42 +00:00
|
|
|
public final float mKeyPopupHintLetterPadding;
|
2012-01-26 09:03:30 +00:00
|
|
|
public final float mKeyShiftedLetterHintPadding;
|
2011-06-28 07:32:39 +00:00
|
|
|
public final int mShadowColor;
|
|
|
|
public final float mShadowRadius;
|
|
|
|
public final Drawable mKeyBackground;
|
|
|
|
public final int mKeyHintLetterColor;
|
|
|
|
public final int mKeyHintLabelColor;
|
2012-01-26 09:03:30 +00:00
|
|
|
public final int mKeyShiftedLetterHintInactivatedColor;
|
|
|
|
public final int mKeyShiftedLetterHintActivatedColor;
|
2011-06-28 07:32:39 +00:00
|
|
|
|
2012-02-02 15:18:47 +00:00
|
|
|
/* package */ final float mKeyLetterRatio;
|
2011-06-28 07:32:39 +00:00
|
|
|
private final float mKeyLargeLetterRatio;
|
|
|
|
private final float mKeyLabelRatio;
|
|
|
|
private final float mKeyHintLetterRatio;
|
2012-01-26 09:03:30 +00:00
|
|
|
private final float mKeyShiftedLetterHintRatio;
|
2011-06-28 07:32:39 +00:00
|
|
|
private final float mKeyHintLabelRatio;
|
2011-08-29 11:28:46 +00:00
|
|
|
private static final float UNDEFINED_RATIO = -1.0f;
|
2011-06-28 07:32:39 +00:00
|
|
|
|
|
|
|
public final Rect mPadding = new Rect();
|
|
|
|
public int mKeyLetterSize;
|
|
|
|
public int mKeyLargeLetterSize;
|
|
|
|
public int mKeyLabelSize;
|
|
|
|
public int mKeyHintLetterSize;
|
2012-01-26 09:03:30 +00:00
|
|
|
public int mKeyShiftedLetterHintSize;
|
2011-06-28 07:32:39 +00:00
|
|
|
public int mKeyHintLabelSize;
|
2012-03-14 05:46:22 +00:00
|
|
|
public int mAnimAlpha;
|
2011-06-28 07:32:39 +00:00
|
|
|
|
|
|
|
public KeyDrawParams(TypedArray a) {
|
|
|
|
mKeyBackground = a.getDrawable(R.styleable.KeyboardView_keyBackground);
|
2011-08-30 05:55:56 +00:00
|
|
|
if (a.hasValue(R.styleable.KeyboardView_keyLetterSize)) {
|
2011-08-29 11:28:46 +00:00
|
|
|
mKeyLetterRatio = UNDEFINED_RATIO;
|
2011-08-30 06:19:42 +00:00
|
|
|
mKeyLetterSize = a.getDimensionPixelSize(R.styleable.KeyboardView_keyLetterSize, 0);
|
2011-08-29 11:28:46 +00:00
|
|
|
} else {
|
|
|
|
mKeyLetterRatio = getRatio(a, R.styleable.KeyboardView_keyLetterRatio);
|
|
|
|
}
|
|
|
|
if (a.hasValue(R.styleable.KeyboardView_keyLabelSize)) {
|
|
|
|
mKeyLabelRatio = UNDEFINED_RATIO;
|
2011-08-30 06:19:42 +00:00
|
|
|
mKeyLabelSize = a.getDimensionPixelSize(R.styleable.KeyboardView_keyLabelSize, 0);
|
2011-08-29 11:28:46 +00:00
|
|
|
} else {
|
|
|
|
mKeyLabelRatio = getRatio(a, R.styleable.KeyboardView_keyLabelRatio);
|
|
|
|
}
|
2011-08-30 06:19:42 +00:00
|
|
|
mKeyLargeLetterRatio = getRatio(a, R.styleable.KeyboardView_keyLargeLetterRatio);
|
2011-06-28 07:32:39 +00:00
|
|
|
mKeyHintLetterRatio = getRatio(a, R.styleable.KeyboardView_keyHintLetterRatio);
|
2012-01-26 09:03:30 +00:00
|
|
|
mKeyShiftedLetterHintRatio = getRatio(a,
|
|
|
|
R.styleable.KeyboardView_keyShiftedLetterHintRatio);
|
2011-06-28 07:32:39 +00:00
|
|
|
mKeyHintLabelRatio = getRatio(a, R.styleable.KeyboardView_keyHintLabelRatio);
|
|
|
|
mKeyLabelHorizontalPadding = a.getDimension(
|
|
|
|
R.styleable.KeyboardView_keyLabelHorizontalPadding, 0);
|
|
|
|
mKeyHintLetterPadding = a.getDimension(
|
|
|
|
R.styleable.KeyboardView_keyHintLetterPadding, 0);
|
2011-09-07 12:56:42 +00:00
|
|
|
mKeyPopupHintLetterPadding = a.getDimension(
|
|
|
|
R.styleable.KeyboardView_keyPopupHintLetterPadding, 0);
|
2012-01-26 09:03:30 +00:00
|
|
|
mKeyShiftedLetterHintPadding = a.getDimension(
|
|
|
|
R.styleable.KeyboardView_keyShiftedLetterHintPadding, 0);
|
2011-06-28 07:32:39 +00:00
|
|
|
mKeyTextColor = a.getColor(R.styleable.KeyboardView_keyTextColor, 0xFF000000);
|
|
|
|
mKeyTextInactivatedColor = a.getColor(
|
|
|
|
R.styleable.KeyboardView_keyTextInactivatedColor, 0xFF000000);
|
|
|
|
mKeyHintLetterColor = a.getColor(R.styleable.KeyboardView_keyHintLetterColor, 0);
|
|
|
|
mKeyHintLabelColor = a.getColor(R.styleable.KeyboardView_keyHintLabelColor, 0);
|
2012-01-26 09:03:30 +00:00
|
|
|
mKeyShiftedLetterHintInactivatedColor = a.getColor(
|
|
|
|
R.styleable.KeyboardView_keyShiftedLetterHintInactivatedColor, 0);
|
|
|
|
mKeyShiftedLetterHintActivatedColor = a.getColor(
|
|
|
|
R.styleable.KeyboardView_keyShiftedLetterHintActivatedColor, 0);
|
2011-06-28 07:32:39 +00:00
|
|
|
mKeyTextStyle = Typeface.defaultFromStyle(
|
|
|
|
a.getInt(R.styleable.KeyboardView_keyTextStyle, Typeface.NORMAL));
|
|
|
|
mShadowColor = a.getColor(R.styleable.KeyboardView_shadowColor, 0);
|
|
|
|
mShadowRadius = a.getFloat(R.styleable.KeyboardView_shadowRadius, 0f);
|
|
|
|
|
|
|
|
mKeyBackground.getPadding(mPadding);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateKeyHeight(int keyHeight) {
|
2011-08-29 11:28:46 +00:00
|
|
|
if (mKeyLetterRatio >= 0.0f)
|
|
|
|
mKeyLetterSize = (int)(keyHeight * mKeyLetterRatio);
|
|
|
|
if (mKeyLabelRatio >= 0.0f)
|
|
|
|
mKeyLabelSize = (int)(keyHeight * mKeyLabelRatio);
|
2011-08-30 06:19:42 +00:00
|
|
|
mKeyLargeLetterSize = (int)(keyHeight * mKeyLargeLetterRatio);
|
2011-06-28 07:32:39 +00:00
|
|
|
mKeyHintLetterSize = (int)(keyHeight * mKeyHintLetterRatio);
|
2012-01-26 09:03:30 +00:00
|
|
|
mKeyShiftedLetterHintSize = (int)(keyHeight * mKeyShiftedLetterHintRatio);
|
2011-06-28 07:32:39 +00:00
|
|
|
mKeyHintLabelSize = (int)(keyHeight * mKeyHintLabelRatio);
|
|
|
|
}
|
2012-03-14 05:46:22 +00:00
|
|
|
|
2012-04-12 06:33:48 +00:00
|
|
|
public void blendAlpha(Paint paint) {
|
2012-03-14 05:46:22 +00:00
|
|
|
final int color = paint.getColor();
|
|
|
|
paint.setARGB((paint.getAlpha() * mAnimAlpha) / ALPHA_OPAQUE,
|
|
|
|
Color.red(color), Color.green(color), Color.blue(color));
|
|
|
|
}
|
2011-06-28 07:32:39 +00:00
|
|
|
}
|
|
|
|
|
2011-12-17 17:58:13 +00:00
|
|
|
/* package */ static class KeyPreviewDrawParams {
|
2011-06-28 07:32:39 +00:00
|
|
|
// XML attributes.
|
|
|
|
public final Drawable mPreviewBackground;
|
|
|
|
public final Drawable mPreviewLeftBackground;
|
|
|
|
public final Drawable mPreviewRightBackground;
|
|
|
|
public final int mPreviewTextColor;
|
|
|
|
public final int mPreviewOffset;
|
|
|
|
public final int mPreviewHeight;
|
|
|
|
public final Typeface mKeyTextStyle;
|
2012-01-25 11:14:39 +00:00
|
|
|
public final int mLingerTimeout;
|
2011-06-28 07:32:39 +00:00
|
|
|
|
|
|
|
private final float mPreviewTextRatio;
|
|
|
|
private final float mKeyLetterRatio;
|
|
|
|
|
2012-04-27 14:44:28 +00:00
|
|
|
// The graphical geometry of the key preview.
|
|
|
|
// <-width->
|
|
|
|
// +-------+ ^
|
|
|
|
// | | |
|
|
|
|
// |preview| height (visible)
|
|
|
|
// | | |
|
|
|
|
// + + ^ v
|
|
|
|
// \ / |offset
|
|
|
|
// +-\ /-+ v
|
|
|
|
// | +-+ |
|
|
|
|
// |parent |
|
|
|
|
// | key|
|
|
|
|
// +-------+
|
|
|
|
// The background of a {@link TextView} being used for a key preview may have invisible
|
|
|
|
// paddings. To align the more keys keyboard panel's visible part with the visible part of
|
|
|
|
// the background, we need to record the width and height of key preview that don't include
|
|
|
|
// invisible paddings.
|
|
|
|
public int mPreviewVisibleWidth;
|
|
|
|
public int mPreviewVisibleHeight;
|
|
|
|
// The key preview may have an arbitrary offset and its background that may have a bottom
|
|
|
|
// padding. To align the more keys keyboard and the key preview we also need to record the
|
|
|
|
// offset between the top edge of parent key and the bottom of the visible part of key
|
|
|
|
// preview background.
|
|
|
|
public int mPreviewVisibleOffset;
|
|
|
|
|
2011-06-28 07:32:39 +00:00
|
|
|
public int mPreviewTextSize;
|
|
|
|
public int mKeyLetterSize;
|
|
|
|
public final int[] mCoordinates = new int[2];
|
|
|
|
|
2011-07-01 05:21:25 +00:00
|
|
|
private static final int PREVIEW_ALPHA = 240;
|
|
|
|
|
2011-06-28 07:32:39 +00:00
|
|
|
public KeyPreviewDrawParams(TypedArray a, KeyDrawParams keyDrawParams) {
|
|
|
|
mPreviewBackground = a.getDrawable(R.styleable.KeyboardView_keyPreviewBackground);
|
|
|
|
mPreviewLeftBackground = a.getDrawable(
|
|
|
|
R.styleable.KeyboardView_keyPreviewLeftBackground);
|
|
|
|
mPreviewRightBackground = a.getDrawable(
|
|
|
|
R.styleable.KeyboardView_keyPreviewRightBackground);
|
2011-07-01 05:21:25 +00:00
|
|
|
setAlpha(mPreviewBackground, PREVIEW_ALPHA);
|
|
|
|
setAlpha(mPreviewLeftBackground, PREVIEW_ALPHA);
|
|
|
|
setAlpha(mPreviewRightBackground, PREVIEW_ALPHA);
|
2011-06-28 07:32:39 +00:00
|
|
|
mPreviewOffset = a.getDimensionPixelOffset(
|
|
|
|
R.styleable.KeyboardView_keyPreviewOffset, 0);
|
|
|
|
mPreviewHeight = a.getDimensionPixelSize(
|
|
|
|
R.styleable.KeyboardView_keyPreviewHeight, 80);
|
|
|
|
mPreviewTextRatio = getRatio(a, R.styleable.KeyboardView_keyPreviewTextRatio);
|
|
|
|
mPreviewTextColor = a.getColor(R.styleable.KeyboardView_keyPreviewTextColor, 0);
|
2012-01-25 11:14:39 +00:00
|
|
|
mLingerTimeout = a.getInt(R.styleable.KeyboardView_keyPreviewLingerTimeout, 0);
|
2011-06-28 07:32:39 +00:00
|
|
|
|
|
|
|
mKeyLetterRatio = keyDrawParams.mKeyLetterRatio;
|
|
|
|
mKeyTextStyle = keyDrawParams.mKeyTextStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateKeyHeight(int keyHeight) {
|
|
|
|
mPreviewTextSize = (int)(keyHeight * mPreviewTextRatio);
|
|
|
|
mKeyLetterSize = (int)(keyHeight * mKeyLetterRatio);
|
|
|
|
}
|
2011-07-01 05:21:25 +00:00
|
|
|
|
|
|
|
private static void setAlpha(Drawable drawable, int alpha) {
|
|
|
|
if (drawable == null)
|
|
|
|
return;
|
|
|
|
drawable.setAlpha(alpha);
|
|
|
|
}
|
2011-06-28 07:32:39 +00:00
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
public KeyboardView(Context context, AttributeSet attrs) {
|
2010-08-20 05:35:02 +00:00
|
|
|
this(context, attrs, R.attr.keyboardViewStyle);
|
|
|
|
}
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
public KeyboardView(Context context, AttributeSet attrs, int defStyle) {
|
2010-08-20 05:35:02 +00:00
|
|
|
super(context, attrs, defStyle);
|
|
|
|
|
2011-01-19 11:59:57 +00:00
|
|
|
final TypedArray a = context.obtainStyledAttributes(
|
2010-12-02 09:46:21 +00:00
|
|
|
attrs, R.styleable.KeyboardView, defStyle, R.style.KeyboardView);
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2011-06-28 07:32:39 +00:00
|
|
|
mKeyDrawParams = new KeyDrawParams(a);
|
|
|
|
mKeyPreviewDrawParams = new KeyPreviewDrawParams(a, mKeyDrawParams);
|
2011-07-25 21:05:02 +00:00
|
|
|
mKeyPreviewLayoutId = a.getResourceId(R.styleable.KeyboardView_keyPreviewLayout, 0);
|
|
|
|
if (mKeyPreviewLayoutId == 0) {
|
2011-06-27 08:51:36 +00:00
|
|
|
mShowKeyPreviewPopup = false;
|
|
|
|
}
|
2011-08-31 05:22:53 +00:00
|
|
|
mVerticalCorrection = a.getDimensionPixelOffset(
|
|
|
|
R.styleable.KeyboardView_verticalCorrection, 0);
|
2011-08-31 06:26:32 +00:00
|
|
|
mMoreKeysLayout = a.getResourceId(R.styleable.KeyboardView_moreKeysLayout, 0);
|
2012-03-13 08:28:52 +00:00
|
|
|
mBackgroundDimAlpha = a.getInt(R.styleable.KeyboardView_backgroundDimAlpha, 0);
|
2011-06-15 02:49:57 +00:00
|
|
|
a.recycle();
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2012-01-25 11:14:39 +00:00
|
|
|
mDelayAfterPreview = mKeyPreviewDrawParams.mLingerTimeout;
|
2010-08-20 05:35:02 +00:00
|
|
|
|
|
|
|
mPaint.setAntiAlias(true);
|
|
|
|
}
|
|
|
|
|
2011-05-12 14:49:19 +00:00
|
|
|
// Read fraction value in TypedArray as float.
|
2012-01-25 02:12:48 +00:00
|
|
|
/* package */ static float getRatio(TypedArray a, int index) {
|
2011-05-12 14:49:19 +00:00
|
|
|
return a.getFraction(index, 1000, 1000, 1) / 1000.0f;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2010-12-02 09:46:21 +00:00
|
|
|
public void setKeyboard(Keyboard keyboard) {
|
2012-01-25 11:14:39 +00:00
|
|
|
// Remove any pending messages.
|
|
|
|
mDrawingHandler.cancelAllMessages();
|
2011-08-06 06:33:51 +00:00
|
|
|
if (mKeyboard != null) {
|
|
|
|
PointerTracker.dismissAllKeyPreviews();
|
|
|
|
}
|
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();
|
2011-09-22 10:29:58 +00:00
|
|
|
final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap;
|
2011-06-28 07:32:39 +00:00
|
|
|
mKeyDrawParams.updateKeyHeight(keyHeight);
|
|
|
|
mKeyPreviewDrawParams.updateKeyHeight(keyHeight);
|
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
|
|
|
*/
|
2011-05-11 11:51:07 +00:00
|
|
|
public void setKeyPreviewPopupEnabled(boolean previewEnabled, int delay) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-07-21 06:57:00 +00:00
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
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
|
|
|
|
public void onDraw(Canvas canvas) {
|
|
|
|
super.onDraw(canvas);
|
2011-07-21 06:57:00 +00:00
|
|
|
if (mBufferNeedsUpdate || mBuffer == null) {
|
|
|
|
mBufferNeedsUpdate = false;
|
2010-08-20 05:35:02 +00:00
|
|
|
onBufferDraw();
|
|
|
|
}
|
|
|
|
canvas.drawBitmap(mBuffer, 0, 0, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onBufferDraw() {
|
2011-02-01 05:02:22 +00:00
|
|
|
final int width = getWidth();
|
|
|
|
final int height = getHeight();
|
|
|
|
if (width == 0 || height == 0)
|
|
|
|
return;
|
|
|
|
if (mBuffer == null || mBuffer.getWidth() != width || mBuffer.getHeight() != height) {
|
2011-06-20 02:58:56 +00:00
|
|
|
if (mBuffer != null)
|
|
|
|
mBuffer.recycle();
|
2011-02-01 05:02:22 +00:00
|
|
|
mBuffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
2012-03-13 08:28:52 +00:00
|
|
|
mInvalidateAllKeys = true;
|
2011-06-20 02:58:56 +00:00
|
|
|
if (mCanvas != null) {
|
|
|
|
mCanvas.setBitmap(mBuffer);
|
|
|
|
} else {
|
|
|
|
mCanvas = new Canvas(mBuffer);
|
|
|
|
}
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mKeyboard == null) return;
|
|
|
|
|
2012-03-13 08:28:52 +00:00
|
|
|
final Canvas canvas = mCanvas;
|
|
|
|
final Paint paint = mPaint;
|
2011-06-28 07:32:39 +00:00
|
|
|
final KeyDrawParams params = mKeyDrawParams;
|
2012-03-13 08:28:52 +00:00
|
|
|
|
|
|
|
if (mInvalidateAllKeys || mInvalidatedKeys.isEmpty()) {
|
2012-04-18 05:54:40 +00:00
|
|
|
mInvalidatedKeysRect.set(0, 0, width, height);
|
2012-03-13 08:28:52 +00:00
|
|
|
canvas.clipRect(mInvalidatedKeysRect, Op.REPLACE);
|
|
|
|
canvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR);
|
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-03-13 08:28:52 +00:00
|
|
|
onDrawKey(key, canvas, paint, params);
|
|
|
|
}
|
2012-04-18 05:54:40 +00:00
|
|
|
if (mNeedsToDimEntireKeyboard) {
|
|
|
|
drawDimRectangle(canvas, mInvalidatedKeysRect, mBackgroundDimAlpha, paint);
|
|
|
|
}
|
2012-03-13 08:28:52 +00:00
|
|
|
} else {
|
|
|
|
// Draw invalidated keys.
|
|
|
|
for (final Key key : mInvalidatedKeys) {
|
|
|
|
final int x = key.mX + getPaddingLeft();
|
|
|
|
final int y = key.mY + getPaddingTop();
|
|
|
|
mInvalidatedKeysRect.set(x, y, x + key.mWidth, y + key.mHeight);
|
|
|
|
canvas.clipRect(mInvalidatedKeysRect, Op.REPLACE);
|
|
|
|
canvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR);
|
|
|
|
onDrawKey(key, canvas, paint, params);
|
2012-04-18 05:54:40 +00:00
|
|
|
if (mNeedsToDimEntireKeyboard) {
|
|
|
|
drawDimRectangle(canvas, mInvalidatedKeysRect, mBackgroundDimAlpha, paint);
|
|
|
|
}
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-05 09:41:12 +00:00
|
|
|
|
2012-03-13 08:28:52 +00:00
|
|
|
mInvalidatedKeys.clear();
|
|
|
|
mInvalidatedKeysRect.setEmpty();
|
|
|
|
mInvalidateAllKeys = false;
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2011-09-01 08:50:51 +00:00
|
|
|
public void dimEntireKeyboard(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
|
|
|
}
|
|
|
|
|
2011-12-17 17:58:13 +00:00
|
|
|
private void onDrawKey(Key key, Canvas canvas, Paint paint, KeyDrawParams params) {
|
2012-03-13 08:28:52 +00:00
|
|
|
final int keyDrawX = key.mX + key.mVisualInsetsLeft + getPaddingLeft();
|
|
|
|
final int keyDrawY = key.mY + getPaddingTop();
|
|
|
|
canvas.translate(keyDrawX, keyDrawY);
|
|
|
|
|
2012-03-14 05:46:22 +00:00
|
|
|
params.mAnimAlpha = ALPHA_OPAQUE;
|
2012-02-02 15:18:47 +00:00
|
|
|
if (!key.isSpacer()) {
|
|
|
|
onDrawKeyBackground(key, canvas, params);
|
|
|
|
}
|
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-01-25 02:12:48 +00:00
|
|
|
protected void onDrawKeyBackground(Key key, Canvas canvas, KeyDrawParams params) {
|
2011-12-17 17:58:13 +00:00
|
|
|
final int bgWidth = key.mWidth - key.mVisualInsetsLeft - key.mVisualInsetsRight
|
|
|
|
+ params.mPadding.left + params.mPadding.right;
|
|
|
|
final int bgHeight = key.mHeight + params.mPadding.top + params.mPadding.bottom;
|
|
|
|
final int bgX = -params.mPadding.left;
|
|
|
|
final int bgY = -params.mPadding.top;
|
|
|
|
final int[] drawableState = key.getCurrentDrawableState();
|
|
|
|
final Drawable background = params.mKeyBackground;
|
|
|
|
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-01-25 02:12:48 +00:00
|
|
|
protected void onDrawKeyTopVisuals(Key key, Canvas canvas, Paint paint, KeyDrawParams params) {
|
2011-07-21 02:05:27 +00:00
|
|
|
final int keyWidth = key.mWidth - key.mVisualInsetsLeft - key.mVisualInsetsRight;
|
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-01-25 05:54:28 +00:00
|
|
|
final Drawable icon = key.getIcon(mKeyboard.mIconsSet);
|
2012-03-14 05:46:22 +00:00
|
|
|
if (icon != null) {
|
|
|
|
icon.setAlpha(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;
|
2011-06-25 10:38:55 +00:00
|
|
|
// For characters, use large font. For labels like "Done", use smaller font.
|
2011-06-28 07:32:39 +00:00
|
|
|
paint.setTypeface(key.selectTypeface(params.mKeyTextStyle));
|
|
|
|
final int labelSize = key.selectTextSize(params.mKeyLetterSize,
|
|
|
|
params.mKeyLargeLetterSize, params.mKeyLabelSize, params.mKeyHintLabelSize);
|
2011-06-25 10:38:55 +00:00
|
|
|
paint.setTextSize(labelSize);
|
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()) {
|
2011-06-28 07:32:39 +00:00
|
|
|
positionX = (int)params.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()) {
|
2011-06-28 07:32:39 +00:00
|
|
|
positionX = keyWidth - (int)params.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-01-26 09:03:30 +00:00
|
|
|
paint.setColor(key.isShiftedLetterActivated()
|
|
|
|
? params.mKeyTextInactivatedColor : params.mKeyTextColor);
|
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
|
2011-06-28 07:32:39 +00:00
|
|
|
paint.setShadowLayer(params.mShadowRadius, 0, 0, params.mShadowColor);
|
2011-04-15 04:05:58 +00:00
|
|
|
} else {
|
|
|
|
// Make label invisible
|
|
|
|
paint.setColor(Color.TRANSPARENT);
|
|
|
|
}
|
2012-04-12 06:33:48 +00:00
|
|
|
params.blendAlpha(paint);
|
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-01-25 08:01:53 +00:00
|
|
|
final String hint = key.mHintLabel;
|
2011-06-25 10:38:55 +00:00
|
|
|
final int hintColor;
|
|
|
|
final int hintSize;
|
2011-06-28 07:32:39 +00:00
|
|
|
if (key.hasHintLabel()) {
|
|
|
|
hintColor = params.mKeyHintLabelColor;
|
|
|
|
hintSize = params.mKeyHintLabelSize;
|
2011-06-25 10:38:55 +00:00
|
|
|
paint.setTypeface(Typeface.DEFAULT);
|
2012-01-26 09:03:30 +00:00
|
|
|
} else if (key.hasShiftedLetterHint()) {
|
|
|
|
hintColor = key.isShiftedLetterActivated()
|
|
|
|
? params.mKeyShiftedLetterHintActivatedColor
|
|
|
|
: params.mKeyShiftedLetterHintInactivatedColor;
|
|
|
|
hintSize = params.mKeyShiftedLetterHintSize;
|
2011-06-28 07:32:39 +00:00
|
|
|
} else { // key.hasHintLetter()
|
|
|
|
hintColor = params.mKeyHintLetterColor;
|
|
|
|
hintSize = params.mKeyHintLetterSize;
|
2011-06-15 03:36:53 +00:00
|
|
|
}
|
2011-06-25 10:38:55 +00:00
|
|
|
paint.setColor(hintColor);
|
2012-04-12 06:33:48 +00:00
|
|
|
params.blendAlpha(paint);
|
2011-06-25 10:38:55 +00:00
|
|
|
paint.setTextSize(hintSize);
|
|
|
|
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-01-26 09:03:30 +00:00
|
|
|
hintX = keyWidth - params.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.
|
2011-09-07 03:07:56 +00:00
|
|
|
hintX = keyWidth - params.mKeyHintLetterPadding
|
|
|
|
- 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
|
|
|
}
|
|
|
|
canvas.drawText(hint, 0, hint.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) {
|
2011-06-25 10:38:55 +00:00
|
|
|
final int iconWidth = icon.getIntrinsicWidth();
|
|
|
|
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()) {
|
2011-06-28 07:32:39 +00:00
|
|
|
iconX = (int)params.mKeyLabelHorizontalPadding;
|
2011-06-25 10:38:55 +00:00
|
|
|
alignX = iconX;
|
2011-08-03 04:29:24 +00:00
|
|
|
} else if (key.isAlignRight()) {
|
2011-06-28 07:32:39 +00:00
|
|
|
iconX = keyWidth - (int)params.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-01-25 02:12:48 +00:00
|
|
|
protected void drawKeyPopupHint(Key key, Canvas canvas, Paint paint, KeyDrawParams params) {
|
2011-12-17 17:58:13 +00:00
|
|
|
final int keyWidth = key.mWidth - key.mVisualInsetsLeft - key.mVisualInsetsRight;
|
|
|
|
final int keyHeight = key.mHeight;
|
|
|
|
|
2012-04-16 03:11:05 +00:00
|
|
|
paint.setTypeface(params.mKeyTextStyle);
|
2011-12-17 17:58:13 +00:00
|
|
|
paint.setTextSize(params.mKeyHintLetterSize);
|
|
|
|
paint.setColor(params.mKeyHintLabelColor);
|
|
|
|
paint.setTextAlign(Align.CENTER);
|
|
|
|
final float hintX = keyWidth - params.mKeyHintLetterPadding
|
|
|
|
- getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint) / 2;
|
|
|
|
final float hintY = keyHeight - params.mKeyPopupHintLetterPadding;
|
|
|
|
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-03-16 07:41:18 +00:00
|
|
|
private static int getCharGeometryCacheKey(char referenceChar, 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-03-16 07:41:18 +00:00
|
|
|
private float getCharHeight(char[] referenceChar, Paint paint) {
|
|
|
|
final Integer 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-03-16 07:41:18 +00:00
|
|
|
private float getCharWidth(char[] referenceChar, Paint paint) {
|
|
|
|
final Integer 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-03-16 07:41:18 +00:00
|
|
|
public float getLabelWidth(String label, Paint paint) {
|
2012-01-25 02:12:48 +00:00
|
|
|
paint.getTextBounds(label.toString(), 0, label.length(), mTextBounds);
|
|
|
|
return mTextBounds.width();
|
2011-08-03 04:29:24 +00:00
|
|
|
}
|
|
|
|
|
2012-01-25 02:12:48 +00:00
|
|
|
protected static void drawIcon(Canvas canvas, Drawable icon, int x, int y, int width,
|
2010-11-24 07:59:56 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-08-16 08:08:30 +00:00
|
|
|
private static void drawHorizontalLine(Canvas canvas, float y, float w, int color,
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-06-28 07:32:39 +00:00
|
|
|
private static void drawVerticalLine(Canvas canvas, float x, float h, int color, 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);
|
|
|
|
}
|
|
|
|
|
2011-06-28 07:32:39 +00:00
|
|
|
private static void drawRectangle(Canvas canvas, float x, float y, float w, float h, int color,
|
2010-11-24 07:59:56 +00:00
|
|
|
Paint paint) {
|
|
|
|
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-04-18 05:54:40 +00:00
|
|
|
// Overlay a dark rectangle to dim.
|
|
|
|
private static void drawDimRectangle(Canvas canvas, Rect rect, int alpha, Paint paint) {
|
|
|
|
paint.setColor(Color.BLACK);
|
|
|
|
paint.setAlpha(alpha);
|
|
|
|
canvas.drawRect(rect, paint);
|
|
|
|
}
|
|
|
|
|
2012-03-16 07:41:18 +00:00
|
|
|
public Paint newDefaultLabelPaint() {
|
|
|
|
final Paint paint = new Paint();
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setTypeface(mKeyDrawParams.mKeyTextStyle);
|
|
|
|
paint.setTextSize(mKeyDrawParams.mKeyLabelSize);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-07-25 21:05:02 +00:00
|
|
|
// Called by {@link PointerTracker} constructor to create a TextView.
|
|
|
|
@Override
|
|
|
|
public TextView inflateKeyPreviewText() {
|
|
|
|
final Context context = getContext();
|
|
|
|
if (mKeyPreviewLayoutId != 0) {
|
|
|
|
return (TextView)LayoutInflater.from(context).inflate(mKeyPreviewLayoutId, null);
|
|
|
|
} else {
|
|
|
|
return new TextView(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-19 06:18:20 +00:00
|
|
|
@Override
|
|
|
|
public void dismissKeyPreview(PointerTracker tracker) {
|
2011-07-30 02:52:49 +00:00
|
|
|
mDrawingHandler.dismissKeyPreview(mDelayAfterPreview, tracker);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2011-04-18 08:00:58 +00:00
|
|
|
private void addKeyPreview(TextView keyPreview) {
|
2011-06-20 15:11:17 +00:00
|
|
|
if (mPreviewPlacer == null) {
|
2011-08-06 06:33:51 +00:00
|
|
|
mPreviewPlacer = new RelativeLayout(getContext());
|
|
|
|
final ViewGroup windowContentView =
|
|
|
|
(ViewGroup)getRootView().findViewById(android.R.id.content);
|
|
|
|
windowContentView.addView(mPreviewPlacer);
|
2011-04-18 08:00:58 +00:00
|
|
|
}
|
2011-08-06 06:33:51 +00:00
|
|
|
mPreviewPlacer.addView(
|
2012-04-02 13:55:12 +00:00
|
|
|
keyPreview, ViewLayoutUtils.newLayoutParam(mPreviewPlacer, 0, 0));
|
2011-04-18 08:00:58 +00:00
|
|
|
}
|
|
|
|
|
2012-04-27 14:44:28 +00:00
|
|
|
@Override
|
|
|
|
public void showKeyPreview(PointerTracker tracker) {
|
|
|
|
if (!mShowKeyPreviewPopup) return;
|
|
|
|
|
2011-07-25 21:05:02 +00:00
|
|
|
final TextView previewText = tracker.getKeyPreviewText();
|
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.
|
2011-06-27 15:28:29 +00:00
|
|
|
if (key == null)
|
2010-08-31 11:21:45 +00:00
|
|
|
return;
|
2011-04-27 05:14:45 +00:00
|
|
|
|
2011-06-28 07:32:39 +00:00
|
|
|
final KeyPreviewDrawParams params = mKeyPreviewDrawParams;
|
2012-04-23 09:50:07 +00:00
|
|
|
final String label = key.isShiftedLetterActivated() ? key.mHintLabel : key.mLabel;
|
2012-02-27 07:16:24 +00:00
|
|
|
// What we show as preview should match what we show on a key top in onBufferDraw().
|
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) {
|
2011-06-28 07:32:39 +00:00
|
|
|
previewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, params.mKeyLetterSize);
|
2011-04-27 05:14:45 +00:00
|
|
|
previewText.setTypeface(Typeface.DEFAULT_BOLD);
|
2010-08-20 05:35:02 +00:00
|
|
|
} else {
|
2011-06-28 07:32:39 +00:00
|
|
|
previewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, params.mPreviewTextSize);
|
|
|
|
previewText.setTypeface(params.mKeyTextStyle);
|
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-07-17 22:23:52 +00:00
|
|
|
previewText.setBackgroundDrawable(params.mPreviewBackground);
|
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-04-27 14:44:28 +00:00
|
|
|
final int keyDrawWidth = key.mWidth - key.mVisualInsetsLeft - key.mVisualInsetsRight;
|
|
|
|
final int previewWidth = previewText.getMeasuredWidth();
|
2011-06-28 07:32:39 +00:00
|
|
|
final int previewHeight = params.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.
|
|
|
|
params.mPreviewVisibleWidth = previewWidth - previewText.getPaddingLeft()
|
|
|
|
- previewText.getPaddingRight();
|
|
|
|
params.mPreviewVisibleHeight = previewHeight - previewText.getPaddingTop()
|
|
|
|
- previewText.getPaddingBottom();
|
|
|
|
// The distance between the top edge of the parent key and the bottom of the visible part
|
|
|
|
// of the key preview background.
|
|
|
|
params.mPreviewVisibleOffset = params.mPreviewOffset - previewText.getPaddingBottom();
|
2011-06-28 07:32:39 +00:00
|
|
|
getLocationInWindow(params.mCoordinates);
|
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.
|
|
|
|
int previewX = key.mX + key.mVisualInsetsLeft - (previewWidth - keyDrawWidth) / 2
|
|
|
|
+ params.mCoordinates[0];
|
2011-11-02 02:00:45 +00:00
|
|
|
if (previewX < 0) {
|
2011-06-27 18:01:35 +00:00
|
|
|
previewX = 0;
|
2011-11-02 02:00:45 +00:00
|
|
|
if (params.mPreviewLeftBackground != null) {
|
|
|
|
previewText.setBackgroundDrawable(params.mPreviewLeftBackground);
|
|
|
|
}
|
|
|
|
} else if (previewX > getWidth() - previewWidth) {
|
2011-06-27 18:01:35 +00:00
|
|
|
previewX = getWidth() - previewWidth;
|
2011-11-02 02:00:45 +00:00
|
|
|
if (params.mPreviewRightBackground != null) {
|
|
|
|
previewText.setBackgroundDrawable(params.mPreviewRightBackground);
|
|
|
|
}
|
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.
|
|
|
|
final int previewY = key.mY - previewHeight + params.mPreviewOffset
|
|
|
|
+ params.mCoordinates[1];
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2011-06-27 18:01:35 +00:00
|
|
|
// Set the preview background state
|
|
|
|
previewText.getBackground().setState(
|
2011-08-31 06:26:32 +00:00
|
|
|
key.mMoreKeys != null ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
|
2011-06-28 07:32:39 +00:00
|
|
|
previewText.setTextColor(params.mPreviewTextColor);
|
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;
|
2011-07-21 06:57:00 +00:00
|
|
|
mBufferNeedsUpdate = 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
|
2010-08-31 08:01:21 +00:00
|
|
|
public void invalidateKey(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-03-13 08:28:52 +00:00
|
|
|
mInvalidatedKeysRect.union(x, y, x + key.mWidth, y + key.mHeight);
|
2011-07-21 06:57:00 +00:00
|
|
|
mBufferNeedsUpdate = true;
|
2012-03-13 08:28:52 +00:00
|
|
|
invalidate(mInvalidatedKeysRect);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void closing() {
|
2011-07-25 21:05:02 +00:00
|
|
|
PointerTracker.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();
|
2011-08-06 06:33:51 +00:00
|
|
|
if (mPreviewPlacer != null) {
|
|
|
|
mPreviewPlacer.removeAllViews();
|
|
|
|
}
|
2011-10-14 09:42:10 +00:00
|
|
|
if (mBuffer != null) {
|
|
|
|
mBuffer.recycle();
|
|
|
|
mBuffer = null;
|
|
|
|
}
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
}
|