aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/LatinIME.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin/LatinIME.java')
-rw-r--r--java/src/com/android/inputmethod/latin/LatinIME.java157
1 files changed, 112 insertions, 45 deletions
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 7bd09811c..094ccd77f 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -44,7 +44,9 @@ import android.os.Message;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.text.InputType;
+import android.text.SpannableString;
import android.text.TextUtils;
+import android.text.style.SuggestionSpan;
import android.util.Log;
import android.util.PrintWriterPrinter;
import android.util.Printer;
@@ -72,6 +74,8 @@ import com.android.inputmethod.keyboard.KeyboardActionListener;
import com.android.inputmethod.keyboard.KeyboardId;
import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.MainKeyboardView;
+import com.android.inputmethod.latin.RichInputConnection.Range;
+import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.Utils.Stats;
import com.android.inputmethod.latin.define.ProductionFlag;
import com.android.inputmethod.latin.suggestions.SuggestionStripView;
@@ -196,6 +200,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
private static final int MSG_PENDING_IMS_CALLBACK = 1;
private static final int MSG_UPDATE_SUGGESTION_STRIP = 2;
private static final int MSG_SHOW_GESTURE_PREVIEW_AND_SUGGESTION_STRIP = 3;
+ private static final int MSG_RESUME_SUGGESTIONS = 4;
private static final int ARG1_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT = 1;
@@ -233,6 +238,9 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
latinIme.showGesturePreviewAndSuggestionStrip((SuggestedWords)msg.obj,
msg.arg1 == ARG1_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT);
break;
+ case MSG_RESUME_SUGGESTIONS:
+ latinIme.restartSuggestionsOnWordTouchedByCursor();
+ break;
}
}
@@ -240,6 +248,10 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
sendMessageDelayed(obtainMessage(MSG_UPDATE_SUGGESTION_STRIP), mDelayUpdateSuggestions);
}
+ public void postResumeSuggestions() {
+ sendMessageDelayed(obtainMessage(MSG_RESUME_SUGGESTIONS), mDelayUpdateSuggestions);
+ }
+
public void cancelUpdateSuggestionStrip() {
removeMessages(MSG_UPDATE_SUGGESTION_STRIP);
}
@@ -803,10 +815,6 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
@Override
public void onWindowHidden() {
- if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
- ResearchLogger.latinIME_onWindowHidden(mLastSelectionStart, mLastSelectionEnd,
- getCurrentInputConnection());
- }
super.onWindowHidden();
final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView();
if (mainKeyboardView != null) {
@@ -834,8 +842,10 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
// Remove pending messages related to update suggestions
mHandler.cancelUpdateSuggestionStrip();
resetComposingState(true /* alsoResetLastComposedWord */);
+ // Notify ResearchLogger
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
- ResearchLogger.getInstance().latinIME_onFinishInputViewInternal();
+ ResearchLogger.latinIME_onFinishInputViewInternal(finishingInput, mLastSelectionStart,
+ mLastSelectionEnd, getCurrentInputConnection());
}
}
@@ -911,13 +921,12 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
resetEntireInputState(newSelStart);
}
+ // We moved the cursor. If we are touching a word, we need to resume suggestion.
+ mHandler.postResumeSuggestions();
+
mKeyboardSwitcher.updateShiftState();
}
mExpectingUpdateSelection = false;
- // TODO: Decide to call restartSuggestionsOnWordBeforeCursorIfAtEndOfWord() or not
- // here. It would probably be too expensive to call directly here but we may want to post a
- // message to delay it. The point would be to unify behavior between backspace to the
- // end of a word and manually put the pointer at the end of the word.
// Make a note of the cursor position
mLastSelectionStart = newSelStart;
@@ -984,7 +993,8 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
}
}
if (!mSettings.getCurrent().isApplicationSpecifiedCompletionsOn()) return;
- mApplicationSpecifiedCompletions = applicationSpecifiedCompletions;
+ mApplicationSpecifiedCompletions =
+ CompletionInfoUtils.removeNulls(applicationSpecifiedCompletions);
if (applicationSpecifiedCompletions == null) {
clearSuggestionStrip();
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
@@ -1145,11 +1155,11 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
if (!mWordComposer.isComposingWord()) return;
final String typedWord = mWordComposer.getTypedWord();
if (typedWord.length() > 0) {
- commitChosenWord(typedWord, LastComposedWord.COMMIT_TYPE_USER_TYPED_WORD,
- separatorString);
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
ResearchLogger.getInstance().onWordFinished(typedWord, mWordComposer.isBatchMode());
}
+ commitChosenWord(typedWord, LastComposedWord.COMMIT_TYPE_USER_TYPED_WORD,
+ separatorString);
}
}
@@ -1244,10 +1254,6 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
} else {
wordToEdit = word;
}
- mPositionalInfoForUserDictPendingAddition =
- new PositionalInfoForUserDictPendingAddition(
- wordToEdit, mLastSelectionEnd, getCurrentInputEditorInfo(),
- mLastComposedWord.mCapitalizedMode);
mUserDictionary.addWordToUserDictionary(wordToEdit);
}
@@ -1541,7 +1547,8 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
}
} else {
final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor();
- if (mSettings.getCurrent().isUsuallyFollowedBySpace(codePointBeforeCursor)) {
+ if (Character.isLetter(codePointBeforeCursor)
+ || mSettings.getCurrent().isUsuallyFollowedBySpace(codePointBeforeCursor)) {
mSpaceState = SPACE_STATE_PHANTOM;
}
}
@@ -1552,7 +1559,8 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
private static final class BatchInputUpdater implements Handler.Callback {
private final Handler mHandler;
private LatinIME mLatinIme;
- private boolean mInBatchInput; // synchronized using "this".
+ private final Object mLock = new Object();
+ private boolean mInBatchInput; // synchronized using {@link #mLock}.
private BatchInputUpdater() {
final HandlerThread handlerThread = new HandlerThread(
@@ -1583,21 +1591,25 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
}
// Run in the UI thread.
- public synchronized void onStartBatchInput(final LatinIME latinIme) {
- mHandler.removeMessages(MSG_UPDATE_GESTURE_PREVIEW_AND_SUGGESTION_STRIP);
- mLatinIme = latinIme;
- mInBatchInput = true;
+ public void onStartBatchInput(final LatinIME latinIme) {
+ synchronized (mLock) {
+ mHandler.removeMessages(MSG_UPDATE_GESTURE_PREVIEW_AND_SUGGESTION_STRIP);
+ mLatinIme = latinIme;
+ mInBatchInput = true;
+ }
}
// Run in the Handler thread.
- private synchronized void updateBatchInput(final InputPointers batchPointers) {
- if (!mInBatchInput) {
- // Batch input has ended or canceled while the message was being delivered.
- return;
+ private void updateBatchInput(final InputPointers batchPointers) {
+ synchronized (mLock) {
+ if (!mInBatchInput) {
+ // Batch input has ended or canceled while the message was being delivered.
+ return;
+ }
+ final SuggestedWords suggestedWords = getSuggestedWordsGestureLocked(batchPointers);
+ mLatinIme.mHandler.showGesturePreviewAndSuggestionStrip(
+ suggestedWords, false /* dismissGestureFloatingPreviewText */);
}
- final SuggestedWords suggestedWords = getSuggestedWordsGestureLocked(batchPointers);
- mLatinIme.mHandler.showGesturePreviewAndSuggestionStrip(
- suggestedWords, false /* dismissGestureFloatingPreviewText */);
}
// Run in the UI thread.
@@ -1610,19 +1622,23 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
.sendToTarget();
}
- public synchronized void onCancelBatchInput() {
- mInBatchInput = false;
- mLatinIme.mHandler.showGesturePreviewAndSuggestionStrip(
- SuggestedWords.EMPTY, true /* dismissGestureFloatingPreviewText */);
+ public void onCancelBatchInput() {
+ synchronized (mLock) {
+ mInBatchInput = false;
+ mLatinIme.mHandler.showGesturePreviewAndSuggestionStrip(
+ SuggestedWords.EMPTY, true /* dismissGestureFloatingPreviewText */);
+ }
}
// Run in the UI thread.
- public synchronized SuggestedWords onEndBatchInput(final InputPointers batchPointers) {
- mInBatchInput = false;
- final SuggestedWords suggestedWords = getSuggestedWordsGestureLocked(batchPointers);
- mLatinIme.mHandler.showGesturePreviewAndSuggestionStrip(
- suggestedWords, true /* dismissGestureFloatingPreviewText */);
- return suggestedWords;
+ public SuggestedWords onEndBatchInput(final InputPointers batchPointers) {
+ synchronized (mLock) {
+ mInBatchInput = false;
+ final SuggestedWords suggestedWords = getSuggestedWordsGestureLocked(batchPointers);
+ mLatinIme.mHandler.showGesturePreviewAndSuggestionStrip(
+ suggestedWords, true /* dismissGestureFloatingPreviewText */);
+ return suggestedWords;
+ }
}
// {@link LatinIME#getSuggestedWords(int)} method calls with same session id have to
@@ -1718,6 +1734,9 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
// during key repeat.
mHandler.postUpdateShiftState();
+ if (mWordComposer.isComposingWord() && !mWordComposer.isCursorAtEndOfComposingWord()) {
+ resetEntireInputState(mLastSelectionStart);
+ }
if (mWordComposer.isComposingWord()) {
final int length = mWordComposer.size();
if (length > 0) {
@@ -1849,6 +1868,10 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
promotePhantomSpace();
}
+ if (mWordComposer.isComposingWord() && !mWordComposer.isCursorAtEndOfComposingWord()) {
+ resetEntireInputState(mLastSelectionStart);
+ isComposingWord = false;
+ }
// NOTE: isCursorTouchingWord() is a blocking IPC call, so it often takes several
// dozen milliseconds. Avoid calling it as much as possible, since we are on the UI
// thread here.
@@ -1907,7 +1930,6 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
private boolean handleSeparator(final int primaryCode, final int x, final int y,
final int spaceState) {
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
- ResearchLogger.recordTimeForLogUnitSplit();
ResearchLogger.latinIME_handleSeparator(primaryCode, mWordComposer.isComposingWord());
}
boolean didAutoCorrect = false;
@@ -2176,8 +2198,9 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
// Called from {@link SuggestionStripView} through the {@link SuggestionStripView#Listener}
// interface
@Override
- public void pickSuggestionManually(final int index, final String suggestion) {
+ public void pickSuggestionManually(final int index, final SuggestedWordInfo suggestionInfo) {
final SuggestedWords suggestedWords = mSuggestedWords;
+ final String suggestion = suggestionInfo.mWord;
// If this is a punctuation picked from the suggestion strip, pass it to onCodeInput
if (suggestion.length() == 1 && isShowingPunctuationList()) {
// Word separators are suggested before the user inputs something.
@@ -2243,7 +2266,8 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
// AND it's in none of our current dictionaries (main, user or otherwise).
// Please note that if mSuggest is null, it means that everything is off: suggestion
// and correction, so we shouldn't try to show the hint
- final boolean showingAddToDictionaryHint = index == 0 && mSuggest != null
+ final boolean showingAddToDictionaryHint =
+ SuggestedWordInfo.KIND_TYPED == suggestionInfo.mKind && mSuggest != null
// If the suggestion is not in the dictionary, the hint should be shown.
&& !AutoCorrection.isValidWord(mSuggest.getUnigramDictionaries(), suggestion, true);
@@ -2321,6 +2345,48 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
}
/**
+ * Check if the cursor is touching a word. If so, restart suggestions on this word, else
+ * do nothing.
+ */
+ private void restartSuggestionsOnWordTouchedByCursor() {
+ // If the cursor is not touching a word, or if there is a selection, return right away.
+ if (mLastSelectionStart != mLastSelectionEnd) return;
+ if (!mConnection.isCursorTouchingWord(mSettings.getCurrent())) return;
+ final Range range = mConnection.getWordRangeAtCursor(mSettings.getWordSeparators(),
+ 0 /* additionalPrecedingWordsCount */);
+ final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList();
+ if (range.mWord instanceof SpannableString) {
+ final SpannableString spannableString = (SpannableString)range.mWord;
+ final String typedWord = spannableString.toString();
+ int i = 0;
+ for (Object object : spannableString.getSpans(0, spannableString.length(),
+ SuggestionSpan.class)) {
+ SuggestionSpan span = (SuggestionSpan)object;
+ for (String s : span.getSuggestions()) {
+ ++i;
+ if (!TextUtils.equals(s, typedWord)) {
+ suggestions.add(new SuggestedWordInfo(s,
+ SuggestionStripView.MAX_SUGGESTIONS - i,
+ SuggestedWordInfo.KIND_RESUMED, Dictionary.TYPE_RESUMED));
+ }
+ }
+ }
+ }
+ mWordComposer.setComposingWord(range.mWord, mKeyboardSwitcher.getKeyboard());
+ mWordComposer.setCursorPositionWithinWord(range.mCharsBefore);
+ mConnection.setComposingRegion(mLastSelectionStart - range.mCharsBefore,
+ mLastSelectionEnd + range.mCharsAfter);
+ if (suggestions.isEmpty()) {
+ suggestions.add(new SuggestedWordInfo(range.mWord.toString(), 1,
+ SuggestedWordInfo.KIND_TYPED, Dictionary.TYPE_RESUMED));
+ }
+ showSuggestionStrip(new SuggestedWords(suggestions,
+ true /* typedWordValid */, false /* willAutoCorrect */,
+ false /* isPunctuationSuggestions */, false /* isObsoleteSuggestions */,
+ false /* isPrediction */), range.mWord.toString());
+ }
+
+ /**
* Check if the cursor is actually at the end of a word. If so, restart suggestions on this
* word, else do nothing.
*/
@@ -2328,17 +2394,18 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
final CharSequence word =
mConnection.getWordBeforeCursorIfAtEndOfWord(mSettings.getCurrent());
if (null != word) {
- restartSuggestionsOnWordBeforeCursor(word);
+ final String wordString = word.toString();
+ restartSuggestionsOnWordBeforeCursor(wordString);
// TODO: Handle the case where the user manually moves the cursor and then backs up over
// a separator. In that case, the current log unit should not be uncommitted.
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
- ResearchLogger.getInstance().uncommitCurrentLogUnit(word.toString(),
+ ResearchLogger.getInstance().uncommitCurrentLogUnit(wordString,
true /* dumpCurrentLogUnit */);
}
}
}
- private void restartSuggestionsOnWordBeforeCursor(final CharSequence word) {
+ private void restartSuggestionsOnWordBeforeCursor(final String word) {
mWordComposer.setComposingWord(word, mKeyboardSwitcher.getKeyboard());
final int length = word.length();
mConnection.deleteSurroundingText(length, 0);