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)">
Latitude/Longitude
</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"
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.
@@ -2028,7 +2022,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.">
Please input your weight to calculate your calories expenditure.
Weight for calculating calories.
</string>
<string name="settings_stats_rate_title"
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.
Character limit: ~23 (1 line)">
Calories expenditure
Calories
</string>
<string name="stats_fastest_pace"
translation_description="In the 'Stats' page, the fastest pace.">
+3 -2
View File
@@ -36,12 +36,13 @@ limitations under the License.
<CheckBoxPreference
android:defaultValue="false"
android:key="@string/stats_show_calorie_key"
android:title="@string/settings_stats_calorie" />
android:title="@string/stats_calorie" />
<EditTextPreference
android:enabled="false"
android:imeOptions="actionDone"
android:inputType="number"
android:key="@string/stats_weight_key"
android:persistent="false"
android:title="@string/settings_stats_calorie_weight" />
</PreferenceCategory>
</PreferenceCategory>
</PreferenceScreen>
@@ -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);
}
}
@@ -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);
}
}
}
@@ -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";