From 545db52f942616f4d2ac7f98fa340bb76dc1ae5d Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Thu, 12 Sep 2019 18:33:45 +0200 Subject: [PATCH] Cleanup. --- .../opentracks/AggregatedStatsActivity.java | 9 ++- .../fragments/MarkerDetailFragment.java | 4 +- .../opentracks/fragments/StatsFragment.java | 6 +- .../opentracks/util/StatsUtils.java | 72 +++++++------------ 4 files changed, 35 insertions(+), 56 deletions(-) diff --git a/src/main/java/de/dennisguse/opentracks/AggregatedStatsActivity.java b/src/main/java/de/dennisguse/opentracks/AggregatedStatsActivity.java index 347c85f40..3fcee644e 100644 --- a/src/main/java/de/dennisguse/opentracks/AggregatedStatsActivity.java +++ b/src/main/java/de/dennisguse/opentracks/AggregatedStatsActivity.java @@ -18,13 +18,12 @@ package de.dennisguse.opentracks; import android.os.Bundle; +import java.util.List; + import de.dennisguse.opentracks.content.ContentProviderUtils; import de.dennisguse.opentracks.content.Track; import de.dennisguse.opentracks.stats.TripStatistics; import de.dennisguse.opentracks.util.StatsUtils; -import de.dennisguse.opentracks.R; - -import java.util.List; /** * An activity to view aggregated stats from all recorded tracks. @@ -36,8 +35,8 @@ public class AggregatedStatsActivity extends AbstractActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - StatsUtils.setTripStatisticsValues(this, this, null, getTripStatistics(), null); - StatsUtils.setLocationValues(this, this, null, null, false); + StatsUtils.setTripStatisticsValues(this, getTripStatistics(), null); + StatsUtils.setLocationValues(this, null, false); } @Override diff --git a/src/main/java/de/dennisguse/opentracks/fragments/MarkerDetailFragment.java b/src/main/java/de/dennisguse/opentracks/fragments/MarkerDetailFragment.java index 09313dc78..ad30e8d5c 100644 --- a/src/main/java/de/dennisguse/opentracks/fragments/MarkerDetailFragment.java +++ b/src/main/java/de/dennisguse/opentracks/fragments/MarkerDetailFragment.java @@ -259,8 +259,8 @@ public class MarkerDetailFragment extends Fragment { setLocation(R.id.marker_detail_statistics_location, false); Track track = contentProviderUtils.getTrack(waypoint.getTrackId()); - StatsUtils.setTripStatisticsValues(getActivity(), null, getView(), waypoint.getTripStatistics(), null); - StatsUtils.setLocationValues(getActivity(), null, getView(), waypoint.getLocation(), false); + StatsUtils.setTripStatisticsValues(getActivity(), waypoint.getTripStatistics(), null); + StatsUtils.setLocationValues(getActivity(), waypoint.getLocation(), false); } } diff --git a/src/main/java/de/dennisguse/opentracks/fragments/StatsFragment.java b/src/main/java/de/dennisguse/opentracks/fragments/StatsFragment.java index 1d59fcdb6..08421682d 100644 --- a/src/main/java/de/dennisguse/opentracks/fragments/StatsFragment.java +++ b/src/main/java/de/dennisguse/opentracks/fragments/StatsFragment.java @@ -197,7 +197,7 @@ public class StatsFragment extends Fragment implements TrackDataListener { lastLocation = null; } } - StatsUtils.setLocationValues(getActivity(), getActivity(), null, lastLocation, isSelectedTrackRecording()); + StatsUtils.setLocationValues(getActivity(), lastLocation, isSelectedTrackRecording()); } } }); @@ -318,8 +318,8 @@ public class StatsFragment extends Fragment implements TrackDataListener { */ private void updateUi(FragmentActivity activity) { String trackIconValue = TrackIconUtils.getIconValue(activity, category); - StatsUtils.setTripStatisticsValues(activity, activity, null, lastTripStatistics, trackIconValue); - StatsUtils.setLocationValues(activity, activity, null, lastLocation, isSelectedTrackRecording()); + StatsUtils.setTripStatisticsValues(activity, lastTripStatistics, trackIconValue); + StatsUtils.setLocationValues(activity, lastLocation, isSelectedTrackRecording()); updateSensorDataUI(); } } diff --git a/src/main/java/de/dennisguse/opentracks/util/StatsUtils.java b/src/main/java/de/dennisguse/opentracks/util/StatsUtils.java index 004831e54..d389db446 100644 --- a/src/main/java/de/dennisguse/opentracks/util/StatsUtils.java +++ b/src/main/java/de/dennisguse/opentracks/util/StatsUtils.java @@ -45,15 +45,13 @@ public class StatsUtils { /** * Sets the location values. * - * @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 activity the activity for finding views * @param location the location * @param isRecording true if recording */ - public static void setLocationValues(Context context, Activity activity, View view, Location location, boolean isRecording) { - boolean metricUnits = PreferencesUtils.isMetricUnits(context); - boolean reportSpeed = PreferencesUtils.isReportSpeed(context); + public static void setLocationValues(Activity activity, Location location, boolean isRecording) { + boolean metricUnits = PreferencesUtils.isMetricUnits(activity); + boolean reportSpeed = PreferencesUtils.isReportSpeed(activity); // Set speed/pace View speedContainer = activity.findViewById(R.id.stats_speed); @@ -63,7 +61,7 @@ public class StatsUtils { speedLabel.setText(reportSpeed ? R.string.stats_speed : R.string.stats_pace); double speed = location != null && location.hasSpeed() ? location.getSpeed() : Double.NaN; - Pair parts = StringUtils.getSpeedParts(context, speed, metricUnits, reportSpeed); + Pair parts = StringUtils.getSpeedParts(activity, speed, metricUnits, reportSpeed); TextView speedValue = activity.findViewById(R.id.stats_speed_value); speedValue.setText(parts.first); @@ -73,13 +71,13 @@ public class StatsUtils { } // Set elevation - boolean showGradeElevation = PreferencesUtils.getBoolean(context, R.string.stats_show_grade_elevation_key, PreferencesUtils.STATS_SHOW_ELEVATION_DEFAULT) && isRecording; - View elevationContainer = getView(activity, view, R.id.stats_elevation); + boolean showGradeElevation = PreferencesUtils.getBoolean(activity, R.string.stats_show_grade_elevation_key, PreferencesUtils.STATS_SHOW_ELEVATION_DEFAULT) && isRecording; + View elevationContainer = activity.findViewById(R.id.stats_elevation); elevationContainer.setVisibility(showGradeElevation ? View.VISIBLE : View.GONE); if (showGradeElevation) { double altitude = location != null && location.hasAltitude() ? location.getAltitude() : Double.NaN; - Pair parts = formatElevation(context, altitude, metricUnits); + Pair parts = formatElevation(activity, altitude, metricUnits); TextView elevationValue = activity.findViewById(R.id.stats_elevation_current_value); elevationValue.setText(parts.first); @@ -88,12 +86,12 @@ public class StatsUtils { } // Set coordinate - boolean showCoordinate = isRecording && PreferencesUtils.getBoolean(context, R.string.stats_show_coordinate_key, PreferencesUtils.STATS_SHOW_COORDINATE_DEFAULT); + boolean showCoordinate = isRecording && PreferencesUtils.getBoolean(activity, R.string.stats_show_coordinate_key, PreferencesUtils.STATS_SHOW_COORDINATE_DEFAULT); - View coordinateSeparator = getView(activity, view, R.id.stats_coordinate_separator); + View coordinateSeparator = activity.findViewById(R.id.stats_coordinate_separator); coordinateSeparator.setVisibility(showCoordinate ? View.VISIBLE : View.GONE); - View coordinateContainer = getView(activity, view, R.id.stats_coordinate_container); + View coordinateContainer = activity.findViewById(R.id.stats_coordinate_container); coordinateContainer.setVisibility(showCoordinate ? View.VISIBLE : View.GONE); if (showCoordinate) { double latitude = location != null ? location.getLatitude() : Double.NaN; @@ -145,20 +143,18 @@ public class StatsUtils { /** * Sets the trip statistics values. * - * @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 activity the activity for finding views * @param tripStatistics the trip statistics * @param trackIconValue the track icon value or null to hide the track icon spinner */ - public static void setTripStatisticsValues(Context context, Activity activity, View view, TripStatistics tripStatistics, String trackIconValue) { - boolean metricUnits = PreferencesUtils.isMetricUnits(context); - boolean reportSpeed = PreferencesUtils.isReportSpeed(context); + public static void setTripStatisticsValues(Activity activity, TripStatistics tripStatistics, String trackIconValue) { + boolean metricUnits = PreferencesUtils.isMetricUnits(activity); + boolean reportSpeed = PreferencesUtils.isReportSpeed(activity); // Set total distance { double totalDistance = tripStatistics == null ? Double.NaN : tripStatistics.getTotalDistance(); - Pair parts = StringUtils.getDistanceParts(context, totalDistance, metricUnits); + Pair parts = StringUtils.getDistanceParts(activity, totalDistance, metricUnits); TextView distanceValue = activity.findViewById(R.id.stats_distance_value); distanceValue.setText(parts.first); @@ -171,7 +167,7 @@ public class StatsUtils { { activity.findViewById(R.id.stats_activity_type_label).setVisibility(trackIconValue != null ? View.VISIBLE : View.GONE); - Spinner spinner = (Spinner) getView(activity, view, R.id.stats_activity_type_icon); + Spinner spinner = activity.findViewById(R.id.stats_activity_type_icon); spinner.setVisibility(trackIconValue != null ? View.VISIBLE : View.GONE); if (trackIconValue != null) { TrackIconUtils.setIconSpinner(spinner, trackIconValue); @@ -192,7 +188,7 @@ public class StatsUtils { TextView speedLabel = activity.findViewById(R.id.stats_average_speed_label); speedLabel.setText(reportSpeed ? R.string.stats_average_speed : R.string.stats_average_pace); - Pair parts = StringUtils.getSpeedParts(context, speed, metricUnits, reportSpeed); + Pair parts = StringUtils.getSpeedParts(activity, speed, metricUnits, reportSpeed); TextView speedValue = activity.findViewById(R.id.stats_average_speed_value); speedValue.setText(parts.first); TextView speedUnit = activity.findViewById(R.id.stats_average_speed_unit); @@ -206,7 +202,7 @@ public class StatsUtils { TextView speedLabel = activity.findViewById(R.id.stats_max_speed_label); speedLabel.setText(reportSpeed ? R.string.stats_max_speed : R.string.stats_fastest_pace); - Pair parts = StringUtils.getSpeedParts(context, speed, metricUnits, reportSpeed); + Pair parts = StringUtils.getSpeedParts(activity, speed, metricUnits, reportSpeed); TextView speedValue = activity.findViewById(R.id.stats_max_speed_value); speedValue.setText(parts.first); TextView speedUnit = activity.findViewById(R.id.stats_max_speed_unit); @@ -220,7 +216,7 @@ public class StatsUtils { TextView speedLabel = activity.findViewById(R.id.stats_average_speed_label); speedLabel.setText(reportSpeed ? R.string.stats_average_speed : R.string.stats_average_pace); - Pair parts = StringUtils.getSpeedParts(context, speed, metricUnits, reportSpeed); + Pair parts = StringUtils.getSpeedParts(activity, speed, metricUnits, reportSpeed); TextView speedValue = activity.findViewById(R.id.stats_average_speed_value); speedValue.setText(parts.first); TextView speedUnit = activity.findViewById(R.id.stats_average_speed_unit); @@ -234,7 +230,7 @@ public class StatsUtils { TextView speedLabel = activity.findViewById(R.id.stats_moving_speed_label); speedLabel.setText(reportSpeed ? R.string.stats_average_moving_speed : R.string.stats_average_moving_pace); - Pair parts = StringUtils.getSpeedParts(context, speed, metricUnits, reportSpeed); + Pair parts = StringUtils.getSpeedParts(activity, speed, metricUnits, reportSpeed); TextView speedValue = activity.findViewById(R.id.stats_moving_speed_value); speedValue.setText(parts.first); TextView speedUnit = activity.findViewById(R.id.stats_moving_speed_unit); @@ -244,17 +240,17 @@ public class StatsUtils { // Set elevation { - boolean showElevation = PreferencesUtils.getBoolean(context, R.string.stats_show_grade_elevation_key, PreferencesUtils.STATS_SHOW_ELEVATION_DEFAULT); - View gradeElevationSeparator = getView(activity, view, R.id.stats_elevation_separator); + boolean showElevation = PreferencesUtils.getBoolean(activity, R.string.stats_show_grade_elevation_key, PreferencesUtils.STATS_SHOW_ELEVATION_DEFAULT); + View gradeElevationSeparator = activity.findViewById(R.id.stats_elevation_separator); gradeElevationSeparator.setVisibility(showElevation ? View.VISIBLE : View.GONE); - View gradeElevationContainer = getView(activity, view, R.id.stats_elevation_container); + View gradeElevationContainer = activity.findViewById(R.id.stats_elevation_container); gradeElevationContainer.setVisibility(showElevation ? View.VISIBLE : View.GONE); if (showElevation) { { double elevation = tripStatistics == null ? Double.NaN : tripStatistics.getMinElevation(); - Pair parts = formatElevation(context, elevation, metricUnits); + Pair parts = formatElevation(activity, elevation, metricUnits); TextView elevationValue = activity.findViewById(R.id.stats_elevation_min_value); elevationValue.setText(parts.first); @@ -264,7 +260,7 @@ public class StatsUtils { { double elevation = tripStatistics == null ? Double.NaN : tripStatistics.getMaxElevation(); - Pair parts = formatElevation(context, elevation, metricUnits); + Pair parts = formatElevation(activity, elevation, metricUnits); TextView elevationValue = activity.findViewById(R.id.stats_elevation_max_value); elevationValue.setText(parts.first); @@ -296,20 +292,4 @@ public class StatsUtils { } return new Pair<>(value, unit); } - - /** - * Get a view. - * - * @param activity the activity - * @param view the containing view - * @param id the id - */ - //TODO What is the difference between the two cases? Is it (still) necessary? - private static View getView(Activity activity, View view, int id) { - if (activity != null) { - return activity.findViewById(id); - } else { - return view.findViewById(id); - } - } } \ No newline at end of file