diff --git a/app/src/main/java/com/termux/app/TermuxService.java b/app/src/main/java/com/termux/app/TermuxService.java index d334a4d6..902aa1b1 100644 --- a/app/src/main/java/com/termux/app/TermuxService.java +++ b/app/src/main/java/com/termux/app/TermuxService.java @@ -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 diff --git a/termux-shared/src/main/java/com/termux/shared/shell/command/runner/app/AppShell.java b/termux-shared/src/main/java/com/termux/shared/shell/command/runner/app/AppShell.java index 614e6816..3f49e5ae 100644 --- a/termux-shared/src/main/java/com/termux/shared/shell/command/runner/app/AppShell.java +++ b/termux-shared/src/main/java/com/termux/shared/shell/command/runner/app/AppShell.java @@ -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 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 environment = shellEnvironmentClient.setupShellCommandEnvironment(currentPackageContext, executionCommand); + if (additionalEnvironment != null) + environment.putAll(additionalEnvironment); List environmentList = ShellEnvironmentUtils.convertEnvironmentToEnviron(environment); Collections.sort(environmentList); String[] environmentArray = environmentList.toArray(new String[0]); diff --git a/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java b/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java index 93ae2463..0d66a3f5 100644 --- a/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java @@ -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; diff --git a/termux-shared/src/main/java/com/termux/shared/termux/file/TermuxFileUtils.java b/termux-shared/src/main/java/com/termux/shared/termux/file/TermuxFileUtils.java index 8af32d87..6172a412 100644 --- a/termux-shared/src/main/java/com/termux/shared/termux/file/TermuxFileUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/termux/file/TermuxFileUtils.java @@ -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; diff --git a/termux-shared/src/main/java/com/termux/shared/termux/shell/command/runner/terminal/TermuxSession.java b/termux-shared/src/main/java/com/termux/shared/termux/shell/command/runner/terminal/TermuxSession.java index d9ad1a61..c95de6ef 100644 --- a/termux-shared/src/main/java/com/termux/shared/termux/shell/command/runner/terminal/TermuxSession.java +++ b/termux-shared/src/main/java/com/termux/shared/termux/shell/command/runner/terminal/TermuxSession.java @@ -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 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 environment = shellEnvironmentClient.setupShellCommandEnvironment(currentPackageContext, executionCommand); + if (additionalEnvironment != null) + environment.putAll(additionalEnvironment); List environmentList = ShellEnvironmentUtils.convertEnvironmentToEnviron(environment); Collections.sort(environmentList); String[] environmentArray = environmentList.toArray(new String[0]);