diff --git a/MyTracks/src/com/google/android/apps/mytracks/TrackEditActivity.java b/MyTracks/src/com/google/android/apps/mytracks/TrackEditActivity.java index dfd923776..821c4c152 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/TrackEditActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/TrackEditActivity.java @@ -200,7 +200,7 @@ public class TrackEditActivity extends AbstractMyTracksActivity myTracksProviderUtils.updateTrack(track); TrackRecordingServiceConnectionUtils.updateCalorie(trackRecordingServiceConnection); } else { - CalorieUtils.updateTrackStatistics(getApplicationContext(), -1, track); + CalorieUtils.updateCalorie(getApplicationContext(), -1, track); // Update data with new calorie. myTracksProviderUtils.updateTrack(track); 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 90bbd48db..20daec67a 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java @@ -1358,13 +1358,11 @@ public class TrackRecordingService extends Service { } /** - * Updates the calorie of current recording track after the current - * track is edited by user. + * Updates the calorie of current recording track after the current track is + * edited by user. */ public void updateCalorie() { - if (myTracksLocationManager == null || executorService == null - || !myTracksLocationManager.isAllowed() || executorService.isShutdown() - || executorService.isTerminated()) { + if (executorService == null || executorService.isShutdown() || executorService.isTerminated()) { return; } executorService.submit(new Runnable() { @@ -1373,23 +1371,32 @@ public class TrackRecordingService extends Service { // Update track statistics. long trackId = PreferencesUtils.getLong(getApplicationContext(), R.string.recording_track_id_key); + if (trackId == PreferencesUtils.RECORDING_TRACK_ID_DEFAULT) { + return; + } Track currentTrack = myTracksProviderUtils.getTrack(trackId); - CalorieUtils.updateTrackStatistics(getApplicationContext(), -1, currentTrack); - trackTripStatisticsUpdater.updateCalorie(currentTrack.getTripStatistics().getCalorie()); + if (currentTrack == null) { + return; + } + double newCalorie = CalorieUtils.updateCalorie(getApplicationContext(), -1, currentTrack); + trackTripStatisticsUpdater.updateCalorie(newCalorie); // Update to database. - MyTracksProviderUtils.Factory.get(context).updateTrack(currentTrack); + myTracksProviderUtils.updateTrack(currentTrack); // Update marker statistics. Waypoint lastWayPoint = myTracksProviderUtils.getLastWaypoint(trackId, WaypointType.WAYPOINT); + if (lastWayPoint == null) { + return; + } long lastWayPoint_pointId = myTracksProviderUtils.getTrackPointId(trackId, lastWayPoint.getLocation()); if (lastWayPoint_pointId > 0) { - CalorieUtils.updateTrackStatistics(getApplicationContext(), lastWayPoint_pointId, + newCalorie = CalorieUtils.updateCalorie(getApplicationContext(), lastWayPoint_pointId, currentTrack); } - markerTripStatisticsUpdater.updateCalorie(currentTrack.getTripStatistics().getCalorie()); + markerTripStatisticsUpdater.updateCalorie(newCalorie); } }); } 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 6630d9b3c..0e45efe7c 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/stats/TripStatisticsUpdater.java +++ b/MyTracks/src/com/google/android/apps/mytracks/stats/TripStatisticsUpdater.java @@ -124,14 +124,14 @@ public class TripStatisticsUpdater { * * @param location the location * @param minRecordingDistance the min recording distance - * @param isCalculateCalorie true means calculate calorie + * @param calculateCalorie 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 */ public void addLocation(Location location, int minRecordingDistance, - boolean isCalculateCalorie, ActivityType activityType, int weight) { + boolean calculateCalorie, ActivityType activityType, int weight) { // Always update time updateTime(location.getTime()); if (!LocationUtils.isValidLocation(location)) { @@ -195,7 +195,7 @@ public class TripStatisticsUpdater { location.getTime(), location.getSpeed(), lastLocation.getTime(), lastLocation.getSpeed()); } - if (isCalculateCalorie) { + if (calculateCalorie) { // Update calorie double calorie = CalorieUtils.getCalorie(lastMovingLocation, location, gradeBuffer.getAverage(), weight, activityType); 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 aa18ad403..bdbd90777 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/CalorieUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/CalorieUtils.java @@ -277,10 +277,11 @@ public class CalorieUtils { * * @param context the context * @param track the track to calculate - * @param startTrackPointId the starting track point id. -1L to ignore - * @return the TripStatisticsUpdater of track + * @param startTrackPointId the starting track point id. Make this value less + * than 1 to calculate the entire track + * @return the calorie value of track */ - public static TripStatisticsUpdater updateTrackStatistics(Context context, + public static double updateCalorie(Context context, long startTrackPointId, Track track) { ActivityType activityType = getActivityType(context, track.getCategory()); @@ -307,6 +308,6 @@ public class CalorieUtils { } } track.setTripStatistics(tripStatisticsUpdater.getTripStatistics()); - return tripStatisticsUpdater; + return tripStatisticsUpdater.getTripStatistics().getCalorie(); } } \ No newline at end of file