From c562cbc2bb49817033225fa30fa0a05a34485357 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 26 Sep 2017 20:46:58 +0800 Subject: [PATCH] Update zip and magisk installation --- .../com/topjohnwu/magisk/asyncs/FlashZip.java | 28 ++++++++----------- .../magisk/asyncs/InstallMagisk.java | 9 +++--- .../magisk/asyncs/RestoreStockBoot.java | 2 +- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/FlashZip.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/FlashZip.java index 2f310f0fb..687a644fc 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/FlashZip.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/FlashZip.java @@ -10,6 +10,8 @@ import com.topjohnwu.magisk.utils.AdaptiveList; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.ZipUtils; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; @@ -21,27 +23,19 @@ import java.util.List; public class FlashZip extends ParallelTask { private Uri mUri; - private File mCachedFile, mScriptFile, mCheckFile; - - private String mFilename; + private File mCachedFile; private AdaptiveList mList; public FlashZip(Activity context, Uri uri, AdaptiveList list) { super(context); mUri = uri; mList = list; - mCachedFile = new File(context.getCacheDir(), "install.zip"); - mScriptFile = new File(context.getCacheDir(), "/META-INF/com/google/android/update-binary"); - mCheckFile = new File(mScriptFile.getParent(), "updater-script"); - - // Try to get the filename ourselves - mFilename = Utils.getNameFromUri(context, mUri); } private boolean unzipAndCheck() throws Exception { - ZipUtils.unzip(mCachedFile, mCachedFile.getParentFile(), "META-INF/com/google/android", false); - List ret = Utils.readFile(getShell(), mCheckFile.getPath()); + ZipUtils.unzip(mCachedFile, mCachedFile.getParentFile(), "META-INF/com/google/android", true); + List ret = Utils.readFile(getShell(), new File(mCachedFile.getParentFile(), "updater-script").getPath()); return Utils.isValidShellResponse(ret) && ret.get(0).contains("#MAGISK"); } @@ -66,12 +60,13 @@ public class FlashZip extends ParallelTask { mCachedFile.delete(); try ( InputStream in = mm.getContentResolver().openInputStream(mUri); - OutputStream out = new FileOutputStream(mCachedFile) + OutputStream out = new BufferedOutputStream(new FileOutputStream(mCachedFile)) ) { if (in == null) throw new FileNotFoundException(); - byte buffer[] = new byte[1024]; + InputStream buf= new BufferedInputStream(in); + byte buffer[] = new byte[4096]; int length; - while ((length = in.read(buffer)) > 0) + while ((length = buf.read(buffer)) > 0) out.write(buffer, 0, length); } catch (FileNotFoundException e) { mList.add("! Invalid Uri"); @@ -81,9 +76,10 @@ public class FlashZip extends ParallelTask { throw e; } if (!unzipAndCheck()) return 0; - mList.add("- Installing " + mFilename); + mList.add("- Installing " + Utils.getNameFromUri(mm, mUri)); getShell().su(mList, - "BOOTMODE=true sh " + mScriptFile + " dummy 1 " + mCachedFile + + "cd " + mCachedFile.getParent(), + "BOOTMODE=true sh update-binary dummy 1 " + mCachedFile + " && echo 'Success!' || echo 'Failed!'" ); if (TextUtils.equals(mList.get(mList.size() - 1), "Success!")) diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java index 69b82febc..16143ae1b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java @@ -114,8 +114,8 @@ public class InstallMagisk extends ParallelTask { boot = new File(install, "boot.img"); // Copy boot image to local try ( - InputStream in = mm.getContentResolver().openInputStream(mBootImg); - OutputStream out = new FileOutputStream(boot) + InputStream in = mm.getContentResolver().openInputStream(mBootImg); + OutputStream out = new FileOutputStream(boot) ) { InputStream source; if (in == null) throw new FileNotFoundException(); @@ -131,7 +131,7 @@ public class InstallMagisk extends ParallelTask { source = tar; } else { // Direct copy raw image - source = in; + source = new BufferedInputStream(in); } byte buffer[] = new byte[1024]; int length; @@ -204,8 +204,7 @@ public class InstallMagisk extends ParallelTask { break; case DIRECT_MODE: // Direct flash boot image - getShell().su_raw("cat " + patched_boot + " /dev/zero | dd of=" + mBootLocation + " bs=4096"); - mList.add("Flashing patched boot to " + mBootLocation); + getShell().su(mList, "flash_boot_image " + patched_boot + " " + mBootLocation); break; default: return false; diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/RestoreStockBoot.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/RestoreStockBoot.java index d3a0b9406..267157680 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/RestoreStockBoot.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/RestoreStockBoot.java @@ -25,7 +25,7 @@ public class RestoreStockBoot extends ParallelTask { String stock_boot = "/data/stock_boot_" + ret.get(0).substring(ret.get(0).indexOf('=') + 1) + ".img.gz"; if (!Utils.itemExist(getShell(), stock_boot)) return false; - getShell().su_raw("gzip -d < " + stock_boot + " | cat - /dev/zero | dd of=" + mBoot + " bs=4096"); + getShell().su_raw("flash_boot_image " + stock_boot + " " + mBoot); return true; }