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