33import android .annotation .SuppressLint ;
44import android .content .Context ;
55import android .os .Bundle ;
6+ import android .os .Parcel ;
67import android .text .TextUtils ;
78
9+ import androidx .annotation .NonNull ;
810import androidx .annotation .Nullable ;
911
12+ import com .termux .shared .data .DataUtils ;
1013import com .termux .shared .logger .Logger ;
1114import com .termux .shared .packages .PackageUtils ;
1215import 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