diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2014-03-08 10:04:01 +0100 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2014-03-08 10:04:01 +0100 |
| commit | e0ab26ff7af5a1df16b21af21bd683b53fabaaa8 (patch) | |
| tree | 6ac0baa569386174b5acf33b683507bf008f0092 /main/src | |
| parent | 534c9c813a2bce2c65dc13663df9e3ad14fd30f1 (diff) | |
| parent | 291a622a9d8b1e5c791157a61e5ef467710be42d (diff) | |
| download | cgeo-e0ab26ff7af5a1df16b21af21bd683b53fabaaa8.zip cgeo-e0ab26ff7af5a1df16b21af21bd683b53fabaaa8.tar.gz cgeo-e0ab26ff7af5a1df16b21af21bd683b53fabaaa8.tar.bz2 | |
Merge branch 'use-combined-provider' into upstream
Diffstat (limited to 'main/src')
22 files changed, 278 insertions, 382 deletions
diff --git a/main/src/cgeo/geocaching/AbstractPopupActivity.java b/main/src/cgeo/geocaching/AbstractPopupActivity.java index 38e37da..375d06a 100644 --- a/main/src/cgeo/geocaching/AbstractPopupActivity.java +++ b/main/src/cgeo/geocaching/AbstractPopupActivity.java @@ -8,10 +8,11 @@ import cgeo.geocaching.gcvote.GCVote; import cgeo.geocaching.gcvote.GCVoteRating; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.Units; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.ui.CacheDetailsCreator; import cgeo.geocaching.ui.LoggingUI; -import cgeo.geocaching.utils.GeoDirHandler; +import cgeo.geocaching.sensors.GeoDirHandler; import cgeo.geocaching.utils.Log; import org.apache.commons.lang3.StringUtils; @@ -40,7 +41,7 @@ public abstract class AbstractPopupActivity extends AbstractActivity implements private final GeoDirHandler geoUpdate = new GeoDirHandler() { @Override - public void updateGeoData(final IGeoData geo) { + public void updateGeoDir(final IGeoData geo, final float dir) { try { if (geo.getCoords() != null && cache != null && cache.getCoords() != null) { cacheDistance.setText(Units.getDistanceFromKilometers(geo.getCoords().distanceTo(cache.getCoords()))); @@ -159,7 +160,7 @@ public abstract class AbstractPopupActivity extends AbstractActivity implements @Override public void onPause() { - geoUpdate.stopGeo(); + geoUpdate.stop(); super.onPause(); } @@ -183,7 +184,7 @@ public abstract class AbstractPopupActivity extends AbstractActivity implements public void onResume() { super.onResume(); init(); - geoUpdate.startGeo(); + geoUpdate.start(); } @Override @@ -255,7 +256,7 @@ public abstract class AbstractPopupActivity extends AbstractActivity implements * @param view * unused here but needed since this method is referenced from XML layout */ - public final void goDefaultNavigation(View view) { + public final void goDefaultNavigation(@SuppressWarnings("unused") View view) { navigateTo(); finish(); } diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java index 801dec4..5d529ee 100644 --- a/main/src/cgeo/geocaching/CacheDetailActivity.java +++ b/main/src/cgeo/geocaching/CacheDetailActivity.java @@ -21,6 +21,7 @@ import cgeo.geocaching.geopoint.Units; import cgeo.geocaching.list.StoredList; import cgeo.geocaching.network.HtmlImage; import cgeo.geocaching.network.Network; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.ui.AbstractCachingPageViewCreator; import cgeo.geocaching.ui.AnchorAwareLinkMovementMethod; @@ -40,7 +41,7 @@ import cgeo.geocaching.ui.dialog.Dialogs; import cgeo.geocaching.ui.logs.CacheLogsViewCreator; import cgeo.geocaching.utils.CancellableHandler; import cgeo.geocaching.utils.CryptUtils; -import cgeo.geocaching.utils.GeoDirHandler; +import cgeo.geocaching.sensors.GeoDirHandler; import cgeo.geocaching.utils.ImageUtils; import cgeo.geocaching.utils.Log; import cgeo.geocaching.utils.MatcherWrapper; @@ -143,7 +144,7 @@ public class CacheDetailActivity extends AbstractViewPagerActivity<CacheDetailAc private final GeoDirHandler locationUpdater = new GeoDirHandler() { @Override - public void updateGeoData(final IGeoData geo) { + public void updateGeoDir(final IGeoData geo, final float dir) { if (cacheDistanceView == null) { return; } @@ -317,7 +318,7 @@ public class CacheDetailActivity extends AbstractViewPagerActivity<CacheDetailAc notifyDataSetChanged(); refreshOnResume = false; } - locationUpdater.startGeo(); + locationUpdater.start(); } @Override @@ -338,7 +339,7 @@ public class CacheDetailActivity extends AbstractViewPagerActivity<CacheDetailAc @Override public void onPause() { - locationUpdater.stopGeo(); + locationUpdater.stop(); super.onPause(); } diff --git a/main/src/cgeo/geocaching/CacheListActivity.java b/main/src/cgeo/geocaching/CacheListActivity.java index fa84ac6..cde7a79 100644 --- a/main/src/cgeo/geocaching/CacheListActivity.java +++ b/main/src/cgeo/geocaching/CacheListActivity.java @@ -36,6 +36,7 @@ import cgeo.geocaching.maps.CGeoMap; import cgeo.geocaching.network.Cookies; import cgeo.geocaching.network.Network; import cgeo.geocaching.network.Parameters; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.sorting.CacheComparator; import cgeo.geocaching.sorting.ComparatorUserInterface; @@ -46,7 +47,7 @@ import cgeo.geocaching.ui.dialog.Dialogs; import cgeo.geocaching.utils.AsyncTaskWithProgress; import cgeo.geocaching.utils.CancellableHandler; import cgeo.geocaching.utils.DateUtils; -import cgeo.geocaching.utils.GeoDirHandler; +import cgeo.geocaching.sensors.GeoDirHandler; import cgeo.geocaching.utils.Log; import ch.boye.httpclientandroidlib.HttpResponse; @@ -122,24 +123,12 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA private final GeoDirHandler geoDirHandler = new GeoDirHandler() { @Override - public void updateGeoData(final IGeoData geo) { + public void updateGeoDir(final IGeoData geo, final float dir) { if (geo.getCoords() != null) { adapter.setActualCoordinates(geo.getCoords()); - } - if (!Settings.isUseCompass() || geo.getSpeed() > 5) { // use GPS when speed is higher than 18 km/h - adapter.setActualHeading(geo.getBearing()); - } - } - - @Override - public void updateDirection(final float direction) { - if (!Settings.isLiveList()) { - return; - } - - if (app.currentGeo().getSpeed() <= 5) { // use compass when speed is lower than 18 km/h) { - final float northHeading = DirectionProvider.getDirectionNow(CacheListActivity.this, direction); - adapter.setActualHeading(northHeading); + if (Settings.isLiveList()) { + adapter.setActualHeading(dir); + } } } @@ -465,10 +454,7 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA public void onResume() { super.onResume(); - geoDirHandler.startGeo(); - if (Settings.isLiveMap()) { - geoDirHandler.startDir(); - } + geoDirHandler.start(); adapter.setSelectMode(false); setAdapterCurrentCoordinates(true); @@ -499,7 +485,7 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA @Override public void onPause() { - geoDirHandler.stopGeoAndDir(); + geoDirHandler.stop(); super.onPause(); } diff --git a/main/src/cgeo/geocaching/CgeoApplication.java b/main/src/cgeo/geocaching/CgeoApplication.java index 7bee97e..cbe81fa 100644 --- a/main/src/cgeo/geocaching/CgeoApplication.java +++ b/main/src/cgeo/geocaching/CgeoApplication.java @@ -1,9 +1,14 @@ package cgeo.geocaching; +import cgeo.geocaching.sensors.DirectionProvider; +import cgeo.geocaching.sensors.GeoDataProvider; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.ui.dialog.Dialogs; import cgeo.geocaching.utils.Log; +import org.apache.commons.lang3.tuple.ImmutablePair; import rx.Observable; +import rx.functions.Func2; import android.app.Activity; import android.app.Application; @@ -20,6 +25,7 @@ public class CgeoApplication extends Application { public boolean showLoginToast = true; //login toast shown just once. private boolean liveMapHintShownInThisSession = false; // livemap hint has been shown private static CgeoApplication instance; + private Observable<ImmutablePair<IGeoData,Float>> geoDir; public CgeoApplication() { setInstance(this); @@ -66,34 +72,32 @@ public class CgeoApplication extends Application { }.start(); } - public Observable<IGeoData> currentGeoObject() { - if (geo == null) { + public Observable<ImmutablePair<IGeoData, Float>> geoDirObservable() { + if (geoDir == null) { synchronized(this) { - if (geo == null) { - geo = GeoDataProvider.create(this); + if (geoDir == null) { + geoDir = Observable.combineLatest(GeoDataProvider.create(this), DirectionProvider.create(this), new Func2<IGeoData, Float, ImmutablePair<IGeoData, Float>>() { + @Override + public ImmutablePair<IGeoData, Float> call(final IGeoData geoData, final Float dir) { + return new ImmutablePair<IGeoData, Float>(geoData, dir); + } + }); } } } - return geo; + return geoDir; } - public IGeoData currentGeo() { - return currentGeoObject().first().toBlockingObservable().single(); + private ImmutablePair<IGeoData, Float> currentGeoDir() { + return geoDirObservable().first().toBlockingObservable().single(); } - public Observable<Float> currentDirObject() { - if (dir == null) { - synchronized(this) { - if (dir == null) { - dir = DirectionProvider.create(this); - } - } - } - return dir; + public IGeoData currentGeo() { + return currentGeoDir().left; } public Float currentDirection() { - return currentDirObject().first().toBlockingObservable().single(); + return currentGeoDir().right; } public boolean isLiveMapHintShownInThisSession() { diff --git a/main/src/cgeo/geocaching/CompassActivity.java b/main/src/cgeo/geocaching/CompassActivity.java index 6fc2de1..a51e477 100644 --- a/main/src/cgeo/geocaching/CompassActivity.java +++ b/main/src/cgeo/geocaching/CompassActivity.java @@ -8,12 +8,14 @@ import cgeo.geocaching.enumerations.LoadFlags; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.Units; import cgeo.geocaching.maps.CGeoMap; +import cgeo.geocaching.sensors.DirectionProvider; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.speech.SpeechService; import cgeo.geocaching.ui.CompassView; import cgeo.geocaching.ui.Formatter; import cgeo.geocaching.ui.LoggingUI; -import cgeo.geocaching.utils.GeoDirHandler; +import cgeo.geocaching.sensors.GeoDirHandler; import cgeo.geocaching.utils.Log; import org.apache.commons.lang3.StringUtils; @@ -119,12 +121,12 @@ public class CompassActivity extends AbstractActivity { super.onResume(); // sensor and geolocation manager - geoDirHandler.startGeoAndDir(); + geoDirHandler.start(); } @Override public void onPause() { - geoDirHandler.stopGeoAndDir(); + geoDirHandler.stop(); super.onPause(); } @@ -150,13 +152,8 @@ public class CompassActivity extends AbstractActivity { final CgeoApplication app = CgeoApplication.getInstance(); final IGeoData geo = app.currentGeo(); if (geo != null) { - geoDirHandler.updateGeoData(geo); + geoDirHandler.updateGeoDir(geo, app.currentDirection()); } - final Float dir = app.currentDirection(); - if (dir != null) { - geoDirHandler.updateDirection(dir); - } - } @Override @@ -198,11 +195,6 @@ public class CompassActivity extends AbstractActivity { boolean oldSetting = Settings.isUseCompass(); Settings.setUseCompass(!oldSetting); invalidateOptionsMenuCompatible(); - if (oldSetting) { - geoDirHandler.stopDir(); - } else { - geoDirHandler.startDir(); - } return true; case R.id.menu_edit_destination: Intent pointIntent = new Intent(this, NavigateAnyPointActivity.class); @@ -274,7 +266,7 @@ public class CompassActivity extends AbstractActivity { private GeoDirHandler geoDirHandler = new GeoDirHandler() { @Override - public void updateGeoData(final IGeoData geo) { + public void updateGeoDir(final IGeoData geo, final float dir) { try { if (geo.getCoords() != null) { if (geo.getSatellitesVisible() >= 0) { @@ -299,20 +291,11 @@ public class CompassActivity extends AbstractActivity { navLocation.setText(res.getString(R.string.loc_trying)); } - if (!Settings.isUseCompass() || geo.getSpeed() > 5) { // use GPS when speed is higher than 18 km/h - updateNorthHeading(geo.getBearing()); - } + updateNorthHeading(DirectionProvider.getDirectionNow(CompassActivity.this, dir)); } catch (RuntimeException e) { Log.w("Failed to LocationUpdater location."); } } - - @Override - public void updateDirection(final float direction) { - if (app.currentGeo().getSpeed() <= 5) { // use compass when speed is lower than 18 km/h - updateNorthHeading(DirectionProvider.getDirectionNow(CompassActivity.this, direction)); - } - } }; private void updateNorthHeading(final float northHeading) { diff --git a/main/src/cgeo/geocaching/EditWaypointActivity.java b/main/src/cgeo/geocaching/EditWaypointActivity.java index 9010d3a..98884b8 100644 --- a/main/src/cgeo/geocaching/EditWaypointActivity.java +++ b/main/src/cgeo/geocaching/EditWaypointActivity.java @@ -10,10 +10,11 @@ import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.geopoint.DistanceParser; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.GeopointFormatter; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.ui.dialog.CoordinatesInputDialog; import cgeo.geocaching.ui.dialog.Dialogs; -import cgeo.geocaching.utils.GeoDirHandler; +import cgeo.geocaching.sensors.GeoDirHandler; import cgeo.geocaching.utils.Log; import cgeo.geocaching.utils.TextUtils; @@ -203,12 +204,12 @@ public class EditWaypointActivity extends AbstractActivity { public void onResume() { super.onResume(); - geoDirHandler.startGeo(); + geoDirHandler.start(); } @Override public void onPause() { - geoDirHandler.stopGeo(); + geoDirHandler.stop(); super.onPause(); } @@ -263,7 +264,7 @@ public class EditWaypointActivity extends AbstractActivity { final private GeoDirHandler geoDirHandler = new GeoDirHandler() { @Override - public void updateGeoData(final IGeoData geo) { + public void updateGeoDir(final IGeoData geo, final float dir) { if (geo.getCoords() == null) { return; } diff --git a/main/src/cgeo/geocaching/MainActivity.java b/main/src/cgeo/geocaching/MainActivity.java index ba7795a..bfe0217 100644 --- a/main/src/cgeo/geocaching/MainActivity.java +++ b/main/src/cgeo/geocaching/MainActivity.java @@ -13,12 +13,13 @@ import cgeo.geocaching.geopoint.Units; import cgeo.geocaching.list.PseudoList; import cgeo.geocaching.list.StoredList; import cgeo.geocaching.maps.CGeoMap; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.settings.SettingsActivity; import cgeo.geocaching.ui.Formatter; import cgeo.geocaching.ui.dialog.Dialogs; import cgeo.geocaching.utils.DatabaseBackupUtils; -import cgeo.geocaching.utils.GeoDirHandler; +import cgeo.geocaching.sensors.GeoDirHandler; import cgeo.geocaching.utils.Log; import cgeo.geocaching.utils.Version; @@ -142,7 +143,7 @@ public class MainActivity extends AbstractActivity { private int satellitesVisible = 0; @Override - public void updateGeoData(final IGeoData data) { + public void updateGeoDir(final IGeoData data, final float dir) { if (data.getGpsEnabled() == gpsEnabled && data.getSatellitesFixed() == satellitesFixed && data.getSatellitesVisible() == satellitesVisible) { @@ -214,8 +215,8 @@ public class MainActivity extends AbstractActivity { @Override public void onResume() { super.onResume(); - locationUpdater.startGeo(); - satellitesHandler.startGeo(); + locationUpdater.start(); + satellitesHandler.start(); updateUserInfoHandler.sendEmptyMessage(-1); startBackgroundLogin(); init(); @@ -256,8 +257,8 @@ public class MainActivity extends AbstractActivity { @Override public void onPause() { initialized = false; - locationUpdater.stopGeo(); - satellitesHandler.stopGeo(); + locationUpdater.stop(); + satellitesHandler.stop(); super.onPause(); } @@ -510,7 +511,7 @@ public class MainActivity extends AbstractActivity { private class UpdateLocation extends GeoDirHandler { @Override - public void updateGeoData(final IGeoData geo) { + public void updateGeoDir(final IGeoData geo, final float dir) { if (!nearestView.isClickable()) { nearestView.setFocusable(true); nearestView.setClickable(true); diff --git a/main/src/cgeo/geocaching/NavigateAnyPointActivity.java b/main/src/cgeo/geocaching/NavigateAnyPointActivity.java index f24e86e..72cbc64 100644 --- a/main/src/cgeo/geocaching/NavigateAnyPointActivity.java +++ b/main/src/cgeo/geocaching/NavigateAnyPointActivity.java @@ -9,12 +9,13 @@ import cgeo.geocaching.apps.cache.navi.NavigationAppFactory; import cgeo.geocaching.geopoint.DistanceParser; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.GeopointFormatter; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.ui.AbstractViewHolder; import cgeo.geocaching.ui.Formatter; import cgeo.geocaching.ui.dialog.CoordinatesInputDialog; import cgeo.geocaching.ui.dialog.Dialogs; -import cgeo.geocaching.utils.GeoDirHandler; +import cgeo.geocaching.sensors.GeoDirHandler; import cgeo.geocaching.utils.Log; import org.apache.commons.lang3.StringUtils; @@ -234,13 +235,13 @@ public class NavigateAnyPointActivity extends AbstractActivity { @Override public void onResume() { super.onResume(); - geoDirHandler.startGeo(); + geoDirHandler.start(); init(); } @Override public void onPause() { - geoDirHandler.stopGeo(); + geoDirHandler.stop(); super.onPause(); } @@ -454,7 +455,7 @@ public class NavigateAnyPointActivity extends AbstractActivity { private final GeoDirHandler geoDirHandler = new GeoDirHandler() { @Override - public void updateGeoData(final IGeoData geo) { + public void updateGeoDir(final IGeoData geo, final float dir) { try { latButton.setHint(geo.getCoords().format(GeopointFormatter.Format.LAT_DECMINUTE_RAW)); lonButton.setHint(geo.getCoords().format(GeopointFormatter.Format.LON_DECMINUTE_RAW)); diff --git a/main/src/cgeo/geocaching/SearchActivity.java b/main/src/cgeo/geocaching/SearchActivity.java index 8a89e5f..2a37e27 100644 --- a/main/src/cgeo/geocaching/SearchActivity.java +++ b/main/src/cgeo/geocaching/SearchActivity.java @@ -11,6 +11,7 @@ import cgeo.geocaching.connector.trackable.TrackableConnector; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.GeopointFormatter; import cgeo.geocaching.search.AutoCompleteAdapter; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.ui.dialog.CoordinatesInputDialog; import cgeo.geocaching.ui.dialog.Dialogs; diff --git a/main/src/cgeo/geocaching/WaypointPopup.java b/main/src/cgeo/geocaching/WaypointPopup.java index 5633e6f..916ad4c 100644 --- a/main/src/cgeo/geocaching/WaypointPopup.java +++ b/main/src/cgeo/geocaching/WaypointPopup.java @@ -6,6 +6,7 @@ import butterknife.InjectView; import cgeo.geocaching.apps.cache.navi.NavigationAppFactory; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.Units; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.ui.CacheDetailsCreator; import cgeo.geocaching.utils.Log; diff --git a/main/src/cgeo/geocaching/apps/cache/navi/GoogleMapsDirectionApp.java b/main/src/cgeo/geocaching/apps/cache/navi/GoogleMapsDirectionApp.java index c4351bb..4924786 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/GoogleMapsDirectionApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/GoogleMapsDirectionApp.java @@ -1,7 +1,7 @@ package cgeo.geocaching.apps.cache.navi; import cgeo.geocaching.CgeoApplication; -import cgeo.geocaching.IGeoData; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.R; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.maps.MapProviderFactory; diff --git a/main/src/cgeo/geocaching/filter/DistanceFilter.java b/main/src/cgeo/geocaching/filter/DistanceFilter.java index cddcf72..c217e7c 100644 --- a/main/src/cgeo/geocaching/filter/DistanceFilter.java +++ b/main/src/cgeo/geocaching/filter/DistanceFilter.java @@ -2,7 +2,7 @@ package cgeo.geocaching.filter; import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.Geocache; -import cgeo.geocaching.IGeoData; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.R; import cgeo.geocaching.geopoint.Geopoint; diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index e9ee8c2..8b6764d 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -3,9 +3,7 @@ package cgeo.geocaching.maps; import cgeo.geocaching.CacheListActivity; import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.DataStore; -import cgeo.geocaching.DirectionProvider; import cgeo.geocaching.Geocache; -import cgeo.geocaching.IGeoData; import cgeo.geocaching.R; import cgeo.geocaching.SearchResult; import cgeo.geocaching.Waypoint; @@ -31,18 +29,18 @@ import cgeo.geocaching.maps.interfaces.MapProvider; import cgeo.geocaching.maps.interfaces.MapSource; import cgeo.geocaching.maps.interfaces.MapViewImpl; import cgeo.geocaching.maps.interfaces.OnMapDragListener; +import cgeo.geocaching.sensors.GeoDirHandler; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.ui.dialog.LiveMapInfoDialogBuilder; import cgeo.geocaching.utils.AngleUtils; import cgeo.geocaching.utils.CancellableHandler; -import cgeo.geocaching.utils.GeoDirHandler; import cgeo.geocaching.utils.LeastRecentlyUsedSet; import cgeo.geocaching.utils.Log; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.HashCodeBuilder; - import rx.functions.Action1; import android.app.Activity; @@ -492,7 +490,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto public void onResume() { super.onResume(); - geoDirUpdate.startGeoAndDir(); + geoDirUpdate.start(); if (!CollectionUtils.isEmpty(dirtyCaches)) { for (String geocode : dirtyCaches) { @@ -515,7 +513,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto @Override public void onPause() { stopTimer(); - geoDirUpdate.stopGeoAndDir(); + geoDirUpdate.stop(); savePrefs(); if (mapView != null) { @@ -887,26 +885,14 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private long timeLastPositionOverlayCalculation = 0; @Override - public void updateGeoData(final IGeoData geo) { + public void updateGeoDir(final IGeoData geo, final float dir) { if (geo.isPseudoLocation()) { locationValid = false; } else { locationValid = true; currentLocation = geo.getLocation(); - - if (!Settings.isUseCompass() || geo.getSpeed() > 5) { // use GPS when speed is higher than 18 km/h - currentHeading = geo.getBearing(); - } - - repaintPositionOverlay(); - } - } - - @Override - public void updateDirection(final float direction) { - if (app.currentGeo().getSpeed() <= 5) { // use compass when speed is lower than 18 km/h - currentHeading = DirectionProvider.getDirectionNow(activity, direction); + currentHeading = dir; repaintPositionOverlay(); } } diff --git a/main/src/cgeo/geocaching/DirectionProvider.java b/main/src/cgeo/geocaching/sensors/DirectionProvider.java index 49a433e..162f14c 100644 --- a/main/src/cgeo/geocaching/DirectionProvider.java +++ b/main/src/cgeo/geocaching/sensors/DirectionProvider.java @@ -1,4 +1,4 @@ -package cgeo.geocaching; +package cgeo.geocaching.sensors; import cgeo.geocaching.compatibility.Compatibility; diff --git a/main/src/cgeo/geocaching/sensors/GeoData.java b/main/src/cgeo/geocaching/sensors/GeoData.java new file mode 100644 index 0000000..c0b3974 --- /dev/null +++ b/main/src/cgeo/geocaching/sensors/GeoData.java @@ -0,0 +1,67 @@ +package cgeo.geocaching.sensors; + +import cgeo.geocaching.enumerations.LocationProviderType; +import cgeo.geocaching.geopoint.Geopoint; + +import android.location.Location; +import android.location.LocationManager; + +class GeoData extends Location implements IGeoData { + private final boolean gpsEnabled; + private final int satellitesVisible; + private final int satellitesFixed; + private final boolean pseudoLocation; + + GeoData(final Location location, final boolean gpsEnabled, final int satellitesVisible, final int satellitesFixed, final boolean pseudoLocation) { + super(location); + this.gpsEnabled = gpsEnabled; + this.satellitesVisible = satellitesVisible; + this.satellitesFixed = satellitesFixed; + this.pseudoLocation = pseudoLocation; + } + + @Override + public Location getLocation() { + return this; + } + + private static LocationProviderType getLocationProviderType(final String provider) { + if (provider.equals(LocationManager.GPS_PROVIDER)) { + return LocationProviderType.GPS; + } + if (provider.equals(LocationManager.NETWORK_PROVIDER)) { + return LocationProviderType.NETWORK; + } + return LocationProviderType.LAST; + } + + @Override + public LocationProviderType getLocationProvider() { + return getLocationProviderType(getProvider()); + } + + @Override + public Geopoint getCoords() { + return new Geopoint(this); + } + + @Override + public boolean getGpsEnabled() { + return gpsEnabled; + } + + @Override + public int getSatellitesVisible() { + return satellitesVisible; + } + + @Override + public int getSatellitesFixed() { + return satellitesFixed; + } + + @Override + public boolean isPseudoLocation() { + return pseudoLocation; + } +} diff --git a/main/src/cgeo/geocaching/GeoDataProvider.java b/main/src/cgeo/geocaching/sensors/GeoDataProvider.java index e18f735..05d9467 100644 --- a/main/src/cgeo/geocaching/GeoDataProvider.java +++ b/main/src/cgeo/geocaching/sensors/GeoDataProvider.java @@ -1,18 +1,20 @@ -package cgeo.geocaching; +package cgeo.geocaching.sensors; -import cgeo.geocaching.enumerations.LocationProviderType; -import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.utils.Log; import org.apache.commons.lang3.StringUtils; import rx.Observable; import rx.Observable.OnSubscribe; +import rx.Scheduler.Inner; import rx.Subscriber; import rx.Subscription; +import rx.android.schedulers.AndroidSchedulers; +import rx.functions.Action0; +import rx.functions.Action1; import rx.observables.ConnectableObservable; import rx.subjects.BehaviorSubject; +import rx.subscriptions.CompositeSubscription; import rx.subscriptions.Subscriptions; -import rx.functions.Action0; import android.content.Context; import android.location.GpsSatellite; @@ -22,7 +24,9 @@ import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; -class GeoDataProvider implements OnSubscribe<IGeoData> { +import java.util.concurrent.TimeUnit; + +public class GeoDataProvider implements OnSubscribe<IGeoData> { private static final String LAST_LOCATION_PSEUDO_PROVIDER = "last"; private final LocationManager geoManager; @@ -52,64 +56,6 @@ class GeoDataProvider implements OnSubscribe<IGeoData> { } } - private static class GeoData extends Location implements IGeoData { - public boolean gpsEnabled = false; - public int satellitesVisible = 0; - public int satellitesFixed = 0; - - GeoData(final Location location, final boolean gpsEnabled, final int satellitesVisible, final int satellitesFixed) { - super(location); - this.gpsEnabled = gpsEnabled; - this.satellitesVisible = satellitesVisible; - this.satellitesFixed = satellitesFixed; - } - - @Override - public Location getLocation() { - return this; - } - - private static LocationProviderType getLocationProviderType(final String provider) { - if (provider.equals(LocationManager.GPS_PROVIDER)) { - return LocationProviderType.GPS; - } - if (provider.equals(LocationManager.NETWORK_PROVIDER)) { - return LocationProviderType.NETWORK; - } - return LocationProviderType.LAST; - } - - @Override - public LocationProviderType getLocationProvider() { - return getLocationProviderType(getProvider()); - } - - @Override - public Geopoint getCoords() { - return new Geopoint(this); - } - - @Override - public boolean getGpsEnabled() { - return gpsEnabled; - } - - @Override - public int getSatellitesVisible() { - return satellitesVisible; - } - - @Override - public int getSatellitesFixed() { - return satellitesFixed; - } - - @Override - public boolean isPseudoLocation() { - return StringUtils.equals(getProvider(), GeoDataProvider.LAST_LOCATION_PSEUDO_PROVIDER); - } - } - /** * Build a new geo data provider object. * <p/> @@ -136,28 +82,40 @@ class GeoDataProvider implements OnSubscribe<IGeoData> { final ConnectableObservable<IGeoData> worker = new ConnectableObservable<IGeoData>(this) { @Override public Subscription connect() { - final GpsStatus.Listener gpsStatusListener = new GpsStatusListener(); - geoManager.addGpsStatusListener(gpsStatusListener); - - final Listener networkListener = new Listener(LocationManager.NETWORK_PROVIDER, netLocation); - final Listener gpsListener = new Listener(LocationManager.GPS_PROVIDER, gpsLocation); - - for (final Listener listener : new Listener[] { networkListener, gpsListener }) { - try { - geoManager.requestLocationUpdates(listener.locationProvider, 0, 0, listener); - } catch (final Exception e) { - Log.w("There is no location provider " + listener.locationProvider); - } - } - - return Subscriptions.create(new Action0() { + final CompositeSubscription subscription = new CompositeSubscription(); + AndroidSchedulers.mainThread().schedule(new Action1<Inner>() { @Override - public void call() { - geoManager.removeUpdates(networkListener); - geoManager.removeUpdates(gpsListener); - geoManager.removeGpsStatusListener(gpsStatusListener); + public void call(final Inner inner) { + final GpsStatus.Listener gpsStatusListener = new GpsStatusListener(); + geoManager.addGpsStatusListener(gpsStatusListener); + + final Listener networkListener = new Listener(LocationManager.NETWORK_PROVIDER, netLocation); + final Listener gpsListener = new Listener(LocationManager.GPS_PROVIDER, gpsLocation); + + for (final Listener listener : new Listener[] { networkListener, gpsListener }) { + try { + geoManager.requestLocationUpdates(listener.locationProvider, 0, 0, listener); + } catch (final Exception e) { + Log.w("There is no location provider " + listener.locationProvider); + } + } + + subscription.add(Subscriptions.create(new Action0() { + @Override + public void call() { + AndroidSchedulers.mainThread().schedule(new Action1<Inner>() { + @Override + public void call(final Inner inner) { + geoManager.removeUpdates(networkListener); + geoManager.removeUpdates(gpsListener); + geoManager.removeGpsStatusListener(gpsStatusListener); + } + }, 2500, TimeUnit.MILLISECONDS); + } + })); } }); + return subscription; } }; @@ -189,7 +147,7 @@ class GeoDataProvider implements OnSubscribe<IGeoData> { } // Start with an historical GeoData just in case someone queries it before we get // a chance to get any information. - return new GeoData(initialLocation, false, 0, 0); + return new GeoData(initialLocation, false, 0, 0, true); } private static void copyCoords(final Location target, final Location source) { @@ -297,7 +255,8 @@ class GeoDataProvider implements OnSubscribe<IGeoData> { // We do not necessarily get signalled when satellites go to 0/0. final int visible = gpsLocation.isRecent() ? satellitesVisible : 0; - final IGeoData current = new GeoData(locationData.location, gpsEnabled, visible, satellitesFixed); + final boolean pseudoLocation = StringUtils.equals(locationData.location.getProvider(), LAST_LOCATION_PSEUDO_PROVIDER); + final IGeoData current = new GeoData(locationData.location, gpsEnabled, visible, satellitesFixed, pseudoLocation); subject.onNext(current); } diff --git a/main/src/cgeo/geocaching/sensors/GeoDirHandler.java b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java new file mode 100644 index 0000000..523fb1f --- /dev/null +++ b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java @@ -0,0 +1,61 @@ +package cgeo.geocaching.sensors; + +import cgeo.geocaching.CgeoApplication; +import cgeo.geocaching.settings.Settings; + +import org.apache.commons.lang3.tuple.ImmutablePair; +import rx.Subscription; +import rx.android.schedulers.AndroidSchedulers; +import rx.functions.Action1; + +/** + * GeoData and Direction handler. + * <p> + * To use this class, override {@link #updateGeoDir(IGeoData, Float)}. You need to start the handler using + * {@link #start()}. A good place to do so might be the {@code onResume} method of the Activity. Stop the Handler + * accordingly in {@code onPause}. + */ +public abstract class GeoDirHandler { + private static final CgeoApplication app = CgeoApplication.getInstance(); + + private Subscription subscription = null; + + /** + * Update method called when new data is available. + * + * @param geoData the new geographical data + * @param direction the new direction + * + * If the device goes fast enough, or if the compass use is not enabled in the settings, + * the GPS direction information will be used instead of the compass one. + */ + public void updateGeoDir(@SuppressWarnings("unused") final IGeoData geoData, @SuppressWarnings("unused") final float direction) { + } + + private void handleGeoDir(final ImmutablePair<IGeoData, Float> geoDir) { + final IGeoData geoData = geoDir.left; + final boolean useGPSBearing = !Settings.isUseCompass() || geoData.getSpeed() > 5; + updateGeoDir(geoData, useGPSBearing ? geoData.getBearing() : geoDir.right); + } + + /** + * Register the current GeoDirHandler for GeoData and direction information (if the + * preferences allow it). + */ + public void start() { + subscription = app.geoDirObservable().subscribe(new Action1<ImmutablePair<IGeoData, Float>>() { + @Override + public void call(final ImmutablePair<IGeoData, Float> geoDir) { + handleGeoDir(geoDir); + } + }, AndroidSchedulers.mainThread()); + } + + /** + * Unregister the current GeoDirHandler for GeoData information. + */ + public void stop() { + subscription.unsubscribe(); + } + +} diff --git a/main/src/cgeo/geocaching/IGeoData.java b/main/src/cgeo/geocaching/sensors/IGeoData.java index c5ef698..5b4f046 100644 --- a/main/src/cgeo/geocaching/IGeoData.java +++ b/main/src/cgeo/geocaching/sensors/IGeoData.java @@ -1,4 +1,4 @@ -package cgeo.geocaching; +package cgeo.geocaching.sensors; import cgeo.geocaching.enumerations.LocationProviderType; import cgeo.geocaching.geopoint.Geopoint; diff --git a/main/src/cgeo/geocaching/speech/SpeechService.java b/main/src/cgeo/geocaching/speech/SpeechService.java index 8c650c3..b13218c 100644 --- a/main/src/cgeo/geocaching/speech/SpeechService.java +++ b/main/src/cgeo/geocaching/speech/SpeechService.java @@ -1,12 +1,11 @@ package cgeo.geocaching.speech; -import cgeo.geocaching.CgeoApplication; -import cgeo.geocaching.DirectionProvider; import cgeo.geocaching.R; import cgeo.geocaching.activity.ActivityMixin; import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.settings.Settings; -import cgeo.geocaching.utils.GeoDirHandler; +import cgeo.geocaching.sensors.GeoDirHandler; import cgeo.geocaching.utils.Log; import org.apache.commons.lang3.StringUtils; @@ -42,28 +41,32 @@ public class SpeechService extends Service implements OnInitListener { private boolean initialized = false; protected float direction; protected Geopoint position; - protected boolean directionInitialized = !Settings.isUseCompass(); // don't wait for magnetometer, if it shall not be used - protected boolean positionInitialized = false; - GeoDirHandler geoHandler = new GeoDirHandler() { + final GeoDirHandler geoDirHandler = new GeoDirHandler() { @Override - public void updateDirection(float newDirection) { - if (CgeoApplication.getInstance().currentGeo().getSpeed() <= 5) { - direction = DirectionProvider.getDirectionNow(startingActivity, newDirection); - directionInitialized = true; - updateCompass(); + public void updateGeoDir(final IGeoData newGeo, final float newDirection) { + position = newGeo.getCoords(); + direction = newDirection; + // avoid any calculation, if the delay since the last output is not long enough + final long now = System.currentTimeMillis(); + if (now - lastSpeechTime <= SPEECH_MINPAUSE_SECONDS * 1000) { + return; } - } - @Override - public void updateGeoData(cgeo.geocaching.IGeoData newGeo) { - position = newGeo.getCoords(); - positionInitialized = true; - if (!Settings.isUseCompass() || newGeo.getSpeed() > 5) { - direction = newGeo.getBearing(); - directionInitialized = true; + // to speak, we want max pause to have elapsed or distance to geopoint to have changed by a given amount + final float distance = position.distanceTo(target); + if (now - lastSpeechTime <= SPEECH_MAXPAUSE_SECONDS * 1000) { + if (Math.abs(lastSpeechDistance - distance) < getDeltaForDistance(distance)) { + return; + } + } + + final String text = TextFactory.getText(position, target, direction); + if (StringUtils.isNotEmpty(text)) { + lastSpeechTime = System.currentTimeMillis(); + lastSpeechDistance = distance; + speak(text); } - updateCompass(); } }; /** @@ -78,34 +81,6 @@ public class SpeechService extends Service implements OnInitListener { return null; } - protected void updateCompass() { - // make sure we have both sensor values before talking - if (!positionInitialized || !directionInitialized) { - return; - } - - // avoid any calculation, if the delay since the last output is not long enough - final long now = System.currentTimeMillis(); - if (now - lastSpeechTime <= SPEECH_MINPAUSE_SECONDS * 1000) { - return; - } - - // to speak, we want max pause to have elapsed or distance to geopoint to have changed by a given amount - final float distance = position.distanceTo(target); - if (now - lastSpeechTime <= SPEECH_MAXPAUSE_SECONDS * 1000) { - if (Math.abs(lastSpeechDistance - distance) < getDeltaForDistance(distance)) { - return; - } - } - - final String text = TextFactory.getText(position, target, direction); - if (StringUtils.isNotEmpty(text)) { - lastSpeechTime = System.currentTimeMillis(); - lastSpeechDistance = distance; - speak(text); - } - } - /** * Return distance required to be moved based on overall distance.<br> * @@ -131,7 +106,7 @@ public class SpeechService extends Service implements OnInitListener { @Override public void onDestroy() { - geoHandler.stopGeoAndDir(); + geoDirHandler.stop(); if (tts != null) { tts.stop(); tts.shutdown(); @@ -157,7 +132,8 @@ public class SpeechService extends Service implements OnInitListener { if (switchLocale == TextToSpeech.LANG_MISSING_DATA) { startingActivity.startActivity(new Intent(Engine.ACTION_INSTALL_TTS_DATA)); return; - } else if (switchLocale == TextToSpeech.LANG_NOT_SUPPORTED) { + } + if (switchLocale == TextToSpeech.LANG_NOT_SUPPORTED) { Log.e("Current languge not supported by text to speech."); ActivityMixin.showToast(startingActivity, R.string.err_tts_lang_not_supported); return; @@ -165,11 +141,7 @@ public class SpeechService extends Service implements OnInitListener { initialized = true; - if (Settings.isUseCompass()) { - geoHandler.startGeoAndDir(); - } else { - geoHandler.startGeo(); - } + geoDirHandler.start(); } @Override diff --git a/main/src/cgeo/geocaching/ui/CacheListAdapter.java b/main/src/cgeo/geocaching/ui/CacheListAdapter.java index e7752d6..828bd43 100644 --- a/main/src/cgeo/geocaching/ui/CacheListAdapter.java +++ b/main/src/cgeo/geocaching/ui/CacheListAdapter.java @@ -5,7 +5,7 @@ import butterknife.InjectView; import cgeo.geocaching.CacheDetailActivity; import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.Geocache; -import cgeo.geocaching.IGeoData; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.R; import cgeo.geocaching.enumerations.CacheListType; import cgeo.geocaching.enumerations.CacheType; diff --git a/main/src/cgeo/geocaching/ui/dialog/CoordinatesInputDialog.java b/main/src/cgeo/geocaching/ui/dialog/CoordinatesInputDialog.java index 651ff6e..c150434 100644 --- a/main/src/cgeo/geocaching/ui/dialog/CoordinatesInputDialog.java +++ b/main/src/cgeo/geocaching/ui/dialog/CoordinatesInputDialog.java @@ -1,7 +1,7 @@ package cgeo.geocaching.ui.dialog; import cgeo.geocaching.Geocache; -import cgeo.geocaching.IGeoData; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.R; import cgeo.geocaching.activity.AbstractActivity; import cgeo.geocaching.activity.ActivityMixin; diff --git a/main/src/cgeo/geocaching/utils/GeoDirHandler.java b/main/src/cgeo/geocaching/utils/GeoDirHandler.java deleted file mode 100644 index 7050e61..0000000 --- a/main/src/cgeo/geocaching/utils/GeoDirHandler.java +++ /dev/null @@ -1,129 +0,0 @@ -package cgeo.geocaching.utils; - -import cgeo.geocaching.CgeoApplication; -import cgeo.geocaching.IGeoData; -import cgeo.geocaching.settings.Settings; - -import rx.Scheduler; -import rx.Subscription; -import rx.android.schedulers.AndroidSchedulers; -import rx.schedulers.Schedulers; -import rx.functions.Action1; - -import java.util.concurrent.TimeUnit; - -/** - * GeoData and Direction handler. - * <p> - * To use this class, override at least one of {@link #updateDirection(float)} or {@link #updateGeoData(IGeoData)}. You - * need to start the handler using one of - * <ul> - * <li>{@link #startDir()}</li> - * <li>{@link #startGeo()}</li> - * <li>{@link #startGeoAndDir()}</li> - * </ul> - * A good place might be the {@code onResume} method of the Activity. Stop the Handler accordingly in {@code onPause}. - * </p> - */ -public abstract class GeoDirHandler { - private static final CgeoApplication app = CgeoApplication.getInstance(); - - private Subscription dirSubscription = null; - private Subscription geoSubscription = null; - - /** - * Update method called when new IGeoData is available. - * - * @param data - * the new data - */ - public void updateGeoData(final IGeoData data) { - // Override this in children - } - - /** - * Update method called when new direction data is available. - * - * @param direction - * the new direction - */ - public void updateDirection(final float direction) { - // Override this in children - } - - /** - * Register the current GeoDirHandler for GeoData information. - */ - public synchronized void startGeo() { - geoSubscription = app.currentGeoObject() - .subscribeOn(AndroidSchedulers.mainThread()) - .subscribe(new Action1<IGeoData>() { - @Override - public void call(final IGeoData geoData) { - updateGeoData(geoData); - } - }); - } - - /** - * Register the current GeoDirHandler for direction information if the preferences - * allow it. - */ - public synchronized void startDir() { - if (Settings.isUseCompass()) { - dirSubscription = app.currentDirObject() - .subscribeOn(AndroidSchedulers.mainThread()) - .subscribe(new Action1<Float>() { - @Override - public void call(final Float direction) { - updateDirection(direction); - } - }); - } - } - - /** - * Register the current GeoDirHandler for GeoData and direction information (if the - * preferences allow it). - */ - public void startGeoAndDir() { - startGeo(); - startDir(); - } - - /** - * Unregister the current GeoDirHandler for GeoData information. - */ - public synchronized void stopGeo() { - // Delay the unsubscription by 2.5 seconds, so that another activity has - // the time to subscribe and the GPS receiver will not be turned down. - if (geoSubscription != null) { - final Subscription subscription = geoSubscription; - geoSubscription = null; - Schedulers.newThread().schedule(new Action1<Scheduler.Inner>() { - @Override - public void call(final Scheduler.Inner inner) { - subscription.unsubscribe(); - } - }, 2500, TimeUnit.MILLISECONDS); - } - } - - /** - * Unregister the current GeoDirHandler for direction information. - */ - public synchronized void stopDir() { - if (dirSubscription != null) { - dirSubscription.unsubscribe(); - dirSubscription = null; - } - } - - /** - * Unregister the current GeoDirHandler for GeoData and direction information. - */ - public void stopGeoAndDir() { - stopGeo(); - stopDir(); - } -} |
