2017-02-12 19:49:46 +08:00
|
|
|
package com.topjohnwu.magisk.asyncs;
|
|
|
|
|
2017-07-31 01:19:43 +08:00
|
|
|
import android.app.Activity;
|
2017-02-12 19:49:46 +08:00
|
|
|
import android.net.Uri;
|
2017-11-18 03:55:47 +08:00
|
|
|
import android.view.View;
|
2017-02-12 19:49:46 +08:00
|
|
|
|
2018-07-31 17:42:35 +08:00
|
|
|
import com.topjohnwu.magisk.Const;
|
2018-07-31 17:41:54 +08:00
|
|
|
import com.topjohnwu.magisk.Data;
|
2017-11-18 03:55:47 +08:00
|
|
|
import com.topjohnwu.magisk.FlashActivity;
|
2017-02-12 19:49:46 +08:00
|
|
|
import com.topjohnwu.magisk.MagiskManager;
|
2018-06-02 22:00:52 +08:00
|
|
|
import com.topjohnwu.magisk.components.SnackbarMaker;
|
2017-02-12 19:49:46 +08:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
|
|
|
import com.topjohnwu.magisk.utils.ZipUtils;
|
2018-01-21 06:07:24 +08:00
|
|
|
import com.topjohnwu.superuser.Shell;
|
2018-02-12 23:07:35 +08:00
|
|
|
import com.topjohnwu.superuser.ShellUtils;
|
2017-02-12 19:49:46 +08:00
|
|
|
|
2017-09-26 20:46:58 +08:00
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.BufferedOutputStream;
|
2017-02-12 19:49:46 +08:00
|
|
|
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-08-31 03:07:33 +08:00
|
|
|
public class FlashZip extends ParallelTask<Void, Void, Integer> {
|
2017-02-12 19:49:46 +08:00
|
|
|
|
2017-02-15 05:24:02 +08:00
|
|
|
private Uri mUri;
|
2017-09-26 20:46:58 +08:00
|
|
|
private File mCachedFile;
|
2017-11-18 03:55:47 +08:00
|
|
|
private List<String> console, logs;
|
2017-02-12 19:49:46 +08:00
|
|
|
|
2017-11-18 03:55:47 +08:00
|
|
|
public FlashZip(Activity context, Uri uri, List<String> console, List<String> logs) {
|
2017-02-12 19:49:46 +08:00
|
|
|
super(context);
|
|
|
|
mUri = uri;
|
2017-11-18 03:55:47 +08:00
|
|
|
this.console = console;
|
|
|
|
this.logs = logs;
|
2017-07-19 18:01:22 +08:00
|
|
|
mCachedFile = new File(context.getCacheDir(), "install.zip");
|
2017-02-12 19:49:46 +08:00
|
|
|
}
|
|
|
|
|
2017-05-12 03:32:42 +08:00
|
|
|
private boolean unzipAndCheck() throws Exception {
|
2017-09-26 20:46:58 +08:00
|
|
|
ZipUtils.unzip(mCachedFile, mCachedFile.getParentFile(), "META-INF/com/google/android", true);
|
2018-06-27 05:58:56 +08:00
|
|
|
return ShellUtils.fastCmdResult("grep -q '#MAGISK' " + new File(mCachedFile.getParentFile(), "updater-script"));
|
2017-02-12 19:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-07-16 01:20:39 +08:00
|
|
|
protected Integer doInBackground(Void... voids) {
|
2018-07-31 17:41:54 +08:00
|
|
|
MagiskManager mm = Data.MM();
|
2017-02-12 19:49:46 +08:00
|
|
|
try {
|
2017-11-18 03:55:47 +08:00
|
|
|
console.add("- Copying zip to temp directory");
|
2017-07-19 18:01:22 +08:00
|
|
|
|
|
|
|
mCachedFile.delete();
|
|
|
|
try (
|
2017-09-03 15:35:14 +08:00
|
|
|
InputStream in = mm.getContentResolver().openInputStream(mUri);
|
2017-09-26 20:46:58 +08:00
|
|
|
OutputStream out = new BufferedOutputStream(new FileOutputStream(mCachedFile))
|
2017-07-19 18:01:22 +08:00
|
|
|
) {
|
|
|
|
if (in == null) throw new FileNotFoundException();
|
2017-09-26 20:46:58 +08:00
|
|
|
InputStream buf= new BufferedInputStream(in);
|
2018-02-12 23:07:35 +08:00
|
|
|
ShellUtils.pump(buf, out);
|
2017-07-19 18:01:22 +08:00
|
|
|
} catch (FileNotFoundException e) {
|
2017-11-18 03:55:47 +08:00
|
|
|
console.add("! Invalid Uri");
|
2017-07-19 18:01:22 +08:00
|
|
|
throw e;
|
|
|
|
} catch (IOException e) {
|
2017-11-18 03:55:47 +08:00
|
|
|
console.add("! Cannot copy to cache");
|
2017-07-19 18:01:22 +08:00
|
|
|
throw e;
|
|
|
|
}
|
2017-07-18 00:59:22 +08:00
|
|
|
if (!unzipAndCheck()) return 0;
|
2017-11-18 03:55:47 +08:00
|
|
|
console.add("- Installing " + Utils.getNameFromUri(mm, mUri));
|
2018-07-27 04:48:32 +08:00
|
|
|
if (!Shell.su("cd " + mCachedFile.getParent(),
|
|
|
|
"BOOTMODE=true sh update-binary dummy 1 " + mCachedFile)
|
|
|
|
.to(console, logs)
|
|
|
|
.exec().isSuccess())
|
2017-11-18 03:55:47 +08:00
|
|
|
return -1;
|
|
|
|
|
2017-07-18 00:59:22 +08:00
|
|
|
} catch (Exception e) {
|
2017-02-12 19:49:46 +08:00
|
|
|
e.printStackTrace();
|
2017-11-18 03:55:47 +08:00
|
|
|
return -1;
|
2017-02-12 19:49:46 +08:00
|
|
|
}
|
2017-11-18 03:55:47 +08:00
|
|
|
console.add("- All done!");
|
|
|
|
return 1;
|
2017-02-12 19:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// -1 = error, manual install; 0 = invalid zip; 1 = success
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Integer result) {
|
2017-11-18 03:55:47 +08:00
|
|
|
FlashActivity activity = (FlashActivity) getActivity();
|
2018-07-27 04:48:32 +08:00
|
|
|
Shell.su("rm -rf " + mCachedFile.getParent(), "rm -rf " + Const.TMP_FOLDER_PATH).submit();
|
2017-02-12 19:49:46 +08:00
|
|
|
switch (result) {
|
|
|
|
case -1:
|
2017-11-18 03:55:47 +08:00
|
|
|
console.add("! Installation failed");
|
2018-06-02 22:00:52 +08:00
|
|
|
SnackbarMaker.showUri(getActivity(), mUri);
|
2017-07-31 01:19:43 +08:00
|
|
|
break;
|
2017-02-12 19:49:46 +08:00
|
|
|
case 0:
|
2017-11-18 03:55:47 +08:00
|
|
|
console.add("! This zip is not a Magisk Module!");
|
2017-07-31 01:19:43 +08:00
|
|
|
break;
|
2017-02-12 19:49:46 +08:00
|
|
|
case 1:
|
2017-07-19 18:01:22 +08:00
|
|
|
// Success
|
2017-10-16 00:54:48 +08:00
|
|
|
new LoadModules().exec();
|
2017-02-12 19:49:46 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-11-18 03:55:47 +08:00
|
|
|
activity.reboot.setVisibility(result > 0 ? View.VISIBLE : View.GONE);
|
|
|
|
activity.buttonPanel.setVisibility(View.VISIBLE);
|
2017-02-12 19:49:46 +08:00
|
|
|
}
|
|
|
|
}
|