2016-08-22 21:18:28 +00:00
|
|
|
package com.topjohnwu.magisk.utils;
|
2016-08-22 19:50:46 +00:00
|
|
|
|
2016-08-28 22:35:07 +00:00
|
|
|
import android.Manifest;
|
2017-01-10 14:30:05 +00:00
|
|
|
import android.app.AlertDialog;
|
2016-08-28 22:35:07 +00:00
|
|
|
import android.app.DownloadManager;
|
2016-08-27 19:52:03 +00:00
|
|
|
import android.content.Context;
|
2016-08-28 22:35:07 +00:00
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Environment;
|
|
|
|
import android.support.v4.app.ActivityCompat;
|
2017-01-08 13:41:19 +00:00
|
|
|
import android.text.TextUtils;
|
2016-08-27 19:52:03 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2017-01-25 05:16:50 +00:00
|
|
|
import com.topjohnwu.magisk.Global;
|
2016-08-27 19:52:03 +00:00
|
|
|
import com.topjohnwu.magisk.R;
|
2016-09-26 02:45:34 +00:00
|
|
|
import com.topjohnwu.magisk.receivers.DownloadReceiver;
|
2016-08-27 19:52:03 +00:00
|
|
|
|
2016-08-28 22:35:07 +00:00
|
|
|
import java.io.File;
|
2016-08-22 08:09:36 +00:00
|
|
|
import java.util.List;
|
2016-08-20 15:26:49 +00:00
|
|
|
|
|
|
|
public class Utils {
|
|
|
|
|
2016-12-27 06:30:26 +00:00
|
|
|
public static boolean isDownloading = false;
|
|
|
|
|
2016-09-25 13:31:38 +00:00
|
|
|
public static boolean itemExist(String path) {
|
2016-09-30 02:52:04 +00:00
|
|
|
return itemExist(true, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean itemExist(boolean root, String path) {
|
2016-09-25 13:31:38 +00:00
|
|
|
String command = "if [ -e " + path + " ]; then echo true; else echo false; fi";
|
2017-01-26 05:46:54 +00:00
|
|
|
List<String> ret;
|
2016-09-30 02:52:04 +00:00
|
|
|
if (Shell.rootAccess() && root) {
|
2017-01-26 05:46:54 +00:00
|
|
|
ret = Shell.su(command);
|
|
|
|
return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0));
|
2016-09-25 07:16:10 +00:00
|
|
|
} else {
|
2016-09-30 02:52:04 +00:00
|
|
|
return new File(path).exists();
|
2016-09-25 07:16:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 13:31:38 +00:00
|
|
|
public static boolean commandExists(String s) {
|
|
|
|
String command = "if [ -z $(which " + s + ") ]; then echo false; else echo true; fi";
|
2017-01-26 05:46:54 +00:00
|
|
|
List<String> ret = Shell.sh(command);
|
|
|
|
return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0));
|
2016-09-17 18:32:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:58:15 +00:00
|
|
|
public static boolean createFile(String path) {
|
2016-11-08 21:17:50 +00:00
|
|
|
String folder = path.substring(0, path.lastIndexOf('/'));
|
|
|
|
String command = "mkdir -p " + folder + " 2>/dev/null; touch " + path + " 2>/dev/null; if [ -f \"" + path + "\" ]; then echo true; else echo false; fi";
|
2017-01-26 05:46:54 +00:00
|
|
|
List<String> ret = Shell.su(command);
|
|
|
|
return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0));
|
2016-08-24 21:58:15 +00:00
|
|
|
}
|
2016-08-20 15:26:49 +00:00
|
|
|
|
2016-10-17 02:11:26 +00:00
|
|
|
public static boolean removeItem(String path) {
|
|
|
|
String command = "rm -rf " + path + " 2>/dev/null; if [ -e " + path + " ]; then echo false; else echo true; fi";
|
2017-01-26 05:46:54 +00:00
|
|
|
List<String> ret = Shell.su(command);
|
|
|
|
return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0));
|
2016-09-17 18:32:08 +00:00
|
|
|
}
|
|
|
|
|
2016-12-25 09:17:20 +00:00
|
|
|
public static List<String> getModList(String path) {
|
2016-08-24 21:58:15 +00:00
|
|
|
List<String> ret;
|
2016-09-21 14:22:36 +00:00
|
|
|
String command = "find " + path + " -type d -maxdepth 1 ! -name \"*.core\" ! -name \"*lost+found\" ! -name \"*magisk\"";
|
2017-01-10 14:47:58 +00:00
|
|
|
ret = Shell.su(command);
|
2016-08-24 21:58:15 +00:00
|
|
|
return ret;
|
2016-08-20 15:26:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:58:15 +00:00
|
|
|
public static List<String> readFile(String path) {
|
|
|
|
List<String> ret;
|
2016-09-21 14:22:36 +00:00
|
|
|
String command = "cat " + path;
|
|
|
|
if (Shell.rootAccess()) {
|
|
|
|
ret = Shell.su(command);
|
|
|
|
} else {
|
|
|
|
ret = Shell.sh(command);
|
|
|
|
}
|
2016-08-24 21:58:15 +00:00
|
|
|
return ret;
|
2016-08-22 17:44:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-20 14:13:29 +00:00
|
|
|
public static void dlAndReceive(Context context, DownloadReceiver receiver, String link, String filename) {
|
2016-12-27 06:30:26 +00:00
|
|
|
if (isDownloading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-28 22:35:07 +00:00
|
|
|
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
Toast.makeText(context, R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
|
|
|
|
return;
|
|
|
|
}
|
2016-09-28 06:50:26 +00:00
|
|
|
|
2016-12-27 06:30:26 +00:00
|
|
|
File file = new File(Environment.getExternalStorageDirectory() + "/MagiskManager/" + filename);
|
|
|
|
|
2016-09-28 06:50:26 +00:00
|
|
|
if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs()) || (file.exists() && !file.delete())) {
|
2016-09-29 15:24:31 +00:00
|
|
|
Toast.makeText(context, R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
|
2016-12-27 06:30:26 +00:00
|
|
|
return;
|
2016-09-24 04:25:12 +00:00
|
|
|
}
|
2016-08-28 22:35:07 +00:00
|
|
|
|
2016-12-27 06:30:26 +00:00
|
|
|
Toast.makeText(context, context.getString(R.string.downloading_toast, filename), Toast.LENGTH_LONG).show();
|
|
|
|
isDownloading = true;
|
|
|
|
|
2016-08-28 22:35:07 +00:00
|
|
|
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
|
|
|
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link));
|
2016-09-28 06:50:26 +00:00
|
|
|
request.setDestinationUri(Uri.fromFile(file));
|
2016-08-28 22:35:07 +00:00
|
|
|
|
|
|
|
receiver.setDownloadID(downloadManager.enqueue(request));
|
2016-11-20 14:13:29 +00:00
|
|
|
receiver.setFilename(filename);
|
2016-08-28 22:35:07 +00:00
|
|
|
context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
|
|
|
}
|
|
|
|
|
2016-11-20 14:13:29 +00:00
|
|
|
public static String getLegalFilename(CharSequence filename) {
|
|
|
|
return filename.toString().replace(" ", "_").replace("'", "").replace("\"", "")
|
|
|
|
.replace("$", "").replace("`", "").replace("(", "").replace(")", "")
|
|
|
|
.replace("#", "").replace("@", "").replace("*", "");
|
|
|
|
}
|
|
|
|
|
2016-12-25 09:17:20 +00:00
|
|
|
public static String detectBootImage() {
|
|
|
|
String[] commands = {
|
|
|
|
"for PARTITION in kern-a KERN-A android_boot ANDROID_BOOT kernel KERNEL boot BOOT lnx LNX; do",
|
|
|
|
"BOOTIMAGE=`readlink /dev/block/by-name/$PARTITION || readlink /dev/block/platform/*/by-name/$PARTITION || readlink /dev/block/platform/*/*/by-name/$PARTITION`",
|
|
|
|
"if [ ! -z \"$BOOTIMAGE\" ]; then break; fi",
|
|
|
|
"done",
|
|
|
|
"echo \"${BOOTIMAGE##*/}\""
|
|
|
|
};
|
|
|
|
List<String> ret = Shell.su(commands);
|
2017-01-26 05:46:54 +00:00
|
|
|
if (isValidShellResponse(ret))
|
2016-12-25 09:17:20 +00:00
|
|
|
return ret.get(0);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-01-10 14:30:05 +00:00
|
|
|
public static AlertDialog.Builder getAlertDialogBuilder(Context context) {
|
2017-01-25 05:16:50 +00:00
|
|
|
if (Global.Configs.isDarkTheme) {
|
2017-01-10 14:30:05 +00:00
|
|
|
return new AlertDialog.Builder(context, R.style.AlertDialog_dh);
|
|
|
|
} else {
|
|
|
|
return new AlertDialog.Builder(context);
|
|
|
|
}
|
2017-01-04 22:11:09 +00:00
|
|
|
}
|
|
|
|
|
2017-01-08 13:41:19 +00:00
|
|
|
public static boolean lowercaseContains(CharSequence string, CharSequence nonNullLowercaseSearch) {
|
|
|
|
return !TextUtils.isEmpty(string) && string.toString().toLowerCase().contains(nonNullLowercaseSearch);
|
|
|
|
}
|
|
|
|
|
2017-01-26 05:46:54 +00:00
|
|
|
public static boolean isValidShellResponse(List<String> list) {
|
|
|
|
if (list != null && list.size() != 0) {
|
|
|
|
// Check if all empty
|
|
|
|
for (String res : list) {
|
|
|
|
if (!TextUtils.isEmpty(res)) return true;
|
|
|
|
}
|
2016-11-23 14:38:15 +00:00
|
|
|
}
|
2017-01-26 05:46:54 +00:00
|
|
|
return false;
|
2016-11-23 14:38:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 14:22:36 +00:00
|
|
|
}
|