mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-20 23:56:20 +08:00
Change loadUrl(javascript...) to use new callback mechanism.
This commit is contained in:
parent
1850d2c04e
commit
b4d3a10773
@ -2,7 +2,6 @@ package com.phonegap;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import android.content.Context;
|
||||
import android.webkit.WebView;
|
||||
|
||||
/**
|
||||
@ -21,7 +20,7 @@ public class AccelBroker {
|
||||
public static int ERROR_NOT_FOUND = 4;
|
||||
|
||||
private WebView mAppView; // WebView object
|
||||
private Context mCtx; // Activity (DroidGap) object
|
||||
private DroidGap mCtx; // DroidGap object
|
||||
private AccelListener listener; // Accelerator listener
|
||||
private HashMap<String,Integer> listenerIds; // List of listener ids
|
||||
|
||||
@ -31,7 +30,7 @@ public class AccelBroker {
|
||||
* @param view
|
||||
* @param ctx
|
||||
*/
|
||||
public AccelBroker(WebView view, Context ctx)
|
||||
public AccelBroker(WebView view, DroidGap ctx)
|
||||
{
|
||||
mCtx = ctx;
|
||||
mAppView = view;
|
||||
|
@ -16,7 +16,7 @@ import android.webkit.WebView;
|
||||
public class AccelListener implements SensorEventListener{
|
||||
|
||||
WebView mAppView; // WebView object
|
||||
Context mCtx; // Activity (DroidGap) object
|
||||
DroidGap mCtx; // DroidGap object
|
||||
|
||||
float x,y,z; // most recent acceleration values
|
||||
long timeStamp; // time of most recent value
|
||||
@ -31,7 +31,7 @@ public class AccelListener implements SensorEventListener{
|
||||
* @param ctx The Activity (DroidGap) object
|
||||
* @param appView
|
||||
*/
|
||||
public AccelListener(Context ctx, WebView appView) {
|
||||
public AccelListener(DroidGap ctx, WebView appView) {
|
||||
this.mCtx = ctx;
|
||||
this.mAppView = appView;
|
||||
this.sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);
|
||||
|
@ -37,7 +37,7 @@ public class CameraLauncher {
|
||||
byte[] code = jpeg_data.toByteArray();
|
||||
byte[] output = Base64.encodeBase64(code);
|
||||
String js_out = new String(output);
|
||||
mAppView.loadUrl("javascript:navigator.camera.win('" + js_out + "');");
|
||||
mGap.sendJavascript("navigator.camera.win('" + js_out + "');");
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
@ -49,7 +49,7 @@ public class CameraLauncher {
|
||||
|
||||
public void failPicture(String err)
|
||||
{
|
||||
mAppView.loadUrl("javascript:navigator.camera.fail('" + err + "');");
|
||||
mGap.sendJavascript("navigator.camera.fail('" + err + "');");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import android.webkit.WebView;
|
||||
|
||||
/**
|
||||
* 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{
|
||||
|
||||
@ -34,9 +34,9 @@ public class CompassListener implements SensorEventListener{
|
||||
* @param appView
|
||||
* @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.sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);
|
||||
}
|
||||
|
@ -21,13 +21,13 @@ public class ContactManager {
|
||||
}
|
||||
|
||||
private static final String LOG_TAG = "Contact Query";
|
||||
Activity mApp;
|
||||
DroidGap mApp;
|
||||
WebView mView;
|
||||
Uri mPeople = android.provider.Contacts.People.CONTENT_URI;
|
||||
Uri mPhone = android.provider.Contacts.Phones.CONTENT_URI;
|
||||
Uri mEmail = android.provider.Contacts.ContactMethods.CONTENT_URI;
|
||||
|
||||
ContactManager(WebView view, Activity app)
|
||||
ContactManager(WebView view, DroidGap app)
|
||||
{
|
||||
mApp = app;
|
||||
mView = view;
|
||||
@ -160,23 +160,28 @@ public class ContactManager {
|
||||
email = "";
|
||||
|
||||
// Code for backwards compatibility with the OLD Contacts API
|
||||
if (all)
|
||||
mView.loadUrl("javascript:navigator.ContactManager.droidAddContact('" + name + "','" + phoneNumber + "','" + email +"')");
|
||||
else
|
||||
mView.loadUrl("javascript:navigator.contacts.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +"')");
|
||||
|
||||
if (all) {
|
||||
mApp.sendJavascript("navigator.ContactManager.droidAddContact('" + name + "','" + phoneNumber + "','" + email +"');");
|
||||
}
|
||||
else {
|
||||
mApp.sendJavascript("navigator.contacts.droidFoundContact('" + name + "','" + phoneNumber + "','" + email +"');");
|
||||
}
|
||||
} while (cur.moveToNext());
|
||||
if (all)
|
||||
mView.loadUrl("javascript:navigator.ContactManager.droidDone()");
|
||||
else
|
||||
mView.loadUrl("javascript:navigator.contacts.droidDone();");
|
||||
if (all) {
|
||||
mApp.sendJavascript("navigator.ContactManager.droidDone();");
|
||||
}
|
||||
else {
|
||||
mApp.sendJavascript("navigator.contacts.droidDone();");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(all)
|
||||
mView.loadUrl("javascript:navigator.ContactManager.fail()");
|
||||
else
|
||||
mView.loadUrl("javascript:navigator.contacts.fail('None found!')");
|
||||
if (all) {
|
||||
mApp.sendJavascript("navigator.ContactManager.fail();");
|
||||
}
|
||||
else {
|
||||
mApp.sendJavascript("navigator.contacts.fail('None found!');");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -199,10 +204,10 @@ public class ContactManager {
|
||||
if(data != null)
|
||||
{
|
||||
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());
|
||||
mView.loadUrl("javascript:navigator.contacts.droidDoneContacts();");
|
||||
mApp.sendJavascript("navigator.contacts.droidDoneContacts();");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -244,7 +244,7 @@ public class DroidGap extends Activity {
|
||||
|
||||
if (android.os.Build.VERSION.RELEASE.startsWith("1."))
|
||||
{
|
||||
cupcakeStorage = new Storage(appView);
|
||||
cupcakeStorage = new Storage(appView, this);
|
||||
geo = new GeoBroker(appView, this);
|
||||
appView.addJavascriptInterface(cupcakeStorage, "droidStorage");
|
||||
appView.addJavascriptInterface(geo, "Geo");
|
||||
|
@ -14,11 +14,11 @@ import android.webkit.WebView;
|
||||
|
||||
public class GeoBroker {
|
||||
private WebView mAppView;
|
||||
private Context mCtx;
|
||||
private DroidGap mCtx;
|
||||
private HashMap<String, GeoListener> geoListeners;
|
||||
private GeoListener global;
|
||||
|
||||
public GeoBroker(WebView view, Context ctx)
|
||||
public GeoBroker(WebView view, DroidGap ctx)
|
||||
{
|
||||
mCtx = ctx;
|
||||
mAppView = view;
|
||||
|
@ -12,29 +12,29 @@ public class GeoListener {
|
||||
GpsListener mGps;
|
||||
NetworkListener mNetwork;
|
||||
LocationManager mLocMan;
|
||||
Context mCtx;
|
||||
private DroidGap mCtx;
|
||||
private WebView mAppView;
|
||||
|
||||
int interval;
|
||||
|
||||
GeoListener(String i, Context ctx, int time, WebView appView)
|
||||
{
|
||||
GeoListener(String i, DroidGap ctx, int time, WebView appView) {
|
||||
id = i;
|
||||
interval = time;
|
||||
mCtx = ctx;
|
||||
mGps = null;
|
||||
mNetwork = null;
|
||||
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);
|
||||
if (mLocMan.getProvider(LocationManager.NETWORK_PROVIDER) != null)
|
||||
}
|
||||
if (mLocMan.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
|
||||
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!
|
||||
*/
|
||||
@ -45,49 +45,45 @@ public class GeoListener {
|
||||
*/
|
||||
params = loc.getLatitude() + "," + loc.getLongitude() + ", " + loc.getAltitude() + "," + loc.getAccuracy() + "," + loc.getBearing();
|
||||
params += "," + loc.getSpeed() + "," + loc.getTime();
|
||||
if(id != "global")
|
||||
{
|
||||
mAppView.loadUrl("javascript:navigator._geo.success(" + id + "," + params + ")");
|
||||
if (id != "global") {
|
||||
mCtx.sendJavascript("navigator._geo.success(" + id + "," + params + ");");
|
||||
}
|
||||
else
|
||||
{
|
||||
mAppView.loadUrl("javascript:navigator.geolocation.gotCurrentPosition(" + params + ")");
|
||||
else {
|
||||
mCtx.sendJavascript("navigator.geolocation.gotCurrentPosition(" + params + ");");
|
||||
this.stop();
|
||||
}
|
||||
}
|
||||
|
||||
void fail()
|
||||
{
|
||||
// Do we need to know why? How would we handle this?
|
||||
void fail() {
|
||||
// Do we need to know why? How would we handle this?
|
||||
if (id != "global") {
|
||||
mAppView.loadUrl("javascript:navigator._geo.fail(" + id + ")");
|
||||
}
|
||||
else
|
||||
{
|
||||
mAppView.loadUrl("javascript:navigator._geo.fail()");
|
||||
mCtx.sendJavascript("navigator._geo.fail(" + id + ");");
|
||||
} else {
|
||||
mCtx.sendJavascript("navigator._geo.fail();");
|
||||
}
|
||||
}
|
||||
|
||||
void start(int interval)
|
||||
{
|
||||
if(mGps != null)
|
||||
void start(int interval) {
|
||||
if (mGps != null) {
|
||||
mGps.start(interval);
|
||||
if(mNetwork != null)
|
||||
}
|
||||
if (mNetwork != null) {
|
||||
mNetwork.start(interval);
|
||||
if(mNetwork == null && mGps == null)
|
||||
{
|
||||
// Really, how the hell were you going to get the location???
|
||||
mAppView.loadUrl("javascript:navigator._geo.fail()");
|
||||
}
|
||||
if (mNetwork == null && mGps == null) {
|
||||
// Really, how were you going to get the location???
|
||||
mCtx.sendJavascript("navigator._geo.fail();");
|
||||
}
|
||||
}
|
||||
|
||||
// This stops the listener
|
||||
void stop()
|
||||
{
|
||||
if(mGps != null)
|
||||
void stop() {
|
||||
if (mGps != null) {
|
||||
mGps.stop();
|
||||
if(mNetwork != null)
|
||||
}
|
||||
if (mNetwork != null) {
|
||||
mNetwork.stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.phonegap;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.*;
|
||||
import android.util.Log;
|
||||
@ -13,63 +12,56 @@ public class Storage {
|
||||
String path;
|
||||
String txid = "";
|
||||
WebView appView;
|
||||
Context mCtx;
|
||||
DroidGap mCtx;
|
||||
|
||||
Storage(WebView view)
|
||||
{
|
||||
Storage(WebView view, DroidGap ctx) {
|
||||
appView = view;
|
||||
mCtx = ctx;
|
||||
}
|
||||
|
||||
public void setStorage(String appPackage)
|
||||
{
|
||||
|
||||
public void setStorage(String appPackage) {
|
||||
path = "/data/data/" + appPackage + "/databases/";
|
||||
}
|
||||
|
||||
public void openDatabase(String db, String version, String display_name, long size)
|
||||
{
|
||||
if (path != null)
|
||||
{
|
||||
public void openDatabase(String db, String version, String display_name, long size) {
|
||||
if (path != null) {
|
||||
path += db + ".db";
|
||||
myDb = SQLiteDatabase.openOrCreateDatabase(path, null);
|
||||
}
|
||||
}
|
||||
|
||||
public void executeSql(String query, String[] params, String tx_id)
|
||||
{
|
||||
try{
|
||||
txid = tx_id;
|
||||
Cursor myCursor = myDb.rawQuery(query, params);
|
||||
processResults(myCursor);
|
||||
}
|
||||
catch (SQLiteException ex)
|
||||
{
|
||||
Log.d(LOG_TAG, ex.getMessage());
|
||||
txid = "";
|
||||
appView.loadUrl("droiddb.fail(" + ex.getMessage() + "," + txid + ")");
|
||||
}
|
||||
public void executeSql(String query, String[] params, String tx_id) {
|
||||
try {
|
||||
txid = tx_id;
|
||||
Cursor myCursor = myDb.rawQuery(query, params);
|
||||
processResults(myCursor);
|
||||
} catch (SQLiteException ex) {
|
||||
Log.d(LOG_TAG, ex.getMessage());
|
||||
txid = "";
|
||||
mCtx.sendJavascript("droiddb.fail(" + ex.getMessage() + "," + txid + ");");
|
||||
}
|
||||
}
|
||||
|
||||
public void processResults(Cursor cur)
|
||||
{
|
||||
public void processResults(Cursor cur) {
|
||||
String key = "";
|
||||
String value = "";
|
||||
String resultString = "";
|
||||
if (cur.moveToFirst()) {
|
||||
int colCount = cur.getColumnCount();
|
||||
do {
|
||||
resultString = "{";
|
||||
for(int i = 0; i < colCount; ++i)
|
||||
{
|
||||
key = cur.getColumnName(i);
|
||||
value = cur.getString(i);
|
||||
resultString += " \"" + key + "\" : \"" + value + "\"";
|
||||
if (i != (colCount - 1))
|
||||
resultString += ",";
|
||||
}
|
||||
resultString += "}";
|
||||
appView.loadUrl("javascript:droiddb.addResult('" + resultString + "', " + txid + ")");
|
||||
int colCount = cur.getColumnCount();
|
||||
do {
|
||||
resultString = "{";
|
||||
for (int i = 0; i < colCount; ++i) {
|
||||
key = cur.getColumnName(i);
|
||||
value = cur.getString(i);
|
||||
resultString += " \"" + key + "\" : \"" + value + "\"";
|
||||
if (i != (colCount - 1)) {
|
||||
resultString += ",";
|
||||
}
|
||||
}
|
||||
resultString += "}";
|
||||
mCtx.sendJavascript("droiddb.addResult('" + resultString + "', " + txid + ");");
|
||||
} while (cur.moveToNext());
|
||||
appView.loadUrl("javascript:droiddb.completeQuery(" + txid + ")");
|
||||
mCtx.sendJavascript("droiddb.completeQuery(" + txid + ");");
|
||||
txid = "";
|
||||
myDb.close();
|
||||
}
|
||||
|
@ -11,43 +11,37 @@ import android.webkit.WebView;
|
||||
|
||||
public class TempListener implements SensorEventListener {
|
||||
WebView mAppView;
|
||||
Context mCtx;
|
||||
DroidGap mCtx;
|
||||
Sensor mSensor;
|
||||
|
||||
private SensorManager sensorManager;
|
||||
|
||||
TempListener(Context ctx, WebView appView)
|
||||
{
|
||||
TempListener(DroidGap ctx, WebView appView) {
|
||||
mCtx = ctx;
|
||||
mAppView = appView;
|
||||
mAppView = appView;
|
||||
sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);
|
||||
}
|
||||
|
||||
public void start()
|
||||
{
|
||||
public void start() {
|
||||
List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_TEMPERATURE);
|
||||
if (list.size() > 0)
|
||||
{
|
||||
if (list.size() > 0) {
|
||||
this.mSensor = list.get(0);
|
||||
this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_NORMAL);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
public void stop() {
|
||||
this.sensorManager.unregisterListener(this);
|
||||
}
|
||||
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
// We want to know what temp this is.
|
||||
float temp = event.values[0];
|
||||
mAppView.loadUrl("javascript:gotTemp(" + temp + ")");
|
||||
mCtx.sendJavascript("gotTemp(" + temp + ");");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user