restructured geolocation plugin

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

Branch: refs/heads/CordovaWebView
Commit: 724ea49f0b4e26286fda38b380f3f5d128ad5d15
Parents: 6fc2a3b
Author: Fil Maj <maj....@gmail.com>
Authored: Fri Mar 30 01:48:12 2012 -0700
Committer: Fil Maj <maj....@gmail.com>
Committed: Mon May 7 16:09:20 2012 -0700

----------------------------------------------------------------------
 .../apache/cordova/CordovaLocationListener.java    |  198 ++++++++++++
 framework/src/org/apache/cordova/GeoBroker.java    |  243 ++++++++-------
 framework/src/org/apache/cordova/GeoListener.java  |  133 --------
 framework/src/org/apache/cordova/GpsListener.java  |  138 +--------
 .../src/org/apache/cordova/NetworkListener.java    |  143 +--------
 5 files changed, 354 insertions(+), 501 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/724ea49f/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
new file mode 100755
index 0000000..6eccfb4
--- /dev/null
+++ b/framework/src/org/apache/cordova/CordovaLocationListener.java
@@ -0,0 +1,198 @@
+/*
+       Licensed to the Apache Software Foundation (ASF) under one
+       or more contributor license agreements.  See the NOTICE file
+       distributed with this work for additional information
+       regarding copyright ownership.  The ASF licenses this file
+       to you under the Apache License, Version 2.0 (the
+       "License"); you may not use this file except in compliance
+       with the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+       Unless required by applicable law or agreed to in writing,
+       software distributed under the License is distributed on an
+       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+       KIND, either express or implied.  See the License for the
+       specific language governing permissions and limitations
+       under the License.
+*/
+package org.apache.cordova;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import android.location.Location;
+import android.location.LocationListener;
+import android.location.LocationManager;
+import android.os.Bundle;
+import android.util.Log;
+
+public class CordovaLocationListener implements LocationListener {
+    public static int PERMISSION_DENIED = 1;
+    public static int POSITION_UNAVAILABLE = 2;
+    public static int TIMEOUT = 3;
+
+    protected LocationManager locationManager;
+    private GeoBroker owner;
+    protected boolean running = false;
+    
+    public HashMap<String, String> watches = new HashMap<String, String>();
+    private List<String> callbacks = new ArrayList<String>();
+
+    private String TAG = "[Cordova Location Listener]";
+       
+    public CordovaLocationListener(LocationManager manager, GeoBroker broker, 
String tag) {
+       this.locationManager = manager;
+       this.owner = broker;
+       this.TAG = tag;
+    }
+    
+    protected void fail(int code, String message) {
+        for (String callbackId: this.callbacks)
+        {
+               this.owner.fail(code, message, callbackId);
+        }
+        this.callbacks.clear();
+        
+        Iterator it = this.watches.entrySet().iterator();
+        while (it.hasNext()) {
+            Map.Entry pairs = (Map.Entry)it.next();
+            this.owner.fail(code, message, (String)pairs.getValue());
+        }
+    }
+    
+    private void win(Location loc) {
+       for (String callbackId: this.callbacks)
+        {
+               this.owner.win(loc, callbackId);
+        }
+        this.callbacks.clear();
+        
+        Iterator it = this.watches.entrySet().iterator();
+        while (it.hasNext()) {
+            Map.Entry pairs = (Map.Entry)it.next();
+            this.owner.win(loc, (String)pairs.getValue());
+        }
+    }
+    
+    /**
+     * Location Listener Methods 
+     */
+    
+       /**
+        * Called when the provider is disabled by the user.
+        * 
+        * @param provider
+        */
+       public void onProviderDisabled(String provider) {
+               Log.d(TAG, "Location provider '" + provider + "' disabled.");
+               this.fail(POSITION_UNAVAILABLE, "GPS provider disabled.");
+       }
+
+       /**
+        * Called when the provider is enabled by the user.
+        * 
+        * @param provider
+        */
+       public void onProviderEnabled(String provider) {
+               Log.d(TAG, "Location provider "+ provider + " has been 
enabled");
+       }
+
+       /**
+        * Called when the provider status changes. This method is called when 
a 
+        * provider is unable to fetch a location or if the provider has 
recently 
+        * become available after a period of unavailability.
+        * 
+        * @param provider
+        * @param status
+        * @param extras
+        */
+       public void onStatusChanged(String provider, int status, Bundle extras) 
{
+               Log.d(TAG, "The status of the provider " + provider + " has 
changed");
+               if (status == 0) {
+                       Log.d(TAG, provider + " is OUT OF SERVICE");
+                       this.fail(CordovaLocationListener.POSITION_UNAVAILABLE, 
"Provider " + provider + " is out of service.");
+               }
+               else if (status == 1) {
+                       Log.d(TAG, provider + " is TEMPORARILY_UNAVAILABLE");
+               }
+               else {
+                       Log.d(TAG, provider + " is AVAILABLE");
+               }
+       }
+
+       /**
+        * Called when the location has changed.
+        * 
+        * @param location
+        */
+       public void onLocationChanged(Location location) {
+               Log.d(TAG, "The location has been updated!");
+               this.win(location);
+       }
+       
+       // PUBLIC
+       
+       public int size() {
+               return this.watches.size() + this.callbacks.size();
+       }
+       
+       public void addWatch(String timerId, String callbackId) {
+               this.watches.put(timerId, callbackId);
+               if (this.size() == 1) {
+                       this.start();
+               }
+       }
+       public void addCallback(String callbackId) {
+               this.callbacks.add(callbackId);
+               if (this.size() == 1) {
+                       this.start();
+               }
+       }
+       public void clearWatch(String timerId) {
+               if (this.watches.containsKey(timerId)) {
+                       this.watches.remove(timerId);
+               }
+               if (this.size() == 0) {
+                       this.stop();
+               }
+       }
+    
+    /**
+     * Destroy listener.
+     */
+    public void destroy() {
+       this.stop();
+    }
+       
+       // LOCAL
+       
+       /**
+        * Start requesting location updates.
+        * 
+        * @param interval
+        */
+       private void start() {
+               if (!this.running) {
+                       if 
(this.locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
+                               this.running = true;
+                               
this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
60000, 10, this);
+                       } else {
+                               
this.fail(CordovaLocationListener.POSITION_UNAVAILABLE, "Network provider is 
not available.");
+                       }
+               }
+       }
+
+       /**
+        * Stop receiving location updates.
+        */
+       private void stop() {
+               if (this.running) {
+                       this.locationManager.removeUpdates(this);
+                       this.running = false;
+               }
+       }
+}

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/724ea49f/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 aa89d5a..b8cfe4e 100755
--- a/framework/src/org/apache/cordova/GeoBroker.java
+++ b/framework/src/org/apache/cordova/GeoBroker.java
@@ -18,14 +18,15 @@
 */
 package org.apache.cordova;
 
-import java.util.HashMap;
-import java.util.Map.Entry;
-
 import org.apache.cordova.api.Plugin;
 import org.apache.cordova.api.PluginResult;
 import org.json.JSONArray;
 import org.json.JSONException;
+import org.json.JSONObject;
 
+import android.content.Context;
+import android.location.Location;
+import android.location.LocationManager;
 
 /*
  * This class is the interface to the Geolocation.  It's bound to the geo 
object.
@@ -34,132 +35,142 @@ import org.json.JSONException;
  */
 
 public class GeoBroker extends Plugin {
-    
-    // List of gGeolocation listeners
-    private HashMap<String, GeoListener> geoListeners;
-       private GeoListener global;
+    private GPSListener gpsListener;
+    private NetworkListener networkListener;
+    private LocationManager locationManager;
        
-       /**
-        * Constructor.
-        */
-       public GeoBroker() {
-               this.geoListeners = new HashMap<String, GeoListener>();
+    /**
+     * Constructor.
+     */
+    public GeoBroker() {
+       this.locationManager = (LocationManager) 
this.ctx.getSystemService(Context.LOCATION_SERVICE);
+       this.networkListener = new NetworkListener(this.locationManager, this);
+       this.gpsListener = new GPSListener(this.locationManager, this);
+    }
+
+    /**
+     * 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.
+     */
+    public PluginResult execute(String action, JSONArray args, String 
callbackId) {
+        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, 
GeoBroker.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());
+        }
+        return result;
+    }
+
+    private void clearWatch(String id) {
+               this.gpsListener.clearWatch(id);
+               this.networkListener.clearWatch(id);
        }
 
-       /**
-        * 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.
-        */
-       public PluginResult execute(String action, JSONArray args, String 
callbackId) {
-               PluginResult.Status status = PluginResult.Status.OK;
-               String result = "";             
-               
-               try {
-                       if (action.equals("getCurrentLocation")) {
-                               this.getCurrentLocation(args.getBoolean(0), 
args.getInt(1), args.getInt(2));
-                       }
-                       else if (action.equals("start")) {
-                               String s = this.start(args.getString(0), 
args.getBoolean(1), args.getInt(2), args.getInt(3));
-                               return new PluginResult(status, s);
-                       }
-                       else if (action.equals("stop")) {
-                               this.stop(args.getString(0));
-                       }
-                       return new PluginResult(status, result);
-               } catch (JSONException e) {
-                       return new 
PluginResult(PluginResult.Status.JSON_EXCEPTION);
+       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);
+       }
+    }
 
        /**
-        * 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;
-       }
+     * 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.
      */
     public void onDestroy() {
-               java.util.Set<Entry<String,GeoListener>> s = 
this.geoListeners.entrySet();
-        java.util.Iterator<Entry<String,GeoListener>> it = s.iterator();
-        while (it.hasNext()) {
-            Entry<String,GeoListener> entry = it.next();
-            GeoListener listener = entry.getValue();
-            listener.destroy();
-               }
-        this.geoListeners.clear();
-        if (this.global != null) {
-               this.global.destroy();
-        }
-        this.global = null;
+       this.networkListener.destroy();
+       this.gpsListener.destroy();
+        this.networkListener = null;
+        this.gpsListener = null;
     }
 
-    
//--------------------------------------------------------------------------
-    // LOCAL METHODS
-    
//--------------------------------------------------------------------------
-
-    /**
-     * Get current location.
-     * The result is returned to JavaScript via a callback.
-     * 
-        * @param enableHighAccuracy
-        * @param timeout
-        * @param maximumAge
-     */
-       public void getCurrentLocation(boolean enableHighAccuracy, int timeout, 
int maximumAge) {
-               
-               // Create a geolocation listener just for getCurrentLocation 
and call it "global"
-               if (this.global == null) {
-                       this.global = new GeoListener(this, "global", 
maximumAge);
-               }
-               else {
-                       this.global.start(maximumAge);
-               }
-       }
-       
-       /**
-        * Start geolocation listener and add to listener list.
-        * 
-        * @param key                                   The listener id
-        * @param enableHighAccuracy
-        * @param timeout
-        * @param maximumAge
-        * @return
-        */
-       public String start(String key, boolean enableHighAccuracy, int 
timeout, int maximumAge) {
-               
-               // Make sure this listener doesn't already exist
-               GeoListener listener = geoListeners.get(key);
-               if (listener == null) {
-                       listener = new GeoListener(this, key, maximumAge);
-                       geoListeners.put(key, listener);
-               }
-               
-               // Start it
-               listener.start(maximumAge);
-               return key;
-       }
-       
-       /**
-        * Stop geolocation listener and remove from listener list.
-        * 
-        * @param key                   The listener id
-        */
-       public void stop(String key) {
-               GeoListener listener = geoListeners.remove(key);
-               if (listener != null) {
-                       listener.stop();
-               }
-       }
+    public static String returnLocationJSON(Location loc) {
+        return "{" + 
+                 "'latitude':" + loc.getLatitude() + "," + 
+                 "'longitude':" + loc.getLongitude() + "," + 
+                 "'altitude':" + (loc.hasAltitude() ? loc.getAltitude() : 
"null") + "," + 
+                 "'accuracy':" + loc.getAccuracy() + "," + 
+                 "'heading':" + (loc.hasBearing() ? (loc.hasSpeed() ? 
loc.getBearing() : "NaN") : "null") + "," + 
+                 "'speed':" + loc.getSpeed() + "," + 
+                 "'timestamp':" + loc.getTime() + 
+               "}"; 
+      }
+      public void win(Location loc, String callbackId) {
+         PluginResult result = new PluginResult(PluginResult.Status.OK, 
GeoBroker.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);
+      }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/724ea49f/framework/src/org/apache/cordova/GeoListener.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/GeoListener.java 
b/framework/src/org/apache/cordova/GeoListener.java
deleted file mode 100755
index fdfcc42..0000000
--- a/framework/src/org/apache/cordova/GeoListener.java
+++ /dev/null
@@ -1,133 +0,0 @@
-/*
-       Licensed to the Apache Software Foundation (ASF) under one
-       or more contributor license agreements.  See the NOTICE file
-       distributed with this work for additional information
-       regarding copyright ownership.  The ASF licenses this file
-       to you under the Apache License, Version 2.0 (the
-       "License"); you may not use this file except in compliance
-       with the License.  You may obtain a copy of the License at
-
-         http://www.apache.org/licenses/LICENSE-2.0
-
-       Unless required by applicable law or agreed to in writing,
-       software distributed under the License is distributed on an
-       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-       KIND, either express or implied.  See the License for the
-       specific language governing permissions and limitations
-       under the License.
-*/
-package org.apache.cordova;
-
-import android.content.Context;
-import android.location.Location;
-import android.location.LocationManager;
-import android.webkit.WebView;
-
-public class GeoListener {
-       public static int PERMISSION_DENIED = 1;
-       public static int POSITION_UNAVAILABLE = 2;
-       public static int TIMEOUT = 3;
-
-       String id;                                                      // 
Listener ID
-       String successCallback;                         // 
-       String failCallback;
-    GpsListener mGps;                                  // GPS listener
-    NetworkListener mNetwork;                  // Network listener
-    LocationManager mLocMan;                   // Location manager
-    
-    private GeoBroker broker;                  // GeoBroker object
-       
-       int interval;
-       
-       /**
-        * Constructor.
-        * 
-        * @param id                    Listener id
-        * @param ctx
-        * @param time                  Sampling period in msec
-        * @param appView
-        */
-       GeoListener(GeoBroker broker, String id, int time) {
-               this.id = id;
-               this.interval = time;
-               this.broker = broker;
-               this.mGps = null;
-               this.mNetwork = null;
-               this.mLocMan = (LocationManager) 
broker.ctx.getSystemService(Context.LOCATION_SERVICE);
-
-               // If GPS provider, then create and start GPS listener
-               if (this.mLocMan.getProvider(LocationManager.GPS_PROVIDER) != 
null) {
-                       this.mGps = new GpsListener(broker.ctx, time, this);
-               }
-               
-               // If network provider, then create and start network listener
-               if (this.mLocMan.getProvider(LocationManager.NETWORK_PROVIDER) 
!= null) {
-                       this.mNetwork = new NetworkListener(broker.ctx, time, 
this);
-               }
-       }
-       
-       /**
-        * Destroy listener.
-        */
-       public void destroy() {
-               this.stop();
-       }
-       
-       /**
-        * Location found.  Send location back to JavaScript.
-        * 
-        * @param loc
-        */
-       void success(Location loc) {
-               
-               String params = loc.getLatitude() + "," + loc.getLongitude() + 
", " + loc.getAltitude() + 
-                               "," + loc.getAccuracy() + "," + 
loc.getBearing() +
-                               "," + loc.getSpeed() + "," + loc.getTime();
-               
-               if (id == "global") {
-                       this.stop();
-               }
-               this.broker.sendJavascript("navigator._geo.success('" + id + 
"'," +  params + ");");
-       }
-       
-       /**
-        * Location failed.  Send error back to JavaScript.
-        * 
-        * @param code                  The error code
-        * @param msg                   The error message
-        */
-       void fail(int code, String msg) {
-               this.broker.sendJavascript("navigator._geo.fail('" + this.id + 
"', '" + code + "', '" + msg + "');");
-               this.stop();
-       }
-       
-       /**
-        * Start retrieving location.
-        * 
-        * @param interval
-        */
-       void start(int interval) {
-               if (this.mGps != null) {
-                       this.mGps.start(interval);
-               }
-               if (this.mNetwork != null) {
-                       this.mNetwork.start(interval);
-               }
-               if (this.mNetwork == null && this.mGps == null) {
-                       this.fail(POSITION_UNAVAILABLE, "No location providers 
available.");
-               }
-       }
-       
-       /**
-        * Stop listening for location.
-        */
-       void stop() {
-               if (this.mGps != null) {
-                       this.mGps.stop();
-               }
-               if (this.mNetwork != null) {
-                       this.mNetwork.stop();
-               }
-       }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/724ea49f/framework/src/org/apache/cordova/GpsListener.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/GpsListener.java 
b/framework/src/org/apache/cordova/GpsListener.java
index f000692..5010736 100755
--- a/framework/src/org/apache/cordova/GpsListener.java
+++ b/framework/src/org/apache/cordova/GpsListener.java
@@ -19,145 +19,37 @@
 
 package org.apache.cordova;
 
-import org.apache.cordova.api.CordovaInterface;
-
-import android.content.Context;
-import android.location.Location;
 import android.location.LocationManager;
-import android.location.LocationListener;
-import android.os.Bundle;
 
 /**
  * This class handles requests for GPS location services.
  *
  */
-public class GpsListener implements LocationListener {
-       
-       private CordovaInterface mCtx;                          // 
CordovaActivity object
-       
-       private LocationManager mLocMan;                        // Location 
manager object
-       private GeoListener owner;                                      // 
Geolistener object (parent)
-       private boolean hasData = false;                        // Flag 
indicates if location data is available in cLoc
-       private Location cLoc;                                          // Last 
recieved location
-       private boolean running = false;                        // Flag 
indicates if listener is running
-       
-       /**
-        * Constructor.  
-        * Automatically starts listening.
-        * 
-        * @param ctx
-        * @param interval
-        * @param m
-        */
-       public GpsListener(CordovaInterface ctx, int interval, GeoListener m) {
-               this.owner = m;
-               this.mCtx = ctx;
-               this.mLocMan = (LocationManager) 
this.mCtx.getSystemService(Context.LOCATION_SERVICE);
-               this.running = false;
-               this.start(interval);
-       }
-       
-       /**
-        * Get last location.
-        * 
-        * @return                              Location object
-        */
-       public Location getLocation() {
-               this.cLoc = 
this.mLocMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
-               if (this.cLoc != null) {
-                       this.hasData = true;
-               }
-               return this.cLoc;
-       }
-       
-       /**
-        * Called when the provider is disabled by the user.
-        * 
-        * @param provider
-        */
-       public void onProviderDisabled(String provider) {
-               this.owner.fail(GeoListener.POSITION_UNAVAILABLE, "GPS provider 
disabled.");
-       }
-
-       /**
-        * Called when the provider is enabled by the user.
-        * 
-        * @param provider
-        */
-       public void onProviderEnabled(String provider) {
-               System.out.println("GpsListener: The provider "+ provider + " 
is enabled");
+public class GPSListener extends CordovaLocationListener {
+       public GPSListener(LocationManager locationManager, GeoBroker m) {
+               super(locationManager, m, "[Cordova GPSListener]");
+               if 
(this.locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
+               // If network provider, then create and start network listener
+            
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
60000, 0, this);
+        } else {
+               this.fail(CordovaLocationListener.POSITION_UNAVAILABLE, "GPS 
provider is not available.");
+        }
        }
 
-       /**
-        * Called when the provider status changes. This method is called when 
a 
-        * provider is unable to fetch a location or if the provider has 
recently 
-        * become available after a period of unavailability.
-        * 
-        * @param provider
-        * @param status
-        * @param extras
-        */
-       public void onStatusChanged(String provider, int status, Bundle extras) 
{
-               System.out.println("GpsListener: The status of the provider " + 
provider + " has changed");
-               if (status == 0) {
-                       System.out.println("GpsListener: " + provider + " is 
OUT OF SERVICE");
-                       this.owner.fail(GeoListener.POSITION_UNAVAILABLE, "GPS 
out of service.");
-               }
-               else if (status == 1) {
-                       System.out.println("GpsListener: " + provider + " is 
TEMPORARILY_UNAVAILABLE");
-               }
-               else {
-                       System.out.println("GpsListener: " + provider + " is 
Available");
-               }
-       }
-
-       /**
-        * Called when the location has changed.
-        * 
-        * @param location
-        */
-       public void onLocationChanged(Location location) {
-               System.out.println("GpsListener: The location has been 
updated!");
-               this.hasData = true;
-               this.cLoc = location;
-               this.owner.success(location);
-       }
-
-       /**
-        * Determine if location data is available.
-        * 
-        * @return
-        */
-       public boolean hasLocation() {
-               return this.hasData;
-       }
        
        /**
         * Start requesting location updates.
         * 
         * @param interval
         */
-       public void start(int interval) {
+       public void start() {
                if (!this.running) {
-                       this.running = true;
-                       
this.mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, interval, 0, 
this);
-                       this.getLocation();
-
-                       // If GPS provider has data, then send now
-                       if (this.hasData) {
-                               this.owner.success(this.cLoc);
+                       if 
(this.locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
+                               this.running = true;
+                               
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
60000, 0, this);
+                       } else {
+                               
this.fail(CordovaLocationListener.POSITION_UNAVAILABLE, "GPS provider is not 
available.");
                        }
                }
        }
-
-       /**
-        * Stop receiving location updates.
-        */
-       public void stop() {
-               if (this.running) {
-                       this.mLocMan.removeUpdates(this);
-               }
-               this.running = false;
-       }
-       
 }

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/724ea49f/framework/src/org/apache/cordova/NetworkListener.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/NetworkListener.java 
b/framework/src/org/apache/cordova/NetworkListener.java
index fd3fbd5..050560a 100755
--- a/framework/src/org/apache/cordova/NetworkListener.java
+++ b/framework/src/org/apache/cordova/NetworkListener.java
@@ -16,138 +16,23 @@
        specific language governing permissions and limitations
        under the License.
 */
-package org.apache.cordova;
 
-import org.apache.cordova.api.CordovaInterface;
+package org.apache.cordova;
 
-import android.content.Context;
-import android.location.Location;
 import android.location.LocationManager;
-import android.location.LocationListener;
-import android.os.Bundle;
-
-public class NetworkListener implements LocationListener {
-       
-       private CordovaInterface mCtx;              // CordovaActivity object
-       
-       private LocationManager mLocMan;           // Location manager object
-       private GeoListener owner;                 // Geolistener object 
(parent)
-       private boolean hasData = false;           // Flag indicates if 
location data is available in cLoc
-       private Location cLoc;                     // Last recieved location
-       private boolean running = false;           // Flag indicates if 
listener is running
-
-       /**
-        * Constructor.  
-        * Automatically starts listening.
-        * 
-        * @param ctx
-        * @param interval
-        * @param m
-        */
-       public NetworkListener(CordovaInterface ctx, int interval, GeoListener 
m) {
-               this.owner = m;
-               this.mCtx = ctx;
-               this.mLocMan = (LocationManager) 
this.mCtx.getSystemService(Context.LOCATION_SERVICE);
-               this.running = false;
-               this.start(interval);
-       }
-       
-       /**
-        * Get last location.
-        * 
-        * @return                              Location object
-        */
-       public Location getLocation() {
-               this.cLoc = 
this.mLocMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
-               if (this.cLoc != null) {
-                       this.hasData = true;
-               }
-               return this.cLoc;
-       }
-
-       /**
-        * Called when the provider is disabled by the user.
-        * 
-        * @param provider
-        */
-       public void onProviderDisabled(String provider) {
-               System.out.println("NetworkListener: The provider " + provider 
+ " is disabled");
-       }
-
-       /**
-        * Called when the provider is enabled by the user.
-        * 
-        * @param provider
-        */
-       public void onProviderEnabled(String provider) {
-               System.out.println("NetworkListener: The provider "+ provider + 
" is enabled");
-       }
 
-       /**
-        * Called when the provider status changes. This method is called when 
a 
-        * provider is unable to fetch a location or if the provider has 
recently 
-        * become available after a period of unavailability.
-        * 
-        * @param provider
-        * @param status
-        * @param extras
-        */
-       public void onStatusChanged(String provider, int status, Bundle extras) 
{
-               System.out.println("NetworkListener: The status of the provider 
" + provider + " has changed");
-               if (status == 0) {
-                       System.out.println("NetworkListener: " + provider + " 
is OUT OF SERVICE");
-               }
-               else if (status == 1) {
-                       System.out.println("NetworkListener: " + provider + " 
is TEMPORARILY_UNAVAILABLE");
-               }
-               else {
-                       System.out.println("NetworkListener: " + provider + " 
is Available");
-               }
-       }
-
-       /**
-        * Called when the location has changed.
-        * 
-        * @param location
-        */
-       public void onLocationChanged(Location location) {
-               System.out.println("NetworkListener: The location has been 
updated!");
-               this.hasData = true;
-               this.cLoc = location;
-               
-               // The GPS is the primary form of Geolocation in Cordova.  
-               // Only fire the success variables if the GPS is down for some 
reason.
-               if (!this.owner.mGps.hasLocation()) {
-                       this.owner.success(location);
-               }
-       }
-       
-       /**
-        * Start requesting location updates.
-        * 
-        * @param interval
-        */
-       public void start(int interval) {
-               if (!this.running) {
-                       this.running = true;
-                       
this.mLocMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, interval, 
0, this);
-                       this.getLocation();
-                       
-                       // If Network provider has data but GPS provider 
doesn't, then send ours
-                       if (this.hasData && !this.owner.mGps.hasLocation()) {
-                               this.owner.success(this.cLoc);
-                       }
-               }
-       }
-       
-       /**
-        * Stop receiving location updates.
-        */
-       public void stop() {
-               if (this.running) {
-                       this.mLocMan.removeUpdates(this);
-               }
-               this.running = false;
+/**
+ * This class handles requests for GPS location services.
+ *
+ */
+public class NetworkListener extends CordovaLocationListener {
+       public NetworkListener(LocationManager locationManager, GeoBroker m) {
+               super(locationManager, m, "[Cordova NetworkListener]");
+               if 
(this.locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
+               // If network provider, then create and start network listener
+            
this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
60000, 10, this);
+        } else {
+               this.fail(CordovaLocationListener.POSITION_UNAVAILABLE, 
"Network provider is not available.");
+        }
        }
-       
 }

Reply via email to