Skip to content

Commit 0cd7cb8

Browse files
Improve TermuxSession and TermuxTask
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.
1 parent df4d8ac commit 0cd7cb8

17 files changed

+747
-330
lines changed

app/src/main/java/com/termux/app/RunCommandService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import com.termux.shared.notification.NotificationUtils;
1919
import com.termux.app.utils.PluginUtils;
2020
import com.termux.shared.data.DataUtils;
21-
import com.termux.app.models.ExecutionCommand;
21+
import com.termux.shared.models.ExecutionCommand;
2222

2323
/**
2424
* A service that receives {@link RUN_COMMAND_SERVICE#ACTION_RUN_COMMAND} intent from third party apps and

app/src/main/java/com/termux/app/TermuxService.java

+143-59
Large diffs are not rendered by default.

app/src/main/java/com/termux/app/terminal/TermuxSession.java

-116
This file was deleted.

app/src/main/java/com/termux/app/terminal/TermuxSessionClient.java

+3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
import android.widget.ListView;
1414

1515
import com.termux.R;
16+
import com.termux.shared.shell.TermuxSession;
1617
import com.termux.shared.interact.DialogUtils;
1718
import com.termux.app.TermuxActivity;
19+
import com.termux.shared.shell.TermuxSessionClientBase;
1820
import com.termux.shared.termux.TermuxConstants;
1921
import com.termux.app.TermuxService;
2022
import com.termux.shared.settings.properties.TermuxPropertyConstants;
@@ -255,6 +257,7 @@ public void removeFinishedSession(TerminalSession finishedSession) {
255257
TermuxService service = mActivity.getTermuxService();
256258

257259
int index = service.removeTermuxSession(finishedSession);
260+
258261
int size = mActivity.getTermuxService().getTermuxSessionsSize();
259262
if (size == 0) {
260263
// There are no sessions to show, so finish the activity.

app/src/main/java/com/termux/app/terminal/TermuxSessionsListViewController.java

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.termux.R;
2222
import com.termux.app.TermuxActivity;
23+
import com.termux.shared.shell.TermuxSession;
2324
import com.termux.terminal.TerminalSession;
2425

2526
import java.util.List;

app/src/main/java/com/termux/app/terminal/TermuxTask.java

-142
This file was deleted.

app/src/main/java/com/termux/app/terminal/TermuxViewClient.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.termux.R;
2222
import com.termux.app.TermuxActivity;
23+
import com.termux.shared.shell.ShellUtils;
2324
import com.termux.shared.termux.TermuxConstants;
2425
import com.termux.app.activities.ReportActivity;
2526
import com.termux.app.models.ReportInfo;
@@ -341,7 +342,7 @@ public void shareSessionTranscript() {
341342
TerminalSession session = mActivity.getCurrentSession();
342343
if (session == null) return;
343344

344-
String transcriptText = session.getEmulator().getScreen().getTranscriptTextWithoutJoinedLines().trim();
345+
String transcriptText = ShellUtils.getTerminalSessionTranscriptText(session, false, true);
345346
if (transcriptText == null) return;
346347

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

364-
String text = session.getEmulator().getScreen().getTranscriptTextWithFullLinesJoined();
365+
String text = ShellUtils.getTerminalSessionTranscriptText(session, true, true);
365366

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

407-
String transcriptText = session.getEmulator().getScreen().getTranscriptTextWithoutJoinedLines().trim();
408+
String transcriptText = ShellUtils.getTerminalSessionTranscriptText(session, false, true);
408409
if (transcriptText == null) return;
409410

410411
transcriptText = DataUtils.getTruncatedCommandOutput(transcriptText, DataUtils.TRANSACTION_SIZE_LIMIT_IN_BYTES, false, true, false).trim();

app/src/main/java/com/termux/app/utils/PluginUtils.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.termux.shared.settings.properties.SharedProperties;
2222
import com.termux.shared.settings.properties.TermuxPropertyConstants;
2323
import com.termux.app.models.ReportInfo;
24-
import com.termux.app.models.ExecutionCommand;
24+
import com.termux.shared.models.ExecutionCommand;
2525
import com.termux.app.models.UserAction;
2626
import com.termux.shared.data.DataUtils;
2727
import com.termux.shared.markdown.MarkdownUtils;
@@ -56,7 +56,7 @@ public static void processPluginExecutionCommandResult(final Context context, St
5656
logTag = DataUtils.getDefaultIfNull(logTag, LOG_TAG);
5757

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

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

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

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

260261
return true;

app/src/main/res/values/strings.xml

-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@
100100
<!-- Termux Execution Commands -->
101101
<string name="msg_executable_absolute_path">Executable Absolute Path: \"%1$s\"</string>
102102
<string name="msg_working_directory_absolute_path">Working Directory Absolute Path: \"%1$s\"</string>
103-
<string name="error_sending_sigkill_to_process">Sending SIGKILL to process on user request or because android is killing the service</string>
104-
<string name="error_failed_to_execute_termux_session_command">"Failed to execute \"%1$s\" termux session command"</string>
105-
<string name="error_failed_to_execute_termux_task_command">"Failed to execute \"%1$s\" termux task command"</string>
106103

107104

108105

termux-shared/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ android {
1616
// will be runtime exceptions on android < 8
1717
// due to missing classes like java.nio.file.Path.
1818
implementation "commons-io:commons-io:2.5"
19+
20+
implementation project(":terminal-view")
1921
}
2022

2123
defaultConfig {

0 commit comments

Comments
 (0)