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.
Character limit: ~23 (1 line)">
Calorie expediture
Calorie expenditure
</string>
<string name="stats_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'.">
min/mi
</string>
<string name="unit_calorie"
translation_description="Abbreviation for calorie. E.g., '5 Cal'.">
Cal
</string>
<!-- Value -->
<string name="value_adapt_accuracy"
@@ -49,7 +49,7 @@ import java.util.ArrayList;
public class MyTracksProvider extends ContentProvider {
private static final String TAG = MyTracksProvider.class.getSimpleName();
private static final int DATABASE_VERSION = 23;
private static final int DATABASE_VERSION = 22;
@VisibleForTesting
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.");
db.execSQL("ALTER TABLE " + WaypointsColumns.TABLE_NAME + " ADD " + WaypointsColumns.PHOTOURL
+ " STRING");
}
if (oldVersion <= 22) {
Log.w(TAG, "Upgrade DB: Adding track calorie column.");
db.execSQL("ALTER TABLE " + TracksColumns.TABLE_NAME + " ADD " + TracksColumns.CALORIE
+ " INTEGER");
@@ -94,7 +94,7 @@ public class TripStatisticsUpdater {
// A buffer of the recent speed readings (m/s) for calculating max speed
private final DoubleBuffer speedBuffer = new DoubleBuffer(SPEED_SMOOTHING_FACTOR);
// Average weight.
// Default weight, in kilogram
private int defaultWeight = 65;
/**
@@ -40,30 +40,33 @@ public class CalorieUtils {
private static final double L_TO_KCAL = 5;
/**
* Critical speed running in meters per minute. Changes 4.5 miles per hour to
* meters per minute.
* Critical speed running in meters per minute. Converts 4.5 miles per hour to
* 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
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);
}
@@ -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));
}
}
@@ -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;
@@ -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.
*
@@ -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);
}