Magisk/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java

251 lines
11 KiB
Java
Raw Normal View History

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-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;
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-06-06 18:19:23 +00:00
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.NotificationCompat;
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-02-12 12:53:41 +00:00
import com.topjohnwu.magisk.asyncs.LoadRepos;
2017-02-14 21:24:02 +00:00
import com.topjohnwu.magisk.components.SnackbarMaker;
2017-02-12 15:26:30 +00:00
import com.topjohnwu.magisk.database.RepoDatabaseHelper;
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;
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;
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;
public static boolean itemExist(String path) {
String command = "if [ -e " + path + " ]; then echo true; else echo false; fi";
2017-02-05 14:02:14 +00:00
List<String> ret = Shell.su(command);
return isValidShellResponse(ret) && Boolean.parseBoolean(ret.get(0));
}
public static void 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";
Shell.su_async(null, command);
}
2016-08-20 15:26:49 +00:00
public static void removeItem(String path) {
2016-10-17 02:11:26 +00:00
String command = "rm -rf " + path + " 2>/dev/null; if [ -e " + path + " ]; then echo false; else echo true; fi";
Shell.su_async(null, command);
2016-09-17 18:32:08 +00:00
}
2016-12-25 09:17:20 +00:00
public static List<String> getModList(String path) {
2016-09-21 14:22:36 +00:00
String command = "find " + path + " -type d -maxdepth 1 ! -name \"*.core\" ! -name \"*lost+found\" ! -name \"*magisk\"";
2017-02-05 14:02:14 +00:00
return Shell.su(command);
2016-08-20 15:26:49 +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);
}
return ret;
2016-08-22 17:44:34 +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-06-06 18:19:23 +00:00
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
2016-08-28 22:35:07 +00:00
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);
2017-06-06 18:19:23 +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-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);
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);
2017-07-13 18:27:02 +00:00
context.getApplicationContext().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
2016-08-28 22:35:07 +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",
2017-06-06 18:19:23 +00:00
"BOOTIMAGE=`readlink /dev/block/by-name/$PARTITION || " +
"readlink /dev/block/platform/*/by-name/$PARTITION || " +
"readlink /dev/block/platform/*/*/by-name/$PARTITION`",
2016-12-25 09:17:20 +00:00
"if [ ! -z \"$BOOTIMAGE\" ]; then break; fi",
"done",
"echo \"$BOOTIMAGE\""
2016-12-25 09:17:20 +00:00
};
List<String> ret = Shell.su(commands);
if (isValidShellResponse(ret)) {
2016-12-25 09:17:20 +00:00
return ret.get(0);
}
2016-12-25 09:17:20 +00:00
return null;
}
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)));
}
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;
getMagiskManager(mActivity).safetyNetDone.trigger();
2017-02-12 11:49:46 +00:00
}
}.requestTest();
}
2017-02-12 12:53:41 +00:00
public static void clearRepoCache(Activity activity) {
MagiskManager magiskManager = getMagiskManager(activity);
2017-02-12 15:26:30 +00:00
magiskManager.prefs.edit().remove(LoadRepos.ETAG_KEY).apply();
new RepoDatabaseHelper(activity).clearRepo();
2017-02-12 12:53:41 +00:00
Toast.makeText(activity, R.string.repo_cache_cleared, Toast.LENGTH_SHORT).show();
}
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();
}
public static boolean checkBits(int bits, int... masks) {
for (int mask : masks) {
if ((bits & mask) == 0)
return false;
}
return true;
}
2017-06-06 18:19:23 +00:00
public static void showMagiskUpdate(MagiskManager magiskManager) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(magiskManager);
builder.setSmallIcon(R.drawable.ic_magisk)
.setContentTitle(magiskManager.getString(R.string.magisk_update_title))
.setContentText(magiskManager.getString(R.string.magisk_update_available, magiskManager.remoteMagiskVersionString))
2017-07-13 18:27:02 +00:00
.setChannelId(MagiskManager.NOTIFICATION_CHANNEL)
2017-06-06 18:19:23 +00:00
.setVibrate(new long[]{0, 100, 100, 100})
.setAutoCancel(true);
Intent intent = new Intent(magiskManager, SplashActivity.class);
intent.putExtra(MagiskManager.INTENT_SECTION, "install");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(magiskManager);
stackBuilder.addParentStack(SplashActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(MAGISK_UPDATE_NOTIFICATION_ID,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) magiskManager.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MAGISK_UPDATE_NOTIFICATION_ID, builder.build());
}
public static void showManagerUpdate(MagiskManager magiskManager) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(magiskManager);
builder.setSmallIcon(R.drawable.ic_magisk)
.setContentTitle(magiskManager.getString(R.string.manager_update_title))
.setContentText(magiskManager.getString(R.string.manager_download_install))
2017-07-13 18:27:02 +00:00
.setChannelId(MagiskManager.NOTIFICATION_CHANNEL)
2017-06-06 18:19:23 +00:00
.setVibrate(new long[]{0, 100, 100, 100})
.setAutoCancel(true);
Intent intent = new Intent(magiskManager, ManagerUpdate.class);
2017-06-08 14:35:30 +00:00
intent.putExtra("link", magiskManager.managerLink);
intent.putExtra("version", magiskManager.remoteManagerVersionString);
2017-06-06 18:19:23 +00:00
PendingIntent pendingIntent = PendingIntent.getBroadcast(magiskManager,
APK_UPDATE_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) magiskManager.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(APK_UPDATE_NOTIFICATION_ID, builder.build());
}
2016-09-21 14:22:36 +00:00
}