Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(share): Prevent share if sharing in progress #489

Merged
merged 4 commits into from Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}

}
}