From b28ed26a2235e77ce5d57f7b8212e9618887b65c Mon Sep 17 00:00:00 2001 From: Nikos Date: Fri, 10 Jul 2020 00:07:33 +0300 Subject: [PATCH] feat(android): share both text and url at the same time --- .../java/com/getcapacitor/plugin/Share.java | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 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 4c6e6bd9e1..3385df01c2 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/Share.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/Share.java @@ -27,27 +27,29 @@ public void share(PluginCall call) { call.error("Must provide a URL or Message"); return; } + + if(url != null && !isFileUrl(url) && !isHttpUrl(url)) { + call.error("Unsupported url"); + return; + } + Intent intent = new Intent(Intent.ACTION_SEND); + if (text != null) { // If they supplied both fields, concat em - if (url != null && url.startsWith("http")) { - text = text + " " + url; - } + if (url != null && isHttpUrl(url)) text = text + " " + url; intent.putExtra(Intent.EXTRA_TEXT, text); 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())); - intent.putExtra(Intent.EXTRA_STREAM, fileUrl); - } else { - call.error("Unsupported url"); - return; - } + } + + if(url != null && isHttpUrl(url) && text == null) { + intent.putExtra(Intent.EXTRA_TEXT, url); + intent.setTypeAndNormalize("text/plain"); + } else if (url != null && isFileUrl(url)) { + String type = getMimeType(url); + intent.setType(type); + Uri fileUrl = FileProvider.getUriForFile(getActivity(), getContext().getPackageName() + ".fileprovider", new File(Uri.parse(url).getPath())); + intent.putExtra(Intent.EXTRA_STREAM, fileUrl); } if (title != null) { @@ -69,4 +71,12 @@ private String getMimeType(String url) { } return type; } + + private boolean isFileUrl(String url) { + return url.startsWith("file:"); + } + + private boolean isHttpUrl(String url) { + return url.startsWith("http"); + } }