Skip to content

Inter-application IO via android.content.Intent #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Dec 22, 2016 · 5 comments
Closed

Inter-application IO via android.content.Intent #1

ghost opened this issue Dec 22, 2016 · 5 comments
Assignees

Comments

@ghost
Copy link

ghost commented Dec 22, 2016

Please add inter application input and output via android.content.Intent. Any application can then have an expression evaluated by this application via android.app.Activity.startActivityForResult() and returned to it via android.app.Activity.OnActivityResult().

@gianluca-nitti gianluca-nitti self-assigned this Dec 22, 2016
@gianluca-nitti
Copy link
Owner

Thank you for the suggestion. I'll try to implement it asap.

@gianluca-nitti
Copy link
Owner

@FrankWestlake I just read some documentation about Intents and inter-app interactions, however I'm not sure what intent-filter(s) the ExprEval activity should accept, in your opinion? More specifically, which action and category?

    <intent-filter>
        <action android:name="android.intent.action.???"/>
        <category android:name="android.intent.category.???"/>
        <data android:mimeType="text/plain"/> <!-- an expression is a text string -->
    </intent-filter>

@ghost
Copy link
Author

ghost commented Dec 23, 2016

I'm not an Android programmer, I just program Android, so I've only learned what I need for my own applications.

ACTION
I don't know if there is an existing Intent.action for performing a calculation; probably not. However, it is proper and common to create your own action string. Something like "com.gianluca-nitti.android-expr-eval.ACTION_CALCULATE_THIS_AND_RETURN_THE_RESULT".

That action is then in your manifest's intent filter so that when other applications use it Android knows to call your Activity.

In your Activity.onCreate() use getIntent().getAction() (watch for null) to determine the action if it is a launcher start-up or the calculate-and-return.

DATA
You can have the calling application encode the request in the Intent.data or you can require that it be placed in an Extra, an android.os.Bundle() which is already attached to the Intent. The mathematical operators might cause encode/decode problems in the Action.data element so maybe it would be better to use an element in the Extra.

CATEGORY
It's been many years since I read the function of the category so I don't recall any details, but "category.DEFAULT" has always worked and seems most appropriate.

<intent-filter>
    <action android:name="com.gianluca-nitti.android-expr-eval.ACTION_CALCULATE_THIS_AND_RETURN_THE_RESULT"/>
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

I think Android only uses the category when it needs to search for an application that might be able to satisfy the intent. If a calling application does not know who to call it just gives Android the action, category, and possible data, and Android checks all manifests for an application to call. But your application can also be called explicitly by

    activity.startActivityForResult(
      new Intent()
      .setClassName("com.gianluca-nitti.android-expr-eval", "com.gianluca-nitti.android-expr-eval.MyCalcActivity")
      .setAction("com.gianluca-nitti.android-expr-eval.ACTION_CALCULATE_THIS_AND_RETURN_THE_RESULT")
      .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
      .putExtra("calculate", "1+2*3/4")
    , 123456
    );

Without the "Intent.setClassName()" Android should find your application anyway because of the action string in your IntentFilter.

Your result would be returned via setResult():

setResult(
RESULT_OK
, new Intent()
.putExtra("calculate", "1+2*3/4")
.putExtra("result", "2.5")
);

Frank

@gianluca-nitti
Copy link
Owner

OK, thanks for the explanation. I made a first basic implementation in 2108b12 with the custom action technique.
For now it doesn't return any result (I'll add this soon); I'll also try to make it work with the intent-filter for ACTION_PROCESS_TEXT, as described here.
I made a separate dialog-sized activity for this purpose, which I think is more suited for a "return result" button I'm going to add.

What actually works in 2108b12 is the following: in adb shell, you can write something like am start -a "com.github.gianlucanitti.expreval.ACTION_EVAL" --es "expression" "3*sqrt(16)-2" (which, AFAIK, is like calling activity.startActivity(...)) and you'll get this:

screenshot_1482595420

@gianluca-nitti
Copy link
Owner

Okay, I think this can be closed.
The current implementation supports both a custom intent string, com.github.gianlucanitti.expreval.ACTION_EVAL (expression to evaluate must be put in an extra named "expression", and the result will be returned as an extra named "result"), and the standard intent ACTION_PROCESS_TEXT, introduced in Android 6.0 (API level 23), that makes possible to evaluate an expression from a text selection in any application (and replace the expression text, if it's not read-only, with the result), as this animation shows:

process_text

This will be available in the next release and it's documented in the README.
Thank you very much @FrankWestlake for the suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant