From b6328f0aaa9c3fa139bf8d639460c029d9967c0b Mon Sep 17 00:00:00 2001 From: Vincent Velociter Date: Tue, 17 Mar 2020 15:27:22 +0100 Subject: [PATCH] fix(android): allow Share plugin to provide text or url only (#2436) --- .../java/com/getcapacitor/plugin/Share.java | 17 +++++++++++------ example/src/pages/share/share.html | 6 ++++++ example/src/pages/share/share.ts | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/Share.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/Share.java index 70d8ab877d..4c6e6bd9e1 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/Share.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/Share.java @@ -28,13 +28,18 @@ public void share(PluginCall call) { return; } Intent intent = new Intent(Intent.ACTION_SEND); - // If they supplied both fields, concat em - if (text != null && url != null && url.startsWith("http")) { - text = text + " " + url; - intent.setTypeAndNormalize("text/plain"); + if (text != null) { + // If they supplied both fields, concat em + if (url != null && url.startsWith("http")) { + text = text + " " + url; + } intent.putExtra(Intent.EXTRA_TEXT, text); - } else if(url != null) { - if (url.startsWith("file:")) { + intent.setTypeAndNormalize("text/plain"); + } else if (url != null) { + if (url.startsWith("http")) { + intent.putExtra(Intent.EXTRA_TEXT, url); + intent.setTypeAndNormalize("text/plain"); + } else if (url.startsWith("file:")) { String type = getMimeType(url); intent.setType(type); Uri fileUrl = FileProvider.getUriForFile(getActivity(), getContext().getPackageName() + ".fileprovider", new File(Uri.parse(url).getPath())); diff --git a/example/src/pages/share/share.html b/example/src/pages/share/share.html index 25e3bcf405..b5c0111e99 100644 --- a/example/src/pages/share/share.html +++ b/example/src/pages/share/share.html @@ -17,4 +17,10 @@ + + diff --git a/example/src/pages/share/share.ts b/example/src/pages/share/share.ts index 941f2391ab..0c39aef5df 100644 --- a/example/src/pages/share/share.ts +++ b/example/src/pages/share/share.ts @@ -34,4 +34,18 @@ export class SharePage { console.log('Share return', shareRet); } + async showSharingTextOnly() { + let shareRet = await Share.share({ + text: 'Really awesome thing you need to see right meow', + }); + console.log('Share return', shareRet); + } + + async showSharingUrlOnly() { + let shareRet = await Share.share({ + url: 'http://ionicframework.com/', + }); + console.log('Share return', shareRet); + } + }