diff --git a/MyTracks/res/values/strings.xml b/MyTracks/res/values/strings.xml index a5cadd55d..05e38ed43 100644 --- a/MyTracks/res/values/strings.xml +++ b/MyTracks/res/values/strings.xml @@ -2007,12 +2007,6 @@ limitations under the License. Character limit: ~23 (1 line)"> Latitude/Longitude - - Calories - - Please input your weight to calculate your calories expenditure. + Weight for calculating calories. - Calories expenditure + Calories diff --git a/MyTracks/res/xml/stats_settings.xml b/MyTracks/res/xml/stats_settings.xml index c4bca4ab4..238bfb419 100644 --- a/MyTracks/res/xml/stats_settings.xml +++ b/MyTracks/res/xml/stats_settings.xml @@ -36,12 +36,13 @@ limitations under the License. + android:title="@string/stats_calorie" /> - + \ No newline at end of file diff --git a/MyTracks/src/com/google/android/apps/mytracks/settings/StatsSettingsActivity.java b/MyTracks/src/com/google/android/apps/mytracks/settings/StatsSettingsActivity.java index 338cf86a9..0f8abe1fb 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/settings/StatsSettingsActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/settings/StatsSettingsActivity.java @@ -58,7 +58,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { weightPreference = (EditTextPreference) findPreference(getString(R.string.stats_weight_key)); configCaloriePreference(); - configWeightPreference(R.string.stats_weight_key, PreferencesUtils.STATS_WEIGHT_DEFAULT); + configWeightPreference(); /* * Note configureUnitsListPreference will trigger * configureRateListPreference @@ -81,9 +81,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { * It is necessary to change the weight summary as they are in the same * activity. */ - updateWeightSummary(weightPreference, R.string.stats_weight_key, - PreferencesUtils.STATS_WEIGHT_DEFAULT, - PreferencesUtils.STATS_UNITS_DEFAULT.equals((String) newValue)); + updateWeightSummary(PreferencesUtils.STATS_UNITS_DEFAULT.equals((String) newValue)); return true; } }; @@ -139,11 +137,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { */ @SuppressWarnings("deprecation") private void showWeightInputPreferenceDialog() { - int value = PreferencesUtils.getInt(this, R.string.stats_weight_key, - PreferencesUtils.STATS_WEIGHT_DEFAULT_INVALID); - if (value == PreferencesUtils.STATS_WEIGHT_DEFAULT_INVALID) { - showDialog(WEIGHT_INPUT_DIALOG); - } + showDialog(WEIGHT_INPUT_DIALOG); } @Override @@ -158,27 +152,19 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { weightInput.setSelectAllOnFocus(true); AlertDialog.Builder inputDialogBuilder = new AlertDialog.Builder(this); inputDialogBuilder - .setTitle(R.string.settings_stats_calorie_weight) .setMessage(R.string.settings_stats_calorie_weight_description) - .setCancelable(false) + .setTitle(R.string.settings_stats_calorie_weight) .setView(weightInput) .setPositiveButton(getString(R.string.generic_ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int number) { - storeWeightValue(R.string.stats_weight_key, - PreferencesUtils.STATS_WEIGHT_DEFAULT, weightInput.getText().toString()); - updateWeightSummary(weightPreference, R.string.stats_weight_key, - PreferencesUtils.STATS_WEIGHT_DEFAULT); + storeWeightValue(weightInput.getText().toString()); + updateWeightSummary(); dialogInterface.cancel(); } - }) - .setNegativeButton(getString(R.string.generic_cancel), - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialogInterface, int number) { - dialogInterface.cancel(); - } - }); - inputDialogBuilder.create().show(); + }).setNegativeButton(getString(R.string.generic_cancel), null).create().show(); + break; + default: break; } return dialog; @@ -192,13 +178,13 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { * @param defaultValue default value of this preference * @param isEnable true means enable the weight preference */ - private void configWeightPreference(final int key, final int defaultValue) { - updateWeightSummary(weightPreference, key, defaultValue); + private void configWeightPreference() { + updateWeightSummary(); weightPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - storeWeightValue(key, defaultValue, (String) newValue); - updateWeightSummary(preference, key, defaultValue); + storeWeightValue((String) newValue); + updateWeightSummary(); return true; } }); @@ -206,8 +192,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { weightPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference pref) { - int value = getWeightDisplayValue(key, defaultValue, - PreferencesUtils.isMetricUnits(getApplicationContext())); + int value = getWeightDisplayValue(PreferencesUtils.isMetricUnits(getApplicationContext())); ((EditTextPreference) pref).getEditText().setText(String.valueOf(value)); return true; } @@ -221,9 +206,9 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { * @param keyId the key id * @param defaultValue the default value */ - private void updateWeightSummary(Preference preference, int keyId, int defaultValue) { + private void updateWeightSummary() { boolean metricUnits = PreferencesUtils.isMetricUnits(this); - updateWeightSummary(preference, keyId, defaultValue, metricUnits); + updateWeightSummary(metricUnits); } /** @@ -234,10 +219,9 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { * @param defaultValue the default value * @param metricUnits the status of metric units */ - private void updateWeightSummary(Preference preference, int keyId, int defaultValue, - boolean metricUnits) { - int displayValue = getWeightDisplayValue(keyId, defaultValue, metricUnits); - preference.setSummary(getString(metricUnits ? R.string.value_integer_kilogram + private void updateWeightSummary(boolean metricUnits) { + int displayValue = getWeightDisplayValue(metricUnits); + weightPreference.setSummary(getString(metricUnits ? R.string.value_integer_kilogram : R.string.value_integer_pound, displayValue)); } @@ -248,8 +232,9 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { * @param keyId the key id * @param defaultValue the default value */ - private int getWeightDisplayValue(int keyId, int defaultValue, boolean metricUnits) { - int value = PreferencesUtils.getInt(this, keyId, defaultValue); + private int getWeightDisplayValue(boolean metricUnits) { + int value = PreferencesUtils.getInt(this, R.string.stats_weight_key, + PreferencesUtils.STATS_WEIGHT_DEFAULT); if (!metricUnits) { value = (int) Math.round(value * UnitConversions.KG_TO_LB); } @@ -263,7 +248,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { * @param defaultValue the default value * @param displayValue the display value */ - private void storeWeightValue(int keyId, int defaultValue, String displayValue) { + private void storeWeightValue(String displayValue) { /* * TODO add a method to an abstract class or an utility class to avoid * duplicating store preference logic in MapSettingsActivity.java. @@ -276,9 +261,9 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { } } catch (NumberFormatException e) { Log.e(TAG, "invalid value " + displayValue); - value = defaultValue; + value = PreferencesUtils.STATS_WEIGHT_DEFAULT; } - PreferencesUtils.setInt(this, keyId, value); + PreferencesUtils.setInt(this, R.string.stats_weight_key, value); } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/CalorieUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/CalorieUtils.java index 83d244ada..be7982615 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/CalorieUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/CalorieUtils.java @@ -20,7 +20,7 @@ import com.google.common.annotations.VisibleForTesting; import android.location.Location; /** - * Utils to calculate caloric expenditure. + * Utils to calculate calories. * * @author youtaol */ @@ -140,16 +140,16 @@ public class CalorieUtils { } /** - * Calculates the calories expenditure by cycling. + * Calculates the calories by cycling. * * @param start the start location * @param stop the stop location * @param grade the grade to calculate * @param weight the weight of user, in kilogram - * @return the calories expenditure between the start and stop location. + * @return the calories between the start and stop location. */ @VisibleForTesting - static double calculateExpenditureCycling(Location start, Location stop, double grade, int weight) { + static double calculateCaloriesCycling(Location start, Location stop, double grade, int weight) { // Gets duration in seconds double duration = (double) (stop.getTime() - start.getTime()) * UnitConversions.MS_TO_S; // Get speed in meters per second @@ -162,17 +162,17 @@ public class CalorieUtils { } /** - * Calculates the calories expenditure by running or walking. + * Calculates the calories by running or walking. * * @param start the start location * @param stop the stop location * @param grade the grade to calculate * @param weight the weight of user, in kilogram - * @return the calories expenditure between the start and stop location, in + * @return the calories between the start and stop location, in * calories. */ @VisibleForTesting - static double calculateExpenditureFoot(Location start, Location stop, double grade, int weight) { + static double calculateCaloriesFoot(Location start, Location stop, double grade, int weight) { // Get speed in meters per second double averageSpeed = (start.getSpeed() + stop.getSpeed()) / 2.0; // Get VO2 in mL/kg/min @@ -203,21 +203,21 @@ public class CalorieUtils { } /** - * Calculates the calories expenditure between two locations. + * Calculates the calories between two locations. * * @param start the start location * @param stop the stop location * @param grade the grade to calculate * @param weight the weight of user * @param activityType can be foot or cycling - * @return the calories expenditure between the start and stop location. + * @return the calories between the start and stop location. */ public static double getCalories(Location start, Location stop, double grade, int weight, ActivityType activityType) { if (ActivityType.CYCLING == activityType) { - return calculateExpenditureCycling(start, stop, grade, weight); + return calculateCaloriesCycling(start, stop, grade, weight); } else { - return calculateExpenditureFoot(start, stop, grade, weight); + return calculateCaloriesFoot(start, stop, grade, weight); } } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/PreferencesUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/PreferencesUtils.java index d71a24911..080abc81f 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/PreferencesUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/PreferencesUtils.java @@ -107,7 +107,6 @@ public class PreferencesUtils { public static final String STATS_UNITS_DEFAULT = "METRIC"; public static final boolean STATS_SHOW_CALORIE_DEFAULT = false; public static final int STATS_WEIGHT_DEFAULT = 65; // in kilogram - public static final int STATS_WEIGHT_DEFAULT_INVALID = -1; // Track color public static final String TRACK_COLOR_MODE_DEFAULT = "SINGLE"; diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/util/CalorieUtilsTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/util/CalorieUtilsTest.java index 85cb36ec2..d8e39a29b 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/util/CalorieUtilsTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/util/CalorieUtilsTest.java @@ -46,7 +46,7 @@ public class CalorieUtilsTest extends TestCase { public void testGetCalories_foot() { double actual = CalorieUtils.getCalories(start, stop, grade, weight, CalorieUtils.ActivityType.FOOT); - double expected = CalorieUtils.calculateExpenditureFoot(start, stop, grade, weight); + double expected = CalorieUtils.calculateCaloriesFoot(start, stop, grade, weight); assertEquals(expected, actual); } @@ -58,7 +58,7 @@ public class CalorieUtilsTest extends TestCase { double expectGrade = 0; double actual = CalorieUtils.getCalories(start, stop, actualGrade, weight, CalorieUtils.ActivityType.FOOT); - double expected = CalorieUtils.calculateExpenditureFoot(start, stop, expectGrade, weight); + double expected = CalorieUtils.calculateCaloriesFoot(start, stop, expectGrade, weight); assertEquals(expected, actual); } @@ -68,7 +68,7 @@ public class CalorieUtilsTest extends TestCase { public void testGetCalories_cycling() { double actual = CalorieUtils.getCalories(start, stop, grade, weight, CalorieUtils.ActivityType.CYCLING); - double expected = CalorieUtils.calculateExpenditureCycling(start, stop, grade, weight); + double expected = CalorieUtils.calculateCaloriesCycling(start, stop, grade, weight); assertEquals(expected, actual); }