diff options
6 files changed, 21 insertions, 11 deletions
diff --git a/main/src/cgeo/geocaching/CacheListActivity.java b/main/src/cgeo/geocaching/CacheListActivity.java index 1ae6378..f892bcb 100644 --- a/main/src/cgeo/geocaching/CacheListActivity.java +++ b/main/src/cgeo/geocaching/CacheListActivity.java @@ -126,7 +126,7 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA @Override public void updateDirection(final float direction) { if (Settings.isLiveList()) { - adapter.setActualHeading(DirectionProvider.getDirectionNow(CacheListActivity.this, direction)); + adapter.setActualHeading(DirectionProvider.getDirectionNow(direction)); } } diff --git a/main/src/cgeo/geocaching/CompassActivity.java b/main/src/cgeo/geocaching/CompassActivity.java index b4bff88..ff7a025 100644 --- a/main/src/cgeo/geocaching/CompassActivity.java +++ b/main/src/cgeo/geocaching/CompassActivity.java @@ -287,7 +287,7 @@ public class CompassActivity extends AbstractActivity { navLocation.setText(res.getString(R.string.loc_trying)); } - updateNorthHeading(DirectionProvider.getDirectionNow(CompassActivity.this, dir)); + updateNorthHeading(DirectionProvider.getDirectionNow(dir)); } catch (RuntimeException e) { Log.w("Failed to LocationUpdater location."); } diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index cab961a..f06a42f 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -895,7 +895,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto locationValid = true; currentLocation = geo.getLocation(); - currentHeading = DirectionProvider.getDirectionNow(activity, dir); + currentHeading = DirectionProvider.getDirectionNow(dir); repaintPositionOverlay(); } } diff --git a/main/src/cgeo/geocaching/sensors/DirectionProvider.java b/main/src/cgeo/geocaching/sensors/DirectionProvider.java index 44732f7..788d5bd 100644 --- a/main/src/cgeo/geocaching/sensors/DirectionProvider.java +++ b/main/src/cgeo/geocaching/sensors/DirectionProvider.java @@ -1,5 +1,6 @@ package cgeo.geocaching.sensors; +import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.utils.AngleUtils; import cgeo.geocaching.utils.StartableHandlerThread; @@ -8,7 +9,6 @@ import rx.Observable.OnSubscribe; import rx.Subscriber; import rx.subjects.BehaviorSubject; -import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; @@ -17,11 +17,14 @@ import android.hardware.SensorManager; import android.os.Handler; import android.os.Process; import android.view.Surface; +import android.view.WindowManager; public class DirectionProvider { private static final BehaviorSubject<Float> subject = BehaviorSubject.create(0.0f); + private static final WindowManager windowManager = (WindowManager) CgeoApplication.getInstance().getSystemService(Context.WINDOW_SERVICE); + static class Listener implements SensorEventListener, StartableHandlerThread.Callback { private int count = 0; @@ -85,17 +88,20 @@ public class DirectionProvider { /** * Take the phone rotation (through a given activity) in account and adjust the direction. * - * @param activity the activity to consider when computing the rotation * @param direction the unadjusted direction in degrees, in the [0, 360[ range * @return the adjusted direction in degrees, in the [0, 360[ range */ - public static float getDirectionNow(final Activity activity, final float direction) { - return AngleUtils.normalize(direction + getRotationOffset(activity)); + public static float getDirectionNow(final float direction) { + return AngleUtils.normalize(direction + getRotationOffset()); + } + + static float reverseDirectionNow(final float direction) { + return AngleUtils.normalize(direction - getRotationOffset()); } - private static int getRotationOffset(final Activity activity) { - switch (activity.getWindowManager().getDefaultDisplay().getRotation()) { + private static int getRotationOffset() { + switch (windowManager.getDefaultDisplay().getRotation()) { case Surface.ROTATION_90: return 90; case Surface.ROTATION_180: diff --git a/main/src/cgeo/geocaching/sensors/GeoDirHandler.java b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java index 09c0794..c10cb48 100644 --- a/main/src/cgeo/geocaching/sensors/GeoDirHandler.java +++ b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java @@ -18,6 +18,10 @@ import rx.subscriptions.CompositeSubscription; * To use this class, override {@link #updateGeoDir(IGeoData, float)}. You need to start the handler using * {@link #start(int)}. A good place to do so might be the {@code onResume} method of the Activity. Stop the Handler * accordingly in {@code onPause}. + * + * The direction is always relative to the top of the device (natural direction), and that it must + * be fixed using {@link DirectionProvider#getDirectionNow(float)}. When the direction is derived from the GPS, + * it is altered so that the fix can still be applied as if the information came from the compass. */ public abstract class GeoDirHandler { @@ -71,7 +75,7 @@ public abstract class GeoDirHandler { private static float fixDirection(final IGeoData geoData, final float direction) { final boolean useGPSBearing = !Settings.isUseCompass() || geoData.getSpeed() > 5; - return useGPSBearing ? geoData.getBearing() : direction; + return useGPSBearing ? DirectionProvider.reverseDirectionNow(geoData.getBearing()) : direction; } /** diff --git a/tests/src/cgeo/geocaching/sensors/SensorsTest.java b/tests/src/cgeo/geocaching/sensors/SensorsTest.java index 7bf723a..87ff1de 100644 --- a/tests/src/cgeo/geocaching/sensors/SensorsTest.java +++ b/tests/src/cgeo/geocaching/sensors/SensorsTest.java @@ -21,7 +21,7 @@ public class SensorsTest extends ActivityInstrumentationTestCase2<MainActivity> } public void testGetDirectionNow() { - final float angle = DirectionProvider.getDirectionNow(activity, 1.0f); + final float angle = DirectionProvider.getDirectionNow(1.0f); Assert.assertTrue(angle == 1.0f || angle == 91.0f || angle == 181.0f || angle == 271.0f); } |
