2009-03-13 22:11:42 +00:00
|
|
|
/*
|
2010-03-26 22:07:10 +00:00
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
2010-12-02 09:46:21 +00:00
|
|
|
*
|
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
|
2010-12-02 09:46:21 +00:00
|
|
|
*
|
2009-03-13 22:11:42 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2010-12-02 09:46:21 +00:00
|
|
|
*
|
2009-03-13 22:11:42 +00:00
|
|
|
* 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;
|
|
|
|
|
|
|
|
import com.android.inputmethod.latin.R;
|
|
|
|
import com.android.inputmethod.latin.SubtypeSwitcher;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Resources;
|
2011-02-10 11:53:58 +00:00
|
|
|
import android.content.res.Resources.Theme;
|
2010-01-30 04:09:49 +00:00
|
|
|
import android.content.res.TypedArray;
|
2009-10-12 20:48:35 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.Canvas;
|
2011-01-12 11:33:54 +00:00
|
|
|
import android.graphics.Color;
|
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;
|
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-09-06 05:50:50 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
2010-12-02 11:54:32 +00:00
|
|
|
// TODO: We should remove this class
|
2010-12-02 09:46:21 +00:00
|
|
|
public class LatinKeyboard extends Keyboard {
|
2010-12-02 11:54:32 +00:00
|
|
|
public 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
|
|
|
|
2011-01-25 03:13:35 +00:00
|
|
|
private final Context mContext;
|
|
|
|
|
|
|
|
/* Space key and its icons, drawables and colors. */
|
|
|
|
private final Key mSpaceKey;
|
|
|
|
private final Drawable mSpaceIcon;
|
|
|
|
private final Drawable mSpacePreviewIcon;
|
|
|
|
private final int[] mSpaceKeyIndexArray;
|
2010-12-14 06:31:47 +00:00
|
|
|
private final Drawable mSpaceAutoCorrectionIndicator;
|
2010-09-21 13:53:12 +00:00
|
|
|
private final Drawable mButtonArrowLeftIcon;
|
|
|
|
private final Drawable mButtonArrowRightIcon;
|
2011-01-12 11:33:54 +00:00
|
|
|
private final int mSpacebarTextColor;
|
|
|
|
private final int mSpacebarTextShadowColor;
|
2011-01-25 03:13:35 +00:00
|
|
|
private final int mSpacebarVerticalCorrection;
|
2011-01-12 11:33:54 +00:00
|
|
|
private float mSpacebarTextFadeFactor = 0.0f;
|
2010-01-30 04:09:49 +00:00
|
|
|
private int mSpaceDragStartX;
|
|
|
|
private int mSpaceDragLastDiff;
|
|
|
|
private boolean mCurrentlyInSpace;
|
|
|
|
private SlidingLocaleDrawable mSlidingLocaleIcon;
|
2011-01-25 03:13:35 +00:00
|
|
|
|
|
|
|
/* Shortcut key and its icons if available */
|
|
|
|
private final Key mShortcutKey;
|
|
|
|
private final Drawable mEnabledShortcutIcon;
|
|
|
|
private final Drawable mDisabledShortcutIcon;
|
|
|
|
|
2010-01-30 04:09:49 +00:00
|
|
|
private static final float SPACEBAR_DRAG_THRESHOLD = 0.8f;
|
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)
|
2010-12-02 09:46:21 +00:00
|
|
|
public static final float SPACEBAR_LANGUAGE_BASELINE = 0.6f;
|
2010-09-17 11:25:13 +00:00
|
|
|
// 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-12-16 07:52:00 +00:00
|
|
|
private static final String SMALL_TEXT_SIZE_OF_LANGUAGE_ON_SPACEBAR = "small";
|
|
|
|
private static final String MEDIUM_TEXT_SIZE_OF_LANGUAGE_ON_SPACEBAR = "medium";
|
|
|
|
|
2010-12-02 09:46:21 +00:00
|
|
|
public LatinKeyboard(Context context, KeyboardId id) {
|
2010-12-14 06:31:47 +00:00
|
|
|
super(context, id.getXmlId(), id);
|
2009-10-12 20:48:35 +00:00
|
|
|
final Resources res = context.getResources();
|
2010-01-30 04:09:49 +00:00
|
|
|
mContext = context;
|
2011-01-25 03:13:35 +00:00
|
|
|
|
|
|
|
final List<Key> keys = getKeys();
|
|
|
|
int spaceKeyIndex = -1;
|
|
|
|
int shortcutKeyIndex = -1;
|
|
|
|
final int keyCount = keys.size();
|
|
|
|
for (int index = 0; index < keyCount; index++) {
|
|
|
|
// For now, assuming there are up to one space key and one shortcut key respectively.
|
|
|
|
switch (keys.get(index).mCode) {
|
|
|
|
case CODE_SPACE:
|
|
|
|
spaceKeyIndex = index;
|
|
|
|
break;
|
|
|
|
case CODE_VOICE:
|
|
|
|
shortcutKeyIndex = index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The index of space key is available only after Keyboard constructor has finished.
|
|
|
|
mSpaceKey = (spaceKeyIndex >= 0) ? keys.get(spaceKeyIndex) : null;
|
|
|
|
mSpaceIcon = (mSpaceKey != null) ? mSpaceKey.getIcon() : null;
|
|
|
|
mSpacePreviewIcon = (mSpaceKey != null) ? mSpaceKey.getPreviewIcon() : null;
|
|
|
|
mSpaceKeyIndexArray = new int[] { spaceKeyIndex };
|
|
|
|
|
|
|
|
mShortcutKey = (shortcutKeyIndex >= 0) ? keys.get(shortcutKeyIndex) : null;
|
|
|
|
mEnabledShortcutIcon = (mShortcutKey != null) ? mShortcutKey.getIcon() : null;
|
|
|
|
|
2011-01-12 11:33:54 +00:00
|
|
|
mSpacebarTextColor = res.getColor(R.color.latinkeyboard_bar_language_text);
|
2010-12-02 09:46:21 +00:00
|
|
|
if (id.mColorScheme == KeyboardView.COLOR_SCHEME_BLACK) {
|
2011-01-12 11:33:54 +00:00
|
|
|
mSpacebarTextShadowColor = res.getColor(
|
2010-11-22 00:40:38 +00:00
|
|
|
R.color.latinkeyboard_bar_language_shadow_black);
|
2011-01-28 07:06:22 +00:00
|
|
|
mDisabledShortcutIcon = res.getDrawable(R.drawable.sym_bkeyboard_voice_off);
|
2010-12-02 09:46:21 +00:00
|
|
|
} else { // default color scheme is KeyboardView.COLOR_SCHEME_WHITE
|
2011-01-12 11:33:54 +00:00
|
|
|
mSpacebarTextShadowColor = res.getColor(
|
2010-11-22 00:40:38 +00:00
|
|
|
R.color.latinkeyboard_bar_language_shadow_white);
|
2011-01-28 07:06:22 +00:00
|
|
|
mDisabledShortcutIcon = res.getDrawable(R.drawable.sym_keyboard_voice_off_holo);
|
2010-11-22 00:40:38 +00:00
|
|
|
}
|
2010-12-11 08:06:24 +00:00
|
|
|
mSpaceAutoCorrectionIndicator = res.getDrawable(R.drawable.sym_keyboard_space_led);
|
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);
|
2011-01-25 03:13:35 +00:00
|
|
|
mSpacebarVerticalCorrection = res.getDimensionPixelOffset(
|
2009-08-21 01:35:30 +00:00
|
|
|
R.dimen.spacebar_vertical_correction);
|
2010-09-28 00:45:45 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 11:33:54 +00:00
|
|
|
public void setSpacebarTextFadeFactor(float fadeFactor, LatinKeyboardView view) {
|
|
|
|
mSpacebarTextFadeFactor = fadeFactor;
|
|
|
|
updateSpacebarForLocale(false);
|
|
|
|
if (view != null)
|
|
|
|
view.invalidateKey(mSpaceKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int getSpacebarTextColor(int color, float fadeFactor) {
|
|
|
|
final int newColor = Color.argb((int)(Color.alpha(color) * fadeFactor),
|
|
|
|
Color.red(color), Color.green(color), Color.blue(color));
|
|
|
|
return newColor;
|
|
|
|
}
|
|
|
|
|
2011-01-25 03:13:35 +00:00
|
|
|
public void updateShortcutKey(boolean available, LatinKeyboardView view) {
|
|
|
|
if (mShortcutKey == null)
|
|
|
|
return;
|
|
|
|
mShortcutKey.mEnabled = available;
|
|
|
|
mShortcutKey.setIcon(available ? mEnabledShortcutIcon : mDisabledShortcutIcon);
|
|
|
|
if (view != null)
|
|
|
|
view.invalidateKey(mShortcutKey);
|
|
|
|
}
|
|
|
|
|
2010-09-02 13:54:37 +00:00
|
|
|
/**
|
|
|
|
* @return a key which should be invalidated.
|
|
|
|
*/
|
2010-12-11 08:06:24 +00:00
|
|
|
public Key onAutoCorrectionStateChanged(boolean isAutoCorrection) {
|
2011-01-12 11:33:54 +00:00
|
|
|
updateSpacebarForLocale(isAutoCorrection);
|
2010-09-02 13:54:37 +00:00
|
|
|
return mSpaceKey;
|
|
|
|
}
|
|
|
|
|
2011-01-12 11:33:54 +00:00
|
|
|
private void updateSpacebarForLocale(boolean isAutoCorrection) {
|
2010-12-14 06:31:47 +00:00
|
|
|
final Resources res = mContext.getResources();
|
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-12-03 04:17:26 +00:00
|
|
|
mSpaceKey.setIcon(new BitmapDrawable(res,
|
2011-01-12 11:33:54 +00:00
|
|
|
drawSpacebar(OPACITY_FULLY_OPAQUE, isAutoCorrection)));
|
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-12-11 08:06:24 +00:00
|
|
|
if (isAutoCorrection) {
|
2010-12-03 04:17:26 +00:00
|
|
|
mSpaceKey.setIcon(new BitmapDrawable(res,
|
2011-01-12 11:33:54 +00:00
|
|
|
drawSpacebar(OPACITY_FULLY_OPAQUE, isAutoCorrection)));
|
2010-09-03 10:05:23 +00:00
|
|
|
} else {
|
2010-12-03 04:17:26 +00:00
|
|
|
mSpaceKey.setIcon(mSpaceIcon);
|
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();
|
|
|
|
}
|
|
|
|
|
2011-01-12 11:33:54 +00:00
|
|
|
// Layout local language name and left and right arrow on spacebar.
|
|
|
|
private static String layoutSpacebar(Paint paint, Locale locale, Drawable lArrow,
|
2010-09-17 11:25:13 +00:00
|
|
|
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.
|
2011-01-12 11:33:54 +00:00
|
|
|
String language = SubtypeSwitcher.getFullDisplayName(locale, true);
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-12 11:33:54 +00:00
|
|
|
private Bitmap drawSpacebar(int opacity, boolean isAutoCorrection) {
|
2010-12-02 11:54:32 +00:00
|
|
|
final int width = mSpaceKey.mWidth;
|
2011-01-07 12:08:34 +00:00
|
|
|
final int height = mSpaceIcon != null ? mSpaceIcon.getIntrinsicHeight() : mSpaceKey.mHeight;
|
2010-09-17 11:25:13 +00:00
|
|
|
final Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
|
|
|
final Canvas canvas = new Canvas(buffer);
|
2010-12-14 06:31:47 +00:00
|
|
|
final Resources res = mContext.getResources();
|
2010-10-22 13:18:40 +00:00
|
|
|
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-12-16 07:52:00 +00:00
|
|
|
final String textSizeOfLanguageOnSpacebar = res.getString(
|
|
|
|
R.string.config_text_size_of_language_on_spacebar,
|
|
|
|
SMALL_TEXT_SIZE_OF_LANGUAGE_ON_SPACEBAR);
|
|
|
|
final int textStyle;
|
|
|
|
final int defaultTextSize;
|
|
|
|
if (MEDIUM_TEXT_SIZE_OF_LANGUAGE_ON_SPACEBAR.equals(textSizeOfLanguageOnSpacebar)) {
|
|
|
|
textStyle = android.R.style.TextAppearance_Medium;
|
|
|
|
defaultTextSize = 18;
|
|
|
|
} else {
|
|
|
|
textStyle = android.R.style.TextAppearance_Small;
|
|
|
|
defaultTextSize = 14;
|
|
|
|
}
|
|
|
|
|
2010-09-17 11:25:13 +00:00
|
|
|
final boolean allowVariableTextSize = true;
|
2011-01-12 11:33:54 +00:00
|
|
|
final String language = layoutSpacebar(paint, subtypeSwitcher.getInputLocale(),
|
2010-09-17 11:25:13 +00:00
|
|
|
mButtonArrowLeftIcon, mButtonArrowRightIcon, width, height,
|
2011-02-10 11:53:58 +00:00
|
|
|
getTextSizeFromTheme(mContext.getTheme(), textStyle, defaultTextSize),
|
2010-09-17 11:25:13 +00:00
|
|
|
allowVariableTextSize);
|
|
|
|
|
|
|
|
// Draw language text with shadow
|
2011-01-17 04:43:51 +00:00
|
|
|
// In case there is no space icon, we will place the language text at the center of
|
|
|
|
// spacebar.
|
2010-09-17 11:25:13 +00:00
|
|
|
final float descent = paint.descent();
|
2011-01-17 04:43:51 +00:00
|
|
|
final float textHeight = -paint.ascent() + descent;
|
|
|
|
final float baseline = (mSpaceIcon != null) ? height * SPACEBAR_LANGUAGE_BASELINE
|
|
|
|
: height / 2 + textHeight / 2;
|
2011-01-12 11:33:54 +00:00
|
|
|
paint.setColor(getSpacebarTextColor(mSpacebarTextShadowColor, mSpacebarTextFadeFactor));
|
2010-09-17 11:25:13 +00:00
|
|
|
canvas.drawText(language, width / 2, baseline - descent - 1, paint);
|
2011-01-12 11:33:54 +00:00
|
|
|
paint.setColor(getSpacebarTextColor(mSpacebarTextColor, mSpacebarTextFadeFactor));
|
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
|
2011-01-12 11:33:54 +00:00
|
|
|
if (SubtypeSwitcher.getInstance().useSpacebarLanguageSwitcher()
|
2010-11-17 22:58:14 +00:00
|
|
|
&& 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-12-11 08:06:24 +00:00
|
|
|
if (isAutoCorrection) {
|
2010-09-03 10:05:23 +00:00
|
|
|
final int iconWidth = width * SPACE_LED_LENGTH_PERCENT / 100;
|
2010-12-11 08:06:24 +00:00
|
|
|
final int iconHeight = mSpaceAutoCorrectionIndicator.getIntrinsicHeight();
|
2010-09-02 13:54:37 +00:00
|
|
|
int x = (width - iconWidth) / 2;
|
|
|
|
int y = height - iconHeight;
|
2010-12-11 08:06:24 +00:00
|
|
|
mSpaceAutoCorrectionIndicator.setBounds(x, y, x + iconWidth, y + iconHeight);
|
|
|
|
mSpaceAutoCorrectionIndicator.draw(canvas);
|
2011-01-07 12:08:34 +00:00
|
|
|
} else if (mSpaceIcon != null) {
|
2010-09-02 13:54:37 +00:00
|
|
|
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-12-02 11:54:32 +00:00
|
|
|
final int width = Math.max(mSpaceKey.mWidth,
|
2010-09-17 11:25:13 +00:00
|
|
|
(int)(getMinWidth() * SPACEBAR_POPUP_MIN_RATIO));
|
|
|
|
final int height = mSpacePreviewIcon.getIntrinsicHeight();
|
2010-12-02 09:46:21 +00:00
|
|
|
mSlidingLocaleIcon =
|
|
|
|
new SlidingLocaleDrawable(mContext, mSpacePreviewIcon, width, height);
|
2010-09-17 11:25:13 +00:00
|
|
|
mSlidingLocaleIcon.setBounds(0, 0, width, height);
|
2010-12-03 04:17:26 +00:00
|
|
|
mSpaceKey.setPreviewIcon(mSlidingLocaleIcon);
|
2010-01-30 04:09:49 +00:00
|
|
|
}
|
|
|
|
mSlidingLocaleIcon.setDiff(diff);
|
|
|
|
if (Math.abs(diff) == Integer.MAX_VALUE) {
|
2010-12-03 04:17:26 +00:00
|
|
|
mSpaceKey.setPreviewIcon(mSpacePreviewIcon);
|
2010-01-30 04:09:49 +00:00
|
|
|
} else {
|
2010-12-03 04:17:26 +00:00
|
|
|
mSpaceKey.setPreviewIcon(mSlidingLocaleIcon);
|
2010-01-30 04:09:49 +00:00
|
|
|
}
|
2010-12-03 04:17:26 +00:00
|
|
|
mSpaceKey.getPreviewIcon().invalidateSelf();
|
2010-01-30 04:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getLanguageChangeDirection() {
|
2010-11-17 22:58:14 +00:00
|
|
|
if (mSpaceKey == null || SubtypeSwitcher.getInstance().getEnabledKeyboardLocaleCount() <= 1
|
2010-12-02 11:54:32 +00:00
|
|
|
|| Math.abs(mSpaceDragLastDiff) < mSpaceKey.mWidth * SPACEBAR_DRAG_THRESHOLD) {
|
2010-01-30 04:09:49 +00:00
|
|
|
return 0; // No change
|
|
|
|
}
|
|
|
|
return mSpaceDragLastDiff > 0 ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
2010-10-17 11:46:01 +00:00
|
|
|
public void keyReleased() {
|
2010-01-30 04:09:49 +00:00
|
|
|
mCurrentlyInSpace = false;
|
|
|
|
mSpaceDragLastDiff = 0;
|
|
|
|
if (mSpaceKey != null) {
|
|
|
|
updateLocaleDrag(Integer.MAX_VALUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the magic of locking the touch gesture into the spacebar when
|
|
|
|
* switching input languages.
|
|
|
|
*/
|
2010-12-03 00:39:42 +00:00
|
|
|
@Override
|
2010-12-10 06:24:28 +00:00
|
|
|
public boolean isInside(Key key, int pointX, int pointY) {
|
|
|
|
int x = pointX;
|
|
|
|
int y = pointY;
|
2010-12-20 11:30:26 +00:00
|
|
|
final int code = key.mCode;
|
2011-01-25 02:30:29 +00:00
|
|
|
if (code == CODE_SPACE) {
|
2011-01-25 03:13:35 +00:00
|
|
|
y += mSpacebarVerticalCorrection;
|
2011-01-12 11:33:54 +00:00
|
|
|
if (SubtypeSwitcher.getInstance().useSpacebarLanguageSwitcher()
|
2010-11-17 22:58:14 +00:00
|
|
|
&& 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 {
|
2010-12-03 00:39:42 +00:00
|
|
|
boolean isOnSpace = key.isOnKey(x, y);
|
|
|
|
if (isOnSpace) {
|
2010-01-30 04:09:49 +00:00
|
|
|
mCurrentlyInSpace = true;
|
|
|
|
mSpaceDragStartX = x;
|
|
|
|
updateLocaleDrag(0);
|
|
|
|
}
|
2010-12-03 00:39:42 +00:00
|
|
|
return isOnSpace;
|
2010-01-30 04:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock into the spacebar
|
|
|
|
if (mCurrentlyInSpace) return false;
|
|
|
|
|
2010-12-03 00:39:42 +00:00
|
|
|
return key.isOnKey(x, y);
|
2010-01-30 04:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int[] getNearestKeys(int x, int y) {
|
|
|
|
if (mCurrentlyInSpace) {
|
2011-01-11 10:02:21 +00:00
|
|
|
return mSpaceKeyIndexArray;
|
2010-01-30 04:09:49 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-10 11:53:58 +00:00
|
|
|
private static int getTextSizeFromTheme(Theme theme, int style, int defValue) {
|
|
|
|
TypedArray array = theme.obtainStyledAttributes(
|
2010-01-30 04:09:49 +00:00
|
|
|
style, new int[] { android.R.attr.textSize });
|
|
|
|
int textSize = array.getDimensionPixelSize(array.getResourceId(0, 0), defValue);
|
|
|
|
return textSize;
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|