2017-02-12 19:49:46 +08:00
|
|
|
package com.topjohnwu.magisk.asyncs;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.ProgressDialog;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import com.topjohnwu.magisk.MagiskManager;
|
|
|
|
import com.topjohnwu.magisk.R;
|
2017-02-15 05:24:02 +08:00
|
|
|
import com.topjohnwu.magisk.components.AlertDialogBuilder;
|
2017-02-12 19:49:46 +08:00
|
|
|
import com.topjohnwu.magisk.utils.Logger;
|
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
|
|
|
import com.topjohnwu.magisk.utils.ZipUtils;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.List;
|
|
|
|
|
2017-07-16 01:20:39 +08:00
|
|
|
public class FlashZip extends ParallelTask<Void, String, Integer> {
|
2017-02-12 19:49:46 +08:00
|
|
|
|
2017-02-15 05:24:02 +08:00
|
|
|
private Uri mUri;
|
|
|
|
private File mCachedFile, mScriptFile, mCheckFile;
|
2017-02-12 19:49:46 +08:00
|
|
|
|
|
|
|
private String mFilename;
|
|
|
|
private ProgressDialog progress;
|
|
|
|
|
2017-02-15 05:24:02 +08:00
|
|
|
public FlashZip(Activity context, Uri uri) {
|
2017-02-12 19:49:46 +08:00
|
|
|
super(context);
|
|
|
|
mUri = uri;
|
|
|
|
|
2017-02-15 05:24:02 +08:00
|
|
|
mCachedFile = new File(magiskManager.getCacheDir(), "install.zip");
|
|
|
|
mScriptFile = new File(magiskManager.getCacheDir(), "/META-INF/com/google/android/update-binary");
|
|
|
|
mCheckFile = new File(mScriptFile.getParent(), "updater-script");
|
2017-02-12 19:49:46 +08:00
|
|
|
|
|
|
|
// Try to get the filename ourselves
|
2017-02-15 05:24:02 +08:00
|
|
|
mFilename = Utils.getNameFromUri(magiskManager, mUri);
|
2017-02-12 19:49:46 +08:00
|
|
|
}
|
|
|
|
|
2017-02-15 05:24:02 +08:00
|
|
|
private void copyToCache() throws Throwable {
|
2017-02-12 19:49:46 +08:00
|
|
|
publishProgress(magiskManager.getString(R.string.copying_msg));
|
2017-02-15 05:24:02 +08:00
|
|
|
|
2017-02-12 19:49:46 +08:00
|
|
|
if (mCachedFile.exists() && !mCachedFile.delete()) {
|
|
|
|
Logger.error("FlashZip: Error while deleting already existing file");
|
|
|
|
throw new IOException();
|
|
|
|
}
|
|
|
|
try (
|
|
|
|
InputStream in = magiskManager.getContentResolver().openInputStream(mUri);
|
|
|
|
OutputStream outputStream = new FileOutputStream(mCachedFile)
|
|
|
|
) {
|
|
|
|
byte buffer[] = new byte[1024];
|
|
|
|
int length;
|
|
|
|
if (in == null) throw new FileNotFoundException();
|
2017-02-15 05:24:02 +08:00
|
|
|
while ((length = in.read(buffer)) > 0)
|
2017-02-12 19:49:46 +08:00
|
|
|
outputStream.write(buffer, 0, length);
|
2017-02-15 05:24:02 +08:00
|
|
|
|
2017-02-12 19:49:46 +08:00
|
|
|
Logger.dev("FlashZip: File created successfully - " + mCachedFile.getPath());
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
Logger.error("FlashZip: Invalid Uri");
|
|
|
|
throw e;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Logger.error("FlashZip: Error in creating file");
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 03:32:42 +08:00
|
|
|
private boolean unzipAndCheck() throws Exception {
|
2017-02-12 19:49:46 +08:00
|
|
|
ZipUtils.unzip(mCachedFile, mCachedFile.getParentFile(), "META-INF/com/google/android");
|
|
|
|
List<String> ret;
|
2017-07-16 01:20:39 +08:00
|
|
|
ret = Utils.readFile(magiskManager.rootShell, mCheckFile.getPath());
|
2017-02-12 19:49:46 +08:00
|
|
|
return Utils.isValidShellResponse(ret) && ret.get(0).contains("#MAGISK");
|
|
|
|
}
|
|
|
|
|
2017-02-15 05:24:02 +08:00
|
|
|
private int cleanup(int ret) {
|
2017-07-16 01:20:39 +08:00
|
|
|
magiskManager.rootShell.su(
|
2017-02-15 05:24:02 +08:00
|
|
|
"rm -rf " + mCachedFile.getParent() + "/*",
|
|
|
|
"rm -rf " + MagiskManager.TMP_FOLDER_PATH
|
|
|
|
);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-12 19:49:46 +08:00
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
|
|
|
progress = new ProgressDialog(activity);
|
|
|
|
progress.setTitle(R.string.zip_install_progress_title);
|
|
|
|
progress.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onProgressUpdate(String... values) {
|
|
|
|
progress.setMessage(values[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-16 01:20:39 +08:00
|
|
|
protected Integer doInBackground(Void... voids) {
|
2017-02-12 19:49:46 +08:00
|
|
|
Logger.dev("FlashZip Running... " + mFilename);
|
|
|
|
List<String> ret;
|
|
|
|
try {
|
|
|
|
copyToCache();
|
2017-02-15 05:24:02 +08:00
|
|
|
if (!unzipAndCheck()) return cleanup(0);
|
|
|
|
publishProgress(magiskManager.getString(R.string.zip_install_progress_msg, mFilename));
|
2017-07-16 01:20:39 +08:00
|
|
|
ret = magiskManager.rootShell.su(
|
2017-02-15 05:24:02 +08:00
|
|
|
"BOOTMODE=true sh " + mScriptFile + " dummy 1 " + mCachedFile,
|
|
|
|
"if [ $? -eq 0 ]; then echo true; else echo false; fi"
|
|
|
|
);
|
|
|
|
if (!Utils.isValidShellResponse(ret)) return -1;
|
|
|
|
Logger.dev("FlashZip: Console log:");
|
|
|
|
for (String line : ret) {
|
|
|
|
Logger.dev(line);
|
|
|
|
}
|
|
|
|
if (Boolean.parseBoolean(ret.get(ret.size() - 1)))
|
|
|
|
return cleanup(1);
|
|
|
|
|
2017-02-12 19:49:46 +08:00
|
|
|
} catch (Throwable e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2017-02-15 05:24:02 +08:00
|
|
|
return cleanup(-1);
|
2017-02-12 19:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// -1 = error, manual install; 0 = invalid zip; 1 = success
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Integer result) {
|
|
|
|
progress.dismiss();
|
|
|
|
switch (result) {
|
|
|
|
case -1:
|
|
|
|
Toast.makeText(magiskManager, magiskManager.getString(R.string.install_error), Toast.LENGTH_LONG).show();
|
2017-02-15 05:24:02 +08:00
|
|
|
Utils.showUriSnack(activity, mUri);
|
2017-02-12 19:49:46 +08:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
Toast.makeText(magiskManager, magiskManager.getString(R.string.invalid_zip), Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
onSuccess();
|
|
|
|
break;
|
|
|
|
}
|
2017-06-06 03:06:23 +08:00
|
|
|
super.onPostExecute(result);
|
2017-02-12 19:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void onSuccess() {
|
|
|
|
magiskManager.updateCheckDone.trigger();
|
|
|
|
new LoadModules(activity).exec();
|
|
|
|
|
2017-02-15 05:24:02 +08:00
|
|
|
new AlertDialogBuilder(activity)
|
2017-02-12 19:49:46 +08:00
|
|
|
.setTitle(R.string.reboot_title)
|
|
|
|
.setMessage(R.string.reboot_msg)
|
2017-07-16 01:20:39 +08:00
|
|
|
.setPositiveButton(R.string.reboot, (dialogInterface, i) -> magiskManager.rootShell.su("reboot"))
|
2017-02-12 19:49:46 +08:00
|
|
|
.setNegativeButton(R.string.no_thanks, null)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
}
|