From 651034e2a8b9a2b418b394b9915703eef03570c3 Mon Sep 17 00:00:00 2001 From: youtaol Date: Thu, 10 Oct 2013 19:19:02 +0800 Subject: [PATCH] Update the TripStatisticsUpdater after change the activity type of current recording calorie. Using the activity type but calorie value to determine whether show activity set button or not. --- .../apps/mytracks/TrackEditActivity.java | 19 ++++++++++++ .../services/TrackRecordingService.java | 29 +++++++++++++++++++ .../mytracks/stats/TripStatisticsUpdater.java | 9 ++++++ .../apps/mytracks/util/CalorieUtils.java | 8 ++--- .../apps/mytracks/util/StatsUtils.java | 10 ++++++- .../TrackRecordingServiceConnectionUtils.java | 20 +++++++++++++ .../services/ITrackRecordingService.aidl | 7 +++++ .../apps/mytracks/stats/TripStatistics.java | 18 ++---------- 8 files changed, 100 insertions(+), 20 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/TrackEditActivity.java b/MyTracks/src/com/google/android/apps/mytracks/TrackEditActivity.java index 70cd0c416..0a479b78e 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/TrackEditActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/TrackEditActivity.java @@ -20,10 +20,12 @@ import com.google.android.apps.mytracks.content.MyTracksProviderUtils; import com.google.android.apps.mytracks.content.Track; import com.google.android.apps.mytracks.fragments.ChooseActivityTypeDialogFragment; import com.google.android.apps.mytracks.fragments.ChooseActivityTypeDialogFragment.ChooseActivityTypeCaller; +import com.google.android.apps.mytracks.services.TrackRecordingServiceConnection; import com.google.android.apps.mytracks.util.ApiAdapterFactory; import com.google.android.apps.mytracks.util.CalorieUtils; import com.google.android.apps.mytracks.util.TrackIconUtils; import com.google.android.apps.mytracks.util.TrackNameUtils; +import com.google.android.apps.mytracks.util.TrackRecordingServiceConnectionUtils; import com.google.android.maps.mytracks.R; import android.graphics.Bitmap; @@ -58,6 +60,7 @@ public class TrackEditActivity extends AbstractMyTracksActivity private static final String ICON_VALUE_KEY = "icon_value_key"; private Long trackId; + private TrackRecordingServiceConnection trackRecordingServiceConnection; private MyTracksProviderUtils myTracksProviderUtils; private Track track; private String iconValue; @@ -71,6 +74,7 @@ public class TrackEditActivity extends AbstractMyTracksActivity protected void onCreate(Bundle bundle) { super.onCreate(bundle); + trackRecordingServiceConnection = new TrackRecordingServiceConnection(this, null); trackId = getIntent().getLongExtra(EXTRA_TRACK_ID, -1L); if (trackId == -1L) { Log.e(TAG, "invalid trackId"); @@ -185,7 +189,10 @@ public class TrackEditActivity extends AbstractMyTracksActivity // TODO Is there no race condition when setCalorie is called. double calorie = CalorieUtils.calculateTrackCalorie(getApplicationContext(), track, category); + track.getTripStatistics().setCalorie(calorie); + TrackRecordingServiceConnectionUtils.updateCalorie(trackRecordingServiceConnection, + calorie); } track.setCategory(category); track.setIcon(TrackIconUtils.getIconValue(TrackEditActivity.this, category)); @@ -216,6 +223,18 @@ public class TrackEditActivity extends AbstractMyTracksActivity cancel.setVisibility(View.VISIBLE); } } + + @Override + protected void onStart() { + super.onStart(); + TrackRecordingServiceConnectionUtils.startConnection(this, trackRecordingServiceConnection); + } + + @Override + protected void onStop() { + super.onStop(); + trackRecordingServiceConnection.unbind(); + } @Override public void onSaveInstanceState(Bundle outState) { 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 0c2b6dcf2..8e33cb9c6 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java @@ -1333,5 +1333,34 @@ public class TrackRecordingService extends Service { deathRecipient.binderDied(); } } + + @Override + public void updateCalorie(double calorie) { + if (!canAccess()) { + return; + } + trackRecordingService.updateCalorie(calorie); + } + } + + /** + * Updates the calorie value. + * + * @param calorie new calorie value. + */ + public void updateCalorie(final double calorie) { + if (myTracksLocationManager == null || executorService == null + || !myTracksLocationManager.isAllowed() || executorService.isShutdown() + || executorService.isTerminated()) { + return; + } + executorService.submit(new Runnable() { + @Override + public void run() { + trackTripStatisticsUpdater.updateCalorie(calorie); + } + }); + + } } 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 6da2a4026..c153dad19 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/stats/TripStatisticsUpdater.java +++ b/MyTracks/src/com/google/android/apps/mytracks/stats/TripStatisticsUpdater.java @@ -223,6 +223,15 @@ public class TripStatisticsUpdater { lastLocation = location; lastMovingLocation = location; } + + /** + * Updates the calorie value. + * + * @param calorie new calorie value. + */ + public void updateCalorie(double calorie) { + currentSegment.setCalorie(calorie); + } /** * Gets the smoothed elevation over several readings. The elevation readings 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 2d3593893..e17d0fad9 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/CalorieUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/CalorieUtils.java @@ -18,7 +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.LocationIterator; import com.google.android.apps.mytracks.content.Track; -import com.google.android.apps.mytracks.stats.TripStatistics; import com.google.android.maps.mytracks.R; import com.google.common.annotations.VisibleForTesting; @@ -220,7 +219,7 @@ public class CalorieUtils { public static double getCalorie(Location start, Location stop, double grade, int weight, ActivityType activityType) { if (activityType == ActivityType.INVALID) { - return TripStatistics.CALORIE_UNDEFINED; + return 0.0; } return ActivityType.CYCLING == activityType ? calculateCalorieCycling(start, stop, grade, weight) : calculateCalorieFoot(start, stop, grade, weight); @@ -259,7 +258,7 @@ public class CalorieUtils { * @param category the category of track * @return activityType the activity type of track. */ - private static ActivityType getActivityType(Context context, String category) { + public static ActivityType getActivityType(Context context, String category) { ActivityType activityType = ActivityType.INVALID; if (category.equals(context.getString(R.string.activity_type_walking)) || category.equals(context.getString(R.string.activity_type_running))) { @@ -282,7 +281,7 @@ public class CalorieUtils { public static double calculateTrackCalorie(Context context, Track track, String category) { ActivityType activityType = getActivityType(context, category); if (activityType == ActivityType.INVALID) { - return TripStatistics.CALORIE_UNDEFINED; + return 0.0; } double calorie = 0.0; @@ -303,4 +302,5 @@ public class CalorieUtils { } return calorie; } + } \ No newline at end of file diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/StatsUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/StatsUtils.java index dc9c2945e..a481af233 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/StatsUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/StatsUtils.java @@ -17,7 +17,9 @@ package com.google.android.apps.mytracks.util; import com.google.android.apps.mytracks.TrackEditActivity; +import com.google.android.apps.mytracks.content.MyTracksProviderUtils; import com.google.android.apps.mytracks.stats.TripStatistics; +import com.google.android.apps.mytracks.util.CalorieUtils.ActivityType; import com.google.android.maps.mytracks.R; import android.app.Activity; @@ -186,6 +188,9 @@ public class StatsUtils { View calorieContainer = activity.findViewById(R.id.stats_calorie_container); if (showCalorie) { + if(trackId == PreferencesUtils.RECORDING_TRACK_ID_DEFAULT) { + return; + } calorieHorizontalLine.setVisibility(View.VISIBLE); calorieContainer.setVisibility(View.VISIBLE); double calories = Double.NaN; @@ -353,8 +358,11 @@ public class StatsUtils { String calorieString; View view = activity.findViewById(itemId); Button button = (Button) activity.findViewById(R.id.stats_calorie_setup); + MyTracksProviderUtils providerUtils = MyTracksProviderUtils.Factory.get(activity); + ActivityType activityType = CalorieUtils.getActivityType(activity, + providerUtils.getTrack(trackId).getCategory()); // Current activity type is not supported. - if (calorie == TripStatistics.CALORIE_UNDEFINED) { + if (activityType == ActivityType.INVALID) { button.setVisibility(View.VISIBLE); view.setVisibility(View.GONE); 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 f84351386..afe817669 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/TrackRecordingServiceConnectionUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/TrackRecordingServiceConnectionUtils.java @@ -193,4 +193,24 @@ public class TrackRecordingServiceConnectionUtils { Toast.makeText(context, R.string.marker_add_error, Toast.LENGTH_LONG).show(); return -1L; } + + /** + * Updates the calorie of current recording track. + */ + public static void updateCalorie(TrackRecordingServiceConnection trackRecordingServiceConnection, + double calorie) { + ITrackRecordingService trackRecordingService = trackRecordingServiceConnection + .getServiceIfBound(); + if (trackRecordingService == null) { + Log.d(TAG, "Unable to update calorie, no track recording service"); + } else { + try { + trackRecordingService.updateCalorie(calorie); + } catch (RemoteException e) { + Log.e(TAG, "Unable to update calorie", e); + } catch (IllegalStateException e) { + Log.e(TAG, "Unable to update calorie.", e); + } + } + } } 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 59b80b566..ba0d72833 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/services/ITrackRecordingService.aidl +++ b/MyTracksLib/src/com/google/android/apps/mytracks/services/ITrackRecordingService.aidl @@ -106,4 +106,11 @@ interface ITrackRecordingService { * return a Sensor.SensorState enum value. */ int getSensorState(); + + /** + * Updates the calorie value. + * + * @param calorie new calorie value. + */ + void updateCalorie(double calorie); } diff --git a/MyTracksLib/src/com/google/android/apps/mytracks/stats/TripStatistics.java b/MyTracksLib/src/com/google/android/apps/mytracks/stats/TripStatistics.java index 1f70a66ab..52c2dece6 100644 --- a/MyTracksLib/src/com/google/android/apps/mytracks/stats/TripStatistics.java +++ b/MyTracksLib/src/com/google/android/apps/mytracks/stats/TripStatistics.java @@ -62,14 +62,8 @@ public class TripStatistics implements Parcelable { // The min and max grade seen on this trip. private final ExtremityMonitor gradeExtremities = new ExtremityMonitor(); - /** - * This value means the calorie value is invalid due to activity type is not - * supported. - */ - public static final double CALORIE_UNDEFINED = -1.0; - // The calorie of current track. - private double calorie = CALORIE_UNDEFINED; + private double calorie = 0.0; /** * Default constructor. @@ -128,9 +122,7 @@ public class TripStatistics implements Parcelable { gradeExtremities.update(other.gradeExtremities.getMin()); gradeExtremities.update(other.gradeExtremities.getMax()); } - if (other.calorie != CALORIE_UNDEFINED) { - calorie += other.calorie; - } + calorie += other.calorie; } /** @@ -585,11 +577,7 @@ public class TripStatistics implements Parcelable { * @param calorieAdded add the value to the total calorie */ public void addCalorie(double calorieAdded) { - if (calorieAdded != CALORIE_UNDEFINED) { - calorie = calorie == CALORIE_UNDEFINED ? calorieAdded : calorie + calorieAdded; - } else { - calorie = CALORIE_UNDEFINED; - } + calorie += calorieAdded; } /**