/** * TODO: Not tested! * * @param toggle * @throws IOException * @throws TimeoutException * @throws BrokenBusyboxException */ public void toggleAdbDaemon(boolean toggle) throws FailedExecuteCommand, TimeoutException, IOException { SimpleCommand disableAdb = new SimpleCommand("setprop persist.service.adb.enable 0", "stop adbd"); SimpleCommand enableAdb = new SimpleCommand( "setprop persist.service.adb.enable 1", "stop adbd", "sleep 1", "start adbd"); if (toggle) { shell.execCommand(enableAdb); } else { shell.execCommand(disableAdb); } }
/** * Copys a file to a destination. Because cp is not available on all android devices, we use dd or * cat. * * @param source example: /data/data/org.adaway/files/hosts * @param destination example: /system/etc/hosts * @param remountAsRw remounts the destination as read/write before writing to it * @param preservePermissions tries to copy file attributes from source to destination, if only * cat is available only permissions are preserved * @return true if it was successfully copied * @throws BrokenBusyboxException * @throws IOException * @throws TimeoutException */ public void copyFile( File source, File destination, boolean remountAsRw, boolean preservePermissions) throws FailedExecuteCommand, FileNotFoundException, IOException { /* * dd can only copy files, but we can not check if the source is a file without invoking * shell commands, because from Java we probably have no read access, thus we only check if * they are ending with trailing slashes */ if (source.isDirectory() || destination.isDirectory()) { throw new FileNotFoundException("dd can only copy files!"); } // remount destination as read/write before copying to it if (remountAsRw) { if (!remount(destination, "RW")) { Log.d( RootCommands.TAG, "Remounting failed! There is probably no need to remount this partition!"); } } // get permissions of source before overwriting String permissions = null; if (preservePermissions) { permissions = getFilePermissions(source.getAbsolutePath()); } try { shell.execCommand("toolbox dd if=" + source + " of=" + destination); } catch (FailedExecuteCommand e) { shell.execCommand("toolbox cat " + source + " > " + destination); } // set back permissions from source to destination if (preservePermissions) { setFilePermissions(destination, permissions); } // remount destination back to read only if (remountAsRw) { if (!remount(destination, "RO")) { Log.d( RootCommands.TAG, "Remounting failed! There is probably no need to remount this partition!"); } } }
/** * Shutdown or reboot device. Possible actions are REBOOT_HOTREBOOT, REBOOT_REBOOT, * REBOOT_SHUTDOWN, REBOOT_RECOVERY * * @param action * @throws IOException * @throws TimeoutException * @throws BrokenBusyboxException */ public void reboot(int action) throws FailedExecuteCommand, TimeoutException, IOException { if (action == REBOOT_HOTREBOOT) { killAll("system_server"); // or: killAll("zygote"); } else { String command; switch (action) { case REBOOT_REBOOT: command = "reboot"; break; case REBOOT_SHUTDOWN: command = "reboot -p"; break; case REBOOT_RECOVERY: command = "reboot recovery"; break; case REBOOT_BOOTLOADER: command = "reboot bootloader"; break; default: command = "reboot"; break; } shell.execCommand(command); } }
/** * Checks if user accepted root access * * <p>(commands: id) * * @return true if user has given root access */ public boolean isRootAccessGiven() { try { return shell.execCommand("toolbox id").contains("uid=0"); } catch (FailedExecuteCommand failedExecuteCommand) { failedExecuteCommand.printStackTrace(); return false; } }
/** * This method can be used to to check if a process is running * * @param processName name of process to check * @return <code>true</code> if process was found * @throws IOException * @throws BrokenBusyboxException * @throws TimeoutException (Could not determine if the process is running) */ public boolean isProcessRunning(String processName) throws FailedExecuteCommand, TimeoutException, IOException { PsCommand psCommand = new PsCommand(processName); shell.execCommand(psCommand); // if pids are available process is running! return !psCommand.getPids().isEmpty(); }
/** * This method can be used to kill a running process * * <p>(commands: ps, kill) * * @param processName name of process to kill * @return <code>true</code> if process was found and killed successfully * @throws IOException * @throws TimeoutException * @throws BrokenBusyboxException */ public boolean killAll(String processName) throws FailedExecuteCommand, TimeoutException, IOException { Log.d(RootCommands.TAG, "Killing process " + processName); PsCommand psCommand = new PsCommand(processName); shell.execCommand(psCommand); // kill processes if (!psCommand.getPids().isEmpty()) { // example: kill -9 1234 1222 5343 SimpleCommand killCommand = new SimpleCommand("toolbox kill -9 " + psCommand.getPidsString()); shell.execCommand(killCommand); return killCommand.getExitCode() == 0; } else { Log.d(RootCommands.TAG, "No pid found! Nothing was killed!"); return false; } }
/** * This will return a String that represent the symlink for a specified file. * * @param file The path to the file to get the Symlink for. (must have absolute path) * @return A String that represent the symlink for a specified file or null if no symlink exists. * @throws IOException * @throws TimeoutException * @throws BrokenBusyboxException */ public String getSymlink(String file) throws FailedExecuteCommand, TimeoutException, IOException { Log.d(RootCommands.TAG, "Find symlink for " + file); String symlink; LsCommand lsCommand = new LsCommand(file); shell.execCommand(lsCommand); symlink = lsCommand.getSymlink(); return symlink; }
/** * @param file String that represent the file, including the full path to the file and its name. * @return File permissions as String, for example: 777, returns null on error * @throws IOException * @throws TimeoutException * @throws BrokenBusyboxException */ public String getFilePermissions(String file) throws FailedExecuteCommand, BrokenBusyboxException, IOException { Log.d(RootCommands.TAG, "Checking permissions for " + file); String permissions = null; if (fileExists(file)) { Log.d(RootCommands.TAG, file + " was found."); LsCommand lsCommand = new LsCommand(file); shell.execCommand(lsCommand); permissions = lsCommand.getPermissions(); } return permissions; }
public String generateChecksum(File file) throws FailedExecuteCommand { return shell.execCommand("sha1sum " + file.getAbsolutePath()); }
/** * Use this to check whether or not a file exists on the filesystem. * * @param file String that represent the file, including the full path to the file and its name. * @return a boolean that will indicate whether or not the file exists. * @throws IOException * @throws TimeoutException * @throws BrokenBusyboxException */ public boolean fileExists(String file) throws FailedExecuteCommand, IOException { FileExistsCommand fileExistsCommand = new FileExistsCommand(file); shell.execCommand(fileExistsCommand); return fileExistsCommand.isFileExists(); }
public void setFilePermissions(File file, String permissions) throws FailedExecuteCommand { Log.d(RootCommands.TAG, "Set permissions of " + file + " to " + permissions); shell.execCommand("toolbox chmod " + permissions + " \"" + file + "\""); }