aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/suggestions
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin/suggestions')
-rw-r--r--java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java8
-rw-r--r--java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java2
-rw-r--r--java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java126
-rw-r--r--java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java37
4 files changed, 96 insertions, 77 deletions
diff --git a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java
index 322ae5b0f..acd47450b 100644
--- a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java
+++ b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestions.java
@@ -24,14 +24,13 @@ 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;
+import com.android.inputmethod.latin.utils.TypefaceUtils;
public final class MoreSuggestions extends Keyboard {
public static final int SUGGESTION_CODE_BASE = 1024;
@@ -207,11 +206,12 @@ public final class MoreSuggestions extends Keyboard {
final int y = params.getY(index);
final int width = params.getWidth(index);
final String word = mSuggestedWords.getWord(index);
- final String info = Utils.getDebugInfo(mSuggestedWords, index);
+ final String info = mSuggestedWords.getDebugString(index);
final int indexInMoreSuggestions = index + SUGGESTION_CODE_BASE;
final Key key = new Key(
params, word, info, KeyboardIconsSet.ICON_UNDEFINED, indexInMoreSuggestions,
- null, x, y, width, params.mDefaultRowHeight, 0);
+ null /* outputText */, x, y, width, params.mDefaultRowHeight,
+ 0 /* labelFlags */, Key.BACKGROUND_TYPE_NORMAL);
params.markAsEdgeKey(key, index);
params.onAddKey(key);
final int columnNumber = params.getColumnNumber(index);
diff --git a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java
index d585b5c7f..0ebe37782 100644
--- a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java
+++ b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java
@@ -34,7 +34,7 @@ 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);
+ this(context, attrs, R.attr.moreKeysKeyboardViewStyle);
}
public MoreSuggestionsView(final Context context, final AttributeSet attrs,
diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java
index e4c5a06a2..8d2689a7d 100644
--- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java
+++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripLayoutHelper.java
@@ -45,13 +45,12 @@ import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
-import com.android.inputmethod.keyboard.ViewLayoutUtils;
-import com.android.inputmethod.latin.AutoCorrection;
import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.R;
-import com.android.inputmethod.latin.ResourceUtils;
import com.android.inputmethod.latin.SuggestedWords;
-import com.android.inputmethod.latin.Utils;
+import com.android.inputmethod.latin.utils.AutoCorrectionUtils;
+import com.android.inputmethod.latin.utils.ResourceUtils;
+import com.android.inputmethod.latin.utils.ViewLayoutUtils;
import java.util.ArrayList;
@@ -86,6 +85,7 @@ final class SuggestionStripLayoutHelper {
private final float mAlphaObsoleted;
private final float mCenterSuggestionWeight;
private final int mCenterPositionInStrip;
+ private final int mTypedWordPositionWhenAutocorrect;
private final Drawable mMoreSuggestionsHint;
private static final String MORE_SUGGESTIONS_HINT = "\u2026";
private static final String LEFTWARDS_ARROW = "\u2190";
@@ -122,27 +122,15 @@ final class SuggestionStripLayoutHelper {
mSuggestionsStripHeight = res.getDimensionPixelSize(R.dimen.suggestions_strip_height);
final TypedArray a = context.obtainStyledAttributes(attrs,
- R.styleable.SuggestionStripView, defStyle, R.style.SuggestionStripViewStyle);
+ R.styleable.SuggestionStripView, defStyle, R.style.SuggestionStripView);
mSuggestionStripOption = a.getInt(
R.styleable.SuggestionStripView_suggestionStripOption, 0);
- final float alphaValidTypedWord = ResourceUtils.getFraction(a,
- R.styleable.SuggestionStripView_alphaValidTypedWord, 1.0f);
- final float alphaTypedWord = ResourceUtils.getFraction(a,
- R.styleable.SuggestionStripView_alphaTypedWord, 1.0f);
- final float alphaAutoCorrect = ResourceUtils.getFraction(a,
- R.styleable.SuggestionStripView_alphaAutoCorrect, 1.0f);
- final float alphaSuggested = ResourceUtils.getFraction(a,
- R.styleable.SuggestionStripView_alphaSuggested, 1.0f);
mAlphaObsoleted = ResourceUtils.getFraction(a,
- R.styleable.SuggestionStripView_alphaSuggested, 1.0f);
- mColorValidTypedWord = applyAlpha(a.getColor(
- R.styleable.SuggestionStripView_colorValidTypedWord, 0), alphaValidTypedWord);
- mColorTypedWord = applyAlpha(a.getColor(
- R.styleable.SuggestionStripView_colorTypedWord, 0), alphaTypedWord);
- mColorAutoCorrect = applyAlpha(a.getColor(
- R.styleable.SuggestionStripView_colorAutoCorrect, 0), alphaAutoCorrect);
- mColorSuggested = applyAlpha(a.getColor(
- R.styleable.SuggestionStripView_colorSuggested, 0), alphaSuggested);
+ R.styleable.SuggestionStripView_alphaObsoleted, 1.0f);
+ mColorValidTypedWord = a.getColor(R.styleable.SuggestionStripView_colorValidTypedWord, 0);
+ mColorTypedWord = a.getColor(R.styleable.SuggestionStripView_colorTypedWord, 0);
+ mColorAutoCorrect = a.getColor(R.styleable.SuggestionStripView_colorAutoCorrect, 0);
+ mColorSuggested = a.getColor(R.styleable.SuggestionStripView_colorSuggested, 0);
mSuggestionsCountInStrip = a.getInt(
R.styleable.SuggestionStripView_suggestionsCountInStrip,
DEFAULT_SUGGESTIONS_COUNT_IN_STRIP);
@@ -159,6 +147,10 @@ final class SuggestionStripLayoutHelper {
mMoreSuggestionsHint = getMoreSuggestionsHint(res,
res.getDimension(R.dimen.more_suggestions_hint_text_size), mColorAutoCorrect);
mCenterPositionInStrip = mSuggestionsCountInStrip / 2;
+ // Assuming there are at least three suggestions. Also, note that the suggestions are
+ // laid out according to script direction, so this is left of the center for LTR scripts
+ // and right of the center for RTL scripts.
+ mTypedWordPositionWhenAutocorrect = mCenterPositionInStrip - 1;
mMoreSuggestionsBottomGap = res.getDimensionPixelOffset(
R.dimen.more_suggestions_bottom_gap);
mMoreSuggestionsRowHeight = res.getDimensionPixelSize(R.dimen.more_suggestions_row_height);
@@ -233,24 +225,31 @@ final class SuggestionStripLayoutHelper {
return spannedWord;
}
- private int getIndexInSuggestedWords(final int positionInStrip,
+ private int getPositionInSuggestionStrip(final int indexInSuggestedWords,
final SuggestedWords suggestedWords) {
- // TODO: This works for 3 suggestions. Revisit this algorithm when there are 5 or more
- // suggestions.
- final int mostImportantIndexInSuggestedWords = suggestedWords.willAutoCorrect()
- ? SuggestedWords.INDEX_OF_AUTO_CORRECTION : SuggestedWords.INDEX_OF_TYPED_WORD;
- if (positionInStrip == mCenterPositionInStrip) {
- return mostImportantIndexInSuggestedWords;
+ final int indexToDisplayMostImportantSuggestion;
+ final int indexToDisplaySecondMostImportantSuggestion;
+ if (suggestedWords.willAutoCorrect()) {
+ indexToDisplayMostImportantSuggestion = SuggestedWords.INDEX_OF_AUTO_CORRECTION;
+ indexToDisplaySecondMostImportantSuggestion = SuggestedWords.INDEX_OF_TYPED_WORD;
+ } else {
+ indexToDisplayMostImportantSuggestion = SuggestedWords.INDEX_OF_TYPED_WORD;
+ indexToDisplaySecondMostImportantSuggestion = SuggestedWords.INDEX_OF_AUTO_CORRECTION;
}
- if (positionInStrip == mostImportantIndexInSuggestedWords) {
+ if (indexInSuggestedWords == indexToDisplayMostImportantSuggestion) {
return mCenterPositionInStrip;
}
- return positionInStrip;
+ if (indexInSuggestedWords == indexToDisplaySecondMostImportantSuggestion) {
+ return mTypedWordPositionWhenAutocorrect;
+ }
+ // If neither of those, the order in the suggestion strip is the same as in SuggestedWords.
+ return indexInSuggestedWords;
}
- private int getSuggestionTextColor(final int positionInStrip,
+ private int getSuggestionTextColor(final int indexInSuggestedWords,
final SuggestedWords suggestedWords) {
- final int indexInSuggestedWords = getIndexInSuggestedWords(positionInStrip, suggestedWords);
+ final int positionInStrip =
+ getPositionInSuggestionStrip(indexInSuggestedWords, suggestedWords);
// TODO: Need to revisit this logic with bigram suggestions
final boolean isSuggested = (indexInSuggestedWords != SuggestedWords.INDEX_OF_TYPED_WORD);
@@ -268,7 +267,7 @@ final class SuggestionStripLayoutHelper {
// If we auto-correct, then the autocorrection is in slot 0 and the typed word
// is in slot 1.
if (positionInStrip == mCenterPositionInStrip
- && AutoCorrection.shouldBlockAutoCorrectionBySafetyNet(
+ && AutoCorrectionUtils.shouldBlockAutoCorrectionBySafetyNet(
suggestedWords.getWord(SuggestedWords.INDEX_OF_AUTO_CORRECTION),
suggestedWords.getWord(SuggestedWords.INDEX_OF_TYPED_WORD))) {
return 0xFFFF0000;
@@ -302,6 +301,23 @@ final class SuggestionStripLayoutHelper {
final int countInStrip = mSuggestionsCountInStrip;
setupWordViewsTextAndColor(suggestedWords, countInStrip);
+ final TextView centerWordView = mWordViews.get(mCenterPositionInStrip);
+ final int stripWidth = placerView.getWidth();
+ final int centerWidth = getSuggestionWidth(mCenterPositionInStrip, stripWidth);
+ if (getTextScaleX(centerWordView.getText(), centerWidth, centerWordView.getPaint())
+ < MIN_TEXT_XSCALE) {
+ // Layout only the most relevant suggested word at the center of the suggestion strip
+ // by consolidating all slots in the strip.
+ mMoreSuggestionsAvailable = (suggestedWords.size() > 1);
+ layoutWord(mCenterPositionInStrip, stripWidth);
+ stripView.addView(centerWordView);
+ setLayoutWeight(centerWordView, 1.0f, ViewGroup.LayoutParams.MATCH_PARENT);
+ if (SuggestionStripView.DBG) {
+ layoutDebugInfo(mCenterPositionInStrip, placerView, stripWidth);
+ }
+ return;
+ }
+
mMoreSuggestionsAvailable = (suggestedWords.size() > countInStrip);
int x = 0;
for (int positionInStrip = 0; positionInStrip < countInStrip; positionInStrip++) {
@@ -312,7 +328,7 @@ final class SuggestionStripLayoutHelper {
x += divider.getMeasuredWidth();
}
- final int width = getSuggestionWidth(positionInStrip, placerView.getWidth());
+ final int width = getSuggestionWidth(positionInStrip, stripWidth);
final TextView wordView = layoutWord(positionInStrip, width);
stripView.addView(wordView);
setLayoutWeight(wordView, getSuggestionWeight(positionInStrip),
@@ -335,7 +351,7 @@ final class SuggestionStripLayoutHelper {
* increase towards the right for LTR scripts and the left for RTL scripts, starting with 0.
* The position of the most important suggestion is in {@link #mCenterPositionInStrip}. This
* usually doesn't match the index in <code>suggedtedWords</code> -- see
- * {@link #getIndexInSuggestedWords(int,SuggestedWords)}.
+ * {@link #getPositionInSuggestionStrip(int,SuggestedWords)}.
*
* @param positionInStrip the position in the suggestion strip.
* @param width the maximum width for layout in pixels.
@@ -396,10 +412,19 @@ final class SuggestionStripLayoutHelper {
private void setupWordViewsTextAndColor(final SuggestedWords suggestedWords,
final int countInStrip) {
+ // Clear all suggestions first
+ for (int positionInStrip = 0; positionInStrip < countInStrip; ++positionInStrip) {
+ mWordViews.get(positionInStrip).setText(null);
+ // Make this inactive for touches in {@link #layoutWord(int,int)}.
+ if (SuggestionStripView.DBG) {
+ mDebugInfoViews.get(positionInStrip).setText(null);
+ }
+ }
final int count = Math.min(suggestedWords.size(), countInStrip);
- for (int positionInStrip = 0; positionInStrip < count; positionInStrip++) {
- final int indexInSuggestedWords =
- getIndexInSuggestedWords(positionInStrip, suggestedWords);
+ for (int indexInSuggestedWords = 0; indexInSuggestedWords < count;
+ indexInSuggestedWords++) {
+ final int positionInStrip =
+ getPositionInSuggestionStrip(indexInSuggestedWords, suggestedWords);
final TextView wordView = mWordViews.get(positionInStrip);
// {@link TextView#getTag()} is used to get the index in suggestedWords at
// {@link SuggestionStripView#onClick(View)}.
@@ -408,14 +433,7 @@ final class SuggestionStripLayoutHelper {
wordView.setTextColor(getSuggestionTextColor(positionInStrip, suggestedWords));
if (SuggestionStripView.DBG) {
mDebugInfoViews.get(positionInStrip).setText(
- Utils.getDebugInfo(suggestedWords, indexInSuggestedWords));
- }
- }
- for (int positionInStrip = count; positionInStrip < countInStrip; positionInStrip++) {
- mWordViews.get(positionInStrip).setText(null);
- // Make this inactive for touches in {@link #layoutWord(int,int)}.
- if (SuggestionStripView.DBG) {
- mDebugInfoViews.get(positionInStrip).setText(null);
+ suggestedWords.getDebugString(indexInSuggestedWords));
}
}
}
@@ -432,8 +450,10 @@ final class SuggestionStripLayoutHelper {
final TextView wordView = mWordViews.get(positionInStrip);
wordView.setEnabled(true);
wordView.setTextColor(mColorAutoCorrect);
- final String punctuation = suggestedWords.getWord(positionInStrip);
- wordView.setText(punctuation);
+ // {@link TextView#getTag()} is used to get the index in suggestedWords at
+ // {@link SuggestionStripView#onClick(View)}.
+ wordView.setTag(positionInStrip);
+ wordView.setText(suggestedWords.getWord(positionInStrip));
wordView.setTextScaleX(1.0f);
wordView.setCompoundDrawables(null, null, null, null);
stripView.addView(wordView);
@@ -451,6 +471,8 @@ final class SuggestionStripLayoutHelper {
final int wordWidth = (int)(width * mCenterSuggestionWeight);
final CharSequence text = getEllipsizedText(word, wordWidth, wordView.getPaint());
final float wordScaleX = wordView.getTextScaleX();
+ // {@link TextView#setTag()} is used to hold the word to be added to dictionary. The word
+ // will be extracted at {@link #getAddToDictionaryWord()}.
wordView.setTag(word);
wordView.setText(text);
wordView.setTextScaleX(wordScaleX);
@@ -480,8 +502,10 @@ final class SuggestionStripLayoutHelper {
hintView.setOnClickListener(listener);
}
- public CharSequence getAddToDictionaryWord() {
- return (CharSequence)mWordToSaveView.getTag();
+ public String getAddToDictionaryWord() {
+ // String tag is set at
+ // {@link #layoutAddToDictionaryHint(String,ViewGroup,int,CharSequence,OnClickListener}.
+ return (String)mWordToSaveView.getTag();
}
public boolean isAddToDictionaryShowing(final View v) {
diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
index b2b9427af..75f17c559 100644
--- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
+++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
@@ -34,7 +34,6 @@ import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.MainKeyboardView;
import com.android.inputmethod.keyboard.MoreKeysPanel;
import com.android.inputmethod.latin.AudioAndHapticFeedbackManager;
-import com.android.inputmethod.latin.CollectionUtils;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.R;
@@ -42,6 +41,7 @@ import com.android.inputmethod.latin.SuggestedWords;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.define.ProductionFlag;
import com.android.inputmethod.latin.suggestions.MoreSuggestions.MoreSuggestionsListener;
+import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.research.ResearchLogger;
import java.util.ArrayList;
@@ -162,27 +162,27 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
mSuggestionsStrip.removeAllViews();
removeAllViews();
addView(mSuggestionsStrip);
- dismissMoreSuggestions();
+ mMoreSuggestionsView.dismissMoreKeysPanel();
}
private final MoreSuggestionsListener mMoreSuggestionsListener = new MoreSuggestionsListener() {
@Override
public void onSuggestionSelected(final int index, final SuggestedWordInfo wordInfo) {
mListener.pickSuggestionManually(index, wordInfo);
- dismissMoreSuggestions();
+ mMoreSuggestionsView.dismissMoreKeysPanel();
}
@Override
public void onCancelInput() {
- dismissMoreSuggestions();
+ mMoreSuggestionsView.dismissMoreKeysPanel();
}
};
private final MoreKeysPanel.Controller mMoreSuggestionsController =
new MoreKeysPanel.Controller() {
@Override
- public boolean onDismissMoreKeysPanel() {
- return mMainKeyboardView.onDismissMoreKeysPanel();
+ public void onDismissMoreKeysPanel(final MoreKeysPanel panel) {
+ mMainKeyboardView.onDismissMoreKeysPanel(panel);
}
@Override
@@ -191,18 +191,14 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
}
@Override
- public void onCancelMoreKeysPanel() {
- dismissMoreSuggestions();
+ public void onCancelMoreKeysPanel(final MoreKeysPanel panel) {
+ mMoreSuggestionsView.dismissMoreKeysPanel();
}
};
- boolean dismissMoreSuggestions() {
- return mMoreSuggestionsView.dismissMoreKeysPanel();
- }
-
@Override
public boolean onLongClick(final View view) {
- AudioAndHapticFeedbackManager.getInstance().hapticAndAudioFeedback(
+ AudioAndHapticFeedbackManager.getInstance().performHapticAndAudioFeedback(
Constants.NOT_A_CODE, this);
return showMoreSuggestions();
}
@@ -274,15 +270,10 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
return super.dispatchTouchEvent(me);
}
- final MoreKeysPanel moreKeysPanel = mMoreSuggestionsView;
final int action = me.getAction();
- final long eventTime = me.getEventTime();
final int index = me.getActionIndex();
- final int id = me.getPointerId(index);
final int x = (int)me.getX(index);
final int y = (int)me.getY(index);
- final int translatedX = moreKeysPanel.translateX(x);
- final int translatedY = moreKeysPanel.translateY(y);
if (mMoreSuggestionsMode == MORE_SUGGESTIONS_CHECKING_MODAL_OR_SLIDING) {
if (Math.abs(x - mOriginX) >= mMoreSuggestionsModalTolerance
@@ -299,19 +290,23 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
}
// MORE_SUGGESTIONS_IN_SLIDING_MODE
- mMoreSuggestionsView.processMotionEvent(action, translatedX, translatedY, id, eventTime);
+ me.setLocation(mMoreSuggestionsView.translateX(x), mMoreSuggestionsView.translateY(y));
+ mMoreSuggestionsView.onTouchEvent(me);
return true;
}
@Override
public void onClick(final View view) {
if (mLayoutHelper.isAddToDictionaryShowing(view)) {
- mListener.addWordToUserDictionary(mLayoutHelper.getAddToDictionaryWord().toString());
+ mListener.addWordToUserDictionary(mLayoutHelper.getAddToDictionaryWord());
clear();
return;
}
final Object tag = view.getTag();
+ // Integer tag is set at
+ // {@link SuggestionStripLayoutHelper#setupWordViewsTextAndColor(SuggestedWords,int)} and
+ // {@link SuggestionStripLayoutHelper#layoutPunctuationSuggestions(SuggestedWords,ViewGroup}
if (!(tag instanceof Integer)) {
return;
}
@@ -327,6 +322,6 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
- dismissMoreSuggestions();
+ mMoreSuggestionsView.dismissMoreKeysPanel();
}
}