Add PendingIntent in TermuxService to return data from execution

This commit adds an optional final argument to the BackgroundJob
constructor for a PendingIntent to return the results of its
execution to, and also attempts to pass an optional pendingIntent to
it from the Service start intent
This commit is contained in:
Archenoth
2019-12-02 02:38:37 -07:00
committed by Fredrik Fornwall
parent 76df44e6bb
commit 7f7c1efac1
2 changed files with 50 additions and 19 deletions

View File

@@ -1,5 +1,9 @@
package com.termux.app; package com.termux.app;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log; import android.util.Log;
import java.io.BufferedReader; import java.io.BufferedReader;
@@ -25,6 +29,10 @@ public final class BackgroundJob {
final Process mProcess; final Process mProcess;
public BackgroundJob(String cwd, String fileToExecute, final String[] args, final TermuxService service){ public BackgroundJob(String cwd, String fileToExecute, final String[] args, final TermuxService service){
this(cwd, fileToExecute, args, service, null);
}
public BackgroundJob(String cwd, String fileToExecute, final String[] args, final TermuxService service, PendingIntent pendingIntent) {
String[] env = buildEnvironment(false, cwd); String[] env = buildEnvironment(false, cwd);
if (cwd == null) cwd = TermuxService.HOME_PATH; if (cwd == null) cwd = TermuxService.HOME_PATH;
@@ -43,6 +51,28 @@ public final class BackgroundJob {
mProcess = process; mProcess = process;
final int pid = getPid(mProcess); final int pid = getPid(mProcess);
final Bundle result = new Bundle();
final StringBuilder outResult = new StringBuilder();
final StringBuilder errResult = new StringBuilder();
Thread errThread = new Thread() {
@Override
public void run() {
InputStream stderr = mProcess.getErrorStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stderr, StandardCharsets.UTF_8));
String line;
try {
// FIXME: Long lines.
while ((line = reader.readLine()) != null) {
errResult.append(line).append('\n');
Log.i(LOG_TAG, "[" + pid + "] stderr: " + line);
}
} catch (IOException e) {
// Ignore.
}
}
};
errThread.start();
new Thread() { new Thread() {
@Override @Override
@@ -50,11 +80,13 @@ public final class BackgroundJob {
Log.i(LOG_TAG, "[" + pid + "] starting: " + processDescription); Log.i(LOG_TAG, "[" + pid + "] starting: " + processDescription);
InputStream stdout = mProcess.getInputStream(); InputStream stdout = mProcess.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout, StandardCharsets.UTF_8)); BufferedReader reader = new BufferedReader(new InputStreamReader(stdout, StandardCharsets.UTF_8));
String line; String line;
try { try {
// FIXME: Long lines. // FIXME: Long lines.
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
Log.i(LOG_TAG, "[" + pid + "] stdout: " + line); Log.i(LOG_TAG, "[" + pid + "] stdout: " + line);
outResult.append(line).append('\n');
} }
} catch (IOException e) { } catch (IOException e) {
Log.e(LOG_TAG, "Error reading output", e); Log.e(LOG_TAG, "Error reading output", e);
@@ -68,26 +100,25 @@ public final class BackgroundJob {
} else { } else {
Log.w(LOG_TAG, "[" + pid + "] exited with code: " + exitCode); Log.w(LOG_TAG, "[" + pid + "] exited with code: " + exitCode);
} }
} catch (InterruptedException e) {
// Ignore.
}
}
}.start();
result.putString("stdout", outResult.toString());
result.putInt("exitCode", exitCode);
new Thread() { errThread.join();
@Override result.putString("stderr", errResult.toString());
public void run() {
InputStream stderr = mProcess.getErrorStream(); Intent data = new Intent();
BufferedReader reader = new BufferedReader(new InputStreamReader(stderr, StandardCharsets.UTF_8)); data.putExtra("result", result);
String line;
if(pendingIntent != null) {
try { try {
// FIXME: Long lines. pendingIntent.send(service.getApplicationContext(), Activity.RESULT_OK, data);
while ((line = reader.readLine()) != null) { } catch (PendingIntent.CanceledException e) {
Log.i(LOG_TAG, "[" + pid + "] stderr: " + line); // The caller doesn't want the result? That's fine, just ignore
} }
} catch (IOException e) { }
// Ignore. } catch (InterruptedException e) {
// Ignore
} }
} }
}.start(); }.start();

View File

@@ -148,7 +148,7 @@ public final class TermuxService extends Service implements SessionChangedCallba
String cwd = intent.getStringExtra(EXTRA_CURRENT_WORKING_DIRECTORY); String cwd = intent.getStringExtra(EXTRA_CURRENT_WORKING_DIRECTORY);
if (intent.getBooleanExtra(EXTRA_EXECUTE_IN_BACKGROUND, false)) { if (intent.getBooleanExtra(EXTRA_EXECUTE_IN_BACKGROUND, false)) {
BackgroundJob task = new BackgroundJob(cwd, executablePath, arguments, this); BackgroundJob task = new BackgroundJob(cwd, executablePath, arguments, this, intent.getParcelableExtra("pendingIntent"));
mBackgroundTasks.add(task); mBackgroundTasks.add(task);
updateNotification(); updateNotification();
} else { } else {