diff --git a/framework/assets/js/notification.js b/framework/assets/js/notification.js old mode 100644 new mode 100755 index e78e9a5e..2d783211 --- a/framework/assets/js/notification.js +++ b/framework/assets/js/notification.js @@ -11,8 +11,9 @@ function Notification() { * @param {String} [buttonLabel="OK"] Label of the close button (default: OK) */ Notification.prototype.alert = function(message, title, buttonLabel) { - // Default is to use a browser alert; this will use "index.html" as the title though - alert(message); + var _title = (title || "Alert"); + var _buttonLabel = (buttonLabel || "OK"); + PhoneGap.execAsync(null, null, "Notification", "alert", [message,_title,_buttonLabel]); }; /** diff --git a/framework/src/com/phonegap/AccelListener.java b/framework/src/com/phonegap/AccelListener.java index b24261d0..fb4005a7 100755 --- a/framework/src/com/phonegap/AccelListener.java +++ b/framework/src/com/phonegap/AccelListener.java @@ -14,14 +14,12 @@ import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.content.Context; -import android.content.Intent; -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, Plugin { +public class AccelListener extends Plugin implements SensorEventListener { public static int STOPPED = 0; public static int STARTING = 1; @@ -29,9 +27,6 @@ public class AccelListener implements SensorEventListener, Plugin { public static int ERROR_FAILED_TO_START = 3; public float TIMEOUT = 30000; // Timeout in msec to shut off listener - - WebView webView; // WebView object - DroidGap ctx; // DroidGap object float x,y,z; // most recent acceleration values long timestamp; // time of most recent value @@ -59,28 +54,19 @@ public class AccelListener implements SensorEventListener, Plugin { * @param ctx The context of the main Activity. */ public void setContext(DroidGap ctx) { - this.ctx = ctx; + super.setContext(ctx); this.sensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE); } /** - * Sets the main View of the application, this is the WebView within which - * a PhoneGap app runs. + * Executes the request and returns PluginResult. * - * @param webView The PhoneGap WebView + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackId The callback id used when calling back into JavaScript. + * @return A PluginResult object with a status and message. */ - public void setView(WebView webView) { - this.webView = webView; - } - - /** - * Executes the request and returns CommandResult. - * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. - */ - public PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; @@ -171,18 +157,6 @@ public class AccelListener implements SensorEventListener, Plugin { } return false; } - - /** - * Called when the system is about to start resuming a previous activity. - */ - public void onPause() { - } - - /** - * Called when the activity will start interacting with the user. - */ - public void onResume() { - } /** * Called by AccelBroker when listener is to be shut down. @@ -192,18 +166,6 @@ public class AccelListener implements SensorEventListener, Plugin { this.stop(); } - /** - * Called when an activity you launched exits, giving you the requestCode you started it with, - * the resultCode it returned, and any additional data from it. - * - * @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"). - */ - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - } - //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- diff --git a/framework/src/com/phonegap/AudioHandler.java b/framework/src/com/phonegap/AudioHandler.java index e2ca4b83..9beb7a65 100755 --- a/framework/src/com/phonegap/AudioHandler.java +++ b/framework/src/com/phonegap/AudioHandler.java @@ -7,13 +7,10 @@ import org.json.JSONArray; import org.json.JSONException; import com.phonegap.api.Plugin; -import com.phonegap.api.PluginManager; import com.phonegap.api.PluginResult; import android.content.Context; -import android.content.Intent; import android.media.AudioManager; -import android.webkit.WebView; /** * This class called by DroidGap to play and record audio. @@ -26,10 +23,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 Plugin { - - WebView webView; // WebView object - DroidGap ctx; // DroidGap object +public class AudioHandler extends Plugin { HashMap players; // Audio player object @@ -41,33 +35,14 @@ public class AudioHandler implements Plugin { } /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. + * Executes the request and returns PluginResult. * - * @param ctx The context of the main Activity. + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackId The callback id used when calling back into JavaScript. + * @return A PluginResult object with a status and message. */ - public void setContext(DroidGap ctx) { - this.ctx = ctx; - } - - /** - * Sets the main View of the application, this is the WebView within which - * a PhoneGap app runs. - * - * @param webView The PhoneGap WebView - */ - public void setView(WebView webView) { - this.webView = webView; - } - - /** - * Executes the request and returns CommandResult. - * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. - */ - public PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; @@ -118,18 +93,6 @@ public class AudioHandler implements Plugin { return false; } - /** - * Called when the system is about to start resuming a previous activity. - */ - public void onPause() { - } - - /** - * Called when the activity will start interacting with the user. - */ - public void onResume() { - } - /** * Stop all audio players and recorders. */ @@ -144,18 +107,6 @@ public class AudioHandler implements Plugin { this.players.clear(); } - /** - * Called when an activity you launched exits, giving you the requestCode you started it with, - * the resultCode it returned, and any additional data from it. - * - * @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"). - */ - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - } - //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- diff --git a/framework/src/com/phonegap/AudioPlayer.java b/framework/src/com/phonegap/AudioPlayer.java index 36aa62f9..48a58004 100755 --- a/framework/src/com/phonegap/AudioPlayer.java +++ b/framework/src/com/phonegap/AudioPlayer.java @@ -95,7 +95,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On public void startRecording(String file) { if (this.mPlayer != null) { System.out.println("AudioPlayer Error: Can't record in play mode."); - this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_PLAY_MODE_SET+");"); + this.handler.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_PLAY_MODE_SET+");"); } // Make sure we're not already recording @@ -116,11 +116,11 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On } catch (IOException e) { e.printStackTrace(); } - this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_STARTING_RECORDING+");"); + this.handler.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_STARTING_RECORDING+");"); } else { System.out.println("AudioPlayer Error: Already recording."); - this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_ALREADY_RECORDING+");"); + this.handler.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_ALREADY_RECORDING+");"); } } @@ -162,7 +162,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On public void startPlaying(String file) { if (this.recorder != null) { System.out.println("AudioPlayer Error: Can't play in record mode."); - this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_RECORD_MODE_SET+");"); + this.handler.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_RECORD_MODE_SET+");"); } // If this is a new request to play audio, or stopped @@ -205,7 +205,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On } catch (Exception e) { e.printStackTrace(); - this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_STARTING_PLAYBACK+");"); + this.handler.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_STARTING_PLAYBACK+");"); } } @@ -219,7 +219,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On } else { System.out.println("AudioPlayer Error: startPlaying() called during invalid state: "+this.state); - this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_RESUME_STATE+");"); + this.handler.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_RESUME_STATE+");"); } } } @@ -236,7 +236,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On } else { System.out.println("AudioPlayer Error: pausePlaying() called during invalid state: "+this.state); - this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_PAUSE_STATE+");"); + this.handler.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_PAUSE_STATE+");"); } } @@ -250,7 +250,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On } else { System.out.println("AudioPlayer Error: stopPlaying() called during invalid state: "+this.state); - this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_STOP_STATE+");"); + this.handler.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+MEDIA_ERROR_STOP_STATE+");"); } } @@ -348,7 +348,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On this.prepareOnly = false; // Send status notification to JavaScript - this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_DURATION+","+this.duration+");"); + this.handler.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_DURATION+","+this.duration+");"); } @@ -368,7 +368,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On this.mPlayer.release(); // Send error notification to JavaScript - this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+arg1+");"); + this.handler.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_ERROR+", "+arg1+");"); return false; } @@ -379,7 +379,7 @@ public class AudioPlayer implements OnCompletionListener, OnPreparedListener, On */ private void setState(int state) { if (this.state != state) { - this.handler.ctx.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_STATE+", "+this.state+");"); + this.handler.sendJavascript("PhoneGap.Media.onStatus('" + this.id + "', "+MEDIA_STATE+", "+state+");"); } this.state = state; diff --git a/framework/src/com/phonegap/CameraLauncher.java b/framework/src/com/phonegap/CameraLauncher.java index cb05721b..dab270c3 100755 --- a/framework/src/com/phonegap/CameraLauncher.java +++ b/framework/src/com/phonegap/CameraLauncher.java @@ -20,7 +20,6 @@ import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.net.Uri; import android.os.Environment; -import android.webkit.WebView; import android.provider.MediaStore; /** @@ -28,7 +27,7 @@ import android.provider.MediaStore; * 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 Plugin { +public class CameraLauncher extends Plugin { private static final int DATA_URL = 0; // Return base64 encoded string private static final int FILE_URI = 1; // Return file uri (content://media/external/images/media/2 for Android) @@ -37,9 +36,6 @@ public class CameraLauncher implements Plugin { private static final int CAMERA = 1; // Take picture from camera private static final int SAVEDPHOTOALBUM = 2; // Choose image from picture library (same as PHOTOLIBRARY for Android) - WebView webView; // WebView object - DroidGap ctx; // DroidGap object - private int mQuality; // Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality) private Uri imageUri; // Uri of captured image @@ -50,33 +46,14 @@ public class CameraLauncher implements Plugin { } /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. + * Executes the request and returns PluginResult. * - * @param ctx The context of the main Activity. + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackId The callback id used when calling back into JavaScript. + * @return A PluginResult object with a status and message. */ - public void setContext(DroidGap ctx) { - this.ctx = ctx; - } - - /** - * Sets the main View of the application, this is the WebView within which - * a PhoneGap app runs. - * - * @param webView The PhoneGap WebView - */ - public void setView(WebView webView) { - this.webView = webView; - } - - /** - * Executes the request and returns CommandResult. - * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. - */ - public PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; @@ -103,35 +80,6 @@ public class CameraLauncher implements Plugin { return new PluginResult(PluginResult.Status.JSON_EXCEPTION); } } - - /** - * Identifies if action to be executed returns a value and should be run synchronously. - * - * @param action The action to execute - * @return T=returns value - */ - public boolean isSynch(String action) { - return false; - } - - /** - * Called when the system is about to start resuming a previous activity. - */ - public void onPause() { - } - - /** - * Called when the activity will start interacting with the user. - */ - public void onResume() { - } - - /** - * Called by AccelBroker when listener is to be shut down. - * Stop listener. - */ - public void onDestroy() { - } //-------------------------------------------------------------------------- // LOCAL METHODS @@ -235,7 +183,7 @@ public class CameraLauncher implements Plugin { os.close(); // Send Uri back to JavaScript for viewing image - this.ctx.sendJavascript("navigator.camera.success('" + uri.toString() + "');"); + this.sendJavascript("navigator.camera.success('" + uri.toString() + "');"); } } catch (IOException e) { e.printStackTrace(); @@ -272,7 +220,7 @@ public class CameraLauncher implements Plugin { // If sending filename back else if (destType == FILE_URI) { - this.ctx.sendJavascript("navigator.camera.success('" + uri + "');"); + this.sendJavascript("navigator.camera.success('" + uri + "');"); } } else if (resultCode == Activity.RESULT_CANCELED) { @@ -296,7 +244,7 @@ public class CameraLauncher implements Plugin { byte[] code = jpeg_data.toByteArray(); byte[] output = Base64.encodeBase64(code); String js_out = new String(output); - this.ctx.sendJavascript("navigator.camera.success('" + js_out + "');"); + this.sendJavascript("navigator.camera.success('" + js_out + "');"); } } catch(Exception e) { @@ -310,6 +258,6 @@ public class CameraLauncher implements Plugin { * @param err */ public void failPicture(String err) { - this.ctx.sendJavascript("navigator.camera.error('" + err + "');"); + this.sendJavascript("navigator.camera.error('" + err + "');"); } } diff --git a/framework/src/com/phonegap/CompassListener.java b/framework/src/com/phonegap/CompassListener.java index 6ff6efe1..88e96b89 100755 --- a/framework/src/com/phonegap/CompassListener.java +++ b/framework/src/com/phonegap/CompassListener.java @@ -13,13 +13,11 @@ import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.content.Context; -import android.content.Intent; -import android.webkit.WebView; /** * This class listens to the compass sensor and stores the latest heading value. */ -public class CompassListener implements SensorEventListener, Plugin{ +public class CompassListener extends Plugin implements SensorEventListener { public static int STOPPED = 0; public static int STARTING = 1; @@ -28,9 +26,6 @@ public class CompassListener implements SensorEventListener, Plugin{ public long TIMEOUT = 30000; // Timeout in msec to shut off listener - WebView webView; // WebView object - DroidGap ctx; // DroidGap object - int status; // status of listener float heading; // most recent heading value long timeStamp; // time of most recent value @@ -54,28 +49,19 @@ public class CompassListener implements SensorEventListener, Plugin{ * @param ctx The context of the main Activity. */ public void setContext(DroidGap ctx) { - this.ctx = ctx; + super.setContext(ctx); this.sensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE); } /** - * Sets the main View of the application, this is the WebView within which - * a PhoneGap app runs. + * Executes the request and returns PluginResult. * - * @param webView The PhoneGap WebView + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackId The callback id used when calling back into JavaScript. + * @return A PluginResult object with a status and message. */ - public void setView(WebView webView) { - this.webView = webView; - } - - /** - * Executes the request and returns CommandResult. - * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. - */ - public PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; @@ -149,18 +135,6 @@ public class CompassListener implements SensorEventListener, Plugin{ } return false; } - - /** - * Called when the system is about to start resuming a previous activity. - */ - public void onPause() { - } - - /** - * Called when the activity will start interacting with the user. - */ - public void onResume() { - } /** * Called when listener is to be shut down and object is being destroyed. @@ -169,18 +143,6 @@ public class CompassListener implements SensorEventListener, Plugin{ this.stop(); } - /** - * Called when an activity you launched exits, giving you the requestCode you started it with, - * the resultCode it returned, and any additional data from it. - * - * @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"). - */ - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - } - //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- diff --git a/framework/src/com/phonegap/ContactManager.java b/framework/src/com/phonegap/ContactManager.java index 3d3bb7e4..00ad3e12 100755 --- a/framework/src/com/phonegap/ContactManager.java +++ b/framework/src/com/phonegap/ContactManager.java @@ -3,20 +3,13 @@ package com.phonegap; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; - import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; - import android.util.Log; -import android.webkit.WebView; -import android.content.Intent; -public class ContactManager implements Plugin { +public class ContactManager extends Plugin { private static ContactAccessor contactAccessor; - WebView webView; // WebView object - DroidGap ctx; // DroidGap object - private static final String LOG_TAG = "Contact Query"; /** @@ -26,33 +19,14 @@ public class ContactManager implements Plugin { } /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. + * Executes the request and returns PluginResult. * - * @param ctx The context of the main Activity. + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackId The callback id used when calling back into JavaScript. + * @return A PluginResult object with a status and message. */ - public void setContext(DroidGap ctx) { - this.ctx = ctx; - } - - /** - * Sets the main View of the application, this is the WebView within which - * a PhoneGap app runs. - * - * @param webView The PhoneGap WebView - */ - public void setView(WebView webView) { - this.webView = webView; - } - - /** - * Executes the request and returns CommandResult. - * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. - */ - public PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { if (contactAccessor == null) { contactAccessor = ContactAccessor.getInstance(webView, ctx); } @@ -83,45 +57,4 @@ public class ContactManager implements Plugin { return new PluginResult(PluginResult.Status.JSON_EXCEPTION); } } - - /** - * Identifies if action to be executed returns a value and should be run synchronously. - * - * @param action The action to execute - * @return T=returns value - */ - public boolean isSynch(String action) { - return false; - } - - /** - * Called when the system is about to start resuming a previous activity. - */ - public void onPause() { - } - - /** - * Called when the activity will start interacting with the user. - */ - public void onResume() { - } - - /** - * Called by AccelBroker when listener is to be shut down. - * Stop listener. - */ - public void onDestroy() { - } - - /** - * Called when an activity you launched exits, giving you the requestCode you started it with, - * the resultCode it returned, and any additional data from it. - * - * @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"). - */ - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - } } diff --git a/framework/src/com/phonegap/CryptoHandler.java b/framework/src/com/phonegap/CryptoHandler.java index 96ca274a..746433c8 100755 --- a/framework/src/com/phonegap/CryptoHandler.java +++ b/framework/src/com/phonegap/CryptoHandler.java @@ -6,14 +6,8 @@ import org.json.JSONException; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; -import android.content.Intent; -import android.webkit.WebView; - -public class CryptoHandler implements Plugin { - - WebView webView; // WebView object - DroidGap ctx; // DroidGap object - +public class CryptoHandler extends Plugin { + /** * Constructor. */ @@ -21,33 +15,14 @@ public class CryptoHandler implements Plugin { } /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. + * Executes the request and returns PluginResult. * - * @param ctx The context of the main Activity. + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackId The callback id used when calling back into JavaScript. + * @return A PluginResult object with a status and message. */ - public void setContext(DroidGap ctx) { - this.ctx = ctx; - } - - /** - * Sets the main View of the application, this is the WebView within which - * a PhoneGap app runs. - * - * @param webView The PhoneGap WebView - */ - public void setView(WebView webView) { - this.webView = webView; - } - - /** - * Executes the request and returns CommandResult. - * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. - */ - public PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; @@ -64,47 +39,6 @@ public class CryptoHandler implements Plugin { } } - /** - * Identifies if action to be executed returns a value and should be run synchronously. - * - * @param action The action to execute - * @return T=returns value - */ - public boolean isSynch(String action) { - return false; - } - - /** - * Called when the system is about to start resuming a previous activity. - */ - public void onPause() { - } - - /** - * Called when the activity will start interacting with the user. - */ - public void onResume() { - } - - /** - * Called by AccelBroker when listener is to be shut down. - * Stop listener. - */ - public void onDestroy() { - } - - /** - * Called when an activity you launched exits, giving you the requestCode you started it with, - * the resultCode it returned, and any additional data from it. - * - * @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"). - */ - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - } - //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- @@ -113,7 +47,7 @@ public class CryptoHandler implements Plugin { try { String encrypted = SimpleCrypto.encrypt(pass,text); // TODO: Why not just return text now? - this.ctx.sendJavascript("Crypto.gotCryptedString('" + text + "')"); + this.sendJavascript("Crypto.gotCryptedString('" + text + "')"); } catch (Exception e) { e.printStackTrace(); } @@ -122,7 +56,7 @@ public class CryptoHandler implements Plugin { public void decrypt(String pass, String text) { try { String decrypted = SimpleCrypto.decrypt(pass,text); - this.ctx.sendJavascript("Crypto.gotPlainString('" + text + "')"); + this.sendJavascript("Crypto.gotPlainString('" + text + "')"); } catch (Exception e) { e.printStackTrace(); } diff --git a/framework/src/com/phonegap/Device.java b/framework/src/com/phonegap/Device.java index 80c1281b..a4cebbf4 100755 --- a/framework/src/com/phonegap/Device.java +++ b/framework/src/com/phonegap/Device.java @@ -29,19 +29,14 @@ import org.json.JSONObject; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; import android.content.Context; -import android.content.Intent; import android.provider.Settings; import android.telephony.TelephonyManager; -import android.webkit.WebView; -public class Device implements Plugin { +public class Device extends Plugin { public static String phonegapVersion = "pre-0.92 EDGE"; // PhoneGap version public static String platform = "Android"; // Device OS public static String uuid; // Device UUID - private DroidGap ctx; // DroidGap object - @SuppressWarnings("unused") - private WebView webView; // Webview object /** * Constructor. @@ -56,28 +51,19 @@ public class Device implements Plugin { * @param ctx The context of the main Activity. */ public void setContext(DroidGap ctx) { - this.ctx = ctx; + super.setContext(ctx); Device.uuid = getUuid(); } /** - * Sets the main View of the application, this is the WebView within which - * a PhoneGap app runs. + * Executes the request and returns PluginResult. * - * @param webView The PhoneGap WebView + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackId The callback id used when calling back into JavaScript. + * @return A PluginResult object with a status and message. */ - public void setView(WebView webView) { - this.webView = webView; - } - - /** - * Executes the request and returns CommandResult. - * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. - */ - public PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; @@ -113,37 +99,6 @@ public class Device implements Plugin { return false; } - /** - * Called when the system is about to start resuming a previous activity. - */ - public void onPause() { - } - - /** - * Called when the activity will start interacting with the user. - */ - public void onResume() { - } - - /** - * Called when the activity is to be shut down. - * Stop listener. - */ - public void onDestroy() { - } - - /** - * Called when an activity you launched exits, giving you the requestCode you started it with, - * the resultCode it returned, and any additional data from it. - * - * @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"). - */ - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - } - //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index a861732c..3c7cabc6 100755 --- a/framework/src/com/phonegap/DroidGap.java +++ b/framework/src/com/phonegap/DroidGap.java @@ -275,15 +275,6 @@ public class DroidGap extends Activity { appView.addJavascriptInterface(this.callbackServer, "CallbackServer"); appView.addJavascriptInterface(new SplashScreen(this), "SplashScreen"); - // Add in support for storage and location for Android 1.X devices - if (android.os.Build.VERSION.RELEASE.startsWith("1.")) { - Package pack = this.getClass().getPackage(); - String appPackage = pack.getName(); - Storage cupcakeStorage = (Storage)this.pluginManager.addPlugin("com.phonegap.Storage"); - cupcakeStorage.setStorage(appPackage); - - - } this.addService("Geolocation", "com.phonegap.GeoBroker"); this.addService("Device", "com.phonegap.Device"); @@ -294,13 +285,23 @@ public class DroidGap extends Activity { this.addService("Contacts", "com.phonegap.ContactManager"); this.addService("Crypto", "com.phonegap.CryptoHandler"); this.addService("File", "com.phonegap.FileUtils"); - this.addService("Location", "com.phonegap.GeoBroker"); + this.addService("Location", "com.phonegap.GeoBroker"); // Always add Location, even though it is built-in on 2.x devices. Let JavaScript decide which one to use. this.addService("Network Status", "com.phonegap.NetworkManager"); this.addService("Notification", "com.phonegap.Notification"); this.addService("Storage", "com.phonegap.Storage"); this.addService("Temperature", "com.phonegap.TempListener"); - } + + // Add in support for storage for Android 1.X devices + if (android.os.Build.VERSION.RELEASE.startsWith("1.")) { + System.out.println("Android 1.X device"); + + Package pack = this.getClass().getPackage(); + String appPackage = pack.getName(); + this.pluginManager.exec("Storage", "setStorage", null, "["+appPackage+"]", false); + } + } + /** * Load the url into the webview. * diff --git a/framework/src/com/phonegap/FileUtils.java b/framework/src/com/phonegap/FileUtils.java index 598c23e9..094fa08a 100755 --- a/framework/src/com/phonegap/FileUtils.java +++ b/framework/src/com/phonegap/FileUtils.java @@ -9,14 +9,11 @@ import org.json.JSONException; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; -import android.content.Intent; -import android.webkit.WebView; - /** * This class provides SD card file and directory services to JavaScript. * Only files on the SD card can be accessed. */ -public class FileUtils implements Plugin { +public class FileUtils extends Plugin { public static int NOT_FOUND_ERR = 8; public static int SECURITY_ERR = 18; @@ -25,10 +22,6 @@ public class FileUtils implements Plugin { public static int NOT_READABLE_ERR = 24; public static int ENCODING_ERR = 26; - - WebView webView; // WebView object - DroidGap ctx; // DroidGap object - FileReader f_in; FileWriter f_out; @@ -40,33 +33,14 @@ public class FileUtils implements Plugin { } /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. + * Executes the request and returns PluginResult. * - * @param ctx The context of the main Activity. + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackId The callback id used when calling back into JavaScript. + * @return A PluginResult object with a status and message. */ - public void setContext(DroidGap ctx) { - this.ctx = ctx; - } - - /** - * Sets the main View of the application, this is the WebView within which - * a PhoneGap app runs. - * - * @param webView The PhoneGap WebView - */ - public void setView(WebView webView) { - this.webView = webView; - } - - /** - * Executes the request and returns CommandResult. - * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. - */ - public PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; //System.out.println("FileUtils.execute("+action+")"); @@ -161,37 +135,6 @@ public class FileUtils implements Plugin { return true; } - /** - * Called when the system is about to start resuming a previous activity. - */ - public void onPause() { - } - - /** - * Called when the activity will start interacting with the user. - */ - public void onResume() { - } - - /** - * Called by AccelBroker when listener is to be shut down. - * Stop listener. - */ - public void onDestroy() { - } - - /** - * Called when an activity you launched exits, giving you the requestCode you started it with, - * the resultCode it returned, and any additional data from it. - * - * @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"). - */ - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - } - //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- diff --git a/framework/src/com/phonegap/GeoBroker.java b/framework/src/com/phonegap/GeoBroker.java index 45281d76..6dafc401 100755 --- a/framework/src/com/phonegap/GeoBroker.java +++ b/framework/src/com/phonegap/GeoBroker.java @@ -9,20 +9,14 @@ import org.json.JSONException; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; -import android.content.Intent; -import android.webkit.WebView; - /* * This class is the interface to the Geolocation. It's bound to the geo object. * * This class only starts and stops various GeoListeners, which consist of a GPS and a Network Listener */ -public class GeoBroker implements Plugin { +public class GeoBroker extends Plugin { - WebView webView; // WebView object - DroidGap ctx; // DroidGap object - // List of gGeolocation listeners private HashMap geoListeners; private GeoListener global; @@ -35,33 +29,14 @@ public class GeoBroker implements Plugin { } /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. + * Executes the request and returns PluginResult. * - * @param ctx The context of the main Activity. + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackId The callback id used when calling back into JavaScript. + * @return A PluginResult object with a status and message. */ - public void setContext(DroidGap ctx) { - this.ctx = ctx; - } - - /** - * Sets the main View of the application, this is the WebView within which - * a PhoneGap app runs. - * - * @param webView The PhoneGap WebView - */ - public void setView(WebView webView) { - this.webView = webView; - } - - /** - * Executes the request and returns CommandResult. - * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. - */ - public PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; @@ -92,18 +67,6 @@ public class GeoBroker implements Plugin { // Starting listeners is easier to run on main thread, so don't run async. return true; } - - /** - * Called when the system is about to start resuming a previous activity. - */ - public void onPause() { - } - - /** - * Called when the activity will start interacting with the user. - */ - public void onResume() { - } /** * Called when the activity is to be shut down. @@ -124,18 +87,6 @@ public class GeoBroker implements Plugin { this.global = null; } - /** - * Called when an activity you launched exits, giving you the requestCode you started it with, - * the resultCode it returned, and any additional data from it. - * - * @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"). - */ - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - } - //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- @@ -152,7 +103,7 @@ public class GeoBroker implements Plugin { // Create a geolocation listener just for getCurrentLocation and call it "global" if (this.global == null) { - this.global = new GeoListener("global", this.ctx, maximumAge, this.webView); + this.global = new GeoListener(this, "global", maximumAge); } else { this.global.start(maximumAge); @@ -173,7 +124,7 @@ public class GeoBroker implements Plugin { // Make sure this listener doesn't already exist GeoListener listener = geoListeners.get(key); if (listener == null) { - listener = new GeoListener(key, this.ctx, maximumAge, this.webView); + listener = new GeoListener(this, key, maximumAge); geoListeners.put(key, listener); } diff --git a/framework/src/com/phonegap/GeoListener.java b/framework/src/com/phonegap/GeoListener.java index b78f6fb1..90e876a8 100755 --- a/framework/src/com/phonegap/GeoListener.java +++ b/framework/src/com/phonegap/GeoListener.java @@ -17,9 +17,7 @@ public class GeoListener { NetworkListener mNetwork; // Network listener LocationManager mLocMan; // Location manager - private DroidGap ctx; // DroidGap object - @SuppressWarnings("unused") - private WebView mAppView; // Webview object + private GeoBroker broker; // GeoBroker object int interval; @@ -31,24 +29,23 @@ public class GeoListener { * @param time Sampling period in msec * @param appView */ - GeoListener(String id, DroidGap ctx, int time, WebView appView) { + GeoListener(GeoBroker broker, String id, int time) { this.id = id; this.interval = time; - this.ctx = ctx; + this.broker = broker; this.mGps = null; this.mNetwork = null; - this.mLocMan = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE); + this.mLocMan = (LocationManager) broker.ctx.getSystemService(Context.LOCATION_SERVICE); // If GPS provider, then create and start GPS listener if (this.mLocMan.getProvider(LocationManager.GPS_PROVIDER) != null) { - this.mGps = new GpsListener(ctx, time, this); + this.mGps = new GpsListener(broker.ctx, time, this); } // If network provider, then create and start network listener if (this.mLocMan.getProvider(LocationManager.NETWORK_PROVIDER) != null) { - this.mNetwork = new NetworkListener(ctx, time, this); + this.mNetwork = new NetworkListener(broker.ctx, time, this); } - this.mAppView = appView; } /** @@ -72,7 +69,7 @@ public class GeoListener { if (id == "global") { this.stop(); } - this.ctx.sendJavascript("navigator._geo.success('" + id + "'," + params + ");"); + this.broker.sendJavascript("navigator._geo.success('" + id + "'," + params + ");"); } /** @@ -82,7 +79,7 @@ public class GeoListener { * @param msg The error message */ void fail(int code, String msg) { - this.ctx.sendJavascript("navigator._geo.fail('" + this.id + "', " + ", " + code + ", '" + msg + "');"); + this.broker.sendJavascript("navigator._geo.fail('" + this.id + "', " + ", " + code + ", '" + msg + "');"); this.stop(); } diff --git a/framework/src/com/phonegap/NetworkManager.java b/framework/src/com/phonegap/NetworkManager.java index 82c75fe5..256d34e3 100755 --- a/framework/src/com/phonegap/NetworkManager.java +++ b/framework/src/com/phonegap/NetworkManager.java @@ -9,19 +9,13 @@ 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 Plugin { +public class NetworkManager extends Plugin { public static int NOT_REACHABLE = 0; public static int REACHABLE_VIA_CARRIER_DATA_NETWORK = 1; public static int REACHABLE_VIA_WIFI_NETWORK = 2; - - - WebView webView; // WebView object - DroidGap ctx; // DroidGap object ConnectivityManager sockMan; @@ -38,28 +32,19 @@ public class NetworkManager implements Plugin { * @param ctx The context of the main Activity. */ public void setContext(DroidGap ctx) { - this.ctx = ctx; + super.setContext(ctx); this.sockMan = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); } /** - * Sets the main View of the application, this is the WebView within which - * a PhoneGap app runs. + * Executes the request and returns PluginResult. * - * @param webView The PhoneGap WebView + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackId The callback id used when calling back into JavaScript. + * @return A PluginResult object with a status and message. */ - public void setView(WebView webView) { - this.webView = webView; - } - - /** - * Executes the request and returns CommandResult. - * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. - */ - public PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { @@ -92,37 +77,6 @@ public class NetworkManager implements Plugin { return false; } - /** - * Called when the system is about to start resuming a previous activity. - */ - public void onPause() { - } - - /** - * Called when the activity will start interacting with the user. - */ - public void onResume() { - } - - /** - * Called by AccelBroker when listener is to be shut down. - * Stop listener. - */ - public void onDestroy() { - } - - /** - * Called when an activity you launched exits, giving you the requestCode you started it with, - * the resultCode it returned, and any additional data from it. - * - * @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"). - */ - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - } - //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- @@ -169,13 +123,13 @@ public class NetworkManager implements Plugin { uri = "http://" + uri; } - if (isAvailable()) { + if (this.isAvailable()) { try { DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(uri); httpclient.execute(httpget); - if (isWifiActive()) { + if (this.isWifiActive()) { reachable = REACHABLE_VIA_WIFI_NETWORK; } else { diff --git a/framework/src/com/phonegap/Notification.java b/framework/src/com/phonegap/Notification.java index 68a18397..3d76366f 100755 --- a/framework/src/com/phonegap/Notification.java +++ b/framework/src/com/phonegap/Notification.java @@ -4,22 +4,19 @@ import org.json.JSONArray; import org.json.JSONException; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; +import android.app.AlertDialog; import android.content.Context; -import android.content.Intent; +import android.content.DialogInterface; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Vibrator; -import android.webkit.WebView; /** * This class provides access to notifications on the device. */ -public class Notification implements Plugin { - - WebView webView; // WebView object - DroidGap ctx; // DroidGap object - +public class Notification extends Plugin { + /** * Constructor. */ @@ -27,33 +24,14 @@ public class Notification implements Plugin { } /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. + * Executes the request and returns PluginResult. * - * @param ctx The context of the main Activity. + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackId The callback id used when calling back into JavaScript. + * @return A PluginResult object with a status and message. */ - public void setContext(DroidGap ctx) { - this.ctx = ctx; - } - - /** - * Sets the main View of the application, this is the WebView within which - * a PhoneGap app runs. - * - * @param webView The PhoneGap WebView - */ - public void setView(WebView webView) { - this.webView = webView; - } - - /** - * Executes the request and returns CommandResult. - * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. - */ - public PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; @@ -64,6 +42,9 @@ public class Notification implements Plugin { else if (action.equals("vibrate")) { this.vibrate(args.getLong(0)); } + else if (action.equals("alert")) { + this.alert(args.getString(0),args.getString(1),args.getString(2)); + } return new PluginResult(status, result); } catch (JSONException e) { return new PluginResult(PluginResult.Status.JSON_EXCEPTION); @@ -77,40 +58,12 @@ public class Notification implements Plugin { * @return T=returns value */ public boolean isSynch(String action) { - return false; + if(action.equals("alert")) + return true; + else + return false; } - /** - * Called when the system is about to start resuming a previous activity. - */ - public void onPause() { - } - - /** - * Called when the activity will start interacting with the user. - */ - public void onResume() { - } - - /** - * Called by AccelBroker when listener is to be shut down. - * Stop listener. - */ - public void onDestroy() { - } - - /** - * Called when an activity you launched exits, giving you the requestCode you started it with, - * the resultCode it returned, and any additional data from it. - * - * @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"). - */ - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - } - //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- @@ -153,5 +106,26 @@ public class Notification implements Plugin { Vibrator vibrator = (Vibrator) this.ctx.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(time); } + + /** + * Builds and shows a native Android alert with given Strings + * @param message The message the alert should display + * @param title The title of the alert + * @param buttonLabel The label of the button + */ + public synchronized void alert(String message,String title,String buttonLabel){ + AlertDialog.Builder dlg = new AlertDialog.Builder(this.ctx); + dlg.setMessage(message); + dlg.setTitle(title); + dlg.setCancelable(false); + dlg.setPositiveButton(buttonLabel, + new AlertDialog.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + dlg.create(); + dlg.show(); + } } diff --git a/framework/src/com/phonegap/Storage.java b/framework/src/com/phonegap/Storage.java old mode 100644 new mode 100755 index 46a8c63c..a3339306 --- a/framework/src/com/phonegap/Storage.java +++ b/framework/src/com/phonegap/Storage.java @@ -6,18 +6,13 @@ import org.json.JSONException; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; -import android.content.Intent; import android.database.Cursor; import android.database.sqlite.*; import android.util.Log; -import android.webkit.WebView; -public class Storage implements Plugin { +public class Storage extends Plugin { private static final String LOG_TAG = "SQLite Storage:"; - - WebView webView; // WebView object - DroidGap ctx; // DroidGap object SQLiteDatabase myDb; String path; @@ -30,33 +25,14 @@ public class Storage implements Plugin { } /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. + * Executes the request and returns PluginResult. * - * @param ctx The context of the main Activity. + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackId The callback id used when calling back into JavaScript. + * @return A PluginResult object with a status and message. */ - public void setContext(DroidGap ctx) { - this.ctx = ctx; - } - - /** - * Sets the main View of the application, this is the WebView within which - * a PhoneGap app runs. - * - * @param webView The PhoneGap WebView - */ - public void setView(WebView webView) { - this.webView = webView; - } - - /** - * Executes the request and returns CommandResult. - * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. - */ - public PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; @@ -92,37 +68,6 @@ public class Storage implements Plugin { return false; } - /** - * Called when the system is about to start resuming a previous activity. - */ - public void onPause() { - } - - /** - * Called when the activity will start interacting with the user. - */ - public void onResume() { - } - - /** - * Called by AccelBroker when listener is to be shut down. - * Stop listener. - */ - public void onDestroy() { - } - - /** - * Called when an activity you launched exits, giving you the requestCode you started it with, - * the resultCode it returned, and any additional data from it. - * - * @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"). - */ - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - } - //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- @@ -146,7 +91,7 @@ public class Storage implements Plugin { } catch (SQLiteException ex) { Log.d(LOG_TAG, ex.getMessage()); txid = ""; - this.ctx.sendJavascript("droiddb.fail(" + ex.getMessage() + "," + txid + ");"); + this.sendJavascript("droiddb.fail(" + ex.getMessage() + "," + txid + ");"); } } @@ -167,9 +112,9 @@ public class Storage implements Plugin { } } resultString += "}"; - this.ctx.sendJavascript("droiddb.addResult('" + resultString + "', " + txid + ");"); + this.sendJavascript("droiddb.addResult('" + resultString + "', " + txid + ");"); } while (cur.moveToNext()); - this.ctx.sendJavascript("droiddb.completeQuery(" + txid + ");"); + this.sendJavascript("droiddb.completeQuery(" + txid + ");"); txid = ""; myDb.close(); } diff --git a/framework/src/com/phonegap/TempListener.java b/framework/src/com/phonegap/TempListener.java index 5b79d3ab..5370421f 100644 --- a/framework/src/com/phonegap/TempListener.java +++ b/framework/src/com/phonegap/TempListener.java @@ -12,13 +12,8 @@ import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.content.Context; -import android.content.Intent; -import android.webkit.WebView; -public class TempListener implements SensorEventListener, Plugin { - - WebView webView; // WebView object - DroidGap ctx; // DroidGap object +public class TempListener extends Plugin implements SensorEventListener { Sensor mSensor; private SensorManager sensorManager; @@ -36,28 +31,19 @@ public class TempListener implements SensorEventListener, Plugin { * @param ctx The context of the main Activity. */ public void setContext(DroidGap ctx) { - this.ctx = ctx; + super.setContext(ctx); this.sensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE); } /** - * Sets the main View of the application, this is the WebView within which - * a PhoneGap app runs. + * Executes the request and returns PluginResult. * - * @param webView The PhoneGap WebView + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackId The callback id used when calling back into JavaScript. + * @return A PluginResult object with a status and message. */ - public void setView(WebView webView) { - this.webView = webView; - } - - /** - * Executes the request and returns CommandResult. - * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. - */ - public PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; @@ -69,28 +55,6 @@ public class TempListener implements SensorEventListener, Plugin { } return new PluginResult(status, result); } - - /** - * Identifies if action to be executed returns a value and should be run synchronously. - * - * @param action The action to execute - * @return T=returns value - */ - public boolean isSynch(String action) { - return false; - } - - /** - * Called when the system is about to start resuming a previous activity. - */ - public void onPause() { - } - - /** - * Called when the activity will start interacting with the user. - */ - public void onResume() { - } /** * Called by AccelBroker when listener is to be shut down. @@ -100,18 +64,6 @@ public class TempListener implements SensorEventListener, Plugin { this.stop(); } - /** - * Called when an activity you launched exits, giving you the requestCode you started it with, - * the resultCode it returned, and any additional data from it. - * - * @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"). - */ - public void onActivityResult(int requestCode, int resultCode, Intent intent) { - } - //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- @@ -135,7 +87,7 @@ public class TempListener implements SensorEventListener, Plugin { public void onSensorChanged(SensorEvent event) { // We want to know what temp this is. float temp = event.values[0]; - this.ctx.sendJavascript("gotTemp(" + temp + ");"); + this.sendJavascript("gotTemp(" + temp + ");"); } } \ No newline at end of file diff --git a/framework/src/com/phonegap/api/IPlugin.java b/framework/src/com/phonegap/api/IPlugin.java new file mode 100755 index 00000000..2bb4e4db --- /dev/null +++ b/framework/src/com/phonegap/api/IPlugin.java @@ -0,0 +1,75 @@ +package com.phonegap.api; + +import org.json.JSONArray; +import com.phonegap.DroidGap; +import android.content.Intent; +import android.webkit.WebView; + +/** + * Plugin interface must be implemented by any plugin classes. + * + * The execute method is called by the PluginManager. + */ +public interface IPlugin { + + /** + * Executes the request and returns PluginResult. + * + * @param action The action to execute. + * @param args JSONArry 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. + */ + PluginResult execute(String action, JSONArray args, String callbackId); + + /** + * Identifies if action to be executed returns a value and should be run synchronously. + * + * @param action The action to execute + * @return T=returns value + */ + public boolean isSynch(String action); + + /** + * 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. + */ + void setContext(DroidGap ctx); + + /** + * Sets the main View of the application, this is the WebView within which + * a PhoneGap app runs. + * + * @param webView The PhoneGap WebView + */ + void setView(WebView webView); + + /** + * Called when the system is about to start resuming a previous activity. + */ + void onPause(); + + /** + * Called when the activity will start interacting with the user. + */ + void onResume(); + + /** + * The final call you receive before your activity is destroyed. + */ + void onDestroy(); + + /** + * Called when an activity you launched exits, giving you the requestCode you started it with, + * the resultCode it returned, and any additional data from it. + * + * @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"). + */ + void onActivityResult(int requestCode, int resultCode, Intent intent); + +} diff --git a/framework/src/com/phonegap/api/Plugin.java b/framework/src/com/phonegap/api/Plugin.java index 78a97ca3..5eb19ddb 100755 --- a/framework/src/com/phonegap/api/Plugin.java +++ b/framework/src/com/phonegap/api/Plugin.java @@ -1,10 +1,7 @@ package com.phonegap.api; import org.json.JSONArray; - import com.phonegap.DroidGap; - -import android.content.Context; import android.content.Intent; import android.webkit.WebView; @@ -13,15 +10,20 @@ import android.webkit.WebView; * * The execute method is called by the PluginManager. */ -public interface Plugin { +public abstract class Plugin implements IPlugin { + + public WebView webView; // WebView object + public DroidGap ctx; // DroidGap object + /** * Executes the request and returns PluginResult. * - * @param action The action to execute. - * @param args JSONArry of arguments for the plugin. - * @return A PluginResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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. */ - PluginResult execute(String action, JSONArray args); + public abstract PluginResult execute(String action, JSONArray args, String callbackId); /** * Identifies if action to be executed returns a value and should be run synchronously. @@ -29,7 +31,9 @@ public interface Plugin { * @param action The action to execute * @return T=returns value */ - public boolean isSynch(String action); + public boolean isSynch(String action) { + return false; + } /** * Sets the context of the Plugin. This can then be used to do things like @@ -37,7 +41,9 @@ public interface Plugin { * * @param ctx The context of the main Activity. */ - void setContext(DroidGap ctx); + public void setContext(DroidGap ctx) { + this.ctx = ctx; + } /** * Sets the main View of the application, this is the WebView within which @@ -45,22 +51,27 @@ public interface Plugin { * * @param webView The PhoneGap WebView */ - void setView(WebView webView); - + public void setView(WebView webView) { + this.webView = webView; + } + /** * Called when the system is about to start resuming a previous activity. */ - void onPause(); + public void onPause() { + } /** * Called when the activity will start interacting with the user. */ - void onResume(); + public void onResume() { + } /** * The final call you receive before your activity is destroyed. */ - void onDestroy(); + public void onDestroy() { + } /** * Called when an activity you launched exits, giving you the requestCode you started it with, @@ -71,6 +82,40 @@ public interface Plugin { * @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"). */ - void onActivityResult(int requestCode, int resultCode, Intent intent); + public void onActivityResult(int requestCode, int resultCode, Intent intent) { + } + /** + * Send generic JavaScript statement back to JavaScript. + * success(...) and error(...) should be used instead where possible. + * + * @param statement + */ + public void sendJavascript(String statement) { + this.ctx.callbackServer.sendJavascript(statement); + } + + /** + * Call the JavaScript success callback for this plugin. + * + * This can be used if the execute code for the plugin is asynchronous meaning + * that execute should return null and the callback from the async operation can + * call success(...) or error(...) + * + * @param pluginResult The result to return. + * @param callbackId The callback id used when calling back into JavaScript. + */ + public void success(PluginResult pluginResult, String callbackId) { + this.ctx.callbackServer.sendJavascript(pluginResult.toSuccessCallbackString(callbackId)); + } + + /** + * Call the JavaScript error callback for this plugin. + * + * @param pluginResult The result to return. + * @param callbackId The callback id used when calling back into JavaScript. + */ + public void error(PluginResult pluginResult, String callbackId) { + this.ctx.callbackServer.sendJavascript(pluginResult.toErrorCallbackString(callbackId)); + } } diff --git a/framework/src/com/phonegap/api/PluginManager.java b/framework/src/com/phonegap/api/PluginManager.java index 00b1358c..2addf3af 100755 --- a/framework/src/com/phonegap/api/PluginManager.java +++ b/framework/src/com/phonegap/api/PluginManager.java @@ -57,6 +57,7 @@ public final class PluginManager { * * @return JSON encoded string with a response message and status. */ + @SuppressWarnings("unchecked") public String exec(final String service, final String action, final String callbackId, final String jsonArgs, final boolean async) { System.out.println("PluginManager.exec("+service+", "+action+", "+callbackId+", "+jsonArgs+", "+async+")"); PluginResult cr = null; @@ -68,20 +69,25 @@ public final class PluginManager { if (clazz != null) { c = getClassByName(clazz); } - if ((c == null) || isPhoneGapPlugin(c)) { - final Plugin plugin = this.addPlugin(clazz); + if (isPhoneGapPlugin(c)) { + final Plugin plugin = this.addPlugin(clazz, c); final DroidGap ctx = this.ctx; runAsync = async && !plugin.isSynch(action); - if (async && !plugin.isSynch(action)) { + if (runAsync) { // 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 - PluginResult cr = plugin.execute(action, args); - // Check the status for 0 (success) or otherwise - if (cr.getStatus() == 0) { - ctx.sendJavascript(cr.toSuccessCallbackString(callbackId)); - } else { + try { + // Call execute on the plugin so that it can do it's thing + PluginResult cr = plugin.execute(action, args, callbackId); + // Check the status for 0 (success) or otherwise + if (cr.getStatus() == 0) { + ctx.sendJavascript(cr.toSuccessCallbackString(callbackId)); + } else { + ctx.sendJavascript(cr.toErrorCallbackString(callbackId)); + } + } catch (Exception e) { + PluginResult cr = new PluginResult(PluginResult.Status.ERROR); ctx.sendJavascript(cr.toErrorCallbackString(callbackId)); } } @@ -90,7 +96,7 @@ public final class PluginManager { return ""; } else { // Call execute on the plugin so that it can do it's thing - cr = plugin.execute(action, args); + cr = plugin.execute(action, args, callbackId); } } } catch (ClassNotFoundException e) { @@ -101,6 +107,9 @@ public final class PluginManager { } // if async we have already returned at this point unless there was an error... if (runAsync) { + if (cr == null) { + cr = new PluginResult(PluginResult.Status.CLASS_NOT_FOUND_EXCEPTION); + } ctx.sendJavascript(cr.toErrorCallbackString(callbackId)); } if (cr != null) { @@ -116,6 +125,7 @@ public final class PluginManager { * @return * @throws ClassNotFoundException */ + @SuppressWarnings("unchecked") private Class getClassByName(final String clazz) throws ClassNotFoundException { return Class.forName(clazz); } @@ -127,32 +137,56 @@ public final class PluginManager { * @param c The class to check the interfaces of. * @return Boolean indicating if the class implements com.phonegap.api.Plugin */ + @SuppressWarnings("unchecked") private boolean isPhoneGapPlugin(Class c) { - boolean isPlugin = false; - Class[] interfaces = c.getInterfaces(); - for (int j=0; j