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
-
Method Summary
Modifier and TypeMethodDescriptionabstract Shell
build()
Combine all of the options that have been set and build a newShell
instance.abstract Shell
Combine all of the options that have been set and build a newShell
instance with the provided process.final Shell
Combine all of the options that have been set and build a newShell
instance with the provided commands.static Shell.Builder
create()
Create a newShell.Builder
.abstract Shell.Builder
setCommands
(String... commands) Set the commands that will be used to create a newShell
.final Shell.Builder
setContext
(Context context) Set theContext
to use when creating a shell.abstract Shell.Builder
setFlags
(int flags) Set flags to control how a newShell
will be constructed.final Shell.Builder
setInitializers
(Class<? extends Shell.Initializer>... classes) Set the desiredShell.Initializer
s.abstract Shell.Builder
setTimeout
(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.Initializer
s.- Parameters:
classes
- the classes of desired initializers.- Returns:
- this Builder object for chaining of calls.
- See Also:
-
setFlags
Set flags to control how a newShell
will be constructed.- Parameters:
flags
- the desired flags. Value is either 0 or bitwise-or'd value ofShell.FLAG_NON_ROOT_SHELL
orShell.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 theContext
to 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 newShell
instance.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_SHELL
is not set andShell.FLAG_MOUNT_MASTER
is 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_SHELL
is 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
Shell
withShell.getStatus()
since it may be constructed with callingsh
.If
setCommands(String...)
is called, the provided commands will be used to create a newProcess
directly. If the process fails to create, or the process is not a valid shell, it will throwNoShellException
.- Returns:
- the created
Shell
instance. - Throws:
NoShellException
- impossible to construct aShell
instance, or initialization failed when using the configuredShell.Initializer
s.
- If
-
build
Combine all of the options that have been set and build a newShell
instance with the provided commands.- Parameters:
commands
- commands that will be passed toRuntime.exec(String[])
to create a newProcess
.- Returns:
- the built
Shell
instance. - Throws:
NoShellException
- the provided command cannot create aShell
instance, or initialization failed when using the configuredShell.Initializer
s.
-
build
Combine all of the options that have been set and build a newShell
instance with the provided process.- Parameters:
process
- a shellProcess
that has already been created.- Returns:
- the built
Shell
instance. - Throws:
NoShellException
- the provided process is not a valid shell, or initialization failed when using the configuredShell.Initializer
s.
-