diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2013-05-10 17:16:07 +0200 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2013-05-10 19:38:50 +0200 |
| commit | 4a24570ba09de30525d4d1ee6e1814ec90927237 (patch) | |
| tree | 8905dfcc30ce147a97f61396ce52bf2b6ba9557e /main/src/cgeo/geocaching/speech/TextFactory.java | |
| parent | 780b883f8b98c7f2074bccb5f6fd931c74758a46 (diff) | |
| download | cgeo-4a24570ba09de30525d4d1ee6e1814ec90927237.zip cgeo-4a24570ba09de30525d4d1ee6e1814ec90927237.tar.gz cgeo-4a24570ba09de30525d4d1ee6e1814ec90927237.tar.bz2 | |
#2710: speech output in compass
Diffstat (limited to 'main/src/cgeo/geocaching/speech/TextFactory.java')
| -rw-r--r-- | main/src/cgeo/geocaching/speech/TextFactory.java | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/speech/TextFactory.java b/main/src/cgeo/geocaching/speech/TextFactory.java new file mode 100644 index 0000000..6b25147 --- /dev/null +++ b/main/src/cgeo/geocaching/speech/TextFactory.java @@ -0,0 +1,52 @@ +package cgeo.geocaching.speech; + +import cgeo.geocaching.R; +import cgeo.geocaching.cgeoapplication; +import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.utils.AngleUtils; + +import java.util.Locale; + +/** + * Creates the output to be read by TTS. + * + */ +public class TextFactory { + public static String getText(Geopoint position, Geopoint target, float direction) { + if (position == null || target == null) { + return null; + } + return getDirection(position, target, direction) + ". " + getDistance(position, target); + } + + private static String getDistance(Geopoint position, Geopoint target) { + float kilometers = position.distanceTo(target); + if (kilometers >= 5.0) { + return getString(R.string.tts_kilometers, String.valueOf(Math.round(kilometers))); + } + if (kilometers >= 1.0) { + String digits = String.format(Locale.getDefault(), "%.1f", kilometers); + return getString(R.string.tts_kilometers, digits); + } + int meters = (int) (kilometers * 1000.0); + if (meters > 50) { + return getString(R.string.tts_meters, String.valueOf(Math.round(meters / 10.0) * 10)); + } + return getString(R.string.tts_meters, String.valueOf(meters)); + } + + private static String getString(int resourceId, Object... formatArgs) { + return cgeoapplication.getInstance().getString(resourceId, formatArgs); + } + + private static String getDirection(Geopoint position, Geopoint target, float direction) { + final int bearing = (int) position.bearingTo(target); + int degrees = (int) AngleUtils.normalize(bearing - direction); + + int hours = (degrees + 15) / 30; + if (hours == 0) { + hours = 12; + } + return getString(R.string.tts_oclock, String.valueOf(hours)); + } +} |
