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-07-18 00:59:22 +08:00
|
|
|
import com.topjohnwu.magisk.utils.AdaptiveList;
|
2017-02-12 19:49:46 +08:00
|
|
|
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-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;
|
|
|
|
private File mCachedFile, mScriptFile, mCheckFile;
|
2017-02-12 19:49:46 +08:00
|
|
|
|
|
|
|
private String mFilename;
|
2017-07-18 00:59:22 +08:00
|
|
|
private AdaptiveList<String> mList;
|
2017-02-12 19:49:46 +08:00
|
|
|
|
2017-07-31 01:19:43 +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;
|
2017-02-12 19:49:46 +08:00
|
|
|
|
2017-07-19 18:01:22 +08:00
|
|
|
mCachedFile = new File(context.getCacheDir(), "install.zip");
|
|
|
|
mScriptFile = new File(context.getCacheDir(), "/META-INF/com/google/android/update-binary");
|
2017-02-15 05:24:02 +08:00
|
|
|
mCheckFile = new File(mScriptFile.getParent(), "updater-script");
|
2017-02-12 19:49:46 +08:00
|
|
|
|
|
|
|
// Try to get the filename ourselves
|
2017-07-19 18:01:22 +08:00
|
|
|
mFilename = Utils.getNameFromUri(context, mUri);
|
2017-02-12 19:49:46 +08:00
|
|
|
}
|
|
|
|
|
2017-05-12 03:32:42 +08:00
|
|
|
private boolean unzipAndCheck() throws Exception {
|
2017-08-31 03:07:33 +08:00
|
|
|
ZipUtils.unzip(mCachedFile, mCachedFile.getParentFile(), "META-INF/com/google/android", false);
|
2017-07-19 18:01:22 +08:00
|
|
|
List<String> ret = Utils.readFile(getShell(), mCheckFile.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-09-03 15:35:14 +08:00
|
|
|
MagiskManager mm = getMagiskManager();
|
|
|
|
if (mm == null) return -1;
|
2017-02-12 19:49:46 +08:00
|
|
|
try {
|
2017-08-31 03:07:33 +08:00
|
|
|
mList.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-08-31 03:07:33 +08:00
|
|
|
OutputStream out = new FileOutputStream(mCachedFile)
|
2017-07-19 18:01:22 +08:00
|
|
|
) {
|
|
|
|
if (in == null) throw new FileNotFoundException();
|
|
|
|
byte buffer[] = new byte[1024];
|
|
|
|
int length;
|
|
|
|
while ((length = in.read(buffer)) > 0)
|
2017-08-31 03:07:33 +08:00
|
|
|
out.write(buffer, 0, length);
|
2017-07-19 18:01:22 +08:00
|
|
|
} 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-08-31 03:07:33 +08:00
|
|
|
mList.add("- Installing " + mFilename);
|
2017-08-03 23:33:08 +08:00
|
|
|
getShell().su(mList,
|
2017-07-18 00:59:22 +08:00
|
|
|
"BOOTMODE=true sh " + mScriptFile + " dummy 1 " + mCachedFile +
|
|
|
|
" && 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-09-03 15:35:14 +08:00
|
|
|
MagiskManager mm = getMagiskManager();
|
|
|
|
if (mm == null) return;
|
2017-08-03 23:33:08 +08:00
|
|
|
getShell().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));
|
2017-07-19 18:01:22 +08:00
|
|
|
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:
|
2017-07-19 18:01:22 +08:00
|
|
|
// Success
|
2017-09-03 15:35:14 +08:00
|
|
|
new LoadModules(mm).exec();
|
2017-02-12 19:49:46 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-06-06 03:06:23 +08:00
|
|
|
super.onPostExecute(result);
|
2017-02-12 19:49:46 +08:00
|
|
|
}
|
|
|
|
}
|