diff options
Diffstat (limited to 'java/src')
8 files changed, 131 insertions, 79 deletions
diff --git a/java/src/com/android/inputmethod/event/InputTransaction.java b/java/src/com/android/inputmethod/event/InputTransaction.java index 2e9014f20..4fe9b403e 100644 --- a/java/src/com/android/inputmethod/event/InputTransaction.java +++ b/java/src/com/android/inputmethod/event/InputTransaction.java @@ -40,6 +40,7 @@ public class InputTransaction { // Outputs private int mRequiredShiftUpdate = SHIFT_NO_UPDATE; + private boolean mRequiresUpdateSuggestions = false; public InputTransaction(final SettingsValues settingsValues, final Event event, final long timestamp, final int spaceState, final int shiftState) { @@ -50,10 +51,34 @@ public class InputTransaction { mShiftState = shiftState; } + /** + * Indicate that this transaction requires some type of shift update. + * @param updateType What type of shift update this requires. + */ public void requireShiftUpdate(final int updateType) { mRequiredShiftUpdate = Math.max(mRequiredShiftUpdate, updateType); } + + /** + * Gets what type of shift update this transaction requires. + * @return The shift update type. + */ public int getRequiredShiftUpdate() { return mRequiredShiftUpdate; } + + /** + * Indicate that this transaction requires updating the suggestions. + */ + public void setRequiresUpdateSuggestions() { + mRequiresUpdateSuggestions = true; + } + + /** + * Find out whether this transaction requires updating the suggestions. + * @return Whether this transaction requires updating the suggestions. + */ + public boolean requiresUpdateSuggestions() { + return mRequiresUpdateSuggestions; + } } diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java index 8246c924b..03425ef1f 100644 --- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java @@ -60,7 +60,7 @@ import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.settings.DebugSettings; import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CoordinateUtils; -import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; +import com.android.inputmethod.latin.utils.SpacebarLanguageUtils; import com.android.inputmethod.latin.utils.TypefaceUtils; import com.android.inputmethod.latin.utils.UsabilityStudyLogUtils; import com.android.inputmethod.research.ResearchLogger; @@ -918,14 +918,13 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack // Layout language name on spacebar. private String layoutLanguageOnSpacebar(final Paint paint, final InputMethodSubtype subtype, final int width) { - // Choose appropriate language name to fit into the width. - final String fullText = SubtypeLocaleUtils.getFullDisplayName(subtype); + final String fullText = SpacebarLanguageUtils.getFullDisplayName(subtype); if (fitsTextIntoWidth(width, fullText, paint)) { return fullText; } - final String middleText = SubtypeLocaleUtils.getMiddleDisplayName(subtype); + final String middleText = SpacebarLanguageUtils.getMiddleDisplayName(subtype); if (fitsTextIntoWidth(width, middleText, paint)) { return middleText; } diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 0c0be82df..53e6232b6 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -38,7 +38,6 @@ import android.net.ConnectivityManager; import android.os.Debug; import android.os.IBinder; import android.os.Message; -import android.os.SystemClock; import android.preference.PreferenceManager; import android.text.InputType; import android.text.TextUtils; @@ -1230,7 +1229,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen final InputTransaction completeInputTransaction = mInputLogic.onCodeInput(mSettings.getCurrent(), event, mKeyboardSwitcher.getKeyboardShiftMode(), mHandler); - updateShiftModeAfterInputTransaction(completeInputTransaction.getRequiredShiftUpdate()); + updateStateAfterInputTransaction(completeInputTransaction); mKeyboardSwitcher.onCodeInput(codePoint); } @@ -1450,7 +1449,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen final InputTransaction completeInputTransaction = mInputLogic.onPickSuggestionManually( mSettings.getCurrent(), index, suggestionInfo, mKeyboardSwitcher.getKeyboardShiftMode(), mHandler); - updateShiftModeAfterInputTransaction(completeInputTransaction.getRequiredShiftUpdate()); + updateStateAfterInputTransaction(completeInputTransaction); } @Override @@ -1488,8 +1487,14 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } } - private void updateShiftModeAfterInputTransaction(final int requiredShiftUpdate) { - switch (requiredShiftUpdate) { + /** + * After an input transaction has been executed, some state must be updated. This includes + * the shift state of the keyboard and suggestions. This method looks at the finished + * inputTransaction to find out what is necessary and updates the state accordingly. + * @param inputTransaction The transaction that has been executed. + */ + private void updateStateAfterInputTransaction(final InputTransaction inputTransaction) { + switch (inputTransaction.getRequiredShiftUpdate()) { case InputTransaction.SHIFT_UPDATE_LATER: mHandler.postUpdateShiftState(); break; @@ -1498,6 +1503,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen break; default: // SHIFT_NO_UPDATE } + if (inputTransaction.requiresUpdateSuggestions()) { + mHandler.postUpdateSuggestionStrip(); + } } private void hapticAndAudioFeedback(final int code, final int repeatCount) { diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java index e2cdbb39c..ec6bd289a 100644 --- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java +++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java @@ -130,8 +130,11 @@ public final class InputLogic { // so we try using some heuristics to find out about these and fix them. mConnection.tryFixLyingCursorPosition(); cancelDoubleSpacePeriodCountdown(); - mInputLogicHandler.destroy(); - mInputLogicHandler = new InputLogicHandler(mLatinIME, this); + if (InputLogicHandler.NULL_HANDLER == mInputLogicHandler) { + mInputLogicHandler = new InputLogicHandler(mLatinIME, this); + } else { + mInputLogicHandler.reset(); + } } /** @@ -142,8 +145,7 @@ public final class InputLogic { mConnection.finishComposingText(); } resetComposingState(true /* alsoResetLastComposedWord */); - mInputLogicHandler.destroy(); - mInputLogicHandler = InputLogicHandler.NULL_HANDLER; + mInputLogicHandler.reset(); } /** @@ -403,7 +405,7 @@ public final class InputLogic { // A special key, like delete, shift, emoji, or the settings key. switch (event.mKeyCode) { case Constants.CODE_DELETE: - handleBackspace(inputTransaction, handler); + handleBackspace(inputTransaction); LatinImeLogger.logOnDelete(event.mX, event.mY); break; case Constants.CODE_SHIFT: @@ -672,7 +674,7 @@ public final class InputLogic { commitTyped(inputTransaction.mSettingsValues, LastComposedWord.NOT_A_SEPARATOR); } } - handleNonSeparator(inputTransaction.mSettingsValues, inputTransaction, handler); + handleNonSeparator(inputTransaction.mSettingsValues, inputTransaction); } return didAutoCorrect; } @@ -683,9 +685,7 @@ public final class InputLogic { * @param inputTransaction The transaction in progress. */ private void handleNonSeparator(final SettingsValues settingsValues, - final InputTransaction inputTransaction, - // TODO: Remove this argument - final LatinIME.UIHandler handler) { + final InputTransaction inputTransaction) { final int codePoint = inputTransaction.mEvent.mCodePoint; // TODO: refactor this method to stop flipping isComposingWord around all the time, and // make it shorter (possibly cut into several pieces). Also factor handleNonSpecialCharacter @@ -761,7 +761,7 @@ public final class InputLogic { // In case the "add to dictionary" hint was still displayed. mSuggestionStripViewAccessor.dismissAddToDictionaryHint(); } - handler.postUpdateSuggestionStrip(); + inputTransaction.setRequiresUpdateSuggestions(); if (settingsValues.mIsInternal) { LatinImeLoggerUtils.onNonSeparator((char)codePoint, inputTransaction.mEvent.mX, inputTransaction.mEvent.mY); @@ -843,7 +843,7 @@ public final class InputLogic { } startDoubleSpacePeriodCountdown(inputTransaction); - handler.postUpdateSuggestionStrip(); + inputTransaction.setRequiresUpdateSuggestions(); } else { if (swapWeakSpace) { swapSwapperAndSpace(inputTransaction); @@ -879,9 +879,7 @@ public final class InputLogic { * Handle a press on the backspace key. * @param inputTransaction The transaction in progress. */ - private void handleBackspace(final InputTransaction inputTransaction, - // TODO: remove this argument - final LatinIME.UIHandler handler) { + private void handleBackspace(final InputTransaction inputTransaction) { mSpaceState = SpaceState.NONE; mDeleteCount++; @@ -910,7 +908,7 @@ public final class InputLogic { mWordComposer.deleteLast(inputTransaction.mEvent); } mConnection.setComposingText(getTextWithUnderline(mWordComposer.getTypedWord()), 1); - handler.postUpdateSuggestionStrip(); + inputTransaction.setRequiresUpdateSuggestions(); if (!mWordComposer.isComposingWord()) { // If we just removed the last character, auto-caps mode may have changed so we // need to re-evaluate. @@ -921,7 +919,7 @@ public final class InputLogic { if (inputTransaction.mSettingsValues.mIsInternal) { LatinImeLoggerUtils.onAutoCorrectionCancellation(); } - revertCommit(inputTransaction.mSettingsValues, handler); + revertCommit(inputTransaction); return; } if (mEnteredText != null && mConnection.sameAsTextBeforeCursor(mEnteredText)) { @@ -1400,11 +1398,9 @@ public final class InputLogic { * * This is triggered upon pressing backspace just after a commit with auto-correction. * - * @param settingsValues the current settings values. + * @param inputTransaction The transaction in progress. */ - private void revertCommit(final SettingsValues settingsValues, - // TODO: remove this argument - final LatinIME.UIHandler handler) { + private void revertCommit(final InputTransaction inputTransaction) { final String previousWord = mLastComposedWord.mPrevWord; final CharSequence originallyTypedWord = mLastComposedWord.mTypedWord; final CharSequence committedWord = mLastComposedWord.mCommittedWord; @@ -1448,7 +1444,8 @@ public final class InputLogic { // Given this, we add it to the list of suggestions, otherwise we discard it. if (span instanceof SuggestionSpan) { final SuggestionSpan suggestionSpan = (SuggestionSpan)span; - if (!suggestionSpan.getLocale().equals(settingsValues.mLocale.toString())) { + if (!suggestionSpan.getLocale().equals( + inputTransaction.mSettingsValues.mLocale.toString())) { continue; } for (final String suggestion : suggestionSpan.getSuggestions()) { @@ -1463,11 +1460,11 @@ public final class InputLogic { } } // Add the suggestion list to the list of suggestions. - textToCommit.setSpan(new SuggestionSpan(settingsValues.mLocale, + textToCommit.setSpan(new SuggestionSpan(inputTransaction.mSettingsValues.mLocale, suggestions.toArray(new String[suggestions.size()]), 0 /* flags */), 0 /* start */, lastCharIndex /* end */, 0 /* flags */); } - if (settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces) { + if (inputTransaction.mSettingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces) { // For languages with spaces, we revert to the typed string, but the cursor is still // after the separator so we don't resume suggestions. If the user wants to correct // the word, they have to press backspace again. @@ -1480,7 +1477,7 @@ public final class InputLogic { mLatinIME.getCoordinatesForCurrentKeyboard(codePoints), previousWord); mConnection.setComposingText(textToCommit, 1); } - if (settingsValues.mIsInternal) { + if (inputTransaction.mSettingsValues.mIsInternal) { LatinImeLoggerUtils.onSeparator(mLastComposedWord.mSeparatorString, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE); } @@ -1493,7 +1490,7 @@ public final class InputLogic { // separator. mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD; // We have a separator between the word and the cursor: we should show predictions. - handler.postUpdateSuggestionStrip(); + inputTransaction.setRequiresUpdateSuggestions(); } /** diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java index db96de305..42f0d7c00 100644 --- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java +++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogicHandler.java @@ -43,7 +43,7 @@ class InputLogicHandler implements Handler.Callback { // is initialized, though probably only the monkey can actually do this. public static final InputLogicHandler NULL_HANDLER = new InputLogicHandler() { @Override - public void destroy() {} + public void reset() {} @Override public boolean handleMessage(final Message msg) { return true; } @Override @@ -75,8 +75,8 @@ class InputLogicHandler implements Handler.Callback { mInputLogic = inputLogic; } - public void destroy() { - mNonUIThreadHandler.getLooper().quit(); + public void reset() { + mNonUIThreadHandler.removeCallbacksAndMessages(null); } /** diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index a07e8eb6a..6a52481b9 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -268,6 +268,7 @@ public final class AndroidSpellCheckerService extends SpellCheckerService // if it doesn't. See documentation for binarySearch. final int insertIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1; + // Weak <- insertIndex == 0, ..., insertIndex == mLength -> Strong if (insertIndex == 0 && mLength >= mMaxLength) { // In the future, we may want to keep track of the best suggestion score even if // we are asked for 0 suggestions. In this case, we can use the following @@ -285,11 +286,6 @@ public final class AndroidSpellCheckerService extends SpellCheckerService // } return true; } - if (insertIndex >= mMaxLength) { - // We found a suggestion, but its score is too weak to be kept considering - // the suggestion limit. - return true; - } final String wordString = new String(word, wordOffset, wordLength); if (mLength < mMaxLength) { @@ -297,12 +293,13 @@ public final class AndroidSpellCheckerService extends SpellCheckerService ++mLength; System.arraycopy(mScores, insertIndex, mScores, insertIndex + 1, copyLen); mSuggestions.add(insertIndex, wordString); + mScores[insertIndex] = score; } else { - System.arraycopy(mScores, 1, mScores, 0, insertIndex); + System.arraycopy(mScores, 1, mScores, 0, insertIndex - 1); mSuggestions.add(insertIndex, wordString); mSuggestions.remove(0); + mScores[insertIndex - 1] = score; } - mScores[insertIndex] = score; return true; } diff --git a/java/src/com/android/inputmethod/latin/utils/SpacebarLanguageUtils.java b/java/src/com/android/inputmethod/latin/utils/SpacebarLanguageUtils.java new file mode 100644 index 000000000..89837c641 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/utils/SpacebarLanguageUtils.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.utils; + +import android.view.inputmethod.InputMethodSubtype; + +import java.util.Locale; + +public final class SpacebarLanguageUtils { + private SpacebarLanguageUtils() { + // Intentional empty constructor for utility class. + } + + // InputMethodSubtype's display name for spacebar text in its locale. + // isAdditionalSubtype (T=true, F=false) + // locale layout | Middle Full + // ------ ------- - --------- ---------------------- + // en_US qwerty F English English (US) exception + // en_GB qwerty F English English (UK) exception + // es_US spanish F Español Español (EE.UU.) exception + // fr azerty F Français Français + // fr_CA qwerty F Français Français (Canada) + // fr_CH swiss F Français Français (Suisse) + // de qwertz F Deutsch Deutsch + // de_CH swiss T Deutsch Deutsch (Schweiz) + // zz qwerty F QWERTY QWERTY + // fr qwertz T Français Français + // de qwerty T Deutsch Deutsch + // en_US azerty T English English (US) + // zz azerty T AZERTY AZERTY + // Get InputMethodSubtype's full display name in its locale. + public static String getFullDisplayName(final InputMethodSubtype subtype) { + if (SubtypeLocaleUtils.isNoLanguage(subtype)) { + return SubtypeLocaleUtils.getKeyboardLayoutSetDisplayName(subtype); + } + return SubtypeLocaleUtils.getSubtypeLocaleDisplayName(subtype.getLocale()); + } + + // Get InputMethodSubtype's middle display name in its locale. + public static String getMiddleDisplayName(final InputMethodSubtype subtype) { + if (SubtypeLocaleUtils.isNoLanguage(subtype)) { + return SubtypeLocaleUtils.getKeyboardLayoutSetDisplayName(subtype); + } + final Locale locale = SubtypeLocaleUtils.getSubtypeLocale(subtype); + return SubtypeLocaleUtils.getSubtypeLocaleDisplayName(locale.getLanguage()); + } +} diff --git a/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java b/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java index 4f556f972..2452864d5 100644 --- a/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java +++ b/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java @@ -292,41 +292,6 @@ public final class SubtypeLocaleUtils { return keyboardLayoutSet; } - // InputMethodSubtype's display name for spacebar text in its locale. - // isAdditionalSubtype (T=true, F=false) - // locale layout | Middle Full - // ------ ------- - --------- ---------------------- - // en_US qwerty F English English (US) exception - // en_GB qwerty F English English (UK) exception - // es_US spanish F Español Español (EE.UU.) exception - // fr azerty F Français Français - // fr_CA qwerty F Français Français (Canada) - // fr_CH swiss F Français Français (Suisse) - // de qwertz F Deutsch Deutsch - // de_CH swiss T Deutsch Deutsch (Schweiz) - // zz qwerty F QWERTY QWERTY - // fr qwertz T Français Français - // de qwerty T Deutsch Deutsch - // en_US azerty T English English (US) - // zz azerty T AZERTY AZERTY - - // Get InputMethodSubtype's full display name in its locale. - public static String getFullDisplayName(final InputMethodSubtype subtype) { - if (isNoLanguage(subtype)) { - return getKeyboardLayoutSetDisplayName(subtype); - } - return getSubtypeLocaleDisplayName(subtype.getLocale()); - } - - // Get InputMethodSubtype's middle display name in its locale. - public static String getMiddleDisplayName(final InputMethodSubtype subtype) { - if (isNoLanguage(subtype)) { - return getKeyboardLayoutSetDisplayName(subtype); - } - final Locale locale = getSubtypeLocale(subtype); - return getSubtypeLocaleDisplayName(locale.getLanguage()); - } - // TODO: Get this information from the framework instead of maintaining here by ourselves. // Sorted list of known Right-To-Left language codes. private static final String[] SORTED_RTL_LANGUAGES = { |