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/DirectionProvider.java38
-rw-r--r--main/src/cgeo/geocaching/sensors/GeoDirHandler.java7
2 files changed, 38 insertions, 7 deletions
diff --git a/main/src/cgeo/geocaching/sensors/DirectionProvider.java b/main/src/cgeo/geocaching/sensors/DirectionProvider.java
index 788d5bd..ff4a439 100644
--- a/main/src/cgeo/geocaching/sensors/DirectionProvider.java
+++ b/main/src/cgeo/geocaching/sensors/DirectionProvider.java
@@ -30,6 +30,7 @@ public class DirectionProvider {
private int count = 0;
private SensorManager sensorManager;
+
@Override
public void onSensorChanged(final SensorEvent event) {
subject.onNext(event.values[0]);
@@ -50,23 +51,52 @@ public class DirectionProvider {
//Log.i(Settings.tag, "Compass' accuracy is low (" + accuracy + ")");
}
- // This will be removed when using a new location service. Until then, it is okay to be used.
- @SuppressWarnings("deprecation")
@Override
public void start(final Context context, final Handler handler) {
+ if (!hasSensor(context)) {
+ return;
+ }
if (++count == 1) {
- sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
- sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL, handler);
+ Sensor orientationSensor = getOrientationSensor(context);
+ sensorManager.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL, handler);
}
}
@Override
public void stop() {
+ if (!hasSensor) {
+ return;
+ }
if (--count == 0) {
sensorManager.unregisterListener(this);
}
}
+ /**
+ * Assume that there is an orientation sensor, unless we have really checked that
+ */
+ private boolean hasSensor = true;
+
+ /**
+ * Flag for one time check if there is a sensor.
+ */
+ private boolean hasSensorChecked = false;
+
+ public boolean hasSensor(Context context) {
+ if (hasSensorChecked == false) {
+ hasSensor = getOrientationSensor(context) != null;
+ hasSensorChecked = true;
+ }
+ return hasSensor;
+ }
+
+ // This will be removed when using a new location service. Until then, it is okay to be used.
+ @SuppressWarnings("deprecation")
+ private Sensor getOrientationSensor(final Context context) {
+ sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
+ return sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
+ }
+
}
private static final StartableHandlerThread handlerThread =
diff --git a/main/src/cgeo/geocaching/sensors/GeoDirHandler.java b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
index c10cb48..0f30142 100644
--- a/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
+++ b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
@@ -4,6 +4,7 @@ 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;
@@ -37,7 +38,7 @@ public abstract class GeoDirHandler {
*
* @param geoData the new geographical data
*/
- public void updateGeoData(@SuppressWarnings("unused") final IGeoData geoData) {
+ public void updateGeoData(final IGeoData geoData) {
}
/**
@@ -46,7 +47,7 @@ public abstract class GeoDirHandler {
*
* @param direction the new direction
*/
- public void updateDirection(@SuppressWarnings("unused") final float direction) {
+ public void updateDirection(final float direction) {
}
/**
@@ -59,7 +60,7 @@ public abstract class GeoDirHandler {
* 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) {
+ public void updateGeoDir(final IGeoData geoData, final float direction) {
}
private static Observable<Float> fixedDirection() {