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(); new UninstallMagisk(this, uri, console, logs).exec();
break; break;
case Const.Value.FLASH_MAGISK: 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; break;
case Const.Value.FLASH_INACTIVE_SLOT: 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; break;
case Const.Value.PATCH_BOOT: case Const.Value.PATCH_BOOT:
new InstallMagisk(this, console, logs, uri, new InstallMagisk(this, console, logs,
intent.getParcelableExtra(Const.Key.FLASH_SET_BOOT)).exec(); intent.getParcelableExtra(Const.Key.FLASH_SET_BOOT)).exec();
break; break;
} }

View File

@ -32,7 +32,7 @@ public class CheckSafetyNet extends ParallelTask<Void, Void, Exception> {
private void dlSnet() throws Exception { private void dlSnet() throws Exception {
Shell.sh("rm -rf " + dexPath.getParent()).exec(); Shell.sh("rm -rf " + dexPath.getParent()).exec();
dexPath.getParentFile().mkdir(); dexPath.getParentFile().mkdir();
HttpURLConnection conn = WebService.request(Const.Url.SNET_URL, null); HttpURLConnection conn = WebService.mustRequest(Const.Url.SNET_URL, null);
try ( try (
OutputStream out = new BufferedOutputStream(new FileOutputStream(dexPath)); OutputStream out = new BufferedOutputStream(new FileOutputStream(dexPath));
InputStream in = new BufferedInputStream(conn.getInputStream())) { InputStream in = new BufferedInputStream(conn.getInputStream())) {

View File

@ -4,6 +4,7 @@ import android.app.Activity;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.support.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View; import android.view.View;
import android.widget.Toast; import android.widget.Toast;
@ -14,6 +15,7 @@ import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.container.TarEntry; import com.topjohnwu.magisk.container.TarEntry;
import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.WebService;
import com.topjohnwu.magisk.utils.ZipUtils; import com.topjohnwu.magisk.utils.ZipUtils;
import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils; import com.topjohnwu.superuser.ShellUtils;
@ -31,9 +33,11 @@ import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -44,7 +48,7 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
private static final int FIX_ENV_MODE = 2; private static final int FIX_ENV_MODE = 2;
public static final int SECOND_SLOT_MODE = 3; public static final int SECOND_SLOT_MODE = 3;
private Uri bootUri, mZip; private Uri bootUri;
private List<String> console, logs; private List<String> console, logs;
private String mBoot; private String mBoot;
private int mode; private int mode;
@ -52,22 +56,21 @@ public class InstallMagisk extends ParallelTask<Void, Void, Boolean> {
private ProgressDialog dialog; private ProgressDialog dialog;
private MagiskManager mm; private MagiskManager mm;
public InstallMagisk(Activity context, Uri zip) { public InstallMagisk(Activity context) {
super(context); super(context);
mZip = zip;
mm = MagiskManager.get(); mm = MagiskManager.get();
mode = FIX_ENV_MODE; mode = FIX_ENV_MODE;
} }
public InstallMagisk(Activity context, List<String> console, List<String> logs, Uri zip, int mode) { public InstallMagisk(Activity context, List<String> console, List<String> logs, int mode) {
this(context, zip); this(context);
this.console = console; this.console = console;
this.logs = logs; this.logs = logs;
this.mode = mode; this.mode = mode;
} }
public InstallMagisk(FlashActivity context, List<String> console, List<String> logs, Uri zip, Uri boot) { public InstallMagisk(FlashActivity context, List<String> console, List<String> logs, Uri boot) {
this(context, console, logs, zip, PATCH_MODE); this(context, console, logs, PATCH_MODE);
bootUri = boot; 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 { 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"); console.add("- Extracting files");
try (InputStream in = mm.getContentResolver().openInputStream(mZip)) { try (InputStream in = buf) {
if (in == null) throw new FileNotFoundException(); ZipUtils.unzip(in, installDir, arch + "/", true);
BufferedInputStream buf = new BufferedInputStream(in); in.reset();
buf.mark(Integer.MAX_VALUE); ZipUtils.unzip(in, installDir, "common/", true);
ZipUtils.unzip(buf, installDir, arch + "/", true); in.reset();
buf.reset(); ZipUtils.unzip(in, installDir, "chromeos/", false);
ZipUtils.unzip(buf, installDir, "common/", true); in.reset();
buf.reset(); ZipUtils.unzip(in, installDir, "META-INF/com/google/android/update-binary", true);
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;
} catch (IOException e) { } catch (IOException e) {
console.add("! Cannot unzip zip"); console.add("! Cannot unzip zip");
throw e; throw e;

View File

@ -87,15 +87,8 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
if (activity == null) return null; if (activity == null) return null;
try { try {
// Request zip from Internet // Request zip from Internet
HttpURLConnection conn; HttpURLConnection conn = WebService.mustRequest(mLink, null);
do { total = conn.getContentLength();
conn = WebService.request(mLink, null);
total = conn.getContentLength();
if (total < 0)
conn.disconnect();
else
break;
} while (true);
// Temp files // Temp files
File temp1 = new File(activity.getCacheDir(), "1.zip"); 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) { private void updateDlProgress(int step) {
progress += 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 @Override

View File

@ -1,33 +1,19 @@
package com.topjohnwu.magisk.components; package com.topjohnwu.magisk.components;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.asyncs.InstallMagisk; import com.topjohnwu.magisk.asyncs.InstallMagisk;
import com.topjohnwu.magisk.receivers.DownloadReceiver;
import com.topjohnwu.magisk.utils.Utils;
public class EnvFixDialog extends CustomAlertDialog { public class EnvFixDialog extends CustomAlertDialog {
public EnvFixDialog(@NonNull Activity activity) { public EnvFixDialog(@NonNull Activity activity) {
super(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); setTitle(R.string.env_fix_title);
setMessage(R.string.env_fix_msg); setMessage(R.string.env_fix_msg);
setCancelable(true); setCancelable(true);
setPositiveButton(R.string.yes, (d, i) -> Utils.dlAndReceive(activity, setPositiveButton(R.string.yes, (d, i) -> new InstallMagisk(activity).exec());
new DownloadReceiver() {
@Override
public void onDownloadDone(Context context, Uri uri) {
new InstallMagisk(activity, uri).exec();
}
}, mm.magiskLink, filename));
setNegativeButton(R.string.no_thanks, null); setNegativeButton(R.string.no_thanks, null);
} }
} }

View File

@ -23,7 +23,7 @@ class InstallMethodDialog extends AlertDialog.Builder {
MagiskManager mm = Utils.getMagiskManager(activity); MagiskManager mm = Utils.getMagiskManager(activity);
setTitle(R.string.select_method); setTitle(R.string.select_method);
setItems(options.toArray(new String [0]), (dialog, idx) -> { setItems(options.toArray(new String [0]), (dialog, idx) -> {
DownloadReceiver receiver = null; Intent intent;
switch (idx) { switch (idx) {
case 1: case 1:
if (mm.remoteMagiskVersionCode < 1400) { if (mm.remoteMagiskVersionCode < 1400) {
@ -32,70 +32,38 @@ class InstallMethodDialog extends AlertDialog.Builder {
return; return;
} }
MagiskManager.toast(R.string.boot_file_patch_msg, Toast.LENGTH_LONG); MagiskManager.toast(R.string.boot_file_patch_msg, Toast.LENGTH_LONG);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent = new Intent(Intent.ACTION_GET_CONTENT).setType("*/*");
intent.setType("*/*");
activity.startActivityForResult(intent, Const.ID.SELECT_BOOT, activity.startActivityForResult(intent, Const.ID.SELECT_BOOT,
(requestCode, resultCode, data) -> { (requestCode, resultCode, data) -> {
if (requestCode == Const.ID.SELECT_BOOT && if (requestCode == Const.ID.SELECT_BOOT &&
resultCode == Activity.RESULT_OK && data != null) { resultCode == Activity.RESULT_OK && data != null) {
Utils.dlAndReceive(activity, new SelectBoot(data), Intent i = new Intent(activity, FlashActivity.class)
mm.magiskLink, filename); .putExtra(Const.Key.FLASH_SET_BOOT, data.getData())
.putExtra(Const.Key.FLASH_ACTION, Const.Value.PATCH_BOOT);
activity.startActivity(i);
} }
}); });
return; break;
case 0: case 0:
receiver = new DownloadReceiver() { Utils.dlAndReceive(activity, new DownloadReceiver() {
@Override @Override
public void onDownloadDone(Context context, Uri uri) { public void onDownloadDone(Context context, Uri uri) {
SnackbarMaker.showUri(activity, uri); SnackbarMaker.showUri(activity, uri);
} }
}; }, mm.magiskLink, filename);
break; break;
case 2: case 2:
receiver = new DownloadReceiver() { intent = new Intent(activity, FlashActivity.class)
@Override .putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_MAGISK);
public void onDownloadDone(Context context, Uri uri) { activity.startActivity(intent);
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);
}
};
break; break;
case 3: case 3:
receiver = new DownloadReceiver() { intent = new Intent(activity, FlashActivity.class)
@Override .putExtra(Const.Key.FLASH_ACTION, Const.Value.FLASH_INACTIVE_SLOT);
public void onDownloadDone(Context context, Uri uri) { activity.startActivity(intent);
Intent intent = new Intent(context, FlashActivity.class); break;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.setData(uri).putExtra(Const.Key.FLASH_ACTION,
Const.Value.FLASH_INACTIVE_SLOT);
context.startActivity(intent);
}
};
default: 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; 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;
}
} }