105 lines
3.6 KiB
Java
Raw Normal View History

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;
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;
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;
private List<String> console, logs;
2017-02-12 19:49:46 +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;
this.console = console;
this.logs = logs;
mCachedFile = new File(context.getCacheDir(), "install.zip");
2017-02-12 19:49:46 +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 {
console.add("- Copying zip to temp directory");
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))
) {
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);
} catch (FileNotFoundException e) {
console.add("! Invalid Uri");
throw e;
} catch (IOException e) {
console.add("! Cannot copy to cache");
throw e;
}
2017-07-18 00:59:22 +08:00
if (!unzipAndCheck()) return 0;
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())
return -1;
2017-07-18 00:59:22 +08:00
} catch (Exception e) {
2017-02-12 19:49:46 +08:00
e.printStackTrace();
return -1;
2017-02-12 19:49:46 +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) {
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:
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:
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:
// Success
2017-10-16 00:54:48 +08:00
new LoadModules().exec();
2017-02-12 19:49:46 +08:00
break;
}
activity.reboot.setVisibility(result > 0 ? View.VISIBLE : View.GONE);
activity.buttonPanel.setVisibility(View.VISIBLE);
2017-02-12 19:49:46 +08:00
}
}