Added option to intercept progress while copying files
This commit is contained in:
parent
736729f5ef
commit
724893879f
@ -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) {
|
||||
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user