diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin')
9 files changed, 121 insertions, 80 deletions
diff --git a/java/src/com/android/inputmethod/latin/Constants.java b/java/src/com/android/inputmethod/latin/Constants.java index b4e115c7d..43af66eb7 100644 --- a/java/src/com/android/inputmethod/latin/Constants.java +++ b/java/src/com/android/inputmethod/latin/Constants.java @@ -220,6 +220,7 @@ public final class Constants { public static final String REGEXP_PERIOD = "\\."; public static final String STRING_SPACE = " "; + public static final String STRING_PERIOD_AND_SPACE = ". "; /** * Special keys code. Must be negative. diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index a8f9efb05..6e5e0deaa 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -1001,6 +1001,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen if (!mSettings.getCurrent().isApplicationSpecifiedCompletionsOn()) { return; } + // If we have an update request in flight, we need to cancel it so it does not override + // these completions. + mHandler.cancelUpdateSuggestionStrip(); if (applicationSpecifiedCompletions == null) { setNeutralSuggestionStrip(); return; diff --git a/java/src/com/android/inputmethod/latin/PrevWordsInfo.java b/java/src/com/android/inputmethod/latin/PrevWordsInfo.java index 5dda44445..f45c73f53 100644 --- a/java/src/com/android/inputmethod/latin/PrevWordsInfo.java +++ b/java/src/com/android/inputmethod/latin/PrevWordsInfo.java @@ -27,7 +27,8 @@ import com.android.inputmethod.latin.utils.StringUtils; public class PrevWordsInfo { public static final PrevWordsInfo EMPTY_PREV_WORDS_INFO = new PrevWordsInfo(WordInfo.EMPTY_WORD_INFO); - public static final PrevWordsInfo BEGINNING_OF_SENTENCE = new PrevWordsInfo(); + public static final PrevWordsInfo BEGINNING_OF_SENTENCE = + new PrevWordsInfo(WordInfo.BEGINNING_OF_SENTENCE); /** * Word information used to represent previous words information. @@ -57,6 +58,24 @@ public class PrevWordsInfo { public boolean isValid() { return mWord != null; } + + @Override + public int hashCode() { + return Arrays.hashCode(new Object[] { mWord, mIsBeginningOfSentence } ); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof WordInfo)) return false; + final WordInfo wordInfo = (WordInfo)o; + if (mWord == null || wordInfo.mWord == null) { + return mWord == wordInfo.mWord + && mIsBeginningOfSentence == wordInfo.mIsBeginningOfSentence; + } + return mWord.equals(wordInfo.mWord) + && mIsBeginningOfSentence == wordInfo.mIsBeginningOfSentence; + } } // The words immediately before the considered word. EMPTY_WORD_INFO element means we don't @@ -67,16 +86,9 @@ public class PrevWordsInfo { // calling getSuggetions* in this situation. public WordInfo[] mPrevWordsInfo = new WordInfo[Constants.MAX_PREV_WORD_COUNT_FOR_N_GRAM]; - // Beginning of sentence. - public PrevWordsInfo() { - mPrevWordsInfo[0] = WordInfo.BEGINNING_OF_SENTENCE; - Arrays.fill(mPrevWordsInfo, 1 /* start */, mPrevWordsInfo.length, WordInfo.EMPTY_WORD_INFO); - } - // Construct from the previous word information. public PrevWordsInfo(final WordInfo prevWordInfo) { mPrevWordsInfo[0] = prevWordInfo; - Arrays.fill(mPrevWordsInfo, 1 /* start */, mPrevWordsInfo.length, WordInfo.EMPTY_WORD_INFO); } // Construct from WordInfo array. n-th element represents (n+1)-th previous word's information. @@ -116,6 +128,19 @@ public class PrevWordsInfo { } @Override + public int hashCode() { + return Arrays.hashCode(mPrevWordsInfo); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof PrevWordsInfo)) return false; + final PrevWordsInfo prevWordsInfo = (PrevWordsInfo)o; + return Arrays.equals(mPrevWordsInfo, prevWordsInfo.mPrevWordsInfo); + } + + @Override public String toString() { final StringBuffer builder = new StringBuffer(); for (int i = 0; i < mPrevWordsInfo.length; i++) { @@ -123,7 +148,7 @@ public class PrevWordsInfo { builder.append("PrevWord["); builder.append(i); builder.append("]: "); - if (!wordInfo.isValid()) { + if (wordInfo == null || !wordInfo.isValid()) { builder.append("Empty. "); continue; } diff --git a/java/src/com/android/inputmethod/latin/RichInputConnection.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java index 3be6bccc6..62b55bca1 100644 --- a/java/src/com/android/inputmethod/latin/RichInputConnection.java +++ b/java/src/com/android/inputmethod/latin/RichInputConnection.java @@ -26,6 +26,7 @@ import android.view.inputmethod.ExtractedText; import android.view.inputmethod.ExtractedTextRequest; import android.view.inputmethod.InputConnection; +import com.android.inputmethod.latin.PrevWordsInfo.WordInfo; import com.android.inputmethod.latin.settings.SpacingAndPunctuations; import com.android.inputmethod.latin.utils.CapsModeUtils; import com.android.inputmethod.latin.utils.DebugLogUtils; @@ -49,8 +50,10 @@ public final class RichInputConnection { private static final boolean DBG = false; private static final boolean DEBUG_PREVIOUS_TEXT = false; private static final boolean DEBUG_BATCH_NESTING = false; - // Provision for a long word pair and a separator - private static final int LOOKBACK_CHARACTER_NUM = Constants.DICTIONARY_MAX_WORD_LENGTH * 2 + 1; + // Provision for long words and separators between the words. + private static final int LOOKBACK_CHARACTER_NUM = Constants.DICTIONARY_MAX_WORD_LENGTH + * (Constants.MAX_PREV_WORD_COUNT_FOR_N_GRAM + 1) /* words */ + + Constants.MAX_PREV_WORD_COUNT_FOR_N_GRAM /* separators */; private static final Pattern spaceRegex = Pattern.compile("\\s+"); private static final int INVALID_CURSOR_POSITION = -1; @@ -544,22 +547,25 @@ public final class RichInputConnection { return Arrays.binarySearch(sortedSeparators, code) >= 0; } - // Get information of the nth word before cursor. n = 1 retrieves the word immediately before - // the cursor, n = 2 retrieves the word before that, and so on. This splits on whitespace only. + // Get context information from nth word before the cursor. n = 1 retrieves the words + // immediately before the cursor, n = 2 retrieves the words before that, and so on. This splits + // on whitespace only. // Also, it won't return words that end in a separator (if the nth word before the cursor // ends in a separator, it returns information representing beginning-of-sentence). - // Example : - // (n = 1) "abc def|" -> def - // (n = 1) "abc def |" -> def - // (n = 1) "abc 'def|" -> 'def + // Example (when Constants.MAX_PREV_WORD_COUNT_FOR_N_GRAM is 2): + // (n = 1) "abc def|" -> abc, def + // (n = 1) "abc def |" -> abc, def + // (n = 1) "abc 'def|" -> empty, 'def // (n = 1) "abc def. |" -> beginning-of-sentence // (n = 1) "abc def . |" -> beginning-of-sentence - // (n = 2) "abc def|" -> abc - // (n = 2) "abc def |" -> abc + // (n = 2) "abc def|" -> beginning-of-sentence, abc + // (n = 2) "abc def |" -> beginning-of-sentence, abc // (n = 2) "abc 'def|" -> empty. The context is different from "abc def", but we cannot // represent this situation using PrevWordsInfo. See TODO in the method. - // (n = 2) "abc def. |" -> abc - // (n = 2) "abc def . |" -> def + // TODO: The next example's result should be "abc, def". This have to be fixed before we + // retrieve the prior context of Beginning-of-Sentence. + // (n = 2) "abc def. |" -> beginning-of-sentence, abc + // (n = 2) "abc def . |" -> abc, def // (n = 2) "abc|" -> beginning-of-sentence // (n = 2) "abc |" -> beginning-of-sentence // (n = 2) "abc. def|" -> beginning-of-sentence @@ -567,43 +573,50 @@ public final class RichInputConnection { final SpacingAndPunctuations spacingAndPunctuations, final int n) { if (prev == null) return PrevWordsInfo.EMPTY_PREV_WORDS_INFO; final String[] w = spaceRegex.split(prev); - - // Referring to the word after the nth word. - if ((n - 1) > 0 && (n - 1) <= w.length) { - final String wordFollowingTheNthPrevWord = w[w.length - n + 1]; - if (!wordFollowingTheNthPrevWord.isEmpty()) { - final char firstChar = wordFollowingTheNthPrevWord.charAt(0); - if (spacingAndPunctuations.isWordConnector(firstChar)) { - // The word following the n-th prev word is starting with a word connector. - // TODO: Return meaningful context for this case. - return PrevWordsInfo.EMPTY_PREV_WORDS_INFO; + final WordInfo[] prevWordsInfo = new WordInfo[Constants.MAX_PREV_WORD_COUNT_FOR_N_GRAM]; + for (int i = 0; i < prevWordsInfo.length; i++) { + final int focusedWordIndex = w.length - n - i; + // Referring to the word after the focused word. + if ((focusedWordIndex + 1) >= 0 && (focusedWordIndex + 1) < w.length) { + final String wordFollowingTheNthPrevWord = w[focusedWordIndex + 1]; + if (!wordFollowingTheNthPrevWord.isEmpty()) { + final char firstChar = wordFollowingTheNthPrevWord.charAt(0); + if (spacingAndPunctuations.isWordConnector(firstChar)) { + // The word following the focused word is starting with a word connector. + // TODO: Return meaningful context for this case. + prevWordsInfo[i] = WordInfo.EMPTY_WORD_INFO; + break; + } } } + // If we can't find (n + i) words, the context is beginning-of-sentence. + if (focusedWordIndex < 0) { + prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE; + break; + } + final String focusedWord = w[focusedWordIndex]; + // If the word is empty, the context is beginning-of-sentence. + final int length = focusedWord.length(); + if (length <= 0) { + prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE; + break; + } + // If ends in a sentence separator, the context is beginning-of-sentence. + final char lastChar = focusedWord.charAt(length - 1); + if (spacingAndPunctuations.isSentenceSeparator(lastChar)) { + prevWordsInfo[i] = WordInfo.BEGINNING_OF_SENTENCE; + break; + } + // If ends in a word separator or connector, the context is unclear. + // TODO: Return meaningful context for this case. + if (spacingAndPunctuations.isWordSeparator(lastChar) + || spacingAndPunctuations.isWordConnector(lastChar)) { + prevWordsInfo[i] = WordInfo.EMPTY_WORD_INFO; + break; + } + prevWordsInfo[i] = new WordInfo(focusedWord); } - - // If we can't find n words, or we found an empty word, the context is - // beginning-of-sentence. - if (w.length < n) { - return PrevWordsInfo.BEGINNING_OF_SENTENCE; - } - final String nthPrevWord = w[w.length - n]; - final int length = nthPrevWord.length(); - if (length <= 0) { - return PrevWordsInfo.BEGINNING_OF_SENTENCE; - } - - // If ends in a sentence separator, the context is beginning-of-sentence. - final char lastChar = nthPrevWord.charAt(length - 1); - if (spacingAndPunctuations.isSentenceSeparator(lastChar)) { - return PrevWordsInfo.BEGINNING_OF_SENTENCE; - } - // If ends in a word separator or connector, the context is unclear. - // TODO: Return meaningful context for this case. - if (spacingAndPunctuations.isWordSeparator(lastChar) - || spacingAndPunctuations.isWordConnector(lastChar)) { - return PrevWordsInfo.EMPTY_PREV_WORDS_INFO; - } - return new PrevWordsInfo(new PrevWordsInfo.WordInfo(nthPrevWord)); + return new PrevWordsInfo(prevWordsInfo); } /** @@ -741,13 +754,12 @@ public final class RichInputConnection { // Here we test whether we indeed have a period and a space before us. This should not // be needed, but it's there just in case something went wrong. final CharSequence textBeforeCursor = getTextBeforeCursor(2, 0); - final String periodSpace = ". "; - if (!TextUtils.equals(periodSpace, textBeforeCursor)) { + if (!TextUtils.equals(Constants.STRING_PERIOD_AND_SPACE, textBeforeCursor)) { // Theoretically we should not be coming here if there isn't ". " before the // cursor, but the application may be changing the text while we are typing, so // anything goes. We should not crash. Log.d(TAG, "Tried to revert double-space combo but we didn't find " - + "\"" + periodSpace + "\" just before the cursor."); + + "\"" + Constants.STRING_PERIOD_AND_SPACE + "\" just before the cursor."); return false; } // Double-space results in ". ". A backspace to cancel this should result in a single diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 692c03a4c..e43db352d 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -126,16 +126,17 @@ public final class Suggest { final boolean didRemoveTypedWord = SuggestedWordInfo.removeDups(typedWord, suggestionsContainer); + final SuggestedWordInfo firstSuggestedWordInfo; final String whitelistedWord; if (suggestionsContainer.isEmpty()) { + firstSuggestedWordInfo = null; whitelistedWord = null; } else { - final SuggestedWordInfo firstSuggestedWordInfo = suggestionsContainer.get(0); - final String firstSuggestion = firstSuggestedWordInfo.mWord; + firstSuggestedWordInfo = suggestionsContainer.get(0); if (!firstSuggestedWordInfo.isKindOf(SuggestedWordInfo.KIND_WHITELIST)) { whitelistedWord = null; } else { - whitelistedWord = firstSuggestion; + whitelistedWord = firstSuggestedWordInfo.mWord; } } @@ -151,10 +152,10 @@ public final class Suggest { // the current settings. It may also be useful to know, when the setting is off, whether // the word *would* have been auto-corrected. if (!isCorrectionEnabled || !allowsToBeAutoCorrected || isPrediction - || suggestionResults.isEmpty() || wordComposer.hasDigits() + || null == firstSuggestedWordInfo || wordComposer.hasDigits() || wordComposer.isMostlyCaps() || wordComposer.isResumed() || !mDictionaryFacilitator.hasInitializedMainDictionary() - || suggestionResults.first().isKindOf(SuggestedWordInfo.KIND_SHORTCUT)) { + || firstSuggestedWordInfo.isKindOf(SuggestedWordInfo.KIND_SHORTCUT)) { // If we don't have a main dictionary, we never want to auto-correct. The reason for // this is, the user may have a contact whose name happens to match a valid word in // their language, and it will unexpectedly auto-correct. For example, if the user @@ -166,7 +167,7 @@ public final class Suggest { hasAutoCorrection = false; } else { hasAutoCorrection = AutoCorrectionUtils.suggestionExceedsAutoCorrectionThreshold( - suggestionResults.first(), consideredWord, mAutoCorrectionThreshold); + firstSuggestedWordInfo, consideredWord, mAutoCorrectionThreshold); } if (!TextUtils.isEmpty(typedWord)) { diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java index ec57cd71f..15ddcf9ae 100644 --- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java +++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java @@ -1882,10 +1882,11 @@ public final class InputLogic { final CharSequence chosenWordWithSuggestions = SuggestionSpanUtils.getTextWithSuggestionSpan(mLatinIME, chosenWord, suggestedWords); - // Use the 2nd previous word as the previous word because the 1st previous word is the word - // to be committed. + // When we are composing word, get previous words information from the 2nd previous word + // because the 1st previous word is the word to be committed. Otherwise get previous words + // information from the 1st previous word. final PrevWordsInfo prevWordsInfo = mConnection.getPrevWordsInfoFromNthPreviousWord( - settingsValues.mSpacingAndPunctuations, 2); + settingsValues.mSpacingAndPunctuations, mWordComposer.isComposingWord() ? 2 : 1); mConnection.commitText(chosenWordWithSuggestions, 1); // Add the word to the user history dictionary performAdditionToUserHistoryDictionary(settingsValues, chosenWord, prevWordsInfo); diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java index db15c1cc6..be33f339d 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java @@ -258,7 +258,9 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session { return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO | SuggestionsInfo.RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS, new String[] { - TextUtils.join(Constants.STRING_SPACE, splitText) }); + TextUtils.join(Constants.STRING_SPACE, splitText), + TextUtils.join(Constants.STRING_PERIOD_AND_SPACE, + splitText) }); } } return dictInfo.mDictionary.isValidWord(inText) diff --git a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java index 528d500d2..47921ca0a 100644 --- a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java +++ b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java @@ -20,8 +20,6 @@ import android.content.Context; import android.util.AttributeSet; import android.util.Log; -import com.android.inputmethod.accessibility.AccessibilityUtils; -import com.android.inputmethod.accessibility.MoreSuggestionsAccessibilityDelegate; import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.KeyboardActionListener; @@ -51,6 +49,7 @@ public final class MoreSuggestionsView extends MoreKeysKeyboardView { super(context, attrs, defStyle); } + // TODO: Remove redundant override method. @Override public void setKeyboard(final Keyboard keyboard) { super.setKeyboard(keyboard); @@ -58,16 +57,10 @@ public final class MoreSuggestionsView extends MoreKeysKeyboardView { // above {@link MoreKeysKeyboardView#setKeyboard(Keyboard)} call. // With accessibility mode on, {@link #mAccessibilityDelegate} is set to a // {@link MoreKeysKeyboardAccessibilityDelegate} object at the above - // {@link MoreKeysKeyboardView#setKeyboard(Keyboard)} call. And the object has to be - // overwritten by a {@link MoreSuggestionsAccessibilityDelegate} object here. - if (AccessibilityUtils.getInstance().isAccessibilityEnabled()) { - if (!(mAccessibilityDelegate instanceof MoreSuggestionsAccessibilityDelegate)) { - mAccessibilityDelegate = new MoreSuggestionsAccessibilityDelegate( - this, mKeyDetector); - mAccessibilityDelegate.setOpenAnnounce(R.string.spoken_open_more_suggestions); - mAccessibilityDelegate.setCloseAnnounce(R.string.spoken_close_more_suggestions); - } - mAccessibilityDelegate.setKeyboard(keyboard); + // {@link MoreKeysKeyboardView#setKeyboard(Keyboard)} call. + if (mAccessibilityDelegate != null) { + mAccessibilityDelegate.setOpenAnnounce(R.string.spoken_open_more_suggestions); + mAccessibilityDelegate.setCloseAnnounce(R.string.spoken_close_more_suggestions); } } diff --git a/java/src/com/android/inputmethod/latin/utils/ScriptUtils.java b/java/src/com/android/inputmethod/latin/utils/ScriptUtils.java index 4dfb38d80..00355c307 100644 --- a/java/src/com/android/inputmethod/latin/utils/ScriptUtils.java +++ b/java/src/com/android/inputmethod/latin/utils/ScriptUtils.java @@ -23,9 +23,12 @@ import java.util.TreeMap; * A class to help with handling different writing scripts. */ public class ScriptUtils { + // TODO: should we use ISO 15924 identifiers instead? public static final int SCRIPT_LATIN = 0; public static final int SCRIPT_CYRILLIC = 1; public static final int SCRIPT_GREEK = 2; + public static final int SCRIPT_ARABIC = 3; + public static final int SCRIPT_HEBREW = 4; public static final TreeMap<String, Integer> mLanguageToScript; static { // List of the supported languages and their associated script. We won't check |