summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobrun <tobrun@mapbox.com>2016-02-12 11:53:20 +0100
committerTobrun <tobrun@mapbox.com>2016-02-12 11:53:20 +0100
commitab677a7905b0f81850d6aa3dcdd2caebc0dbc851 (patch)
tree80ff91c10f9541ef2c31d3993aa776bd63012d37
parenta20225bbf1d3229847bc5b36dacd8babc6d55945 (diff)
downloadqtlocation-mapboxgl-ab677a7905b0f81850d6aa3dcdd2caebc0dbc851.tar.gz
[android] #3900 - Using CopyWriteArrayList to avoid concurrency issues with MapChangeListeners
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java14
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MyLocationTrackingModeActivity.java26
2 files changed, 22 insertions, 18 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java
index d056f6ac9f..f40dff27cf 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java
@@ -88,7 +88,9 @@ import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.Iterator;
import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
/**
* <p>
@@ -124,7 +126,7 @@ public class MapView extends FrameLayout {
private ImageView mAttributionsView;
private UserLocationView mUserLocationView;
- private List<OnMapChangedListener> mOnMapChangedListener;
+ private CopyOnWriteArrayList<OnMapChangedListener> mOnMapChangedListener;
private ZoomButtonsController mZoomButtonsController;
private ConnectivityReceiver mConnectivityReceiver;
private float mScreenDensity = 1.0f;
@@ -164,7 +166,7 @@ public class MapView extends FrameLayout {
}
private void initialize(@NonNull Context context, @Nullable AttributeSet attrs) {
- mOnMapChangedListener = new ArrayList<>();
+ mOnMapChangedListener = new CopyOnWriteArrayList<>();
mMapboxMap = new MapboxMap(this);
mAnnotations = new ArrayList<>();
mIcons = new ArrayList<>();
@@ -2440,9 +2442,11 @@ public class MapView extends FrameLayout {
// Forward to any listeners
protected void onMapChanged(int mapChange) {
if (mOnMapChangedListener != null) {
- int count = mOnMapChangedListener.size();
- for (int i = 0; i < count; i++) {
- mOnMapChangedListener.get(i).onMapChanged(mapChange);
+ OnMapChangedListener listener;
+ final Iterator<OnMapChangedListener>iterator= mOnMapChangedListener.iterator();
+ while(iterator.hasNext()){
+ listener = iterator.next();
+ listener.onMapChanged(mapChange);
}
}
}
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MyLocationTrackingModeActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MyLocationTrackingModeActivity.java
index 411ca36316..1f30a4f0ea 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MyLocationTrackingModeActivity.java
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MyLocationTrackingModeActivity.java
@@ -39,25 +39,13 @@ public class MyLocationTrackingModeActivity extends AppCompatActivity implements
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
- ActionBar actionBar = getSupportActionBar();
+ final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
- ArrayAdapter<CharSequence> locationTrackingAdapter = ArrayAdapter.createFromResource(actionBar.getThemedContext(), R.array.user_tracking_mode, android.R.layout.simple_spinner_item);
- locationTrackingAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- mLocationSpinner = (Spinner) findViewById(R.id.spinner_location);
- mLocationSpinner.setAdapter(locationTrackingAdapter);
- mLocationSpinner.setOnItemSelectedListener(this);
-
- ArrayAdapter<CharSequence> bearingTrackingAdapter = ArrayAdapter.createFromResource(actionBar.getThemedContext(), R.array.user_bearing_mode, android.R.layout.simple_spinner_item);
- bearingTrackingAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- mBearingSpinner = (Spinner) findViewById(R.id.spinner_bearing);
- mBearingSpinner.setAdapter(bearingTrackingAdapter);
- mBearingSpinner.setOnItemSelectedListener(this);
-
mMapView = (MapView) findViewById(R.id.mapView);
mMapView.setAccessToken(ApiAccess.getToken(this));
mMapView.onCreate(savedInstanceState);
@@ -68,6 +56,18 @@ public class MyLocationTrackingModeActivity extends AppCompatActivity implements
mapboxMap.setOnMyLocationChangeListener(MyLocationTrackingModeActivity.this);
+ ArrayAdapter<CharSequence> locationTrackingAdapter = ArrayAdapter.createFromResource(actionBar.getThemedContext(), R.array.user_tracking_mode, android.R.layout.simple_spinner_item);
+ locationTrackingAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+ mLocationSpinner = (Spinner) findViewById(R.id.spinner_location);
+ mLocationSpinner.setAdapter(locationTrackingAdapter);
+ mLocationSpinner.setOnItemSelectedListener(MyLocationTrackingModeActivity.this);
+
+ ArrayAdapter<CharSequence> bearingTrackingAdapter = ArrayAdapter.createFromResource(actionBar.getThemedContext(), R.array.user_bearing_mode, android.R.layout.simple_spinner_item);
+ bearingTrackingAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+ mBearingSpinner = (Spinner) findViewById(R.id.spinner_bearing);
+ mBearingSpinner.setAdapter(bearingTrackingAdapter);
+ mBearingSpinner.setOnItemSelectedListener(MyLocationTrackingModeActivity.this);
+
try {
mapboxMap.setMyLocationEnabled(true);
} catch (SecurityException e) {