forked from upstream-mirrors/OpenTracks
In ChartFragment, MapFragment, StatsFragment, for callbacks from
TrackDataHub, check that the fragment is resumed. In any runnable executed on the UI thread, also check that the fragment is resumed.
This commit is contained in:
@@ -83,7 +83,7 @@ public class ChartFragment extends Fragment implements TrackDataListener {
|
|||||||
private final Runnable updateChart = new Runnable() {
|
private final Runnable updateChart = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (trackDataHub == null) {
|
if (!isResumed() || trackDataHub == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,70 +178,88 @@ public class ChartFragment extends Fragment implements TrackDataListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTrackUpdated(Track track) {
|
public void onTrackUpdated(Track track) {
|
||||||
|
if (isResumed()) {
|
||||||
if (track == null || track.getTripStatistics() == null) {
|
if (track == null || track.getTripStatistics() == null) {
|
||||||
startTime = -1L;
|
startTime = -1L;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
startTime = track.getTripStatistics().getStartTime();
|
startTime = track.getTripStatistics().getStartTime();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearTrackPoints() {
|
public void clearTrackPoints() {
|
||||||
|
if (isResumed()) {
|
||||||
tripStatisticsBuilder = startTime != -1L ? new TripStatisticsBuilder(startTime) : null;
|
tripStatisticsBuilder = startTime != -1L ? new TripStatisticsBuilder(startTime) : null;
|
||||||
pendingPoints.clear();
|
pendingPoints.clear();
|
||||||
chartView.reset();
|
chartView.reset();
|
||||||
|
|
||||||
getActivity().runOnUiThread(new Runnable() {
|
getActivity().runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
if (isResumed()) {
|
||||||
chartView.resetScroll();
|
chartView.resetScroll();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSampledInTrackPoint(Location location) {
|
public void onSampledInTrackPoint(Location location) {
|
||||||
|
if (isResumed()) {
|
||||||
double[] data = new double[ChartView.NUM_SERIES + 1];
|
double[] data = new double[ChartView.NUM_SERIES + 1];
|
||||||
fillDataPoint(location, data);
|
fillDataPoint(location, data);
|
||||||
pendingPoints.add(data);
|
pendingPoints.add(data);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSampledOutTrackPoint(Location location) {
|
public void onSampledOutTrackPoint(Location location) {
|
||||||
|
if (isResumed()) {
|
||||||
fillDataPoint(location, null);
|
fillDataPoint(location, null);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSegmentSplit(Location location) {
|
public void onSegmentSplit(Location location) {
|
||||||
|
if (isResumed()) {
|
||||||
fillDataPoint(location, null);
|
fillDataPoint(location, null);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNewTrackPointsDone() {
|
public void onNewTrackPointsDone() {
|
||||||
|
if (isResumed()) {
|
||||||
chartView.addDataPoints(pendingPoints);
|
chartView.addDataPoints(pendingPoints);
|
||||||
pendingPoints.clear();
|
pendingPoints.clear();
|
||||||
getActivity().runOnUiThread(updateChart);
|
getActivity().runOnUiThread(updateChart);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearWaypoints() {
|
public void clearWaypoints() {
|
||||||
|
if (isResumed()) {
|
||||||
chartView.clearWaypoints();
|
chartView.clearWaypoints();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNewWaypoint(Waypoint waypoint) {
|
public void onNewWaypoint(Waypoint waypoint) {
|
||||||
if (waypoint != null && LocationUtils.isValidLocation(waypoint.getLocation())) {
|
if (isResumed() && waypoint != null && LocationUtils.isValidLocation(waypoint.getLocation())) {
|
||||||
chartView.addWaypoint(waypoint);
|
chartView.addWaypoint(waypoint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNewWaypointsDone() {
|
public void onNewWaypointsDone() {
|
||||||
|
if (isResumed()) {
|
||||||
getActivity().runOnUiThread(updateChart);
|
getActivity().runOnUiThread(updateChart);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onMetricUnitsChanged(boolean metric) {
|
public boolean onMetricUnitsChanged(boolean metric) {
|
||||||
|
if (isResumed()) {
|
||||||
if (metricUnits == metric) {
|
if (metricUnits == metric) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -250,14 +268,19 @@ public class ChartFragment extends Fragment implements TrackDataListener {
|
|||||||
getActivity().runOnUiThread(new Runnable() {
|
getActivity().runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
if (isResumed()) {
|
||||||
chartView.requestLayout();
|
chartView.requestLayout();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onReportSpeedChanged(boolean speed) {
|
public boolean onReportSpeedChanged(boolean speed) {
|
||||||
|
if (isResumed()) {
|
||||||
if (reportSpeed == speed) {
|
if (reportSpeed == speed) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -270,20 +293,27 @@ public class ChartFragment extends Fragment implements TrackDataListener {
|
|||||||
getActivity().runOnUiThread(new Runnable() {
|
getActivity().runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
if (isResumed()) {
|
||||||
chartView.requestLayout();
|
chartView.requestLayout();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onMinRecordingDistanceChanged(int value) {
|
public boolean onMinRecordingDistanceChanged(int value) {
|
||||||
|
if (isResumed()) {
|
||||||
if (minRecordingDistance == value) {
|
if (minRecordingDistance == value) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
minRecordingDistance = value;
|
minRecordingDistance = value;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks the chart settings.
|
* Checks the chart settings.
|
||||||
|
|||||||
@@ -275,6 +275,7 @@ public class MapFragment extends Fragment
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLocationStateChanged(LocationState state) {
|
public void onLocationStateChanged(LocationState state) {
|
||||||
|
if (isResumed()) {
|
||||||
final String message;
|
final String message;
|
||||||
final boolean isGpsDisabled;
|
final boolean isGpsDisabled;
|
||||||
if (!isSelectedTrackRecording()) {
|
if (!isSelectedTrackRecording()) {
|
||||||
@@ -308,10 +309,13 @@ public class MapFragment extends Fragment
|
|||||||
getActivity().runOnUiThread(new Runnable() {
|
getActivity().runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if (message != null) {
|
if (isResumed()) {
|
||||||
|
if (message == null) {
|
||||||
|
messageTextView.setVisibility(View.GONE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
messageTextView.setText(message);
|
messageTextView.setText(message);
|
||||||
messageTextView.setVisibility(View.VISIBLE);
|
messageTextView.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
if (isGpsDisabled) {
|
if (isGpsDisabled) {
|
||||||
Toast.makeText(getActivity(), R.string.gps_not_found, Toast.LENGTH_LONG).show();
|
Toast.makeText(getActivity(), R.string.gps_not_found, Toast.LENGTH_LONG).show();
|
||||||
|
|
||||||
@@ -320,39 +324,44 @@ public class MapFragment extends Fragment
|
|||||||
} else {
|
} else {
|
||||||
messageTextView.setOnClickListener(null);
|
messageTextView.setOnClickListener(null);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
messageTextView.setVisibility(View.GONE);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLocationChanged(Location location) {
|
public void onLocationChanged(Location location) {
|
||||||
|
if (isResumed()) {
|
||||||
currentLocation = location;
|
currentLocation = location;
|
||||||
updateCurrentLocation();
|
updateCurrentLocation();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onHeadingChanged(double heading) {
|
public void onHeadingChanged(double heading) {
|
||||||
|
if (isResumed()) {
|
||||||
if (mapOverlay.setHeading((float) heading)) {
|
if (mapOverlay.setHeading((float) heading)) {
|
||||||
mapView.postInvalidate();
|
mapView.postInvalidate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSelectedTrackChanged(final Track track) {
|
public void onSelectedTrackChanged(final Track track) {
|
||||||
|
if (isResumed()) {
|
||||||
getActivity().runOnUiThread(new Runnable() {
|
getActivity().runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
if (isResumed()) {
|
||||||
boolean hasTrack = track != null;
|
boolean hasTrack = track != null;
|
||||||
mapOverlay.setTrackDrawingEnabled(hasTrack);
|
mapOverlay.setTrackDrawingEnabled(hasTrack);
|
||||||
|
|
||||||
if (hasTrack) {
|
if (hasTrack) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
/*
|
/*
|
||||||
* Synchronize to prevent race condition in changing markerTrackId
|
* Synchronize to prevent race condition in changing
|
||||||
* and markerId variables.
|
* markerTrackId and markerId variables.
|
||||||
*/
|
*/
|
||||||
currentSelectedTrackId = track.getId();
|
currentSelectedTrackId = track.getId();
|
||||||
updateMap(track);
|
updateMap(track);
|
||||||
@@ -361,8 +370,10 @@ public class MapFragment extends Fragment
|
|||||||
}
|
}
|
||||||
mapView.invalidate();
|
mapView.invalidate();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTrackUpdated(Track track) {
|
public void onTrackUpdated(Track track) {
|
||||||
@@ -371,13 +382,17 @@ public class MapFragment extends Fragment
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearTrackPoints() {
|
public void clearTrackPoints() {
|
||||||
|
if (isResumed()) {
|
||||||
mapOverlay.clearPoints();
|
mapOverlay.clearPoints();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSampledInTrackPoint(Location location) {
|
public void onSampledInTrackPoint(Location location) {
|
||||||
|
if (isResumed()) {
|
||||||
mapOverlay.addLocation(location);
|
mapOverlay.addLocation(location);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSampledOutTrackPoint(Location location) {
|
public void onSampledOutTrackPoint(Location location) {
|
||||||
@@ -386,22 +401,28 @@ public class MapFragment extends Fragment
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSegmentSplit(Location location) {
|
public void onSegmentSplit(Location location) {
|
||||||
|
if (isResumed()) {
|
||||||
mapOverlay.addSegmentSplit();
|
mapOverlay.addSegmentSplit();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNewTrackPointsDone() {
|
public void onNewTrackPointsDone() {
|
||||||
|
if (isResumed()) {
|
||||||
mapView.postInvalidate();
|
mapView.postInvalidate();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearWaypoints() {
|
public void clearWaypoints() {
|
||||||
|
if (isResumed()) {
|
||||||
mapOverlay.clearWaypoints();
|
mapOverlay.clearWaypoints();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNewWaypoint(Waypoint waypoint) {
|
public void onNewWaypoint(Waypoint waypoint) {
|
||||||
if (waypoint != null && LocationUtils.isValidLocation(waypoint.getLocation())) {
|
if (isResumed() && waypoint != null && LocationUtils.isValidLocation(waypoint.getLocation())) {
|
||||||
// TODO: Optimize locking inside addWaypoint
|
// TODO: Optimize locking inside addWaypoint
|
||||||
mapOverlay.addWaypoint(waypoint);
|
mapOverlay.addWaypoint(waypoint);
|
||||||
}
|
}
|
||||||
@@ -409,8 +430,10 @@ public class MapFragment extends Fragment
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNewWaypointsDone() {
|
public void onNewWaypointsDone() {
|
||||||
|
if (isResumed()) {
|
||||||
mapView.postInvalidate();
|
mapView.postInvalidate();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onMetricUnitsChanged(boolean metric) {
|
public boolean onMetricUnitsChanged(boolean metric) {
|
||||||
|
|||||||
@@ -58,10 +58,9 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
// A runnable to update the total time field.
|
// A runnable to update the total time field.
|
||||||
private final Runnable updateTotalTime = new Runnable() {
|
private final Runnable updateTotalTime = new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
FragmentActivity activity = getActivity();
|
if (isResumed() && isSelectedTrackRecording() && !isSelectedTrackPaused()
|
||||||
if (activity != null && lastTripStatistics != null && isSelectedTrackRecording()
|
&& lastTripStatistics != null) {
|
||||||
&& !isSelectedTrackPaused()) {
|
StatsUtils.setTotalTimeValue(getActivity(), System.currentTimeMillis()
|
||||||
StatsUtils.setTotalTimeValue(activity, System.currentTimeMillis()
|
|
||||||
- lastTripStatistics.getStopTime() + lastTripStatistics.getTotalTime());
|
- lastTripStatistics.getStopTime() + lastTripStatistics.getTotalTime());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,11 +73,8 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Log.d(TAG, "UI update thread started");
|
Log.d(TAG, "UI update thread started");
|
||||||
while (isSelectedTrackRecording()) {
|
while (isResumed() && isSelectedTrackRecording()) {
|
||||||
FragmentActivity activity = getActivity();
|
getActivity().runOnUiThread(updateTotalTime);
|
||||||
if (activity != null) {
|
|
||||||
activity.runOnUiThread(updateTotalTime);
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(1000L);
|
Thread.sleep(1000L);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@@ -121,12 +117,13 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
@Override
|
@Override
|
||||||
public void onLocationStateChanged(LocationState state) {
|
public void onLocationStateChanged(LocationState state) {
|
||||||
if (isResumed() && state != LocationState.GOOD_FIX) {
|
if (isResumed() && state != LocationState.GOOD_FIX) {
|
||||||
final FragmentActivity activity = getActivity();
|
getActivity().runOnUiThread(new Runnable() {
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
if (isResumed()) {
|
||||||
lastLocation = null;
|
lastLocation = null;
|
||||||
StatsUtils.setLocationValues(activity, lastLocation, true);
|
StatsUtils.setLocationValues(getActivity(), lastLocation, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -135,17 +132,18 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
@Override
|
@Override
|
||||||
public void onLocationChanged(final Location location) {
|
public void onLocationChanged(final Location location) {
|
||||||
if (isResumed()) {
|
if (isResumed()) {
|
||||||
final FragmentActivity activity = getActivity();
|
getActivity().runOnUiThread(new Runnable() {
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
if (isResumed()) {
|
||||||
if (isSelectedTrackRecording() && !isSelectedTrackPaused()) {
|
if (isSelectedTrackRecording() && !isSelectedTrackPaused()) {
|
||||||
lastLocation = location;
|
lastLocation = location;
|
||||||
StatsUtils.setLocationValues(activity, location, true);
|
StatsUtils.setLocationValues(getActivity(), location, true);
|
||||||
} else {
|
} else {
|
||||||
if (lastLocation != null) {
|
if (lastLocation != null) {
|
||||||
lastLocation = null;
|
lastLocation = null;
|
||||||
StatsUtils.setLocationValues(activity, lastLocation, true);
|
StatsUtils.setLocationValues(getActivity(), lastLocation, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -175,12 +173,13 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
@Override
|
@Override
|
||||||
public void onTrackUpdated(final Track track) {
|
public void onTrackUpdated(final Track track) {
|
||||||
if (isResumed()) {
|
if (isResumed()) {
|
||||||
final FragmentActivity activity = getActivity();
|
getActivity().runOnUiThread(new Runnable() {
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
if (isResumed()) {
|
||||||
lastTripStatistics = track != null ? track.getTripStatistics() : null;
|
lastTripStatistics = track != null ? track.getTripStatistics() : null;
|
||||||
updateUi(activity);
|
updateUi(getActivity());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -229,11 +228,12 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
@Override
|
@Override
|
||||||
public boolean onMetricUnitsChanged(final boolean metric) {
|
public boolean onMetricUnitsChanged(final boolean metric) {
|
||||||
if (isResumed()) {
|
if (isResumed()) {
|
||||||
final FragmentActivity activity = getActivity();
|
getActivity().runOnUiThread(new Runnable() {
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
updateUi(activity);
|
if (isResumed()) {
|
||||||
|
updateUi(getActivity());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -243,11 +243,12 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
@Override
|
@Override
|
||||||
public boolean onReportSpeedChanged(final boolean speed) {
|
public boolean onReportSpeedChanged(final boolean speed) {
|
||||||
if (isResumed()) {
|
if (isResumed()) {
|
||||||
final FragmentActivity activity = getActivity();
|
getActivity().runOnUiThread(new Runnable() {
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
updateUi(activity);
|
if (isResumed()) {
|
||||||
|
updateUi(getActivity());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user