aboutsummaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2014-03-05 13:30:47 +0100
committerSamuel Tardieu <sam@rfc1149.net>2014-03-05 13:30:47 +0100
commit06dd704130431c58fb0548b150dac4fb56c6658f (patch)
tree6dfb50d734639ecacf50d004454d15e11501ae1e /main/src
parentc2122b4c84492be27145070a33dd9f8708b2b715 (diff)
downloadcgeo-06dd704130431c58fb0548b150dac4fb56c6658f.zip
cgeo-06dd704130431c58fb0548b150dac4fb56c6658f.tar.gz
cgeo-06dd704130431c58fb0548b150dac4fb56c6658f.tar.bz2
refactoring: remove the need for PeriodicHandler
Diffstat (limited to 'main/src')
-rw-r--r--main/src/cgeo/geocaching/ui/CompassView.java44
-rw-r--r--main/src/cgeo/geocaching/utils/PeriodicHandler.java88
2 files changed, 24 insertions, 108 deletions
diff --git a/main/src/cgeo/geocaching/ui/CompassView.java b/main/src/cgeo/geocaching/ui/CompassView.java
index 5e80f32..e266559 100644
--- a/main/src/cgeo/geocaching/ui/CompassView.java
+++ b/main/src/cgeo/geocaching/ui/CompassView.java
@@ -2,8 +2,6 @@ package cgeo.geocaching.ui;
import cgeo.geocaching.R;
import cgeo.geocaching.utils.AngleUtils;
-import cgeo.geocaching.utils.PeriodicHandler;
-import cgeo.geocaching.utils.PeriodicHandler.PeriodicHandlerListener;
import android.content.Context;
import android.graphics.Bitmap;
@@ -14,8 +12,14 @@ import android.graphics.PaintFlagsDrawFilter;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.view.View;
+import rx.Scheduler;
+import rx.Subscription;
+import rx.functions.Action1;
+import rx.schedulers.Schedulers;
-public class CompassView extends View implements PeriodicHandlerListener {
+import java.util.concurrent.TimeUnit;
+
+public class CompassView extends View {
private Context context = null;
private Bitmap compassUnderlay = null;
@@ -49,7 +53,7 @@ public class CompassView extends View implements PeriodicHandlerListener {
private int compassOverlayWidth = 0;
private int compassOverlayHeight = 0;
private boolean initialDisplay;
- private final PeriodicHandler redrawHandler = new PeriodicHandler(40, this);
+ private Subscription periodicUpdate;
public CompassView(Context contextIn) {
super(contextIn);
@@ -83,12 +87,26 @@ public class CompassView extends View implements PeriodicHandlerListener {
synchronized (this) {
initialDisplay = true;
}
- redrawHandler.start();
+ periodicUpdate = Schedulers.io().schedulePeriodically(new Action1<Scheduler.Inner>() {
+ @Override
+ public void call(final Scheduler.Inner inner) {
+ synchronized (CompassView.this) {
+ final float newAzimuthShown = smoothUpdate(northMeasured, azimuthShown);
+ final float newCacheHeadingShown = smoothUpdate(cacheHeadingMeasured, cacheHeadingShown);
+ if (Math.abs(AngleUtils.difference(azimuthShown, newAzimuthShown)) >= 2 ||
+ Math.abs(AngleUtils.difference(cacheHeadingShown, newCacheHeadingShown)) >= 2) {
+ azimuthShown = newAzimuthShown;
+ cacheHeadingShown = newCacheHeadingShown;
+ postInvalidate();
+ }
+ }
+ }
+ }, 0, 40, TimeUnit.MILLISECONDS);
}
@Override
public void onDetachedFromWindow() {
- redrawHandler.stop();
+ periodicUpdate.unsubscribe();
super.onDetachedFromWindow();
if (compassUnderlay != null) {
@@ -150,20 +168,6 @@ public class CompassView extends View implements PeriodicHandlerListener {
}
@Override
- public synchronized void onPeriodic() {
- final float newAzimuthShown = smoothUpdate(northMeasured, azimuthShown);
- final float newCacheHeadingShown = smoothUpdate(cacheHeadingMeasured, cacheHeadingShown);
- if (Math.abs(AngleUtils.difference(azimuthShown, newAzimuthShown)) >= 2 ||
- Math.abs(AngleUtils.difference(cacheHeadingShown, newCacheHeadingShown)) >= 2) {
- synchronized(this) {
- azimuthShown = newAzimuthShown;
- cacheHeadingShown = newCacheHeadingShown;
- }
- invalidate();
- }
- }
-
- @Override
protected void onDraw(Canvas canvas) {
// use local synchronized variables to avoid them being changed from the device during drawing
float azimuthDrawn;
diff --git a/main/src/cgeo/geocaching/utils/PeriodicHandler.java b/main/src/cgeo/geocaching/utils/PeriodicHandler.java
deleted file mode 100644
index 288bbb0..0000000
--- a/main/src/cgeo/geocaching/utils/PeriodicHandler.java
+++ /dev/null
@@ -1,88 +0,0 @@
-package cgeo.geocaching.utils;
-
-import android.os.Handler;
-import android.os.Message;
-
-import java.lang.ref.WeakReference;
-
-/**
- * A PeriodicHandler class helps with the implementation of a periodic
- * action embedded in a thread with a looper such as the UI thread.
- * The onPeriodic() method of the listener will be called periodically.
- * The clock may drift as the implementation does not target real-time
- * actions.
- *
- * The handler will be interrupted if the device goes to sleep.
- *
- * The handler only keeps a weak reference to the listener. If the listener
- * is garbage-collected without having stopped the timer, the handler will
- * stop itself.
- */
-final public class PeriodicHandler extends Handler {
-
- public static interface PeriodicHandlerListener {
- public void onPeriodic();
- }
-
- final static private int START = 0;
- final static private int STOP = 1;
- final static private int ACT = 2;
-
- final private long period;
-
- final private WeakReference<PeriodicHandlerListener> listenerRef;
-
- /**
- * Create a new PeriodicHandler object.
- *
- * @param period
- * The period in milliseconds.
- */
- public PeriodicHandler(final long period, final PeriodicHandlerListener listener) {
- this.period = period;
- listenerRef = new WeakReference<PeriodicHandlerListener>(listener);
- }
-
- @Override
- public void handleMessage(final Message msg) {
- switch (msg.what) {
- case START:
- removeMessages(ACT);
- sendEmptyMessage(ACT);
- break;
- case STOP:
- removeMessages(ACT);
- break;
- case ACT:
- final PeriodicHandlerListener listener = listenerRef.get();
- if (listener != null) {
- sendEmptyMessageDelayed(ACT, period);
- listener.onPeriodic();
- }
- break;
- default:
- throw new UnsupportedOperationException();
- }
- }
-
- /**
- * Start the periodic handler.
- *
- * Restarting a handler that is already started will only
- * reset its clock.
- */
- public void start() {
- sendEmptyMessage(START);
- }
-
- /**
- * Stop the periodic handler.
- *
- * If this method is called from the looper thread, this call is
- * guaranteed to be synchronous.
- */
- public void stop() {
- sendMessageAtFrontOfQueue(obtainMessage(STOP));
- }
-
-}