Skip to content

Commit

Permalink
Add Android Share plugin for PhoneGap -- lets us send little messages…
Browse files Browse the repository at this point in the history
… out via whatever apps support them on the device.
  • Loading branch information
bvibber committed Oct 26, 2011
1 parent 6d02d6a commit 24816b6
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
2 changes: 2 additions & 0 deletions assets/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

<script type="text/javascript" charset="utf-8" src="plugin/toast/phonegap-toast.js"></script>

<script type="text/javascript" charset="utf-8" src="plugin/share/share.js"></script>

<!-- app logic -->
<script type="text/javascript" charset="utf-8" src="js/main.js"></script>
<script type="text/javascript" charset="utf-8" src="js/bookmarks.js"></script>
Expand Down
25 changes: 25 additions & 0 deletions assets/www/plugin/share/share.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
*
* Phonegap share plugin for Android
* Kevin Schaul 2011
*
*/

var Share = function() {};

Share.prototype.show = function(content, success, fail) {
return PhoneGap.exec( function(args) {
success(args);
}, function(args) {
fail(args);
}, 'Share', '', [content]);
};

PhoneGap.addConstructor(function() {
/**
* Phonegap version < 1.0
* use the following line
*/
// PhoneGap.addPlugin('share', new Share());
PluginManager.addService("Share","com.schaul.plugins.share.Share");
});
57 changes: 57 additions & 0 deletions doc/Share/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Share plugin for Android/Phonegap
By Kevin Schaul - @foxyNinja7

## Using the plugin

### PhoneGap 1.0 and later

* Add java code to your project's build source

* Register the plugin in the plugins.xml file

```xml
<plugin name="Share" value="com.schaul.plugins.share.Share"/>
```

* Call the plugin, specifying subject, text, success function, and failure function

```javascript
window.plugins.share.show({
subject: 'I like turtles',
text: 'http://www.mndaily.com'},
function() {}, // Success function
function() {alert('Share failed')} // Failure function
);
```

## Release notes

### 8/22/2011
* Updated for PhoneGap 1.0

### 7/11/2011
* Initial release

## License

The MIT License

Copyright (c) 2011 Kevin Schaul

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
1 change: 1 addition & 0 deletions res/xml/plugins.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
<plugin name="SelectTextPlugin" value="org.wikipedia.SelectTextPlugin" />
<plugin name="SoftKeyBoard" value="com.phonegap.softkeyboard.SoftKeyBoard" />
<plugin name="ToastPlugin" value="com.chariotsolutions.toast.plugin.ToastPlugin" />
<plugin name="Share" value="com.schaul.plugins.share.Share"/>
</plugins>
40 changes: 40 additions & 0 deletions src/com/schaul/plugins/share/Share.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
*
* Phonegap share plugin for Android
* Kevin Schaul 2011
*
*/

package com.schaul.plugins.share;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;

import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;

public class Share extends Plugin {

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
try {
JSONObject jo = args.getJSONObject(0);
doSendIntent(jo.getString("subject"), jo.getString("text"));
return new PluginResult(PluginResult.Status.OK);
} catch (JSONException e) {
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}

private void doSendIntent(String subject, String text) {
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
this.ctx.startActivity(sendIntent);
}

}

0 comments on commit 24816b6

Please sign in to comment.