From d16949c139142cc2038f4b195ed0697e5d1eecf9 Mon Sep 17 00:00:00 2001 From: youtaol Date: Tue, 20 Aug 2013 11:06:37 +0800 Subject: [PATCH] Change code style about code of calorie calculation. --- MyTracks/res/layout/stats_content.xml | 2 +- MyTracks/res/values/strings.xml | 4 +- .../mytracks/content/MyTracksProvider.java | 2 +- .../mytracks/stats/TripStatisticsUpdater.java | 5 +- .../apps/mytracks/util/CalorieUtils.java | 65 ++++++++++--------- .../apps/mytracks/util/StatsUtils.java | 5 +- .../apps/mytracks/util/UnitConversions.java | 17 +++++ .../apps/mytracks/content/TracksColumns.java | 10 +-- .../apps/mytracks/stats/TripStatistics.java | 17 ++--- 9 files changed, 78 insertions(+), 49 deletions(-) diff --git a/MyTracks/res/layout/stats_content.xml b/MyTracks/res/layout/stats_content.xml index dd8fb3ca7..9fa4abfff 100644 --- a/MyTracks/res/layout/stats_content.xml +++ b/MyTracks/res/layout/stats_content.xml @@ -138,7 +138,7 @@ limitations under the License. android:id="@+id/stats_calorie_container" android:layout_height="wrap_content" android:layout_width="match_parent" > - + diff --git a/MyTracks/res/values/strings.xml b/MyTracks/res/values/strings.xml index 2ef5b4cfe..051c77225 100644 --- a/MyTracks/res/values/strings.xml +++ b/MyTracks/res/values/strings.xml @@ -2112,9 +2112,9 @@ limitations under the License. Elevation + Character limit: ~23 (1 line)"> Calorie expediture + * Calculates the calorie of cycling. Use below equation:
* P = g * m * Vg * (K1 + s) + K2 * (Va)^2 * Vg *
    *
  • P - Power in watts(Joule/second)
  • @@ -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. */ 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 15ada089f..760518662 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/StatsUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/StatsUtils.java @@ -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); } } \ 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 be8055138..de6453120 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/UnitConversions.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/UnitConversions.java @@ -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; diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/content/TracksColumns.java b/MyTracksLib/src/com/google/android/apps/mytracks/content/TracksColumns.java index 34b901a13..4535da26a 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/content/TracksColumns.java +++ b/MyTracksLib/src/com/google/android/apps/mytracks/content/TracksColumns.java @@ -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 }; } 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 1adeb7db2..144182363 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/stats/TripStatistics.java +++ b/MyTracksLib/src/com/google/android/apps/mytracks/stats/TripStatistics.java @@ -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; } /**