Update Shell

This commit is contained in:
topjohnwu 2017-08-03 23:33:08 +08:00
parent 8195a4d616
commit 2ecbca303b
10 changed files with 35 additions and 30 deletions

View File

@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.topjohnwu.magisk"

View File

@ -142,7 +142,7 @@ public class MagiskFragment extends Fragment
@Override
public void onDownloadDone(Uri uri) {
if (Shell.rootAccess()) {
magiskManager.shell.su_raw(
Shell.getShell(getActivity()).su_raw(
"rm -f /dev/.magisk",
"echo \"BOOTIMAGE=" + boot + "\" >> /dev/.magisk",
"echo \"KEEPFORCEENCRYPT=" + String.valueOf(enc) + "\" >> /dev/.magisk",
@ -206,7 +206,7 @@ public class MagiskFragment extends Fragment
@Override
public void onFinish() {
progress.setMessage(getString(R.string.reboot_countdown, 0));
magiskManager.shell.su_raw(
Shell.getShell(getActivity()).su_raw(
"mv -f " + uninstaller + " /cache/" + MagiskManager.UNINSTALLER,
"mv -f " + utils + " /data/magisk/" + MagiskManager.UTIL_FUNCTIONS,
"reboot"

View File

@ -135,11 +135,11 @@ public class MagiskLogFragment extends Fragment {
switch (mode) {
case 0:
StringBuildingList logList = new StringBuildingList();
magiskManager.shell.su(logList, "cat " + MAGISK_LOG);
Shell.getShell(magiskManager).su(logList, "cat " + MAGISK_LOG);
return logList.toString();
case 1:
magiskManager.shell.su_raw("echo > " + MAGISK_LOG);
Shell.getShell(magiskManager).su_raw("echo > " + MAGISK_LOG);
SnackbarMaker.make(txtLog, R.string.logs_cleared, Snackbar.LENGTH_SHORT).show();
return "";
@ -160,7 +160,7 @@ public class MagiskLogFragment extends Fragment {
try (FileWriter out = new FileWriter(targetFile)) {
FileWritingList fileWritingList = new FileWritingList(out);
magiskManager.shell.su(fileWritingList, "cat " + MAGISK_LOG);
Shell.getShell(magiskManager).su(fileWritingList, "cat " + MAGISK_LOG);
} catch (IOException e) {
e.printStackTrace();
return false;

View File

@ -188,28 +188,28 @@ public class SettingsActivity extends Activity implements CallbackEvent.Listener
case "disable":
enabled = prefs.getBoolean("disable", false);
if (enabled) {
Utils.createFile(magiskManager.shell, MagiskManager.MAGISK_DISABLE_FILE);
Utils.createFile(Shell.getShell(magiskManager), MagiskManager.MAGISK_DISABLE_FILE);
} else {
Utils.removeItem(magiskManager.shell, MagiskManager.MAGISK_DISABLE_FILE);
Utils.removeItem(Shell.getShell(magiskManager), MagiskManager.MAGISK_DISABLE_FILE);
}
Toast.makeText(getActivity(), R.string.settings_reboot_toast, Toast.LENGTH_LONG).show();
break;
case "magiskhide":
enabled = prefs.getBoolean("magiskhide", false);
if (enabled) {
Utils.enableMagiskHide(magiskManager.shell);
Utils.enableMagiskHide(Shell.getShell(magiskManager));
} else {
Utils.disableMagiskHide(magiskManager.shell);
Utils.disableMagiskHide(Shell.getShell(magiskManager));
}
break;
case "hosts":
enabled = prefs.getBoolean("hosts", false);
if (enabled) {
magiskManager.shell.su_raw(
Shell.getShell(magiskManager).su_raw(
"cp -af /system/etc/hosts /magisk/.core/hosts",
"mount -o bind /magisk/.core/hosts /system/etc/hosts");
} else {
magiskManager.shell.su_raw(
Shell.getShell(magiskManager).su_raw(
"umount -l /system/etc/hosts",
"rm -f /magisk/.core/hosts");
}

View File

@ -55,7 +55,7 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
filter = new ApplicationFilter();
pm = context.getPackageManager();
magiskHideDone = Utils.getMagiskManager(context).magiskHideDone;
shell = Utils.getMagiskManager(context).shell;
shell = Shell.getShell(context);
new LoadApps().exec();
}

View File

@ -2,11 +2,8 @@ package com.topjohnwu.magisk.asyncs;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.WebService;
import java.io.File;
@ -17,16 +14,14 @@ import java.io.InputStream;
public class DownloadBusybox extends ParallelTask<Void, Void, Void> {
private File busybox;
private Shell shell;
public DownloadBusybox(Context context, File bb) {
busybox = bb;
shell = Utils.getMagiskManager(context).shell;
}
@Override
protected Void doInBackground(Void... voids) {
shell.su("rm -rf " + busybox.getParentFile());
getShell().su("rm -rf " + busybox.getParentFile());
busybox.getParentFile().mkdirs();
try {
FileOutputStream out = new FileOutputStream(busybox);
@ -44,7 +39,7 @@ public class DownloadBusybox extends ParallelTask<Void, Void, Void> {
}
out.close();
in.close();
shell.su_raw(
getShell().su_raw(
"chmod -R 755 " + busybox.getParent(),
busybox + " --install -s " + busybox.getParent()
);

View File

@ -1,7 +1,6 @@
package com.topjohnwu.magisk.asyncs;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
@ -83,7 +82,7 @@ public class FlashZip extends ParallelTask<Void, String, Integer> {
}
if (!unzipAndCheck()) return 0;
mList.add(magiskManager.getString(R.string.zip_install_progress_msg, mFilename));
magiskManager.shell.su(mList,
getShell().su(mList,
"BOOTMODE=true sh " + mScriptFile + " dummy 1 " + mCachedFile +
" && echo 'Success!' || echo 'Failed!'"
);
@ -100,7 +99,7 @@ public class FlashZip extends ParallelTask<Void, String, Integer> {
protected void onPostExecute(Integer result) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return;
magiskManager.shell.su_raw(
getShell().su_raw(
"rm -rf " + mCachedFile.getParent(),
"rm -rf " + MagiskManager.TMP_FOLDER_PATH
);

View File

@ -23,10 +23,10 @@ public class LoadModules extends ParallelTask<Void, Void, Void> {
magiskManager.moduleMap = new ValueSortedMap<>();
for (String path : Utils.getModList(magiskManager.shell, MagiskManager.MAGISK_PATH)) {
for (String path : Utils.getModList(getShell(), MagiskManager.MAGISK_PATH)) {
Logger.dev("LoadModules: Adding modules from " + path);
try {
Module module = new Module(magiskManager.shell, path);
Module module = new Module(getShell(), path);
magiskManager.moduleMap.put(module.getId(), module);
} catch (BaseModule.CacheModException ignored) {}
}

View File

@ -38,7 +38,7 @@ public abstract class ParallelTask<Params, Progress, Result> extends AsyncTask<P
protected Shell getShell() {
MagiskManager magiskManager = getMagiskManager();
return magiskManager == null ? null : getMagiskManager().shell;
return magiskManager == null ? null : Shell.getShell(magiskManager);
}
@SuppressWarnings("unchecked")

View File

@ -2,6 +2,8 @@ package com.topjohnwu.magisk.utils;
import android.content.Context;
import com.topjohnwu.magisk.MagiskManager;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
@ -87,7 +89,12 @@ public class Shell {
}
public static Shell getShell(Context context) {
return Utils.getMagiskManager(context).shell;
MagiskManager magiskManager = Utils.getMagiskManager(context);
if (!magiskManager.shell.isValid) {
// Get new shell if needed
magiskManager.shell = getShell();
}
return magiskManager.shell;
}
public static boolean rootAccess() {
@ -102,12 +109,16 @@ public class Shell {
}
public void sh_raw(String... commands) {
sh_raw(false, commands);
}
public void sh_raw(boolean stdout, String... commands) {
if (!isValid) return;
synchronized (shellProcess) {
try {
for (String command : commands) {
Logger.shell(command);
STDIN.write((command + "\n").getBytes("UTF-8"));
STDIN.write((command + (stdout ? "\n" : " >/dev/null\n")).getBytes("UTF-8"));
STDIN.flush();
}
} catch (IOException e) {
@ -130,8 +141,8 @@ public class Shell {
synchronized (shellProcess) {
StreamGobbler out = new StreamGobbler(STDOUT, output);
out.start();
sh_raw(commands);
sh_raw("echo \'-shell-done-\'");
sh_raw(true, commands);
sh_raw(true, "echo \'-shell-done-\'");
try { out.join(); } catch (InterruptedException ignored) {}
}
}