From 34797837363588f4511403e39017bf6b656685cb Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Thu, 24 Jun 2021 19:38:22 +0200 Subject: [PATCH] fix(share): Prevent share if sharing in progress (#489) --- .../plugins/share/SharePlugin.java | 121 +++++++++--------- share/ios/Plugin/SharePlugin.swift | 6 +- 2 files changed, 68 insertions(+), 59 deletions(-) diff --git a/share/android/src/main/java/com/capacitorjs/plugins/share/SharePlugin.java b/share/android/src/main/java/com/capacitorjs/plugins/share/SharePlugin.java index 7a05a1a59..23efd2961 100644 --- a/share/android/src/main/java/com/capacitorjs/plugins/share/SharePlugin.java +++ b/share/android/src/main/java/com/capacitorjs/plugins/share/SharePlugin.java @@ -21,6 +21,7 @@ public class SharePlugin extends Plugin { private BroadcastReceiver broadcastReceiver; private boolean stopped = false; + private boolean isPresenting = false; private ComponentName chosenComponent; @Override @@ -46,76 +47,82 @@ private void activityResult(PluginCall call, ActivityResult result) { callResult.put("activityType", chosenComponent != null ? chosenComponent.getPackageName() : ""); call.resolve(callResult); } + isPresenting = false; } @PluginMethod public void share(PluginCall call) { - String title = call.getString("title", ""); - String text = call.getString("text"); - String url = call.getString("url"); - String dialogTitle = call.getString("dialogTitle", "Share"); - - if (text == null && url == null) { - call.reject("Must provide a URL or Message"); - return; - } - - if (url != null && !isFileUrl(url) && !isHttpUrl(url)) { - call.reject("Unsupported url"); - return; - } + if (!isPresenting) { + String title = call.getString("title", ""); + String text = call.getString("text"); + String url = call.getString("url"); + String dialogTitle = call.getString("dialogTitle", "Share"); + + if (text == null && url == null) { + call.reject("Must provide a URL or Message"); + return; + } - Intent intent = new Intent(Intent.ACTION_SEND); + if (url != null && !isFileUrl(url) && !isHttpUrl(url)) { + call.reject("Unsupported url"); + return; + } - if (text != null) { - // If they supplied both fields, concat them - if (url != null && isHttpUrl(url)) text = text + " " + url; - intent.putExtra(Intent.EXTRA_TEXT, text); - intent.setTypeAndNormalize("text/plain"); - } + Intent intent = new Intent(Intent.ACTION_SEND); - 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); - if (type == null) { - type = "*/*"; + if (text != null) { + // If they supplied both fields, concat them + if (url != null && isHttpUrl(url)) text = text + " " + url; + intent.putExtra(Intent.EXTRA_TEXT, text); + intent.setTypeAndNormalize("text/plain"); } - intent.setType(type); - Uri fileUrl = FileProvider.getUriForFile( - getActivity(), - getContext().getPackageName() + ".fileprovider", - new File(Uri.parse(url).getPath()) - ); - intent.putExtra(Intent.EXTRA_STREAM, fileUrl); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - intent.setData(fileUrl); + + 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); + if (type == null) { + type = "*/*"; + } + intent.setType(type); + Uri fileUrl = FileProvider.getUriForFile( + getActivity(), + getContext().getPackageName() + ".fileprovider", + new File(Uri.parse(url).getPath()) + ); + intent.putExtra(Intent.EXTRA_STREAM, fileUrl); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + intent.setData(fileUrl); + } + intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } - intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - } - if (title != null) { - intent.putExtra(Intent.EXTRA_SUBJECT, title); - } + if (title != null) { + intent.putExtra(Intent.EXTRA_SUBJECT, title); + } - Intent chooser = null; - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { - // requestCode parameter is not used. Providing 0 - PendingIntent pi = PendingIntent.getBroadcast( - getContext(), - 0, - new Intent(Intent.EXTRA_CHOSEN_COMPONENT), - PendingIntent.FLAG_UPDATE_CURRENT - ); - chooser = Intent.createChooser(intent, dialogTitle, pi.getIntentSender()); - chosenComponent = null; + Intent chooser = null; + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { + // requestCode parameter is not used. Providing 0 + PendingIntent pi = PendingIntent.getBroadcast( + getContext(), + 0, + new Intent(Intent.EXTRA_CHOSEN_COMPONENT), + PendingIntent.FLAG_UPDATE_CURRENT + ); + chooser = Intent.createChooser(intent, dialogTitle, pi.getIntentSender()); + chosenComponent = null; + } else { + chooser = Intent.createChooser(intent, dialogTitle); + } + chooser.addCategory(Intent.CATEGORY_DEFAULT); + stopped = false; + isPresenting = true; + startActivityForResult(call, chooser, "activityResult"); } else { - chooser = Intent.createChooser(intent, dialogTitle); + call.reject("Can't share while sharing is in progress"); } - chooser.addCategory(Intent.CATEGORY_DEFAULT); - stopped = false; - startActivityForResult(call, chooser, "activityResult"); } @Override diff --git a/share/ios/Plugin/SharePlugin.swift b/share/ios/Plugin/SharePlugin.swift index b3c686980..e2c767ff5 100644 --- a/share/ios/Plugin/SharePlugin.swift +++ b/share/ios/Plugin/SharePlugin.swift @@ -44,10 +44,12 @@ public class SharePlugin: CAPPlugin { } } - + if self?.bridge?.viewController?.presentedViewController != nil { + call.reject("Can't share while sharing is in progress") + return + } self?.setCenteredPopover(actionController) self?.bridge?.viewController?.present(actionController, animated: true, completion: nil) } - } }