Re-calculate the calorie while user change the activity type of track. Show standard "value_unknown" (-) when the activity type is not supported.

This commit is contained in:
youtaol
2013-09-06 14:54:02 +08:00
parent 7233137e93
commit 282c48f530
7 changed files with 72 additions and 23 deletions
@@ -16,6 +16,7 @@
package com.google.android.apps.mytracks.util;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils.LocationIterator;
import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.stats.TripStatistics;
import com.google.android.maps.mytracks.R;
@@ -224,9 +225,9 @@ public class CalorieUtils {
return ActivityType.CYCLING == activityType ? calculateCalorieCycling(start, stop, grade,
weight) : calculateCalorieFoot(start, stop, grade, weight);
}
/**
* Gets the activity type for calculating calorie by the category of track.
* Gets the activity type for calculating calorie by the id of track.
*
* @param context current context
* @param trackId the id of track
@@ -250,4 +251,56 @@ public class CalorieUtils {
return activityType;
}
/**
* Gets the activity type for calculating calorie by the category of track.
*
* @param context current context
* @param category the category of track
* @return activityType the activity type of track.
*/
private static ActivityType getActivityType(Context context, String category) {
ActivityType activityType = ActivityType.INVALID;
if (category.equals(context.getString(R.string.activity_type_walking))
|| category.equals(context.getString(R.string.activity_type_running))) {
activityType = ActivityType.FOOT;
} else if (category.equals(context.getString(R.string.activity_type_cycling))
|| category.equals(context.getString(R.string.activity_type_biking))) {
activityType = ActivityType.CYCLING;
}
return activityType;
}
/**
* Calculates the calorie of track.
*
* @param context the context
* @param track the track to calculate
* @param category the category of track
*/
public static double calculateTrackCalorie(Context context, Track track, String category) {
ActivityType activityType = getActivityType(context, category);
if (activityType == ActivityType.INVALID) {
return TripStatistics.INVALID_CALORIE;
}
double calorie = 0;
MyTracksProviderUtils providerUtils = MyTracksProviderUtils.Factory.get(context);
long trackId = track.getId();
LocationIterator points = providerUtils.getTrackPointLocationIterator(trackId, -1, false,
MyTracksProviderUtils.DEFAULT_LOCATION_FACTORY);
// TODO How get grade
double grade = 0;
if (points.hasNext()) {
Location start = points.next();
while (points.hasNext()) {
Location stop = points.next();
calorie += getCalorie(start, stop, grade, PreferencesUtils.getInt(context,
R.string.stats_weight_key, PreferencesUtils.STATS_WEIGHT_DEFAULT), activityType);
start = stop;
}
}
return calorie;
}
}
@@ -17,7 +17,6 @@
package com.google.android.apps.mytracks.util;
import com.google.android.apps.mytracks.stats.TripStatistics;
import com.google.android.apps.mytracks.util.CalorieUtils.ActivityType;
import com.google.android.maps.mytracks.R;
import android.app.Activity;
@@ -111,8 +110,7 @@ public class StatsUtils {
* @param tripStatistics the trip statistics
* @param trackId the id of track
*/
public static void setTripStatisticsValues(Activity activity, TripStatistics tripStatistics,
long trackId) {
public static void setTripStatisticsValues(Activity activity, TripStatistics tripStatistics) {
boolean metricUnits = PreferencesUtils.isMetricUnits(activity);
boolean reportSpeed = PreferencesUtils.isReportSpeed(activity);
@@ -190,7 +188,7 @@ public class StatsUtils {
if (tripStatistics != null) {
calories = tripStatistics.getCalorie();
}
setCalorie(activity, R.id.stats_calorie, calories, trackId);
setCalorie(activity, R.id.stats_calorie, calories);
} else {
calorieHorizontalLine.setVisibility(View.GONE);
calorieContainer.setVisibility(View.GONE);
@@ -345,15 +343,13 @@ public class StatsUtils {
* @param calorie the value of calorie
* @param trackId the id of track
*/
private static void setCalorie(Activity activity, int itemId, double calorie, long trackId) {
int labelId = R.string.stats_calorie;
private static void setCalorie(Activity activity, int itemId, double calorie) {
String calorieString = String.format(Locale.getDefault(), CALORIES_FORMAT, calorie);
// Current activity type is not supported.
if (trackId != PreferencesUtils.RECORDING_TRACK_ID_DEFAULT
&& CalorieUtils.getActivityType(activity, trackId) == ActivityType.INVALID) {
labelId = R.string.stats_calorie_invalid_type;
if (calorie == TripStatistics.INVALID_CALORIE) {
calorieString = activity.getString(R.string.value_unknown);
}
setItem(activity, itemId, labelId,
String.format(Locale.getDefault(), CALORIES_FORMAT, calorie),
setItem(activity, itemId, R.string.stats_calorie, calorieString,
activity.getString(R.string.unit_calorie));
}
}