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
@@ -19,7 +19,6 @@ package com.google.android.apps.mytracks;
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.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.StatsUtils;
import com.google.android.maps.mytracks.R;
@@ -37,8 +36,7 @@ public class AggregatedStatsActivity extends AbstractMyTracksActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StatsUtils.setTripStatisticsValues(this, getTripStatistics(),
PreferencesUtils.RECORDING_TRACK_ID_DEFAULT);
StatsUtils.setTripStatisticsValues(this, getTripStatistics());
StatsUtils.setLocationValues(this, null, false);
}
@@ -21,6 +21,7 @@ import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.fragments.ChooseActivityTypeDialogFragment;
import com.google.android.apps.mytracks.fragments.ChooseActivityTypeDialogFragment.ChooseActivityTypeCaller;
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
import com.google.android.apps.mytracks.util.CalorieUtils;
import com.google.android.apps.mytracks.util.TrackIconUtils;
import com.google.android.apps.mytracks.util.TrackNameUtils;
import com.google.android.maps.mytracks.R;
@@ -180,6 +181,10 @@ public class TrackEditActivity extends AbstractMyTracksActivity
public void onClick(View v) {
track.setName(name.getText().toString());
String category = activityType.getText().toString();
if(!category.equals(track.getCategory())) {
double calorie = CalorieUtils.calculateTrackCalorie(getApplicationContext(), track, category);
track.getTripStatistics().setCalorie(calorie);
}
track.setCategory(category);
track.setIcon(TrackIconUtils.getIconValue(TrackEditActivity.this, category));
track.setDescription(description.getText().toString());
@@ -192,8 +192,7 @@ public class MarkerDetailFragment extends Fragment {
TextView name = (TextView) getView().findViewById(R.id.marker_detail_statistics_name);
name.setText(getString(R.string.generic_name_line, waypoint.getName()));
StatsUtils.setTripStatisticsValues(getActivity(), waypoint.getTripStatistics(),
waypoint.getTrackId());
StatsUtils.setTripStatisticsValues(getActivity(), waypoint.getTripStatistics());
StatsUtils.setLocationValues(getActivity(), waypoint.getLocation(), false);
}
}
@@ -259,11 +259,7 @@ public class StatsFragment extends Fragment implements TrackDataListener {
* Updates the UI.
*/
private void updateUi(FragmentActivity activity) {
StatsUtils.setTripStatisticsValues(
activity,
lastTripStatistics,
trackDataHub == null ? PreferencesUtils.RECORDING_TRACK_ID_DEFAULT : trackDataHub
.getSelectedTrackId());
StatsUtils.setTripStatisticsValues(activity, lastTripStatistics);
StatsUtils.setLocationValues(activity, lastLocation, isSelectedTrackRecording());
}
}
@@ -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));
}
}
@@ -586,6 +586,8 @@ public class TripStatistics implements Parcelable {
public void addCalorie(double calorieAdded) {
if (calorieAdded != INVALID_CALORIE) {
this.calorie += calorieAdded;
} else {
this.calorie = INVALID_CALORIE;
}
}