Change code style about code of calorie calculation.

This commit is contained in:
youtaol
2013-08-20 11:06:37 +08:00
parent cbd6d8158a
commit d16949c139
9 changed files with 78 additions and 49 deletions
+1 -1
View File
@@ -138,7 +138,7 @@ limitations under the License.
android:id="@+id/stats_calorie_container" android:id="@+id/stats_calorie_container"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_width="match_parent" > android:layout_width="match_parent" >
<!-- Calories value --> <!-- calorie value -->
<include <include
android:id="@+id/stats_calorie" android:id="@+id/stats_calorie"
layout="@layout/stats_small_item" /> layout="@layout/stats_small_item" />
+2 -2
View File
@@ -2112,9 +2112,9 @@ limitations under the License.
Elevation Elevation
</string> </string>
<string name="stats_calorie" <string name="stats_calorie"
translation_description="In the 'Stats' page, the calorie expediture of track. translation_description="In the 'Stats' page, the calorie burned.
Character limit: ~23 (1 line)"> Character limit: ~23 (1 line)">
Calorie expediture Calorie expediture
</string> </string>
<string name="stats_fastest_pace" <string name="stats_fastest_pace"
@@ -132,7 +132,7 @@ public class MyTracksProvider extends ContentProvider {
} }
if (oldVersion <= 22) { if (oldVersion <= 22) {
Log.w(TAG, "Upgrade DB: Adding track calories 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,6 +94,9 @@ 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.
private int defaultWeight = 65;
/** /**
* Creates a new trip statistics updater. * Creates a new trip statistics updater.
* *
@@ -195,7 +198,7 @@ public class TripStatisticsUpdater {
// Update calorie // Update calorie
double calorie = CalorieUtils.getCalories(lastMovingLocation, location, double calorie = CalorieUtils.getCalories(lastMovingLocation, location,
gradeBuffer.getAverage(), 65, ActivityType.FOOT); gradeBuffer.getAverage(), defaultWeight, ActivityType.FOOT);
currentSegment.addCalorie(calorie); currentSegment.addCalorie(calorie);
lastLocation = location; lastLocation = location;
@@ -35,36 +35,35 @@ public class CalorieUtils {
private static final double RESTING_VO2 = 3.5; private static final double RESTING_VO2 = 3.5;
/** /**
* Ratio of change vo2 to kcal/L * Ratio of change Liter to kcal. In kcal per Liter.
*/ */
private static final double VO2H_TO_KCAL = 5; private static final double L_TO_KCAL = 5;
private static final double MILLISECOND_TO_SECOND = 1000.0;
private static final double MILLILITER_TO_LITER = 1000.0;
private static final double SECOND_TO_MINUTE = 60.0;
/** /**
* Changes 4.5 miles per hour to meters per minutes. * Critical speed running in meters per minute. Changes 4.5 miles per hour to
* meters per minute.
*/ */
@VisibleForTesting @VisibleForTesting
static final double CRTICAL_SPEED_RUNNING = 4.5 * UnitConversions.MI_TO_KM * 1000 static final double CRTICAL_SPEED_RUNNING = 4.5 * UnitConversions.MI_TO_KM
/ SECOND_TO_MINUTE; * UnitConversions.KM_TO_M * UnitConversions.MIN_TO_HR;
public enum ActivityType { public enum ActivityType {
CYCLING, FOOT CYCLING, FOOT
} }
/** /**
* Calculates the calorie expenditure of walking. This equation is appropriate * Calculates the calorie of walking. This equation is appropriate
* for fairly slow speed ranges—from 1.9 to approximately 4 miles per hour * for fairly slow speed ranges—from 1.9 to approximately 4 miles per hour
* (mph). * (mph).
* *
* @param speed is calculated in meters per minute (m/min) * @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
speed *= UnitConversions.MIN_TO_SECOND;
/* /*
* 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
@@ -74,17 +73,18 @@ public class CalorieUtils {
} }
/** /**
* Calculates the calorie expenditure of running. This equation is appropriate * 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 * for speeds greater than 5.0 mph (or 3.0 mph or greater if the subject is
* truly jogging). * truly jogging).
* *
* @param speed is calculated in meters per minute (m/min) * @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
speed *= UnitConversions.MIN_TO_SECOND;
/* /*
* 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
@@ -94,7 +94,7 @@ public class CalorieUtils {
} }
/** /**
* Calculates the calorie expenditure of cycling. Use below equation: <br> * Calculates the calorie of cycling. Use below equation: <br>
* P = g * m * Vg * (K1 + s) + K2 * (Va)^2 * Vg * P = g * m * Vg * (K1 + s) + K2 * (Va)^2 * Vg
* <ul> * <ul>
* <li>P - Power in watts(Joule/second)</li> * <li>P - Power in watts(Joule/second)</li>
@@ -117,8 +117,17 @@ public class CalorieUtils {
*/ */
@VisibleForTesting @VisibleForTesting
static double calculateCyclingCalories(double speed, double grade, int weight, double timeUsed) { static double calculateCyclingCalories(double speed, double grade, int weight, double timeUsed) {
double earthGravity = 9.8;
// Lumped constant for all frictional losses (tires, bearings, chain)
double K1 = 0.0053;
// Lumped constant for aerodynamic drag
double K2 = 0.185;
// Get the Power // Get the Power
double power = 9.8 * weight * speed * (0.0053 + grade) + 0.185 * (speed * speed * speed); double power = earthGravity * weight * speed * (K1 + grade) + K2 * (speed * speed * speed);
// Get the calories // Get the calories
return power * timeUsed / UnitConversions.KCAL_TO_J; return power * timeUsed / UnitConversions.KCAL_TO_J;
} }
@@ -135,8 +144,8 @@ public class CalorieUtils {
@VisibleForTesting @VisibleForTesting
static double calculateExpenditureCycling(Location start, Location stop, double grade, int weight) { static double calculateExpenditureCycling(Location start, Location stop, double grade, int weight) {
// Gets time in seconds // Gets time in seconds
double time = (double) (stop.getTime() - start.getTime()) / MILLISECOND_TO_SECOND; double time = (double) (stop.getTime() - start.getTime()) * UnitConversions.MS_TO_S;
// Meters per second // Get speed in meters per second
double speed = (start.getSpeed() + stop.getSpeed()) / 2.0; double speed = (start.getSpeed() + stop.getSpeed()) / 2.0;
if (grade < 0) { if (grade < 0) {
grade = 0.0; grade = 0.0;
@@ -156,24 +165,20 @@ public class CalorieUtils {
*/ */
@VisibleForTesting @VisibleForTesting
static double calculateExpenditureFoot(Location start, Location stop, double grade, int weight) { static double calculateExpenditureFoot(Location start, Location stop, double grade, int weight) {
// Meters per minute // Get speed in meters per second
double averageSpeed = (start.getSpeed() + stop.getSpeed()) * SECOND_TO_MINUTE / 2.0; double averageSpeed = (start.getSpeed() + stop.getSpeed()) / 2.0;
// Get VO2 // Get VO2 in mL/kg/min
double VO2 = getVO2(averageSpeed, grade); double VO2 = getVO2(averageSpeed, grade);
// Seconds // Minutes
double time = (double) (stop.getTime() - start.getTime()) / MILLISECOND_TO_SECOND; double time = (double) (stop.getTime() - start.getTime()) * UnitConversions.MS_TO_S * UnitConversions.S_TO_MIN;
// Change mL/kg/min to mL/kg // Get the calorie. The unit of calorie is kcal which is came from mL/kg/min * min * L/mL * kg * kcal/L
double VO2All_mL = VO2 * time / SECOND_TO_MINUTE; return VO2 * time * UnitConversions.ML_TO_L * weight * L_TO_KCAL;
// Change mL/kg to L/kg
double VO2All_L = VO2All_mL / MILLILITER_TO_LITER;
// Get the calorie
return VO2All_L * weight * VO2H_TO_KCAL;
} }
/** /**
* Gets the VO2 value. * Gets the VO2 value.
* *
* @param speed in meters per minute * @param speed in meters per second
* @param grade the grade to calculate * @param grade the grade to calculate
* @return the VO2 value. * @return the VO2 value.
*/ */
@@ -40,6 +40,9 @@ 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() {}
/** /**
@@ -331,6 +334,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), null); String.format(Locale.getDefault(), CALORIES_FORMAT, calorie), CALORIES_UNIT);
} }
} }
@@ -22,6 +22,21 @@ package com.google.android.apps.mytracks.util;
*/ */
public class UnitConversions { public class UnitConversions {
// Milliliter to liter
static final double ML_TO_L = 1 / 1000.0;
// Millisecond to second
static final double MS_TO_S = 1 / 1000.0;
// Minute to second
static final double MIN_TO_SECOND = 60.0;
// Second to minute
static final double S_TO_MIN = 1 / MIN_TO_SECOND;
// Minute to hour
static final double MIN_TO_HR = 1 / 60.0;
private UnitConversions() {} private UnitConversions() {}
// multiplication factor to convert kilometers to miles // multiplication factor to convert kilometers to miles
@@ -39,6 +54,8 @@ public class UnitConversions {
// multiplication factor to convert meters to kilometers // multiplication factor to convert meters to kilometers
public static final double M_TO_KM = 1 / 1000.0; public static final double M_TO_KM = 1 / 1000.0;
public static final double KM_TO_M = 1 / M_TO_KM;
// multiplication factor to convert meters per second to kilometers per hour // multiplication factor to convert meters per second to kilometers per hour
public static final double MS_TO_KMH = 3.6; public static final double MS_TO_KMH = 3.6;
@@ -89,8 +89,8 @@ public interface TracksColumns extends BaseColumns {
// The owner of the shared with me track // The owner of the shared with me track
public static final String SHAREDOWNER = "sharedOwner"; public static final String SHAREDOWNER = "sharedOwner";
// Calorie expenditure of the track // Calorie burned of the track
public static final String CALORIE = "calories"; public static final String CALORIE = "calorie";
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" // table public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" // table
+ _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " // id + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " // id
@@ -124,7 +124,7 @@ public interface TracksColumns extends BaseColumns {
+ MODIFIEDTIME + " INTEGER, " // modified time + MODIFIEDTIME + " INTEGER, " // modified time
+ SHAREDWITHME + " INTEGER, " // shared with me + SHAREDWITHME + " INTEGER, " // shared with me
+ SHAREDOWNER + " STRING, " // shared owner + SHAREDOWNER + " STRING, " // shared owner
+ CALORIE + " INTEGER);"; // calories + CALORIE + " INTEGER);"; // calorie
public static final String[] COLUMNS = { _ID, // id public static final String[] COLUMNS = { _ID, // id
NAME, // name NAME, // name
@@ -157,7 +157,7 @@ public interface TracksColumns extends BaseColumns {
MODIFIEDTIME, // modified time MODIFIEDTIME, // modified time
SHAREDWITHME, // shared with me SHAREDWITHME, // shared with me
SHAREDOWNER,// shared owner SHAREDOWNER,// shared owner
CALORIE }; // calories CALORIE }; // calorie
public static final byte[] COLUMN_TYPES = { ContentTypeIds.LONG_TYPE_ID, // id public static final byte[] COLUMN_TYPES = { ContentTypeIds.LONG_TYPE_ID, // id
ContentTypeIds.STRING_TYPE_ID, // name ContentTypeIds.STRING_TYPE_ID, // name
@@ -190,6 +190,6 @@ public interface TracksColumns extends BaseColumns {
ContentTypeIds.LONG_TYPE_ID, // modified time ContentTypeIds.LONG_TYPE_ID, // modified time
ContentTypeIds.BOOLEAN_TYPE_ID, // shared with me ContentTypeIds.BOOLEAN_TYPE_ID, // shared with me
ContentTypeIds.STRING_TYPE_ID, // shared owner ContentTypeIds.STRING_TYPE_ID, // shared owner
ContentTypeIds.INT_TYPE_ID // calories ContentTypeIds.INT_TYPE_ID // calorie
}; };
} }
@@ -62,8 +62,8 @@ public class TripStatistics implements Parcelable {
// The min and max grade seen on this trip. // The min and max grade seen on this trip.
private final ExtremityMonitor gradeExtremities = new ExtremityMonitor(); private final ExtremityMonitor gradeExtremities = new ExtremityMonitor();
// The calorie expenditure of current track. // The calorie of current track.
private double calorie = 0; private double calorie;
/** /**
* Default constructor. * Default constructor.
@@ -493,7 +493,7 @@ public class TripStatistics implements Parcelable {
+ "; Max Longitude: " + getRightDegrees() + "; Max Speed: " + getMaxSpeed() + "; Max Longitude: " + getRightDegrees() + "; Max Speed: " + getMaxSpeed()
+ "; Min Elevation: " + getMinElevation() + "; Max Elevation: " + getMaxElevation() + "; Min Elevation: " + getMinElevation() + "; Max Elevation: " + getMaxElevation()
+ "; Elevation Gain: " + getTotalElevationGain() + "; Min Grade: " + getMinGrade() + "; Elevation Gain: " + getTotalElevationGain() + "; Min Grade: " + getMinGrade()
+ "; Max Grade: " + getMaxGrade() + "; Calorie expenditure: " + getCalorie() + "; Max Grade: " + getMaxGrade() + "; Calorie: " + getCalorie()
+ "}"; + "}";
} }
@@ -531,8 +531,7 @@ public class TripStatistics implements Parcelable {
double maxGrade = source.readDouble(); double maxGrade = source.readDouble();
data.gradeExtremities.set(minGrade, maxGrade); data.gradeExtremities.set(minGrade, maxGrade);
double calorie = source.readDouble(); data.calorie = source.readDouble();
data.setCalorie(calorie);
return data; return data;
} }
@@ -576,7 +575,7 @@ public class TripStatistics implements Parcelable {
/** /**
* Adds calorie value. * Adds calorie value.
* *
* @param calorieAdded * @param calorieAdded add the value to the total calorie
*/ */
public void addCalorie(double calorieAdded) { public void addCalorie(double calorieAdded) {
this.calorie += calorieAdded; this.calorie += calorieAdded;
@@ -584,9 +583,11 @@ public class TripStatistics implements Parcelable {
/** /**
* Sets calorie value. * Sets calorie value.
*
* @param calorie the new value of calorie
*/ */
public void setCalorie(double newCalorie) { public void setCalorie(double calorie) {
calorie = newCalorie; this.calorie = calorie;
} }
/** /**