Skip to content
Snippets Groups Projects
Commit cd8499f7 authored by Aleksandras Kostarevas's avatar Aleksandras Kostarevas
Browse files

Mitigate OOM during update

parent f134517f
No related branches found
No related tags found
1 merge request!7Merge model-metadata to master
......@@ -63,6 +63,7 @@
android:allowBackup="true"
android:defaultToDeviceProtectedStorage="true"
android:directBootAware="true"
android:largeHeap="true"
android:name=".CrashLoggingApplication">
<!-- Services -->
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment