From b9fd09d043c54f52c9dc640dc9000923be8a828f Mon Sep 17 00:00:00 2001 From: youtaol Date: Wed, 21 Aug 2013 10:23:36 +0800 Subject: [PATCH] Change code style about calorie calculation. --- MyTracks/res/values/strings.xml | 6 ++- .../mytracks/content/MyTracksProvider.java | 5 +- .../mytracks/stats/TripStatisticsUpdater.java | 2 +- .../apps/mytracks/util/CalorieUtils.java | 54 ++++++++++--------- .../apps/mytracks/util/StatsUtils.java | 5 +- .../apps/mytracks/util/UnitConversions.java | 15 +++--- .../apps/mytracks/stats/TripStatistics.java | 9 ++-- .../apps/mytracks/util/CalorieUtilsTest.java | 8 +-- 8 files changed, 56 insertions(+), 48 deletions(-) diff --git a/MyTracks/res/values/strings.xml b/MyTracks/res/values/strings.xml index 051c77225..610f44282 100644 --- a/MyTracks/res/values/strings.xml +++ b/MyTracks/res/values/strings.xml @@ -2115,7 +2115,7 @@ limitations under the License. translation_description="In the 'Stats' page, the calorie burned. Character limit: ~23 (1 line)"> - Calorie expediture + Calorie expenditure @@ -2382,6 +2382,10 @@ limitations under the License. translation_description="Abbreviation for minutes per mile. E.g., '5 min/mi'."> min/mi + + Cal + Reference */ @VisibleForTesting static final double CRTICAL_SPEED_RUNNING = 4.5 * UnitConversions.MI_TO_KM - * UnitConversions.KM_TO_M * UnitConversions.MIN_TO_HR; + * UnitConversions.KM_TO_M / UnitConversions.HR_TO_MIN; public enum ActivityType { CYCLING, FOOT } /** - * Calculates the calorie of walking. This equation is appropriate - * for fairly slow speed ranges—from 1.9 to approximately 4 miles per hour - * (mph). + * Calculates the calorie of walking. This equation is appropriate for fairly + * slow speed ranges—from 1.9 to approximately 4 miles per hour (mph). * * @param speed is calculated in meters per second (m/s) * @param grade * @return the VO2 value in ml/kg/min. */ @VisibleForTesting - static double calculateWalkingVO2(double speed, double grade) { + static double calculateWalkingVo2(double speed, double grade) { // Change meters per second to meters per minute - speed *= UnitConversions.MIN_TO_SECOND; + speed = speed / UnitConversions.S_TO_MIN; /* * 0.1 means oxygen cost per meter of moving each kilogram (kg) of body * weight while walking (horizontally). 1.8 means oxygen cost per meter of @@ -73,18 +76,18 @@ public class CalorieUtils { } /** - * Calculates the calorie of running. This equation is appropriate - * for speeds greater than 5.0 mph (or 3.0 mph or greater if the subject is - * truly jogging). + * Calculates the calorie of running. This equation is appropriate for speeds + * greater than 5.0 mph (or 3.0 mph or greater if the subject is truly + * jogging). * * @param speed is calculated in meters per second (m/s) * @param grade * @return the VO2 value in ml/kg/min. */ @VisibleForTesting - static double calculateRunningVO2(double speed, double grade) { + static double calculateRunningVo2(double speed, double grade) { // Change meters per second to meters per minute - speed *= UnitConversions.MIN_TO_SECOND; + speed *= UnitConversions.MIN_TO_S; /* * 0.2 means oxygen cost per meter of moving each kg of body weight while * running (horizontally). 0.9 means oxygen cost per meter of moving total @@ -111,7 +114,7 @@ public class CalorieUtils { * * @param speed is calculated in meters per second (m/s) * @param grade the grade to calculate - * @param weight of rider plus bike + * @param weight of rider plus bike, in kilogram * @param timeUsed how many times used in second * @return the power value watts(Joule/second). */ @@ -125,7 +128,7 @@ public class CalorieUtils { // Lumped constant for aerodynamic drag double K2 = 0.185; - // Get the Power + // Get the Power, the unit is Watt (Joule/second) double power = earthGravity * weight * speed * (K1 + grade) + K2 * (speed * speed * speed); // Get the calories @@ -138,7 +141,7 @@ public class CalorieUtils { * @param start the start location * @param stop the stop location * @param grade the grade to calculate - * @param weight the weight of user + * @param weight the weight of user, in kilogram * @return the calories expenditure between the start and stop location. */ @VisibleForTesting @@ -160,19 +163,22 @@ public class CalorieUtils { * @param start the start location * @param stop the stop location * @param grade the grade to calculate - * @param weight the weight of user - * @return the calories expenditure between the start and stop location. + * @param weight the weight of user, in kilogram + * @return the calories expenditure between the start and stop location, in + * calories. */ @VisibleForTesting static double calculateExpenditureFoot(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 = getVo2(averageSpeed, grade); // Minutes - double time = (double) (stop.getTime() - start.getTime()) * UnitConversions.MS_TO_S * UnitConversions.S_TO_MIN; - // Get the calorie. The unit of calorie is kcal which is came from mL/kg/min * min * L/mL * kg * kcal/L - return VO2 * time * UnitConversions.ML_TO_L * weight * L_TO_KCAL; + double time = (double) (stop.getTime() - start.getTime()) * UnitConversions.MS_TO_S + * UnitConversions.S_TO_MIN; + // Get the calorie. The unit of calorie is kcal which is came from mL/kg/min + // * min * kg * L/mL * kcal/L + return vo2 * time * weight * UnitConversions.ML_TO_L * L_TO_KCAL; } /** @@ -183,12 +189,12 @@ public class CalorieUtils { * @return the VO2 value. */ @VisibleForTesting - static double getVO2(double speed, double grade) { + static double getVo2(double speed, double grade) { if (grade < 0) { grade = 0.0; } - return speed > CRTICAL_SPEED_RUNNING ? calculateRunningVO2(speed, grade) : calculateWalkingVO2( + return speed > CRTICAL_SPEED_RUNNING ? calculateRunningVo2(speed, grade) : calculateWalkingVo2( speed, grade); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/StatsUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/StatsUtils.java index 760518662..acd2123a5 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/StatsUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/StatsUtils.java @@ -40,9 +40,6 @@ public class StatsUtils { private static final String GRADE_FORMAT = "%1$d"; private static final String CALORIES_FORMAT = "%1$.0f"; - private static final String CALORIES_UNIT = "Cal"; - - private StatsUtils() {} /** @@ -334,6 +331,6 @@ public class StatsUtils { */ private static void setCalorie(Activity activity, int itemId, int labelId, double calorie) { setItem(activity, itemId, labelId, - String.format(Locale.getDefault(), CALORIES_FORMAT, calorie), CALORIES_UNIT); + String.format(Locale.getDefault(), CALORIES_FORMAT, calorie), activity.getString(R.string.unit_calorie)); } } \ No newline at end of file diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/UnitConversions.java b/MyTracks/src/com/google/android/apps/mytracks/util/UnitConversions.java index de6453120..3ff534cb7 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/UnitConversions.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/UnitConversions.java @@ -22,22 +22,25 @@ package com.google.android.apps.mytracks.util; */ public class UnitConversions { + private UnitConversions() {} + // Milliliter to liter - static final double ML_TO_L = 1 / 1000.0; + public static final double ML_TO_L = 1 / 1000.0; // Millisecond to second - static final double MS_TO_S = 1 / 1000.0; + public static final double MS_TO_S = 1 / 1000.0; // Minute to second - static final double MIN_TO_SECOND = 60.0; + public static final double MIN_TO_S = 60.0; // Second to minute - static final double S_TO_MIN = 1 / MIN_TO_SECOND; + public static final double S_TO_MIN = 1 / MIN_TO_S; // Minute to hour - static final double MIN_TO_HR = 1 / 60.0; + public static final double HR_TO_MIN = 60.0; - private UnitConversions() {} + // Minute to hour + public static final double MIN_TO_HR = 1 / HR_TO_MIN; // multiplication factor to convert kilometers to miles public static final double KM_TO_MI = 0.621371192; diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/stats/TripStatistics.java b/MyTracksLib/src/com/google/android/apps/mytracks/stats/TripStatistics.java index 144182363..6ffaefee3 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/stats/TripStatistics.java +++ b/MyTracksLib/src/com/google/android/apps/mytracks/stats/TripStatistics.java @@ -575,12 +575,13 @@ public class TripStatistics implements Parcelable { /** * Adds calorie value. * - * @param calorieAdded add the value to the total calorie + * @param calorie add the value to the total calorie */ - public void addCalorie(double calorieAdded) { - this.calorie += calorieAdded; + @SuppressWarnings("hiding") + public void addCalorie(double calorie) { + this.calorie += calorie; } - + /** * Sets calorie value. * diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/util/CalorieUtilsTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/util/CalorieUtilsTest.java index 52c820a08..85cb36ec2 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/util/CalorieUtilsTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/util/CalorieUtilsTest.java @@ -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.getVo2(CalorieUtils.CRTICAL_SPEED_RUNNING * 2, grade); + double expected = CalorieUtils.calculateRunningVo2(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.getVo2(footSpeed, grade); + double expected = CalorieUtils.calculateWalkingVo2(footSpeed, grade); assertEquals(expected, actual); }