Fix bug with slow speed preference and unit conversion

This commit is contained in:
Jimmy Shih
2014-01-13 13:13:25 -08:00
parent 3a1e1bc7f8
commit 9f6d412c98
7 changed files with 33 additions and 35 deletions
@@ -92,7 +92,7 @@ public class MapOverlay {
private final boolean valid; private final boolean valid;
private final LatLng latLng; private final LatLng latLng;
private final int speed; private final double speed;
/** /**
* Constructor for an invalid cached location. * Constructor for an invalid cached location.
@@ -100,7 +100,7 @@ public class MapOverlay {
public CachedLocation() { public CachedLocation() {
this.valid = false; this.valid = false;
this.latLng = null; this.latLng = null;
this.speed = -1; this.speed = -1.0;
} }
/** /**
@@ -109,9 +109,7 @@ public class MapOverlay {
public CachedLocation(Location location) { public CachedLocation(Location location) {
this.valid = LocationUtils.isValidLocation(location); this.valid = LocationUtils.isValidLocation(location);
this.latLng = valid ? new LatLng(location.getLatitude(), location.getLongitude()) : null; this.latLng = valid ? new LatLng(location.getLatitude(), location.getLongitude()) : null;
this.speed = location.hasSpeed() ? (int) Math.floor( this.speed = location.hasSpeed() ? location.getSpeed() * UnitConversions.MS_TO_KMH : -1.0;
location.getSpeed() * UnitConversions.MS_TO_KMH)
: -1;
} }
/** /**
@@ -124,7 +122,7 @@ public class MapOverlay {
/** /**
* Gets the speed in kilometers per hour. * Gets the speed in kilometers per hour.
*/ */
public int getSpeed() { public double getSpeed() {
return speed; return speed;
} }
@@ -47,8 +47,8 @@ public class DynamicSpeedTrackPathDescriptor implements TrackPathDescriptor {
private final Context context; private final Context context;
private int speedMargin; private int speedMargin;
private int slowSpeed; private double slowSpeed;
private int normalSpeed; private double normalSpeed;
private double averageMovingSpeed; private double averageMovingSpeed;
@VisibleForTesting @VisibleForTesting
@@ -62,14 +62,14 @@ public class DynamicSpeedTrackPathDescriptor implements TrackPathDescriptor {
} }
@Override @Override
public int getSlowSpeed() { public double getSlowSpeed() {
slowSpeed = (int) (averageMovingSpeed - (averageMovingSpeed * speedMargin / 100.0)); slowSpeed = averageMovingSpeed - (averageMovingSpeed * speedMargin / 100.0);
return slowSpeed; return slowSpeed;
} }
@Override @Override
public int getNormalSpeed() { public double getNormalSpeed() {
normalSpeed = (int) (averageMovingSpeed + (averageMovingSpeed * speedMargin / 100.0)); normalSpeed = averageMovingSpeed + (averageMovingSpeed * speedMargin / 100.0);
return normalSpeed; return normalSpeed;
} }
@@ -19,6 +19,7 @@ package com.google.android.apps.mytracks.maps;
import com.google.android.apps.mytracks.Constants; import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.stats.TripStatistics; import com.google.android.apps.mytracks.stats.TripStatistics;
import com.google.android.apps.mytracks.util.PreferencesUtils; import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.content.Context; 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))) { || key.equals(PreferencesUtils.getKey(context, R.string.track_color_mode_slow_key))) {
slowSpeed = PreferencesUtils.getInt(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); PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT);
if (!PreferencesUtils.isMetricUnits(context)) {
slowSpeed = slowSpeed * UnitConversions.MI_TO_KM;
}
} }
if (key == null || key.equals( if (key == null || key.equals(
PreferencesUtils.getKey(context, R.string.track_color_mode_medium_key))) { PreferencesUtils.getKey(context, R.string.track_color_mode_medium_key))) {
normalSpeed = PreferencesUtils.getInt(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); PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT);
if (!PreferencesUtils.isMetricUnits(context)) {
normalSpeed = normalSpeed * UnitConversions.MI_TO_KM;
}
} }
} }
}; };
private final Context context; private final Context context;
private int slowSpeed; private double slowSpeed;
private int normalSpeed; private double normalSpeed;
public FixedSpeedTrackPathDescriptor(Context context) { public FixedSpeedTrackPathDescriptor(Context context) {
this.context = context; this.context = context;
@@ -62,12 +69,12 @@ public class FixedSpeedTrackPathDescriptor implements TrackPathDescriptor {
} }
@Override @Override
public int getSlowSpeed() { public double getSlowSpeed() {
return slowSpeed; return slowSpeed;
} }
@Override @Override
public int getNormalSpeed() { public double getNormalSpeed() {
return normalSpeed; return normalSpeed;
} }
@@ -105,7 +105,7 @@ public class MultiColorTrackPath implements TrackPath {
} }
@VisibleForTesting @VisibleForTesting
protected int getColor(int speed) { protected int getColor(double speed) {
if (speed <= trackPathDescriptor.getSlowSpeed()) { if (speed <= trackPathDescriptor.getSlowSpeed()) {
return slowColor; return slowColor;
} else if (speed <= trackPathDescriptor.getNormalSpeed()) { } else if (speed <= trackPathDescriptor.getNormalSpeed()) {
@@ -28,12 +28,12 @@ public interface TrackPathDescriptor {
/** /**
* Gets the maximum speed which is considered slow. * Gets the maximum speed which is considered slow.
*/ */
public int getSlowSpeed(); public double getSlowSpeed();
/** /**
* Gets the maximum speed which is considered normal. * Gets the maximum speed which is considered normal.
*/ */
public int getNormalSpeed(); public double getNormalSpeed();
/** /**
* Updates state. * Updates state.
@@ -17,7 +17,6 @@
package com.google.android.apps.mytracks.settings; package com.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.util.PreferencesUtils; import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.app.Dialog; import android.app.Dialog;
@@ -247,10 +246,7 @@ public class MapSettingsActivity extends AbstractSettingsActivity {
int keyId, int minValue, int maxValue, int defaultValue, String displayValue) { int keyId, int minValue, int maxValue, int defaultValue, String displayValue) {
int value; int value;
try { try {
value = Integer.parseInt(displayValue); value = Integer.parseInt(displayValue);
if (!PreferencesUtils.isMetricUnits(this)) {
value = (int) (value * UnitConversions.MI_TO_KM);
}
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Log.e(TAG, "invalid value " + displayValue); Log.e(TAG, "invalid value " + displayValue);
value = defaultValue; value = defaultValue;
@@ -273,10 +269,6 @@ public class MapSettingsActivity extends AbstractSettingsActivity {
* @param defaultValue the default value * @param defaultValue the default value
*/ */
private int getSpeedDisplayValue(int keyId, int defaultValue) { private int getSpeedDisplayValue(int keyId, int defaultValue) {
int value = PreferencesUtils.getInt(this, keyId, defaultValue); return PreferencesUtils.getInt(this, keyId, defaultValue);
if (!PreferencesUtils.isMetricUnits(this)) {
value = (int) (value * UnitConversions.KM_TO_MI);
}
return value;
} }
} }
@@ -50,8 +50,9 @@ public class FixedSpeedTrackPathDescriptorTest extends AndroidTestCase {
context, R.string.track_color_mode_medium_key, normalSpeedExpectations[i]); context, R.string.track_color_mode_medium_key, normalSpeedExpectations[i]);
FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor( FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor(
context); context);
assertEquals(slowSpeedExpectations[i], fixedSpeedTrackPathDescriptor.getSlowSpeed()); assertEquals(slowSpeedExpectations[i], (int) fixedSpeedTrackPathDescriptor.getSlowSpeed());
assertEquals(normalSpeedExpectations[i], fixedSpeedTrackPathDescriptor.getNormalSpeed()); assertEquals(
normalSpeedExpectations[i], (int) fixedSpeedTrackPathDescriptor.getNormalSpeed());
} }
} }
@@ -62,11 +63,11 @@ public class FixedSpeedTrackPathDescriptorTest extends AndroidTestCase {
public void testGetSpeed() { public void testGetSpeed() {
FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor( FixedSpeedTrackPathDescriptor fixedSpeedTrackPathDescriptor = new FixedSpeedTrackPathDescriptor(
context); context);
int slowSpeed = fixedSpeedTrackPathDescriptor.getSlowSpeed(); double slowSpeed = fixedSpeedTrackPathDescriptor.getSlowSpeed();
int normalSpeed = fixedSpeedTrackPathDescriptor.getNormalSpeed(); double normalSpeed = fixedSpeedTrackPathDescriptor.getNormalSpeed();
// Change value in shared preferences // 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_slow_key, (int) (slowSpeed + 2));
PreferencesUtils.setInt(context, R.string.track_color_mode_medium_key, normalSpeed + 2); PreferencesUtils.setInt(context, R.string.track_color_mode_medium_key, (int) (normalSpeed + 2));
assertEquals(slowSpeed, fixedSpeedTrackPathDescriptor.getSlowSpeed()); assertEquals(slowSpeed, fixedSpeedTrackPathDescriptor.getSlowSpeed());
assertEquals(normalSpeed, fixedSpeedTrackPathDescriptor.getNormalSpeed()); assertEquals(normalSpeed, fixedSpeedTrackPathDescriptor.getNormalSpeed());
} }