Remove the parameter of updateCalorie method of TrackRecordingService. Re-calculate the calorie of track and last marker in TrackRecordingService.

This commit is contained in:
youtaol
2013-10-25 14:41:21 +08:00
parent c68ec5ed9a
commit c43e9047bc
10 changed files with 107 additions and 80 deletions
@@ -191,13 +191,18 @@ public class TrackEditActivity extends AbstractMyTracksActivity
track.setModifiedTime(System.currentTimeMillis()); track.setModifiedTime(System.currentTimeMillis());
if (!category.equals(track.getCategory())) { if (!category.equals(track.getCategory())) {
track.setCategory(category); track.setCategory(category);
// If edit recording track. // If edit recording track.
if (track.getId() == PreferencesUtils.getLong(getApplicationContext(), if (track.getId() == PreferencesUtils.getLong(getApplicationContext(),
R.string.recording_track_id_key)) { R.string.recording_track_id_key)) {
TrackRecordingServiceConnectionUtils.updateCalorie(trackRecordingServiceConnection,
track); // Update data without new calorie.
myTracksProviderUtils.updateTrack(track);
TrackRecordingServiceConnectionUtils.updateCalorie(trackRecordingServiceConnection);
} else { } else {
CalorieUtils.updateTrackStatistics(getApplicationContext(), track); CalorieUtils.updateTrackStatistics(getApplicationContext(), -1, track);
// Update data with new calorie.
myTracksProviderUtils.updateTrack(track); myTracksProviderUtils.updateTrack(track);
} }
} }
@@ -28,6 +28,7 @@ import com.google.android.apps.mytracks.content.TrackDataType;
import com.google.android.apps.mytracks.content.Waypoint; import com.google.android.apps.mytracks.content.Waypoint;
import com.google.android.apps.mytracks.stats.TripStatistics; import com.google.android.apps.mytracks.stats.TripStatistics;
import com.google.android.apps.mytracks.stats.TripStatisticsUpdater; import com.google.android.apps.mytracks.stats.TripStatisticsUpdater;
import com.google.android.apps.mytracks.util.CalorieUtils;
import com.google.android.apps.mytracks.util.LocationUtils; import com.google.android.apps.mytracks.util.LocationUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils; import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.UnitConversions; import com.google.android.apps.mytracks.util.UnitConversions;
@@ -453,7 +454,10 @@ public class ChartFragment extends Fragment implements TrackDataListener {
double power = Double.NaN; double power = Double.NaN;
if (tripStatisticsUpdater != null) { if (tripStatisticsUpdater != null) {
tripStatisticsUpdater.addLocation(location, recordingDistanceInterval, context); tripStatisticsUpdater.addLocation(location, recordingDistanceInterval, false, CalorieUtils
.getActivityType(context,
PreferencesUtils.getLong(context, R.string.recording_track_id_key)), PreferencesUtils
.getInt(context, R.string.stats_weight_key, PreferencesUtils.STATS_WEIGHT_DEFAULT));
TripStatistics tripStatistics = tripStatisticsUpdater.getTripStatistics(); TripStatistics tripStatistics = tripStatisticsUpdater.getTripStatistics();
if (chartByDistance) { if (chartByDistance) {
double distance = tripStatistics.getTotalDistance() * UnitConversions.M_TO_KM; double distance = tripStatistics.getTotalDistance() * UnitConversions.M_TO_KM;
@@ -24,6 +24,7 @@ import com.google.android.apps.mytracks.content.Waypoint.WaypointType;
import com.google.android.apps.mytracks.services.TrackRecordingService; import com.google.android.apps.mytracks.services.TrackRecordingService;
import com.google.android.apps.mytracks.stats.TripStatistics; import com.google.android.apps.mytracks.stats.TripStatistics;
import com.google.android.apps.mytracks.stats.TripStatisticsUpdater; import com.google.android.apps.mytracks.stats.TripStatisticsUpdater;
import com.google.android.apps.mytracks.util.CalorieUtils;
import com.google.android.apps.mytracks.util.FileUtils; import com.google.android.apps.mytracks.util.FileUtils;
import com.google.android.apps.mytracks.util.LocationUtils; import com.google.android.apps.mytracks.util.LocationUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils; import com.google.android.apps.mytracks.util.PreferencesUtils;
@@ -241,9 +242,16 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track
// No more track points. Ignore the rest of the waypoints. // No more track points. Ignore the rest of the waypoints.
return; return;
} }
trackTripStatisticstrackUpdater.addLocation(trackPoint, recordingDistanceInterval, trackTripStatisticstrackUpdater.addLocation(trackPoint, recordingDistanceInterval, false,
context); CalorieUtils.getActivityType(context,
markerTripStatisticsUpdater.addLocation(trackPoint, recordingDistanceInterval, context); PreferencesUtils.getLong(context, R.string.recording_track_id_key)),
PreferencesUtils.getInt(context, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT));
markerTripStatisticsUpdater.addLocation(trackPoint, recordingDistanceInterval, false,
CalorieUtils.getActivityType(context,
PreferencesUtils.getLong(context, R.string.recording_track_id_key)),
PreferencesUtils.getInt(context, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT));
} }
if (waypoint.getLocation().getTime() > trackPoint.getTime()) { if (waypoint.getLocation().getTime() > trackPoint.getTime()) {
trackPoint = null; trackPoint = null;
@@ -546,7 +554,10 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track
trackData.tripStatisticsUpdater = new TripStatisticsUpdater( trackData.tripStatisticsUpdater = new TripStatisticsUpdater(
location.getTime() != -1L ? location.getTime() : trackData.importTime); location.getTime() != -1L ? location.getTime() : trackData.importTime);
} }
trackData.tripStatisticsUpdater.addLocation(location, recordingDistanceInterval, context); trackData.tripStatisticsUpdater.addLocation(location, recordingDistanceInterval, false,
CalorieUtils.getActivityType(context,
PreferencesUtils.getLong(context, R.string.recording_track_id_key)), PreferencesUtils
.getInt(context, R.string.stats_weight_key, PreferencesUtils.STATS_WEIGHT_DEFAULT));
trackData.bufferedLocations[trackData.numBufferedLocations] = location; trackData.bufferedLocations[trackData.numBufferedLocations] = location;
trackData.numBufferedLocations++; trackData.numBufferedLocations++;
@@ -29,6 +29,7 @@ import com.google.android.apps.mytracks.io.gdata.maps.XmlMapsGDataParserFactory;
import com.google.android.apps.mytracks.io.sendtogoogle.AbstractSendAsyncTask; import com.google.android.apps.mytracks.io.sendtogoogle.AbstractSendAsyncTask;
import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils; import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils;
import com.google.android.apps.mytracks.stats.TripStatisticsUpdater; import com.google.android.apps.mytracks.stats.TripStatisticsUpdater;
import com.google.android.apps.mytracks.util.CalorieUtils;
import com.google.android.apps.mytracks.util.LocationUtils; import com.google.android.apps.mytracks.util.LocationUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils; import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.common.gdata.AndroidXmlParserFactory; import com.google.android.common.gdata.AndroidXmlParserFactory;
@@ -277,7 +278,11 @@ public class SendMapsAsyncTask extends AbstractSendAsyncTask {
sentStartMarker = true; sentStartMarker = true;
} }
tripStatisticsUpdater.addLocation(location, recordingDistanceInterval, context); tripStatisticsUpdater.addLocation(location, recordingDistanceInterval, false, CalorieUtils
.getActivityType(context,
PreferencesUtils.getLong(context, R.string.recording_track_id_key)),
PreferencesUtils.getInt(context, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT));
if (i % elevationSamplingFrequency == 0) { if (i % elevationSamplingFrequency == 0) {
distances.add(tripStatisticsUpdater.getTripStatistics().getTotalDistance()); distances.add(tripStatisticsUpdater.getTripStatistics().getTotalDistance());
elevations.add(tripStatisticsUpdater.getSmoothedElevation()); elevations.add(tripStatisticsUpdater.getSmoothedElevation());
@@ -660,9 +660,17 @@ public class TrackRecordingService extends Service {
if (cursor.moveToLast()) { if (cursor.moveToLast()) {
do { do {
Location location = myTracksProviderUtils.createTrackPoint(cursor); Location location = myTracksProviderUtils.createTrackPoint(cursor);
trackTripStatisticsUpdater.addLocation(location, recordingDistanceInterval, context); trackTripStatisticsUpdater.addLocation(location, recordingDistanceInterval, false,
CalorieUtils.getActivityType(context,
PreferencesUtils.getLong(context, R.string.recording_track_id_key)),
PreferencesUtils.getInt(context, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT));
if (location.getTime() > markerStartTime) { if (location.getTime() > markerStartTime) {
markerTripStatisticsUpdater.addLocation(location, recordingDistanceInterval, context); markerTripStatisticsUpdater.addLocation(location, recordingDistanceInterval, false,
CalorieUtils.getActivityType(context,
PreferencesUtils.getLong(context, R.string.recording_track_id_key)),
PreferencesUtils.getInt(context, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT));
} }
} while (cursor.moveToPrevious()); } while (cursor.moveToPrevious());
} }
@@ -1002,9 +1010,13 @@ public class TrackRecordingService extends Service {
try { try {
Uri uri = myTracksProviderUtils.insertTrackPoint(location, track.getId()); Uri uri = myTracksProviderUtils.insertTrackPoint(location, track.getId());
long trackPointId = Long.parseLong(uri.getLastPathSegment()); long trackPointId = Long.parseLong(uri.getLastPathSegment());
trackTripStatisticsUpdater.addLocationCalorie(location, recordingDistanceInterval, trackTripStatisticsUpdater.addLocation(location, recordingDistanceInterval, true,
CalorieUtils.getActivityType(context, track.getId()), context); CalorieUtils.getActivityType(context, track.getId()), PreferencesUtils.getInt(context,
markerTripStatisticsUpdater.addLocation(location, recordingDistanceInterval, context); R.string.stats_weight_key, PreferencesUtils.STATS_WEIGHT_DEFAULT));
markerTripStatisticsUpdater.addLocation(location, recordingDistanceInterval, false,
CalorieUtils.getActivityType(context,
PreferencesUtils.getLong(context, R.string.recording_track_id_key)), PreferencesUtils
.getInt(context, R.string.stats_weight_key, PreferencesUtils.STATS_WEIGHT_DEFAULT));
updateRecordingTrack(track, trackPointId, LocationUtils.isValidLocation(location)); updateRecordingTrack(track, trackPointId, LocationUtils.isValidLocation(location));
} catch (SQLiteException e) { } catch (SQLiteException e) {
/* /*
@@ -1337,21 +1349,19 @@ public class TrackRecordingService extends Service {
} }
@Override @Override
public void updateTripStatistics(Track track) { public void updateCalorie() {
if (!canAccess()) { if (!canAccess()) {
return; return;
} }
trackRecordingService.updateTripStatistics(track); trackRecordingService.updateCalorie();
} }
} }
/** /**
* Updates the TripStatistics of current recording track after the current * Updates the calorie of current recording track after the current
* track is edited by user. * track is edited by user.
*
* @param track the current recording track
*/ */
public void updateTripStatistics(final Track track) { public void updateCalorie() {
if (myTracksLocationManager == null || executorService == null if (myTracksLocationManager == null || executorService == null
|| !myTracksLocationManager.isAllowed() || executorService.isShutdown() || !myTracksLocationManager.isAllowed() || executorService.isShutdown()
|| executorService.isTerminated()) { || executorService.isTerminated()) {
@@ -1360,9 +1370,26 @@ public class TrackRecordingService extends Service {
executorService.submit(new Runnable() { executorService.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
trackTripStatisticsUpdater = CalorieUtils.updateTrackStatistics(getApplicationContext(), // Update track statistics.
track); long trackId = PreferencesUtils.getLong(getApplicationContext(),
MyTracksProviderUtils.Factory.get(context).updateTrack(track); R.string.recording_track_id_key);
Track currentTrack = myTracksProviderUtils.getTrack(trackId);
CalorieUtils.updateTrackStatistics(getApplicationContext(), -1, currentTrack);
trackTripStatisticsUpdater.updateCalorie(currentTrack.getTripStatistics().getCalorie());
// Update to database.
MyTracksProviderUtils.Factory.get(context).updateTrack(currentTrack);
// Update marker statistics.
Waypoint lastWayPoint = myTracksProviderUtils.getLastWaypoint(trackId,
WaypointType.WAYPOINT);
long lastWayPoint_pointId = myTracksProviderUtils.getTrackPointId(trackId,
lastWayPoint.getLocation());
if (lastWayPoint_pointId > 0) {
CalorieUtils.updateTrackStatistics(getApplicationContext(), lastWayPoint_pointId,
currentTrack);
}
markerTripStatisticsUpdater.updateCalorie(currentTrack.getTripStatistics().getCalorie());
} }
}); });
} }
@@ -22,11 +22,8 @@ import static com.google.android.apps.mytracks.services.TrackRecordingService.PA
import com.google.android.apps.mytracks.util.CalorieUtils; import com.google.android.apps.mytracks.util.CalorieUtils;
import com.google.android.apps.mytracks.util.CalorieUtils.ActivityType; import com.google.android.apps.mytracks.util.CalorieUtils.ActivityType;
import com.google.android.apps.mytracks.util.LocationUtils; import com.google.android.apps.mytracks.util.LocationUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import android.content.Context;
import android.location.Location; import android.location.Location;
import android.util.Log; import android.util.Log;
@@ -122,49 +119,19 @@ public class TripStatisticsUpdater {
return stats; return stats;
} }
/**
* Adds a location.
*
* @param location the location
* @param minRecordingDistance the min recording distance
* @param context the context to get the weight and track category
*/
public void addLocation(Location location, int minRecordingDistance, Context context) {
addLocation(
location,
minRecordingDistance,
CalorieUtils.getActivityType(context,
PreferencesUtils.getLong(context, R.string.recording_track_id_key)), 0, false);
}
/**
* Adds a location and calculate calorie. Only calculate calorie when add
* location to a recording track.
*
* @param location the location
* @param minRecordingDistance the min recording distance
* @param activityType the activity type
* @param context the context to get the weight and track category
*/
public void addLocationCalorie(Location location, int minRecordingDistance,
ActivityType activityType, Context context) {
int weight = PreferencesUtils.getInt(context, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT);
addLocation(location, minRecordingDistance, activityType, weight, true);
}
/** /**
* Adds a location. TODO: This assume location has a valid time. * Adds a location. TODO: This assume location has a valid time.
* *
* @param location the location * @param location the location
* @param minRecordingDistance the min recording distance * @param minRecordingDistance the min recording distance
* @param activityType the activity type of current track
* @param weight the weight to calculate calorie
* @param isCalculateCalorie true means calculate calorie * @param isCalculateCalorie true means calculate calorie
* @param activityType the activity type of current track which is used to
* calculate calorie
* @param weight the weight to calculate calorie which is used to calculate
* calorie
*/ */
@VisibleForTesting public void addLocation(Location location, int minRecordingDistance,
void addLocation(Location location, int minRecordingDistance, ActivityType activityType, boolean isCalculateCalorie, ActivityType activityType, int weight) {
int weight, boolean isCalculateCalorie) {
// Always update time // Always update time
updateTime(location.getTime()); updateTime(location.getTime());
if (!LocationUtils.isValidLocation(location)) { if (!LocationUtils.isValidLocation(location)) {
@@ -381,4 +348,14 @@ public class TripStatisticsUpdater {
return true; return true;
} }
} }
/**
* Updates the calorie value;
*
* @param calorie
*/
public void updateCalorie(double calorie) {
tripStatistics.setCalorie(calorie);
currentSegment.setCalorie(0);
}
} }
@@ -277,16 +277,17 @@ public class CalorieUtils {
* *
* @param context the context * @param context the context
* @param track the track to calculate * @param track the track to calculate
* @param category the category of track * @param startTrackPointId the starting track point id. -1L to ignore
* @return the TripStatisticsUpdater of track * @return the TripStatisticsUpdater of track
*/ */
public static TripStatisticsUpdater updateTrackStatistics(Context context, Track track) { public static TripStatisticsUpdater updateTrackStatistics(Context context,
long startTrackPointId, Track track) {
ActivityType activityType = getActivityType(context, track.getCategory()); ActivityType activityType = getActivityType(context, track.getCategory());
MyTracksProviderUtils providerUtils = MyTracksProviderUtils.Factory.get(context); MyTracksProviderUtils providerUtils = MyTracksProviderUtils.Factory.get(context);
long trackId = track.getId(); long trackId = track.getId();
LocationIterator points = providerUtils.getTrackPointLocationIterator(trackId, -1, false, LocationIterator points = providerUtils.getTrackPointLocationIterator(trackId,
MyTracksProviderUtils.DEFAULT_LOCATION_FACTORY); startTrackPointId, false, MyTracksProviderUtils.DEFAULT_LOCATION_FACTORY);
TripStatisticsUpdater tripStatisticsUpdater = new TripStatisticsUpdater(track TripStatisticsUpdater tripStatisticsUpdater = new TripStatisticsUpdater(track
.getTripStatistics().getStartTime()); .getTripStatistics().getStartTime());
@@ -294,11 +295,15 @@ public class CalorieUtils {
if (activityType == ActivityType.INVALID) { if (activityType == ActivityType.INVALID) {
tripStatisticsUpdater.addLocation(points.next(), PreferencesUtils.getInt(context, tripStatisticsUpdater.addLocation(points.next(), PreferencesUtils.getInt(context,
R.string.recording_distance_interval_key, R.string.recording_distance_interval_key,
PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT), context); PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT), false, activityType,
PreferencesUtils.getInt(context, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT));
} else { } else {
tripStatisticsUpdater.addLocationCalorie(points.next(), PreferencesUtils.getInt(context, tripStatisticsUpdater.addLocation(points.next(), PreferencesUtils.getInt(context,
R.string.recording_distance_interval_key, R.string.recording_distance_interval_key,
PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT), activityType, context); PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT), true, activityType,
PreferencesUtils.getInt(context, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT));
} }
} }
track.setTripStatistics(tripStatisticsUpdater.getTripStatistics()); track.setTripStatistics(tripStatisticsUpdater.getTripStatistics());
@@ -17,7 +17,6 @@
package com.google.android.apps.mytracks.util; package com.google.android.apps.mytracks.util;
import com.google.android.apps.mytracks.TrackEditActivity; import com.google.android.apps.mytracks.TrackEditActivity;
import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.WaypointCreationRequest; import com.google.android.apps.mytracks.content.WaypointCreationRequest;
import com.google.android.apps.mytracks.services.ITrackRecordingService; import com.google.android.apps.mytracks.services.ITrackRecordingService;
import com.google.android.apps.mytracks.services.TrackRecordingService; import com.google.android.apps.mytracks.services.TrackRecordingService;
@@ -199,16 +198,15 @@ public class TrackRecordingServiceConnectionUtils {
* Updates the calorie of current recording track. * Updates the calorie of current recording track.
* *
* @param trackRecordingServiceConnection * @param trackRecordingServiceConnection
* @param track the current recording track
*/ */
public static void updateCalorie(TrackRecordingServiceConnection trackRecordingServiceConnection, Track track) { public static void updateCalorie(TrackRecordingServiceConnection trackRecordingServiceConnection) {
ITrackRecordingService trackRecordingService = trackRecordingServiceConnection ITrackRecordingService trackRecordingService = trackRecordingServiceConnection
.getServiceIfBound(); .getServiceIfBound();
if (trackRecordingService == null) { if (trackRecordingService == null) {
Log.d(TAG, "Unable to update calorie, no track recording service"); Log.d(TAG, "Unable to update calorie, no track recording service");
} else { } else {
try { try {
trackRecordingService.updateTripStatistics(track); trackRecordingService.updateCalorie();
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Unable to update calorie", e); Log.e(TAG, "Unable to update calorie", e);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
@@ -1,2 +0,0 @@
package com.google.android.apps.mytracks.content;
parcelable Track;
@@ -16,7 +16,6 @@
package com.google.android.apps.mytracks.services; package com.google.android.apps.mytracks.services;
import com.google.android.apps.mytracks.content.WaypointCreationRequest; import com.google.android.apps.mytracks.content.WaypointCreationRequest;
import com.google.android.apps.mytracks.content.Track;
/** /**
* MyTracks service. * MyTracks service.
@@ -109,9 +108,7 @@ interface ITrackRecordingService {
int getSensorState(); int getSensorState();
/** /**
* Updates the TripStatistics of current recording track. * Updates the calorie of current recording track.
*
* @param track the current recording track
*/ */
void updateTripStatistics(in Track track); void updateCalorie();
} }