diff options
Diffstat (limited to 'java/src')
16 files changed, 414 insertions, 209 deletions
diff --git a/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java b/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java index 2789bcb39..bcdcef7dc 100644 --- a/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/EditorInfoCompatUtils.java @@ -27,7 +27,7 @@ public class EditorInfoCompatUtils { private static final Field FIELD_IME_FLAG_NAVIGATE_PREVIOUS = CompatUtils.getField( EditorInfo.class, "IME_FLAG_NAVIGATE_PREVIOUS"); private static final Field FIELD_IME_ACTION_PREVIOUS = CompatUtils.getField( - EditorInfo.class, "IME_FLAG_ACTION_PREVIOUS"); + EditorInfo.class, "IME_ACTION_PREVIOUS"); private static final Integer OBJ_IME_FLAG_NAVIGATE_NEXT = (Integer) CompatUtils .getFieldValue(null, null, FIELD_IME_FLAG_NAVIGATE_NEXT); private static final Integer OBJ_IME_FLAG_NAVIGATE_PREVIOUS = (Integer) CompatUtils diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java index cb529461a..e5ee272a2 100644 --- a/java/src/com/android/inputmethod/keyboard/Key.java +++ b/java/src/com/android/inputmethod/keyboard/Key.java @@ -37,25 +37,24 @@ public class Key { * The key code (unicode or custom code) that this key generates. */ public final int mCode; - /** The unicode that this key generates in manual temporary upper case mode. */ - public final int mManualTemporaryUpperCaseCode; /** Label to display */ public final CharSequence mLabel; + /** Hint letter to display on the key in conjunction with the label */ + public final CharSequence mHintLetter; /** Option of the label */ public final int mLabelOption; + public static final int LABEL_OPTION_ALIGN_LEFT = 0x01; + public static final int LABEL_OPTION_ALIGN_RIGHT = 0x02; + public static final int LABEL_OPTION_ALIGN_BOTTOM = 0x08; + public static final int LABEL_OPTION_FONT_NORMAL = 0x10; + private static final int LABEL_OPTION_POPUP_HINT = 0x20; + private static final int LABEL_OPTION_HAS_UPPERCASE_LETTER = 0x40; /** Icon to display instead of a label. Icon takes precedence over a label */ private Drawable mIcon; /** Preview version of the icon, for the preview popup */ private Drawable mPreviewIcon; - /** Hint icon to display on the key in conjunction with the label */ - public final Drawable mHintIcon; - /** - * The hint icon to display on the key when keyboard is in manual temporary upper case - * mode. - */ - public final Drawable mManualTemporaryUpperCaseHintIcon; /** Width of the key, not including the gap */ public final int mWidth; @@ -154,9 +153,7 @@ public class Key { mVisualInsetsLeft = mVisualInsetsRight = 0; mWidth = width - mGap; mEdgeFlags = edgeFlags; - mHintIcon = null; - mManualTemporaryUpperCaseHintIcon = null; - mManualTemporaryUpperCaseCode = Keyboard.CODE_DUMMY; + mHintLetter = null; mLabelOption = 0; mFunctional = false; mSticky = false; @@ -273,16 +270,10 @@ public class Key { Keyboard.setDefaultBounds(mPreviewIcon); mIcon = style.getDrawable(keyAttr, R.styleable.Keyboard_Key_keyIcon); Keyboard.setDefaultBounds(mIcon); - mHintIcon = style.getDrawable(keyAttr, R.styleable.Keyboard_Key_keyHintIcon); - Keyboard.setDefaultBounds(mHintIcon); - mManualTemporaryUpperCaseHintIcon = style.getDrawable(keyAttr, - R.styleable.Keyboard_Key_manualTemporaryUpperCaseHintIcon); - Keyboard.setDefaultBounds(mManualTemporaryUpperCaseHintIcon); + mHintLetter = style.getText(keyAttr, R.styleable.Keyboard_Key_keyHintLetter); mLabel = style.getText(keyAttr, R.styleable.Keyboard_Key_keyLabel); mLabelOption = style.getFlag(keyAttr, R.styleable.Keyboard_Key_keyLabelOption, 0); - mManualTemporaryUpperCaseCode = style.getInt(keyAttr, - R.styleable.Keyboard_Key_manualTemporaryUpperCaseCode, Keyboard.CODE_DUMMY); mOutputText = style.getText(keyAttr, R.styleable.Keyboard_Key_keyOutputText); // Choose the first letter of the label as primary code if not // specified. @@ -305,6 +296,14 @@ public class Key { } } + public boolean hasPopupHint() { + return (mLabelOption & LABEL_OPTION_POPUP_HINT) != 0; + } + + public boolean hasUppercaseLetter() { + return (mLabelOption & LABEL_OPTION_HAS_UPPERCASE_LETTER) != 0; + } + private static boolean isDigitPopupCharacter(CharSequence label) { return label != null && label.length() == 1 && Character.isDigit(label.charAt(0)); } diff --git a/java/src/com/android/inputmethod/keyboard/KeyStyles.java b/java/src/com/android/inputmethod/keyboard/KeyStyles.java index d53df788f..ce5ee5495 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyStyles.java +++ b/java/src/com/android/inputmethod/keyboard/KeyStyles.java @@ -183,7 +183,7 @@ public class KeyStyles { readText(keyAttr, R.styleable.Keyboard_Key_keyOutputText); readDrawable(keyAttr, R.styleable.Keyboard_Key_keyIcon); readDrawable(keyAttr, R.styleable.Keyboard_Key_iconPreview); - readDrawable(keyAttr, R.styleable.Keyboard_Key_keyHintIcon); + readText(keyAttr, R.styleable.Keyboard_Key_keyHintLetter); readDrawable(keyAttr, R.styleable.Keyboard_Key_shiftedIcon); readBoolean(keyAttr, R.styleable.Keyboard_Key_isFunctional); readBoolean(keyAttr, R.styleable.Keyboard_Key_isSticky); diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java index 267abccb3..58629ba51 100644 --- a/java/src/com/android/inputmethod/keyboard/Keyboard.java +++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java @@ -16,15 +16,15 @@ package com.android.inputmethod.keyboard; -import com.android.inputmethod.latin.R; - -import org.xmlpull.v1.XmlPullParserException; - import android.content.Context; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.util.Log; +import com.android.inputmethod.latin.R; + +import org.xmlpull.v1.XmlPullParserException; + import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -171,8 +171,8 @@ public class Keyboard { mDefaultVerticalGap = 0; mDefaultHeight = mDefaultWidth; mId = id; - loadKeyboard(context, xmlLayoutResId); mProximityInfo = new ProximityInfo(GRID_WIDTH, GRID_HEIGHT); + loadKeyboard(context, xmlLayoutResId); } public int getProximityInfo() { @@ -434,7 +434,7 @@ public class Keyboard { private void loadKeyboard(Context context, int xmlLayoutResId) { try { - KeyboardParser parser = new KeyboardParser(this, context.getResources()); + KeyboardParser parser = new KeyboardParser(this, context); parser.parseKeyboard(xmlLayoutResId); // mMinWidth is the width of this keyboard which is maximum width of row. mMinWidth = parser.getMaxRowWidth(); diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardId.java b/java/src/com/android/inputmethod/keyboard/KeyboardId.java index 7c03ec71e..cdaed95a5 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardId.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardId.java @@ -43,7 +43,7 @@ public class KeyboardId { public final int mMode; public final int mXmlId; public final int mColorScheme; - public final boolean mWebInput; + public final boolean mNavigateAction; public final boolean mPasswordInput; public final boolean mHasSettingsKey; public final boolean mVoiceKeyEnabled; @@ -67,7 +67,9 @@ public class KeyboardId { this.mMode = mode; this.mXmlId = xmlId; this.mColorScheme = colorScheme; - this.mWebInput = InputTypeCompatUtils.isWebInputType(inputType); + this.mNavigateAction = InputTypeCompatUtils.isWebInputType(inputType) + || EditorInfoCompatUtils.hasFlagNavigateNext(imeOptions) + || EditorInfoCompatUtils.hasFlagNavigatePrevious(imeOptions); this.mPasswordInput = InputTypeCompatUtils.isPasswordInputType(inputType) || InputTypeCompatUtils.isVisiblePasswordInputType(inputType); this.mHasSettingsKey = hasSettingsKey; @@ -89,7 +91,7 @@ public class KeyboardId { mode, xmlId, colorScheme, - mWebInput, + mNavigateAction, mPasswordInput, hasSettingsKey, voiceKeyEnabled, @@ -143,7 +145,7 @@ public class KeyboardId { && other.mMode == this.mMode && other.mXmlId == this.mXmlId && other.mColorScheme == this.mColorScheme - && other.mWebInput == this.mWebInput + && other.mNavigateAction == this.mNavigateAction && other.mPasswordInput == this.mPasswordInput && other.mHasSettingsKey == this.mHasSettingsKey && other.mVoiceKeyEnabled == this.mVoiceKeyEnabled @@ -166,7 +168,7 @@ public class KeyboardId { modeName(mMode), EditorInfoCompatUtils.imeOptionsName(mImeAction), colorSchemeName(mColorScheme), - (mWebInput ? " webInput" : ""), + (mNavigateAction ? " navigateAction" : ""), (mPasswordInput ? " passwordInput" : ""), (mHasSettingsKey ? " hasSettingsKey" : ""), (mVoiceKeyEnabled ? " voiceKeyEnabled" : ""), diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardParser.java b/java/src/com/android/inputmethod/keyboard/KeyboardParser.java index c2db62a1c..20af12bc5 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardParser.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardParser.java @@ -16,12 +16,6 @@ package com.android.inputmethod.keyboard; -import com.android.inputmethod.compat.EditorInfoCompatUtils; -import com.android.inputmethod.latin.R; - -import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserException; - import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; @@ -31,6 +25,12 @@ import android.util.TypedValue; import android.util.Xml; import android.view.InflateException; +import com.android.inputmethod.compat.EditorInfoCompatUtils; +import com.android.inputmethod.latin.R; + +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -121,6 +121,7 @@ public class KeyboardParser { public static final String TAG_KEY_STYLE = "key-style"; private final Keyboard mKeyboard; + private final Context mContext; private final Resources mResources; private int mHorizontalEdgesPadding; @@ -131,8 +132,10 @@ public class KeyboardParser { private Row mCurrentRow = null; private final KeyStyles mKeyStyles = new KeyStyles(); - public KeyboardParser(Keyboard keyboard, Resources res) { + public KeyboardParser(Keyboard keyboard, Context context) { mKeyboard = keyboard; + mContext = context; + final Resources res = context.getResources(); mResources = res; mHorizontalEdgesPadding = (int)res.getDimension(R.dimen.keyboard_horizontal_edges_padding); } @@ -187,8 +190,9 @@ public class KeyboardParser { private void parseKeyboardAttributes(XmlResourceParser parser) { final Keyboard keyboard = mKeyboard; - final TypedArray keyboardAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser), - R.styleable.Keyboard); + final TypedArray keyboardAttr = mContext.obtainStyledAttributes( + Xml.asAttributeSet(parser), R.styleable.Keyboard, R.attr.keyboardStyle, + R.style.Keyboard); final TypedArray keyAttr = mResources.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.Keyboard_Key); try { @@ -469,8 +473,8 @@ public class KeyboardParser { try { final boolean modeMatched = matchTypedValue(a, R.styleable.Keyboard_Case_mode, id.mMode, KeyboardId.modeName(id.mMode)); - final boolean webInputMatched = matchBoolean(a, - R.styleable.Keyboard_Case_webInput, id.mWebInput); + final boolean navigateActionMatched = matchBoolean(a, + R.styleable.Keyboard_Case_navigateAction, id.mNavigateAction); final boolean passwordInputMatched = matchBoolean(a, R.styleable.Keyboard_Case_passwordInput, id.mPasswordInput); final boolean settingsKeyMatched = matchBoolean(a, @@ -493,7 +497,7 @@ public class KeyboardParser { R.styleable.Keyboard_Case_languageCode, id.mLocale.getLanguage()); final boolean countryCodeMatched = matchString(a, R.styleable.Keyboard_Case_countryCode, id.mLocale.getCountry()); - final boolean selected = modeMatched && webInputMatched && passwordInputMatched + final boolean selected = modeMatched && navigateActionMatched && passwordInputMatched && settingsKeyMatched && voiceEnabledMatched && voiceKeyMatched && colorSchemeMatched && imeActionMatched && localeCodeMatched && languageCodeMatched && countryCodeMatched; @@ -503,7 +507,7 @@ public class KeyboardParser { textAttr(KeyboardId.colorSchemeName( viewAttr.getInt( R.styleable.KeyboardView_colorScheme, -1)), "colorScheme"), - booleanAttr(a, R.styleable.Keyboard_Case_webInput, "webInput"), + booleanAttr(a, R.styleable.Keyboard_Case_navigateAction, "navigateAction"), booleanAttr(a, R.styleable.Keyboard_Case_passwordInput, "passwordInput"), booleanAttr(a, R.styleable.Keyboard_Case_hasSettingsKey, "hasSettingsKey"), booleanAttr(a, R.styleable.Keyboard_Case_voiceKeyEnabled, "voiceKeyEnabled"), diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java index 7c68c93f2..50c61ffae 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java @@ -57,6 +57,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha private SubtypeSwitcher mSubtypeSwitcher; private SharedPreferences mPrefs; + private View mCurrentInputView; private LatinKeyboardView mKeyboardView; private LatinIME mInputMethodService; @@ -201,7 +202,9 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha final Locale savedLocale = Utils.setSystemLocale(res, mSubtypeSwitcher.getInputLocale()); - keyboard = new LatinKeyboard(mInputMethodService, id, id.mWidth); + final Context themeContext = new ContextThemeWrapper(mInputMethodService, + KEYBOARD_THEMES[mThemeIndex]); + keyboard = new LatinKeyboard(themeContext, id, id.mWidth); if (id.mEnableShiftLock) { keyboard.enableShiftLock(); @@ -294,7 +297,7 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha } public boolean isInputViewShown() { - return mKeyboardView != null && mKeyboardView.isShown(); + return mCurrentInputView != null && mCurrentInputView.isShown(); } public boolean isKeyboardAvailable() { @@ -714,9 +717,6 @@ public class KeyboardSwitcher implements SharedPreferences.OnSharedPreferenceCha return createInputView(mThemeIndex, true); } - // Instance variable only for {@link #createInputView(int, boolean)}. - private View mCurrentInputView; - private View createInputView(final int newThemeIndex, final boolean forceRecreate) { if (mCurrentInputView != null && mThemeIndex == newThemeIndex && !forceRecreate) return mCurrentInputView; diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java index f8bce40b1..44f2ff3ff 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java @@ -16,9 +16,6 @@ package com.android.inputmethod.keyboard; -import com.android.inputmethod.latin.LatinImeLogger; -import com.android.inputmethod.latin.R; - import android.content.Context; import android.content.pm.PackageManager; import android.content.res.Resources; @@ -50,6 +47,9 @@ import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; +import com.android.inputmethod.latin.LatinImeLogger; +import com.android.inputmethod.latin.R; + import java.util.ArrayList; import java.util.HashMap; import java.util.WeakHashMap; @@ -63,13 +63,18 @@ import java.util.WeakHashMap; * @attr ref R.styleable#KeyboardView_keyBackground * @attr ref R.styleable#KeyboardView_keyHysteresisDistance * @attr ref R.styleable#KeyboardView_keyLetterRatio - * @attr ref R.styleable#KeyboardView_keyLetterStyle + * @attr ref R.styleable#KeyboardView_keyLabelRatio + * @attr ref R.styleable#KeyboardView_keyHintLetterRatio + * @attr ref R.styleable#KeyboardView_keyUppercaseLetterRatio + * @attr ref R.styleable#KeyboardView_keyTextStyle * @attr ref R.styleable#KeyboardView_keyPreviewLayout * @attr ref R.styleable#KeyboardView_keyPreviewOffset * @attr ref R.styleable#KeyboardView_keyPreviewHeight * @attr ref R.styleable#KeyboardView_keyTextColor * @attr ref R.styleable#KeyboardView_keyTextColorDisabled - * @attr ref R.styleable#KeyboardView_labelTextRatio + * @attr ref R.styleable#KeyboardView_keyHintLetterColor + * @attr ref R.styleable#KeyboardView_keyUppercaseLetterInactivatedColor + * @attr ref R.styleable#KeyboardView_keyUppercaseLetterActivatedColor * @attr ref R.styleable#KeyboardView_verticalCorrection * @attr ref R.styleable#KeyboardView_popupLayout * @attr ref R.styleable#KeyboardView_shadowColor @@ -96,9 +101,11 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { // XML attribute private final float mKeyLetterRatio; private final int mKeyTextColor; - private final int mKeyTextColorDisabled; - private final Typeface mKeyLetterStyle; - private final float mLabelTextRatio; + private final int mKeyTextInactivatedColor; + private final Typeface mKeyTextStyle; + private final float mKeyLabelRatio; + private final float mKeyHintLetterRatio; + private final float mKeyUppercaseLetterRatio; private final int mColorScheme; private final int mShadowColor; private final float mShadowRadius; @@ -109,11 +116,17 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { private final int mPreviewOffset; private final int mPreviewHeight; private final int mPopupLayout; + private final Drawable mKeyPopupHintIcon; + private final int mKeyHintLetterColor; + private final int mKeyUppercaseLetterInactivatedColor; + private final int mKeyUppercaseLetterActivatedColor; // Main keyboard private Keyboard mKeyboard; private int mKeyLetterSize; - private int mLabelTextSize; + private int mKeyLabelSize; + private int mKeyHintLetterSize; + private int mKeyUppercaseLetterSize; // Key preview private boolean mInForeground; @@ -168,18 +181,17 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { private Bitmap mBuffer; /** The canvas for the above mutable keyboard bitmap */ private Canvas mCanvas; - private final Paint mPaint; - private final Rect mPadding; + private final Paint mPaint = new Paint(); + private final Rect mPadding = new Rect(); + private final Rect mTextBounds = new Rect(); // This map caches key label text height in pixel as value and key label text size as map key. private final HashMap<Integer, Integer> mTextHeightCache = new HashMap<Integer, Integer>(); + // This map caches key label text width in pixel as value and key label text size as map key. + private final HashMap<Integer, Integer> mTextWidthCache = new HashMap<Integer, Integer>(); // Distance from horizontal center of the key, proportional to key label text height and width. - private final float KEY_LABEL_VERTICAL_ADJUSTMENT_FACTOR_CENTER = 0.45f; - private final float KEY_LABEL_VERTICAL_PADDING_FACTOR = 1.60f; - private final String KEY_LABEL_REFERENCE_CHAR = "H"; - private final int KEY_LABEL_OPTION_ALIGN_LEFT = 1; - private final int KEY_LABEL_OPTION_ALIGN_RIGHT = 2; - private final int KEY_LABEL_OPTION_ALIGN_BOTTOM = 8; - private final int KEY_LABEL_OPTION_FONT_NORMAL = 16; + private static final float KEY_LABEL_VERTICAL_ADJUSTMENT_FACTOR_CENTER = 0.45f; + private static final float KEY_LABEL_VERTICAL_PADDING_FACTOR = 1.60f; + private static final String KEY_LABEL_REFERENCE_CHAR = "M"; private final int mKeyLabelHorizontalPadding; private final UIHandler mHandler = new UIHandler(); @@ -321,18 +333,28 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { mPreviewOffset = a.getDimensionPixelOffset(R.styleable.KeyboardView_keyPreviewOffset, 0); mPreviewHeight = a.getDimensionPixelSize(R.styleable.KeyboardView_keyPreviewHeight, 80); mKeyLetterRatio = getRatio(a, R.styleable.KeyboardView_keyLetterRatio); + mKeyLabelRatio = getRatio(a, R.styleable.KeyboardView_keyLabelRatio); + mKeyHintLetterRatio = getRatio(a, R.styleable.KeyboardView_keyHintLetterRatio); + mKeyUppercaseLetterRatio = getRatio(a, + R.styleable.KeyboardView_keyUppercaseLetterRatio); mKeyTextColor = a.getColor(R.styleable.KeyboardView_keyTextColor, 0xFF000000); - mKeyTextColorDisabled = a.getColor( - R.styleable.KeyboardView_keyTextColorDisabled, 0xFF000000); - mLabelTextRatio = getRatio(a, R.styleable.KeyboardView_labelTextRatio); + mKeyTextInactivatedColor = a.getColor( + R.styleable.KeyboardView_keyTextInactivatedColor, 0xFF000000); + mKeyPopupHintIcon = a.getDrawable(R.styleable.KeyboardView_keyPopupHintIcon); + mKeyHintLetterColor = a.getColor(R.styleable.KeyboardView_keyHintLetterColor, 0); + mKeyUppercaseLetterInactivatedColor = a.getColor( + R.styleable.KeyboardView_keyUppercaseLetterInactivatedColor, 0); + mKeyUppercaseLetterActivatedColor = a.getColor( + R.styleable.KeyboardView_keyUppercaseLetterActivatedColor, 0); + mKeyTextStyle = Typeface.defaultFromStyle( + a.getInt(R.styleable.KeyboardView_keyTextStyle, Typeface.NORMAL)); mPopupLayout = a.getResourceId(R.styleable.KeyboardView_popupLayout, 0); mShadowColor = a.getColor(R.styleable.KeyboardView_shadowColor, 0); mShadowRadius = a.getFloat(R.styleable.KeyboardView_shadowRadius, 0f); // TODO: Use Theme (android.R.styleable.Theme_backgroundDimAmount) mBackgroundDimAmount = a.getFloat(R.styleable.KeyboardView_backgroundDimAmount, 0.5f); - mKeyLetterStyle = Typeface.defaultFromStyle( - a.getInt(R.styleable.KeyboardView_keyLetterStyle, Typeface.NORMAL)); mColorScheme = a.getInt(R.styleable.KeyboardView_colorScheme, COLOR_SCHEME_WHITE); + a.recycle(); final Resources res = getResources(); @@ -347,12 +369,10 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { mKeyLabelHorizontalPadding = (int)res.getDimension( R.dimen.key_label_horizontal_alignment_padding); - mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setTextAlign(Align.CENTER); mPaint.setAlpha(255); - mPadding = new Rect(0, 0, 0, 0); mKeyBackground.getPadding(mPadding); mSwipeThreshold = (int) (500 * res.getDisplayMetrics().density); @@ -496,7 +516,10 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { mPopupPanelCache.clear(); final int keyHeight = keyboard.getRowHeight() - keyboard.getVerticalGap(); mKeyLetterSize = (int)(keyHeight * mKeyLetterRatio); - mLabelTextSize = (int)(keyHeight * mLabelTextRatio); + mKeyLabelSize = (int)(keyHeight * mKeyLabelRatio); + mKeyHintLetterSize = (int)(keyHeight * mKeyHintLetterRatio); + mKeyUppercaseLetterSize = (int)( + keyHeight * mKeyUppercaseLetterRatio); mPreviewTextSize = (int)(keyHeight * mPreviewTextRatio); } @@ -683,7 +706,7 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { // Vertical label text alignment. final float baseline; - if ((key.mLabelOption & KEY_LABEL_OPTION_ALIGN_BOTTOM) != 0) { + if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_BOTTOM) != 0) { baseline = key.mHeight - labelCharHeight * KEY_LABEL_VERTICAL_PADDING_FACTOR; if (DEBUG_SHOW_ALIGN) drawHorizontalLine(canvas, (int)baseline, keyDrawWidth, 0xc0008000, @@ -697,12 +720,12 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { } // Horizontal label text alignment final int positionX; - if ((key.mLabelOption & KEY_LABEL_OPTION_ALIGN_LEFT) != 0) { + if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_LEFT) != 0) { positionX = mKeyLabelHorizontalPadding + padding.left; paint.setTextAlign(Align.LEFT); if (DEBUG_SHOW_ALIGN) drawVerticalLine(canvas, positionX, rowHeight, 0xc0800080, new Paint()); - } else if ((key.mLabelOption & KEY_LABEL_OPTION_ALIGN_RIGHT) != 0) { + } else if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_RIGHT) != 0) { positionX = keyDrawWidth - mKeyLabelHorizontalPadding - padding.right; paint.setTextAlign(Align.RIGHT); if (DEBUG_SHOW_ALIGN) @@ -715,8 +738,8 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { drawVerticalLine(canvas, positionX, rowHeight, 0xc0008080, new Paint()); } } - if (key.mManualTemporaryUpperCaseHintIcon != null && isManualTemporaryUpperCase) { - paint.setColor(mKeyTextColorDisabled); + if (key.hasUppercaseLetter() && isManualTemporaryUpperCase) { + paint.setColor(mKeyTextInactivatedColor); } else { paint.setColor(mKeyTextColor); } @@ -732,6 +755,27 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { paint.setShadowLayer(0, 0, 0, 0); } + // Draw hint letter. + if (key.mHintLetter != null) { + final String label = key.mHintLetter.toString(); + final int textColor; + final int textSize; + if (key.hasUppercaseLetter()) { + textColor = isManualTemporaryUpperCase ? mKeyUppercaseLetterActivatedColor + : mKeyUppercaseLetterInactivatedColor; + textSize = mKeyUppercaseLetterSize; + } else { + textColor = mKeyHintLetterColor; + textSize = mKeyHintLetterSize; + } + paint.setColor(textColor); + paint.setTextSize(textSize); + // Note: padding.right for drawX? + final float drawX = keyDrawWidth - getLabelCharWidth(textSize, paint); + final float drawY = -paint.ascent() + padding.top; + canvas.drawText(label, drawX, drawY, paint); + } + // Draw key icon. final Drawable icon = key.getIcon(); if (key.mLabel == null && icon != null) { @@ -739,11 +783,11 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { final int drawableHeight = icon.getIntrinsicHeight(); final int drawableX; final int drawableY = (key.mHeight + padding.top - padding.bottom - drawableHeight) / 2; - if ((key.mLabelOption & KEY_LABEL_OPTION_ALIGN_LEFT) != 0) { + if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_LEFT) != 0) { drawableX = padding.left + mKeyLabelHorizontalPadding; if (DEBUG_SHOW_ALIGN) drawVerticalLine(canvas, drawableX, rowHeight, 0xc0800080, new Paint()); - } else if ((key.mLabelOption & KEY_LABEL_OPTION_ALIGN_RIGHT) != 0) { + } else if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_RIGHT) != 0) { drawableX = keyDrawWidth - padding.right - mKeyLabelHorizontalPadding - drawableWidth; if (DEBUG_SHOW_ALIGN) @@ -761,15 +805,14 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { 0x80c00000, new Paint()); } - // Draw hint icon. - if (key.mHintIcon != null) { + // Draw popup hint icon "...". + // TODO: Draw "..." by text. + if (key.hasPopupHint()) { final int drawableWidth = keyDrawWidth; final int drawableHeight = key.mHeight; final int drawableX = 0; final int drawableY = HINT_ICON_VERTICAL_ADJUSTMENT_PIXEL; - Drawable hintIcon = (isManualTemporaryUpperCase - && key.mManualTemporaryUpperCaseHintIcon != null) - ? key.mManualTemporaryUpperCaseHintIcon : key.mHintIcon; + final Drawable hintIcon = mKeyPopupHintIcon; drawIcon(canvas, hintIcon, drawableX, drawableY, drawableWidth, drawableHeight); if (DEBUG_SHOW_ALIGN) drawRectangle(canvas, drawableX, drawableY, drawableWidth, drawableHeight, @@ -784,15 +827,15 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { final int labelSize; final Typeface labelStyle; if (label.length() > 1) { - labelSize = mLabelTextSize; - if ((keyLabelOption & KEY_LABEL_OPTION_FONT_NORMAL) != 0) { + labelSize = mKeyLabelSize; + if ((keyLabelOption & Key.LABEL_OPTION_FONT_NORMAL) != 0) { labelStyle = Typeface.DEFAULT; } else { labelStyle = Typeface.DEFAULT_BOLD; } } else { labelSize = mKeyLetterSize; - labelStyle = mKeyLetterStyle; + labelStyle = mKeyTextStyle; } paint.setTextSize(labelSize); paint.setTypeface(labelStyle); @@ -805,14 +848,26 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { if (labelHeightValue != null) { labelCharHeight = labelHeightValue; } else { - Rect textBounds = new Rect(); - paint.getTextBounds(KEY_LABEL_REFERENCE_CHAR, 0, 1, textBounds); - labelCharHeight = textBounds.height(); + paint.getTextBounds(KEY_LABEL_REFERENCE_CHAR, 0, 1, mTextBounds); + labelCharHeight = mTextBounds.height(); mTextHeightCache.put(labelSize, labelCharHeight); } return labelCharHeight; } + private int getLabelCharWidth(int labelSize, Paint paint) { + Integer labelWidthValue = mTextWidthCache.get(labelSize); + final int labelCharWidth; + if (labelWidthValue != null) { + labelCharWidth = labelWidthValue; + } else { + paint.getTextBounds(KEY_LABEL_REFERENCE_CHAR, 0, 1, mTextBounds); + labelCharWidth = mTextBounds.width(); + mTextWidthCache.put(labelSize, labelCharWidth); + } + return labelCharWidth; + } + private static void drawIcon(Canvas canvas, Drawable icon, int x, int y, int width, int height) { canvas.translate(x, y); @@ -935,7 +990,7 @@ public class KeyboardView extends View implements PointerTracker.UIProxy { previewText.setTypeface(Typeface.DEFAULT_BOLD); } else { previewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mPreviewTextSize); - previewText.setTypeface(mKeyLetterStyle); + previewText.setTypeface(mKeyTextStyle); } } else { final Drawable previewIcon = key.getPreviewIcon(); diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java index fe27ab412..a6ac76751 100644 --- a/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java +++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java @@ -57,9 +57,9 @@ public class LatinKeyboard extends Keyboard { private final Drawable mSpaceIcon; private final Drawable mSpacePreviewIcon; private final int mSpaceKeyIndex; - private final Drawable mSpaceAutoCorrectionIndicator; - private final Drawable mButtonArrowLeftIcon; - private final Drawable mButtonArrowRightIcon; + private final Drawable mAutoCorrectionSpacebarLedIcon; + private final Drawable mSpacebarArrowLeftIcon; + private final Drawable mSpacebarArrowRightIcon; private final int mSpacebarTextColor; private final int mSpacebarTextShadowColor; private float mSpacebarTextFadeFactor = 0.0f; @@ -90,7 +90,6 @@ public class LatinKeyboard extends Keyboard { public LatinKeyboard(Context context, KeyboardId id, int width) { super(context, id.getXmlId(), id, width); - final Resources res = context.getResources(); mContext = context; final List<Key> keys = getKeys(); @@ -118,19 +117,20 @@ public class LatinKeyboard extends Keyboard { mShortcutKey = (shortcutKeyIndex >= 0) ? keys.get(shortcutKeyIndex) : null; mEnabledShortcutIcon = (mShortcutKey != null) ? mShortcutKey.getIcon() : null; - mSpacebarTextColor = res.getColor(R.color.latinkeyboard_bar_language_text); - if (id.mColorScheme == KeyboardView.COLOR_SCHEME_BLACK) { - mSpacebarTextShadowColor = res.getColor( - R.color.latinkeyboard_bar_language_shadow_black); - mDisabledShortcutIcon = res.getDrawable(R.drawable.sym_bkeyboard_voice_off); - } else { // default color scheme is KeyboardView.COLOR_SCHEME_WHITE - mSpacebarTextShadowColor = res.getColor( - R.color.latinkeyboard_bar_language_shadow_white); - mDisabledShortcutIcon = res.getDrawable(R.drawable.sym_keyboard_voice_off_holo); - } - mSpaceAutoCorrectionIndicator = res.getDrawable(R.drawable.sym_keyboard_space_led); - mButtonArrowLeftIcon = res.getDrawable(R.drawable.sym_keyboard_language_arrows_left); - mButtonArrowRightIcon = res.getDrawable(R.drawable.sym_keyboard_language_arrows_right); + final TypedArray a = context.obtainStyledAttributes( + null, R.styleable.LatinKeyboard, R.attr.latinKeyboardStyle, R.style.LatinKeyboard); + mAutoCorrectionSpacebarLedIcon = a.getDrawable( + R.styleable.LatinKeyboard_autoCorrectionSpacebarLedIcon); + mDisabledShortcutIcon = a.getDrawable(R.styleable.LatinKeyboard_disabledShortcutIcon); + mSpacebarTextColor = a.getColor(R.styleable.LatinKeyboard_spacebarTextColor, 0); + mSpacebarTextShadowColor = a.getColor( + R.styleable.LatinKeyboard_spacebarTextShadowColor, 0); + mSpacebarArrowLeftIcon = a.getDrawable( + R.styleable.LatinKeyboard_spacebarArrowLeftIcon); + mSpacebarArrowRightIcon = a.getDrawable( + R.styleable.LatinKeyboard_spacebarArrowRightIcon); + a.recycle(); + // The threshold is "key width" x 1.25 mSpacebarLanguageSwitchThreshold = (getMostCommonKeyWidth() * 5) / 4; } @@ -163,6 +163,10 @@ public class LatinKeyboard extends Keyboard { view.invalidateKey(mShortcutKey); } + public boolean needsAutoCorrectionSpacebarLed() { + return mAutoCorrectionSpacebarLedIcon != null; + } + /** * @return a key which should be invalidated. */ @@ -282,7 +286,7 @@ public class LatinKeyboard extends Keyboard { } final String language = layoutSpacebar(paint, inputLocale, - mButtonArrowLeftIcon, mButtonArrowRightIcon, width, height, + mSpacebarArrowLeftIcon, mSpacebarArrowRightIcon, width, height, getTextSizeFromTheme(mContext.getTheme(), textStyle, defaultTextSize)); // Draw language text with shadow @@ -303,21 +307,21 @@ public class LatinKeyboard extends Keyboard { if (mSubtypeSwitcher.useSpacebarLanguageSwitcher() && mSubtypeSwitcher.getEnabledKeyboardLocaleCount() > 1 && !(isPhoneKeyboard() || isNumberKeyboard())) { - mButtonArrowLeftIcon.setColorFilter(getSpacebarDrawableFilter(textFadeFactor)); - mButtonArrowRightIcon.setColorFilter(getSpacebarDrawableFilter(textFadeFactor)); - mButtonArrowLeftIcon.draw(canvas); - mButtonArrowRightIcon.draw(canvas); + mSpacebarArrowLeftIcon.setColorFilter(getSpacebarDrawableFilter(textFadeFactor)); + mSpacebarArrowRightIcon.setColorFilter(getSpacebarDrawableFilter(textFadeFactor)); + mSpacebarArrowLeftIcon.draw(canvas); + mSpacebarArrowRightIcon.draw(canvas); } } // Draw the spacebar icon at the bottom if (isAutoCorrection) { final int iconWidth = width * SPACE_LED_LENGTH_PERCENT / 100; - final int iconHeight = mSpaceAutoCorrectionIndicator.getIntrinsicHeight(); + final int iconHeight = mAutoCorrectionSpacebarLedIcon.getIntrinsicHeight(); int x = (width - iconWidth) / 2; int y = height - iconHeight; - mSpaceAutoCorrectionIndicator.setBounds(x, y, x + iconWidth, y + iconHeight); - mSpaceAutoCorrectionIndicator.draw(canvas); + mAutoCorrectionSpacebarLedIcon.setBounds(x, y, x + iconWidth, y + iconHeight); + mAutoCorrectionSpacebarLedIcon.draw(canvas); } else if (mSpaceIcon != null) { final int iconWidth = mSpaceIcon.getIntrinsicWidth(); final int iconHeight = mSpaceIcon.getIntrinsicHeight(); diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java index 6b4e9469f..397ac7eec 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java +++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java @@ -22,6 +22,7 @@ import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.SubtypeSwitcher; import android.content.res.Resources; +import android.os.SystemClock; import android.util.Log; import android.view.MotionEvent; @@ -540,8 +541,11 @@ public class PointerTracker { public void onLongPressed(PointerTrackerQueue queue) { mKeyAlreadyProcessed = true; - if (queue != null) + if (queue != null) { + // TODO: Support chording + long-press input. + queue.releaseAllPointersExcept(this, SystemClock.uptimeMillis(), true); queue.remove(this); + } } public void onCancelEvent(int x, int y, long eventTime, PointerTrackerQueue queue) { @@ -635,8 +639,7 @@ public class PointerTracker { Key key = getKey(keyIndex); if (key.mCode == Keyboard.CODE_SHIFT) { mHandler.startLongPressShiftTimer(mLongPressShiftKeyTimeout, keyIndex, this); - } else if (key.mManualTemporaryUpperCaseCode != Keyboard.CODE_DUMMY - && mKeyboard.isManualTemporaryUpperCase()) { + } else if (key.hasUppercaseLetter() && mKeyboard.isManualTemporaryUpperCase()) { // We need not start long press timer on the key which has manual temporary upper case // code defined and the keyboard is in manual temporary upper case mode. return; @@ -663,10 +666,9 @@ public class PointerTracker { mKeyDetector.getKeyIndexAndNearbyCodes(x, y, codes); // If keyboard is in manual temporary upper case state and key has manual temporary - // shift code, alternate character code should be sent. - if (mKeyboard.isManualTemporaryUpperCase() - && key.mManualTemporaryUpperCaseCode != Keyboard.CODE_DUMMY) { - code = key.mManualTemporaryUpperCaseCode; + // uppercase letter as key hint letter, alternate character code should be sent. + if (mKeyboard.isManualTemporaryUpperCase() && key.hasUppercaseLetter()) { + code = key.mHintLetter.charAt(0); codes[0] = code; } diff --git a/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java b/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java index 60d87f789..ff64c73cd 100644 --- a/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/PopupMiniKeyboardView.java @@ -37,7 +37,6 @@ public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel { private int mOriginX; private int mOriginY; - private int mTrackerId; private long mDownTime; public PopupMiniKeyboardView(Context context, AttributeSet attrs) { @@ -98,30 +97,19 @@ public class PopupMiniKeyboardView extends KeyboardView implements PopupPanel { mOriginX = x + container.getPaddingLeft() - mCoordinates[0]; mOriginY = y + container.getPaddingTop() - mCoordinates[1]; - mTrackerId = tracker.mPointerId; mDownTime = SystemClock.uptimeMillis(); // Inject down event on the key to mini keyboard. - final MotionEvent downEvent = translateMotionEvent(MotionEvent.ACTION_DOWN, pointX, - pointY + parentKey.mHeight / 2, mDownTime); + final MotionEvent downEvent = MotionEvent.obtain(mDownTime, mDownTime, + MotionEvent.ACTION_DOWN, pointX - mOriginX, + pointY + parentKey.mHeight / 2 - mOriginY, 0); onTouchEvent(downEvent); downEvent.recycle(); } - private MotionEvent translateMotionEvent(int action, float x, float y, long eventTime) { - return MotionEvent.obtain(mDownTime, eventTime, action, x - mOriginX, y - mOriginY, 0); - } - @Override public boolean onTouchEvent(MotionEvent me) { - final int index = me.getActionIndex(); - final int id = me.getPointerId(index); - if (id == mTrackerId) { - final MotionEvent translated = translateMotionEvent(me.getAction(), me.getX(index), - me.getY(index), me.getEventTime()); - super.onTouchEvent(translated); - translated.recycle(); - } - return true; + me.offsetLocation(-mOriginX, -mOriginY); + return super.onTouchEvent(me); } } diff --git a/java/src/com/android/inputmethod/keyboard/SlidingLocaleDrawable.java b/java/src/com/android/inputmethod/keyboard/SlidingLocaleDrawable.java index 5cf31cb14..a20bf1cd3 100644 --- a/java/src/com/android/inputmethod/keyboard/SlidingLocaleDrawable.java +++ b/java/src/com/android/inputmethod/keyboard/SlidingLocaleDrawable.java @@ -16,22 +16,21 @@ package com.android.inputmethod.keyboard; -import com.android.inputmethod.latin.R; -import com.android.inputmethod.latin.SubtypeSwitcher; - import android.content.Context; -import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.Paint; -import android.graphics.PixelFormat; import android.graphics.Paint.Align; +import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; import android.text.TextPaint; import android.view.ViewConfiguration; +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.SubtypeSwitcher; + /** * 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 @@ -39,16 +38,16 @@ import android.view.ViewConfiguration; */ public class SlidingLocaleDrawable extends Drawable { private static final int SLIDE_SPEED_MULTIPLIER_RATIO = 150; - private final Context mContext; - private final Resources mRes; private final int mWidth; private final int mHeight; private final Drawable mBackground; + private final int mSpacebarTextColor; private final TextPaint mTextPaint; private final int mMiddleX; private final Drawable mLeftDrawable; private final Drawable mRightDrawable; private final int mThreshold; + private int mDiff; private boolean mHitThreshold; private String mCurrentLanguage; @@ -56,29 +55,31 @@ public class SlidingLocaleDrawable extends Drawable { private String mPrevLanguage; public SlidingLocaleDrawable(Context context, Drawable background, int width, int height) { - mContext = context; - mRes = context.getResources(); mBackground = background; Keyboard.setDefaultBounds(mBackground); mWidth = width; mHeight = height; final TextPaint textPaint = new TextPaint(); - textPaint.setTextSize(getTextSizeFromTheme(android.R.style.TextAppearance_Medium, 18)); + textPaint.setTextSize(getTextSizeFromTheme( + context, android.R.style.TextAppearance_Medium, 18)); textPaint.setColor(Color.TRANSPARENT); textPaint.setTextAlign(Align.CENTER); textPaint.setAntiAlias(true); mTextPaint = textPaint; mMiddleX = (mWidth - mBackground.getIntrinsicWidth()) / 2; - 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); - mThreshold = ViewConfiguration.get(mContext).getScaledTouchSlop(); + + final TypedArray a = context.obtainStyledAttributes( + null, R.styleable.LatinKeyboard, R.attr.latinKeyboardStyle, R.style.LatinKeyboard); + mSpacebarTextColor = a.getColor(R.styleable.LatinKeyboard_spacebarTextColor, 0); + mLeftDrawable = a.getDrawable(R.styleable.LatinKeyboard_spacebarArrowPreviewLeftIcon); + mRightDrawable = a.getDrawable(R.styleable.LatinKeyboard_spacebarArrowPreviewRightIcon); + a.recycle(); + + mThreshold = ViewConfiguration.get(context).getScaledTouchSlop(); } - private int getTextSizeFromTheme(int style, int defValue) { - TypedArray array = mContext.getTheme().obtainStyledAttributes( + private static int getTextSizeFromTheme(Context context, int style, int defValue) { + TypedArray array = context.getTheme().obtainStyledAttributes( style, new int[] { android.R.attr.textSize }); int textSize = array.getDimensionPixelSize(array.getResourceId(0, 0), defValue); return textSize; @@ -118,7 +119,7 @@ public class SlidingLocaleDrawable extends Drawable { // Draw language text with shadow final float baseline = mHeight * LatinKeyboard.SPACEBAR_LANGUAGE_BASELINE - paint.descent(); - paint.setColor(mRes.getColor(R.color.latinkeyboard_feedback_language_text)); + paint.setColor(mSpacebarTextColor); 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); diff --git a/java/src/com/android/inputmethod/latin/CandidateView.java b/java/src/com/android/inputmethod/latin/CandidateView.java index b4f6b2c91..2a29e1f8f 100644 --- a/java/src/com/android/inputmethod/latin/CandidateView.java +++ b/java/src/com/android/inputmethod/latin/CandidateView.java @@ -18,6 +18,7 @@ package com.android.inputmethod.latin; import android.content.Context; import android.content.res.Resources; +import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.Typeface; import android.os.Handler; @@ -38,6 +39,7 @@ import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.view.ViewGroup; +import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; @@ -56,16 +58,29 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo private static final CharacterStyle BOLD_SPAN = new StyleSpan(Typeface.BOLD); private static final CharacterStyle UNDERLINE_SPAN = new UnderlineSpan(); - private static final int MAX_SUGGESTIONS = 16; + // The maximum number of suggestions available. See {@link Suggest#mPrefMaxSuggestions}. + private static final int MAX_SUGGESTIONS = 18; + private static final int UNSPECIFIED_MEASURESPEC = MeasureSpec.makeMeasureSpec( + 0, MeasureSpec.UNSPECIFIED); private static final boolean DBG = LatinImeLogger.sDBG; + private static final int NUM_CANDIDATES_IN_STRIP = 3; + private final ImageView mExpandCandidatesPane; + private final ImageView mCloseCandidatesPane; + private ViewGroup mCandidatesPane; + private ViewGroup mCandidatesPaneContainer; + private View mKeyboardView; private final ArrayList<TextView> mWords = new ArrayList<TextView>(); private final ArrayList<View> mDividers = new ArrayList<View>(); private final int mCandidatePadding; - private final boolean mConfigCandidateHighlightFontColorEnabled; + private final int mCandidateStripHeight; private final CharacterStyle mInvertedForegroundColorSpan; private final CharacterStyle mInvertedBackgroundColorSpan; + private final int mAutoCorrectHighlight; + private static final int AUTO_CORRECT_BOLD = 0x01; + private static final int AUTO_CORRECT_UNDERLINE = 0x02; + private static final int AUTO_CORRECT_INVERT = 0x04; private final int mColorTypedWord; private final int mColorAutoCorrect; private final int mColorSuggestedCandidate; @@ -129,27 +144,44 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo * @param attrs */ public CandidateView(Context context, AttributeSet attrs) { - super(context, attrs); + this(context, attrs, R.attr.candidateViewStyle); + } + + public CandidateView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); Resources res = context.getResources(); - mPreviewPopup = new PopupWindow(context); LayoutInflater inflater = LayoutInflater.from(context); + inflater.inflate(R.layout.candidates_strip, this); + + mPreviewPopup = new PopupWindow(context); mPreviewText = (TextView) inflater.inflate(R.layout.candidate_preview, null); mPreviewPopup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); mPreviewPopup.setContentView(mPreviewText); mPreviewPopup.setBackgroundDrawable(null); - mConfigCandidateHighlightFontColorEnabled = - res.getBoolean(R.bool.config_candidate_highlight_font_color_enabled); - mColorTypedWord = res.getColor(R.color.candidate_typed_word); - mColorAutoCorrect = res.getColor(R.color.candidate_auto_correct); - mColorSuggestedCandidate = res.getColor(R.color.candidate_suggested); - mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorTypedWord ^ 0x00ffffff); - mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorTypedWord); mCandidatePadding = res.getDimensionPixelOffset(R.dimen.candidate_padding); + mCandidateStripHeight = res.getDimensionPixelOffset(R.dimen.candidate_strip_height); for (int i = 0; i < MAX_SUGGESTIONS; i++) { - final TextView tv = (TextView)inflater.inflate(R.layout.candidate, null); + final TextView tv; + switch (i) { + case 0: + tv = (TextView)findViewById(R.id.candidate_left); + tv.setPadding(mCandidatePadding, 0, 0, 0); + break; + case 1: + tv = (TextView)findViewById(R.id.candidate_center); + break; + case 2: + tv = (TextView)findViewById(R.id.candidate_right); + break; + default: + tv = (TextView)inflater.inflate(R.layout.candidate, null); + break; + } + if (i < NUM_CANDIDATES_IN_STRIP) + setLayoutWeight(tv, 1.0f); tv.setTag(i); tv.setOnClickListener(this); if (i == 0) @@ -157,19 +189,53 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo mWords.add(tv); if (i > 0) { final View divider = inflater.inflate(R.layout.candidate_divider, null); + divider.measure(UNSPECIFIED_MEASURESPEC, UNSPECIFIED_MEASURESPEC); mDividers.add(divider); } } - scrollTo(0, getScrollY()); + final TypedArray a = context.obtainStyledAttributes( + attrs, R.styleable.CandidateView, defStyle, R.style.CandidateViewStyle); + mAutoCorrectHighlight = a.getInt(R.styleable.CandidateView_autoCorrectHighlight, 0); + mColorTypedWord = a.getColor(R.styleable.CandidateView_colorTypedWord, 0); + mColorAutoCorrect = a.getColor(R.styleable.CandidateView_colorAutoCorrect, 0); + mColorSuggestedCandidate = a.getColor(R.styleable.CandidateView_colorSuggested, 0); + mInvertedForegroundColorSpan = new ForegroundColorSpan(mColorTypedWord ^ 0x00ffffff); + mInvertedBackgroundColorSpan = new BackgroundColorSpan(mColorTypedWord); + + mExpandCandidatesPane = (ImageView)findViewById(R.id.expand_candidates_pane); + mExpandCandidatesPane.setImageDrawable( + a.getDrawable(R.styleable.CandidateView_iconExpandPane)); + mExpandCandidatesPane.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View view) { + expandCandidatesPane(); + } + }); + mCloseCandidatesPane = (ImageView)findViewById(R.id.close_candidates_pane); + mCloseCandidatesPane.setImageDrawable( + a.getDrawable(R.styleable.CandidateView_iconClosePane)); + mCloseCandidatesPane.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View view) { + closeCandidatesPane(); + } + }); + + a.recycle(); } /** * A connection back to the input method. * @param listener */ - public void setListener(Listener listener) { + public void setListener(Listener listener, View inputView) { mListener = listener; + mKeyboardView = inputView.findViewById(R.id.keyboard_view); + mCandidatesPane = (ViewGroup)inputView.findViewById(R.id.candidates_pane); + mCandidatesPane.setOnClickListener(this); + mCandidatesPaneContainer = (ViewGroup)inputView.findViewById( + R.id.candidates_pane_container); } public void setSuggestions(SuggestedWords suggestions) { @@ -183,22 +249,32 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo } } + private static void setLayoutWeight(View v, float weight) { + ViewGroup.LayoutParams lp = v.getLayoutParams(); + if (lp instanceof LinearLayout.LayoutParams) { + LinearLayout.LayoutParams llp = (LinearLayout.LayoutParams)lp; + llp.width = 0; + llp.weight = weight; + } + } + private CharSequence getStyledCandidateWord(CharSequence word, boolean isAutoCorrect) { if (!isAutoCorrect) return word; - final CharacterStyle style = mConfigCandidateHighlightFontColorEnabled ? BOLD_SPAN - : UNDERLINE_SPAN; final Spannable spannedWord = new SpannableString(word); - spannedWord.setSpan(style, 0, word.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); + if ((mAutoCorrectHighlight & AUTO_CORRECT_BOLD) != 0) + spannedWord.setSpan(BOLD_SPAN, 0, word.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); + if ((mAutoCorrectHighlight & AUTO_CORRECT_UNDERLINE) != 0) + spannedWord.setSpan(UNDERLINE_SPAN, 0, word.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); return spannedWord; } private int getCandidateTextColor(boolean isAutoCorrect, boolean isSuggestedCandidate, SuggestedWordInfo info) { final int color; - if (isAutoCorrect && mConfigCandidateHighlightFontColorEnabled) { + if (isAutoCorrect) { color = mColorAutoCorrect; - } else if (isSuggestedCandidate && mConfigCandidateHighlightFontColorEnabled) { + } else if (isSuggestedCandidate) { color = mColorSuggestedCandidate; } else { color = mColorTypedWord; @@ -216,7 +292,14 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo final List<SuggestedWordInfo> suggestedWordInfoList = suggestions.mSuggestedWordInfoList; clear(); + final int paneWidth = getWidth(); + final int dividerWidth = mDividers.get(0).getMeasuredWidth(); + int x = 0; + int y = 0; + int fromIndex = NUM_CANDIDATES_IN_STRIP; final int count = Math.min(mWords.size(), suggestions.size()); + closeCandidatesPane(); + mExpandCandidatesPane.setEnabled(count >= NUM_CANDIDATES_IN_STRIP); for (int i = 0; i < count; i++) { final CharSequence word = suggestions.getWord(i); if (word == null) continue; @@ -233,39 +316,97 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo final boolean isPunctuationSuggestions = (word.length() == 1 && count > 1); final TextView tv = mWords.get(i); + // TODO: Reorder candidates in strip as appropriate. The center candidate should hold + // the word when space is typed (valid typed word or auto corrected word). tv.setTextColor(getCandidateTextColor(isAutoCorrect, isSuggestedCandidate || isPunctuationSuggestions, info)); tv.setText(getStyledCandidateWord(word, isAutoCorrect)); - if (i == 0) { - tv.setPadding(mCandidatePadding, 0, 0, 0); - } else if (i == count - 1) { - tv.setPadding(0, 0, mCandidatePadding, 0); - } else { - tv.setPadding(0, 0, 0, 0); + // TODO: call TextView.setTextScaleX() to fit the candidate in single line. + if (i >= NUM_CANDIDATES_IN_STRIP) { + tv.measure(UNSPECIFIED_MEASURESPEC, UNSPECIFIED_MEASURESPEC); + final int width = tv.getMeasuredWidth(); + // TODO: Handle overflow case. + if (dividerWidth + x + width >= paneWidth) { + centeringCandidates(fromIndex, i - 1, x, paneWidth); + x = 0; + y += mCandidateStripHeight; + fromIndex = i; + } + if (x != 0) { + final View divider = mDividers.get(i - NUM_CANDIDATES_IN_STRIP); + mCandidatesPane.addView(divider); + placeCandidateAt(divider, x, y); + x += dividerWidth; + } + mCandidatesPane.addView(tv); + placeCandidateAt(tv, x, y); + x += width; } - if (i > 0) - addView(mDividers.get(i - 1)); - addView(tv); if (DBG && info != null) { final TextView dv = new TextView(getContext(), null); dv.setTextSize(10.0f); dv.setTextColor(0xff808080); dv.setText(info.getDebugString()); - addView(dv); + // TODO: debug view for candidate strip needed. + mCandidatesPane.addView(dv); LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)dv.getLayoutParams(); lp.gravity = Gravity.BOTTOM; } } + if (x != 0) { + // Centering last candidates row. + centeringCandidates(fromIndex, count - 1, x, paneWidth); + } + } + + private void placeCandidateAt(View v, int x, int y) { + ViewGroup.LayoutParams lp = v.getLayoutParams(); + if (lp instanceof ViewGroup.MarginLayoutParams) { + ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)lp; + mlp.width = v.getMeasuredWidth(); + mlp.height = v.getMeasuredHeight(); + mlp.setMargins(x, y + (mCandidateStripHeight - mlp.height) / 2, 0, 0); + } + } - scrollTo(0, getScrollY()); - requestLayout(); + private void centeringCandidates(int from, int to, int width, int paneWidth) { + final ViewGroup pane = mCandidatesPane; + final int fromIndex = pane.indexOfChild(mWords.get(from)); + final int toIndex = pane.indexOfChild(mWords.get(to)); + final int offset = (paneWidth - width) / 2; + for (int index = fromIndex; index <= toIndex; index++) { + offsetMargin(pane.getChildAt(index), offset, 0); + } + } + + private static void offsetMargin(View v, int dx, int dy) { + if (v == null) + return; + ViewGroup.LayoutParams lp = v.getLayoutParams(); + if (lp instanceof ViewGroup.MarginLayoutParams) { + ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)lp; + mlp.setMargins(mlp.leftMargin + dx, mlp.topMargin + dy, 0, 0); + } + } + + private void expandCandidatesPane() { + mExpandCandidatesPane.setVisibility(View.GONE); + mCloseCandidatesPane.setVisibility(View.VISIBLE); + mCandidatesPaneContainer.setMinimumHeight(mKeyboardView.getMeasuredHeight()); + mCandidatesPaneContainer.setVisibility(View.VISIBLE); + mKeyboardView.setVisibility(View.GONE); + } + + private void closeCandidatesPane() { + mExpandCandidatesPane.setVisibility(View.VISIBLE); + mCloseCandidatesPane.setVisibility(View.GONE); + mCandidatesPaneContainer.setVisibility(View.GONE); + mKeyboardView.setVisibility(View.VISIBLE); } public void onAutoCorrectionInverted(CharSequence autoCorrectedWord) { - // Displaying auto corrected word as inverted is enabled only when highlighting candidate - // with color is disabled. - if (mConfigCandidateHighlightFontColorEnabled) + if ((mAutoCorrectHighlight & AUTO_CORRECT_INVERT) == 0) return; final TextView tv = mWords.get(1); final Spannable word = new SpannableString(autoCorrectedWord); @@ -278,10 +419,6 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo mShowingAutoCorrectionInverted = true; } - public boolean isConfigCandidateHighlightFontColorEnabled() { - return mConfigCandidateHighlightFontColorEnabled; - } - public boolean isShowingAddToDictionaryHint() { return mShowingAddToDictionary; } @@ -310,7 +447,9 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo public void clear() { mShowingAddToDictionary = false; mShowingAutoCorrectionInverted = false; - removeAllViews(); + for (int i = 0; i < NUM_CANDIDATES_IN_STRIP; i++) + mWords.get(i).setText(null); + mCandidatesPane.removeAllViews(); } private void hidePreview() { @@ -349,9 +488,13 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo @Override public boolean onLongClick(View view) { - final int index = (Integer) view.getTag(); + final Object tag = view.getTag(); + if (!(tag instanceof Integer)) + return true; + final int index = (Integer) tag; if (index >= mSuggestions.size()) return true; + final CharSequence word = mSuggestions.getWord(index); if (word.length() < 2) return false; @@ -361,15 +504,22 @@ public class CandidateView extends LinearLayout implements OnClickListener, OnLo @Override public void onClick(View view) { - final int index = (Integer) view.getTag(); + final Object tag = view.getTag(); + if (!(tag instanceof Integer)) + return; + final int index = (Integer) tag; if (index >= mSuggestions.size()) return; + final CharSequence word = mSuggestions.getWord(index); if (mShowingAddToDictionary && index == 0) { addToDictionary(word); } else { mListener.pickSuggestionManually(index, word); } + // Because some punctuation letters are not treated as word separator depending on locale, + // {@link #setSuggestions} might not be called and candidates pane left opened. + closeCandidatesPane(); } @Override diff --git a/java/src/com/android/inputmethod/latin/DictionaryCollection.java b/java/src/com/android/inputmethod/latin/DictionaryCollection.java index 3fcb6ed55..5e7de3e6b 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryCollection.java +++ b/java/src/com/android/inputmethod/latin/DictionaryCollection.java @@ -54,8 +54,8 @@ public class DictionaryCollection extends Dictionary { @Override public boolean isValidWord(CharSequence word) { - for (final Dictionary dict : mDictionaries) - if (dict.isValidWord(word)) return true; + for (int i = mDictionaries.size() - 1; i >= 0; --i) + if (mDictionaries.get(i).isValidWord(word)) return true; return false; } diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 940f6b835..87912eb00 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -502,7 +502,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar super.setInputView(view); mCandidateViewContainer = view.findViewById(R.id.candidates_container); mCandidateView = (CandidateView) view.findViewById(R.id.candidates); - mCandidateView.setListener(this); + mCandidateView.setListener(this, view); mCandidateStripHeight = (int)mResources.getDimension(R.dimen.candidate_strip_height); } @@ -1464,7 +1464,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar if (mCandidateView != null) { mCandidateView.setSuggestions(words); - if (mCandidateView.isConfigCandidateHighlightFontColorEnabled()) { + if (mKeyboardSwitcher.getLatinKeyboard().needsAutoCorrectionSpacebarLed()) { mKeyboardSwitcher.onAutoCorrectionStateChanged( words.hasWordAboveAutoCorrectionScoreThreshold()); } diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 33f9820cc..62788fb9e 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -84,7 +84,7 @@ public class Suggest implements Dictionary.WordCallback { private final Map<String, Dictionary> mUnigramDictionaries = new HashMap<String, Dictionary>(); private final Map<String, Dictionary> mBigramDictionaries = new HashMap<String, Dictionary>(); - private int mPrefMaxSuggestions = 12; + private int mPrefMaxSuggestions = 18; private static final int PREF_MAX_BIGRAMS = 60; |