aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/sensors
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/sensors')
-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
3 files changed, 65 insertions, 114 deletions
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();
- }
}