Skip to content

Commit

Permalink
Updated magisk installer so it uses predownloaded file
Browse files Browse the repository at this point in the history
  • Loading branch information
diareuse authored and topjohnwu committed Jul 20, 2019
1 parent 88a3948 commit 6a0f6ab
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
12 changes: 8 additions & 4 deletions app/src/main/java/com/topjohnwu/magisk/model/flash/Patching.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import com.topjohnwu.magisk.tasks.MagiskInstaller
import com.topjohnwu.superuser.Shell

sealed class Patching(
file: Uri,
private val console: MutableList<String>,
logs: MutableList<String>,
private val resultListener: FlashResultListener
) : MagiskInstaller(console, logs) {
) : MagiskInstaller(console, logs, file) {

override fun onResult(success: Boolean) {
if (success) {
Expand All @@ -21,29 +22,32 @@ sealed class Patching(
}

class File(
file: Uri,
private val uri: Uri,
console: MutableList<String>,
logs: MutableList<String>,
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<String>,
logs: MutableList<String>,
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<String>,
logs: MutableList<String>,
resultListener: FlashResultListener
) : Patching(console, logs, resultListener) {
) : Patching(file, console, logs, resultListener) {
override fun operations() =
findImage() && extractZip() && patchBoot() && flashBoot()
}
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/topjohnwu/magisk/tasks/InstallerHelper.kt
Original file line number Diff line number Diff line change
@@ -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) }
}
}
}
40 changes: 27 additions & 13 deletions app/src/main/java/com/topjohnwu/magisk/tasks/MagiskInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
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;
Expand Down Expand Up @@ -41,13 +38,21 @@
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<String> console, logs;
@Nullable
private final Uri preDownloadedFile;
private final List<String> console;
private final List<String> logs;
private boolean isTar = false;

private class ProgressLog implements DownloadProgressListener {
Expand All @@ -72,14 +77,16 @@ public void onProgress(long bytesDownloaded, long totalBytes) {
protected MagiskInstaller() {
console = 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;
logs = err;
installDir = new File(App.deContext.getFilesDir().getParent(), "install");
Shell.sh("rm -rf " + installDir).exec();
installDir.mkdirs();
preDownloadedFile = magisk;
}

protected boolean findImage() {
Expand All @@ -94,7 +101,7 @@ protected boolean findImage() {

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,
Expand Down Expand Up @@ -122,14 +129,21 @@ protected boolean extractZip() {
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 {
Expand All @@ -151,7 +165,7 @@ protected boolean extractZip() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import java.util.*

class FlashViewModel(
action: String,
file: Uri,
installer: Uri,
uri: Uri,
private val resources: Resources
) : MagiskViewModel(), FlashResultListener {
Expand All @@ -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()
}
}
Expand Down

0 comments on commit 6a0f6ab

Please sign in to comment.