From 5fc1510bc1cbe9bcd1105a403e07b5400470ae96 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Tue, 20 May 2014 17:31:24 +0900 Subject: [PATCH] Refactor MoreKeysKeyboardView to use Key class This can make MoreSuggestionsView to use extended Key class to hold a index of a suggested word. Change-Id: I54d03d2447b04e3caf3e19e7cadcd391cbf58dd5 --- .../keyboard/MoreKeysKeyboardView.java | 5 ++-- .../latin/suggestions/MoreSuggestions.java | 24 ++++++++++++------- .../suggestions/MoreSuggestionsView.java | 11 +++++++-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java index 65242dd76..4a2b37e4c 100644 --- a/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java @@ -130,7 +130,7 @@ public class MoreKeysKeyboardView extends KeyboardView implements MoreKeysPanel public void onUpEvent(final int x, final int y, final int pointerId, final long eventTime) { if (mCurrentKey != null && mActivePointerId == pointerId) { updateReleaseKeyGraphics(mCurrentKey); - onCodeInput(mCurrentKey.getCode(), x, y); + onKeyInput(mCurrentKey, x, y); mCurrentKey = null; } } @@ -138,7 +138,8 @@ public class MoreKeysKeyboardView extends KeyboardView implements MoreKeysPanel /** * Performs the specific action for this panel when the user presses a key on the panel. */ - protected void onCodeInput(final int code, final int x, final int y) { + protected void onKeyInput(final Key key, final int x, final int y) { + final int code = key.getCode(); if (code == Constants.CODE_OUTPUT_TEXT) { mListener.onTextInput(mCurrentKey.getOutputText()); } else if (code != Constants.CODE_UNSPECIFIED) { diff --git a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java index 5a325ea82..e90b15ca5 100644 --- a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java +++ b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java @@ -27,14 +27,13 @@ import com.android.inputmethod.keyboard.KeyboardActionListener; import com.android.inputmethod.keyboard.internal.KeyboardBuilder; import com.android.inputmethod.keyboard.internal.KeyboardIconsSet; import com.android.inputmethod.keyboard.internal.KeyboardParams; +import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.SuggestedWords; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.utils.TypefaceUtils; public final class MoreSuggestions extends Keyboard { - public static final int SUGGESTION_CODE_BASE = 1024; - public final SuggestedWords mSuggestedWords; public static abstract class MoreSuggestionsListener extends KeyboardActionListener.Adapter { @@ -178,7 +177,7 @@ public final class MoreSuggestions extends Keyboard { } } - private static boolean isIndexSubjectToAutoCorrection(final SuggestedWords suggestedWords, + static boolean isIndexSubjectToAutoCorrection(final SuggestedWords suggestedWords, final int index) { return suggestedWords.mWillAutoCorrect && index == SuggestedWords.INDEX_OF_AUTO_CORRECTION; } @@ -226,11 +225,7 @@ public final class MoreSuggestions extends Keyboard { word = mSuggestedWords.getLabel(index); info = mSuggestedWords.getDebugString(index); } - final int indexInMoreSuggestions = index + SUGGESTION_CODE_BASE; - final Key key = new Key(word, KeyboardIconsSet.ICON_UNDEFINED, - indexInMoreSuggestions, null /* outputText */, info, 0 /* labelFlags */, - Key.BACKGROUND_TYPE_NORMAL, x, y, width, params.mDefaultRowHeight, - params.mHorizontalGap, params.mVerticalGap); + final Key key = new MoreSuggestionKey(word, info, index, params); params.markAsEdgeKey(key, index); params.onAddKey(key); final int columnNumber = params.getColumnNumber(index); @@ -245,6 +240,19 @@ public final class MoreSuggestions extends Keyboard { } } + static final class MoreSuggestionKey extends Key { + public final int mSuggestedWordIndex; + + public MoreSuggestionKey(final String word, final String info, final int index, + final MoreSuggestionsParam params) { + super(word /* label */, KeyboardIconsSet.ICON_UNDEFINED, Constants.CODE_OUTPUT_TEXT, + word /* outputText */, info, 0 /* labelFlags */, Key.BACKGROUND_TYPE_NORMAL, + params.getX(index), params.getY(index), params.getWidth(index), + params.mDefaultRowHeight, params.mHorizontalGap, params.mVerticalGap); + mSuggestedWordIndex = index; + } + } + private static final class Divider extends Key.Spacer { private final Drawable mIcon; diff --git a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java index 549ff0d9d..7fd64c4bf 100644 --- a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java +++ b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java @@ -20,10 +20,12 @@ import android.content.Context; import android.util.AttributeSet; import android.util.Log; +import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.MoreKeysKeyboardView; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.SuggestedWords; +import com.android.inputmethod.latin.suggestions.MoreSuggestions.MoreSuggestionKey; import com.android.inputmethod.latin.suggestions.MoreSuggestions.MoreSuggestionsListener; /** @@ -59,7 +61,12 @@ public final class MoreSuggestionsView extends MoreKeysKeyboardView { } @Override - public void onCodeInput(final int code, final int x, final int y) { + protected void onKeyInput(final Key key, final int x, final int y) { + if (!(key instanceof MoreSuggestionKey)) { + Log.e(TAG, "Expected key is MoreSuggestionKey, but found " + + key.getClass().getName()); + return; + } final Keyboard keyboard = getKeyboard(); if (!(keyboard instanceof MoreSuggestions)) { Log.e(TAG, "Expected keyboard is MoreSuggestions, but found " @@ -67,7 +74,7 @@ public final class MoreSuggestionsView extends MoreKeysKeyboardView { return; } final SuggestedWords suggestedWords = ((MoreSuggestions)keyboard).mSuggestedWords; - final int index = code - MoreSuggestions.SUGGESTION_CODE_BASE; + final int index = ((MoreSuggestionKey)key).mSuggestedWordIndex; if (index < 0 || index >= suggestedWords.size()) { Log.e(TAG, "Selected suggestion has an illegal index: " + index); return;