mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-13 22:33:07 +02:00
Use the activity type of current track to calculate calorie.
This commit is contained in:
@@ -15,8 +15,13 @@
|
||||
*/
|
||||
package com.google.android.apps.mytracks.util;
|
||||
|
||||
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
|
||||
import com.google.android.apps.mytracks.content.Track;
|
||||
import com.google.android.apps.mytracks.stats.TripStatistics;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
|
||||
import android.content.Context;
|
||||
import android.location.Location;
|
||||
|
||||
/**
|
||||
@@ -64,7 +69,7 @@ public class CalorieUtils {
|
||||
* UnitConversions.KM_TO_M / UnitConversions.HR_TO_MIN;
|
||||
|
||||
public enum ActivityType {
|
||||
CYCLING, FOOT
|
||||
CYCLING, FOOT, INVALID
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,7 +218,36 @@ public class CalorieUtils {
|
||||
*/
|
||||
public static double getCalorie(Location start, Location stop, double grade, int weight,
|
||||
ActivityType activityType) {
|
||||
if (activityType == ActivityType.INVALID) {
|
||||
return TripStatistics.INVALID_CALORIE;
|
||||
}
|
||||
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.
|
||||
*
|
||||
* @param context current context
|
||||
* @param trackId the id of track
|
||||
* @return activityType the activity type of track.
|
||||
*/
|
||||
public static ActivityType getActivityType(Context context, long trackId) {
|
||||
ActivityType activityType = ActivityType.INVALID;
|
||||
|
||||
Track track = MyTracksProviderUtils.Factory.get(context).getTrack(trackId);
|
||||
if (track != null) {
|
||||
String category = track.getCategory();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@
|
||||
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;
|
||||
@@ -108,8 +109,10 @@ public class StatsUtils {
|
||||
*
|
||||
* @param activity the activity
|
||||
* @param tripStatistics the trip statistics
|
||||
* @param trackId the id of track
|
||||
*/
|
||||
public static void setTripStatisticsValues(Activity activity, TripStatistics tripStatistics) {
|
||||
public static void setTripStatisticsValues(Activity activity, TripStatistics tripStatistics,
|
||||
long trackId) {
|
||||
boolean metricUnits = PreferencesUtils.isMetricUnits(activity);
|
||||
boolean reportSpeed = PreferencesUtils.isReportSpeed(activity);
|
||||
|
||||
@@ -183,12 +186,15 @@ public class StatsUtils {
|
||||
if (showCalorie) {
|
||||
calorieHorizontalLine.setVisibility(View.VISIBLE);
|
||||
calorieContainer.setVisibility(View.VISIBLE);
|
||||
double calories = tripStatistics == null ? Double.NaN : tripStatistics.getCalorie();
|
||||
setCalorie(activity, R.id.stats_calorie, R.string.stats_calorie, calories);
|
||||
double calories = Double.NaN;
|
||||
if (tripStatistics != null) {
|
||||
calories = tripStatistics.getCalorie();
|
||||
}
|
||||
setCalorie(activity, R.id.stats_calorie, calories, trackId);
|
||||
} else {
|
||||
calorieHorizontalLine.setVisibility(View.GONE);
|
||||
calorieContainer.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -336,11 +342,18 @@ public class StatsUtils {
|
||||
*
|
||||
* @param activity the activity
|
||||
* @param itemId the item id
|
||||
* @param labelId the calorie label id
|
||||
* @param calorie the value of calorie
|
||||
* @param trackId the id of track
|
||||
*/
|
||||
private static void setCalorie(Activity activity, int itemId, int labelId, double calorie) {
|
||||
private static void setCalorie(Activity activity, int itemId, double calorie, long trackId) {
|
||||
int labelId = R.string.stats_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;
|
||||
}
|
||||
setItem(activity, itemId, labelId,
|
||||
String.format(Locale.getDefault(), CALORIES_FORMAT, calorie), activity.getString(R.string.unit_calorie));
|
||||
String.format(Locale.getDefault(), CALORIES_FORMAT, calorie),
|
||||
activity.getString(R.string.unit_calorie));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user