这是indexloc提供的服务,不要输入任何密码
Skip to content

Commit 05af1af

Browse files
Added/Updated: Add support for stdin for background commands and redesign plugin configuring views
Users can now pass scripts via `stdin`, like a `bash` script to the `$PREFIX/bin/bash` shell and a `python` script to the `$PREFIX/bin/python` shell or any other commands. Note that if passing script via `stdin`, do not pass arguments, since it will fail depending on shell, at least will for `bash`. Max length of script supported is `45K` characters as per Tasker plugin bundle limits, check `EditConfigurationActivity.setStdinView()` for details. The plugin configuring screen has been redesigned with `CardView` support. For `stdin`, the `TextIOActivity` provided by `termux-shared` will be used. The `strings.xml` naming convention has been updated. Closes #46
1 parent f6e3368 commit 05af1af

File tree

8 files changed

+570
-267
lines changed

8 files changed

+570
-267
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ android {
5353
}
5454

5555
dependencies {
56+
implementation 'androidx.appcompat:appcompat:1.3.1'
57+
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
5658
testImplementation 'junit:junit:4.13.2'
5759
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
5860
androidTestImplementation 'androidx.test:runner:1.4.0'

app/src/main/AndroidManifest.xml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
34
package="com.termux.tasker"
45
android:sharedUserId="${TERMUX_PACKAGE_NAME}"
5-
android:sharedUserLabel="@string/shared_user_label" >
6+
android:sharedUserLabel="@string/shared_user_label">
67

78
<application
89
android:name=".TermuxTaskerApplication"
910
android:label="@string/application_name"
1011
android:icon="@drawable/ic_launcher"
1112
android:theme="@style/AppTheme"
12-
1313
android:allowBackup="true"
1414
android:fullBackupOnly="false"
15-
android:supportsRtl="true" >
15+
android:supportsRtl="true">
1616

1717
<!--
1818
This is the "edit" Activity. Note that Locale will reject plug-in Activities for the following reasons:·
@@ -34,6 +34,13 @@
3434
</intent-filter>
3535
</activity>
3636

37+
<!-- If you see "Unresolved package" errors for TextIOActivity with manifest placeholder, ignore it.
38+
Android Studio linter is broken and correct package name will be used at build time." -->
39+
<activity
40+
android:name="${TERMUX_PACKAGE_NAME}.shared.activities.TextIOActivity"
41+
android:theme="@style/Theme.AppCompat.TermuxTextIOActivity"
42+
tools:ignore="MissingClass" />
43+
3744
<!--
3845
This is the "fire" BroadcastReceiver. Note that Locale will reject plug-in BroadcastReceivers
3946
for the following reasons:
@@ -53,10 +60,8 @@
5360
</intent-filter>
5461
</receiver>
5562

56-
<!--
57-
This is service that will receive execution result from the execution service via a PendingIntent
58-
-->
63+
<!-- This is service that will receive execution result from the execution service via a PendingIntent -->
5964
<service android:name=".PluginResultsService" />
6065
</application>
6166

62-
</manifest>
67+
</manifest>

app/src/main/java/com/termux/tasker/EditConfigurationActivity.java

Lines changed: 274 additions & 152 deletions
Large diffs are not rendered by default.

app/src/main/java/com/termux/tasker/FireReceiver.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public void onReceive(final Context context, final Intent intent) {
7373
executionCommand.inBackground = !(intent.getBooleanExtra(PluginBundleManager.EXTRA_TERMINAL, false));
7474
final boolean waitForResult = bundle.getBoolean(PluginBundleManager.EXTRA_WAIT_FOR_RESULT, true);
7575

76+
if (executionCommand.inBackground)
77+
executionCommand.stdin = IntentUtils.getStringExtraIfSet(intent, PluginBundleManager.EXTRA_STDIN, null);
7678

7779

7880
// If Termux app is not installed, enabled or accessible with current context or if
@@ -177,6 +179,7 @@ public void onReceive(final Context context, final Intent intent) {
177179
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_ARGUMENTS, executionCommand.arguments);
178180
if (executionCommand.workingDirectory != null && !executionCommand.workingDirectory.isEmpty())
179181
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_WORKDIR, executionCommand.workingDirectory);
182+
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_STDIN, executionCommand.stdin);
180183
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_BACKGROUND, executionCommand.inBackground);
181184

182185
// Send execution intent to TERMUX_SERVICE

app/src/main/java/com/termux/tasker/PluginBundleManager.java

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import android.annotation.SuppressLint;
44
import android.content.Context;
55
import android.os.Bundle;
6+
import android.os.Parcel;
67
import android.text.TextUtils;
78

9+
import androidx.annotation.NonNull;
810
import androidx.annotation.Nullable;
911

12+
import com.termux.shared.data.DataUtils;
1013
import com.termux.shared.logger.Logger;
1114
import com.termux.shared.packages.PackageUtils;
1215
import com.termux.shared.termux.TermuxConstants;
@@ -25,12 +28,17 @@ public class PluginBundleManager {
2528
/** The {@code String} extra for path to current working directory for execution. */
2629
public static final String EXTRA_WORKDIR = TermuxConstants.TERMUX_TASKER_PACKAGE_NAME + ".extra.WORKDIR"; // Default: "com.termux.tasker.extra.WORKDIR"
2730

31+
/** The {@code String} extra for stdin for background commands. */
32+
public static final String EXTRA_STDIN = TermuxConstants.TERMUX_TASKER_PACKAGE_NAME + ".extra.STDIN"; // Default: "com.termux.tasker.extra.STDIN"
33+
2834
/** The {@code boolean} extra for whether the executable should be run inside a terminal. */
2935
public static final String EXTRA_TERMINAL = TermuxConstants.TERMUX_TASKER_PACKAGE_NAME + ".extra.TERMINAL"; // Default: "com.termux.tasker.extra.TERMINAL"
3036

3137
/** The {@code boolean} extra for whether plugin action should wait for result of commands or not. */
3238
public static final String EXTRA_WAIT_FOR_RESULT = TermuxConstants.TERMUX_TASKER_PACKAGE_NAME + ".extra.WAIT_FOR_RESULT"; // Default: "com.termux.tasker.extra.WAIT_FOR_RESULT"
3339

40+
41+
3442
/**
3543
* The {@code int} extra for the versionCode of the plugin app that saved the Bundle.
3644
*
@@ -40,6 +48,9 @@ public class PluginBundleManager {
4048
*/
4149
public static final String BUNDLE_EXTRA_INT_VERSION_CODE = TermuxConstants.TERMUX_TASKER_PACKAGE_NAME + ".extra.VERSION_CODE"; // Default: "com.termux.tasker.extra.VERSION_CODE"
4250

51+
public static final String UNICODE_CHECK = "\u2713";
52+
public static final String UNICODE_UNCHECK = "\u2715";
53+
4354
/**
4455
* Method to verify the content of the bundle are correct.
4556
* <p>
@@ -50,7 +61,7 @@ public class PluginBundleManager {
5061
* @return Returns the {@code errmsg} if Bundle is not valid, otherwise {@code null}.
5162
*/
5263
@SuppressLint("DefaultLocale")
53-
public static String parseBundle(final Context context, final Bundle bundle) {
64+
public static String parseBundle(@NonNull final Context context, final Bundle bundle) {
5465
if (bundle == null) return context.getString(R.string.error_null_or_empty_executable);
5566

5667
/*
@@ -61,6 +72,7 @@ public static String parseBundle(final Context context, final Bundle bundle) {
6172
* - BUNDLE_EXTRA_INT_VERSION_CODE
6273
* The bundle may optionally contain:
6374
* - EXTRA_WORKDIR
75+
* - EXTRA_STDIN
6476
* - EXTRA_TERMINAL
6577
* - EXTRA_WAIT_FOR_RESULT
6678
* - VARIABLE_REPLACE_KEYS
@@ -79,13 +91,13 @@ public static String parseBundle(final Context context, final Bundle bundle) {
7991
}
8092

8193
/*
82-
* Check if bundle contains at least 3 keys but no more than 7.
94+
* Check if bundle contains at least 3 keys but no more than 8.
8395
* Run this test after checking for required Bundle extras above so that the error message
8496
* is more useful. (E.g. the caller will see what extras are missing, rather than just a
8597
* message that there is the wrong number).
8698
*/
87-
if (bundle.keySet().size() < 3 || bundle.keySet().size() > 7) {
88-
return String.format("The bundle must contain 3-7 keys, but currently contains %d keys.", bundle.keySet().size());
99+
if (bundle.keySet().size() < 3 || bundle.keySet().size() > 8) {
100+
return String.format("The bundle must contain 3-8 keys, but currently contains %d keys.", bundle.keySet().size());
89101
}
90102

91103
if (TextUtils.isEmpty(bundle.getString(EXTRA_EXECUTABLE))) {
@@ -109,8 +121,9 @@ public static String parseBundle(final Context context, final Bundle bundle) {
109121
}
110122

111123
@Nullable
112-
public static Bundle generateBundle(final Context context, final String executable,
124+
public static Bundle generateBundle(@NonNull final Context context, final String executable,
113125
final String arguments, final String workingDirectory,
126+
final String stdin,
114127
final boolean inTerminal, final boolean waitForResult) {
115128
final Bundle result = new Bundle();
116129
result.putString(EXTRA_EXECUTABLE, executable);
@@ -119,6 +132,9 @@ public static Bundle generateBundle(final Context context, final String executab
119132
result.putBoolean(EXTRA_TERMINAL, inTerminal);
120133
result.putBoolean(EXTRA_WAIT_FOR_RESULT, waitForResult);
121134

135+
if (!inTerminal)
136+
result.putString(EXTRA_STDIN, stdin);
137+
122138
Integer versionCode = PackageUtils.getVersionCodeForPackage(context);
123139
if (versionCode == null) {
124140
Logger.showToast(context, context.getString(R.string.error_get_version_code_failed, context.getPackageName()), true);
@@ -128,4 +144,37 @@ public static Bundle generateBundle(final Context context, final String executab
128144
result.putInt(BUNDLE_EXTRA_INT_VERSION_CODE, versionCode);
129145
return result;
130146
}
147+
148+
/**
149+
* The message that will be displayed by the plugin host app for the action configuration.
150+
* Blurb length can be a maximum of 60 characters as defined by locale lib.
151+
* @return A blurb for the plug-in.
152+
*/
153+
public static String generateBlurb(@NonNull final Context context, final String executable,
154+
final String arguments, final String workingDirectory, final String stdin,
155+
final boolean inTerminal, final boolean waitForResult) {
156+
StringBuilder builder = new StringBuilder();
157+
builder.append(context.getString(R.string.blurb_executable_and_arguments, executable, arguments));
158+
builder.append("\n\n").append(context.getString(R.string.blurb_working_directory, (!DataUtils.isNullOrEmpty(workingDirectory) ? UNICODE_CHECK : UNICODE_UNCHECK)));
159+
160+
if (!inTerminal)
161+
builder.append("\n").append(context.getString(R.string.blurb_stdin, (!DataUtils.isNullOrEmpty(stdin) ? UNICODE_CHECK : UNICODE_UNCHECK)));
162+
163+
builder.append("\n").append(context.getString(R.string.blurb_in_terminal, (inTerminal ? UNICODE_CHECK : UNICODE_UNCHECK)));
164+
builder.append("\n").append(context.getString(R.string.blurb_wait_for_result, (waitForResult ? UNICODE_CHECK : UNICODE_UNCHECK)));
165+
166+
String blurb = builder.toString();
167+
final int maxBlurbLength = 120; // R.integer.twofortyfouram_locale_maximum_blurb_length is set to 60 but we are ignoring that since Tasker doesn't have that limit.
168+
return (blurb.length() > maxBlurbLength) ? blurb.substring(0, maxBlurbLength) : blurb;
169+
}
170+
171+
/** Get size of {@link Bundle} when stored as a {@link Parcel}. */
172+
public static int getBundleSize(@NonNull Bundle bundle) {
173+
Parcel parcel = Parcel.obtain();
174+
bundle.writeToParcel(parcel, 0);
175+
int size = parcel.dataSize();
176+
parcel.recycle();
177+
return size;
178+
}
179+
131180
}

0 commit comments

Comments
 (0)