Updated magisk installer so it uses predownloaded file

This commit is contained in:
Viktor De Pasquale 2019-07-10 19:13:15 +02:00 committed by John Wu
parent 88a394836f
commit 6a0f6ab319
4 changed files with 59 additions and 23 deletions

View File

@ -5,10 +5,11 @@ import com.topjohnwu.magisk.tasks.MagiskInstaller
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
sealed class Patching( sealed class Patching(
file: Uri,
private val console: MutableList<String>, private val console: MutableList<String>,
logs: MutableList<String>, logs: MutableList<String>,
private val resultListener: FlashResultListener private val resultListener: FlashResultListener
) : MagiskInstaller(console, logs) { ) : MagiskInstaller(console, logs, file) {
override fun onResult(success: Boolean) { override fun onResult(success: Boolean) {
if (success) { if (success) {
@ -21,29 +22,32 @@ sealed class Patching(
} }
class File( class File(
file: Uri,
private val uri: Uri, private val uri: Uri,
console: MutableList<String>, console: MutableList<String>,
logs: MutableList<String>, logs: MutableList<String>,
resultListener: FlashResultListener resultListener: FlashResultListener
) : Patching(console, logs, resultListener) { ) : Patching(file, console, logs, resultListener) {
override fun operations() = override fun operations() =
extractZip() && handleFile(uri) && patchBoot() && storeBoot() extractZip() && handleFile(uri) && patchBoot() && storeBoot()
} }
class SecondSlot( class SecondSlot(
file: Uri,
console: MutableList<String>, console: MutableList<String>,
logs: MutableList<String>, logs: MutableList<String>,
resultListener: FlashResultListener resultListener: FlashResultListener
) : Patching(console, logs, resultListener) { ) : Patching(file, console, logs, resultListener) {
override fun operations() = override fun operations() =
findSecondaryImage() && extractZip() && patchBoot() && flashBoot() && postOTA() findSecondaryImage() && extractZip() && patchBoot() && flashBoot() && postOTA()
} }
class Direct( class Direct(
file: Uri,
console: MutableList<String>, console: MutableList<String>,
logs: MutableList<String>, logs: MutableList<String>,
resultListener: FlashResultListener resultListener: FlashResultListener
) : Patching(console, logs, resultListener) { ) : Patching(file, console, logs, resultListener) {
override fun operations() = override fun operations() =
findImage() && extractZip() && patchBoot() && flashBoot() findImage() && extractZip() && patchBoot() && flashBoot()
} }

View File

@ -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<Context>().readUri(uri).use { input ->
zip.outputStream().use { out -> input.copyTo(out) }
}
}
}

View File

@ -4,9 +4,6 @@ import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.MainThread;
import androidx.annotation.WorkerThread;
import com.topjohnwu.magisk.App; import com.topjohnwu.magisk.App;
import com.topjohnwu.magisk.Const; import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Info; import com.topjohnwu.magisk.Info;
@ -41,13 +38,21 @@ import java.util.List;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; 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 { public abstract class MagiskInstaller {
protected String srcBoot; protected String srcBoot;
protected File destFile; protected File destFile;
protected File installDir; protected File installDir;
private List<String> console, logs; @Nullable
private final Uri preDownloadedFile;
private final List<String> console;
private final List<String> logs;
private boolean isTar = false; private boolean isTar = false;
private class ProgressLog implements DownloadProgressListener { private class ProgressLog implements DownloadProgressListener {
@ -72,14 +77,16 @@ public abstract class MagiskInstaller {
protected MagiskInstaller() { protected MagiskInstaller() {
console = NOPList.getInstance(); console = NOPList.getInstance();
logs = NOPList.getInstance(); logs = NOPList.getInstance();
preDownloadedFile = null;
} }
public MagiskInstaller(List<String> out, List<String> err) { public MagiskInstaller(List<String> out, List<String> err, @NonNull Uri magisk) {
console = out; console = out;
logs = err; logs = err;
installDir = new File(App.deContext.getFilesDir().getParent(), "install"); installDir = new File(App.deContext.getFilesDir().getParent(), "install");
Shell.sh("rm -rf " + installDir).exec(); Shell.sh("rm -rf " + installDir).exec();
installDir.mkdirs(); installDir.mkdirs();
preDownloadedFile = magisk;
} }
protected boolean findImage() { protected boolean findImage() {
@ -94,7 +101,7 @@ public abstract class MagiskInstaller {
protected boolean findSecondaryImage() { protected boolean findSecondaryImage() {
String slot = ShellUtils.fastCmd("echo $SLOT"); 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); console.add("- Target slot: " + target);
srcBoot = ShellUtils.fastCmd( srcBoot = ShellUtils.fastCmd(
"SLOT=" + target, "SLOT=" + target,
@ -122,14 +129,21 @@ public abstract class MagiskInstaller {
console.add("- Device platform: " + Build.CPU_ABI); console.add("- Device platform: " + Build.CPU_ABI);
File zip = new File(App.self.getCacheDir(), "magisk.zip"); 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())) { InstallerHelper.copyFileTo(preDownloadedFile, zip);
console.add("- Downloading zip");
Networking.get(Info.remote.getMagisk().getLink())
.setDownloadProgressListener(new ProgressLog())
.execForFile(zip);
} else { } 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 { try {
@ -151,7 +165,7 @@ public abstract class MagiskInstaller {
name = ze.getName(); name = ze.getName();
if (name == null) if (name == null)
continue; continue;
File dest = (installDir instanceof SuFile) ? File dest = installDir instanceof SuFile ?
new SuFile(installDir, name) : new SuFile(installDir, name) :
new File(installDir, name); new File(installDir, name);
dest.getParentFile().mkdirs(); dest.getParentFile().mkdirs();

View File

@ -28,7 +28,7 @@ import java.util.*
class FlashViewModel( class FlashViewModel(
action: String, action: String,
file: Uri, installer: Uri,
uri: Uri, uri: Uri,
private val resources: Resources private val resources: Resources
) : MagiskViewModel(), FlashResultListener { ) : MagiskViewModel(), FlashResultListener {
@ -55,19 +55,19 @@ class FlashViewModel(
when (action) { when (action) {
Const.Value.FLASH_ZIP -> Flashing Const.Value.FLASH_ZIP -> Flashing
.Install(uri, outItems, logItems, this) .Install(installer, outItems, logItems, this)
.exec() .exec()
Const.Value.UNINSTALL -> Flashing Const.Value.UNINSTALL -> Flashing
.Uninstall(uri, outItems, logItems, this) .Uninstall(installer, outItems, logItems, this)
.exec() .exec()
Const.Value.FLASH_MAGISK -> Patching Const.Value.FLASH_MAGISK -> Patching
.Direct(outItems, logItems, this) .Direct(installer, outItems, logItems, this)
.exec() .exec()
Const.Value.FLASH_INACTIVE_SLOT -> Patching Const.Value.FLASH_INACTIVE_SLOT -> Patching
.SecondSlot(outItems, logItems, this) .SecondSlot(installer, outItems, logItems, this)
.exec() .exec()
Const.Value.PATCH_FILE -> Patching Const.Value.PATCH_FILE -> Patching
.File(uri, outItems, logItems, this) .File(installer, uri, outItems, logItems, this)
.exec() .exec()
} }
} }