After user edit the track. Update recorded track statistics immediately and update recording track statistics in track recording service.

Change some code style.
This commit is contained in:
youtaol
2013-10-23 17:14:45 +08:00
parent d64b1c74f4
commit c68ec5ed9a
8 changed files with 83 additions and 116 deletions
@@ -184,24 +184,25 @@ public class TrackEditActivity extends AbstractMyTracksActivity
save.setOnClickListener(new View.OnClickListener() { save.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
track.setName(name.getText().toString());
String category = activityType.getText().toString(); String category = activityType.getText().toString();
if (!category.equals(track.getCategory())) { track.setName(name.getText().toString());
// TODO Is there no race condition when setCalorie is called.
double[] calories = CalorieUtils.calculateTrackCalorie(getApplicationContext(), track,
category);
track.getTripStatistics().setCalorie(calories[0]);
TrackRecordingServiceConnectionUtils.updateCalorie(trackRecordingServiceConnection,
calories);
}
track.setCategory(category);
track.setIcon(TrackIconUtils.getIconValue(TrackEditActivity.this, category)); track.setIcon(TrackIconUtils.getIconValue(TrackEditActivity.this, category));
track.setDescription(description.getText().toString()); track.setDescription(description.getText().toString());
track.setModifiedTime(System.currentTimeMillis()); track.setModifiedTime(System.currentTimeMillis());
myTracksProviderUtils.updateTrack(track); if (!category.equals(track.getCategory())) {
boolean driveSync = PreferencesUtils.getBoolean( track.setCategory(category);
TrackEditActivity.this, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT); // If edit recording track.
if (track.getId() == PreferencesUtils.getLong(getApplicationContext(),
R.string.recording_track_id_key)) {
TrackRecordingServiceConnectionUtils.updateCalorie(trackRecordingServiceConnection,
track);
} else {
CalorieUtils.updateTrackStatistics(getApplicationContext(), track);
myTracksProviderUtils.updateTrack(track);
}
}
boolean driveSync = PreferencesUtils.getBoolean(TrackEditActivity.this,
R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT);
if (driveSync) { if (driveSync) {
PreferencesUtils.addToList(TrackEditActivity.this, R.string.drive_edited_list_key, PreferencesUtils.addToList(TrackEditActivity.this, R.string.drive_edited_list_key,
PreferencesUtils.DRIVE_EDITED_LIST_DEFAULT, String.valueOf(track.getId())); PreferencesUtils.DRIVE_EDITED_LIST_DEFAULT, String.valueOf(track.getId()));
@@ -36,6 +36,7 @@ import com.google.android.apps.mytracks.services.tasks.PeriodicTaskExecutor;
import com.google.android.apps.mytracks.services.tasks.SplitPeriodicTaskFactory; import com.google.android.apps.mytracks.services.tasks.SplitPeriodicTaskFactory;
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.IntentUtils; import com.google.android.apps.mytracks.util.IntentUtils;
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;
@@ -1001,7 +1002,8 @@ 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.addLocation(location, recordingDistanceInterval, context); trackTripStatisticsUpdater.addLocationCalorie(location, recordingDistanceInterval,
CalorieUtils.getActivityType(context, track.getId()), context);
markerTripStatisticsUpdater.addLocation(location, recordingDistanceInterval, context); markerTripStatisticsUpdater.addLocation(location, recordingDistanceInterval, context);
updateRecordingTrack(track, trackPointId, LocationUtils.isValidLocation(location)); updateRecordingTrack(track, trackPointId, LocationUtils.isValidLocation(location));
} catch (SQLiteException e) { } catch (SQLiteException e) {
@@ -1335,21 +1337,21 @@ public class TrackRecordingService extends Service {
} }
@Override @Override
public void updateCalorie(double calorieTotal, double calorieCurrentSegment) { public void updateTripStatistics(Track track) {
if (!canAccess()) { if (!canAccess()) {
return; return;
} }
trackRecordingService.updateCalorie(calorieTotal, calorieCurrentSegment); trackRecordingService.updateTripStatistics(track);
} }
} }
/** /**
* Updates the calorie value. * Updates the TripStatistics of current recording track after the current
* track is edited by user.
* *
* @param calorieTotal the calorie value of entire track * @param track the current recording track
* @param calorieCurrentSegment the calorie value of current segment
*/ */
public void updateCalorie(final double calorieTotal, final double calorieCurrentSegment) { public void updateTripStatistics(final Track track) {
if (myTracksLocationManager == null || executorService == null if (myTracksLocationManager == null || executorService == null
|| !myTracksLocationManager.isAllowed() || executorService.isShutdown() || !myTracksLocationManager.isAllowed() || executorService.isShutdown()
|| executorService.isTerminated()) { || executorService.isTerminated()) {
@@ -1358,7 +1360,9 @@ public class TrackRecordingService extends Service {
executorService.submit(new Runnable() { executorService.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
trackTripStatisticsUpdater.updateCalorie(calorieTotal, calorieCurrentSegment); trackTripStatisticsUpdater = CalorieUtils.updateTrackStatistics(getApplicationContext(),
track);
MyTracksProviderUtils.Factory.get(context).updateTrack(track);
} }
}); });
} }
@@ -130,13 +130,27 @@ public class TripStatisticsUpdater {
* @param context the context to get the weight and track category * @param context the context to get the weight and track category
*/ */
public void addLocation(Location location, int minRecordingDistance, Context context) { public void addLocation(Location location, int minRecordingDistance, Context context) {
int weight = PreferencesUtils.getInt(context, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT);
addLocation( addLocation(
location, location,
minRecordingDistance, minRecordingDistance,
CalorieUtils.getActivityType(context, CalorieUtils.getActivityType(context,
PreferencesUtils.getLong(context, R.string.recording_track_id_key)), weight); 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);
} }
/** /**
@@ -146,13 +160,13 @@ public class TripStatisticsUpdater {
* @param minRecordingDistance the min recording distance * @param minRecordingDistance the min recording distance
* @param activityType the activity type of current track * @param activityType the activity type of current track
* @param weight the weight to calculate calorie * @param weight the weight to calculate calorie
* @param isCalculateCalorie true means calculate calorie
*/ */
@VisibleForTesting @VisibleForTesting
void addLocation(Location location, int minRecordingDistance, ActivityType activityType, void addLocation(Location location, int minRecordingDistance, 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)) {
// Either pause or resume marker // Either pause or resume marker
if (location.getLatitude() == PAUSE_LATITUDE) { if (location.getLatitude() == PAUSE_LATITUDE) {
@@ -214,26 +228,16 @@ public class TripStatisticsUpdater {
location.getTime(), location.getSpeed(), lastLocation.getTime(), lastLocation.getSpeed()); location.getTime(), location.getSpeed(), lastLocation.getTime(), lastLocation.getSpeed());
} }
// Update calorie if (isCalculateCalorie) {
double calorie = CalorieUtils.getCalorie(lastMovingLocation, location, // Update calorie
gradeBuffer.getAverage(), weight, activityType); double calorie = CalorieUtils.getCalorie(lastMovingLocation, location,
currentSegment.addCalorie(calorie); gradeBuffer.getAverage(), weight, activityType);
currentSegment.addCalorie(calorie);
}
lastLocation = location; lastLocation = location;
lastMovingLocation = location; lastMovingLocation = location;
} }
/**
* Updates the calorie value.
*
* @param calorieTotal the calorie value of entire track
* @param calorieCurrentSegment the calorie value of current segment
*/
public void updateCalorie(double calorieTotal, double calorieCurrentSegment) {
tripStatistics.setCalorie(calorieTotal);
currentSegment.setCalorie(calorieCurrentSegment);
}
/** /**
* Gets the smoothed elevation over several readings. The elevation readings * Gets the smoothed elevation over several readings. The elevation readings
* is noisy so the smoothed elevation is better than the raw elevation for * is noisy so the smoothed elevation is better than the raw elevation for
@@ -18,8 +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;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils.LocationIterator; import com.google.android.apps.mytracks.content.MyTracksProviderUtils.LocationIterator;
import com.google.android.apps.mytracks.content.Track; import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.services.TrackRecordingService;
import com.google.android.apps.mytracks.stats.DoubleBuffer;
import com.google.android.apps.mytracks.stats.TripStatisticsUpdater; import com.google.android.apps.mytracks.stats.TripStatisticsUpdater;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
@@ -280,69 +278,30 @@ 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 category the category of track
* @return a double array of calorie value and the size of this array is 2. * @return the TripStatisticsUpdater of track
* The first value is the calorie of entire track and the second value
* is the calorie of current segment
*/ */
public static double[] calculateTrackCalorie(Context context, Track track, String category) { public static TripStatisticsUpdater updateTrackStatistics(Context context, Track track) {
double[] calories = {0.0, 0.0}; ActivityType activityType = getActivityType(context, track.getCategory());
ActivityType activityType = getActivityType(context, category);
if (activityType == ActivityType.INVALID) {
return calories;
}
double calorieTotal = 0.0;
double calorieCurrentSegment = 0.0;
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, -1, false,
MyTracksProviderUtils.DEFAULT_LOCATION_FACTORY); MyTracksProviderUtils.DEFAULT_LOCATION_FACTORY);
DoubleBuffer gradeBuffer = new DoubleBuffer(TripStatisticsUpdater.GRADE_SMOOTHING_FACTOR); TripStatisticsUpdater tripStatisticsUpdater = new TripStatisticsUpdater(track
.getTripStatistics().getStartTime());
if (points.hasNext()) { while (points.hasNext()) {
Location start = points.next(); if (activityType == ActivityType.INVALID) {
tripStatisticsUpdater.addLocation(points.next(), PreferencesUtils.getInt(context,
while (points.hasNext()) { R.string.recording_distance_interval_key,
Location stop = points.next(); PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT), context);
} else {
if (stop.getLatitude() == TrackRecordingService.PAUSE_LATITUDE tripStatisticsUpdater.addLocationCalorie(points.next(), PreferencesUtils.getInt(context,
|| stop.getLatitude() == TrackRecordingService.RESUME_LATITUDE) { R.string.recording_distance_interval_key,
calorieCurrentSegment = 0.0; PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT), activityType, context);
continue;
}
double grade = updateGrade(gradeBuffer, stop.distanceTo(start),
stop.getAltitude() - start.getAltitude());
double calorieAdded = getCalorie(start, stop, grade, PreferencesUtils.getInt(context,
R.string.stats_weight_key, PreferencesUtils.STATS_WEIGHT_DEFAULT), activityType);
calorieTotal += calorieAdded;
calorieCurrentSegment += calorieAdded;
start = stop;
} }
} }
calories[0] = calorieTotal; track.setTripStatistics(tripStatisticsUpdater.getTripStatistics());
calories[1] = calorieCurrentSegment; return tripStatisticsUpdater;
return calories;
} }
/**
* Updates a grade reading.
*
* @param gradeBuffer
* @param distance
* @param rise
* @return
*/
private static double updateGrade(DoubleBuffer gradeBuffer, float distance, Double rise) {
double grade = 0;
if (rise > 0 && distance > 0) {
gradeBuffer.setNext(rise / distance);
grade = gradeBuffer.getAverage();
} else {
gradeBuffer.setNext(0);
}
return grade;
}
} }
@@ -17,6 +17,7 @@
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;
@@ -197,21 +198,17 @@ public class TrackRecordingServiceConnectionUtils {
/** /**
* Updates the calorie of current recording track. * Updates the calorie of current recording track.
* *
* @param trackRecordingServiceConnection the TrackRecordingServiceConnection * @param trackRecordingServiceConnection
* instance * @param track the current recording track
* @param calories a double array of calorie value and the size of this array
* is 2. The first value is the calorie of entire track and the
* second value is the calorie of current segment
*/ */
public static void updateCalorie(TrackRecordingServiceConnection trackRecordingServiceConnection, public static void updateCalorie(TrackRecordingServiceConnection trackRecordingServiceConnection, Track track) {
double[] calories) {
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.updateCalorie(calories[0], calories[1]); trackRecordingService.updateTripStatistics(track);
} 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) {
@@ -0,0 +1,2 @@
package com.google.android.apps.mytracks.content;
parcelable Track;
@@ -16,6 +16,7 @@
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.
@@ -108,10 +109,9 @@ interface ITrackRecordingService {
int getSensorState(); int getSensorState();
/** /**
* Updates the calorie value. * Updates the TripStatistics of current recording track.
* *
* @param calorieTotal the calorie value of entire track * @param track the current recording track
* @param calorieCurrentSegment the calorie value of current segment
*/ */
void updateCalorie(double calorieTotal, double calorieCurrentSegment); void updateTripStatistics(in Track track);
} }
@@ -226,7 +226,7 @@ public class TripStatisticsUpdaterTest extends TestCase {
startTime + (timeOffset + i) * TEN_SECONDS); startTime + (timeOffset + i) * TEN_SECONDS);
tripStatisticsUpdater.addLocation(location, tripStatisticsUpdater.addLocation(location,
PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT, ActivityType.FOOT, PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT, ActivityType.FOOT,
PreferencesUtils.STATS_WEIGHT_DEFAULT); PreferencesUtils.STATS_WEIGHT_DEFAULT, true);
tripStatistics = tripStatisticsUpdater.getTripStatistics(); tripStatistics = tripStatisticsUpdater.getTripStatistics();
assertEquals((timeOffset + i) * TEN_SECONDS, tripStatistics.getTotalTime()); assertEquals((timeOffset + i) * TEN_SECONDS, tripStatistics.getTotalTime());
@@ -273,7 +273,7 @@ public class TripStatisticsUpdaterTest extends TestCase {
locationOffset, locationOffset * .001, 0, startTime + (i + timeOffset) * TEN_SECONDS); locationOffset, locationOffset * .001, 0, startTime + (i + timeOffset) * TEN_SECONDS);
tripStatisticsUpdater.addLocation(location, tripStatisticsUpdater.addLocation(location,
PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT, ActivityType.FOOT, PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT, ActivityType.FOOT,
PreferencesUtils.STATS_WEIGHT_DEFAULT); PreferencesUtils.STATS_WEIGHT_DEFAULT, false);
tripStatistics = tripStatisticsUpdater.getTripStatistics(); tripStatistics = tripStatisticsUpdater.getTripStatistics();
assertEquals((i + timeOffset) * TEN_SECONDS, tripStatistics.getTotalTime()); assertEquals((i + timeOffset) * TEN_SECONDS, tripStatistics.getTotalTime());
@@ -311,7 +311,7 @@ public class TripStatisticsUpdaterTest extends TestCase {
startTime + (timeOffset + i) * TEN_SECONDS); startTime + (timeOffset + i) * TEN_SECONDS);
tripStatisticsUpdater.addLocation(location, tripStatisticsUpdater.addLocation(location,
PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT, ActivityType.FOOT, PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT, ActivityType.FOOT,
PreferencesUtils.STATS_WEIGHT_DEFAULT); PreferencesUtils.STATS_WEIGHT_DEFAULT, true);
tripStatistics = tripStatisticsUpdater.getTripStatistics(); tripStatistics = tripStatisticsUpdater.getTripStatistics();
assertTrue(tripStatistics.getMovingTime() <= tripStatistics.getTotalTime()); assertTrue(tripStatistics.getMovingTime() <= tripStatistics.getTotalTime());