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) { PhoneGap.exec = function(clazz, action, args) {
try { try {
var callbackId = 0; 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+";"); eval("var v="+r+";");
// If status is OK, then return value back to caller // If status is OK, then return value back to caller
@ -257,7 +257,7 @@ PhoneGap.execAsync = function(success, fail, clazz, action, args) {
try { try {
var callbackId = clazz + PhoneGap.callbackId++; var callbackId = clazz + PhoneGap.callbackId++;
PhoneGap.callbacks[callbackId] = {success:success, fail:fail}; 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+";"); eval("var v="+r+";");
// If status is OK, then return value back to caller // 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.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import com.phonegap.api.Command; import com.phonegap.api.Plugin;
import com.phonegap.api.CommandManager; import com.phonegap.api.PluginManager;
import com.phonegap.api.CommandResult; import com.phonegap.api.PluginResult;
import android.hardware.Sensor; import android.hardware.Sensor;
import android.hardware.SensorEvent; import android.hardware.SensorEvent;
@ -22,7 +22,7 @@ import android.webkit.WebView;
* This class listens to the accelerometer sensor and stores the latest * This class listens to the accelerometer sensor and stores the latest
* acceleration values x,y,z. * 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 STOPPED = 0;
public static int STARTING = 1; public static int STARTING = 1;
@ -81,62 +81,62 @@ public class AccelListener implements SensorEventListener, Command{
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message. * @return A CommandResult object with a status and message.
*/ */
public CommandResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";
try { try {
if (action.equals("getStatus")) { if (action.equals("getStatus")) {
int i = this.getStatus(); int i = this.getStatus();
return new CommandResult(status, i); return new PluginResult(status, i);
} }
else if (action.equals("start")) { else if (action.equals("start")) {
int i = this.start(); int i = this.start();
return new CommandResult(status, i); return new PluginResult(status, i);
} }
else if (action.equals("stop")) { else if (action.equals("stop")) {
this.stop(); this.stop();
return new CommandResult(status, 0); return new PluginResult(status, 0);
} }
else if (action.equals("getAcceleration")) { else if (action.equals("getAcceleration")) {
JSONObject r = new JSONObject(); JSONObject r = new JSONObject();
r.put("x", this.x); r.put("x", this.x);
r.put("y", this.y); r.put("y", this.y);
r.put("z", this.z); r.put("z", this.z);
return new CommandResult(status, r); return new PluginResult(status, r);
} }
else if (action.equals("getX")) { else if (action.equals("getX")) {
float f = this.getX(); float f = this.getX();
return new CommandResult(status, f); return new PluginResult(status, f);
} }
else if (action.equals("getY")) { else if (action.equals("getY")) {
float f = this.getY(); float f = this.getY();
return new CommandResult(status, f); return new PluginResult(status, f);
} }
else if (action.equals("getZ")) { else if (action.equals("getZ")) {
float f = this.getZ(); float f = this.getZ();
return new CommandResult(status, f); return new PluginResult(status, f);
} }
else if (action.equals("setTimeout")) { else if (action.equals("setTimeout")) {
try { try {
float timeout = Float.parseFloat(args.getString(0)); float timeout = Float.parseFloat(args.getString(0));
this.setTimeout(timeout); this.setTimeout(timeout);
return new CommandResult(status, 0); return new PluginResult(status, 0);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
status = CommandResult.Status.INVALID_ACTION; status = PluginResult.Status.INVALID_ACTION;
e.printStackTrace(); e.printStackTrace();
} catch (JSONException e) { } catch (JSONException e) {
status = CommandResult.Status.JSON_EXCEPTION; status = PluginResult.Status.JSON_EXCEPTION;
e.printStackTrace(); e.printStackTrace();
} }
} }
else if (action.equals("getTimeout")) { else if (action.equals("getTimeout")) {
float f = this.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) { } 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.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import com.phonegap.api.Command; import com.phonegap.api.Plugin;
import com.phonegap.api.CommandManager; import com.phonegap.api.PluginManager;
import com.phonegap.api.CommandResult; import com.phonegap.api.PluginResult;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -26,7 +26,7 @@ import android.webkit.WebView;
* android_asset: file name must start with /android_asset/sound.mp3 * android_asset: file name must start with /android_asset/sound.mp3
* sdcard: file name is just sound.mp3 * sdcard: file name is just sound.mp3
*/ */
public class AudioHandler implements Command { public class AudioHandler implements Plugin {
WebView webView; // WebView object WebView webView; // WebView object
DroidGap ctx; // DroidGap object DroidGap ctx; // DroidGap object
@ -67,8 +67,8 @@ public class AudioHandler implements Command {
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message. * @return A CommandResult object with a status and message.
*/ */
public CommandResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";
try { try {
@ -89,16 +89,16 @@ public class AudioHandler implements Command {
} }
else if (action.equals("getCurrentPositionAudio")) { else if (action.equals("getCurrentPositionAudio")) {
long l = this.getCurrentPositionAudio(args.getString(0)); long l = this.getCurrentPositionAudio(args.getString(0));
return new CommandResult(status, l); return new PluginResult(status, l);
} }
else if (action.equals("getDurationAudio")) { else if (action.equals("getDurationAudio")) {
long l = this.getDurationAudio(args.getString(0), args.getString(1)); 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) { } catch (JSONException e) {
e.printStackTrace(); 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. * It is called by the AudioHandler PhoneGap class.
* Only one file can be played or recorded per class instance. * 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 * android_asset: file name must start with /android_asset/sound.mp3
* sdcard: file name is just 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+");"); 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)) { else if ((this.mPlayer == null) || (this.state == MEDIA_STOPPED)) {
try { try {
// If stopped, then reset player // 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.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import com.phonegap.api.Command; import com.phonegap.api.Plugin;
import com.phonegap.api.CommandManager; import com.phonegap.api.PluginManager;
import com.phonegap.api.CommandResult; import com.phonegap.api.PluginResult;
import android.app.Activity; import android.app.Activity;
import android.content.ContentValues; 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 * and returns the captured image. When the camera view is closed, the screen displayed before
* the camera view was shown is redisplayed. * the camera view was shown is redisplayed.
*/ */
public class CameraLauncher implements Command { public class CameraLauncher implements Plugin {
WebView webView; // WebView object WebView webView; // WebView object
DroidGap ctx; // DroidGap object DroidGap ctx; // DroidGap object
@ -70,8 +70,8 @@ public class CameraLauncher implements Command {
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message. * @return A CommandResult object with a status and message.
*/ */
public CommandResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";
try { try {
@ -81,10 +81,10 @@ public class CameraLauncher implements Command {
else if (action.equals("takePicture")) { else if (action.equals("takePicture")) {
this.takePicture(args.getInt(0)); this.takePicture(args.getInt(0));
} }
return new CommandResult(status, result); return new PluginResult(status, result);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); 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)); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
this.imageUri = 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(), * @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from. * allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult(). * @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) { 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.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import com.phonegap.api.Command; import com.phonegap.api.Plugin;
import com.phonegap.api.CommandResult; import com.phonegap.api.PluginResult;
import android.hardware.Sensor; import android.hardware.Sensor;
import android.hardware.SensorEvent; 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. * 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 STOPPED = 0;
public static int STARTING = 1; public static int STARTING = 1;
@ -75,8 +75,8 @@ public class CompassListener implements SensorEventListener, Command{
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message. * @return A CommandResult object with a status and message.
*/ */
public CommandResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";
try { try {
@ -88,23 +88,23 @@ public class CompassListener implements SensorEventListener, Command{
} }
else if (action.equals("getStatus")) { else if (action.equals("getStatus")) {
int i = this.getStatus(); int i = this.getStatus();
return new CommandResult(status, i); return new PluginResult(status, i);
} }
else if (action.equals("getHeading")) { else if (action.equals("getHeading")) {
float f = this.getHeading(); float f = this.getHeading();
return new CommandResult(status, f); return new PluginResult(status, f);
} }
else if (action.equals("setTimeout")) { else if (action.equals("setTimeout")) {
this.setTimeout(args.getLong(0)); this.setTimeout(args.getLong(0));
} }
else if (action.equals("getTimeout")) { else if (action.equals("getTimeout")) {
long l = this.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) { } catch (JSONException e) {
e.printStackTrace(); 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.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import com.phonegap.api.Command; import com.phonegap.api.Plugin;
import com.phonegap.api.CommandResult; import com.phonegap.api.PluginResult;
import android.provider.Contacts.ContactMethods; import android.provider.Contacts.ContactMethods;
import android.provider.Contacts.People; import android.provider.Contacts.People;
@ -16,7 +16,7 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteException;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class ContactManager implements Command { public class ContactManager implements Plugin {
public class ContactTriplet public class ContactTriplet
{ {
@ -66,8 +66,8 @@ public class ContactManager implements Command {
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message. * @return A CommandResult object with a status and message.
*/ */
public CommandResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";
try { try {
@ -77,9 +77,9 @@ public class ContactManager implements Command {
else if (action.equals("search")) { else if (action.equals("search")) {
this.search(args.getString(0), args.getString(1), args.getString(2)); this.search(args.getString(0), args.getString(1), args.getString(2));
} }
return new CommandResult(status, result); return new PluginResult(status, result);
} catch (JSONException e) { } 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.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import com.phonegap.api.Command; import com.phonegap.api.Plugin;
import com.phonegap.api.CommandResult; import com.phonegap.api.PluginResult;
import android.content.Intent; import android.content.Intent;
import android.webkit.WebView; import android.webkit.WebView;
public class CryptoHandler implements Command { public class CryptoHandler implements Plugin {
WebView webView; // WebView object WebView webView; // WebView object
DroidGap ctx; // DroidGap object DroidGap ctx; // DroidGap object
@ -47,8 +47,8 @@ public class CryptoHandler implements Command {
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message. * @return A CommandResult object with a status and message.
*/ */
public CommandResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";
try { try {
@ -58,9 +58,9 @@ public class CryptoHandler implements Command {
else if (action.equals("decrypt")) { else if (action.equals("decrypt")) {
this.decrypt(args.getString(0), args.getString(1)); this.decrypt(args.getString(0), args.getString(1));
} }
return new CommandResult(status, result); return new PluginResult(status, result);
} catch (JSONException e) { } 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) /* License (MIT)
* Copyright (c) 2008 Nitobi * Copyright (c) 2008 Nitobi
* website: http://phonegap.com * 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.HashMap;
import java.util.Map.Entry; import com.phonegap.api.PluginManager;
import com.phonegap.api.CommandManager;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
@ -88,14 +87,14 @@ public class DroidGap extends Activity {
private FileUtils fs; private FileUtils fs;
private BrowserKey mKey; private BrowserKey mKey;
public CallbackServer callbackServer; public CallbackServer callbackServer;
private CommandManager commandManager; private PluginManager pluginManager;
private String url; // The initial URL for our app private String url; // The initial URL for our app
private String baseUrl; // The base of the initial URL for our app private String baseUrl; // The base of the initial URL for our app
// Variables to manage ActivityResultCallbacks // Variables to manage ActivityResultCallbacks
private int activityResultCallbackCounter = 1000; 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. * Called when the activity is first created.
@ -174,8 +173,8 @@ public class DroidGap extends Activity {
WebViewReflect.setGeolocationEnabled(settings, true); WebViewReflect.setGeolocationEnabled(settings, true);
// Bind the appView object to the gap class methods // Bind the appView object to the gap class methods
bindBrowser(appView); bindBrowser(appView);
if (this.commandManager.getCommand("com.phonegap.Storage") != null) { if (this.pluginManager.getPlugin("com.phonegap.Storage") != null) {
Storage cupcakeStorage = (Storage)this.commandManager.getCommand("com.phonegap.Storage"); Storage cupcakeStorage = (Storage)this.pluginManager.getPlugin("com.phonegap.Storage");
cupcakeStorage.setStorage(appPackage); cupcakeStorage.setStorage(appPackage);
} }
} }
@ -195,11 +194,11 @@ public class DroidGap extends Activity {
/** /**
* Called when the system is about to start resuming a previous activity. * Called when the system is about to start resuming a previous activity.
*/ */
protected void onPause(){ protected void onPause() {
super.onPause(); super.onPause();
// Forward to commands // Forward to plugins
this.commandManager.onPause(); this.pluginManager.onPause();
// Send pause event to JavaScript // Send pause event to JavaScript
appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};"); 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. * Called when the activity will start interacting with the user.
*/ */
protected void onResume(){ protected void onResume() {
super.onResume(); super.onResume();
// Forward to commands // Forward to plugins
this.commandManager.onResume(); this.pluginManager.onResume();
// Send resume event to JavaScript // Send resume event to JavaScript
appView.loadUrl("javascript:try{PhoneGap.onResume.fire();}catch(e){};"); appView.loadUrl("javascript:try{PhoneGap.onResume.fire();}catch(e){};");
@ -242,8 +241,8 @@ public class DroidGap extends Activity {
if (mKey != null) { if (mKey != null) {
} }
// Forward to commands // Forward to plugins
this.commandManager.onDestroy(); this.pluginManager.onDestroy();
if (callbackServer != null) { if (callbackServer != null) {
callbackServer.destroy(); callbackServer.destroy();
@ -252,13 +251,13 @@ public class DroidGap extends Activity {
private void bindBrowser(WebView appView) { private void bindBrowser(WebView appView) {
callbackServer = new CallbackServer(); callbackServer = new CallbackServer();
commandManager = new CommandManager(appView, this); pluginManager = new PluginManager(appView, this);
gap = new Device(appView, this); gap = new Device(appView, this);
fs = new FileUtils(appView, this); fs = new FileUtils(appView, this);
mKey = new BrowserKey(appView, this); mKey = new BrowserKey(appView, this);
// This creates the new javascript interfaces for PhoneGap // This creates the new javascript interfaces for PhoneGap
appView.addJavascriptInterface(commandManager, "CommandManager"); appView.addJavascriptInterface(pluginManager, "PluginManager");
appView.addJavascriptInterface(gap, "DroidGap"); appView.addJavascriptInterface(gap, "DroidGap");
appView.addJavascriptInterface(fs, "FileUtil"); appView.addJavascriptInterface(fs, "FileUtil");
@ -270,8 +269,8 @@ public class DroidGap extends Activity {
if (android.os.Build.VERSION.RELEASE.startsWith("1.")) if (android.os.Build.VERSION.RELEASE.startsWith("1."))
{ {
Log.d(LOG_TAG, "bindBrowser: Adding droidStorage"); //@ibm Log.d(LOG_TAG, "bindBrowser: Adding droidStorage"); //@ibm
this.commandManager.addCommand("com.phonegap.Storage"); this.pluginManager.addPlugin("com.phonegap.Storage");
this.commandManager.addCommand("com.phonegap.GeoBroker"); this.pluginManager.addPlugin("com.phonegap.GeoBroker");
} }
} }
@ -625,7 +624,7 @@ public class DroidGap extends Activity {
* @param intent The intent to start * @param intent The intent to start
* @return The request code to use for the callback * @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++; int requestCode = this.activityResultCallbackCounter++;
this.activityResultCallbacks.put(requestCode, command); this.activityResultCallbacks.put(requestCode, command);
super.startActivityForResult(intent, requestCode); super.startActivityForResult(intent, requestCode);
@ -645,7 +644,7 @@ public class DroidGap extends Activity {
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent); super.onActivityResult(requestCode, resultCode, intent);
Command callback = this.activityResultCallbacks.remove(requestCode); Plugin callback = this.activityResultCallbacks.remove(requestCode);
if (callback != null) { if (callback != null) {
callback.onActivityResult(requestCode, resultCode, intent); callback.onActivityResult(requestCode, resultCode, intent);
} }

View File

@ -5,8 +5,8 @@ import java.util.HashMap;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import com.phonegap.api.Command; import com.phonegap.api.Plugin;
import com.phonegap.api.CommandResult; import com.phonegap.api.PluginResult;
import android.content.Intent; import android.content.Intent;
import android.webkit.WebView; 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 * 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 WebView webView; // WebView object
DroidGap ctx; // DroidGap object DroidGap ctx; // DroidGap object
@ -59,8 +59,8 @@ public class GeoBroker implements Command {
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message. * @return A CommandResult object with a status and message.
*/ */
public CommandResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";
try { try {
@ -69,14 +69,14 @@ public class GeoBroker implements Command {
} }
else if (action.equals("start")) { else if (action.equals("start")) {
String s = this.start(args.getInt(0), args.getString(1)); 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")) { else if (action.equals("stop")) {
this.stop(args.getString(0)); this.stop(args.getString(0));
} }
return new CommandResult(status, result); return new PluginResult(status, result);
} catch (JSONException e) { } 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.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import com.phonegap.api.Command; import com.phonegap.api.Plugin;
import com.phonegap.api.CommandResult; import com.phonegap.api.PluginResult;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.*; import android.net.*;
import android.webkit.WebView; import android.webkit.WebView;
public class NetworkManager implements Command { public class NetworkManager implements Plugin {
WebView webView; // WebView object WebView webView; // WebView object
DroidGap ctx; // DroidGap object DroidGap ctx; // DroidGap object
@ -54,25 +54,25 @@ public class NetworkManager implements Command {
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message. * @return A CommandResult object with a status and message.
*/ */
public CommandResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";
try { try {
if (action.equals("isAvailable")) { if (action.equals("isAvailable")) {
boolean b = this.isAvailable(); boolean b = this.isAvailable();
return new CommandResult(status, b); return new PluginResult(status, b);
} }
else if (action.equals("isWifiActive")) { else if (action.equals("isWifiActive")) {
boolean b = this.isWifiActive(); boolean b = this.isWifiActive();
return new CommandResult(status, b); return new PluginResult(status, b);
} }
else if (action.equals("isReachable")) { else if (action.equals("isReachable")) {
boolean b = this.isReachable(args.getString(0)); 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) { } 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.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import com.phonegap.api.Command; import com.phonegap.api.Plugin;
import com.phonegap.api.CommandResult; import com.phonegap.api.PluginResult;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
@ -12,7 +12,7 @@ import android.database.sqlite.*;
import android.util.Log; import android.util.Log;
import android.webkit.WebView; import android.webkit.WebView;
public class Storage implements Command { public class Storage implements Plugin {
private static final String LOG_TAG = "SQLite Storage:"; 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. * @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message. * @return A CommandResult object with a status and message.
*/ */
public CommandResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";
try { try {
@ -76,9 +76,9 @@ public class Storage implements Command {
} }
this.executeSql(args.getString(0), s, args.getString(2)); this.executeSql(args.getString(0), s, args.getString(2));
} }
return new CommandResult(status, result); return new PluginResult(status, result);
} catch (JSONException e) { } 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 org.json.JSONArray;
import com.phonegap.api.Command; import com.phonegap.api.Plugin;
import com.phonegap.api.CommandResult; import com.phonegap.api.PluginResult;
import android.hardware.Sensor; import android.hardware.Sensor;
import android.hardware.SensorEvent; import android.hardware.SensorEvent;
@ -15,7 +15,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.webkit.WebView; import android.webkit.WebView;
public class TempListener implements SensorEventListener, Command { public class TempListener implements SensorEventListener, Plugin {
WebView webView; // WebView object WebView webView; // WebView object
DroidGap ctx; // DroidGap object DroidGap ctx; // DroidGap object
@ -57,8 +57,8 @@ public class TempListener implements SensorEventListener, Command {
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the command.
* @return A CommandResult object with a status and message. * @return A CommandResult object with a status and message.
*/ */
public CommandResult execute(String action, JSONArray args) { public PluginResult execute(String action, JSONArray args) {
CommandResult.Status status = CommandResult.Status.OK; PluginResult.Status status = PluginResult.Status.OK;
String result = ""; String result = "";
if (action.equals("start")) { if (action.equals("start")) {
@ -67,7 +67,7 @@ public class TempListener implements SensorEventListener, Command {
else if (action.equals("stop")) { else if (action.equals("stop")) {
this.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; import android.webkit.WebView;
/** /**
* Command interface must be implemented by any plugin classes. * Plugin interface must be implemented by any plugin classes.
*
* The execute method is called by the CommandManager.
*
* @author davejohnson
* *
* 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 action The plugin to execute.
* @param args JSONArry of arguments for the command. * @param args JSONArry of arguments for the plugin.
* @return A CommandResult object with a status and message. * @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. * get file paths associated with the Activity.
* *
* @param ctx The context of the main Activity. * @param ctx The context of the main Activity.

View File

@ -13,22 +13,26 @@ import android.webkit.WebView;
import com.phonegap.DroidGap; 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. * from JavaScript.
*
* @author davejohnson
*
*/ */
public final class CommandManager { public final class PluginManager {
private HashMap<String, Command> commands = new HashMap<String,Command>(); private HashMap<String, Plugin> plugins = new HashMap<String,Plugin>();
private final DroidGap ctx; private final DroidGap ctx;
private final WebView app; 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.ctx = ctx;
this.app = app; this.app = app;
} }
@ -37,7 +41,7 @@ public final class CommandManager {
* Receives a request for execution and fulfills it by finding the appropriate * Receives a request for execution and fulfills it by finding the appropriate
* Java class and calling it's execute method. * 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 * string is returned that will indicate if any errors have occurred when trying to find
* or execute the class denoted by the clazz argument. * 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. * @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) { 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 { try {
final JSONArray args = new JSONArray(jsonArgs); final JSONArray args = new JSONArray(jsonArgs);
Class c = getClassByName(clazz); Class c = getClassByName(clazz);
if (isPhoneGapCommand(c)) { if (isPhoneGapPlugin(c)) {
// Create a new instance of the plugin and set the context and webview final Plugin plugin = this.addPlugin(clazz); //cmd;
final Command plugin = this.addCommand(clazz);
final DroidGap ctx = this.ctx; final DroidGap ctx = this.ctx;
if (async) { if (async) {
// Run this on a different thread so that this one can return back to JS // Run this on a different thread so that this one can return back to JS
Thread thread = new Thread(new Runnable() { Thread thread = new Thread(new Runnable() {
public void run() { public void run() {
// Call execute on the plugin so that it can do it's thing // 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 // Check the status for 0 (success) or otherwise
if (cr.getStatus() == 0) { if (cr.getStatus() == 0) {
ctx.sendJavascript(cr.toSuccessCallbackString(callbackId)); ctx.sendJavascript(cr.toSuccessCallbackString(callbackId));
//app.loadUrl(cr.toSuccessCallbackString(callbackId));
} else { } else {
ctx.sendJavascript(cr.toErrorCallbackString(callbackId)); ctx.sendJavascript(cr.toErrorCallbackString(callbackId));
//app.loadUrl(cr.toErrorCallbackString(callbackId));
} }
} }
}); });
@ -85,14 +91,18 @@ public final class CommandManager {
} }
} }
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
cr = new CommandResult(CommandResult.Status.CLASS_NOT_FOUND_EXCEPTION); cr = new PluginResult(PluginResult.Status.CLASS_NOT_FOUND_EXCEPTION);
} catch (JSONException e) { } catch (JSONException e) {
System.out.println("ERROR: "+e.toString()); 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 we have already returned at this point unless there was an error...
if (async) { if (async) {
ctx.sendJavascript(cr.toErrorCallbackString(callbackId)); 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' }" ); 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 * 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. * @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) { private boolean isPhoneGapPlugin(Class c) {
boolean isCommand = false; boolean isPlugin = false;
Class[] interfaces = c.getInterfaces(); Class[] interfaces = c.getInterfaces();
for (int j=0; j<interfaces.length; j++) { for (int j=0; j<interfaces.length; j++) {
if (interfaces[j].getName().equals("com.phonegap.api.Command")) { if (interfaces[j].getName().equals("com.phonegap.api.Plugin")) {
isCommand = true; isPlugin = true;
break; break;
} }
} }
return isCommand; return isPlugin;
} }
/** /**
* Add command to be loaded and cached. * Add plugin to be loaded and cached.
* If command is already created, then just return it. * If plugin is already created, then just return it.
* *
* @param className The class to load * @param className The class to load
* @return The command * @return The plugin
*/ */
public Command addCommand(String className) { public Plugin addPlugin(String className) {
if (this.commands.containsKey(className)) { System.out.println("PluginManager.addPlugin("+className+")");
return this.getCommand(className); if (this.plugins.containsKey(className)) {
return this.getPlugin(className);
} }
try { try {
Command command = (Command)Class.forName(className).newInstance(); Plugin plugin = (Plugin)Class.forName(className).newInstance();
this.commands.put(className, command); this.plugins.put(className, plugin);
command.setContext((DroidGap)this.ctx); plugin.setContext((DroidGap)this.ctx);
command.setView(this.app); plugin.setView(this.app);
return command; return plugin;
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
System.out.println("Error adding command "+className+"."); System.out.println("Error adding plugin "+className+".");
} }
return null; 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 * @return
*/ */
public Command getCommand(String className) { public Plugin getPlugin(String className) {
Command command = this.commands.get(className); Plugin plugin = this.plugins.get(className);
return command; return plugin;
} }
/** /**
* Called when the system is about to start resuming a previous activity. * Called when the system is about to start resuming a previous activity.
*/ */
public void onPause() { public void onPause() {
java.util.Set<Entry<String,Command>> s = this.commands.entrySet(); java.util.Set<Entry<String,Plugin>> s = this.plugins.entrySet();
java.util.Iterator<Entry<String,Command>> it = s.iterator(); java.util.Iterator<Entry<String,Plugin>> it = s.iterator();
while(it.hasNext()) { while(it.hasNext()) {
Entry<String,Command> entry = it.next(); Entry<String,Plugin> entry = it.next();
Command command = entry.getValue(); Plugin plugin = entry.getValue();
command.onPause(); plugin.onPause();
} }
} }
@ -180,12 +191,12 @@ public final class CommandManager {
* Called when the activity will start interacting with the user. * Called when the activity will start interacting with the user.
*/ */
public void onResume() { public void onResume() {
java.util.Set<Entry<String,Command>> s = this.commands.entrySet(); java.util.Set<Entry<String,Plugin>> s = this.plugins.entrySet();
java.util.Iterator<Entry<String,Command>> it = s.iterator(); java.util.Iterator<Entry<String,Plugin>> it = s.iterator();
while(it.hasNext()) { while(it.hasNext()) {
Entry<String,Command> entry = it.next(); Entry<String,Plugin> entry = it.next();
Command command = entry.getValue(); Plugin plugin = entry.getValue();
command.onResume(); plugin.onResume();
} }
} }
@ -193,13 +204,26 @@ public final class CommandManager {
* The final call you receive before your activity is destroyed. * The final call you receive before your activity is destroyed.
*/ */
public void onDestroy() { public void onDestroy() {
java.util.Set<Entry<String,Command>> s = this.commands.entrySet(); java.util.Set<Entry<String,Plugin>> s = this.plugins.entrySet();
java.util.Iterator<Entry<String,Command>> it = s.iterator(); java.util.Iterator<Entry<String,Plugin>> it = s.iterator();
while(it.hasNext()) { while(it.hasNext()) {
Entry<String,Command> entry = it.next(); Entry<String,Plugin> entry = it.next();
Command command = entry.getValue(); Plugin plugin = entry.getValue();
command.onDestroy(); 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; import org.json.JSONObject;
public class CommandResult { public class PluginResult {
private final int status; private final int status;
private final String message; private final String message;
public CommandResult(Status status) { public PluginResult(Status status) {
this.status = status.ordinal(); 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.status = status.ordinal();
this.message = "'" + message + "'"; this.message = "'" + message + "'";
} }
public CommandResult(Status status, JSONObject message) { public PluginResult(Status status, JSONObject message) {
this.status = status.ordinal(); this.status = status.ordinal();
this.message = message.toString(); this.message = message.toString();
} }
public CommandResult(Status status, int i) { // TODO: BC: Added
public PluginResult(Status status, int i) {
this.status = status.ordinal(); this.status = status.ordinal();
this.message = ""+i; this.message = ""+i;
} }
public CommandResult(Status status, float f) { public PluginResult(Status status, float f) {
this.status = status.ordinal(); this.status = status.ordinal();
this.message = ""+f; this.message = ""+f;
} }
public CommandResult(Status status, boolean b) { public PluginResult(Status status, boolean b) {
this.status = status.ordinal(); this.status = status.ordinal();
this.message = ""+b; this.message = ""+b;
} }