aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main/src/cgeo/geocaching/AbstractPopupActivity.java8
-rw-r--r--main/src/cgeo/geocaching/CacheDetailActivity.java6
-rw-r--r--main/src/cgeo/geocaching/CacheListActivity.java27
-rw-r--r--main/src/cgeo/geocaching/CgeoApplication.java35
-rw-r--r--main/src/cgeo/geocaching/CompassActivity.java29
-rw-r--r--main/src/cgeo/geocaching/EditWaypointActivity.java6
-rw-r--r--main/src/cgeo/geocaching/MainActivity.java12
-rw-r--r--main/src/cgeo/geocaching/NavigateAnyPointActivity.java6
-rw-r--r--main/src/cgeo/geocaching/maps/CGeoMap.java26
-rw-r--r--main/src/cgeo/geocaching/sensors/GeoData.java8
-rw-r--r--main/src/cgeo/geocaching/sensors/GeoDataProvider.java56
-rw-r--r--main/src/cgeo/geocaching/sensors/GeoDirHandler.java115
-rw-r--r--main/src/cgeo/geocaching/speech/SpeechService.java79
13 files changed, 144 insertions, 269 deletions
diff --git a/main/src/cgeo/geocaching/AbstractPopupActivity.java b/main/src/cgeo/geocaching/AbstractPopupActivity.java
index 4cbe61d..375d06a 100644
--- a/main/src/cgeo/geocaching/AbstractPopupActivity.java
+++ b/main/src/cgeo/geocaching/AbstractPopupActivity.java
@@ -41,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())));
@@ -160,7 +160,7 @@ public abstract class AbstractPopupActivity extends AbstractActivity implements
@Override
public void onPause() {
- geoUpdate.stopGeo();
+ geoUpdate.stop();
super.onPause();
}
@@ -184,7 +184,7 @@ public abstract class AbstractPopupActivity extends AbstractActivity implements
public void onResume() {
super.onResume();
init();
- geoUpdate.startGeo();
+ geoUpdate.start();
}
@Override
@@ -256,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 767b8a4..5d529ee 100644
--- a/main/src/cgeo/geocaching/CacheDetailActivity.java
+++ b/main/src/cgeo/geocaching/CacheDetailActivity.java
@@ -144,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;
}
@@ -318,7 +318,7 @@ public class CacheDetailActivity extends AbstractViewPagerActivity<CacheDetailAc
notifyDataSetChanged();
refreshOnResume = false;
}
- locationUpdater.startGeo();
+ locationUpdater.start();
}
@Override
@@ -339,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 55f16c8..cde7a79 100644
--- a/main/src/cgeo/geocaching/CacheListActivity.java
+++ b/main/src/cgeo/geocaching/CacheListActivity.java
@@ -123,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);
+ }
}
}
@@ -466,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);
@@ -500,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 e4c8068..cbe81fa 100644
--- a/main/src/cgeo/geocaching/CgeoApplication.java
+++ b/main/src/cgeo/geocaching/CgeoApplication.java
@@ -6,7 +6,9 @@ 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;
@@ -23,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);
@@ -69,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 4c2a33a..a51e477 100644
--- a/main/src/cgeo/geocaching/CompassActivity.java
+++ b/main/src/cgeo/geocaching/CompassActivity.java
@@ -121,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();
}
@@ -152,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
@@ -200,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);
@@ -276,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) {
@@ -301,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 53dacc4..98884b8 100644
--- a/main/src/cgeo/geocaching/EditWaypointActivity.java
+++ b/main/src/cgeo/geocaching/EditWaypointActivity.java
@@ -204,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();
}
@@ -264,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 88204e5..bfe0217 100644
--- a/main/src/cgeo/geocaching/MainActivity.java
+++ b/main/src/cgeo/geocaching/MainActivity.java
@@ -143,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) {
@@ -215,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();
@@ -257,8 +257,8 @@ public class MainActivity extends AbstractActivity {
@Override
public void onPause() {
initialized = false;
- locationUpdater.stopGeo();
- satellitesHandler.stopGeo();
+ locationUpdater.stop();
+ satellitesHandler.stop();
super.onPause();
}
@@ -511,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 3fa4b65..72cbc64 100644
--- a/main/src/cgeo/geocaching/NavigateAnyPointActivity.java
+++ b/main/src/cgeo/geocaching/NavigateAnyPointActivity.java
@@ -235,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();
}
@@ -455,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/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java
index 81fd693..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.sensors.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.sensors.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/sensors/GeoData.java b/main/src/cgeo/geocaching/sensors/GeoData.java
index d53ac73..c0b3974 100644
--- a/main/src/cgeo/geocaching/sensors/GeoData.java
+++ b/main/src/cgeo/geocaching/sensors/GeoData.java
@@ -7,10 +7,10 @@ import android.location.Location;
import android.location.LocationManager;
class GeoData extends Location implements IGeoData {
- public final boolean gpsEnabled;
- public final int satellitesVisible;
- public final int satellitesFixed;
- public final boolean pseudoLocation;
+ 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);
diff --git a/main/src/cgeo/geocaching/sensors/GeoDataProvider.java b/main/src/cgeo/geocaching/sensors/GeoDataProvider.java
index 004a687..05d9467 100644
--- a/main/src/cgeo/geocaching/sensors/GeoDataProvider.java
+++ b/main/src/cgeo/geocaching/sensors/GeoDataProvider.java
@@ -5,11 +5,15 @@ 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 android.content.Context;
@@ -20,6 +24,8 @@ import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
+import java.util.concurrent.TimeUnit;
+
public class GeoDataProvider implements OnSubscribe<IGeoData> {
private static final String LAST_LOCATION_PSEUDO_PROVIDER = "last";
@@ -76,28 +82,40 @@ public 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;
}
};
diff --git a/main/src/cgeo/geocaching/sensors/GeoDirHandler.java b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
index 146ff76..523fb1f 100644
--- a/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
+++ b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
@@ -3,126 +3,59 @@ package cgeo.geocaching.sensors;
import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.settings.Settings;
-import rx.Scheduler;
+import org.apache.commons.lang3.tuple.ImmutablePair;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
-import rx.schedulers.Schedulers;
-
-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>
+ * 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 dirSubscription = null;
- private Subscription geoSubscription = null;
+ private Subscription subscription = null;
/**
- * Update method called when new IGeoData is available.
+ * Update method called when new data 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 geoData the new geographical data
+ * @param direction the new direction
*
- * @param direction
- * the new direction
- */
- public void updateDirection(final float direction) {
- // Override this in children
- }
-
- /**
- * Register the current GeoDirHandler for GeoData information.
+ * 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 synchronized void startGeo() {
- geoSubscription = app.currentGeoObject()
- .subscribeOn(AndroidSchedulers.mainThread())
- .subscribe(new Action1<IGeoData>() {
- @Override
- public void call(final IGeoData geoData) {
- updateGeoData(geoData);
- }
- });
+ public void updateGeoDir(@SuppressWarnings("unused") final IGeoData geoData, @SuppressWarnings("unused") final float direction) {
}
- /**
- * 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);
- }
- });
- }
+ 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 startGeoAndDir() {
- startGeo();
- startDir();
+ 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 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);
- }
+ public void stop() {
+ subscription.unsubscribe();
}
- /**
- * 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();
- }
}
diff --git a/main/src/cgeo/geocaching/speech/SpeechService.java b/main/src/cgeo/geocaching/speech/SpeechService.java
index eadbac8..b13218c 100644
--- a/main/src/cgeo/geocaching/speech/SpeechService.java
+++ b/main/src/cgeo/geocaching/speech/SpeechService.java
@@ -1,7 +1,5 @@
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;
@@ -43,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(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();
}
};
/**
@@ -79,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>
*
@@ -132,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();
@@ -158,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;
@@ -166,11 +141,7 @@ public class SpeechService extends Service implements OnInitListener {
initialized = true;
- if (Settings.isUseCompass()) {
- geoHandler.startGeoAndDir();
- } else {
- geoHandler.startGeo();
- }
+ geoDirHandler.start();
}
@Override