2019-01-30 09:10:12 +01:00
|
|
|
package com.topjohnwu.magisk.utils;
|
2016-08-22 21:50:46 +02:00
|
|
|
|
2016-08-27 21:52:03 +02:00
|
|
|
import android.content.Context;
|
2017-01-27 23:13:07 +01:00
|
|
|
import android.content.SharedPreferences;
|
2018-10-18 01:44:48 +02:00
|
|
|
import android.content.pm.ApplicationInfo;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.content.res.Configuration;
|
|
|
|
import android.content.res.Resources;
|
2017-02-14 22:24:02 +01:00
|
|
|
import android.database.Cursor;
|
2016-08-29 00:35:07 +02:00
|
|
|
import android.net.Uri;
|
2018-08-01 18:41:10 +02:00
|
|
|
import android.os.AsyncTask;
|
2018-12-13 10:35:50 +01:00
|
|
|
import android.os.Build;
|
2017-02-14 22:24:02 +01:00
|
|
|
import android.provider.OpenableColumns;
|
2018-07-31 11:41:54 +02:00
|
|
|
import android.widget.Toast;
|
2016-08-27 21:52:03 +02:00
|
|
|
|
2019-01-30 09:10:12 +01:00
|
|
|
import com.topjohnwu.magisk.App;
|
|
|
|
import com.topjohnwu.magisk.Config;
|
|
|
|
import com.topjohnwu.magisk.Const;
|
|
|
|
import com.topjohnwu.magisk.container.Module;
|
|
|
|
import com.topjohnwu.magisk.container.ValueSortedMap;
|
2018-12-12 11:51:45 +01:00
|
|
|
import com.topjohnwu.net.Networking;
|
2018-10-28 05:54:56 +01:00
|
|
|
import com.topjohnwu.superuser.Shell;
|
2018-08-01 18:41:10 +02:00
|
|
|
import com.topjohnwu.superuser.io.SuFile;
|
2016-08-27 21:52:03 +02:00
|
|
|
|
2017-07-22 16:14:02 +02:00
|
|
|
import java.util.Locale;
|
2018-08-01 18:41:10 +02:00
|
|
|
import java.util.Map;
|
2016-08-20 17:26:49 +02:00
|
|
|
|
|
|
|
public class Utils {
|
|
|
|
|
2018-12-13 10:35:50 +01:00
|
|
|
public static void toast(CharSequence msg, int duration) {
|
|
|
|
App.mainHandler.post(() -> Toast.makeText(App.self, msg, duration).show());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void toast(int resId, int duration) {
|
|
|
|
App.mainHandler.post(() -> Toast.makeText(App.self, resId, duration).show());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String dlString(String url) {
|
|
|
|
String s = Networking.get(url).execForString().getResult();
|
|
|
|
return s == null ? "" : s;
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:13:07 +01:00
|
|
|
public static int getPrefsInt(SharedPreferences prefs, String key, int def) {
|
|
|
|
return Integer.parseInt(prefs.getString(key, String.valueOf(def)));
|
|
|
|
}
|
|
|
|
|
2017-08-28 19:34:42 +02:00
|
|
|
public static int getPrefsInt(SharedPreferences prefs, String key) {
|
|
|
|
return getPrefsInt(prefs, key, 0);
|
|
|
|
}
|
|
|
|
|
2017-02-14 22:24:02 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-19 20:09:08 +01:00
|
|
|
public static int dpInPx(int dp) {
|
2018-12-13 10:35:50 +01:00
|
|
|
float scale = App.self.getResources().getDisplayMetrics().density;
|
2017-11-19 20:09:08 +01:00
|
|
|
return (int) (dp * scale + 0.5);
|
|
|
|
}
|
2017-12-01 19:35:07 +01:00
|
|
|
|
2017-12-08 16:38:03 +01:00
|
|
|
public static String fmt(String fmt, Object... args) {
|
|
|
|
return String.format(Locale.US, fmt, args);
|
|
|
|
}
|
2018-05-05 20:51:23 +02:00
|
|
|
|
2018-12-13 10:35:50 +01:00
|
|
|
public static String getAppLabel(ApplicationInfo info, PackageManager pm) {
|
|
|
|
try {
|
|
|
|
if (info.labelRes > 0) {
|
|
|
|
Resources res = pm.getResourcesForApplication(info);
|
|
|
|
Configuration config = new Configuration();
|
|
|
|
config.setLocale(LocaleManager.locale);
|
|
|
|
res.updateConfiguration(config, res.getDisplayMetrics());
|
|
|
|
return res.getString(info.labelRes);
|
2018-07-30 21:51:11 +02:00
|
|
|
}
|
2018-12-13 10:35:50 +01:00
|
|
|
} catch (Exception ignored) {}
|
|
|
|
return info.loadLabel(pm).toString();
|
2018-07-31 11:41:54 +02:00
|
|
|
}
|
|
|
|
|
2018-12-13 10:35:50 +01:00
|
|
|
public static String getLegalFilename(CharSequence filename) {
|
|
|
|
return filename.toString().replace(" ", "_").replace("'", "").replace("\"", "")
|
|
|
|
.replace("$", "").replace("`", "").replace("*", "").replace("/", "_")
|
|
|
|
.replace("#", "").replace("@", "").replace("\\", "_");
|
2018-07-31 11:41:54 +02:00
|
|
|
}
|
2018-08-01 18:41:10 +02:00
|
|
|
|
|
|
|
public static void loadModules() {
|
|
|
|
Topic.reset(Topic.MODULE_LOAD_DONE);
|
|
|
|
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> {
|
|
|
|
Map<String, Module> moduleMap = new ValueSortedMap<>();
|
|
|
|
SuFile path = new SuFile(Const.MAGISK_PATH);
|
2018-10-23 10:16:32 +02:00
|
|
|
SuFile[] modules = path.listFiles(
|
2018-08-01 18:41:10 +02:00
|
|
|
(file, name) -> !name.equals("lost+found") && !name.equals(".core"));
|
2018-10-23 10:16:32 +02:00
|
|
|
for (SuFile file : modules) {
|
|
|
|
if (file.isFile()) continue;
|
|
|
|
Module module = new Module(Const.MAGISK_PATH + "/" + file.getName());
|
2018-08-01 18:41:10 +02:00
|
|
|
moduleMap.put(module.getId(), module);
|
|
|
|
}
|
|
|
|
Topic.publish(Topic.MODULE_LOAD_DONE, moduleMap);
|
|
|
|
});
|
|
|
|
}
|
2018-10-18 01:44:48 +02:00
|
|
|
|
2018-10-28 05:54:56 +01:00
|
|
|
public static boolean showSuperUser() {
|
|
|
|
return Shell.rootAccess() && (Const.USER_ID == 0 ||
|
2019-01-21 21:49:03 +01:00
|
|
|
(int) Config.get(Config.Key.SU_MULTIUSER_MODE) !=
|
|
|
|
Config.Value.MULTIUSER_MODE_OWNER_MANAGED);
|
2018-10-28 05:54:56 +01:00
|
|
|
}
|
2018-12-02 10:47:57 +01:00
|
|
|
|
2018-12-13 10:35:50 +01:00
|
|
|
public static Context getDEContext() {
|
|
|
|
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ?
|
|
|
|
App.self.createDeviceProtectedStorageContext() : App.self;
|
2018-12-02 10:47:57 +01:00
|
|
|
}
|
2018-12-24 18:08:46 +01:00
|
|
|
|
|
|
|
public static void reboot() {
|
2019-01-21 21:49:03 +01:00
|
|
|
Shell.su("/system/bin/reboot" + (Config.recovery ? " recovery" : "")).submit();
|
2018-12-24 18:08:46 +01:00
|
|
|
}
|
2018-12-13 10:35:50 +01:00
|
|
|
}
|