You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by bo...@apache.org on 2012/06/07 22:20:55 UTC
[46/50] [abbrv] android commit: Update
Update
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/01abb320
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/01abb320
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/01abb320
Branch: refs/heads/master
Commit: 01abb3202544820f8ad821079e42fce7d2b6a711
Parents: d44d9dd
Author: Bryce Curtis <cu...@gmail.com>
Authored: Mon May 14 23:18:26 2012 -0500
Committer: Bryce Curtis <cu...@gmail.com>
Committed: Mon May 14 23:18:26 2012 -0500
----------------------------------------------------------------------
framework/src/org/apache/cordova/GeoBroker.java | 183 +++++++++---------
1 files changed, 92 insertions(+), 91 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/01abb320/framework/src/org/apache/cordova/GeoBroker.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/GeoBroker.java b/framework/src/org/apache/cordova/GeoBroker.java
index 6a150b3..18d405b 100755
--- a/framework/src/org/apache/cordova/GeoBroker.java
+++ b/framework/src/org/apache/cordova/GeoBroker.java
@@ -38,7 +38,7 @@ public class GeoBroker extends Plugin {
private GPSListener gpsListener;
private NetworkListener networkListener;
private LocationManager locationManager;
-
+
/**
* Constructor.
*/
@@ -54,65 +54,65 @@ public class GeoBroker extends Plugin {
* @return A PluginResult object with a status and message.
*/
public PluginResult execute(String action, JSONArray args, String callbackId) {
- if (this.locationManager == null) {
- this.locationManager = (LocationManager) this.ctx.getSystemService(Context.LOCATION_SERVICE);
- this.networkListener = new NetworkListener(this.locationManager, this);
- this.gpsListener = new GPSListener(this.locationManager, this);
- }
+ if (this.locationManager == null) {
+ this.locationManager = (LocationManager) this.ctx.getActivity().getSystemService(Context.LOCATION_SERVICE);
+ this.networkListener = new NetworkListener(this.locationManager, this);
+ this.gpsListener = new GPSListener(this.locationManager, this);
+ }
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, this.returnLocationJSON(last));
- } else {
- this.getCurrentLocation(callbackId, enableHighAccuracy);
- }
+ 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, this.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);
+ 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);
+ String id = args.getString(0);
+ this.clearWatch(id);
}
} catch (JSONException e) {
- result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
+ 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) {
- if (enableHighAccuracy) {
- this.gpsListener.addCallback(callbackId);
- } else {
- this.networkListener.addCallback(callbackId);
- }
- }
-
+ this.gpsListener.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);
- }
+ 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.
*
* @param action The action to execute
@@ -122,65 +122,66 @@ public class GeoBroker extends Plugin {
// Starting listeners is easier to run on main thread, so don't run async.
return true;
}
-
+
/**
* Called when the activity is to be shut down.
* Stop listener.
*/
public void onDestroy() {
- this.networkListener.destroy();
- this.gpsListener.destroy();
+ this.networkListener.destroy();
+ this.gpsListener.destroy();
this.networkListener = null;
this.gpsListener = null;
}
public JSONObject returnLocationJSON(Location loc) {
- JSONObject o = new JSONObject();
-
- try {
- o.put("latitude", loc.getLatitude());
- o.put("longitude", loc.getLongitude());
- o.put("altitude", (loc.hasAltitude() ? loc.getAltitude() : null));
- o.put("accuracy", loc.getAccuracy());
- o.put("heading", (loc.hasBearing() ? (loc.hasSpeed() ? loc.getBearing() : null) : null));
- o.put("speed", loc.getSpeed());
- o.put("timestamp", loc.getTime());
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
-
- return o;
- }
- public void win(Location loc, String 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);
- }
+ JSONObject o = new JSONObject();
+
+ try {
+ o.put("latitude", loc.getLatitude());
+ o.put("longitude", loc.getLongitude());
+ o.put("altitude", (loc.hasAltitude() ? loc.getAltitude() : null));
+ o.put("accuracy", loc.getAccuracy());
+ o.put("heading", (loc.hasBearing() ? (loc.hasSpeed() ? loc.getBearing() : null) : null));
+ o.put("speed", loc.getSpeed());
+ o.put("timestamp", loc.getTime());
+ } catch (JSONException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return o;
+ }
+
+ public void win(Location loc, String 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);
+ }
}