Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e3ae09a2 authored by Mark Harman's avatar Mark Harman
Browse files

Fix crash going to settings due to late onLocationChanged() call from after...

Fix crash going to settings due to late onLocationChanged() call from after turning off location updates!
parent 61b54d5d
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -9377,6 +9377,44 @@ public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActiv
        locationInfo = new LocationSupplier.LocationInfo();
        mActivity.getLocationSupplier().getLocation(locationInfo);
        assertFalse(locationInfo.LocationWasCached());
        // now test repeatedly going to settings and back - guard against crash we had where onLocationChanged got called one more time after
        // location listeners had been freed
        for(int i=0;i<20;i++) {
            assertTrue(mActivity.getLocationSupplier().hasLocationListeners());
            Thread.sleep((i % 5) * 100);
            // go to settings
            assertFalse(mActivity.isCameraInBackground());
            Log.d(TAG, "about to click settings");
            clickView(settingsButton);
            Log.d(TAG, "done clicking settings");
            this.getInstrumentation().waitForIdleSync();
            Log.d(TAG, "after idle sync");
            assertTrue(mActivity.isCameraInBackground());
            Thread.sleep(100);
            assertTrue(mActivity.getLocationSupplier().noLocationListeners());
            assertFalse(mActivity.getLocationSupplier().testHasReceivedLocation());
            assertNull(mActivity.getLocationSupplier().getLocation());
            Thread.sleep(200);
            assertTrue(mActivity.getLocationSupplier().noLocationListeners());
            assertFalse(mActivity.getLocationSupplier().testHasReceivedLocation());
            assertNull(mActivity.getLocationSupplier().getLocation());
            // go back
            assertTrue(mActivity.isCameraInBackground());
            Log.d(TAG, "go back");
            mActivity.runOnUiThread(new Runnable() {
                public void run() {
                    mActivity.onBackPressed();
                }
            });
            this.getInstrumentation().waitForIdleSync();
            Log.d(TAG, "after idle sync");
            assertFalse(mActivity.isCameraInBackground());
        }
    }
    private void subTestPhotoStamp() throws IOException {
+18 −2
Original line number Diff line number Diff line
@@ -45,12 +45,26 @@ public class LocationSupplier {
        return null;
    }

    /** Cache the current best location. Note that we intentionally call getLocation() from this
     *  method rather than passing it a location from onLocationChanged(), as we don't want a
     *  coarse location overriding a better fine location.
     */
    private void cacheLocation() {
        if( MyDebug.LOG )
            Log.d(TAG, "cacheLocation");
        cached_location = new Location(getLocation());
        Location location = getLocation();
        if( location == null ) {
            // this isn't an error as it can happen that we receive a call to onLocationChanged() after
            // having freed the location listener (possibly because LocationManager had already queued
            // a call to onLocationChanged?
            // we should not set cached_location to null in such cases
            Log.d(TAG, "### asked to cache location when location not available");
        }
        else {
            cached_location = new Location(location);
            cached_location_ms = System.currentTimeMillis();
        }
    }

    public static class LocationInfo {
        private boolean location_was_cached;
@@ -243,6 +257,8 @@ public class LocationSupplier {
                locationListeners[i] = null;
            }
            locationListeners = null;
            if( MyDebug.LOG )
                Log.d(TAG, "location listeners now freed");
        }
    }