cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bra...@apache.org
Subject [8/18] android commit: Port Location listeners and plugin to CordovaPlugin.
Date Fri, 12 Oct 2012 21:06:18 GMT
Port Location listeners and plugin to CordovaPlugin.


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/d81727a0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/d81727a0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/d81727a0

Branch: refs/heads/master
Commit: d81727a08cea2b6bd5ac8d12f9fc1694b5002c94
Parents: b582e15
Author: Braden Shepherdson <braden@chromium.org>
Authored: Thu Oct 11 18:26:19 2012 -0400
Committer: Braden Shepherdson <braden@chromium.org>
Committed: Thu Oct 11 18:26:19 2012 -0400

----------------------------------------------------------------------
 .../apache/cordova/CordovaLocationListener.java    |   33 ++--
 framework/src/org/apache/cordova/GeoBroker.java    |  114 +++++++--------
 2 files changed, 70 insertions(+), 77 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/d81727a0/framework/src/org/apache/cordova/CordovaLocationListener.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaLocationListener.java b/framework/src/org/apache/cordova/CordovaLocationListener.java
index 0ad441d..7b7a9f7 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<String, String> watches = new HashMap<String, String>();
-    private List<String> callbacks = new ArrayList<String>();
+    public HashMap<String, CallbackContext> watches = new HashMap<String, CallbackContext>();
+    private List<CallbackContext> callbacks = new ArrayList<CallbackContext>();
 
     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<CallbackContext> 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<CallbackContext> 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();
         }

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/d81727a0/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 e6798a9..6bda653 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,34 +108,23 @@ 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)


Mime
View raw message