From db3ff7b24a2cb8c84b00cc5cec083361018569a1 Mon Sep 17 00:00:00 2001 From: Leonid Pliushch Date: Tue, 9 Jun 2020 15:32:22 +0300 Subject: [PATCH] Provide a service for executing commands by third-party applications Re-implementation of https://github.com/termux/termux-app/pull/1029. If Termux has property "allow-external-apps" set to "true", a third-party program will be able to send intents for executing custom commands within Termux environment. Third-party program must declare permission "com.termux.permission.RUN_COMMAND". --- app/src/main/AndroidManifest.xml | 15 ++++ .../main/java/com/termux/app/RunCommand.java | 88 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + 3 files changed, 105 insertions(+) create mode 100644 app/src/main/java/com/termux/app/RunCommand.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 608d34b8..bb6ced2c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,6 +8,12 @@ + + @@ -114,6 +120,15 @@ android:name="com.termux.app.TermuxService" android:exported="false" /> + + + + + + = Build.VERSION_CODES.O) { + this.startForegroundService(execIntent); + } else { + this.startService(execIntent); + } + } + + return Service.START_NOT_STICKY; + } + + private boolean allowExternalApps() { + File propsFile = new File(TermuxService.HOME_PATH + "/.termux/termux.properties"); + if (!propsFile.exists()) + propsFile = new File(TermuxService.HOME_PATH + "/.config/termux/termux.properties"); + + Properties props = new Properties(); + try { + if (propsFile.isFile() && propsFile.canRead()) { + try (FileInputStream in = new FileInputStream(propsFile)) { + props.load(new InputStreamReader(in, StandardCharsets.UTF_8)); + } + } + } catch (Exception e) { + Log.e("termux", "Error loading props", e); + } + + return props.getProperty("allow-external-apps", "false").equals("true"); + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 42ac4af8..d3a16a54 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2,6 +2,8 @@ Termux Termux user + Run commands in Termux environment + Allow third-party applications to execute arbitrary commands within Termux environment. New session Failsafe Keyboard