Skip to content

Commit

Permalink
fix(share): Prevent share if sharing in progress (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Jun 24, 2021
1 parent 821c4f6 commit 3479783
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 59 deletions.
Expand Up @@ -21,6 +21,7 @@ public class SharePlugin extends Plugin {

private BroadcastReceiver broadcastReceiver;
private boolean stopped = false;
private boolean isPresenting = false;
private ComponentName chosenComponent;

@Override
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions share/ios/Plugin/SharePlugin.swift
Expand Up @@ -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)
}

}
}

0 comments on commit 3479783

Please sign in to comment.