Update the TripStatisticsUpdater after change the activity type of current recording calorie. Using the activity type but calorie value to determine whether show activity set button or not.

This commit is contained in:
youtaol
2013-10-10 19:19:02 +08:00
parent e228918cca
commit 651034e2a8
8 changed files with 100 additions and 20 deletions
@@ -20,10 +20,12 @@ import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
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.services.TrackRecordingServiceConnection;
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.apps.mytracks.util.TrackRecordingServiceConnectionUtils;
import com.google.android.maps.mytracks.R;
import android.graphics.Bitmap;
@@ -58,6 +60,7 @@ public class TrackEditActivity extends AbstractMyTracksActivity
private static final String ICON_VALUE_KEY = "icon_value_key";
private Long trackId;
private TrackRecordingServiceConnection trackRecordingServiceConnection;
private MyTracksProviderUtils myTracksProviderUtils;
private Track track;
private String iconValue;
@@ -71,6 +74,7 @@ public class TrackEditActivity extends AbstractMyTracksActivity
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
trackRecordingServiceConnection = new TrackRecordingServiceConnection(this, null);
trackId = getIntent().getLongExtra(EXTRA_TRACK_ID, -1L);
if (trackId == -1L) {
Log.e(TAG, "invalid trackId");
@@ -185,7 +189,10 @@ public class TrackEditActivity extends AbstractMyTracksActivity
// TODO Is there no race condition when setCalorie is called.
double calorie = CalorieUtils.calculateTrackCalorie(getApplicationContext(), track,
category);
track.getTripStatistics().setCalorie(calorie);
TrackRecordingServiceConnectionUtils.updateCalorie(trackRecordingServiceConnection,
calorie);
}
track.setCategory(category);
track.setIcon(TrackIconUtils.getIconValue(TrackEditActivity.this, category));
@@ -216,6 +223,18 @@ public class TrackEditActivity extends AbstractMyTracksActivity
cancel.setVisibility(View.VISIBLE);
}
}
@Override
protected void onStart() {
super.onStart();
TrackRecordingServiceConnectionUtils.startConnection(this, trackRecordingServiceConnection);
}
@Override
protected void onStop() {
super.onStop();
trackRecordingServiceConnection.unbind();
}
@Override
public void onSaveInstanceState(Bundle outState) {
@@ -1333,5 +1333,34 @@ public class TrackRecordingService extends Service {
deathRecipient.binderDied();
}
}
@Override
public void updateCalorie(double calorie) {
if (!canAccess()) {
return;
}
trackRecordingService.updateCalorie(calorie);
}
}
/**
* Updates the calorie value.
*
* @param calorie new calorie value.
*/
public void updateCalorie(final double calorie) {
if (myTracksLocationManager == null || executorService == null
|| !myTracksLocationManager.isAllowed() || executorService.isShutdown()
|| executorService.isTerminated()) {
return;
}
executorService.submit(new Runnable() {
@Override
public void run() {
trackTripStatisticsUpdater.updateCalorie(calorie);
}
});
}
}
@@ -223,6 +223,15 @@ public class TripStatisticsUpdater {
lastLocation = location;
lastMovingLocation = location;
}
/**
* Updates the calorie value.
*
* @param calorie new calorie value.
*/
public void updateCalorie(double calorie) {
currentSegment.setCalorie(calorie);
}
/**
* Gets the smoothed elevation over several readings. The elevation readings
@@ -18,7 +18,6 @@ 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;
@@ -220,7 +219,7 @@ 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 0.0;
}
return ActivityType.CYCLING == activityType ? calculateCalorieCycling(start, stop, grade,
weight) : calculateCalorieFoot(start, stop, grade, weight);
@@ -259,7 +258,7 @@ public class CalorieUtils {
* @param category the category of track
* @return activityType the activity type of track.
*/
private static ActivityType getActivityType(Context context, String category) {
public 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))) {
@@ -282,7 +281,7 @@ public class CalorieUtils {
public static double calculateTrackCalorie(Context context, Track track, String category) {
ActivityType activityType = getActivityType(context, category);
if (activityType == ActivityType.INVALID) {
return TripStatistics.CALORIE_UNDEFINED;
return 0.0;
}
double calorie = 0.0;
@@ -303,4 +302,5 @@ public class CalorieUtils {
}
return calorie;
}
}
@@ -17,7 +17,9 @@
package com.google.android.apps.mytracks.util;
import com.google.android.apps.mytracks.TrackEditActivity;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
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;
@@ -186,6 +188,9 @@ public class StatsUtils {
View calorieContainer = activity.findViewById(R.id.stats_calorie_container);
if (showCalorie) {
if(trackId == PreferencesUtils.RECORDING_TRACK_ID_DEFAULT) {
return;
}
calorieHorizontalLine.setVisibility(View.VISIBLE);
calorieContainer.setVisibility(View.VISIBLE);
double calories = Double.NaN;
@@ -353,8 +358,11 @@ public class StatsUtils {
String calorieString;
View view = activity.findViewById(itemId);
Button button = (Button) activity.findViewById(R.id.stats_calorie_setup);
MyTracksProviderUtils providerUtils = MyTracksProviderUtils.Factory.get(activity);
ActivityType activityType = CalorieUtils.getActivityType(activity,
providerUtils.getTrack(trackId).getCategory());
// Current activity type is not supported.
if (calorie == TripStatistics.CALORIE_UNDEFINED) {
if (activityType == ActivityType.INVALID) {
button.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
@@ -193,4 +193,24 @@ public class TrackRecordingServiceConnectionUtils {
Toast.makeText(context, R.string.marker_add_error, Toast.LENGTH_LONG).show();
return -1L;
}
/**
* Updates the calorie of current recording track.
*/
public static void updateCalorie(TrackRecordingServiceConnection trackRecordingServiceConnection,
double calorie) {
ITrackRecordingService trackRecordingService = trackRecordingServiceConnection
.getServiceIfBound();
if (trackRecordingService == null) {
Log.d(TAG, "Unable to update calorie, no track recording service");
} else {
try {
trackRecordingService.updateCalorie(calorie);
} catch (RemoteException e) {
Log.e(TAG, "Unable to update calorie", e);
} catch (IllegalStateException e) {
Log.e(TAG, "Unable to update calorie.", e);
}
}
}
}
@@ -106,4 +106,11 @@ interface ITrackRecordingService {
* return a Sensor.SensorState enum value.
*/
int getSensorState();
/**
* Updates the calorie value.
*
* @param calorie new calorie value.
*/
void updateCalorie(double calorie);
}
@@ -62,14 +62,8 @@ public class TripStatistics implements Parcelable {
// The min and max grade seen on this trip.
private final ExtremityMonitor gradeExtremities = new ExtremityMonitor();
/**
* This value means the calorie value is invalid due to activity type is not
* supported.
*/
public static final double CALORIE_UNDEFINED = -1.0;
// The calorie of current track.
private double calorie = CALORIE_UNDEFINED;
private double calorie = 0.0;
/**
* Default constructor.
@@ -128,9 +122,7 @@ public class TripStatistics implements Parcelable {
gradeExtremities.update(other.gradeExtremities.getMin());
gradeExtremities.update(other.gradeExtremities.getMax());
}
if (other.calorie != CALORIE_UNDEFINED) {
calorie += other.calorie;
}
calorie += other.calorie;
}
/**
@@ -585,11 +577,7 @@ public class TripStatistics implements Parcelable {
* @param calorieAdded add the value to the total calorie
*/
public void addCalorie(double calorieAdded) {
if (calorieAdded != CALORIE_UNDEFINED) {
calorie = calorie == CALORIE_UNDEFINED ? calorieAdded : calorie + calorieAdded;
} else {
calorie = CALORIE_UNDEFINED;
}
calorie += calorieAdded;
}
/**