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

404 lines
16 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;
2016-09-16 04:46:10 +00:00
import android.app.ActivityManager;
2016-08-28 22:35:07 +00:00
import android.app.DownloadManager;
2016-08-27 19:52:03 +00:00
import android.content.Context;
2016-08-28 22:35:07 +00:00
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
2016-08-28 22:35:07 +00:00
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
2016-08-28 22:35:07 +00:00
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.Settings;
2016-08-28 22:35:07 +00:00
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
2016-09-08 20:47:10 +00:00
import android.util.Base64;
2016-09-01 21:58:26 +00:00
import android.util.Log;
2016-08-28 22:35:07 +00:00
import android.view.View;
2016-08-27 19:52:03 +00:00
import android.widget.Toast;
import com.kcoppock.broadcasttilesupport.BroadcastTileIntentBuilder;
2016-09-28 17:42:25 +00:00
import com.topjohnwu.magisk.MagiskFragment;
2016-08-27 19:52:03 +00:00
import com.topjohnwu.magisk.R;
2016-09-26 02:45:34 +00:00
import com.topjohnwu.magisk.receivers.DownloadReceiver;
import com.topjohnwu.magisk.receivers.PrivateBroadcastReceiver;
import com.topjohnwu.magisk.services.MonitorService;
2016-09-22 04:36:28 +00:00
import com.topjohnwu.magisk.services.TileServiceCompat;
import com.topjohnwu.magisk.services.TileServiceNewApi;
2016-08-27 19:52:03 +00:00
2016-08-28 22:35:07 +00:00
import java.io.File;
2016-09-08 20:47:10 +00:00
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
2016-09-21 21:55:20 +00:00
import java.util.Arrays;
import java.util.LinkedList;
2016-08-22 08:09:36 +00:00
import java.util.List;
2016-08-20 15:26:49 +00:00
2016-09-08 20:47:10 +00:00
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
2016-08-20 15:26:49 +00:00
public class Utils {
2016-09-08 19:47:04 +00:00
private static final String TAG = "Magisk";
2016-08-28 22:35:07 +00:00
private static final String cryptoPass = "MagiskRox666";
private static final String secret = "GTYybRBTYf5his9kQ16ZNO7qgkBJ/5MyVe4CGceAOIoXgSnnk8FTd4F1dE9p5Eus";
2016-09-21 14:22:36 +00:00
public static void init(Context context) {
List<String> ret = Shell.sh("getprop magisk.version");
if (ret.get(0).isEmpty()) {
2016-09-28 17:42:25 +00:00
MagiskFragment.magiskVersion = -1;
2016-09-21 14:22:36 +00:00
} else {
2016-09-28 17:42:25 +00:00
MagiskFragment.magiskVersion = Integer.parseInt(ret.get(0));
2016-09-21 14:22:36 +00:00
}
2016-09-28 19:37:57 +00:00
String toolPath = context.getApplicationInfo().dataDir + "/tools";
2016-09-29 15:24:31 +00:00
Shell.su("PATH=" + toolPath + ":$PATH");
2016-09-21 14:22:36 +00:00
}
public static boolean itemExist(String path) {
List<String> ret;
String command = "if [ -e " + path + " ]; then echo true; else echo false; fi";
if (Shell.rootAccess()) {
ret = Shell.su(command);
} else {
ret = Shell.sh(command);
}
return Boolean.parseBoolean(ret.get(0));
}
public static boolean commandExists(String s) {
List<String> ret;
String command = "if [ -z $(which " + s + ") ]; then echo false; else echo true; fi";
ret = Shell.sh(command);
2016-09-17 18:32:08 +00:00
return Boolean.parseBoolean(ret.get(0));
}
public static boolean rootEnabled() {
return commandExists("su");
2016-08-22 17:44:34 +00:00
}
2016-09-23 21:22:11 +00:00
public static boolean autoToggleEnabled(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
2016-09-27 16:33:01 +00:00
Logger.dev("Utils: AutoRootEnableCheck is " + preferences.getBoolean("autoRootEnable", false));
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("autoRootEnable", false);
}
public static boolean createFile(String path) {
2016-09-17 18:32:08 +00:00
String command = "touch " + path + " 2>/dev/null; if [ -f " + path + " ]; then echo true; else echo false; fi";
2016-09-28 06:50:26 +00:00
return Shell.rootAccess() && Boolean.parseBoolean(Shell.su(command).get(0));
}
2016-08-20 15:26:49 +00:00
public static boolean removeFile(String path) {
2016-09-17 18:32:08 +00:00
String command = "rm -f " + path + " 2>/dev/null; if [ -f " + path + " ]; then echo false; else echo true; fi";
2016-09-28 06:50:26 +00:00
return Shell.rootAccess() && Boolean.parseBoolean(Shell.su(command).get(0));
2016-09-17 18:32:08 +00:00
}
public static void toggleRoot(Boolean b, Context context) {
2016-09-28 17:42:25 +00:00
if (MagiskFragment.magiskVersion != -1) {
if (b) {
Shell.su("ln -s $(getprop magisk.supath) /magisk/.core/bin", "setprop magisk.root 1");
} else {
Shell.su("rm -rf /magisk/.core/bin", "setprop magisk.root 0");
}
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("enable_quicktile", false)) {
2016-09-29 15:24:31 +00:00
setupQuickSettingsTile(context);
}
2016-09-28 06:50:26 +00:00
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("root", b).apply();
2016-08-20 15:26:49 +00:00
}
}
2016-08-22 08:09:36 +00:00
public static void toggleAutoRoot(Boolean b, Context context) {
2016-09-27 16:33:01 +00:00
Logger.dev("Utils: toggleAutocalled for " + b );
2016-09-28 17:42:25 +00:00
if (MagiskFragment.magiskVersion != -1) {
if (!Utils.hasServicePermission(context)) {
Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
Toast.makeText(context, "Please enable Magisk in accessibility for auto-toggle work.", Toast.LENGTH_LONG).show();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} else {
2016-09-27 16:33:01 +00:00
Logger.dev("Utils: toggleAuto checks passed, setting" + b );
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("autoRootEnable", b).apply();
Intent myServiceIntent = new Intent(context, MonitorService.class);
myServiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (b) {
context.startService(myServiceIntent);
} else {
context.stopService(myServiceIntent);
}
}
}
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("enable_quicktile", false)) {
2016-09-29 15:24:31 +00:00
setupQuickSettingsTile(context);
}
}
static List<String> getModList(String path) {
List<String> ret;
2016-09-21 14:22:36 +00:00
String command = "find " + path + " -type d -maxdepth 1 ! -name \"*.core\" ! -name \"*lost+found\" ! -name \"*magisk\"";
if (Shell.rootAccess()) {
ret = Shell.su(command);
} else {
ret = Shell.sh(command);
}
return ret;
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
}
2016-09-28 06:50:26 +00:00
public static void downloadAndReceive(Context context, DownloadReceiver receiver, String link, String filename) {
File file = new File(Environment.getExternalStorageDirectory() + "/MagiskManager/" + filename);
2016-08-28 22:35:07 +00: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 06:50:26 +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-08-28 22:35:07 +00:00
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link));
2016-09-28 06:50:26 +00:00
request.setDestinationUri(Uri.fromFile(file));
2016-08-28 22:35:07 +00:00
receiver.setDownloadID(downloadManager.enqueue(request));
context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
public static String getToken() {
2016-09-17 18:32:08 +00:00
try {
DESKeySpec keySpec = new DESKeySpec(cryptoPass.getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpec);
byte[] encrypedPwdBytes = Base64.decode(secret, Base64.DEFAULT);
2016-09-17 18:32:08 +00:00
// cipher is not thread safe
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypedValueBytes = (cipher.doFinal(encrypedPwdBytes));
return new String(decrypedValueBytes);
} catch (InvalidKeyException | UnsupportedEncodingException | NoSuchAlgorithmException
| BadPaddingException | NoSuchPaddingException | IllegalBlockSizeException
| InvalidKeySpecException e) {
e.printStackTrace();
}
return secret;
2016-09-17 18:32:08 +00:00
}
2016-09-29 15:24:31 +00:00
public static void setupQuickSettingsTile(Context mContext) {
2016-09-27 16:33:01 +00:00
Logger.dev("Utils: SetupQuickSettings called");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
2016-09-27 16:33:01 +00:00
Logger.dev("Utils: Starting N quick settings service");
2016-09-22 04:36:28 +00:00
Intent serviceIntent = new Intent(mContext, TileServiceNewApi.class);
mContext.startService(serviceIntent);
}
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
2016-09-27 16:33:01 +00:00
Logger.dev("Utils: Marshmallow build detected");
String mLabelString;
int mRootIcon = R.drawable.root_white;
int mAutoRootIcon = R.drawable.ic_autoroot_white;
int mRootDisabled = R.drawable.root_grey;
int mRootsState = CheckRootsState(mContext);
2016-09-27 16:33:01 +00:00
Logger.dev("Utils: Root State returned as " + mRootsState);
final Intent enableBroadcast = new Intent(PrivateBroadcastReceiver.ACTION_ENABLEROOT);
final Intent disableBroadcast = new Intent(PrivateBroadcastReceiver.ACTION_DISABLEROOT);
final Intent autoBroadcast = new Intent(PrivateBroadcastReceiver.ACTION_AUTOROOT);
Intent intent;
int mIcon;
switch (mRootsState) {
case 2:
2016-09-23 21:22:11 +00:00
mLabelString = mContext.getString(R.string.auto_toggle);
mIcon = mAutoRootIcon;
intent = autoBroadcast;
break;
case 1:
mLabelString = "Root enabled";
mIcon = mRootIcon;
intent = disableBroadcast;
break;
case 0:
mLabelString = "Root disabled";
mIcon = mRootDisabled;
intent = enableBroadcast;
break;
default:
mLabelString = "Root disabled";
mIcon = mRootDisabled;
intent = disableBroadcast;
break;
}
Intent tileConfigurationIntent = new BroadcastTileIntentBuilder(mContext, "Magisk")
.setLabel(mLabelString)
.setIconResource(mIcon)
.setOnClickBroadcast(intent)
.setOnLongClickBroadcast(autoBroadcast)
.setVisible(true)
.build();
mContext.sendBroadcast(tileConfigurationIntent);
2016-09-24 15:07:30 +00:00
mContext.startService(new Intent(mContext, TileServiceCompat.class));
}
}
2016-09-29 15:24:31 +00:00
public static boolean installTile(Context context) {
2016-09-22 21:47:54 +00:00
String qsTileId;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
qsTileId = "custom(com.topjohnwu.magisk/.services.TileServiceNewApi)";
} else {
qsTileId = "intent(Magisk)";
}
2016-09-21 21:55:20 +00:00
List<String> lines = Shell.su("settings get secure sysui_qs_tiles");
if (lines != null && lines.size() == 1) {
List<String> tiles = new LinkedList<>(Arrays.asList(lines.get(0).split(",")));
2016-09-27 16:33:01 +00:00
Logger.dev("Utils: Current Tile String is " + tiles);
2016-09-21 21:55:20 +00:00
if (tiles.size() > 1) {
for (String tile : tiles) {
if (tile.equals(qsTileId)) {
2016-09-29 15:24:31 +00:00
return true;
2016-09-21 21:55:20 +00:00
}
}
2016-09-22 21:47:54 +00:00
tiles.add(Math.round(tiles.size() / 2), qsTileId);
2016-09-21 21:55:20 +00:00
String newTiles = TextUtils.join(",", tiles);
2016-09-27 16:33:01 +00:00
Logger.dev("Utils: NewtilesString is " + newTiles);
2016-09-21 21:55:20 +00:00
Shell.su("settings put secure sysui_qs_tiles \"" + newTiles + "\"");
2016-09-22 21:47:54 +00:00
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
Utils.refreshService(context);
}
2016-09-29 15:24:31 +00:00
return true;
2016-09-21 21:55:20 +00:00
}
}
2016-09-29 15:24:31 +00:00
return false;
2016-09-21 21:55:20 +00:00
}
2016-09-29 15:24:31 +00:00
public static boolean uninstallTile(Context context) {
2016-09-21 21:55:20 +00:00
2016-09-22 21:47:54 +00:00
String qsTileId;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
qsTileId = "custom(com.topjohnwu.magisk/.services.TileServiceNewApi)";
} else {
qsTileId = "intent(Magisk)";
}
2016-09-21 21:55:20 +00:00
List<String> lines = Shell.su("settings get secure sysui_qs_tiles");
if (lines != null && lines.size() == 1) {
List<String> tiles = new LinkedList<>(Arrays.asList(lines.get(0).split(",")));
2016-09-21 21:55:20 +00:00
if (tiles.size() > 1) {
boolean isPresent = false;
for (int i = 0; i < tiles.size(); i++) {
if (tiles.get(i).equals(qsTileId)) {
isPresent = true;
tiles.remove(i);
break;
}
}
if (isPresent) {
String newTiles = TextUtils.join(",", tiles);
Shell.su("settings put secure sysui_qs_tiles \"" + newTiles + "\"");
2016-09-22 21:47:54 +00:00
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
Utils.refreshService(context);
}
2016-09-21 21:55:20 +00:00
}
2016-09-29 15:24:31 +00:00
return true;
2016-09-21 21:55:20 +00:00
}
}
2016-09-29 15:24:31 +00:00
return false;
2016-09-21 21:55:20 +00:00
}
2016-09-22 21:47:54 +00:00
private static void refreshService(Context context) {
2016-09-22 04:36:28 +00:00
context.startService(new Intent(context, TileServiceCompat.class));
2016-09-21 21:55:20 +00:00
}
// Gets an overall state for the quick settings tile
// 0 for root disabled, 1 for root enabled (no auto), 2 for auto-root
public static int CheckRootsState(Context mContext) {
2016-09-23 21:22:11 +00:00
if (autoToggleEnabled(mContext)) {
return 2;
} else {
2016-09-20 22:01:20 +00:00
if (rootEnabled()) {
return 1;
} else {
return 0;
}
}
}
// To check if service is enabled
public static boolean hasServicePermission(Context mContext) {
int accessibilityEnabled = 0;
final String service = mContext.getPackageName() + "/" + MonitorService.class.getCanonicalName();
try {
accessibilityEnabled = Settings.Secure.getInt(
mContext.getApplicationContext().getContentResolver(),
android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (Settings.SettingNotFoundException e) {
Log.e(TAG, "Error finding setting, default accessibility to not found: "
+ e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
String settingValue = Settings.Secure.getString(
mContext.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
mStringColonSplitter.setString(settingValue);
while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();
if (accessibilityService.equalsIgnoreCase(service)) {
return true;
}
}
}
} else {
Log.v(TAG, "***ACCESSIBILITY IS DISABLED***");
}
return false;
}
2016-09-16 04:46:10 +00:00
public static boolean isMyServiceRunning(Class<?> serviceClass, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
2016-09-16 04:46:10 +00:00
return false;
}
2016-09-21 14:22:36 +00:00
public interface ItemClickListener {
2016-09-21 14:22:36 +00:00
void onItemClick(View view, int position);
}
2016-09-21 14:22:36 +00:00
}