mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-08 04:13:06 +02:00
Merge heads
This commit is contained in:
@@ -15,8 +15,14 @@
|
||||
*/
|
||||
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;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
|
||||
import android.content.Context;
|
||||
import android.location.Location;
|
||||
|
||||
/**
|
||||
@@ -64,7 +70,7 @@ public class CalorieUtils {
|
||||
* UnitConversions.KM_TO_M / UnitConversions.HR_TO_MIN;
|
||||
|
||||
public enum ActivityType {
|
||||
CYCLING, FOOT
|
||||
CYCLING, FOOT, INVALID
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,14 +134,14 @@ public class CalorieUtils {
|
||||
* @param grade the grade to calculate
|
||||
* @param weight of rider plus bike, in kilogram
|
||||
* @param timeUsed how many times used in second
|
||||
* @return the power value watts(Joule/second).
|
||||
* @return the calorie value in kcal.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
static double calculateCyclingCalorie(double speed, double grade, int weight, double timeUsed) {
|
||||
// Get the Power, the unit is Watt (Joule/second)
|
||||
double power = earthGravity * weight * speed * (K1 + grade) + K2 * (speed * speed * speed);
|
||||
|
||||
// Get the calories
|
||||
// Get the calories in kcal
|
||||
return power * timeUsed / UnitConversions.KCAL_TO_J;
|
||||
}
|
||||
|
||||
@@ -213,7 +219,88 @@ public class CalorieUtils {
|
||||
*/
|
||||
public static double getCalorie(Location start, Location stop, double grade, int weight,
|
||||
ActivityType activityType) {
|
||||
if (activityType == ActivityType.INVALID) {
|
||||
return TripStatistics.CALORIE_UNDEFINED;
|
||||
}
|
||||
return ActivityType.CYCLING == activityType ? calculateCalorieCycling(start, stop, grade,
|
||||
weight) : calculateCalorieFoot(start, stop, grade, weight);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the activity type for calculating calorie by the id 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.CALORIE_UNDEFINED;
|
||||
}
|
||||
|
||||
double calorie = 0.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;
|
||||
}
|
||||
}
|
||||
@@ -16,12 +16,16 @@
|
||||
|
||||
package com.google.android.apps.mytracks.util;
|
||||
|
||||
import com.google.android.apps.mytracks.TrackEditActivity;
|
||||
import com.google.android.apps.mytracks.stats.TripStatistics;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.location.Location;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.Locale;
|
||||
@@ -108,8 +112,9 @@ 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 +188,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 +344,42 @@ 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 which is used to start track edit activity
|
||||
* when user want to change the activity type
|
||||
*/
|
||||
private static void setCalorie(Activity activity, int itemId, int labelId, double calorie) {
|
||||
setItem(activity, itemId, labelId,
|
||||
String.format(Locale.getDefault(), CALORIES_FORMAT, calorie), activity.getString(R.string.unit_calorie));
|
||||
private static void setCalorie(final Activity activity, int itemId, double calorie,
|
||||
final long trackId) {
|
||||
String calorieString;
|
||||
View view = activity.findViewById(itemId);
|
||||
Button button = (Button) activity.findViewById(R.id.stats_calorie_setup);
|
||||
// Current activity type is not supported.
|
||||
if (calorie == TripStatistics.CALORIE_UNDEFINED) {
|
||||
button.setVisibility(View.VISIBLE);
|
||||
view.setVisibility(View.GONE);
|
||||
|
||||
/*
|
||||
* Return when it is not a valid track, which may be aggregated stats or
|
||||
* marker.
|
||||
*/
|
||||
if (trackId == PreferencesUtils.RECORDING_TRACK_ID_DEFAULT) {
|
||||
button.setVisibility(View.GONE);
|
||||
return;
|
||||
}
|
||||
button.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Intent intent = IntentUtils.newIntent(activity, TrackEditActivity.class).putExtra(
|
||||
TrackEditActivity.EXTRA_TRACK_ID, trackId);
|
||||
activity.startActivity(intent);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
button.setVisibility(View.GONE);
|
||||
view.setVisibility(View.VISIBLE);
|
||||
calorieString = String.format(Locale.getDefault(), CALORIES_FORMAT, calorie);
|
||||
setItem(activity, itemId, R.string.stats_calorie, calorieString,
|
||||
activity.getString(R.string.unit_calorie));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,5 +78,8 @@ public class UnitConversions {
|
||||
public static final double DEG_TO_RAD = Math.PI / 180.0;
|
||||
|
||||
// multiplication factor to convert Kcal to joules.
|
||||
public static final double KCAL_TO_J = 4.184;
|
||||
public static final double KCAL_TO_J = 4184;
|
||||
|
||||
// multiplication factor to convert joules to Kcal.
|
||||
public static final double J_TO_KCAL = 1 / KCAL_TO_J;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user