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;
|
|
|
|
|
2010-01-30 04:09:49 +00:00
|
|
|
import java.util.List;
|
2009-10-12 20:48:35 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
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-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.Paint.Align;
|
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.inputmethodservice.Keyboard;
|
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;
|
|
|
|
|
|
|
|
public class LatinKeyboard extends Keyboard {
|
|
|
|
|
2010-02-05 22:07:04 +00:00
|
|
|
private static final boolean DEBUG_PREFERRED_LETTER = false;
|
|
|
|
private static final String TAG = "LatinKeyboard";
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
private Drawable mShiftLockIcon;
|
|
|
|
private Drawable mShiftLockPreviewIcon;
|
|
|
|
private Drawable mOldShiftIcon;
|
|
|
|
private Drawable mOldShiftPreviewIcon;
|
2009-10-12 20:48:35 +00:00
|
|
|
private Drawable mSpaceIcon;
|
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-01-30 04:09:49 +00:00
|
|
|
private Drawable mButtonArrowLeftIcon;
|
|
|
|
private Drawable mButtonArrowRightIcon;
|
2009-03-13 22:11:42 +00:00
|
|
|
private Key mShiftKey;
|
|
|
|
private Key mEnterKey;
|
2009-10-12 20:48:35 +00:00
|
|
|
private Key mF1Key;
|
|
|
|
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-01-22 21:57:20 +00:00
|
|
|
/* package */ Locale mLocale;
|
2010-01-30 04:09:49 +00:00
|
|
|
private LanguageSwitcher mLanguageSwitcher;
|
2009-10-12 20:48:35 +00:00
|
|
|
private Resources mRes;
|
2010-01-30 04:09:49 +00:00
|
|
|
private Context mContext;
|
2009-10-12 20:48:35 +00:00
|
|
|
private int mMode;
|
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;
|
|
|
|
private boolean mIsAlphaKeyboard;
|
|
|
|
private CharSequence m123Label;
|
2010-01-30 04:09:49 +00:00
|
|
|
private boolean mCurrentlyInSpace;
|
|
|
|
private SlidingLocaleDrawable mSlidingLocaleIcon;
|
|
|
|
private Rect mBounds = new Rect();
|
2010-02-05 22:07:04 +00:00
|
|
|
private int[] mPrefLetterFrequencies;
|
|
|
|
private boolean mPreemptiveCorrection;
|
|
|
|
private int mPrefLetter;
|
|
|
|
private int mPrefLetterX;
|
|
|
|
private int mPrefLetterY;
|
|
|
|
private int mPrefDistance;
|
2009-10-22 21:51:39 +00:00
|
|
|
|
|
|
|
private int mExtensionResId;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
|
|
|
private static final int SHIFT_OFF = 0;
|
|
|
|
private static final int SHIFT_ON = 1;
|
|
|
|
private static final int SHIFT_LOCKED = 2;
|
|
|
|
|
|
|
|
private int mShiftState = SHIFT_OFF;
|
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-01-30 04:09:49 +00:00
|
|
|
|
2009-08-21 01:35:30 +00:00
|
|
|
static int sSpacebarVerticalCorrection;
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
public LatinKeyboard(Context context, int xmlLayoutResId) {
|
2010-02-26 19:47:05 +00:00
|
|
|
this(context, xmlLayoutResId, 0);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2010-02-26 19:47:05 +00:00
|
|
|
public LatinKeyboard(Context context, int xmlLayoutResId, int mode) {
|
2009-03-13 22:11:42 +00:00
|
|
|
super(context, xmlLayoutResId, mode);
|
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
|
|
|
mMode = mode;
|
|
|
|
mRes = res;
|
2009-08-21 01:35:30 +00:00
|
|
|
mShiftLockIcon = res.getDrawable(R.drawable.sym_keyboard_shift_locked);
|
|
|
|
mShiftLockPreviewIcon = res.getDrawable(R.drawable.sym_keyboard_feedback_shift_locked);
|
2009-03-13 22:11:42 +00:00
|
|
|
mShiftLockPreviewIcon.setBounds(0, 0,
|
|
|
|
mShiftLockPreviewIcon.getIntrinsicWidth(),
|
|
|
|
mShiftLockPreviewIcon.getIntrinsicHeight());
|
2009-10-12 20:48:35 +00:00
|
|
|
mSpaceIcon = res.getDrawable(R.drawable.sym_keyboard_space);
|
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-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-02-26 19:47:05 +00:00
|
|
|
mIsAlphaKeyboard = xmlLayoutResId == R.xml.kbd_qwerty;
|
2010-01-30 04:09:49 +00:00
|
|
|
mSpaceKeyIndex = indexOf((int) ' ');
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public LatinKeyboard(Context context, int layoutTemplateResId,
|
|
|
|
CharSequence characters, int columns, int horizontalPadding) {
|
|
|
|
super(context, layoutTemplateResId, characters, columns, horizontalPadding);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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]) {
|
|
|
|
case 10:
|
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;
|
|
|
|
case 32:
|
|
|
|
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
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|
2009-10-12 20:48:35 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
void setImeOptions(Resources res, int mode, int options) {
|
|
|
|
if (mEnterKey != null) {
|
|
|
|
// Reset some of the rarely used attributes.
|
|
|
|
mEnterKey.popupCharacters = null;
|
|
|
|
mEnterKey.popupResId = 0;
|
|
|
|
mEnterKey.text = null;
|
|
|
|
switch (options&(EditorInfo.IME_MASK_ACTION|EditorInfo.IME_FLAG_NO_ENTER_ACTION)) {
|
|
|
|
case EditorInfo.IME_ACTION_GO:
|
|
|
|
mEnterKey.iconPreview = null;
|
|
|
|
mEnterKey.icon = null;
|
|
|
|
mEnterKey.label = res.getText(R.string.label_go_key);
|
|
|
|
break;
|
|
|
|
case EditorInfo.IME_ACTION_NEXT:
|
|
|
|
mEnterKey.iconPreview = null;
|
|
|
|
mEnterKey.icon = null;
|
|
|
|
mEnterKey.label = res.getText(R.string.label_next_key);
|
|
|
|
break;
|
|
|
|
case EditorInfo.IME_ACTION_DONE:
|
|
|
|
mEnterKey.iconPreview = null;
|
|
|
|
mEnterKey.icon = null;
|
|
|
|
mEnterKey.label = res.getText(R.string.label_done_key);
|
|
|
|
break;
|
|
|
|
case EditorInfo.IME_ACTION_SEARCH:
|
|
|
|
mEnterKey.iconPreview = res.getDrawable(
|
|
|
|
R.drawable.sym_keyboard_feedback_search);
|
|
|
|
mEnterKey.icon = res.getDrawable(
|
|
|
|
R.drawable.sym_keyboard_search);
|
|
|
|
mEnterKey.label = null;
|
|
|
|
break;
|
|
|
|
case EditorInfo.IME_ACTION_SEND:
|
|
|
|
mEnterKey.iconPreview = null;
|
|
|
|
mEnterKey.icon = null;
|
|
|
|
mEnterKey.label = res.getText(R.string.label_send_key);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (mode == KeyboardSwitcher.MODE_IM) {
|
|
|
|
mEnterKey.icon = null;
|
|
|
|
mEnterKey.iconPreview = null;
|
|
|
|
mEnterKey.label = ":-)";
|
|
|
|
mEnterKey.text = ":-) ";
|
|
|
|
mEnterKey.popupResId = R.xml.popup_smileys;
|
|
|
|
} else {
|
|
|
|
mEnterKey.iconPreview = res.getDrawable(
|
|
|
|
R.drawable.sym_keyboard_feedback_return);
|
|
|
|
mEnterKey.icon = res.getDrawable(
|
|
|
|
R.drawable.sym_keyboard_return);
|
|
|
|
mEnterKey.label = null;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Set the initial size of the preview icon
|
|
|
|
if (mEnterKey.iconPreview != null) {
|
|
|
|
mEnterKey.iconPreview.setBounds(0, 0,
|
|
|
|
mEnterKey.iconPreview.getIntrinsicWidth(),
|
|
|
|
mEnterKey.iconPreview.getIntrinsicHeight());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void enableShiftLock() {
|
|
|
|
int index = getShiftKeyIndex();
|
|
|
|
if (index >= 0) {
|
|
|
|
mShiftKey = getKeys().get(index);
|
|
|
|
if (mShiftKey instanceof LatinKey) {
|
|
|
|
((LatinKey)mShiftKey).enableShiftLock();
|
|
|
|
}
|
|
|
|
mOldShiftIcon = mShiftKey.icon;
|
|
|
|
mOldShiftPreviewIcon = mShiftKey.iconPreview;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setShiftLocked(boolean shiftLocked) {
|
|
|
|
if (mShiftKey != null) {
|
|
|
|
if (shiftLocked) {
|
|
|
|
mShiftKey.on = true;
|
|
|
|
mShiftKey.icon = mShiftLockIcon;
|
|
|
|
mShiftState = SHIFT_LOCKED;
|
|
|
|
} else {
|
|
|
|
mShiftKey.on = false;
|
|
|
|
mShiftKey.icon = mShiftLockIcon;
|
|
|
|
mShiftState = SHIFT_ON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean isShiftLocked() {
|
|
|
|
return mShiftState == SHIFT_LOCKED;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean setShifted(boolean shiftState) {
|
|
|
|
boolean shiftChanged = false;
|
|
|
|
if (mShiftKey != null) {
|
|
|
|
if (shiftState == false) {
|
|
|
|
shiftChanged = mShiftState != SHIFT_OFF;
|
|
|
|
mShiftState = SHIFT_OFF;
|
|
|
|
mShiftKey.on = false;
|
|
|
|
mShiftKey.icon = mOldShiftIcon;
|
|
|
|
} else {
|
|
|
|
if (mShiftState == SHIFT_OFF) {
|
|
|
|
shiftChanged = mShiftState == SHIFT_OFF;
|
|
|
|
mShiftState = SHIFT_ON;
|
|
|
|
mShiftKey.icon = mShiftLockIcon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return super.setShifted(shiftState);
|
|
|
|
}
|
|
|
|
return shiftChanged;
|
|
|
|
}
|
2009-10-12 20:48:35 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
@Override
|
|
|
|
public boolean isShifted() {
|
|
|
|
if (mShiftKey != null) {
|
|
|
|
return mShiftState != SHIFT_OFF;
|
|
|
|
} else {
|
|
|
|
return super.isShifted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-22 21:51:39 +00:00
|
|
|
public void setExtension(int resId) {
|
|
|
|
mExtensionResId = resId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getExtension() {
|
|
|
|
return mExtensionResId;
|
|
|
|
}
|
|
|
|
|
2010-02-02 21:04:06 +00:00
|
|
|
private void setDefaultBounds(Drawable drawable) {
|
|
|
|
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
|
|
|
|
}
|
|
|
|
|
2010-02-26 19:47:05 +00:00
|
|
|
public void setVoiceMode(boolean hasVoiceButton, boolean hasVoice) {
|
|
|
|
mHasVoiceButton = hasVoiceButton;
|
|
|
|
mVoiceEnabled = hasVoice;
|
|
|
|
updateF1Key();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateF1Key() {
|
2010-01-28 15:33:27 +00:00
|
|
|
if (mF1Key == null) return;
|
2010-02-26 19:47:05 +00:00
|
|
|
if (m123Key != null && mIsAlphaKeyboard) {
|
|
|
|
if (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
|
|
|
}
|
|
|
|
|
|
|
|
if (mHasVoiceButton && mVoiceEnabled) {
|
2010-01-28 15:33:27 +00:00
|
|
|
mF1Key.codes = new int[] { LatinKeyboardView.KEYCODE_VOICE };
|
|
|
|
mF1Key.label = null;
|
|
|
|
mF1Key.icon = mMicIcon;
|
|
|
|
mF1Key.iconPreview = mMicPreviewIcon;
|
2010-02-26 19:47:05 +00:00
|
|
|
} else {
|
|
|
|
mF1Key.label = ",";
|
|
|
|
mF1Key.codes = new int[] { ',' };
|
|
|
|
mF1Key.icon = null;
|
|
|
|
mF1Key.iconPreview = null;
|
2010-01-28 15:33:27 +00:00
|
|
|
}
|
2010-01-22 21:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void updateSpaceBarForLocale() {
|
2009-10-12 20:48:35 +00:00
|
|
|
if (mLocale != null) {
|
|
|
|
// Create the graphic for spacebar
|
|
|
|
Bitmap buffer = Bitmap.createBitmap(mSpaceKey.width, mSpaceIcon.getIntrinsicHeight(),
|
|
|
|
Bitmap.Config.ARGB_8888);
|
|
|
|
Canvas canvas = new Canvas(buffer);
|
2010-02-06 00:24:40 +00:00
|
|
|
drawSpaceBar(canvas, buffer.getWidth(), buffer.getHeight(), 255);
|
2010-01-22 21:57:20 +00:00
|
|
|
mSpaceKey.icon = new BitmapDrawable(mRes, buffer);
|
2010-01-30 04:09:49 +00:00
|
|
|
mSpaceKey.repeatable = mLanguageSwitcher.getLocaleCount() < 2;
|
2009-10-12 20:48:35 +00:00
|
|
|
} else {
|
|
|
|
mSpaceKey.icon = mRes.getDrawable(R.drawable.sym_keyboard_space);
|
2010-01-22 21:57:20 +00:00
|
|
|
mSpaceKey.repeatable = true;
|
2009-10-12 20:48:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-06 00:24:40 +00:00
|
|
|
private void drawSpaceBar(Canvas canvas, int width, int height, int opacity) {
|
|
|
|
canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
|
|
|
|
Paint paint = new Paint();
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setAlpha(opacity);
|
|
|
|
// Get the text size from the theme
|
|
|
|
paint.setTextSize(getTextSizeFromTheme(android.R.style.TextAppearance_Small, 14));
|
|
|
|
paint.setTextAlign(Align.CENTER);
|
|
|
|
//// Draw a drop shadow for the text
|
|
|
|
//paint.setShadowLayer(2f, 0, 0, 0xFF000000);
|
|
|
|
final String language = getInputLanguage(mSpaceKey.width, paint);
|
|
|
|
final int ascent = (int) -paint.ascent();
|
|
|
|
paint.setColor(0x80000000);
|
|
|
|
canvas.drawText(language,
|
|
|
|
width / 2, ascent - 1, paint);
|
|
|
|
paint.setColor(0xFF808080);
|
|
|
|
canvas.drawText(language,
|
|
|
|
width / 2, ascent, paint);
|
|
|
|
// Put arrows on either side of the text
|
|
|
|
if (mLanguageSwitcher.getLocaleCount() > 1) {
|
|
|
|
Rect bounds = new Rect();
|
|
|
|
paint.getTextBounds(language, 0, language.length(), bounds);
|
|
|
|
drawButtonArrow(mButtonArrowLeftIcon, canvas,
|
|
|
|
(mSpaceKey.width - bounds.right) / 2
|
|
|
|
- mButtonArrowLeftIcon.getIntrinsicWidth(),
|
|
|
|
(int) paint.getTextSize());
|
|
|
|
drawButtonArrow(mButtonArrowRightIcon, canvas,
|
|
|
|
(mSpaceKey.width + bounds.right) / 2, (int) paint.getTextSize());
|
|
|
|
}
|
|
|
|
// Draw the spacebar icon at the bottom
|
|
|
|
int x = (width - mSpaceIcon.getIntrinsicWidth()) / 2;
|
|
|
|
int y = height - mSpaceIcon.getIntrinsicHeight();
|
|
|
|
mSpaceIcon.setBounds(x, y,
|
|
|
|
x + mSpaceIcon.getIntrinsicWidth(), y + mSpaceIcon.getIntrinsicHeight());
|
|
|
|
mSpaceIcon.draw(canvas);
|
|
|
|
}
|
|
|
|
|
2010-01-30 04:09:49 +00:00
|
|
|
private void drawButtonArrow(Drawable arrow, Canvas canvas, int x, int bottomY) {
|
|
|
|
arrow.setBounds(x, bottomY - arrow.getIntrinsicHeight(), x + arrow.getIntrinsicWidth(),
|
|
|
|
bottomY);
|
|
|
|
arrow.draw(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getInputLanguage(int widthAvail, Paint paint) {
|
|
|
|
return chooseDisplayName(mLanguageSwitcher.getInputLocale(), widthAvail, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getNextInputLanguage(int widthAvail, Paint paint) {
|
|
|
|
return chooseDisplayName(mLanguageSwitcher.getNextInputLocale(), widthAvail, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getPrevInputLanguage(int widthAvail, Paint paint) {
|
|
|
|
return chooseDisplayName(mLanguageSwitcher.getPrevInputLocale(), widthAvail, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String chooseDisplayName(Locale locale, int widthAvail, Paint paint) {
|
|
|
|
if (widthAvail < (int) (.35 * getMinWidth())) {
|
2010-02-06 00:24:40 +00:00
|
|
|
return LanguageSwitcher.toTitleCase(locale.getLanguage().substring(0, 2));
|
2010-01-30 04:09:49 +00:00
|
|
|
} else {
|
2010-02-06 00:24:40 +00:00
|
|
|
return LanguageSwitcher.toTitleCase(locale.getDisplayLanguage(locale));
|
2010-01-30 04:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateLocaleDrag(int diff) {
|
|
|
|
if (mSlidingLocaleIcon == null) {
|
|
|
|
mSlidingLocaleIcon = new SlidingLocaleDrawable(mSpacePreviewIcon, mSpaceKey.width,
|
|
|
|
mSpacePreviewIcon.getIntrinsicHeight());
|
|
|
|
mSlidingLocaleIcon.setBounds(0, 0, mSpaceKey.width,
|
|
|
|
mSpacePreviewIcon.getIntrinsicHeight());
|
|
|
|
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() {
|
|
|
|
if (mSpaceKey == null || mLanguageSwitcher.getLocaleCount() < 2
|
|
|
|
|| Math.abs(mSpaceDragLastDiff) < mSpaceKey.width * SPACEBAR_DRAG_THRESHOLD ) {
|
|
|
|
return 0; // No change
|
|
|
|
}
|
|
|
|
return mSpaceDragLastDiff > 0 ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLanguageSwitcher(LanguageSwitcher switcher) {
|
|
|
|
mLanguageSwitcher = switcher;
|
|
|
|
Locale locale = mLanguageSwitcher.getLocaleCount() > 0
|
|
|
|
? mLanguageSwitcher.getInputLocale()
|
|
|
|
: null;
|
2010-05-03 18:14:31 +00:00
|
|
|
// If the language count is 1 and is the same as the system language, don't show it.
|
|
|
|
if (locale != null
|
|
|
|
&& mLanguageSwitcher.getLocaleCount() == 1
|
|
|
|
&& mLanguageSwitcher.getSystemLocale().getLanguage()
|
|
|
|
.equalsIgnoreCase(locale.getLanguage())) {
|
|
|
|
locale = null;
|
|
|
|
}
|
2009-10-12 20:48:35 +00:00
|
|
|
if (mLocale != null && mLocale.equals(locale)) return;
|
|
|
|
mLocale = locale;
|
2010-01-22 21:57:20 +00:00
|
|
|
updateSpaceBarForLocale();
|
2009-10-12 20:48:35 +00:00
|
|
|
}
|
|
|
|
|
2010-01-30 04:09:49 +00:00
|
|
|
boolean isCurrentlyInSpace() {
|
|
|
|
return mCurrentlyInSpace;
|
|
|
|
}
|
|
|
|
|
2010-02-05 22:07:04 +00:00
|
|
|
void setPreferredLetters(int[] frequencies) {
|
|
|
|
mPrefLetterFrequencies = frequencies;
|
|
|
|
mPrefLetter = 0;
|
|
|
|
}
|
|
|
|
|
2010-01-30 04:09:49 +00:00
|
|
|
void keyReleased() {
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
boolean isInside(LatinKey key, int x, int y) {
|
|
|
|
final int code = key.codes[0];
|
|
|
|
if (code == KEYCODE_SHIFT ||
|
|
|
|
code == KEYCODE_DELETE) {
|
|
|
|
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;
|
|
|
|
if (mLanguageSwitcher.getLocaleCount() > 1) {
|
|
|
|
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) {
|
|
|
|
if (DEBUG_PREFERRED_LETTER && mPrefLetter == code
|
|
|
|
&& !key.isInsideSuper(x, y)) {
|
|
|
|
Log.d(TAG, "CORRECTED !!!!!!");
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return super.getNearestKeys(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
class LatinKey extends Keyboard.Key {
|
2009-03-13 22:11:42 +00:00
|
|
|
|
|
|
|
private boolean mShiftLockEnabled;
|
|
|
|
|
|
|
|
public LatinKey(Resources res, Keyboard.Row parent, int x, int y,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void enableShiftLock() {
|
|
|
|
mShiftLockEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-02-05 22:07:04 +00:00
|
|
|
boolean result = LatinKeyboard.this.isInside(this, x, y);
|
|
|
|
return result;
|
2010-01-30 04:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean isInsideSuper(int x, int y) {
|
2009-03-13 22:11:42 +00:00
|
|
|
return super.isInside(x, y);
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
class SlidingLocaleDrawable extends Drawable {
|
|
|
|
|
|
|
|
private int mWidth;
|
|
|
|
private int mHeight;
|
|
|
|
private Drawable mBackground;
|
|
|
|
private int mDiff;
|
|
|
|
private TextPaint mTextPaint;
|
|
|
|
private int mMiddleX;
|
|
|
|
private int mAscent;
|
|
|
|
private Drawable mLeftDrawable;
|
|
|
|
private Drawable mRightDrawable;
|
|
|
|
private boolean mHitThreshold;
|
|
|
|
private int mThreshold;
|
|
|
|
private String mCurrentLanguage;
|
|
|
|
private String mNextLanguage;
|
|
|
|
private String mPrevLanguage;
|
|
|
|
|
|
|
|
public SlidingLocaleDrawable(Drawable background, int width, int height) {
|
|
|
|
mBackground = background;
|
|
|
|
mBackground.setBounds(0, 0,
|
|
|
|
mBackground.getIntrinsicWidth(), mBackground.getIntrinsicHeight());
|
|
|
|
mWidth = width;
|
|
|
|
mHeight = height;
|
|
|
|
mTextPaint = new TextPaint();
|
|
|
|
int textSize = getTextSizeFromTheme(android.R.style.TextAppearance_Medium, 18);
|
|
|
|
mTextPaint.setTextSize(textSize);
|
|
|
|
mTextPaint.setColor(0);
|
|
|
|
mTextPaint.setTextAlign(Align.CENTER);
|
|
|
|
mTextPaint.setAlpha(255);
|
|
|
|
mTextPaint.setAntiAlias(true);
|
|
|
|
mAscent = (int) mTextPaint.ascent();
|
|
|
|
mMiddleX = (mWidth - mBackground.getIntrinsicWidth()) / 2;
|
|
|
|
mLeftDrawable =
|
|
|
|
mRes.getDrawable(R.drawable.sym_keyboard_feedback_language_arrows_left);
|
|
|
|
mRightDrawable =
|
|
|
|
mRes.getDrawable(R.drawable.sym_keyboard_feedback_language_arrows_right);
|
|
|
|
mLeftDrawable.setBounds(0, 0,
|
|
|
|
mLeftDrawable.getIntrinsicWidth(), mLeftDrawable.getIntrinsicHeight());
|
|
|
|
mRightDrawable.setBounds(mWidth - mRightDrawable.getIntrinsicWidth(), 0,
|
|
|
|
mWidth, mRightDrawable.getIntrinsicHeight());
|
|
|
|
mThreshold = ViewConfiguration.get(mContext).getScaledTouchSlop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setDiff(int diff) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void draw(Canvas canvas) {
|
|
|
|
canvas.save();
|
|
|
|
if (mHitThreshold) {
|
2010-02-02 18:42:32 +00:00
|
|
|
mTextPaint.setColor(0xFF000000);
|
2010-01-30 04:09:49 +00:00
|
|
|
canvas.clipRect(0, 0, mWidth, mHeight);
|
|
|
|
if (mCurrentLanguage == null) {
|
|
|
|
mCurrentLanguage = getInputLanguage(mWidth, mTextPaint);
|
|
|
|
mNextLanguage = getNextInputLanguage(mWidth, mTextPaint);
|
|
|
|
mPrevLanguage = getPrevInputLanguage(mWidth, mTextPaint);
|
|
|
|
}
|
|
|
|
canvas.drawText(mCurrentLanguage,
|
|
|
|
mWidth / 2 + mDiff, -mAscent + 4, mTextPaint);
|
|
|
|
canvas.drawText(mNextLanguage,
|
|
|
|
mDiff - mWidth / 2, -mAscent + 4, mTextPaint);
|
|
|
|
canvas.drawText(mPrevLanguage,
|
|
|
|
mDiff + mWidth + mWidth / 2, -mAscent + 4, mTextPaint);
|
|
|
|
mLeftDrawable.draw(canvas);
|
|
|
|
mRightDrawable.draw(canvas);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|