From 6a0f6ab319681643178edb4f8fbe8f61fada8f97 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Wed, 10 Jul 2019 19:13:15 +0200 Subject: [PATCH] Updated magisk installer so it uses predownloaded file --- .../topjohnwu/magisk/model/flash/Patching.kt | 12 ++++-- .../topjohnwu/magisk/tasks/InstallerHelper.kt | 18 +++++++++ .../magisk/tasks/MagiskInstaller.java | 40 +++++++++++++------ .../magisk/ui/flash/FlashViewModel.kt | 12 +++--- 4 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/com/topjohnwu/magisk/tasks/InstallerHelper.kt diff --git a/app/src/main/java/com/topjohnwu/magisk/model/flash/Patching.kt b/app/src/main/java/com/topjohnwu/magisk/model/flash/Patching.kt index ebb21128e..e8e4bbd77 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/flash/Patching.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/flash/Patching.kt @@ -5,10 +5,11 @@ import com.topjohnwu.magisk.tasks.MagiskInstaller import com.topjohnwu.superuser.Shell sealed class Patching( + file: Uri, private val console: MutableList, logs: MutableList, private val resultListener: FlashResultListener -) : MagiskInstaller(console, logs) { +) : MagiskInstaller(console, logs, file) { override fun onResult(success: Boolean) { if (success) { @@ -21,29 +22,32 @@ sealed class Patching( } class File( + file: Uri, private val uri: Uri, console: MutableList, logs: MutableList, resultListener: FlashResultListener - ) : Patching(console, logs, resultListener) { + ) : Patching(file, console, logs, resultListener) { override fun operations() = extractZip() && handleFile(uri) && patchBoot() && storeBoot() } class SecondSlot( + file: Uri, console: MutableList, logs: MutableList, resultListener: FlashResultListener - ) : Patching(console, logs, resultListener) { + ) : Patching(file, console, logs, resultListener) { override fun operations() = findSecondaryImage() && extractZip() && patchBoot() && flashBoot() && postOTA() } class Direct( + file: Uri, console: MutableList, logs: MutableList, resultListener: FlashResultListener - ) : Patching(console, logs, resultListener) { + ) : Patching(file, console, logs, resultListener) { override fun operations() = findImage() && extractZip() && patchBoot() && flashBoot() } diff --git a/app/src/main/java/com/topjohnwu/magisk/tasks/InstallerHelper.kt b/app/src/main/java/com/topjohnwu/magisk/tasks/InstallerHelper.kt new file mode 100644 index 000000000..a674464b5 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/tasks/InstallerHelper.kt @@ -0,0 +1,18 @@ +package com.topjohnwu.magisk.tasks + +import android.content.Context +import android.net.Uri +import com.topjohnwu.magisk.utils.get +import com.topjohnwu.magisk.utils.readUri +import java.io.File + +object InstallerHelper { + @JvmStatic + fun copyFileTo(uri: Uri, zip: File) { + zip.deleteRecursively() + + get().readUri(uri).use { input -> + zip.outputStream().use { out -> input.copyTo(out) } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/tasks/MagiskInstaller.java b/app/src/main/java/com/topjohnwu/magisk/tasks/MagiskInstaller.java index 197125158..99fd005b3 100644 --- a/app/src/main/java/com/topjohnwu/magisk/tasks/MagiskInstaller.java +++ b/app/src/main/java/com/topjohnwu/magisk/tasks/MagiskInstaller.java @@ -4,9 +4,6 @@ import android.net.Uri; import android.os.Build; import android.text.TextUtils; -import androidx.annotation.MainThread; -import androidx.annotation.WorkerThread; - import com.topjohnwu.magisk.App; import com.topjohnwu.magisk.Const; import com.topjohnwu.magisk.Info; @@ -41,13 +38,21 @@ import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; +import androidx.annotation.MainThread; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.WorkerThread; + public abstract class MagiskInstaller { protected String srcBoot; protected File destFile; protected File installDir; - private List console, logs; + @Nullable + private final Uri preDownloadedFile; + private final List console; + private final List logs; private boolean isTar = false; private class ProgressLog implements DownloadProgressListener { @@ -72,14 +77,16 @@ public abstract class MagiskInstaller { protected MagiskInstaller() { console = NOPList.getInstance(); logs = NOPList.getInstance(); + preDownloadedFile = null; } - public MagiskInstaller(List out, List err) { + public MagiskInstaller(List out, List err, @NonNull Uri magisk) { console = out; logs = err; installDir = new File(App.deContext.getFilesDir().getParent(), "install"); Shell.sh("rm -rf " + installDir).exec(); installDir.mkdirs(); + preDownloadedFile = magisk; } protected boolean findImage() { @@ -94,7 +101,7 @@ public abstract class MagiskInstaller { protected boolean findSecondaryImage() { String slot = ShellUtils.fastCmd("echo $SLOT"); - String target = (TextUtils.equals(slot, "_a") ? "_b" : "_a"); + String target = TextUtils.equals(slot, "_a") ? "_b" : "_a"; console.add("- Target slot: " + target); srcBoot = ShellUtils.fastCmd( "SLOT=" + target, @@ -122,14 +129,21 @@ public abstract class MagiskInstaller { console.add("- Device platform: " + Build.CPU_ABI); File zip = new File(App.self.getCacheDir(), "magisk.zip"); + if (preDownloadedFile != null) { + console.add("- Using already downloaded file"); - if (!ShellUtils.checkSum("MD5", zip, Info.remote.getMagisk().getHash())) { - console.add("- Downloading zip"); - Networking.get(Info.remote.getMagisk().getLink()) - .setDownloadProgressListener(new ProgressLog()) - .execForFile(zip); + InstallerHelper.copyFileTo(preDownloadedFile, zip); } else { - console.add("- Existing zip found"); + console.add("- Using legacy download method"); + + if (!ShellUtils.checkSum("MD5", zip, Info.remote.getMagisk().getHash())) { + console.add("- Downloading zip"); + Networking.get(Info.remote.getMagisk().getLink()) + .setDownloadProgressListener(new ProgressLog()) + .execForFile(zip); + } else { + console.add("- Existing zip found"); + } } try { @@ -151,7 +165,7 @@ public abstract class MagiskInstaller { name = ze.getName(); if (name == null) continue; - File dest = (installDir instanceof SuFile) ? + File dest = installDir instanceof SuFile ? new SuFile(installDir, name) : new File(installDir, name); dest.getParentFile().mkdirs(); diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt index 46d0533ed..a45fc3be9 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt @@ -28,7 +28,7 @@ import java.util.* class FlashViewModel( action: String, - file: Uri, + installer: Uri, uri: Uri, private val resources: Resources ) : MagiskViewModel(), FlashResultListener { @@ -55,19 +55,19 @@ class FlashViewModel( when (action) { Const.Value.FLASH_ZIP -> Flashing - .Install(uri, outItems, logItems, this) + .Install(installer, outItems, logItems, this) .exec() Const.Value.UNINSTALL -> Flashing - .Uninstall(uri, outItems, logItems, this) + .Uninstall(installer, outItems, logItems, this) .exec() Const.Value.FLASH_MAGISK -> Patching - .Direct(outItems, logItems, this) + .Direct(installer, outItems, logItems, this) .exec() Const.Value.FLASH_INACTIVE_SLOT -> Patching - .SecondSlot(outItems, logItems, this) + .SecondSlot(installer, outItems, logItems, this) .exec() Const.Value.PATCH_FILE -> Patching - .File(uri, outItems, logItems, this) + .File(installer, uri, outItems, logItems, this) .exec() } }