From 9f6d412c982e3dc6c38f851aeb38866f48a9b203 Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Mon, 13 Jan 2014 13:13:25 -0800 Subject: [PATCH] Fix bug with slow speed preference and unit conversion --- .../google/android/apps/mytracks/MapOverlay.java | 10 ++++------ .../maps/DynamicSpeedTrackPathDescriptor.java | 12 ++++++------ .../maps/FixedSpeedTrackPathDescriptor.java | 15 +++++++++++---- .../apps/mytracks/maps/MultiColorTrackPath.java | 2 +- .../apps/mytracks/maps/TrackPathDescriptor.java | 4 ++-- .../mytracks/settings/MapSettingsActivity.java | 12 ++---------- .../maps/FixedSpeedTrackPathDescriptorTest.java | 13 +++++++------ 7 files changed, 33 insertions(+), 35 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/MapOverlay.java b/MyTracks/src/com/google/android/apps/mytracks/MapOverlay.java index 5fbb31eb7..3c5093166 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/MapOverlay.java +++ b/MyTracks/src/com/google/android/apps/mytracks/MapOverlay.java @@ -92,7 +92,7 @@ public class MapOverlay { private final boolean valid; private final LatLng latLng; - private final int speed; + private final double speed; /** * Constructor for an invalid cached location. @@ -100,7 +100,7 @@ public class MapOverlay { public CachedLocation() { this.valid = false; this.latLng = null; - this.speed = -1; + this.speed = -1.0; } /** @@ -109,9 +109,7 @@ public class MapOverlay { public CachedLocation(Location location) { this.valid = LocationUtils.isValidLocation(location); this.latLng = valid ? new LatLng(location.getLatitude(), location.getLongitude()) : null; - this.speed = location.hasSpeed() ? (int) Math.floor( - location.getSpeed() * UnitConversions.MS_TO_KMH) - : -1; + this.speed = location.hasSpeed() ? location.getSpeed() * UnitConversions.MS_TO_KMH : -1.0; } /** @@ -124,7 +122,7 @@ public class MapOverlay { /** * Gets the speed in kilometers per hour. */ - public int getSpeed() { + public double getSpeed() { return speed; } diff --git a/MyTracks/src/com/google/android/apps/mytracks/maps/DynamicSpeedTrackPathDescriptor.java b/MyTracks/src/com/google/android/apps/mytracks/maps/DynamicSpeedTrackPathDescriptor.java index c73855ad6..abc8e2300 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/maps/DynamicSpeedTrackPathDescriptor.java +++ b/MyTracks/src/com/google/android/apps/mytracks/maps/DynamicSpeedTrackPathDescriptor.java @@ -47,8 +47,8 @@ public class DynamicSpeedTrackPathDescriptor implements TrackPathDescriptor { private final Context context; private int speedMargin; - private int slowSpeed; - private int normalSpeed; + private double slowSpeed; + private double normalSpeed; private double averageMovingSpeed; @VisibleForTesting @@ -62,14 +62,14 @@ public class DynamicSpeedTrackPathDescriptor implements TrackPathDescriptor { } @Override - public int getSlowSpeed() { - slowSpeed = (int) (averageMovingSpeed - (averageMovingSpeed * speedMargin / 100.0)); + public double getSlowSpeed() { + slowSpeed = averageMovingSpeed - (averageMovingSpeed * speedMargin / 100.0); return slowSpeed; } @Override - public int getNormalSpeed() { - normalSpeed = (int) (averageMovingSpeed + (averageMovingSpeed * speedMargin / 100.0)); + public double getNormalSpeed() { + normalSpeed = averageMovingSpeed + (averageMovingSpeed * speedMargin / 100.0); return normalSpeed; } diff --git a/MyTracks/src/com/google/android/apps/mytracks/maps/FixedSpeedTrackPathDescriptor.java b/MyTracks/src/com/google/android/apps/mytracks/maps/FixedSpeedTrackPathDescriptor.java index ecfd0bf81..49ca699b1 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/maps/FixedSpeedTrackPathDescriptor.java +++ b/MyTracks/src/com/google/android/apps/mytracks/maps/FixedSpeedTrackPathDescriptor.java @@ -19,6 +19,7 @@ package com.google.android.apps.mytracks.maps; import com.google.android.apps.mytracks.Constants; import com.google.android.apps.mytracks.stats.TripStatistics; import com.google.android.apps.mytracks.util.PreferencesUtils; +import com.google.android.apps.mytracks.util.UnitConversions; import com.google.android.maps.mytracks.R; import android.content.Context; @@ -40,18 +41,24 @@ public class FixedSpeedTrackPathDescriptor implements TrackPathDescriptor { || key.equals(PreferencesUtils.getKey(context, R.string.track_color_mode_slow_key))) { slowSpeed = PreferencesUtils.getInt(context, R.string.track_color_mode_slow_key, PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT); + if (!PreferencesUtils.isMetricUnits(context)) { + slowSpeed = slowSpeed * UnitConversions.MI_TO_KM; + } } if (key == null || key.equals( PreferencesUtils.getKey(context, R.string.track_color_mode_medium_key))) { normalSpeed = PreferencesUtils.getInt(context, R.string.track_color_mode_medium_key, PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT); + if (!PreferencesUtils.isMetricUnits(context)) { + normalSpeed = normalSpeed * UnitConversions.MI_TO_KM; + } } } }; private final Context context; - private int slowSpeed; - private int normalSpeed; + private double slowSpeed; + private double normalSpeed; public FixedSpeedTrackPathDescriptor(Context context) { this.context = context; @@ -62,12 +69,12 @@ public class FixedSpeedTrackPathDescriptor implements TrackPathDescriptor { } @Override - public int getSlowSpeed() { + public double getSlowSpeed() { return slowSpeed; } @Override - public int getNormalSpeed() { + public double getNormalSpeed() { return normalSpeed; } diff --git a/MyTracks/src/com/google/android/apps/mytracks/maps/MultiColorTrackPath.java b/MyTracks/src/com/google/android/apps/mytracks/maps/MultiColorTrackPath.java index 73bf7679b..9367932c2 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/maps/MultiColorTrackPath.java +++ b/MyTracks/src/com/google/android/apps/mytracks/maps/MultiColorTrackPath.java @@ -105,7 +105,7 @@ public class MultiColorTrackPath implements TrackPath { } @VisibleForTesting - protected int getColor(int speed) { + protected int getColor(double speed) { if (speed <= trackPathDescriptor.getSlowSpeed()) { return slowColor; } else if (speed <= trackPathDescriptor.getNormalSpeed()) { diff --git a/MyTracks/src/com/google/android/apps/mytracks/maps/TrackPathDescriptor.java b/MyTracks/src/com/google/android/apps/mytracks/maps/TrackPathDescriptor.java index e3864776a..c54506fb3 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/maps/TrackPathDescriptor.java +++ b/MyTracks/src/com/google/android/apps/mytracks/maps/TrackPathDescriptor.java @@ -28,12 +28,12 @@ public interface TrackPathDescriptor { /** * Gets the maximum speed which is considered slow. */ - public int getSlowSpeed(); + public double getSlowSpeed(); /** * Gets the maximum speed which is considered normal. */ - public int getNormalSpeed(); + public double getNormalSpeed(); /** * Updates state. diff --git a/MyTracks/src/com/google/android/apps/mytracks/settings/MapSettingsActivity.java b/MyTracks/src/com/google/android/apps/mytracks/settings/MapSettingsActivity.java index 77fa046d7..b678b09f2 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/settings/MapSettingsActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/settings/MapSettingsActivity.java @@ -17,7 +17,6 @@ package com.google.android.apps.mytracks.settings; import com.google.android.apps.mytracks.util.PreferencesUtils; -import com.google.android.apps.mytracks.util.UnitConversions; import com.google.android.maps.mytracks.R; import android.app.Dialog; @@ -247,10 +246,7 @@ public class MapSettingsActivity extends AbstractSettingsActivity { int keyId, int minValue, int maxValue, int defaultValue, String displayValue) { int value; try { - value = Integer.parseInt(displayValue); - if (!PreferencesUtils.isMetricUnits(this)) { - value = (int) (value * UnitConversions.MI_TO_KM); - } + value = Integer.parseInt(displayValue); } catch (NumberFormatException e) { Log.e(TAG, "invalid value " + displayValue); value = defaultValue; @@ -273,10 +269,6 @@ public class MapSettingsActivity extends AbstractSettingsActivity { * @param defaultValue the default value */ private int getSpeedDisplayValue(int keyId, int defaultValue) { - int value = PreferencesUtils.getInt(this, keyId, defaultValue); - if (!PreferencesUtils.isMetricUnits(this)) { - value = (int) (value * UnitConversions.KM_TO_MI); - } - return value; + return PreferencesUtils.getInt(this, keyId, defaultValue); } } diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/maps/FixedSpeedTrackPathDescriptorTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/maps/FixedSpeedTrackPathDescriptorTest.java index 44305520b..05de042ec 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/maps/FixedSpeedTrackPathDescriptorTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/maps/FixedSpeedTrackPathDescriptorTest.java @@ -50,8 +50,9 @@ public class FixedSpeedTrackPathDescriptorTest extends AndroidTestCase { context, R.string.track_color_mode_medium_key, normalSpeedExpectations[i]); FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor( context); - assertEquals(slowSpeedExpectations[i], fixedSpeedTrackPathDescriptor.getSlowSpeed()); - assertEquals(normalSpeedExpectations[i], fixedSpeedTrackPathDescriptor.getNormalSpeed()); + assertEquals(slowSpeedExpectations[i], (int) fixedSpeedTrackPathDescriptor.getSlowSpeed()); + assertEquals( + normalSpeedExpectations[i], (int) fixedSpeedTrackPathDescriptor.getNormalSpeed()); } } @@ -62,11 +63,11 @@ public class FixedSpeedTrackPathDescriptorTest extends AndroidTestCase { public void testGetSpeed() { FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor( context); - int slowSpeed = fixedSpeedTrackPathDescriptor.getSlowSpeed(); - int normalSpeed = fixedSpeedTrackPathDescriptor.getNormalSpeed(); + double slowSpeed = fixedSpeedTrackPathDescriptor.getSlowSpeed(); + double normalSpeed = fixedSpeedTrackPathDescriptor.getNormalSpeed(); // Change value in shared preferences - PreferencesUtils.setInt(context, R.string.track_color_mode_slow_key, slowSpeed + 2); - PreferencesUtils.setInt(context, R.string.track_color_mode_medium_key, normalSpeed + 2); + PreferencesUtils.setInt(context, R.string.track_color_mode_slow_key, (int) (slowSpeed + 2)); + PreferencesUtils.setInt(context, R.string.track_color_mode_medium_key, (int) (normalSpeed + 2)); assertEquals(slowSpeed, fixedSpeedTrackPathDescriptor.getSlowSpeed()); assertEquals(normalSpeed, fixedSpeedTrackPathDescriptor.getNormalSpeed()); }