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 2013/04/17 23:23:21 UTC

git commit: Updating Geolocation Plugin

Updated Branches:
  refs/heads/master e91dd49b6 -> 57adba406


Updating Geolocation Plugin


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation/commit/57adba40
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation/tree/57adba40
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation/diff/57adba40

Branch: refs/heads/master
Commit: 57adba406292dd6f107b2cfc7beb2a0621f4efaa
Parents: e91dd49
Author: Joe Bowser <bo...@apache.org>
Authored: Wed Apr 17 14:22:01 2013 -0700
Committer: Joe Bowser <bo...@apache.org>
Committed: Wed Apr 17 14:22:01 2013 -0700

----------------------------------------------------------------------
 src/android/CordovaLocationListener.java |   62 +++++++++++++++++++++----
 src/android/GeoBroker.java               |   18 ++++---
 2 files changed, 63 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation/blob/57adba40/src/android/CordovaLocationListener.java
----------------------------------------------------------------------
diff --git a/src/android/CordovaLocationListener.java b/src/android/CordovaLocationListener.java
index 86ba989..8b2bb97 100755
--- a/src/android/CordovaLocationListener.java
+++ b/src/android/CordovaLocationListener.java
@@ -22,6 +22,8 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Timer;
+import java.util.TimerTask;
 
 import org.apache.cordova.api.CallbackContext;
 
@@ -42,6 +44,8 @@ public class CordovaLocationListener implements LocationListener {
 
     public HashMap<String, CallbackContext> watches = new HashMap<String, CallbackContext>();
     private List<CallbackContext> callbacks = new ArrayList<CallbackContext>();
+    
+    private Timer timer = null;
 
     private String TAG = "[Cordova Location Listener]";
 
@@ -52,11 +56,12 @@ public class CordovaLocationListener implements LocationListener {
     }
 
     protected void fail(int code, String message) {
+    	this.cancelTimer();
         for (CallbackContext callbackContext: this.callbacks)
         {
-            this.owner.fail(code, message, callbackContext);
+            this.owner.fail(code, message, callbackContext, false);
         }
-        if(this.owner.isGlobalListener(this))
+        if(this.owner.isGlobalListener(this) && this.watches.size() == 0)
         {
         	Log.d(TAG, "Stopping global listener");
         	this.stop();
@@ -65,16 +70,17 @@ public class CordovaLocationListener implements LocationListener {
 
         Iterator<CallbackContext> it = this.watches.values().iterator();
         while (it.hasNext()) {
-            this.owner.fail(code, message, it.next());
+            this.owner.fail(code, message, it.next(), true);
         }
     }
 
     private void win(Location loc) {
+    	this.cancelTimer();
         for (CallbackContext callbackContext: this.callbacks)
         {
-            this.owner.win(loc, callbackContext);
+            this.owner.win(loc, callbackContext, false);
         }
-        if(this.owner.isGlobalListener(this))
+        if(this.owner.isGlobalListener(this) && this.watches.size() == 0)
         {
         	Log.d(TAG, "Stopping global listener");
         	this.stop();
@@ -83,7 +89,7 @@ public class CordovaLocationListener implements LocationListener {
 
         Iterator<CallbackContext> it = this.watches.values().iterator();
         while (it.hasNext()) {
-            this.owner.win(loc, it.next());
+            this.owner.win(loc, it.next(), true);
         }
     }
 
@@ -155,8 +161,12 @@ public class CordovaLocationListener implements LocationListener {
             this.start();
         }
     }
-    public void addCallback(CallbackContext callbackContext) {
-        this.callbacks.add(callbackContext);
+    public void addCallback(CallbackContext callbackContext, int timeout) {
+    	if(this.timer == null) {
+    		this.timer = new Timer();
+    	}
+    	this.timer.schedule(new LocationTimeoutTask(callbackContext, this), timeout);
+        this.callbacks.add(callbackContext);        
         if (this.size() == 1) {
             this.start();
         }
@@ -173,7 +183,7 @@ public class CordovaLocationListener implements LocationListener {
     /**
      * Destroy listener.
      */
-    public void destroy() {
+    public void destroy() {    	
         this.stop();
     }
 
@@ -199,9 +209,43 @@ public class CordovaLocationListener implements LocationListener {
      * Stop receiving location updates.
      */
     private void stop() {
+    	this.cancelTimer();
         if (this.running) {
             this.locationManager.removeUpdates(this);
             this.running = false;
         }
     }
+    
+    private void cancelTimer() {
+    	if(this.timer != null) {
+    		this.timer.cancel();
+        	this.timer.purge();
+        	this.timer = null;
+    	}
+    }
+    
+    private class LocationTimeoutTask extends TimerTask {
+    	
+    	private CallbackContext callbackContext = null;
+    	private CordovaLocationListener listener = null;
+    	
+    	public LocationTimeoutTask(CallbackContext callbackContext, CordovaLocationListener listener) {
+    		this.callbackContext = callbackContext;
+    		this.listener = listener;
+    	}
+
+		@Override
+		public void run() {
+			for (CallbackContext callbackContext: listener.callbacks) {
+				if(this.callbackContext == callbackContext) {
+					listener.callbacks.remove(callbackContext);
+					break;
+				}
+			}
+			
+			if(listener.size() == 0) {
+				listener.stop();
+			}
+		}    	
+    }
 }

http://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation/blob/57adba40/src/android/GeoBroker.java
----------------------------------------------------------------------
diff --git a/src/android/GeoBroker.java b/src/android/GeoBroker.java
index 8bcfb32..bfabf4b 100755
--- a/src/android/GeoBroker.java
+++ b/src/android/GeoBroker.java
@@ -38,7 +38,7 @@ import android.location.LocationManager;
 public class GeoBroker extends CordovaPlugin {
     private GPSListener gpsListener;
     private NetworkListener networkListener;
-    private LocationManager locationManager;
+    private LocationManager locationManager;    
 
     /**
      * Constructor.
@@ -73,7 +73,7 @@ public class GeoBroker extends CordovaPlugin {
                     PluginResult result = new PluginResult(PluginResult.Status.OK, this.returnLocationJSON(last));
                     callbackContext.sendPluginResult(result);
                 } else {
-                    this.getCurrentLocation(callbackContext, enableHighAccuracy);
+                    this.getCurrentLocation(callbackContext, enableHighAccuracy, args.optInt(2, 60000));
                 }
             }
             else if (action.equals("addWatch")) {
@@ -102,11 +102,11 @@ public class GeoBroker extends CordovaPlugin {
         this.networkListener.clearWatch(id);
     }
 
-    private void getCurrentLocation(CallbackContext callbackContext, boolean enableHighAccuracy) {
+    private void getCurrentLocation(CallbackContext callbackContext, boolean enableHighAccuracy, int timeout) {
         if (enableHighAccuracy) {
-            this.gpsListener.addCallback(callbackContext);
+            this.gpsListener.addCallback(callbackContext, timeout);
         } else {
-            this.networkListener.addCallback(callbackContext);
+            this.networkListener.addCallback(callbackContext, timeout);
         }
     }
 
@@ -160,8 +160,9 @@ public class GeoBroker extends CordovaPlugin {
         return o;
     }
 
-    public void win(Location loc, CallbackContext callbackContext) {
-        PluginResult result = new PluginResult(PluginResult.Status.OK, this.returnLocationJSON(loc));
+    public void win(Location loc, CallbackContext callbackContext, boolean keepCallback) {
+    	PluginResult result = new PluginResult(PluginResult.Status.OK, this.returnLocationJSON(loc));
+    	result.setKeepCallback(keepCallback);
         callbackContext.sendPluginResult(result);
     }
 
@@ -172,7 +173,7 @@ public class GeoBroker extends CordovaPlugin {
      * @param msg			The error message
      * @throws JSONException 
      */
-    public void fail(int code, String msg, CallbackContext callbackContext) {
+    public void fail(int code, String msg, CallbackContext callbackContext, boolean keepCallback) {
         JSONObject obj = new JSONObject();
         String backup = null;
         try {
@@ -189,6 +190,7 @@ public class GeoBroker extends CordovaPlugin {
             result = new PluginResult(PluginResult.Status.ERROR, backup);
         }
 
+        result.setKeepCallback(keepCallback);
         callbackContext.sendPluginResult(result);
     }