mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-02 09:33:06 +02:00
Make sure StatsFragment do not crash when switching tabs very quickly.
This commit is contained in:
@@ -118,9 +118,9 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
|
||||
@Override
|
||||
public void onProviderStateChange(ProviderState state) {
|
||||
if (state == ProviderState.DISABLED || state == ProviderState.NO_FIX) {
|
||||
if (isResumed() && (state == ProviderState.DISABLED || state == ProviderState.NO_FIX)) {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
@Override
|
||||
public void run() {
|
||||
lastLocation = null;
|
||||
StatsUtils.setLocationValues(getActivity(), lastLocation, true);
|
||||
@@ -131,7 +131,7 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
|
||||
@Override
|
||||
public void onCurrentLocationChanged(final Location location) {
|
||||
if (isRecording()) {
|
||||
if (isResumed() && isRecording()) {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -149,35 +149,39 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
|
||||
@Override
|
||||
public void onSelectedTrackChanged(Track track, boolean isRecording) {
|
||||
if (uiUpdateThread == null && isRecording) {
|
||||
uiUpdateThread = new UiUpdateThread();
|
||||
uiUpdateThread.start();
|
||||
} else if (uiUpdateThread != null && !isRecording) {
|
||||
uiUpdateThread.interrupt();
|
||||
uiUpdateThread = null;
|
||||
if (isResumed()) {
|
||||
if (uiUpdateThread == null && isRecording) {
|
||||
uiUpdateThread = new UiUpdateThread();
|
||||
uiUpdateThread.start();
|
||||
} else if (uiUpdateThread != null && !isRecording) {
|
||||
uiUpdateThread.interrupt();
|
||||
uiUpdateThread = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTrackUpdated(final Track track) {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (track == null || track.getTripStatistics() == null) {
|
||||
lastLocation = null;
|
||||
lastTripStatistics = null;
|
||||
if (isResumed()) {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (track == null || track.getTripStatistics() == null) {
|
||||
lastLocation = null;
|
||||
lastTripStatistics = null;
|
||||
updateUi();
|
||||
return;
|
||||
}
|
||||
lastTripStatistics = track.getTripStatistics();
|
||||
|
||||
startTime = track.getTripStatistics().getStartTime();
|
||||
if (!isRecording()) {
|
||||
lastLocation = null;
|
||||
}
|
||||
updateUi();
|
||||
return;
|
||||
}
|
||||
lastTripStatistics = track.getTripStatistics();
|
||||
|
||||
startTime = track.getTripStatistics().getStartTime();
|
||||
if (!isRecording()) {
|
||||
lastLocation = null;
|
||||
}
|
||||
updateUi();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -222,23 +226,27 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
|
||||
@Override
|
||||
public boolean onUnitsChanged(final boolean metric) {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateUi();
|
||||
}
|
||||
});
|
||||
if (isResumed()) {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateUi();
|
||||
}
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onReportSpeedChanged(final boolean speed) {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateUi();
|
||||
}
|
||||
});
|
||||
if (isResumed()) {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateUi();
|
||||
}
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -70,6 +70,9 @@ public class StatsUtils {
|
||||
R.string.stats_show_coordinate_key, PreferencesUtils.STATS_SHOW_COORDINATE_DEFAULT);
|
||||
View coordinateLabelTableRow = activity.findViewById(R.id.stats_coordinate_label_table_row);
|
||||
View coordinateValueTableRow = activity.findViewById(R.id.stats_coordinate_value_table_row);
|
||||
if (coordinateLabelTableRow == null || coordinateValueTableRow == null) {
|
||||
return;
|
||||
}
|
||||
coordinateLabelTableRow.setVisibility(showCoordinate ? View.VISIBLE : View.GONE);
|
||||
coordinateValueTableRow.setVisibility(showCoordinate ? View.VISIBLE : View.GONE);
|
||||
if (showCoordinate) {
|
||||
@@ -149,6 +152,12 @@ public class StatsUtils {
|
||||
View elevationValueTableRow1 = activity.findViewById(R.id.stats_elevation_value_table_row1);
|
||||
View elevationLabelTableRow2 = activity.findViewById(R.id.stats_elevation_label_table_row2);
|
||||
View elevationValueTableRow2 = activity.findViewById(R.id.stats_elevation_value_table_row2);
|
||||
if (elevationLabelTableRow1 == null
|
||||
|| elevationValueTableRow1 == null
|
||||
|| elevationLabelTableRow2 == null
|
||||
|| elevationValueTableRow2 == null) {
|
||||
return;
|
||||
}
|
||||
elevationLabelTableRow1.setVisibility(showElevation ? View.VISIBLE : View.GONE);
|
||||
elevationValueTableRow1.setVisibility(showElevation ? View.VISIBLE : View.GONE);
|
||||
elevationLabelTableRow2.setVisibility(showElevation ? View.VISIBLE : View.GONE);
|
||||
@@ -168,6 +177,9 @@ public class StatsUtils {
|
||||
activity, R.string.stats_show_grade_key, PreferencesUtils.STATS_SHOW_GRADE_DEFAULT);
|
||||
View gradeLabelTableRow = activity.findViewById(R.id.stats_grade_label_table_row);
|
||||
View gradeValueTableRow = activity.findViewById(R.id.stats_grade_value_table_row);
|
||||
if (gradeLabelTableRow == null || gradeValueTableRow == null) {
|
||||
return;
|
||||
}
|
||||
gradeLabelTableRow.setVisibility(showGrade ? View.VISIBLE : View.GONE);
|
||||
gradeValueTableRow.setVisibility(showGrade ? View.VISIBLE : View.GONE);
|
||||
if (showGrade) {
|
||||
@@ -190,6 +202,9 @@ public class StatsUtils {
|
||||
private static void setTimeLabel(Activity activity, int id, int totalTimeId, int movingTimeId,
|
||||
boolean totalTime) {
|
||||
TextView textView = (TextView) activity.findViewById(id);
|
||||
if (textView == null) {
|
||||
return;
|
||||
}
|
||||
textView.setText(totalTime ? totalTimeId : movingTimeId);
|
||||
}
|
||||
|
||||
@@ -205,6 +220,9 @@ public class StatsUtils {
|
||||
private static void setSpeedLabel(
|
||||
Activity activity, int id, int speedId, int paceId, boolean reportSpeed) {
|
||||
TextView textView = (TextView) activity.findViewById(id);
|
||||
if (textView == null) {
|
||||
return;
|
||||
}
|
||||
textView.setText(reportSpeed ? speedId : paceId);
|
||||
}
|
||||
|
||||
@@ -220,6 +238,9 @@ public class StatsUtils {
|
||||
private static void setSpeedValue(
|
||||
Activity activity, int id, double speed, boolean metricUnits, boolean reportSpeed) {
|
||||
TextView textView = (TextView) activity.findViewById(id);
|
||||
if (textView == null) {
|
||||
return;
|
||||
}
|
||||
textView.setText(StringUtils.formatSpeed(activity, speed, metricUnits, reportSpeed));
|
||||
}
|
||||
|
||||
@@ -234,6 +255,9 @@ public class StatsUtils {
|
||||
private static void setDistanceValue(
|
||||
Activity activity, int id, double distance, boolean metricUnits) {
|
||||
TextView textView = (TextView) activity.findViewById(id);
|
||||
if (textView == null) {
|
||||
return;
|
||||
}
|
||||
textView.setText(StringUtils.formatDistance(activity, distance, metricUnits));
|
||||
}
|
||||
|
||||
@@ -246,6 +270,9 @@ public class StatsUtils {
|
||||
*/
|
||||
private static void setTimeValue(Activity activity, int id, long time) {
|
||||
TextView textView = (TextView) activity.findViewById(id);
|
||||
if (textView == null) {
|
||||
return;
|
||||
}
|
||||
String value = time == -1L ? activity.getString(R.string.value_unknown)
|
||||
: StringUtils.formatElapsedTime(time);
|
||||
textView.setText(value);
|
||||
@@ -262,6 +289,9 @@ public class StatsUtils {
|
||||
private static void setElevationValue(
|
||||
Activity activity, int id, double elevation, boolean metricUnits) {
|
||||
TextView textView = (TextView) activity.findViewById(id);
|
||||
if (textView == null) {
|
||||
return;
|
||||
}
|
||||
String value;
|
||||
if (Double.isNaN(elevation) || Double.isInfinite(elevation)) {
|
||||
value = activity.getString(R.string.value_unknown);
|
||||
@@ -285,6 +315,9 @@ public class StatsUtils {
|
||||
*/
|
||||
private static void setGradeValue(Activity activity, int id, double grade) {
|
||||
TextView textView = (TextView) activity.findViewById(id);
|
||||
if (textView == null) {
|
||||
return;
|
||||
}
|
||||
String value;
|
||||
if (Double.isNaN(grade) || Double.isInfinite(grade)) {
|
||||
value = activity.getString(R.string.value_unknown);
|
||||
@@ -303,6 +336,9 @@ public class StatsUtils {
|
||||
*/
|
||||
private static void setCoordinateValue(Activity activity, int id, double coordinate) {
|
||||
TextView textView = (TextView) activity.findViewById(id);
|
||||
if (textView == null) {
|
||||
return;
|
||||
}
|
||||
String value;
|
||||
if (Double.isNaN(coordinate) || Double.isInfinite(coordinate)) {
|
||||
value = activity.getString(R.string.value_unknown);
|
||||
|
||||
Reference in New Issue
Block a user