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:key="@string/stats_show_calorie_key"
android:title="@string/stats_calorie" /> android:title="@string/stats_calorie" />
<EditTextPreference <EditTextPreference
android:enabled="false" android:dependency="@string/stats_show_calorie_key"
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>
@@ -59,6 +59,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
configCaloriePreference(); configCaloriePreference();
configWeightPreference(); configWeightPreference();
updateWeightSummary();
/* /*
* Note configureUnitsListPreference will trigger * Note configureUnitsListPreference will trigger
* configureRateListPreference * configureRateListPreference
@@ -117,29 +118,18 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
*/ */
private void configCaloriePreference() { private void configCaloriePreference() {
caloriePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { caloriePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@SuppressWarnings("deprecation")
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
boolean value = (Boolean) newValue; boolean value = (Boolean) newValue;
if (value) { if (value) {
weightPreference.setEnabled(true); showDialog(WEIGHT_INPUT_DIALOG);
showWeightInputPreferenceDialog();
} else {
weightPreference.setEnabled(false);
} }
return true; 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 @Override
@Deprecated @Deprecated
protected Dialog onCreateDialog(int id) { 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.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
weightInput.setText(Integer.toString(PreferencesUtils.STATS_WEIGHT_DEFAULT)); weightInput.setText(Integer.toString(PreferencesUtils.STATS_WEIGHT_DEFAULT));
weightInput.setSelectAllOnFocus(true); weightInput.setSelectAllOnFocus(true);
AlertDialog.Builder inputDialogBuilder = new AlertDialog.Builder(this); dialog = (new AlertDialog.Builder(this))
inputDialogBuilder
.setMessage(R.string.settings_stats_calorie_weight_description) .setMessage(R.string.settings_stats_calorie_weight_description)
.setTitle(R.string.settings_stats_calorie_weight) .setNegativeButton(getString(R.string.generic_cancel), null)
.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) {
@@ -162,7 +150,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
updateWeightSummary(); updateWeightSummary();
dialogInterface.cancel(); dialogInterface.cancel();
} }
}).setNegativeButton(getString(R.string.generic_cancel), null).create().show(); }).setTitle(R.string.settings_stats_calorie_weight).setView(weightInput).create();
break; break;
default: default:
break; break;
@@ -179,7 +167,6 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
* @param isEnable true means enable the weight preference * @param isEnable true means enable the weight preference
*/ */
private void configWeightPreference() { private void configWeightPreference() {
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) {
@@ -195,7 +195,7 @@ public class TripStatisticsUpdater {
// TODO 2: How to get the value of ActivityType. // TODO 2: How to get the value of ActivityType.
// Update calorie // Update calorie
double calorie = CalorieUtils.getCalories(lastMovingLocation, location, double calorie = CalorieUtils.getCalorie(lastMovingLocation, location,
gradeBuffer.getAverage(), PreferencesUtils.STATS_WEIGHT_DEFAULT, ActivityType.FOOT); gradeBuffer.getAverage(), PreferencesUtils.STATS_WEIGHT_DEFAULT, ActivityType.FOOT);
currentSegment.addCalorie(calorie); currentSegment.addCalorie(calorie);
@@ -76,7 +76,7 @@ public class CalorieUtils {
* @return the VO2 value in ml/kg/min. * @return the VO2 value in ml/kg/min.
*/ */
@VisibleForTesting @VisibleForTesting
static double calculateWalkingVo2(double speed, double grade) { static double getLowSpeedFootVo2(double speed, double grade) {
// Change meters per second to meters per minute // Change meters per second to meters per minute
speed = speed / UnitConversions.S_TO_MIN; speed = speed / UnitConversions.S_TO_MIN;
/* /*
@@ -97,7 +97,7 @@ public class CalorieUtils {
* @return the VO2 value in ml/kg/min. * @return the VO2 value in ml/kg/min.
*/ */
@VisibleForTesting @VisibleForTesting
static double calculateRunningVo2(double speed, double grade) { static double getHighSpeedFootVo2(double speed, double grade) {
// Change meters per second to meters per minute // Change meters per second to meters per minute
speed = speed / UnitConversions.S_TO_MIN; speed = speed / UnitConversions.S_TO_MIN;
/* /*
@@ -131,7 +131,7 @@ public class CalorieUtils {
* @return the power value watts(Joule/second). * @return the power value watts(Joule/second).
*/ */
@VisibleForTesting @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) // Get the Power, the unit is Watt (Joule/second)
double power = earthGravity * weight * speed * (K1 + grade) + K2 * (speed * speed * speed); 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. * @return the calories between the start and stop location.
*/ */
@VisibleForTesting @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 // 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
@@ -158,7 +158,7 @@ public class CalorieUtils {
grade = 0.0; 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 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 between the start and stop location, in * @return the calories between the start and stop location, in calories.
* calories.
*/ */
@VisibleForTesting @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 // 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
double vo2 = getVo2(averageSpeed, grade); double vo2 = getFootVo2(averageSpeed, grade);
// Minutes // Minutes
double time = (double) (stop.getTime() - start.getTime()) * UnitConversions.MS_TO_S double time = (double) (stop.getTime() - start.getTime()) * UnitConversions.MS_TO_S
* UnitConversions.S_TO_MIN; * UnitConversions.S_TO_MIN;
@@ -193,12 +192,12 @@ public class CalorieUtils {
* @return the VO2 value. * @return the VO2 value.
*/ */
@VisibleForTesting @VisibleForTesting
static double getVo2(double speed, double grade) { static double getFootVo2(double speed, double grade) {
if (grade < 0) { if (grade < 0) {
grade = 0.0; grade = 0.0;
} }
return speed > CRTICAL_SPEED_RUNNING ? calculateRunningVo2(speed, grade) : calculateWalkingVo2( return speed > CRTICAL_SPEED_RUNNING ? getHighSpeedFootVo2(speed, grade) : getLowSpeedFootVo2(
speed, grade); speed, grade);
} }
@@ -212,12 +211,9 @@ public class CalorieUtils {
* @param activityType can be foot or cycling * @param activityType can be foot or cycling
* @return the calories 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 getCalorie(Location start, Location stop, double grade, int weight,
ActivityType activityType) { ActivityType activityType) {
if (ActivityType.CYCLING == activityType) { return ActivityType.CYCLING == activityType ? calculateCalorieCycling(start, stop, grade,
return calculateCaloriesCycling(start, stop, grade, weight); weight) : calculateCalorieFoot(start, stop, grade, weight);
} else {
return calculateCaloriesFoot(start, stop, grade, weight);
}
} }
} }
@@ -44,9 +44,9 @@ public class CalorieUtilsTest extends TestCase {
* Checks using foot calculation equation. * Checks using foot calculation equation.
*/ */
public void testGetCalories_foot() { public void testGetCalories_foot() {
double actual = CalorieUtils.getCalories(start, stop, grade, weight, double actual = CalorieUtils.getCalorie(start, stop, grade, weight,
CalorieUtils.ActivityType.FOOT); CalorieUtils.ActivityType.FOOT);
double expected = CalorieUtils.calculateCaloriesFoot(start, stop, grade, weight); double expected = CalorieUtils.calculateCalorieFoot(start, stop, grade, weight);
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@@ -56,9 +56,9 @@ public class CalorieUtilsTest extends TestCase {
public void testGetCalories_footNegativeGrade() { public void testGetCalories_footNegativeGrade() {
double actualGrade = -5; double actualGrade = -5;
double expectGrade = 0; double expectGrade = 0;
double actual = CalorieUtils.getCalories(start, stop, actualGrade, weight, double actual = CalorieUtils.getCalorie(start, stop, actualGrade, weight,
CalorieUtils.ActivityType.FOOT); CalorieUtils.ActivityType.FOOT);
double expected = CalorieUtils.calculateCaloriesFoot(start, stop, expectGrade, weight); double expected = CalorieUtils.calculateCalorieFoot(start, stop, expectGrade, weight);
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@@ -66,9 +66,9 @@ public class CalorieUtilsTest extends TestCase {
* Checks using cycling calculation equation. * Checks using cycling calculation equation.
*/ */
public void testGetCalories_cycling() { public void testGetCalories_cycling() {
double actual = CalorieUtils.getCalories(start, stop, grade, weight, double actual = CalorieUtils.getCalorie(start, stop, grade, weight,
CalorieUtils.ActivityType.CYCLING); CalorieUtils.ActivityType.CYCLING);
double expected = CalorieUtils.calculateCaloriesCycling(start, stop, grade, weight); double expected = CalorieUtils.calculateCalorieCycling(start, stop, grade, weight);
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@@ -76,8 +76,8 @@ public class CalorieUtilsTest extends TestCase {
* Checks using running VO2 equation. * Checks using running VO2 equation.
*/ */
public void testGetVO2_running() { public void testGetVO2_running() {
double actual = CalorieUtils.getVo2(CalorieUtils.CRTICAL_SPEED_RUNNING * 2, grade); double actual = CalorieUtils.getFootVo2(CalorieUtils.CRTICAL_SPEED_RUNNING * 2, grade);
double expected = CalorieUtils.calculateRunningVo2(CalorieUtils.CRTICAL_SPEED_RUNNING * 2, double expected = CalorieUtils.getHighSpeedFootVo2(CalorieUtils.CRTICAL_SPEED_RUNNING * 2,
grade); grade);
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@@ -89,8 +89,8 @@ public class CalorieUtilsTest extends TestCase {
// Test at half the critical speed // Test at half the critical speed
double footSpeed = CalorieUtils.CRTICAL_SPEED_RUNNING / 2.0; double footSpeed = CalorieUtils.CRTICAL_SPEED_RUNNING / 2.0;
double actual = CalorieUtils.getVo2(footSpeed, grade); double actual = CalorieUtils.getFootVo2(footSpeed, grade);
double expected = CalorieUtils.calculateWalkingVo2(footSpeed, grade); double expected = CalorieUtils.getLowSpeedFootVo2(footSpeed, grade);
assertEquals(expected, actual); assertEquals(expected, actual);
} }