Change loadUrl(javascript...) to use new callback mechanism.

This commit is contained in:
Bryce Curtis 2010-08-19 14:37:49 -05:00
parent 1850d2c04e
commit b4d3a10773
10 changed files with 104 additions and 118 deletions

View File

@ -2,7 +2,6 @@ package com.phonegap;
import java.util.HashMap; import java.util.HashMap;
import android.content.Context;
import android.webkit.WebView; import android.webkit.WebView;
/** /**
@ -21,7 +20,7 @@ public class AccelBroker {
public static int ERROR_NOT_FOUND = 4; public static int ERROR_NOT_FOUND = 4;
private WebView mAppView; // WebView object private WebView mAppView; // WebView object
private Context mCtx; // Activity (DroidGap) object private DroidGap mCtx; // DroidGap object
private AccelListener listener; // Accelerator listener private AccelListener listener; // Accelerator listener
private HashMap<String,Integer> listenerIds; // List of listener ids private HashMap<String,Integer> listenerIds; // List of listener ids
@ -31,7 +30,7 @@ public class AccelBroker {
* @param view * @param view
* @param ctx * @param ctx
*/ */
public AccelBroker(WebView view, Context ctx) public AccelBroker(WebView view, DroidGap ctx)
{ {
mCtx = ctx; mCtx = ctx;
mAppView = view; mAppView = view;

View File

@ -16,7 +16,7 @@ import android.webkit.WebView;
public class AccelListener implements SensorEventListener{ public class AccelListener implements SensorEventListener{
WebView mAppView; // WebView object WebView mAppView; // WebView object
Context mCtx; // Activity (DroidGap) object DroidGap mCtx; // DroidGap object
float x,y,z; // most recent acceleration values float x,y,z; // most recent acceleration values
long timeStamp; // time of most recent value long timeStamp; // time of most recent value
@ -31,7 +31,7 @@ public class AccelListener implements SensorEventListener{
* @param ctx The Activity (DroidGap) object * @param ctx The Activity (DroidGap) object
* @param appView * @param appView
*/ */
public AccelListener(Context ctx, WebView appView) { public AccelListener(DroidGap ctx, WebView appView) {
this.mCtx = ctx; this.mCtx = ctx;
this.mAppView = appView; this.mAppView = appView;
this.sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE); this.sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);

View File

@ -37,7 +37,7 @@ public class CameraLauncher {
byte[] code = jpeg_data.toByteArray(); byte[] code = jpeg_data.toByteArray();
byte[] output = Base64.encodeBase64(code); byte[] output = Base64.encodeBase64(code);
String js_out = new String(output); String js_out = new String(output);
mAppView.loadUrl("javascript:navigator.camera.win('" + js_out + "');"); mGap.sendJavascript("navigator.camera.win('" + js_out + "');");
} }
} }
catch(Exception e) catch(Exception e)
@ -49,7 +49,7 @@ public class CameraLauncher {
public void failPicture(String err) public void failPicture(String err)
{ {
mAppView.loadUrl("javascript:navigator.camera.fail('" + err + "');"); mGap.sendJavascript("navigator.camera.fail('" + err + "');");
} }
} }

View File

@ -11,7 +11,7 @@ import android.webkit.WebView;
/** /**
* This class listens to the compass sensor and calls navigator.compass.setHeading(heading) * This class listens to the compass sensor and calls navigator.compass.setHeading(heading)
* method in Javascript every sensor change event it receives. * method in JavaScript every sensor change event it receives.
*/ */
public class CompassListener implements SensorEventListener{ public class CompassListener implements SensorEventListener{
@ -34,9 +34,9 @@ public class CompassListener implements SensorEventListener{
* @param appView * @param appView
* @param ctx The Activity (DroidGap) object * @param ctx The Activity (DroidGap) object
*/ */
CompassListener(WebView appView, Context ctx) CompassListener(WebView appView, DroidGap ctx)
{ {
this.mCtx = (DroidGap)ctx; this.mCtx = ctx;
this.mAppView = appView; this.mAppView = appView;
this.sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE); this.sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);
} }

View File

@ -21,13 +21,13 @@ public class ContactManager {
} }
private static final String LOG_TAG = "Contact Query"; private static final String LOG_TAG = "Contact Query";
Activity mApp; DroidGap mApp;
WebView mView; WebView mView;
Uri mPeople = android.provider.Contacts.People.CONTENT_URI; Uri mPeople = android.provider.Contacts.People.CONTENT_URI;
Uri mPhone = android.provider.Contacts.Phones.CONTENT_URI; Uri mPhone = android.provider.Contacts.Phones.CONTENT_URI;
Uri mEmail = android.provider.Contacts.ContactMethods.CONTENT_URI; Uri mEmail = android.provider.Contacts.ContactMethods.CONTENT_URI;
ContactManager(WebView view, Activity app) ContactManager(WebView view, DroidGap app)
{ {
mApp = app; mApp = app;
mView = view; mView = view;
@ -160,23 +160,28 @@ public class ContactManager {
email = ""; email = "";
// Code for backwards compatibility with the OLD Contacts API // Code for backwards compatibility with the OLD Contacts API
if (all) if (all) {
mView.loadUrl("javascript:navigator.ContactManager.droidAddContact('" + name + "','" + phoneNumber + "','" + email +"')"); mApp.sendJavascript("navigator.ContactManager.droidAddContact('" + name + "','" + phoneNumber + "','" + email +"');");
else }
mView.loadUrl("javascript:navigator.contacts.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +"')"); else {
mApp.sendJavascript("navigator.contacts.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +"');");
}
} while (cur.moveToNext()); } while (cur.moveToNext());
if (all) if (all) {
mView.loadUrl("javascript:navigator.ContactManager.droidDone()"); mApp.sendJavascript("navigator.ContactManager.droidDone();");
else }
mView.loadUrl("javascript:navigator.contacts.droidDone();"); else {
mApp.sendJavascript("navigator.contacts.droidDone();");
}
} }
else else
{ {
if(all) if (all) {
mView.loadUrl("javascript:navigator.ContactManager.fail()"); mApp.sendJavascript("navigator.ContactManager.fail();");
else }
mView.loadUrl("javascript:navigator.contacts.fail('None found!')"); else {
mApp.sendJavascript("navigator.contacts.fail('None found!');");
}
} }
} }
@ -199,10 +204,10 @@ public class ContactManager {
if(data != null) if(data != null)
{ {
data.email = email; data.email = email;
mView.loadUrl("javascript:navigator.Contacts.droidFoundContact('" + data.name + "','" + data.phone + "','" + data.email +"')"); mApp.sendJavascript("navigator.Contacts.droidFoundContact('" + data.name + "','" + data.phone + "','" + data.email +"');");
} }
} while (cur.moveToNext()); } while (cur.moveToNext());
mView.loadUrl("javascript:navigator.contacts.droidDoneContacts();"); mApp.sendJavascript("navigator.contacts.droidDoneContacts();");
} }
} }

View File

@ -244,7 +244,7 @@ public class DroidGap extends Activity {
if (android.os.Build.VERSION.RELEASE.startsWith("1.")) if (android.os.Build.VERSION.RELEASE.startsWith("1."))
{ {
cupcakeStorage = new Storage(appView); cupcakeStorage = new Storage(appView, this);
geo = new GeoBroker(appView, this); geo = new GeoBroker(appView, this);
appView.addJavascriptInterface(cupcakeStorage, "droidStorage"); appView.addJavascriptInterface(cupcakeStorage, "droidStorage");
appView.addJavascriptInterface(geo, "Geo"); appView.addJavascriptInterface(geo, "Geo");

View File

@ -14,11 +14,11 @@ import android.webkit.WebView;
public class GeoBroker { public class GeoBroker {
private WebView mAppView; private WebView mAppView;
private Context mCtx; private DroidGap mCtx;
private HashMap<String, GeoListener> geoListeners; private HashMap<String, GeoListener> geoListeners;
private GeoListener global; private GeoListener global;
public GeoBroker(WebView view, Context ctx) public GeoBroker(WebView view, DroidGap ctx)
{ {
mCtx = ctx; mCtx = ctx;
mAppView = view; mAppView = view;

View File

@ -12,29 +12,29 @@ public class GeoListener {
GpsListener mGps; GpsListener mGps;
NetworkListener mNetwork; NetworkListener mNetwork;
LocationManager mLocMan; LocationManager mLocMan;
Context mCtx; private DroidGap mCtx;
private WebView mAppView; private WebView mAppView;
int interval; int interval;
GeoListener(String i, Context ctx, int time, WebView appView) GeoListener(String i, DroidGap ctx, int time, WebView appView) {
{
id = i; id = i;
interval = time; interval = time;
mCtx = ctx; mCtx = ctx;
mGps = null; mGps = null;
mNetwork = null; mNetwork = null;
mLocMan = (LocationManager) mCtx.getSystemService(Context.LOCATION_SERVICE); mLocMan = (LocationManager) mCtx.getSystemService(Context.LOCATION_SERVICE);
if (mLocMan.getProvider(LocationManager.GPS_PROVIDER) != null) if (mLocMan.getProvider(LocationManager.GPS_PROVIDER) != null) {
mGps = new GpsListener(mCtx, interval, this); mGps = new GpsListener(mCtx, interval, this);
if (mLocMan.getProvider(LocationManager.NETWORK_PROVIDER) != null) }
if (mLocMan.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
mNetwork = new NetworkListener(mCtx, interval, this); mNetwork = new NetworkListener(mCtx, interval, this);
mAppView = appView; }
mAppView = appView;
} }
void success(Location loc) void success(Location loc) {
{
/* /*
* We only need to figure out what we do when we succeed! * We only need to figure out what we do when we succeed!
*/ */
@ -45,49 +45,45 @@ public class GeoListener {
*/ */
params = loc.getLatitude() + "," + loc.getLongitude() + ", " + loc.getAltitude() + "," + loc.getAccuracy() + "," + loc.getBearing(); params = loc.getLatitude() + "," + loc.getLongitude() + ", " + loc.getAltitude() + "," + loc.getAccuracy() + "," + loc.getBearing();
params += "," + loc.getSpeed() + "," + loc.getTime(); params += "," + loc.getSpeed() + "," + loc.getTime();
if(id != "global") if (id != "global") {
{ mCtx.sendJavascript("navigator._geo.success(" + id + "," + params + ");");
mAppView.loadUrl("javascript:navigator._geo.success(" + id + "," + params + ")");
} }
else else {
{ mCtx.sendJavascript("navigator.geolocation.gotCurrentPosition(" + params + ");");
mAppView.loadUrl("javascript:navigator.geolocation.gotCurrentPosition(" + params + ")");
this.stop(); this.stop();
} }
} }
void fail() void fail() {
{ // Do we need to know why? How would we handle this?
// Do we need to know why? How would we handle this?
if (id != "global") { if (id != "global") {
mAppView.loadUrl("javascript:navigator._geo.fail(" + id + ")"); mCtx.sendJavascript("navigator._geo.fail(" + id + ");");
} } else {
else mCtx.sendJavascript("navigator._geo.fail();");
{
mAppView.loadUrl("javascript:navigator._geo.fail()");
} }
} }
void start(int interval) void start(int interval) {
{ if (mGps != null) {
if(mGps != null)
mGps.start(interval); mGps.start(interval);
if(mNetwork != null) }
if (mNetwork != null) {
mNetwork.start(interval); mNetwork.start(interval);
if(mNetwork == null && mGps == null) }
{ if (mNetwork == null && mGps == null) {
// Really, how the hell were you going to get the location??? // Really, how were you going to get the location???
mAppView.loadUrl("javascript:navigator._geo.fail()"); mCtx.sendJavascript("navigator._geo.fail();");
} }
} }
// This stops the listener // This stops the listener
void stop() void stop() {
{ if (mGps != null) {
if(mGps != null)
mGps.stop(); mGps.stop();
if(mNetwork != null) }
if (mNetwork != null) {
mNetwork.stop(); mNetwork.stop();
}
} }
} }

View File

@ -1,6 +1,5 @@
package com.phonegap; package com.phonegap;
import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.*; import android.database.sqlite.*;
import android.util.Log; import android.util.Log;
@ -13,63 +12,56 @@ public class Storage {
String path; String path;
String txid = ""; String txid = "";
WebView appView; WebView appView;
Context mCtx; DroidGap mCtx;
Storage(WebView view) Storage(WebView view, DroidGap ctx) {
{
appView = view; appView = view;
mCtx = ctx;
} }
public void setStorage(String appPackage) public void setStorage(String appPackage) {
{
path = "/data/data/" + appPackage + "/databases/"; path = "/data/data/" + appPackage + "/databases/";
} }
public void openDatabase(String db, String version, String display_name, long size) public void openDatabase(String db, String version, String display_name, long size) {
{ if (path != null) {
if (path != null)
{
path += db + ".db"; path += db + ".db";
myDb = SQLiteDatabase.openOrCreateDatabase(path, null); myDb = SQLiteDatabase.openOrCreateDatabase(path, null);
} }
} }
public void executeSql(String query, String[] params, String tx_id) public void executeSql(String query, String[] params, String tx_id) {
{ try {
try{ txid = tx_id;
txid = tx_id; Cursor myCursor = myDb.rawQuery(query, params);
Cursor myCursor = myDb.rawQuery(query, params); processResults(myCursor);
processResults(myCursor); } catch (SQLiteException ex) {
} Log.d(LOG_TAG, ex.getMessage());
catch (SQLiteException ex) txid = "";
{ mCtx.sendJavascript("droiddb.fail(" + ex.getMessage() + "," + txid + ");");
Log.d(LOG_TAG, ex.getMessage()); }
txid = "";
appView.loadUrl("droiddb.fail(" + ex.getMessage() + "," + txid + ")");
}
} }
public void processResults(Cursor cur) public void processResults(Cursor cur) {
{
String key = ""; String key = "";
String value = ""; String value = "";
String resultString = ""; String resultString = "";
if (cur.moveToFirst()) { if (cur.moveToFirst()) {
int colCount = cur.getColumnCount(); int colCount = cur.getColumnCount();
do { do {
resultString = "{"; resultString = "{";
for(int i = 0; i < colCount; ++i) for (int i = 0; i < colCount; ++i) {
{ key = cur.getColumnName(i);
key = cur.getColumnName(i); value = cur.getString(i);
value = cur.getString(i); resultString += " \"" + key + "\" : \"" + value + "\"";
resultString += " \"" + key + "\" : \"" + value + "\""; if (i != (colCount - 1)) {
if (i != (colCount - 1)) resultString += ",";
resultString += ","; }
} }
resultString += "}"; resultString += "}";
appView.loadUrl("javascript:droiddb.addResult('" + resultString + "', " + txid + ")"); mCtx.sendJavascript("droiddb.addResult('" + resultString + "', " + txid + ");");
} while (cur.moveToNext()); } while (cur.moveToNext());
appView.loadUrl("javascript:droiddb.completeQuery(" + txid + ")"); mCtx.sendJavascript("droiddb.completeQuery(" + txid + ");");
txid = ""; txid = "";
myDb.close(); myDb.close();
} }

View File

@ -11,43 +11,37 @@ import android.webkit.WebView;
public class TempListener implements SensorEventListener { public class TempListener implements SensorEventListener {
WebView mAppView; WebView mAppView;
Context mCtx; DroidGap mCtx;
Sensor mSensor; Sensor mSensor;
private SensorManager sensorManager; private SensorManager sensorManager;
TempListener(Context ctx, WebView appView) TempListener(DroidGap ctx, WebView appView) {
{
mCtx = ctx; mCtx = ctx;
mAppView = appView; mAppView = appView;
sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE); sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);
} }
public void start() public void start() {
{
List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_TEMPERATURE); List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_TEMPERATURE);
if (list.size() > 0) if (list.size() > 0) {
{
this.mSensor = list.get(0); this.mSensor = list.get(0);
this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_NORMAL); this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_NORMAL);
} }
} }
public void stop() public void stop() {
{
this.sensorManager.unregisterListener(this); this.sensorManager.unregisterListener(this);
} }
public void onAccuracyChanged(Sensor sensor, int accuracy) { public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
public void onSensorChanged(SensorEvent event) { public void onSensorChanged(SensorEvent event) {
// We want to know what temp this is. // We want to know what temp this is.
float temp = event.values[0]; float temp = event.values[0];
mAppView.loadUrl("javascript:gotTemp(" + temp + ")"); mCtx.sendJavascript("gotTemp(" + temp + ");");
} }
} }