Make sure StatsFragment do not crash when switching tabs very quickly.

This commit is contained in:
Jimmy Shih
2012-06-22 15:27:50 -07:00
parent e47906d479
commit 6ea3c5632f
2 changed files with 81 additions and 37 deletions
@@ -118,9 +118,9 @@ public class StatsFragment extends Fragment implements TrackDataListener {
@Override @Override
public void onProviderStateChange(ProviderState state) { 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() { getActivity().runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
lastLocation = null; lastLocation = null;
StatsUtils.setLocationValues(getActivity(), lastLocation, true); StatsUtils.setLocationValues(getActivity(), lastLocation, true);
@@ -131,7 +131,7 @@ public class StatsFragment extends Fragment implements TrackDataListener {
@Override @Override
public void onCurrentLocationChanged(final Location location) { public void onCurrentLocationChanged(final Location location) {
if (isRecording()) { if (isResumed() && isRecording()) {
getActivity().runOnUiThread(new Runnable() { getActivity().runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
@@ -149,35 +149,39 @@ public class StatsFragment extends Fragment implements TrackDataListener {
@Override @Override
public void onSelectedTrackChanged(Track track, boolean isRecording) { public void onSelectedTrackChanged(Track track, boolean isRecording) {
if (uiUpdateThread == null && isRecording) { if (isResumed()) {
uiUpdateThread = new UiUpdateThread(); if (uiUpdateThread == null && isRecording) {
uiUpdateThread.start(); uiUpdateThread = new UiUpdateThread();
} else if (uiUpdateThread != null && !isRecording) { uiUpdateThread.start();
uiUpdateThread.interrupt(); } else if (uiUpdateThread != null && !isRecording) {
uiUpdateThread = null; uiUpdateThread.interrupt();
uiUpdateThread = null;
}
} }
} }
@Override @Override
public void onTrackUpdated(final Track track) { public void onTrackUpdated(final Track track) {
getActivity().runOnUiThread(new Runnable() { if (isResumed()) {
@Override getActivity().runOnUiThread(new Runnable() {
public void run() { @Override
if (track == null || track.getTripStatistics() == null) { public void run() {
lastLocation = null; if (track == null || track.getTripStatistics() == null) {
lastTripStatistics = null; lastLocation = null;
lastTripStatistics = null;
updateUi();
return;
}
lastTripStatistics = track.getTripStatistics();
startTime = track.getTripStatistics().getStartTime();
if (!isRecording()) {
lastLocation = null;
}
updateUi(); updateUi();
return;
} }
lastTripStatistics = track.getTripStatistics(); });
}
startTime = track.getTripStatistics().getStartTime();
if (!isRecording()) {
lastLocation = null;
}
updateUi();
}
});
} }
@Override @Override
@@ -222,23 +226,27 @@ public class StatsFragment extends Fragment implements TrackDataListener {
@Override @Override
public boolean onUnitsChanged(final boolean metric) { public boolean onUnitsChanged(final boolean metric) {
getActivity().runOnUiThread(new Runnable() { if (isResumed()) {
@Override getActivity().runOnUiThread(new Runnable() {
public void run() { @Override
updateUi(); public void run() {
} updateUi();
}); }
});
}
return true; return true;
} }
@Override @Override
public boolean onReportSpeedChanged(final boolean speed) { public boolean onReportSpeedChanged(final boolean speed) {
getActivity().runOnUiThread(new Runnable() { if (isResumed()) {
@Override getActivity().runOnUiThread(new Runnable() {
public void run() { @Override
updateUi(); public void run() {
} updateUi();
}); }
});
}
return true; return true;
} }
@@ -70,6 +70,9 @@ public class StatsUtils {
R.string.stats_show_coordinate_key, PreferencesUtils.STATS_SHOW_COORDINATE_DEFAULT); R.string.stats_show_coordinate_key, PreferencesUtils.STATS_SHOW_COORDINATE_DEFAULT);
View coordinateLabelTableRow = activity.findViewById(R.id.stats_coordinate_label_table_row); View coordinateLabelTableRow = activity.findViewById(R.id.stats_coordinate_label_table_row);
View coordinateValueTableRow = activity.findViewById(R.id.stats_coordinate_value_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); coordinateLabelTableRow.setVisibility(showCoordinate ? View.VISIBLE : View.GONE);
coordinateValueTableRow.setVisibility(showCoordinate ? View.VISIBLE : View.GONE); coordinateValueTableRow.setVisibility(showCoordinate ? View.VISIBLE : View.GONE);
if (showCoordinate) { if (showCoordinate) {
@@ -149,6 +152,12 @@ public class StatsUtils {
View elevationValueTableRow1 = activity.findViewById(R.id.stats_elevation_value_table_row1); View elevationValueTableRow1 = activity.findViewById(R.id.stats_elevation_value_table_row1);
View elevationLabelTableRow2 = activity.findViewById(R.id.stats_elevation_label_table_row2); View elevationLabelTableRow2 = activity.findViewById(R.id.stats_elevation_label_table_row2);
View elevationValueTableRow2 = activity.findViewById(R.id.stats_elevation_value_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); elevationLabelTableRow1.setVisibility(showElevation ? View.VISIBLE : View.GONE);
elevationValueTableRow1.setVisibility(showElevation ? View.VISIBLE : View.GONE); elevationValueTableRow1.setVisibility(showElevation ? View.VISIBLE : View.GONE);
elevationLabelTableRow2.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); activity, R.string.stats_show_grade_key, PreferencesUtils.STATS_SHOW_GRADE_DEFAULT);
View gradeLabelTableRow = activity.findViewById(R.id.stats_grade_label_table_row); View gradeLabelTableRow = activity.findViewById(R.id.stats_grade_label_table_row);
View gradeValueTableRow = activity.findViewById(R.id.stats_grade_value_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); gradeLabelTableRow.setVisibility(showGrade ? View.VISIBLE : View.GONE);
gradeValueTableRow.setVisibility(showGrade ? View.VISIBLE : View.GONE); gradeValueTableRow.setVisibility(showGrade ? View.VISIBLE : View.GONE);
if (showGrade) { if (showGrade) {
@@ -190,6 +202,9 @@ public class StatsUtils {
private static void setTimeLabel(Activity activity, int id, int totalTimeId, int movingTimeId, private static void setTimeLabel(Activity activity, int id, int totalTimeId, int movingTimeId,
boolean totalTime) { boolean totalTime) {
TextView textView = (TextView) activity.findViewById(id); TextView textView = (TextView) activity.findViewById(id);
if (textView == null) {
return;
}
textView.setText(totalTime ? totalTimeId : movingTimeId); textView.setText(totalTime ? totalTimeId : movingTimeId);
} }
@@ -205,6 +220,9 @@ public class StatsUtils {
private static void setSpeedLabel( private static void setSpeedLabel(
Activity activity, int id, int speedId, int paceId, boolean reportSpeed) { Activity activity, int id, int speedId, int paceId, boolean reportSpeed) {
TextView textView = (TextView) activity.findViewById(id); TextView textView = (TextView) activity.findViewById(id);
if (textView == null) {
return;
}
textView.setText(reportSpeed ? speedId : paceId); textView.setText(reportSpeed ? speedId : paceId);
} }
@@ -220,6 +238,9 @@ public class StatsUtils {
private static void setSpeedValue( private static void setSpeedValue(
Activity activity, int id, double speed, boolean metricUnits, boolean reportSpeed) { Activity activity, int id, double speed, boolean metricUnits, boolean reportSpeed) {
TextView textView = (TextView) activity.findViewById(id); TextView textView = (TextView) activity.findViewById(id);
if (textView == null) {
return;
}
textView.setText(StringUtils.formatSpeed(activity, speed, metricUnits, reportSpeed)); textView.setText(StringUtils.formatSpeed(activity, speed, metricUnits, reportSpeed));
} }
@@ -234,6 +255,9 @@ public class StatsUtils {
private static void setDistanceValue( private static void setDistanceValue(
Activity activity, int id, double distance, boolean metricUnits) { Activity activity, int id, double distance, boolean metricUnits) {
TextView textView = (TextView) activity.findViewById(id); TextView textView = (TextView) activity.findViewById(id);
if (textView == null) {
return;
}
textView.setText(StringUtils.formatDistance(activity, distance, metricUnits)); textView.setText(StringUtils.formatDistance(activity, distance, metricUnits));
} }
@@ -246,6 +270,9 @@ public class StatsUtils {
*/ */
private static void setTimeValue(Activity activity, int id, long time) { private static void setTimeValue(Activity activity, int id, long time) {
TextView textView = (TextView) activity.findViewById(id); TextView textView = (TextView) activity.findViewById(id);
if (textView == null) {
return;
}
String value = time == -1L ? activity.getString(R.string.value_unknown) String value = time == -1L ? activity.getString(R.string.value_unknown)
: StringUtils.formatElapsedTime(time); : StringUtils.formatElapsedTime(time);
textView.setText(value); textView.setText(value);
@@ -262,6 +289,9 @@ public class StatsUtils {
private static void setElevationValue( private static void setElevationValue(
Activity activity, int id, double elevation, boolean metricUnits) { Activity activity, int id, double elevation, boolean metricUnits) {
TextView textView = (TextView) activity.findViewById(id); TextView textView = (TextView) activity.findViewById(id);
if (textView == null) {
return;
}
String value; String value;
if (Double.isNaN(elevation) || Double.isInfinite(elevation)) { if (Double.isNaN(elevation) || Double.isInfinite(elevation)) {
value = activity.getString(R.string.value_unknown); value = activity.getString(R.string.value_unknown);
@@ -285,6 +315,9 @@ public class StatsUtils {
*/ */
private static void setGradeValue(Activity activity, int id, double grade) { private static void setGradeValue(Activity activity, int id, double grade) {
TextView textView = (TextView) activity.findViewById(id); TextView textView = (TextView) activity.findViewById(id);
if (textView == null) {
return;
}
String value; String value;
if (Double.isNaN(grade) || Double.isInfinite(grade)) { if (Double.isNaN(grade) || Double.isInfinite(grade)) {
value = activity.getString(R.string.value_unknown); value = activity.getString(R.string.value_unknown);
@@ -303,6 +336,9 @@ public class StatsUtils {
*/ */
private static void setCoordinateValue(Activity activity, int id, double coordinate) { private static void setCoordinateValue(Activity activity, int id, double coordinate) {
TextView textView = (TextView) activity.findViewById(id); TextView textView = (TextView) activity.findViewById(id);
if (textView == null) {
return;
}
String value; String value;
if (Double.isNaN(coordinate) || Double.isInfinite(coordinate)) { if (Double.isNaN(coordinate) || Double.isInfinite(coordinate)) {
value = activity.getString(R.string.value_unknown); value = activity.getString(R.string.value_unknown);