Bug 646685 - Fix watchPosition after isBetterPosition was removed. r=blassey
authorDoug Turner <dougt@dougt.org>
Wed, 30 Mar 2011 21:13:35 -0700
changeset 64514 c5bd92bec6d6ed9480b0771e3f4ab0ce5cdd6a18
parent 64513 565c588e3e5190ef2dd2680e1fc23e2f2f07d2de
child 64515 4c7d52ed5ccd786d362f756809a64e9ce67bc6d5
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersblassey
bugs646685
milestone2.2a1pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 646685 - Fix watchPosition after isBetterPosition was removed. r=blassey
dom/src/geolocation/nsGeolocation.cpp
dom/src/geolocation/nsGeolocation.h
embedding/android/GeckoApp.java
embedding/android/GeckoAppShell.java
embedding/android/GeckoSurfaceView.java
widget/src/android/AndroidJavaWrappers.cpp
--- a/dom/src/geolocation/nsGeolocation.cpp
+++ b/dom/src/geolocation/nsGeolocation.cpp
@@ -238,17 +238,16 @@ nsDOMGeoPositionError::NotifyCallback(ns
 
 nsGeolocationRequest::nsGeolocationRequest(nsGeolocation* aLocator,
                                            nsIDOMGeoPositionCallback* aCallback,
                                            nsIDOMGeoPositionErrorCallback* aErrorCallback,
                                            nsIDOMGeoPositionOptions* aOptions,
                                            PRBool aWatchPositionRequest)
   : mAllowed(PR_FALSE),
     mCleared(PR_FALSE),
-    mIsFirstUpdate(PR_TRUE),
     mIsWatchPositionRequest(aWatchPositionRequest),
     mCallback(aCallback),
     mErrorCallback(aErrorCallback),
     mOptions(aOptions),
     mLocator(aLocator)
 {
 }
 
@@ -458,21 +457,18 @@ nsGeolocationRequest::SendLocation(nsIDO
 
   if (mIsWatchPositionRequest)
     SetTimeoutTimer();
 }
 
 void
 nsGeolocationRequest::Update(nsIDOMGeoPosition* aPosition)
 {
-  if (mIsFirstUpdate) {
-    mIsFirstUpdate = PR_FALSE;
-    nsCOMPtr<nsIRunnable> ev  = new RequestSendLocationEvent(aPosition, this);
-    NS_DispatchToMainThread(ev);
-  }
+  nsCOMPtr<nsIRunnable> ev  = new RequestSendLocationEvent(aPosition, this);
+  NS_DispatchToMainThread(ev);
 }
 
 void
 nsGeolocationRequest::Shutdown()
 {
   if (mTimeoutTimer) {
     mTimeoutTimer->Cancel();
     mTimeoutTimer = nsnull;
--- a/dom/src/geolocation/nsGeolocation.h
+++ b/dom/src/geolocation/nsGeolocation.h
@@ -112,17 +112,16 @@ class nsGeolocationRequest
   void IPDLRelease() { Release(); }
 #endif
 
  private:
 
   void NotifyError(PRInt16 errorCode);
   PRPackedBool mAllowed;
   PRPackedBool mCleared;
-  PRPackedBool mIsFirstUpdate;
   PRPackedBool mIsWatchPositionRequest;
 
   nsCOMPtr<nsITimer> mTimeoutTimer;
   nsCOMPtr<nsIDOMGeoPositionCallback> mCallback;
   nsCOMPtr<nsIDOMGeoPositionErrorCallback> mErrorCallback;
   nsCOMPtr<nsIDOMGeoPositionOptions> mOptions;
 
   nsRefPtr<nsGeolocation> mLocator;
--- a/embedding/android/GeckoApp.java
+++ b/embedding/android/GeckoApp.java
@@ -68,16 +68,17 @@ abstract public class GeckoApp
 
     public static FrameLayout mainLayout;
     public static GeckoSurfaceView surfaceView;
     public static GeckoApp mAppContext;
     public static boolean mFullscreen = false;
     public static boolean mStartedEarly = false;
     public static File sGREDir = null;
     static Thread mLibLoadThread = null;
+    public Handler mMainHandler;
 
     enum LaunchState {PreLaunch, Launching, WaitButton,
                       Launched, GeckoRunning, GeckoExiting};
     private static LaunchState sLaunchState = LaunchState.PreLaunch;
 
 
     static boolean checkLaunchState(LaunchState checkState) {
         synchronized(sLaunchState) {
@@ -171,16 +172,17 @@ abstract public class GeckoApp
         return true;
     }
 
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState)
     {
         mAppContext = this;
+        mMainHandler = new Handler();
 
         SharedPreferences settings = getPreferences(Activity.MODE_PRIVATE);
         String localeCode = settings.getString(getPackageName() + ".locale", "");
         if (localeCode != null && localeCode.length() > 0)
             GeckoAppShell.setSelectedLocale(localeCode);
 
         Log.i("GeckoApp", "create");
         super.onCreate(savedInstanceState);
--- a/embedding/android/GeckoAppShell.java
+++ b/embedding/android/GeckoAppShell.java
@@ -114,16 +114,21 @@ public class GeckoAppShell
             Looper.prepare();
             try {
                 mHandlerQueue.put(new Handler());
             } catch (InterruptedException ie) {}
             Looper.loop();
         }
     }
 
+    // Get a Handler for the main java thread
+    public static Handler getMainHandler() {
+        return GeckoApp.mAppContext.mMainHandler;
+    }
+
     private static Handler sHandler = null;
 
     // Get a Handler for a looper thread, or create one if it doesn't exist yet
     public static Handler getHandler() {
         if (sHandler == null) {
             LooperThread lt = new LooperThread();
             lt.start();
             try {
@@ -553,34 +558,42 @@ public class GeckoAppShell
                 return;
 
             sm.registerListener(GeckoApp.surfaceView, accelSensor, SensorManager.SENSOR_DELAY_GAME);
         } else {
             sm.unregisterListener(GeckoApp.surfaceView);
         }
     }
 
-    public static void enableLocation(boolean enable) {
-        LocationManager lm = (LocationManager)
-            GeckoApp.surfaceView.getContext().getSystemService(Context.LOCATION_SERVICE);
+    public static void enableLocation(final boolean enable) {
+     
+        getMainHandler().post(new Runnable() { 
+                public void run() {
+                    GeckoSurfaceView view = GeckoApp.surfaceView;
+                    LocationManager lm = (LocationManager)
+                        view.getContext().getSystemService(Context.LOCATION_SERVICE);
 
-        if (enable) {
-            Criteria crit = new Criteria();
-            crit.setAccuracy(Criteria.ACCURACY_FINE);
-            String provider = lm.getBestProvider(crit, true);
-            if (provider == null)
-                return;
+                    if (enable) {
+                        Criteria crit = new Criteria();
+                        crit.setAccuracy(Criteria.ACCURACY_FINE);
+                        String provider = lm.getBestProvider(crit, true);
+                        if (provider == null)
+                            return;
 
-            Location loc = lm.getLastKnownLocation(provider);
-            if (loc != null)
-                sendEventToGecko(new GeckoEvent(loc, null));
-            lm.requestLocationUpdates(provider, 100, (float).5, GeckoApp.surfaceView, Looper.getMainLooper());
-        } else {
-            lm.removeUpdates(GeckoApp.surfaceView);
-        }
+                        Looper l = Looper.getMainLooper();
+                        Location loc = lm.getLastKnownLocation(provider);
+                        if (loc != null) {
+                            view.onLocationChanged(loc);
+                        }
+                        lm.requestLocationUpdates(provider, 100, (float).5, view, l);
+                    } else {
+                        lm.removeUpdates(view);
+                    }
+                }
+            });
     }
 
     public static void moveTaskToBack() {
         GeckoApp.mAppContext.moveTaskToBack(true);
     }
 
     public static void returnIMEQueryResult(String result, int selectionStart, int selectionLength) {
         GeckoApp.surfaceView.inputConnection.mSelectionStart = selectionStart;
--- a/embedding/android/GeckoSurfaceView.java
+++ b/embedding/android/GeckoSurfaceView.java
@@ -417,43 +417,44 @@ class GeckoSurfaceView
         protected Void doInBackground(Location... location) {
             try {
                 List<Address> addresses = mGeocoder.getFromLocation(location[0].getLatitude(),
                                                                     location[0].getLongitude(), 1);
                 // grab the first address.  in the future,
                 // may want to expose multiple, or filter
                 // for best.
                 mLastGeoAddress = addresses.get(0);
+                GeckoAppShell.sendEventToGecko(new GeckoEvent(location[0], mLastGeoAddress));
             } catch (Exception e) {
                 Log.w("GeckoSurfaceView", "GeocoderTask "+e);
             }
             return null;
         }
     }
 
     // geolocation
     public void onLocationChanged(Location location)
     {
         if (mGeocoder == null)
-            mGeocoder = new Geocoder(getContext());
+            mGeocoder = new Geocoder(getContext(), Locale.getDefault());
 
         if (mLastGeoAddress == null) {
             new GeocoderTask().execute(location);
         }
         else {
             float[] results = new float[1];
             Location.distanceBetween(location.getLatitude(),
                                      location.getLongitude(),
                                      mLastGeoAddress.getLatitude(),
                                      mLastGeoAddress.getLongitude(),
                                      results);
             // pfm value.  don't want to slam the
             // geocoder with very similar values, so
             // only call after about 100m
-            if (results[0] > 100) 
+            if (results[0] > 100)
                 new GeocoderTask().execute(location);
         }
 
         GeckoAppShell.sendEventToGecko(new GeckoEvent(location, mLastGeoAddress));
     }
 
     public void onProviderDisabled(String provider)
     {
--- a/widget/src/android/AndroidJavaWrappers.cpp
+++ b/widget/src/android/AndroidJavaWrappers.cpp
@@ -220,26 +220,28 @@ AndroidAddress::CreateGeoPositionAddress
     nsJNIString city(static_cast<jstring>(jenv->CallObjectMethod(jobj, jGetLocalityMethod)), jenv);
     nsJNIString county(static_cast<jstring>(jenv->CallObjectMethod(jobj, jGetSubAdminAreaMethod)), jenv);
     nsJNIString country(static_cast<jstring>(jenv->CallObjectMethod(jobj, jGetCountryNameMethod)), jenv);
     nsJNIString countryCode(static_cast<jstring>(jenv->CallObjectMethod(jobj, jGetCountryCodeMethod)), jenv);
     nsJNIString premises(static_cast<jstring>(jenv->CallObjectMethod(jobj, jGetPremisesMethod)), jenv);
     nsJNIString postalCode(static_cast<jstring>(jenv->CallObjectMethod(jobj, jGetPostalCodeMethod)), jenv);
     nsJNIString region(static_cast<jstring>(jenv->CallObjectMethod(jobj, jGetAdminAreaMethod, 0)), jenv);
 
+#ifdef DEBUG
     printf_stderr("!!!!!!!!!!!!!! AndroidAddress::CreateGeoPositionAddress:\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n",
                   NS_LossyConvertUTF16toASCII(streetNumber).get(),
                   NS_LossyConvertUTF16toASCII(street).get(),
                   NS_LossyConvertUTF16toASCII(premises).get(),
                   NS_LossyConvertUTF16toASCII(city).get(),
                   NS_LossyConvertUTF16toASCII(county).get(),
                   NS_LossyConvertUTF16toASCII(region).get(),
                   NS_LossyConvertUTF16toASCII(country).get(),
                   NS_LossyConvertUTF16toASCII(countryCode).get(),
                   NS_LossyConvertUTF16toASCII(postalCode).get());
+#endif
 
     return new nsGeoPositionAddress(streetNumber,
                                     street,
                                     premises,
                                     city,
                                     county,
                                     region,
                                     country,