Magisk/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java

132 lines
4.5 KiB
Java
Raw Normal View History

2017-02-15 05:24:02 +08:00
package com.topjohnwu.magisk.asyncs;
import android.Manifest;
2017-02-15 05:24:02 +08:00
import android.app.Activity;
import android.app.ProgressDialog;
2017-07-18 00:59:22 +08:00
import android.content.Intent;
2017-02-15 05:24:02 +08:00
import android.net.Uri;
import android.os.Environment;
2017-02-15 05:24:02 +08:00
import android.widget.Toast;
2017-07-18 00:59:22 +08:00
import com.topjohnwu.magisk.FlashActivity;
2017-02-15 05:24:02 +08:00
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.WebService;
2017-02-15 05:24:02 +08:00
import com.topjohnwu.magisk.utils.ZipUtils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
2017-02-15 05:24:02 +08:00
import java.io.OutputStream;
public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
private ProgressDialog progressDialog;
2017-02-16 17:50:36 +08:00
private boolean mInstall;
private String mLink, mFile;
2017-02-15 05:24:02 +08:00
public ProcessRepoZip(Activity context, String link, String filename, boolean install) {
2017-02-15 05:24:02 +08:00
super(context);
mLink = link;
mFile = Environment.getExternalStorageDirectory() + "/MagiskManager/" + filename;
2017-02-16 17:50:36 +08:00
mInstall = install;
2017-02-15 05:24:02 +08:00
}
@Override
protected void onPreExecute() {
Activity activity = getActivity();
2017-02-15 05:24:02 +08:00
progressDialog = ProgressDialog.show(activity,
activity.getString(R.string.zip_download_title),
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));
2017-02-15 05:24:02 +08:00
}
@Override
protected Boolean doInBackground(Void... params) {
Activity activity = getActivity();
if (activity == null) return null;
2017-02-15 05:24:02 +08:00
try {
// Request zip from Internet
InputStream in = WebService.request(WebService.GET, mLink, null);
if (in == null) return false;
in = new BufferedInputStream(in);
// Temp files
2017-07-19 16:10:17 +08:00
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();
2017-02-15 05:24:02 +08:00
// Then sign the zip for the first time, temp1 -> temp2
2017-06-16 03:07:46 +08:00
ZipUtils.signZip(activity, temp1, temp2, false);
2017-06-16 03:07:46 +08:00
// Adjust the zip to prevent unzip issues, temp2 -> temp1
ZipUtils.zipAdjust(temp2.getPath(), temp1.getPath());
2017-06-16 03:07:46 +08:00
// Finally, sign the whole zip file again, temp1 -> temp2
ZipUtils.signZip(activity, temp1, temp2, true);
// Write it to the target zip, temp2 -> file
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(mFile));
InputStream source = new BufferedInputStream(new FileInputStream(temp2))
2017-07-22 17:39:34 +08:00
) {
byte[] buffer = new byte[4096];
int length;
while ((length = source.read(buffer)) > 0)
2017-07-22 17:39:34 +08:00
out.write(buffer, 0, length);
2017-02-15 05:24:02 +08:00
}
// Delete temp files
temp1.delete();
temp2.delete();
2017-02-15 05:24:02 +08:00
return true;
} catch (Exception e) {
Logger.error("ProcessRepoZip: Error!");
e.printStackTrace();
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
Activity activity = getActivity();
if (activity == null) return;
2017-02-15 05:24:02 +08:00
progressDialog.dismiss();
Uri uri = Uri.fromFile(new File(mFile));
2017-02-15 05:24:02 +08:00
if (result) {
if (Shell.rootAccess() && mInstall) {
2017-08-31 03:07:33 +08:00
Intent intent = new Intent(getActivity(), FlashActivity.class);
intent.setData(uri).putExtra(FlashActivity.SET_ACTION, FlashActivity.FLASH_ZIP);
2017-08-31 03:07:33 +08:00
activity.startActivity(intent);
} else {
Utils.showUriSnack(activity, uri);
}
2017-02-15 05:24:02 +08:00
} else {
Utils.getMagiskManager(activity).toast(R.string.process_error, Toast.LENGTH_LONG);
2017-02-15 05:24:02 +08:00
}
2017-08-31 03:07:33 +08:00
super.onPostExecute(result);
2017-02-15 05:24:02 +08:00
}
@Override
public ParallelTask<Void, Void, Boolean> exec(Void... voids) {
Utils.runWithPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, () -> super.exec(voids));
return this;
}
2017-02-15 05:24:02 +08:00
}