Added: Add ShellCommandShellEnvironment and TermuxShellCommandShellEnvironment to export ExecutionCommand variables

This adds onto f102ea20 to build termux environment. Variables for `ExecutionCommand` app have the `SHELL_CMD__` scope. Docs will be provided for details of the variables.

- `SHELL_CMD__SHELL_ID`
- `SHELL_CMD__SHELL_NAME`
- `SHELL_CMD__APP_SHELL_NUMBER_SINCE_BOOT`
- `SHELL_CMD__TERMINAL_SESSION_NUMBER_SINCE_BOOT`
- `SHELL_CMD__APP_SHELL_NUMBER_SINCE_APP_START`
- `SHELL_CMD__TERMINAL_SESSION_NUMBER_SINCE_APP_START`

The commit also adds `SystemEventReceiver` to Termux app that will receive `ACTION_BOOT_COMPLETED`.
This commit is contained in:
agnostic-apollo
2022-06-12 00:31:38 +05:00
parent ebdab0e59c
commit 150b1ff99c
12 changed files with 317 additions and 3 deletions

View File

@@ -195,6 +195,10 @@ public class ExecutionCommand {
/** The {@link ShellCreateMode} of commands. */
public String shellCreateMode;
/** Whether to set {@link ExecutionCommand} shell environment. */
public boolean setShellCommandShellEnvironment;
/** The command label for the {@link ExecutionCommand}. */
@@ -396,6 +400,8 @@ public class ExecutionCommand {
logString.append("\n").append(executionCommand.getShellCreateModeLogString());
}
logString.append("\n").append(executionCommand.getSetRunnerShellEnvironmentLogString());
if (!ignoreNull || executionCommand.commandIntent != null)
logString.append("\n").append(executionCommand.getCommandIntentLogString());
@@ -490,7 +496,7 @@ public class ExecutionCommand {
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("Set Shell Command Shell Environment", executionCommand.setShellCommandShellEnvironment, "-"));
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("isPluginExecutionCommand", executionCommand.isPluginExecutionCommand, "-"));
@@ -588,6 +594,10 @@ public class ExecutionCommand {
return Logger.getSingleLineLogStringEntry("Shell Create Mode", shellCreateMode, "-");
}
public String getSetRunnerShellEnvironmentLogString() {
return "Set Shell Command Shell Environment: `" + setShellCommandShellEnvironment + "`";
}
public String getCommandDescriptionLogString() {
return Logger.getSingleLineLogStringEntry("Command Description", commandDescription, "-");
}

View File

@@ -20,6 +20,12 @@ import java.util.HashMap;
*/
public class AndroidShellEnvironment extends UnixShellEnvironment {
protected ShellCommandShellEnvironment shellCommandShellEnvironment;
public AndroidShellEnvironment() {
shellCommandShellEnvironment = new ShellCommandShellEnvironment();
}
/** Get shell environment for Android. */
@NonNull
@Override
@@ -85,6 +91,9 @@ public class AndroidShellEnvironment extends UnixShellEnvironment {
getDefaultWorkingDirectoryPath());
ShellEnvironmentUtils.createHomeDir(environment);
if (executionCommand.setShellCommandShellEnvironment && shellCommandShellEnvironment != null)
environment.putAll(shellCommandShellEnvironment.getEnvironment(currentPackageContext, executionCommand));
return environment;
}

View File

@@ -0,0 +1,62 @@
package com.termux.shared.shell.command.environment;
import android.content.Context;
import androidx.annotation.NonNull;
import com.termux.shared.shell.command.ExecutionCommand;
import java.util.HashMap;
/**
* Environment for {@link ExecutionCommand}.
*/
public class ShellCommandShellEnvironment {
/** Environment variable prefix for the {@link ExecutionCommand}. */
public static final String SHELL_CMD_ENV_PREFIX = "SHELL_CMD__";
/** Environment variable for the {@link ExecutionCommand.Runner} name. */
public static final String ENV_SHELL_CMD__RUNNER_NAME = SHELL_CMD_ENV_PREFIX + "RUNNER_NAME";
/** Environment variable for the package name running the {@link ExecutionCommand}. */
public static final String ENV_SHELL_CMD__PACKAGE_NAME = SHELL_CMD_ENV_PREFIX + "PACKAGE_NAME";
/** Environment variable for the {@link ExecutionCommand#id}/TermuxShellManager.SHELL_ID name.
* This will be common for all runners. */
public static final String ENV_SHELL_CMD__SHELL_ID = SHELL_CMD_ENV_PREFIX + "SHELL_ID";
/** Environment variable for the {@link ExecutionCommand#shellName} name. */
public static final String ENV_SHELL_CMD__SHELL_NAME = SHELL_CMD_ENV_PREFIX + "SHELL_NAME";
/** Environment variable for the {@link ExecutionCommand.Runner#APP_SHELL} number since boot. */
public static final String ENV_SHELL_CMD__APP_SHELL_NUMBER_SINCE_BOOT = SHELL_CMD_ENV_PREFIX + "APP_SHELL_NUMBER_SINCE_BOOT";
/** Environment variable for the {{@link ExecutionCommand.Runner#TERMINAL_SESSION} number since boot. */
public static final String ENV_SHELL_CMD__TERMINAL_SESSION_NUMBER_SINCE_BOOT = SHELL_CMD_ENV_PREFIX + "TERMINAL_SESSION_NUMBER_SINCE_BOOT";
/** Environment variable for the {@link ExecutionCommand.Runner#APP_SHELL} number since app start. */
public static final String ENV_SHELL_CMD__APP_SHELL_NUMBER_SINCE_APP_START = SHELL_CMD_ENV_PREFIX + "APP_SHELL_NUMBER_SINCE_APP_START";
/** Environment variable for the {@link ExecutionCommand.Runner#TERMINAL_SESSION} number since app start. */
public static final String ENV_SHELL_CMD__TERMINAL_SESSION_NUMBER_SINCE_APP_START = SHELL_CMD_ENV_PREFIX + "TERMINAL_SESSION_NUMBER_SINCE_APP_START";
/** Get shell environment containing info for {@link ExecutionCommand}. */
@NonNull
public HashMap<String, String> getEnvironment(@NonNull Context currentPackageContext,
@NonNull ExecutionCommand executionCommand) {
HashMap<String, String> environment = new HashMap<>();
ExecutionCommand.Runner runner = ExecutionCommand.Runner.runnerOf(executionCommand.runner);
if (runner == null) return environment;
ShellEnvironmentUtils.putToEnvIfSet(environment, ENV_SHELL_CMD__RUNNER_NAME, runner.getName());
ShellEnvironmentUtils.putToEnvIfSet(environment, ENV_SHELL_CMD__PACKAGE_NAME, currentPackageContext.getPackageName());
ShellEnvironmentUtils.putToEnvIfSet(environment, ENV_SHELL_CMD__SHELL_ID, String.valueOf(executionCommand.id));
ShellEnvironmentUtils.putToEnvIfSet(environment, ENV_SHELL_CMD__SHELL_NAME, executionCommand.shellName);
return environment;
}
}