Use batchEdit during voice input transaction

This commit is contained in:
Aleksandras Kostarevas 2024-03-21 19:59:51 -05:00
parent 20f897aa56
commit 21da28ff22
2 changed files with 20 additions and 11 deletions

View File

@ -6,6 +6,7 @@ import android.content.Intent
import android.os.Build import android.os.Build
import android.os.VibrationEffect import android.os.VibrationEffect
import android.os.Vibrator import android.os.Vibrator
import android.util.Log
import android.view.View import android.view.View
import android.view.WindowManager import android.view.WindowManager
import android.view.inputmethod.InlineSuggestionsResponse import android.view.inputmethod.InlineSuggestionsResponse
@ -67,9 +68,19 @@ private class LatinIMEActionInputTransaction(
shouldApplySpace: Boolean shouldApplySpace: Boolean
): ActionInputTransaction { ): ActionInputTransaction {
private val isSpaceNecessary: Boolean private val isSpaceNecessary: Boolean
private var isFinished = false
init { init {
val priorText = inputLogic.mConnection.getTextBeforeCursor(1, 0) val priorText = inputLogic.mConnection.getTextBeforeCursor(1, 0)
isSpaceNecessary = shouldApplySpace && !priorText.isNullOrEmpty() && !priorText.last().isWhitespace() isSpaceNecessary = shouldApplySpace && !priorText.isNullOrEmpty() && !priorText.last().isWhitespace()
Log.i("LatinIMEActionInputTransaction", "Beginning batch edit due to input transaction")
inputLogic.mConnection.beginBatchEdit()
}
private fun finish() {
isFinished = true
inputLogic.mConnection.endBatchEdit()
Log.i("LatinIMEActionInputTransaction", "Ending batch edit due to input transaction having been finished or cancelled")
} }
private fun transformText(text: String): String { private fun transformText(text: String): String {
@ -77,6 +88,7 @@ private class LatinIMEActionInputTransaction(
} }
override fun updatePartial(text: String) { override fun updatePartial(text: String) {
if(isFinished) return
inputLogic.mConnection.setComposingText( inputLogic.mConnection.setComposingText(
transformText(text), transformText(text),
1 1
@ -84,14 +96,18 @@ private class LatinIMEActionInputTransaction(
} }
override fun commit(text: String) { override fun commit(text: String) {
if(isFinished) return
inputLogic.mConnection.commitText( inputLogic.mConnection.commitText(
transformText(text), transformText(text),
1 1
) )
finish()
} }
override fun cancel() { override fun cancel() {
if(isFinished) return
inputLogic.mConnection.finishComposingText() inputLogic.mConnection.finishComposingText()
finish()
} }
} }

View File

@ -152,14 +152,7 @@ private class VoiceInputActionWindow(
recognizerView.start() recognizerView.start()
} }
private var inputTransaction: ActionInputTransaction? = null private var inputTransaction = manager.createInputTransaction(true)
private fun getOrStartInputTransaction(): ActionInputTransaction {
if (inputTransaction == null) {
inputTransaction = manager.createInputTransaction(true)
}
return inputTransaction!!
}
@Composable @Composable
private fun ModelDownloader(modelException: ModelDoesNotExistException) { private fun ModelDownloader(modelException: ModelDoesNotExistException) {
@ -215,7 +208,7 @@ private class VoiceInputActionWindow(
state.soundPlayer.playCancelSound() state.soundPlayer.playCancelSound()
cancelPlayed = true cancelPlayed = true
} }
inputTransaction?.cancel() inputTransaction.cancel()
} }
} }
@ -228,12 +221,12 @@ private class VoiceInputActionWindow(
override fun finished(result: String) { override fun finished(result: String) {
wasFinished = true wasFinished = true
getOrStartInputTransaction().commit(result) inputTransaction.commit(result)
manager.closeActionWindow() manager.closeActionWindow()
} }
override fun partialResult(result: String) { override fun partialResult(result: String) {
getOrStartInputTransaction().updatePartial(result) inputTransaction.updatePartial(result)
} }
override fun requestPermission(onGranted: () -> Unit, onRejected: () -> Unit): Boolean { override fun requestPermission(onGranted: () -> Unit, onRejected: () -> Unit): Boolean {