mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 10:45:23 +08:00
Added: Store pid in ExecutionCommand for sessions and tasks
This commit is contained in:
@@ -5,18 +5,15 @@ import android.app.Notification;
|
|||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.ActivityNotFoundException;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.net.Uri;
|
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.provider.Settings;
|
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
@@ -780,12 +777,12 @@ public final class TermuxService extends Service implements AppShell.AppShellCli
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void setCurrentStoredTerminalSession(TerminalSession session) {
|
private void setCurrentStoredTerminalSession(TerminalSession terminalSession) {
|
||||||
if (session == null) return;
|
if (terminalSession == null) return;
|
||||||
// Make the newly created session the current one to be displayed
|
// Make the newly created session the current one to be displayed
|
||||||
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(this);
|
TermuxAppSharedPreferences preferences = TermuxAppSharedPreferences.build(this);
|
||||||
if (preferences == null) return;
|
if (preferences == null) return;
|
||||||
preferences.setCurrentSession(session.mHandle);
|
preferences.setCurrentSession(terminalSession.mHandle);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized boolean isTermuxSessionsEmpty() {
|
public synchronized boolean isTermuxSessionsEmpty() {
|
||||||
@@ -808,11 +805,25 @@ public final class TermuxService extends Service implements AppShell.AppShellCli
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public synchronized TermuxSession getTermuxSessionForTerminalSession(TerminalSession terminalSession) {
|
||||||
|
if (terminalSession == null) return null;
|
||||||
|
|
||||||
|
for (int i = 0; i < mTermuxSessions.size(); i++) {
|
||||||
|
if (mTermuxSessions.get(i).getTerminalSession().equals(terminalSession))
|
||||||
|
return mTermuxSessions.get(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public synchronized TermuxSession getLastTermuxSession() {
|
public synchronized TermuxSession getLastTermuxSession() {
|
||||||
return mTermuxSessions.isEmpty() ? null : mTermuxSessions.get(mTermuxSessions.size() - 1);
|
return mTermuxSessions.isEmpty() ? null : mTermuxSessions.get(mTermuxSessions.size() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized int getIndexOfSession(TerminalSession terminalSession) {
|
public synchronized int getIndexOfSession(TerminalSession terminalSession) {
|
||||||
|
if (terminalSession == null) return -1;
|
||||||
|
|
||||||
for (int i = 0; i < mTermuxSessions.size(); i++) {
|
for (int i = 0; i < mTermuxSessions.size(); i++) {
|
||||||
if (mTermuxSessions.get(i).getTerminalSession().equals(terminalSession))
|
if (mTermuxSessions.get(i).getTerminalSession().equals(terminalSession))
|
||||||
return i;
|
return i;
|
||||||
|
@@ -234,6 +234,17 @@ public class TermuxTerminalSessionClient extends TermuxTerminalSessionClientBase
|
|||||||
mActivity.getTerminalView().setTerminalCursorBlinkerState(enabled, false);
|
mActivity.getTerminalView().setTerminalCursorBlinkerState(enabled, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setTerminalShellPid(@NonNull TerminalSession terminalSession, int pid) {
|
||||||
|
TermuxService service = mActivity.getTermuxService();
|
||||||
|
if (service == null) return;
|
||||||
|
|
||||||
|
TermuxSession termuxSession = service.getTermuxSessionForTerminalSession(terminalSession);
|
||||||
|
if (termuxSession != null)
|
||||||
|
termuxSession.getExecutionCommand().mPid = pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should be called when mActivity.onResetTerminalSession() is called
|
* Should be called when mActivity.onResetTerminalSession() is called
|
||||||
*/
|
*/
|
||||||
|
@@ -126,6 +126,7 @@ public final class TerminalSession extends TerminalOutput {
|
|||||||
int[] processId = new int[1];
|
int[] processId = new int[1];
|
||||||
mTerminalFileDescriptor = JNI.createSubprocess(mShellPath, mCwd, mArgs, mEnv, processId, rows, columns);
|
mTerminalFileDescriptor = JNI.createSubprocess(mShellPath, mCwd, mArgs, mEnv, processId, rows, columns);
|
||||||
mShellPid = processId[0];
|
mShellPid = processId[0];
|
||||||
|
mClient.setTerminalShellPid(this, mShellPid);
|
||||||
|
|
||||||
final FileDescriptor terminalFileDescriptorWrapped = wrapFileDescriptor(mTerminalFileDescriptor, mClient);
|
final FileDescriptor terminalFileDescriptorWrapped = wrapFileDescriptor(mTerminalFileDescriptor, mClient);
|
||||||
|
|
||||||
|
@@ -26,6 +26,8 @@ public interface TerminalSessionClient {
|
|||||||
|
|
||||||
void onTerminalCursorStateChange(boolean state);
|
void onTerminalCursorStateChange(boolean state);
|
||||||
|
|
||||||
|
void setTerminalShellPid(@NonNull TerminalSession session, int pid);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Integer getTerminalCursorStyle();
|
Integer getTerminalCursorStyle();
|
||||||
|
@@ -58,6 +58,8 @@ public class ExecutionCommand {
|
|||||||
/** The optional unique id for the {@link ExecutionCommand}. */
|
/** The optional unique id for the {@link ExecutionCommand}. */
|
||||||
public Integer id;
|
public Integer id;
|
||||||
|
|
||||||
|
/** The process id of command. */
|
||||||
|
public int mPid = -1;
|
||||||
|
|
||||||
/** The current state of the {@link ExecutionCommand}. */
|
/** The current state of the {@link ExecutionCommand}. */
|
||||||
private ExecutionState currentState = ExecutionState.PRE_EXECUTION;
|
private ExecutionState currentState = ExecutionState.PRE_EXECUTION;
|
||||||
@@ -266,6 +268,9 @@ public class ExecutionCommand {
|
|||||||
|
|
||||||
logString.append(executionCommand.getCommandIdAndLabelLogString()).append(":");
|
logString.append(executionCommand.getCommandIdAndLabelLogString()).append(":");
|
||||||
|
|
||||||
|
if (executionCommand.mPid != -1)
|
||||||
|
logString.append("\n").append(executionCommand.getPidLogString());
|
||||||
|
|
||||||
if (executionCommand.previousState != ExecutionState.PRE_EXECUTION)
|
if (executionCommand.previousState != ExecutionState.PRE_EXECUTION)
|
||||||
logString.append("\n").append(executionCommand.getPreviousStateLogString());
|
logString.append("\n").append(executionCommand.getPreviousStateLogString());
|
||||||
logString.append("\n").append(executionCommand.getCurrentStateLogString());
|
logString.append("\n").append(executionCommand.getCurrentStateLogString());
|
||||||
@@ -358,6 +363,8 @@ public class ExecutionCommand {
|
|||||||
|
|
||||||
markdownString.append("## ").append(executionCommand.commandLabel).append("\n");
|
markdownString.append("## ").append(executionCommand.commandLabel).append("\n");
|
||||||
|
|
||||||
|
if (executionCommand.mPid != -1)
|
||||||
|
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Pid", executionCommand.mPid, "-"));
|
||||||
|
|
||||||
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Previous State", executionCommand.previousState.getName(), "-"));
|
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Previous State", executionCommand.previousState.getName(), "-"));
|
||||||
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Current State", executionCommand.currentState.getName(), "-"));
|
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Current State", executionCommand.currentState.getName(), "-"));
|
||||||
@@ -408,6 +415,10 @@ public class ExecutionCommand {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getPidLogString() {
|
||||||
|
return "Pid: `" + mPid + "`";
|
||||||
|
}
|
||||||
|
|
||||||
public String getCurrentStateLogString() {
|
public String getCurrentStateLogString() {
|
||||||
return "Current State: `" + currentState.getName() + "`";
|
return "Current State: `" + currentState.getName() + "`";
|
||||||
}
|
}
|
||||||
|
@@ -137,16 +137,16 @@ public final class AppShell {
|
|||||||
* @param context The {@link Context} for operations.
|
* @param context The {@link Context} for operations.
|
||||||
*/
|
*/
|
||||||
private void executeInner(@NonNull final Context context) throws IllegalThreadStateException, InterruptedException {
|
private void executeInner(@NonNull final Context context) throws IllegalThreadStateException, InterruptedException {
|
||||||
final int pid = ShellUtils.getPid(mProcess);
|
mExecutionCommand.mPid = ShellUtils.getPid(mProcess);
|
||||||
|
|
||||||
Logger.logDebug(LOG_TAG, "Running \"" + mExecutionCommand.getCommandIdAndLabelLogString() + "\" AppShell with pid " + pid);
|
Logger.logDebug(LOG_TAG, "Running \"" + mExecutionCommand.getCommandIdAndLabelLogString() + "\" AppShell with pid " + mExecutionCommand.mPid);
|
||||||
|
|
||||||
mExecutionCommand.resultData.exitCode = null;
|
mExecutionCommand.resultData.exitCode = null;
|
||||||
|
|
||||||
// setup stdin, and stdout and stderr gobblers
|
// setup stdin, and stdout and stderr gobblers
|
||||||
DataOutputStream STDIN = new DataOutputStream(mProcess.getOutputStream());
|
DataOutputStream STDIN = new DataOutputStream(mProcess.getOutputStream());
|
||||||
StreamGobbler STDOUT = new StreamGobbler(pid + "-stdout", mProcess.getInputStream(), mExecutionCommand.resultData.stdout, mExecutionCommand.backgroundCustomLogLevel);
|
StreamGobbler STDOUT = new StreamGobbler(mExecutionCommand.mPid + "-stdout", mProcess.getInputStream(), mExecutionCommand.resultData.stdout, mExecutionCommand.backgroundCustomLogLevel);
|
||||||
StreamGobbler STDERR = new StreamGobbler(pid + "-stderr", mProcess.getErrorStream(), mExecutionCommand.resultData.stderr, mExecutionCommand.backgroundCustomLogLevel);
|
StreamGobbler STDERR = new StreamGobbler(mExecutionCommand.mPid + "-stderr", mProcess.getErrorStream(), mExecutionCommand.resultData.stderr, mExecutionCommand.backgroundCustomLogLevel);
|
||||||
|
|
||||||
// start gobbling
|
// start gobbling
|
||||||
STDOUT.start();
|
STDOUT.start();
|
||||||
@@ -196,9 +196,9 @@ public final class AppShell {
|
|||||||
|
|
||||||
// Process result
|
// Process result
|
||||||
if (exitCode == 0)
|
if (exitCode == 0)
|
||||||
Logger.logDebug(LOG_TAG, "The \"" + mExecutionCommand.getCommandIdAndLabelLogString() + "\" AppShell with pid " + pid + " exited normally");
|
Logger.logDebug(LOG_TAG, "The \"" + mExecutionCommand.getCommandIdAndLabelLogString() + "\" AppShell with pid " + mExecutionCommand.mPid + " exited normally");
|
||||||
else
|
else
|
||||||
Logger.logDebug(LOG_TAG, "The \"" + mExecutionCommand.getCommandIdAndLabelLogString() + "\" AppShell with pid " + pid + " exited with code: " + exitCode);
|
Logger.logDebug(LOG_TAG, "The \"" + mExecutionCommand.getCommandIdAndLabelLogString() + "\" AppShell with pid " + mExecutionCommand.mPid + " exited with code: " + exitCode);
|
||||||
|
|
||||||
// If the execution command has already failed, like SIGKILL was sent, then don't continue
|
// If the execution command has already failed, like SIGKILL was sent, then don't continue
|
||||||
if (mExecutionCommand.isStateFailed()) {
|
if (mExecutionCommand.isStateFailed()) {
|
||||||
|
@@ -44,6 +44,9 @@ public class TermuxTerminalSessionClientBase implements TerminalSessionClient {
|
|||||||
public void onTerminalCursorStateChange(boolean state) {
|
public void onTerminalCursorStateChange(boolean state) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setTerminalShellPid(@NonNull TerminalSession session, int pid) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Reference in New Issue
Block a user