Don't use DownloadManager for Magisk

This commit is contained in:
topjohnwu 2018-07-29 22:58:22 +08:00
parent 89861eceef
commit 40b63bfebe
7 changed files with 109 additions and 99 deletions

View File

@ -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;
}

View File

@ -32,7 +32,7 @@ public class CheckSafetyNet extends ParallelTask<Void, Void, Exception> {
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())) {

View File

@ -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<Void, Void, Boolean> {
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<String> console, logs;
private String mBoot;
private int mode;
@ -52,22 +56,21 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
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<String> console, List<String> logs, Uri zip, int mode) {
this(context, zip);
public InstallMagisk(Activity context, List<String> console, List<String> logs, int mode) {
this(context);
this.console = console;
this.logs = logs;
this.mode = mode;
}
public InstallMagisk(FlashActivity context, List<String> console, List<String> logs, Uri zip, Uri boot) {
this(context, console, logs, zip, PATCH_MODE);
public InstallMagisk(FlashActivity context, List<String> console, List<String> logs, Uri boot) {
this(context, console, logs, PATCH_MODE);
bootUri = boot;
}
@ -80,23 +83,69 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
}
}
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;

View File

@ -87,15 +87,8 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
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<Void, Object, Boolean> {
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

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -69,4 +69,17 @@ public class WebService {
return conn;
}
public static HttpURLConnection mustRequest(String address, Map<String, String> 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;
}
}