From d81727a08cea2b6bd5ac8d12f9fc1694b5002c94 Mon Sep 17 00:00:00 2001 From: Braden Shepherdson Date: Thu, 11 Oct 2012 18:26:19 -0400 Subject: [PATCH] Port Location listeners and plugin to CordovaPlugin. --- .../cordova/CordovaLocationListener.java | 33 +++-- .../src/org/apache/cordova/GeoBroker.java | 114 +++++++++--------- 2 files changed, 70 insertions(+), 77 deletions(-) diff --git a/framework/src/org/apache/cordova/CordovaLocationListener.java b/framework/src/org/apache/cordova/CordovaLocationListener.java index 0ad441d6..7b7a9f77 100755 --- a/framework/src/org/apache/cordova/CordovaLocationListener.java +++ b/framework/src/org/apache/cordova/CordovaLocationListener.java @@ -22,7 +22,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; -import java.util.Map; + +import org.apache.cordova.api.CallbackContext; import android.location.Location; import android.location.LocationListener; @@ -39,8 +40,8 @@ public class CordovaLocationListener implements LocationListener { private GeoBroker owner; protected boolean running = false; - public HashMap watches = new HashMap(); - private List callbacks = new ArrayList(); + public HashMap watches = new HashMap(); + private List callbacks = new ArrayList(); private String TAG = "[Cordova Location Listener]"; @@ -51,9 +52,9 @@ public class CordovaLocationListener implements LocationListener { } protected void fail(int code, String message) { - for (String callbackId: this.callbacks) + for (CallbackContext callbackContext: this.callbacks) { - this.owner.fail(code, message, callbackId); + this.owner.fail(code, message, callbackContext); } if(this.owner.isGlobalListener(this)) { @@ -62,17 +63,16 @@ public class CordovaLocationListener implements LocationListener { } this.callbacks.clear(); - Iterator it = this.watches.entrySet().iterator(); + Iterator it = this.watches.values().iterator(); while (it.hasNext()) { - Map.Entry pairs = (Map.Entry)it.next(); - this.owner.fail(code, message, (String)pairs.getValue()); + this.owner.fail(code, message, it.next()); } } private void win(Location loc) { - for (String callbackId: this.callbacks) + for (CallbackContext callbackContext: this.callbacks) { - this.owner.win(loc, callbackId); + this.owner.win(loc, callbackContext); } if(this.owner.isGlobalListener(this)) { @@ -81,10 +81,9 @@ public class CordovaLocationListener implements LocationListener { } this.callbacks.clear(); - Iterator it = this.watches.entrySet().iterator(); + Iterator it = this.watches.values().iterator(); while (it.hasNext()) { - Map.Entry pairs = (Map.Entry)it.next(); - this.owner.win(loc, (String)pairs.getValue()); + this.owner.win(loc, it.next()); } } @@ -150,14 +149,14 @@ public class CordovaLocationListener implements LocationListener { return this.watches.size() + this.callbacks.size(); } - public void addWatch(String timerId, String callbackId) { - this.watches.put(timerId, callbackId); + public void addWatch(String timerId, CallbackContext callbackContext) { + this.watches.put(timerId, callbackContext); if (this.size() == 1) { this.start(); } } - public void addCallback(String callbackId) { - this.callbacks.add(callbackId); + public void addCallback(CallbackContext callbackContext) { + this.callbacks.add(callbackContext); if (this.size() == 1) { this.start(); } diff --git a/framework/src/org/apache/cordova/GeoBroker.java b/framework/src/org/apache/cordova/GeoBroker.java index e6798a99..6bda653c 100755 --- a/framework/src/org/apache/cordova/GeoBroker.java +++ b/framework/src/org/apache/cordova/GeoBroker.java @@ -18,7 +18,8 @@ */ package org.apache.cordova; -import org.apache.cordova.api.Plugin; +import org.apache.cordova.api.CallbackContext; +import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.PluginResult; import org.json.JSONArray; import org.json.JSONException; @@ -34,7 +35,7 @@ import android.location.LocationManager; * This class only starts and stops various GeoListeners, which consist of a GPS and a Network Listener */ -public class GeoBroker extends Plugin { +public class GeoBroker extends CordovaPlugin { private GPSListener gpsListener; private NetworkListener networkListener; private LocationManager locationManager; @@ -49,53 +50,57 @@ public class GeoBroker extends Plugin { * 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. + * @param args JSONArry of arguments for the plugin. + * @param callbackContext The callback id used when calling back into JavaScript. + * @return True if the action was valid, or false if not. */ - public PluginResult execute(String action, JSONArray args, String callbackId) { + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { if (this.locationManager == null) { this.locationManager = (LocationManager) this.cordova.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 = "Location API is not available for this device."; PluginResult result = new PluginResult(status, message); - + if ( locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) || - locationManager.isProviderEnabled( LocationManager.NETWORK_PROVIDER )) { - - 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 (last != null && (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); - } - 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()); - } + locationManager.isProviderEnabled( LocationManager.NETWORK_PROVIDER )) { + + 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 (last != null && (System.currentTimeMillis() - last.getTime()) <= maximumAge) { + result = new PluginResult(PluginResult.Status.OK, this.returnLocationJSON(last)); + } else { + this.getCurrentLocation(callbackContext, enableHighAccuracy); + } + } + else if (action.equals("addWatch")) { + String id = args.getString(0); + boolean enableHighAccuracy = args.getBoolean(1); + this.addWatch(id, callbackContext, enableHighAccuracy); + } + else if (action.equals("clearWatch")) { + String id = args.getString(0); + this.clearWatch(id); + } + else { + return false; + } + } catch (JSONException e) { + result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage()); + } } - return result; - + callbackContext.sendPluginResult(result); + return true; + } private void clearWatch(String id) { @@ -103,33 +108,22 @@ public class GeoBroker extends Plugin { this.networkListener.clearWatch(id); } - private void getCurrentLocation(String callbackId, boolean enableHighAccuracy) { + private void getCurrentLocation(CallbackContext callbackContext, boolean enableHighAccuracy) { if (enableHighAccuracy) { - this.gpsListener.addCallback(callbackId); + this.gpsListener.addCallback(callbackContext); } else { - this.networkListener.addCallback(callbackId); + this.networkListener.addCallback(callbackContext); } } - private void addWatch(String timerId, String callbackId, boolean enableHighAccuracy) { + private void addWatch(String timerId, CallbackContext callbackContext, boolean enableHighAccuracy) { if (enableHighAccuracy) { - this.gpsListener.addWatch(timerId, callbackId); + this.gpsListener.addWatch(timerId, callbackContext); } else { - this.networkListener.addWatch(timerId, callbackId); + this.networkListener.addWatch(timerId, callbackContext); } } - /** - * Identifies if action to be executed returns a value and should be run synchronously. - * - * @param action The action to execute - * @return T=returns value - */ - public boolean isSynch(String action) { - // 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. @@ -172,9 +166,9 @@ public class GeoBroker extends Plugin { return o; } - public void win(Location loc, String callbackId) { + public void win(Location loc, CallbackContext callbackContext) { PluginResult result = new PluginResult(PluginResult.Status.OK, this.returnLocationJSON(loc)); - this.success(result, callbackId); + callbackContext.sendPluginResult(result); } /** @@ -184,7 +178,7 @@ public class GeoBroker extends Plugin { * @param msg The error message * @throws JSONException */ - public void fail(int code, String msg, String callbackId) { + public void fail(int code, String msg, CallbackContext callbackContext) { JSONObject obj = new JSONObject(); String backup = null; try { @@ -201,9 +195,9 @@ public class GeoBroker extends Plugin { result = new PluginResult(PluginResult.Status.ERROR, backup); } - this.error(result, callbackId); + callbackContext.sendPluginResult(result); } - + public boolean isGlobalListener(CordovaLocationListener listener) { if (gpsListener != null && networkListener != null)