diff options
Diffstat (limited to 'main/src/cgeo/geocaching')
| -rw-r--r-- | main/src/cgeo/geocaching/sensors/DirectionProvider.java | 27 |
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 |
