diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/LatinIME.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/LatinIME.java index 9063e693e..73f08af88 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/LatinIME.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/LatinIME.java @@ -36,7 +36,6 @@ import android.os.IBinder; import android.os.Message; import android.preference.PreferenceManager; import android.text.InputType; -import android.text.TextUtils; import android.util.Log; import android.util.PrintWriterPrinter; import android.util.Printer; @@ -246,68 +245,68 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } final KeyboardSwitcher switcher = latinIme.mKeyboardSwitcher; switch (msg.what) { - case MSG_UPDATE_SUGGESTION_STRIP: - cancelUpdateSuggestionStrip(); - latinIme.mInputLogic.performUpdateSuggestionStripSync( - latinIme.mSettings.getCurrent(), msg.arg1 /* inputStyle */); - break; - case MSG_UPDATE_SHIFT_STATE: - switcher.requestUpdatingShiftState(latinIme.getCurrentAutoCapsState(), - latinIme.getCurrentRecapitalizeState()); - break; - case MSG_SHOW_GESTURE_PREVIEW_AND_SUGGESTION_STRIP: - if (msg.arg1 == ARG1_NOT_GESTURE_INPUT) { - final SuggestedWords suggestedWords = (SuggestedWords) msg.obj; - latinIme.showSuggestionStrip(suggestedWords); - } else { - latinIme.showGesturePreviewAndSuggestionStrip((SuggestedWords) msg.obj, - msg.arg1 == ARG1_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT); - } - break; - case MSG_RESUME_SUGGESTIONS: - latinIme.mInputLogic.restartSuggestionsOnWordTouchedByCursor( - latinIme.mSettings.getCurrent(), false /* forStartInput */, - latinIme.mKeyboardSwitcher.getCurrentKeyboardScriptId()); - break; - case MSG_RESUME_SUGGESTIONS_FOR_START_INPUT: - latinIme.mInputLogic.restartSuggestionsOnWordTouchedByCursor( - latinIme.mSettings.getCurrent(), true /* forStartInput */, - latinIme.mKeyboardSwitcher.getCurrentKeyboardScriptId()); - break; - case MSG_REOPEN_DICTIONARIES: - // We need to re-evaluate the currently composing word in case the script has - // changed. - postWaitForDictionaryLoad(); - latinIme.resetDictionaryFacilitatorIfNecessary(); - break; - case MSG_UPDATE_TAIL_BATCH_INPUT_COMPLETED: - final SuggestedWords suggestedWords = (SuggestedWords) msg.obj; - latinIme.mInputLogic.onUpdateTailBatchInputCompleted( - latinIme.mSettings.getCurrent(), - suggestedWords, latinIme.mKeyboardSwitcher); - latinIme.onTailBatchInputResultShown(suggestedWords); - break; - case MSG_RESET_CACHES: - final SettingsValues settingsValues = latinIme.mSettings.getCurrent(); - if (latinIme.mInputLogic.retryResetCachesAndReturnSuccess( - msg.arg1 == ARG1_TRUE /* tryResumeSuggestions */, - msg.arg2 /* remainingTries */, this /* handler */)) { - // If we were able to reset the caches, then we can reload the keyboard. - // Otherwise, we'll do it when we can. - latinIme.mKeyboardSwitcher.loadKeyboard(latinIme.getCurrentInputEditorInfo(), - settingsValues, latinIme.getCurrentAutoCapsState(), + case MSG_UPDATE_SUGGESTION_STRIP: + cancelUpdateSuggestionStrip(); + latinIme.mInputLogic.performUpdateSuggestionStripSync( + latinIme.mSettings.getCurrent(), msg.arg1 /* inputStyle */); + break; + case MSG_UPDATE_SHIFT_STATE: + switcher.requestUpdatingShiftState(latinIme.getCurrentAutoCapsState(), latinIme.getCurrentRecapitalizeState()); - } - break; - case MSG_WAIT_FOR_DICTIONARY_LOAD: - Log.i(TAG, "Timeout waiting for dictionary load"); - break; - case MSG_DEALLOCATE_MEMORY: - latinIme.deallocateMemory(); - break; - case MSG_SWITCH_LANGUAGE_AUTOMATICALLY: - latinIme.switchLanguage((InputMethodSubtype)msg.obj); - break; + break; + case MSG_SHOW_GESTURE_PREVIEW_AND_SUGGESTION_STRIP: + if (msg.arg1 == ARG1_NOT_GESTURE_INPUT) { + final SuggestedWords suggestedWords = (SuggestedWords) msg.obj; + latinIme.showSuggestionStrip(suggestedWords); + } else { + latinIme.showGesturePreviewAndSuggestionStrip((SuggestedWords) msg.obj, + msg.arg1 == ARG1_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT); + } + break; + case MSG_RESUME_SUGGESTIONS: + latinIme.mInputLogic.restartSuggestionsOnWordTouchedByCursor( + latinIme.mSettings.getCurrent(), false /* forStartInput */, + latinIme.mKeyboardSwitcher.getCurrentKeyboardScriptId()); + break; + case MSG_RESUME_SUGGESTIONS_FOR_START_INPUT: + latinIme.mInputLogic.restartSuggestionsOnWordTouchedByCursor( + latinIme.mSettings.getCurrent(), true /* forStartInput */, + latinIme.mKeyboardSwitcher.getCurrentKeyboardScriptId()); + break; + case MSG_REOPEN_DICTIONARIES: + // We need to re-evaluate the currently composing word in case the script has + // changed. + postWaitForDictionaryLoad(); + latinIme.resetDictionaryFacilitatorIfNecessary(); + break; + case MSG_UPDATE_TAIL_BATCH_INPUT_COMPLETED: + final SuggestedWords suggestedWords = (SuggestedWords) msg.obj; + latinIme.mInputLogic.onUpdateTailBatchInputCompleted( + latinIme.mSettings.getCurrent(), + suggestedWords, latinIme.mKeyboardSwitcher); + latinIme.onTailBatchInputResultShown(suggestedWords); + break; + case MSG_RESET_CACHES: + final SettingsValues settingsValues = latinIme.mSettings.getCurrent(); + if (latinIme.mInputLogic.retryResetCachesAndReturnSuccess( + msg.arg1 == ARG1_TRUE /* tryResumeSuggestions */, + msg.arg2 /* remainingTries */, this /* handler */)) { + // If we were able to reset the caches, then we can reload the keyboard. + // Otherwise, we'll do it when we can. + latinIme.mKeyboardSwitcher.loadKeyboard(latinIme.getCurrentInputEditorInfo(), + settingsValues, latinIme.getCurrentAutoCapsState(), + latinIme.getCurrentRecapitalizeState()); + } + break; + case MSG_WAIT_FOR_DICTIONARY_LOAD: + Log.i(TAG, "Timeout waiting for dictionary load"); + break; + case MSG_DEALLOCATE_MEMORY: + latinIme.deallocateMemory(); + break; + case MSG_SWITCH_LANGUAGE_AUTOMATICALLY: + latinIme.switchLanguage((InputMethodSubtype)msg.obj); + break; } } @@ -321,7 +320,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } private void postResumeSuggestionsInternal(final boolean shouldDelay, - final boolean forStartInput) { + final boolean forStartInput) { final LatinIME latinIme = getOwnerInstance(); if (latinIme == null) { return; @@ -407,7 +406,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } public void showGesturePreviewAndSuggestionStrip(final SuggestedWords suggestedWords, - final boolean dismissGestureFloatingPreviewText) { + final boolean dismissGestureFloatingPreviewText) { removeMessages(MSG_SHOW_GESTURE_PREVIEW_AND_SUGGESTION_STRIP); final int arg1 = dismissGestureFloatingPreviewText ? ARG1_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT @@ -458,7 +457,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } private void executePendingImsCallback(final LatinIME latinIme, final EditorInfo editorInfo, - boolean restarting) { + boolean restarting) { if (mHasPendingFinishInputView) { latinIme.onFinishInputViewInternal(mHasPendingFinishInput); } @@ -882,7 +881,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen if (DebugFlags.DEBUG_ENABLED) { Log.d(TAG, "onStartInputView: editorInfo:" + String.format("inputType=0x%08x imeOptions=0x%08x", - editorInfo.inputType, editorInfo.imeOptions)); + editorInfo.inputType, editorInfo.imeOptions)); Log.d(TAG, "All caps = " + ((editorInfo.inputType & InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS) != 0) + ", sentence caps = " @@ -1071,8 +1070,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen @Override public void onUpdateSelection(final int oldSelStart, final int oldSelEnd, - final int newSelStart, final int newSelEnd, - final int composingSpanStart, final int composingSpanEnd) { + final int newSelStart, final int newSelEnd, + final int composingSpanStart, final int composingSpanEnd) { super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, composingSpanStart, composingSpanEnd); if (DebugFlags.DEBUG_ENABLED) { @@ -1088,7 +1087,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen final SettingsValues settingsValues = mSettings.getCurrent(); if (isInputViewShown() && mInputLogic.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, - settingsValues)) { + settingsValues)) { mKeyboardSwitcher.requestUpdatingShiftState(getCurrentAutoCapsState(), getCurrentRecapitalizeState()); } @@ -1356,10 +1355,18 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen @Override public void onMovePointer(int steps) { - for (; steps < 0; steps++) - mInputLogic.sendDownUpKeyEvent(KeyEvent.KEYCODE_DPAD_LEFT); - for (; steps > 0; steps--) - mInputLogic.sendDownUpKeyEvent(KeyEvent.KEYCODE_DPAD_RIGHT); + if (steps < 0) { + int availableCharacters = mInputLogic.mConnection.getTextBeforeCursor(64, 0).length(); + steps = availableCharacters < -steps ? -availableCharacters : steps; + } + else if (steps > 0) { + int availableCharacters = mInputLogic.mConnection.getTextAfterCursor(64, 0).length(); + steps = Math.min(availableCharacters, steps); + } else + return; + + int newPosition = mInputLogic.mConnection.mExpectedSelStart + steps; + mInputLogic.mConnection.setSelection(newPosition, newPosition); } @Override @@ -1371,12 +1378,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mInputLogic.mConnection.setSelection(start, end); } - @Override public void onUpWithDeletePointerActive() { if (mInputLogic.mConnection.hasSelection()) { - mInputLogic.sendDownUpKeyEvent(KeyEvent.KEYCODE_DEL); - mInputLogic.sendDownUpKeyEvent(KeyEvent.KEYCODE_SHIFT_LEFT); + mInputLogic.finishInput(); + onCodeInput(Constants.CODE_DELETE, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE, false); } } @@ -1415,7 +1421,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // Implementation of {@link KeyboardActionListener}. @Override public void onCodeInput(final int codePoint, final int x, final int y, - final boolean isKeyRepeat) { + final boolean isKeyRepeat) { // TODO: this processing does not belong inside LatinIME, the caller should be doing this. final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView(); // x and y include some padding, but everything down the line (especially native @@ -1449,7 +1455,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // public for testing, as we don't want to copy the same logic into test code @Nonnull public static Event createSoftwareKeypressEvent(final int keyCodeOrCodePoint, final int keyX, - final int keyY, final boolean isKeyRepeat) { + final int keyY, final boolean isKeyRepeat) { final int keyCode; final int codePoint; if (keyCodeOrCodePoint <= 0) { @@ -1514,7 +1520,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // This method must run on the UI Thread. void showGesturePreviewAndSuggestionStrip(@Nonnull final SuggestedWords suggestedWords, - final boolean dismissGestureFloatingPreviewText) { + final boolean dismissGestureFloatingPreviewText) { showSuggestionStrip(suggestedWords); final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView(); mainKeyboardView.showGestureFloatingPreviewText(suggestedWords, @@ -1555,7 +1561,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen ImportantNoticeUtils.shouldShowImportantNotice(this, currentSettingsValues); final boolean shouldShowSuggestionCandidates = currentSettingsValues.mInputAttributes.mShouldShowSuggestions - && currentSettingsValues.isSuggestionsEnabledPerUserSettings(); + && currentSettingsValues.isSuggestionsEnabledPerUserSettings(); final boolean shouldShowSuggestionsStripUnlessPassword = shouldShowImportantNotice || currentSettingsValues.mShowsVoiceInputKey || shouldShowSuggestionCandidates @@ -1569,7 +1575,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen final boolean isEmptyApplicationSpecifiedCompletions = currentSettingsValues.isApplicationSpecifiedCompletionsOn() - && suggestedWords.isEmpty(); + && suggestedWords.isEmpty(); final boolean noSuggestionsFromDictionaries = suggestedWords.isEmpty() || suggestedWords.isPunctuationSuggestions() || isEmptyApplicationSpecifiedCompletions; @@ -1594,7 +1600,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // TODO[IL]: Move this out of LatinIME. public void getSuggestedWords(final int inputStyle, final int sequenceNumber, - final OnGetSuggestedWordsCallback callback) { + final OnGetSuggestedWordsCallback callback) { final Keyboard keyboard = mKeyboardSwitcher.getKeyboard(); if (keyboard == null) { callback.onGetSuggestedWords(SuggestedWords.getEmptyInstance()); @@ -1664,14 +1670,14 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen */ private void updateStateAfterInputTransaction(final InputTransaction inputTransaction) { switch (inputTransaction.getRequiredShiftUpdate()) { - case InputTransaction.SHIFT_UPDATE_LATER: - mHandler.postUpdateShiftState(); - break; - case InputTransaction.SHIFT_UPDATE_NOW: - mKeyboardSwitcher.requestUpdatingShiftState(getCurrentAutoCapsState(), - getCurrentRecapitalizeState()); - break; - default: // SHIFT_NO_UPDATE + case InputTransaction.SHIFT_UPDATE_LATER: + mHandler.postUpdateShiftState(); + break; + case InputTransaction.SHIFT_UPDATE_NOW: + mKeyboardSwitcher.requestUpdatingShiftState(getCurrentAutoCapsState(), + getCurrentRecapitalizeState()); + break; + default: // SHIFT_NO_UPDATE } if (inputTransaction.requiresUpdateSuggestions()) { final int inputStyle; @@ -1720,7 +1726,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // release matching call is {@link #onReleaseKey(int,boolean)} below. @Override public void onPressKey(final int primaryCode, final int repeatCount, - final boolean isSinglePointer) { + final boolean isSinglePointer) { mKeyboardSwitcher.onPressKey(primaryCode, isSinglePointer, getCurrentAutoCapsState(), getCurrentRecapitalizeState()); hapticAndAudioFeedback(primaryCode, repeatCount); @@ -1831,18 +1837,18 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen public void onClick(DialogInterface di, int position) { di.dismiss(); switch (position) { - case 0: - final Intent intent = IntentUtils.getInputLanguageSelectionIntent( - imeId, - Intent.FLAG_ACTIVITY_NEW_TASK - | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED - | Intent.FLAG_ACTIVITY_CLEAR_TOP); - intent.putExtra(Intent.EXTRA_TITLE, languageSelectionTitle); - startActivity(intent); - break; - case 1: - launchSettings(); - break; + case 0: + final Intent intent = IntentUtils.getInputLanguageSelectionIntent( + imeId, + Intent.FLAG_ACTIVITY_NEW_TASK + | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED + | Intent.FLAG_ACTIVITY_CLEAR_TOP); + intent.putExtra(Intent.EXTRA_TITLE, languageSelectionTitle); + startActivity(intent); + break; + case 1: + launchSettings(); + break; } } }; @@ -1891,10 +1897,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen void replaceDictionariesForTest(final Locale locale) { final SettingsValues settingsValues = mSettings.getCurrent(); mDictionaryFacilitator.resetDictionaries(this, locale, - settingsValues.mUseContactsDict, settingsValues.mUsePersonalizedDicts, - false /* forceReloadMainDictionary */, - settingsValues.mAccount, "", /* dictionaryNamePrefix */ - this /* DictionaryInitializationListener */); + settingsValues.mUseContactsDict, settingsValues.mUsePersonalizedDicts, + false /* forceReloadMainDictionary */, + settingsValues.mAccount, "", /* dictionaryNamePrefix */ + this /* DictionaryInitializationListener */); } // DO NOT USE THIS for any other purpose than testing.