mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-31 17:32:51 +08:00
Android API Updating
This commit is contained in:
parent
745e550815
commit
0514195d94
@ -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<String, GeoListener> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user