# HG changeset patch # User Jay Gindin # Date 1548737578 18000 # Mon Jan 28 23:52:58 2019 -0500 # Node ID e4cde46629ce774ca5aa40bd077f2a0404b02053 # Parent 015a2e537c75fdab063ad41144266fcaacf06fc3 LocationResolver doesn't need an async task. The way this gets invoked now, it will already be running on a background thread. Add explicit start() and stop() methods to the location resolvers. Make sure we only have a single LocationToAddressConverter. diff --git a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/AndroidResolver.java b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/AndroidResolver.java --- a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/AndroidResolver.java +++ b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/AndroidResolver.java @@ -36,15 +36,9 @@ } - @Override - public void onDestroy() { - // Nothing to destroy here... - } - - @SuppressWarnings( "FeatureEnvy" ) @Nullable - protected ZmanimLocation resolveFromCoordinatesInBackground( @NotNull ZmanimLocation location ) { + protected ZmanimLocation resolveFromCoordinates( @NotNull ZmanimLocation location ) { if ( !Geocoder.isPresent() ) { return null; @@ -81,7 +75,7 @@ @Nullable @SuppressWarnings( "FeatureEnvy" ) - protected ZmanimLocation resolveFromNameInBackground( @NotNull ZmanimLocation location ) { + protected ZmanimLocation resolveFromName( @NotNull ZmanimLocation location ) { ZmanimLocation resolved = null; diff --git a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/CachedLocationResolver.java b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/CachedLocationResolver.java --- a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/CachedLocationResolver.java +++ b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/CachedLocationResolver.java @@ -85,12 +85,19 @@ } }; + } + + + @Override + public void start() { + super.start(); + eventBus.addHandler( locationAcquiredHandler ); } @Override - public void onDestroy() { + public void stop() { eventBus.removeHandler( locationAcquiredHandler ); } @@ -98,7 +105,7 @@ @Nullable @Override - protected ZmanimLocation resolveFromCoordinatesInBackground( @NotNull ZmanimLocation zmanimLocation ) { + protected ZmanimLocation resolveFromCoordinates( @NotNull ZmanimLocation zmanimLocation ) { return findCachedMatch( zmanimLocation ); } @@ -106,7 +113,7 @@ @Nullable @Override - protected ZmanimLocation resolveFromNameInBackground( @NotNull ZmanimLocation zmanimLocation ) { + protected ZmanimLocation resolveFromName( @NotNull ZmanimLocation zmanimLocation ) { return findCachedMatch( zmanimLocation ); } diff --git a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/JsonQueryingResolver.java b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/JsonQueryingResolver.java --- a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/JsonQueryingResolver.java +++ b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/JsonQueryingResolver.java @@ -61,7 +61,7 @@ @Nullable @Override - protected final ZmanimLocation resolveFromCoordinatesInBackground( @NotNull ZmanimLocation zmanimLocation ) { + protected final ZmanimLocation resolveFromCoordinates( @NotNull ZmanimLocation zmanimLocation ) { List> params = new ArrayList>(); fillInLatitudeAndLongitude( Double.toString( zmanimLocation.getLatitude() ), @@ -73,7 +73,7 @@ @Nullable @Override - protected final ZmanimLocation resolveFromNameInBackground( @NotNull ZmanimLocation zmanimLocation ) { + protected final ZmanimLocation resolveFromName( @NotNull ZmanimLocation zmanimLocation ) { List> params = new ArrayList>(); fillInSearch( zmanimLocation, params ); diff --git a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/LocationResolver.java b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/LocationResolver.java --- a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/LocationResolver.java +++ b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/LocationResolver.java @@ -16,7 +16,6 @@ import android.content.Context; import android.location.Address; -import android.os.AsyncTask; import java.util.Collections; import java.util.List; @@ -109,10 +108,15 @@ } + public void start() { + } + + /** * Called when the resolver is being destroyed. */ - public abstract void onDestroy(); + public void stop() { + } final void resolve( @@ -120,27 +124,15 @@ @NotNull final List nextResolvers ) { - AsyncTask task = new AsyncTask() { - @Override - protected ZmanimLocation doInBackground( ZmanimLocation... zmanimLocations ) { + final ZmanimLocation zmanimLocation; + if ( location.hasCoordinates() ) { + zmanimLocation = resolveFromCoordinates( location ); + } + else { + zmanimLocation = resolveFromName( location ); + } - if ( location.hasCoordinates() ) { - return resolveFromCoordinatesInBackground( zmanimLocations[0] ); - } - else { - return resolveFromNameInBackground( zmanimLocations[0] ); - } - } - - - @Override - protected void onPostExecute( ZmanimLocation resolvedLocation ) { - super.onPostExecute( resolvedLocation ); - handlePostExecute( location, resolvedLocation, nextResolvers ); - } - }; - - task.execute( location ); + handlePostExecute( location, zmanimLocation, nextResolvers ); } @@ -151,7 +143,7 @@ * @return An actual ZmanimLocation that has been properly resolved, or null if resolution failed. */ @Nullable - protected abstract ZmanimLocation resolveFromCoordinatesInBackground( @NotNull ZmanimLocation zmanimLocation ); + protected abstract ZmanimLocation resolveFromCoordinates( @NotNull ZmanimLocation zmanimLocation ); /** * This method, guaranteed to be run on a background thread, must perform whatever actions necessary to resolve the @@ -160,7 +152,7 @@ * @return An actual ZmanimLocation that has been properly resolved, or null if resolution failed. */ @Nullable - protected abstract ZmanimLocation resolveFromNameInBackground( @NotNull ZmanimLocation zmanimLocation ); + protected abstract ZmanimLocation resolveFromName( @NotNull ZmanimLocation zmanimLocation ); private void handlePostExecute( diff --git a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/LocationToAddressConverter.java b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/LocationToAddressConverter.java --- a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/LocationToAddressConverter.java +++ b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/LocationToAddressConverter.java @@ -18,6 +18,7 @@ import android.os.AsyncTask; import javax.inject.Inject; +import javax.inject.Singleton; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; @@ -27,6 +28,7 @@ /** * Listens for RawLocationAcquiredEvent and PlaceAcquiredEvent and starts the process of translating to an address. */ +@Singleton public class LocationToAddressConverter { @@ -42,6 +44,8 @@ private final LocationAcquiredEvent.Handler locationAcquiredHandler; private final PlaceAcquiredEvent.Handler placeAcquiredHandler; + private boolean isStarted = false; + @Inject public LocationToAddressConverter( @@ -117,8 +121,18 @@ public void start() { + if ( isStarted ) { + return; + } + eventBus.addHandler( locationAcquiredHandler ); eventBus.addHandler( placeAcquiredHandler ); + + for ( LocationResolver resolver : orderedResolvers ) { + resolver.start(); + } + + isStarted = true; } @@ -128,8 +142,10 @@ eventBus.removeHandler( placeAcquiredHandler ); for ( LocationResolver resolver : orderedResolvers ) { - resolver.onDestroy(); + resolver.stop(); } + + isStarted = false; } diff --git a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/MapQuestResolver.java b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/MapQuestResolver.java --- a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/MapQuestResolver.java +++ b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/MapQuestResolver.java @@ -33,12 +33,6 @@ @Override - public void onDestroy() { - - } - - - @Override protected void fillInLatitudeAndLongitude( String latitude, String longitude, diff --git a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/NominatimResolver.java b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/NominatimResolver.java --- a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/NominatimResolver.java +++ b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/NominatimResolver.java @@ -58,12 +58,6 @@ @Override - public void onDestroy() { - - } - - - @Override protected void fillInLatitudeAndLongitude( String latitude, String longitude, diff --git a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/PassthroughResolver.java b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/PassthroughResolver.java --- a/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/PassthroughResolver.java +++ b/Android/src/main/java/com/gindin/zmanim/android/location/resolvers/PassthroughResolver.java @@ -26,18 +26,12 @@ } - @Override - public void onDestroy() { - - } - - /** * Since we couldn't resolve this any other way, then the resolution here is just to leave it alone. */ @Nullable @Override - protected ZmanimLocation resolveFromCoordinatesInBackground( @NotNull ZmanimLocation zmanimLocation ) { + protected ZmanimLocation resolveFromCoordinates( @NotNull ZmanimLocation zmanimLocation ) { return zmanimLocation; } @@ -47,7 +41,7 @@ */ @Nullable @Override - protected ZmanimLocation resolveFromNameInBackground( @NotNull ZmanimLocation zmanimLocation ) { + protected ZmanimLocation resolveFromName( @NotNull ZmanimLocation zmanimLocation ) { return zmanimLocation; } diff --git a/Android/src/test/java/com/gindin/zmanim/android/location/resolvers/UT_CachedLocationResolver.java b/Android/src/test/java/com/gindin/zmanim/android/location/resolvers/UT_CachedLocationResolver.java --- a/Android/src/test/java/com/gindin/zmanim/android/location/resolvers/UT_CachedLocationResolver.java +++ b/Android/src/test/java/com/gindin/zmanim/android/location/resolvers/UT_CachedLocationResolver.java @@ -53,7 +53,7 @@ .inTimeZone( TimeZone.getTimeZone( "America/Los_Angeles" ) ) .build(); - ZmanimLocation resolvedLocation = cachedLocationResolver.resolveFromCoordinatesInBackground( location2 ); + ZmanimLocation resolvedLocation = cachedLocationResolver.resolveFromCoordinates( location2 ); Assert.assertEquals( location, resolvedLocation ); } @@ -78,7 +78,7 @@ .build(); // The caching resolver won't know about this other location, so it'll return null. - ZmanimLocation resolvedLocation = cachedLocationResolver.resolveFromCoordinatesInBackground( location2 ); + ZmanimLocation resolvedLocation = cachedLocationResolver.resolveFromCoordinates( location2 ); Assert.assertNull( resolvedLocation ); } diff --git a/Android/src/test/java/com/gindin/zmanim/android/location/resolvers/UT_MapQuestResolver.java b/Android/src/test/java/com/gindin/zmanim/android/location/resolvers/UT_MapQuestResolver.java --- a/Android/src/test/java/com/gindin/zmanim/android/location/resolvers/UT_MapQuestResolver.java +++ b/Android/src/test/java/com/gindin/zmanim/android/location/resolvers/UT_MapQuestResolver.java @@ -58,7 +58,7 @@ .atLongitude( longitude ) .build(); - ZmanimLocation resolved = resolver.resolveFromCoordinatesInBackground( unresolved ); + ZmanimLocation resolved = resolver.resolveFromCoordinates( unresolved ); Assert.assertNotNull( resolved ); Assert.assertEquals( expectedLocationName, resolved.getLocationName() ); } @@ -75,7 +75,7 @@ .locationName( name ) .build(); - ZmanimLocation resolved = resolver.resolveFromNameInBackground( unresolved ); + ZmanimLocation resolved = resolver.resolveFromName( unresolved ); Assert.assertNotNull(resolved ); Assert.assertEquals( expectedLocationName, resolved.getLocationName() ); Assert.assertEquals( expectedLatitude, resolved.getLatitude(), 0 ); diff --git a/Android/src/test/java/com/gindin/zmanim/android/location/resolvers/UT_NominatimResolver.java b/Android/src/test/java/com/gindin/zmanim/android/location/resolvers/UT_NominatimResolver.java --- a/Android/src/test/java/com/gindin/zmanim/android/location/resolvers/UT_NominatimResolver.java +++ b/Android/src/test/java/com/gindin/zmanim/android/location/resolvers/UT_NominatimResolver.java @@ -58,7 +58,7 @@ .atLongitude( longitude ) .build(); - ZmanimLocation resolved = resolver.resolveFromCoordinatesInBackground( unresolved ); + ZmanimLocation resolved = resolver.resolveFromCoordinates( unresolved ); Assert.assertNotNull( resolved ); Assert.assertEquals( expectedLocationName, resolved.getLocationName() ); } @@ -75,7 +75,7 @@ .locationName( name ) .build(); - ZmanimLocation resolved = resolver.resolveFromNameInBackground( unresolved ); + ZmanimLocation resolved = resolver.resolveFromName( unresolved ); if ( expectedLocationName == null ) { Assert.assertNull( resolved ); }