GeoBroker checks if location service avialable for device first.

This commit is contained in:
Evgeni Petrov 2012-08-08 11:23:08 +03:00 committed by Andrew Grieve
parent c52dc10c9e
commit e575212c49

View File

@ -59,36 +59,43 @@ public class GeoBroker extends Plugin {
this.networkListener = new NetworkListener(this.locationManager, this); this.networkListener = new NetworkListener(this.locationManager, this);
this.gpsListener = new GPSListener(this.locationManager, this); this.gpsListener = new GPSListener(this.locationManager, this);
} }
PluginResult.Status status = PluginResult.Status.NO_RESULT; PluginResult.Status status = PluginResult.Status.NO_RESULT;
String message = ""; String message = "Location API is not available for this device.";
PluginResult result = new PluginResult(status, message); PluginResult result = new PluginResult(status, message);
result.setKeepCallback(true);
if ( locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ||
try { locationManager.isProviderEnabled( LocationManager.NETWORK_PROVIDER )) {
if (action.equals("getLocation")) {
boolean enableHighAccuracy = args.getBoolean(0); result.setKeepCallback(true);
int maximumAge = args.getInt(1);
Location last = this.locationManager.getLastKnownLocation((enableHighAccuracy ? LocationManager.GPS_PROVIDER : LocationManager.NETWORK_PROVIDER)); try {
// Check if we can use lastKnownLocation to get a quick reading and use less battery if (action.equals("getLocation")) {
if ((System.currentTimeMillis() - last.getTime()) <= maximumAge) { boolean enableHighAccuracy = args.getBoolean(0);
result = new PluginResult(PluginResult.Status.OK, this.returnLocationJSON(last)); int maximumAge = args.getInt(1);
} else { Location last = this.locationManager.getLastKnownLocation((enableHighAccuracy ? LocationManager.GPS_PROVIDER : LocationManager.NETWORK_PROVIDER));
this.getCurrentLocation(callbackId, enableHighAccuracy); // 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, this.returnLocationJSON(last));
else if (action.equals("addWatch")) { } else {
String id = args.getString(0); this.getCurrentLocation(callbackId, enableHighAccuracy);
boolean enableHighAccuracy = args.getBoolean(1); }
this.addWatch(id, callbackId, enableHighAccuracy); }
} else if (action.equals("addWatch")) {
else if (action.equals("clearWatch")) { String id = args.getString(0);
String id = args.getString(0); boolean enableHighAccuracy = args.getBoolean(1);
this.clearWatch(id); this.addWatch(id, callbackId, enableHighAccuracy);
} }
} catch (JSONException e) { else if (action.equals("clearWatch")) {
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage()); String id = args.getString(0);
this.clearWatch(id);
}
} catch (JSONException e) {
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
}
} }
return result; return result;
} }
private void clearWatch(String id) { private void clearWatch(String id) {