Skip to content
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

View intent on Android #89

Open
armandn opened this issue Feb 23, 2024 · 1 comment
Open

View intent on Android #89

armandn opened this issue Feb 23, 2024 · 1 comment

Comments

@armandn
Copy link

armandn commented Feb 23, 2024

I managed to register my app as the default handler for a file type (mime type) and I think the plugin could be modified/extended to do this as well.

Note that in my tests I did not register a separate activity, I used MainActivity. it seems to work fine, although I'll have to do more tests.

In AndroidManifest.xml:

<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="[MY MIME TYPE]" />
</intent-filter>

I then modified SendIntent.java:

In checkSendIntentReceived:

...
if ((Intent.ACTION_SEND.equals(action) || Intent.ACTION_VIEW.equals(action)) && type != null) {
...

In readItemAt:
from

       Uri uri = null;

        if (intent.getClipData() != null && intent.getClipData().getItemAt(index) != null)
            uri = intent.getClipData().getItemAt(index).getUri();

        String url = null;

to

        Uri uri = null;
        String url = null;

        if (intent.getClipData() != null && intent.getClipData().getItemAt(index) != null)
            uri = intent.getClipData().getItemAt(index).getUri();
        else if (intent.getData() != null) {
            Uri copyfileUri = copyfile(intent.getData());
            url = (copyfileUri != null) ? copyfileUri.toString() : null;
        }

I also modified readFileName() to avoid some possible errors:

    public String readFileName(Uri uri) {
        try (Cursor returnCursor = getContext().getContentResolver().query(uri, null, null, null, null)) {
            if (returnCursor != null && returnCursor.moveToFirst()) {
                int displayNameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                if (displayNameIndex != -1) {
                    return returnCursor.getString(displayNameIndex);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

Finally, I also have this in MainActivity.java:

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String action = intent.getAction();
        String type = intent.getType();
        if ((Intent.ACTION_SEND.equals(action) || Intent.ACTION_VIEW.equals(action)) && type != null) {
            bridge.getActivity().setIntent(intent);
            bridge.eval("window.dispatchEvent(new Event('sendIntentReceived'))", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String s) {
                }
            });
        }
    }

I am pretty new at Android so I'm not entirely sure the code is correct and doesn't introduce other unwanted behavior.

@carsten-klaffke
Copy link
Owner

Hello @armandn, thanks for sharing this! This was requested in 87. We'll have to see if your code changes interfere with the "normal" share functionality. But if not, we could include this as an addition to the project. If you find time, please create a pull request for this!

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

2 participants