From 68146329b963931a13bf4f3fbb8d32fda9567d3e Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Mon, 4 Oct 2010 23:58:14 -0500 Subject: [PATCH 1/9] Add IPlugin interface and change Plugin to be abstract class. Plugins can either implement IPlugin or extend Plugin. --- framework/src/com/phonegap/AccelListener.java | 43 +---------- framework/src/com/phonegap/AudioHandler.java | 52 +------------ framework/src/com/phonegap/AudioPlayer.java | 22 +++--- .../src/com/phonegap/CameraLauncher.java | 63 ++-------------- .../src/com/phonegap/CompassListener.java | 43 +---------- .../src/com/phonegap/ContactManager.java | 70 +---------------- framework/src/com/phonegap/CryptoHandler.java | 75 +------------------ framework/src/com/phonegap/Device.java | 50 +------------ framework/src/com/phonegap/DroidGap.java | 4 +- framework/src/com/phonegap/FileUtils.java | 60 +-------------- framework/src/com/phonegap/GeoBroker.java | 56 +------------- framework/src/com/phonegap/GeoListener.java | 19 ++--- .../src/com/phonegap/NetworkManager.java | 55 +------------- framework/src/com/phonegap/Notification.java | 60 +-------------- framework/src/com/phonegap/Storage.java | 64 +--------------- framework/src/com/phonegap/TempListener.java | 55 +------------- framework/src/com/phonegap/api/IPlugin.java | 74 ++++++++++++++++++ framework/src/com/phonegap/api/Plugin.java | 44 ++++++++--- 18 files changed, 161 insertions(+), 748 deletions(-) create mode 100755 framework/src/com/phonegap/api/IPlugin.java diff --git a/framework/src/com/phonegap/AccelListener.java b/framework/src/com/phonegap/AccelListener.java index b24261d0..2b3eb0ba 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,20 +54,10 @@ 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. - * - * @param webView The PhoneGap WebView - */ - public void setView(WebView webView) { - this.webView = webView; - } - /** * Executes the request and returns CommandResult. * @@ -171,18 +156,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 +165,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..1d0717cc 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 @@ -40,26 +34,6 @@ public class AudioHandler implements Plugin { this.players = new HashMap(); } - /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. - * - * @param ctx The context of the main Activity. - */ - 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. * @@ -118,18 +92,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 +106,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..8f885f10 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 @@ -49,26 +45,6 @@ public class CameraLauncher implements Plugin { public CameraLauncher() { } - /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. - * - * @param ctx The context of the main Activity. - */ - 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. * @@ -103,35 +79,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 +182,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 +219,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 +243,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 +257,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..90f5ca3d 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,20 +49,10 @@ 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. - * - * @param webView The PhoneGap WebView - */ - public void setView(WebView webView) { - this.webView = webView; - } - /** * Executes the request and returns CommandResult. * @@ -149,18 +134,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 +142,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..921c3bc2 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"; /** @@ -25,26 +18,6 @@ public class ContactManager implements Plugin { public ContactManager() { } - /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. - * - * @param ctx The context of the main Activity. - */ - 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. * @@ -83,45 +56,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..bb8061ec 100755 --- a/framework/src/com/phonegap/CryptoHandler.java +++ b/framework/src/com/phonegap/CryptoHandler.java @@ -6,40 +6,14 @@ 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. */ public CryptoHandler() { } - /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. - * - * @param ctx The context of the main Activity. - */ - 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. * @@ -64,47 +38,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 +46,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 +55,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..0c10139a 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,20 +51,10 @@ 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. - * - * @param webView The PhoneGap WebView - */ - public void setView(WebView webView) { - this.webView = webView; - } - /** * Executes the request and returns CommandResult. * @@ -113,37 +98,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..d51a1805 100755 --- a/framework/src/com/phonegap/DroidGap.java +++ b/framework/src/com/phonegap/DroidGap.java @@ -279,8 +279,8 @@ public class DroidGap extends Activity { 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); + // TODO: Storage cupcakeStorage = (Storage)this.pluginManager.addPlugin("com.phonegap.Storage"); + // TODO: cupcakeStorage.setStorage(appPackage); } diff --git a/framework/src/com/phonegap/FileUtils.java b/framework/src/com/phonegap/FileUtils.java index 598c23e9..71a43376 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; @@ -39,26 +32,6 @@ public class FileUtils implements Plugin { System.out.println("FileUtils()"); } - /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. - * - * @param ctx The context of the main Activity. - */ - 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. * @@ -161,37 +134,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..3cd52f1d 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; @@ -34,26 +28,6 @@ public class GeoBroker implements Plugin { this.geoListeners = new HashMap(); } - /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. - * - * @param ctx The context of the main Activity. - */ - 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. * @@ -92,18 +66,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 +86,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 +102,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 +123,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..fa650e7b 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,20 +32,10 @@ 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. - * - * @param webView The PhoneGap WebView - */ - public void setView(WebView webView) { - this.webView = webView; - } - /** * Executes the request and returns CommandResult. * @@ -92,37 +76,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 +122,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..c15e9125 100755 --- a/framework/src/com/phonegap/Notification.java +++ b/framework/src/com/phonegap/Notification.java @@ -5,47 +5,22 @@ import org.json.JSONException; import com.phonegap.api.Plugin; import com.phonegap.api.PluginResult; import android.content.Context; -import android.content.Intent; 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. */ public Notification() { } - /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. - * - * @param ctx The context of the main Activity. - */ - 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. * @@ -80,37 +55,6 @@ public class Notification 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 //-------------------------------------------------------------------------- diff --git a/framework/src/com/phonegap/Storage.java b/framework/src/com/phonegap/Storage.java index 46a8c63c..fcf16ff0 100644 --- 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; @@ -29,26 +24,6 @@ public class Storage implements Plugin { public Storage() { } - /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. - * - * @param ctx The context of the main Activity. - */ - 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. * @@ -92,37 +67,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 +90,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 +111,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..62f634e2 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,20 +31,10 @@ 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. - * - * @param webView The PhoneGap WebView - */ - public void setView(WebView webView) { - this.webView = webView; - } - /** * Executes the request and returns CommandResult. * @@ -69,28 +54,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 +63,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 +86,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..c895129b --- /dev/null +++ b/framework/src/com/phonegap/api/IPlugin.java @@ -0,0 +1,74 @@ +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. + * @return A PluginResult object with a status and message. + */ + PluginResult execute(String action, JSONArray args); + + /** + * 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..ccc8a9d7 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,7 +10,11 @@ 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. * @@ -21,7 +22,7 @@ public interface Plugin { * @param args JSONArry of arguments for the plugin. * @return A PluginResult object with a status and message. */ - PluginResult execute(String action, JSONArray args); + public abstract PluginResult execute(String action, JSONArray args); /** * Identifies if action to be executed returns a value and should be run synchronously. @@ -29,7 +30,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 +40,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 +50,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 +81,16 @@ 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 JavaScript statement back to JavaScript. + * + * @param statement + */ + public void sendJavascript(String statement) { + this.ctx.callbackServer.sendJavascript(statement); + } } From 385be26046c65ed383d5ef0c82a5916f7bac0d19 Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Tue, 5 Oct 2010 00:39:57 -0500 Subject: [PATCH 2/9] Check plugin against new Plugin and IPlugin to determine if valid plugin class. --- framework/src/com/phonegap/api/PluginManager.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/framework/src/com/phonegap/api/PluginManager.java b/framework/src/com/phonegap/api/PluginManager.java index 00b1358c..c2e8e658 100755 --- a/framework/src/com/phonegap/api/PluginManager.java +++ b/framework/src/com/phonegap/api/PluginManager.java @@ -101,6 +101,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) { @@ -128,15 +131,16 @@ public final class PluginManager { * @return Boolean indicating if the class implements com.phonegap.api.Plugin */ private boolean isPhoneGapPlugin(Class c) { - boolean isPlugin = false; + if (c.getSuperclass().getName().equals("com.phonegap.api.Plugin")) { + return true; + } Class[] interfaces = c.getInterfaces(); for (int j=0; j Date: Tue, 5 Oct 2010 10:08:48 +0100 Subject: [PATCH 3/9] Update PluginManager to not call Class.forName twice --- .../src/com/phonegap/api/PluginManager.java | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/framework/src/com/phonegap/api/PluginManager.java b/framework/src/com/phonegap/api/PluginManager.java index c2e8e658..ac67ca31 100755 --- a/framework/src/com/phonegap/api/PluginManager.java +++ b/framework/src/com/phonegap/api/PluginManager.java @@ -68,11 +68,11 @@ 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, callbackId); 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() { @@ -131,18 +131,31 @@ public final class PluginManager { * @return Boolean indicating if the class implements com.phonegap.api.Plugin */ private boolean isPhoneGapPlugin(Class c) { - if (c.getSuperclass().getName().equals("com.phonegap.api.Plugin")) { - return true; - } - Class[] interfaces = c.getInterfaces(); - for (int j=0; j Date: Tue, 5 Oct 2010 10:32:00 +0100 Subject: [PATCH 4/9] Add callbackId and JS callback sugar to plugin class and interface --- framework/src/com/phonegap/api/IPlugin.java | 13 ++++++- framework/src/com/phonegap/api/Plugin.java | 42 +++++++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/framework/src/com/phonegap/api/IPlugin.java b/framework/src/com/phonegap/api/IPlugin.java index c895129b..90e55110 100755 --- a/framework/src/com/phonegap/api/IPlugin.java +++ b/framework/src/com/phonegap/api/IPlugin.java @@ -44,7 +44,18 @@ public interface IPlugin { * @param webView The PhoneGap WebView */ void setView(WebView webView); - + + /** + * Sets the callback ID that is required to call a success or error + * JavaScript callback. + * + * The JavaScript callback call looks like this: + * PhoneGap.callbackSuccess(callbackId, { message: 'foo' }); + * + * @param callbackId + */ + void setCallbackId(String callbackId); + /** * Called when the system is about to start resuming a previous activity. */ diff --git a/framework/src/com/phonegap/api/Plugin.java b/framework/src/com/phonegap/api/Plugin.java index ccc8a9d7..2676a2c7 100755 --- a/framework/src/com/phonegap/api/Plugin.java +++ b/framework/src/com/phonegap/api/Plugin.java @@ -13,7 +13,8 @@ import android.webkit.WebView; public abstract class Plugin implements IPlugin { public WebView webView; // WebView object - public DroidGap ctx; // DroidGap object + public DroidGap ctx; // DroidGap object + public String callbackId; // key for the JavaScript callback /** * Executes the request and returns PluginResult. @@ -53,7 +54,20 @@ public abstract class Plugin implements IPlugin { public void setView(WebView webView) { this.webView = webView; } - + + /** + * Sets the callback ID that is required to call a success or error + * JavaScript callback. + * + * The JavaScript callback call looks like this: + * PhoneGap.callbackSuccess(callbackId, { message: 'foo' }); + * + * @param callbackId + */ + public void setCallbackId(String callbackId) { + this.callbackId = callbackId; + } + /** * Called when the system is about to start resuming a previous activity. */ @@ -85,7 +99,8 @@ public abstract class Plugin implements IPlugin { } /** - * Send JavaScript statement back to JavaScript. + * Send generic JavaScript statement back to JavaScript. + * success(...) and error(...) should be used instead where possible. * * @param statement */ @@ -93,4 +108,25 @@ public abstract class Plugin implements IPlugin { 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 + */ + public void success(PluginResult pluginResult) { + this.ctx.callbackServer.sendJavascript(pluginResult.toSuccessCallbackString(this.callbackId)); + } + + /** + * Call the JavaScript error callback for this plugin. + * + * @param pluginResult + */ + public void error(PluginResult pluginResult) { + this.ctx.callbackServer.sendJavascript(pluginResult.toErrorCallbackString(this.callbackId)); + } } From 5a2398dbea6cd7f34f9548ddbe595dba8f720130 Mon Sep 17 00:00:00 2001 From: Dave Johnson Date: Tue, 5 Oct 2010 10:35:56 +0100 Subject: [PATCH 5/9] Add call to setCallbackdId in addPlugin --- framework/src/com/phonegap/api/PluginManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/com/phonegap/api/PluginManager.java b/framework/src/com/phonegap/api/PluginManager.java index ac67ca31..28a21857 100755 --- a/framework/src/com/phonegap/api/PluginManager.java +++ b/framework/src/com/phonegap/api/PluginManager.java @@ -173,7 +173,7 @@ public final class PluginManager { this.plugins.put(className, plugin); plugin.setContext((DroidGap)this.ctx); plugin.setView(this.app); - //plugin.setCallbackId(callbackId); + plugin.setCallbackId(callbackId); return plugin; } catch (Exception e) { From e3cef1662998bf793f4af7269125fcfc66028c75 Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Tue, 5 Oct 2010 10:15:18 -0500 Subject: [PATCH 6/9] Remove getClassForService() and make addPlugin() private. --- .../src/com/phonegap/api/PluginManager.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/framework/src/com/phonegap/api/PluginManager.java b/framework/src/com/phonegap/api/PluginManager.java index 28a21857..637e92dd 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; @@ -119,6 +120,7 @@ public final class PluginManager { * @return * @throws ClassNotFoundException */ + @SuppressWarnings("unchecked") private Class getClassByName(final String clazz) throws ClassNotFoundException { return Class.forName(clazz); } @@ -130,6 +132,7 @@ 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) { if (c != null) { if (c.getSuperclass().getName().equals("com.phonegap.api.Plugin")) { @@ -145,11 +148,17 @@ public final class PluginManager { return false; } + /** + * Add plugin to be loaded and cached. This creates an instance of the plugin. + * If plugin is already created, then just return it. + * + * @param className The class to load + * @return The plugin + */ public Plugin addPlugin(String className) { try { return this.addPlugin(className, this.getClassByName(className), ""); } catch (ClassNotFoundException e) { - // TODO Auto-generated catch block e.printStackTrace(); System.out.println("Error adding plugin "+className+"."); } @@ -157,13 +166,16 @@ public final class PluginManager { } /** - * Add plugin to be loaded and cached. + * Add plugin to be loaded and cached. This creates an instance of the plugin. * If plugin is already created, then just return it. * * @param className The class to load + * @param clazz The class object (must be a class object of the className) + * @param callbackId The callback id to use when calling back into JavaScript * @return The plugin */ - public Plugin addPlugin(String className, Class clazz, String callbackId) { + @SuppressWarnings("unchecked") + private Plugin addPlugin(String className, Class clazz, String callbackId) { if (this.plugins.containsKey(className)) { return this.getPlugin(className); } @@ -196,6 +208,7 @@ public final class PluginManager { /** * Add a class that implements a service. + * This does not create the class instance. It just maps service name to class name. * * @param serviceType * @param className @@ -210,9 +223,9 @@ public final class PluginManager { * @param serviceType * @return */ - private String getClassForService(String serviceType) { - return this.services.get(serviceType); - } + //private String getClassForService(String serviceType) { + // return this.services.get(serviceType); + //} /** * Called when the system is about to start resuming a previous activity. From 23b02e7267e6eb4239204efcb4a7101d08c07b0d Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Tue, 5 Oct 2010 10:16:08 -0500 Subject: [PATCH 7/9] Change initialization of Storage for 1.x devices to use service call. --- framework/src/com/phonegap/DroidGap.java | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index d51a1805..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(); - // TODO: Storage cupcakeStorage = (Storage)this.pluginManager.addPlugin("com.phonegap.Storage"); - // TODO: 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. * From 9adb85a64bc51d8d37281f9243a45151070cba22 Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Tue, 5 Oct 2010 20:35:51 -0500 Subject: [PATCH 8/9] Add callbackId to Plugin.execute() so result can be sent back when overlapping calls to same plugin occur. --- framework/src/com/phonegap/AccelListener.java | 11 +++--- framework/src/com/phonegap/AudioHandler.java | 11 +++--- .../src/com/phonegap/CameraLauncher.java | 11 +++--- .../src/com/phonegap/CompassListener.java | 11 +++--- .../src/com/phonegap/ContactManager.java | 11 +++--- framework/src/com/phonegap/CryptoHandler.java | 11 +++--- framework/src/com/phonegap/Device.java | 11 +++--- framework/src/com/phonegap/FileUtils.java | 11 +++--- framework/src/com/phonegap/GeoBroker.java | 11 +++--- .../src/com/phonegap/NetworkManager.java | 11 +++--- framework/src/com/phonegap/Notification.java | 11 +++--- framework/src/com/phonegap/Storage.java | 11 +++--- framework/src/com/phonegap/TempListener.java | 11 +++--- framework/src/com/phonegap/api/IPlugin.java | 20 +++------- framework/src/com/phonegap/api/Plugin.java | 37 +++++++------------ .../src/com/phonegap/api/PluginManager.java | 36 ++++++++---------- 16 files changed, 111 insertions(+), 125 deletions(-) mode change 100644 => 100755 framework/src/com/phonegap/Storage.java diff --git a/framework/src/com/phonegap/AccelListener.java b/framework/src/com/phonegap/AccelListener.java index 2b3eb0ba..fb4005a7 100755 --- a/framework/src/com/phonegap/AccelListener.java +++ b/framework/src/com/phonegap/AccelListener.java @@ -59,13 +59,14 @@ public class AccelListener extends Plugin implements SensorEventListener { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The 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 PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/AudioHandler.java b/framework/src/com/phonegap/AudioHandler.java index 1d0717cc..9beb7a65 100755 --- a/framework/src/com/phonegap/AudioHandler.java +++ b/framework/src/com/phonegap/AudioHandler.java @@ -35,13 +35,14 @@ public class AudioHandler extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The 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 PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/CameraLauncher.java b/framework/src/com/phonegap/CameraLauncher.java index 8f885f10..dab270c3 100755 --- a/framework/src/com/phonegap/CameraLauncher.java +++ b/framework/src/com/phonegap/CameraLauncher.java @@ -46,13 +46,14 @@ public class CameraLauncher extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The 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 PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/CompassListener.java b/framework/src/com/phonegap/CompassListener.java index 90f5ca3d..88e96b89 100755 --- a/framework/src/com/phonegap/CompassListener.java +++ b/framework/src/com/phonegap/CompassListener.java @@ -54,13 +54,14 @@ public class CompassListener extends Plugin implements SensorEventListener { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The 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 PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/ContactManager.java b/framework/src/com/phonegap/ContactManager.java index 921c3bc2..00ad3e12 100755 --- a/framework/src/com/phonegap/ContactManager.java +++ b/framework/src/com/phonegap/ContactManager.java @@ -19,13 +19,14 @@ public class ContactManager extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The 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 PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { if (contactAccessor == null) { contactAccessor = ContactAccessor.getInstance(webView, ctx); } diff --git a/framework/src/com/phonegap/CryptoHandler.java b/framework/src/com/phonegap/CryptoHandler.java index bb8061ec..746433c8 100755 --- a/framework/src/com/phonegap/CryptoHandler.java +++ b/framework/src/com/phonegap/CryptoHandler.java @@ -15,13 +15,14 @@ public class CryptoHandler extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The 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 PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/Device.java b/framework/src/com/phonegap/Device.java index 0c10139a..a4cebbf4 100755 --- a/framework/src/com/phonegap/Device.java +++ b/framework/src/com/phonegap/Device.java @@ -56,13 +56,14 @@ public class Device extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The 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 PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/FileUtils.java b/framework/src/com/phonegap/FileUtils.java index 71a43376..094fa08a 100755 --- a/framework/src/com/phonegap/FileUtils.java +++ b/framework/src/com/phonegap/FileUtils.java @@ -33,13 +33,14 @@ public class FileUtils extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The 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 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+")"); diff --git a/framework/src/com/phonegap/GeoBroker.java b/framework/src/com/phonegap/GeoBroker.java index 3cd52f1d..6dafc401 100755 --- a/framework/src/com/phonegap/GeoBroker.java +++ b/framework/src/com/phonegap/GeoBroker.java @@ -29,13 +29,14 @@ public class GeoBroker extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The 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 PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/NetworkManager.java b/framework/src/com/phonegap/NetworkManager.java index fa650e7b..256d34e3 100755 --- a/framework/src/com/phonegap/NetworkManager.java +++ b/framework/src/com/phonegap/NetworkManager.java @@ -37,13 +37,14 @@ public class NetworkManager extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The 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 PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { diff --git a/framework/src/com/phonegap/Notification.java b/framework/src/com/phonegap/Notification.java index c15e9125..e999c3b9 100755 --- a/framework/src/com/phonegap/Notification.java +++ b/framework/src/com/phonegap/Notification.java @@ -22,13 +22,14 @@ public class Notification extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The 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 PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/Storage.java b/framework/src/com/phonegap/Storage.java old mode 100644 new mode 100755 index fcf16ff0..a3339306 --- a/framework/src/com/phonegap/Storage.java +++ b/framework/src/com/phonegap/Storage.java @@ -25,13 +25,14 @@ public class Storage extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The 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 PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/TempListener.java b/framework/src/com/phonegap/TempListener.java index 62f634e2..5370421f 100644 --- a/framework/src/com/phonegap/TempListener.java +++ b/framework/src/com/phonegap/TempListener.java @@ -36,13 +36,14 @@ public class TempListener extends Plugin implements SensorEventListener { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The 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 PluginResult execute(String action, JSONArray args) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/api/IPlugin.java b/framework/src/com/phonegap/api/IPlugin.java index 90e55110..2bb4e4db 100755 --- a/framework/src/com/phonegap/api/IPlugin.java +++ b/framework/src/com/phonegap/api/IPlugin.java @@ -15,11 +15,12 @@ public interface IPlugin { /** * 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); + PluginResult execute(String action, JSONArray args, String callbackId); /** * Identifies if action to be executed returns a value and should be run synchronously. @@ -45,17 +46,6 @@ public interface IPlugin { */ void setView(WebView webView); - /** - * Sets the callback ID that is required to call a success or error - * JavaScript callback. - * - * The JavaScript callback call looks like this: - * PhoneGap.callbackSuccess(callbackId, { message: 'foo' }); - * - * @param callbackId - */ - void setCallbackId(String callbackId); - /** * Called when the system is about to start resuming a previous activity. */ diff --git a/framework/src/com/phonegap/api/Plugin.java b/framework/src/com/phonegap/api/Plugin.java index 2676a2c7..5eb19ddb 100755 --- a/framework/src/com/phonegap/api/Plugin.java +++ b/framework/src/com/phonegap/api/Plugin.java @@ -14,16 +14,16 @@ public abstract class Plugin implements IPlugin { public WebView webView; // WebView object public DroidGap ctx; // DroidGap object - public String callbackId; // key for the JavaScript callback /** * 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. */ - public abstract 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. @@ -55,19 +55,6 @@ public abstract class Plugin implements IPlugin { this.webView = webView; } - /** - * Sets the callback ID that is required to call a success or error - * JavaScript callback. - * - * The JavaScript callback call looks like this: - * PhoneGap.callbackSuccess(callbackId, { message: 'foo' }); - * - * @param callbackId - */ - public void setCallbackId(String callbackId) { - this.callbackId = callbackId; - } - /** * Called when the system is about to start resuming a previous activity. */ @@ -115,18 +102,20 @@ public abstract class Plugin implements IPlugin { * that execute should return null and the callback from the async operation can * call success(...) or error(...) * - * @param pluginResult + * @param pluginResult The result to return. + * @param callbackId The callback id used when calling back into JavaScript. */ - public void success(PluginResult pluginResult) { - this.ctx.callbackServer.sendJavascript(pluginResult.toSuccessCallbackString(this.callbackId)); + public void success(PluginResult pluginResult, String callbackId) { + this.ctx.callbackServer.sendJavascript(pluginResult.toSuccessCallbackString(callbackId)); } /** * Call the JavaScript error callback for this plugin. * - * @param pluginResult + * @param pluginResult The result to return. + * @param callbackId The callback id used when calling back into JavaScript. */ - public void error(PluginResult pluginResult) { - this.ctx.callbackServer.sendJavascript(pluginResult.toErrorCallbackString(this.callbackId)); + 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 637e92dd..2addf3af 100755 --- a/framework/src/com/phonegap/api/PluginManager.java +++ b/framework/src/com/phonegap/api/PluginManager.java @@ -70,19 +70,24 @@ public final class PluginManager { c = getClassByName(clazz); } if (isPhoneGapPlugin(c)) { - final Plugin plugin = this.addPlugin(clazz, c, callbackId); + final Plugin plugin = this.addPlugin(clazz, c); final DroidGap ctx = this.ctx; runAsync = 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)); } } @@ -91,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) { @@ -157,7 +162,7 @@ public final class PluginManager { */ public Plugin addPlugin(String className) { try { - return this.addPlugin(className, this.getClassByName(className), ""); + return this.addPlugin(className, this.getClassByName(className)); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("Error adding plugin "+className+"."); @@ -175,7 +180,7 @@ public final class PluginManager { * @return The plugin */ @SuppressWarnings("unchecked") - private Plugin addPlugin(String className, Class clazz, String callbackId) { + private Plugin addPlugin(String className, Class clazz) { if (this.plugins.containsKey(className)) { return this.getPlugin(className); } @@ -185,7 +190,6 @@ public final class PluginManager { this.plugins.put(className, plugin); plugin.setContext((DroidGap)this.ctx); plugin.setView(this.app); - plugin.setCallbackId(callbackId); return plugin; } catch (Exception e) { @@ -217,16 +221,6 @@ public final class PluginManager { this.services.put(serviceType, className); } - /** - * Get the class that implements a service. - * - * @param serviceType - * @return - */ - //private String getClassForService(String serviceType) { - // return this.services.get(serviceType); - //} - /** * Called when the system is about to start resuming a previous activity. */ From 98206852de3c1a41c30a3749d2bb9d45eab74044 Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Wed, 6 Oct 2010 13:31:30 -0500 Subject: [PATCH 9/9] Update alert() to implement navigator.notification.alert API. This update is from janmonschke (Jan Monschke). --- framework/assets/js/notification.js | 5 ++-- framework/src/com/phonegap/Notification.java | 31 +++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) mode change 100644 => 100755 framework/assets/js/notification.js 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/Notification.java b/framework/src/com/phonegap/Notification.java index e999c3b9..3d76366f 100755 --- a/framework/src/com/phonegap/Notification.java +++ b/framework/src/com/phonegap/Notification.java @@ -4,7 +4,9 @@ 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.DialogInterface; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; @@ -40,6 +42,9 @@ public class Notification extends 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); @@ -53,7 +58,10 @@ public class Notification extends Plugin { * @return T=returns value */ public boolean isSynch(String action) { - return false; + if(action.equals("alert")) + return true; + else + return false; } //-------------------------------------------------------------------------- @@ -98,5 +106,26 @@ public class Notification extends 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(); + } }