aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/sensors
diff options
context:
space:
mode:
authorMichael Keppler <michael.keppler@gmx.de>2014-04-22 09:50:52 +0200
committerMichael Keppler <michael.keppler@gmx.de>2014-04-22 09:50:52 +0200
commita1eb213952f27a4eea6aee25f6db57e0b2440ff8 (patch)
tree154b98b70871a167c1cc68ce5f2137300545cad7 /main/src/cgeo/geocaching/sensors
parent2bb1067d303b9723428089fa035fb5f8d54d1b61 (diff)
downloadcgeo-a1eb213952f27a4eea6aee25f6db57e0b2440ff8.zip
cgeo-a1eb213952f27a4eea6aee25f6db57e0b2440ff8.tar.gz
cgeo-a1eb213952f27a4eea6aee25f6db57e0b2440ff8.tar.bz2
handle missing orientation sensor
If a device has no orientation sensor (like Genymotion emulator), then a SensorManager log entry is created each time we try to register a listener. Avoid the registration if there is no sensor.
Diffstat (limited to 'main/src/cgeo/geocaching/sensors')
-rw-r--r--main/src/cgeo/geocaching/sensors/DirectionProvider.java27
1 files changed, 26 insertions, 1 deletions
diff --git a/main/src/cgeo/geocaching/sensors/DirectionProvider.java b/main/src/cgeo/geocaching/sensors/DirectionProvider.java
index 788d5bd..8e7507d 100644
--- a/main/src/cgeo/geocaching/sensors/DirectionProvider.java
+++ b/main/src/cgeo/geocaching/sensors/DirectionProvider.java
@@ -54,14 +54,21 @@ public class DirectionProvider {
@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 = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
+ sensorManager.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL, handler);
}
}
@Override
public void stop() {
+ if (!hasSensor) {
+ return;
+ }
if (--count == 0) {
sensorManager.unregisterListener(this);
}
@@ -86,6 +93,24 @@ public class DirectionProvider {
}
/**
+ * Assume that there is an orientation sensor, unless we have really checked that
+ */
+ private static boolean hasSensor = true;
+
+ /**
+ * Flag for one time check if there is a sensor.
+ */
+ private static boolean hasSensorChecked = false;
+
+ public static boolean hasSensor(Context context) {
+ if (hasSensorChecked == false) {
+ SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
+ hasSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION) != null;
+ }
+ return hasSensor;
+ }
+
+ /**
* Take the phone rotation (through a given activity) in account and adjust the direction.
*
* @param direction the unadjusted direction in degrees, in the [0, 360[ range