Merge download and process repo modules

This commit is contained in:
topjohnwu 2017-09-03 22:10:54 +08:00
parent ec0af99a2e
commit 926c7359a2
4 changed files with 62 additions and 50 deletions

View File

@ -3,7 +3,6 @@ package com.topjohnwu.magisk.adapters;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Pair; import android.util.Pair;
@ -21,7 +20,6 @@ import com.topjohnwu.magisk.components.AlertDialogBuilder;
import com.topjohnwu.magisk.database.RepoDatabaseHelper; import com.topjohnwu.magisk.database.RepoDatabaseHelper;
import com.topjohnwu.magisk.module.Module; import com.topjohnwu.magisk.module.Module;
import com.topjohnwu.magisk.module.Repo; import com.topjohnwu.magisk.module.Repo;
import com.topjohnwu.magisk.receivers.DownloadReceiver;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import java.util.ArrayList; import java.util.ArrayList;
@ -106,26 +104,13 @@ public class ReposAdapter extends SectionedAdapter<ReposAdapter.SectionHolder, R
.setTitle(context.getString(R.string.repo_install_title, repo.getName())) .setTitle(context.getString(R.string.repo_install_title, repo.getName()))
.setMessage(context.getString(R.string.repo_install_msg, filename)) .setMessage(context.getString(R.string.repo_install_msg, filename))
.setCancelable(true) .setCancelable(true)
.setPositiveButton(R.string.install, (d, i) -> Utils.dlAndReceive( .setPositiveButton(R.string.install, (d, i) ->
context, new ProcessRepoZip((Activity) context, repo.getZipUrl(),
new DownloadReceiver() { Utils.getLegalFilename(filename), true).exec()
@Override )
public void onDownloadDone(Uri uri) { .setNeutralButton(R.string.download, (d, i) ->
new ProcessRepoZip((Activity) context, uri, true).exec(); new ProcessRepoZip((Activity) context, repo.getZipUrl(),
} Utils.getLegalFilename(filename), false).exec())
},
repo.getZipUrl(),
Utils.getLegalFilename(filename)))
.setNeutralButton(R.string.download, (d, i) -> Utils.dlAndReceive(
context,
new DownloadReceiver() {
@Override
public void onDownloadDone(Uri uri) {
new ProcessRepoZip((Activity) context, uri, false).exec();
}
},
repo.getZipUrl(),
Utils.getLegalFilename(filename)))
.setNegativeButton(R.string.no_thanks, null) .setNegativeButton(R.string.no_thanks, null)
.show(); .show();
}); });

View File

@ -1,9 +1,11 @@
package com.topjohnwu.magisk.asyncs; package com.topjohnwu.magisk.asyncs;
import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Environment;
import android.widget.Toast; import android.widget.Toast;
import com.topjohnwu.magisk.FlashActivity; import com.topjohnwu.magisk.FlashActivity;
@ -11,32 +13,42 @@ import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Logger; import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Shell;
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 java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> { public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
private Uri mUri;
private ProgressDialog progressDialog; private ProgressDialog progressDialog;
private boolean mInstall; private boolean mInstall;
private String mLink, mFile;
public ProcessRepoZip(Activity context, Uri uri, boolean install) { public ProcessRepoZip(Activity context, String link, String filename, boolean install) {
super(context); super(context);
mUri = uri; mLink = link;
mFile = Environment.getExternalStorageDirectory() + "/MagiskManager/" + filename;
mInstall = install; mInstall = install;
} }
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity == null) return;
progressDialog = ProgressDialog.show(activity, progressDialog = ProgressDialog.show(activity,
activity.getString(R.string.zip_process_title), activity.getString(R.string.zip_download_title),
activity.getString(R.string.zip_process_msg)); activity.getString(R.string.zip_download_msg));
}
@Override
protected void onProgressUpdate(Void... values) {
progressDialog.setTitle(R.string.zip_process_title);
progressDialog.setMessage(getActivity().getString(R.string.zip_process_msg));
} }
@Override @Override
@ -45,15 +57,20 @@ public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
if (activity == null) return null; if (activity == null) return null;
try { try {
// Create temp file // Request zip from Internet
File temp1 = new File(activity.getCacheDir(), "1.zip"); InputStream in = WebService.request(WebService.GET, mLink, null);
File temp2 = new File(activity.getCacheDir(), "2.zip"); if (in == null) return false;
activity.getCacheDir().mkdirs(); in = new BufferedInputStream(in);
temp1.createNewFile();
temp2.createNewFile();
// First remove top folder in Github source zip, Uri -> temp1 // Temp files
ZipUtils.removeTopFolder(activity.getContentResolver().openInputStream(mUri), temp1); File temp1 = new File(activity.getCacheDir(), "1.zip");
File temp2 = new File(temp1.getParentFile(), "2.zip");
temp1.getParentFile().mkdir();
// First remove top folder in Github source zip, Web -> temp1
ZipUtils.removeTopFolder(in, temp1);
publishProgress();
// Then sign the zip for the first time, temp1 -> temp2 // Then sign the zip for the first time, temp1 -> temp2
ZipUtils.signZip(activity, temp1, temp2, false); ZipUtils.signZip(activity, temp1, temp2, false);
@ -64,18 +81,17 @@ public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
// Finally, sign the whole zip file again, temp1 -> temp2 // Finally, sign the whole zip file again, temp1 -> temp2
ZipUtils.signZip(activity, temp1, temp2, true); ZipUtils.signZip(activity, temp1, temp2, true);
// Write it back to the downloaded zip, temp2 -> Uri // Write it to the target zip, temp2 -> file
try (OutputStream out = activity.getContentResolver().openOutputStream(mUri); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(mFile));
FileInputStream in = new FileInputStream(temp2) InputStream source = new BufferedInputStream(new FileInputStream(temp2))
) { ) {
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
int length; int length;
if (out == null) throw new FileNotFoundException(); while ((length = source.read(buffer)) > 0)
while ((length = in.read(buffer)) > 0)
out.write(buffer, 0, length); out.write(buffer, 0, length);
} }
// Delete the temp file // Delete temp files
temp1.delete(); temp1.delete();
temp2.delete(); temp2.delete();
@ -92,17 +108,24 @@ public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity == null) return; if (activity == null) return;
progressDialog.dismiss(); progressDialog.dismiss();
Uri uri = Uri.fromFile(new File(mFile));
if (result) { if (result) {
if (Shell.rootAccess() && mInstall) { if (Shell.rootAccess() && mInstall) {
Intent intent = new Intent(getActivity(), FlashActivity.class); Intent intent = new Intent(getActivity(), FlashActivity.class);
intent.setData(mUri).putExtra(FlashActivity.SET_ACTION, FlashActivity.FLASH_ZIP); intent.setData(uri).putExtra(FlashActivity.SET_ACTION, FlashActivity.FLASH_ZIP);
activity.startActivity(intent); activity.startActivity(intent);
} else { } else {
Utils.showUriSnack(activity, mUri); Utils.showUriSnack(activity, uri);
} }
} else { } else {
Utils.getMagiskManager(activity).toast(R.string.process_error, Toast.LENGTH_LONG); Utils.getMagiskManager(activity).toast(R.string.process_error, Toast.LENGTH_LONG);
} }
super.onPostExecute(result); super.onPostExecute(result);
} }
@Override
public ParallelTask<Void, Void, Boolean> exec(Void... voids) {
Utils.runWithPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, () -> super.exec(voids));
return this;
}
} }

View File

@ -22,6 +22,8 @@ import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder; import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;
import org.bouncycastle.util.encoders.Base64; import org.bouncycastle.util.encoders.Base64;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -126,7 +128,7 @@ public class ZipUtils {
public static void removeTopFolder(InputStream in, File output) throws IOException { public static void removeTopFolder(InputStream in, File output) throws IOException {
try { try {
JarInputStream source = new JarInputStream(in); JarInputStream source = new JarInputStream(in);
JarOutputStream dest = new JarOutputStream(new FileOutputStream(output)); JarOutputStream dest = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(output)));
JarEntry entry; JarEntry entry;
String path; String path;
int size; int size;
@ -157,7 +159,7 @@ public class ZipUtils {
} }
public static void unzip(File zip, File folder, String path, boolean junkPath) throws Exception { public static void unzip(File zip, File folder, String path, boolean junkPath) throws Exception {
InputStream in = new FileInputStream(zip); InputStream in = new BufferedInputStream(new FileInputStream(zip));
unzip(in, folder, path, junkPath); unzip(in, folder, path, junkPath);
in.close(); in.close();
} }
@ -198,7 +200,7 @@ public class ZipUtils {
public static void signZip(Context context, File input, File output, boolean minSign) { public static void signZip(Context context, File input, File output, boolean minSign) {
int alignment = 4; int alignment = 4;
JarFile inputJar = null; JarFile inputJar = null;
FileOutputStream outputFile = null; BufferedOutputStream outputFile = null;
int hashes = 0; int hashes = 0;
try { try {
X509Certificate publicKey = readPublicKey(context.getAssets().open(PUBLIC_KEY_NAME)); X509Certificate publicKey = readPublicKey(context.getAssets().open(PUBLIC_KEY_NAME));
@ -210,7 +212,7 @@ public class ZipUtils {
long timestamp = publicKey.getNotBefore().getTime() + 3600L * 1000; long timestamp = publicKey.getNotBefore().getTime() + 3600L * 1000;
PrivateKey privateKey = readPrivateKey(context.getAssets().open(PRIVATE_KEY_NAME)); PrivateKey privateKey = readPrivateKey(context.getAssets().open(PRIVATE_KEY_NAME));
outputFile = new FileOutputStream(output); outputFile = new BufferedOutputStream(new FileOutputStream(output));
if (minSign) { if (minSign) {
ZipUtils.signWholeFile(input, publicKey, privateKey, outputFile); ZipUtils.signWholeFile(input, publicKey, privateKey, outputFile);
} else { } else {

View File

@ -114,7 +114,6 @@
<string name="copying_msg">Copying zip to temp directory</string> <string name="copying_msg">Copying zip to temp directory</string>
<string name="zip_install_progress_title">Installing</string> <string name="zip_install_progress_title">Installing</string>
<string name="zip_unzip_msg">Unzipping zip file …</string> <string name="zip_unzip_msg">Unzipping zip file …</string>
<string name="zip_process_msg">Processing zip file …</string>
<string name="zip_install_progress_msg">Installing %1$s …</string> <string name="zip_install_progress_msg">Installing %1$s …</string>
<string name="no_magisk_title">Magisk Is Not Installed!</string> <string name="no_magisk_title">Magisk Is Not Installed!</string>
<string name="no_magisk_msg">Do you want to download and install Magisk?</string> <string name="no_magisk_msg">Do you want to download and install Magisk?</string>
@ -130,7 +129,10 @@
<string name="understand">I understand</string> <string name="understand">I understand</string>
<string name="process_error">Process error</string> <string name="process_error">Process error</string>
<string name="internal_storage">The zip is stored in:\n[Internal Storage]%1$s</string> <string name="internal_storage">The zip is stored in:\n[Internal Storage]%1$s</string>
<string name="zip_download_title">Downloading</string>
<string name="zip_download_msg">Downloading zip file …</string>
<string name="zip_process_title">Processing</string> <string name="zip_process_title">Processing</string>
<string name="zip_process_msg">Processing zip file …</string>
<string name="manual_boot_image">Please manually select a boot image!</string> <string name="manual_boot_image">Please manually select a boot image!</string>
<string name="manager_update_title">New Magisk Manager Update Available!</string> <string name="manager_update_title">New Magisk Manager Update Available!</string>
<string name="manager_download_install">Press to download and install</string> <string name="manager_download_install">Press to download and install</string>