forked from upstream-mirrors/OpenTracks
Use dependency property to enable/disable weight preference. Change code style.
This commit is contained in:
@@ -59,6 +59,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
|
||||
|
||||
configCaloriePreference();
|
||||
configWeightPreference();
|
||||
updateWeightSummary();
|
||||
/*
|
||||
* Note configureUnitsListPreference will trigger
|
||||
* configureRateListPreference
|
||||
@@ -117,29 +118,18 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
|
||||
*/
|
||||
private void configCaloriePreference() {
|
||||
caloriePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
boolean value = (Boolean) newValue;
|
||||
if (value) {
|
||||
weightPreference.setEnabled(true);
|
||||
showWeightInputPreferenceDialog();
|
||||
} else {
|
||||
weightPreference.setEnabled(false);
|
||||
showDialog(WEIGHT_INPUT_DIALOG);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows this dialog to input weight value while enable show calorie and the
|
||||
* value is not set yet.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
private void showWeightInputPreferenceDialog() {
|
||||
showDialog(WEIGHT_INPUT_DIALOG);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
protected Dialog onCreateDialog(int id) {
|
||||
@@ -150,11 +140,9 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
|
||||
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
|
||||
dialog = (new AlertDialog.Builder(this))
|
||||
.setMessage(R.string.settings_stats_calorie_weight_description)
|
||||
.setTitle(R.string.settings_stats_calorie_weight)
|
||||
.setView(weightInput)
|
||||
.setNegativeButton(getString(R.string.generic_cancel), null)
|
||||
.setPositiveButton(getString(R.string.generic_ok),
|
||||
new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialogInterface, int number) {
|
||||
@@ -162,7 +150,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
|
||||
updateWeightSummary();
|
||||
dialogInterface.cancel();
|
||||
}
|
||||
}).setNegativeButton(getString(R.string.generic_cancel), null).create().show();
|
||||
}).setTitle(R.string.settings_stats_calorie_weight).setView(weightInput).create();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -179,7 +167,6 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
|
||||
* @param isEnable true means enable the weight preference
|
||||
*/
|
||||
private void configWeightPreference() {
|
||||
updateWeightSummary();
|
||||
weightPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
|
||||
@@ -195,7 +195,7 @@ public class TripStatisticsUpdater {
|
||||
// TODO 2: How to get the value of ActivityType.
|
||||
|
||||
// Update calorie
|
||||
double calorie = CalorieUtils.getCalories(lastMovingLocation, location,
|
||||
double calorie = CalorieUtils.getCalorie(lastMovingLocation, location,
|
||||
gradeBuffer.getAverage(), PreferencesUtils.STATS_WEIGHT_DEFAULT, ActivityType.FOOT);
|
||||
currentSegment.addCalorie(calorie);
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ public class CalorieUtils {
|
||||
* @return the VO2 value in ml/kg/min.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
static double calculateWalkingVo2(double speed, double grade) {
|
||||
static double getLowSpeedFootVo2(double speed, double grade) {
|
||||
// Change meters per second to meters per minute
|
||||
speed = speed / UnitConversions.S_TO_MIN;
|
||||
/*
|
||||
@@ -97,7 +97,7 @@ public class CalorieUtils {
|
||||
* @return the VO2 value in ml/kg/min.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
static double calculateRunningVo2(double speed, double grade) {
|
||||
static double getHighSpeedFootVo2(double speed, double grade) {
|
||||
// Change meters per second to meters per minute
|
||||
speed = speed / UnitConversions.S_TO_MIN;
|
||||
/*
|
||||
@@ -131,7 +131,7 @@ public class CalorieUtils {
|
||||
* @return the power value watts(Joule/second).
|
||||
*/
|
||||
@VisibleForTesting
|
||||
static double calculateCyclingCalories(double speed, double grade, int weight, double timeUsed) {
|
||||
static double calculateCyclingCalorie(double speed, double grade, int weight, double timeUsed) {
|
||||
// Get the Power, the unit is Watt (Joule/second)
|
||||
double power = earthGravity * weight * speed * (K1 + grade) + K2 * (speed * speed * speed);
|
||||
|
||||
@@ -149,7 +149,7 @@ public class CalorieUtils {
|
||||
* @return the calories between the start and stop location.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
static double calculateCaloriesCycling(Location start, Location stop, double grade, int weight) {
|
||||
static double calculateCalorieCycling(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
|
||||
@@ -158,7 +158,7 @@ public class CalorieUtils {
|
||||
grade = 0.0;
|
||||
}
|
||||
|
||||
return calculateCyclingCalories(speed, grade, weight, duration);
|
||||
return calculateCyclingCalorie(speed, grade, weight, duration);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -168,15 +168,14 @@ public class CalorieUtils {
|
||||
* @param stop the stop location
|
||||
* @param grade the grade to calculate
|
||||
* @param weight the weight of user, in kilogram
|
||||
* @return the calories between the start and stop location, in
|
||||
* calories.
|
||||
* @return the calories between the start and stop location, in calories.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
static double calculateCaloriesFoot(Location start, Location stop, double grade, int weight) {
|
||||
static double calculateCalorieFoot(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
|
||||
double vo2 = getVo2(averageSpeed, grade);
|
||||
double vo2 = getFootVo2(averageSpeed, grade);
|
||||
// Minutes
|
||||
double time = (double) (stop.getTime() - start.getTime()) * UnitConversions.MS_TO_S
|
||||
* UnitConversions.S_TO_MIN;
|
||||
@@ -193,12 +192,12 @@ public class CalorieUtils {
|
||||
* @return the VO2 value.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
static double getVo2(double speed, double grade) {
|
||||
static double getFootVo2(double speed, double grade) {
|
||||
if (grade < 0) {
|
||||
grade = 0.0;
|
||||
}
|
||||
|
||||
return speed > CRTICAL_SPEED_RUNNING ? calculateRunningVo2(speed, grade) : calculateWalkingVo2(
|
||||
return speed > CRTICAL_SPEED_RUNNING ? getHighSpeedFootVo2(speed, grade) : getLowSpeedFootVo2(
|
||||
speed, grade);
|
||||
}
|
||||
|
||||
@@ -212,12 +211,9 @@ public class CalorieUtils {
|
||||
* @param activityType can be foot or cycling
|
||||
* @return the calories between the start and stop location.
|
||||
*/
|
||||
public static double getCalories(Location start, Location stop, double grade, int weight,
|
||||
public static double getCalorie(Location start, Location stop, double grade, int weight,
|
||||
ActivityType activityType) {
|
||||
if (ActivityType.CYCLING == activityType) {
|
||||
return calculateCaloriesCycling(start, stop, grade, weight);
|
||||
} else {
|
||||
return calculateCaloriesFoot(start, stop, grade, weight);
|
||||
}
|
||||
return ActivityType.CYCLING == activityType ? calculateCalorieCycling(start, stop, grade,
|
||||
weight) : calculateCalorieFoot(start, stop, grade, weight);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user