From 6f7905ae757c30ac0f8080f025b88afc61a6f6b1 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Wed, 1 May 2013 17:02:27 +0900 Subject: [PATCH] Introduce MoreSuggestionsListener class Bug: 8694255 Change-Id: I9420a7cb1983c6211f58d13b3b11db4347c3f99b --- .../latin/suggestions/MoreSuggestions.java | 34 ++++++++++++------- .../suggestions/MoreSuggestionsView.java | 27 +++++++++++++-- .../suggestions/SuggestionStripView.java | 12 +++---- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java index 3037669c0..09f81d4c7 100644 --- a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java +++ b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java @@ -23,19 +23,28 @@ import android.graphics.drawable.Drawable; import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.Keyboard; +import com.android.inputmethod.keyboard.KeyboardActionListener; import com.android.inputmethod.keyboard.TypefaceUtils; 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.R; import com.android.inputmethod.latin.SuggestedWords; +import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.Utils; public final class MoreSuggestions extends Keyboard { public static final int SUGGESTION_CODE_BASE = 1024; - MoreSuggestions(final MoreSuggestionsParam params) { + public final SuggestedWords mSuggestedWords; + + public static abstract class MoreSuggestionsListener extends KeyboardActionListener.Adapter { + public abstract void onSuggestionSelected(final int index, final SuggestedWordInfo info); + } + + MoreSuggestions(final MoreSuggestionsParam params, final SuggestedWords suggestedWords) { super(params); + mSuggestedWords = suggestedWords; } private static final class MoreSuggestionsParam extends KeyboardParams { @@ -52,8 +61,9 @@ public final class MoreSuggestions extends Keyboard { super(); } - public int layout(final SuggestedWords suggestions, final int fromPos, final int maxWidth, - final int minWidth, final int maxRow, final Paint paint, final Resources res) { + public int layout(final SuggestedWords suggestedWords, final int fromPos, + final int maxWidth, final int minWidth, final int maxRow, final Paint paint, + final Resources res) { clearKeys(); mDivider = res.getDrawable(R.drawable.more_suggestions_divider); mDividerWidth = mDivider.getIntrinsicWidth(); @@ -61,9 +71,9 @@ public final class MoreSuggestions extends Keyboard { int row = 0; int pos = fromPos, rowStartPos = fromPos; - final int size = Math.min(suggestions.size(), SuggestionStripView.MAX_SUGGESTIONS); + final int size = Math.min(suggestedWords.size(), SuggestionStripView.MAX_SUGGESTIONS); while (pos < size) { - final String word = suggestions.getWord(pos); + final String word = suggestedWords.getWord(pos); // TODO: Should take care of text x-scaling. mWidths[pos] = (int)(TypefaceUtils.getLabelWidth(word, paint) + padding); final int numColumn = pos - rowStartPos + 1; @@ -163,7 +173,7 @@ public final class MoreSuggestions extends Keyboard { public static final class Builder extends KeyboardBuilder { private final MoreSuggestionsView mPaneView; - private SuggestedWords mSuggestions; + private SuggestedWords mSuggestedWords; private int mFromPos; private int mToPos; @@ -172,7 +182,7 @@ public final class MoreSuggestions extends Keyboard { mPaneView = paneView; } - public Builder layout(final SuggestedWords suggestions, final int fromPos, + public Builder layout(final SuggestedWords suggestedWords, final int fromPos, final int maxWidth, final int minWidth, final int maxRow, final Keyboard parentKeyboard) { final int xmlId = R.xml.kbd_suggestions_pane_template; @@ -180,11 +190,11 @@ public final class MoreSuggestions extends Keyboard { mParams.mVerticalGap = mParams.mTopPadding = parentKeyboard.mVerticalGap / 2; mPaneView.updateKeyboardGeometry(mParams.mDefaultRowHeight); - final int count = mParams.layout(suggestions, fromPos, maxWidth, minWidth, maxRow, + final int count = mParams.layout(suggestedWords, fromPos, maxWidth, minWidth, maxRow, mPaneView.newLabelPaint(null /* key */), mResources); mFromPos = fromPos; mToPos = fromPos + count; - mSuggestions = suggestions; + mSuggestedWords = suggestedWords; return this; } @@ -195,8 +205,8 @@ public final class MoreSuggestions extends Keyboard { final int x = params.getX(pos); final int y = params.getY(pos); final int width = params.getWidth(pos); - final String word = mSuggestions.getWord(pos).toString(); - final String info = Utils.getDebugInfo(mSuggestions, pos); + final String word = mSuggestedWords.getWord(pos); + final String info = Utils.getDebugInfo(mSuggestedWords, pos); final int index = pos + SUGGESTION_CODE_BASE; final Key key = new Key( params, word, info, KeyboardIconsSet.ICON_UNDEFINED, index, null, x, y, @@ -211,7 +221,7 @@ public final class MoreSuggestions extends Keyboard { params.onAddKey(divider); } } - return new MoreSuggestions(params); + return new MoreSuggestions(params, mSuggestedWords); } } diff --git a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java index 6509f394b..d585b5c7f 100644 --- a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java +++ b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java @@ -18,15 +18,21 @@ package com.android.inputmethod.latin.suggestions; import android.content.Context; import android.util.AttributeSet; +import android.util.Log; +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.MoreSuggestionsListener; /** * A view that renders a virtual {@link MoreSuggestions}. It handles rendering of keys and detecting * key presses and touch movements. */ public final class MoreSuggestionsView extends MoreKeysKeyboardView { + private static final String TAG = MoreSuggestionsView.class.getSimpleName(); + public MoreSuggestionsView(final Context context, final AttributeSet attrs) { this(context, attrs, R.attr.moreSuggestionsViewStyle); } @@ -54,9 +60,24 @@ public final class MoreSuggestionsView extends MoreKeysKeyboardView { @Override public void onCodeInput(final int code, final int x, final int y) { - final int index = code - MoreSuggestions.SUGGESTION_CODE_BASE; - if (index >= 0 && index < SuggestionStripView.MAX_SUGGESTIONS) { - mListener.onCustomRequest(index); + final Keyboard keyboard = getKeyboard(); + if (!(keyboard instanceof MoreSuggestions)) { + Log.e(TAG, "Expected keyboard is MoreSuggestions, but found " + + keyboard.getClass().getName()); + return; } + final SuggestedWords suggestedWords = ((MoreSuggestions)keyboard).mSuggestedWords; + final int index = code - MoreSuggestions.SUGGESTION_CODE_BASE; + if (index < 0 || index >= suggestedWords.size()) { + Log.e(TAG, "Selected suggestion has an illegal index: " + index); + return; + } + if (!(mListener instanceof MoreSuggestionsListener)) { + Log.e(TAG, "Expected mListener is MoreSuggestionsListener, but found " + + mListener.getClass().getName()); + return; + } + ((MoreSuggestionsListener)mListener).onSuggestionSelected( + index, suggestedWords.getInfo(index)); } } diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java index 2a21ec2f5..ad350a02f 100644 --- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java +++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java @@ -50,7 +50,6 @@ import android.widget.RelativeLayout; import android.widget.TextView; import com.android.inputmethod.keyboard.Keyboard; -import com.android.inputmethod.keyboard.KeyboardActionListener; import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.keyboard.MainKeyboardView; import com.android.inputmethod.keyboard.MoreKeysPanel; @@ -65,6 +64,7 @@ import com.android.inputmethod.latin.SuggestedWords; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.Utils; import com.android.inputmethod.latin.define.ProductionFlag; +import com.android.inputmethod.latin.suggestions.MoreSuggestions.MoreSuggestionsListener; import com.android.inputmethod.research.ResearchLogger; import java.util.ArrayList; @@ -93,7 +93,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick private final ArrayList mDividers = CollectionUtils.newArrayList(); Listener mListener; - SuggestedWords mSuggestedWords = SuggestedWords.EMPTY; + private SuggestedWords mSuggestedWords = SuggestedWords.EMPTY; private final SuggestionStripViewParams mParams; private static final float MIN_TEXT_XSCALE = 0.70f; @@ -652,15 +652,11 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick dismissMoreSuggestions(); } - private final KeyboardActionListener mMoreSuggestionsListener = - new KeyboardActionListener.Adapter() { + private final MoreSuggestionsListener mMoreSuggestionsListener = new MoreSuggestionsListener() { @Override - public boolean onCustomRequest(final int requestCode) { - final int index = requestCode; - final SuggestedWordInfo wordInfo = mSuggestedWords.getInfo(index); + public void onSuggestionSelected(final int index, final SuggestedWordInfo wordInfo) { mListener.pickSuggestionManually(index, wordInfo); dismissMoreSuggestions(); - return true; } @Override