com.topjohnwu.superuser.Shell Java Examples
The following examples show how to use
com.topjohnwu.superuser.Shell.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RootUtils.java From SmartFlasher with GNU General Public License v3.0 | 6 votes |
@NonNull public static String runAndGetError(String command) { StringBuilder sb = new StringBuilder(); List<String> outputs = new ArrayList<>(); List<String> stderr = new ArrayList<>(); try { Shell.su(command).to(outputs, stderr).exec(); outputs.addAll(stderr); if (ShellUtils.isValidOutput(outputs)) { for (String output : outputs) { sb.append(output).append("\n"); } } return removeSuffix(sb.toString()).trim(); } catch (Exception e) { return ""; } }
Example #2
Source File: JobImpl.java From libsu with Apache License 2.0 | 6 votes |
private Shell.Result exec0() { if (out instanceof NOPList) out = new ArrayList<>(); ResultImpl result = new ResultImpl(); result.out = out; result.err = redirect ? out : err; Shell.Task task = shell.newTask(handlers, result); try { shell.execTask(task); } catch (IOException e) { if (e instanceof ShellTerminatedException) { return ResultImpl.SHELL_ERR; } else { InternalUtils.stackTrace(e); return ResultImpl.INSTANCE; } } if (redirect) result.err = null; return result; }
Example #3
Source File: RootUtils.java From SmartPack-Kernel-Manager with GNU General Public License v3.0 | 6 votes |
@NonNull public static String runAndGetError(String command) { StringBuilder sb = new StringBuilder(); List<String> outputs = new ArrayList<>(); List<String> stderr = new ArrayList<>(); try { Shell.su(command).to(outputs, stderr).exec(); outputs.addAll(stderr); if (ShellUtils.isValidOutput(outputs)) { for (String output : outputs) { sb.append(output).append("\n"); } } return Utils.removeSuffix(sb.toString(), "\n").trim(); } catch (Exception e) { return ""; } }
Example #4
Source File: CompileDialogFragment.java From EdXposedManager with GNU General Public License v3.0 | 6 votes |
@Override protected String doInBackground(String... commands) { if (outerRef.get() == null) { return outerRef.get().requireContext().getString(R.string.compile_failed); } // Also get STDERR List<String> stdout = new ArrayList<>(); List<String> stderr = new ArrayList<>(); Shell.Result result = Shell.su(commands).to(stdout, stderr).exec(); List<String> ret; if(stderr.size() > 0) { return "Error: " + TextUtils.join("\n", stderr); } else if(!result.isSuccess()) { // they might don't write to stderr return "Error: " + TextUtils.join("\n", stdout); } else { return TextUtils.join("\n", stdout); } }
Example #5
Source File: InstallApkUtil.java From EdXposedManager with GNU General Public License v3.0 | 6 votes |
@Override protected Integer doInBackground(Void... params) { int returnCode = 0; if (isApkRootInstallOn) { try { String path = "/data/local/tmp/"; String fileName = new File(info.localFilename).getName(); output = Shell.su("cat \"" + info.localFilename + "\">" + path + fileName).exec().getOut(); Shell.Result result = Shell.su("pm install -r -f \"" + path + fileName + "\"").exec(); returnCode = result.getCode(); output = result.getOut(); Shell.su("rm -f " + path + fileName).exec(); } catch (IllegalStateException e) { returnCode = ERROR_ROOT_NOT_GRANTED; } } return returnCode; }
Example #6
Source File: PendingJob.java From libsu with Apache License 2.0 | 6 votes |
@NonNull @Override public Shell.Result exec() { try { shell = (ShellImpl) Shell.getShell(); } catch (NoShellException e) { return ResultImpl.INSTANCE; } if (isSU && !shell.isRoot()) return ResultImpl.INSTANCE; Shell.Result res = super.exec(); if (!retry && res == ResultImpl.SHELL_ERR) { // The cached shell is terminated, try to re-run this task retry = true; return exec(); } return res; }
Example #7
Source File: BaseFragment.java From EdXposedManager with GNU General Public License v3.0 | 6 votes |
private void reboot(String mode) { if (startShell()) return; List<String> messages = new LinkedList<>(); String command = "/system/bin/svc power reboot"; if (mode != null) { command += " " + mode; if (mode.equals("recovery")) // create a flag used by some kernels to boot into recovery Shell.su("touch /cache/recovery/boot").exec(); } Shell.Result result = Shell.su(command).exec(); if (result.getCode() != 0) { messages.add(result.getOut().toString()); messages.add(""); messages.add(getString(R.string.reboot_failed)); showAlert(TextUtils.join("\n", messages).trim()); } }
Example #8
Source File: ShellIO.java From libsu with Apache License 2.0 | 6 votes |
@Override public void setLength(long newLength) throws IOException { if (newLength == 0) { if (!file.clear()) throw new IOException("Cannot clear file"); return; } Shell.getShell().execTask((in, out, err) -> { String cmd = String.format(Locale.ROOT, "dd of='%s' bs=%d seek=1 count=0 2>/dev/null; echo", file.getAbsolutePath(), newLength); InternalUtils.log(TAG, cmd); in.write(cmd.getBytes("UTF-8")); in.write('\n'); in.flush(); // Wait till the operation is done out.read(JUNK); }); }
Example #9
Source File: ShellIO.java From libsu with Apache License 2.0 | 6 votes |
/** * Optimized for stream based I/O */ void streamWrite(byte[] b, int off, int len) throws IOException { Shell.getShell().execTask((in, out, err) -> { String cmd = String.format(Locale.ROOT, "dd bs=%d count=1 >> '%s' 2>/dev/null; echo", len, file.getAbsolutePath()); InternalUtils.log(TAG, cmd); in.write(cmd.getBytes("UTF-8")); in.write('\n'); in.flush(); in.write(b, off, len); in.flush(); // Wait till the operation is done out.read(JUNK); }); fileOff += len; }
Example #10
Source File: ShellIO.java From libsu with Apache License 2.0 | 6 votes |
private void write0(@NonNull byte[] b, int off, int len) throws IOException { Shell.getShell().execTask((in, out, err) -> { String cmd; if (fileOff == 0) { cmd = String.format(Locale.ROOT, "dd of='%s' bs=%d count=1 %s 2>/dev/null; echo", file.getAbsolutePath(), len, WRITE_CONV); } else { cmd = String.format(Locale.ROOT, "dd of='%s' ibs=%d count=1 obs=%d seek=1 %s 2>/dev/null; echo", file.getAbsolutePath(), len, fileOff, WRITE_CONV); } InternalUtils.log(TAG, cmd); in.write(cmd.getBytes("UTF-8")); in.write('\n'); in.flush(); in.write(b, off, len); in.flush(); // Wait till the operation is done out.read(JUNK); }); fileOff += len; }
Example #11
Source File: PendingJob.java From libsu with Apache License 2.0 | 6 votes |
@Override public void submit(Shell.ResultCallback cb) { Shell.getShell(s -> { if (isSU && !s.isRoot() && cb != null) { cb.onResult(ResultImpl.INSTANCE); return; } shell = (ShellImpl) s; super.submit(res -> { if (!retry && res == ResultImpl.SHELL_ERR) { // The cached shell is terminated, try to re-schedule this task retry = true; submit(cb); } else if (cb != null) { cb.onResult(res); } }); }); }
Example #12
Source File: JobImpl.java From libsu with Apache License 2.0 | 5 votes |
@NonNull @Override public Shell.Job to(List<String> output) { out = output; redirect = InternalUtils.hasFlag(Shell.FLAG_REDIRECT_STDERR); return this; }
Example #13
Source File: SuShell.java From WADB with Apache License 2.0 | 5 votes |
public static void run(String[] commands, Callback callback) { Shell.su(commands).to(new CallbackList<String>() { @Override public void onAddElement(String s) { if (callback != null) { callback.onLine(s); } } }).submit(result -> { if (callback != null) { callback.onResult(new Result(result.getCode(), result.getOut())); } }); }
Example #14
Source File: JobImpl.java From libsu with Apache License 2.0 | 5 votes |
@NonNull @Override public Shell.Job to(List<String> stdout, List<String> stderr) { out = stdout; err = stderr; redirect = false; return this; }
Example #15
Source File: JobImpl.java From libsu with Apache License 2.0 | 5 votes |
@NonNull @Override public Shell.Job add(@NonNull InputStream in) { if (in != null) handlers.add(InputHandler.newInstance(in)); return this; }
Example #16
Source File: JobImpl.java From libsu with Apache License 2.0 | 5 votes |
@NonNull @Override public Shell.Job add(@NonNull String... cmds) { if (cmds != null && cmds.length > 0) handlers.add(InputHandler.newInstance(cmds)); return this; }
Example #17
Source File: Env.java From libsu with Apache License 2.0 | 5 votes |
private static Shell checkShell() { Shell shell = Shell.getShell(); int code = shell.hashCode(); if (code != shellHash) { blockdev = null; stat = null; wc = null; shellHash = code; } return shell; }
Example #18
Source File: SuFileOutputStream.java From libsu with Apache License 2.0 | 5 votes |
private static OutputStream getOut(File file, boolean append) throws FileNotFoundException { if (file instanceof SuFile) { return IOFactory.createShellOutputStream((SuFile) file, append); } else { try { // Try normal FileOutputStream return new FileOutputStream(file, append); } catch (FileNotFoundException e) { if (!Shell.rootAccess()) throw e; return IOFactory.createShellOutputStream(new SuFile(file), append); } } }
Example #19
Source File: RootUtils.java From SmartPack-Kernel-Manager with GNU General Public License v3.0 | 5 votes |
public static void closeSU() { try { Objects.requireNonNull(Shell.getCachedShell()).close(); } catch (Exception e) { e.printStackTrace(); } }
Example #20
Source File: RootUtils.java From SmartPack-Kernel-Manager with GNU General Public License v3.0 | 5 votes |
@NonNull public static String runAndGetOutput(String command) { StringBuilder sb = new StringBuilder(); try { List<String> outputs = Shell.su(command).exec().getOut(); if (ShellUtils.isValidOutput(outputs)) { for (String output : outputs) { sb.append(output).append("\n"); } } return Utils.removeSuffix(sb.toString(), "\n").trim(); } catch (Exception e) { return ""; } }
Example #21
Source File: RootFile.java From SmartPack-Kernel-Manager with GNU General Public License v3.0 | 5 votes |
public void write(String text, boolean append) { String[] array = text.split("\\r?\\n"); if (!append) delete(); for (String line : array) { Shell.su("echo '" + line + "' >> " + mFile).exec(); } RootUtils.chmod(mFile, "755"); }
Example #22
Source File: SuShell.java From WADB with Apache License 2.0 | 5 votes |
public synchronized static boolean available() { if (!Shell.rootAccess()) { try { Shell.getShell().close(); } catch (IOException e) { e.printStackTrace(); } Shell.newInstance(); Shell.su("echo test").exec(); } return Shell.rootAccess(); }
Example #23
Source File: SuShell.java From WADB with Apache License 2.0 | 5 votes |
public synchronized static void close() { Shell cached = Shell.getCachedShell(); if (cached != null) { try { cached.close(); } catch (IOException e) { e.printStackTrace(); } } }
Example #24
Source File: BaseFragment.java From EdXposedManager with GNU General Public License v3.0 | 5 votes |
private void softReboot() { if (startShell()) return; List<String> messages = new LinkedList<>(); Shell.Result result = Shell.su("setprop ctl.restart surfaceflinger; setprop ctl.restart zygote").exec(); if (result.getCode() != 0) { messages.add(result.getOut().toString()); messages.add(""); messages.add(getString(R.string.reboot_failed)); showAlert(TextUtils.join("\n", messages).trim()); } }
Example #25
Source File: JobImpl.java From libsu with Apache License 2.0 | 5 votes |
@Override public void submit(Shell.ResultCallback cb) { if (out instanceof NOPList && cb == null) out = null; shell.SERIAL_EXECUTOR.execute(() -> { Shell.Result result = exec0(); if (cb != null) UiThreadHandler.run(() -> cb.onResult(result)); }); }
Example #26
Source File: RootUtils.java From SmartFlasher with GNU General Public License v3.0 | 5 votes |
@NonNull public static String runAndGetOutput(String command) { StringBuilder sb = new StringBuilder(); try { List<String> outputs = Shell.su(command).exec().getOut(); if (ShellUtils.isValidOutput(outputs)) { for (String output : outputs) { sb.append(output).append("\n"); } } return removeSuffix(sb.toString()).trim(); } catch (Exception e) { return ""; } }
Example #27
Source File: NotificationUtil.java From EdXposedManager with GNU General Public License v3.0 | 5 votes |
@Override public void onReceive(Context context, Intent intent) { /* * Close the notification bar in order to see the toast that module * was enabled successfully. Furthermore, if SU permissions haven't * been granted yet, the SU dialog will be prompted behind the * expanded notification panel and is therefore not visible to the * user. */ sContext.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); cancelAll(); if (intent.hasExtra(EXTRA_ACTIVATE_MODULE)) { String packageName = intent.getStringExtra(EXTRA_ACTIVATE_MODULE); ModuleUtil moduleUtil = ModuleUtil.getInstance(); moduleUtil.setModuleEnabled(packageName, true); moduleUtil.updateModulesList(false, null); Toast.makeText(sContext, R.string.module_activated, Toast.LENGTH_SHORT).show(); if (intent.hasExtra(EXTRA_ACTIVATE_MODULE_AND_RETURN)) return; } if (!Shell.rootAccess()) { Log.e(TAG, "NotificationUtil -> Could not start root shell"); return; } boolean isSoftReboot = intent.getBooleanExtra(EXTRA_SOFT_REBOOT, false); Shell.Result result = isSoftReboot ? Shell.su("setprop ctl.restart surfaceflinger; setprop ctl.restart zygote").exec() : Shell.su("svc power reboot").exec(); int returnCode = result.getCode(); if (returnCode != 0) { Log.e(TAG, "NotificationUtil -> Could not reboot:"); for (String line : result.getOut()) { Log.e(TAG, line); } } }
Example #28
Source File: BaseFragment.java From EdXposedManager with GNU General Public License v3.0 | 5 votes |
private boolean startShell() { if (Shell.rootAccess()) return false; showAlert(getString(R.string.root_failed)); return true; }
Example #29
Source File: RootFile.java From SmartPack-Kernel-Manager with GNU General Public License v3.0 | 4 votes |
public void mkdir() { Shell.su("mkdir -p '" + mFile + "'").exec(); }
Example #30
Source File: RootUtils.java From SmartPack-Kernel-Manager with GNU General Public License v3.0 | 4 votes |
public static void chmod(String file, String permission) { Shell.su("chmod " + permission + " " + file).submit(); }