diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2012-05-31 13:33:34 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2012-05-31 13:33:34 +0200 |
| commit | 0034e358668d5911639628468be7409b2696d6c5 (patch) | |
| tree | 3f0201411526129b7417aa4be7285da00f3fc005 | |
| parent | 452a8d10efabb873f780788350aba639d2abfdc5 (diff) | |
| download | cgeo-0034e358668d5911639628468be7409b2696d6c5.zip cgeo-0034e358668d5911639628468be7409b2696d6c5.tar.gz cgeo-0034e358668d5911639628468be7409b2696d6c5.tar.bz2 | |
Fix #1685: make sure the compass range fits within bounds
| -rw-r--r-- | main/src/cgeo/geocaching/DirectionProvider.java | 9 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/compatibility/Compatibility.java | 15 |
2 files changed, 19 insertions, 5 deletions
diff --git a/main/src/cgeo/geocaching/DirectionProvider.java b/main/src/cgeo/geocaching/DirectionProvider.java index 6802b2a..7bb940f 100644 --- a/main/src/cgeo/geocaching/DirectionProvider.java +++ b/main/src/cgeo/geocaching/DirectionProvider.java @@ -49,8 +49,15 @@ public class DirectionProvider extends MemorySubject<Float> implements SensorEve notifyObservers(event.values[0]); } + /** + * 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 Compatibility.getDirectionNow(direction, activity); + return Compatibility.getDirectionNow(direction, activity) % 360; } } diff --git a/main/src/cgeo/geocaching/compatibility/Compatibility.java b/main/src/cgeo/geocaching/compatibility/Compatibility.java index f88ec2d..d869c58 100644 --- a/main/src/cgeo/geocaching/compatibility/Compatibility.java +++ b/main/src/cgeo/geocaching/compatibility/Compatibility.java @@ -38,8 +38,16 @@ public final class Compatibility { } } - public static float getDirectionNow(final float directionNowPre, - final Activity activity) { + /** + * Add 90, 180 or 270 degrees to the given rotation. + * <br/> + * Note: the result is not normalized and may fall outside your desired range. + * + * @param directionNowPre the direction in degrees before adjustment + * @param activity the activity whose rotation is used to adjust the direction + * @return the adjusted direction + */ + public static float getDirectionNow(final float directionNowPre, final Activity activity) { if (isLevel8) { try { final int rotation = level8.getRotation(activity); @@ -55,8 +63,7 @@ public final class Compatibility { Log.e("Cannot call getRotation()", e); } } else { - final Display display = activity.getWindowManager() - .getDefaultDisplay(); + final Display display = activity.getWindowManager().getDefaultDisplay(); final int rotation = display.getOrientation(); if (rotation == Configuration.ORIENTATION_LANDSCAPE) { return directionNowPre + 90; |
