Setting and retrieving data with action plugin

181 views
Skip to first unread message

barryb...@gmail.com

unread,
Sep 27, 2014, 9:52:25 PM9/27/14
to task...@googlegroups.com
I am writing a Tasker action plugin for my app. I am storing just a single (but very long, approximately 1800-2000 characters) String in a Bundle set at "com.twofortyfouram.locale.Intent.EXTRA_BUNDLE" within the intent.

My EditAction.java works fine, and saves the String/Bundle fine with this code:

                        Bundle extra = new Bundle();
                        extra.putString(EditAction.ACTION_DATA, myString);
                        i.putExtra(PluginReceiver.PLUGIN_BUNDLE_EXTRA, extra);
                        i.putExtra(PluginReceiver.PLUGIN_BLURB_EXTRA, myBlurb);
                        setResult(RESULT_OK, i);

But this goes sour when I am receiving the Bundle back again, either with my EditAction.java or the FireAction.java. My code for parsing the received intent looks like this:

        if (i.hasExtra(PluginReceiver.PLUGIN_BUNDLE_EXTRA)) {
            Bundle pluginBundle = i.getBundleExtra(PluginReceiver.PLUGIN_BUNDLE_EXTRA); //REFERENCE "a"
            String stringData;
            if (pluginBundle == null) {
                stringData = i.getStringExtra(PluginReceiver.PLUGIN_BUNDLE_EXTRA);  //REFERENCE "b"

                String prefix = "actionData=";

                int start = stringData.indexOf(prefix);
                int end = stringData.indexOf("}", start);
                if(start > 0 && end > 0) {
                    stringData = stringData.substring(start + prefix.length(), end);
                } else {
                    throw new RuntimeException("Could not find bundle data. Restart the Tasker app.");
                }
            } else {
                stringData = pluginBundle.getString(EditAction.ACTION_DATA);
            }

            return stringData;
        }

Often (in fact about 80% of the time) when I am debugging, i.getBundleExtra(...) at reference "a" will return null. Which is weird because I know for certain the data is in there from inspecting the variables in the debugger. Weirder yet, I get this in logcat after that same line finishes executing (these are not my debugging print statements):

09-27 21:16:38.882  18795-18795/com.myapp.app W/Bundle﹕ Key com.twofortyfouram.locale.Intent.EXTRA_BUNDLE expected Bundle but value was a java.lang.String.  The default value <null> was returned.
09-27 21:16:38.884  18795-18795/com.myapp.app W/Bundle﹕ Attempt to cast generated internal exception:
    java.lang.ClassCastException: java.lang.String cannot be cast to android.os.Bundle
            at android.os.Bundle.getBundle(Bundle.java:1190)
            at android.content.Intent.getBundleExtra(Intent.java:4900)
            at com.myapp.app.receivers.PluginReceiver.getActionFromIntent(PluginReceiver.java:52)
            at com.myapp.app.EditAction.onCreate(EditAction.java:87)
            at android.app.Activity.performCreate(Activity.java:5248)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)

Most of the time, the method i.getStringExtra(...) at reference "b" will return a String representation of the Bundle object I am suppose to be getting. The code following after that line (with the string manipulation) it is some stupid hack I wrote to get the data I am after out of the string returned. This gets the method working about half the time. The other half of the time the "actionData" is missing from the string, causing the RuntimeException to be thrown.

I noticed that if I force close the Tasker app, and start it again, then go into my EditAction class via editing the plugin it will sometimes get the intent to behave correctly.

Am I missing something here? Or is this behaving unexpectedly?

Thanks!



Pent

unread,
Sep 30, 2014, 2:44:11 AM9/30/14
to task...@googlegroups.com, barryb...@gmail.com
2K is not especially long, but what if you use a shorter String ?

Pent

barryb...@gmail.com

unread,
Oct 11, 2014, 7:05:20 PM10/11/14
to task...@googlegroups.com, barryb...@gmail.com
Just tested, a short string works every time with consistent results. Is this a limitation with the Tasker app? Can I just not have a piece of data 2K bytes long stored in the bundle?

-Thanks!

Benjamin Chevoor

unread,
Oct 11, 2014, 8:01:21 PM10/11/14
to task...@googlegroups.com
Actually just kidding. It still isn't working with a shorter string. When my PluginReceiver is called the bundle inside the intent is still null (same at "reference point a"). And I still get the logcat message "expected Bundle but value was a java.lang.String". 

Any thoughts?

Thanks!

barryb...@gmail.com

unread,
Oct 11, 2014, 8:02:04 PM10/11/14
to task...@googlegroups.com, barryb...@gmail.com
Actually just kidding. It still isn't working with a shorter string. When my PluginReceiver is called, the bundle inside the intent is still null (same at "reference point a"). And I still get the logcat message "expected Bundle but value was a java.lang.String". 

Any thoughts?

Thanks!

Pent

unread,
Oct 12, 2014, 8:53:48 AM10/12/14
to task...@googlegroups.com, barryb...@gmail.com
Next question, what if you disable Xposed and reboot first.

Do you have a non-rooted device for testing ?

Pent

barryb...@gmail.com

unread,
Oct 13, 2014, 11:44:04 AM10/13/14
to task...@googlegroups.com, barryb...@gmail.com
I do have a non-rooted deviced. Just stock 4.4.2. I just tested it on there, but had the same problem. Do you have any other ideas I could try?

Thanks!

Pent

unread,
Oct 13, 2014, 1:22:49 PM10/13/14
to task...@googlegroups.com, barryb...@gmail.com
Does the basic Toast example plugin work ?

http://www.twofortyfouram.com/developer/toast.zip

If so, you need to compare code closely to see what you're doing differently.

Pent

sup...@twofortyfouram.com

unread,
Oct 14, 2014, 9:38:24 AM10/14/14
to task...@googlegroups.com, barryb...@gmail.com
Your problem is here:

stringData = i.getStringExtra(PluginReceiver.PLUGIN_BUNDLE_EXTRA);  //REFERENCE "b"


It should be:

stringData = pluginBundle.getStringExtra(EditAction.ACTION_DATA);  //REFERENCE "b"

barryb...@gmail.com

unread,
Oct 18, 2014, 3:48:20 PM10/18/14
to task...@googlegroups.com, barryb...@gmail.com, sup...@twofortyfouram.com
I found the problem! It is rather stupid. The static constant I was using for the plugin bundle was incorrect. 

WAS:
public static final String PLUGIN_BUNDLE_EXTRA = "com.twofortyfouram.locale.Intent.EXTRA_BUNDLE";

NOW:
public static final String PLUGIN_BUNDLE_EXTRA = "com.twofortyfouram.locale.intent.extra.BUNDLE";

Whatever example I downloaded and copied from had this incorrect String and was the source of my issue.

Thanks for all your help, gentlemen. I appreciate it!

sup...@twofortyfouram.com

unread,
Oct 18, 2014, 7:46:50 PM10/18/14
to task...@googlegroups.com, barryb...@gmail.com
The previous post contains misleading information.  In order to avoid others being confused by this thread, let me set the record straight.  There is nothing wrong with the plug-in examples.  A freshly downloaded copy of the Toast plug-in example from here http://www.twofortyfouram.com/developer/toast.zip contains the class locale-api/src/com/twofortyfouram/locale/Intent.java and within that class the static field assignment is as follows:
    public static final String EXTRA_BUNDLE = "com.twofortyfouram.locale.intent.extra.BUNDLE"; //$NON-NLS-1$

So again, the example plug-in is correct.  Any issue was caused by modifications made to the example after it was downloaded.


Thanks,
Carter

Emanuel Moecklin

unread,
Mar 6, 2015, 4:01:04 PM3/6/15
to task...@googlegroups.com, barryb...@gmail.com, sup...@twofortyfouram.com
The confusion comes from the fact that the variable is:
com.twofortyfouram.locale.Intent.EXTRA_BUNDLE

while the String itself is:
com.twofortyfouram.locale.intent.extra.BUNDLE

which can lead to errors when the code is refactored (happened to me too).

Cheers
Emanuel
Reply all
Reply to author
Forward
0 new messages