Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ Launches an Android intent. For example:
function() {alert('Failed to open URL via Android Intent')};
);

Launches an Anroid activity by package name and class

window.plugins.webintent.startActivity(
{
action: window.plugins.webintent.ACTION_MAIN,
handler: { packageName: 'com.nianticproject.ingress', className: 'com.nianticproject.ingress.NemesisActivity' }
},
function() {},
function() {alert('Failed to open URL via Android Intent') }
);

### hasExtra ###
checks if this app was invoked with the specified extra. For example:
Expand Down
12 changes: 6 additions & 6 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:rim="http://www.blackberry.com/ns/widgets"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.borismus.webintent"
version="0.1.0">
id="net.tunts.webintent"
version="0.2.1">
<name>WebIntent</name>
<description>Web Intent Plugin</description>
<license>Apache 2.0</license>
<keywords>cordova,webintent</keywords>

<js-module src="www/webintent.js" name="webIntnet">
<clobbers target="webIntent" />
<js-module src="www/webintent.js" name="WebIntent">
<clobbers target="WebIntent" />
</js-module>

<!-- android -->
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="WebIntent" >
<param name="android-package" value="com.borismus.webintent"/>
<param name="android-package" value="net.tunts.webintent.WebIntent"/>
</feature>
</config-file>

<source-file src="src/android/WebIntent.java" target-dir="src/com/borismus" />
<source-file src="src/android/WebIntent.java" target-dir="src/net/tunts/webintent" />
</platform>

</plugin>
158 changes: 102 additions & 56 deletions src/android/WebIntent.java
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
package com.borismus.webintent;
package net.tunts.webintent;

import java.util.HashMap;
import java.util.Map;

import org.apache.cordova.DroidGap;
import org.apache.cordova.CordovaActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.text.Html;

import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CallbackContext;

/**
* WebIntent is a PhoneGap plugin that bridges Android intents and web
* applications:
* 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.
* 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 {
public class WebIntent extends CordovaPlugin {

private String onNewIntentCallback = null;
private CallbackContext onNewIntentCallback = null;

/**
* 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.
* @param action The action to execute.
* @param args JSONArray of arguments for the plugin.
* @param callbackContext The callbackContext used when calling back into JavaScript.
* @return boolean
*/
public PluginResult execute(String action, JSONArray args, String callbackId) {
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
try {
if (action.equals("startActivity")) {
if (args.length() != 1) {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}

// 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;
JSONObject handler = obj.has("handler") ? obj.getJSONObject("handler") : null;
Map<String, String> extrasMap = new HashMap<String, String>();
Map<String, String> handlerMap = null;

// Populate the extras if any exist
if (extras != null) {
Expand All @@ -66,49 +66,86 @@ public PluginResult execute(String action, JSONArray args, String callbackId) {
}
}

startActivity(obj.getString("action"), uri, type, extrasMap);
return new PluginResult(PluginResult.Status.OK);
if (handler != null) {
handlerMap = new HashMap<String, String>();

handlerMap.put("packageName", handler.getString("packageName"));
handlerMap.put("className", handler.getString("className"));
}

try{
startActivity(obj.getString("action"), uri, type, extrasMap, handlerMap);
} catch (ActivityNotFoundException e){
callbackContext.error(e.getMessage());
return false;
}

callbackContext.success();
return true;

} else if (action.equals("hasExtra")) {
if (args.length() != 1) {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
Intent i = ((CordovaActivity) this.cordova.getActivity()).getIntent();
String extraName = args.getString(0);
return new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
PluginResult res = new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
callbackContext.sendPluginResult(res);
return true;

} else if (action.equals("getExtra")) {
if (args.length() != 1) {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
Intent i = ((CordovaActivity) this.cordova.getActivity()).getIntent();
String extraName = args.getString(0);

if (i.hasExtra(extraName)) {
return new PluginResult(PluginResult.Status.OK, i.getStringExtra(extraName));
PluginResult res = new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
callbackContext.sendPluginResult(res);
return true;

} else {
return new PluginResult(PluginResult.Status.ERROR);
PluginResult res = new PluginResult(PluginResult.Status.ERROR);
callbackContext.sendPluginResult(res);
return false;
}

} else if (action.equals("getUri")) {
if (args.length() != 0) {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}

Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
Intent i = ((CordovaActivity) this.cordova.getActivity()).getIntent();
String uri = i.getDataString();
return new PluginResult(PluginResult.Status.OK, uri);

callbackContext.success(uri);
return true;

} else if (action.equals("onNewIntent")) {
if (args.length() != 0) {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}

this.onNewIntentCallback = callbackId;
PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
return result;
} else if (action.equals("sendBroadcast"))
{
this.onNewIntentCallback = callbackContext;
PluginResult res = new PluginResult(PluginResult.Status.NO_RESULT);
res.setKeepCallback(true);
callbackContext.sendPluginResult(res);
return true;

} else if (action.equals("sendBroadcast")) {
if (args.length() != 1) {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}

// Parse the arguments
Expand All @@ -128,35 +165,44 @@ public PluginResult execute(String action, JSONArray args, String callbackId) {
}

sendBroadcast(obj.getString("action"), extrasMap);
return new PluginResult(PluginResult.Status.OK);
callbackContext.success();
return true;

}
return new PluginResult(PluginResult.Status.INVALID_ACTION);
} catch (JSONException e) {
e.printStackTrace();
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);

PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;

}
catch (JSONException e) {
callbackContext.error(e.getMessage());
return false;
}
}

@Override
public void onNewIntent(Intent intent) {
if (this.onNewIntentCallback != null) {
PluginResult result = new PluginResult(PluginResult.Status.OK, intent.getDataString());
result.setKeepCallback(true);
this.success(result, this.onNewIntentCallback);
this.onNewIntentCallback.success(intent.getDataString());
}
}

void startActivity(String action, Uri uri, String type, Map<String, String> extras) {
void startActivity(String action, Uri uri, String type, Map<String, String> extras, Map<String, String> handlerMap) {
Intent i = (uri != null ? new Intent(action, uri) : new Intent(action));


if (handlerMap != null) {
i.setClassName(handlerMap.get("packageName"), handlerMap.get("className"));
}

if (type != null && uri != null) {
i.setDataAndType(uri, type); //Fix the crash problem with android 2.3.6
i.setDataAndType(uri, type); // Fix the crash problem with android 2.3.6
} else {
if (type != null) {
i.setType(type);
}
}

for (String key : extras.keySet()) {
String value = extras.get(key);
// If type is text html, the extra text must sent as HTML
Expand All @@ -173,7 +219,7 @@ void startActivity(String action, Uri uri, String type, Map<String, String> extr
i.putExtra(key, value);
}
}
((DroidGap)this.cordova.getActivity()).startActivity(i);
((CordovaActivity) this.cordova.getActivity()).startActivity(i);
}

void sendBroadcast(String action, Map<String, String> extras) {
Expand All @@ -184,6 +230,6 @@ void sendBroadcast(String action, Map<String, String> extras) {
intent.putExtra(key, value);
}

((DroidGap)this.cordova.getActivity()).sendBroadcast(intent);
((CordovaActivity) this.cordova.getActivity()).sendBroadcast(intent);
}
}
}
12 changes: 6 additions & 6 deletions www/webintent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

WebIntent.prototype.ACTION_SEND = "android.intent.action.SEND";
WebIntent.prototype.ACTION_VIEW= "android.intent.action.VIEW";
WebIntent.prototype.ACTION_MAIN = "android.intent.action.MAIN";
WebIntent.prototype.EXTRA_TEXT = "android.intent.extra.TEXT";
WebIntent.prototype.EXTRA_SUBJECT = "android.intent.extra.SUBJECT";
WebIntent.prototype.EXTRA_STREAM = "android.intent.extra.STREAM";
Expand Down Expand Up @@ -63,11 +64,10 @@
}, 'WebIntent', 'sendBroadcast', [params]);
};

cordova.addConstructor(function() {
window.webintent = new WebIntent();
window.webintent = new WebIntent();

// backwards compatibility
window.plugins = window.plugins || {};
window.plugins.webintent = window.webintent;

// backwards compatibility
window.plugins = window.plugins || {};
window.plugins.webintent = window.webintent;
});
})(window.PhoneGap || window.Cordova || window.cordova);