Added: Add support in AppShell and TermuxSession for caller to add/override additional environment variables not added by IShellEnvironment.setupShellCommandEnvironment()

This commit is contained in:
agnostic-apollo
2022-06-11 19:23:53 +05:00
parent 13d93ccac7
commit 2998558e9f
5 changed files with 17 additions and 5 deletions

View File

@@ -467,7 +467,7 @@ public final class TermuxService extends Service implements AppShell.AppShellCli
Logger.logVerboseExtended(LOG_TAG, executionCommand.toString());
AppShell newTermuxTask = AppShell.execute(this, executionCommand, this,
new TermuxShellEnvironment(),false);
new TermuxShellEnvironment(), null,false);
if (newTermuxTask == null) {
Logger.logError(LOG_TAG, "Failed to execute new TermuxTask command for:\n" + executionCommand.getCommandIdAndLabelLogString());
// If the execution command was started for a plugin, then process the error
@@ -579,7 +579,7 @@ public final class TermuxService extends Service implements AppShell.AppShellCli
// then no need to set stdout
executionCommand.terminalTranscriptRows = mProperties.getTerminalTranscriptRows();
TermuxSession newTermuxSession = TermuxSession.execute(this, executionCommand, getTermuxTerminalSessionClient(),
this, new TermuxShellEnvironment(), executionCommand.isPluginExecutionCommand);
this, new TermuxShellEnvironment(), null, 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

View File

@@ -6,6 +6,7 @@ import android.system.Os;
import android.system.OsConstants;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.common.base.Joiner;
import com.termux.shared.R;
@@ -65,6 +66,8 @@ public final class AppShell {
* {@code null} is returned by this method. This can
* optionally be {@code null}.
* @param shellEnvironmentClient The {@link IShellEnvironment} interface implementation.
* @param additionalEnvironment The additional shell environment variables to export. Existing
* variables will be overridden.
* @param isSynchronous If set to {@code true}, then the command will be executed in the
* caller thread and results returned synchronously in the {@link ExecutionCommand}
* sub object of the {@link AppShell} returned.
@@ -75,6 +78,7 @@ public final class AppShell {
public static AppShell execute(@NonNull final Context currentPackageContext, @NonNull ExecutionCommand executionCommand,
final AppShellClient appShellClient,
@NonNull final IShellEnvironment shellEnvironmentClient,
@Nullable HashMap<String, String> additionalEnvironment,
final boolean isSynchronous) {
if (executionCommand.executable == null || executionCommand.executable.isEmpty()) {
executionCommand.setStateFailed(Errno.ERRNO_FAILED.getCode(),
@@ -103,6 +107,8 @@ public final class AppShell {
// Setup command environment
HashMap<String, String> environment = shellEnvironmentClient.setupShellCommandEnvironment(currentPackageContext,
executionCommand);
if (additionalEnvironment != null)
environment.putAll(additionalEnvironment);
List<String> environmentList = ShellEnvironmentUtils.convertEnvironmentToEnviron(environment);
Collections.sort(environmentList);
String[] environmentArray = environmentList.toArray(new String[0]);

View File

@@ -597,7 +597,7 @@ public class TermuxUtils {
null, ExecutionCommand.Runner.APP_SHELL.getName(), false);
executionCommand.commandLabel = "APT Info Command";
executionCommand.backgroundCustomLogLevel = Logger.LOG_LEVEL_OFF;
AppShell appShell = AppShell.execute(context, executionCommand, null, new TermuxShellEnvironment(), true);
AppShell appShell = AppShell.execute(context, executionCommand, null, new TermuxShellEnvironment(), null, true);
if (appShell == null || !executionCommand.isSuccessful() || executionCommand.resultData.exitCode != 0) {
Logger.logErrorExtended(LOG_TAG, executionCommand.toString());
return null;
@@ -656,7 +656,7 @@ public class TermuxUtils {
null, logcatScript + "\n", "/", ExecutionCommand.Runner.APP_SHELL.getName(), true);
executionCommand.commandLabel = "Logcat dump command";
executionCommand.backgroundCustomLogLevel = Logger.LOG_LEVEL_OFF;
AppShell appShell = AppShell.execute(context, executionCommand, null, new TermuxShellEnvironment(), true);
AppShell appShell = AppShell.execute(context, executionCommand, null, new TermuxShellEnvironment(), null, true);
if (appShell == null || !executionCommand.isSuccessful()) {
Logger.logErrorExtended(LOG_TAG, executionCommand.toString());
return null;

View File

@@ -364,7 +364,7 @@ public class TermuxFileUtils {
statScript.toString() + "\n", "/", ExecutionCommand.Runner.APP_SHELL.getName(), true);
executionCommand.commandLabel = TermuxConstants.TERMUX_APP_NAME + " Files Stat Command";
executionCommand.backgroundCustomLogLevel = Logger.LOG_LEVEL_OFF;
AppShell appShell = AppShell.execute(context, executionCommand, null, new TermuxShellEnvironment(), true);
AppShell appShell = AppShell.execute(context, executionCommand, null, new TermuxShellEnvironment(), null, true);
if (appShell == null || !executionCommand.isSuccessful()) {
Logger.logErrorExtended(LOG_TAG, executionCommand.toString());
return null;

View File

@@ -4,6 +4,7 @@ import android.content.Context;
import android.system.OsConstants;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.common.base.Joiner;
import com.termux.shared.R;
@@ -62,6 +63,8 @@ public class TermuxSession {
* @param terminalSessionClient The {@link TerminalSessionClient} interface implementation.
* @param termuxSessionClient The {@link TermuxSessionClient} interface implementation.
* @param shellEnvironmentClient The {@link IShellEnvironment} interface implementation.
* @param additionalEnvironment The additional shell environment variables to export. Existing
* variables will be overridden.
* @param setStdoutOnExit If set to {@code true}, then the {@link ResultData#stdout}
* available in the {@link TermuxSessionClient#onTermuxSessionExited(TermuxSession)}
* callback will be set to the {@link TerminalSession} transcript. The session
@@ -74,6 +77,7 @@ public class TermuxSession {
public static TermuxSession execute(@NonNull final Context currentPackageContext, @NonNull ExecutionCommand executionCommand,
@NonNull final TerminalSessionClient terminalSessionClient, final TermuxSessionClient termuxSessionClient,
@NonNull final IShellEnvironment shellEnvironmentClient,
@Nullable HashMap<String, String> additionalEnvironment,
final boolean setStdoutOnExit) {
if (executionCommand.executable != null && executionCommand.executable.isEmpty())
executionCommand.executable = null;
@@ -132,6 +136,8 @@ public class TermuxSession {
// Setup command environment
HashMap<String, String> environment = shellEnvironmentClient.setupShellCommandEnvironment(currentPackageContext,
executionCommand);
if (additionalEnvironment != null)
environment.putAll(additionalEnvironment);
List<String> environmentList = ShellEnvironmentUtils.convertEnvironmentToEnviron(environment);
Collections.sort(environmentList);
String[] environmentArray = environmentList.toArray(new String[0]);