Skip to content

Commit

Permalink
Improve TermuxSession and TermuxTask
Browse files Browse the repository at this point in the history
TermuxSession, TermuxTask and TermuxSessionClientBase have been moved to termux-shared. They are now not dependent on TermuxService anymore and have become abstract so that they can be called from anywhere. TermuxSession.TermuxSessionClient and TermuxTask.TermuxTaskClient interfaces have been created for callbacks, which the TermuxService now implements.

The TermuxTask now also supports synchronous command execution as well to run shell commands from anywhere for internal use by termux app and its plugins.

TermuxTask now also supports killing the process being run by sending it a SIGKILL. This is used by TermuxService to kill all the TermuxTasks (in addition to TermuxSessions) it manages when it is destroyed, either by user exiting it or by android killing it. Only the tasks that were started by a plugin which **expects** the result back via a pending intent will be killed, but the remaining background tasks will keep on running until the termux app process is killed by android, like by OOM. Check TermuxService.killAllTermuxExecutionCommands() for more details on how TermuxService kills TermuxTasks and TermuxSessions.

Fixed null pointer exception when getting terminal transcript if TerminalEmulator not initialized.
  • Loading branch information
agnostic-apollo committed Apr 12, 2021
1 parent df4d8ac commit 0cd7cb8
Show file tree
Hide file tree
Showing 17 changed files with 747 additions and 330 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/termux/app/RunCommandService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.termux.shared.notification.NotificationUtils;
import com.termux.app.utils.PluginUtils;
import com.termux.shared.data.DataUtils;
import com.termux.app.models.ExecutionCommand;
import com.termux.shared.models.ExecutionCommand;

/**
* A service that receives {@link RUN_COMMAND_SERVICE#ACTION_RUN_COMMAND} intent from third party apps and
Expand Down
202 changes: 143 additions & 59 deletions app/src/main/java/com/termux/app/TermuxService.java

Large diffs are not rendered by default.

116 changes: 0 additions & 116 deletions app/src/main/java/com/termux/app/terminal/TermuxSession.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import android.widget.ListView;

import com.termux.R;
import com.termux.shared.shell.TermuxSession;
import com.termux.shared.interact.DialogUtils;
import com.termux.app.TermuxActivity;
import com.termux.shared.shell.TermuxSessionClientBase;
import com.termux.shared.termux.TermuxConstants;
import com.termux.app.TermuxService;
import com.termux.shared.settings.properties.TermuxPropertyConstants;
Expand Down Expand Up @@ -255,6 +257,7 @@ public void removeFinishedSession(TerminalSession finishedSession) {
TermuxService service = mActivity.getTermuxService();

int index = service.removeTermuxSession(finishedSession);

int size = mActivity.getTermuxService().getTermuxSessionsSize();
if (size == 0) {
// There are no sessions to show, so finish the activity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.termux.R;
import com.termux.app.TermuxActivity;
import com.termux.shared.shell.TermuxSession;
import com.termux.terminal.TerminalSession;

import java.util.List;
Expand Down
142 changes: 0 additions & 142 deletions app/src/main/java/com/termux/app/terminal/TermuxTask.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.termux.R;
import com.termux.app.TermuxActivity;
import com.termux.shared.shell.ShellUtils;
import com.termux.shared.termux.TermuxConstants;
import com.termux.app.activities.ReportActivity;
import com.termux.app.models.ReportInfo;
Expand Down Expand Up @@ -341,7 +342,7 @@ public void shareSessionTranscript() {
TerminalSession session = mActivity.getCurrentSession();
if (session == null) return;

String transcriptText = session.getEmulator().getScreen().getTranscriptTextWithoutJoinedLines().trim();
String transcriptText = ShellUtils.getTerminalSessionTranscriptText(session, false, true);
if (transcriptText == null) return;

try {
Expand All @@ -361,7 +362,7 @@ public void showUrlSelection() {
TerminalSession session = mActivity.getCurrentSession();
if (session == null) return;

String text = session.getEmulator().getScreen().getTranscriptTextWithFullLinesJoined();
String text = ShellUtils.getTerminalSessionTranscriptText(session, true, true);

LinkedHashSet<CharSequence> urlSet = DataUtils.extractUrls(text);
if (urlSet.isEmpty()) {
Expand Down Expand Up @@ -404,7 +405,7 @@ public void reportIssueFromTranscript() {
TerminalSession session = mActivity.getCurrentSession();
if (session == null) return;

String transcriptText = session.getEmulator().getScreen().getTranscriptTextWithoutJoinedLines().trim();
String transcriptText = ShellUtils.getTerminalSessionTranscriptText(session, false, true);
if (transcriptText == null) return;

transcriptText = DataUtils.getTruncatedCommandOutput(transcriptText, DataUtils.TRANSACTION_SIZE_LIMIT_IN_BYTES, false, true, false).trim();
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/termux/app/utils/PluginUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.termux.shared.settings.properties.SharedProperties;
import com.termux.shared.settings.properties.TermuxPropertyConstants;
import com.termux.app.models.ReportInfo;
import com.termux.app.models.ExecutionCommand;
import com.termux.shared.models.ExecutionCommand;
import com.termux.app.models.UserAction;
import com.termux.shared.data.DataUtils;
import com.termux.shared.markdown.MarkdownUtils;
Expand Down Expand Up @@ -56,7 +56,7 @@ public static void processPluginExecutionCommandResult(final Context context, St
logTag = DataUtils.getDefaultIfNull(logTag, LOG_TAG);

if(!executionCommand.hasExecuted()) {
Logger.logWarn(logTag, "Ignoring call to processPluginExecutionCommandResult() since the execution command has not been ExecutionState.EXECUTED");
Logger.logWarn(logTag, "Ignoring call to processPluginExecutionCommandResult() since the execution command state is not higher than the ExecutionState.EXECUTED");
return;
}

Expand Down Expand Up @@ -114,7 +114,7 @@ public static void processPluginExecutionCommandError(final Context context, Str
logTag = DataUtils.getDefaultIfNull(logTag, LOG_TAG);

if(!executionCommand.isStateFailed()) {
Logger.logWarn(logTag, "Ignoring call to processPluginExecutionCommandError() since the execution command does not have ExecutionState.FAILED state");
Logger.logWarn(logTag, "Ignoring call to processPluginExecutionCommandError() since the execution command is not in ExecutionState.FAILED");
return;
}

Expand Down Expand Up @@ -255,6 +255,7 @@ public static boolean sendPluginExecutionCommandResultPendingIntent(Context cont
pluginPendingIntent.send(context, Activity.RESULT_OK, resultIntent);
} catch (PendingIntent.CanceledException e) {
// The caller doesn't want the result? That's fine, just ignore
Logger.logDebug(logTag, "The Execution Command \"" + label + "\" creator " + pluginPendingIntent.getCreatorPackage() + " does not want the results anymore");
}

return true;
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@
<!-- Termux Execution Commands -->
<string name="msg_executable_absolute_path">Executable Absolute Path: \"%1$s\"</string>
<string name="msg_working_directory_absolute_path">Working Directory Absolute Path: \"%1$s\"</string>
<string name="error_sending_sigkill_to_process">Sending SIGKILL to process on user request or because android is killing the service</string>
<string name="error_failed_to_execute_termux_session_command">"Failed to execute \"%1$s\" termux session command"</string>
<string name="error_failed_to_execute_termux_task_command">"Failed to execute \"%1$s\" termux task command"</string>



Expand Down
2 changes: 2 additions & 0 deletions termux-shared/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ android {
// will be runtime exceptions on android < 8
// due to missing classes like java.nio.file.Path.
implementation "commons-io:commons-io:2.5"

implementation project(":terminal-view")
}

defaultConfig {
Expand Down
Loading

0 comments on commit 0cd7cb8

Please sign in to comment.