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.
This commit is contained in:
agnostic-apollo
2021-04-12 14:26:53 +05:00
parent df4d8ac7e5
commit 0cd7cb8162
17 changed files with 747 additions and 330 deletions

View File

@@ -2,11 +2,16 @@ package com.termux.shared.shell;
import android.content.Context;
import androidx.annotation.NonNull;
import com.termux.shared.termux.TermuxConstants;
import com.termux.shared.file.FileUtils;
import com.termux.shared.logger.Logger;
import com.termux.shared.packages.PackageUtils;
import com.termux.shared.termux.TermuxUtils;
import com.termux.terminal.TerminalBuffer;
import com.termux.terminal.TerminalEmulator;
import com.termux.terminal.TerminalSession;
import java.io.File;
import java.io.FileInputStream;
@@ -85,7 +90,7 @@ public class ShellUtils {
}
}
public static String[] setupProcessArgs(String fileToExecute, String[] arguments) {
public static String[] setupProcessArgs(@NonNull String fileToExecute, String[] arguments) {
// The file to execute may either be:
// - An elf file, in which we execute it directly.
// - A script file without shebang, which we execute with our standard shell $PREFIX/bin/sh instead of the
@@ -153,4 +158,28 @@ public class ShellUtils {
}
}
public static String getTerminalSessionTranscriptText(TerminalSession terminalSession, boolean linesJoined, boolean trim) {
if (terminalSession == null) return null;
TerminalEmulator terminalEmulator = terminalSession.getEmulator();
if (terminalEmulator == null) return null;
TerminalBuffer terminalBuffer = terminalEmulator.getScreen();
if (terminalBuffer == null) return null;
String transcriptText;
if(linesJoined)
transcriptText = terminalBuffer.getTranscriptTextWithFullLinesJoined();
else
transcriptText = terminalBuffer.getTranscriptTextWithoutJoinedLines();
if (transcriptText == null) return null;
if(trim)
transcriptText = transcriptText.trim();
return transcriptText;
}
}