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-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-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;
|
2011-06-15 04:38:58 +00:00
|
|
|
import com.android.inputmethod.latin.LatinImeLogger;
|
|
|
|
import com.android.inputmethod.latin.R;
|
2012-08-29 13:02:00 +00:00
|
|
|
import com.android.inputmethod.latin.ResourceUtils;
|
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
|
|
|
*
|
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
|
2012-08-28 02:28:42 +00:00
|
|
|
* @attr ref R.styleable#KeyboardView_keyTypeface
|
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 {
|
2012-08-21 01:42:49 +00:00
|
|
|
private static final String TAG = KeyboardView.class.getSimpleName();
|
|
|
|
|
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 };
|
2012-08-29 13:34:29 +00:00
|
|
|
|
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;
|
|
|
|
|
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;
|
2012-08-22 05:15:56 +00:00
|
|
|
private final SparseArray<TextView> mKeyPreviewTexts = CollectionUtils.newSparseArray();
|
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;
|
2012-07-27 10:32:47 +00:00
|
|
|
private final PreviewPlacerView mPreviewPlacerView;
|
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-08-02 06:10:59 +00:00
|
|
|
private Canvas mOffscreenCanvas;
|
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
|
|
|
|
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:
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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-28 02:28:42 +00:00
|
|
|
// Move this class to internal package
|
2012-05-18 08:02:53 +00:00
|
|
|
protected static class KeyDrawParams {
|
|
|
|
// XML attributes
|
|
|
|
public final int mKeyTextColor;
|
|
|
|
public final int mKeyTextInactivatedColor;
|
|
|
|
public final float mKeyLabelHorizontalPadding;
|
|
|
|
public final float mKeyHintLetterPadding;
|
|
|
|
public final float mKeyPopupHintLetterPadding;
|
|
|
|
public final float mKeyShiftedLetterHintPadding;
|
|
|
|
public final int mShadowColor;
|
|
|
|
public final float mShadowRadius;
|
|
|
|
public final Drawable mKeyBackground;
|
|
|
|
public final int mKeyHintLetterColor;
|
|
|
|
public final int mKeyHintLabelColor;
|
|
|
|
public final int mKeyShiftedLetterHintInactivatedColor;
|
|
|
|
public final int mKeyShiftedLetterHintActivatedColor;
|
|
|
|
|
2012-08-28 02:28:42 +00:00
|
|
|
private final Typeface mKeyTypefaceFromKeyboardView;
|
|
|
|
private final float mKeyLetterRatio;
|
2012-08-29 13:34:29 +00:00
|
|
|
private final int mKeyLetterSizeFromKeyboardView;
|
2012-05-18 08:02:53 +00:00
|
|
|
private final float mKeyLargeLetterRatio;
|
|
|
|
private final float mKeyLabelRatio;
|
|
|
|
private final float mKeyLargeLabelRatio;
|
|
|
|
private final float mKeyHintLetterRatio;
|
|
|
|
private final float mKeyShiftedLetterHintRatio;
|
|
|
|
private final float mKeyHintLabelRatio;
|
|
|
|
|
|
|
|
public final Rect mPadding = new Rect();
|
2012-08-28 02:28:42 +00:00
|
|
|
public Typeface mKeyTypeface;
|
2012-05-18 08:02:53 +00:00
|
|
|
public int mKeyLetterSize;
|
|
|
|
public int mKeyLargeLetterSize;
|
|
|
|
public int mKeyLabelSize;
|
|
|
|
public int mKeyLargeLabelSize;
|
|
|
|
public int mKeyHintLetterSize;
|
|
|
|
public int mKeyShiftedLetterHintSize;
|
|
|
|
public int mKeyHintLabelSize;
|
|
|
|
public int mAnimAlpha;
|
|
|
|
|
2012-08-28 03:39:28 +00:00
|
|
|
public KeyDrawParams(final TypedArray a) {
|
2012-05-18 08:02:53 +00:00
|
|
|
mKeyBackground = a.getDrawable(R.styleable.KeyboardView_keyBackground);
|
2012-08-29 13:34:29 +00:00
|
|
|
mKeyLetterRatio = ResourceUtils.getFraction(a, R.styleable.KeyboardView_keyLetterSize);
|
|
|
|
mKeyLetterSizeFromKeyboardView = ResourceUtils.getDimensionPixelSize(a,
|
|
|
|
R.styleable.KeyboardView_keyLetterSize);
|
|
|
|
mKeyLabelRatio = ResourceUtils.getFraction(a, R.styleable.KeyboardView_keyLabelSize);
|
|
|
|
mKeyLabelSize = ResourceUtils.getDimensionPixelSize(a,
|
|
|
|
R.styleable.KeyboardView_keyLabelSize);
|
2012-08-29 13:02:00 +00:00
|
|
|
mKeyLargeLabelRatio = ResourceUtils.getFraction(a,
|
|
|
|
R.styleable.KeyboardView_keyLargeLabelRatio);
|
|
|
|
mKeyLargeLetterRatio = ResourceUtils.getFraction(a,
|
|
|
|
R.styleable.KeyboardView_keyLargeLetterRatio);
|
|
|
|
mKeyHintLetterRatio = ResourceUtils.getFraction(a,
|
|
|
|
R.styleable.KeyboardView_keyHintLetterRatio);
|
|
|
|
mKeyShiftedLetterHintRatio = ResourceUtils.getFraction(a,
|
2012-05-18 08:02:53 +00:00
|
|
|
R.styleable.KeyboardView_keyShiftedLetterHintRatio);
|
2012-08-29 13:02:00 +00:00
|
|
|
mKeyHintLabelRatio = ResourceUtils.getFraction(a,
|
|
|
|
R.styleable.KeyboardView_keyHintLabelRatio);
|
2012-05-18 08:02:53 +00:00
|
|
|
mKeyLabelHorizontalPadding = a.getDimension(
|
|
|
|
R.styleable.KeyboardView_keyLabelHorizontalPadding, 0);
|
|
|
|
mKeyHintLetterPadding = a.getDimension(
|
|
|
|
R.styleable.KeyboardView_keyHintLetterPadding, 0);
|
|
|
|
mKeyPopupHintLetterPadding = a.getDimension(
|
|
|
|
R.styleable.KeyboardView_keyPopupHintLetterPadding, 0);
|
|
|
|
mKeyShiftedLetterHintPadding = a.getDimension(
|
|
|
|
R.styleable.KeyboardView_keyShiftedLetterHintPadding, 0);
|
2012-08-29 13:34:29 +00:00
|
|
|
mKeyTextColor = a.getColor(
|
|
|
|
R.styleable.KeyboardView_keyTextColor, Color.WHITE);
|
2012-05-18 08:02:53 +00:00
|
|
|
mKeyTextInactivatedColor = a.getColor(
|
2012-08-29 13:34:29 +00:00
|
|
|
R.styleable.KeyboardView_keyTextInactivatedColor, Color.WHITE);
|
|
|
|
mKeyHintLetterColor = a.getColor(
|
|
|
|
R.styleable.KeyboardView_keyHintLetterColor, Color.TRANSPARENT);
|
|
|
|
mKeyHintLabelColor = a.getColor(
|
|
|
|
R.styleable.KeyboardView_keyHintLabelColor, Color.TRANSPARENT);
|
2012-05-18 08:02:53 +00:00
|
|
|
mKeyShiftedLetterHintInactivatedColor = a.getColor(
|
2012-08-29 13:34:29 +00:00
|
|
|
R.styleable.KeyboardView_keyShiftedLetterHintInactivatedColor,
|
|
|
|
Color.TRANSPARENT);
|
2012-05-18 08:02:53 +00:00
|
|
|
mKeyShiftedLetterHintActivatedColor = a.getColor(
|
2012-08-29 13:34:29 +00:00
|
|
|
R.styleable.KeyboardView_keyShiftedLetterHintActivatedColor, Color.TRANSPARENT);
|
2012-08-28 02:28:42 +00:00
|
|
|
mKeyTypefaceFromKeyboardView = Typeface.defaultFromStyle(
|
|
|
|
a.getInt(R.styleable.KeyboardView_keyTypeface, Typeface.NORMAL));
|
|
|
|
mKeyTypeface = mKeyTypefaceFromKeyboardView;
|
2012-08-29 13:34:29 +00:00
|
|
|
mShadowColor = a.getColor(R.styleable.KeyboardView_shadowColor, Color.TRANSPARENT);
|
2012-05-18 08:02:53 +00:00
|
|
|
mShadowRadius = a.getFloat(R.styleable.KeyboardView_shadowRadius, 0f);
|
|
|
|
|
|
|
|
mKeyBackground.getPadding(mPadding);
|
|
|
|
}
|
|
|
|
|
2012-08-28 02:28:42 +00:00
|
|
|
public void updateParams(final Keyboard keyboard) {
|
|
|
|
mKeyTypeface = (keyboard.mKeyTypeface != null)
|
|
|
|
? keyboard.mKeyTypeface : mKeyTypefaceFromKeyboardView;
|
|
|
|
final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap;
|
2012-08-29 13:34:29 +00:00
|
|
|
mKeyLetterSize = selectTextSizeFromDimensionOrRatio(keyHeight,
|
|
|
|
mKeyLetterSizeFromKeyboardView, mKeyLetterRatio,
|
|
|
|
mKeyLetterSizeFromKeyboardView);
|
|
|
|
// Override if size/ratio is specified in Keyboard.
|
|
|
|
mKeyLetterSize = selectTextSizeFromDimensionOrRatio(keyHeight, keyboard.mKeyLetterSize,
|
|
|
|
keyboard.mKeyLetterRatio, mKeyLetterSize);
|
2012-08-29 13:02:00 +00:00
|
|
|
if (ResourceUtils.isValidFraction(mKeyLabelRatio)) {
|
2012-05-18 08:02:53 +00:00
|
|
|
mKeyLabelSize = (int)(keyHeight * mKeyLabelRatio);
|
2012-07-30 07:50:03 +00:00
|
|
|
}
|
2012-05-18 08:02:53 +00:00
|
|
|
mKeyLargeLabelSize = (int)(keyHeight * mKeyLargeLabelRatio);
|
|
|
|
mKeyLargeLetterSize = (int)(keyHeight * mKeyLargeLetterRatio);
|
2012-08-29 13:34:29 +00:00
|
|
|
mKeyHintLetterSize = selectTextSizeFromKeyboardOrView(keyHeight,
|
|
|
|
keyboard.mKeyHintLetterRatio, mKeyHintLetterRatio);
|
|
|
|
mKeyShiftedLetterHintSize = selectTextSizeFromKeyboardOrView(keyHeight,
|
|
|
|
keyboard.mKeyShiftedLetterHintRatio, mKeyShiftedLetterHintRatio);
|
2012-05-18 08:02:53 +00:00
|
|
|
mKeyHintLabelSize = (int)(keyHeight * mKeyHintLabelRatio);
|
|
|
|
}
|
|
|
|
|
2012-08-29 13:34:29 +00:00
|
|
|
private static final int selectTextSizeFromDimensionOrRatio(final int keyHeight,
|
|
|
|
final int dimens, final float ratio, final int defaultDimens) {
|
|
|
|
if (ResourceUtils.isValidDimensionPixelSize(dimens)) {
|
|
|
|
return dimens;
|
|
|
|
}
|
|
|
|
if (ResourceUtils.isValidFraction(ratio)) {
|
|
|
|
return (int)(keyHeight * ratio);
|
|
|
|
}
|
|
|
|
return defaultDimens;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final int selectTextSizeFromKeyboardOrView(final int keyHeight,
|
|
|
|
final float ratioFromKeyboard, final float ratioFromView) {
|
|
|
|
final float ratio = ResourceUtils.isValidFraction(ratioFromKeyboard)
|
|
|
|
? ratioFromKeyboard : ratioFromView;
|
|
|
|
return (int)(keyHeight * ratio);
|
|
|
|
}
|
|
|
|
|
2012-08-28 02:28:42 +00:00
|
|
|
public void blendAlpha(final Paint paint) {
|
2012-05-18 08:02:53 +00:00
|
|
|
final int color = paint.getColor();
|
2012-07-19 08:20:54 +00:00
|
|
|
paint.setARGB((paint.getAlpha() * mAnimAlpha) / Constants.Color.ALPHA_OPAQUE,
|
2012-05-18 08:02:53 +00:00
|
|
|
Color.red(color), Color.green(color), Color.blue(color));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 02:28:42 +00:00
|
|
|
// TODO: Move this class to internal package.
|
2012-05-18 08:02:53 +00:00
|
|
|
/* package */ static class KeyPreviewDrawParams {
|
|
|
|
// 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 int mLingerTimeout;
|
|
|
|
|
|
|
|
private final float mPreviewTextRatio;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2012-08-28 02:28:42 +00:00
|
|
|
public Typeface mKeyTypeface;
|
2012-05-18 08:02:53 +00:00
|
|
|
public int mPreviewTextSize;
|
|
|
|
public int mKeyLetterSize;
|
|
|
|
public final int[] mCoordinates = new int[2];
|
|
|
|
|
|
|
|
private static final int PREVIEW_ALPHA = 240;
|
|
|
|
|
2012-08-28 02:28:42 +00:00
|
|
|
public KeyPreviewDrawParams(final TypedArray a) {
|
2012-05-18 08:02:53 +00:00
|
|
|
mPreviewBackground = a.getDrawable(R.styleable.KeyboardView_keyPreviewBackground);
|
|
|
|
mPreviewLeftBackground = a.getDrawable(
|
|
|
|
R.styleable.KeyboardView_keyPreviewLeftBackground);
|
|
|
|
mPreviewRightBackground = a.getDrawable(
|
|
|
|
R.styleable.KeyboardView_keyPreviewRightBackground);
|
|
|
|
setAlpha(mPreviewBackground, PREVIEW_ALPHA);
|
|
|
|
setAlpha(mPreviewLeftBackground, PREVIEW_ALPHA);
|
|
|
|
setAlpha(mPreviewRightBackground, PREVIEW_ALPHA);
|
|
|
|
mPreviewOffset = a.getDimensionPixelOffset(
|
|
|
|
R.styleable.KeyboardView_keyPreviewOffset, 0);
|
|
|
|
mPreviewHeight = a.getDimensionPixelSize(
|
|
|
|
R.styleable.KeyboardView_keyPreviewHeight, 80);
|
2012-08-29 13:02:00 +00:00
|
|
|
mPreviewTextRatio = ResourceUtils.getFraction(a,
|
|
|
|
R.styleable.KeyboardView_keyPreviewTextRatio);
|
2012-05-18 08:02:53 +00:00
|
|
|
mPreviewTextColor = a.getColor(R.styleable.KeyboardView_keyPreviewTextColor, 0);
|
|
|
|
mLingerTimeout = a.getInt(R.styleable.KeyboardView_keyPreviewLingerTimeout, 0);
|
|
|
|
}
|
|
|
|
|
2012-08-28 02:28:42 +00:00
|
|
|
public void updateParams(final Keyboard keyboard, final KeyDrawParams keyDrawParams) {
|
|
|
|
final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap;
|
2012-08-29 13:02:00 +00:00
|
|
|
if (ResourceUtils.isValidFraction(mPreviewTextRatio)) {
|
2012-07-30 07:50:03 +00:00
|
|
|
mPreviewTextSize = (int)(keyHeight * mPreviewTextRatio);
|
|
|
|
}
|
2012-08-28 02:28:42 +00:00
|
|
|
mKeyLetterSize = keyDrawParams.mKeyLetterSize;
|
|
|
|
mKeyTypeface = keyDrawParams.mKeyTypeface;
|
2012-05-18 08:02:53 +00:00
|
|
|
}
|
|
|
|
|
2012-08-28 02:28:42 +00:00
|
|
|
private static void setAlpha(final Drawable drawable, final int alpha) {
|
2012-07-30 07:50:03 +00:00
|
|
|
if (drawable == null) return;
|
2012-05-18 08:02:53 +00:00
|
|
|
drawable.setAlpha(alpha);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2011-06-28 07:32:39 +00:00
|
|
|
mKeyDrawParams = new KeyDrawParams(a);
|
2012-08-28 02:28:42 +00:00
|
|
|
mKeyPreviewDrawParams = new KeyPreviewDrawParams(a);
|
2012-08-20 02:24:16 +00:00
|
|
|
mDelayAfterPreview = mKeyPreviewDrawParams.mLingerTimeout;
|
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-08-20 02:24:16 +00:00
|
|
|
mPreviewPlacerView = new PreviewPlacerView(context, attrs);
|
2010-08-20 05:35:02 +00:00
|
|
|
mPaint.setAntiAlias(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
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-28 02:28:42 +00:00
|
|
|
mKeyDrawParams.updateParams(keyboard);
|
|
|
|
mKeyPreviewDrawParams.updateParams(keyboard, mKeyDrawParams);
|
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
|
|
|
}
|
|
|
|
|
2012-08-08 02:41:58 +00:00
|
|
|
public void setGesturePreviewMode(boolean drawsGesturePreviewTrail,
|
|
|
|
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
|
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);
|
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-08-20 02:24:16 +00:00
|
|
|
maybeCreateOffscreenCanvas();
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 02:24:16 +00:00
|
|
|
private void maybeCreateOffscreenCanvas() {
|
|
|
|
// TODO: Stop using the offscreen canvas even when in software rendering
|
|
|
|
if (mOffscreenCanvas != null) {
|
|
|
|
mOffscreenCanvas.setBitmap(mOffscreenBuffer);
|
|
|
|
} else {
|
|
|
|
mOffscreenCanvas = new Canvas(mOffscreenBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 06:10:59 +00:00
|
|
|
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;
|
2011-06-28 07:32:39 +00:00
|
|
|
final KeyDrawParams params = mKeyDrawParams;
|
2012-03-13 08:28:52 +00:00
|
|
|
|
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-03-13 08:28:52 +00:00
|
|
|
onDrawKey(key, canvas, paint, params);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Draw invalidated keys.
|
|
|
|
for (final Key key : mInvalidatedKeys) {
|
2012-08-02 07:39:27 +00:00
|
|
|
if (mKeyboard.hasKey(key)) {
|
|
|
|
onDrawKey(key, canvas, paint, params);
|
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
|
|
|
}
|
|
|
|
|
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-07-19 08:20:54 +00:00
|
|
|
params.mAnimAlpha = Constants.Color.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-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;
|
2011-06-25 10:38:55 +00:00
|
|
|
// For characters, use large font. For labels like "Done", use smaller font.
|
2012-08-28 02:28:42 +00:00
|
|
|
paint.setTypeface(key.selectTypeface(params.mKeyTypeface));
|
2012-05-18 08:07:14 +00:00
|
|
|
final int labelSize = key.selectTextSize(params.mKeyLetterSize,
|
|
|
|
params.mKeyLargeLetterSize, params.mKeyLabelSize, params.mKeyLargeLabelSize,
|
|
|
|
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-08-28 02:28:42 +00:00
|
|
|
paint.setTypeface(params.mKeyTypeface);
|
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) {
|
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-03-16 07:41:18 +00:00
|
|
|
private float getCharWidth(char[] referenceChar, 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-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-03-16 07:41:18 +00:00
|
|
|
public Paint newDefaultLabelPaint() {
|
|
|
|
final Paint paint = new Paint();
|
|
|
|
paint.setAntiAlias(true);
|
2012-08-28 02:28:42 +00:00
|
|
|
paint.setTypeface(mKeyDrawParams.mKeyTypeface);
|
2012-03-16 07:41:18 +00:00
|
|
|
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();
|
2012-08-07 02:48:10 +00:00
|
|
|
if (mPreviewPlacerView != null) {
|
|
|
|
mPreviewPlacerView.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
|
|
|
|
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) {
|
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-07-20 12:01:44 +00:00
|
|
|
final int[] viewOrigin = new int[2];
|
|
|
|
getLocationInWindow(viewOrigin);
|
2012-07-27 10:32:47 +00:00
|
|
|
mPreviewPlacerView.setOrigin(viewOrigin[0], viewOrigin[1]);
|
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-07-19 08:20:54 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:06:19 +00:00
|
|
|
public void showGestureFloatingPreviewText(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-08-23 06:01:46 +00:00
|
|
|
public void showGesturePreviewTrail(PointerTracker tracker) {
|
2012-07-27 10:32:47 +00:00
|
|
|
locatePreviewPlacerView();
|
|
|
|
mPreviewPlacerView.invalidatePointer(tracker);
|
2012-07-19 08:20:54 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 07:00:02 +00:00
|
|
|
@SuppressWarnings("deprecation") // setBackgroundDrawable is replaced by setBackground in API16
|
2012-04-27 14:44:28 +00:00
|
|
|
@Override
|
|
|
|
public void showKeyPreview(PointerTracker tracker) {
|
|
|
|
if (!mShowKeyPreviewPopup) return;
|
|
|
|
|
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.
|
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-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) {
|
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);
|
2012-08-28 02:28:42 +00:00
|
|
|
previewText.setTypeface(params.mKeyTypeface);
|
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;
|
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-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
|
|
|
}
|
|
|
|
}
|