diff --git a/app/src/full/java/com/topjohnwu/magisk/FlashActivity.java b/app/src/full/java/com/topjohnwu/magisk/FlashActivity.java index e383e2e81..70b3916b1 100644 --- a/app/src/full/java/com/topjohnwu/magisk/FlashActivity.java +++ b/app/src/full/java/com/topjohnwu/magisk/FlashActivity.java @@ -119,13 +119,13 @@ public class FlashActivity extends Activity { new UninstallMagisk(this, uri, console, logs).exec(); break; case Const.Value.FLASH_MAGISK: - new InstallMagisk(this, console, logs, uri, InstallMagisk.DIRECT_MODE).exec(); + new InstallMagisk(this, console, logs, InstallMagisk.DIRECT_MODE).exec(); break; case Const.Value.FLASH_INACTIVE_SLOT: - new InstallMagisk(this, console, logs, uri, InstallMagisk.SECOND_SLOT_MODE).exec(); + new InstallMagisk(this, console, logs, InstallMagisk.SECOND_SLOT_MODE).exec(); break; case Const.Value.PATCH_BOOT: - new InstallMagisk(this, console, logs, uri, + new InstallMagisk(this, console, logs, intent.getParcelableExtra(Const.Key.FLASH_SET_BOOT)).exec(); break; } diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/CheckSafetyNet.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/CheckSafetyNet.java index 7e21f3d2b..8425000a0 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/CheckSafetyNet.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/CheckSafetyNet.java @@ -32,7 +32,7 @@ public class CheckSafetyNet extends ParallelTask { private void dlSnet() throws Exception { Shell.sh("rm -rf " + dexPath.getParent()).exec(); dexPath.getParentFile().mkdir(); - HttpURLConnection conn = WebService.request(Const.Url.SNET_URL, null); + HttpURLConnection conn = WebService.mustRequest(Const.Url.SNET_URL, null); try ( OutputStream out = new BufferedOutputStream(new FileOutputStream(dexPath)); InputStream in = new BufferedInputStream(conn.getInputStream())) { diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java index fab7fcff7..d1c89c84e 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.app.ProgressDialog; import android.net.Uri; import android.os.Build; +import android.support.annotation.NonNull; import android.text.TextUtils; import android.view.View; import android.widget.Toast; @@ -14,6 +15,7 @@ import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.container.TarEntry; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Utils; +import com.topjohnwu.magisk.utils.WebService; import com.topjohnwu.magisk.utils.ZipUtils; import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.ShellUtils; @@ -31,9 +33,11 @@ import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.HttpURLConnection; import java.util.Arrays; import java.util.List; @@ -44,7 +48,7 @@ public class InstallMagisk extends ParallelTask { private static final int FIX_ENV_MODE = 2; public static final int SECOND_SLOT_MODE = 3; - private Uri bootUri, mZip; + private Uri bootUri; private List console, logs; private String mBoot; private int mode; @@ -52,22 +56,21 @@ public class InstallMagisk extends ParallelTask { private ProgressDialog dialog; private MagiskManager mm; - public InstallMagisk(Activity context, Uri zip) { + public InstallMagisk(Activity context) { super(context); - mZip = zip; mm = MagiskManager.get(); mode = FIX_ENV_MODE; } - public InstallMagisk(Activity context, List console, List logs, Uri zip, int mode) { - this(context, zip); + public InstallMagisk(Activity context, List console, List logs, int mode) { + this(context); this.console = console; this.logs = logs; this.mode = mode; } - public InstallMagisk(FlashActivity context, List console, List logs, Uri zip, Uri boot) { - this(context, console, logs, zip, PATCH_MODE); + public InstallMagisk(FlashActivity context, List console, List logs, Uri boot) { + this(context, console, logs, PATCH_MODE); bootUri = boot; } @@ -80,23 +83,69 @@ public class InstallMagisk extends ParallelTask { } } + private class ProgressStream extends FilterInputStream { + + private int prev = -1; + private int progress = 0; + private int total; + + private ProgressStream(HttpURLConnection conn) throws IOException { + super(conn.getInputStream()); + total = conn.getContentLength(); + } + + private void update(int step) { + progress += step; + int curr = (int) (10 * (double) progress / total); + if (prev != curr) { + prev = curr; + console.add("... " + prev * 10 + "%"); + } + } + + @Override + public int read() throws IOException { + int b = super.read(); + if (b > 0) + update(1); + return b; + } + + @Override + public int read(@NonNull byte[] b) throws IOException { + return read(b, 0, b.length); + } + + @Override + public int read(@NonNull byte[] b, int off, int len) throws IOException { + int step = super.read(b, off, len); + if (step > 0) + update(step); + return step; + } + } + private void extractFiles(String arch) throws IOException { + console.add("- Downloading zip"); + String filename = Utils.fmt("Magisk-v%s(%d).zip", + mm.remoteMagiskVersionString, mm.remoteMagiskVersionCode); + HttpURLConnection conn = WebService.mustRequest(mm.magiskLink, null); + BufferedInputStream buf = new BufferedInputStream(new ProgressStream(conn)); + buf.mark(Integer.MAX_VALUE); + try (OutputStream out = new FileOutputStream(new File(Const.EXTERNAL_PATH, filename))) { + ShellUtils.pump(buf, out); + buf.reset(); + } + conn.disconnect(); console.add("- Extracting files"); - try (InputStream in = mm.getContentResolver().openInputStream(mZip)) { - if (in == null) throw new FileNotFoundException(); - BufferedInputStream buf = new BufferedInputStream(in); - buf.mark(Integer.MAX_VALUE); - ZipUtils.unzip(buf, installDir, arch + "/", true); - buf.reset(); - ZipUtils.unzip(buf, installDir, "common/", true); - buf.reset(); - ZipUtils.unzip(buf, installDir, "chromeos/", false); - buf.reset(); - ZipUtils.unzip(buf, installDir, "META-INF/com/google/android/update-binary", true); - buf.close(); - } catch (FileNotFoundException e) { - console.add("! Invalid Uri"); - throw e; + try (InputStream in = buf) { + ZipUtils.unzip(in, installDir, arch + "/", true); + in.reset(); + ZipUtils.unzip(in, installDir, "common/", true); + in.reset(); + ZipUtils.unzip(in, installDir, "chromeos/", false); + in.reset(); + ZipUtils.unzip(in, installDir, "META-INF/com/google/android/update-binary", true); } catch (IOException e) { console.add("! Cannot unzip zip"); throw e; diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java index f0f564b68..683030fee 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java @@ -87,15 +87,8 @@ public class ProcessRepoZip extends ParallelTask { if (activity == null) return null; try { // Request zip from Internet - HttpURLConnection conn; - do { - conn = WebService.request(mLink, null); - total = conn.getContentLength(); - if (total < 0) - conn.disconnect(); - else - break; - } while (true); + HttpURLConnection conn = WebService.mustRequest(mLink, null); + total = conn.getContentLength(); // Temp files File temp1 = new File(activity.getCacheDir(), "1.zip"); @@ -170,7 +163,8 @@ public class ProcessRepoZip extends ParallelTask { private void updateDlProgress(int step) { progress += step; - progressDialog.setMessage(getActivity().getString(R.string.zip_download_msg, (int) (100 * (double) progress / total + 0.5))); + progressDialog.setMessage(getActivity().getString(R.string.zip_download_msg, + (int) (100 * (double) progress / total + 0.5))); } @Override diff --git a/app/src/full/java/com/topjohnwu/magisk/components/EnvFixDialog.java b/app/src/full/java/com/topjohnwu/magisk/components/EnvFixDialog.java index 2a3afe31f..eff0d16de 100644 --- a/app/src/full/java/com/topjohnwu/magisk/components/EnvFixDialog.java +++ b/app/src/full/java/com/topjohnwu/magisk/components/EnvFixDialog.java @@ -1,33 +1,19 @@ package com.topjohnwu.magisk.components; import android.app.Activity; -import android.content.Context; -import android.net.Uri; import android.support.annotation.NonNull; -import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.asyncs.InstallMagisk; -import com.topjohnwu.magisk.receivers.DownloadReceiver; -import com.topjohnwu.magisk.utils.Utils; public class EnvFixDialog extends CustomAlertDialog { public EnvFixDialog(@NonNull Activity activity) { super(activity); - MagiskManager mm = Utils.getMagiskManager(activity); - String filename = Utils.fmt("Magisk-v%s(%d).zip", - mm.remoteMagiskVersionString, mm.remoteMagiskVersionCode); setTitle(R.string.env_fix_title); setMessage(R.string.env_fix_msg); setCancelable(true); - setPositiveButton(R.string.yes, (d, i) -> Utils.dlAndReceive(activity, - new DownloadReceiver() { - @Override - public void onDownloadDone(Context context, Uri uri) { - new InstallMagisk(activity, uri).exec(); - } - }, mm.magiskLink, filename)); + setPositiveButton(R.string.yes, (d, i) -> new InstallMagisk(activity).exec()); setNegativeButton(R.string.no_thanks, null); } } diff --git a/app/src/full/java/com/topjohnwu/magisk/components/InstallMethodDialog.java b/app/src/full/java/com/topjohnwu/magisk/components/InstallMethodDialog.java index 66d2aad90..b609fa53e 100644 --- a/app/src/full/java/com/topjohnwu/magisk/components/InstallMethodDialog.java +++ b/app/src/full/java/com/topjohnwu/magisk/components/InstallMethodDialog.java @@ -23,7 +23,7 @@ class InstallMethodDialog extends AlertDialog.Builder { MagiskManager mm = Utils.getMagiskManager(activity); setTitle(R.string.select_method); setItems(options.toArray(new String [0]), (dialog, idx) -> { - DownloadReceiver receiver = null; + Intent intent; switch (idx) { case 1: if (mm.remoteMagiskVersionCode < 1400) { @@ -32,70 +32,38 @@ class InstallMethodDialog extends AlertDialog.Builder { return; } MagiskManager.toast(R.string.boot_file_patch_msg, Toast.LENGTH_LONG); - Intent intent = new Intent(Intent.ACTION_GET_CONTENT); - intent.setType("*/*"); + intent = new Intent(Intent.ACTION_GET_CONTENT).setType("*/*"); activity.startActivityForResult(intent, Const.ID.SELECT_BOOT, (requestCode, resultCode, data) -> { if (requestCode == Const.ID.SELECT_BOOT && resultCode == Activity.RESULT_OK && data != null) { - Utils.dlAndReceive(activity, new SelectBoot(data), - mm.magiskLink, filename); + Intent i = new Intent(activity, FlashActivity.class) + .putExtra(Const.Key.FLASH_SET_BOOT, data.getData()) + .putExtra(Const.Key.FLASH_ACTION, Const.Value.PATCH_BOOT); + activity.startActivity(i); } }); - return; + break; case 0: - receiver = new DownloadReceiver() { + Utils.dlAndReceive(activity, new DownloadReceiver() { @Override public void onDownloadDone(Context context, Uri uri) { SnackbarMaker.showUri(activity, uri); } - }; + }, mm.magiskLink, filename); break; case 2: - receiver = new DownloadReceiver() { - @Override - public void onDownloadDone(Context context, Uri uri) { - Intent intent = new Intent(context, FlashActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .setData(uri).putExtra(Const.Key.FLASH_ACTION, - Const.Value.FLASH_MAGISK); - context.startActivity(intent); - } - }; + intent = new Intent(activity, FlashActivity.class) + .putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_MAGISK); + activity.startActivity(intent); break; case 3: - receiver = new DownloadReceiver() { - @Override - public void onDownloadDone(Context context, Uri uri) { - Intent intent = new Intent(context, FlashActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .setData(uri).putExtra(Const.Key.FLASH_ACTION, - Const.Value.FLASH_INACTIVE_SLOT); - context.startActivity(intent); - } - }; + intent = new Intent(activity, FlashActivity.class) + .putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_INACTIVE_SLOT); + activity.startActivity(intent); + break; default: } - Utils.dlAndReceive(activity, receiver, mm.magiskLink, filename); }); } - - private static class SelectBoot extends DownloadReceiver { - - private Intent data; - - public SelectBoot(Intent data) { - this.data = data; - } - - @Override - public void onDownloadDone(Context context, Uri uri) { - Intent intent = new Intent(context, FlashActivity.class); - intent.setData(uri) - .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .putExtra(Const.Key.FLASH_SET_BOOT, data.getData()) - .putExtra(Const.Key.FLASH_ACTION, Const.Value.PATCH_BOOT); - context.startActivity(intent); - } - } } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/WebService.java b/app/src/main/java/com/topjohnwu/magisk/utils/WebService.java index df578b0e6..68e29d6a2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/WebService.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/WebService.java @@ -69,4 +69,17 @@ public class WebService { return conn; } + + public static HttpURLConnection mustRequest(String address, Map header) throws IOException { + HttpURLConnection conn; + do { + conn = WebService.request(address, header); + int total = conn.getContentLength(); + if (total < 0) + conn.disconnect(); + else + break; + } while (true); + return conn; + } }