aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin')
-rw-r--r--java/src/com/android/inputmethod/latin/LatinIME.java13
-rw-r--r--java/src/com/android/inputmethod/latin/MoreSuggestions.java13
-rw-r--r--java/src/com/android/inputmethod/latin/MoreSuggestionsView.java2
-rw-r--r--java/src/com/android/inputmethod/latin/SuggestionsView.java47
-rw-r--r--java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java107
5 files changed, 124 insertions, 58 deletions
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index cea59fe0a..191ae5811 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -157,6 +157,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
private Settings.Values mSettingsValues;
+ private View mKeyPreviewBackingView;
private View mSuggestionsContainer;
private int mSuggestionsStripHeight;
private SuggestionsView mSuggestionsView;
@@ -607,6 +608,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
@Override
public void setInputView(View view) {
super.setInputView(view);
+ mKeyPreviewBackingView = view.findViewById(R.id.key_preview_backing);
mSuggestionsContainer = view.findViewById(R.id.suggestions_container);
mSuggestionsView = (SuggestionsView) view.findViewById(R.id.suggestions_view);
if (mSuggestionsView != null)
@@ -945,12 +947,13 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
&& (needsInputViewShown ? mKeyboardSwitcher.isInputViewShown() : true);
if (isFullscreenMode()) {
// No need to have extra space to show the key preview.
- mSuggestionsContainer.setMinimumHeight(0);
+ mKeyPreviewBackingView.setVisibility(View.GONE);
mSuggestionsContainer.setVisibility(
shouldShowSuggestions ? View.VISIBLE : View.GONE);
} else {
// We must control the visibility of the suggestion strip in order to avoid clipped
// key previews, even when we don't show the suggestion strip.
+ mKeyPreviewBackingView.setVisibility(View.VISIBLE);
mSuggestionsContainer.setVisibility(
shouldShowSuggestions ? View.VISIBLE : View.INVISIBLE);
}
@@ -967,15 +970,17 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
final KeyboardView inputView = mKeyboardSwitcher.getKeyboardView();
if (inputView == null || mSuggestionsContainer == null)
return;
- final int containerHeight = mSuggestionsContainer.getHeight();
- int touchY = containerHeight;
+ final int backingHeight = (mKeyPreviewBackingView.getVisibility() == View.GONE) ? 0
+ : mKeyPreviewBackingView.getHeight();
+ final int extraHeight = mSuggestionsContainer.getHeight() + backingHeight;
+ int touchY = extraHeight;
// Need to set touchable region only if input view is being shown
if (mKeyboardSwitcher.isInputViewShown()) {
if (mSuggestionsContainer.getVisibility() == View.VISIBLE) {
touchY -= mSuggestionsStripHeight;
}
final int touchWidth = inputView.getWidth();
- final int touchHeight = inputView.getHeight() + containerHeight
+ final int touchHeight = inputView.getHeight() + extraHeight
// Extend touchable region below the keyboard.
+ EXTENDED_TOUCHABLE_REGION_HEIGHT;
if (DEBUG) {
diff --git a/java/src/com/android/inputmethod/latin/MoreSuggestions.java b/java/src/com/android/inputmethod/latin/MoreSuggestions.java
index a9e75b7b3..10d5b5c48 100644
--- a/java/src/com/android/inputmethod/latin/MoreSuggestions.java
+++ b/java/src/com/android/inputmethod/latin/MoreSuggestions.java
@@ -50,8 +50,8 @@ public class MoreSuggestions extends Keyboard {
private static final int MAX_COLUMNS_IN_ROW = 3;
private int mNumRows;
- public int layout(SuggestedWords suggestions, int fromPos, int maxWidth, int maxHeight,
- KeyboardView view) {
+ public int layout(SuggestedWords suggestions, int fromPos, int maxWidth, int minWidth,
+ int maxRow, KeyboardView view) {
clearKeys();
final Paint paint = new Paint();
paint.setAntiAlias(true);
@@ -68,7 +68,7 @@ public class MoreSuggestions extends Keyboard {
final int numColumn = pos - rowStartPos + 1;
if (numColumn > MAX_COLUMNS_IN_ROW
|| !fitInWidth(rowStartPos, pos + 1, maxWidth / numColumn)) {
- if ((row + 1) * mDefaultRowHeight > maxHeight) {
+ if ((row + 1) >= maxRow) {
break;
}
mNumColumnsInRow[row] = pos - rowStartPos;
@@ -81,7 +81,7 @@ public class MoreSuggestions extends Keyboard {
}
mNumColumnsInRow[row] = pos - rowStartPos;
mNumRows = row + 1;
- mWidth = mOccupiedWidth = calcurateMaxRowWidth(fromPos, pos);
+ mWidth = mOccupiedWidth = Math.max(minWidth, calcurateMaxRowWidth(fromPos, pos));
mHeight = mOccupiedHeight = mNumRows * mDefaultRowHeight + mVerticalGap;
return pos - fromPos;
}
@@ -163,13 +163,14 @@ public class MoreSuggestions extends Keyboard {
}
public Builder layout(SuggestedWords suggestions, int fromPos, int maxWidth,
- int maxHeight) {
+ int minWidth, int maxRow) {
final Keyboard keyboard = KeyboardSwitcher.getInstance().getLatinKeyboard();
final int xmlId = R.xml.kbd_suggestions_pane_template;
load(keyboard.mId.cloneWithNewXml(mResources.getResourceEntryName(xmlId), xmlId));
mParams.mVerticalGap = mParams.mTopPadding = keyboard.mVerticalGap / 2;
- final int count = mParams.layout(suggestions, fromPos, maxWidth, maxHeight, mPaneView);
+ final int count = mParams.layout(suggestions, fromPos, maxWidth, minWidth, maxRow,
+ mPaneView);
mFromPos = fromPos;
mToPos = fromPos + count;
mSuggestions = suggestions;
diff --git a/java/src/com/android/inputmethod/latin/MoreSuggestionsView.java b/java/src/com/android/inputmethod/latin/MoreSuggestionsView.java
index 9fd90241b..0f8192bbb 100644
--- a/java/src/com/android/inputmethod/latin/MoreSuggestionsView.java
+++ b/java/src/com/android/inputmethod/latin/MoreSuggestionsView.java
@@ -73,7 +73,7 @@ public class MoreSuggestionsView extends KeyboardView implements MoreKeysPanel {
};
public MoreSuggestionsView(Context context, AttributeSet attrs) {
- this(context, attrs, R.attr.suggestionsPaneViewStyle);
+ this(context, attrs, R.attr.moreSuggestionsViewStyle);
}
public MoreSuggestionsView(Context context, AttributeSet attrs, int defStyle) {
diff --git a/java/src/com/android/inputmethod/latin/SuggestionsView.java b/java/src/com/android/inputmethod/latin/SuggestionsView.java
index 10cd73dd3..e99c6d474 100644
--- a/java/src/com/android/inputmethod/latin/SuggestionsView.java
+++ b/java/src/com/android/inputmethod/latin/SuggestionsView.java
@@ -35,7 +35,6 @@ import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
-import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
@@ -45,10 +44,10 @@ import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
+import android.widget.RelativeLayout;
import android.widget.TextView;
import com.android.inputmethod.compat.FrameLayoutCompatUtils;
-import com.android.inputmethod.compat.LinearLayoutCompatUtils;
import com.android.inputmethod.keyboard.KeyboardActionListener;
import com.android.inputmethod.keyboard.KeyboardView;
import com.android.inputmethod.keyboard.MoreKeysPanel;
@@ -58,7 +57,8 @@ import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import java.util.ArrayList;
import java.util.List;
-public class SuggestionsView extends LinearLayout implements OnClickListener, OnLongClickListener {
+public class SuggestionsView extends RelativeLayout implements OnClickListener,
+ OnLongClickListener {
public interface Listener {
public boolean addWordToDictionary(String word);
public void pickSuggestionManually(int index, CharSequence word);
@@ -69,7 +69,6 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On
private static final boolean DBG = LatinImeLogger.sDBG;
- private final ViewGroup mSuggestionsPlacer;
private final ViewGroup mSuggestionsStrip;
private KeyboardView mKeyboardView;
@@ -146,12 +145,15 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On
private static class SuggestionsViewParams {
private static final int DEFAULT_SUGGESTIONS_COUNT_IN_STRIP = 3;
private static final int DEFAULT_CENTER_SUGGESTION_PERCENTILE = 40;
+ private static final int DEFAULT_MAX_MORE_SUGGESTIONS_ROW = 2;
private static final int PUNCTUATIONS_IN_STRIP = 6;
public final int mPadding;
public final int mDividerWidth;
public final int mSuggestionsStripHeight;
public final int mSuggestionsCountInStrip;
+ public final int mMaxMoreSuggestionsRow;
+ public final float mMinMoreSuggestionsWidth;
private final List<TextView> mWords;
private final List<View> mDividers;
@@ -211,6 +213,11 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On
mCenterSuggestionWeight = a.getInt(
R.styleable.SuggestionsView_centerSuggestionPercentile,
DEFAULT_CENTER_SUGGESTION_PERCENTILE) / 100.0f;
+ mMaxMoreSuggestionsRow = a.getInt(
+ R.styleable.SuggestionsView_maxMoreSuggestionsRow,
+ DEFAULT_MAX_MORE_SUGGESTIONS_ROW);
+ mMinMoreSuggestionsWidth = getRatio(a,
+ R.styleable.SuggestionsView_minMoreSuggestionsWidth);
a.recycle();
mCenterSuggestionIndex = mSuggestionsCountInStrip / 2;
@@ -225,6 +232,11 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On
mHintToSaveText = context.getText(R.string.hint_add_to_dictionary);
}
+ // Read fraction value in TypedArray as float.
+ private static float getRatio(TypedArray a, int index) {
+ return a.getFraction(index, 1000, 1000, 1) / 1000.0f;
+ }
+
private CharSequence getStyledSuggestionWord(SuggestedWords suggestions, int pos) {
final CharSequence word = suggestions.getWord(pos);
final boolean isAutoCorrect = pos == 1 && willAutoCorrect(suggestions);
@@ -451,18 +463,7 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On
}
public SuggestionsView(Context context, AttributeSet attrs, int defStyle) {
- // Note: Up to version 10 (Gingerbread) of the API, LinearLayout doesn't have 3-argument
- // constructor.
- // TODO: Call 3-argument constructor, super(context, attrs, defStyle), when we abandon
- // backward compatibility with the version 10 or earlier of the API.
- super(context, attrs);
- if (defStyle != R.attr.suggestionsViewStyle) {
- throw new IllegalArgumentException(
- "can't accept defStyle other than R.attr.suggestionsViewStyle: defStyle="
- + defStyle);
- }
- setBackgroundDrawable(LinearLayoutCompatUtils.getBackgroundDrawable(
- context, attrs, defStyle, R.style.SuggestionsViewStyle));
+ super(context, attrs, defStyle);
final LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.suggestions_strip, this);
@@ -474,7 +475,6 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On
mPreviewPopup.setContentView(mPreviewText);
mPreviewPopup.setBackgroundDrawable(null);
- mSuggestionsPlacer = (ViewGroup)findViewById(R.id.suggestions_placer);
mSuggestionsStrip = (ViewGroup)findViewById(R.id.suggestions_strip);
for (int pos = 0; pos < MAX_SUGGESTIONS; pos++) {
final TextView word = (TextView)inflater.inflate(R.layout.suggestion_word, null);
@@ -527,7 +527,7 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On
if (mSuggestions.size() == 0)
return;
- mParams.layout(mSuggestions, mSuggestionsStrip, mSuggestionsPlacer, getWidth());
+ mParams.layout(mSuggestions, mSuggestionsStrip, this, getWidth());
}
private static CharSequence getDebugInfo(SuggestedWords suggestions, int pos) {
@@ -648,9 +648,9 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On
public void clear() {
mShowingAutoCorrectionInverted = false;
- mSuggestionsPlacer.removeAllViews();
- mSuggestionsPlacer.addView(mSuggestionsStrip);
mSuggestionsStrip.removeAllViews();
+ removeAllViews();
+ addView(mSuggestionsStrip);
dismissMoreSuggestions();
}
@@ -730,11 +730,10 @@ public class SuggestionsView extends LinearLayout implements OnClickListener, On
final View container = mMoreSuggestionsContainer;
final int maxWidth = stripWidth - container.getPaddingLeft()
- container.getPaddingRight();
- final DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
- // TODO: Revise how we determine the height
- final int maxHeight = dm.heightPixels - mKeyboardView.getHeight() - getHeight() * 3;
final MoreSuggestions.Builder builder = mMoreSuggestionsBuilder;
- builder.layout(mSuggestions, params.mSuggestionsCountInStrip, maxWidth, maxHeight);
+ builder.layout(mSuggestions, params.mSuggestionsCountInStrip, maxWidth,
+ (int)(maxWidth * params.mMinMoreSuggestionsWidth),
+ params.mMaxMoreSuggestionsRow);
mMoreSuggestionsView.setKeyboard(builder.build());
container.measure(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java
index a6a5b6dd6..85c142f1e 100644
--- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java
+++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java
@@ -34,6 +34,7 @@ import com.android.inputmethod.latin.Dictionary.WordCallback;
import com.android.inputmethod.latin.DictionaryCollection;
import com.android.inputmethod.latin.DictionaryFactory;
import com.android.inputmethod.latin.LocaleUtils;
+import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.SynchronouslyLoadedUserDictionary;
import com.android.inputmethod.latin.UserDictionary;
import com.android.inputmethod.latin.Utils;
@@ -62,18 +63,38 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
private Map<String, Dictionary> mUserDictionaries =
Collections.synchronizedMap(new TreeMap<String, Dictionary>());
+ private double mTypoThreshold;
+
+ @Override public void onCreate() {
+ super.onCreate();
+ mTypoThreshold = Double.parseDouble(getString(R.string.spellchecker_typo_threshold_value));
+ }
+
@Override
public Session createSession() {
- return new AndroidSpellCheckerSession();
+ return new AndroidSpellCheckerSession(this);
}
private static class SuggestionsGatherer implements WordCallback {
+ public static class Result {
+ public final String[] mSuggestions;
+ public final boolean mLooksLikeTypo;
+ public Result(final String[] gatheredSuggestions, final boolean looksLikeTypo) {
+ mSuggestions = gatheredSuggestions;
+ mLooksLikeTypo = looksLikeTypo;
+ }
+ }
+
private final int DEFAULT_SUGGESTION_LENGTH = 16;
private final ArrayList<CharSequence> mSuggestions;
private final int[] mScores;
private final int mMaxLength;
private int mLength = 0;
- private boolean mSeenSuggestions = false;
+
+ // The two following attributes are only ever filled if the requested max length
+ // is 0 (or less, which is treated the same).
+ private String mBestSuggestion = null;
+ private int mBestScore = Integer.MIN_VALUE; // As small as possible
SuggestionsGatherer(final int maxLength) {
mMaxLength = maxLength;
@@ -89,14 +110,26 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
// if it doesn't. See documentation for binarySearch.
final int insertIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1;
- mSeenSuggestions = true;
if (mLength < mMaxLength) {
final int copyLen = mLength - insertIndex;
++mLength;
System.arraycopy(mScores, insertIndex, mScores, insertIndex + 1, copyLen);
mSuggestions.add(insertIndex, new String(word, wordOffset, wordLength));
} else {
- if (insertIndex == 0) return true;
+ if (insertIndex == 0) {
+ // If the maxLength is 0 (should never be less, but if it is, it's treated as 0)
+ // then we need to keep track of the best suggestion in mBestScore and
+ // mBestSuggestion. This is so that we know whether the best suggestion makes
+ // the score cutoff, since we need to know that to return a meaningful
+ // looksLikeTypo.
+ if (0 >= mMaxLength) {
+ if (score > mBestScore) {
+ mBestScore = score;
+ mBestSuggestion = new String(word, wordOffset, wordLength);
+ }
+ }
+ return true;
+ }
System.arraycopy(mScores, 1, mScores, 0, insertIndex);
mSuggestions.add(insertIndex, new String(word, wordOffset, wordLength));
mSuggestions.remove(0);
@@ -106,20 +139,41 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
return true;
}
- public String[] getGatheredSuggestions() {
- if (!mSeenSuggestions) return null;
- if (0 == mLength) return EMPTY_STRING_ARRAY;
-
- if (DBG) {
- if (mLength != mSuggestions.size()) {
- Log.e(TAG, "Suggestion size is not the same as stored mLength");
+ public Result getResults(final CharSequence originalText, final double threshold) {
+ final String[] gatheredSuggestions;
+ final boolean looksLikeTypo;
+ if (0 == mLength) {
+ // Either we found no suggestions, or we found some BUT the max length was 0.
+ // If we found some mBestSuggestion will not be null. If it is null, then
+ // we found none, regardless of the max length.
+ if (null == mBestSuggestion) {
+ gatheredSuggestions = null;
+ looksLikeTypo = false;
+ } else {
+ gatheredSuggestions = EMPTY_STRING_ARRAY;
+ final double normalizedScore =
+ Utils.calcNormalizedScore(originalText, mBestSuggestion, mBestScore);
+ looksLikeTypo = (normalizedScore > threshold);
+ }
+ } else {
+ if (DBG) {
+ if (mLength != mSuggestions.size()) {
+ Log.e(TAG, "Suggestion size is not the same as stored mLength");
+ }
}
+ Collections.reverse(mSuggestions);
+ Utils.removeDupes(mSuggestions);
+ // This returns a String[], while toArray() returns an Object[] which cannot be cast
+ // into a String[].
+ gatheredSuggestions = mSuggestions.toArray(EMPTY_STRING_ARRAY);
+
+ final int bestScore = mScores[0];
+ final CharSequence bestSuggestion = mSuggestions.get(0);
+ final double normalizedScore =
+ Utils.calcNormalizedScore(originalText, bestSuggestion, bestScore);
+ looksLikeTypo = (normalizedScore > threshold);
}
- Collections.reverse(mSuggestions);
- Utils.removeDupes(mSuggestions);
- // This returns a String[], while toArray() returns an Object[] which cannot be cast
- // into a String[].
- return mSuggestions.toArray(EMPTY_STRING_ARRAY);
+ return new Result(gatheredSuggestions, looksLikeTypo);
}
}
@@ -164,16 +218,22 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
return new DictAndProximity(dictionaryCollection, proximityInfo);
}
- private class AndroidSpellCheckerSession extends Session {
+ private static class AndroidSpellCheckerSession extends Session {
// Immutable, but need the locale which is not available in the constructor yet
- DictionaryPool mDictionaryPool;
+ private DictionaryPool mDictionaryPool;
// Likewise
- Locale mLocale;
+ private Locale mLocale;
+
+ private final AndroidSpellCheckerService mService;
+
+ AndroidSpellCheckerSession(final AndroidSpellCheckerService service) {
+ mService = service;
+ }
@Override
public void onCreate() {
final String localeString = getLocale();
- mDictionaryPool = getDictionaryPool(localeString);
+ mDictionaryPool = mService.getDictionaryPool(localeString);
mLocale = LocaleUtils.constructLocaleFromString(localeString);
}
@@ -242,13 +302,14 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
return EMPTY_SUGGESTIONS_INFO;
}
- final String[] suggestions = suggestionsGatherer.getGatheredSuggestions();
+ final SuggestionsGatherer.Result result =
+ suggestionsGatherer.getResults(text, mService.mTypoThreshold);
final int flags =
(isInDict ? SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY : 0)
- | (null != suggestions
+ | (result.mLooksLikeTypo
? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0);
- return new SuggestionsInfo(flags, suggestions);
+ return new SuggestionsInfo(flags, result.mSuggestions);
}
}
}