2016-08-23 05:18:28 +08:00
|
|
|
package com.topjohnwu.magisk.utils;
|
2016-08-23 03:50:46 +08:00
|
|
|
|
2016-08-29 06:35:07 +08:00
|
|
|
import android.Manifest;
|
2017-02-12 20:53:41 +08:00
|
|
|
import android.app.Activity;
|
2016-08-29 06:35:07 +08:00
|
|
|
import android.app.DownloadManager;
|
2016-08-28 03:52:03 +08:00
|
|
|
import android.content.Context;
|
2016-08-29 06:35:07 +08:00
|
|
|
import android.content.IntentFilter;
|
2017-01-28 06:13:07 +08:00
|
|
|
import android.content.SharedPreferences;
|
2016-08-29 06:35:07 +08:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Environment;
|
|
|
|
import android.support.v4.app.ActivityCompat;
|
2017-02-07 06:02:06 +08:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2017-01-08 14:41:19 +01:00
|
|
|
import android.text.TextUtils;
|
2016-08-28 03:52:03 +08:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2017-02-07 02:01:32 +08:00
|
|
|
import com.topjohnwu.magisk.MagiskManager;
|
2016-08-28 03:52:03 +08:00
|
|
|
import com.topjohnwu.magisk.R;
|
2017-02-12 20:53:41 +08:00
|
|
|
import com.topjohnwu.magisk.asyncs.LoadRepos;
|
2017-02-12 23:26:30 +08:00
|
|
|
import com.topjohnwu.magisk.database.RepoDatabaseHelper;
|
2016-09-26 10:45:34 +08:00
|
|
|
import com.topjohnwu.magisk.receivers.DownloadReceiver;
|
2016-08-28 03:52:03 +08:00
|
|
|
|
2016-08-29 06:35:07 +08:00
|
|
|
import java.io.File;
|
2016-08-22 10:09:36 +02:00
|
|
|
import java.util.List;
|
2016-08-20 17:26:49 +02:00
|
|
|
|
|
|
|
public class Utils {
|
|
|
|
|
2016-12-27 14:30:26 +08:00
|
|
|
public static boolean isDownloading = false;
|
|
|
|
|
2016-09-25 21:31:38 +08:00
|
|
|
public static boolean itemExist(String path) {
|
|
|
|
String command = "if [ -e " + path + " ]; then echo true; else echo false; fi";
|
2017-02-05 22:02:14 +08:00
|
|
|
List<String> ret = Shell.su(command);
|
|
|
|
return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0));
|
2016-09-25 02:16:10 -05:00
|
|
|
}
|
|
|
|
|
2016-09-25 21:31:38 +08:00
|
|
|
public static boolean commandExists(String s) {
|
|
|
|
String command = "if [ -z $(which " + s + ") ]; then echo false; else echo true; fi";
|
2017-01-26 13:46:54 +08:00
|
|
|
List<String> ret = Shell.sh(command);
|
|
|
|
return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0));
|
2016-09-18 02:32:08 +08:00
|
|
|
}
|
|
|
|
|
2016-08-25 05:58:15 +08:00
|
|
|
public static boolean createFile(String path) {
|
2016-11-09 05:17:50 +08: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 13:46:54 +08:00
|
|
|
List<String> ret = Shell.su(command);
|
|
|
|
return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0));
|
2016-08-25 05:58:15 +08:00
|
|
|
}
|
2016-08-20 17:26:49 +02:00
|
|
|
|
2016-10-17 10:11:26 +08: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 13:46:54 +08:00
|
|
|
List<String> ret = Shell.su(command);
|
|
|
|
return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0));
|
2016-09-18 02:32:08 +08:00
|
|
|
}
|
|
|
|
|
2016-12-25 17:17:20 +08:00
|
|
|
public static List<String> getModList(String path) {
|
2016-09-21 09:22:36 -05:00
|
|
|
String command = "find " + path + " -type d -maxdepth 1 ! -name \"*.core\" ! -name \"*lost+found\" ! -name \"*magisk\"";
|
2017-02-05 22:02:14 +08:00
|
|
|
return Shell.su(command);
|
2016-08-20 17:26:49 +02:00
|
|
|
}
|
|
|
|
|
2016-08-25 05:58:15 +08:00
|
|
|
public static List<String> readFile(String path) {
|
|
|
|
List<String> ret;
|
2016-09-21 09:22:36 -05:00
|
|
|
String command = "cat " + path;
|
|
|
|
if (Shell.rootAccess()) {
|
|
|
|
ret = Shell.su(command);
|
|
|
|
} else {
|
|
|
|
ret = Shell.sh(command);
|
|
|
|
}
|
2016-08-25 05:58:15 +08:00
|
|
|
return ret;
|
2016-08-23 01:44:34 +08:00
|
|
|
}
|
|
|
|
|
2016-11-20 22:13:29 +08:00
|
|
|
public static void dlAndReceive(Context context, DownloadReceiver receiver, String link, String filename) {
|
2017-02-12 19:49:46 +08:00
|
|
|
if (isDownloading)
|
2016-12-27 14:30:26 +08:00
|
|
|
return;
|
|
|
|
|
2016-08-29 06:35:07 +08: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 14:50:26 +08:00
|
|
|
|
2016-12-27 14:30:26 +08:00
|
|
|
File file = new File(Environment.getExternalStorageDirectory() + "/MagiskManager/" + filename);
|
|
|
|
|
2016-09-28 14:50:26 +08:00
|
|
|
if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs()) || (file.exists() && !file.delete())) {
|
2016-09-29 23:24:31 +08:00
|
|
|
Toast.makeText(context, R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
|
2016-12-27 14:30:26 +08:00
|
|
|
return;
|
2016-09-23 23:25:12 -05:00
|
|
|
}
|
2016-08-29 06:35:07 +08:00
|
|
|
|
2016-12-27 14:30:26 +08:00
|
|
|
Toast.makeText(context, context.getString(R.string.downloading_toast, filename), Toast.LENGTH_LONG).show();
|
|
|
|
isDownloading = true;
|
|
|
|
|
2016-08-29 06:35:07 +08:00
|
|
|
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
|
|
|
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link));
|
2016-09-28 14:50:26 +08:00
|
|
|
request.setDestinationUri(Uri.fromFile(file));
|
2016-08-29 06:35:07 +08:00
|
|
|
|
|
|
|
receiver.setDownloadID(downloadManager.enqueue(request));
|
2016-11-20 22:13:29 +08:00
|
|
|
receiver.setFilename(filename);
|
2016-08-29 06:35:07 +08:00
|
|
|
context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
|
|
|
}
|
|
|
|
|
2016-11-20 22:13:29 +08:00
|
|
|
public static String getLegalFilename(CharSequence filename) {
|
|
|
|
return filename.toString().replace(" ", "_").replace("'", "").replace("\"", "")
|
|
|
|
.replace("$", "").replace("`", "").replace("(", "").replace(")", "")
|
|
|
|
.replace("#", "").replace("@", "").replace("*", "");
|
|
|
|
}
|
|
|
|
|
2016-12-25 17:17:20 +08: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 13:46:54 +08:00
|
|
|
if (isValidShellResponse(ret))
|
2016-12-25 17:17:20 +08:00
|
|
|
return ret.get(0);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-01-10 22:30:05 +08:00
|
|
|
public static AlertDialog.Builder getAlertDialogBuilder(Context context) {
|
2017-02-07 02:01:32 +08:00
|
|
|
if (((MagiskManager) context.getApplicationContext()).isDarkTheme) {
|
2017-01-10 22:30:05 +08:00
|
|
|
return new AlertDialog.Builder(context, R.style.AlertDialog_dh);
|
|
|
|
} else {
|
|
|
|
return new AlertDialog.Builder(context);
|
|
|
|
}
|
2017-01-04 23:11:09 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 14:41:19 +01:00
|
|
|
public static boolean lowercaseContains(CharSequence string, CharSequence nonNullLowercaseSearch) {
|
|
|
|
return !TextUtils.isEmpty(string) && string.toString().toLowerCase().contains(nonNullLowercaseSearch);
|
|
|
|
}
|
|
|
|
|
2017-01-26 13:46:54 +08: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 22:38:15 +08:00
|
|
|
}
|
2017-01-26 13:46:54 +08:00
|
|
|
return false;
|
2016-11-23 22:38:15 +08:00
|
|
|
}
|
|
|
|
|
2017-01-28 06:13:07 +08:00
|
|
|
public static int getPrefsInt(SharedPreferences prefs, String key, int def) {
|
|
|
|
return Integer.parseInt(prefs.getString(key, String.valueOf(def)));
|
|
|
|
}
|
|
|
|
|
2017-02-12 05:02:18 +08:00
|
|
|
public static MagiskManager getMagiskManager(Context context) {
|
|
|
|
return (MagiskManager) context.getApplicationContext();
|
2017-02-05 22:02:14 +08:00
|
|
|
}
|
|
|
|
|
2017-02-12 19:49:46 +08:00
|
|
|
public static void checkSafetyNet(MagiskManager magiskManager) {
|
|
|
|
new SafetyNetHelper(magiskManager) {
|
|
|
|
@Override
|
|
|
|
public void handleResults(int i) {
|
|
|
|
magiskManager.SNCheckResult = i;
|
|
|
|
magiskManager.safetyNetDone.trigger();
|
|
|
|
}
|
|
|
|
}.requestTest();
|
|
|
|
}
|
2017-02-12 20:53:41 +08:00
|
|
|
|
|
|
|
public static void clearRepoCache(Activity activity) {
|
|
|
|
MagiskManager magiskManager = getMagiskManager(activity);
|
2017-02-12 23:26:30 +08:00
|
|
|
magiskManager.prefs.edit().remove(LoadRepos.ETAG_KEY).apply();
|
|
|
|
new RepoDatabaseHelper(activity).clearRepo();
|
2017-02-12 20:53:41 +08:00
|
|
|
Toast.makeText(activity, R.string.repo_cache_cleared, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
2016-09-21 09:22:36 -05:00
|
|
|
}
|