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

99 lines
3.2 KiB
Java
Raw Normal View History

2017-02-15 05:24:02 +08:00
package com.topjohnwu.magisk.asyncs;
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.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.ZipUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
2017-02-15 05:24:02 +08:00
import java.io.OutputStream;
public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
private Uri mUri;
private ProgressDialog progressDialog;
2017-02-16 17:50:36 +08:00
private boolean mInstall;
2017-02-15 05:24:02 +08:00
2017-02-16 17:50:36 +08:00
public ProcessRepoZip(Activity context, Uri uri, boolean install) {
2017-02-15 05:24:02 +08:00
super(context);
mUri = uri;
2017-02-16 17:50:36 +08:00
mInstall = install;
2017-02-15 05:24:02 +08:00
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(activity,
2017-02-16 17:50:36 +08:00
activity.getString(R.string.zip_process_title),
activity.getString(R.string.zip_process_msg));
2017-02-15 05:24:02 +08:00
}
@Override
protected Boolean doInBackground(Void... params) {
try {
// Create temp file
File temp1 = new File(magiskManager.getCacheDir(), "1.zip");
File temp2 = new File(magiskManager.getCacheDir(), "2.zip");
2017-06-16 03:07:46 +08:00
magiskManager.getCacheDir().mkdirs();
temp1.createNewFile();
temp2.createNewFile();
// First remove top folder in Github source zip, Uri -> temp1
2017-06-16 03:07:46 +08:00
ZipUtils.removeTopFolder(activity.getContentResolver().openInputStream(mUri), temp1);
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);
2017-06-16 03:07:46 +08:00
// Write it back to the downloaded zip, temp2 -> Uri
FileInputStream in = new FileInputStream(temp2);
try (OutputStream target = activity.getContentResolver().openOutputStream(mUri)) {
byte[] buffer = new byte[4096];
int length;
2017-06-16 03:07:46 +08:00
if (target == null) throw new FileNotFoundException();
while ((length = in.read(buffer)) > 0)
target.write(buffer, 0, length);
2017-02-15 05:24:02 +08:00
}
// Delete the temp file
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) {
progressDialog.dismiss();
if (result) {
if (Shell.rootAccess() && mInstall) {
2017-07-18 00:59:22 +08:00
magiskManager.startActivity(new Intent(magiskManager, FlashActivity.class).setData(mUri));
} else {
2017-02-15 05:24:02 +08:00
Utils.showUriSnack(activity, mUri);
}
2017-02-15 05:24:02 +08:00
} else {
2017-07-18 00:59:22 +08:00
magiskManager.toast(R.string.process_error, Toast.LENGTH_LONG);
2017-02-15 05:24:02 +08:00
}
}
}