Skip to content

Commit

Permalink
Merge pull request #8 from TheNothingMan/master
Browse files Browse the repository at this point in the history
Added the ability to use Tasker variables as arguments for scripts.
  • Loading branch information
fornwall committed Sep 14, 2017
2 parents 786db2b + ff23faa commit 8ea25bd
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 13 deletions.
18 changes: 14 additions & 4 deletions app/src/main/java/com/termux/tasker/EditConfigurationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.EditText;

import java.io.File;

Expand All @@ -28,6 +29,7 @@ public final class EditConfigurationActivity extends AbstractPluginActivity {
public static final File TASKER_DIR = new File("/data/data/com.termux/files/home/.termux/tasker/");

private AutoCompleteTextView mExecutableText;
private EditText mArgumentsText;
private CheckBox mInTerminalCheckbox;

@Override
Expand Down Expand Up @@ -63,6 +65,7 @@ public void onDismiss(DialogInterface dialogInterface) {
BundleScrubber.scrub(localeBundle);

mExecutableText = (AutoCompleteTextView) findViewById(R.id.executable_path);
mArgumentsText = (EditText) findViewById(R.id.arguments);
mInTerminalCheckbox = (CheckBox) findViewById(R.id.in_terminal);

final File[] files = TASKER_DIR.listFiles();
Expand Down Expand Up @@ -100,6 +103,8 @@ public void afterTextChanged(Editable editable) {
if (PluginBundleManager.isBundleValid(localeBundle)) {
final String selectedExecutable = localeBundle.getString(PluginBundleManager.EXTRA_EXECUTABLE);
mExecutableText.setText(selectedExecutable);
final String selectedArguments = localeBundle.getString(PluginBundleManager.EXTRA_ARGUMENTS);
mArgumentsText.setText(selectedArguments);
final boolean inTerminal = localeBundle.getBoolean(PluginBundleManager.EXTRA_TERMINAL);
mInTerminalCheckbox.setChecked(inTerminal);
}
Expand All @@ -110,6 +115,7 @@ public void afterTextChanged(Editable editable) {
public void finish() {
if (!isCanceled()) {
final String executable = mExecutableText.getText().toString();
final String arguments = mArgumentsText.getText().toString();
final boolean inTerminal = mInTerminalCheckbox.isChecked();

if (executable.length() > 0) {
Expand All @@ -123,10 +129,14 @@ public void finish() {
* Android platform objects (A Serializable class private to this plug-in's APK cannot be
* stored in the Bundle, as Locale's classloader will not recognize it).
*/
final Bundle resultBundle = PluginBundleManager.generateBundle(getApplicationContext(), executable, inTerminal);
final Bundle resultBundle = PluginBundleManager.generateBundle(getApplicationContext(), executable, arguments, inTerminal);

// The blurb is a concise status text to be displayed in the host's UI.
final String blurb = generateBlurb(executable, inTerminal);
final String blurb = generateBlurb(executable, arguments, inTerminal);
if (TaskerPlugin.Setting.hostSupportsOnFireVariableReplacement(this)){
TaskerPlugin.Setting.setVariableReplaceKeys(resultBundle,new String[] {PluginBundleManager.EXTRA_EXECUTABLE,
PluginBundleManager.EXTRA_ARGUMENTS,PluginBundleManager.EXTRA_TERMINAL});
}

resultIntent.putExtra(com.twofortyfouram.locale.Intent.EXTRA_BUNDLE, resultBundle);
resultIntent.putExtra(com.twofortyfouram.locale.Intent.EXTRA_STRING_BLURB, blurb);
Expand All @@ -141,9 +151,9 @@ public void finish() {
* @param executable The toast message to be displayed by the plug-in. Cannot be null.
* @return A blurb for the plug-in.
*/
String generateBlurb(final String executable, boolean inTerminal) {
String generateBlurb(final String executable, final String arguments, boolean inTerminal) {
final int stringResource = inTerminal ? R.string.blurb_in_terminal : R.string.blurb_in_background;
final String message = getString(stringResource, executable);
final String message =getString(stringResource, executable,arguments);
final int maxBlurbLength = 60; // R.integer.twofortyfouram_locale_maximum_blurb_length.
return (message.length() > maxBlurbLength) ? message.substring(0, maxBlurbLength) : message;
}
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/termux/tasker/FireReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* This is the "fire" BroadcastReceiver for a Locale Plug-in.
Expand All @@ -31,7 +35,13 @@ public void onReceive(final Context context, final Intent intent) {
if (!PluginBundleManager.isBundleValid(bundle)) return;

final String executable = bundle.getString(PluginBundleManager.EXTRA_EXECUTABLE);
final String arguments = bundle.getString(PluginBundleManager.EXTRA_ARGUMENTS);
final boolean inTerminal = bundle.getBoolean(PluginBundleManager.EXTRA_TERMINAL);
Matcher matcher = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(arguments);
List<String> list = new ArrayList<String>();
while (matcher.find()){
list.add(matcher.group(1).replace("\"",""));
}

File executableFile = new File(EditConfigurationActivity.TASKER_DIR, executable);
if (!executableFile.isFile()) {
Expand All @@ -47,6 +57,7 @@ public void onReceive(final Context context, final Intent intent) {
Intent executeIntent = new Intent(ACTION_EXECUTE, scriptUri);
executeIntent.setClassName("com.termux", TERMUX_SERVICE);
if (!inTerminal) executeIntent.putExtra("com.termux.execute.background", true);
executeIntent.putExtra(PluginBundleManager.EXTRA_ARGUMENTS, list.toArray(new String[list.size()]));
context.startService(executeIntent);
}

Expand Down
22 changes: 19 additions & 3 deletions app/src/main/java/com/termux/tasker/PluginBundleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
*/
final class PluginBundleManager {

/**
* Type: {@code sting}.
*
* The arguments to pass to the script.
*/
public static final String EXTRA_ARGUMENTS = "com.termux.execute.arguments";

/**
* Type: {@code String}.
*
Expand Down Expand Up @@ -51,6 +58,11 @@ public static boolean isBundleValid(final Bundle bundle) {
return false;
}

if (!bundle.containsKey(EXTRA_ARGUMENTS)) {
Log.e(Constants.LOG_TAG, String.format("bundle must contain extra %s", EXTRA_ARGUMENTS));
return false;
}

if (!bundle.containsKey(BUNDLE_EXTRA_INT_VERSION_CODE)) {
Log.e(Constants.LOG_TAG, String.format("bundle must contain extra %s", BUNDLE_EXTRA_INT_VERSION_CODE));
return false;
Expand All @@ -61,8 +73,11 @@ public static boolean isBundleValid(final Bundle bundle) {
* extras above so that the error message is more useful. (E.g. the caller will see what extras are
* missing, rather than just a message that there is the wrong number).
*/
if (3 != bundle.keySet().size()) {
Log.e(Constants.LOG_TAG, String.format("bundle must contain 3 keys, but currently contains %d keys", bundle.keySet().size()));
if (4 != bundle.keySet().size()) {
if (bundle.containsKey("net.dinglisch.android.tasker.extras.VARIABLE_REPLACE_KEYS")){
return true;
}
Log.e(Constants.LOG_TAG, String.format("bundle must contain 4 keys, but currently contains %d keys", bundle.keySet().size()));
return false;
}

Expand All @@ -79,9 +94,10 @@ public static boolean isBundleValid(final Bundle bundle) {
return true;
}

public static Bundle generateBundle(final Context context, final String executable, final boolean inTerminal) {
public static Bundle generateBundle(final Context context, final String executable, final String arguments, final boolean inTerminal) {
final Bundle result = new Bundle();
result.putInt(BUNDLE_EXTRA_INT_VERSION_CODE, Constants.getVersionCode(context));
result.putString(EXTRA_ARGUMENTS,arguments);
result.putString(EXTRA_EXECUTABLE, executable);
result.putBoolean(EXTRA_TERMINAL, inTerminal);
return result;
Expand Down
19 changes: 15 additions & 4 deletions app/src/main/res/layout/edit_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,25 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:id="@+id/argument_label"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/arguments"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:hint="@string/arguments_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<CheckBox
android:id="@+id/in_terminal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"

android:layout_width="match_parent"
android:layout_height="wrap_content"

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:text="@string/execute_in_terminal" />

</LinearLayout>
5 changes: 3 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
<string name="no_tasker_folder_title">No ~/.termux/tasker/ directory</string>
<string name="no_tasker_folder_message">You need to create a ~/.termux/tasker/ directory containing scripts to be executed.</string>

<string name="blurb_in_background">Execute ~/.termux/tasker/%s.</string>
<string name="blurb_in_terminal">Execute ~/.termux/tasker/%s in a terminal session.</string>
<string name="blurb_in_background">Execute ~/.termux/tasker/%1$s %2$s</string>
<string name="blurb_in_terminal">Execute ~/.termux/tasker/%1$s %2$s in a terminal session.</string>

<!-- From Tasker API: -->
<string name="twofortyfouram_locale_breadcrumb_format" tools:ignore="UnusedResources">%1$s%2$s%3$s</string>
<string name="twofortyfouram_locale_breadcrumb_separator" tools:ignore="UnusedResources">\u0020&gt;\u0020</string>
<string name="twofortyfouram_locale_menu_dontsave">Cancel</string>
<string name="twofortyfouram_locale_menu_save">Done</string>
<string name="arguments_hint">Arguments</string>

</resources>

0 comments on commit 8ea25bd

Please sign in to comment.