Add synchronization when accessing waypoints in ChartView

This commit is contained in:
Jimmy Shih
2012-12-03 10:17:33 -08:00
parent 76c7fcd89a
commit b0713cc0e7
@@ -309,15 +309,19 @@ public class ChartView extends View {
* @param waypoint the waypoint * @param waypoint the waypoint
*/ */
public void addWaypoint(Waypoint waypoint) { public void addWaypoint(Waypoint waypoint) {
synchronized (waypoints) {
waypoints.add(waypoint); waypoints.add(waypoint);
} }
}
/** /**
* Clears the waypoints. * Clears the waypoints.
*/ */
public void clearWaypoints() { public void clearWaypoints() {
synchronized (waypoints) {
waypoints.clear(); waypoints.clear();
} }
}
/** /**
* Returns true if can zoom in. * Returns true if can zoom in.
@@ -445,6 +449,7 @@ public class ChartView extends View {
if (Math.abs(event.getY() - topBorder - spacer - markerHeight / 2) < markerHeight) { if (Math.abs(event.getY() - topBorder - spacer - markerHeight / 2) < markerHeight) {
int minDistance = Integer.MAX_VALUE; int minDistance = Integer.MAX_VALUE;
Waypoint nearestWaypoint = null; Waypoint nearestWaypoint = null;
synchronized (waypoints) {
for (int i = 0; i < waypoints.size(); i++) { for (int i = 0; i < waypoints.size(); i++) {
Waypoint waypoint = waypoints.get(i); Waypoint waypoint = waypoints.get(i);
int distance = Math.abs( int distance = Math.abs(
@@ -454,6 +459,7 @@ public class ChartView extends View {
nearestWaypoint = waypoint; nearestWaypoint = waypoint;
} }
} }
}
if (nearestWaypoint != null && minDistance < markerWidth) { if (nearestWaypoint != null && minDistance < markerWidth) {
Intent intent = IntentUtils.newIntent(getContext(), MarkerDetailActivity.class) Intent intent = IntentUtils.newIntent(getContext(), MarkerDetailActivity.class)
.putExtra(MarkerDetailActivity.EXTRA_MARKER_ID, nearestWaypoint.getId()); .putExtra(MarkerDetailActivity.EXTRA_MARKER_ID, nearestWaypoint.getId());
@@ -543,6 +549,7 @@ public class ChartView extends View {
* @param canvas the canvas * @param canvas the canvas
*/ */
private void drawWaypoints(Canvas canvas) { private void drawWaypoints(Canvas canvas) {
synchronized (waypoints) {
for (int i = 0; i < waypoints.size(); i++) { for (int i = 0; i < waypoints.size(); i++) {
final Waypoint waypoint = waypoints.get(i); final Waypoint waypoint = waypoints.get(i);
if (waypoint.getLocation() == null) { if (waypoint.getLocation() == null) {
@@ -556,8 +563,8 @@ public class ChartView extends View {
float x = getX(getWaypointXValue(waypoint)); float x = getX(getWaypointXValue(waypoint));
canvas.drawLine( canvas.drawLine(
x, topBorder + spacer + markerHeight / 2, x, topBorder + effectiveHeight, markerPaint); x, topBorder + spacer + markerHeight / 2, x, topBorder + effectiveHeight, markerPaint);
canvas.translate(x - (float) (markerWidth * MapOverlay.WAYPOINT_X_ANCHOR), canvas.translate(
topBorder + spacer); x - (float) (markerWidth * MapOverlay.WAYPOINT_X_ANCHOR), topBorder + spacer);
if (waypoints.get(i).getType() == Waypoint.TYPE_STATISTICS) { if (waypoints.get(i).getType() == Waypoint.TYPE_STATISTICS) {
statisticsMarker.draw(canvas); statisticsMarker.draw(canvas);
} else { } else {
@@ -566,6 +573,7 @@ public class ChartView extends View {
canvas.restore(); canvas.restore();
} }
} }
}
/** /**
* Draws the grid. * Draws the grid.