From cd8499f742517428b5c927d8c761323e5d5133c1 Mon Sep 17 00:00:00 2001 From: Aleksandras Kostarevas Date: Thu, 14 Mar 2024 14:45:23 -0500 Subject: [PATCH] Mitigate OOM during update --- java/AndroidManifest.xml | 1 + .../latin/uix/settings/UpdateScreen.kt | 15 +++++++-------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/java/AndroidManifest.xml b/java/AndroidManifest.xml index afc336cef..258b2f65a 100644 --- a/java/AndroidManifest.xml +++ b/java/AndroidManifest.xml @@ -63,6 +63,7 @@ android:allowBackup="true" android:defaultToDeviceProtectedStorage="true" android:directBootAware="true" + android:largeHeap="true" android:name=".CrashLoggingApplication"> diff --git a/java/src/org/futo/inputmethod/latin/uix/settings/UpdateScreen.kt b/java/src/org/futo/inputmethod/latin/uix/settings/UpdateScreen.kt index a85f366f0..31addbc67 100644 --- a/java/src/org/futo/inputmethod/latin/uix/settings/UpdateScreen.kt +++ b/java/src/org/futo/inputmethod/latin/uix/settings/UpdateScreen.kt @@ -92,7 +92,7 @@ object UpdateStatus { val isDownloading = mutableStateOf(false) val downloadText = mutableStateOf("") - var downloadedUpdate: ByteArray? = null + var downloadedUpdate: ByteArrayOutputStream? = null } private suspend fun install(scope: CoroutineScope, context: Context, inputStream: InputStream, dataLength: Long, updateStatusText: (String) -> Unit) { @@ -109,7 +109,7 @@ private suspend fun install(scope: CoroutineScope, context: Context, inputStream session = packageInstaller.openSession(sessionId) if(UpdateStatus.downloadedUpdate == null) { - ByteArrayOutputStream(dataLength.toInt()).use { outputStream -> + UpdateStatus.downloadedUpdate = ByteArrayOutputStream(dataLength.toInt()).use { outputStream -> inputStream.copyToOutputStream(dataLength, outputStream) { progress -> val progressText = "${(progress * 100.0f).toInt()}%"; if (lastProgressText != progressText) { @@ -119,17 +119,16 @@ private suspend fun install(scope: CoroutineScope, context: Context, inputStream } } - UpdateStatus.downloadedUpdate = outputStream.toByteArray() + // Note: .use will close the outputStream, but closing has no effect + // on ByteArrayOutputStream + outputStream } } session.openWrite("package", 0, dataLength).use { sessionStream -> - UpdateStatus.downloadedUpdate!!.inputStream().use { byteStream -> - byteStream.copyToOutputStream(dataLength, sessionStream) { } - } - - session.fsync(sessionStream); + UpdateStatus.downloadedUpdate!!.writeTo(sessionStream) + session.fsync(sessionStream) }; val intent = Intent(context, InstallReceiver::class.java);