114 lines
3.8 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;
2017-07-18 00:59:22 +08:00
import android.text.TextUtils;
2017-02-12 19:49:46 +08:00
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
2017-09-30 03:04:23 +08:00
import com.topjohnwu.magisk.container.AdaptiveList;
2017-10-15 23:02:44 +08:00
import com.topjohnwu.magisk.utils.Shell;
2017-02-12 19:49:46 +08:00
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.ZipUtils;
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-09-30 03:04:23 +08:00
private AdaptiveList<String> mList;
2017-02-12 19:49:46 +08:00
2017-09-30 03:04:23 +08:00
public FlashZip(Activity context, Uri uri, AdaptiveList<String> list) {
2017-02-12 19:49:46 +08:00
super(context);
mUri = uri;
2017-07-18 00:59:22 +08:00
mList = list;
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);
2017-10-15 23:02:44 +08:00
List<String> ret = Utils.readFile(new File(mCachedFile.getParentFile(), "updater-script").getPath());
2017-02-12 19:49:46 +08:00
return Utils.isValidShellResponse(ret) && ret.get(0).contains("#MAGISK");
}
@Override
protected void onPreExecute() {
2017-07-18 00:59:22 +08:00
// UI updates must run in the UI thread
mList.setCallback(this::publishProgress);
2017-02-12 19:49:46 +08:00
}
@Override
2017-08-31 03:07:33 +08:00
protected void onProgressUpdate(Void... values) {
2017-07-18 00:59:22 +08:00
mList.updateView();
2017-02-12 19:49:46 +08:00
}
@Override
2017-07-16 01:20:39 +08:00
protected Integer doInBackground(Void... voids) {
2017-10-16 00:54:48 +08:00
MagiskManager mm = MagiskManager.get();
2017-02-12 19:49:46 +08:00
try {
2017-08-31 03:07:33 +08:00
mList.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);
2017-10-31 16:31:58 +08:00
Utils.inToOut(buf, out);
} catch (FileNotFoundException e) {
mList.add("! Invalid Uri");
throw e;
} catch (IOException e) {
mList.add("! Cannot copy to cache");
throw e;
}
2017-07-18 00:59:22 +08:00
if (!unzipAndCheck()) return 0;
2017-09-26 20:46:58 +08:00
mList.add("- Installing " + Utils.getNameFromUri(mm, mUri));
2017-10-15 23:02:44 +08:00
Shell.su(mList,
2017-09-26 20:46:58 +08:00
"cd " + mCachedFile.getParent(),
"BOOTMODE=true sh update-binary dummy 1 " + mCachedFile +
2017-07-18 00:59:22 +08:00
" && echo 'Success!' || echo 'Failed!'"
2017-02-15 05:24:02 +08:00
);
2017-07-18 00:59:22 +08:00
if (TextUtils.equals(mList.get(mList.size() - 1), "Success!"))
return 1;
} catch (Exception e) {
2017-02-12 19:49:46 +08:00
e.printStackTrace();
}
2017-07-18 00:59:22 +08:00
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-10-16 00:54:48 +08:00
MagiskManager mm = MagiskManager.get();
2017-10-15 23:02:44 +08:00
Shell.su_raw(
2017-07-31 01:19:43 +08:00
"rm -rf " + mCachedFile.getParent(),
2017-07-18 00:59:22 +08:00
"rm -rf " + MagiskManager.TMP_FOLDER_PATH
);
2017-02-12 19:49:46 +08:00
switch (result) {
case -1:
2017-09-03 15:35:14 +08:00
mList.add(mm.getString(R.string.install_error));
Utils.showUriSnack(getActivity(), mUri);
2017-07-31 01:19:43 +08:00
break;
2017-02-12 19:49:46 +08:00
case 0:
2017-09-03 15:35:14 +08:00
mList.add(mm.getString(R.string.invalid_zip));
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;
}
super.onPostExecute(result);
2017-02-12 19:49:46 +08:00
}
}