Change commands to plugins.

This commit is contained in:
Bryce Curtis 2010-09-07 13:59:54 -05:00
parent 9e931cc3f6
commit 4f360c2680
16 changed files with 219 additions and 198 deletions

View File

@ -235,7 +235,7 @@ PhoneGap.callbacks = {};
PhoneGap.exec = function(clazz, action, args) {
try {
var callbackId = 0;
var r = CommandManager.exec(clazz, action, callbackId, JSON.stringify(args), false);
var r = PluginManager.exec(clazz, action, callbackId, JSON.stringify(args), false);
eval("var v="+r+";");
// If status is OK, then return value back to caller
@ -257,7 +257,7 @@ PhoneGap.execAsync = function(success, fail, clazz, action, args) {
try {
var callbackId = clazz + PhoneGap.callbackId++;
PhoneGap.callbacks[callbackId] = {success:success, fail:fail};
var r = CommandManager.exec(clazz, action, callbackId, JSON.stringify(args), true);
var r = PluginManager.exec(clazz, action, callbackId, JSON.stringify(args), true);
eval("var v="+r+";");
// If status is OK, then return value back to caller

View File

@ -6,9 +6,9 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.phonegap.api.Command;
import com.phonegap.api.CommandManager;
import com.phonegap.api.CommandResult;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginManager;
import com.phonegap.api.PluginResult;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
@ -22,7 +22,7 @@ import android.webkit.WebView;
* This class listens to the accelerometer sensor and stores the latest
* acceleration values x,y,z.
*/
public class AccelListener implements SensorEventListener, Command{
public class AccelListener implements SensorEventListener, Plugin{
public static int STOPPED = 0;
public static int STARTING = 1;
@ -81,62 +81,62 @@ public class AccelListener implements SensorEventListener, Command{
* @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
*/
public CommandResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK;
public PluginResult execute(String action, JSONArray args) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
if (action.equals("getStatus")) {
int i = this.getStatus();
return new CommandResult(status, i);
return new PluginResult(status, i);
}
else if (action.equals("start")) {
int i = this.start();
return new CommandResult(status, i);
return new PluginResult(status, i);
}
else if (action.equals("stop")) {
this.stop();
return new CommandResult(status, 0);
return new PluginResult(status, 0);
}
else if (action.equals("getAcceleration")) {
JSONObject r = new JSONObject();
r.put("x", this.x);
r.put("y", this.y);
r.put("z", this.z);
return new CommandResult(status, r);
return new PluginResult(status, r);
}
else if (action.equals("getX")) {
float f = this.getX();
return new CommandResult(status, f);
return new PluginResult(status, f);
}
else if (action.equals("getY")) {
float f = this.getY();
return new CommandResult(status, f);
return new PluginResult(status, f);
}
else if (action.equals("getZ")) {
float f = this.getZ();
return new CommandResult(status, f);
return new PluginResult(status, f);
}
else if (action.equals("setTimeout")) {
try {
float timeout = Float.parseFloat(args.getString(0));
this.setTimeout(timeout);
return new CommandResult(status, 0);
return new PluginResult(status, 0);
} catch (NumberFormatException e) {
status = CommandResult.Status.INVALID_ACTION;
status = PluginResult.Status.INVALID_ACTION;
e.printStackTrace();
} catch (JSONException e) {
status = CommandResult.Status.JSON_EXCEPTION;
status = PluginResult.Status.JSON_EXCEPTION;
e.printStackTrace();
}
}
else if (action.equals("getTimeout")) {
float f = this.getTimeout();
return new CommandResult(status, f);
return new PluginResult(status, f);
}
return new CommandResult(status, result);
return new PluginResult(status, result);
} catch (JSONException e) {
return new CommandResult(CommandResult.Status.JSON_EXCEPTION);
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}

View File

@ -6,9 +6,9 @@ import java.util.Map.Entry;
import org.json.JSONArray;
import org.json.JSONException;
import com.phonegap.api.Command;
import com.phonegap.api.CommandManager;
import com.phonegap.api.CommandResult;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginManager;
import com.phonegap.api.PluginResult;
import android.content.Context;
import android.content.Intent;
@ -26,7 +26,7 @@ import android.webkit.WebView;
* android_asset: file name must start with /android_asset/sound.mp3
* sdcard: file name is just sound.mp3
*/
public class AudioHandler implements Command {
public class AudioHandler implements Plugin {
WebView webView; // WebView object
DroidGap ctx; // DroidGap object
@ -67,8 +67,8 @@ public class AudioHandler implements Command {
* @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
*/
public CommandResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK;
public PluginResult execute(String action, JSONArray args) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
@ -89,16 +89,16 @@ public class AudioHandler implements Command {
}
else if (action.equals("getCurrentPositionAudio")) {
long l = this.getCurrentPositionAudio(args.getString(0));
return new CommandResult(status, l);
return new PluginResult(status, l);
}
else if (action.equals("getDurationAudio")) {
long l = this.getDurationAudio(args.getString(0), args.getString(1));
return new CommandResult(status, l);
return new PluginResult(status, l);
}
return new CommandResult(status, result);
return new PluginResult(status, result);
} catch (JSONException e) {
e.printStackTrace();
return new CommandResult(CommandResult.Status.JSON_EXCEPTION);
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}

View File

@ -14,7 +14,7 @@ import android.media.MediaPlayer.OnPreparedListener;
* It is called by the AudioHandler PhoneGap class.
* Only one file can be played or recorded per class instance.
*
* Local audio files must reside on sdcard
* Local audio files must reside in one of two places:
* android_asset: file name must start with /android_asset/sound.mp3
* sdcard: file name is just sound.mp3
*/
@ -169,7 +169,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On
this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_RECORD_MODE_SET+");");
}
// If this is a new request to play audio
// If this is a new request to play audio, or stopped
else if ((this.mPlayer == null) || (this.state == MEDIA_STOPPED)) {
try {
// If stopped, then reset player

View File

@ -9,9 +9,9 @@ import org.apache.commons.codec.binary.Base64;
import org.json.JSONArray;
import org.json.JSONException;
import com.phonegap.api.Command;
import com.phonegap.api.CommandManager;
import com.phonegap.api.CommandResult;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginManager;
import com.phonegap.api.PluginResult;
import android.app.Activity;
import android.content.ContentValues;
@ -28,7 +28,7 @@ import android.webkit.WebView;
* and returns the captured image. When the camera view is closed, the screen displayed before
* the camera view was shown is redisplayed.
*/
public class CameraLauncher implements Command {
public class CameraLauncher implements Plugin {
WebView webView; // WebView object
DroidGap ctx; // DroidGap object
@ -70,8 +70,8 @@ public class CameraLauncher implements Command {
* @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
*/
public CommandResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK;
public PluginResult execute(String action, JSONArray args) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
@ -81,10 +81,10 @@ public class CameraLauncher implements Command {
else if (action.equals("takePicture")) {
this.takePicture(args.getInt(0));
}
return new CommandResult(status, result);
return new PluginResult(status, result);
} catch (JSONException e) {
e.printStackTrace();
return new CommandResult(CommandResult.Status.JSON_EXCEPTION);
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
@ -144,7 +144,7 @@ public class CameraLauncher implements Command {
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
this.imageUri = Uri.fromFile(photo);
this.ctx.startActivityForResult((Command) this, intent);
this.ctx.startActivityForResult((Plugin) this, intent);
}
/**
@ -153,7 +153,7 @@ public class CameraLauncher implements Command {
* @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult().
* @param data An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

View File

@ -5,8 +5,8 @@ import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import com.phonegap.api.Command;
import com.phonegap.api.CommandResult;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
@ -19,7 +19,7 @@ import android.webkit.WebView;
/**
* This class listens to the compass sensor and stores the latest heading value.
*/
public class CompassListener implements SensorEventListener, Command{
public class CompassListener implements SensorEventListener, Plugin{
public static int STOPPED = 0;
public static int STARTING = 1;
@ -75,8 +75,8 @@ public class CompassListener implements SensorEventListener, Command{
* @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
*/
public CommandResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK;
public PluginResult execute(String action, JSONArray args) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
@ -88,23 +88,23 @@ public class CompassListener implements SensorEventListener, Command{
}
else if (action.equals("getStatus")) {
int i = this.getStatus();
return new CommandResult(status, i);
return new PluginResult(status, i);
}
else if (action.equals("getHeading")) {
float f = this.getHeading();
return new CommandResult(status, f);
return new PluginResult(status, f);
}
else if (action.equals("setTimeout")) {
this.setTimeout(args.getLong(0));
}
else if (action.equals("getTimeout")) {
long l = this.getTimeout();
return new CommandResult(status, l);
return new PluginResult(status, l);
}
return new CommandResult(status, result);
return new PluginResult(status, result);
} catch (JSONException e) {
e.printStackTrace();
return new CommandResult(CommandResult.Status.JSON_EXCEPTION);
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}

View File

@ -3,8 +3,8 @@ package com.phonegap;
import org.json.JSONArray;
import org.json.JSONException;
import com.phonegap.api.Command;
import com.phonegap.api.CommandResult;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import android.provider.Contacts.ContactMethods;
import android.provider.Contacts.People;
@ -16,7 +16,7 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteException;
@SuppressWarnings("deprecation")
public class ContactManager implements Command {
public class ContactManager implements Plugin {
public class ContactTriplet
{
@ -66,8 +66,8 @@ public class ContactManager implements Command {
* @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
*/
public CommandResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK;
public PluginResult execute(String action, JSONArray args) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
@ -77,9 +77,9 @@ public class ContactManager implements Command {
else if (action.equals("search")) {
this.search(args.getString(0), args.getString(1), args.getString(2));
}
return new CommandResult(status, result);
return new PluginResult(status, result);
} catch (JSONException e) {
return new CommandResult(CommandResult.Status.JSON_EXCEPTION);
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}

View File

@ -3,13 +3,13 @@ package com.phonegap;
import org.json.JSONArray;
import org.json.JSONException;
import com.phonegap.api.Command;
import com.phonegap.api.CommandResult;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import android.content.Intent;
import android.webkit.WebView;
public class CryptoHandler implements Command {
public class CryptoHandler implements Plugin {
WebView webView; // WebView object
DroidGap ctx; // DroidGap object
@ -47,8 +47,8 @@ public class CryptoHandler implements Command {
* @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
*/
public CommandResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK;
public PluginResult execute(String action, JSONArray args) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
@ -58,9 +58,9 @@ public class CryptoHandler implements Command {
else if (action.equals("decrypt")) {
this.decrypt(args.getString(0), args.getString(1));
}
return new CommandResult(status, result);
return new PluginResult(status, result);
} catch (JSONException e) {
return new CommandResult(CommandResult.Status.JSON_EXCEPTION);
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}

View File

@ -1,4 +1,4 @@
package com.phonegap;
package com.phonegap;
/* License (MIT)
* Copyright (c) 2008 Nitobi
* website: http://phonegap.com
@ -24,10 +24,9 @@
import com.phonegap.api.Command;
import com.phonegap.api.Plugin;
import java.util.HashMap;
import java.util.Map.Entry;
import com.phonegap.api.CommandManager;
import com.phonegap.api.PluginManager;
import android.app.Activity;
import android.app.AlertDialog;
@ -88,14 +87,14 @@ public class DroidGap extends Activity {
private FileUtils fs;
private BrowserKey mKey;
public CallbackServer callbackServer;
private CommandManager commandManager;
private PluginManager pluginManager;
private String url; // The initial URL for our app
private String baseUrl; // The base of the initial URL for our app
// Variables to manage ActivityResultCallbacks
private int activityResultCallbackCounter = 1000;
private HashMap<Integer,Command> activityResultCallbacks = new HashMap<Integer,Command>();
private HashMap<Integer,Plugin> activityResultCallbacks = new HashMap<Integer,Plugin>();
/**
* Called when the activity is first created.
@ -174,8 +173,8 @@ public class DroidGap extends Activity {
WebViewReflect.setGeolocationEnabled(settings, true);
// Bind the appView object to the gap class methods
bindBrowser(appView);
if (this.commandManager.getCommand("com.phonegap.Storage") != null) {
Storage cupcakeStorage = (Storage)this.commandManager.getCommand("com.phonegap.Storage");
if (this.pluginManager.getPlugin("com.phonegap.Storage") != null) {
Storage cupcakeStorage = (Storage)this.pluginManager.getPlugin("com.phonegap.Storage");
cupcakeStorage.setStorage(appPackage);
}
}
@ -195,12 +194,12 @@ public class DroidGap extends Activity {
/**
* Called when the system is about to start resuming a previous activity.
*/
protected void onPause(){
protected void onPause() {
super.onPause();
// Forward to commands
this.commandManager.onPause();
// Forward to plugins
this.pluginManager.onPause();
// Send pause event to JavaScript
appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};");
@ -212,11 +211,11 @@ public class DroidGap extends Activity {
/**
* Called when the activity will start interacting with the user.
*/
protected void onResume(){
protected void onResume() {
super.onResume();
// Forward to commands
this.commandManager.onResume();
// Forward to plugins
this.pluginManager.onResume();
// Send resume event to JavaScript
appView.loadUrl("javascript:try{PhoneGap.onResume.fire();}catch(e){};");
@ -242,8 +241,8 @@ public class DroidGap extends Activity {
if (mKey != null) {
}
// Forward to commands
this.commandManager.onDestroy();
// Forward to plugins
this.pluginManager.onDestroy();
if (callbackServer != null) {
callbackServer.destroy();
@ -252,13 +251,13 @@ public class DroidGap extends Activity {
private void bindBrowser(WebView appView) {
callbackServer = new CallbackServer();
commandManager = new CommandManager(appView, this);
pluginManager = new PluginManager(appView, this);
gap = new Device(appView, this);
fs = new FileUtils(appView, this);
mKey = new BrowserKey(appView, this);
// This creates the new javascript interfaces for PhoneGap
appView.addJavascriptInterface(commandManager, "CommandManager");
appView.addJavascriptInterface(pluginManager, "PluginManager");
appView.addJavascriptInterface(gap, "DroidGap");
appView.addJavascriptInterface(fs, "FileUtil");
@ -270,8 +269,8 @@ public class DroidGap extends Activity {
if (android.os.Build.VERSION.RELEASE.startsWith("1."))
{
Log.d(LOG_TAG, "bindBrowser: Adding droidStorage"); //@ibm
this.commandManager.addCommand("com.phonegap.Storage");
this.commandManager.addCommand("com.phonegap.GeoBroker");
this.pluginManager.addPlugin("com.phonegap.Storage");
this.pluginManager.addPlugin("com.phonegap.GeoBroker");
}
}
@ -625,7 +624,7 @@ public class DroidGap extends Activity {
* @param intent The intent to start
* @return The request code to use for the callback
*/
public int startActivityForResult(Command command, Intent intent) {
public int startActivityForResult(Plugin command, Intent intent) {
int requestCode = this.activityResultCallbackCounter++;
this.activityResultCallbacks.put(requestCode, command);
super.startActivityForResult(intent, requestCode);
@ -645,9 +644,9 @@ public class DroidGap extends Activity {
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Command callback = this.activityResultCallbacks.remove(requestCode);
Plugin callback = this.activityResultCallbacks.remove(requestCode);
if (callback != null) {
callback.onActivityResult(requestCode, resultCode, intent);
}
}
}
}

View File

@ -5,8 +5,8 @@ import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import com.phonegap.api.Command;
import com.phonegap.api.CommandResult;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import android.content.Intent;
import android.webkit.WebView;
@ -17,7 +17,7 @@ import android.webkit.WebView;
* This class only starts and stops various GeoListeners, which consist of a GPS and a Network Listener
*/
public class GeoBroker implements Command {
public class GeoBroker implements Plugin {
WebView webView; // WebView object
DroidGap ctx; // DroidGap object
@ -59,8 +59,8 @@ public class GeoBroker implements Command {
* @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
*/
public CommandResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK;
public PluginResult execute(String action, JSONArray args) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
@ -69,14 +69,14 @@ public class GeoBroker implements Command {
}
else if (action.equals("start")) {
String s = this.start(args.getInt(0), args.getString(1));
return new CommandResult(status, s);
return new PluginResult(status, s);
}
else if (action.equals("stop")) {
this.stop(args.getString(0));
}
return new CommandResult(status, result);
return new PluginResult(status, result);
} catch (JSONException e) {
return new CommandResult(CommandResult.Status.JSON_EXCEPTION);
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}

View File

@ -5,15 +5,15 @@ import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import com.phonegap.api.Command;
import com.phonegap.api.CommandResult;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import android.content.Context;
import android.content.Intent;
import android.net.*;
import android.webkit.WebView;
public class NetworkManager implements Command {
public class NetworkManager implements Plugin {
WebView webView; // WebView object
DroidGap ctx; // DroidGap object
@ -54,25 +54,25 @@ public class NetworkManager implements Command {
* @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
*/
public CommandResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK;
public PluginResult execute(String action, JSONArray args) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
if (action.equals("isAvailable")) {
boolean b = this.isAvailable();
return new CommandResult(status, b);
return new PluginResult(status, b);
}
else if (action.equals("isWifiActive")) {
boolean b = this.isWifiActive();
return new CommandResult(status, b);
return new PluginResult(status, b);
}
else if (action.equals("isReachable")) {
boolean b = this.isReachable(args.getString(0));
return new CommandResult(status, b);
return new PluginResult(status, b);
}
return new CommandResult(status, result);
return new PluginResult(status, result);
} catch (JSONException e) {
return new CommandResult(CommandResult.Status.JSON_EXCEPTION);
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}

View File

@ -3,8 +3,8 @@ package com.phonegap;
import org.json.JSONArray;
import org.json.JSONException;
import com.phonegap.api.Command;
import com.phonegap.api.CommandResult;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import android.content.Intent;
import android.database.Cursor;
@ -12,7 +12,7 @@ import android.database.sqlite.*;
import android.util.Log;
import android.webkit.WebView;
public class Storage implements Command {
public class Storage implements Plugin {
private static final String LOG_TAG = "SQLite Storage:";
@ -56,8 +56,8 @@ public class Storage implements Command {
* @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
*/
public CommandResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK;
public PluginResult execute(String action, JSONArray args) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
@ -76,9 +76,9 @@ public class Storage implements Command {
}
this.executeSql(args.getString(0), s, args.getString(2));
}
return new CommandResult(status, result);
return new PluginResult(status, result);
} catch (JSONException e) {
return new CommandResult(CommandResult.Status.JSON_EXCEPTION);
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}

View File

@ -4,8 +4,8 @@ import java.util.List;
import org.json.JSONArray;
import com.phonegap.api.Command;
import com.phonegap.api.CommandResult;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
@ -15,7 +15,7 @@ import android.content.Context;
import android.content.Intent;
import android.webkit.WebView;
public class TempListener implements SensorEventListener, Command {
public class TempListener implements SensorEventListener, Plugin {
WebView webView; // WebView object
DroidGap ctx; // DroidGap object
@ -57,8 +57,8 @@ public class TempListener implements SensorEventListener, Command {
* @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
*/
public CommandResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK;
public PluginResult execute(String action, JSONArray args) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
if (action.equals("start")) {
@ -67,7 +67,7 @@ public class TempListener implements SensorEventListener, Command {
else if (action.equals("stop")) {
this.stop();
}
return new CommandResult(status, result);
return new PluginResult(status, result);
}
/**

View File

@ -9,25 +9,22 @@ import android.content.Intent;
import android.webkit.WebView;
/**
* Command interface must be implemented by any plugin classes.
*
* The execute method is called by the CommandManager.
*
* @author davejohnson
* Plugin interface must be implemented by any plugin classes.
*
* The execute method is called by the PluginManager.
*/
public interface Command {
public interface Plugin {
/**
* Executes the request and returns CommandResult.
* Executes the request and returns PluginResult.
*
* @param action The command to execute.
* @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message.
* @param action The plugin to execute.
* @param args JSONArry of arguments for the plugin.
* @return A PluginResult object with a status and message.
*/
CommandResult execute(String action, JSONArray args);
PluginResult execute(String action, JSONArray args);
/**
* Sets the context of the Command. This can then be used to do things like
* Sets the context of the Plugin. This can then be used to do things like
* get file paths associated with the Activity.
*
* @param ctx The context of the main Activity.

View File

@ -13,22 +13,26 @@ import android.webkit.WebView;
import com.phonegap.DroidGap;
/**
* CommandManager is exposed to JavaScript in the PhoneGap WebView.
* PluginManager is exposed to JavaScript in the PhoneGap WebView.
*
* Calling native plugin code can be done by calling CommandManager.exec(...)
* Calling native plugin code can be done by calling PluginManager.exec(...)
* from JavaScript.
*
* @author davejohnson
*
*/
public final class CommandManager {
private HashMap<String, Command> commands = new HashMap<String,Command>();
public final class PluginManager {
private HashMap<String, Plugin> plugins = new HashMap<String,Plugin>();
private final DroidGap ctx;
private final WebView app;
public CommandManager(WebView app, DroidGap ctx) {
/**
* Constructor.
*
* @param app
* @param ctx
*/
public PluginManager(WebView app, DroidGap ctx) {
System.out.println("PluginManager()");
this.ctx = ctx;
this.app = app;
}
@ -37,7 +41,7 @@ public final class CommandManager {
* Receives a request for execution and fulfills it by finding the appropriate
* Java class and calling it's execute method.
*
* CommandManager.exec can be used either synchronously or async. In either case, a JSON encoded
* PluginManager.exec can be used either synchronously or async. In either case, a JSON encoded
* string is returned that will indicate if any errors have occurred when trying to find
* or execute the class denoted by the clazz argument.
*
@ -55,25 +59,27 @@ public final class CommandManager {
* @return JSON encoded string with a response message and status.
*/
public String exec(final String clazz, final String action, final String callbackId, final String jsonArgs, final boolean async) {
CommandResult cr = null;
System.out.println("PluginManager.exec("+clazz+", "+action+", "+callbackId+", "+jsonArgs+", "+async+")");
PluginResult cr = null;
try {
final JSONArray args = new JSONArray(jsonArgs);
Class c = getClassByName(clazz);
if (isPhoneGapCommand(c)) {
// Create a new instance of the plugin and set the context and webview
final Command plugin = this.addCommand(clazz);
if (isPhoneGapPlugin(c)) {
final Plugin plugin = this.addPlugin(clazz); //cmd;
final DroidGap ctx = this.ctx;
if (async) {
// Run this on a different thread so that this one can return back to JS
Thread thread = new Thread(new Runnable() {
public void run() {
// Call execute on the plugin so that it can do it's thing
CommandResult cr = plugin.execute(action, args);
PluginResult cr = plugin.execute(action, args);
// Check the status for 0 (success) or otherwise
if (cr.getStatus() == 0) {
ctx.sendJavascript(cr.toSuccessCallbackString(callbackId));
//app.loadUrl(cr.toSuccessCallbackString(callbackId));
} else {
ctx.sendJavascript(cr.toErrorCallbackString(callbackId));
//app.loadUrl(cr.toErrorCallbackString(callbackId));
}
}
});
@ -85,14 +91,18 @@ public final class CommandManager {
}
}
} catch (ClassNotFoundException e) {
cr = new CommandResult(CommandResult.Status.CLASS_NOT_FOUND_EXCEPTION);
cr = new PluginResult(PluginResult.Status.CLASS_NOT_FOUND_EXCEPTION);
} catch (JSONException e) {
System.out.println("ERROR: "+e.toString());
cr = new CommandResult(CommandResult.Status.JSON_EXCEPTION);
cr = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
// if async we have already returned at this point unless there was an error...
if (async) {
ctx.sendJavascript(cr.toErrorCallbackString(callbackId));
//app.loadUrl(cr.toErrorCallbackString(callbackId));
}
if (cr != null) {
System.out.println(" -- returning result: "+cr.getJSONString());
}
return ( cr != null ? cr.getJSONString() : "{ status: 0, message: 'all good' }" );
}
@ -110,69 +120,70 @@ public final class CommandManager {
/**
* Get the interfaces that a class implements and see if it implements the
* com.phonegap.api.Command interface.
* com.phonegap.api.Plugin interface.
*
* @param c The class to check the interfaces of.
* @return Boolean indicating if the class implements com.phonegap.api.Command
* @return Boolean indicating if the class implements com.phonegap.api.Plugin
*/
private boolean isPhoneGapCommand(Class c) {
boolean isCommand = false;
private boolean isPhoneGapPlugin(Class c) {
boolean isPlugin = false;
Class[] interfaces = c.getInterfaces();
for (int j=0; j<interfaces.length; j++) {
if (interfaces[j].getName().equals("com.phonegap.api.Command")) {
isCommand = true;
if (interfaces[j].getName().equals("com.phonegap.api.Plugin")) {
isPlugin = true;
break;
}
}
return isCommand;
return isPlugin;
}
/**
* Add command to be loaded and cached.
* If command is already created, then just return it.
* Add plugin to be loaded and cached.
* If plugin is already created, then just return it.
*
* @param className The class to load
* @return The command
* @return The plugin
*/
public Command addCommand(String className) {
if (this.commands.containsKey(className)) {
return this.getCommand(className);
public Plugin addPlugin(String className) {
System.out.println("PluginManager.addPlugin("+className+")");
if (this.plugins.containsKey(className)) {
return this.getPlugin(className);
}
try {
Command command = (Command)Class.forName(className).newInstance();
this.commands.put(className, command);
command.setContext((DroidGap)this.ctx);
command.setView(this.app);
return command;
Plugin plugin = (Plugin)Class.forName(className).newInstance();
this.plugins.put(className, plugin);
plugin.setContext((DroidGap)this.ctx);
plugin.setView(this.app);
return plugin;
}
catch (Exception e) {
e.printStackTrace();
System.out.println("Error adding command "+className+".");
System.out.println("Error adding plugin "+className+".");
}
return null;
}
/**
* Get the loaded command.
* Get the loaded plugin.
*
* @param className The class of the loaded command.
* @param className The class of the loaded plugin.
* @return
*/
public Command getCommand(String className) {
Command command = this.commands.get(className);
return command;
public Plugin getPlugin(String className) {
Plugin plugin = this.plugins.get(className);
return plugin;
}
/**
* Called when the system is about to start resuming a previous activity.
*/
public void onPause() {
java.util.Set<Entry<String,Command>> s = this.commands.entrySet();
java.util.Iterator<Entry<String,Command>> it = s.iterator();
java.util.Set<Entry<String,Plugin>> s = this.plugins.entrySet();
java.util.Iterator<Entry<String,Plugin>> it = s.iterator();
while(it.hasNext()) {
Entry<String,Command> entry = it.next();
Command command = entry.getValue();
command.onPause();
Entry<String,Plugin> entry = it.next();
Plugin plugin = entry.getValue();
plugin.onPause();
}
}
@ -180,12 +191,12 @@ public final class CommandManager {
* Called when the activity will start interacting with the user.
*/
public void onResume() {
java.util.Set<Entry<String,Command>> s = this.commands.entrySet();
java.util.Iterator<Entry<String,Command>> it = s.iterator();
java.util.Set<Entry<String,Plugin>> s = this.plugins.entrySet();
java.util.Iterator<Entry<String,Plugin>> it = s.iterator();
while(it.hasNext()) {
Entry<String,Command> entry = it.next();
Command command = entry.getValue();
command.onResume();
Entry<String,Plugin> entry = it.next();
Plugin plugin = entry.getValue();
plugin.onResume();
}
}
@ -193,13 +204,26 @@ public final class CommandManager {
* The final call you receive before your activity is destroyed.
*/
public void onDestroy() {
java.util.Set<Entry<String,Command>> s = this.commands.entrySet();
java.util.Iterator<Entry<String,Command>> it = s.iterator();
java.util.Set<Entry<String,Plugin>> s = this.plugins.entrySet();
java.util.Iterator<Entry<String,Plugin>> it = s.iterator();
while(it.hasNext()) {
Entry<String,Command> entry = it.next();
Command command = entry.getValue();
command.onDestroy();
Entry<String,Plugin> entry = it.next();
Plugin plugin = entry.getValue();
plugin.onDestroy();
}
}
/**
* Send JavaScript statement back to JavaScript.
*
* @param message
*/
/*
public void sendJavascript(String statement) {
//System.out.println("Module.sendResponse("+statement+")");
this.ctx.callbackServer.sendJavascript(statement);
}
*/
}

View File

@ -2,34 +2,35 @@ package com.phonegap.api;
import org.json.JSONObject;
public class CommandResult {
public class PluginResult {
private final int status;
private final String message;
public CommandResult(Status status) {
public PluginResult(Status status) {
this.status = status.ordinal();
this.message = CommandResult.StatusMessages[this.status];
this.message = PluginResult.StatusMessages[this.status];
}
public CommandResult(Status status, String message) {
public PluginResult(Status status, String message) {
this.status = status.ordinal();
this.message = "'" + message + "'";
}
public CommandResult(Status status, JSONObject message) {
public PluginResult(Status status, JSONObject message) {
this.status = status.ordinal();
this.message = message.toString();
}
public CommandResult(Status status, int i) {
// TODO: BC: Added
public PluginResult(Status status, int i) {
this.status = status.ordinal();
this.message = ""+i;
}
public CommandResult(Status status, float f) {
public PluginResult(Status status, float f) {
this.status = status.ordinal();
this.message = ""+f;
}
public CommandResult(Status status, boolean b) {
public PluginResult(Status status, boolean b) {
this.status = status.ordinal();
this.message = ""+b;
}