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-02-12 12:53:41 +00:00
|
|
|
import android.app.Activity;
|
2016-08-28 22:35:07 +00:00
|
|
|
import android.app.DownloadManager;
|
2017-06-06 18:19:23 +00:00
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
2016-08-27 19:52:03 +00:00
|
|
|
import android.content.Context;
|
2017-06-06 18:19:23 +00:00
|
|
|
import android.content.Intent;
|
2016-08-28 22:35:07 +00:00
|
|
|
import android.content.IntentFilter;
|
2017-01-27 22:13:07 +00:00
|
|
|
import android.content.SharedPreferences;
|
2016-08-28 22:35:07 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2017-07-22 14:14:02 +00:00
|
|
|
import android.content.res.Configuration;
|
2017-02-14 21:24:02 +00:00
|
|
|
import android.database.Cursor;
|
2017-05-23 08:51:23 +00:00
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.NetworkInfo;
|
2016-08-28 22:35:07 +00:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Environment;
|
2017-02-14 21:24:02 +00:00
|
|
|
import android.provider.OpenableColumns;
|
2017-07-22 14:14:02 +00:00
|
|
|
import android.support.annotation.StringRes;
|
2017-02-14 21:24:02 +00:00
|
|
|
import android.support.design.widget.Snackbar;
|
2016-08-28 22:35:07 +00:00
|
|
|
import android.support.v4.app.ActivityCompat;
|
2017-05-19 19:04:14 +00:00
|
|
|
import android.support.v4.app.FragmentActivity;
|
2017-08-28 17:56:43 +00:00
|
|
|
import android.support.v4.app.NotificationCompat;
|
2017-06-06 18:19:23 +00:00
|
|
|
import android.support.v4.app.TaskStackBuilder;
|
2017-08-28 19:08:09 +00:00
|
|
|
import android.support.v4.content.ContextCompat;
|
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-02-06 18:01:32 +00:00
|
|
|
import com.topjohnwu.magisk.MagiskManager;
|
2016-08-27 19:52:03 +00:00
|
|
|
import com.topjohnwu.magisk.R;
|
2017-06-06 18:19:23 +00:00
|
|
|
import com.topjohnwu.magisk.SplashActivity;
|
2017-07-20 18:46:02 +00:00
|
|
|
import com.topjohnwu.magisk.asyncs.UpdateRepos;
|
2017-02-14 21:24:02 +00:00
|
|
|
import com.topjohnwu.magisk.components.SnackbarMaker;
|
2016-09-26 02:45:34 +00:00
|
|
|
import com.topjohnwu.magisk.receivers.DownloadReceiver;
|
2017-06-06 18:19:23 +00:00
|
|
|
import com.topjohnwu.magisk.receivers.ManagerUpdate;
|
2016-08-27 19:52:03 +00:00
|
|
|
|
2016-08-28 22:35:07 +00:00
|
|
|
import java.io.File;
|
2017-08-21 19:01:54 +00:00
|
|
|
import java.security.SecureRandom;
|
2017-07-22 14:14:02 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
2016-08-22 08:09:36 +00:00
|
|
|
import java.util.List;
|
2017-07-22 14:14:02 +00:00
|
|
|
import java.util.Locale;
|
2016-08-20 15:26:49 +00:00
|
|
|
|
|
|
|
public class Utils {
|
|
|
|
|
2016-12-27 06:30:26 +00:00
|
|
|
public static boolean isDownloading = false;
|
|
|
|
|
2017-06-06 18:19:23 +00:00
|
|
|
private static final int MAGISK_UPDATE_NOTIFICATION_ID = 1;
|
|
|
|
private static final int APK_UPDATE_NOTIFICATION_ID = 2;
|
|
|
|
|
2017-07-15 17:20:39 +00:00
|
|
|
public static boolean itemExist(Shell shell, String path) {
|
2017-07-17 16:59:22 +00:00
|
|
|
String command = "[ -e " + path + " ] && echo true || echo false";
|
2017-07-15 17:20:39 +00:00
|
|
|
List<String> ret = shell.su(command);
|
2017-02-05 14:02:14 +00:00
|
|
|
return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0));
|
2016-09-25 07:16:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 17:20:39 +00:00
|
|
|
public static void createFile(Shell shell, String path) {
|
2016-11-08 21:17:50 +00:00
|
|
|
String folder = path.substring(0, path.lastIndexOf('/'));
|
2017-07-17 16:59:22 +00:00
|
|
|
String command = "mkdir -p " + folder + " 2>/dev/null; touch " + path + " 2>/dev/null;";
|
2017-07-15 19:32:29 +00:00
|
|
|
shell.su_raw(command);
|
2016-08-24 21:58:15 +00:00
|
|
|
}
|
2016-08-20 15:26:49 +00:00
|
|
|
|
2017-07-15 17:20:39 +00:00
|
|
|
public static void removeItem(Shell shell, String path) {
|
2017-07-17 16:59:22 +00:00
|
|
|
String command = "rm -rf " + path + " 2>/dev/null";
|
2017-07-15 19:32:29 +00:00
|
|
|
shell.su_raw(command);
|
2016-09-17 18:32:08 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 17:20:39 +00:00
|
|
|
public static List<String> getModList(Shell shell, String path) {
|
2017-07-19 16:51:30 +00:00
|
|
|
String command = "ls -d " + path + "/* | grep -v lost+found";
|
2017-07-15 17:20:39 +00:00
|
|
|
return shell.su(command);
|
2016-08-20 15:26:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 17:20:39 +00:00
|
|
|
public static List<String> readFile(Shell shell, String path) {
|
2017-07-19 16:51:30 +00:00
|
|
|
String command = "cat " + path + " | sed '$a\\ ' | sed '$d'";
|
2017-07-15 19:32:29 +00:00
|
|
|
return shell.su(command);
|
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) {
|
2017-02-12 11:49:46 +00:00
|
|
|
if (isDownloading)
|
2016-12-27 06:30:26 +00:00
|
|
|
return;
|
|
|
|
|
2017-08-28 19:08:09 +00:00
|
|
|
runWithPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE, () -> {
|
|
|
|
File file = new File(Environment.getExternalStorageDirectory() + "/MagiskManager/" + filename);
|
2016-12-27 06:30:26 +00:00
|
|
|
|
2017-08-28 19:08:09 +00:00
|
|
|
if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs())
|
|
|
|
|| (file.exists() && !file.delete())) {
|
|
|
|
Toast.makeText(context, R.string.permissionNotGranted, Toast.LENGTH_LONG).show();
|
|
|
|
return;
|
|
|
|
}
|
2016-08-28 22:35:07 +00:00
|
|
|
|
2017-08-28 19:08:09 +00:00
|
|
|
Toast.makeText(context, context.getString(R.string.downloading_toast, filename), Toast.LENGTH_LONG).show();
|
|
|
|
isDownloading = true;
|
2016-12-27 06:30:26 +00:00
|
|
|
|
2017-08-28 19:08:09 +00:00
|
|
|
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
2016-08-28 22:35:07 +00:00
|
|
|
|
2017-08-28 19:08:09 +00:00
|
|
|
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));
|
|
|
|
});
|
2016-08-28 22:35:07 +00:00
|
|
|
}
|
|
|
|
|
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("*", "");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-27 22:13:07 +00:00
|
|
|
public static int getPrefsInt(SharedPreferences prefs, String key, int def) {
|
|
|
|
return Integer.parseInt(prefs.getString(key, String.valueOf(def)));
|
|
|
|
}
|
|
|
|
|
2017-08-28 17:34:42 +00:00
|
|
|
public static int getPrefsInt(SharedPreferences prefs, String key) {
|
|
|
|
return getPrefsInt(prefs, key, 0);
|
|
|
|
}
|
|
|
|
|
2017-02-11 21:02:18 +00:00
|
|
|
public static MagiskManager getMagiskManager(Context context) {
|
|
|
|
return (MagiskManager) context.getApplicationContext();
|
2017-02-05 14:02:14 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 19:04:14 +00:00
|
|
|
public static void checkSafetyNet(FragmentActivity activity) {
|
|
|
|
new SafetyNetHelper(activity) {
|
2017-02-12 11:49:46 +00:00
|
|
|
@Override
|
2017-05-19 19:04:14 +00:00
|
|
|
public void handleResults(Result result) {
|
|
|
|
getMagiskManager(mActivity).SNCheckResult = result;
|
2017-08-28 20:10:04 +00:00
|
|
|
getMagiskManager(mActivity).safetyNetDone.publish(false);
|
2017-02-12 11:49:46 +00:00
|
|
|
}
|
|
|
|
}.requestTest();
|
|
|
|
}
|
2017-02-12 12:53:41 +00:00
|
|
|
|
2017-07-20 16:46:13 +00:00
|
|
|
public static void clearRepoCache(Context context) {
|
2017-09-03 07:35:14 +00:00
|
|
|
MagiskManager mm = getMagiskManager(context);
|
|
|
|
mm.prefs.edit().remove(UpdateRepos.ETAG_KEY).apply();
|
|
|
|
mm.repoDB.clearRepo();
|
|
|
|
mm.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT);
|
2017-02-12 12:53:41 +00:00
|
|
|
}
|
2017-02-14 21:24:02 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2017-05-23 08:51:23 +00:00
|
|
|
|
|
|
|
public static boolean checkNetworkStatus(Context context) {
|
|
|
|
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
|
|
|
|
return networkInfo != null && networkInfo.isConnected();
|
|
|
|
}
|
|
|
|
|
2017-09-03 07:35:14 +00:00
|
|
|
public static void showMagiskUpdate(MagiskManager mm) {
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(mm, MagiskManager.NOTIFICATION_CHANNEL);
|
2017-06-06 18:19:23 +00:00
|
|
|
builder.setSmallIcon(R.drawable.ic_magisk)
|
2017-09-03 07:35:14 +00:00
|
|
|
.setContentTitle(mm.getString(R.string.magisk_update_title))
|
|
|
|
.setContentText(mm.getString(R.string.magisk_update_available, mm.remoteMagiskVersionString))
|
2017-06-06 18:19:23 +00:00
|
|
|
.setVibrate(new long[]{0, 100, 100, 100})
|
|
|
|
.setAutoCancel(true);
|
2017-09-03 07:35:14 +00:00
|
|
|
Intent intent = new Intent(mm, SplashActivity.class);
|
2017-08-29 18:28:24 +00:00
|
|
|
intent.putExtra(MagiskManager.INTENT_SECTION, "magisk");
|
2017-09-03 07:35:14 +00:00
|
|
|
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mm);
|
2017-06-06 18:19:23 +00:00
|
|
|
stackBuilder.addParentStack(SplashActivity.class);
|
|
|
|
stackBuilder.addNextIntent(intent);
|
|
|
|
PendingIntent pendingIntent = stackBuilder.getPendingIntent(MAGISK_UPDATE_NOTIFICATION_ID,
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
builder.setContentIntent(pendingIntent);
|
|
|
|
NotificationManager notificationManager =
|
2017-09-03 07:35:14 +00:00
|
|
|
(NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE);
|
2017-06-06 18:19:23 +00:00
|
|
|
notificationManager.notify(MAGISK_UPDATE_NOTIFICATION_ID, builder.build());
|
|
|
|
}
|
|
|
|
|
2017-09-03 07:35:14 +00:00
|
|
|
public static void showManagerUpdate(MagiskManager mm) {
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(mm, MagiskManager.NOTIFICATION_CHANNEL);
|
2017-06-06 18:19:23 +00:00
|
|
|
builder.setSmallIcon(R.drawable.ic_magisk)
|
2017-09-03 07:35:14 +00:00
|
|
|
.setContentTitle(mm.getString(R.string.manager_update_title))
|
|
|
|
.setContentText(mm.getString(R.string.manager_download_install))
|
2017-06-06 18:19:23 +00:00
|
|
|
.setVibrate(new long[]{0, 100, 100, 100})
|
|
|
|
.setAutoCancel(true);
|
2017-09-03 07:35:14 +00:00
|
|
|
Intent intent = new Intent(mm, ManagerUpdate.class);
|
|
|
|
intent.putExtra(MagiskManager.INTENT_LINK, mm.managerLink);
|
|
|
|
intent.putExtra(MagiskManager.INTENT_VERSION, mm.remoteManagerVersionString);
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(mm,
|
2017-06-06 18:19:23 +00:00
|
|
|
APK_UPDATE_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
builder.setContentIntent(pendingIntent);
|
|
|
|
NotificationManager notificationManager =
|
2017-09-03 07:35:14 +00:00
|
|
|
(NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE);
|
2017-06-06 18:19:23 +00:00
|
|
|
notificationManager.notify(APK_UPDATE_NOTIFICATION_ID, builder.build());
|
|
|
|
}
|
2017-07-20 21:08:39 +00:00
|
|
|
|
|
|
|
public static void enableMagiskHide(Shell shell) {
|
|
|
|
shell.su_raw("magiskhide --enable");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void disableMagiskHide(Shell shell) {
|
|
|
|
shell.su_raw("magiskhide --disable");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<String> listMagiskHide(Shell shell) {
|
|
|
|
return shell.su("magiskhide --ls");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void addMagiskHide(Shell shell, String pkg) {
|
|
|
|
shell.su_raw("magiskhide --add " + pkg);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void rmMagiskHide(Shell shell, String pkg) {
|
|
|
|
shell.su_raw("magiskhide --rm " + pkg);
|
|
|
|
}
|
2017-07-22 14:14:02 +00:00
|
|
|
|
|
|
|
public static String getLocaleString(Context context, Locale locale, @StringRes int id) {
|
|
|
|
Configuration config = context.getResources().getConfiguration();
|
|
|
|
config.setLocale(locale);
|
|
|
|
Context localizedContext = context.createConfigurationContext(config);
|
|
|
|
return localizedContext.getString(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<Locale> getAvailableLocale(Context context) {
|
|
|
|
List<Locale> locales = new ArrayList<>();
|
|
|
|
HashSet<String> set = new HashSet<>();
|
|
|
|
Locale locale;
|
|
|
|
|
2017-08-26 17:38:05 +00:00
|
|
|
int compareId = R.string.download_file_error;
|
|
|
|
|
2017-07-22 14:14:02 +00:00
|
|
|
// Add default locale
|
|
|
|
locales.add(Locale.ENGLISH);
|
2017-08-26 17:38:05 +00:00
|
|
|
set.add(getLocaleString(context, Locale.ENGLISH, compareId));
|
2017-07-22 14:14:02 +00:00
|
|
|
|
|
|
|
// Add some special locales
|
|
|
|
locales.add(Locale.TAIWAN);
|
2017-08-26 17:38:05 +00:00
|
|
|
set.add(getLocaleString(context, Locale.TAIWAN, compareId));
|
2017-07-22 14:14:02 +00:00
|
|
|
locale = new Locale("pt", "BR");
|
|
|
|
locales.add(locale);
|
2017-08-26 17:38:05 +00:00
|
|
|
set.add(getLocaleString(context, locale, compareId));
|
2017-07-22 14:14:02 +00:00
|
|
|
|
|
|
|
// Other locales
|
|
|
|
for (String s : context.getAssets().getLocales()) {
|
|
|
|
locale = Locale.forLanguageTag(s);
|
2017-08-26 17:38:05 +00:00
|
|
|
if (set.add(getLocaleString(context, locale, compareId))) {
|
2017-07-22 14:14:02 +00:00
|
|
|
locales.add(locale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Collections.sort(locales, (l1, l2) -> l1.getDisplayName(l1).compareTo(l2.getDisplayName(l2)));
|
|
|
|
|
|
|
|
return locales;
|
|
|
|
}
|
2017-08-21 19:01:54 +00:00
|
|
|
|
|
|
|
public static String genPackageName(String prefix, int length) {
|
|
|
|
StringBuilder builder = new StringBuilder(length);
|
|
|
|
builder.append(prefix);
|
|
|
|
length -= prefix.length();
|
|
|
|
SecureRandom random = new SecureRandom();
|
|
|
|
String base = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
String alpha = base + base.toUpperCase();
|
|
|
|
String full = alpha + "0123456789..........";
|
|
|
|
char next, prev = '\0';
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
if (prev == '.' || i == length - 1 || i == 0) {
|
|
|
|
next = alpha.charAt(random.nextInt(alpha.length()));
|
|
|
|
} else {
|
|
|
|
next = full.charAt(random.nextInt(full.length()));
|
|
|
|
}
|
|
|
|
builder.append(next);
|
|
|
|
prev = next;
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
2017-08-28 19:08:09 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2016-09-21 14:22:36 +00:00
|
|
|
}
|