From 724893879f27a3c513e0df25c122960732ae0405 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Tue, 9 Jul 2019 20:00:57 +0200 Subject: [PATCH] Added option to intercept progress while copying files --- .../data/repository/MagiskRepository.kt | 10 +++---- .../com/topjohnwu/magisk/utils/XNetwork.kt | 27 +++++++++++++++++-- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt b/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt index cc65d0f5f..c3973ca39 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/repository/MagiskRepository.kt @@ -12,7 +12,7 @@ import com.topjohnwu.magisk.model.entity.HideTarget import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.utils.inject import com.topjohnwu.magisk.utils.toSingle -import com.topjohnwu.magisk.utils.writeToFile +import com.topjohnwu.magisk.utils.writeToCachedFile import com.topjohnwu.superuser.Shell import io.reactivex.Single @@ -24,21 +24,21 @@ class MagiskRepository( fun fetchMagisk() = fetchUpdate() .flatMap { apiRaw.fetchFile(it.magisk.link) } - .map { it.writeToFile(context, FILE_MAGISK_ZIP) } + .map { it.writeToCachedFile(context, FILE_MAGISK_ZIP) } fun fetchManager() = fetchUpdate() .flatMap { apiRaw.fetchFile(it.app.link) } - .map { it.writeToFile(context, FILE_MAGISK_APK) } + .map { it.writeToCachedFile(context, FILE_MAGISK_APK) } fun fetchUninstaller() = fetchUpdate() .flatMap { apiRaw.fetchFile(it.uninstaller.link) } - .map { it.writeToFile(context, FILE_UNINSTALLER_ZIP) } + .map { it.writeToCachedFile(context, FILE_UNINSTALLER_ZIP) } fun fetchSafetynet() = apiRaw.fetchSafetynet() fun fetchBootctl() = apiRaw .fetchBootctl() - .map { it.writeToFile(context, FILE_BOOTCTL_SH) } + .map { it.writeToCachedFile(context, FILE_BOOTCTL_SH) } fun fetchUpdate() = when (Config.updateChannel) { diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/XNetwork.kt b/app/src/main/java/com/topjohnwu/magisk/utils/XNetwork.kt index 7548c2274..df7c91c41 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/XNetwork.kt +++ b/app/src/main/java/com/topjohnwu/magisk/utils/XNetwork.kt @@ -3,13 +3,36 @@ package com.topjohnwu.magisk.utils import android.content.Context import okhttp3.ResponseBody import java.io.File +import java.io.InputStream +import java.io.OutputStream -fun ResponseBody.writeToFile(context: Context, fileName: String): File { +inline fun ResponseBody.writeToCachedFile( + context: Context, + fileName: String, + progress: (Long) -> Unit = {} +): File { val file = File(context.cacheDir, fileName) withStreams(byteStream(), file.outputStream()) { inStream, outStream -> - inStream.copyTo(outStream) + inStream.copyToWithProgress(outStream, progress) } return file } fun ResponseBody.writeToString() = string() + +inline fun InputStream.copyToWithProgress( + out: OutputStream, + progressEmitter: (Long) -> Unit, + bufferSize: Int = DEFAULT_BUFFER_SIZE +): Long { + var bytesCopied: Long = 0 + val buffer = ByteArray(bufferSize) + var bytes = read(buffer) + while (bytes >= 0) { + out.write(buffer, 0, bytes) + bytesCopied += bytes + bytes = read(buffer) + progressEmitter(bytesCopied) + } + return bytesCopied +} \ No newline at end of file