aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main/src/cgeo/geocaching/cgeonavigate.java34
-rw-r--r--main/src/cgeo/geocaching/ui/CompassView.java51
2 files changed, 26 insertions, 59 deletions
diff --git a/main/src/cgeo/geocaching/cgeonavigate.java b/main/src/cgeo/geocaching/cgeonavigate.java
index 848c2ed..567ae74 100644
--- a/main/src/cgeo/geocaching/cgeonavigate.java
+++ b/main/src/cgeo/geocaching/cgeonavigate.java
@@ -8,7 +8,6 @@ import cgeo.geocaching.maps.CGeoMap;
import cgeo.geocaching.ui.CompassView;
import cgeo.geocaching.utils.GeoDirHandler;
import cgeo.geocaching.utils.Log;
-import cgeo.geocaching.utils.PeriodicHandler;
import org.apache.commons.lang3.StringUtils;
@@ -36,7 +35,6 @@ public class cgeonavigate extends AbstractActivity {
private PowerManager pm = null;
private Geopoint dstCoords = null;
private float cacheHeading = 0;
- private Float northHeading = null;
private String title = null;
private String name = null;
private TextView navType = null;
@@ -46,16 +44,6 @@ public class cgeonavigate extends AbstractActivity {
private TextView distanceView = null;
private TextView headingView = null;
private CompassView compassView = null;
- private PeriodicHandler updaterHandler = new PeriodicHandler(20) {
-
- @Override
- public void act() {
- if (compassView != null && northHeading != null) {
- compassView.updateNorth(northHeading, cacheHeading);
- }
- }
-
- };
private String geocode;
public cgeonavigate() {
@@ -104,12 +92,6 @@ public class cgeonavigate extends AbstractActivity {
}
@Override
- public void onStart() {
- super.onStart();
- updaterHandler.start();
- }
-
- @Override
public void onResume() {
super.onResume();
@@ -133,12 +115,6 @@ public class cgeonavigate extends AbstractActivity {
}
@Override
- public void onStop() {
- updaterHandler.stop();
- super.onStop();
- }
-
- @Override
public void onPause() {
geoDirHandler.stopGeoAndDir();
super.onPause();
@@ -293,7 +269,7 @@ public class cgeonavigate extends AbstractActivity {
}
if (!Settings.isUseCompass() || geo.getSpeed() > 5) { // use GPS when speed is higher than 18 km/h
- northHeading = geo.getBearing();
+ updateNorthHeading(geo.getBearing());
}
} catch (Exception e) {
Log.w("Failed to LocationUpdater location.");
@@ -303,11 +279,17 @@ public class cgeonavigate extends AbstractActivity {
@Override
public void updateDirection(final float direction) {
if (app.currentGeo().getSpeed() <= 5) { // use compass when speed is lower than 18 km/h
- northHeading = DirectionProvider.getDirectionNow(cgeonavigate.this, direction);
+ updateNorthHeading(DirectionProvider.getDirectionNow(cgeonavigate.this, direction));
}
}
};
+ private void updateNorthHeading(final float northHeading) {
+ if (compassView != null) {
+ compassView.updateNorth(northHeading, cacheHeading);
+ }
+ }
+
public static void startActivity(final Context context, final String geocode, final String displayedName, final Geopoint coords, final Collection<IWaypoint> coordinatesWithType) {
coordinates.clear();
if (coordinatesWithType != null) { // avoid possible NPE
diff --git a/main/src/cgeo/geocaching/ui/CompassView.java b/main/src/cgeo/geocaching/ui/CompassView.java
index 8a295c3..d0b4a36 100644
--- a/main/src/cgeo/geocaching/ui/CompassView.java
+++ b/main/src/cgeo/geocaching/ui/CompassView.java
@@ -1,7 +1,7 @@
package cgeo.geocaching.ui;
import cgeo.geocaching.R;
-import cgeo.geocaching.utils.Log;
+import cgeo.geocaching.utils.PeriodicHandler;
import android.content.Context;
import android.graphics.Bitmap;
@@ -9,14 +9,11 @@ import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
-import android.os.Handler;
-import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
public class CompassView extends View {
- private volatile boolean wantStop = false;
private Context context = null;
private Bitmap compassUnderlay = null;
private Bitmap compassRose = null;
@@ -49,17 +46,7 @@ public class CompassView extends View {
private int compassOverlayWidth = 0;
private int compassOverlayHeight = 0;
private boolean initialDisplay;
- private Handler changeHandler = new Handler() {
-
- @Override
- public void handleMessage(Message message) {
- try {
- invalidate();
- } catch (Exception e) {
- Log.e("CompassView.changeHandler: " + e.toString());
- }
- }
- };
+ private final RedrawHandler redrawHandler = new RedrawHandler();
public CompassView(Context contextIn) {
super(contextIn);
@@ -91,14 +78,12 @@ public class CompassView extends View {
remfil = new PaintFlagsDrawFilter(Paint.FILTER_BITMAP_FLAG, 0);
initialDisplay = true;
- wantStop = false;
-
- new changeThread().start();
+ redrawHandler.start();
}
@Override
public void onDetachedFromWindow() {
- wantStop = true;
+ redrawHandler.stop();
if (compassUnderlay != null) {
compassUnderlay.recycle();
@@ -165,25 +150,25 @@ public class CompassView extends View {
return actual + offset;
}
- private class changeThread extends Thread {
+ private class RedrawHandler extends PeriodicHandler {
- @Override
- public void run() {
- while (!wantStop) {
- try {
- sleep(50);
- } catch (Exception e) {
- // nothing
- }
+ public RedrawHandler() {
+ super(40);
+ }
- synchronized (CompassView.this) {
- azimuthShown = smoothUpdate(northMeasured, azimuthShown);
- cacheHeadingShown = smoothUpdate(cacheHeadingMeasured, cacheHeadingShown);
+ @Override
+ public void act() {
+ final double newAzimuthShown = smoothUpdate(northMeasured, azimuthShown);
+ final double newCacheHeadingShown = smoothUpdate(cacheHeadingMeasured, cacheHeadingShown);
+ if (Math.abs(newAzimuthShown - azimuthShown) >= 2 || Math.abs(newCacheHeadingShown - cacheHeadingShown) >= 2) {
+ synchronized(CompassView.this) {
+ azimuthShown = newAzimuthShown;
+ cacheHeadingShown = newCacheHeadingShown;
}
-
- changeHandler.sendMessage(Message.obtain());
+ invalidate();
}
}
+
}
@Override