Change code style.

This commit is contained in:
youtaol
2013-08-19 11:30:56 +08:00
parent 4cf647b02c
commit cbd6d8158a
11 changed files with 97 additions and 85 deletions
+5 -6
View File
@@ -129,19 +129,18 @@ limitations under the License.
android:id="@+id/stats_longitude"
layout="@layout/stats_small_item" />
</LinearLayout>
<!-- Row 7: calories horizontal line -->
<!-- Row 7: calorie horizontal line -->
<View
android:id="@+id/stats_calories_horizontal_line"
android:id="@+id/stats_calorie_horizontal_line"
style="@style/StatsHorizontalLine" />
<!-- Row 7: calories -->
<!-- Row 7: calorie -->
<LinearLayout
android:id="@+id/stats_calories_container"
android:id="@+id/stats_calorie_container"
android:layout_height="wrap_content"
android:layout_width="match_parent" >
<!-- Calories value -->
<include
android:id="@+id/stats_calories"
android:id="@+id/stats_calorie"
layout="@layout/stats_small_item" />
</LinearLayout>
</merge>
+5 -3
View File
@@ -2111,9 +2111,11 @@ limitations under the License.
Character limit: ~23 (1 line)">
Elevation
</string>
<string name="stats_calories"
translation_description="In the 'Stats' page, the calories expediture of track.">
Calories expediture
<string name="stats_calorie"
translation_description="In the 'Stats' page, the calorie expediture of track.
Character limit: ~23 (1 line)">
Calorie expediture
</string>
<string name="stats_fastest_pace"
translation_description="In the 'Stats' page, the fastest pace.">
@@ -133,7 +133,7 @@ public class MyTracksProvider extends ContentProvider {
if (oldVersion <= 22) {
Log.w(TAG, "Upgrade DB: Adding track calories column.");
db.execSQL("ALTER TABLE " + TracksColumns.TABLE_NAME + " ADD " + TracksColumns.CALORIES
db.execSQL("ALTER TABLE " + TracksColumns.TABLE_NAME + " ADD " + TracksColumns.CALORIE
+ " INTEGER");
}
}
@@ -193,9 +193,10 @@ public class TripStatisticsUpdater {
// TODO 1: How to get the value of weight.
// TODO 2: How to get the value of ActivityType.
double value = CalorieUtils.getCalories(lastMovingLocation, location,
// Update calorie
double calorie = CalorieUtils.getCalories(lastMovingLocation, location,
gradeBuffer.getAverage(), 65, ActivityType.FOOT);
currentSegment.addCaloticExpenditure(value);
currentSegment.addCalorie(calorie);
lastLocation = location;
lastMovingLocation = location;
@@ -39,11 +39,16 @@ public class CalorieUtils {
*/
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;
/**
* Changes 4.5 miles per hour to meters per minutes.
*/
@VisibleForTesting
static final double CRTICAL_SPEED_RUNNING = 4.5 * UnitConversions.MI_TO_KM * 1000 / 60;
static final double CRTICAL_SPEED_RUNNING = 4.5 * UnitConversions.MI_TO_KM * 1000
/ SECOND_TO_MINUTE;
public enum ActivityType {
CYCLING, FOOT
@@ -56,7 +61,7 @@ public class CalorieUtils {
*
* @param speed is calculated in meters per minute (m/min)
* @param grade
* @return the VO2 value in ml/kg/min
* @return the VO2 value in ml/kg/min.
*/
@VisibleForTesting
static double calculateWalkingVO2(double speed, double grade) {
@@ -75,7 +80,7 @@ public class CalorieUtils {
*
* @param speed is calculated in meters per minute (m/min)
* @param grade
* @return the VO2 value in ml/kg/min
* @return the VO2 value in ml/kg/min.
*/
@VisibleForTesting
static double calculateRunningVO2(double speed, double grade) {
@@ -108,13 +113,13 @@ public class CalorieUtils {
* @param grade the grade to calculate
* @param weight of rider plus bike
* @param timeUsed how many times used in second
* @return the power value watts(Joule/second)
* @return the power value watts(Joule/second).
*/
@VisibleForTesting
static double calculateCyclingCalories(double speed, double grade, int weight, double timeUsed) {
// Get the Power.
// Get the Power
double power = 9.8 * weight * speed * (0.0053 + grade) + 0.185 * (speed * speed * speed);
// Get the calories.
// Get the calories
return power * timeUsed / UnitConversions.KCAL_TO_J;
}
@@ -125,16 +130,16 @@ public class CalorieUtils {
* @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
* @return the calories expenditure between the start and stop location.
*/
@VisibleForTesting
static double calculateExpenditureCycling(Location start, Location stop, double grade, int weight) {
// Seconds.
double time = (double) (stop.getTime() - start.getTime()) / 1000;
// Meter per second.
double speed = (start.getSpeed() + stop.getSpeed()) / 2;
// Gets time in seconds
double time = (double) (stop.getTime() - start.getTime()) / MILLISECOND_TO_SECOND;
// Meters per second
double speed = (start.getSpeed() + stop.getSpeed()) / 2.0;
if (grade < 0) {
grade = 0;
grade = 0.0;
}
return calculateCyclingCalories(speed, grade, weight, time);
@@ -147,45 +152,39 @@ public class CalorieUtils {
* @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
* @return the calories expenditure between the start and stop location.
*/
@VisibleForTesting
static double calculateExpenditureFoot(Location start, Location stop, double grade, int weight) {
// Meter per minute.
double speed = (start.getSpeed() + stop.getSpeed()) * 60 / 2;
// Meters per minute
double averageSpeed = (start.getSpeed() + stop.getSpeed()) * SECOND_TO_MINUTE / 2.0;
// Get VO2
double VO2 = getVO2(speed, grade);
// Seconds.
double time = (double) (stop.getTime() - start.getTime()) / 1000;
// Change mL/kg/min to mL/kg.
double VO2All = VO2 * time / 60;
// Change mL/kg to L/kg.
VO2All = VO2All / 1000;
// Get the calorie.
return VO2All * weight * VO2H_TO_KCAL;
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;
}
/**
* Gets the VO2 value.
*
* @param start the start location
* @param stop the stop location
* @param speed in meters per minute
* @param grade the grade to calculate
* @return the VO2 value
* @return the VO2 value.
*/
@VisibleForTesting
static double getVO2(double speed, double grade) {
if (grade < 0) {
grade = 0;
grade = 0.0;
}
double VO2 = 0;
if (speed > CRTICAL_SPEED_RUNNING) {
VO2 = calculateRunningVO2(speed, grade);
} else {
VO2 = calculateWalkingVO2(speed, grade);
}
return VO2;
return speed > CRTICAL_SPEED_RUNNING ? calculateRunningVO2(speed, grade) : calculateWalkingVO2(
speed, grade);
}
/**
@@ -196,7 +195,7 @@ public class CalorieUtils {
* @param grade the grade to calculate
* @param weight the weight of user
* @param activityType can be foot or cycling
* @return the calories expenditure between the start and stop location
* @return the calories expenditure between the start and stop location.
*/
public static double getCalories(Location start, Location stop, double grade, int weight,
ActivityType activityType) {
@@ -177,8 +177,8 @@ public class StatsUtils {
}
// Set calories
double calories = tripStatistics == null ? Double.NaN : tripStatistics.getCaloricExpenditure();
setCalories(activity, R.id.stats_calories, R.string.stats_calories, calories);
double calories = tripStatistics == null ? Double.NaN : tripStatistics.getCalorie();
setCalorie(activity, R.id.stats_calorie, R.string.stats_calorie, calories);
}
/**
@@ -322,18 +322,15 @@ public class StatsUtils {
}
/**
* Sets calories.
* Sets calorie.
*
* @param activity the activity
* @param itemId the item id
* @param speedLabelId the speed label id
* @param paceLabelId the pace label id
* @param speed the speed in meters per second
* @param metricUnits true if metric units
* @param reportSpeed true if report speed
* @param labelId the calorie label id
* @param calorie the value of calorie
*/
private static void setCalories(Activity activity, int itemId, int labelId, double calories) {
private static void setCalorie(Activity activity, int itemId, int labelId, double calorie) {
setItem(activity, itemId, labelId,
String.format(Locale.getDefault(), CALORIES_FORMAT, calories), "");
String.format(Locale.getDefault(), CALORIES_FORMAT, calorie), null);
}
}
@@ -87,7 +87,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
int modifiedTimeIndex = cursor.getColumnIndexOrThrow(TracksColumns.MODIFIEDTIME);
int sharedWithMeIndex = cursor.getColumnIndexOrThrow(TracksColumns.SHAREDWITHME);
int sharedOwnerIndex = cursor.getColumnIndexOrThrow(TracksColumns.SHAREDOWNER);
int caloriesIndex = cursor.getColumnIndexOrThrow(TracksColumns.CALORIES);
int caloriesIndex = cursor.getColumnIndexOrThrow(TracksColumns.CALORIE);
Track track = new Track();
TripStatistics tripStatistics = track.getTripStatistics();
@@ -154,7 +154,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
tripStatistics.setMaxGrade(cursor.getFloat(maxGradeIndex));
}
if (!cursor.isNull(caloriesIndex)) {
tripStatistics.setCaloricExpenditure(cursor.getFloat(caloriesIndex));
tripStatistics.setCalorie(cursor.getFloat(caloriesIndex));
}
if (!cursor.isNull(iconIndex)) {
track.setIcon(cursor.getString(iconIndex));
@@ -320,7 +320,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils {
values.put(TracksColumns.MODIFIEDTIME, track.getModifiedTime());
values.put(TracksColumns.SHAREDWITHME, track.isSharedWithMe());
values.put(TracksColumns.SHAREDOWNER, track.getSharedOwner());
values.put(TracksColumns.CALORIES, tripStatistics.getCaloricExpenditure());
values.put(TracksColumns.CALORIE, tripStatistics.getCalorie());
return values;
}
@@ -66,7 +66,6 @@ public interface TracksColumns extends BaseColumns {
public static final String MINLON = "minlon"; // minimum longitude
public static final String MAXLON = "maxlon"; // maximum longitude
public static final String AVGSPEED = "avgspeed"; // average speed
public static final String CALORIES = "calories"; // calories
// average moving speed
public static final String AVGMOVINGSPEED = "avgmovingspeed";
@@ -89,6 +88,9 @@ 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";
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" // table
+ _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " // id
@@ -122,7 +124,7 @@ public interface TracksColumns extends BaseColumns {
+ MODIFIEDTIME + " INTEGER, " // modified time
+ SHAREDWITHME + " INTEGER, " // shared with me
+ SHAREDOWNER + " STRING, " // shared owner
+ CALORIES + " INTEGER);"; // calories
+ CALORIE + " INTEGER);"; // calories
public static final String[] COLUMNS = { _ID, // id
NAME, // name
@@ -155,7 +157,7 @@ public interface TracksColumns extends BaseColumns {
MODIFIEDTIME, // modified time
SHAREDWITHME, // shared with me
SHAREDOWNER,// shared owner
CALORIES }; // calories
CALORIE }; // calories
public static final byte[] COLUMN_TYPES = { ContentTypeIds.LONG_TYPE_ID, // id
ContentTypeIds.STRING_TYPE_ID, // name
@@ -63,7 +63,7 @@ public class TripStatistics implements Parcelable {
private final ExtremityMonitor gradeExtremities = new ExtremityMonitor();
// The calorie expenditure of current track.
private double caloricExpenditure = 0;
private double calorie = 0;
/**
* Default constructor.
@@ -89,7 +89,7 @@ public class TripStatistics implements Parcelable {
other.elevationExtremities.getMin(), other.elevationExtremities.getMax());
totalElevationGain = other.totalElevationGain;
gradeExtremities.set(other.gradeExtremities.getMin(), other.gradeExtremities.getMax());
caloricExpenditure = other.caloricExpenditure;
calorie = other.calorie;
}
/**
@@ -122,7 +122,7 @@ public class TripStatistics implements Parcelable {
gradeExtremities.update(other.gradeExtremities.getMin());
gradeExtremities.update(other.gradeExtremities.getMax());
}
caloricExpenditure += other.caloricExpenditure;
calorie += other.calorie;
}
/**
@@ -493,7 +493,8 @@ 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() + "}";
+ "; Max Grade: " + getMaxGrade() + "; Calorie expenditure: " + getCalorie()
+ "}";
}
/**
@@ -529,6 +530,9 @@ public class TripStatistics implements Parcelable {
double minGrade = source.readDouble();
double maxGrade = source.readDouble();
data.gradeExtremities.set(minGrade, maxGrade);
double calorie = source.readDouble();
data.setCalorie(calorie);
return data;
}
@@ -566,28 +570,29 @@ public class TripStatistics implements Parcelable {
dest.writeDouble(totalElevationGain);
dest.writeDouble(gradeExtremities.getMin());
dest.writeDouble(gradeExtremities.getMax());
dest.writeDouble(calorie);
}
/**
* Adds calories value.
* Adds calorie value.
*
* @param calories
* @param calorieAdded
*/
public void addCaloticExpenditure(double calories) {
caloricExpenditure = caloricExpenditure + calories;
public void addCalorie(double calorieAdded) {
this.calorie += calorieAdded;
}
/**
* Sets calories value.
* Sets calorie value.
*/
public void setCaloricExpenditure(double value) {
caloricExpenditure = value;
public void setCalorie(double newCalorie) {
calorie = newCalorie;
}
/**
* Gets calories value.
*/
public double getCaloricExpenditure() {
return caloricExpenditure;
public double getCalorie() {
return calorie;
}
}
@@ -67,6 +67,7 @@ public class TripStatisticsTest extends TestCase {
statistics.setMinGrade(-25.0); // Resulting min grade
statistics2.setMaxGrade(35.0); // Resulting max grade
statistics2.setMinGrade(0.0);
statistics2.setCalorie(50.0);
// Resulting bounds: -10000, 35000, 30000, -40000
statistics.setBounds(-10000, 20000, 30000, -40000);
@@ -89,6 +90,7 @@ public class TripStatisticsTest extends TestCase {
assertEquals(3575.0, statistics.getMaxElevation());
assertEquals(-25.0, statistics.getMinGrade());
assertEquals(35.0, statistics.getMaxGrade());
assertEquals(50.0, statistics.getCalorie());
}
public void testGetAverageSpeed() {
@@ -29,14 +29,15 @@ public class CalorieUtilsTest extends TestCase {
Location start = new Location(LocationManager.GPS_PROVIDER);
Location stop = new Location(LocationManager.GPS_PROVIDER);
private double grade = 10;
private double grade = 10.0;
private int weight = 20;
private final long TIME_INTERVAL = 1000l;
@Override
protected void setUp() throws Exception {
super.setUp();
// Make the time interval is not 0.
stop.setTime(start.getTime() + 1000);
// Make the time interval is not 0
stop.setTime(start.getTime() + TIME_INTERVAL);
}
/**
@@ -53,9 +54,11 @@ public class CalorieUtilsTest extends TestCase {
* Checks whether using foot calculation equation while grade is negative.
*/
public void testGetCalories_footNegativeGrade() {
double actual = CalorieUtils.getCalories(start, stop, -5, weight,
double actualGrade = -5;
double expectGrade = 0;
double actual = CalorieUtils.getCalories(start, stop, actualGrade, weight,
CalorieUtils.ActivityType.FOOT);
double expected = CalorieUtils.calculateExpenditureFoot(start, stop, 0, weight);
double expected = CalorieUtils.calculateExpenditureFoot(start, stop, expectGrade, weight);
assertEquals(expected, actual);
}
@@ -83,9 +86,11 @@ public class CalorieUtilsTest extends TestCase {
* Checks using walking VO2 equation.
*/
public void testGetVO2_walking() {
double actual = CalorieUtils.getVO2(CalorieUtils.CRTICAL_SPEED_RUNNING / 2, grade);
double expected = CalorieUtils.calculateWalkingVO2(CalorieUtils.CRTICAL_SPEED_RUNNING / 2,
grade);
// 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);
assertEquals(expected, actual);
}