This commit is contained in:
Bryce Curtis 2012-05-14 23:18:26 -05:00
parent d44d9ddca6
commit 01abb32025

View File

@ -54,11 +54,11 @@ public class GeoBroker extends Plugin {
* @return A PluginResult object with a status and message. * @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args, String callbackId) { public PluginResult execute(String action, JSONArray args, String callbackId) {
if (this.locationManager == null) { if (this.locationManager == null) {
this.locationManager = (LocationManager) this.ctx.getSystemService(Context.LOCATION_SERVICE); this.locationManager = (LocationManager) this.ctx.getActivity().getSystemService(Context.LOCATION_SERVICE);
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 = "";
PluginResult result = new PluginResult(status, message); PluginResult result = new PluginResult(status, message);
@ -66,53 +66,53 @@ public class GeoBroker extends Plugin {
try { try {
if (action.equals("getLocation")) { if (action.equals("getLocation")) {
boolean enableHighAccuracy = args.getBoolean(0); boolean enableHighAccuracy = args.getBoolean(0);
int maximumAge = args.getInt(1); int maximumAge = args.getInt(1);
Location last = this.locationManager.getLastKnownLocation((enableHighAccuracy ? LocationManager.GPS_PROVIDER : LocationManager.NETWORK_PROVIDER)); 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 // Check if we can use lastKnownLocation to get a quick reading and use less battery
if ((System.currentTimeMillis() - last.getTime()) <= maximumAge) { if ((System.currentTimeMillis() - last.getTime()) <= maximumAge) {
result = new PluginResult(PluginResult.Status.OK, this.returnLocationJSON(last)); result = new PluginResult(PluginResult.Status.OK, this.returnLocationJSON(last));
} else { } else {
this.getCurrentLocation(callbackId, enableHighAccuracy); this.getCurrentLocation(callbackId, enableHighAccuracy);
} }
} }
else if (action.equals("addWatch")) { else if (action.equals("addWatch")) {
String id = args.getString(0); String id = args.getString(0);
boolean enableHighAccuracy = args.getBoolean(1); boolean enableHighAccuracy = args.getBoolean(1);
this.addWatch(id, callbackId, enableHighAccuracy); this.addWatch(id, callbackId, enableHighAccuracy);
} }
else if (action.equals("clearWatch")) { else if (action.equals("clearWatch")) {
String id = args.getString(0); String id = args.getString(0);
this.clearWatch(id); this.clearWatch(id);
} }
} catch (JSONException e) { } catch (JSONException e) {
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage()); result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
} }
return result; return result;
} }
private void clearWatch(String id) { private void clearWatch(String id) {
this.gpsListener.clearWatch(id); this.gpsListener.clearWatch(id);
this.networkListener.clearWatch(id); this.networkListener.clearWatch(id);
}
private void getCurrentLocation(String callbackId, boolean enableHighAccuracy) {
if (enableHighAccuracy) {
this.gpsListener.addCallback(callbackId);
} else {
this.networkListener.addCallback(callbackId);
}
}
private void addWatch(String timerId, String callbackId, boolean enableHighAccuracy) {
if (enableHighAccuracy) {
this.gpsListener.addWatch(timerId, callbackId);
} else {
this.networkListener.addWatch(timerId, callbackId);
}
} }
/** private void getCurrentLocation(String callbackId, boolean enableHighAccuracy) {
if (enableHighAccuracy) {
this.gpsListener.addCallback(callbackId);
} else {
this.networkListener.addCallback(callbackId);
}
}
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
@ -128,59 +128,60 @@ public class GeoBroker extends Plugin {
* Stop listener. * Stop listener.
*/ */
public void onDestroy() { public void onDestroy() {
this.networkListener.destroy(); this.networkListener.destroy();
this.gpsListener.destroy(); this.gpsListener.destroy();
this.networkListener = null; this.networkListener = null;
this.gpsListener = null; this.gpsListener = null;
} }
public JSONObject returnLocationJSON(Location loc) { public JSONObject returnLocationJSON(Location loc) {
JSONObject o = new JSONObject(); JSONObject o = new JSONObject();
try { try {
o.put("latitude", loc.getLatitude()); o.put("latitude", loc.getLatitude());
o.put("longitude", loc.getLongitude()); o.put("longitude", loc.getLongitude());
o.put("altitude", (loc.hasAltitude() ? loc.getAltitude() : null)); o.put("altitude", (loc.hasAltitude() ? loc.getAltitude() : null));
o.put("accuracy", loc.getAccuracy()); o.put("accuracy", loc.getAccuracy());
o.put("heading", (loc.hasBearing() ? (loc.hasSpeed() ? loc.getBearing() : null) : null)); o.put("heading", (loc.hasBearing() ? (loc.hasSpeed() ? loc.getBearing() : null) : null));
o.put("speed", loc.getSpeed()); o.put("speed", loc.getSpeed());
o.put("timestamp", loc.getTime()); o.put("timestamp", loc.getTime());
} catch (JSONException e) { } catch (JSONException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
return o;
}
return o; public void win(Location loc, String callbackId) {
} PluginResult result = new PluginResult(PluginResult.Status.OK, this.returnLocationJSON(loc));
public void win(Location loc, String callbackId) { this.success(result, callbackId);
PluginResult result = new PluginResult(PluginResult.Status.OK, this.returnLocationJSON(loc)); }
this.success(result, callbackId);
}
/**
* Location failed. Send error back to JavaScript.
*
* @param code The error code
* @param msg The error message
* @throws JSONException
*/
public void fail(int code, String msg, String callbackId) {
JSONObject obj = new JSONObject();
String backup = null;
try {
obj.put("code", code);
obj.put("message", msg);
} catch (JSONException e) {
obj = null;
backup = "{'code':" + code + ",'message':'" + msg.replaceAll("'", "\'") + "'}";
}
PluginResult result;
if (obj != null) {
result = new PluginResult(PluginResult.Status.ERROR, obj);
} else {
result = new PluginResult(PluginResult.Status.ERROR, backup);
}
this.error(result, callbackId); /**
} * Location failed. Send error back to JavaScript.
*
* @param code The error code
* @param msg The error message
* @throws JSONException
*/
public void fail(int code, String msg, String callbackId) {
JSONObject obj = new JSONObject();
String backup = null;
try {
obj.put("code", code);
obj.put("message", msg);
} catch (JSONException e) {
obj = null;
backup = "{'code':" + code + ",'message':'" + msg.replaceAll("'", "\'") + "'}";
}
PluginResult result;
if (obj != null) {
result = new PluginResult(PluginResult.Status.ERROR, obj);
} else {
result = new PluginResult(PluginResult.Status.ERROR, backup);
}
this.error(result, callbackId);
}
} }