com.stericson.RootShell.execution.Shell Java Examples

The following examples show how to use com.stericson.RootShell.execution.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: UsbBroadcastReceiver.java    From USB_Mass_Storage_Enabler with MIT License 6 votes vote down vote up
boolean disableUMS(Context context, boolean removeNotif) {
    try {
        String disableCommand = data.getString(Constants.disableUMScmds, "");
        if(disableCommand.isEmpty()) {
            Toast.makeText(context, context.getString(R.string.toast_open_app_once), Toast.LENGTH_SHORT).show();
            return false;
        }
        Shell rootShell = RootTools.getShell(true);
        Command cmd = new Command(0, disableCommand);
        rootShell.add(cmd);
        //if(!rootShell.isExecuting) rootShell.close();
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(context, context.getString(R.string.error_ums_disable), Toast.LENGTH_SHORT).show();
        return false;
    }
    Toast.makeText(context, context.getString(R.string.toast_ums_disabled), Toast.LENGTH_SHORT).show();
    isUMSdisabled = true;
    if(removeNotif) removeNotification(context);
    else showNotification(context);
    //TODO: if(MainActivity.isAppOpen) MainActivity.updateUSBconfig(); //When toggled from notif
    return true;
}
 
Example #2
Source File: UsbBroadcastReceiver.java    From USB_Mass_Storage_Enabler with MIT License 6 votes vote down vote up
boolean enableUMS(Context context) {
    try {
        String enableCommand = data.getString(Constants.setPermissionCmds, "")+"\n"+data.getString(Constants.enableUMScmds, "");
        if(enableCommand.isEmpty()) {
            Toast.makeText(context, context.getString(R.string.toast_open_app_once), Toast.LENGTH_SHORT).show();
            return false;
        }
        Shell rootShell = RootTools.getShell(true);
        Command cmd = new Command(0, "setenforce 0\n"+enableCommand);
        rootShell.add(cmd);
        //if(!rootShell.isExecuting) rootShell.close();
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(context, context.getString(R.string.error_ums), Toast.LENGTH_SHORT).show();
        openApp(context);
        return false;
    }
    Toast.makeText(context, context.getString(R.string.toast_ums_enabled), Toast.LENGTH_SHORT).show();
    isUMSdisabled = false;
    showNotification(context);
    return true;
}
 
Example #3
Source File: RootToolsInternalMethods.java    From PhoneProfilesPlus with Apache License 2.0 6 votes vote down vote up
/**
 * This method will return the inode number of a file. This method is dependent on having a version of
 * ls that supports the -i parameter.
 *
 * @param file path to the file that you wish to return the inode number
 * @return String The inode number for this file or "" if the inode number could not be found.
 */
public String getInode(String file) {
    try {
        Command command = new Command(Constants.GI, false, "/data/local/ls -i " + file) {

            @Override
            public void commandOutput(int id, String line) {
                if (id == Constants.GI) {
                    if (!line.trim().equals("") && Character.isDigit(line.trim().substring(0, 1).toCharArray()[0])) {
                        InternalVariables.inode = line.trim().split(" ")[0];
                    }
                }

                super.commandOutput(id, line);
            }
        };
        Shell.startRootShell().add(command);
        commandWait(Shell.startRootShell(), command);

        return InternalVariables.inode;
    } catch (Exception ignore) {
        return "";
    }
}
 
Example #4
Source File: RootShell.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
/**
 * Control how many time of retries should request
 *
 * @param timeout The timeout
 * @param retries The number of retries
 *
 * @return <code>true</code> if your app has been given root access.
 * @throws TimeoutException if this operation times out. (cannot determine if access is given)
 */
public static boolean isAccessGiven(int timeout, int retries) {
    final Set<String> ID = new HashSet<>();
    final int IAG = 158;

    try {
        RootShell.log("Checking for Root access");

        Command command = new Command(IAG, false, "id") {
            @Override
            public void commandOutput(int id, String line) {
                if (id == IAG) {
                    ID.addAll(Arrays.asList(line.split(" ")));
                }

                super.commandOutput(id, line);
            }
        };

        Shell shell = Shell.startRootShell(timeout, retries);
        shell.add(command);
        commandWait(shell, command);

        //parse the userid
        for (String userid : ID) {
            RootShell.log(userid);

            if (userid.toLowerCase().contains("uid=0")) {
                RootShell.log("Access Given");
                return true;
            }
        }

        return false;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
 
Example #5
Source File: RootShell.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
/**
 * This will close either the root shell or the standard shell depending on what you specify.
 *
 * @param root a <code>boolean</code> to specify whether to close the root shell or the standard shell.
 */
public static void closeShell(boolean root) throws IOException {
    if (root) {
        Shell.closeRootShell();
    } else {
        Shell.closeShell();
    }
}
 
Example #6
Source File: RootToolsInternalMethods.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
/**
 * This will return an ArrayList of the class Symlink. The class Symlink contains the following
 * property's: path SymlinkPath
 * <p/>
 * These will provide you with any Symlinks in the given path.
 *
 * @param path path to search for Symlinks.
 * @return <code>ArrayList<Symlink></code> an ArrayList of the class Symlink.
 * @throws Exception if we cannot return the Symlinks.
 */
public ArrayList<Symlink> getSymlinks(String path) throws Exception {

    // this command needs find
    if (!checkUtil("find")) {
        throw new Exception();
    }

    InternalVariables.symlinks = new ArrayList<>();

    Command command = new Command(0, false, "find " + path + " -type l -exec ls -l {} \\;") {
        @Override
        public void commandOutput(int id, String line) {
            if (id == Constants.GET_SYMLINKS) {
                RootTools.log(line);

                String[] fields = line.split(" ");
                InternalVariables.symlinks.add(new Symlink(new File(fields[fields.length - 3]), // file
                        new File(fields[fields.length - 1]) // SymlinkPath
                ));

            }

            super.commandOutput(id, line);
        }
    };
    Shell.startRootShell().add(command);
    commandWait(Shell.startRootShell(), command);

    if (InternalVariables.symlinks != null) {
        return InternalVariables.symlinks;
    } else {
        throw new Exception();
    }
}
 
Example #7
Source File: RootToolsInternalMethods.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
/**
 * This will return an ArrayList of the class Mount. The class mount contains the following
 * property's: device mountPoint type flags
 * <p/>
 * These will provide you with any information you need to work with the mount points.
 *
 * @return <code>ArrayList<Mount></code> an ArrayList of the class Mount.
 * @throws Exception if we cannot return the mount points.
 */
public ArrayList<Mount> getMounts() throws Exception {

    InternalVariables.mounts = new ArrayList<>();

    //noinspection ConstantConditions
    if(null == InternalVariables.mounts || InternalVariables.mounts.isEmpty()) {
        Shell shell = RootTools.getShell(true);

        Command cmd = new Command(Constants.GET_MOUNTS,
                false,
                "cat /proc/mounts") {

            @Override
            public void commandOutput(int id, String line) {
                if (id == Constants.GET_MOUNTS) {
                    RootTools.log(line);

                    String[] fields = line.split(" ");

                    if(fields.length > 3) {
                        InternalVariables.mounts.add(new Mount(new File(fields[0]), // device
                                new File(fields[1]), // mountPoint
                                fields[2], // fstype
                                fields[3] // flags
                        ));
                    }
                }

                super.commandOutput(id, line);
            }
        };
        shell.add(cmd);
        this.commandWait(shell, cmd);
    }

    return InternalVariables.mounts;
}
 
Example #8
Source File: Runner.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
public void run()
{
    String privateFilesPath = null;
    try
    {
        privateFilesPath = context.getFilesDir().getCanonicalPath();
    }
    catch (IOException e)
    {
        if (RootTools.debugMode)
        {
            Log.e(LOG_TAG, "Problem occurred while trying to locate private files directory!");
        }
        e.printStackTrace();
    }
    if (privateFilesPath != null)
    {
        try
        {
            Command command = new Command(0, false, privateFilesPath + "/" + binaryName + " " + parameter);
            Shell.startRootShell().add(command);
            commandWait(command);

        }
        catch (Exception ignored)
        {
        }
    }
}
 
Example #9
Source File: BrightnessDialogPreferenceFragmentX.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
private void setAdaptiveBrightness(final float value) {
    if (preference.adaptiveAllowed) {
        /*if (android.os.Build.VERSION.SDK_INT < 23)    // Not working in Android M (exception)
            Settings.System.putFloat(context.getContentResolver(),
                    ActivateProfileHelper.ADAPTIVE_BRIGHTNESS_SETTING_NAME, value);
        else*/ {
            try {
                Settings.System.putFloat(context.getContentResolver(),
                        ActivateProfileHelper.ADAPTIVE_BRIGHTNESS_SETTING_NAME, value);
            } catch (Exception ee) {
                PPApplication.startHandlerThread(/*"BrightnessDialogPreferenceFragmentX.setAdaptiveBrightness"*/);
                final Handler handler = new Handler(PPApplication.handlerThread.getLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        //PPApplication.logE("PPApplication.startHandlerThread", "START run - from=BrightnessDialogPreferenceFragmentX.setAdaptiveBrightness");

                        if ((!ApplicationPreferences.applicationNeverAskForGrantRoot) &&
                                (PPApplication.isRooted(false) && PPApplication.settingsBinaryExists(false))) {
                            synchronized (PPApplication.rootMutex) {
                                String command1 = "settings put system " + ActivateProfileHelper.ADAPTIVE_BRIGHTNESS_SETTING_NAME + " " + value;
                                //if (PPApplication.isSELinuxEnforcing())
                                //	command1 = PPApplication.getSELinuxEnforceCommand(command1, Shell.ShellContext.SYSTEM_APP);
                                Command command = new Command(0, false, command1); //, command2);
                                try {
                                    RootTools.getShell(true, Shell.ShellContext.SYSTEM_APP).add(command);
                                    PPApplication.commandWait(command, "BrightnessDialogPreferenceFragmentX.setAdaptiveBrightness");
                                } catch (Exception e) {
                                    // com.stericson.RootShell.exceptions.RootDeniedException: Root Access Denied
                                    //Log.e("BrightnessDialogPreferenceFragmentX.setAdaptiveBrightness", Log.getStackTraceString(e));
                                    //PPApplication.recordException(e);
                                }
                            }
                        }

                        //PPApplication.logE("PPApplication.startHandlerThread", "END run - from=BrightnessDialogPreferenceFragmentX.setAdaptiveBrightness");
                    }
                });
            }
        }
    }
}
 
Example #10
Source File: RootShell.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
/**
 * This will close the custom shell that you opened.
 */
public static void closeCustomShell() throws IOException {
    Shell.closeCustomShell();
}
 
Example #11
Source File: RootShell.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
/**
 * This will close all open shells.
 */
public static void closeAllShells() throws IOException {
    Shell.closeAll();
}
 
Example #12
Source File: RootToolsInternalMethods.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
/**
 * This will return a String that represent the symlink for a specified file.
 * <p/>
 *
 * @param file file to get the Symlink for. (must have absolute path)
 * @return <code>String</code> a String that represent the symlink for a specified file or an
 * empty string if no symlink exists.
 */
public String getSymlink(String file) {
    RootTools.log("Looking for Symlink for " + file);

    try {
        final List<String> results = new ArrayList<>();

        Command command = new Command(Constants.GSYM, false, "ls -l " + file) {

            @Override
            public void commandOutput(int id, String line) {
                if (id == Constants.GSYM) {
                    if (!line.trim().equals("")) {
                        results.add(line);
                    }
                }

                super.commandOutput(id, line);
            }
        };
        Shell.startRootShell().add(command);
        commandWait(Shell.startRootShell(), command);

        String[] symlink = results.get(0).split(" ");
        if (symlink.length > 2 && symlink[symlink.length - 2].equals("->")) {
            RootTools.log("Symlink found.");

            String final_symlink;

            if (!symlink[symlink.length - 1].equals("") && !symlink[symlink.length - 1].contains("/")) {
                //We assume that we need to get the path for this symlink as it is probably not absolute.
                List<String> paths = RootShell.findBinary(symlink[symlink.length - 1], true);
                if (paths.size() > 0) {
                    //We return the first found location.
                    final_symlink = paths.get(0) + symlink[symlink.length - 1];
                } else {
                    //we could'nt find a path, return the symlink by itself.
                    final_symlink = symlink[symlink.length - 1];
                }
            } else {
                final_symlink = symlink[symlink.length - 1];
            }

            return final_symlink;
        }
    } catch (Exception e) {
        if (RootTools.debugMode) {
            e.printStackTrace();
        }
    }

    RootTools.log("Symlink not found");
    return "";
}
 
Example #13
Source File: RootShell.java    From PhoneProfilesPlus with Apache License 2.0 3 votes vote down vote up
/**
 * This will open or return, if one is already open, a shell, you are responsible for managing the shell, reading the output
 * and for closing the shell when you are done using it.
 *
 * @param root         a <code>boolean</code> to Indicate whether or not you want to open a root shell or a standard shell
 * @param timeout      an <code>int</code> to Indicate the length of time to wait before giving up on opening a shell.
 * @param shellContext the context to execute the shell with
 * @param retry        a <code>int</code> to indicate how many times the ROOT shell should try to open with root priviliges...
 */
public static Shell getShell(boolean root, int timeout, Shell.ShellContext shellContext, int retry) throws IOException, TimeoutException, RootDeniedException {
    if (root) {
        return Shell.startRootShell(timeout, shellContext, retry);
    } else {
        return Shell.startShell(timeout);
    }
}
 
Example #14
Source File: RootTools.java    From PhoneProfilesPlus with Apache License 2.0 2 votes vote down vote up
/**
 * Executes a given command with root access or without depending on the value of the boolean passed.
 * This will also start a root shell or a standard shell without you having to open it specifically.
 * <p/>
 * You will still need to close the shell after you are done using the shell.
 *
 * @param shell   The shell to execute the command on, this can be a root shell or a standard shell.
 * @param command The command to execute in the shell
 *
 * @throws IOException
 */
@SuppressWarnings("unused")
public static void runShellCommand(Shell shell, Command command) throws IOException {
    shell.add(command);
}
 
Example #15
Source File: RootTools.java    From PhoneProfilesPlus with Apache License 2.0 2 votes vote down vote up
/**
 * This will open or return, if one is already open, a shell, you are responsible for managing the shell, reading the output
 * and for closing the shell when you are done using it.
 *
 * @param root a <code>boolean</code> to Indicate whether or not you want to open a root shell or a standard shell
 * @throws TimeoutException
 * @throws com.stericson.RootShell.exceptions.RootDeniedException
 * @throws IOException
 */
public static Shell getShell(boolean root) throws IOException, TimeoutException, RootDeniedException {
    return RootTools.getShell(root, 0);
}
 
Example #16
Source File: RootTools.java    From PhoneProfilesPlus with Apache License 2.0 2 votes vote down vote up
/**
 * This will open or return, if one is already open, a shell, you are responsible for managing the shell, reading the output
 * and for closing the shell when you are done using it.
 *
 * @param root    a <code>boolean</code> to Indicate whether or not you want to open a root shell or a standard shell
 * @param timeout an <code>int</code> to Indicate the length of time to wait before giving up on opening a shell.
 * @throws TimeoutException
 * @throws com.stericson.RootShell.exceptions.RootDeniedException
 * @throws IOException
 */
@SuppressWarnings("WeakerAccess")
public static Shell getShell(boolean root, int timeout) throws IOException, TimeoutException, RootDeniedException {
    return getShell(root, timeout, Shell.defaultContext, 3);
}
 
Example #17
Source File: RootTools.java    From PhoneProfilesPlus with Apache License 2.0 2 votes vote down vote up
/**
 * This will open or return, if one is already open, a shell, you are responsible for managing the shell, reading the output
 * and for closing the shell when you are done using it.
 *
 * @param root         a <code>boolean</code> to Indicate whether or not you want to open a root shell or a standard shell
 * @param shellContext the context to execute the shell with
 * @throws TimeoutException
 * @throws com.stericson.RootShell.exceptions.RootDeniedException
 * @throws IOException
 */
public static Shell getShell(boolean root, Shell.ShellContext shellContext) throws IOException, TimeoutException, RootDeniedException {
    return getShell(root, 0, shellContext, 3);
}
 
Example #18
Source File: RootTools.java    From PhoneProfilesPlus with Apache License 2.0 2 votes vote down vote up
/**
 * This will open or return, if one is already open, a shell, you are responsible for managing the shell, reading the output
 * and for closing the shell when you are done using it.
 *
 * @param root         a <code>boolean</code> to Indicate whether or not you want to open a root shell or a standard shell
 * @param timeout      an <code>int</code> to Indicate the length of time to wait before giving up on opening a shell.
 * @param shellContext the context to execute the shell with
 * @throws TimeoutException
 * @throws com.stericson.RootShell.exceptions.RootDeniedException
 * @throws IOException
 */
@SuppressWarnings("unused")
public static Shell getShell(boolean root, int timeout, Shell.ShellContext shellContext) throws IOException, TimeoutException, RootDeniedException {
    return getShell(root, timeout, shellContext, 3);
}
 
Example #19
Source File: RootTools.java    From PhoneProfilesPlus with Apache License 2.0 2 votes vote down vote up
/**
 * This will open or return, if one is already open, a shell, you are responsible for managing the shell, reading the output
 * and for closing the shell when you are done using it.
 *
 * @param root         a <code>boolean</code> to Indicate whether or not you want to open a root shell or a standard shell
 * @param timeout      an <code>int</code> to Indicate the length of time to wait before giving up on opening a shell.
 * @param shellContext the context to execute the shell with
 * @param retry        a <code>int</code> to indicate how many times the ROOT shell should try to open with root priviliges...
 * @throws TimeoutException
 * @throws com.stericson.RootShell.exceptions.RootDeniedException
 * @throws IOException
 */
@SuppressWarnings("WeakerAccess")
public static Shell getShell(boolean root, int timeout, Shell.ShellContext shellContext, int retry) throws IOException, TimeoutException, RootDeniedException {
    return RootShell.getShell(root, timeout, shellContext, retry);
}
 
Example #20
Source File: RootShell.java    From PhoneProfilesPlus with Apache License 2.0 2 votes vote down vote up
/**
 * This will open or return, if one is already open, a custom shell, you are responsible for managing the shell, reading the output
 * and for closing the shell when you are done using it.
 *
 * @param shellPath a <code>String</code> to Indicate the path to the shell that you want to open.
 * @param timeout   an <code>int</code> to Indicate the length of time before giving up on opening a shell.
 * @throws TimeoutException
 * @throws com.stericson.RootShell.exceptions.RootDeniedException
 * @throws IOException
 */
public static Shell getCustomShell(String shellPath, int timeout) throws IOException, TimeoutException, RootDeniedException
{
    //return RootShell.getCustomShell(shellPath, timeout);
    return Shell.startCustomShell(shellPath, timeout);
}
 
Example #21
Source File: RootShell.java    From PhoneProfilesPlus with Apache License 2.0 2 votes vote down vote up
/**
 * This will open or return, if one is already open, a shell, you are responsible for managing the shell, reading the output
 * and for closing the shell when you are done using it.
 *
 * @param root         a <code>boolean</code> to Indicate whether or not you want to open a root shell or a standard shell
 * @param timeout      an <code>int</code> to Indicate the length of time to wait before giving up on opening a shell.
 * @param shellContext the context to execute the shell with
 */
@SuppressWarnings("unused")
public static Shell getShell(boolean root, int timeout, Shell.ShellContext shellContext) throws IOException, TimeoutException, RootDeniedException {
    return getShell(root, timeout, shellContext, 3);
}
 
Example #22
Source File: RootShell.java    From PhoneProfilesPlus with Apache License 2.0 2 votes vote down vote up
/**
 * This will open or return, if one is already open, a shell, you are responsible for managing the shell, reading the output
 * and for closing the shell when you are done using it.
 *
 * @param root         a <code>boolean</code> to Indicate whether or not you want to open a root shell or a standard shell
 * @param shellContext the context to execute the shell with
 */
@SuppressWarnings("unused")
public static Shell getShell(boolean root, Shell.ShellContext shellContext) throws IOException, TimeoutException, RootDeniedException {
    return getShell(root, 0, shellContext, 3);
}
 
Example #23
Source File: RootShell.java    From PhoneProfilesPlus with Apache License 2.0 2 votes vote down vote up
/**
 * This will open or return, if one is already open, a shell, you are responsible for managing the shell, reading the output
 * and for closing the shell when you are done using it.
 *
 * @param root    a <code>boolean</code> to Indicate whether or not you want to open a root shell or a standard shell
 * @param timeout an <code>int</code> to Indicate the length of time to wait before giving up on opening a shell.
 */
@SuppressWarnings("WeakerAccess")
public static Shell getShell(boolean root, int timeout) throws IOException, TimeoutException, RootDeniedException {
    return getShell(root, timeout, Shell.defaultContext, 3);
}
 
Example #24
Source File: RootShell.java    From PhoneProfilesPlus with Apache License 2.0 2 votes vote down vote up
/**
 * This will open or return, if one is already open, a shell, you are responsible for managing the shell, reading the output
 * and for closing the shell when you are done using it.
 *
 * @param root a <code>boolean</code> to Indicate whether or not you want to open a root shell or a standard shell
 */
public static Shell getShell(boolean root) throws IOException, TimeoutException, RootDeniedException {
    return RootShell.getShell(root, 0);
}
 
Example #25
Source File: RootTools.java    From PhoneProfilesPlus with Apache License 2.0 2 votes vote down vote up
/**
 * This will open or return, if one is already open, a custom shell, you are responsible for managing the shell, reading the output
 * and for closing the shell when you are done using it.
 *
 * @param shellPath a <code>String</code> to Indicate the path to the shell that you want to open.
 * @throws TimeoutException
 * @throws com.stericson.RootShell.exceptions.RootDeniedException
 * @throws IOException
 */
@SuppressWarnings("unused")
public static Shell getCustomShell(String shellPath) throws IOException, TimeoutException, RootDeniedException {
    return RootTools.getCustomShell(shellPath, 10000);
}
 
Example #26
Source File: RootTools.java    From PhoneProfilesPlus with Apache License 2.0 2 votes vote down vote up
/**
 * This will open or return, if one is already open, a custom shell, you are responsible for managing the shell, reading the output
 * and for closing the shell when you are done using it.
 *
 * @param shellPath a <code>String</code> to Indicate the path to the shell that you want to open.
 * @param timeout   an <code>int</code> to Indicate the length of time before giving up on opening a shell.
 * @throws TimeoutException
 * @throws com.stericson.RootShell.exceptions.RootDeniedException
 * @throws IOException
 */
@SuppressWarnings("WeakerAccess")
public static Shell getCustomShell(String shellPath, int timeout) throws IOException, TimeoutException, RootDeniedException {
    return RootShell.getCustomShell(shellPath, timeout);
}