Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
}
}
when (primaryCode) {
KeyCode.TOGGLE_LITERAL_MODE -> return latinIME.toggleLiteralMode()
KeyCode.TOGGLE_SELECTION_MODE -> {
sPersistentSelectionModeActive = !sPersistentSelectionModeActive
keyboardSwitcher.mainKeyboardView?.invalidateAllKeys()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class KeyboardIconsSet private constructor() {
ToolbarKey.TOUCHPAD -> R.drawable.ic_touchpad
ToolbarKey.TEXT_EDIT -> R.drawable.ic_text_edit
ToolbarKey.AUTOCORRECT -> R.drawable.ic_autocorrect
ToolbarKey.LITERAL -> R.drawable.ic_literal
ToolbarKey.CLEAR_CLIPBOARD -> R.drawable.ic_bin
ToolbarKey.CLOSE_HISTORY -> R.drawable.ic_close
ToolbarKey.HANDWRITING -> R.drawable.ic_edit
Expand Down Expand Up @@ -245,6 +246,7 @@ class KeyboardIconsSet private constructor() {
ToolbarKey.TOUCHPAD -> R.drawable.ic_touchpad
ToolbarKey.TEXT_EDIT -> R.drawable.ic_text_edit
ToolbarKey.AUTOCORRECT -> R.drawable.ic_autocorrect
ToolbarKey.LITERAL -> R.drawable.ic_literal
ToolbarKey.CLEAR_CLIPBOARD -> R.drawable.ic_bin
ToolbarKey.CLOSE_HISTORY -> R.drawable.ic_close
ToolbarKey.HANDWRITING -> R.drawable.ic_edit
Expand Down Expand Up @@ -325,6 +327,7 @@ class KeyboardIconsSet private constructor() {
ToolbarKey.TOUCHPAD -> R.drawable.ic_touchpad_rounded
ToolbarKey.TEXT_EDIT -> R.drawable.ic_text_edit
ToolbarKey.AUTOCORRECT -> R.drawable.ic_autocorrect_rounded
ToolbarKey.LITERAL -> R.drawable.ic_literal
ToolbarKey.CLEAR_CLIPBOARD -> R.drawable.ic_bin
ToolbarKey.CLOSE_HISTORY -> R.drawable.ic_close_rounded
ToolbarKey.HANDWRITING -> R.drawable.ic_edit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ object KeyCode {
const val CLEAR_HANDWRITING = -10075
const val SWITCH_TO_USER_IME = -10076
const val OCR = -10077
const val TOGGLE_LITERAL_MODE = -10078


// Intents
Expand All @@ -230,7 +231,7 @@ object KeyCode {
TIMESTAMP, CTRL_LEFT, CTRL_RIGHT, ALT_LEFT, ALT_RIGHT, META_LEFT, META_RIGHT, SEND_INTENT_ONE, SEND_INTENT_TWO,
SEND_INTENT_THREE, INLINE_EMOJI_SEARCH_DONE, META_LOCK, PROOFREAD, TRANSLATE, SHOW_TRANSLATE_LANGUAGES,
CUSTOM_AI_1, CUSTOM_AI_2, CUSTOM_AI_3, CUSTOM_AI_4, CUSTOM_AI_5,
CUSTOM_AI_6, CUSTOM_AI_7, CUSTOM_AI_8, CUSTOM_AI_9, CUSTOM_AI_10, CLIPBOARD_SEARCH, TOGGLE_FLOATING_KEYBOARD, TOGGLE_TOUCHPAD_MODE, TOGGLE_TEXT_EDIT_MODE, TOGGLE_SELECTION_MODE, HANDWRITING, CLEAR_HANDWRITING, OCR,
CUSTOM_AI_6, CUSTOM_AI_7, CUSTOM_AI_8, CUSTOM_AI_9, CUSTOM_AI_10, CLIPBOARD_SEARCH, TOGGLE_FLOATING_KEYBOARD, TOGGLE_TOUCHPAD_MODE, TOGGLE_TEXT_EDIT_MODE, TOGGLE_SELECTION_MODE, HANDWRITING, CLEAR_HANDWRITING, OCR, TOGGLE_LITERAL_MODE,
CUSTOM1, CUSTOM2, CUSTOM3, CUSTOM4, CUSTOM5, SWITCH_TO_USER_IME
-> this

Expand Down
108 changes: 95 additions & 13 deletions app/src/main/java/helium314/keyboard/latin/LatinIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,46 @@ class LatinIME : InputMethodService(),
private val mTempRect = Rect()
private val mFloatingTouchableRect = Rect()
@Volatile var isCursorGestureActive = false
@Volatile var isLiteralMode = false
private set
private var literalEditor: EditorInfo? = null
private var literalEditorIdentity: LiteralEditorIdentity? = null
private var literalEditorConnection: android.view.inputmethod.InputConnection? = null

private data class LiteralEditorIdentity(
val packageName: String?, val fieldId: Int, val fieldName: String?,
val inputType: Int, val imeOptions: Int
)

fun toggleLiteralMode() {
if (currentInputEditorInfo == null) return
// Finish the visible composition as typed, without correction, expansion, or learning.
inputLogic.finishInput()
isLiteralMode = !isLiteralMode
handler.cancelPendingSuggestions()
sSettingsDirty = true
loadSettings()
setNeutralSuggestionStrip()
keyboardSwitcher.mainKeyboardView?.apply {
setGestureHandlingEnabledByUser(
settings.current.mGestureInputEnabled, settings.current.mGestureTrailEnabled,
settings.current.mGestureFloatingPreviewTextEnabled
)
showGestureFloatingPreviewText(SuggestedWords.getEmptyInstance(), true)
}
keyboardSwitcher.requestUpdatingShiftState(currentAutoCapsState, currentRecapitalizeState)
suggestionStripView?.updateToolbarButtonsActivatedState()
}

private fun resetLiteralMode() {
isLiteralMode = false
literalEditor = null
literalEditorIdentity = null
literalEditorConnection = null
inputLogic.invalidateSuggestions()
handler.cancelPendingSuggestions()
sSettingsDirty = true
}

private var voicePluginManager: VoicePluginManager? = null
private var voiceInputManager: VoiceInputManager? = null
Expand Down Expand Up @@ -370,6 +410,7 @@ class LatinIME : InputMethodService(),
val localeAndConfidenceInfo: String? get() = dictionaryFacilitator.localesAndConfidences()

override fun onDestroy() {
resetLiteralMode()
if (sInstance === this) sInstance = null
if (isShowingOptionDialog()) {
optionsDialog?.dismiss()
Expand Down Expand Up @@ -641,6 +682,16 @@ class LatinIME : InputMethodService(),

fun onStartInputInternal(editorInfo: EditorInfo?, restarting: Boolean) {
super.onStartInput(editorInfo, restarting)
val identity = editorInfo?.let {
LiteralEditorIdentity(it.packageName, it.fieldId, it.fieldName, it.inputType, it.imeOptions)
}
val sameEditor = restarting && identity != null && identity == literalEditorIdentity &&
(identity.fieldId != 0 || editorInfo === literalEditor ||
(currentInputConnection != null && currentInputConnection === literalEditorConnection))
if (!sameEditor) resetLiteralMode()
literalEditor = editorInfo
literalEditorIdentity = identity
literalEditorConnection = currentInputConnection
floatingKeyboardManager?.resetDragAndResizeState()
inputLogic.connection.onStartInput()

Expand Down Expand Up @@ -700,7 +751,7 @@ class LatinIME : InputMethodService(),

updateFullscreenMode()

if (isDifferentTextField || !currentSettingsValues.hasSameOrientation(resources.configuration)) {
if (sSettingsDirty || isDifferentTextField || !currentSettingsValues.hasSameOrientation(resources.configuration)) {
loadSettings()
currentSettingsValues = settings.current
suggestionStripView?.updateVoiceKey()
Expand Down Expand Up @@ -816,6 +867,7 @@ class LatinIME : InputMethodService(),
}

fun onFinishInputInternal() {
resetLiteralMode()
super.onFinishInput()
Log.i(TAG, "onFinishInput")
floatingKeyboardManager?.resetDragAndResizeState()
Expand Down Expand Up @@ -903,6 +955,7 @@ class LatinIME : InputMethodService(),
}

override fun onDisplayCompletions(applicationSpecifiedCompletions: Array<CompletionInfo>?) {
if (isLiteralMode) return
if (DebugFlags.DEBUG_ENABLED) {
Log.i(TAG, "Received completions:")
applicationSpecifiedCompletions?.forEachIndexed { i, info -> Log.i(TAG, " #$i: $info") }
Expand Down Expand Up @@ -1350,6 +1403,7 @@ class LatinIME : InputMethodService(),
private fun hasSuggestionStripView(): Boolean = suggestionStripView != null

private fun setSuggestedWords(suggestedWords: SuggestedWords) {
if (isLiteralMode && !suggestedWords.isEmpty) return
val currentSettingsValues = settings.current
inputLogic.setSuggestedWords(suggestedWords)
if (!hasSuggestionStripView()) return
Expand All @@ -1374,6 +1428,10 @@ class LatinIME : InputMethodService(),
}

override fun setSuggestions(suggestedWords: SuggestedWords) {
if (isLiteralMode) {
setSuggestedWords(SuggestedWords.getEmptyInstance())
return
}
if (tryShowMathSuggestion()) return
if (suggestedWords.isEmpty) {
if (keyboardSwitcher.isHandwritingShowing) {
Expand All @@ -1392,7 +1450,7 @@ class LatinIME : InputMethodService(),
}

override fun pickSuggestionManually(suggestionInfo: SuggestedWordInfo?) {
if (suggestionInfo == null) return
if (suggestionInfo == null || isLiteralMode) return
val completeInputTransaction = inputLogic.onPickSuggestionManually(settings.current, suggestionInfo, keyboardSwitcher.keyboardShiftMode, handler)
updateStateAfterInputTransaction(completeInputTransaction)
if (keyboardSwitcher.isHandwritingShowing) keyboardSwitcher.clearHandwritingCanvas()
Expand All @@ -1403,6 +1461,7 @@ class LatinIME : InputMethodService(),
}

fun tryShowOtpSuggestion(): Boolean {
if (isLiteralMode) return false
val strip = suggestionStripView ?: return false
val otpView = otpSuggestionManager.getOtpSuggestionView(strip)
if (otpView != null) {
Expand All @@ -1413,6 +1472,7 @@ class LatinIME : InputMethodService(),
}

fun tryShowMathSuggestion(): Boolean {
if (isLiteralMode) return false
val strip = suggestionStripView ?: return false
val mathView = mathSuggestionManager.getMathSuggestionView(strip)
if (mathView != null) {
Expand All @@ -1423,6 +1483,7 @@ class LatinIME : InputMethodService(),
}

fun tryShowClipboardSuggestion(): Boolean {
if (isLiteralMode) return false
val strip = suggestionStripView ?: return false
val clipboardView = clipboardHistoryManager.getClipboardSuggestionView(currentInputEditorInfo, strip)
if (clipboardView != null) {
Expand All @@ -1440,6 +1501,11 @@ class LatinIME : InputMethodService(),
get() = suggestionStripView?.isExternalSuggestionVisible == true

override fun setNeutralSuggestionStrip() {
if (isLiteralMode) {
suggestionStripView?.setExternalSuggestionView(null, false)
setSuggestedWords(SuggestedWords.getEmptyInstance())
return
}
if (keyboardSwitcher.isHandwritingShowing) return
val currentSettings = settings.current
if (tryShowExternalSuggestion()) {
Expand Down Expand Up @@ -1741,6 +1807,9 @@ class LatinIME : InputMethodService(),

override fun handleMessage(msg: Message) {
val latinIme = ownerInstance ?: return
if (msg.what in listOf(MSG_UPDATE_SUGGESTION_STRIP, MSG_RESUME_SUGGESTIONS,
MSG_SHOW_GESTURE_PREVIEW_AND_SET_SUGGESTIONS, MSG_UPDATE_TAIL_BATCH_INPUT_COMPLETED) &&
!latinIme.inputLogic.isSuggestionRequestCurrent(msg.arg2)) return
when (msg.what) {
MSG_UPDATE_SUGGESTION_STRIP -> {
cancelUpdateSuggestionStrip()
Expand Down Expand Up @@ -1776,9 +1845,9 @@ class LatinIME : InputMethodService(),
}

fun postUpdateSuggestionStrip(inputStyle: Int) {
val latinIme = ownerInstance
if (latinIme != null && latinIme.keyboardSwitcher.isHandwritingShowing) return
sendMessageDelayed(obtainMessage(MSG_UPDATE_SUGGESTION_STRIP, inputStyle, 0), delayInMillisecondsToUpdateSuggestions.toLong())
val latinIme = ownerInstance ?: return
if (latinIme.isLiteralMode || latinIme.keyboardSwitcher.isHandwritingShowing) return
sendMessageDelayed(obtainMessage(MSG_UPDATE_SUGGESTION_STRIP, inputStyle, latinIme.inputLogic.suggestionGeneration), delayInMillisecondsToUpdateSuggestions.toLong())
}

fun postReopenDictionaries() { sendMessage(obtainMessage(MSG_REOPEN_DICTIONARIES)) }
Expand All @@ -1789,8 +1858,9 @@ class LatinIME : InputMethodService(),
if (latinIme.keyboardSwitcher.isHandwritingShowing) return
if (!latinIme.settings.current.needsToLookupSuggestions()) return
removeMessages(MSG_RESUME_SUGGESTIONS)
if (shouldDelay) sendMessageDelayed(obtainMessage(MSG_RESUME_SUGGESTIONS), delayInMillisecondsToUpdateSuggestions.toLong())
else sendMessage(obtainMessage(MSG_RESUME_SUGGESTIONS))
val message = obtainMessage(MSG_RESUME_SUGGESTIONS, 0, latinIme.inputLogic.suggestionGeneration)
if (shouldDelay) sendMessageDelayed(message, delayInMillisecondsToUpdateSuggestions.toLong())
else sendMessage(message)
}

fun postResetCaches(tryResumeSuggestions: Boolean, remainingTries: Int) {
Expand All @@ -1803,6 +1873,12 @@ class LatinIME : InputMethodService(),
fun hasPendingWaitForDictionaryLoad(): Boolean = hasMessages(MSG_WAIT_FOR_DICTIONARY_LOAD)
fun cancelUpdateSuggestionStrip() { removeMessages(MSG_UPDATE_SUGGESTION_STRIP) }
fun cancelResumeSuggestions() { removeMessages(MSG_RESUME_SUGGESTIONS) }
fun cancelPendingSuggestions() {
cancelUpdateSuggestionStrip()
cancelResumeSuggestions()
removeMessages(MSG_SHOW_GESTURE_PREVIEW_AND_SET_SUGGESTIONS)
removeMessages(MSG_UPDATE_TAIL_BATCH_INPUT_COMPLETED)
}
fun hasPendingUpdateSuggestions(): Boolean = hasMessages(MSG_UPDATE_SUGGESTION_STRIP)
fun hasPendingResumeSuggestions(): Boolean = hasMessages(MSG_RESUME_SUGGESTIONS)
fun hasPendingReopenDictionaries(): Boolean = hasMessages(MSG_REOPEN_DICTIONARIES)
Expand All @@ -1820,19 +1896,25 @@ class LatinIME : InputMethodService(),
for (i in 0..MSG_LAST) removeMessages(i)
}

fun showGesturePreviewAndSetSuggestions(suggestedWords: SuggestedWords, dismissGestureFloatingPreviewText: Boolean) {
fun showGesturePreviewAndSetSuggestions(
suggestedWords: SuggestedWords, dismissGestureFloatingPreviewText: Boolean,
generation: Int = ownerInstance?.inputLogic?.suggestionGeneration ?: 0
) {
if (ownerInstance?.inputLogic?.isSuggestionRequestCurrent(generation) != true) return
removeMessages(MSG_SHOW_GESTURE_PREVIEW_AND_SET_SUGGESTIONS)
val arg1 = if (dismissGestureFloatingPreviewText) ARG1_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT else ARG1_SHOW_GESTURE_FLOATING_PREVIEW_TEXT
obtainMessage(MSG_SHOW_GESTURE_PREVIEW_AND_SET_SUGGESTIONS, arg1, ARG2_UNUSED, suggestedWords).sendToTarget()
obtainMessage(MSG_SHOW_GESTURE_PREVIEW_AND_SET_SUGGESTIONS, arg1, generation, suggestedWords).sendToTarget()
}

fun setSuggestions(suggestedWords: SuggestedWords) {
fun setSuggestions(suggestedWords: SuggestedWords, generation: Int = ownerInstance?.inputLogic?.suggestionGeneration ?: 0) {
if (ownerInstance?.inputLogic?.isSuggestionRequestCurrent(generation) != true) return
removeMessages(MSG_SHOW_GESTURE_PREVIEW_AND_SET_SUGGESTIONS)
obtainMessage(MSG_SHOW_GESTURE_PREVIEW_AND_SET_SUGGESTIONS, ARG1_NOT_GESTURE_INPUT, ARG2_UNUSED, suggestedWords).sendToTarget()
obtainMessage(MSG_SHOW_GESTURE_PREVIEW_AND_SET_SUGGESTIONS, ARG1_NOT_GESTURE_INPUT, generation, suggestedWords).sendToTarget()
}

fun showTailBatchInputResult(suggestedWords: SuggestedWords) {
obtainMessage(MSG_UPDATE_TAIL_BATCH_INPUT_COMPLETED, suggestedWords).sendToTarget()
fun showTailBatchInputResult(suggestedWords: SuggestedWords, generation: Int) {
if (ownerInstance?.inputLogic?.isSuggestionRequestCurrent(generation) != true) return
obtainMessage(MSG_UPDATE_TAIL_BATCH_INPUT_COMPLETED, 0, generation, suggestedWords).sendToTarget()
}

fun postSwitchLanguage(subtype: InputMethodSubtype) {
Expand Down
Loading
Loading