From 0514195d94a944dad0e83d66f8e4a564421a5925 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Fri, 27 Mar 2009 17:08:39 -0700 Subject: [PATCH] Android API Updating --- src/com/nitobi/phonegap/GeoBroker.java | 51 +++++++++++++++++--- src/com/nitobi/phonegap/GeoListener.java | 26 ++++++++-- src/com/nitobi/phonegap/GpsListener.java | 7 ++- src/com/nitobi/phonegap/NetworkListener.java | 14 ++++++ 4 files changed, 87 insertions(+), 11 deletions(-) diff --git a/src/com/nitobi/phonegap/GeoBroker.java b/src/com/nitobi/phonegap/GeoBroker.java index 56817baf..28ad2757 100644 --- a/src/com/nitobi/phonegap/GeoBroker.java +++ b/src/com/nitobi/phonegap/GeoBroker.java @@ -1,19 +1,58 @@ package com.nitobi.phonegap; +import java.util.HashMap; + +import android.content.Context; +import android.location.Location; 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 { - private WebView mAppView; + private WebView mAppView; + private Context mCtx; + private HashMap geoListeners; - - public String start(int freq, String success, String failure) + GeoBroker(WebView view, Context ctx) { - - return null; + mCtx = ctx; + mAppView = view; + } + + public GeoTuple getCurrentLocation() + { + GeoListener listener = new GeoListener("0", mCtx, 60000, mAppView); + Location loc = listener.getCurrentLocation(); + GeoTuple geo = new GeoTuple(); + if (loc == null) + { + geo.lat = 0; + geo.lng = 0; + geo.ele = 0; + } + else + { + geo.lng = loc.getLongitude(); + geo.lat = loc.getLatitude(); + geo.ele = 0; + } + listener.stop(); + return geo; + } + + public String start(int freq, String key) + { + GeoListener listener = new GeoListener(key, mCtx, freq, mAppView); + geoListeners.put(key, listener); + return key; } public void stop(String key) { - + GeoListener geo = geoListeners.get(key); } } diff --git a/src/com/nitobi/phonegap/GeoListener.java b/src/com/nitobi/phonegap/GeoListener.java index a36a262b..6281a817 100644 --- a/src/com/nitobi/phonegap/GeoListener.java +++ b/src/com/nitobi/phonegap/GeoListener.java @@ -1,6 +1,7 @@ package com.nitobi.phonegap; import android.content.Context; +import android.location.Location; import android.webkit.WebView; public class GeoListener { @@ -14,24 +15,41 @@ public class GeoListener { int interval; - GeoListener(String key, Context ctx, int time, String succ, String fail) + GeoListener(String i, Context ctx, int time, WebView appView) { - id = key; + id = i; interval = time; mCtx = ctx; mGps = new GpsListener(mCtx, interval, this); mNetwork = new NetworkListener(mCtx, interval, this); + mAppView = appView; } - void success() + void success(Location loc) { /* * We only need to figure out what we do when we succeed! */ - mAppView.loadUrl("javascript:geoLocation.success(" + ")"); + mAppView.loadUrl("javascript:GeoLocation.success(" + id + ", " + loc.getLatitude() + ", " + loc.getLongitude() + ")"); } void fail() { + // Do we need to know why? How would we handle this? + mAppView.loadUrl("javascript:GeoLocation.fail(" + id + ")"); + } + + // This stops the listener + void stop() + { + mGps.stop(); + mNetwork.stop(); + } + + public Location getCurrentLocation() { + Location loc = mGps.getLocation(); + if (loc == null) + loc = mNetwork.getLocation(); + return loc; } } diff --git a/src/com/nitobi/phonegap/GpsListener.java b/src/com/nitobi/phonegap/GpsListener.java index b38aafe4..d81fd62b 100644 --- a/src/com/nitobi/phonegap/GpsListener.java +++ b/src/com/nitobi/phonegap/GpsListener.java @@ -82,11 +82,16 @@ public class GpsListener implements LocationListener { public void onLocationChanged(Location location) { Log.d(LOG_TAG, "The location has been updated!"); - cLoc = location; + owner.success(location); } public boolean hasLocation() { return (cLoc != null); } + + public void stop() + { + mLocMan.removeUpdates(this); + } } diff --git a/src/com/nitobi/phonegap/NetworkListener.java b/src/com/nitobi/phonegap/NetworkListener.java index 8c4bd9c4..16468d87 100644 --- a/src/com/nitobi/phonegap/NetworkListener.java +++ b/src/com/nitobi/phonegap/NetworkListener.java @@ -81,8 +81,22 @@ public class NetworkListener implements LocationListener { } + /* + * The GPS is the primary form of Geolocation in PhoneGap. Only fire the success variables if the GPS is down + * for some reason + */ public void onLocationChanged(Location location) { Log.d(LOG_TAG, "The location has been updated!"); + if (!owner.mGps.hasLocation()) + { + owner.success(location); + } cLoc = location; } + + public void stop() + { + mLocMan.removeUpdates(this); + } + }