[android] remove usage of the deprecated OnMapChange listener

This commit is contained in:
Łukasz Paczos
2018-10-26 11:56:22 +02:00
committed by Łukasz Paczos
parent 5a6d5ea311
commit e2ec125730
7 changed files with 77 additions and 52 deletions

View File

@@ -33,12 +33,10 @@ class StyleChangeIdlingResource : IdlingResource {
fun waitForStyle(mapView: MapView, mapboxMap: MapboxMap, styleUrl: String) {
isIdle = false
mapView.addOnMapChangedListener(object : MapView.OnMapChangedListener {
override fun onMapChanged(change: Int) {
if (change == MapView.DID_FINISH_LOADING_STYLE) {
mapView.removeOnMapChangedListener(this)
setIdle()
}
mapView.addOnDidFinishLoadingStyleListener(object : MapView.OnDidFinishLoadingStyleListener {
override fun onDidFinishLoadingStyle() {
mapView.removeOnDidFinishLoadingStyleListener(this)
setIdle()
}
})
mapboxMap.setStyleUrl(styleUrl)

View File

@@ -16,6 +16,7 @@ import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.MarkerViewOptions;
@@ -25,7 +26,6 @@ import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.utils.GeoParseUtil;
import com.mapbox.mapboxsdk.testapp.utils.IconUtils;
import timber.log.Timber;
import java.io.IOException;
import java.lang.ref.WeakReference;
@@ -35,6 +35,8 @@ import java.util.List;
import java.util.Locale;
import java.util.Random;
import timber.log.Timber;
/**
* Test activity showcasing adding a large amount of Markers or MarkerViews.
*/
@@ -224,12 +226,17 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
viewCountView = (TextView) findViewById(R.id.countView);
mapView.addOnMapChangedListener(change -> {
if (change == MapView.REGION_IS_CHANGING || change == MapView.REGION_DID_CHANGE) {
if (!mapboxMap.getMarkerViewManager().getMarkerViewAdapters().isEmpty()) {
viewCountView.setText(String.format(Locale.getDefault(), "ViewCache size %d",
mapboxMap.getMarkerViewManager().getMarkerViewContainer().getChildCount()));
}
mapView.addOnCameraIsChangingListener(new MapView.OnCameraIsChangingListener() {
@Override
public void onCameraIsChanging() {
onCameraChange();
}
});
mapView.addOnCameraDidChangeListener(new MapView.OnCameraDidChangeListener() {
@Override
public void onCameraDidChange(boolean animated) {
onCameraChange();
}
});
@@ -243,6 +250,13 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
});
}
}
private void onCameraChange() {
if (!mapboxMap.getMarkerViewManager().getMarkerViewAdapters().isEmpty()) {
viewCountView.setText(String.format(Locale.getDefault(), "ViewCache size %d",
mapboxMap.getMarkerViewManager().getMarkerViewContainer().getChildCount()));
}
}
}
private static class LoadLocationTask extends AsyncTask<Void, Integer, List<LatLng>> {

View File

@@ -58,6 +58,7 @@ public class MarkerViewActivity extends AppCompatActivity {
private MapboxMap mapboxMap;
private MapView mapView;
private TextView viewCountView;
// MarkerView location updates
private MarkerView movingMarkerOne;
@@ -77,7 +78,7 @@ public class MarkerViewActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marker_view);
final TextView viewCountView = (TextView) findViewById(R.id.countView);
viewCountView = (TextView) findViewById(R.id.countView);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(mapboxMap -> {
@@ -140,18 +141,12 @@ public class MarkerViewActivity extends AppCompatActivity {
markerViewManager.addMarkerViewAdapter(new CountryAdapter(MarkerViewActivity.this, mapboxMap));
markerViewManager.addMarkerViewAdapter(new TextAdapter(MarkerViewActivity.this, mapboxMap));
final ViewGroup markerViewContainer = markerViewManager.getMarkerViewContainer();
// add a change listener to validate the size of amount of child views
mapView.addOnMapChangedListener(change -> {
if (change == MapView.REGION_IS_CHANGING || change == MapView.REGION_DID_CHANGE) {
if (!markerViewManager.getMarkerViewAdapters().isEmpty() && viewCountView != null) {
viewCountView.setText(String.format(
Locale.getDefault(),
getString(R.string.viewcache_size),
markerViewContainer.getChildCount())
);
}
mapView.addOnCameraDidChangeListener(new MapView.OnCameraDidChangeListener() {
@Override
public void onCameraDidChange(boolean animated) {
setViewCount();
}
});
@@ -194,6 +189,18 @@ public class MarkerViewActivity extends AppCompatActivity {
});
}
private void setViewCount() {
final MarkerViewManager markerViewManager = mapboxMap.getMarkerViewManager();
final ViewGroup markerViewContainer = markerViewManager.getMarkerViewContainer();
if (!markerViewManager.getMarkerViewAdapters().isEmpty() && viewCountView != null) {
viewCountView.setText(String.format(
Locale.getDefault(),
getString(R.string.viewcache_size),
markerViewContainer.getChildCount())
);
}
}
private void loopMarkerRotate() {
rotateUpdateHandler.postDelayed(rotateMarkerRunnable, 800);
}

View File

@@ -2,6 +2,7 @@ package com.mapbox.mapboxsdk.testapp.activity.fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
@@ -20,7 +21,7 @@ import com.mapbox.mapboxsdk.testapp.R;
* </p>
*/
public class MapFragmentActivity extends AppCompatActivity implements MapFragment.OnMapViewReadyCallback,
OnMapReadyCallback, MapView.OnMapChangedListener {
OnMapReadyCallback, MapView.OnDidFinishRenderingFrameListener {
private MapboxMap mapboxMap;
private MapView mapView;
@@ -31,10 +32,10 @@ public class MapFragmentActivity extends AppCompatActivity implements MapFragmen
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_fragment);
if (savedInstanceState == null) {
MapFragment mapFragment = MapFragment.newInstance(createFragmentOptions());
MapFragment mapFragment = MapFragment.newInstance(createFragmentOptions());
getFragmentManager()
.beginTransaction()
.add(R.id.fragment_container,mapFragment, "com.mapbox.map")
.add(R.id.fragment_container, mapFragment, "com.mapbox.map")
.commit();
mapFragment.getMapAsync(this);
}
@@ -64,7 +65,7 @@ public class MapFragmentActivity extends AppCompatActivity implements MapFragmen
@Override
public void onMapViewReady(MapView map) {
mapView = map;
mapView.addOnMapChangedListener(this);
mapView.addOnDidFinishRenderingFrameListener(this);
}
@Override
@@ -73,17 +74,17 @@ public class MapFragmentActivity extends AppCompatActivity implements MapFragmen
}
@Override
public void onMapChanged(int change) {
if (initialCameraAnimation && change == MapView.DID_FINISH_RENDERING_MAP_FULLY_RENDERED && mapboxMap != null) {
protected void onDestroy() {
super.onDestroy();
mapView.removeOnDidFinishRenderingFrameListener(this);
}
@Override
public void onDidFinishRenderingFrame(boolean fully) {
if (initialCameraAnimation && fully && mapboxMap != null) {
mapboxMap.animateCamera(
CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().tilt(45.0).build()), 5000);
initialCameraAnimation = false;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.removeOnMapChangedListener(this);
}
}

View File

@@ -2,6 +2,7 @@ package com.mapbox.mapboxsdk.testapp.activity.fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
@@ -21,7 +22,7 @@ import com.mapbox.mapboxsdk.testapp.R;
* </p>
*/
public class SupportMapFragmentActivity extends AppCompatActivity implements MapFragment.OnMapViewReadyCallback,
OnMapReadyCallback, MapView.OnMapChangedListener {
OnMapReadyCallback, MapView.OnDidFinishRenderingFrameListener {
private MapboxMap mapboxMap;
private MapView mapView;
@@ -65,7 +66,7 @@ public class SupportMapFragmentActivity extends AppCompatActivity implements Map
@Override
public void onMapViewReady(MapView map) {
mapView = map;
mapView.addOnMapChangedListener(this);
mapView.addOnDidFinishRenderingFrameListener(this);
}
@Override
@@ -74,17 +75,17 @@ public class SupportMapFragmentActivity extends AppCompatActivity implements Map
}
@Override
public void onMapChanged(int change) {
if (initialCameraAnimation && change == MapView.DID_FINISH_RENDERING_MAP_FULLY_RENDERED && mapboxMap != null) {
protected void onDestroy() {
super.onDestroy();
mapView.removeOnDidFinishRenderingFrameListener(this);
}
@Override
public void onDidFinishRenderingFrame(boolean fully) {
if (initialCameraAnimation && fully && mapboxMap != null) {
mapboxMap.animateCamera(
CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().tilt(45.0).build()), 5000);
initialCameraAnimation = false;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.removeOnMapChangedListener(this);
}
}

View File

@@ -80,9 +80,9 @@ public class DebugModeActivity extends AppCompatActivity implements OnMapReadyCa
private void setupMapView(Bundle savedInstanceState) {
mapView = (MapView) findViewById(R.id.mapView);
mapView.addOnMapChangedListener(change -> {
if (change == MapView.DID_FINISH_LOADING_STYLE && mapboxMap != null) {
Timber.v("New style loaded with JSON: %s", mapboxMap.getStyleJson());
mapView.addOnDidFinishLoadingStyleListener(() -> {
if (mapboxMap != null) {
setupNavigationView(mapboxMap.getLayers());
}
});
@@ -98,6 +98,8 @@ public class DebugModeActivity extends AppCompatActivity implements OnMapReadyCa
mapboxMap = map;
mapboxMap.getUiSettings().setZoomControlsEnabled(true);
setupNavigationView(mapboxMap.getLayers());
setupNavigationView(mapboxMap.getLayers());
setupZoomView();
setFpsView();
@@ -111,6 +113,7 @@ public class DebugModeActivity extends AppCompatActivity implements OnMapReadyCa
}
private void setupNavigationView(List<Layer> layerList) {
Timber.v("New style loaded with JSON: %s", mapboxMap.getStyleJson());
final LayerListAdapter adapter = new LayerListAdapter(this, layerList);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);

View File

@@ -80,9 +80,9 @@ public class TextureViewDebugModeActivity extends AppCompatActivity implements O
private void setupMapView(Bundle savedInstanceState) {
mapView = (MapView) findViewById(R.id.mapView);
mapView.addOnMapChangedListener(change -> {
if (change == MapView.DID_FINISH_LOADING_STYLE && mapboxMap != null) {
Timber.v("New style loaded with JSON: %s", mapboxMap.getStyleJson());
mapView.addOnDidFinishLoadingStyleListener(() -> {
if (mapboxMap != null) {
setupNavigationView(mapboxMap.getLayers());
}
});
@@ -109,6 +109,7 @@ public class TextureViewDebugModeActivity extends AppCompatActivity implements O
}
private void setupNavigationView(List<Layer> layerList) {
Timber.v("New style loaded with JSON: %s", mapboxMap.getStyleJson());
final LayerListAdapter adapter = new LayerListAdapter(this, layerList);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);