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 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;
}
@@ -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;
}
@@ -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;
}
@@ -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()) {
@@ -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.
@@ -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);
}
}
@@ -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());
}