Change code style about calorie.

This commit is contained in:
youtaol
2013-08-26 19:23:57 +08:00
parent 16ff92230a
commit 57ca65e3b6
4 changed files with 75 additions and 62 deletions
+5 -5
View File
@@ -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
</string>
<string name="settings_stats_grade_elevation"
translation_description="In the 'Stats settings, the option to show grade and elevation. Grade
@@ -2028,7 +2028,7 @@ limitations under the License.
</string>
<string name="settings_stats_calorie_weight_description"
translation_description="In the 'Stats settings, the description message about weight setting item.">
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.
</string>
<string name="settings_stats_rate_title"
translation_description="In the 'Stats' settings, the option to change the preferred rate,
@@ -2040,12 +2040,12 @@ limitations under the License.
<string name="settings_stats_units_imperial"
translation_description="In the 'Stats' settings, the option to use imperial units, with
examples.">
Imperial (mi, ft, lb)
Imperial (mi, ft)
</string>
<string name="settings_stats_units_metric"
translation_description="In the 'Stats' settings, the option to use metric units, with
examples.">
Metric (km, m, kg)
Metric (km, m)
</string>
<string name="settings_stats_units_title"
translation_description="In the 'Stats' settings page, the option to change the preferred units,
@@ -2150,7 +2150,7 @@ limitations under the License.
translation_description="In the 'Stats' page, the calorie burned.
Character limit: ~23 (1 line)">
Calorie expenditure
Calories expenditure
</string>
<string name="stats_fastest_pace"
translation_description="In the 'Stats' page, the fastest pace.">
@@ -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);
@@ -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";
@@ -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;
}