2019-01-30 03:10:12 -05:00
|
|
|
package com.topjohnwu.magisk.utils;
|
2018-06-02 22:00:52 +08:00
|
|
|
|
2019-03-22 02:32:21 -04:00
|
|
|
import android.content.ComponentName;
|
2018-07-27 04:48:32 +08:00
|
|
|
import android.content.Context;
|
2019-04-10 05:03:26 -04:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
2018-07-27 04:48:32 +08:00
|
|
|
|
2019-03-07 03:41:24 -05:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
2019-01-30 03:10:12 -05:00
|
|
|
import com.topjohnwu.magisk.Config;
|
|
|
|
import com.topjohnwu.magisk.Const;
|
2019-03-08 10:19:22 -05:00
|
|
|
import com.topjohnwu.magisk.R;
|
2018-06-02 22:00:52 +08:00
|
|
|
import com.topjohnwu.superuser.Shell;
|
2018-07-31 03:51:11 +08:00
|
|
|
import com.topjohnwu.superuser.ShellUtils;
|
2018-06-02 22:00:52 +08:00
|
|
|
import com.topjohnwu.superuser.io.SuFile;
|
|
|
|
|
2018-07-27 04:48:32 +08:00
|
|
|
import java.io.InputStream;
|
2019-04-10 05:03:26 -04:00
|
|
|
import java.lang.reflect.Array;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2018-07-27 04:48:32 +08:00
|
|
|
|
|
|
|
public class RootUtils extends Shell.Initializer {
|
|
|
|
|
2019-03-22 02:32:21 -04:00
|
|
|
public static void rmAndLaunch(String rm, ComponentName component) {
|
|
|
|
Shell.su(Utils.fmt("(rm_launch %s %s)&", rm, component.flattenToString())).exec();
|
2018-11-27 03:24:26 -05:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:46:44 -04:00
|
|
|
public static void reboot() {
|
|
|
|
Shell.su("/system/bin/reboot" + (Config.recovery ? " recovery" : "")).submit();
|
|
|
|
}
|
|
|
|
|
2019-04-10 05:03:26 -04:00
|
|
|
public static void startActivity(Intent intent) {
|
|
|
|
if (intent.getComponent() == null)
|
|
|
|
return;
|
|
|
|
List<String> args = new ArrayList<>();
|
|
|
|
args.add("am");
|
|
|
|
args.add("start");
|
|
|
|
intentToCommand(intent, args);
|
|
|
|
Shell.su(Utils.argsToCommand(args)).exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void intentToCommand(Intent intent, List<String> args) {
|
|
|
|
if (intent.getAction() != null) {
|
|
|
|
args.add("-a");
|
|
|
|
args.add(intent.getAction());
|
|
|
|
}
|
|
|
|
if (intent.getComponent() != null) {
|
|
|
|
args.add("-n");
|
|
|
|
args.add(intent.getComponent().flattenToString());
|
|
|
|
}
|
|
|
|
if (intent.getData() != null) {
|
|
|
|
args.add("-d");
|
|
|
|
args.add(intent.getDataString());
|
|
|
|
}
|
|
|
|
if (intent.getCategories() != null) {
|
|
|
|
for (String cat : intent.getCategories()) {
|
|
|
|
args.add("-c");
|
|
|
|
args.add(cat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (intent.getType() != null) {
|
|
|
|
args.add("-t");
|
|
|
|
args.add(intent.getType());
|
|
|
|
}
|
|
|
|
Bundle extras = intent.getExtras();
|
|
|
|
if (extras != null) {
|
|
|
|
for (String key : extras.keySet()) {
|
|
|
|
Object val = extras.get(key);
|
|
|
|
if (val == null)
|
|
|
|
continue;
|
|
|
|
Object value = val;
|
|
|
|
String arg;
|
|
|
|
if (val instanceof String) arg = "--es";
|
|
|
|
else if (val instanceof Boolean) arg = "--ez";
|
|
|
|
else if (val instanceof Integer) arg = "--ei";
|
|
|
|
else if (val instanceof Long) arg = "--el";
|
|
|
|
else if (val instanceof Float) arg = "--ef";
|
|
|
|
else if (val instanceof Uri) arg = "--eu";
|
|
|
|
else if (val instanceof ComponentName) {
|
|
|
|
arg = "--ecn";
|
|
|
|
value = ((ComponentName) val).flattenToString();
|
|
|
|
} else if (val instanceof ArrayList) {
|
|
|
|
ArrayList arr = (ArrayList) val;
|
|
|
|
if (arr.size() <= 0)
|
|
|
|
/* Impossible to know the type due to type erasure */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (arr.get(0) instanceof Integer) arg = "--eial";
|
|
|
|
else if (arr.get(0) instanceof Long) arg = "--elal";
|
|
|
|
else if (arr.get(0) instanceof Float) arg = "--efal";
|
|
|
|
else if (arr.get(0) instanceof String) arg = "--esal";
|
|
|
|
else continue; /* Unsupported */
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (Object o : arr) {
|
|
|
|
sb.append(o.toString().replace(",", "\\,"));
|
|
|
|
sb.append(',');
|
|
|
|
}
|
|
|
|
// Remove trailing comma
|
|
|
|
sb.deleteCharAt(sb.length() - 1);
|
|
|
|
value = sb;
|
|
|
|
} else if (val.getClass().isArray()) {
|
|
|
|
if (val instanceof int[]) arg = "--eia";
|
|
|
|
else if (val instanceof long[]) arg = "--ela";
|
|
|
|
else if (val instanceof float[]) arg = "--efa";
|
|
|
|
else if (val instanceof String[]) arg = "--esa";
|
|
|
|
else continue; /* Unsupported */
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
int len = Array.getLength(val);
|
|
|
|
for (int i = 0; i < len; ++i) {
|
|
|
|
sb.append(Array.get(val, i).toString().replace(",", "\\,"));
|
|
|
|
sb.append(',');
|
|
|
|
}
|
|
|
|
// Remove trailing comma
|
|
|
|
sb.deleteCharAt(sb.length() - 1);
|
|
|
|
value = sb;
|
|
|
|
}
|
|
|
|
else continue; /* Unsupported */
|
|
|
|
|
|
|
|
args.add(arg);
|
|
|
|
args.add(key);
|
|
|
|
args.add(value.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args.add("-f");
|
|
|
|
args.add(String.valueOf(intent.getFlags()));
|
|
|
|
}
|
|
|
|
|
2018-07-27 04:48:32 +08:00
|
|
|
@Override
|
|
|
|
public boolean onInit(Context context, @NonNull Shell shell) {
|
2018-08-29 13:31:26 -04:00
|
|
|
Shell.Job job = shell.newJob();
|
2018-07-27 04:48:32 +08:00
|
|
|
if (shell.isRoot()) {
|
2018-11-15 13:57:41 -05:00
|
|
|
job.add(context.getResources().openRawResource(R.raw.util_functions))
|
|
|
|
.add(context.getResources().openRawResource(R.raw.utils));
|
2018-07-31 03:51:11 +08:00
|
|
|
Const.MAGISK_DISABLE_FILE = new SuFile("/cache/.disable_magisk");
|
2019-01-21 15:49:03 -05:00
|
|
|
Config.loadMagiskInfo();
|
2018-08-29 13:31:26 -04:00
|
|
|
} else {
|
|
|
|
InputStream nonroot = context.getResources().openRawResource(R.raw.nonroot_utils);
|
|
|
|
job.add(nonroot);
|
2018-07-27 04:48:32 +08:00
|
|
|
}
|
2018-08-29 13:31:26 -04:00
|
|
|
|
2018-09-20 16:21:22 -04:00
|
|
|
job.add("mount_partitions", "get_flags", "run_migrations", "export BOOTMODE=true").exec();
|
2018-08-29 13:31:26 -04:00
|
|
|
|
2019-01-21 15:49:03 -05:00
|
|
|
Config.keepVerity = Boolean.parseBoolean(ShellUtils.fastCmd("echo $KEEPVERITY"));
|
|
|
|
Config.keepEnc = Boolean.parseBoolean(ShellUtils.fastCmd("echo $KEEPFORCEENCRYPT"));
|
|
|
|
Config.recovery = Boolean.parseBoolean(ShellUtils.fastCmd("echo $RECOVERYMODE"));
|
2018-07-27 04:48:32 +08:00
|
|
|
return true;
|
2018-06-02 22:00:52 +08:00
|
|
|
}
|
|
|
|
}
|