From 40997b4cb88311ab8cbf6c0a4be8b638afcf163a Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Fri, 3 Sep 2010 17:24:55 -0500 Subject: [PATCH] Update classes to use module, and make constructors consistent. --- framework/src/com/phonegap/AccelListener.java | 7 +- framework/src/com/phonegap/AudioHandler.java | 9 ++- .../src/com/phonegap/CompassListener.java | 9 ++- .../src/com/phonegap/ContactManager.java | 5 +- framework/src/com/phonegap/CryptoHandler.java | 5 +- framework/src/com/phonegap/DroidGap.java | 77 +++++++------------ framework/src/com/phonegap/FileUtils.java | 5 +- framework/src/com/phonegap/GeoBroker.java | 3 +- .../src/com/phonegap/NetworkManager.java | 7 +- framework/src/com/phonegap/Storage.java | 5 +- framework/src/com/phonegap/TempListener.java | 5 +- 11 files changed, 63 insertions(+), 74 deletions(-) diff --git a/framework/src/com/phonegap/AccelListener.java b/framework/src/com/phonegap/AccelListener.java index d25fef79..e6f385d4 100644 --- a/framework/src/com/phonegap/AccelListener.java +++ b/framework/src/com/phonegap/AccelListener.java @@ -13,7 +13,7 @@ import android.webkit.WebView; * This class listens to the accelerometer sensor and stores the latest * acceleration values x,y,z. */ -public class AccelListener implements SensorEventListener{ +public class AccelListener extends Module implements SensorEventListener{ public static int STOPPED = 0; public static int STARTING = 1; @@ -40,6 +40,7 @@ public class AccelListener implements SensorEventListener{ * @param appView */ public AccelListener(WebView appView, DroidGap ctx) { + super(appView, ctx); this.mCtx = ctx; this.mAppView = appView; this.sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE); @@ -95,7 +96,9 @@ public class AccelListener implements SensorEventListener{ * Called by AccelBroker when listener is to be shut down. * Stop listener. */ - public void destroy() { + @Override + public void onDestroy() { + super.onDestroy(); this.stop(); } diff --git a/framework/src/com/phonegap/AudioHandler.java b/framework/src/com/phonegap/AudioHandler.java index 55e29bf5..81727fc8 100755 --- a/framework/src/com/phonegap/AudioHandler.java +++ b/framework/src/com/phonegap/AudioHandler.java @@ -18,7 +18,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 { +public class AudioHandler extends Module { HashMap players; // Audio player object WebView mAppView; // Webview object @@ -30,7 +30,8 @@ public class AudioHandler { * @param view * @param ctx */ - AudioHandler(WebView view, DroidGap ctx) { + public AudioHandler(WebView view, DroidGap ctx) { + super(view, ctx); this.mAppView = view; this.mCtx = ctx; this.players = new HashMap(); @@ -39,7 +40,9 @@ public class AudioHandler { /** * Stop all audio players and recorders. */ - public void destroy() { + @Override + public void onDestroy() { + super.onDestroy(); java.util.Set> s = this.players.entrySet(); java.util.Iterator> it = s.iterator(); while(it.hasNext()) { diff --git a/framework/src/com/phonegap/CompassListener.java b/framework/src/com/phonegap/CompassListener.java index ed314f9a..3b9b4dfa 100644 --- a/framework/src/com/phonegap/CompassListener.java +++ b/framework/src/com/phonegap/CompassListener.java @@ -12,7 +12,7 @@ import android.webkit.WebView; /** * This class listens to the compass sensor and stores the latest heading value. */ -public class CompassListener implements SensorEventListener{ +public class CompassListener extends Module implements SensorEventListener{ public static int STOPPED = 0; public static int STARTING = 1; @@ -38,8 +38,8 @@ public class CompassListener implements SensorEventListener{ * @param appView * @param ctx The Activity (DroidGap) object */ - CompassListener(WebView appView, DroidGap ctx) - { + public CompassListener(WebView appView, DroidGap ctx) { + super(appView, ctx); this.mCtx = ctx; this.mAppView = appView; this.sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE); @@ -91,7 +91,8 @@ public class CompassListener implements SensorEventListener{ /** * Called when listener is to be shut down and object is being destroyed. */ - public void destroy() { + @Override + public void onDestroy() { this.stop(); } diff --git a/framework/src/com/phonegap/ContactManager.java b/framework/src/com/phonegap/ContactManager.java index c161eb93..fcbdab3c 100644 --- a/framework/src/com/phonegap/ContactManager.java +++ b/framework/src/com/phonegap/ContactManager.java @@ -11,7 +11,7 @@ import android.database.Cursor; import android.database.sqlite.SQLiteException; @SuppressWarnings("deprecation") -public class ContactManager { +public class ContactManager extends Module { public class ContactTriplet { @@ -27,8 +27,9 @@ public class ContactManager { Uri mPhone = android.provider.Contacts.Phones.CONTENT_URI; Uri mEmail = android.provider.Contacts.ContactMethods.CONTENT_URI; - ContactManager(WebView view, DroidGap app) + public ContactManager(WebView view, DroidGap app) { + super(view, app); mApp = app; mView = view; } diff --git a/framework/src/com/phonegap/CryptoHandler.java b/framework/src/com/phonegap/CryptoHandler.java index ac6b26cc..9d0d2579 100644 --- a/framework/src/com/phonegap/CryptoHandler.java +++ b/framework/src/com/phonegap/CryptoHandler.java @@ -2,12 +2,13 @@ package com.phonegap; import android.webkit.WebView; -public class CryptoHandler { +public class CryptoHandler extends Module { WebView mView; - CryptoHandler(WebView view) + public CryptoHandler(WebView view, DroidGap gap) { + super(view, gap); mView = view; } diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index 194ee087..3f3a81fa 100755 --- a/framework/src/com/phonegap/DroidGap.java +++ b/framework/src/com/phonegap/DroidGap.java @@ -86,16 +86,7 @@ public class DroidGap extends Activity { private LinearLayout root; private Device gap; - private GeoBroker geo; - private AccelListener accel; - private ContactManager mContacts; - private FileUtils fs; - private NetworkManager netMan; - private CompassListener mCompass; - private Storage cupcakeStorage; - private CryptoHandler crypto; private BrowserKey mKey; - private AudioHandler audio; public CallbackServer callbackServer; private CommandManager commandManager; @@ -186,8 +177,9 @@ public class DroidGap extends Activity { WebViewReflect.setGeolocationEnabled(settings, true); // Bind the appView object to the gap class methods bindBrowser(appView); - if (cupcakeStorage != null) { - cupcakeStorage.setStorage(appPackage); + if (this.getModule("com.phonegap.Storage") != null) { + Storage cupcakeStorage = (Storage)this.getModule("com.phonegap.Storage"); + cupcakeStorage.setStorage(appPackage); } } @@ -262,30 +254,9 @@ public class DroidGap extends Activity { appView.loadUrl("about:blank"); // Clean up objects - if (accel != null) { - accel.destroy(); - } - if (mContacts != null) { - - } - if (fs != null) { - - } - if (netMan != null) { - - } - if (mCompass != null) { - mCompass.destroy(); - } - if (crypto != null) { - - } if (mKey != null) { } - if (audio != null) { - audio.destroy(); - } // Clean up modules java.util.Set> s = this.modules.entrySet(); @@ -305,36 +276,27 @@ public class DroidGap extends Activity { callbackServer = new CallbackServer(); commandManager = new CommandManager(appView, this); gap = new Device(appView, this); - accel = new AccelListener(appView, this); - mContacts = new ContactManager(appView, this); - fs = new FileUtils(appView); - netMan = new NetworkManager(appView, this); - mCompass = new CompassListener(appView, this); - crypto = new CryptoHandler(appView); mKey = new BrowserKey(appView, this); - audio = new AudioHandler(appView, this); // This creates the new javascript interfaces for PhoneGap appView.addJavascriptInterface(commandManager, "CommandManager"); appView.addJavascriptInterface(gap, "DroidGap"); - appView.addJavascriptInterface(accel, "Accel"); + this.addModule("com.phonegap.AccelListener", "Accel"); this.addModule("com.phonegap.CameraLauncher", "GapCam"); - appView.addJavascriptInterface(mContacts, "ContactHook"); - appView.addJavascriptInterface(fs, "FileUtil"); - appView.addJavascriptInterface(netMan, "NetworkManager"); - appView.addJavascriptInterface(mCompass, "CompassHook"); - appView.addJavascriptInterface(crypto, "GapCrypto"); + this.addModule("com.phonegap.ContactManager", "ContactHook"); + this.addModule("com.phonegap.FileUtils", "FileUtil"); + this.addModule("com.phonegap.NetworkManager", "NetworkManager"); + this.addModule("com.phonegap.CompassListener", "CompassHook"); + this.addModule("com.phonegap.CryptoHandler", "GapCrypto"); appView.addJavascriptInterface(mKey, "BackButton"); - appView.addJavascriptInterface(audio, "GapAudio"); + this.addModule("com.phonegap.AudioHandler", "GapAudio"); appView.addJavascriptInterface(callbackServer, "CallbackServer"); appView.addJavascriptInterface(new SplashScreen(this), "SplashScreen"); if (android.os.Build.VERSION.RELEASE.startsWith("1.")) { - cupcakeStorage = new Storage(appView, this); - geo = new GeoBroker(appView, this); - appView.addJavascriptInterface(cupcakeStorage, "droidStorage"); - appView.addJavascriptInterface(geo, "Geo"); + this.addModule("com.phonegap.Storage", "droidStorage"); + this.addModule("com.phonegap.GeoBroker", "Geo"); } } @@ -346,7 +308,7 @@ public class DroidGap extends Activity { * @param javascriptInterface Bind the object to Javascript so that the methods can be * accessed from Javascript using this variable name. */ - public void addModule(String className, String javascriptInterface) { + public Object addModule(String className, String javascriptInterface) { System.out.println("DroidGap.addModule("+className+", "+javascriptInterface+")"); try { Class cl = Class.forName(className); @@ -362,11 +324,24 @@ public class DroidGap extends Activity { if (javascriptInterface != null) { this.appView.addJavascriptInterface(module, javascriptInterface); } + return module; } catch (Exception e) { e.printStackTrace(); System.out.println("Error adding module "+className+"."); } + return null; + } + + /** + * Get the loaded module. + * + * @param className The class of the loaded module. + * @return + */ + public Object getModule(String className) { + Object module = this.modules.get(className); + return module; } /** diff --git a/framework/src/com/phonegap/FileUtils.java b/framework/src/com/phonegap/FileUtils.java index 420a1f0b..bc6b3920 100644 --- a/framework/src/com/phonegap/FileUtils.java +++ b/framework/src/com/phonegap/FileUtils.java @@ -4,15 +4,16 @@ import java.io.*; import android.webkit.WebView; -public class FileUtils { +public class FileUtils extends Module { WebView mView; FileReader f_in; FileWriter f_out; - public FileUtils(WebView view) + public FileUtils(WebView view, DroidGap gap) { + super(view, gap); mView = view; } diff --git a/framework/src/com/phonegap/GeoBroker.java b/framework/src/com/phonegap/GeoBroker.java index 46327c5f..512fa0f1 100644 --- a/framework/src/com/phonegap/GeoBroker.java +++ b/framework/src/com/phonegap/GeoBroker.java @@ -12,7 +12,7 @@ import android.webkit.WebView; * This class only starts and stops various GeoListeners, which consist of a GPS and a Network Listener */ -public class GeoBroker { +public class GeoBroker extends Module { private WebView mAppView; private DroidGap mCtx; private HashMap geoListeners; @@ -20,6 +20,7 @@ public class GeoBroker { public GeoBroker(WebView view, DroidGap ctx) { + super(view, ctx); mCtx = ctx; mAppView = view; geoListeners = new HashMap(); diff --git a/framework/src/com/phonegap/NetworkManager.java b/framework/src/com/phonegap/NetworkManager.java index 5d4c0196..c13df3ac 100644 --- a/framework/src/com/phonegap/NetworkManager.java +++ b/framework/src/com/phonegap/NetworkManager.java @@ -7,14 +7,15 @@ import android.content.Context; import android.net.*; import android.webkit.WebView; -public class NetworkManager { +public class NetworkManager extends Module { - Context mCtx; + DroidGap mCtx; WebView mView; ConnectivityManager sockMan; - NetworkManager(WebView view, Context ctx) + public NetworkManager(WebView view, DroidGap ctx) { + super(view, ctx); mCtx = ctx; mView = view; sockMan = (ConnectivityManager) mCtx.getSystemService(Context.CONNECTIVITY_SERVICE); diff --git a/framework/src/com/phonegap/Storage.java b/framework/src/com/phonegap/Storage.java index a0930996..9b010d53 100644 --- a/framework/src/com/phonegap/Storage.java +++ b/framework/src/com/phonegap/Storage.java @@ -5,7 +5,7 @@ import android.database.sqlite.*; import android.util.Log; import android.webkit.WebView; -public class Storage { +public class Storage extends Module { private static final String LOG_TAG = "SQLite Storage:"; SQLiteDatabase myDb; @@ -14,7 +14,8 @@ public class Storage { WebView appView; DroidGap mCtx; - Storage(WebView view, DroidGap ctx) { + public Storage(WebView view, DroidGap ctx) { + super(view, ctx); appView = view; mCtx = ctx; } diff --git a/framework/src/com/phonegap/TempListener.java b/framework/src/com/phonegap/TempListener.java index c9950906..0d5a47bd 100644 --- a/framework/src/com/phonegap/TempListener.java +++ b/framework/src/com/phonegap/TempListener.java @@ -9,14 +9,15 @@ import android.hardware.SensorManager; import android.content.Context; import android.webkit.WebView; -public class TempListener implements SensorEventListener { +public class TempListener extends Module implements SensorEventListener { WebView mAppView; DroidGap mCtx; Sensor mSensor; private SensorManager sensorManager; - TempListener(DroidGap ctx, WebView appView) { + public TempListener(WebView appView, DroidGap ctx) { + super(appView, ctx); mCtx = ctx; mAppView = appView; sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);