Class Shell.Builder
- Enclosing class:
- Shell
Shell instances.
Set the default builder for the main shell instance with
Shell.setDefaultBuilder(Builder), or directly use a builder object to create new
Shell instances.
Do not subclass this class! Use create() to get a new Builder object.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract Shellbuild()Combine all of the options that have been set and build a newShellinstance.abstract ShellCombine all of the options that have been set and build a newShellinstance with the provided process.final ShellCombine all of the options that have been set and build a newShellinstance with the provided commands.static Shell.Buildercreate()Create a newShell.Builder.abstract Shell.BuildersetCommands(String... commands) Set the commands that will be used to create a newShell.final Shell.BuildersetContext(Context context) Set theContextto use when creating a shell.abstract Shell.BuildersetFlags(int flags) Set flags to control how a newShellwill be constructed.final Shell.BuildersetInitializers(Class<? extends Shell.Initializer>... classes) Set the desiredShell.Initializers.abstract Shell.BuildersetTimeout(long timeout) Set the maximum time to wait for shell verification.
-
Constructor Details
-
Builder
public Builder()
-
-
Method Details
-
create
Create a newShell.Builder.- Returns:
- a new Builder object.
-
setInitializers
@SafeVarargs @NonNull public final Shell.Builder setInitializers(@NonNull Class<? extends Shell.Initializer>... classes) Set the desiredShell.Initializers.- Parameters:
classes- the classes of desired initializers.- Returns:
- this Builder object for chaining of calls.
- See Also:
-
setFlags
Set flags to control how a newShellwill be constructed.- Parameters:
flags- the desired flags. Value is either 0 or bitwise-or'd value ofShell.FLAG_NON_ROOT_SHELLorShell.FLAG_MOUNT_MASTER- Returns:
- this Builder object for chaining of calls.
-
setTimeout
Set the maximum time to wait for shell verification.After the timeout occurs and the shell still has no response, the shell process will be force-closed and throw
NoShellException.- Parameters:
timeout- the maximum time to wait in seconds. The default timeout is 20 seconds.- Returns:
- this Builder object for chaining of calls.
-
setCommands
Set the commands that will be used to create a newShell.- Parameters:
commands- commands that will be passed toRuntime.exec(String[])to create a newProcess.- Returns:
- this Builder object for chaining of calls.
-
setContext
Set theContextto use when creating a shell.The ContextImpl of the application will be obtained through the provided context, and that will be passed to
Shell.Initializer.onInit(Context, Shell).Calling this method is not usually necessary but recommended, as the library can obtain the current application context through Android internal APIs. However, if your application uses
android.R.attr#sharedUserId, or a shell/root service can be created during the application attach process, then setting a Context explicitly using this method is required.- Parameters:
context- a context of the current package.- Returns:
- this Builder object for chaining of calls.
-
build
Combine all of the options that have been set and build a newShellinstance.If not
setCommands(String...), there are 3 methods to construct a Unix shell; if any method fails, it will fallback to the next method:- If
Shell.FLAG_NON_ROOT_SHELLis not set andShell.FLAG_MOUNT_MASTERis set, construct a Unix shell by callingsu --mount-master. It may fail if the root implementation does not support mount master. - If
Shell.FLAG_NON_ROOT_SHELLis not set, construct a Unix shell by callingsu. It may fail if the device is not rooted, or root permission is not granted. - Construct a Unix shell by calling
sh. This would never fail in normal conditions, but should it fail, it will throwNoShellException
ShellwithShell.getStatus()since it may be constructed with callingsh.If
setCommands(String...)is called, the provided commands will be used to create a newProcessdirectly. If the process fails to create, or the process is not a valid shell, it will throwNoShellException.- Returns:
- the created
Shellinstance. - Throws:
NoShellException- impossible to construct aShellinstance, or initialization failed when using the configuredShell.Initializers.
- If
-
build
Combine all of the options that have been set and build a newShellinstance with the provided commands.- Parameters:
commands- commands that will be passed toRuntime.exec(String[])to create a newProcess.- Returns:
- the built
Shellinstance. - Throws:
NoShellException- the provided command cannot create aShellinstance, or initialization failed when using the configuredShell.Initializers.
-
build
Combine all of the options that have been set and build a newShellinstance with the provided process.- Parameters:
process- a shellProcessthat has already been created.- Returns:
- the built
Shellinstance. - Throws:
NoShellException- the provided process is not a valid shell, or initialization failed when using the configuredShell.Initializers.
-