mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-01 02:12:58 +08:00
Update classes to use module, and make constructors consistent.
This commit is contained in:
parent
5d83a44ec3
commit
40997b4cb8
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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<String,AudioPlayer> 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<String,AudioPlayer>();
|
||||
@ -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<Entry<String,AudioPlayer>> s = this.players.entrySet();
|
||||
java.util.Iterator<Entry<String,AudioPlayer>> it = s.iterator();
|
||||
while(it.hasNext()) {
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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<Entry<String,Module>> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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<String, GeoListener> geoListeners;
|
||||
@ -20,6 +20,7 @@ public class GeoBroker {
|
||||
|
||||
public GeoBroker(WebView view, DroidGap ctx)
|
||||
{
|
||||
super(view, ctx);
|
||||
mCtx = ctx;
|
||||
mAppView = view;
|
||||
geoListeners = new HashMap<String, GeoListener>();
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user