Fix StatsUtils to work with views from fragment

This commit is contained in:
Jimmy Shih
2013-11-01 11:28:06 -07:00
parent 5902af967c
commit 4b1eea2ed5
4 changed files with 146 additions and 139 deletions
@@ -37,9 +37,9 @@ public class AggregatedStatsActivity extends AbstractMyTracksActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
StatsUtils.setTripStatisticsValues(this, getTripStatistics(), StatsUtils.setTripStatisticsValues(
PreferencesUtils.RECORDING_TRACK_ID_DEFAULT); this, this, null, getTripStatistics(), PreferencesUtils.RECORDING_TRACK_ID_DEFAULT);
StatsUtils.setLocationValues(this, null, false); StatsUtils.setLocationValues(this, this, null, null, false);
} }
@Override @Override
@@ -252,9 +252,9 @@ public class MarkerDetailFragment extends Fragment {
TextView name = (TextView) getView().findViewById(R.id.marker_detail_statistics_name); TextView name = (TextView) getView().findViewById(R.id.marker_detail_statistics_name);
setTextView(name, waypoint.getName()); setTextView(name, waypoint.getName());
StatsUtils.setTripStatisticsValues( StatsUtils.setTripStatisticsValues(getActivity(), null, getView(),
getActivity(), waypoint.getTripStatistics(), PreferencesUtils.RECORDING_TRACK_ID_DEFAULT); waypoint.getTripStatistics(), PreferencesUtils.RECORDING_TRACK_ID_DEFAULT);
StatsUtils.setLocationValues(getActivity(), waypoint.getLocation(), false); StatsUtils.setLocationValues(getActivity(), null, getView(), waypoint.getLocation(), false);
} }
} }
@@ -155,7 +155,8 @@ public class StatsFragment extends Fragment implements TrackDataListener {
lastLocation = null; 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. * Updates the UI.
*/ */
private void updateUi(FragmentActivity activity) { private void updateUi(FragmentActivity activity) {
StatsUtils.setTripStatisticsValues( StatsUtils.setTripStatisticsValues(activity, activity, null, lastTripStatistics,
activity, trackDataHub == null ? PreferencesUtils.RECORDING_TRACK_ID_DEFAULT
lastTripStatistics, : trackDataHub.getSelectedTrackId());
trackDataHub == null ? PreferencesUtils.RECORDING_TRACK_ID_DEFAULT : trackDataHub StatsUtils.setLocationValues(
.getSelectedTrackId()); activity, activity, null, lastLocation, isSelectedTrackRecording());
StatsUtils.setLocationValues(activity, lastLocation, isSelectedTrackRecording());
} }
} }
@@ -16,18 +16,16 @@
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.content.MyTracksProviderUtils; 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.stats.TripStatistics;
import com.google.android.apps.mytracks.util.CalorieUtils.ActivityType; import com.google.android.apps.mytracks.util.CalorieUtils.ActivityType;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Context;
import android.location.Location; import android.location.Location;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import java.util.Locale; import java.util.Locale;
@@ -45,54 +43,61 @@ public class StatsUtils {
private static final String ELEVATION_FORMAT = "%1$.2f"; private static final String ELEVATION_FORMAT = "%1$.2f";
private static final String GRADE_FORMAT = "%1$d"; private static final String GRADE_FORMAT = "%1$d";
private static final String CALORIES_FORMAT = "%1$.0f"; private static final String CALORIES_FORMAT = "%1$.0f";
private StatsUtils() {} private StatsUtils() {}
/** /**
* Sets the location values. * 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 location the location
* @param isRecording true if recording * @param isRecording true if recording
*/ */
public static void setLocationValues(Activity activity, Location location, boolean isRecording) { public static void setLocationValues(
boolean metricUnits = PreferencesUtils.isMetricUnits(activity); Context context, Activity activity, View view, Location location, boolean isRecording) {
boolean reportSpeed = PreferencesUtils.isReportSpeed(activity); boolean metricUnits = PreferencesUtils.isMetricUnits(context);
boolean reportSpeed = PreferencesUtils.isReportSpeed(context);
// Set speed/pace // Set speed/pace
double speed = isRecording && location != null && location.hasSpeed() ? location.getSpeed() double speed = isRecording && location != null && location.hasSpeed() ? location.getSpeed()
: Double.NaN; : Double.NaN;
setSpeed(activity, R.id.stats_speed, R.string.stats_speed, R.string.stats_pace, speed, setSpeed(context, getView(activity, view, R.id.stats_speed), R.string.stats_speed,
metricUnits, reportSpeed); R.string.stats_pace, speed, metricUnits, reportSpeed);
// Set elevation // Set elevation
boolean showGradeElevation = PreferencesUtils.getBoolean(activity, boolean showGradeElevation = PreferencesUtils.getBoolean(context,
R.string.stats_show_grade_elevation_key, R.string.stats_show_grade_elevation_key,
PreferencesUtils.STATS_SHOW_GRADE_ELEVATION_DEFAULT); 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) { if (showGradeElevation && isRecording) {
double altitude = location != null && location.hasAltitude() ? location.getAltitude() double altitude = location != null && location.hasAltitude() ? location.getAltitude()
: Double.NaN; : Double.NaN;
elevation.setVisibility(View.VISIBLE); elevation.setVisibility(View.VISIBLE);
setElevationValue(activity, R.id.stats_elevation, -1, altitude, metricUnits); setElevationValue(context, elevation, -1, altitude, metricUnits);
} else { } else {
elevation.setVisibility(View.GONE); elevation.setVisibility(View.GONE);
} }
// Set coordinate // Set coordinate
boolean showCoordinate = PreferencesUtils.getBoolean(activity, boolean showCoordinate = PreferencesUtils.getBoolean(context,
R.string.stats_show_coordinate_key, PreferencesUtils.STATS_SHOW_COORDINATE_DEFAULT); R.string.stats_show_coordinate_key, PreferencesUtils.STATS_SHOW_COORDINATE_DEFAULT);
View coordinateHorizontalLine = activity.findViewById(R.id.stats_coordinate_horizontal_line); View coordinateHorizontalLine = getView(activity, view, R.id.stats_coordinate_horizontal_line);
View coordinateContainer = activity.findViewById(R.id.stats_coordinate_container); View coordinateContainer = getView(activity, view, R.id.stats_coordinate_container);
if (showCoordinate && isRecording) { if (showCoordinate && isRecording) {
double latitude = location != null ? location.getLatitude() : Double.NaN; double latitude = location != null ? location.getLatitude() : Double.NaN;
double longitude = location != null ? location.getLongitude() : Double.NaN; double longitude = location != null ? location.getLongitude() : Double.NaN;
coordinateHorizontalLine.setVisibility(View.VISIBLE); coordinateHorizontalLine.setVisibility(View.VISIBLE);
coordinateContainer.setVisibility(View.VISIBLE); coordinateContainer.setVisibility(View.VISIBLE);
setCoordinateValue(activity, R.id.stats_latitude, R.string.stats_latitude, latitude); setCoordinateValue(
setCoordinateValue(activity, R.id.stats_longitude, R.string.stats_longitude, longitude); 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 { } else {
coordinateHorizontalLine.setVisibility(View.GONE); coordinateHorizontalLine.setVisibility(View.GONE);
coordinateContainer.setVisibility(View.GONE); coordinateContainer.setVisibility(View.GONE);
@@ -106,57 +111,66 @@ public class StatsUtils {
* @param totalTime the total time * @param totalTime the total time
*/ */
public static void setTotalTimeValue(Activity activity, long totalTime) { 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. * 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 tripStatistics the trip statistics
* @param trackId the id of track, which is used to set calorie value. Does * @param trackId the id of track, which is used to set calorie value. Does
* not handle the calorie If the value is * not handle the calorie If the value is
* {@link PreferencesUtils#RECORDING_TRACK_ID_DEFAULT} * {@link PreferencesUtils#RECORDING_TRACK_ID_DEFAULT}
*/ */
public static void setTripStatisticsValues(Activity activity, TripStatistics tripStatistics, long trackId) { public static void setTripStatisticsValues(
boolean metricUnits = PreferencesUtils.isMetricUnits(activity); Context context, Activity activity, View view, TripStatistics tripStatistics, long trackId) {
boolean reportSpeed = PreferencesUtils.isReportSpeed(activity); boolean metricUnits = PreferencesUtils.isMetricUnits(context);
boolean reportSpeed = PreferencesUtils.isReportSpeed(context);
// Set total distance // Set total distance
double totalDistance = tripStatistics == null ? Double.NaN : tripStatistics.getTotalDistance(); 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 // 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); tripStatistics != null ? tripStatistics.getTotalTime() : -1L);
// Set average speed/pace // Set average speed/pace
double averageSpeed = tripStatistics != null ? tripStatistics.getAverageSpeed() : Double.NaN; double averageSpeed = tripStatistics != null ? tripStatistics.getAverageSpeed() : Double.NaN;
setSpeed(activity, R.id.stats_average_speed, R.string.stats_average_speed, setSpeed(context, getView(activity, view, R.id.stats_average_speed),
R.string.stats_average_pace, averageSpeed, metricUnits, reportSpeed); R.string.stats_average_speed, R.string.stats_average_pace, averageSpeed, metricUnits,
reportSpeed);
// Set moving time // Set moving time
setTimeValue(activity, R.id.stats_moving_time, R.string.stats_moving_time, setTimeValue(context, getView(activity, view, R.id.stats_moving_time),
tripStatistics != null ? tripStatistics.getMovingTime() : -1L); R.string.stats_moving_time, tripStatistics != null ? tripStatistics.getMovingTime() : -1L);
// Set average moving speed/pace // Set average moving speed/pace
double averageMovingSpeed = tripStatistics != null ? tripStatistics.getAverageMovingSpeed() double averageMovingSpeed = tripStatistics != null ? tripStatistics.getAverageMovingSpeed()
: Double.NaN; : Double.NaN;
setSpeed(activity, R.id.stats_average_moving_time, R.string.stats_average_moving_speed, setSpeed(context, getView(activity, view, R.id.stats_average_moving_time),
R.string.stats_average_moving_pace, averageMovingSpeed, metricUnits, reportSpeed); R.string.stats_average_moving_speed, R.string.stats_average_moving_pace, averageMovingSpeed,
metricUnits, reportSpeed);
// Set max speed/pace // Set max speed/pace
double maxSpeed = tripStatistics == null ? Double.NaN : tripStatistics.getMaxSpeed(); 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, setSpeed(context, getView(activity, view, R.id.stats_max_speed), R.string.stats_max_speed,
maxSpeed, metricUnits, reportSpeed); R.string.stats_fastest_pace, maxSpeed, metricUnits, reportSpeed);
// Set grade/elevation // Set grade/elevation
boolean showGradeElevation = PreferencesUtils.getBoolean(activity, boolean showGradeElevation = PreferencesUtils.getBoolean(context,
R.string.stats_show_grade_elevation_key, R.string.stats_show_grade_elevation_key,
PreferencesUtils.STATS_SHOW_GRADE_ELEVATION_DEFAULT); PreferencesUtils.STATS_SHOW_GRADE_ELEVATION_DEFAULT);
View gradeElevationHorizontalLine = activity.findViewById( View gradeElevationHorizontalLine = getView(
R.id.stats_grade_elevation_horizontal_line); activity, view, R.id.stats_grade_elevation_horizontal_line);
View gradeElevationContainer = activity.findViewById(R.id.stats_grade_elevation_container); View gradeElevationContainer = getView(activity, view, R.id.stats_grade_elevation_container);
if (showGradeElevation) { if (showGradeElevation) {
gradeElevationHorizontalLine.setVisibility(View.VISIBLE); gradeElevationHorizontalLine.setVisibility(View.VISIBLE);
@@ -164,31 +178,33 @@ public class StatsUtils {
// Set grade // Set grade
double minGrade = tripStatistics == null ? Double.NaN : tripStatistics.getMinGrade(); double minGrade = tripStatistics == null ? Double.NaN : tripStatistics.getMinGrade();
double maxGrade = tripStatistics == null ? Double.NaN : tripStatistics.getMaxGrade(); double maxGrade = tripStatistics == null ? Double.NaN : tripStatistics.getMaxGrade();
setGradeValue(activity, R.id.stats_grade_min, R.string.stats_min, minGrade); setGradeValue(
setGradeValue(activity, R.id.stats_grade_max, R.string.stats_max, maxGrade); 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 // Set elevation
double elevationGain = tripStatistics == null ? Double.NaN double elevationGain = tripStatistics == null ? Double.NaN
: tripStatistics.getTotalElevationGain(); : tripStatistics.getTotalElevationGain();
double minElevation = tripStatistics == null ? Double.NaN : tripStatistics.getMinElevation(); double minElevation = tripStatistics == null ? Double.NaN : tripStatistics.getMinElevation();
double maxElevation = tripStatistics == null ? Double.NaN : tripStatistics.getMaxElevation(); double maxElevation = tripStatistics == null ? Double.NaN : tripStatistics.getMaxElevation();
setElevationValue( setElevationValue(context, getView(activity, view, R.id.stats_elevation_gain),
activity, R.id.stats_elevation_gain, R.string.stats_gain, elevationGain, metricUnits); R.string.stats_gain, elevationGain, metricUnits);
setElevationValue( setElevationValue(context, getView(activity, view, R.id.stats_elevation_min),
activity, R.id.stats_elevation_min, R.string.stats_min, minElevation, metricUnits); R.string.stats_min, minElevation, metricUnits);
setElevationValue( setElevationValue(context, getView(activity, view, R.id.stats_elevation_max),
activity, R.id.stats_elevation_max, R.string.stats_max, maxElevation, metricUnits); R.string.stats_max, maxElevation, metricUnits);
} else { } else {
gradeElevationHorizontalLine.setVisibility(View.GONE); gradeElevationHorizontalLine.setVisibility(View.GONE);
gradeElevationContainer.setVisibility(View.GONE); gradeElevationContainer.setVisibility(View.GONE);
} }
if (trackId != PreferencesUtils.RECORDING_TRACK_ID_DEFAULT) { if (trackId != PreferencesUtils.RECORDING_TRACK_ID_DEFAULT) {
// Set calories // Set calories
boolean showCalorie = PreferencesUtils.getBoolean(activity, R.string.stats_show_calorie_key, boolean showCalorie = PreferencesUtils.getBoolean(
PreferencesUtils.STATS_SHOW_CALORIE_DEFAULT); context, R.string.stats_show_calorie_key, PreferencesUtils.STATS_SHOW_CALORIE_DEFAULT);
View calorieHorizontalLine = activity.findViewById(R.id.stats_calorie_horizontal_line); View calorieHorizontalLine = getView(activity, view, R.id.stats_calorie_horizontal_line);
View calorieContainer = activity.findViewById(R.id.stats_calorie_container); View calorieContainer = getView(activity, view, R.id.stats_calorie_container);
if (showCalorie) { if (showCalorie) {
calorieHorizontalLine.setVisibility(View.VISIBLE); calorieHorizontalLine.setVisibility(View.VISIBLE);
calorieContainer.setVisibility(View.VISIBLE); calorieContainer.setVisibility(View.VISIBLE);
@@ -196,7 +212,11 @@ public class StatsUtils {
if (tripStatistics != null) { if (tripStatistics != null) {
calories = tripStatistics.getCalorie(); 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 { } else {
calorieHorizontalLine.setVisibility(View.GONE); calorieHorizontalLine.setVisibility(View.GONE);
calorieContainer.setVisibility(View.GONE); calorieContainer.setVisibility(View.GONE);
@@ -207,57 +227,58 @@ public class StatsUtils {
/** /**
* Sets speed. * Sets speed.
* *
* @param activity the activity * @param context the context
* @param itemId the item id * @param view the containing view
* @param speedLabelId the speed label id * @param speedLabelId the speed label id
* @param paceLabelId the pace label id * @param paceLabelId the pace label id
* @param speed the speed in meters per second * @param speed the speed in meters per second
* @param metricUnits true if metric units * @param metricUnits true if metric units
* @param reportSpeed true if report speed * @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) { double speed, boolean metricUnits, boolean reportSpeed) {
String parts[] = StringUtils.getSpeedParts(activity, speed, metricUnits, reportSpeed); String parts[] = StringUtils.getSpeedParts(context, speed, metricUnits, reportSpeed);
setItem(activity, itemId, reportSpeed ? speedLabelId : paceLabelId, parts[0], parts[1]); setItem(context, view, reportSpeed ? speedLabelId : paceLabelId, parts[0], parts[1]);
} }
/** /**
* Sets distance value. * Sets distance value.
* *
* @param activity the activity * @param context the context
* @param view the containing view
* @param distance the distance in meters * @param distance the distance in meters
* @param metricUnits true if metric units * @param metricUnits true if metric units
*/ */
private static void setDistanceValue(Activity activity, double distance, boolean metricUnits) { private static void setDistanceValue(
String parts[] = StringUtils.getDistanceParts(activity, distance, metricUnits); Context context, View view, double distance, boolean metricUnits) {
setItem(activity, R.id.stats_distance, R.string.stats_distance, parts[0], parts[1]); String parts[] = StringUtils.getDistanceParts(context, distance, metricUnits);
setItem(context, view, R.string.stats_distance, parts[0], parts[1]);
} }
/** /**
* Sets a time value. * Sets a time value.
* *
* @param activity the activity * @param context the context
* @param itemId the item id * @param view the containing view
* @param labelId the label id * @param labelId the label id
* @param time the time * @param time the time
*/ */
private static void setTimeValue(Activity activity, int itemId, int labelId, long time) { private static void setTimeValue(Context context, View view, int labelId, long time) {
String value = time == -1L ? activity.getString(R.string.value_unknown) String value = time == -1L ? null : StringUtils.formatElapsedTime(time);
: StringUtils.formatElapsedTime(time); setItem(context, view, labelId, value, null);
setItem(activity, itemId, labelId, value, null);
} }
/** /**
* Sets an elevation value. * Sets an elevation value.
* *
* @param activity the activity * @param context the context
* @param itemId the item id * @param view the containing view
* @param labelId the label id * @param labelId the label id
* @param elevation the elevation in meters * @param elevation the elevation in meters
* @param metricUnits true if metric units * @param metricUnits true if metric units
*/ */
private static void setElevationValue( 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 value;
String unit; String unit;
if (Double.isNaN(elevation) || Double.isInfinite(elevation)) { if (Double.isNaN(elevation) || Double.isInfinite(elevation)) {
@@ -266,57 +287,75 @@ public class StatsUtils {
} else { } else {
if (metricUnits) { if (metricUnits) {
value = String.format(Locale.getDefault(), ELEVATION_FORMAT, elevation); value = String.format(Locale.getDefault(), ELEVATION_FORMAT, elevation);
unit = activity.getString(R.string.unit_meter); unit = context.getString(R.string.unit_meter);
} else { } else {
elevation *= UnitConversions.M_TO_FT; elevation *= UnitConversions.M_TO_FT;
value = String.format(Locale.getDefault(), ELEVATION_FORMAT, elevation); 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. * Sets a grade value.
* *
* @param activity the activity * @param context the context
* @param itemId the item id * @param view the containing view
* @param labelId the label id * @param labelId the label id
* @param grade the grade in fraction between 0 and 1 * @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 value = Double.isNaN(grade) || Double.isInfinite(grade) ? null
: String.format(Locale.getDefault(), GRADE_FORMAT, Math.round(grade * 100)); : 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. * Sets a coordinate value.
* *
* @param activity the activity * @param context the context
* @param itemId the item id * @param view the containing view
* @param labelId the label id * @param labelId the label id
* @param coordinate the coordinate in degrees * @param coordinate the coordinate in degrees
*/ */
private static void setCoordinateValue( 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 String value = Double.isNaN(coordinate) || Double.isInfinite(coordinate) ? null
: Location.convert(coordinate, Location.FORMAT_DEGREES); : 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. * Sets an item.
* *
* @param activity the activity * @param context the context
* @param itemId the item id * @param view the containing view
* @param labelId the label id. -1 to hide the label * @param labelId the label id. -1 to hide the label
* @param value the value, can be null * @param value the value, can be null
* @param unit the unit. Null to hide the unit * @param unit the unit. Null to hide the unit
*/ */
private static void setItem( private static void setItem(Context context, View view, int labelId, String value, String unit) {
Activity activity, int itemId, int labelId, String value, String unit) {
View view = activity.findViewById(itemId);
TextView labelTextView = (TextView) view.findViewById(R.id.stats_label); TextView labelTextView = (TextView) view.findViewById(R.id.stats_label);
TextView valueTextView = (TextView) view.findViewById(R.id.stats_value); TextView valueTextView = (TextView) view.findViewById(R.id.stats_value);
TextView unitTextView = (TextView) view.findViewById(R.id.stats_unit); TextView unitTextView = (TextView) view.findViewById(R.id.stats_unit);
@@ -331,7 +370,7 @@ public class StatsUtils {
} }
if (value == null) { if (value == null) {
value = activity.getString(R.string.value_unknown); value = context.getString(R.string.value_unknown);
unitTextView.setVisibility(View.GONE); unitTextView.setVisibility(View.GONE);
} else { } else {
if (unit == null) { if (unit == null) {
@@ -345,49 +384,17 @@ public class StatsUtils {
} }
/** /**
* Sets calorie. * Get a view.
* *
* @param activity the activity * @param activity the activity
* @param itemId the item id * @param view the containing view
* @param calorie the value of calorie * @param id the id
* @param trackId the id of track which is used to start track edit activity
* when user want to change the activity type
*/ */
private static void setCalorie(final Activity activity, int itemId, double calorie, private static View getView(Activity activity, View view, int id) {
final long trackId) { if (activity != null) {
String calorieString; return activity.findViewById(id);
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);
}
});
} else { } else {
button.setVisibility(View.GONE); return view.findViewById(id);
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));
} }
} }
} }