Use dependency property to enable/disable weight preference. Change code style.

This commit is contained in:
youtaol
2013-08-28 10:43:46 +08:00
parent 10a3e18e7a
commit c42abaac6b
5 changed files with 32 additions and 49 deletions
+2 -2
View File
@@ -38,11 +38,11 @@ limitations under the License.
android:key="@string/stats_show_calorie_key"
android:title="@string/stats_calorie" />
<EditTextPreference
android:enabled="false"
android:dependency="@string/stats_show_calorie_key"
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>
@@ -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);
}
}
@@ -44,9 +44,9 @@ public class CalorieUtilsTest extends TestCase {
* Checks using foot calculation equation.
*/
public void testGetCalories_foot() {
double actual = CalorieUtils.getCalories(start, stop, grade, weight,
double actual = CalorieUtils.getCalorie(start, stop, grade, weight,
CalorieUtils.ActivityType.FOOT);
double expected = CalorieUtils.calculateCaloriesFoot(start, stop, grade, weight);
double expected = CalorieUtils.calculateCalorieFoot(start, stop, grade, weight);
assertEquals(expected, actual);
}
@@ -56,9 +56,9 @@ public class CalorieUtilsTest extends TestCase {
public void testGetCalories_footNegativeGrade() {
double actualGrade = -5;
double expectGrade = 0;
double actual = CalorieUtils.getCalories(start, stop, actualGrade, weight,
double actual = CalorieUtils.getCalorie(start, stop, actualGrade, weight,
CalorieUtils.ActivityType.FOOT);
double expected = CalorieUtils.calculateCaloriesFoot(start, stop, expectGrade, weight);
double expected = CalorieUtils.calculateCalorieFoot(start, stop, expectGrade, weight);
assertEquals(expected, actual);
}
@@ -66,9 +66,9 @@ public class CalorieUtilsTest extends TestCase {
* Checks using cycling calculation equation.
*/
public void testGetCalories_cycling() {
double actual = CalorieUtils.getCalories(start, stop, grade, weight,
double actual = CalorieUtils.getCalorie(start, stop, grade, weight,
CalorieUtils.ActivityType.CYCLING);
double expected = CalorieUtils.calculateCaloriesCycling(start, stop, grade, weight);
double expected = CalorieUtils.calculateCalorieCycling(start, stop, grade, weight);
assertEquals(expected, actual);
}
@@ -76,8 +76,8 @@ public class CalorieUtilsTest extends TestCase {
* Checks using running VO2 equation.
*/
public void testGetVO2_running() {
double actual = CalorieUtils.getVo2(CalorieUtils.CRTICAL_SPEED_RUNNING * 2, grade);
double expected = CalorieUtils.calculateRunningVo2(CalorieUtils.CRTICAL_SPEED_RUNNING * 2,
double actual = CalorieUtils.getFootVo2(CalorieUtils.CRTICAL_SPEED_RUNNING * 2, grade);
double expected = CalorieUtils.getHighSpeedFootVo2(CalorieUtils.CRTICAL_SPEED_RUNNING * 2,
grade);
assertEquals(expected, actual);
}
@@ -89,8 +89,8 @@ public class CalorieUtilsTest extends TestCase {
// Test at half the critical speed
double footSpeed = CalorieUtils.CRTICAL_SPEED_RUNNING / 2.0;
double actual = CalorieUtils.getVo2(footSpeed, grade);
double expected = CalorieUtils.calculateWalkingVo2(footSpeed, grade);
double actual = CalorieUtils.getFootVo2(footSpeed, grade);
double expected = CalorieUtils.getLowSpeedFootVo2(footSpeed, grade);
assertEquals(expected, actual);
}