Make the weight dialog always display after enable calorie display. Change code style.

This commit is contained in:
youtaol
2013-08-27 17:06:28 +08:00
parent 57ca65e3b6
commit 10a3e18e7a
6 changed files with 44 additions and 65 deletions
+2 -8
View File
@@ -2007,12 +2007,6 @@ limitations under the License.
Character limit: ~23 (1 line)"> Character limit: ~23 (1 line)">
Latitude/Longitude Latitude/Longitude
</string> </string>
<string name="settings_stats_calorie"
translation_description="In the 'Stats settings, the option to show calorie value.
Character limit: ~23 (1 line)">
Calories
</string>
<string name="settings_stats_grade_elevation" <string name="settings_stats_grade_elevation"
translation_description="In the 'Stats settings, the option to show grade and elevation. Grade translation_description="In the 'Stats settings, the option to show grade and elevation. Grade
is the amount of inclination to the horizontal, also known as slope or incline. is the amount of inclination to the horizontal, also known as slope or incline.
@@ -2028,7 +2022,7 @@ limitations under the License.
</string> </string>
<string name="settings_stats_calorie_weight_description" <string name="settings_stats_calorie_weight_description"
translation_description="In the 'Stats settings, the description message about weight setting item."> translation_description="In the 'Stats settings, the description message about weight setting item.">
Please input your weight to calculate your calories expenditure. Weight for calculating calories.
</string> </string>
<string name="settings_stats_rate_title" <string name="settings_stats_rate_title"
translation_description="In the 'Stats' settings, the option to change the preferred rate, translation_description="In the 'Stats' settings, the option to change the preferred rate,
@@ -2150,7 +2144,7 @@ limitations under the License.
translation_description="In the 'Stats' page, the calorie burned. translation_description="In the 'Stats' page, the calorie burned.
Character limit: ~23 (1 line)"> Character limit: ~23 (1 line)">
Calories expenditure Calories
</string> </string>
<string name="stats_fastest_pace" <string name="stats_fastest_pace"
translation_description="In the 'Stats' page, the fastest pace."> translation_description="In the 'Stats' page, the fastest pace.">
+3 -2
View File
@@ -36,12 +36,13 @@ limitations under the License.
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="false" android:defaultValue="false"
android:key="@string/stats_show_calorie_key" android:key="@string/stats_show_calorie_key"
android:title="@string/settings_stats_calorie" /> android:title="@string/stats_calorie" />
<EditTextPreference <EditTextPreference
android:enabled="false"
android:imeOptions="actionDone" android:imeOptions="actionDone"
android:inputType="number" android:inputType="number"
android:key="@string/stats_weight_key" android:key="@string/stats_weight_key"
android:persistent="false" android:persistent="false"
android:title="@string/settings_stats_calorie_weight" /> android:title="@string/settings_stats_calorie_weight" />
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>
@@ -58,7 +58,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
weightPreference = (EditTextPreference) findPreference(getString(R.string.stats_weight_key)); weightPreference = (EditTextPreference) findPreference(getString(R.string.stats_weight_key));
configCaloriePreference(); configCaloriePreference();
configWeightPreference(R.string.stats_weight_key, PreferencesUtils.STATS_WEIGHT_DEFAULT); configWeightPreference();
/* /*
* Note configureUnitsListPreference will trigger * Note configureUnitsListPreference will trigger
* configureRateListPreference * configureRateListPreference
@@ -81,9 +81,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
* It is necessary to change the weight summary as they are in the same * It is necessary to change the weight summary as they are in the same
* activity. * activity.
*/ */
updateWeightSummary(weightPreference, R.string.stats_weight_key, updateWeightSummary(PreferencesUtils.STATS_UNITS_DEFAULT.equals((String) newValue));
PreferencesUtils.STATS_WEIGHT_DEFAULT,
PreferencesUtils.STATS_UNITS_DEFAULT.equals((String) newValue));
return true; return true;
} }
}; };
@@ -139,11 +137,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
private void showWeightInputPreferenceDialog() { private void showWeightInputPreferenceDialog() {
int value = PreferencesUtils.getInt(this, R.string.stats_weight_key, showDialog(WEIGHT_INPUT_DIALOG);
PreferencesUtils.STATS_WEIGHT_DEFAULT_INVALID);
if (value == PreferencesUtils.STATS_WEIGHT_DEFAULT_INVALID) {
showDialog(WEIGHT_INPUT_DIALOG);
}
} }
@Override @Override
@@ -158,27 +152,19 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
weightInput.setSelectAllOnFocus(true); weightInput.setSelectAllOnFocus(true);
AlertDialog.Builder inputDialogBuilder = new AlertDialog.Builder(this); AlertDialog.Builder inputDialogBuilder = new AlertDialog.Builder(this);
inputDialogBuilder inputDialogBuilder
.setTitle(R.string.settings_stats_calorie_weight)
.setMessage(R.string.settings_stats_calorie_weight_description) .setMessage(R.string.settings_stats_calorie_weight_description)
.setCancelable(false) .setTitle(R.string.settings_stats_calorie_weight)
.setView(weightInput) .setView(weightInput)
.setPositiveButton(getString(R.string.generic_ok), .setPositiveButton(getString(R.string.generic_ok),
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int number) { public void onClick(DialogInterface dialogInterface, int number) {
storeWeightValue(R.string.stats_weight_key, storeWeightValue(weightInput.getText().toString());
PreferencesUtils.STATS_WEIGHT_DEFAULT, weightInput.getText().toString()); updateWeightSummary();
updateWeightSummary(weightPreference, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT);
dialogInterface.cancel(); dialogInterface.cancel();
} }
}) }).setNegativeButton(getString(R.string.generic_cancel), null).create().show();
.setNegativeButton(getString(R.string.generic_cancel), break;
new DialogInterface.OnClickListener() { default:
public void onClick(DialogInterface dialogInterface, int number) {
dialogInterface.cancel();
}
});
inputDialogBuilder.create().show();
break; break;
} }
return dialog; return dialog;
@@ -192,13 +178,13 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
* @param defaultValue default value of this preference * @param defaultValue default value of this preference
* @param isEnable true means enable the weight preference * @param isEnable true means enable the weight preference
*/ */
private void configWeightPreference(final int key, final int defaultValue) { private void configWeightPreference() {
updateWeightSummary(weightPreference, key, defaultValue); updateWeightSummary();
weightPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { weightPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
storeWeightValue(key, defaultValue, (String) newValue); storeWeightValue((String) newValue);
updateWeightSummary(preference, key, defaultValue); updateWeightSummary();
return true; return true;
} }
}); });
@@ -206,8 +192,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
weightPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { weightPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override @Override
public boolean onPreferenceClick(Preference pref) { public boolean onPreferenceClick(Preference pref) {
int value = getWeightDisplayValue(key, defaultValue, int value = getWeightDisplayValue(PreferencesUtils.isMetricUnits(getApplicationContext()));
PreferencesUtils.isMetricUnits(getApplicationContext()));
((EditTextPreference) pref).getEditText().setText(String.valueOf(value)); ((EditTextPreference) pref).getEditText().setText(String.valueOf(value));
return true; return true;
} }
@@ -221,9 +206,9 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
* @param keyId the key id * @param keyId the key id
* @param defaultValue the default value * @param defaultValue the default value
*/ */
private void updateWeightSummary(Preference preference, int keyId, int defaultValue) { private void updateWeightSummary() {
boolean metricUnits = PreferencesUtils.isMetricUnits(this); 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 defaultValue the default value
* @param metricUnits the status of metric units * @param metricUnits the status of metric units
*/ */
private void updateWeightSummary(Preference preference, int keyId, int defaultValue, private void updateWeightSummary(boolean metricUnits) {
boolean metricUnits) { int displayValue = getWeightDisplayValue(metricUnits);
int displayValue = getWeightDisplayValue(keyId, defaultValue, metricUnits); weightPreference.setSummary(getString(metricUnits ? R.string.value_integer_kilogram
preference.setSummary(getString(metricUnits ? R.string.value_integer_kilogram
: R.string.value_integer_pound, displayValue)); : R.string.value_integer_pound, displayValue));
} }
@@ -248,8 +232,9 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
* @param keyId the key id * @param keyId the key id
* @param defaultValue the default value * @param defaultValue the default value
*/ */
private int getWeightDisplayValue(int keyId, int defaultValue, boolean metricUnits) { private int getWeightDisplayValue(boolean metricUnits) {
int value = PreferencesUtils.getInt(this, keyId, defaultValue); int value = PreferencesUtils.getInt(this, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT);
if (!metricUnits) { if (!metricUnits) {
value = (int) Math.round(value * UnitConversions.KG_TO_LB); value = (int) Math.round(value * UnitConversions.KG_TO_LB);
} }
@@ -263,7 +248,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
* @param defaultValue the default value * @param defaultValue the default value
* @param displayValue the display 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 * TODO add a method to an abstract class or an utility class to avoid
* duplicating store preference logic in MapSettingsActivity.java. * duplicating store preference logic in MapSettingsActivity.java.
@@ -276,9 +261,9 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
Log.e(TAG, "invalid value " + displayValue); 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);
} }
} }
@@ -20,7 +20,7 @@ import com.google.common.annotations.VisibleForTesting;
import android.location.Location; import android.location.Location;
/** /**
* Utils to calculate caloric expenditure. * Utils to calculate calories.
* *
* @author youtaol * @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 start the start location
* @param stop the stop location * @param stop the stop location
* @param grade the grade to calculate * @param grade the grade to calculate
* @param weight the weight of user, in kilogram * @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 @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 // Gets duration in seconds
double duration = (double) (stop.getTime() - start.getTime()) * UnitConversions.MS_TO_S; double duration = (double) (stop.getTime() - start.getTime()) * UnitConversions.MS_TO_S;
// Get speed in meters per second // 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 start the start location
* @param stop the stop location * @param stop the stop location
* @param grade the grade to calculate * @param grade the grade to calculate
* @param weight the weight of user, in kilogram * @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. * calories.
*/ */
@VisibleForTesting @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 // Get speed in meters per second
double averageSpeed = (start.getSpeed() + stop.getSpeed()) / 2.0; double averageSpeed = (start.getSpeed() + stop.getSpeed()) / 2.0;
// Get VO2 in mL/kg/min // 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 start the start location
* @param stop the stop location * @param stop the stop location
* @param grade the grade to calculate * @param grade the grade to calculate
* @param weight the weight of user * @param weight the weight of user
* @param activityType can be foot or cycling * @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, public static double getCalories(Location start, Location stop, double grade, int weight,
ActivityType activityType) { ActivityType activityType) {
if (ActivityType.CYCLING == activityType) { if (ActivityType.CYCLING == activityType) {
return calculateExpenditureCycling(start, stop, grade, weight); return calculateCaloriesCycling(start, stop, grade, weight);
} else { } else {
return calculateExpenditureFoot(start, stop, grade, weight); return calculateCaloriesFoot(start, stop, grade, weight);
} }
} }
} }
@@ -107,7 +107,6 @@ public class PreferencesUtils {
public static final String STATS_UNITS_DEFAULT = "METRIC"; public static final String STATS_UNITS_DEFAULT = "METRIC";
public static final boolean STATS_SHOW_CALORIE_DEFAULT = false; 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 = 65; // in kilogram
public static final int STATS_WEIGHT_DEFAULT_INVALID = -1;
// Track color // Track color
public static final String TRACK_COLOR_MODE_DEFAULT = "SINGLE"; public static final String TRACK_COLOR_MODE_DEFAULT = "SINGLE";
@@ -46,7 +46,7 @@ public class CalorieUtilsTest extends TestCase {
public void testGetCalories_foot() { public void testGetCalories_foot() {
double actual = CalorieUtils.getCalories(start, stop, grade, weight, double actual = CalorieUtils.getCalories(start, stop, grade, weight,
CalorieUtils.ActivityType.FOOT); CalorieUtils.ActivityType.FOOT);
double expected = CalorieUtils.calculateExpenditureFoot(start, stop, grade, weight); double expected = CalorieUtils.calculateCaloriesFoot(start, stop, grade, weight);
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@@ -58,7 +58,7 @@ public class CalorieUtilsTest extends TestCase {
double expectGrade = 0; double expectGrade = 0;
double actual = CalorieUtils.getCalories(start, stop, actualGrade, weight, double actual = CalorieUtils.getCalories(start, stop, actualGrade, weight,
CalorieUtils.ActivityType.FOOT); CalorieUtils.ActivityType.FOOT);
double expected = CalorieUtils.calculateExpenditureFoot(start, stop, expectGrade, weight); double expected = CalorieUtils.calculateCaloriesFoot(start, stop, expectGrade, weight);
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@@ -68,7 +68,7 @@ public class CalorieUtilsTest extends TestCase {
public void testGetCalories_cycling() { public void testGetCalories_cycling() {
double actual = CalorieUtils.getCalories(start, stop, grade, weight, double actual = CalorieUtils.getCalories(start, stop, grade, weight,
CalorieUtils.ActivityType.CYCLING); CalorieUtils.ActivityType.CYCLING);
double expected = CalorieUtils.calculateExpenditureCycling(start, stop, grade, weight); double expected = CalorieUtils.calculateCaloriesCycling(start, stop, grade, weight);
assertEquals(expected, actual); assertEquals(expected, actual);
} }