Added|Changed!: Rename SESSION_NAME and SESSION_CREATE_MODE to SHELL_NAME and SHELL_CREATE_MODE and add support for ShellCreateMode to AppShells

Renamed extras `TERMUX_APP.TERMUX_SERVICE.EXTRA_SESSION_NAME` to `*.EXTRA_SHELL_NAME`, `TERMUX_APP.RUN_COMMAND_SERVICE.EXTRA_SESSION_NAME` to `*.EXTRA_SHELL_NAME`, `TERMUX_APP.TERMUX_SERVICE.EXTRA_SESSION_CREATE_MODE` to `*.EXTRA_SHELL_CREATE_MODE` and `TERMUX_APP.RUN_COMMAND_SERVICE.EXTRA_SESSION_CREATE_MODE` to `*.EXTRA_SHELL_CREATE_MODE`.

Renamed `enum` class `SessionCreateMode` to `ShellCreateMode`, `sessionName` field to `shellName`, `sessionCreateMode` to `shellCreateMode` in `ExecutionCommand`.

The `TermuxService` `AppShells`/`TermuxTasks` will now consider `ShellCreateMode` as well before starting tasks as done for `TermuxSessions` via 5794ab9a

New task command to not create new foreground session and switch to existing session if one already exits with `shellName` is

```
am startservice --user 0 -n com.termux/com.termux.app.RunCommandService \
-a com.termux.RUN_COMMAND \
--es com.termux.RUN_COMMAND_PATH '/data/data/com.termux/files/usr/bin/bash' \
--es com.termux.RUN_COMMAND_SHELL_CREATE_MODE 'no-shell-with-name' \
--es com.termux.RUN_COMMAND_SHELL_NAME "custom-name"
```

New task command to not create new background task if one already exits with `shellName` is

```
am startservice --user 0 -n com.termux/com.termux.app.RunCommandService \
-a com.termux.RUN_COMMAND \
--es com.termux.RUN_COMMAND_PATH '/data/data/com.termux/files/usr/bin/top' \
--esa com.termux.RUN_COMMAND_ARGUMENTS '-n,5' \
--es com.termux.RUN_COMMAND_SHELL_CREATE_MODE 'no-shell-with-name' \
--es com.termux.RUN_COMMAND_SHELL_NAME "custom-name" \
--es com.termux.RUN_COMMAND_RUNNER "app-shell"
```
This commit is contained in:
agnostic-apollo
2022-06-03 12:09:11 +05:00
parent 46cfea09ec
commit d287734aba
9 changed files with 149 additions and 78 deletions

View File

@@ -105,17 +105,17 @@ public class ExecutionCommand {
}
public enum SessionCreateMode {
public enum ShellCreateMode {
/** Always create {@link TerminalSession}. */
ALWAYS("always"),
/** Create session only if no session with {@link #sessionName} found. */
NO_SESSION_WITH_NAME("no-session-with-name");
/** Create shell only if no shell with {@link #shellName} found. */
NO_SHELL_WITH_NAME("no-shell-with-name");
private final String mode;
SessionCreateMode(final String mode) {
ShellCreateMode(final String mode) {
this.mode = mode;
}
@@ -127,10 +127,10 @@ public class ExecutionCommand {
return sessionCreateMode != null && sessionCreateMode.equals(this.mode);
}
/** Get {@link SessionCreateMode} for {@code mode} if found, otherwise {@code null}. */
/** Get {@link ShellCreateMode} for {@code mode} if found, otherwise {@code null}. */
@Nullable
public static SessionCreateMode modeOf(String mode) {
for (SessionCreateMode v : SessionCreateMode.values()) {
public static ShellCreateMode modeOf(String mode) {
for (ShellCreateMode v : ShellCreateMode.values()) {
if (v.mode.equals(mode)) {
return v;
}
@@ -187,11 +187,12 @@ public class ExecutionCommand {
/** The session action of {@link Runner#TERMINAL_SESSION} commands. */
public String sessionAction;
/** The session name of {@link Runner#TERMINAL_SESSION} commands. */
public String sessionName;
/** The {@link SessionCreateMode} of session for {@link Runner#TERMINAL_SESSION} commands. */
public String sessionCreateMode;
/** The shell name of commands. */
public String shellName;
/** The {@link ShellCreateMode} of commands. */
public String shellCreateMode;
@@ -386,12 +387,12 @@ public class ExecutionCommand {
if (!ignoreNull || executionCommand.sessionAction != null)
logString.append("\n").append(executionCommand.getSessionActionLogString());
if (!ignoreNull || executionCommand.sessionName != null) {
logString.append("\n").append(executionCommand.getSessionNameLogString());
if (!ignoreNull || executionCommand.shellName != null) {
logString.append("\n").append(executionCommand.getShellNameLogString());
}
if (!ignoreNull || executionCommand.sessionCreateMode != null) {
logString.append("\n").append(executionCommand.getSessionCreateModeLogString());
if (!ignoreNull || executionCommand.shellCreateMode != null) {
logString.append("\n").append(executionCommand.getShellCreateModeLogString());
}
if (!ignoreNull || executionCommand.commandIntent != null)
@@ -485,8 +486,9 @@ public class ExecutionCommand {
}
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Session Action", executionCommand.sessionAction, "-"));
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Session Name", executionCommand.sessionName, "-"));
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Session Create Mode", executionCommand.sessionCreateMode, "-"));
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Shell Name", executionCommand.shellName, "-"));
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Shell Create Mode", executionCommand.shellCreateMode, "-"));
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("isPluginExecutionCommand", executionCommand.isPluginExecutionCommand, "-"));
@@ -577,12 +579,12 @@ public class ExecutionCommand {
return Logger.getSingleLineLogStringEntry("Session Action", sessionAction, "-");
}
public String getSessionNameLogString() {
return Logger.getSingleLineLogStringEntry("Session Name", sessionName, "-");
public String getShellNameLogString() {
return Logger.getSingleLineLogStringEntry("Shell Name", shellName, "-");
}
public String getSessionCreateModeLogString() {
return Logger.getSingleLineLogStringEntry("Session Create Mode", sessionCreateMode, "-");
public String getShellCreateModeLogString() {
return Logger.getSingleLineLogStringEntry("Shell Create Mode", shellCreateMode, "-");
}
public String getCommandDescriptionLogString() {

View File

@@ -75,8 +75,15 @@ public final class AppShell {
executionCommand.workingDirectory = "/";
String[] env = shellEnvironmentClient.buildEnvironment(context, executionCommand.isFailsafe, executionCommand.workingDirectory);
// Transform executable path to shell/session name, e.g. "/bin/do-something.sh" => "do-something.sh".
String executableBasename = ShellUtils.getExecutableBasename(executionCommand.executable);
if (executionCommand.shellName == null)
executionCommand.shellName = executableBasename;
final String[] commandArray = shellEnvironmentClient.setupProcessArgs(executionCommand.executable, executionCommand.arguments);
if (executionCommand.commandLabel == null)
executionCommand.commandLabel = executableBasename;
if (!executionCommand.setState(ExecutionState.EXECUTING)) {
executionCommand.setStateFailed(Errno.ERRNO_FAILED.getCode(), context.getString(R.string.error_failed_to_execute_app_shell_command, executionCommand.getCommandIdAndLabelLogString()));
@@ -88,11 +95,6 @@ public final class AppShell {
Logger.logDebugExtended(LOG_TAG, ExecutionCommand.getExecutionInputLogString(executionCommand,
true, Logger.shouldEnableLoggingForCustomLogLevel(executionCommand.backgroundCustomLogLevel)));
String taskName = ShellUtils.getExecutableBasename(executionCommand.executable);
if (executionCommand.commandLabel == null)
executionCommand.commandLabel = taskName;
// Exec the process
final Process process;
try {

View File

@@ -11,7 +11,7 @@ import java.util.Formatter;
import java.util.List;
/*
* Version: v0.45.0
* Version: v0.46.0
* SPDX-License-Identifier: MIT
*
* Changelog
@@ -251,6 +251,12 @@ import java.util.List;
*
* - 0.45.0 (2022-06-01)
* - Added `TERMUX_APP.BUILD_CONFIG_CLASS_NAME`.
*
* - 0.46.0 (2022-06-03)
* - Rename `TERMUX_APP.TERMUX_SERVICE.EXTRA_SESSION_NAME` to `*.EXTRA_SHELL_NAME`,
* `TERMUX_APP.RUN_COMMAND_SERVICE.EXTRA_SESSION_NAME` to `*.EXTRA_SHELL_NAME`,
* `TERMUX_APP.TERMUX_SERVICE.EXTRA_SESSION_CREATE_MODE` to `*.EXTRA_SHELL_CREATE_MODE` and
* `TERMUX_APP.RUN_COMMAND_SERVICE.EXTRA_SESSION_CREATE_MODE` to `*.EXTRA_SHELL_CREATE_MODE`.
*/
/**
@@ -992,10 +998,10 @@ public final class TermuxConstants {
public static final String EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL = TERMUX_PACKAGE_NAME + ".execute.background_custom_log_level"; // Default: "com.termux.execute.background_custom_log_level"
/** Intent {@code String} extra for session action for {@link Runner#TERMINAL_SESSION} commands for the TERMUX_SERVICE.ACTION_SERVICE_EXECUTE intent */
public static final String EXTRA_SESSION_ACTION = TERMUX_PACKAGE_NAME + ".execute.session_action"; // Default: "com.termux.execute.session_action"
/** Intent {@code String} extra for session name for {@link Runner#TERMINAL_SESSION} commands for the TERMUX_SERVICE.ACTION_SERVICE_EXECUTE intent */
public static final String EXTRA_SESSION_NAME = TERMUX_PACKAGE_NAME + ".execute.session_name"; // Default: "com.termux.execute.session_name"
/** Intent {@code String} extra for the {@link ExecutionCommand.SessionCreateMode} for the TERMUX_SERVICE.ACTION_SERVICE_EXECUTE intent. */
public static final String EXTRA_SESSION_CREATE_MODE = TERMUX_PACKAGE_NAME + ".execute.session_create_mode"; // Default: "com.termux.execute.session_create_mode"
/** Intent {@code String} extra for shell name for commands for the TERMUX_SERVICE.ACTION_SERVICE_EXECUTE intent */
public static final String EXTRA_SHELL_NAME = TERMUX_PACKAGE_NAME + ".execute.shell_name"; // Default: "com.termux.execute.shell_name"
/** Intent {@code String} extra for the {@link ExecutionCommand.ShellCreateMode} for the TERMUX_SERVICE.ACTION_SERVICE_EXECUTE intent. */
public static final String EXTRA_SHELL_CREATE_MODE = TERMUX_PACKAGE_NAME + ".execute.shell_create_mode"; // Default: "com.termux.execute.shell_create_mode"
/** Intent {@code String} extra for label of the command for the TERMUX_SERVICE.ACTION_SERVICE_EXECUTE intent */
public static final String EXTRA_COMMAND_LABEL = TERMUX_PACKAGE_NAME + ".execute.command_label"; // Default: "com.termux.execute.command_label"
/** Intent markdown {@code String} extra for description of the command for the TERMUX_SERVICE.ACTION_SERVICE_EXECUTE intent */
@@ -1133,10 +1139,10 @@ public final class TermuxConstants {
public static final String EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL = TERMUX_PACKAGE_NAME + ".RUN_COMMAND_BACKGROUND_CUSTOM_LOG_LEVEL"; // Default: "com.termux.RUN_COMMAND_BACKGROUND_CUSTOM_LOG_LEVEL"
/** Intent {@code String} extra for session action of {@link Runner#TERMINAL_SESSION} commands for the RUN_COMMAND_SERVICE.ACTION_RUN_COMMAND intent */
public static final String EXTRA_SESSION_ACTION = TERMUX_PACKAGE_NAME + ".RUN_COMMAND_SESSION_ACTION"; // Default: "com.termux.RUN_COMMAND_SESSION_ACTION"
/** Intent {@code String} extra for session name of {@link Runner#TERMINAL_SESSION} commands for the RUN_COMMAND_SERVICE.ACTION_RUN_COMMAND intent */
public static final String EXTRA_SESSION_NAME = TERMUX_PACKAGE_NAME + ".RUN_COMMAND_SESSION_NAME"; // Default: "com.termux.RUN_COMMAND_SESSION_NAME"
/** Intent {@code String} extra for the {@link ExecutionCommand.SessionCreateMode} for the RUN_COMMAND_SERVICE.ACTION_RUN_COMMAND intent. */
public static final String EXTRA_SESSION_CREATE_MODE = TERMUX_PACKAGE_NAME + ".RUN_COMMAND_SESSION_CREATE_MODE"; // Default: "com.termux.RUN_COMMAND_SESSION_CREATE_MODE"
/** Intent {@code String} extra for shell name of commands for the RUN_COMMAND_SERVICE.ACTION_RUN_COMMAND intent */
public static final String EXTRA_SHELL_NAME = TERMUX_PACKAGE_NAME + ".RUN_COMMAND_SHELL_NAME"; // Default: "com.termux.RUN_COMMAND_SHELL_NAME"
/** Intent {@code String} extra for the {@link ExecutionCommand.ShellCreateMode} for the RUN_COMMAND_SERVICE.ACTION_RUN_COMMAND intent. */
public static final String EXTRA_SHELL_CREATE_MODE = TERMUX_PACKAGE_NAME + ".RUN_COMMAND_SHELL_CREATE_MODE"; // Default: "com.termux.RUN_COMMAND_SHELL_CREATE_MODE"
/** Intent {@code String} extra for label of the command for the RUN_COMMAND_SERVICE.ACTION_RUN_COMMAND intent */
public static final String EXTRA_COMMAND_LABEL = TERMUX_PACKAGE_NAME + ".RUN_COMMAND_COMMAND_LABEL"; // Default: "com.termux.RUN_COMMAND_COMMAND_LABEL"
/** Intent markdown {@code String} extra for description of the command for the RUN_COMMAND_SERVICE.ACTION_RUN_COMMAND intent */

View File

@@ -7,6 +7,7 @@ import android.content.Context;
import android.os.Build;
import android.os.Environment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.termux.shared.R;
@@ -103,6 +104,28 @@ public class TermuxPluginUtils {
executionCommand.setState(ExecutionCommand.ExecutionState.SUCCESS);
}
/**
* Set {@link ExecutionCommand} state to {@link Errno#ERRNO_FAILED} with {@code errmsg} and
* process error with {@link #processPluginExecutionCommandError(Context, String, ExecutionCommand, boolean)}.
*
*
* @param context The {@link Context} for operations.
* @param logTag The log tag to use for logging.
* @param executionCommand The {@link ExecutionCommand} that failed.
* @param forceNotification If set to {@code true}, then a flash and notification will be shown
* regardless of if pending intent is {@code null} or
* {@link TERMUX_APP#KEY_PLUGIN_ERROR_NOTIFICATIONS_ENABLED}
* is {@code false}.
* @param errmsg The error message to set.
*/
public static void setAndProcessPluginExecutionCommandError(final Context context, String logTag,
final ExecutionCommand executionCommand,
boolean forceNotification,
@NonNull String errmsg) {
executionCommand.setStateFailed(Errno.ERRNO_FAILED.getCode(), errmsg);
processPluginExecutionCommandError(context, logTag, executionCommand, forceNotification);
}
/**
* Process {@link ExecutionCommand} error.
*
@@ -128,7 +151,9 @@ public class TermuxPluginUtils {
* {@link TERMUX_APP#KEY_PLUGIN_ERROR_NOTIFICATIONS_ENABLED}
* is {@code false}.
*/
public static void processPluginExecutionCommandError(final Context context, String logTag, final ExecutionCommand executionCommand, boolean forceNotification) {
public static void processPluginExecutionCommandError(final Context context, String logTag,
final ExecutionCommand executionCommand,
boolean forceNotification) {
if (context == null || executionCommand == null) return;
logTag = DataUtils.getDefaultIfNull(logTag, LOG_TAG);

View File

@@ -131,8 +131,8 @@ public class TermuxSession {
Logger.logDebug(LOG_TAG, "Running \"" + executionCommand.getCommandIdAndLabelLogString() + "\" TermuxSession");
TerminalSession terminalSession = new TerminalSession(executionCommand.executable, executionCommand.workingDirectory, executionCommand.arguments, environment, executionCommand.terminalTranscriptRows, terminalSessionClient);
if (executionCommand.sessionName != null) {
terminalSession.mSessionName = executionCommand.sessionName;
if (executionCommand.shellName != null) {
terminalSession.mSessionName = executionCommand.shellName;
}
return new TermuxSession(terminalSession, executionCommand, termuxSessionClient, setStdoutOnExit);