2009-03-13 22:11:42 +00:00
|
|
|
/*
|
2010-03-26 22:07:10 +00:00
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
2009-03-13 22:11:42 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Resources;
|
2010-01-30 04:09:49 +00:00
|
|
|
import android.content.res.TypedArray;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.content.res.XmlResourceParser;
|
2009-10-12 20:48:35 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.Canvas;
|
2010-01-30 04:09:49 +00:00
|
|
|
import android.graphics.ColorFilter;
|
2009-10-12 20:48:35 +00:00
|
|
|
import android.graphics.Paint;
|
2010-09-06 05:50:50 +00:00
|
|
|
import android.graphics.Paint.Align;
|
2010-01-30 04:09:49 +00:00
|
|
|
import android.graphics.PixelFormat;
|
2009-10-12 20:48:35 +00:00
|
|
|
import android.graphics.PorterDuff;
|
2010-01-30 04:09:49 +00:00
|
|
|
import android.graphics.Rect;
|
2009-10-12 20:48:35 +00:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
2010-01-30 04:09:49 +00:00
|
|
|
import android.text.TextPaint;
|
2010-02-05 22:07:04 +00:00
|
|
|
import android.util.Log;
|
2010-01-30 04:09:49 +00:00
|
|
|
import android.view.ViewConfiguration;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
|
2010-09-27 08:07:09 +00:00
|
|
|
import java.util.HashMap;
|
2010-09-06 05:50:50 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
2010-10-01 10:40:44 +00:00
|
|
|
public class LatinKeyboard extends BaseKeyboard {
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2010-02-05 22:07:04 +00:00
|
|
|
private static final boolean DEBUG_PREFERRED_LETTER = false;
|
|
|
|
private static final String TAG = "LatinKeyboard";
|
2010-09-02 13:54:37 +00:00
|
|
|
private static final int OPACITY_FULLY_OPAQUE = 255;
|
2010-09-03 10:05:23 +00:00
|
|
|
private static final int SPACE_LED_LENGTH_PERCENT = 80;
|
2010-02-05 22:07:04 +00:00
|
|
|
|
2010-11-13 08:16:34 +00:00
|
|
|
private Drawable mShiftedIcon;
|
2009-03-13 22:11:42 +00:00
|
|
|
private Drawable mShiftLockPreviewIcon;
|
2010-11-13 08:16:34 +00:00
|
|
|
private final HashMap<Key, Drawable> mNormalShiftIcons = new HashMap<Key, Drawable>();
|
2009-10-12 20:48:35 +00:00
|
|
|
private Drawable mSpaceIcon;
|
2010-09-02 13:54:37 +00:00
|
|
|
private Drawable mSpaceAutoCompletionIndicator;
|
2010-01-30 04:09:49 +00:00
|
|
|
private Drawable mSpacePreviewIcon;
|
2010-01-28 15:33:27 +00:00
|
|
|
private Drawable mMicIcon;
|
|
|
|
private Drawable mMicPreviewIcon;
|
|
|
|
private Drawable m123MicIcon;
|
|
|
|
private Drawable m123MicPreviewIcon;
|
2010-09-21 13:53:12 +00:00
|
|
|
private final Drawable mButtonArrowLeftIcon;
|
|
|
|
private final Drawable mButtonArrowRightIcon;
|
2009-03-13 22:11:42 +00:00
|
|
|
private Key mEnterKey;
|
2009-10-12 20:48:35 +00:00
|
|
|
private Key mF1Key;
|
2010-10-19 15:18:50 +00:00
|
|
|
private final Drawable mHintIcon;
|
2009-10-12 20:48:35 +00:00
|
|
|
private Key mSpaceKey;
|
2010-02-02 18:42:32 +00:00
|
|
|
private Key m123Key;
|
2010-01-30 04:09:49 +00:00
|
|
|
private int mSpaceKeyIndex = -1;
|
|
|
|
private int mSpaceDragStartX;
|
|
|
|
private int mSpaceDragLastDiff;
|
2010-09-21 13:53:12 +00:00
|
|
|
private final Resources mRes;
|
|
|
|
private final Context mContext;
|
2010-11-11 23:28:14 +00:00
|
|
|
private int mMode; // TODO: remove this and use the corresponding mode in the parent class
|
2010-02-26 19:47:05 +00:00
|
|
|
// Whether this keyboard has voice icon on it
|
|
|
|
private boolean mHasVoiceButton;
|
|
|
|
// Whether voice icon is enabled at all
|
|
|
|
private boolean mVoiceEnabled;
|
2010-09-21 13:53:12 +00:00
|
|
|
private final boolean mIsAlphaKeyboard;
|
2010-02-26 19:47:05 +00:00
|
|
|
private CharSequence m123Label;
|
2010-01-30 04:09:49 +00:00
|
|
|
private boolean mCurrentlyInSpace;
|
|
|
|
private SlidingLocaleDrawable mSlidingLocaleIcon;
|
2010-02-05 22:07:04 +00:00
|
|
|
private int[] mPrefLetterFrequencies;
|
|
|
|
private int mPrefLetter;
|
|
|
|
private int mPrefLetterX;
|
|
|
|
private int mPrefLetterY;
|
|
|
|
private int mPrefDistance;
|
2009-10-22 21:51:39 +00:00
|
|
|
|
2010-10-17 05:19:39 +00:00
|
|
|
// Default Enter key attributes
|
|
|
|
private final Drawable mDefaultEnterIcon;
|
|
|
|
private final Drawable mDefaultEnterPreview;
|
|
|
|
private final CharSequence mDefaultEnterLabel;
|
|
|
|
private final CharSequence mDefaultEnterText;
|
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
// TODO: generalize for any keyboardId
|
|
|
|
private boolean mIsBlackSym;
|
|
|
|
|
2010-11-13 08:16:34 +00:00
|
|
|
private LatinKeyboardShiftState mShiftState = new LatinKeyboardShiftState();
|
2009-08-21 01:35:30 +00:00
|
|
|
|
2010-01-30 04:09:49 +00:00
|
|
|
private static final float SPACEBAR_DRAG_THRESHOLD = 0.8f;
|
2010-02-05 22:07:04 +00:00
|
|
|
private static final float OVERLAP_PERCENTAGE_LOW_PROB = 0.70f;
|
|
|
|
private static final float OVERLAP_PERCENTAGE_HIGH_PROB = 0.85f;
|
2010-09-17 11:25:13 +00:00
|
|
|
// Minimum width of space key preview (proportional to keyboard width)
|
|
|
|
private static final float SPACEBAR_POPUP_MIN_RATIO = 0.4f;
|
|
|
|
// Height in space key the language name will be drawn. (proportional to space key height)
|
|
|
|
private static final float SPACEBAR_LANGUAGE_BASELINE = 0.6f;
|
|
|
|
// If the full language name needs to be smaller than this value to be drawn on space key,
|
|
|
|
// its short language name will be used instead.
|
|
|
|
private static final float MINIMUM_SCALE_OF_LANGUAGE_NAME = 0.8f;
|
2010-01-30 04:09:49 +00:00
|
|
|
|
2010-09-21 13:53:12 +00:00
|
|
|
private static int sSpacebarVerticalCorrection;
|
2009-08-21 01:35:30 +00:00
|
|
|
|
2010-11-11 23:28:14 +00:00
|
|
|
public LatinKeyboard(Context context, KeyboardSwitcher.KeyboardId id) {
|
|
|
|
super(context, id);
|
2009-10-12 20:48:35 +00:00
|
|
|
final Resources res = context.getResources();
|
2010-01-30 04:09:49 +00:00
|
|
|
mContext = context;
|
2009-10-12 20:48:35 +00:00
|
|
|
mRes = res;
|
2010-11-11 23:28:14 +00:00
|
|
|
mMode = id.mMode;
|
2010-11-13 08:16:34 +00:00
|
|
|
mShiftedIcon = res.getDrawable(R.drawable.sym_keyboard_shift_locked);
|
2009-08-21 01:35:30 +00:00
|
|
|
mShiftLockPreviewIcon = res.getDrawable(R.drawable.sym_keyboard_feedback_shift_locked);
|
2010-10-02 06:17:27 +00:00
|
|
|
setDefaultBounds(mShiftLockPreviewIcon);
|
2009-10-12 20:48:35 +00:00
|
|
|
mSpaceIcon = res.getDrawable(R.drawable.sym_keyboard_space);
|
2010-09-02 13:54:37 +00:00
|
|
|
mSpaceAutoCompletionIndicator = res.getDrawable(R.drawable.sym_keyboard_space_led);
|
2010-01-30 04:09:49 +00:00
|
|
|
mSpacePreviewIcon = res.getDrawable(R.drawable.sym_keyboard_feedback_space);
|
2010-01-28 15:33:27 +00:00
|
|
|
mMicIcon = res.getDrawable(R.drawable.sym_keyboard_mic);
|
|
|
|
mMicPreviewIcon = res.getDrawable(R.drawable.sym_keyboard_feedback_mic);
|
2010-02-02 21:04:06 +00:00
|
|
|
setDefaultBounds(mMicPreviewIcon);
|
2010-01-30 04:09:49 +00:00
|
|
|
mButtonArrowLeftIcon = res.getDrawable(R.drawable.sym_keyboard_language_arrows_left);
|
|
|
|
mButtonArrowRightIcon = res.getDrawable(R.drawable.sym_keyboard_language_arrows_right);
|
2010-02-02 18:42:32 +00:00
|
|
|
m123MicIcon = res.getDrawable(R.drawable.sym_keyboard_123_mic);
|
|
|
|
m123MicPreviewIcon = res.getDrawable(R.drawable.sym_keyboard_feedback_123_mic);
|
2010-10-19 15:18:50 +00:00
|
|
|
mHintIcon = res.getDrawable(R.drawable.hint_popup);
|
2010-02-02 21:04:06 +00:00
|
|
|
setDefaultBounds(m123MicPreviewIcon);
|
2009-08-21 01:35:30 +00:00
|
|
|
sSpacebarVerticalCorrection = res.getDimensionPixelOffset(
|
|
|
|
R.dimen.spacebar_vertical_correction);
|
2010-11-11 23:28:14 +00:00
|
|
|
final int xmlLayoutResId = id.getXmlId();
|
2010-08-20 05:35:02 +00:00
|
|
|
mIsAlphaKeyboard = xmlLayoutResId == R.xml.kbd_qwerty
|
|
|
|
|| xmlLayoutResId == R.xml.kbd_qwerty_black;
|
2010-09-21 07:55:18 +00:00
|
|
|
mSpaceKeyIndex = indexOf(LatinIME.KEYCODE_SPACE);
|
2010-10-17 11:46:01 +00:00
|
|
|
|
2010-10-17 05:19:39 +00:00
|
|
|
if (mEnterKey != null) {
|
|
|
|
mDefaultEnterIcon = mEnterKey.icon;
|
|
|
|
mDefaultEnterPreview = mEnterKey.iconPreview;
|
|
|
|
mDefaultEnterLabel = mEnterKey.label;
|
|
|
|
mDefaultEnterText = mEnterKey.text;
|
|
|
|
} else {
|
|
|
|
mDefaultEnterIcon = mDefaultEnterPreview = null;
|
|
|
|
mDefaultEnterLabel = mDefaultEnterText = null;
|
|
|
|
}
|
2010-09-28 00:45:45 +00:00
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
@Override
|
|
|
|
protected Key createKeyFromXml(Resources res, Row parent, int x, int y,
|
|
|
|
XmlResourceParser parser) {
|
|
|
|
Key key = new LatinKey(res, parent, x, y, parser);
|
2009-10-12 20:48:35 +00:00
|
|
|
switch (key.codes[0]) {
|
2010-09-22 09:29:19 +00:00
|
|
|
case LatinIME.KEYCODE_ENTER:
|
2009-03-13 22:11:42 +00:00
|
|
|
mEnterKey = key;
|
2009-10-12 20:48:35 +00:00
|
|
|
break;
|
|
|
|
case LatinKeyboardView.KEYCODE_F1:
|
|
|
|
mF1Key = key;
|
|
|
|
break;
|
2010-09-22 09:29:19 +00:00
|
|
|
case LatinIME.KEYCODE_SPACE:
|
2009-10-12 20:48:35 +00:00
|
|
|
mSpaceKey = key;
|
|
|
|
break;
|
2010-02-02 18:42:32 +00:00
|
|
|
case KEYCODE_MODE_CHANGE:
|
|
|
|
m123Key = key;
|
2010-02-26 19:47:05 +00:00
|
|
|
m123Label = key.label;
|
2010-02-02 18:42:32 +00:00
|
|
|
break;
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-09-20 09:10:54 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
return key;
|
|
|
|
}
|
2009-10-12 20:48:35 +00:00
|
|
|
|
2010-10-12 11:23:08 +00:00
|
|
|
private static void resetKeyAttributes(Key key, CharSequence label) {
|
|
|
|
key.popupCharacters = null;
|
|
|
|
key.popupResId = 0;
|
|
|
|
key.text = null;
|
|
|
|
key.iconPreview = null;
|
|
|
|
key.icon = null;
|
2010-10-19 15:18:50 +00:00
|
|
|
key.hintIcon = null;
|
2010-10-12 11:23:08 +00:00
|
|
|
key.label = label;
|
|
|
|
}
|
|
|
|
|
2010-09-27 08:07:09 +00:00
|
|
|
public void setImeOptions(Resources res, int mode, int options) {
|
2010-10-19 15:18:50 +00:00
|
|
|
mMode = mode;
|
2010-10-12 06:02:44 +00:00
|
|
|
if (mEnterKey == null)
|
|
|
|
return;
|
2010-10-22 13:18:40 +00:00
|
|
|
final boolean configDynamicKeyTopEnterKey = res.getBoolean(
|
2010-10-22 08:19:17 +00:00
|
|
|
R.bool.config_dynamic_key_top_enter_key);
|
2010-10-22 13:18:40 +00:00
|
|
|
if (configDynamicKeyTopEnterKey) {
|
2010-10-22 08:19:17 +00:00
|
|
|
switch (options & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
|
|
|
|
case EditorInfo.IME_ACTION_GO:
|
|
|
|
resetKeyAttributes(mEnterKey, res.getText(R.string.label_go_key));
|
|
|
|
break;
|
|
|
|
case EditorInfo.IME_ACTION_NEXT:
|
|
|
|
resetKeyAttributes(mEnterKey, res.getText(R.string.label_next_key));
|
|
|
|
break;
|
|
|
|
case EditorInfo.IME_ACTION_DONE:
|
|
|
|
resetKeyAttributes(mEnterKey, res.getText(R.string.label_done_key));
|
|
|
|
break;
|
|
|
|
case EditorInfo.IME_ACTION_SEARCH:
|
|
|
|
resetKeyAttributes(mEnterKey, null);
|
|
|
|
mEnterKey.iconPreview = res.getDrawable(R.drawable.sym_keyboard_feedback_search);
|
|
|
|
mEnterKey.icon = res.getDrawable(mIsBlackSym ? R.drawable.sym_bkeyboard_search
|
|
|
|
: R.drawable.sym_keyboard_search);
|
|
|
|
break;
|
|
|
|
case EditorInfo.IME_ACTION_SEND:
|
|
|
|
resetKeyAttributes(mEnterKey, res.getText(R.string.label_send_key));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
resetKeyAttributes(mEnterKey, mDefaultEnterLabel);
|
|
|
|
mEnterKey.text = mDefaultEnterText;
|
|
|
|
mEnterKey.icon = mDefaultEnterIcon;
|
|
|
|
mEnterKey.iconPreview = mDefaultEnterPreview;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-10-12 06:02:44 +00:00
|
|
|
// Set the initial size of the preview icon
|
|
|
|
setDefaultBounds(mEnterKey.iconPreview);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-09-27 08:07:09 +00:00
|
|
|
|
|
|
|
public void enableShiftLock() {
|
2010-10-02 06:50:06 +00:00
|
|
|
for (final Key key : getShiftKeys()) {
|
2010-09-27 08:07:09 +00:00
|
|
|
if (key instanceof LatinKey) {
|
|
|
|
((LatinKey)key).enableShiftLock();
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-11-13 08:16:34 +00:00
|
|
|
mNormalShiftIcons.put(key, key.icon);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-13 08:16:34 +00:00
|
|
|
public boolean setShiftLocked(boolean newShiftLockState) {
|
2010-10-02 06:50:06 +00:00
|
|
|
for (final Key key : getShiftKeys()) {
|
2010-11-13 08:16:34 +00:00
|
|
|
key.on = newShiftLockState;
|
|
|
|
key.icon = newShiftLockState ? mShiftedIcon : mNormalShiftIcons.get(key);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-11-13 08:16:34 +00:00
|
|
|
mShiftState.setShiftLocked(newShiftLockState);
|
2010-11-13 08:16:34 +00:00
|
|
|
return true;
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2010-09-27 08:07:09 +00:00
|
|
|
public boolean isShiftLocked() {
|
2010-11-13 08:16:34 +00:00
|
|
|
return mShiftState.isShiftLocked();
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-10-02 06:17:27 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
@Override
|
2010-11-13 08:16:34 +00:00
|
|
|
public boolean setShifted(boolean newShiftState) {
|
|
|
|
if (getShiftKeys().size() == 0)
|
|
|
|
return super.setShifted(newShiftState);
|
|
|
|
|
|
|
|
for (final Key key : getShiftKeys()) {
|
|
|
|
if (!newShiftState && !mShiftState.isShiftLocked()) {
|
|
|
|
key.icon = mNormalShiftIcons.get(key);
|
|
|
|
} else if (newShiftState && !mShiftState.isShiftedOrShiftLocked()) {
|
|
|
|
key.icon = mShiftedIcon;
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-13 08:16:34 +00:00
|
|
|
return mShiftState.setShifted(newShiftState);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2009-10-12 20:48:35 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
@Override
|
2010-11-13 08:16:34 +00:00
|
|
|
public boolean isShiftedOrShiftLocked() {
|
2010-10-02 06:50:06 +00:00
|
|
|
if (getShiftKeys().size() > 0) {
|
2010-11-13 08:16:34 +00:00
|
|
|
return mShiftState.isShiftedOrShiftLocked();
|
2009-03-13 22:11:42 +00:00
|
|
|
} else {
|
2010-11-13 08:16:34 +00:00
|
|
|
return super.isShiftedOrShiftLocked();
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-13 08:16:34 +00:00
|
|
|
public void setAutomaticTemporaryUpperCase() {
|
|
|
|
setShifted(true);
|
|
|
|
mShiftState.setAutomaticTemporaryUpperCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAutomaticTemporaryUpperCase() {
|
|
|
|
return mIsAlphaKeyboard && mShiftState.isAutomaticTemporaryUpperCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isManualTemporaryUpperCase() {
|
|
|
|
return mIsAlphaKeyboard && mShiftState.isManualTemporaryUpperCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* package */ LatinKeyboardShiftState getKeyboardShiftState() {
|
|
|
|
return mShiftState;
|
2010-10-02 06:17:27 +00:00
|
|
|
}
|
|
|
|
|
2010-10-17 11:46:01 +00:00
|
|
|
public boolean isAlphaKeyboard() {
|
2010-08-20 05:35:02 +00:00
|
|
|
return mIsAlphaKeyboard;
|
|
|
|
}
|
|
|
|
|
2010-11-11 07:03:19 +00:00
|
|
|
public void setColorOfSymbolIcons(boolean isBlack) {
|
2010-09-02 13:54:37 +00:00
|
|
|
mIsBlackSym = isBlack;
|
2010-10-22 13:18:40 +00:00
|
|
|
final Resources res = mRes;
|
2010-09-02 13:54:37 +00:00
|
|
|
if (isBlack) {
|
2010-11-13 08:16:34 +00:00
|
|
|
mShiftedIcon = res.getDrawable(R.drawable.sym_bkeyboard_shift_locked);
|
2010-10-22 13:18:40 +00:00
|
|
|
mSpaceIcon = res.getDrawable(R.drawable.sym_bkeyboard_space);
|
|
|
|
mMicIcon = res.getDrawable(R.drawable.sym_bkeyboard_mic);
|
|
|
|
m123MicIcon = res.getDrawable(R.drawable.sym_bkeyboard_123_mic);
|
2010-08-20 05:35:02 +00:00
|
|
|
} else {
|
2010-11-13 08:16:34 +00:00
|
|
|
mShiftedIcon = res.getDrawable(R.drawable.sym_keyboard_shift_locked);
|
2010-10-22 13:18:40 +00:00
|
|
|
mSpaceIcon = res.getDrawable(R.drawable.sym_keyboard_space);
|
|
|
|
mMicIcon = res.getDrawable(R.drawable.sym_keyboard_mic);
|
|
|
|
m123MicIcon = res.getDrawable(R.drawable.sym_keyboard_123_mic);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
2010-10-19 15:18:50 +00:00
|
|
|
updateDynamicKeys();
|
2010-02-02 21:04:06 +00:00
|
|
|
}
|
|
|
|
|
2010-02-26 19:47:05 +00:00
|
|
|
public void setVoiceMode(boolean hasVoiceButton, boolean hasVoice) {
|
|
|
|
mHasVoiceButton = hasVoiceButton;
|
|
|
|
mVoiceEnabled = hasVoice;
|
2010-10-19 15:18:50 +00:00
|
|
|
updateDynamicKeys();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateDynamicKeys() {
|
|
|
|
update123Key();
|
2010-02-26 19:47:05 +00:00
|
|
|
updateF1Key();
|
|
|
|
}
|
|
|
|
|
2010-10-19 15:18:50 +00:00
|
|
|
private void update123Key() {
|
2010-10-22 13:18:40 +00:00
|
|
|
final boolean configDynamicKeyTopSymbolKey = mRes.getBoolean(
|
|
|
|
R.bool.config_dynamic_key_top_symbol_key);
|
2010-10-19 15:18:50 +00:00
|
|
|
// Update KEYCODE_MODE_CHANGE key only on alphabet mode, not on symbol mode.
|
2010-02-26 19:47:05 +00:00
|
|
|
if (m123Key != null && mIsAlphaKeyboard) {
|
2010-10-22 13:18:40 +00:00
|
|
|
if (configDynamicKeyTopSymbolKey && mVoiceEnabled && !mHasVoiceButton) {
|
2010-02-02 18:42:32 +00:00
|
|
|
m123Key.icon = m123MicIcon;
|
|
|
|
m123Key.iconPreview = m123MicPreviewIcon;
|
2010-02-02 21:04:06 +00:00
|
|
|
m123Key.label = null;
|
2010-02-26 19:47:05 +00:00
|
|
|
} else {
|
|
|
|
m123Key.icon = null;
|
|
|
|
m123Key.iconPreview = null;
|
|
|
|
m123Key.label = m123Label;
|
2010-02-02 18:42:32 +00:00
|
|
|
}
|
2010-02-26 19:47:05 +00:00
|
|
|
}
|
2010-10-19 15:18:50 +00:00
|
|
|
}
|
2010-02-26 19:47:05 +00:00
|
|
|
|
2010-10-19 15:18:50 +00:00
|
|
|
private void updateF1Key() {
|
|
|
|
// Update KEYCODE_F1 key. Please note that some keyboard layouts have no F1 key.
|
|
|
|
if (mF1Key == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mIsAlphaKeyboard) {
|
|
|
|
if (mMode == KeyboardSwitcher.MODE_URL) {
|
|
|
|
setNonMicF1Key(mF1Key, "/", R.xml.popup_slash);
|
|
|
|
} else if (mMode == KeyboardSwitcher.MODE_EMAIL) {
|
|
|
|
setNonMicF1Key(mF1Key, "@", R.xml.popup_at);
|
|
|
|
} else {
|
|
|
|
if (mVoiceEnabled && mHasVoiceButton) {
|
|
|
|
setMicF1Key(mF1Key);
|
|
|
|
} else {
|
|
|
|
setNonMicF1Key(mF1Key, ",", R.xml.popup_comma);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // Symbols keyboard
|
|
|
|
if (mVoiceEnabled && mHasVoiceButton) {
|
|
|
|
setMicF1Key(mF1Key);
|
|
|
|
} else {
|
|
|
|
setNonMicF1Key(mF1Key, ",", R.xml.popup_comma);
|
|
|
|
}
|
2010-01-28 15:33:27 +00:00
|
|
|
}
|
2010-01-22 21:57:20 +00:00
|
|
|
}
|
|
|
|
|
2010-10-19 15:18:50 +00:00
|
|
|
private void setMicF1Key(Key key) {
|
|
|
|
key.label = null;
|
|
|
|
key.codes = new int[] { LatinKeyboardView.KEYCODE_VOICE };
|
|
|
|
key.popupResId = R.xml.popup_mic;
|
|
|
|
key.icon = mMicIcon;
|
|
|
|
key.hintIcon = mHintIcon;
|
|
|
|
key.iconPreview = mMicPreviewIcon;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setNonMicF1Key(Key key, String label, int popupResId) {
|
|
|
|
key.label = label;
|
|
|
|
key.codes = new int[] { label.charAt(0) };
|
|
|
|
key.popupResId = popupResId;
|
|
|
|
key.icon = null;
|
|
|
|
key.hintIcon = mHintIcon;
|
|
|
|
key.iconPreview = null;
|
|
|
|
}
|
|
|
|
|
2010-09-02 13:54:37 +00:00
|
|
|
/**
|
|
|
|
* @return a key which should be invalidated.
|
|
|
|
*/
|
|
|
|
public Key onAutoCompletionStateChanged(boolean isAutoCompletion) {
|
|
|
|
updateSpaceBarForLocale(isAutoCompletion, mIsBlackSym);
|
|
|
|
return mSpaceKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateSpaceBarForLocale(boolean isAutoCompletion, boolean isBlack) {
|
2010-10-22 13:18:40 +00:00
|
|
|
final Resources res = mRes;
|
2010-09-03 10:05:23 +00:00
|
|
|
// If application locales are explicitly selected.
|
2010-11-17 22:58:14 +00:00
|
|
|
if (SubtypeSwitcher.getInstance().needsToDisplayLanguage()) {
|
2010-10-22 13:18:40 +00:00
|
|
|
mSpaceKey.icon = new BitmapDrawable(res,
|
2010-09-03 10:05:23 +00:00
|
|
|
drawSpaceBar(OPACITY_FULLY_OPAQUE, isAutoCompletion, isBlack));
|
2009-10-12 20:48:35 +00:00
|
|
|
} else {
|
2010-09-02 13:54:37 +00:00
|
|
|
// sym_keyboard_space_led can be shared with Black and White symbol themes.
|
2010-09-03 10:05:23 +00:00
|
|
|
if (isAutoCompletion) {
|
2010-10-22 13:18:40 +00:00
|
|
|
mSpaceKey.icon = new BitmapDrawable(res,
|
2010-09-03 10:05:23 +00:00
|
|
|
drawSpaceBar(OPACITY_FULLY_OPAQUE, isAutoCompletion, isBlack));
|
|
|
|
} else {
|
2010-10-22 13:18:40 +00:00
|
|
|
mSpaceKey.icon = isBlack ? res.getDrawable(R.drawable.sym_bkeyboard_space)
|
|
|
|
: res.getDrawable(R.drawable.sym_keyboard_space);
|
2010-09-03 10:05:23 +00:00
|
|
|
}
|
2009-10-12 20:48:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-17 11:25:13 +00:00
|
|
|
// Compute width of text with specified text size using paint.
|
|
|
|
private static int getTextWidth(Paint paint, String text, float textSize, Rect bounds) {
|
|
|
|
paint.setTextSize(textSize);
|
|
|
|
paint.getTextBounds(text, 0, text.length(), bounds);
|
|
|
|
return bounds.width();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Layout local language name and left and right arrow on space bar.
|
|
|
|
private static String layoutSpaceBar(Paint paint, Locale locale, Drawable lArrow,
|
|
|
|
Drawable rArrow, int width, int height, float origTextSize,
|
|
|
|
boolean allowVariableTextSize) {
|
|
|
|
final float arrowWidth = lArrow.getIntrinsicWidth();
|
|
|
|
final float arrowHeight = lArrow.getIntrinsicHeight();
|
|
|
|
final float maxTextWidth = width - (arrowWidth + arrowWidth);
|
|
|
|
final Rect bounds = new Rect();
|
|
|
|
|
|
|
|
// Estimate appropriate language name text size to fit in maxTextWidth.
|
2010-11-18 00:55:23 +00:00
|
|
|
String language = SubtypeSwitcher.getDisplayLanguage(locale);
|
2010-09-17 11:25:13 +00:00
|
|
|
int textWidth = getTextWidth(paint, language, origTextSize, bounds);
|
|
|
|
// Assuming text width and text size are proportional to each other.
|
|
|
|
float textSize = origTextSize * Math.min(maxTextWidth / textWidth, 1.0f);
|
|
|
|
|
|
|
|
final boolean useShortName;
|
|
|
|
if (allowVariableTextSize) {
|
|
|
|
textWidth = getTextWidth(paint, language, textSize, bounds);
|
|
|
|
// If text size goes too small or text does not fit, use short name
|
|
|
|
useShortName = textSize / origTextSize < MINIMUM_SCALE_OF_LANGUAGE_NAME
|
|
|
|
|| textWidth > maxTextWidth;
|
|
|
|
} else {
|
|
|
|
useShortName = textWidth > maxTextWidth;
|
|
|
|
textSize = origTextSize;
|
|
|
|
}
|
|
|
|
if (useShortName) {
|
2010-11-18 00:55:23 +00:00
|
|
|
language = SubtypeSwitcher.getShortDisplayLanguage(locale);
|
2010-09-17 11:25:13 +00:00
|
|
|
textWidth = getTextWidth(paint, language, origTextSize, bounds);
|
|
|
|
textSize = origTextSize * Math.min(maxTextWidth / textWidth, 1.0f);
|
|
|
|
}
|
|
|
|
paint.setTextSize(textSize);
|
|
|
|
|
|
|
|
// Place left and right arrow just before and after language text.
|
|
|
|
final float baseline = height * SPACEBAR_LANGUAGE_BASELINE;
|
|
|
|
final int top = (int)(baseline - arrowHeight);
|
|
|
|
final float remains = (width - textWidth) / 2;
|
|
|
|
lArrow.setBounds((int)(remains - arrowWidth), top, (int)remains, (int)baseline);
|
|
|
|
rArrow.setBounds((int)(remains + textWidth), top, (int)(remains + textWidth + arrowWidth),
|
|
|
|
(int)baseline);
|
|
|
|
|
|
|
|
return language;
|
|
|
|
}
|
|
|
|
|
2010-09-02 13:54:37 +00:00
|
|
|
private Bitmap drawSpaceBar(int opacity, boolean isAutoCompletion, boolean isBlack) {
|
2010-09-17 11:25:13 +00:00
|
|
|
final int width = mSpaceKey.width;
|
|
|
|
final int height = mSpaceIcon.getIntrinsicHeight();
|
|
|
|
final Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
|
|
|
final Canvas canvas = new Canvas(buffer);
|
2010-10-22 13:18:40 +00:00
|
|
|
final Resources res = mRes;
|
|
|
|
canvas.drawColor(res.getColor(R.color.latinkeyboard_transparent), PorterDuff.Mode.CLEAR);
|
2010-09-17 11:25:13 +00:00
|
|
|
|
2010-11-17 22:58:14 +00:00
|
|
|
SubtypeSwitcher subtypeSwitcher = SubtypeSwitcher.getInstance();
|
2010-09-03 10:05:23 +00:00
|
|
|
// If application locales are explicitly selected.
|
2010-11-17 22:58:14 +00:00
|
|
|
if (subtypeSwitcher.needsToDisplayLanguage()) {
|
2010-09-17 11:25:13 +00:00
|
|
|
final Paint paint = new Paint();
|
2010-09-03 10:05:23 +00:00
|
|
|
paint.setAlpha(opacity);
|
2010-09-17 11:25:13 +00:00
|
|
|
paint.setAntiAlias(true);
|
2010-09-03 10:05:23 +00:00
|
|
|
paint.setTextAlign(Align.CENTER);
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2010-09-17 11:25:13 +00:00
|
|
|
final boolean allowVariableTextSize = true;
|
2010-11-17 22:58:14 +00:00
|
|
|
final String language = layoutSpaceBar(paint, subtypeSwitcher.getInputLocale(),
|
2010-09-17 11:25:13 +00:00
|
|
|
mButtonArrowLeftIcon, mButtonArrowRightIcon, width, height,
|
|
|
|
getTextSizeFromTheme(android.R.style.TextAppearance_Small, 14),
|
|
|
|
allowVariableTextSize);
|
|
|
|
|
|
|
|
// Draw language text with shadow
|
2010-10-22 13:18:40 +00:00
|
|
|
final int shadowColor = res.getColor(isBlack
|
2010-09-17 11:25:13 +00:00
|
|
|
? R.color.latinkeyboard_bar_language_shadow_black
|
|
|
|
: R.color.latinkeyboard_bar_language_shadow_white);
|
|
|
|
final float baseline = height * SPACEBAR_LANGUAGE_BASELINE;
|
|
|
|
final float descent = paint.descent();
|
2010-09-03 10:05:23 +00:00
|
|
|
paint.setColor(shadowColor);
|
2010-09-17 11:25:13 +00:00
|
|
|
canvas.drawText(language, width / 2, baseline - descent - 1, paint);
|
2010-10-22 13:18:40 +00:00
|
|
|
paint.setColor(res.getColor(R.color.latinkeyboard_bar_language_text));
|
2010-09-17 11:25:13 +00:00
|
|
|
canvas.drawText(language, width / 2, baseline - descent, paint);
|
|
|
|
|
|
|
|
// Put arrows that are already layed out on either side of the text
|
2010-11-17 22:58:14 +00:00
|
|
|
if (SubtypeSwitcher.USE_SPACEBAR_LANGUAGE_SWITCHER
|
|
|
|
&& subtypeSwitcher.getEnabledKeyboardLocaleCount() > 1) {
|
2010-09-17 11:25:13 +00:00
|
|
|
mButtonArrowLeftIcon.draw(canvas);
|
|
|
|
mButtonArrowRightIcon.draw(canvas);
|
2010-09-03 10:05:23 +00:00
|
|
|
}
|
2010-02-06 00:24:40 +00:00
|
|
|
}
|
2010-09-17 11:25:13 +00:00
|
|
|
|
2010-02-06 00:24:40 +00:00
|
|
|
// Draw the spacebar icon at the bottom
|
2010-09-02 13:54:37 +00:00
|
|
|
if (isAutoCompletion) {
|
2010-09-03 10:05:23 +00:00
|
|
|
final int iconWidth = width * SPACE_LED_LENGTH_PERCENT / 100;
|
2010-09-02 13:54:37 +00:00
|
|
|
final int iconHeight = mSpaceAutoCompletionIndicator.getIntrinsicHeight();
|
|
|
|
int x = (width - iconWidth) / 2;
|
|
|
|
int y = height - iconHeight;
|
|
|
|
mSpaceAutoCompletionIndicator.setBounds(x, y, x + iconWidth, y + iconHeight);
|
|
|
|
mSpaceAutoCompletionIndicator.draw(canvas);
|
|
|
|
} else {
|
|
|
|
final int iconWidth = mSpaceIcon.getIntrinsicWidth();
|
|
|
|
final int iconHeight = mSpaceIcon.getIntrinsicHeight();
|
|
|
|
int x = (width - iconWidth) / 2;
|
|
|
|
int y = height - iconHeight;
|
|
|
|
mSpaceIcon.setBounds(x, y, x + iconWidth, y + iconHeight);
|
|
|
|
mSpaceIcon.draw(canvas);
|
|
|
|
}
|
|
|
|
return buffer;
|
2010-02-06 00:24:40 +00:00
|
|
|
}
|
|
|
|
|
2010-01-30 04:09:49 +00:00
|
|
|
private void updateLocaleDrag(int diff) {
|
|
|
|
if (mSlidingLocaleIcon == null) {
|
2010-09-17 11:25:13 +00:00
|
|
|
final int width = Math.max(mSpaceKey.width,
|
|
|
|
(int)(getMinWidth() * SPACEBAR_POPUP_MIN_RATIO));
|
|
|
|
final int height = mSpacePreviewIcon.getIntrinsicHeight();
|
|
|
|
mSlidingLocaleIcon = new SlidingLocaleDrawable(mSpacePreviewIcon, width, height);
|
|
|
|
mSlidingLocaleIcon.setBounds(0, 0, width, height);
|
2010-01-30 04:09:49 +00:00
|
|
|
mSpaceKey.iconPreview = mSlidingLocaleIcon;
|
|
|
|
}
|
|
|
|
mSlidingLocaleIcon.setDiff(diff);
|
|
|
|
if (Math.abs(diff) == Integer.MAX_VALUE) {
|
|
|
|
mSpaceKey.iconPreview = mSpacePreviewIcon;
|
|
|
|
} else {
|
|
|
|
mSpaceKey.iconPreview = mSlidingLocaleIcon;
|
|
|
|
}
|
|
|
|
mSpaceKey.iconPreview.invalidateSelf();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getLanguageChangeDirection() {
|
2010-11-17 22:58:14 +00:00
|
|
|
if (mSpaceKey == null || SubtypeSwitcher.getInstance().getEnabledKeyboardLocaleCount() <= 1
|
|
|
|
|| Math.abs(mSpaceDragLastDiff) < mSpaceKey.width * SPACEBAR_DRAG_THRESHOLD) {
|
2010-01-30 04:09:49 +00:00
|
|
|
return 0; // No change
|
|
|
|
}
|
|
|
|
return mSpaceDragLastDiff > 0 ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isCurrentlyInSpace() {
|
|
|
|
return mCurrentlyInSpace;
|
|
|
|
}
|
|
|
|
|
2010-10-17 11:46:01 +00:00
|
|
|
public void setPreferredLetters(int[] frequencies) {
|
2010-02-05 22:07:04 +00:00
|
|
|
mPrefLetterFrequencies = frequencies;
|
|
|
|
mPrefLetter = 0;
|
|
|
|
}
|
|
|
|
|
2010-10-17 11:46:01 +00:00
|
|
|
public void keyReleased() {
|
2010-01-30 04:09:49 +00:00
|
|
|
mCurrentlyInSpace = false;
|
|
|
|
mSpaceDragLastDiff = 0;
|
2010-02-05 22:07:04 +00:00
|
|
|
mPrefLetter = 0;
|
|
|
|
mPrefLetterX = 0;
|
|
|
|
mPrefLetterY = 0;
|
|
|
|
mPrefDistance = Integer.MAX_VALUE;
|
2010-01-30 04:09:49 +00:00
|
|
|
if (mSpaceKey != null) {
|
|
|
|
updateLocaleDrag(Integer.MAX_VALUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the magic of locking the touch gesture into the spacebar when
|
|
|
|
* switching input languages.
|
|
|
|
*/
|
2010-10-17 11:46:01 +00:00
|
|
|
public boolean isInside(LatinKey key, int x, int y) {
|
2010-01-30 04:09:49 +00:00
|
|
|
final int code = key.codes[0];
|
2010-10-17 11:46:01 +00:00
|
|
|
if (code == KEYCODE_SHIFT || code == KEYCODE_DELETE) {
|
2010-01-30 04:09:49 +00:00
|
|
|
y -= key.height / 10;
|
|
|
|
if (code == KEYCODE_SHIFT) x += key.width / 6;
|
|
|
|
if (code == KEYCODE_DELETE) x -= key.width / 6;
|
|
|
|
} else if (code == LatinIME.KEYCODE_SPACE) {
|
|
|
|
y += LatinKeyboard.sSpacebarVerticalCorrection;
|
2010-11-17 22:58:14 +00:00
|
|
|
if (SubtypeSwitcher.USE_SPACEBAR_LANGUAGE_SWITCHER
|
|
|
|
&& SubtypeSwitcher.getInstance().getEnabledKeyboardLocaleCount() > 1) {
|
2010-01-30 04:09:49 +00:00
|
|
|
if (mCurrentlyInSpace) {
|
|
|
|
int diff = x - mSpaceDragStartX;
|
|
|
|
if (Math.abs(diff - mSpaceDragLastDiff) > 0) {
|
|
|
|
updateLocaleDrag(diff);
|
|
|
|
}
|
|
|
|
mSpaceDragLastDiff = diff;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
boolean insideSpace = key.isInsideSuper(x, y);
|
|
|
|
if (insideSpace) {
|
|
|
|
mCurrentlyInSpace = true;
|
|
|
|
mSpaceDragStartX = x;
|
|
|
|
updateLocaleDrag(0);
|
|
|
|
}
|
|
|
|
return insideSpace;
|
|
|
|
}
|
|
|
|
}
|
2010-02-05 22:07:04 +00:00
|
|
|
} else if (mPrefLetterFrequencies != null) {
|
|
|
|
// New coordinate? Reset
|
|
|
|
if (mPrefLetterX != x || mPrefLetterY != y) {
|
|
|
|
mPrefLetter = 0;
|
|
|
|
mPrefDistance = Integer.MAX_VALUE;
|
|
|
|
}
|
|
|
|
// Handle preferred next letter
|
|
|
|
final int[] pref = mPrefLetterFrequencies;
|
|
|
|
if (mPrefLetter > 0) {
|
2010-08-20 05:35:02 +00:00
|
|
|
if (DEBUG_PREFERRED_LETTER) {
|
|
|
|
if (mPrefLetter == code && !key.isInsideSuper(x, y)) {
|
|
|
|
Log.d(TAG, "CORRECTED !!!!!!");
|
|
|
|
}
|
2010-02-05 22:07:04 +00:00
|
|
|
}
|
|
|
|
return mPrefLetter == code;
|
|
|
|
} else {
|
|
|
|
final boolean inside = key.isInsideSuper(x, y);
|
|
|
|
int[] nearby = getNearestKeys(x, y);
|
|
|
|
List<Key> nearbyKeys = getKeys();
|
|
|
|
if (inside) {
|
|
|
|
// If it's a preferred letter
|
|
|
|
if (inPrefList(code, pref)) {
|
|
|
|
// Check if its frequency is much lower than a nearby key
|
|
|
|
mPrefLetter = code;
|
|
|
|
mPrefLetterX = x;
|
|
|
|
mPrefLetterY = y;
|
|
|
|
for (int i = 0; i < nearby.length; i++) {
|
|
|
|
Key k = nearbyKeys.get(nearby[i]);
|
|
|
|
if (k != key && inPrefList(k.codes[0], pref)) {
|
|
|
|
final int dist = distanceFrom(k, x, y);
|
|
|
|
if (dist < (int) (k.width * OVERLAP_PERCENTAGE_LOW_PROB) &&
|
|
|
|
(pref[k.codes[0]] > pref[mPrefLetter] * 3)) {
|
|
|
|
mPrefLetter = k.codes[0];
|
|
|
|
mPrefDistance = dist;
|
|
|
|
if (DEBUG_PREFERRED_LETTER) {
|
|
|
|
Log.d(TAG, "CORRECTED ALTHOUGH PREFERRED !!!!!!");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mPrefLetter == code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the surrounding keys and intersect with the preferred list
|
|
|
|
// For all in the intersection
|
|
|
|
// if distance from touch point is within a reasonable distance
|
|
|
|
// make this the pref letter
|
|
|
|
// If no pref letter
|
|
|
|
// return inside;
|
|
|
|
// else return thiskey == prefletter;
|
|
|
|
|
|
|
|
for (int i = 0; i < nearby.length; i++) {
|
|
|
|
Key k = nearbyKeys.get(nearby[i]);
|
|
|
|
if (inPrefList(k.codes[0], pref)) {
|
|
|
|
final int dist = distanceFrom(k, x, y);
|
|
|
|
if (dist < (int) (k.width * OVERLAP_PERCENTAGE_HIGH_PROB)
|
|
|
|
&& dist < mPrefDistance) {
|
|
|
|
mPrefLetter = k.codes[0];
|
|
|
|
mPrefLetterX = x;
|
|
|
|
mPrefLetterY = y;
|
|
|
|
mPrefDistance = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Didn't find any
|
|
|
|
if (mPrefLetter == 0) {
|
|
|
|
return inside;
|
|
|
|
} else {
|
|
|
|
return mPrefLetter == code;
|
|
|
|
}
|
|
|
|
}
|
2010-01-30 04:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lock into the spacebar
|
|
|
|
if (mCurrentlyInSpace) return false;
|
|
|
|
|
|
|
|
return key.isInsideSuper(x, y);
|
|
|
|
}
|
|
|
|
|
2010-02-05 22:07:04 +00:00
|
|
|
private boolean inPrefList(int code, int[] pref) {
|
|
|
|
if (code < pref.length && code >= 0) return pref[code] > 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int distanceFrom(Key k, int x, int y) {
|
|
|
|
if (y > k.y && y < k.y + k.height) {
|
|
|
|
return Math.abs(k.x + k.width / 2 - x);
|
|
|
|
} else {
|
|
|
|
return Integer.MAX_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-30 04:09:49 +00:00
|
|
|
@Override
|
|
|
|
public int[] getNearestKeys(int x, int y) {
|
|
|
|
if (mCurrentlyInSpace) {
|
|
|
|
return new int[] { mSpaceKeyIndex };
|
|
|
|
} else {
|
2010-10-29 09:59:37 +00:00
|
|
|
// Avoid dead pixels at edges of the keyboard
|
|
|
|
return super.getNearestKeys(Math.max(0, Math.min(x, getMinWidth() - 1)),
|
|
|
|
Math.max(0, Math.min(y, getHeight() - 1)));
|
2010-01-30 04:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int indexOf(int code) {
|
|
|
|
List<Key> keys = getKeys();
|
|
|
|
int count = keys.size();
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (keys.get(i).codes[0] == code) return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getTextSizeFromTheme(int style, int defValue) {
|
|
|
|
TypedArray array = mContext.getTheme().obtainStyledAttributes(
|
|
|
|
style, new int[] { android.R.attr.textSize });
|
|
|
|
int textSize = array.getDimensionPixelSize(array.getResourceId(0, 0), defValue);
|
|
|
|
return textSize;
|
|
|
|
}
|
|
|
|
|
2010-10-17 11:46:01 +00:00
|
|
|
public static class LatinKey extends BaseKeyboard.Key {
|
2010-09-01 14:33:43 +00:00
|
|
|
|
|
|
|
// functional normal state (with properties)
|
|
|
|
private final int[] KEY_STATE_FUNCTIONAL_NORMAL = {
|
|
|
|
android.R.attr.state_single
|
|
|
|
};
|
|
|
|
|
|
|
|
// functional pressed state (with properties)
|
|
|
|
private final int[] KEY_STATE_FUNCTIONAL_PRESSED = {
|
|
|
|
android.R.attr.state_single,
|
|
|
|
android.R.attr.state_pressed
|
|
|
|
};
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
private boolean mShiftLockEnabled;
|
2010-09-01 14:33:43 +00:00
|
|
|
|
2010-10-01 10:40:44 +00:00
|
|
|
public LatinKey(Resources res, BaseKeyboard.Row parent, int x, int y,
|
2009-03-13 22:11:42 +00:00
|
|
|
XmlResourceParser parser) {
|
|
|
|
super(res, parent, x, y, parser);
|
2009-03-25 23:58:32 +00:00
|
|
|
if (popupCharacters != null && popupCharacters.length() == 0) {
|
|
|
|
// If there is a keyboard with no keys specified in popupCharacters
|
|
|
|
popupResId = 0;
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-09-01 14:33:43 +00:00
|
|
|
|
|
|
|
private void enableShiftLock() {
|
2009-03-13 22:11:42 +00:00
|
|
|
mShiftLockEnabled = true;
|
|
|
|
}
|
|
|
|
|
2010-09-01 14:33:43 +00:00
|
|
|
// sticky is used for shift key. If a key is not sticky and is modifier,
|
|
|
|
// the key will be treated as functional.
|
|
|
|
private boolean isFunctionalKey() {
|
|
|
|
return !sticky && modifier;
|
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
@Override
|
|
|
|
public void onReleased(boolean inside) {
|
|
|
|
if (!mShiftLockEnabled) {
|
|
|
|
super.onReleased(inside);
|
|
|
|
} else {
|
|
|
|
pressed = !pressed;
|
|
|
|
}
|
|
|
|
}
|
2009-06-04 19:24:14 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
|
|
|
* Overriding this method so that we can reduce the target area for certain keys.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean isInside(int x, int y) {
|
2010-10-17 11:46:01 +00:00
|
|
|
boolean result = (keyboard instanceof LatinKeyboard)
|
|
|
|
&& ((LatinKeyboard)keyboard).isInside(this, x, y);
|
2010-02-05 22:07:04 +00:00
|
|
|
return result;
|
2010-01-30 04:09:49 +00:00
|
|
|
}
|
|
|
|
|
2010-10-17 11:46:01 +00:00
|
|
|
private boolean isInsideSuper(int x, int y) {
|
2009-03-13 22:11:42 +00:00
|
|
|
return super.isInside(x, y);
|
|
|
|
}
|
2010-09-01 14:33:43 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int[] getCurrentDrawableState() {
|
|
|
|
if (isFunctionalKey()) {
|
|
|
|
if (pressed) {
|
|
|
|
return KEY_STATE_FUNCTIONAL_PRESSED;
|
|
|
|
} else {
|
|
|
|
return KEY_STATE_FUNCTIONAL_NORMAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.getCurrentDrawableState();
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-01-30 04:09:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Animation to be displayed on the spacebar preview popup when switching
|
|
|
|
* languages by swiping the spacebar. It draws the current, previous and
|
|
|
|
* next languages and moves them by the delta of touch movement on the spacebar.
|
|
|
|
*/
|
2010-10-17 11:46:01 +00:00
|
|
|
private class SlidingLocaleDrawable extends Drawable {
|
2010-01-30 04:09:49 +00:00
|
|
|
|
2010-09-17 11:25:13 +00:00
|
|
|
private final int mWidth;
|
|
|
|
private final int mHeight;
|
|
|
|
private final Drawable mBackground;
|
|
|
|
private final TextPaint mTextPaint;
|
|
|
|
private final int mMiddleX;
|
|
|
|
private final Drawable mLeftDrawable;
|
|
|
|
private final Drawable mRightDrawable;
|
|
|
|
private final int mThreshold;
|
2010-01-30 04:09:49 +00:00
|
|
|
private int mDiff;
|
|
|
|
private boolean mHitThreshold;
|
|
|
|
private String mCurrentLanguage;
|
|
|
|
private String mNextLanguage;
|
|
|
|
private String mPrevLanguage;
|
|
|
|
|
|
|
|
public SlidingLocaleDrawable(Drawable background, int width, int height) {
|
|
|
|
mBackground = background;
|
2010-10-02 06:17:27 +00:00
|
|
|
setDefaultBounds(mBackground);
|
2010-01-30 04:09:49 +00:00
|
|
|
mWidth = width;
|
|
|
|
mHeight = height;
|
2010-10-22 13:18:40 +00:00
|
|
|
final TextPaint textPaint = new TextPaint();
|
|
|
|
textPaint.setTextSize(getTextSizeFromTheme(android.R.style.TextAppearance_Medium, 18));
|
|
|
|
textPaint.setColor(R.color.latinkeyboard_transparent);
|
|
|
|
textPaint.setTextAlign(Align.CENTER);
|
|
|
|
textPaint.setAlpha(OPACITY_FULLY_OPAQUE);
|
|
|
|
textPaint.setAntiAlias(true);
|
|
|
|
mTextPaint = textPaint;
|
2010-01-30 04:09:49 +00:00
|
|
|
mMiddleX = (mWidth - mBackground.getIntrinsicWidth()) / 2;
|
2010-10-22 13:18:40 +00:00
|
|
|
final Resources res = mRes;
|
|
|
|
mLeftDrawable = res.getDrawable(
|
|
|
|
R.drawable.sym_keyboard_feedback_language_arrows_left);
|
|
|
|
mRightDrawable = res.getDrawable(
|
|
|
|
R.drawable.sym_keyboard_feedback_language_arrows_right);
|
2010-01-30 04:09:49 +00:00
|
|
|
mThreshold = ViewConfiguration.get(mContext).getScaledTouchSlop();
|
|
|
|
}
|
|
|
|
|
2010-09-17 11:25:13 +00:00
|
|
|
private void setDiff(int diff) {
|
2010-01-30 04:09:49 +00:00
|
|
|
if (diff == Integer.MAX_VALUE) {
|
|
|
|
mHitThreshold = false;
|
|
|
|
mCurrentLanguage = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mDiff = diff;
|
|
|
|
if (mDiff > mWidth) mDiff = mWidth;
|
|
|
|
if (mDiff < -mWidth) mDiff = -mWidth;
|
|
|
|
if (Math.abs(mDiff) > mThreshold) mHitThreshold = true;
|
|
|
|
invalidateSelf();
|
|
|
|
}
|
|
|
|
|
2010-09-17 11:25:13 +00:00
|
|
|
|
2010-01-30 04:09:49 +00:00
|
|
|
@Override
|
|
|
|
public void draw(Canvas canvas) {
|
|
|
|
canvas.save();
|
|
|
|
if (mHitThreshold) {
|
2010-09-17 11:25:13 +00:00
|
|
|
Paint paint = mTextPaint;
|
|
|
|
final int width = mWidth;
|
|
|
|
final int height = mHeight;
|
|
|
|
final int diff = mDiff;
|
|
|
|
final Drawable lArrow = mLeftDrawable;
|
|
|
|
final Drawable rArrow = mRightDrawable;
|
|
|
|
canvas.clipRect(0, 0, width, height);
|
2010-01-30 04:09:49 +00:00
|
|
|
if (mCurrentLanguage == null) {
|
2010-11-17 22:58:14 +00:00
|
|
|
SubtypeSwitcher subtypeSwitcher = SubtypeSwitcher.getInstance();
|
|
|
|
mCurrentLanguage = subtypeSwitcher.getInputLanguageName();
|
|
|
|
mNextLanguage = subtypeSwitcher.getNextInputLanguageName();
|
|
|
|
mPrevLanguage = subtypeSwitcher.getPreviousInputLanguageName();
|
2010-01-30 04:09:49 +00:00
|
|
|
}
|
2010-09-17 11:25:13 +00:00
|
|
|
// Draw language text with shadow
|
|
|
|
final float baseline = mHeight * SPACEBAR_LANGUAGE_BASELINE - paint.descent();
|
|
|
|
paint.setColor(mRes.getColor(R.color.latinkeyboard_feedback_language_text));
|
|
|
|
canvas.drawText(mCurrentLanguage, width / 2 + diff, baseline, paint);
|
|
|
|
canvas.drawText(mNextLanguage, diff - width / 2, baseline, paint);
|
|
|
|
canvas.drawText(mPrevLanguage, diff + width + width / 2, baseline, paint);
|
|
|
|
|
2010-10-02 06:17:27 +00:00
|
|
|
setDefaultBounds(lArrow);
|
2010-09-17 11:25:13 +00:00
|
|
|
rArrow.setBounds(width - rArrow.getIntrinsicWidth(), 0, width,
|
|
|
|
rArrow.getIntrinsicHeight());
|
|
|
|
lArrow.draw(canvas);
|
|
|
|
rArrow.draw(canvas);
|
2010-01-30 04:09:49 +00:00
|
|
|
}
|
|
|
|
if (mBackground != null) {
|
|
|
|
canvas.translate(mMiddleX, 0);
|
|
|
|
mBackground.draw(canvas);
|
|
|
|
}
|
|
|
|
canvas.restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getOpacity() {
|
|
|
|
return PixelFormat.TRANSLUCENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setAlpha(int alpha) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setColorFilter(ColorFilter cf) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getIntrinsicWidth() {
|
|
|
|
return mWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getIntrinsicHeight() {
|
|
|
|
return mHeight;
|
|
|
|
}
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|