mirror of
https://github.com/purplecabbage/phonegap-plugins.git
synced 2026-04-24 03:00:11 -04:00
added WebIntent plugin
This commit is contained in:
71
Android/WebIntent/README.md
Normal file
71
Android/WebIntent/README.md
Normal file
@@ -0,0 +1,71 @@
|
||||
# WebIntents plugin for Phonegap #
|
||||
By Boris Smus
|
||||
|
||||
## Adding the Plugin to your project ##
|
||||
1. To install the plugin, move webintent.js to your project's www folder and include a reference to it in your html files.
|
||||
2. Create a folder called "borismus" within your project's src/com/ folder and move the java file into it.
|
||||
|
||||
## Using the plugin ##
|
||||
The plugin creates the object `window.plugins.webintent` with three methods:
|
||||
|
||||
### startActivity ###
|
||||
Launches an Android intent. For example:
|
||||
|
||||
|
||||
window.plugins.webintent.startActivity({
|
||||
action: WebIntent.ACTION_VIEW,
|
||||
url: 'geo:0,0?q=' + address},
|
||||
function() {},
|
||||
function() {alert('Failed to open URL via Android Intent')};
|
||||
);
|
||||
|
||||
|
||||
### hasExtra ###
|
||||
checks if this app was invoked with the specified extra. For example:
|
||||
|
||||
window.plugins.webintent.hasExtra(WebIntent.EXTRA_TEXT,
|
||||
function(has) {
|
||||
// has is true iff it has the extra
|
||||
}, function() {
|
||||
// Something really bad happened.
|
||||
}
|
||||
);
|
||||
|
||||
### getExtra ###
|
||||
Gets the extra that this app was invoked with. For example:
|
||||
|
||||
window.plugins.webintent.getExtra(WebIntent.EXTRA_TEXT,
|
||||
function(url) {
|
||||
// url is the value of EXTRA_TEXT
|
||||
}, function() {
|
||||
// There was no extra supplied.
|
||||
}
|
||||
);
|
||||
|
||||
## BUGS AND CONTRIBUTIONS ##
|
||||
The latest bleeding-edge version is available [on GitHub](https://github.com/borismus/phonegap-plugins/tree/master/Android/)
|
||||
If you have a patch, fork my repo baby and send me a pull request. Submit bug reports on GitHub, please.
|
||||
|
||||
## Licence ##
|
||||
|
||||
The MIT License
|
||||
|
||||
Copyright (c) 2010 Boris Smus
|
||||
|
||||
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.
|
||||
101
Android/WebIntent/WebIntent.java
Normal file
101
Android/WebIntent/WebIntent.java
Normal file
@@ -0,0 +1,101 @@
|
||||
package com.borismus.webintent;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
|
||||
import com.phonegap.api.Plugin;
|
||||
import com.phonegap.api.PluginResult;
|
||||
|
||||
/**
|
||||
* WebIntent is a PhoneGap plugin that bridges Android intents and web applications:
|
||||
*
|
||||
* 1. web apps can spawn intents that call native Android applications.
|
||||
* 2. (after setting up correct intent filters for PhoneGap applications), Android
|
||||
* intents can be handled by PhoneGap web applications.
|
||||
*
|
||||
* @author boris@borismus.com
|
||||
*
|
||||
*/
|
||||
public class WebIntent extends Plugin {
|
||||
|
||||
/**
|
||||
* Executes the request and returns PluginResult.
|
||||
*
|
||||
* @param action The action to execute.
|
||||
* @param args JSONArray of arguments for the plugin.
|
||||
* @param callbackId The callback id used when calling back into JavaScript.
|
||||
* @return A PluginResult object with a status and message.
|
||||
*/
|
||||
public PluginResult execute(String action, JSONArray args, String callbackId) {
|
||||
try {
|
||||
if (action.equals("startActivity")) {
|
||||
if(args.length() != 1) {
|
||||
return new PluginResult(PluginResult.Status.INVALID_ACTION);
|
||||
}
|
||||
|
||||
// Parse the arguments
|
||||
JSONObject obj = args.getJSONObject(0);
|
||||
String type = obj.has("type") ? obj.getString("type") : null;
|
||||
Uri uri = obj.has("url") ? Uri.parse(obj.getString("url")) : null;
|
||||
JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
|
||||
Map<String, String> extrasMap = new HashMap<String, String>();
|
||||
|
||||
// Populate the extras if any exist
|
||||
if (extras != null) {
|
||||
JSONArray extraNames = extras.names();
|
||||
for (int i = 0; i < extraNames.length(); i++) {
|
||||
String key = extraNames.getString(i);
|
||||
String value = extras.getString(key);
|
||||
extrasMap.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
startActivity(obj.getString("action"), uri, type, extrasMap);
|
||||
return new PluginResult(PluginResult.Status.OK);
|
||||
|
||||
} else if (action.equals("hasExtra")) {
|
||||
if (args.length() != 1) {
|
||||
return new PluginResult(PluginResult.Status.INVALID_ACTION);
|
||||
}
|
||||
Intent i = this.ctx.getIntent();
|
||||
String extraName = args.getString(0);
|
||||
return new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
|
||||
|
||||
} else if (action.equals("getExtra")) {
|
||||
if (args.length() != 1) {
|
||||
return new PluginResult(PluginResult.Status.INVALID_ACTION);
|
||||
}
|
||||
Intent i = this.ctx.getIntent();
|
||||
String extraName = args.getString(0);
|
||||
if (i.hasExtra(extraName)) {
|
||||
return new PluginResult(PluginResult.Status.OK, i.getStringExtra(extraName));
|
||||
} else {
|
||||
return new PluginResult(PluginResult.Status.ERROR);
|
||||
}
|
||||
}
|
||||
return new PluginResult(PluginResult.Status.INVALID_ACTION);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
||||
}
|
||||
}
|
||||
|
||||
void startActivity(String action, Uri uri, String type, Map<String, String> extras) {
|
||||
Intent i = (uri != null ? new Intent(action, uri) : new Intent(action));
|
||||
if (type != null) {
|
||||
i.setType(type);
|
||||
}
|
||||
for (String key : extras.keySet()) {
|
||||
String value = extras.get(key);
|
||||
i.putExtra(key, value);
|
||||
}
|
||||
this.ctx.startActivity(i);
|
||||
}
|
||||
}
|
||||
42
Android/WebIntent/webintent.js
Normal file
42
Android/WebIntent/webintent.js
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Phonegap Web Intent plugin
|
||||
* Copyright (c) Boris Smus 2010
|
||||
*
|
||||
*/
|
||||
var WebIntent = function() {
|
||||
|
||||
};
|
||||
|
||||
WebIntent.ACTION_SEND = "android.intent.action.SEND";
|
||||
WebIntent.ACTION_VIEW= "android.intent.action.VIEW";
|
||||
WebIntent.EXTRA_TEXT = "android.intent.extra.TEXT";
|
||||
WebIntent.EXTRA_SUBJECT = "android.intent.extra.SUBJECT";
|
||||
|
||||
WebIntent.prototype.startActivity = function(params, success, fail) {
|
||||
return PhoneGap.exec(function(args) {
|
||||
success(args);
|
||||
}, function(args) {
|
||||
fail(args);
|
||||
}, 'WebIntent', 'startActivity', [params]);
|
||||
};
|
||||
|
||||
WebIntent.prototype.hasExtra = function(params, success, fail) {
|
||||
return PhoneGap.exec(function(args) {
|
||||
success(args);
|
||||
}, function(args) {
|
||||
fail(args);
|
||||
}, 'WebIntent', 'hasExtra', [params]);
|
||||
};
|
||||
|
||||
WebIntent.prototype.getExtra = function(params, success, fail) {
|
||||
return PhoneGap.exec(function(args) {
|
||||
success(args);
|
||||
}, function(args) {
|
||||
fail(args);
|
||||
}, 'WebIntent', 'getExtra', [params]);
|
||||
};
|
||||
|
||||
PhoneGap.addConstructor(function() {
|
||||
PhoneGap.addPlugin('webintent', new WebIntent());
|
||||
PluginManager.addService("WebIntent","com.borismus.webintent.WebIntent");
|
||||
});
|
||||
Reference in New Issue
Block a user