Change code style about calorie calculation.

This commit is contained in:
youtaol
2013-08-21 10:23:36 +08:00
parent d16949c139
commit b9fd09d043
8 changed files with 56 additions and 48 deletions
+5 -1
View File
@@ -2115,7 +2115,7 @@ limitations under the License.
translation_description="In the 'Stats' page, the calorie burned. translation_description="In the 'Stats' page, the calorie burned.
Character limit: ~23 (1 line)"> Character limit: ~23 (1 line)">
Calorie expediture Calorie expenditure
</string> </string>
<string name="stats_fastest_pace" <string name="stats_fastest_pace"
translation_description="In the 'Stats' page, the fastest pace."> translation_description="In the 'Stats' page, the fastest pace.">
@@ -2382,6 +2382,10 @@ limitations under the License.
translation_description="Abbreviation for minutes per mile. E.g., '5 min/mi'."> translation_description="Abbreviation for minutes per mile. E.g., '5 min/mi'.">
min/mi min/mi
</string> </string>
<string name="unit_calorie"
translation_description="Abbreviation for calorie. E.g., '5 Cal'.">
Cal
</string>
<!-- Value --> <!-- Value -->
<string name="value_adapt_accuracy" <string name="value_adapt_accuracy"
@@ -49,7 +49,7 @@ import java.util.ArrayList;
public class MyTracksProvider extends ContentProvider { public class MyTracksProvider extends ContentProvider {
private static final String TAG = MyTracksProvider.class.getSimpleName(); private static final String TAG = MyTracksProvider.class.getSimpleName();
private static final int DATABASE_VERSION = 23; private static final int DATABASE_VERSION = 22;
@VisibleForTesting @VisibleForTesting
static final String DATABASE_NAME = "mytracks.db"; static final String DATABASE_NAME = "mytracks.db";
@@ -129,9 +129,6 @@ public class MyTracksProvider extends ContentProvider {
Log.w(TAG, "Upgrade DB: Adding photo url column."); Log.w(TAG, "Upgrade DB: Adding photo url column.");
db.execSQL("ALTER TABLE " + WaypointsColumns.TABLE_NAME + " ADD " + WaypointsColumns.PHOTOURL db.execSQL("ALTER TABLE " + WaypointsColumns.TABLE_NAME + " ADD " + WaypointsColumns.PHOTOURL
+ " STRING"); + " STRING");
}
if (oldVersion <= 22) {
Log.w(TAG, "Upgrade DB: Adding track calorie column."); Log.w(TAG, "Upgrade DB: Adding track calorie column.");
db.execSQL("ALTER TABLE " + TracksColumns.TABLE_NAME + " ADD " + TracksColumns.CALORIE db.execSQL("ALTER TABLE " + TracksColumns.TABLE_NAME + " ADD " + TracksColumns.CALORIE
+ " INTEGER"); + " INTEGER");
@@ -94,7 +94,7 @@ public class TripStatisticsUpdater {
// A buffer of the recent speed readings (m/s) for calculating max speed // A buffer of the recent speed readings (m/s) for calculating max speed
private final DoubleBuffer speedBuffer = new DoubleBuffer(SPEED_SMOOTHING_FACTOR); private final DoubleBuffer speedBuffer = new DoubleBuffer(SPEED_SMOOTHING_FACTOR);
// Average weight. // Default weight, in kilogram
private int defaultWeight = 65; private int defaultWeight = 65;
/** /**
@@ -40,30 +40,33 @@ public class CalorieUtils {
private static final double L_TO_KCAL = 5; private static final double L_TO_KCAL = 5;
/** /**
* Critical speed running in meters per minute. Changes 4.5 miles per hour to * Critical speed running in meters per minute. Converts 4.5 miles per hour to
* meters per minute. * meters per minute. 4 miles per hour is regarded as the max speed of walking
* and 5 miles per hour is regarded as the minimal speed of running, so we use
* the 4.5 miles per hour as the critical seed. <a href=
* "http://www.ideafit.com/fitness-library/calculating-caloric-expenditure-0"
* >Reference</a>
*/ */
@VisibleForTesting @VisibleForTesting
static final double CRTICAL_SPEED_RUNNING = 4.5 * UnitConversions.MI_TO_KM 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 { public enum ActivityType {
CYCLING, FOOT CYCLING, FOOT
} }
/** /**
* Calculates the calorie of walking. This equation is appropriate * Calculates the calorie of walking. This equation is appropriate for fairly
* for fairly slow speed ranges—from 1.9 to approximately 4 miles per hour * slow speed ranges—from 1.9 to approximately 4 miles per hour (mph).
* (mph).
* *
* @param speed is calculated in meters per second (m/s) * @param speed is calculated in meters per second (m/s)
* @param grade * @param grade
* @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 calculateWalkingVo2(double speed, double grade) {
// Change meters per second to meters per minute // 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 * 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 * 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 * Calculates the calorie of running. This equation is appropriate for speeds
* for speeds greater than 5.0 mph (or 3.0 mph or greater if the subject is * greater than 5.0 mph (or 3.0 mph or greater if the subject is truly
* truly jogging). * jogging).
* *
* @param speed is calculated in meters per second (m/s) * @param speed is calculated in meters per second (m/s)
* @param grade * @param grade
* @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 calculateRunningVo2(double speed, double grade) {
// Change meters per second to meters per minute // 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 * 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 * 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 speed is calculated in meters per second (m/s)
* @param grade the grade to calculate * @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 * @param timeUsed how many times used in second
* @return the power value watts(Joule/second). * @return the power value watts(Joule/second).
*/ */
@@ -125,7 +128,7 @@ public class CalorieUtils {
// Lumped constant for aerodynamic drag // Lumped constant for aerodynamic drag
double K2 = 0.185; 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); double power = earthGravity * weight * speed * (K1 + grade) + K2 * (speed * speed * speed);
// Get the calories // Get the calories
@@ -138,7 +141,7 @@ public class CalorieUtils {
* @param start the start location * @param start the start location
* @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 * @param weight the weight of user, in kilogram
* @return the calories expenditure between the start and stop location. * @return the calories expenditure between the start and stop location.
*/ */
@VisibleForTesting @VisibleForTesting
@@ -160,19 +163,22 @@ public class CalorieUtils {
* @param start the start location * @param start the start location
* @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 * @param weight the weight of user, in kilogram
* @return the calories expenditure between the start and stop location. * @return the calories expenditure between the start and stop location, in
* calories.
*/ */
@VisibleForTesting @VisibleForTesting
static double calculateExpenditureFoot(Location start, Location stop, double grade, int weight) { static double calculateExpenditureFoot(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 = getVo2(averageSpeed, grade);
// Minutes // Minutes
double time = (double) (stop.getTime() - start.getTime()) * UnitConversions.MS_TO_S * UnitConversions.S_TO_MIN; double time = (double) (stop.getTime() - start.getTime()) * UnitConversions.MS_TO_S
// Get the calorie. The unit of calorie is kcal which is came from mL/kg/min * min * L/mL * kg * kcal/L * UnitConversions.S_TO_MIN;
return VO2 * time * UnitConversions.ML_TO_L * weight * L_TO_KCAL; // 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. * @return the VO2 value.
*/ */
@VisibleForTesting @VisibleForTesting
static double getVO2(double speed, double grade) { static double getVo2(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 ? calculateRunningVo2(speed, grade) : calculateWalkingVo2(
speed, grade); speed, grade);
} }
@@ -40,9 +40,6 @@ public class StatsUtils {
private static final String GRADE_FORMAT = "%1$d"; private static final String GRADE_FORMAT = "%1$d";
private static final String CALORIES_FORMAT = "%1$.0f"; private static final String CALORIES_FORMAT = "%1$.0f";
private static final String CALORIES_UNIT = "Cal";
private StatsUtils() {} private StatsUtils() {}
/** /**
@@ -334,6 +331,6 @@ public class StatsUtils {
*/ */
private static void setCalorie(Activity activity, int itemId, int labelId, double calorie) { private static void setCalorie(Activity activity, int itemId, int labelId, double calorie) {
setItem(activity, itemId, labelId, 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));
} }
} }
@@ -22,22 +22,25 @@ package com.google.android.apps.mytracks.util;
*/ */
public class UnitConversions { public class UnitConversions {
private UnitConversions() {}
// Milliliter to liter // 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 // 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 // Minute to second
static final double MIN_TO_SECOND = 60.0; public static final double MIN_TO_S = 60.0;
// Second to minute // 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 // 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 // multiplication factor to convert kilometers to miles
public static final double KM_TO_MI = 0.621371192; public static final double KM_TO_MI = 0.621371192;
@@ -575,12 +575,13 @@ public class TripStatistics implements Parcelable {
/** /**
* Adds calorie value. * 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) { @SuppressWarnings("hiding")
this.calorie += calorieAdded; public void addCalorie(double calorie) {
this.calorie += calorie;
} }
/** /**
* Sets calorie value. * Sets calorie value.
* *
@@ -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.getVo2(CalorieUtils.CRTICAL_SPEED_RUNNING * 2, grade);
double expected = CalorieUtils.calculateRunningVO2(CalorieUtils.CRTICAL_SPEED_RUNNING * 2, double expected = CalorieUtils.calculateRunningVo2(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.getVo2(footSpeed, grade);
double expected = CalorieUtils.calculateWalkingVO2(footSpeed, grade); double expected = CalorieUtils.calculateWalkingVo2(footSpeed, grade);
assertEquals(expected, actual); assertEquals(expected, actual);
} }