Port Location listeners and plugin to CordovaPlugin.

This commit is contained in:
Braden Shepherdson 2012-10-11 18:26:19 -04:00
parent b582e1592a
commit d81727a08c
2 changed files with 70 additions and 77 deletions

View File

@ -22,7 +22,8 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import org.apache.cordova.api.CallbackContext;
import android.location.Location; import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
@ -39,8 +40,8 @@ public class CordovaLocationListener implements LocationListener {
private GeoBroker owner; private GeoBroker owner;
protected boolean running = false; protected boolean running = false;
public HashMap<String, String> watches = new HashMap<String, String>(); public HashMap<String, CallbackContext> watches = new HashMap<String, CallbackContext>();
private List<String> callbacks = new ArrayList<String>(); private List<CallbackContext> callbacks = new ArrayList<CallbackContext>();
private String TAG = "[Cordova Location Listener]"; private String TAG = "[Cordova Location Listener]";
@ -51,9 +52,9 @@ public class CordovaLocationListener implements LocationListener {
} }
protected void fail(int code, String message) { 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)) if(this.owner.isGlobalListener(this))
{ {
@ -62,17 +63,16 @@ public class CordovaLocationListener implements LocationListener {
} }
this.callbacks.clear(); this.callbacks.clear();
Iterator it = this.watches.entrySet().iterator(); Iterator<CallbackContext> it = this.watches.values().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next(); this.owner.fail(code, message, it.next());
this.owner.fail(code, message, (String)pairs.getValue());
} }
} }
private void win(Location loc) { 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)) if(this.owner.isGlobalListener(this))
{ {
@ -81,10 +81,9 @@ public class CordovaLocationListener implements LocationListener {
} }
this.callbacks.clear(); this.callbacks.clear();
Iterator it = this.watches.entrySet().iterator(); Iterator<CallbackContext> it = this.watches.values().iterator();
while (it.hasNext()) { while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next(); this.owner.win(loc, it.next());
this.owner.win(loc, (String)pairs.getValue());
} }
} }
@ -150,14 +149,14 @@ public class CordovaLocationListener implements LocationListener {
return this.watches.size() + this.callbacks.size(); return this.watches.size() + this.callbacks.size();
} }
public void addWatch(String timerId, String callbackId) { public void addWatch(String timerId, CallbackContext callbackContext) {
this.watches.put(timerId, callbackId); this.watches.put(timerId, callbackContext);
if (this.size() == 1) { if (this.size() == 1) {
this.start(); this.start();
} }
} }
public void addCallback(String callbackId) { public void addCallback(CallbackContext callbackContext) {
this.callbacks.add(callbackId); this.callbacks.add(callbackContext);
if (this.size() == 1) { if (this.size() == 1) {
this.start(); this.start();
} }

View File

@ -18,7 +18,8 @@
*/ */
package org.apache.cordova; 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.apache.cordova.api.PluginResult;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; 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 * 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 GPSListener gpsListener;
private NetworkListener networkListener; private NetworkListener networkListener;
private LocationManager locationManager; private LocationManager locationManager;
@ -49,53 +50,57 @@ public class GeoBroker extends Plugin {
* Executes the request and returns PluginResult. * Executes the request and returns PluginResult.
* *
* @param action The action to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the plugin. * @param args JSONArry of arguments for the plugin.
* @param callbackId The callback id used when calling back into JavaScript. * @param callbackContext The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message. * @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) { if (this.locationManager == null) {
this.locationManager = (LocationManager) this.cordova.getActivity().getSystemService(Context.LOCATION_SERVICE); this.locationManager = (LocationManager) this.cordova.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 = "Location API is not available for this device."; String message = "Location API is not available for this device.";
PluginResult result = new PluginResult(status, message); PluginResult result = new PluginResult(status, message);
if ( locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) || if ( locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ||
locationManager.isProviderEnabled( LocationManager.NETWORK_PROVIDER )) { locationManager.isProviderEnabled( LocationManager.NETWORK_PROVIDER )) {
result.setKeepCallback(true); result.setKeepCallback(true);
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 (last != null && (System.currentTimeMillis() - last.getTime()) <= maximumAge) { if (last != null && (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(callbackContext, 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, callbackContext, 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) { else {
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage()); 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) { private void clearWatch(String id) {
@ -103,33 +108,22 @@ public class GeoBroker extends Plugin {
this.networkListener.clearWatch(id); this.networkListener.clearWatch(id);
} }
private void getCurrentLocation(String callbackId, boolean enableHighAccuracy) { private void getCurrentLocation(CallbackContext callbackContext, boolean enableHighAccuracy) {
if (enableHighAccuracy) { if (enableHighAccuracy) {
this.gpsListener.addCallback(callbackId); this.gpsListener.addCallback(callbackContext);
} else { } 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) { if (enableHighAccuracy) {
this.gpsListener.addWatch(timerId, callbackId); this.gpsListener.addWatch(timerId, callbackContext);
} else { } 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. * Called when the activity is to be shut down.
* Stop listener. * Stop listener.
@ -172,9 +166,9 @@ public class GeoBroker extends Plugin {
return o; 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)); 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 * @param msg The error message
* @throws JSONException * @throws JSONException
*/ */
public void fail(int code, String msg, String callbackId) { public void fail(int code, String msg, CallbackContext callbackContext) {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
String backup = null; String backup = null;
try { try {
@ -201,9 +195,9 @@ public class GeoBroker extends Plugin {
result = new PluginResult(PluginResult.Status.ERROR, backup); result = new PluginResult(PluginResult.Status.ERROR, backup);
} }
this.error(result, callbackId); callbackContext.sendPluginResult(result);
} }
public boolean isGlobalListener(CordovaLocationListener listener) public boolean isGlobalListener(CordovaLocationListener listener)
{ {
if (gpsListener != null && networkListener != null) if (gpsListener != null && networkListener != null)