aboutsummaryrefslogtreecommitdiffstats
path: root/main
diff options
context:
space:
mode:
Diffstat (limited to 'main')
-rw-r--r--main/src/cgeo/geocaching/AbstractPopupActivity.java4
-rw-r--r--main/src/cgeo/geocaching/CacheDetailActivity.java4
-rw-r--r--main/src/cgeo/geocaching/CacheListActivity.java19
-rw-r--r--main/src/cgeo/geocaching/CgeoApplication.java52
-rw-r--r--main/src/cgeo/geocaching/CompassActivity.java4
-rw-r--r--main/src/cgeo/geocaching/EditWaypointActivity.java4
-rw-r--r--main/src/cgeo/geocaching/MainActivity.java6
-rw-r--r--main/src/cgeo/geocaching/NavigateAnyPointActivity.java4
-rw-r--r--main/src/cgeo/geocaching/maps/CGeoMap.java8
-rw-r--r--main/src/cgeo/geocaching/sensors/GeoDirHandler.java84
-rw-r--r--main/src/cgeo/geocaching/speech/SpeechService.java6
11 files changed, 137 insertions, 58 deletions
diff --git a/main/src/cgeo/geocaching/AbstractPopupActivity.java b/main/src/cgeo/geocaching/AbstractPopupActivity.java
index 3bb92de..2067a61 100644
--- a/main/src/cgeo/geocaching/AbstractPopupActivity.java
+++ b/main/src/cgeo/geocaching/AbstractPopupActivity.java
@@ -46,7 +46,7 @@ public abstract class AbstractPopupActivity extends AbstractActivity implements
private final GeoDirHandler geoUpdate = new GeoDirHandler() {
@Override
- public void updateGeoDir(final IGeoData geo, final float dir) {
+ public void updateGeoData(final IGeoData geo) {
try {
if (geo.getCoords() != null && cache != null && cache.getCoords() != null) {
cacheDistance.setText(Units.getDistanceFromKilometers(geo.getCoords().distanceTo(cache.getCoords())));
@@ -182,7 +182,7 @@ public abstract class AbstractPopupActivity extends AbstractActivity implements
@Override
public void onResume() {
- super.onResume(geoUpdate.start());
+ super.onResume(geoUpdate.start(GeoDirHandler.UPDATE_GEODATA));
init();
}
diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java
index ff27049..ce296e4 100644
--- a/main/src/cgeo/geocaching/CacheDetailActivity.java
+++ b/main/src/cgeo/geocaching/CacheDetailActivity.java
@@ -145,7 +145,7 @@ public class CacheDetailActivity extends AbstractViewPagerActivity<CacheDetailAc
private final GeoDirHandler locationUpdater = new GeoDirHandler() {
@Override
- public void updateGeoDir(final IGeoData geo, final float dir) {
+ public void updateGeoData(final IGeoData geo) {
if (cacheDistanceView == null) {
return;
}
@@ -320,7 +320,7 @@ public class CacheDetailActivity extends AbstractViewPagerActivity<CacheDetailAc
@Override
public void onResume() {
- super.onResume(locationUpdater.start());
+ super.onResume(locationUpdater.start(GeoDirHandler.UPDATE_GEODATA));
if (refreshOnResume) {
notifyDataSetChanged();
diff --git a/main/src/cgeo/geocaching/CacheListActivity.java b/main/src/cgeo/geocaching/CacheListActivity.java
index 749aca3..7f43d63 100644
--- a/main/src/cgeo/geocaching/CacheListActivity.java
+++ b/main/src/cgeo/geocaching/CacheListActivity.java
@@ -123,12 +123,17 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA
private final GeoDirHandler geoDirHandler = new GeoDirHandler() {
@Override
- public void updateGeoDir(final IGeoData geo, final float dir) {
- if (geo.getCoords() != null) {
- adapter.setActualCoordinates(geo.getCoords());
- if (Settings.isLiveList()) {
- adapter.setActualHeading(dir);
- }
+ public void updateDirection(final float direction) {
+ if (Settings.isLiveList()) {
+ adapter.setActualHeading(direction);
+ }
+ }
+
+ @Override
+ public void updateGeoData(final IGeoData geoData) {
+ final Geopoint coords = geoData.getCoords();
+ if (coords != null) {
+ adapter.setActualCoordinates(coords);
}
}
@@ -456,7 +461,7 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA
public void onResume() {
super.onResume();
- resumeSubscription = geoDirHandler.start();
+ resumeSubscription = geoDirHandler.start(GeoDirHandler.UPDATE_GEODATA | GeoDirHandler.UPDATE_DIRECTION);
adapter.setSelectMode(false);
setAdapterCurrentCoordinates(true);
diff --git a/main/src/cgeo/geocaching/CgeoApplication.java b/main/src/cgeo/geocaching/CgeoApplication.java
index 21307d2..eda8420 100644
--- a/main/src/cgeo/geocaching/CgeoApplication.java
+++ b/main/src/cgeo/geocaching/CgeoApplication.java
@@ -5,9 +5,9 @@ import cgeo.geocaching.sensors.GeoDataProvider;
import cgeo.geocaching.sensors.IGeoData;
import cgeo.geocaching.utils.Log;
-import org.apache.commons.lang3.tuple.ImmutablePair;
import rx.Observable;
-import rx.functions.Func2;
+import rx.functions.Action1;
+import rx.observables.ConnectableObservable;
import android.app.Application;
@@ -17,7 +17,10 @@ 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;
+ private Observable<IGeoData> geoDataObservable;
+ private Observable<Float> directionObservable;
+ private volatile IGeoData currentGeo = null;
+ private volatile float currentDirection = 0.0f;
public CgeoApplication() {
setInstance(this);
@@ -37,31 +40,40 @@ public class CgeoApplication extends Application {
DataStore.removeAllFromCache();
}
- public synchronized Observable<ImmutablePair<IGeoData, Float>> geoDirObservable() {
- if (geoDir == null) {
- final Observable<IGeoData> geo = GeoDataProvider.create(this);
- final Observable<Float> dir = DirectionProvider.create(this);
- final Observable<ImmutablePair<IGeoData, Float>> combined = Observable.combineLatest(geo, dir, new Func2<IGeoData, Float, ImmutablePair<IGeoData, Float>>() {
- @Override
- public ImmutablePair<IGeoData, Float> call(final IGeoData geoData, final Float dir) {
- return ImmutablePair.of(geoData, dir);
- }
- });
- geoDir = combined.publish().refCount();
+ public synchronized Observable<IGeoData> geoDataObservable() {
+ if (geoDataObservable == null) {
+ final ConnectableObservable<IGeoData> onDemand = GeoDataProvider.create(this).publish();
+ onDemand.subscribe(new Action1<IGeoData>() {
+ @Override
+ public void call(final IGeoData geoData) {
+ currentGeo = geoData;
+ }
+ });
+ geoDataObservable = onDemand.refCount();
}
- return geoDir;
+ return geoDataObservable;
}
- private ImmutablePair<IGeoData, Float> currentGeoDir() {
- return geoDirObservable().first().toBlockingObservable().single();
+ public synchronized Observable<Float> directionObservable() {
+ if (directionObservable == null) {
+ final ConnectableObservable<Float> onDemand = DirectionProvider.create(this).publish();
+ onDemand.subscribe(new Action1<Float>() {
+ @Override
+ public void call(final Float direction) {
+ currentDirection = direction;
+ }
+ });
+ directionObservable = onDemand.refCount();
+ }
+ return directionObservable;
}
public IGeoData currentGeo() {
- return currentGeoDir().left;
+ return currentGeo != null ? currentGeo : geoDataObservable().toBlockingObservable().first();
}
- public Float currentDirection() {
- return currentGeoDir().right;
+ public float currentDirection() {
+ return currentDirection;
}
public boolean isLiveMapHintShownInThisSession() {
diff --git a/main/src/cgeo/geocaching/CompassActivity.java b/main/src/cgeo/geocaching/CompassActivity.java
index b7dcd7e..b4bff88 100644
--- a/main/src/cgeo/geocaching/CompassActivity.java
+++ b/main/src/cgeo/geocaching/CompassActivity.java
@@ -9,13 +9,13 @@ import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.Units;
import cgeo.geocaching.maps.CGeoMap;
import cgeo.geocaching.sensors.DirectionProvider;
+import cgeo.geocaching.sensors.GeoDirHandler;
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.sensors.GeoDirHandler;
import cgeo.geocaching.utils.Log;
import org.apache.commons.lang3.StringUtils;
@@ -118,7 +118,7 @@ public class CompassActivity extends AbstractActivity {
@Override
public void onResume() {
- super.onResume(geoDirHandler.start());
+ super.onResume(geoDirHandler.start(GeoDirHandler.UPDATE_GEODIR));
}
@Override
diff --git a/main/src/cgeo/geocaching/EditWaypointActivity.java b/main/src/cgeo/geocaching/EditWaypointActivity.java
index ae17077..45775a7 100644
--- a/main/src/cgeo/geocaching/EditWaypointActivity.java
+++ b/main/src/cgeo/geocaching/EditWaypointActivity.java
@@ -202,7 +202,7 @@ public class EditWaypointActivity extends AbstractActivity {
@Override
public void onResume() {
- super.onResume(geoDirHandler.start());
+ super.onResume(geoDirHandler.start(GeoDirHandler.UPDATE_GEODATA));
}
@Override
@@ -261,7 +261,7 @@ public class EditWaypointActivity extends AbstractActivity {
final private GeoDirHandler geoDirHandler = new GeoDirHandler() {
@Override
- public void updateGeoDir(final IGeoData geo, final float dir) {
+ public void updateGeoData(final IGeoData geo) {
if (geo.getCoords() == null) {
return;
}
diff --git a/main/src/cgeo/geocaching/MainActivity.java b/main/src/cgeo/geocaching/MainActivity.java
index ab6256f..3295d04 100644
--- a/main/src/cgeo/geocaching/MainActivity.java
+++ b/main/src/cgeo/geocaching/MainActivity.java
@@ -144,7 +144,7 @@ public class MainActivity extends AbstractActivity {
private int satellitesVisible = 0;
@Override
- public void updateGeoDir(final IGeoData data, final float dir) {
+ public void updateGeoData(final IGeoData data) {
if (data.getGpsEnabled() == gpsEnabled &&
data.getSatellitesFixed() == satellitesFixed &&
data.getSatellitesVisible() == satellitesVisible) {
@@ -215,7 +215,7 @@ public class MainActivity extends AbstractActivity {
@Override
public void onResume() {
- super.onResume(Subscriptions.from(locationUpdater.start(), satellitesHandler.start()));
+ super.onResume(Subscriptions.from(locationUpdater.start(GeoDirHandler.UPDATE_GEODATA), satellitesHandler.start(GeoDirHandler.UPDATE_GEODATA)));
updateUserInfoHandler.sendEmptyMessage(-1);
startBackgroundLogin();
init();
@@ -508,7 +508,7 @@ public class MainActivity extends AbstractActivity {
private class UpdateLocation extends GeoDirHandler {
@Override
- public void updateGeoDir(final IGeoData geo, final float dir) {
+ public void updateGeoData(final IGeoData geo) {
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 92a08fe..d577eb5 100644
--- a/main/src/cgeo/geocaching/NavigateAnyPointActivity.java
+++ b/main/src/cgeo/geocaching/NavigateAnyPointActivity.java
@@ -234,7 +234,7 @@ public class NavigateAnyPointActivity extends AbstractActivity {
@Override
public void onResume() {
- super.onResume(geoDirHandler.start());
+ super.onResume(geoDirHandler.start(GeoDirHandler.UPDATE_GEODATA));
init();
}
@@ -453,7 +453,7 @@ public class NavigateAnyPointActivity extends AbstractActivity {
private final GeoDirHandler geoDirHandler = new GeoDirHandler() {
@Override
- public void updateGeoDir(final IGeoData geo, final float dir) {
+ public void updateGeoData(final IGeoData geo) {
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/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java
index 61b321d..962f5e5 100644
--- a/main/src/cgeo/geocaching/maps/CGeoMap.java
+++ b/main/src/cgeo/geocaching/maps/CGeoMap.java
@@ -44,6 +44,9 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
import rx.Scheduler;
import rx.Subscription;
import rx.functions.Action1;
+import rx.schedulers.Schedulers;
+import rx.subscriptions.CompositeSubscription;
+import rx.subscriptions.Subscriptions;
import android.app.Activity;
import android.app.AlertDialog;
@@ -69,9 +72,6 @@ import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;
-import rx.schedulers.Schedulers;
-import rx.subscriptions.CompositeSubscription;
-import rx.subscriptions.Subscriptions;
import java.io.File;
import java.util.ArrayList;
@@ -495,7 +495,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto
@Override
public void onResume() {
super.onResume();
- resumeSubscription = Subscriptions.from(geoDirUpdate.start(), startTimer());
+ resumeSubscription = Subscriptions.from(geoDirUpdate.start(GeoDirHandler.UPDATE_GEODIR), startTimer());
if (!CollectionUtils.isEmpty(dirtyCaches)) {
for (String geocode : dirtyCaches) {
diff --git a/main/src/cgeo/geocaching/sensors/GeoDirHandler.java b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
index 588bd84..37a0c5b 100644
--- a/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
+++ b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
@@ -4,10 +4,13 @@ import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.settings.Settings;
import org.apache.commons.lang3.tuple.ImmutablePair;
-
+import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
+import rx.functions.Func1;
+import rx.functions.Func2;
+import rx.subscriptions.CompositeSubscription;
/**
* GeoData and Direction handler.
@@ -17,10 +20,34 @@ import rx.functions.Action1;
* accordingly in {@code onPause}.
*/
public abstract class GeoDirHandler {
+
+ public static final int UPDATE_GEODATA = 1 << 1;
+ public static final int UPDATE_DIRECTION = 1 << 2;
+ public static final int UPDATE_GEODIR = 1 << 3;
+
private static final CgeoApplication app = CgeoApplication.getInstance();
/**
+ * Update method called when new geodata is available. This method is called on the UI thread.
+ * {@link #start(int)} must be called with the {@link #UPDATE_GEODATA} flag set.
+ *
+ * @param geoData the new geographical data
+ */
+ public void updateGeoData(@SuppressWarnings("unused") final IGeoData geoData) {
+ }
+
+ /**
+ * Update method called when new direction is available. This method is called on the UI thread.
+ * {@link #start(int)} must be called with the {@link #UPDATE_DIRECTION} flag set.
+ *
+ * @param direction the new direction
+ */
+ public void updateDirection(@SuppressWarnings("unused") final float direction) {
+ }
+
+ /**
* Update method called when new data is available. This method is called on the UI thread.
+ * {@link #start(int)} must be called with the {@link #UPDATE_GEODIR} flag set.
*
* @param geoData the new geographical data
* @param direction the new direction
@@ -31,23 +58,58 @@ public abstract class GeoDirHandler {
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;
+ private static Observable<Float> fixedDirection() {
+ return app.directionObservable().map(new Func1<Float, Float>() {
+ @Override
+ public Float call(final Float direction) {
+ final IGeoData geoData = app.currentGeo();
+ return fixDirection(geoData, direction);
+ }
+ });
+
+ }
+
+ private static float fixDirection(final IGeoData geoData, final float direction) {
final boolean useGPSBearing = !Settings.isUseCompass() || geoData.getSpeed() > 5;
- updateGeoDir(geoData, useGPSBearing ? geoData.getBearing() : geoDir.right);
+ return useGPSBearing ? geoData.getBearing() : direction;
}
/**
* Register the current GeoDirHandler for GeoData and direction information (if the
* preferences allow it).
*/
- public Subscription start() {
- return app.geoDirObservable().observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<ImmutablePair<IGeoData, Float>>() {
- @Override
- public void call(final ImmutablePair<IGeoData, Float> geoDir) {
- handleGeoDir(geoDir);
- }
- });
+ public Subscription start(final int flags) {
+ final CompositeSubscription subscriptions = new CompositeSubscription();
+ if ((flags & UPDATE_GEODATA) != 0) {
+ subscriptions.add(app.geoDataObservable().observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<IGeoData>() {
+ @Override
+ public void call(final IGeoData geoData) {
+ updateGeoData(geoData);
+ }
+ }));
+ }
+ if ((flags & UPDATE_DIRECTION) != 0) {
+ subscriptions.add(fixedDirection().observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Float>() {
+ @Override
+ public void call(final Float direction) {
+ updateDirection(direction);
+ }
+ }));
+ }
+ if ((flags & UPDATE_GEODIR) != 0) {
+ subscriptions.add(Observable.combineLatest(app.geoDataObservable(), app.directionObservable(), new Func2<IGeoData, Float, ImmutablePair<IGeoData, Float>>() {
+ @Override
+ public ImmutablePair<IGeoData, Float> call(final IGeoData geoData, final Float direction) {
+ return ImmutablePair.of(geoData, fixDirection(geoData, direction));
+ }
+ }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<ImmutablePair<IGeoData, Float>>() {
+ @Override
+ public void call(final ImmutablePair<IGeoData, Float> geoDir) {
+ updateGeoDir(geoDir.left, geoDir.right);
+ }
+ }));
+ }
+ return subscriptions;
}
}
diff --git a/main/src/cgeo/geocaching/speech/SpeechService.java b/main/src/cgeo/geocaching/speech/SpeechService.java
index 086227d..5cf732c 100644
--- a/main/src/cgeo/geocaching/speech/SpeechService.java
+++ b/main/src/cgeo/geocaching/speech/SpeechService.java
@@ -3,12 +3,13 @@ package cgeo.geocaching.speech;
import cgeo.geocaching.R;
import cgeo.geocaching.activity.ActivityMixin;
import cgeo.geocaching.geopoint.Geopoint;
+import cgeo.geocaching.sensors.GeoDirHandler;
import cgeo.geocaching.sensors.IGeoData;
import cgeo.geocaching.settings.Settings;
-import cgeo.geocaching.sensors.GeoDirHandler;
import cgeo.geocaching.utils.Log;
import org.apache.commons.lang3.StringUtils;
+import rx.Subscription;
import android.app.Activity;
import android.app.Service;
@@ -17,7 +18,6 @@ import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.Engine;
import android.speech.tts.TextToSpeech.OnInitListener;
-import rx.Subscription;
import java.util.Locale;
@@ -143,7 +143,7 @@ public class SpeechService extends Service implements OnInitListener {
initialized = true;
- initSubscription = geoDirHandler.start();
+ initSubscription = geoDirHandler.start(GeoDirHandler.UPDATE_GEODIR);
}
@Override