Introduce MoreSuggestionsListener class

Bug: 8694255
Change-Id: I9420a7cb1983c6211f58d13b3b11db4347c3f99b
This commit is contained in:
Tadashi G. Takaoka 2013-05-01 17:02:27 +09:00
parent 17a353831a
commit 6f7905ae75
3 changed files with 50 additions and 23 deletions

View file

@ -23,19 +23,28 @@ import android.graphics.drawable.Drawable;
import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.KeyboardActionListener;
import com.android.inputmethod.keyboard.TypefaceUtils; import com.android.inputmethod.keyboard.TypefaceUtils;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder; import com.android.inputmethod.keyboard.internal.KeyboardBuilder;
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet; import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
import com.android.inputmethod.keyboard.internal.KeyboardParams; import com.android.inputmethod.keyboard.internal.KeyboardParams;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.SuggestedWords; import com.android.inputmethod.latin.SuggestedWords;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.Utils; import com.android.inputmethod.latin.Utils;
public final class MoreSuggestions extends Keyboard { public final class MoreSuggestions extends Keyboard {
public static final int SUGGESTION_CODE_BASE = 1024; 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); super(params);
mSuggestedWords = suggestedWords;
} }
private static final class MoreSuggestionsParam extends KeyboardParams { private static final class MoreSuggestionsParam extends KeyboardParams {
@ -52,8 +61,9 @@ public final class MoreSuggestions extends Keyboard {
super(); super();
} }
public int layout(final SuggestedWords suggestions, final int fromPos, final int maxWidth, public int layout(final SuggestedWords suggestedWords, final int fromPos,
final int minWidth, final int maxRow, final Paint paint, final Resources res) { final int maxWidth, final int minWidth, final int maxRow, final Paint paint,
final Resources res) {
clearKeys(); clearKeys();
mDivider = res.getDrawable(R.drawable.more_suggestions_divider); mDivider = res.getDrawable(R.drawable.more_suggestions_divider);
mDividerWidth = mDivider.getIntrinsicWidth(); mDividerWidth = mDivider.getIntrinsicWidth();
@ -61,9 +71,9 @@ public final class MoreSuggestions extends Keyboard {
int row = 0; int row = 0;
int pos = fromPos, rowStartPos = fromPos; 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) { while (pos < size) {
final String word = suggestions.getWord(pos); final String word = suggestedWords.getWord(pos);
// TODO: Should take care of text x-scaling. // TODO: Should take care of text x-scaling.
mWidths[pos] = (int)(TypefaceUtils.getLabelWidth(word, paint) + padding); mWidths[pos] = (int)(TypefaceUtils.getLabelWidth(word, paint) + padding);
final int numColumn = pos - rowStartPos + 1; final int numColumn = pos - rowStartPos + 1;
@ -163,7 +173,7 @@ public final class MoreSuggestions extends Keyboard {
public static final class Builder extends KeyboardBuilder<MoreSuggestionsParam> { public static final class Builder extends KeyboardBuilder<MoreSuggestionsParam> {
private final MoreSuggestionsView mPaneView; private final MoreSuggestionsView mPaneView;
private SuggestedWords mSuggestions; private SuggestedWords mSuggestedWords;
private int mFromPos; private int mFromPos;
private int mToPos; private int mToPos;
@ -172,7 +182,7 @@ public final class MoreSuggestions extends Keyboard {
mPaneView = paneView; 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 int maxWidth, final int minWidth, final int maxRow,
final Keyboard parentKeyboard) { final Keyboard parentKeyboard) {
final int xmlId = R.xml.kbd_suggestions_pane_template; 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; mParams.mVerticalGap = mParams.mTopPadding = parentKeyboard.mVerticalGap / 2;
mPaneView.updateKeyboardGeometry(mParams.mDefaultRowHeight); 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); mPaneView.newLabelPaint(null /* key */), mResources);
mFromPos = fromPos; mFromPos = fromPos;
mToPos = fromPos + count; mToPos = fromPos + count;
mSuggestions = suggestions; mSuggestedWords = suggestedWords;
return this; return this;
} }
@ -195,8 +205,8 @@ public final class MoreSuggestions extends Keyboard {
final int x = params.getX(pos); final int x = params.getX(pos);
final int y = params.getY(pos); final int y = params.getY(pos);
final int width = params.getWidth(pos); final int width = params.getWidth(pos);
final String word = mSuggestions.getWord(pos).toString(); final String word = mSuggestedWords.getWord(pos);
final String info = Utils.getDebugInfo(mSuggestions, pos); final String info = Utils.getDebugInfo(mSuggestedWords, pos);
final int index = pos + SUGGESTION_CODE_BASE; final int index = pos + SUGGESTION_CODE_BASE;
final Key key = new Key( final Key key = new Key(
params, word, info, KeyboardIconsSet.ICON_UNDEFINED, index, null, x, y, params, word, info, KeyboardIconsSet.ICON_UNDEFINED, index, null, x, y,
@ -211,7 +221,7 @@ public final class MoreSuggestions extends Keyboard {
params.onAddKey(divider); params.onAddKey(divider);
} }
} }
return new MoreSuggestions(params); return new MoreSuggestions(params, mSuggestedWords);
} }
} }

View file

@ -18,15 +18,21 @@ package com.android.inputmethod.latin.suggestions;
import android.content.Context; import android.content.Context;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.MoreKeysKeyboardView; import com.android.inputmethod.keyboard.MoreKeysKeyboardView;
import com.android.inputmethod.latin.R; 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 * A view that renders a virtual {@link MoreSuggestions}. It handles rendering of keys and detecting
* key presses and touch movements. * key presses and touch movements.
*/ */
public final class MoreSuggestionsView extends MoreKeysKeyboardView { public final class MoreSuggestionsView extends MoreKeysKeyboardView {
private static final String TAG = MoreSuggestionsView.class.getSimpleName();
public MoreSuggestionsView(final Context context, final AttributeSet attrs) { public MoreSuggestionsView(final Context context, final AttributeSet attrs) {
this(context, attrs, R.attr.moreSuggestionsViewStyle); this(context, attrs, R.attr.moreSuggestionsViewStyle);
} }
@ -54,9 +60,24 @@ public final class MoreSuggestionsView extends MoreKeysKeyboardView {
@Override @Override
public void onCodeInput(final int code, final int x, final int y) { public void onCodeInput(final int code, final int x, final int y) {
final int index = code - MoreSuggestions.SUGGESTION_CODE_BASE; final Keyboard keyboard = getKeyboard();
if (index >= 0 && index < SuggestionStripView.MAX_SUGGESTIONS) { if (!(keyboard instanceof MoreSuggestions)) {
mListener.onCustomRequest(index); 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));
} }
} }

View file

@ -50,7 +50,6 @@ import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.KeyboardActionListener;
import com.android.inputmethod.keyboard.KeyboardSwitcher; import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.MainKeyboardView; import com.android.inputmethod.keyboard.MainKeyboardView;
import com.android.inputmethod.keyboard.MoreKeysPanel; 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.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.Utils; import com.android.inputmethod.latin.Utils;
import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.define.ProductionFlag;
import com.android.inputmethod.latin.suggestions.MoreSuggestions.MoreSuggestionsListener;
import com.android.inputmethod.research.ResearchLogger; import com.android.inputmethod.research.ResearchLogger;
import java.util.ArrayList; import java.util.ArrayList;
@ -93,7 +93,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
private final ArrayList<View> mDividers = CollectionUtils.newArrayList(); private final ArrayList<View> mDividers = CollectionUtils.newArrayList();
Listener mListener; Listener mListener;
SuggestedWords mSuggestedWords = SuggestedWords.EMPTY; private SuggestedWords mSuggestedWords = SuggestedWords.EMPTY;
private final SuggestionStripViewParams mParams; private final SuggestionStripViewParams mParams;
private static final float MIN_TEXT_XSCALE = 0.70f; private static final float MIN_TEXT_XSCALE = 0.70f;
@ -652,15 +652,11 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
dismissMoreSuggestions(); dismissMoreSuggestions();
} }
private final KeyboardActionListener mMoreSuggestionsListener = private final MoreSuggestionsListener mMoreSuggestionsListener = new MoreSuggestionsListener() {
new KeyboardActionListener.Adapter() {
@Override @Override
public boolean onCustomRequest(final int requestCode) { public void onSuggestionSelected(final int index, final SuggestedWordInfo wordInfo) {
final int index = requestCode;
final SuggestedWordInfo wordInfo = mSuggestedWords.getInfo(index);
mListener.pickSuggestionManually(index, wordInfo); mListener.pickSuggestionManually(index, wordInfo);
dismissMoreSuggestions(); dismissMoreSuggestions();
return true;
} }
@Override @Override