diff --git a/MyTracks/res/values/strings.xml b/MyTracks/res/values/strings.xml index 17aa8f4e6..a5cadd55d 100644 --- a/MyTracks/res/values/strings.xml +++ b/MyTracks/res/values/strings.xml @@ -2011,7 +2011,7 @@ limitations under the License. translation_description="In the 'Stats settings, the option to show calorie value. Character limit: ~23 (1 line)"> - Calorie + Calories - This weight value is only used to calculate the calorie expenditure and would be private for yourself. + Please input your weight to calculate your calories expenditure. - Imperial (mi, ft, lb) + Imperial (mi, ft) - Metric (km, m, kg) + Metric (km, m) - Calorie expenditure + Calories expenditure 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 b436050bf..338cf86a9 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/settings/StatsSettingsActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/settings/StatsSettingsActivity.java @@ -21,6 +21,7 @@ import com.google.android.apps.mytracks.util.UnitConversions; import com.google.android.maps.mytracks.R; import android.app.AlertDialog; +import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.preference.CheckBoxPreference; @@ -45,6 +46,8 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { private CheckBoxPreference caloriePreference; private EditTextPreference weightPreference; + private static final int WEIGHT_INPUT_DIALOG = 1; + @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle bundle) { @@ -54,9 +57,8 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { caloriePreference = (CheckBoxPreference) findPreference(getString(R.string.stats_show_calorie_key)); weightPreference = (EditTextPreference) findPreference(getString(R.string.stats_weight_key)); - configCaloriePreference(caloriePreference); - configWeightPreference(weightPreference, R.string.stats_weight_key, - PreferencesUtils.STATS_WEIGHT_DEFAULT, caloriePreference.isChecked()); + configCaloriePreference(); + configWeightPreference(R.string.stats_weight_key, PreferencesUtils.STATS_WEIGHT_DEFAULT); /* * Note configureUnitsListPreference will trigger * configureRateListPreference @@ -115,9 +117,8 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { * @param key of the preference * @param defaultValue default value of this preference */ - private void configCaloriePreference(CheckBoxPreference preference) { - preference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { - @SuppressWarnings("hiding") + private void configCaloriePreference() { + caloriePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { boolean value = (Boolean) newValue; @@ -136,38 +137,53 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { * Shows this dialog to input weight value while enable show calorie and the * value is not set yet. */ + @SuppressWarnings("deprecation") private void showWeightInputPreferenceDialog() { - int value = PreferencesUtils.getInt(this, R.string.stats_weight_key, -1); - if (value == -1) { - final EditText weightInput = new EditText(this); - weightInput.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); - weightInput.setText(Integer.toString(PreferencesUtils.STATS_WEIGHT_DEFAULT)); - 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) - .setView(weightInput) - .setPositiveButton(getString(R.string.generic_ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - storeWeightValue(R.string.stats_weight_key, PreferencesUtils.STATS_WEIGHT_DEFAULT, - weightInput.getText().toString()); - updateWeightSummary(weightPreference, R.string.stats_weight_key, - PreferencesUtils.STATS_WEIGHT_DEFAULT); - dialog.cancel(); - } - }) - .setNegativeButton(getString(R.string.generic_cancel), - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - } - }); - inputDialogBuilder.create().show(); + 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); } } + @Override + @Deprecated + protected Dialog onCreateDialog(int id) { + Dialog dialog = null; + switch (id) { + case WEIGHT_INPUT_DIALOG: + final EditText weightInput = new EditText(this); + weightInput.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); + weightInput.setText(Integer.toString(PreferencesUtils.STATS_WEIGHT_DEFAULT)); + 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) + .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); + dialogInterface.cancel(); + } + }) + .setNegativeButton(getString(R.string.generic_cancel), + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialogInterface, int number) { + dialogInterface.cancel(); + } + }); + inputDialogBuilder.create().show(); + break; + } + return dialog; + } + /** * Configures the weight preference. * @@ -176,17 +192,9 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { * @param defaultValue default value of this preference * @param isEnable true means enable the weight preference */ - private void configWeightPreference(EditTextPreference preference, final int key, - final int defaultValue, boolean isEnable) { - updateWeightSummary(preference, key, defaultValue); - if (isEnable) { - preference.setEnabled(true); - } else { - preference.setEnabled(false); - } - - preference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { - @SuppressWarnings("hiding") + private void configWeightPreference(final int key, final int defaultValue) { + updateWeightSummary(weightPreference, key, defaultValue); + weightPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { storeWeightValue(key, defaultValue, (String) newValue); @@ -195,7 +203,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { } }); - preference.setOnPreferenceClickListener(new OnPreferenceClickListener() { + weightPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference pref) { int value = getWeightDisplayValue(key, defaultValue, @@ -243,7 +251,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { private int getWeightDisplayValue(int keyId, int defaultValue, boolean metricUnits) { int value = PreferencesUtils.getInt(this, keyId, defaultValue); if (!metricUnits) { - value = (int) (value * UnitConversions.KG_TO_LB); + value = (int) Math.round(value * UnitConversions.KG_TO_LB); } return value; } @@ -256,6 +264,10 @@ public class StatsSettingsActivity extends AbstractSettingsActivity { * @param displayValue the display value */ private void storeWeightValue(int keyId, int defaultValue, String displayValue) { + /* + * TODO add a method to an abstract class or an utility class to avoid + * duplicating store preference logic in MapSettingsActivity.java. + */ int value; try { value = Integer.parseInt(displayValue); 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 080abc81f..d71a24911 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/PreferencesUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/PreferencesUtils.java @@ -107,6 +107,7 @@ 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/MyTracks/src/com/google/android/apps/mytracks/util/UnitConversions.java b/MyTracks/src/com/google/android/apps/mytracks/util/UnitConversions.java index 650554c42..cf20d82d2 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/UnitConversions.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/UnitConversions.java @@ -24,31 +24,31 @@ public class UnitConversions { private UnitConversions() {} - // Milliliter to liter + // multiplication factor to convert milliliters to liters public static final double ML_TO_L = 1 / 1000.0; - // Millisecond to second + // multiplication factor to convert milliseconds to seconds public static final double MS_TO_S = 1 / 1000.0; - // Minute to second + // multiplication factor to convert minutes to seconds public static final double MIN_TO_S = 60.0; - // Second to minute + // multiplication factor to convert seconds to minutes public static final double S_TO_MIN = 1 / MIN_TO_S; - // Minute to hour + // multiplication factor to convert hours to minutes public static final double HR_TO_MIN = 60.0; - // Minute to hour + // multiplication factor to convert minutes to hours public static final double MIN_TO_HR = 1 / HR_TO_MIN; // multiplication factor to convert kilometers to miles public static final double KM_TO_MI = 0.621371192; - // multiplication factor to convert kilogram to pounds + // multiplication factor to convert kilograms to pounds public static final double KG_TO_LB = 2.2046; - - // multiplication factor to convert kilogram to pounds + + // multiplication factor to convert pounds to kilograms public static final double LB_TO_KG = 1 / KG_TO_LB; // multiplication factor to convert miles to kilometers @@ -77,6 +77,6 @@ public class UnitConversions { // multiplication factor to convert degrees to radians public static final double DEG_TO_RAD = Math.PI / 180.0; - // Ratio of change kcal to joule. + // multiplication factor to convert Kcal to joules. public static final double KCAL_TO_J = 4.184; }