mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-11-24 10:35:26 +00:00
Small refactor of download repo and FlashZip
This commit is contained in:
parent
8c077a7373
commit
f5bee7b691
@ -71,18 +71,12 @@ public class MagiskFragment extends Fragment {
|
||||
private SharedPreferences prefs;
|
||||
private SharedPreferences.OnSharedPreferenceChangeListener listener;
|
||||
|
||||
private AlertDialog.OnClickListener flashMagisk = (dialogInterface, i) -> Utils.downloadAndReceive(
|
||||
private AlertDialog.OnClickListener flashMagisk = (dialogInterface, i) -> Utils.dlAndReceive(
|
||||
getActivity(),
|
||||
new DownloadReceiver("Magisk-v" + String.valueOf(remoteMagiskVersion)) {
|
||||
new DownloadReceiver() {
|
||||
@Override
|
||||
public void task(Uri uri) {
|
||||
new Async.FlashZIP(mContext, uri, mName) {
|
||||
@Override
|
||||
protected void preProcessing() throws Throwable {
|
||||
super.preProcessing();
|
||||
new File(mUri.getPath()).delete();
|
||||
}
|
||||
|
||||
new Async.FlashZIP(mContext, uri, mFilename) {
|
||||
@Override
|
||||
protected void done() {
|
||||
Shell.su("setprop magisk.version " + String.valueOf(remoteMagiskVersion));
|
||||
@ -92,9 +86,9 @@ public class MagiskFragment extends Fragment {
|
||||
}
|
||||
},
|
||||
magiskLink,
|
||||
"latest_magisk.zip");
|
||||
"Magisk-v" + String.valueOf(remoteMagiskVersion) + ".zip");
|
||||
|
||||
private AlertDialog.OnClickListener installMagiskApk = (dialogInterface, i) -> Utils.downloadAndReceive(
|
||||
private AlertDialog.OnClickListener installMagiskApk = (dialogInterface, i) -> Utils.dlAndReceive(
|
||||
getActivity(),
|
||||
new DownloadReceiver() {
|
||||
@Override
|
||||
|
@ -83,7 +83,6 @@ public class ModulesFragment extends Fragment {
|
||||
if (data != null) {
|
||||
// Get the URI of the selected file
|
||||
final Uri uri = data.getData();
|
||||
// Get the file path from the URI
|
||||
new Async.FlashZIP(getActivity(), uri).exec();
|
||||
}
|
||||
|
||||
|
@ -92,26 +92,28 @@ public class ReposAdapter extends RecyclerView.Adapter<ReposAdapter.ViewHolder>
|
||||
|
||||
View.OnClickListener listener = view -> {
|
||||
if (view.getId() == holder.updateImage.getId()) {
|
||||
String fullname = repo.getName() + "-" + repo.getVersion();
|
||||
String filename = repo.getName() + "-" + repo.getVersion() + ".zip";
|
||||
builder
|
||||
.setTitle(context.getString(R.string.repo_install_title, repo.getName()))
|
||||
.setMessage(context.getString(R.string.repo_install_msg, fullname))
|
||||
.setMessage(context.getString(R.string.repo_install_msg, filename))
|
||||
.setCancelable(true)
|
||||
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.downloadAndReceive(
|
||||
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.dlAndReceive(
|
||||
context,
|
||||
new DownloadReceiver(fullname) {
|
||||
new DownloadReceiver() {
|
||||
@Override
|
||||
public void task(Uri uri) {
|
||||
new Async.FlashZIP(context, uri, mName) {
|
||||
new Async.FlashZIP(context, uri, mFilename) {
|
||||
/*
|
||||
* !!! This method is now depreciated, will be replaced with new method !!!
|
||||
*/
|
||||
@Override
|
||||
protected void preProcessing() throws Throwable {
|
||||
super.preProcessing();
|
||||
new File(mUri.getPath()).delete();
|
||||
File file = new File(mUri.getPath());
|
||||
Shell.sh(
|
||||
"PATH=" + context.getApplicationInfo().dataDir + "/tools:$PATH",
|
||||
"cd " + mFile.getParent(),
|
||||
"cd " + file.getParent(),
|
||||
"mkdir git",
|
||||
"unzip -o install.zip -d git",
|
||||
"unzip -o " + file + " -d git",
|
||||
"mv git/* install",
|
||||
"cd install",
|
||||
"rm -rf system/placeholder",
|
||||
@ -121,12 +123,13 @@ public class ReposAdapter extends RecyclerView.Adapter<ReposAdapter.ViewHolder>
|
||||
"zip -r ../install.zip *",
|
||||
"rm -rf ../install"
|
||||
);
|
||||
mUri = Uri.fromFile(new File(file.getParent() + "/install.zip"));
|
||||
}
|
||||
}.exec();
|
||||
}
|
||||
},
|
||||
repo.getZipUrl(),
|
||||
repo.getId().replace(" ", "") + ".zip"))
|
||||
Utils.getLegalFilename(filename)))
|
||||
.setNegativeButton(R.string.no_thanks, null)
|
||||
.show();
|
||||
}
|
||||
|
@ -12,15 +12,11 @@ import com.topjohnwu.magisk.R;
|
||||
|
||||
public abstract class DownloadReceiver extends BroadcastReceiver {
|
||||
public Context mContext;
|
||||
public String mName;
|
||||
public String mFilename;
|
||||
long downloadID;
|
||||
|
||||
public DownloadReceiver() {}
|
||||
|
||||
public DownloadReceiver(String name) {
|
||||
mName = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
mContext = context;
|
||||
@ -51,5 +47,9 @@ public abstract class DownloadReceiver extends BroadcastReceiver {
|
||||
downloadID = id;
|
||||
}
|
||||
|
||||
public void setFilename(String filename) {
|
||||
mFilename = filename;
|
||||
}
|
||||
|
||||
public void task(Uri uri) {}
|
||||
}
|
||||
|
@ -172,16 +172,16 @@ public class Async {
|
||||
public static class FlashZIP extends RootTask<Void, Void, Integer> {
|
||||
|
||||
protected Uri mUri;
|
||||
protected File mFile, sdFile;
|
||||
private String mName;
|
||||
protected File mCachedFile, sdFile;
|
||||
private String mFilename;
|
||||
private ProgressDialog progress;
|
||||
private Context mContext;
|
||||
private boolean copyToSD;
|
||||
|
||||
public FlashZIP(Context context, Uri uri, String name) {
|
||||
public FlashZIP(Context context, Uri uri, String filename) {
|
||||
mContext = context;
|
||||
mUri = uri;
|
||||
mName = name;
|
||||
mFilename = filename;
|
||||
copyToSD = true;
|
||||
}
|
||||
|
||||
@ -193,13 +193,13 @@ public class Async {
|
||||
int nameIndex = c.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
||||
c.moveToFirst();
|
||||
if (nameIndex != -1) {
|
||||
mName = c.getString(nameIndex);
|
||||
mFilename = c.getString(nameIndex);
|
||||
}
|
||||
c.close();
|
||||
}
|
||||
if (mName == null) {
|
||||
if (mFilename == null) {
|
||||
int idx = uri.getPath().lastIndexOf('/');
|
||||
mName = uri.getPath().substring(idx + 1);
|
||||
mFilename = uri.getPath().substring(idx + 1);
|
||||
}
|
||||
copyToSD = false;
|
||||
}
|
||||
@ -221,34 +221,37 @@ public class Async {
|
||||
Logger.dev("FlashZip: File created successfully - " + file.getPath());
|
||||
}
|
||||
|
||||
protected void preProcessing() throws Throwable {
|
||||
protected void preProcessing() throws Throwable {}
|
||||
|
||||
protected void copyToCache() throws Throwable {
|
||||
try {
|
||||
InputStream in = mContext.getContentResolver().openInputStream(mUri);
|
||||
mFile = new File(mContext.getCacheDir().getAbsolutePath() + "/install.zip");
|
||||
mFile.delete();
|
||||
Utils.removeItem(mFile.getPath());
|
||||
createFileFromInputStream(in, mFile);
|
||||
mCachedFile = new File(mContext.getCacheDir().getAbsolutePath() + "/install.zip");
|
||||
mCachedFile.delete();
|
||||
Utils.removeItem(mCachedFile.getPath());
|
||||
createFileFromInputStream(in, mCachedFile);
|
||||
in.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.e("Magisk", "FlashZip: Invalid Uri");
|
||||
Log.e(Logger.LOG_TAG, "FlashZip: Invalid Uri");
|
||||
throw e;
|
||||
} catch (IOException e) {
|
||||
Log.e("Magisk", "FlashZip: Error in creating file");
|
||||
Log.e(Logger.LOG_TAG, "FlashZip: Error in creating file");
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
progress = ProgressDialog.show(mContext, mContext.getString(R.string.zip_install_progress_title), mContext.getString(R.string.zip_install_progress_msg, mName));
|
||||
progress = ProgressDialog.show(mContext, mContext.getString(R.string.zip_install_progress_title), mContext.getString(R.string.zip_install_progress_msg, mFilename));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Integer doInBackground(Void... voids) {
|
||||
Logger.dev("FlashZip Running... " + mName);
|
||||
Logger.dev("FlashZip Running... " + mFilename);
|
||||
List<String> ret = null;
|
||||
try {
|
||||
preProcessing();
|
||||
copyToCache();
|
||||
} catch (Throwable e) {
|
||||
this.cancel(true);
|
||||
progress.cancel();
|
||||
@ -257,49 +260,46 @@ public class Async {
|
||||
}
|
||||
if (Shell.rootAccess()) {
|
||||
ret = Shell.su(
|
||||
"unzip -o " + mFile.getPath() + " META-INF/com/google/android/* -d " + mFile.getParent(),
|
||||
"if [ \"$(cat " + mFile.getParent() + "/META-INF/com/google/android/updater-script)\" = \"#MAGISK\" ]; then echo true; else echo false; fi"
|
||||
"unzip -o " + mCachedFile.getPath() + " META-INF/com/google/android/* -d " + mCachedFile.getParent(),
|
||||
"if [ \"$(cat " + mCachedFile.getParent() + "/META-INF/com/google/android/updater-script)\" = \"#MAGISK\" ]; then echo true; else echo false; fi"
|
||||
);
|
||||
if (! Boolean.parseBoolean(ret.get(ret.size() - 1))) {
|
||||
return 0;
|
||||
}
|
||||
ret = Shell.su(
|
||||
"BOOTMODE=true sh " + mFile.getParent() + "/META-INF/com/google/android/update-binary dummy 1 "+ mFile.getPath(),
|
||||
"BOOTMODE=true sh " + mCachedFile.getParent() + "/META-INF/com/google/android/update-binary dummy 1 "+ mCachedFile.getPath(),
|
||||
"if [ $? -eq 0 ]; then echo true; else echo false; fi"
|
||||
);
|
||||
Shell.su("rm -rf " + mFile.getParent() + "/META-INF");
|
||||
Shell.su("rm -rf " + mCachedFile.getParent() + "/META-INF");
|
||||
Logger.dev("FlashZip: Console log:");
|
||||
for (String line : ret) {
|
||||
Logger.dev(line);
|
||||
}
|
||||
}
|
||||
// Copy the file to sdcard
|
||||
if (copyToSD && mFile != null) {
|
||||
String filename = (mName.contains(".zip") ? mName : mName + ".zip");
|
||||
filename = filename.replace(" ", "_").replace("'", "").replace("\"", "")
|
||||
.replace("$", "").replace("`", "").replace("(", "").replace(")", "")
|
||||
.replace("#", "").replace("@", "").replace("*", "");
|
||||
if (copyToSD && mCachedFile != null) {
|
||||
String filename = Utils.getLegalFilename(mFilename.contains(".zip") ? mFilename : mFilename + ".zip");
|
||||
sdFile = new File(Environment.getExternalStorageDirectory() + "/MagiskManager/" + filename);
|
||||
Logger.dev("FlashZip: Copy zip back to " + sdFile.getPath());
|
||||
if ((!sdFile.getParentFile().exists() && !sdFile.getParentFile().mkdirs()) || (sdFile.exists() && !sdFile.delete())) {
|
||||
sdFile = null;
|
||||
} else {
|
||||
try {
|
||||
FileInputStream in = new FileInputStream(mFile);
|
||||
FileInputStream in = new FileInputStream(mCachedFile);
|
||||
createFileFromInputStream(in, sdFile);
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
// Use the badass way :)
|
||||
e.printStackTrace();
|
||||
Shell.su("cp -af " + mFile.getPath() + " " + sdFile.getPath());
|
||||
Shell.su("cp -af " + mCachedFile.getPath() + " " + sdFile.getPath());
|
||||
if (!sdFile.exists()) {
|
||||
sdFile = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (mFile != null && mFile.exists() && !mFile.delete()) {
|
||||
Utils.removeItem(mFile.getPath());
|
||||
if (mCachedFile != null && mCachedFile.exists() && !mCachedFile.delete()) {
|
||||
Utils.removeItem(mCachedFile.getPath());
|
||||
}
|
||||
if (ret != null && Boolean.parseBoolean(ret.get(ret.size() - 1))) {
|
||||
return 1;
|
||||
|
@ -4,7 +4,7 @@ import android.util.Log;
|
||||
|
||||
public class Logger {
|
||||
|
||||
private static final String LOG_TAG = "Magisk: DEV";
|
||||
public static final String LOG_TAG = "Magisk: DEV";
|
||||
|
||||
public static boolean logShell, devLog;
|
||||
|
||||
|
@ -87,7 +87,7 @@ public class Utils {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static void downloadAndReceive(Context context, DownloadReceiver receiver, String link, String filename) {
|
||||
public static void dlAndReceive(Context context, DownloadReceiver receiver, String link, String filename) {
|
||||
File file = new File(Environment.getExternalStorageDirectory() + "/MagiskManager/" + filename);
|
||||
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
||||
Toast.makeText(context, R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
|
||||
@ -103,6 +103,7 @@ public class Utils {
|
||||
request.setDestinationUri(Uri.fromFile(file));
|
||||
|
||||
receiver.setDownloadID(downloadManager.enqueue(request));
|
||||
receiver.setFilename(filename);
|
||||
context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
||||
}
|
||||
|
||||
@ -129,4 +130,10 @@ public class Utils {
|
||||
return secret;
|
||||
}
|
||||
|
||||
public static String getLegalFilename(CharSequence filename) {
|
||||
return filename.toString().replace(" ", "_").replace("'", "").replace("\"", "")
|
||||
.replace("$", "").replace("`", "").replace("(", "").replace(")", "")
|
||||
.replace("#", "").replace("@", "").replace("*", "");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user