mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 20:33:07 +08:00
restructured geolocation plugin
This commit is contained in:
parent
6fc2a3b84e
commit
724ea49f0b
198
framework/src/org/apache/cordova/CordovaLocationListener.java
Executable file
198
framework/src/org/apache/cordova/CordovaLocationListener.java
Executable file
@ -0,0 +1,198 @@
|
|||||||
|
/*
|
||||||
|
Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
or more contributor license agreements. See the NOTICE file
|
||||||
|
distributed with this work for additional information
|
||||||
|
regarding copyright ownership. The ASF licenses this file
|
||||||
|
to you under the Apache License, Version 2.0 (the
|
||||||
|
"License"); you may not use this file except in compliance
|
||||||
|
with the License. You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing,
|
||||||
|
software distributed under the License is distributed on an
|
||||||
|
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
KIND, either express or implied. See the License for the
|
||||||
|
specific language governing permissions and limitations
|
||||||
|
under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.cordova;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import android.location.Location;
|
||||||
|
import android.location.LocationListener;
|
||||||
|
import android.location.LocationManager;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
public class CordovaLocationListener implements LocationListener {
|
||||||
|
public static int PERMISSION_DENIED = 1;
|
||||||
|
public static int POSITION_UNAVAILABLE = 2;
|
||||||
|
public static int TIMEOUT = 3;
|
||||||
|
|
||||||
|
protected LocationManager locationManager;
|
||||||
|
private GeoBroker owner;
|
||||||
|
protected boolean running = false;
|
||||||
|
|
||||||
|
public HashMap<String, String> watches = new HashMap<String, String>();
|
||||||
|
private List<String> callbacks = new ArrayList<String>();
|
||||||
|
|
||||||
|
private String TAG = "[Cordova Location Listener]";
|
||||||
|
|
||||||
|
public CordovaLocationListener(LocationManager manager, GeoBroker broker, String tag) {
|
||||||
|
this.locationManager = manager;
|
||||||
|
this.owner = broker;
|
||||||
|
this.TAG = tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void fail(int code, String message) {
|
||||||
|
for (String callbackId: this.callbacks)
|
||||||
|
{
|
||||||
|
this.owner.fail(code, message, callbackId);
|
||||||
|
}
|
||||||
|
this.callbacks.clear();
|
||||||
|
|
||||||
|
Iterator it = this.watches.entrySet().iterator();
|
||||||
|
while (it.hasNext()) {
|
||||||
|
Map.Entry pairs = (Map.Entry)it.next();
|
||||||
|
this.owner.fail(code, message, (String)pairs.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void win(Location loc) {
|
||||||
|
for (String callbackId: this.callbacks)
|
||||||
|
{
|
||||||
|
this.owner.win(loc, callbackId);
|
||||||
|
}
|
||||||
|
this.callbacks.clear();
|
||||||
|
|
||||||
|
Iterator it = this.watches.entrySet().iterator();
|
||||||
|
while (it.hasNext()) {
|
||||||
|
Map.Entry pairs = (Map.Entry)it.next();
|
||||||
|
this.owner.win(loc, (String)pairs.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Location Listener Methods
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the provider is disabled by the user.
|
||||||
|
*
|
||||||
|
* @param provider
|
||||||
|
*/
|
||||||
|
public void onProviderDisabled(String provider) {
|
||||||
|
Log.d(TAG, "Location provider '" + provider + "' disabled.");
|
||||||
|
this.fail(POSITION_UNAVAILABLE, "GPS provider disabled.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the provider is enabled by the user.
|
||||||
|
*
|
||||||
|
* @param provider
|
||||||
|
*/
|
||||||
|
public void onProviderEnabled(String provider) {
|
||||||
|
Log.d(TAG, "Location provider "+ provider + " has been enabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the provider status changes. This method is called when a
|
||||||
|
* provider is unable to fetch a location or if the provider has recently
|
||||||
|
* become available after a period of unavailability.
|
||||||
|
*
|
||||||
|
* @param provider
|
||||||
|
* @param status
|
||||||
|
* @param extras
|
||||||
|
*/
|
||||||
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
||||||
|
Log.d(TAG, "The status of the provider " + provider + " has changed");
|
||||||
|
if (status == 0) {
|
||||||
|
Log.d(TAG, provider + " is OUT OF SERVICE");
|
||||||
|
this.fail(CordovaLocationListener.POSITION_UNAVAILABLE, "Provider " + provider + " is out of service.");
|
||||||
|
}
|
||||||
|
else if (status == 1) {
|
||||||
|
Log.d(TAG, provider + " is TEMPORARILY_UNAVAILABLE");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Log.d(TAG, provider + " is AVAILABLE");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the location has changed.
|
||||||
|
*
|
||||||
|
* @param location
|
||||||
|
*/
|
||||||
|
public void onLocationChanged(Location location) {
|
||||||
|
Log.d(TAG, "The location has been updated!");
|
||||||
|
this.win(location);
|
||||||
|
}
|
||||||
|
|
||||||
|
// PUBLIC
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return this.watches.size() + this.callbacks.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addWatch(String timerId, String callbackId) {
|
||||||
|
this.watches.put(timerId, callbackId);
|
||||||
|
if (this.size() == 1) {
|
||||||
|
this.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void addCallback(String callbackId) {
|
||||||
|
this.callbacks.add(callbackId);
|
||||||
|
if (this.size() == 1) {
|
||||||
|
this.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void clearWatch(String timerId) {
|
||||||
|
if (this.watches.containsKey(timerId)) {
|
||||||
|
this.watches.remove(timerId);
|
||||||
|
}
|
||||||
|
if (this.size() == 0) {
|
||||||
|
this.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroy listener.
|
||||||
|
*/
|
||||||
|
public void destroy() {
|
||||||
|
this.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
// LOCAL
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start requesting location updates.
|
||||||
|
*
|
||||||
|
* @param interval
|
||||||
|
*/
|
||||||
|
private void start() {
|
||||||
|
if (!this.running) {
|
||||||
|
if (this.locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
|
||||||
|
this.running = true;
|
||||||
|
this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 10, this);
|
||||||
|
} else {
|
||||||
|
this.fail(CordovaLocationListener.POSITION_UNAVAILABLE, "Network provider is not available.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop receiving location updates.
|
||||||
|
*/
|
||||||
|
private void stop() {
|
||||||
|
if (this.running) {
|
||||||
|
this.locationManager.removeUpdates(this);
|
||||||
|
this.running = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -18,14 +18,15 @@
|
|||||||
*/
|
*/
|
||||||
package org.apache.cordova;
|
package org.apache.cordova;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map.Entry;
|
|
||||||
|
|
||||||
import org.apache.cordova.api.Plugin;
|
import org.apache.cordova.api.Plugin;
|
||||||
import org.apache.cordova.api.PluginResult;
|
import org.apache.cordova.api.PluginResult;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.location.Location;
|
||||||
|
import android.location.LocationManager;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This class is the interface to the Geolocation. It's bound to the geo object.
|
* This class is the interface to the Geolocation. It's bound to the geo object.
|
||||||
@ -34,132 +35,142 @@ import org.json.JSONException;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public class GeoBroker extends Plugin {
|
public class GeoBroker extends Plugin {
|
||||||
|
private GPSListener gpsListener;
|
||||||
// List of gGeolocation listeners
|
private NetworkListener networkListener;
|
||||||
private HashMap<String, GeoListener> geoListeners;
|
private LocationManager locationManager;
|
||||||
private GeoListener global;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public GeoBroker() {
|
public GeoBroker() {
|
||||||
this.geoListeners = new HashMap<String, GeoListener>();
|
this.locationManager = (LocationManager) this.ctx.getSystemService(Context.LOCATION_SERVICE);
|
||||||
|
this.networkListener = new NetworkListener(this.locationManager, this);
|
||||||
|
this.gpsListener = new GPSListener(this.locationManager, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the request and returns PluginResult.
|
||||||
|
*
|
||||||
|
* @param action The action to execute.
|
||||||
|
* @param args JSONArry of arguments for the plugin.
|
||||||
|
* @param callbackId The callback id used when calling back into JavaScript.
|
||||||
|
* @return A PluginResult object with a status and message.
|
||||||
|
*/
|
||||||
|
public PluginResult execute(String action, JSONArray args, String callbackId) {
|
||||||
|
PluginResult.Status status = PluginResult.Status.NO_RESULT;
|
||||||
|
String message = "";
|
||||||
|
PluginResult result = new PluginResult(status, message);
|
||||||
|
result.setKeepCallback(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (action.equals("getLocation")) {
|
||||||
|
boolean enableHighAccuracy = args.getBoolean(0);
|
||||||
|
int maximumAge = args.getInt(1);
|
||||||
|
Location last = this.locationManager.getLastKnownLocation((enableHighAccuracy ? LocationManager.GPS_PROVIDER : LocationManager.NETWORK_PROVIDER));
|
||||||
|
// Check if we can use lastKnownLocation to get a quick reading and use less battery
|
||||||
|
if ((System.currentTimeMillis() - last.getTime()) <= maximumAge) {
|
||||||
|
result = new PluginResult(PluginResult.Status.OK, GeoBroker.returnLocationJSON(last));
|
||||||
|
} else {
|
||||||
|
this.getCurrentLocation(callbackId, enableHighAccuracy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (action.equals("addWatch")) {
|
||||||
|
String id = args.getString(0);
|
||||||
|
boolean enableHighAccuracy = args.getBoolean(1);
|
||||||
|
this.addWatch(id, callbackId, enableHighAccuracy);
|
||||||
|
}
|
||||||
|
else if (action.equals("clearWatch")) {
|
||||||
|
String id = args.getString(0);
|
||||||
|
this.clearWatch(id);
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void clearWatch(String id) {
|
||||||
|
this.gpsListener.clearWatch(id);
|
||||||
|
this.networkListener.clearWatch(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private void getCurrentLocation(String callbackId, boolean enableHighAccuracy) {
|
||||||
* Executes the request and returns PluginResult.
|
if (enableHighAccuracy) {
|
||||||
*
|
this.gpsListener.addCallback(callbackId);
|
||||||
* @param action The action to execute.
|
} else {
|
||||||
* @param args JSONArry of arguments for the plugin.
|
this.networkListener.addCallback(callbackId);
|
||||||
* @param callbackId The callback id used when calling back into JavaScript.
|
|
||||||
* @return A PluginResult object with a status and message.
|
|
||||||
*/
|
|
||||||
public PluginResult execute(String action, JSONArray args, String callbackId) {
|
|
||||||
PluginResult.Status status = PluginResult.Status.OK;
|
|
||||||
String result = "";
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (action.equals("getCurrentLocation")) {
|
|
||||||
this.getCurrentLocation(args.getBoolean(0), args.getInt(1), args.getInt(2));
|
|
||||||
}
|
|
||||||
else if (action.equals("start")) {
|
|
||||||
String s = this.start(args.getString(0), args.getBoolean(1), args.getInt(2), args.getInt(3));
|
|
||||||
return new PluginResult(status, s);
|
|
||||||
}
|
|
||||||
else if (action.equals("stop")) {
|
|
||||||
this.stop(args.getString(0));
|
|
||||||
}
|
|
||||||
return new PluginResult(status, result);
|
|
||||||
} catch (JSONException e) {
|
|
||||||
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addWatch(String timerId, String callbackId, boolean enableHighAccuracy) {
|
||||||
|
if (enableHighAccuracy) {
|
||||||
|
this.gpsListener.addWatch(timerId, callbackId);
|
||||||
|
} else {
|
||||||
|
this.networkListener.addWatch(timerId, callbackId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Identifies if action to be executed returns a value and should be run synchronously.
|
* Identifies if action to be executed returns a value and should be run synchronously.
|
||||||
*
|
*
|
||||||
* @param action The action to execute
|
* @param action The action to execute
|
||||||
* @return T=returns value
|
* @return T=returns value
|
||||||
*/
|
*/
|
||||||
public boolean isSynch(String action) {
|
public boolean isSynch(String action) {
|
||||||
// Starting listeners is easier to run on main thread, so don't run async.
|
// Starting listeners is easier to run on main thread, so don't run async.
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the activity is to be shut down.
|
* Called when the activity is to be shut down.
|
||||||
* Stop listener.
|
* Stop listener.
|
||||||
*/
|
*/
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
java.util.Set<Entry<String,GeoListener>> s = this.geoListeners.entrySet();
|
this.networkListener.destroy();
|
||||||
java.util.Iterator<Entry<String,GeoListener>> it = s.iterator();
|
this.gpsListener.destroy();
|
||||||
while (it.hasNext()) {
|
this.networkListener = null;
|
||||||
Entry<String,GeoListener> entry = it.next();
|
this.gpsListener = null;
|
||||||
GeoListener listener = entry.getValue();
|
|
||||||
listener.destroy();
|
|
||||||
}
|
|
||||||
this.geoListeners.clear();
|
|
||||||
if (this.global != null) {
|
|
||||||
this.global.destroy();
|
|
||||||
}
|
|
||||||
this.global = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
public static String returnLocationJSON(Location loc) {
|
||||||
// LOCAL METHODS
|
return "{" +
|
||||||
//--------------------------------------------------------------------------
|
"'latitude':" + loc.getLatitude() + "," +
|
||||||
|
"'longitude':" + loc.getLongitude() + "," +
|
||||||
/**
|
"'altitude':" + (loc.hasAltitude() ? loc.getAltitude() : "null") + "," +
|
||||||
* Get current location.
|
"'accuracy':" + loc.getAccuracy() + "," +
|
||||||
* The result is returned to JavaScript via a callback.
|
"'heading':" + (loc.hasBearing() ? (loc.hasSpeed() ? loc.getBearing() : "NaN") : "null") + "," +
|
||||||
*
|
"'speed':" + loc.getSpeed() + "," +
|
||||||
* @param enableHighAccuracy
|
"'timestamp':" + loc.getTime() +
|
||||||
* @param timeout
|
"}";
|
||||||
* @param maximumAge
|
}
|
||||||
*/
|
public void win(Location loc, String callbackId) {
|
||||||
public void getCurrentLocation(boolean enableHighAccuracy, int timeout, int maximumAge) {
|
PluginResult result = new PluginResult(PluginResult.Status.OK, GeoBroker.returnLocationJSON(loc));
|
||||||
|
this.success(result, callbackId);
|
||||||
// Create a geolocation listener just for getCurrentLocation and call it "global"
|
}
|
||||||
if (this.global == null) {
|
/**
|
||||||
this.global = new GeoListener(this, "global", maximumAge);
|
* Location failed. Send error back to JavaScript.
|
||||||
}
|
*
|
||||||
else {
|
* @param code The error code
|
||||||
this.global.start(maximumAge);
|
* @param msg The error message
|
||||||
}
|
* @throws JSONException
|
||||||
}
|
*/
|
||||||
|
public void fail(int code, String msg, String callbackId) {
|
||||||
/**
|
JSONObject obj = new JSONObject();
|
||||||
* Start geolocation listener and add to listener list.
|
String backup = null;
|
||||||
*
|
try {
|
||||||
* @param key The listener id
|
obj.put("code", code);
|
||||||
* @param enableHighAccuracy
|
obj.put("message", msg);
|
||||||
* @param timeout
|
} catch (JSONException e) {
|
||||||
* @param maximumAge
|
obj = null;
|
||||||
* @return
|
backup = "{'code':" + code + ",'message':'" + msg.replaceAll("'", "\'") + "'}";
|
||||||
*/
|
}
|
||||||
public String start(String key, boolean enableHighAccuracy, int timeout, int maximumAge) {
|
PluginResult result;
|
||||||
|
if (obj != null) {
|
||||||
// Make sure this listener doesn't already exist
|
result = new PluginResult(PluginResult.Status.ERROR, obj);
|
||||||
GeoListener listener = geoListeners.get(key);
|
} else {
|
||||||
if (listener == null) {
|
result = new PluginResult(PluginResult.Status.ERROR, backup);
|
||||||
listener = new GeoListener(this, key, maximumAge);
|
}
|
||||||
geoListeners.put(key, listener);
|
|
||||||
}
|
this.error(result, callbackId);
|
||||||
|
}
|
||||||
// Start it
|
|
||||||
listener.start(maximumAge);
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop geolocation listener and remove from listener list.
|
|
||||||
*
|
|
||||||
* @param key The listener id
|
|
||||||
*/
|
|
||||||
public void stop(String key) {
|
|
||||||
GeoListener listener = geoListeners.remove(key);
|
|
||||||
if (listener != null) {
|
|
||||||
listener.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,133 +0,0 @@
|
|||||||
/*
|
|
||||||
Licensed to the Apache Software Foundation (ASF) under one
|
|
||||||
or more contributor license agreements. See the NOTICE file
|
|
||||||
distributed with this work for additional information
|
|
||||||
regarding copyright ownership. The ASF licenses this file
|
|
||||||
to you under the Apache License, Version 2.0 (the
|
|
||||||
"License"); you may not use this file except in compliance
|
|
||||||
with the License. You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing,
|
|
||||||
software distributed under the License is distributed on an
|
|
||||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
KIND, either express or implied. See the License for the
|
|
||||||
specific language governing permissions and limitations
|
|
||||||
under the License.
|
|
||||||
*/
|
|
||||||
package org.apache.cordova;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.location.Location;
|
|
||||||
import android.location.LocationManager;
|
|
||||||
import android.webkit.WebView;
|
|
||||||
|
|
||||||
public class GeoListener {
|
|
||||||
public static int PERMISSION_DENIED = 1;
|
|
||||||
public static int POSITION_UNAVAILABLE = 2;
|
|
||||||
public static int TIMEOUT = 3;
|
|
||||||
|
|
||||||
String id; // Listener ID
|
|
||||||
String successCallback; //
|
|
||||||
String failCallback;
|
|
||||||
GpsListener mGps; // GPS listener
|
|
||||||
NetworkListener mNetwork; // Network listener
|
|
||||||
LocationManager mLocMan; // Location manager
|
|
||||||
|
|
||||||
private GeoBroker broker; // GeoBroker object
|
|
||||||
|
|
||||||
int interval;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
*
|
|
||||||
* @param id Listener id
|
|
||||||
* @param ctx
|
|
||||||
* @param time Sampling period in msec
|
|
||||||
* @param appView
|
|
||||||
*/
|
|
||||||
GeoListener(GeoBroker broker, String id, int time) {
|
|
||||||
this.id = id;
|
|
||||||
this.interval = time;
|
|
||||||
this.broker = broker;
|
|
||||||
this.mGps = null;
|
|
||||||
this.mNetwork = null;
|
|
||||||
this.mLocMan = (LocationManager) broker.ctx.getSystemService(Context.LOCATION_SERVICE);
|
|
||||||
|
|
||||||
// If GPS provider, then create and start GPS listener
|
|
||||||
if (this.mLocMan.getProvider(LocationManager.GPS_PROVIDER) != null) {
|
|
||||||
this.mGps = new GpsListener(broker.ctx, time, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If network provider, then create and start network listener
|
|
||||||
if (this.mLocMan.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
|
|
||||||
this.mNetwork = new NetworkListener(broker.ctx, time, this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Destroy listener.
|
|
||||||
*/
|
|
||||||
public void destroy() {
|
|
||||||
this.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Location found. Send location back to JavaScript.
|
|
||||||
*
|
|
||||||
* @param loc
|
|
||||||
*/
|
|
||||||
void success(Location loc) {
|
|
||||||
|
|
||||||
String params = loc.getLatitude() + "," + loc.getLongitude() + ", " + loc.getAltitude() +
|
|
||||||
"," + loc.getAccuracy() + "," + loc.getBearing() +
|
|
||||||
"," + loc.getSpeed() + "," + loc.getTime();
|
|
||||||
|
|
||||||
if (id == "global") {
|
|
||||||
this.stop();
|
|
||||||
}
|
|
||||||
this.broker.sendJavascript("navigator._geo.success('" + id + "'," + params + ");");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Location failed. Send error back to JavaScript.
|
|
||||||
*
|
|
||||||
* @param code The error code
|
|
||||||
* @param msg The error message
|
|
||||||
*/
|
|
||||||
void fail(int code, String msg) {
|
|
||||||
this.broker.sendJavascript("navigator._geo.fail('" + this.id + "', '" + code + "', '" + msg + "');");
|
|
||||||
this.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start retrieving location.
|
|
||||||
*
|
|
||||||
* @param interval
|
|
||||||
*/
|
|
||||||
void start(int interval) {
|
|
||||||
if (this.mGps != null) {
|
|
||||||
this.mGps.start(interval);
|
|
||||||
}
|
|
||||||
if (this.mNetwork != null) {
|
|
||||||
this.mNetwork.start(interval);
|
|
||||||
}
|
|
||||||
if (this.mNetwork == null && this.mGps == null) {
|
|
||||||
this.fail(POSITION_UNAVAILABLE, "No location providers available.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop listening for location.
|
|
||||||
*/
|
|
||||||
void stop() {
|
|
||||||
if (this.mGps != null) {
|
|
||||||
this.mGps.stop();
|
|
||||||
}
|
|
||||||
if (this.mNetwork != null) {
|
|
||||||
this.mNetwork.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -19,145 +19,37 @@
|
|||||||
|
|
||||||
package org.apache.cordova;
|
package org.apache.cordova;
|
||||||
|
|
||||||
import org.apache.cordova.api.CordovaInterface;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.location.Location;
|
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.location.LocationListener;
|
|
||||||
import android.os.Bundle;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class handles requests for GPS location services.
|
* This class handles requests for GPS location services.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class GpsListener implements LocationListener {
|
public class GPSListener extends CordovaLocationListener {
|
||||||
|
public GPSListener(LocationManager locationManager, GeoBroker m) {
|
||||||
private CordovaInterface mCtx; // CordovaActivity object
|
super(locationManager, m, "[Cordova GPSListener]");
|
||||||
|
if (this.locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
|
||||||
private LocationManager mLocMan; // Location manager object
|
// If network provider, then create and start network listener
|
||||||
private GeoListener owner; // Geolistener object (parent)
|
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this);
|
||||||
private boolean hasData = false; // Flag indicates if location data is available in cLoc
|
} else {
|
||||||
private Location cLoc; // Last recieved location
|
this.fail(CordovaLocationListener.POSITION_UNAVAILABLE, "GPS provider is not available.");
|
||||||
private boolean running = false; // Flag indicates if listener is running
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
* Automatically starts listening.
|
|
||||||
*
|
|
||||||
* @param ctx
|
|
||||||
* @param interval
|
|
||||||
* @param m
|
|
||||||
*/
|
|
||||||
public GpsListener(CordovaInterface ctx, int interval, GeoListener m) {
|
|
||||||
this.owner = m;
|
|
||||||
this.mCtx = ctx;
|
|
||||||
this.mLocMan = (LocationManager) this.mCtx.getSystemService(Context.LOCATION_SERVICE);
|
|
||||||
this.running = false;
|
|
||||||
this.start(interval);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get last location.
|
|
||||||
*
|
|
||||||
* @return Location object
|
|
||||||
*/
|
|
||||||
public Location getLocation() {
|
|
||||||
this.cLoc = this.mLocMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
|
||||||
if (this.cLoc != null) {
|
|
||||||
this.hasData = true;
|
|
||||||
}
|
|
||||||
return this.cLoc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when the provider is disabled by the user.
|
|
||||||
*
|
|
||||||
* @param provider
|
|
||||||
*/
|
|
||||||
public void onProviderDisabled(String provider) {
|
|
||||||
this.owner.fail(GeoListener.POSITION_UNAVAILABLE, "GPS provider disabled.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when the provider is enabled by the user.
|
|
||||||
*
|
|
||||||
* @param provider
|
|
||||||
*/
|
|
||||||
public void onProviderEnabled(String provider) {
|
|
||||||
System.out.println("GpsListener: The provider "+ provider + " is enabled");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when the provider status changes. This method is called when a
|
|
||||||
* provider is unable to fetch a location or if the provider has recently
|
|
||||||
* become available after a period of unavailability.
|
|
||||||
*
|
|
||||||
* @param provider
|
|
||||||
* @param status
|
|
||||||
* @param extras
|
|
||||||
*/
|
|
||||||
public void onStatusChanged(String provider, int status, Bundle extras) {
|
|
||||||
System.out.println("GpsListener: The status of the provider " + provider + " has changed");
|
|
||||||
if (status == 0) {
|
|
||||||
System.out.println("GpsListener: " + provider + " is OUT OF SERVICE");
|
|
||||||
this.owner.fail(GeoListener.POSITION_UNAVAILABLE, "GPS out of service.");
|
|
||||||
}
|
|
||||||
else if (status == 1) {
|
|
||||||
System.out.println("GpsListener: " + provider + " is TEMPORARILY_UNAVAILABLE");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
System.out.println("GpsListener: " + provider + " is Available");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when the location has changed.
|
|
||||||
*
|
|
||||||
* @param location
|
|
||||||
*/
|
|
||||||
public void onLocationChanged(Location location) {
|
|
||||||
System.out.println("GpsListener: The location has been updated!");
|
|
||||||
this.hasData = true;
|
|
||||||
this.cLoc = location;
|
|
||||||
this.owner.success(location);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine if location data is available.
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public boolean hasLocation() {
|
|
||||||
return this.hasData;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start requesting location updates.
|
* Start requesting location updates.
|
||||||
*
|
*
|
||||||
* @param interval
|
* @param interval
|
||||||
*/
|
*/
|
||||||
public void start(int interval) {
|
public void start() {
|
||||||
if (!this.running) {
|
if (!this.running) {
|
||||||
this.running = true;
|
if (this.locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
|
||||||
this.mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, interval, 0, this);
|
this.running = true;
|
||||||
this.getLocation();
|
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this);
|
||||||
|
} else {
|
||||||
// If GPS provider has data, then send now
|
this.fail(CordovaLocationListener.POSITION_UNAVAILABLE, "GPS provider is not available.");
|
||||||
if (this.hasData) {
|
|
||||||
this.owner.success(this.cLoc);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop receiving location updates.
|
|
||||||
*/
|
|
||||||
public void stop() {
|
|
||||||
if (this.running) {
|
|
||||||
this.mLocMan.removeUpdates(this);
|
|
||||||
}
|
|
||||||
this.running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,138 +16,23 @@
|
|||||||
specific language governing permissions and limitations
|
specific language governing permissions and limitations
|
||||||
under the License.
|
under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.apache.cordova;
|
package org.apache.cordova;
|
||||||
|
|
||||||
import org.apache.cordova.api.CordovaInterface;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.location.Location;
|
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.location.LocationListener;
|
|
||||||
import android.os.Bundle;
|
|
||||||
|
|
||||||
public class NetworkListener implements LocationListener {
|
/**
|
||||||
|
* This class handles requests for GPS location services.
|
||||||
private CordovaInterface mCtx; // CordovaActivity object
|
*
|
||||||
|
*/
|
||||||
private LocationManager mLocMan; // Location manager object
|
public class NetworkListener extends CordovaLocationListener {
|
||||||
private GeoListener owner; // Geolistener object (parent)
|
public NetworkListener(LocationManager locationManager, GeoBroker m) {
|
||||||
private boolean hasData = false; // Flag indicates if location data is available in cLoc
|
super(locationManager, m, "[Cordova NetworkListener]");
|
||||||
private Location cLoc; // Last recieved location
|
if (this.locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
|
||||||
private boolean running = false; // Flag indicates if listener is running
|
// If network provider, then create and start network listener
|
||||||
|
this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 10, this);
|
||||||
/**
|
} else {
|
||||||
* Constructor.
|
this.fail(CordovaLocationListener.POSITION_UNAVAILABLE, "Network provider is not available.");
|
||||||
* Automatically starts listening.
|
}
|
||||||
*
|
|
||||||
* @param ctx
|
|
||||||
* @param interval
|
|
||||||
* @param m
|
|
||||||
*/
|
|
||||||
public NetworkListener(CordovaInterface ctx, int interval, GeoListener m) {
|
|
||||||
this.owner = m;
|
|
||||||
this.mCtx = ctx;
|
|
||||||
this.mLocMan = (LocationManager) this.mCtx.getSystemService(Context.LOCATION_SERVICE);
|
|
||||||
this.running = false;
|
|
||||||
this.start(interval);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get last location.
|
|
||||||
*
|
|
||||||
* @return Location object
|
|
||||||
*/
|
|
||||||
public Location getLocation() {
|
|
||||||
this.cLoc = this.mLocMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
|
||||||
if (this.cLoc != null) {
|
|
||||||
this.hasData = true;
|
|
||||||
}
|
|
||||||
return this.cLoc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when the provider is disabled by the user.
|
|
||||||
*
|
|
||||||
* @param provider
|
|
||||||
*/
|
|
||||||
public void onProviderDisabled(String provider) {
|
|
||||||
System.out.println("NetworkListener: The provider " + provider + " is disabled");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when the provider is enabled by the user.
|
|
||||||
*
|
|
||||||
* @param provider
|
|
||||||
*/
|
|
||||||
public void onProviderEnabled(String provider) {
|
|
||||||
System.out.println("NetworkListener: The provider "+ provider + " is enabled");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when the provider status changes. This method is called when a
|
|
||||||
* provider is unable to fetch a location or if the provider has recently
|
|
||||||
* become available after a period of unavailability.
|
|
||||||
*
|
|
||||||
* @param provider
|
|
||||||
* @param status
|
|
||||||
* @param extras
|
|
||||||
*/
|
|
||||||
public void onStatusChanged(String provider, int status, Bundle extras) {
|
|
||||||
System.out.println("NetworkListener: The status of the provider " + provider + " has changed");
|
|
||||||
if (status == 0) {
|
|
||||||
System.out.println("NetworkListener: " + provider + " is OUT OF SERVICE");
|
|
||||||
}
|
|
||||||
else if (status == 1) {
|
|
||||||
System.out.println("NetworkListener: " + provider + " is TEMPORARILY_UNAVAILABLE");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
System.out.println("NetworkListener: " + provider + " is Available");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when the location has changed.
|
|
||||||
*
|
|
||||||
* @param location
|
|
||||||
*/
|
|
||||||
public void onLocationChanged(Location location) {
|
|
||||||
System.out.println("NetworkListener: The location has been updated!");
|
|
||||||
this.hasData = true;
|
|
||||||
this.cLoc = location;
|
|
||||||
|
|
||||||
// The GPS is the primary form of Geolocation in Cordova.
|
|
||||||
// Only fire the success variables if the GPS is down for some reason.
|
|
||||||
if (!this.owner.mGps.hasLocation()) {
|
|
||||||
this.owner.success(location);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start requesting location updates.
|
|
||||||
*
|
|
||||||
* @param interval
|
|
||||||
*/
|
|
||||||
public void start(int interval) {
|
|
||||||
if (!this.running) {
|
|
||||||
this.running = true;
|
|
||||||
this.mLocMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, interval, 0, this);
|
|
||||||
this.getLocation();
|
|
||||||
|
|
||||||
// If Network provider has data but GPS provider doesn't, then send ours
|
|
||||||
if (this.hasData && !this.owner.mGps.hasLocation()) {
|
|
||||||
this.owner.success(this.cLoc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop receiving location updates.
|
|
||||||
*/
|
|
||||||
public void stop() {
|
|
||||||
if (this.running) {
|
|
||||||
this.mLocMan.removeUpdates(this);
|
|
||||||
}
|
|
||||||
this.running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user