这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/src/main/java/com/termux/app/RunCommandService.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {

executionCommand.backgroundCustomLogLevel = IntentUtils.getIntegerExtraIfSet(intent, RUN_COMMAND_SERVICE.EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL, null);
executionCommand.sessionAction = intent.getStringExtra(RUN_COMMAND_SERVICE.EXTRA_SESSION_ACTION);
executionCommand.sessionName = IntentUtils.getStringExtraIfSet(intent, RUN_COMMAND_SERVICE.EXTRA_SESSION_NAME, null);
executionCommand.sessionCreateMode = IntentUtils.getStringExtraIfSet(intent, RUN_COMMAND_SERVICE.EXTRA_SESSION_CREATE_MODE, null);
executionCommand.commandLabel = IntentUtils.getStringExtraIfSet(intent, RUN_COMMAND_SERVICE.EXTRA_COMMAND_LABEL, "RUN_COMMAND Execution Intent Command");
executionCommand.commandDescription = IntentUtils.getStringExtraIfSet(intent, RUN_COMMAND_SERVICE.EXTRA_COMMAND_DESCRIPTION, null);
executionCommand.commandHelp = IntentUtils.getStringExtraIfSet(intent, RUN_COMMAND_SERVICE.EXTRA_COMMAND_HELP, null);
Expand Down Expand Up @@ -209,6 +211,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
execIntent.putExtra(TERMUX_SERVICE.EXTRA_RUNNER, executionCommand.runner);
execIntent.putExtra(TERMUX_SERVICE.EXTRA_BACKGROUND_CUSTOM_LOG_LEVEL, DataUtils.getStringFromInteger(executionCommand.backgroundCustomLogLevel, null));
execIntent.putExtra(TERMUX_SERVICE.EXTRA_SESSION_ACTION, executionCommand.sessionAction);
execIntent.putExtra(TERMUX_SERVICE.EXTRA_SESSION_NAME, executionCommand.sessionName);
execIntent.putExtra(TERMUX_SERVICE.EXTRA_SESSION_CREATE_MODE, executionCommand.sessionCreateMode);
execIntent.putExtra(TERMUX_SERVICE.EXTRA_COMMAND_LABEL, executionCommand.commandLabel);
execIntent.putExtra(TERMUX_SERVICE.EXTRA_COMMAND_DESCRIPTION, executionCommand.commandDescription);
execIntent.putExtra(TERMUX_SERVICE.EXTRA_COMMAND_HELP, executionCommand.commandHelp);
Expand Down
66 changes: 53 additions & 13 deletions app/src/main/java/com/termux/app/TermuxService.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.termux.shared.data.DataUtils;
import com.termux.shared.shell.command.ExecutionCommand;
import com.termux.shared.shell.command.ExecutionCommand.Runner;
import com.termux.shared.shell.command.ExecutionCommand.SessionCreateMode;
import com.termux.terminal.TerminalEmulator;
import com.termux.terminal.TerminalSession;
import com.termux.terminal.TerminalSessionClient;
Expand Down Expand Up @@ -380,6 +381,8 @@ private void actionServiceExecute(Intent intent) {
executionCommand.workingDirectory = IntentUtils.getStringExtraIfSet(intent, TERMUX_SERVICE.EXTRA_WORKDIR, null);
executionCommand.isFailsafe = intent.getBooleanExtra(TERMUX_ACTIVITY.EXTRA_FAILSAFE_SESSION, false);
executionCommand.sessionAction = intent.getStringExtra(TERMUX_SERVICE.EXTRA_SESSION_ACTION);
executionCommand.sessionName = IntentUtils.getStringExtraIfSet(intent, TERMUX_SERVICE.EXTRA_SESSION_NAME, null);
executionCommand.sessionCreateMode = IntentUtils.getStringExtraIfSet(intent, TERMUX_SERVICE.EXTRA_SESSION_CREATE_MODE, null);
executionCommand.commandLabel = IntentUtils.getStringExtraIfSet(intent, TERMUX_SERVICE.EXTRA_COMMAND_LABEL, "Execution Intent Command");
executionCommand.commandDescription = IntentUtils.getStringExtraIfSet(intent, TERMUX_SERVICE.EXTRA_COMMAND_DESCRIPTION, null);
executionCommand.commandHelp = IntentUtils.getStringExtraIfSet(intent, TERMUX_SERVICE.EXTRA_COMMAND_HELP, null);
Expand All @@ -398,13 +401,13 @@ private void actionServiceExecute(Intent intent) {
mPendingPluginExecutionCommands.add(executionCommand);

if (Runner.APP_SHELL.equalsRunner(executionCommand.runner))
executeTermuxTaskCommand(executionCommand);
executeTermuxTaskCommand(executionCommand);
else if (Runner.TERMINAL_SESSION.equalsRunner(executionCommand.runner))
executeTermuxSessionCommand(executionCommand);
executeTermuxSessionCommand(executionCommand);
else {
String errmsg = this.getString(R.string.error_termux_service_unsupported_execution_command_runner, executionCommand.runner);
executionCommand.setStateFailed(Errno.ERRNO_FAILED.getCode(), errmsg);
PluginUtils.processPluginExecutionCommandError(this, LOG_TAG, executionCommand, false);
String errmsg = getString(R.string.error_termux_service_unsupported_execution_command_runner, executionCommand.runner);
executionCommand.setStateFailed(Errno.ERRNO_FAILED.getCode(), errmsg);
PluginUtils.processPluginExecutionCommandError(this, LOG_TAG, executionCommand, false);
}
}

Expand Down Expand Up @@ -493,16 +496,39 @@ public void onAppShellExited(final AppShell termuxTask) {
private void executeTermuxSessionCommand(ExecutionCommand executionCommand) {
if (executionCommand == null) return;

if (executionCommand.sessionCreateMode == null)
executionCommand.sessionCreateMode = SessionCreateMode.ALWAYS.getMode();

Logger.logDebug(LOG_TAG, "Executing foreground \"" + executionCommand.getCommandIdAndLabelLogString() + "\" TermuxSession command");

String sessionName = null;
// Transform executable path to session name, e.g. "/bin/do-something.sh" => "do-something.sh".
if (executionCommand.sessionName == null && executionCommand.executable != null) {
executionCommand.sessionName = ShellUtils.getExecutableBasename(executionCommand.executable);
}

// Transform executable path to session name, e.g. "/bin/do-something.sh" => "do something.sh".
if (executionCommand.executable != null) {
sessionName = ShellUtils.getExecutableBasename(executionCommand.executable).replace('-', ' ');
TermuxSession newTermuxSession = null;
if (SessionCreateMode.ALWAYS.equalsMode(executionCommand.sessionCreateMode))
; // Default
else if (SessionCreateMode.NO_SESSION_WITH_NAME.equalsMode(executionCommand.sessionCreateMode))
if (DataUtils.isNullOrEmpty(executionCommand.sessionName)) {
String errmsg = getString(R.string.error_termux_service_execution_command_session_name_unset, executionCommand.sessionCreateMode);
executionCommand.setStateFailed(Errno.ERRNO_FAILED.getCode(), errmsg);
PluginUtils.processPluginExecutionCommandError(this, LOG_TAG, executionCommand, false);
return;
} else {
newTermuxSession = getTermuxSessionForName(executionCommand.sessionName);
if (newTermuxSession != null)
Logger.logInfo(LOG_TAG, "Existing session with \"" + executionCommand.sessionName + "\" session name found");
}
else {
String errmsg = getString(R.string.error_termux_service_unsupported_execution_command_session_create_mode, executionCommand.sessionCreateMode);
executionCommand.setStateFailed(Errno.ERRNO_FAILED.getCode(), errmsg);
PluginUtils.processPluginExecutionCommandError(this, LOG_TAG, executionCommand, false);
return;
}

TermuxSession newTermuxSession = createTermuxSession(executionCommand, sessionName);
if (newTermuxSession == null)
newTermuxSession = createTermuxSession(executionCommand);
if (newTermuxSession == null) return;

handleSessionAction(DataUtils.getIntFromString(executionCommand.sessionAction,
Expand All @@ -516,12 +542,14 @@ private void executeTermuxSessionCommand(ExecutionCommand executionCommand) {
*/
@Nullable
public TermuxSession createTermuxSession(String executablePath, String[] arguments, String stdin, String workingDirectory, boolean isFailSafe, String sessionName) {
return createTermuxSession(new ExecutionCommand(getNextExecutionId(), executablePath, arguments, stdin, workingDirectory, Runner.TERMINAL_SESSION.getName(), isFailSafe), sessionName);
ExecutionCommand executionCommand = new ExecutionCommand(getNextExecutionId(), executablePath, arguments, stdin, workingDirectory, Runner.TERMINAL_SESSION.getName(), isFailSafe);
executionCommand.sessionName = sessionName;
return createTermuxSession(executionCommand);
}

/** Create a {@link TermuxSession}. */
@Nullable
public synchronized TermuxSession createTermuxSession(ExecutionCommand executionCommand, String sessionName) {
public synchronized TermuxSession createTermuxSession(ExecutionCommand executionCommand) {
if (executionCommand == null) return null;

Logger.logDebug(LOG_TAG, "Creating \"" + executionCommand.getCommandIdAndLabelLogString() + "\" TermuxSession");
Expand All @@ -538,7 +566,7 @@ public synchronized TermuxSession createTermuxSession(ExecutionCommand execution
// Otherwise if command was manually started by the user like by adding a new terminal session,
// then no need to set stdout
executionCommand.terminalTranscriptRows = mProperties.getTerminalTranscriptRows();
TermuxSession newTermuxSession = TermuxSession.execute(this, executionCommand, getTermuxTerminalSessionClient(), this, new TermuxShellEnvironmentClient(), sessionName, executionCommand.isPluginExecutionCommand);
TermuxSession newTermuxSession = TermuxSession.execute(this, executionCommand, getTermuxTerminalSessionClient(), this, new TermuxShellEnvironmentClient(), executionCommand.isPluginExecutionCommand);
if (newTermuxSession == null) {
Logger.logError(LOG_TAG, "Failed to execute new TermuxSession command for:\n" + executionCommand.getCommandIdAndLabelLogString());
// If the execution command was started for a plugin, then process the error
Expand Down Expand Up @@ -852,6 +880,18 @@ public synchronized TerminalSession getTerminalSessionForHandle(String sessionHa
return null;
}

public synchronized TermuxSession getTermuxSessionForName(String name) {
if (DataUtils.isNullOrEmpty(name)) return null;
TermuxSession termuxSession;
for (int i = 0, len = mTermuxSessions.size(); i < len; i++) {
termuxSession = mTermuxSessions.get(i);
TerminalSession terminalSession = termuxSession.getTerminalSession();
if (terminalSession.mSessionName != null && terminalSession.mSessionName.equals(name))
return termuxSession;
}
return null;
}



public static synchronized int getNextExecutionId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,22 @@ public void renameSession(final TerminalSession sessionToRename) {
if (sessionToRename == null) return;

TextInputDialogUtils.textInput(mActivity, R.string.title_rename_session, sessionToRename.mSessionName, R.string.action_rename_session_confirm, text -> {
sessionToRename.mSessionName = text;
renameSession(sessionToRename, text);
termuxSessionListNotifyUpdated();
}, -1, null, -1, null, null);
}

private void renameSession(TerminalSession sessionToRename, String text) {
if (sessionToRename == null) return;
sessionToRename.mSessionName = text;
TermuxService service = mActivity.getTermuxService();
if (service != null) {
TermuxSession termuxSession = service.getTermuxSessionForTerminalSession(sessionToRename);
if (termuxSession != null)
termuxSession.getExecutionCommand().sessionName = text;
}
}

public void addNewSession(boolean isFailSafe, String sessionName) {
TermuxService service = mActivity.getTermuxService();
if (service == null) return;
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
Grants it from Settings -> Apps -> &TERMUX_APP_NAME; -> Advanced</string>
<string name="error_termux_service_invalid_execution_command_runner">Invalid execution command runner to TermuxService: `%1$s`</string>
<string name="error_termux_service_unsupported_execution_command_runner">Unsupported execution command runner to TermuxService: `%1$s`</string>
<string name="error_termux_service_unsupported_execution_command_session_create_mode">Unsupported execution command session create mode to TermuxService: `%1$s`</string>
<string name="error_termux_service_execution_command_session_name_unset">Session name not set but `%1$s` session create mode passed</string>



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,41 @@ public static Runner runnerOf(@Nullable String name, @NonNull Runner def) {

}

public enum SessionCreateMode {

/** Always create {@link TerminalSession}. */
ALWAYS("always"),

/** Create session only if no session with {@link #sessionName} found. */
NO_SESSION_WITH_NAME("no-session-with-name");

private final String mode;

SessionCreateMode(final String mode) {
this.mode = mode;
}

public String getMode() {
return mode;
}

public boolean equalsMode(String sessionCreateMode) {
return sessionCreateMode != null && sessionCreateMode.equals(this.mode);
}

/** Get {@link SessionCreateMode} for {@code mode} if found, otherwise {@code null}. */
@Nullable
public static SessionCreateMode modeOf(String mode) {
for (SessionCreateMode v : SessionCreateMode.values()) {
if (v.mode.equals(mode)) {
return v;
}
}
return null;
}

}

/** The optional unique id for the {@link ExecutionCommand}. */
public Integer id;

Expand Down Expand Up @@ -148,9 +183,17 @@ public static Runner runnerOf(@Nullable String name, @NonNull Runner def) {
*/
public Integer backgroundCustomLogLevel;

/** The session action of foreground commands. */

/** 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 command label for the {@link ExecutionCommand}. */
public String commandLabel;
Expand Down Expand Up @@ -343,6 +386,14 @@ public static String getExecutionInputLogString(final ExecutionCommand execution
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.sessionCreateMode != null) {
logString.append("\n").append(executionCommand.getSessionCreateModeLogString());
}

if (!ignoreNull || executionCommand.commandIntent != null)
logString.append("\n").append(executionCommand.getCommandIntentLogString());

Expand Down Expand Up @@ -434,6 +485,8 @@ public static String getExecutionCommandMarkdownString(final ExecutionCommand ex
}

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("isPluginExecutionCommand", executionCommand.isPluginExecutionCommand, "-"));
Expand Down Expand Up @@ -524,6 +577,14 @@ public String getSessionActionLogString() {
return Logger.getSingleLineLogStringEntry("Session Action", sessionAction, "-");
}

public String getSessionNameLogString() {
return Logger.getSingleLineLogStringEntry("Session Name", sessionName, "-");
}

public String getSessionCreateModeLogString() {
return Logger.getSingleLineLogStringEntry("Session Create Mode", sessionCreateMode, "-");
}

public String getCommandDescriptionLogString() {
return Logger.getSingleLineLogStringEntry("Command Description", commandDescription, "-");
}
Expand Down
Loading