diff options
Diffstat (limited to 'main/src/cgeo/geocaching/sensors/DirectionProvider.java')
| -rw-r--r-- | main/src/cgeo/geocaching/sensors/DirectionProvider.java | 38 |
1 files changed, 34 insertions, 4 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 = |
