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.VibrationEffect
import android.os.Vibrator
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InlineSuggestionsResponse
@ -67,9 +68,19 @@ private class LatinIMEActionInputTransaction(
shouldApplySpace: Boolean
): ActionInputTransaction {
private val isSpaceNecessary: Boolean
private var isFinished = false
init {
val priorText = inputLogic.mConnection.getTextBeforeCursor(1, 0)
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 {
@ -77,6 +88,7 @@ private class LatinIMEActionInputTransaction(
}
override fun updatePartial(text: String) {
if(isFinished) return
inputLogic.mConnection.setComposingText(
transformText(text),
1
@ -84,14 +96,18 @@ private class LatinIMEActionInputTransaction(
}
override fun commit(text: String) {
if(isFinished) return
inputLogic.mConnection.commitText(
transformText(text),
1
)
finish()
}
override fun cancel() {
if(isFinished) return
inputLogic.mConnection.finishComposingText()
finish()
}
}

View File

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