aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2012-05-31 15:04:02 +0200
committerSamuel Tardieu <sam@rfc1149.net>2012-05-31 15:04:02 +0200
commit0f85374688b34481fa869c679c01afdab90aeac4 (patch)
tree43283b9017389adc2a36b00370895798d3c928fd
parent11dfc41193c31341831d0712692f3f2bf7dda860 (diff)
downloadcgeo-0f85374688b34481fa869c679c01afdab90aeac4.zip
cgeo-0f85374688b34481fa869c679c01afdab90aeac4.tar.gz
cgeo-0f85374688b34481fa869c679c01afdab90aeac4.tar.bz2
Do not repeat similar orientation to observers
Android uses 1° precision in its orientation computation, so it is not uncommon to be signalled the same orientation several times. There is no need for observers to be woken up, as the value has not changed.
-rw-r--r--main/src/cgeo/geocaching/DirectionProvider.java12
1 files changed, 11 insertions, 1 deletions
diff --git a/main/src/cgeo/geocaching/DirectionProvider.java b/main/src/cgeo/geocaching/DirectionProvider.java
index 2f54b41..d8ee782 100644
--- a/main/src/cgeo/geocaching/DirectionProvider.java
+++ b/main/src/cgeo/geocaching/DirectionProvider.java
@@ -14,6 +14,12 @@ public class DirectionProvider extends MemorySubject<Float> implements SensorEve
private final SensorManager sensorManager;
+ // Previous values signalled to observers to avoid resending the same value when the
+ // device doesn't change orientation. The orientation is usually given with a 1 degree
+ // precision by Android, so it is not uncommon to obtain exactly the same value several
+ // times.
+ private float previous = -1;
+
public DirectionProvider(final Context context) {
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
@@ -46,7 +52,11 @@ public class DirectionProvider extends MemorySubject<Float> implements SensorEve
@Override
public void onSensorChanged(final SensorEvent event) {
- notifyObservers(event.values[0]);
+ final float direction = event.values[0];
+ if (direction != previous) {
+ notifyObservers(direction);
+ previous = direction;
+ }
}
/**