diff --git a/MyTracks/src/com/google/android/apps/mytracks/TrackEditActivity.java b/MyTracks/src/com/google/android/apps/mytracks/TrackEditActivity.java index a04688b0d..dfd923776 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/TrackEditActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/TrackEditActivity.java @@ -191,13 +191,18 @@ public class TrackEditActivity extends AbstractMyTracksActivity track.setModifiedTime(System.currentTimeMillis()); if (!category.equals(track.getCategory())) { track.setCategory(category); + // If edit recording track. if (track.getId() == PreferencesUtils.getLong(getApplicationContext(), R.string.recording_track_id_key)) { - TrackRecordingServiceConnectionUtils.updateCalorie(trackRecordingServiceConnection, - track); + + // Update data without new calorie. + myTracksProviderUtils.updateTrack(track); + TrackRecordingServiceConnectionUtils.updateCalorie(trackRecordingServiceConnection); } else { - CalorieUtils.updateTrackStatistics(getApplicationContext(), track); + CalorieUtils.updateTrackStatistics(getApplicationContext(), -1, track); + + // Update data with new calorie. myTracksProviderUtils.updateTrack(track); } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/fragments/ChartFragment.java b/MyTracks/src/com/google/android/apps/mytracks/fragments/ChartFragment.java index 5ae8e2aba..150ccb530 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/fragments/ChartFragment.java +++ b/MyTracks/src/com/google/android/apps/mytracks/fragments/ChartFragment.java @@ -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.stats.TripStatistics; 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.PreferencesUtils; import com.google.android.apps.mytracks.util.UnitConversions; @@ -453,7 +454,10 @@ public class ChartFragment extends Fragment implements TrackDataListener { double power = Double.NaN; 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(); if (chartByDistance) { double distance = tripStatistics.getTotalDistance() * UnitConversions.M_TO_KM; diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/importer/AbstractFileTrackImporter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/importer/AbstractFileTrackImporter.java index 7d62b172a..7735a7e03 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/importer/AbstractFileTrackImporter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/importer/AbstractFileTrackImporter.java @@ -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.stats.TripStatistics; 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.LocationUtils; 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. return; } - trackTripStatisticstrackUpdater.addLocation(trackPoint, recordingDistanceInterval, - context); - markerTripStatisticsUpdater.addLocation(trackPoint, recordingDistanceInterval, context); + trackTripStatisticstrackUpdater.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)); + 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()) { trackPoint = null; @@ -546,7 +554,10 @@ abstract class AbstractFileTrackImporter extends DefaultHandler implements Track trackData.tripStatisticsUpdater = new TripStatisticsUpdater( 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.numBufferedLocations++; diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/maps/SendMapsAsyncTask.java b/MyTracks/src/com/google/android/apps/mytracks/io/maps/SendMapsAsyncTask.java index 1444cf82d..3776779f1 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/maps/SendMapsAsyncTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/maps/SendMapsAsyncTask.java @@ -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.SendToGoogleUtils; 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.PreferencesUtils; import com.google.android.common.gdata.AndroidXmlParserFactory; @@ -277,7 +278,11 @@ public class SendMapsAsyncTask extends AbstractSendAsyncTask { 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) { distances.add(tripStatisticsUpdater.getTripStatistics().getTotalDistance()); elevations.add(tripStatisticsUpdater.getSmoothedElevation()); diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java b/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java index 0b2ee435b..90bbd48db 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java @@ -660,9 +660,17 @@ public class TrackRecordingService extends Service { if (cursor.moveToLast()) { do { 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) { - 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()); } @@ -1002,9 +1010,13 @@ public class TrackRecordingService extends Service { try { Uri uri = myTracksProviderUtils.insertTrackPoint(location, track.getId()); long trackPointId = Long.parseLong(uri.getLastPathSegment()); - trackTripStatisticsUpdater.addLocationCalorie(location, recordingDistanceInterval, - CalorieUtils.getActivityType(context, track.getId()), context); - markerTripStatisticsUpdater.addLocation(location, recordingDistanceInterval, context); + trackTripStatisticsUpdater.addLocation(location, recordingDistanceInterval, true, + CalorieUtils.getActivityType(context, track.getId()), PreferencesUtils.getInt(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)); } catch (SQLiteException e) { /* @@ -1337,21 +1349,19 @@ public class TrackRecordingService extends Service { } @Override - public void updateTripStatistics(Track track) { + public void updateCalorie() { if (!canAccess()) { 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. - * - * @param track the current recording track */ - public void updateTripStatistics(final Track track) { + public void updateCalorie() { if (myTracksLocationManager == null || executorService == null || !myTracksLocationManager.isAllowed() || executorService.isShutdown() || executorService.isTerminated()) { @@ -1360,9 +1370,26 @@ public class TrackRecordingService extends Service { executorService.submit(new Runnable() { @Override public void run() { - trackTripStatisticsUpdater = CalorieUtils.updateTrackStatistics(getApplicationContext(), - track); - MyTracksProviderUtils.Factory.get(context).updateTrack(track); + // Update track statistics. + long trackId = PreferencesUtils.getLong(getApplicationContext(), + 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()); } }); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/stats/TripStatisticsUpdater.java b/MyTracks/src/com/google/android/apps/mytracks/stats/TripStatisticsUpdater.java index 5510f0a01..6630d9b3c 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/stats/TripStatisticsUpdater.java +++ b/MyTracks/src/com/google/android/apps/mytracks/stats/TripStatisticsUpdater.java @@ -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.ActivityType; 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 android.content.Context; import android.location.Location; import android.util.Log; @@ -122,49 +119,19 @@ public class TripStatisticsUpdater { 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. * * @param location the location * @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 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 - void addLocation(Location location, int minRecordingDistance, ActivityType activityType, - int weight, boolean isCalculateCalorie) { + public void addLocation(Location location, int minRecordingDistance, + boolean isCalculateCalorie, ActivityType activityType, int weight) { // Always update time updateTime(location.getTime()); if (!LocationUtils.isValidLocation(location)) { @@ -381,4 +348,14 @@ public class TripStatisticsUpdater { return true; } } + + /** + * Updates the calorie value; + * + * @param calorie + */ + public void updateCalorie(double calorie) { + tripStatistics.setCalorie(calorie); + currentSegment.setCalorie(0); + } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/CalorieUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/CalorieUtils.java index 0adea8b89..aa18ad403 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/CalorieUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/CalorieUtils.java @@ -277,16 +277,17 @@ public class CalorieUtils { * * @param context the context * @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 */ - public static TripStatisticsUpdater updateTrackStatistics(Context context, Track track) { + public static TripStatisticsUpdater updateTrackStatistics(Context context, + long startTrackPointId, Track track) { ActivityType activityType = getActivityType(context, track.getCategory()); MyTracksProviderUtils providerUtils = MyTracksProviderUtils.Factory.get(context); long trackId = track.getId(); - LocationIterator points = providerUtils.getTrackPointLocationIterator(trackId, -1, false, - MyTracksProviderUtils.DEFAULT_LOCATION_FACTORY); + LocationIterator points = providerUtils.getTrackPointLocationIterator(trackId, + startTrackPointId, false, MyTracksProviderUtils.DEFAULT_LOCATION_FACTORY); TripStatisticsUpdater tripStatisticsUpdater = new TripStatisticsUpdater(track .getTripStatistics().getStartTime()); @@ -294,11 +295,15 @@ public class CalorieUtils { if (activityType == ActivityType.INVALID) { tripStatisticsUpdater.addLocation(points.next(), PreferencesUtils.getInt(context, 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 { - tripStatisticsUpdater.addLocationCalorie(points.next(), PreferencesUtils.getInt(context, + tripStatisticsUpdater.addLocation(points.next(), PreferencesUtils.getInt(context, 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()); diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/TrackRecordingServiceConnectionUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/TrackRecordingServiceConnectionUtils.java index 165ce6b84..3cd98a5f1 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/TrackRecordingServiceConnectionUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/TrackRecordingServiceConnectionUtils.java @@ -17,7 +17,6 @@ package com.google.android.apps.mytracks.util; 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.services.ITrackRecordingService; import com.google.android.apps.mytracks.services.TrackRecordingService; @@ -199,16 +198,15 @@ public class TrackRecordingServiceConnectionUtils { * Updates the calorie of current recording track. * * @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 .getServiceIfBound(); if (trackRecordingService == null) { Log.d(TAG, "Unable to update calorie, no track recording service"); } else { try { - trackRecordingService.updateTripStatistics(track); + trackRecordingService.updateCalorie(); } catch (RemoteException e) { Log.e(TAG, "Unable to update calorie", e); } catch (IllegalStateException e) { diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/content/Track.aidl b/MyTracksLib/src/com/google/android/apps/mytracks/content/Track.aidl deleted file mode 100644 index 67d3d1de5..000000000 --- a/MyTracksLib/src/com/google/android/apps/mytracks/content/Track.aidl +++ /dev/null @@ -1,2 +0,0 @@ -package com.google.android.apps.mytracks.content; -parcelable Track; \ No newline at end of file diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/services/ITrackRecordingService.aidl b/MyTracksLib/src/com/google/android/apps/mytracks/services/ITrackRecordingService.aidl index 3a369af0e..0ef27b480 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/services/ITrackRecordingService.aidl +++ b/MyTracksLib/src/com/google/android/apps/mytracks/services/ITrackRecordingService.aidl @@ -16,7 +16,6 @@ package com.google.android.apps.mytracks.services; import com.google.android.apps.mytracks.content.WaypointCreationRequest; -import com.google.android.apps.mytracks.content.Track; /** * MyTracks service. @@ -109,9 +108,7 @@ interface ITrackRecordingService { int getSensorState(); /** - * Updates the TripStatistics of current recording track. - * - * @param track the current recording track + * Updates the calorie of current recording track. */ - void updateTripStatistics(in Track track); + void updateCalorie(); }