From 25712478292b6d73b59c8be432d81dba2f977bda Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Tue, 19 Nov 2013 13:24:40 -0800 Subject: [PATCH] Update stats waypoint calorie when changing activity type --- .../mytracks/content/MyTracksProvider.java | 38 +++--- .../services/TrackRecordingService.java | 21 +--- .../apps/mytracks/util/CalorieUtils.java | 111 ++++++++++++++++-- .../apps/mytracks/util/TrackUtils.java | 20 ++-- .../content/MyTracksProviderUtilsImpl.java | 7 +- .../apps/mytracks/content/TracksColumns.java | 4 +- .../mytracks/content/WaypointsColumns.java | 6 +- 7 files changed, 149 insertions(+), 58 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/content/MyTracksProvider.java b/MyTracks/src/com/google/android/apps/mytracks/content/MyTracksProvider.java index ba815b0e7..7af423b01 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/content/MyTracksProvider.java +++ b/MyTracks/src/com/google/android/apps/mytracks/content/MyTracksProvider.java @@ -91,50 +91,58 @@ public class MyTracksProvider extends ContentProvider { } else { // Incremental upgrades. One if statement per DB version. - // Add track points SENSOR column + // Add track point SENSOR column if (oldVersion <= 17) { - Log.w(TAG, "Upgrade DB: Adding sensor column."); + Log.w(TAG, "Upgrade DB: Adding track point sensor column."); db.execSQL("ALTER TABLE " + TrackPointsColumns.TABLE_NAME + " ADD " + TrackPointsColumns.SENSOR + " BLOB"); } - // Add tracks TABLEID column + // Add track TABLEID column if (oldVersion <= 18) { - Log.w(TAG, "Upgrade DB: Adding tableid column."); + Log.w(TAG, "Upgrade DB: Adding track tableid column."); db.execSQL("ALTER TABLE " + TracksColumns.TABLE_NAME + " ADD " + TracksColumns.TABLEID + " STRING"); } - // Add tracks ICON column + // Add track ICON column if (oldVersion <= 19) { - Log.w(TAG, "Upgrade DB: Adding icon column."); + Log.w(TAG, "Upgrade DB: Adding track icon column."); db.execSQL( "ALTER TABLE " + TracksColumns.TABLE_NAME + " ADD " + TracksColumns.ICON + " STRING"); } // Add track DRIVEID, MODIFIEDTIME, and SHAREDWITHME columns if (oldVersion <= 20) { - Log.w(TAG, "Upgrade DB: Adding driveid column."); + Log.w(TAG, "Upgrade DB: Adding track driveid column."); db.execSQL("ALTER TABLE " + TracksColumns.TABLE_NAME + " ADD " + TracksColumns.DRIVEID + " STRING"); - Log.w(TAG, "Upgrade DB: Adding modifiedtime column."); + Log.w(TAG, "Upgrade DB: Adding track modifiedtime column."); db.execSQL("ALTER TABLE " + TracksColumns.TABLE_NAME + " ADD " + TracksColumns.MODIFIEDTIME + " INTEGER"); - Log.w(TAG, "Upgrade DB: Adding sharedwithme column."); + + Log.w(TAG, "Upgrade DB: Adding track sharedwithme column."); db.execSQL("ALTER TABLE " + TracksColumns.TABLE_NAME + " ADD " + TracksColumns.SHAREDWITHME + " INTEGER"); - Log.w(TAG, "Upgrade DB: Adding sharedowner column."); + + Log.w(TAG, "Upgrade DB: Adding track sharedowner column."); db.execSQL("ALTER TABLE " + TracksColumns.TABLE_NAME + " ADD " + TracksColumns.SHAREDOWNER + " STRING"); } - + + // Add waypoint CALORIE and PHOTOURL columns. Add track CALORIE column. if (oldVersion <= 21) { - Log.w(TAG, "Upgrade DB: Adding photo url column."); - db.execSQL("ALTER TABLE " + WaypointsColumns.TABLE_NAME + " ADD " + WaypointsColumns.PHOTOURL - + " STRING"); + Log.w(TAG, "Upgrade DB: Adding waypoint calorie column."); + db.execSQL("ALTER TABLE " + WaypointsColumns.TABLE_NAME + " ADD " + + WaypointsColumns.CALORIE + " FLOAT"); + + Log.w(TAG, "Upgrade DB: Adding waypoint photo url column."); + db.execSQL("ALTER TABLE " + WaypointsColumns.TABLE_NAME + " ADD " + + WaypointsColumns.PHOTOURL + " STRING"); + Log.w(TAG, "Upgrade DB: Adding track calorie column."); db.execSQL("ALTER TABLE " + TracksColumns.TABLE_NAME + " ADD " + TracksColumns.CALORIE - + " INTEGER"); + + " FLOAT"); } } } 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 ba4359cdd..978923a98 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java @@ -1379,23 +1379,14 @@ public class TrackRecordingService extends Service { return; } - double calorie = CalorieUtils.getTrackCalorie(context, track, -1L); - - // Update to database - track.getTripStatistics().setCalorie(calorie); - myTracksProviderUtils.updateTrack(track); - + + double[] calories = CalorieUtils.updateTrackCalorie(context, track); + // Update track statistics - trackTripStatisticsUpdater.updateCalorie(calorie); + trackTripStatisticsUpdater.updateCalorie(calories[0]); - // Update marker statistics - Waypoint wayPoint = myTracksProviderUtils.getLastWaypoint( - recordingTrackId, WaypointType.STATISTICS); - long trackPointId = wayPoint != null ? myTracksProviderUtils.getTrackPointId( - recordingTrackId, wayPoint.getLocation()) - : -1L; - calorie = CalorieUtils.getTrackCalorie(context, track, trackPointId); - markerTripStatisticsUpdater.updateCalorie(calorie); + // Update marker statistics + markerTripStatisticsUpdater.updateCalorie(calories[1]); } }); } 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 f27115744..574bb26b7 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/CalorieUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/CalorieUtils.java @@ -18,10 +18,13 @@ 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.content.Waypoint; +import com.google.android.apps.mytracks.content.Waypoint.WaypointType; import com.google.android.apps.mytracks.stats.TripStatisticsUpdater; import com.google.android.maps.mytracks.R; import android.content.Context; +import android.database.Cursor; import android.location.Location; /** @@ -82,21 +85,25 @@ public class CalorieUtils { } /** - * Calculates the track calorie in kcal. + * Updates calories for a track and its waypoints. * * @param context the context * @param track the track - * @param startTrackPointId the starting track point id. -1L to calculate the - * entire track + * @return an array of two doubles, first is the track calorie, second is the + * last statistics waypoint calorie */ - public static double getTrackCalorie(Context context, Track track, long startTrackPointId) { + public static double[] updateTrackCalorie(Context context, Track track) { + MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context); ActivityType activityType = getActivityType(context, track.getCategory()); + if (activityType == ActivityType.INVALID) { - return 0.0; + clearCalorie(myTracksProviderUtils, track); + return new double[] { 0.0, 0.0 }; } - MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context); - TripStatisticsUpdater tripStatisticsUpdater = new TripStatisticsUpdater( + TripStatisticsUpdater trackTripStatisticsUpdater = new TripStatisticsUpdater( + track.getTripStatistics().getStartTime()); + TripStatisticsUpdater markerTripStatisticsUpdater = new TripStatisticsUpdater( track.getTripStatistics().getStartTime()); int recordingDistanceInterval = PreferencesUtils.getInt(context, R.string.recording_distance_interval_key, @@ -104,21 +111,53 @@ public class CalorieUtils { double weight = PreferencesUtils.getFloat( context, R.string.weight_key, PreferencesUtils.WEIGHT_DEFAULT); LocationIterator locationIterator = null; + Cursor cursor = null; try { + Waypoint waypoint = null; + locationIterator = myTracksProviderUtils.getTrackPointLocationIterator( - track.getId(), startTrackPointId, false, MyTracksProviderUtils.DEFAULT_LOCATION_FACTORY); + track.getId(), -1L, false, MyTracksProviderUtils.DEFAULT_LOCATION_FACTORY); + cursor = myTracksProviderUtils.getWaypointCursor(track.getId(), -1L, -1); + + if (cursor != null && cursor.moveToFirst()) { + /* + * Yes, this will skip the first waypoint and that is intentional as the + * first waypoint holds the stats for the track. + */ + waypoint = getNextStatisticsWaypoint(myTracksProviderUtils, cursor); + } while (locationIterator.hasNext()) { - tripStatisticsUpdater.addLocation( - locationIterator.next(), recordingDistanceInterval, true, activityType, weight); + Location location = locationIterator.next(); + trackTripStatisticsUpdater.addLocation( + location, recordingDistanceInterval, true, activityType, weight); + markerTripStatisticsUpdater.addLocation( + location, recordingDistanceInterval, true, activityType, weight); + + if (waypoint != null && waypoint.getLocation().getTime() == location.getTime() + && waypoint.getLocation().getLatitude() == location.getLatitude() + && waypoint.getLocation().getLongitude() == location.getLongitude()) { + waypoint.getTripStatistics() + .setCalorie(markerTripStatisticsUpdater.getTripStatistics().getCalorie()); + myTracksProviderUtils.updateWaypoint(waypoint); + markerTripStatisticsUpdater = new TripStatisticsUpdater(location.getTime()); + waypoint = getNextStatisticsWaypoint(myTracksProviderUtils, cursor); + } } } finally { if (locationIterator != null) { locationIterator.close(); } + if (cursor != null) { + cursor.close(); + } } - return tripStatisticsUpdater.getTripStatistics().getCalorie(); + double trackCalorie = trackTripStatisticsUpdater.getTripStatistics().getCalorie(); + track.getTripStatistics().setCalorie(trackCalorie); + myTracksProviderUtils.updateTrack(track); + return new double[] { + trackCalorie, markerTripStatisticsUpdater.getTripStatistics().getCalorie() }; } /** @@ -177,6 +216,56 @@ public class CalorieUtils { } } + /** + * Clears calorie in the track and its waypoints. + * + * @param myTracksProviderUtils the my tracks provider utils + * @param track the track + */ + private static void clearCalorie(MyTracksProviderUtils myTracksProviderUtils, Track track) { + track.getTripStatistics().setCalorie(0); + myTracksProviderUtils.updateTrack(track); + Cursor cursor = null; + try { + cursor = myTracksProviderUtils.getWaypointCursor(track.getId(), -1L, -1); + if (cursor != null && cursor.moveToFirst()) { + /* + * Yes, this will skip the first waypoint and that is intentional as the + * first waypoint holds the stats for the track. + */ + while (cursor.moveToNext()) { + Waypoint waypoint = myTracksProviderUtils.createWaypoint(cursor); + waypoint.getTripStatistics().setCalorie(0); + myTracksProviderUtils.updateWaypoint(waypoint); + } + } + } finally { + if (cursor != null) { + cursor.close(); + } + } + } + + /** + * Gets the next statistics waypoint from a cursor. + * + * @param myTracksProviderUtils the my tracks provider utils + * @param cursor the cursor + */ + private static Waypoint getNextStatisticsWaypoint( + MyTracksProviderUtils myTracksProviderUtils, Cursor cursor) { + if (cursor == null) { + return null; + } + while (cursor.moveToNext()) { + Waypoint waypoint = myTracksProviderUtils.createWaypoint(cursor); + if (waypoint.getType() == WaypointType.STATISTICS) { + return waypoint; + } + } + return null; + } + /** * Gets the running VO2 in ml/kg/min. This equation is appropriate for speeds * greater than 5 mi/hr (or 3 mi/hr or greater if the subject is truly diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/TrackUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/TrackUtils.java index b838bb200..d59825404 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/TrackUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/TrackUtils.java @@ -39,20 +39,10 @@ public class TrackUtils { track.setName(name); } boolean updateCalorie = false; - boolean isRecording = false; if (category != null) { updateCalorie = !category.equals(track.getCategory()) || newWeight; track.setCategory(category); track.setIcon(TrackIconUtils.getIconValue(context, category)); - if (updateCalorie) { - isRecording = track.getId() - == PreferencesUtils.getLong(context, R.string.recording_track_id_key); - if (!isRecording) { - // Update calorie - double calorie = CalorieUtils.getTrackCalorie(context, track, -1L); - track.getTripStatistics().setCalorie(calorie); - } - } } if (description != null) { @@ -61,9 +51,13 @@ public class TrackUtils { track.setModifiedTime(System.currentTimeMillis()); myTracksProviderUtils.updateTrack(track); - if (updateCalorie && isRecording) { - // Update calorie through track recording service - TrackRecordingServiceConnectionUtils.updateCalorie(trackRecordingServiceConnection); + if (updateCalorie) { + if (track.getId() == PreferencesUtils.getLong(context, R.string.recording_track_id_key)) { + // Update calorie through track recording service + TrackRecordingServiceConnectionUtils.updateCalorie(trackRecordingServiceConnection); + } else { + CalorieUtils.updateTrackCalorie(context, track); + } } boolean driveSync = PreferencesUtils.getBoolean( diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtilsImpl.java b/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtilsImpl.java index 8a5e24274..da77fea69 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtilsImpl.java +++ b/MyTracksLib/src/com/google/android/apps/mytracks/content/MyTracksProviderUtilsImpl.java @@ -394,6 +394,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils { int elevationGainIndex = cursor.getColumnIndexOrThrow(WaypointsColumns.ELEVATIONGAIN); int minGradeIndex = cursor.getColumnIndexOrThrow(WaypointsColumns.MINGRADE); int maxGradeIndex = cursor.getColumnIndexOrThrow(WaypointsColumns.MAXGRADE); + int calorieIndex = cursor.getColumnIndexOrThrow(WaypointsColumns.CALORIE); int photoUrlIndex = cursor.getColumnIndexOrThrow(WaypointsColumns.PHOTOURL); Waypoint waypoint = new Waypoint(); @@ -496,7 +497,10 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils { tripStatistics.setMaxGrade(cursor.getFloat(maxGradeIndex)); hasTripStatistics = true; } - + if (!cursor.isNull(calorieIndex)) { + tripStatistics.setCalorie(cursor.getFloat(calorieIndex)); + hasTripStatistics = true; + } if (hasTripStatistics) { waypoint.setTripStatistics(tripStatistics); } @@ -739,6 +743,7 @@ public class MyTracksProviderUtilsImpl implements MyTracksProviderUtils { values.put(WaypointsColumns.ELEVATIONGAIN, tripStatistics.getTotalElevationGain()); values.put(WaypointsColumns.MINGRADE, tripStatistics.getMinGrade()); values.put(WaypointsColumns.MAXGRADE, tripStatistics.getMaxGrade()); + values.put(WaypointsColumns.CALORIE, tripStatistics.getCalorie()); } values.put(WaypointsColumns.PHOTOURL, waypoint.getPhotoUrl()); diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/content/TracksColumns.java b/MyTracksLib/src/com/google/android/apps/mytracks/content/TracksColumns.java index 4535da26a..f2e50102a 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/content/TracksColumns.java +++ b/MyTracksLib/src/com/google/android/apps/mytracks/content/TracksColumns.java @@ -124,7 +124,7 @@ public interface TracksColumns extends BaseColumns { + MODIFIEDTIME + " INTEGER, " // modified time + SHAREDWITHME + " INTEGER, " // shared with me + SHAREDOWNER + " STRING, " // shared owner - + CALORIE + " INTEGER);"; // calorie + + CALORIE + " FLOAT);"; // calorie public static final String[] COLUMNS = { _ID, // id NAME, // name @@ -190,6 +190,6 @@ public interface TracksColumns extends BaseColumns { ContentTypeIds.LONG_TYPE_ID, // modified time ContentTypeIds.BOOLEAN_TYPE_ID, // shared with me ContentTypeIds.STRING_TYPE_ID, // shared owner - ContentTypeIds.INT_TYPE_ID // calorie + ContentTypeIds.FLOAT_TYPE_ID // calorie }; } diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/content/WaypointsColumns.java b/MyTracksLib/src/com/google/android/apps/mytracks/content/WaypointsColumns.java index a6e910255..eb61b72a3 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/content/WaypointsColumns.java +++ b/MyTracksLib/src/com/google/android/apps/mytracks/content/WaypointsColumns.java @@ -70,7 +70,8 @@ public interface WaypointsColumns extends BaseColumns { public static final String ELEVATIONGAIN = "elevationgain"; // elevation gain public static final String MINGRADE = "mingrade"; // min grade public static final String MAXGRADE = "maxgrade"; // max grade - + public static final String CALORIE = "calorie"; // calorie + public static final String PHOTOURL = "photoUrl"; // url for the photo public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" @@ -104,6 +105,7 @@ public interface WaypointsColumns extends BaseColumns { + ELEVATIONGAIN + " FLOAT, " + MINGRADE + " FLOAT, " + MAXGRADE + " FLOAT, " + + CALORIE + " FLOAT, " + PHOTOURL + " STRING" + ");"; @@ -138,6 +140,7 @@ public interface WaypointsColumns extends BaseColumns { ELEVATIONGAIN, MINGRADE, MAXGRADE, + CALORIE, PHOTOURL }; @@ -172,6 +175,7 @@ public interface WaypointsColumns extends BaseColumns { FLOAT_TYPE_ID, // elevation gain FLOAT_TYPE_ID, // min grade FLOAT_TYPE_ID, // max grade + FLOAT_TYPE_ID, // calorie STRING_TYPE_ID // photo url }; }