forked from upstream-mirrors/OpenTracks
Change code style about code of calorie calculation.
This commit is contained in:
@@ -138,7 +138,7 @@ limitations under the License.
|
||||
android:id="@+id/stats_calorie_container"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="match_parent" >
|
||||
<!-- Calories value -->
|
||||
<!-- calorie value -->
|
||||
<include
|
||||
android:id="@+id/stats_calorie"
|
||||
layout="@layout/stats_small_item" />
|
||||
|
||||
@@ -2112,9 +2112,9 @@ limitations under the License.
|
||||
Elevation
|
||||
</string>
|
||||
<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
|
||||
</string>
|
||||
<string name="stats_fastest_pace"
|
||||
|
||||
@@ -132,7 +132,7 @@ public class MyTracksProvider extends ContentProvider {
|
||||
}
|
||||
|
||||
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
|
||||
+ " INTEGER");
|
||||
}
|
||||
|
||||
@@ -94,6 +94,9 @@ 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.
|
||||
private int defaultWeight = 65;
|
||||
|
||||
/**
|
||||
* Creates a new trip statistics updater.
|
||||
*
|
||||
@@ -195,7 +198,7 @@ public class TripStatisticsUpdater {
|
||||
|
||||
// Update calorie
|
||||
double calorie = CalorieUtils.getCalories(lastMovingLocation, location,
|
||||
gradeBuffer.getAverage(), 65, ActivityType.FOOT);
|
||||
gradeBuffer.getAverage(), defaultWeight, ActivityType.FOOT);
|
||||
currentSegment.addCalorie(calorie);
|
||||
|
||||
lastLocation = location;
|
||||
|
||||
@@ -35,36 +35,35 @@ public class CalorieUtils {
|
||||
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 MILLISECOND_TO_SECOND = 1000.0;
|
||||
private static final double MILLILITER_TO_LITER = 1000.0;
|
||||
private static final double SECOND_TO_MINUTE = 60.0;
|
||||
private static final double L_TO_KCAL = 5;
|
||||
|
||||
/**
|
||||
* 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
|
||||
static final double CRTICAL_SPEED_RUNNING = 4.5 * UnitConversions.MI_TO_KM * 1000
|
||||
/ SECOND_TO_MINUTE;
|
||||
static final double CRTICAL_SPEED_RUNNING = 4.5 * UnitConversions.MI_TO_KM
|
||||
* UnitConversions.KM_TO_M * UnitConversions.MIN_TO_HR;
|
||||
|
||||
public enum ActivityType {
|
||||
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
|
||||
* (mph).
|
||||
*
|
||||
* @param speed is calculated in meters per minute (m/min)
|
||||
* @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) {
|
||||
// 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
|
||||
* 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
|
||||
* truly jogging).
|
||||
*
|
||||
* @param speed is calculated in meters per minute (m/min)
|
||||
* @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) {
|
||||
|
||||
// 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
|
||||
* 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
|
||||
* <ul>
|
||||
* <li>P - Power in watts(Joule/second)</li>
|
||||
@@ -117,8 +117,17 @@ public class CalorieUtils {
|
||||
*/
|
||||
@VisibleForTesting
|
||||
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
|
||||
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
|
||||
return power * timeUsed / UnitConversions.KCAL_TO_J;
|
||||
}
|
||||
@@ -135,8 +144,8 @@ public class CalorieUtils {
|
||||
@VisibleForTesting
|
||||
static double calculateExpenditureCycling(Location start, Location stop, double grade, int weight) {
|
||||
// Gets time in seconds
|
||||
double time = (double) (stop.getTime() - start.getTime()) / MILLISECOND_TO_SECOND;
|
||||
// Meters per second
|
||||
double time = (double) (stop.getTime() - start.getTime()) * UnitConversions.MS_TO_S;
|
||||
// Get speed in meters per second
|
||||
double speed = (start.getSpeed() + stop.getSpeed()) / 2.0;
|
||||
if (grade < 0) {
|
||||
grade = 0.0;
|
||||
@@ -156,24 +165,20 @@ public class CalorieUtils {
|
||||
*/
|
||||
@VisibleForTesting
|
||||
static double calculateExpenditureFoot(Location start, Location stop, double grade, int weight) {
|
||||
// Meters per minute
|
||||
double averageSpeed = (start.getSpeed() + stop.getSpeed()) * SECOND_TO_MINUTE / 2.0;
|
||||
// Get VO2
|
||||
// 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);
|
||||
// Seconds
|
||||
double time = (double) (stop.getTime() - start.getTime()) / MILLISECOND_TO_SECOND;
|
||||
// Change mL/kg/min to mL/kg
|
||||
double VO2All_mL = VO2 * time / SECOND_TO_MINUTE;
|
||||
// Change mL/kg to L/kg
|
||||
double VO2All_L = VO2All_mL / MILLILITER_TO_LITER;
|
||||
// Get the calorie
|
||||
return VO2All_L * weight * VO2H_TO_KCAL;
|
||||
// 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the VO2 value.
|
||||
*
|
||||
* @param speed in meters per minute
|
||||
* @param speed in meters per second
|
||||
* @param grade the grade to calculate
|
||||
* @return the VO2 value.
|
||||
*/
|
||||
|
||||
@@ -40,6 +40,9 @@ 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() {}
|
||||
|
||||
/**
|
||||
@@ -331,6 +334,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), null);
|
||||
String.format(Locale.getDefault(), CALORIES_FORMAT, calorie), CALORIES_UNIT);
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,21 @@ package com.google.android.apps.mytracks.util;
|
||||
*/
|
||||
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() {}
|
||||
|
||||
// multiplication factor to convert kilometers to miles
|
||||
@@ -39,6 +54,8 @@ public class UnitConversions {
|
||||
// multiplication factor to convert meters to kilometers
|
||||
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
|
||||
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
|
||||
public static final String SHAREDOWNER = "sharedOwner";
|
||||
|
||||
// Calorie expenditure of the track
|
||||
public static final String CALORIE = "calories";
|
||||
// Calorie burned of the track
|
||||
public static final String CALORIE = "calorie";
|
||||
|
||||
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" // table
|
||||
+ _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " // id
|
||||
@@ -124,7 +124,7 @@ public interface TracksColumns extends BaseColumns {
|
||||
+ MODIFIEDTIME + " INTEGER, " // modified time
|
||||
+ SHAREDWITHME + " INTEGER, " // shared with me
|
||||
+ SHAREDOWNER + " STRING, " // shared owner
|
||||
+ CALORIE + " INTEGER);"; // calories
|
||||
+ CALORIE + " INTEGER);"; // calorie
|
||||
|
||||
public static final String[] COLUMNS = { _ID, // id
|
||||
NAME, // name
|
||||
@@ -157,7 +157,7 @@ public interface TracksColumns extends BaseColumns {
|
||||
MODIFIEDTIME, // modified time
|
||||
SHAREDWITHME, // shared with me
|
||||
SHAREDOWNER,// shared owner
|
||||
CALORIE }; // calories
|
||||
CALORIE }; // calorie
|
||||
|
||||
public static final byte[] COLUMN_TYPES = { ContentTypeIds.LONG_TYPE_ID, // id
|
||||
ContentTypeIds.STRING_TYPE_ID, // name
|
||||
@@ -190,6 +190,6 @@ public interface TracksColumns extends BaseColumns {
|
||||
ContentTypeIds.LONG_TYPE_ID, // modified time
|
||||
ContentTypeIds.BOOLEAN_TYPE_ID, // shared with me
|
||||
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.
|
||||
private final ExtremityMonitor gradeExtremities = new ExtremityMonitor();
|
||||
|
||||
// The calorie expenditure of current track.
|
||||
private double calorie = 0;
|
||||
// The calorie of current track.
|
||||
private double calorie;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
@@ -493,7 +493,7 @@ public class TripStatistics implements Parcelable {
|
||||
+ "; Max Longitude: " + getRightDegrees() + "; Max Speed: " + getMaxSpeed()
|
||||
+ "; Min Elevation: " + getMinElevation() + "; Max Elevation: " + getMaxElevation()
|
||||
+ "; 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();
|
||||
data.gradeExtremities.set(minGrade, maxGrade);
|
||||
|
||||
double calorie = source.readDouble();
|
||||
data.setCalorie(calorie);
|
||||
data.calorie = source.readDouble();
|
||||
|
||||
return data;
|
||||
}
|
||||
@@ -576,7 +575,7 @@ public class TripStatistics implements Parcelable {
|
||||
/**
|
||||
* Adds calorie value.
|
||||
*
|
||||
* @param calorieAdded
|
||||
* @param calorieAdded add the value to the total calorie
|
||||
*/
|
||||
public void addCalorie(double calorieAdded) {
|
||||
this.calorie += calorieAdded;
|
||||
@@ -584,9 +583,11 @@ public class TripStatistics implements Parcelable {
|
||||
|
||||
/**
|
||||
* Sets calorie value.
|
||||
*
|
||||
* @param calorie the new value of calorie
|
||||
*/
|
||||
public void setCalorie(double newCalorie) {
|
||||
calorie = newCalorie;
|
||||
public void setCalorie(double calorie) {
|
||||
this.calorie = calorie;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user