From 4b1eea2ed51e29a232be8db6510889a5f2a1d467 Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Fri, 1 Nov 2013 11:28:06 -0700 Subject: [PATCH] Fix StatsUtils to work with views from fragment --- .../mytracks/AggregatedStatsActivity.java | 6 +- .../fragments/MarkerDetailFragment.java | 6 +- .../mytracks/fragments/StatsFragment.java | 14 +- .../apps/mytracks/util/StatsUtils.java | 259 +++++++++--------- 4 files changed, 146 insertions(+), 139 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/AggregatedStatsActivity.java b/MyTracks/src/com/google/android/apps/mytracks/AggregatedStatsActivity.java index 32c8c417d..f66e70873 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/AggregatedStatsActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/AggregatedStatsActivity.java @@ -37,9 +37,9 @@ public class AggregatedStatsActivity extends AbstractMyTracksActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - StatsUtils.setTripStatisticsValues(this, getTripStatistics(), - PreferencesUtils.RECORDING_TRACK_ID_DEFAULT); - StatsUtils.setLocationValues(this, null, false); + StatsUtils.setTripStatisticsValues( + this, this, null, getTripStatistics(), PreferencesUtils.RECORDING_TRACK_ID_DEFAULT); + StatsUtils.setLocationValues(this, this, null, null, false); } @Override diff --git a/MyTracks/src/com/google/android/apps/mytracks/fragments/MarkerDetailFragment.java b/MyTracks/src/com/google/android/apps/mytracks/fragments/MarkerDetailFragment.java index 663441fb5..1f18c75f7 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/fragments/MarkerDetailFragment.java +++ b/MyTracks/src/com/google/android/apps/mytracks/fragments/MarkerDetailFragment.java @@ -252,9 +252,9 @@ public class MarkerDetailFragment extends Fragment { TextView name = (TextView) getView().findViewById(R.id.marker_detail_statistics_name); setTextView(name, waypoint.getName()); - StatsUtils.setTripStatisticsValues( - getActivity(), waypoint.getTripStatistics(), PreferencesUtils.RECORDING_TRACK_ID_DEFAULT); - StatsUtils.setLocationValues(getActivity(), waypoint.getLocation(), false); + StatsUtils.setTripStatisticsValues(getActivity(), null, getView(), + waypoint.getTripStatistics(), PreferencesUtils.RECORDING_TRACK_ID_DEFAULT); + StatsUtils.setLocationValues(getActivity(), null, getView(), waypoint.getLocation(), false); } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/fragments/StatsFragment.java b/MyTracks/src/com/google/android/apps/mytracks/fragments/StatsFragment.java index b2d1352ae..474a682b9 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/fragments/StatsFragment.java +++ b/MyTracks/src/com/google/android/apps/mytracks/fragments/StatsFragment.java @@ -155,7 +155,8 @@ public class StatsFragment extends Fragment implements TrackDataListener { lastLocation = null; } } - StatsUtils.setLocationValues(getActivity(), lastLocation, isSelectedTrackRecording()); + StatsUtils.setLocationValues( + getActivity(), getActivity(), null, lastLocation, isSelectedTrackRecording()); } } }); @@ -259,11 +260,10 @@ public class StatsFragment extends Fragment implements TrackDataListener { * Updates the UI. */ private void updateUi(FragmentActivity activity) { - StatsUtils.setTripStatisticsValues( - activity, - lastTripStatistics, - trackDataHub == null ? PreferencesUtils.RECORDING_TRACK_ID_DEFAULT : trackDataHub - .getSelectedTrackId()); - StatsUtils.setLocationValues(activity, lastLocation, isSelectedTrackRecording()); + StatsUtils.setTripStatisticsValues(activity, activity, null, lastTripStatistics, + trackDataHub == null ? PreferencesUtils.RECORDING_TRACK_ID_DEFAULT + : trackDataHub.getSelectedTrackId()); + StatsUtils.setLocationValues( + activity, activity, null, lastLocation, isSelectedTrackRecording()); } } 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 e9d7a4966..f3170030d 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/StatsUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/StatsUtils.java @@ -16,18 +16,16 @@ 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.content.Track; 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; -import android.content.Intent; +import android.content.Context; import android.location.Location; import android.view.View; -import android.view.View.OnClickListener; -import android.widget.Button; import android.widget.TextView; import java.util.Locale; @@ -45,54 +43,61 @@ public class StatsUtils { private static final String ELEVATION_FORMAT = "%1$.2f"; private static final String GRADE_FORMAT = "%1$d"; private static final String CALORIES_FORMAT = "%1$.0f"; - + private StatsUtils() {} /** * Sets the location values. * - * @param activity the activity + * @param context the context + * @param activity the activity for finding views. If null, the view cannot be + * null + * @param view the containing view for finding views. If null, the activity + * cannot be null * @param location the location * @param isRecording true if recording */ - public static void setLocationValues(Activity activity, Location location, boolean isRecording) { - boolean metricUnits = PreferencesUtils.isMetricUnits(activity); - boolean reportSpeed = PreferencesUtils.isReportSpeed(activity); + public static void setLocationValues( + Context context, Activity activity, View view, Location location, boolean isRecording) { + boolean metricUnits = PreferencesUtils.isMetricUnits(context); + boolean reportSpeed = PreferencesUtils.isReportSpeed(context); // Set speed/pace double speed = isRecording && location != null && location.hasSpeed() ? location.getSpeed() : Double.NaN; - setSpeed(activity, R.id.stats_speed, R.string.stats_speed, R.string.stats_pace, speed, - metricUnits, reportSpeed); + setSpeed(context, getView(activity, view, R.id.stats_speed), R.string.stats_speed, + R.string.stats_pace, speed, metricUnits, reportSpeed); // Set elevation - boolean showGradeElevation = PreferencesUtils.getBoolean(activity, + boolean showGradeElevation = PreferencesUtils.getBoolean(context, R.string.stats_show_grade_elevation_key, PreferencesUtils.STATS_SHOW_GRADE_ELEVATION_DEFAULT); - View elevation = activity.findViewById(R.id.stats_elevation); + View elevation = getView(activity, view, R.id.stats_elevation); if (showGradeElevation && isRecording) { double altitude = location != null && location.hasAltitude() ? location.getAltitude() : Double.NaN; elevation.setVisibility(View.VISIBLE); - setElevationValue(activity, R.id.stats_elevation, -1, altitude, metricUnits); + setElevationValue(context, elevation, -1, altitude, metricUnits); } else { elevation.setVisibility(View.GONE); } // Set coordinate - boolean showCoordinate = PreferencesUtils.getBoolean(activity, + boolean showCoordinate = PreferencesUtils.getBoolean(context, R.string.stats_show_coordinate_key, PreferencesUtils.STATS_SHOW_COORDINATE_DEFAULT); - View coordinateHorizontalLine = activity.findViewById(R.id.stats_coordinate_horizontal_line); - View coordinateContainer = activity.findViewById(R.id.stats_coordinate_container); + View coordinateHorizontalLine = getView(activity, view, R.id.stats_coordinate_horizontal_line); + View coordinateContainer = getView(activity, view, R.id.stats_coordinate_container); if (showCoordinate && isRecording) { double latitude = location != null ? location.getLatitude() : Double.NaN; double longitude = location != null ? location.getLongitude() : Double.NaN; coordinateHorizontalLine.setVisibility(View.VISIBLE); coordinateContainer.setVisibility(View.VISIBLE); - setCoordinateValue(activity, R.id.stats_latitude, R.string.stats_latitude, latitude); - setCoordinateValue(activity, R.id.stats_longitude, R.string.stats_longitude, longitude); + setCoordinateValue( + context, getView(activity, view, R.id.stats_latitude), R.string.stats_latitude, latitude); + setCoordinateValue(context, getView(activity, view, R.id.stats_longitude), + R.string.stats_longitude, longitude); } else { coordinateHorizontalLine.setVisibility(View.GONE); coordinateContainer.setVisibility(View.GONE); @@ -106,57 +111,66 @@ public class StatsUtils { * @param totalTime the total time */ public static void setTotalTimeValue(Activity activity, long totalTime) { - setTimeValue(activity, R.id.stats_total_time, R.string.stats_total_time, totalTime); + setTimeValue(activity, activity.findViewById(R.id.stats_total_time), R.string.stats_total_time, + totalTime); } /** * Sets the trip statistics values. * - * @param activity the activity + * @param context the context + * @param activity the activity for finding views. If null, then view cannot + * be null + * @param view the containing view for finding views. If null, the activity + * cannot be null * @param tripStatistics the trip statistics * @param trackId the id of track, which is used to set calorie value. Does * not handle the calorie If the value is * {@link PreferencesUtils#RECORDING_TRACK_ID_DEFAULT} */ - public static void setTripStatisticsValues(Activity activity, TripStatistics tripStatistics, long trackId) { - boolean metricUnits = PreferencesUtils.isMetricUnits(activity); - boolean reportSpeed = PreferencesUtils.isReportSpeed(activity); + public static void setTripStatisticsValues( + Context context, Activity activity, View view, TripStatistics tripStatistics, long trackId) { + boolean metricUnits = PreferencesUtils.isMetricUnits(context); + boolean reportSpeed = PreferencesUtils.isReportSpeed(context); // Set total distance double totalDistance = tripStatistics == null ? Double.NaN : tripStatistics.getTotalDistance(); - setDistanceValue(activity, totalDistance, metricUnits); + setDistanceValue( + context, getView(activity, view, R.id.stats_distance), totalDistance, metricUnits); // Set total time - setTimeValue(activity, R.id.stats_total_time, R.string.stats_total_time, + setTimeValue(context, getView(activity, view, R.id.stats_total_time), R.string.stats_total_time, tripStatistics != null ? tripStatistics.getTotalTime() : -1L); // Set average speed/pace double averageSpeed = tripStatistics != null ? tripStatistics.getAverageSpeed() : Double.NaN; - setSpeed(activity, R.id.stats_average_speed, R.string.stats_average_speed, - R.string.stats_average_pace, averageSpeed, metricUnits, reportSpeed); + setSpeed(context, getView(activity, view, R.id.stats_average_speed), + R.string.stats_average_speed, R.string.stats_average_pace, averageSpeed, metricUnits, + reportSpeed); // Set moving time - setTimeValue(activity, R.id.stats_moving_time, R.string.stats_moving_time, - tripStatistics != null ? tripStatistics.getMovingTime() : -1L); + setTimeValue(context, getView(activity, view, R.id.stats_moving_time), + R.string.stats_moving_time, tripStatistics != null ? tripStatistics.getMovingTime() : -1L); // Set average moving speed/pace double averageMovingSpeed = tripStatistics != null ? tripStatistics.getAverageMovingSpeed() : Double.NaN; - setSpeed(activity, R.id.stats_average_moving_time, R.string.stats_average_moving_speed, - R.string.stats_average_moving_pace, averageMovingSpeed, metricUnits, reportSpeed); + setSpeed(context, getView(activity, view, R.id.stats_average_moving_time), + R.string.stats_average_moving_speed, R.string.stats_average_moving_pace, averageMovingSpeed, + metricUnits, reportSpeed); // Set max speed/pace double maxSpeed = tripStatistics == null ? Double.NaN : tripStatistics.getMaxSpeed(); - setSpeed(activity, R.id.stats_max_speed, R.string.stats_max_speed, R.string.stats_fastest_pace, - maxSpeed, metricUnits, reportSpeed); + setSpeed(context, getView(activity, view, R.id.stats_max_speed), R.string.stats_max_speed, + R.string.stats_fastest_pace, maxSpeed, metricUnits, reportSpeed); // Set grade/elevation - boolean showGradeElevation = PreferencesUtils.getBoolean(activity, + boolean showGradeElevation = PreferencesUtils.getBoolean(context, R.string.stats_show_grade_elevation_key, PreferencesUtils.STATS_SHOW_GRADE_ELEVATION_DEFAULT); - View gradeElevationHorizontalLine = activity.findViewById( - R.id.stats_grade_elevation_horizontal_line); - View gradeElevationContainer = activity.findViewById(R.id.stats_grade_elevation_container); + View gradeElevationHorizontalLine = getView( + activity, view, R.id.stats_grade_elevation_horizontal_line); + View gradeElevationContainer = getView(activity, view, R.id.stats_grade_elevation_container); if (showGradeElevation) { gradeElevationHorizontalLine.setVisibility(View.VISIBLE); @@ -164,31 +178,33 @@ public class StatsUtils { // Set grade double minGrade = tripStatistics == null ? Double.NaN : tripStatistics.getMinGrade(); double maxGrade = tripStatistics == null ? Double.NaN : tripStatistics.getMaxGrade(); - setGradeValue(activity, R.id.stats_grade_min, R.string.stats_min, minGrade); - setGradeValue(activity, R.id.stats_grade_max, R.string.stats_max, maxGrade); + setGradeValue( + context, getView(activity, view, R.id.stats_grade_min), R.string.stats_min, minGrade); + setGradeValue( + context, getView(activity, view, R.id.stats_grade_max), R.string.stats_max, maxGrade); // Set elevation double elevationGain = tripStatistics == null ? Double.NaN : tripStatistics.getTotalElevationGain(); double minElevation = tripStatistics == null ? Double.NaN : tripStatistics.getMinElevation(); double maxElevation = tripStatistics == null ? Double.NaN : tripStatistics.getMaxElevation(); - setElevationValue( - activity, R.id.stats_elevation_gain, R.string.stats_gain, elevationGain, metricUnits); - setElevationValue( - activity, R.id.stats_elevation_min, R.string.stats_min, minElevation, metricUnits); - setElevationValue( - activity, R.id.stats_elevation_max, R.string.stats_max, maxElevation, metricUnits); + setElevationValue(context, getView(activity, view, R.id.stats_elevation_gain), + R.string.stats_gain, elevationGain, metricUnits); + setElevationValue(context, getView(activity, view, R.id.stats_elevation_min), + R.string.stats_min, minElevation, metricUnits); + setElevationValue(context, getView(activity, view, R.id.stats_elevation_max), + R.string.stats_max, maxElevation, metricUnits); } else { gradeElevationHorizontalLine.setVisibility(View.GONE); gradeElevationContainer.setVisibility(View.GONE); } - + if (trackId != PreferencesUtils.RECORDING_TRACK_ID_DEFAULT) { // Set calories - boolean showCalorie = PreferencesUtils.getBoolean(activity, R.string.stats_show_calorie_key, - PreferencesUtils.STATS_SHOW_CALORIE_DEFAULT); - View calorieHorizontalLine = activity.findViewById(R.id.stats_calorie_horizontal_line); - View calorieContainer = activity.findViewById(R.id.stats_calorie_container); + boolean showCalorie = PreferencesUtils.getBoolean( + context, R.string.stats_show_calorie_key, PreferencesUtils.STATS_SHOW_CALORIE_DEFAULT); + View calorieHorizontalLine = getView(activity, view, R.id.stats_calorie_horizontal_line); + View calorieContainer = getView(activity, view, R.id.stats_calorie_container); if (showCalorie) { calorieHorizontalLine.setVisibility(View.VISIBLE); calorieContainer.setVisibility(View.VISIBLE); @@ -196,7 +212,11 @@ public class StatsUtils { if (tripStatistics != null) { calories = tripStatistics.getCalorie(); } - setCalorie(activity, R.id.stats_calorie, calories, trackId); + MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context); + Track track = myTracksProviderUtils.getTrack(trackId); + ActivityType activityType = track == null ? ActivityType.INVALID + : CalorieUtils.getActivityType(context, track.getCategory()); + setCalorie(context, getView(activity, view, R.id.stats_calorie), activityType, calories); } else { calorieHorizontalLine.setVisibility(View.GONE); calorieContainer.setVisibility(View.GONE); @@ -207,57 +227,58 @@ public class StatsUtils { /** * Sets speed. * - * @param activity the activity - * @param itemId the item id + * @param context the context + * @param view the containing view * @param speedLabelId the speed label id * @param paceLabelId the pace label id * @param speed the speed in meters per second * @param metricUnits true if metric units * @param reportSpeed true if report speed */ - private static void setSpeed(Activity activity, int itemId, int speedLabelId, int paceLabelId, + private static void setSpeed(Context context, View view, int speedLabelId, int paceLabelId, double speed, boolean metricUnits, boolean reportSpeed) { - String parts[] = StringUtils.getSpeedParts(activity, speed, metricUnits, reportSpeed); - setItem(activity, itemId, reportSpeed ? speedLabelId : paceLabelId, parts[0], parts[1]); + String parts[] = StringUtils.getSpeedParts(context, speed, metricUnits, reportSpeed); + setItem(context, view, reportSpeed ? speedLabelId : paceLabelId, parts[0], parts[1]); } /** * Sets distance value. * - * @param activity the activity + * @param context the context + * @param view the containing view * @param distance the distance in meters * @param metricUnits true if metric units */ - private static void setDistanceValue(Activity activity, double distance, boolean metricUnits) { - String parts[] = StringUtils.getDistanceParts(activity, distance, metricUnits); - setItem(activity, R.id.stats_distance, R.string.stats_distance, parts[0], parts[1]); + private static void setDistanceValue( + Context context, View view, double distance, boolean metricUnits) { + String parts[] = StringUtils.getDistanceParts(context, distance, metricUnits); + setItem(context, view, R.string.stats_distance, parts[0], parts[1]); } /** * Sets a time value. * - * @param activity the activity - * @param itemId the item id + * @param context the context + * @param view the containing view * @param labelId the label id * @param time the time */ - private static void setTimeValue(Activity activity, int itemId, int labelId, long time) { - String value = time == -1L ? activity.getString(R.string.value_unknown) - : StringUtils.formatElapsedTime(time); - setItem(activity, itemId, labelId, value, null); + private static void setTimeValue(Context context, View view, int labelId, long time) { + String value = time == -1L ? null : StringUtils.formatElapsedTime(time); + setItem(context, view, labelId, value, null); } /** * Sets an elevation value. * - * @param activity the activity - * @param itemId the item id + * @param context the context + * @param view the containing view * @param labelId the label id * @param elevation the elevation in meters * @param metricUnits true if metric units */ private static void setElevationValue( - Activity activity, int itemId, int labelId, double elevation, boolean metricUnits) { + Context context, View view, int labelId, double elevation, boolean metricUnits) { String value; String unit; if (Double.isNaN(elevation) || Double.isInfinite(elevation)) { @@ -266,57 +287,75 @@ public class StatsUtils { } else { if (metricUnits) { value = String.format(Locale.getDefault(), ELEVATION_FORMAT, elevation); - unit = activity.getString(R.string.unit_meter); + unit = context.getString(R.string.unit_meter); } else { elevation *= UnitConversions.M_TO_FT; value = String.format(Locale.getDefault(), ELEVATION_FORMAT, elevation); - unit = activity.getString(R.string.unit_feet); + unit = context.getString(R.string.unit_feet); } } - setItem(activity, itemId, labelId, value, unit); + setItem(context, view, labelId, value, unit); } /** * Sets a grade value. * - * @param activity the activity - * @param itemId the item id + * @param context the context + * @param view the containing view * @param labelId the label id * @param grade the grade in fraction between 0 and 1 */ - private static void setGradeValue(Activity activity, int itemId, int labelId, double grade) { + private static void setGradeValue(Context context, View view, int labelId, double grade) { String value = Double.isNaN(grade) || Double.isInfinite(grade) ? null : String.format(Locale.getDefault(), GRADE_FORMAT, Math.round(grade * 100)); - setItem(activity, itemId, labelId, value, GRADE_PERCENTAGE); + setItem(context, view, labelId, value, GRADE_PERCENTAGE); } /** * Sets a coordinate value. * - * @param activity the activity - * @param itemId the item id + * @param context the context + * @param view the containing view * @param labelId the label id * @param coordinate the coordinate in degrees */ private static void setCoordinateValue( - Activity activity, int itemId, int labelId, double coordinate) { + Context context, View view, int labelId, double coordinate) { String value = Double.isNaN(coordinate) || Double.isInfinite(coordinate) ? null : Location.convert(coordinate, Location.FORMAT_DEGREES); - setItem(activity, itemId, labelId, value, COORDINATE_DEGREE); + setItem(context, view, labelId, value, COORDINATE_DEGREE); + } + + /** + * Sets calorie. + * + * @param context the context + * @param view the containing view + * @param activityType the activity type + * @param calorie the value of calorie + */ + private static void setCalorie( + Context context, View view, ActivityType activityType, double calorie) { + if (activityType == ActivityType.INVALID) { + view.setVisibility(View.GONE); + } else { + view.setVisibility(View.VISIBLE); + setItem(context, view, R.string.stats_calorie, + String.format(Locale.getDefault(), CALORIES_FORMAT, calorie), + context.getString(R.string.unit_calorie)); + } } /** * Sets an item. * - * @param activity the activity - * @param itemId the item id + * @param context the context + * @param view the containing view * @param labelId the label id. -1 to hide the label * @param value the value, can be null * @param unit the unit. Null to hide the unit */ - private static void setItem( - Activity activity, int itemId, int labelId, String value, String unit) { - View view = activity.findViewById(itemId); + private static void setItem(Context context, View view, int labelId, String value, String unit) { TextView labelTextView = (TextView) view.findViewById(R.id.stats_label); TextView valueTextView = (TextView) view.findViewById(R.id.stats_value); TextView unitTextView = (TextView) view.findViewById(R.id.stats_unit); @@ -331,7 +370,7 @@ public class StatsUtils { } if (value == null) { - value = activity.getString(R.string.value_unknown); + value = context.getString(R.string.value_unknown); unitTextView.setVisibility(View.GONE); } else { if (unit == null) { @@ -345,49 +384,17 @@ public class StatsUtils { } /** - * Sets calorie. + * Get a view. * * @param activity the activity - * @param itemId the item id - * @param calorie the value of calorie - * @param trackId the id of track which is used to start track edit activity - * when user want to change the activity type + * @param view the containing view + * @param id the id */ - private static void setCalorie(final Activity activity, int itemId, double calorie, - final long trackId) { - 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 (activityType == ActivityType.INVALID) { - button.setVisibility(View.VISIBLE); - view.setVisibility(View.GONE); - - /* - * Return when it is not a valid track, which may be aggregated stats or - * marker. - */ - if (trackId == PreferencesUtils.RECORDING_TRACK_ID_DEFAULT) { - button.setVisibility(View.GONE); - return; - } - button.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - Intent intent = IntentUtils.newIntent(activity, TrackEditActivity.class).putExtra( - TrackEditActivity.EXTRA_TRACK_ID, trackId); - activity.startActivity(intent); - } - }); + private static View getView(Activity activity, View view, int id) { + if (activity != null) { + return activity.findViewById(id); } else { - button.setVisibility(View.GONE); - view.setVisibility(View.VISIBLE); - calorieString = String.format(Locale.getDefault(), CALORIES_FORMAT, calorie); - setItem(activity, itemId, R.string.stats_calorie, calorieString, - activity.getString(R.string.unit_calorie)); + return view.findViewById(id); } } } \ No newline at end of file