mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-03-28 15:32:16 +00:00
218 lines
8.3 KiB
Java
218 lines
8.3 KiB
Java
package com.topjohnwu.magisk.utils;
|
|
|
|
import android.Manifest;
|
|
import android.app.Activity;
|
|
import android.app.DownloadManager;
|
|
import android.app.admin.DevicePolicyManager;
|
|
import android.content.Context;
|
|
import android.content.IntentFilter;
|
|
import android.content.SharedPreferences;
|
|
import android.content.pm.PackageManager;
|
|
import android.content.res.Configuration;
|
|
import android.database.Cursor;
|
|
import android.net.ConnectivityManager;
|
|
import android.net.NetworkInfo;
|
|
import android.net.Uri;
|
|
import android.os.Build;
|
|
import android.os.Environment;
|
|
import android.provider.OpenableColumns;
|
|
import android.support.annotation.StringRes;
|
|
import android.support.design.widget.Snackbar;
|
|
import android.support.v4.app.ActivityCompat;
|
|
import android.support.v4.content.ContextCompat;
|
|
import android.text.TextUtils;
|
|
import android.widget.Toast;
|
|
|
|
import com.topjohnwu.magisk.MagiskManager;
|
|
import com.topjohnwu.magisk.R;
|
|
import com.topjohnwu.magisk.components.SnackbarMaker;
|
|
import com.topjohnwu.magisk.receivers.DownloadReceiver;
|
|
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Locale;
|
|
|
|
public class Utils {
|
|
|
|
public static final String UTIL_FUNCTIONS= "util_functions.sh";
|
|
public static boolean isDownloading = false;
|
|
|
|
public static boolean itemExist(String path) {
|
|
String command = "[ -e " + path + " ] && echo true || echo false";
|
|
List<String> ret = Shell.su(command);
|
|
return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0));
|
|
}
|
|
|
|
public static void createFile(String path) {
|
|
String folder = path.substring(0, path.lastIndexOf('/'));
|
|
String command = "mkdir -p " + folder + " 2>/dev/null; touch " + path + " 2>/dev/null;";
|
|
Shell.su_raw(command);
|
|
}
|
|
|
|
public static void removeItem(String path) {
|
|
String command = "rm -rf " + path + " 2>/dev/null";
|
|
Shell.su_raw(command);
|
|
}
|
|
|
|
public static List<String> readFile(String path) {
|
|
String command = "cat " + path + " | sed '$a\\ ' | sed '$d'";
|
|
return Shell.su(command);
|
|
}
|
|
|
|
public static void dlAndReceive(Context context, DownloadReceiver receiver, String link, String filename) {
|
|
if (isDownloading)
|
|
return;
|
|
|
|
runWithPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE, () -> {
|
|
File file = new File(Environment.getExternalStorageDirectory() + "/MagiskManager/" + filename);
|
|
|
|
if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs())
|
|
|| (file.exists() && !file.delete())) {
|
|
Toast.makeText(context, R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
|
|
return;
|
|
}
|
|
|
|
Toast.makeText(context, context.getString(R.string.downloading_toast, filename), Toast.LENGTH_LONG).show();
|
|
isDownloading = true;
|
|
|
|
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
|
|
|
if (link != null) {
|
|
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link));
|
|
request.setDestinationUri(Uri.fromFile(file));
|
|
receiver.setDownloadID(downloadManager.enqueue(request));
|
|
}
|
|
receiver.setFilename(filename);
|
|
context.getApplicationContext().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
|
|
});
|
|
}
|
|
|
|
public static String getLegalFilename(CharSequence filename) {
|
|
return filename.toString().replace(" ", "_").replace("'", "").replace("\"", "")
|
|
.replace("$", "").replace("`", "").replace("(", "").replace(")", "")
|
|
.replace("#", "").replace("@", "").replace("*", "");
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static int getPrefsInt(SharedPreferences prefs, String key, int def) {
|
|
return Integer.parseInt(prefs.getString(key, String.valueOf(def)));
|
|
}
|
|
|
|
public static int getPrefsInt(SharedPreferences prefs, String key) {
|
|
return getPrefsInt(prefs, key, 0);
|
|
}
|
|
|
|
public static MagiskManager getMagiskManager(Context context) {
|
|
return (MagiskManager) context.getApplicationContext();
|
|
}
|
|
|
|
public static String getNameFromUri(Context context, Uri uri) {
|
|
String name = null;
|
|
try (Cursor c = context.getContentResolver().query(uri, null, null, null, null)) {
|
|
if (c != null) {
|
|
int nameIndex = c.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
|
if (nameIndex != -1) {
|
|
c.moveToFirst();
|
|
name = c.getString(nameIndex);
|
|
}
|
|
}
|
|
}
|
|
if (name == null) {
|
|
int idx = uri.getPath().lastIndexOf('/');
|
|
name = uri.getPath().substring(idx + 1);
|
|
}
|
|
return name;
|
|
}
|
|
|
|
public static void showUriSnack(Activity activity, Uri uri) {
|
|
SnackbarMaker.make(activity, activity.getString(R.string.internal_storage,
|
|
"/MagiskManager/" + Utils.getNameFromUri(activity, uri)),
|
|
Snackbar.LENGTH_LONG)
|
|
.setAction(R.string.ok, (v)->{}).show();
|
|
}
|
|
|
|
public static boolean checkNetworkStatus() {
|
|
ConnectivityManager manager = (ConnectivityManager)
|
|
MagiskManager.get().getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
|
|
return networkInfo != null && networkInfo.isConnected();
|
|
}
|
|
|
|
public static String getLocaleString(Locale locale, @StringRes int id) {
|
|
Context context = MagiskManager.get();
|
|
Configuration config = context.getResources().getConfiguration();
|
|
config.setLocale(locale);
|
|
Context localizedContext = context.createConfigurationContext(config);
|
|
return localizedContext.getString(id);
|
|
}
|
|
|
|
public static List<Locale> getAvailableLocale() {
|
|
List<Locale> locales = new ArrayList<>();
|
|
HashSet<String> set = new HashSet<>();
|
|
Locale locale;
|
|
|
|
@StringRes int compareId = R.string.download_file_error;
|
|
|
|
// Add default locale
|
|
locales.add(Locale.ENGLISH);
|
|
set.add(getLocaleString(Locale.ENGLISH, compareId));
|
|
|
|
// Add some special locales
|
|
locales.add(Locale.TAIWAN);
|
|
set.add(getLocaleString(Locale.TAIWAN, compareId));
|
|
locale = new Locale("pt", "BR");
|
|
locales.add(locale);
|
|
set.add(getLocaleString(locale, compareId));
|
|
|
|
// Other locales
|
|
for (String s : MagiskManager.get().getAssets().getLocales()) {
|
|
locale = Locale.forLanguageTag(s);
|
|
if (set.add(getLocaleString(locale, compareId))) {
|
|
locales.add(locale);
|
|
}
|
|
}
|
|
|
|
Collections.sort(locales, (l1, l2) -> l1.getDisplayName(l1).compareTo(l2.getDisplayName(l2)));
|
|
|
|
return locales;
|
|
}
|
|
|
|
public static void runWithPermission(Context context, String permission, Runnable callback) {
|
|
if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
|
|
// Passed in context should be an activity if not granted, need to show dialog!
|
|
if (!(context instanceof com.topjohnwu.magisk.components.Activity))
|
|
return;
|
|
com.topjohnwu.magisk.components.Activity activity = (com.topjohnwu.magisk.components.Activity) context;
|
|
activity.setPermissionGrantCallback(callback);
|
|
ActivityCompat.requestPermissions(activity, new String[] { permission }, 0);
|
|
} else {
|
|
callback.run();
|
|
}
|
|
}
|
|
|
|
public static boolean useFDE(Context context) {
|
|
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
|
|
&& context.getSystemService(DevicePolicyManager.class).getStorageEncryptionStatus()
|
|
== DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE_PER_USER;
|
|
}
|
|
|
|
public static Context getEncContext() {
|
|
Context context = MagiskManager.get();
|
|
if (useFDE(context))
|
|
return context.createDeviceProtectedStorageContext();
|
|
else
|
|
return context;
|
|
}
|
|
} |