Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	plugin.xml
  • Loading branch information
EddyVerbruggen committed Jan 31, 2016
2 parents 99739d3 + 13788ee commit 093059b
Show file tree
Hide file tree
Showing 13 changed files with 604 additions and 423 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ iOS Quirks:
<button onclick="window.plugins.socialsharing.shareViaWhatsApp('Message via WhatsApp', null /* img */, null /* url */, function() {console.log('share ok')}, function(errormsg){alert(errormsg)})">msg via WhatsApp (with errcallback)</button>
```

##### Experimental feature: sharing directly to someone
Available in 5.0.8 and up - please let me know if this works for your device! Open an issue if not..

```html
<button onclick="window.plugins.socialsharing.shareViaWhatsAppToReceiver(receiver, 'Message via WhatsApp', null /* img */, null /* url */, function() {console.log('share ok')})">msg via WhatsApp for Addressbook ID 101</button>
```
For `receiver` on iOS pass in the Addressbook ID (or 'abid'). You can find those abid's by using the [Cordova Contacts Plugin](https://github.com/apache/cordova-plugin-contacts).
The result in the success callback of the `find` function is a JSON array of contact objects, use the 'id' you find in those objects.
Don't pass in an image on iOS because that can't be sent to someone directly unfortunately. Message and URL are fine though.

On Android pass in the phone number of the person you want to send a message to (untested at the moment).

####SMS
Note that on Android, SMS via Hangouts may not behave correctly
```html
Expand Down Expand Up @@ -381,7 +393,7 @@ Here's the list of available activities you can disable :


## 4b. Usage on Windows Phone
The available methods on WP8 are: `available`, `canShareViaEmail`, `share` and `shareViaEmail`.
The available methods on WP8 are: `available`, `canShareViaEmail`, `share`, `shareViaEmail` and `shareViaSMS`.
Currently the first two always return true, but this may change in the future in case I can find a way to truly detect the availability.

The `share` function on WP8 supports two flavours: message only, or a combination of message, title and link.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-x-socialsharing",
"version": "5.0.6",
"version": "5.0.10",
"description": "Share text, images (and other files), or a link via the native sharing widget of your device. Android is fully supported, as well as iOS 6 and up. WP8 has somewhat limited support.",
"cordova": {
"id": "cordova-plugin-x-socialsharing",
Expand Down Expand Up @@ -29,7 +29,7 @@
"ecosystem:cordova",
"cordova-ios",
"cordova-android",
"cordova-wp8"
"cordova-windows"
],
"engines": [
{
Expand Down
5 changes: 3 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="nl.x-services.plugins.socialsharing"
version="5.0.6">
id="cordova-plugin-x-socialsharing"
version="5.0.10">

<name>SocialSharing</name>

Expand Down Expand Up @@ -71,6 +71,7 @@
</config-file>

<source-file src="src/wp8/SocialSharing.cs" />
<framework src="src/wp8/Newtonsoft.Json.dll" custom="true"/>
</platform>

<!-- windows -->
Expand Down
88 changes: 85 additions & 3 deletions src/android/nl/xservices/plugins/SocialSharing.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public class SocialSharing extends CordovaPlugin {
private static final String ACTION_SHARE_VIA_SMS_EVENT = "shareViaSMS";
private static final String ACTION_SHARE_VIA_EMAIL_EVENT = "shareViaEmail";

private static final int ACTIVITY_CODE_SEND = 1;
private static final int ACTIVITY_CODE_SENDVIAEMAIL = 2;
private static final int ACTIVITY_CODE_SENDVIAWHATSAPP = 3;

private CallbackContext _callbackContext;

Expand Down Expand Up @@ -77,7 +79,11 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
this.pasteMessage = args.getString(4);
return doSendIntent(callbackContext, args.getString(0), args.getString(1), args.getJSONArray(2), args.getString(3), "com.facebook.katana", false);
} else if (ACTION_SHARE_VIA_WHATSAPP_EVENT.equals(action)) {
return doSendIntent(callbackContext, args.getString(0), args.getString(1), args.getJSONArray(2), args.getString(3), "whatsapp", false);
if (notEmpty(args.getString(4))) {
return shareViaWhatsAppDirectly(callbackContext, args.getString(0), args.getString(1), args.getJSONArray(2), args.getString(3), args.getString(4));
} else {
return doSendIntent(callbackContext, args.getString(0), args.getString(1), args.getJSONArray(2), args.getString(3), "whatsapp", false);
}
} else if (ACTION_SHARE_VIA_INSTAGRAM_EVENT.equals(action)) {
if (notEmpty(args.getString(0))) {
copyHintToClipboard(args.getString(0), "Instagram paste message");
Expand Down Expand Up @@ -222,6 +228,7 @@ public void run() {
if (notEmpty(subject)) {
sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
}

// add the URL to the message, as there seems to be no separate field
if (notEmpty(url)) {
if (notEmpty(message)) {
Expand Down Expand Up @@ -275,7 +282,7 @@ public void run() {
if (peek) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
} else {
mycordova.startActivityForResult(plugin, Intent.createChooser(sendIntent, null), 1);
mycordova.startActivityForResult(plugin, Intent.createChooser(sendIntent, null), ACTIVITY_CODE_SEND);
}
}
}
Expand Down Expand Up @@ -319,6 +326,10 @@ private Uri getFileUriAndSetType(Intent sendIntent, String dir, String image, St
Matcher matcher = dispositionPattern.matcher(disposition);
if (matcher.find()) {
filename = matcher.group(1).replaceAll("[^a-zA-Z0-9._-]", "");
if (filename.length() == 0) {
// in this case we can't determine a filetype so some targets (gmail) may not render it correctly
filename = "file";
}
localImage = "file://" + dir + "/" + filename;
}
}
Expand Down Expand Up @@ -350,12 +361,80 @@ private Uri getFileUriAndSetType(Intent sendIntent, String dir, String image, St
}
saveFile(Base64.decode(encodedImg, Base64.DEFAULT), dir, fileName);
localImage = "file://" + dir + "/" + fileName;
} else if (image.startsWith("df:")) {
// safeguard for https://code.google.com/p/android/issues/detail?id=7901#c43
if (!image.contains(";base64,")) {
sendIntent.setType("text/plain");
return null;
}
// format looks like this : df:filename.txt;data:image/png;base64,R0lGODlhDAA...
final String fileName = image.substring(image.indexOf("df:") + 3, image.indexOf(";data:"));
final String fileType = image.substring(image.indexOf(";data:") + 6, image.indexOf(";base64,"));
final String encodedImg = image.substring(image.indexOf(";base64,") + 8);
sendIntent.setType(fileType);
saveFile(Base64.decode(encodedImg, Base64.DEFAULT), dir, sanitizeFilename(fileName));
localImage = "file://" + dir + "/" + fileName;
} else if (!image.startsWith("file://")) {
throw new IllegalArgumentException("URL_NOT_SUPPORTED");
}
return Uri.parse(localImage);
}

private boolean shareViaWhatsAppDirectly(final CallbackContext callbackContext, String message, final String subject, final JSONArray files, final String url, final String number) {
// add the URL to the message, as there seems to be no separate field
if (notEmpty(url)) {
if (notEmpty(message)) {
message += " " + url;
} else {
message = url;
}
}
final String shareMessage = message;
final SocialSharing plugin = this;
cordova.getThreadPool().execute(new SocialSharingRunnable(callbackContext) {
public void run() {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + number));

intent.putExtra("sms_body", shareMessage);
intent.putExtra("sms_subject", subject);
intent.setPackage("com.whatsapp");

try {
if (files.length() > 0 && !"".equals(files.getString(0))) {
final boolean hasMultipleAttachments = files.length() > 1;
final String dir = getDownloadDir();
if (dir != null) {
ArrayList<Uri> fileUris = new ArrayList<Uri>();
Uri fileUri = null;
for (int i = 0; i < files.length(); i++) {
fileUri = getFileUriAndSetType(intent, dir, files.getString(i), subject, i);
if (fileUri != null) {
fileUris.add(fileUri);
}
}
if (!fileUris.isEmpty()) {
if (hasMultipleAttachments) {
intent.putExtra(Intent.EXTRA_STREAM, fileUris);
} else {
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
}
}
}
}
} catch (Exception e) {
callbackContext.error(e.getMessage());
}
try {
cordova.startActivityForResult(plugin, intent, ACTIVITY_CODE_SENDVIAWHATSAPP);
} catch (Exception e) {
callbackContext.error(e.getMessage());
}
}
});
return true;
}

private boolean invokeSMSIntent(final CallbackContext callbackContext, JSONObject options, String p_phonenumbers) {
final String message = options.optString("message");
// TODO test this on a real SMS enabled device before releasing it
Expand Down Expand Up @@ -458,13 +537,16 @@ private void createOrCleanDir(final String downloadDir) throws IOException {
}

private static String getFileName(String url) {
if (url.endsWith("/")) {
url = url.substring(0, url.length()-1);
}
final String pattern = ".*/([^?#]+)?";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(url);
if (m.find()) {
return m.group(1);
} else {
return null;
return "file";
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ios/SocialSharing.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@interface SocialSharing : CDVPlugin <UIPopoverControllerDelegate, MFMailComposeViewControllerDelegate, UIDocumentInteractionControllerDelegate>

@property (nonatomic, strong) MFMailComposeViewController *globalMailComposer;
@property (retain) UIDocumentInteractionController * documentInteractionController;
@property (nonatomic, strong) UIDocumentInteractionController * documentInteractionController;
@property (retain) NSString * tempStoredFile;
@property (retain) CDVInvokedUrlCommand * command;

Expand Down
Loading

0 comments on commit 093059b

Please sign in to comment.