carsten-klaffke / send-intent

Repository for send-intent Capacitor plugin
MIT License
106 stars 12 forks source link

View intent on Android #89

Open armandn opened 6 months ago

armandn commented 6 months ago

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 commented 6 months ago

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!