* commit '6191026ce4e4cc756f47135223f58318d35d3143': Introduce MoreSuggestionsListener class
This commit is contained in:
commit
1e08d3d259
3 changed files with 50 additions and 23 deletions
|
@ -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<MoreSuggestionsParam> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<View> 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
|
||||
|
|
Loading…
Reference in a new issue