[android] add api to enable/disable tile prefetching

This commit is contained in:
Ivo van Dongen
2017-07-18 16:26:24 +02:00
committed by Ivo van Dongen
parent c0dd1103e0
commit c5300ebabf
8 changed files with 128 additions and 1 deletions

View File

@@ -99,6 +99,7 @@ public final class MapboxMap {
setDebugActive(options.getDebugActive());
setApiBaseUrl(options);
setStyleUrl(options);
setPrefetchesTiles(options);
}
/**
@@ -247,6 +248,38 @@ public final class MapboxMap {
nativeMapView.setTransitionDelay(delayMs);
}
/**
* Sets tile pre-fetching from MapboxOptions.
*
* @param options the options object
*/
private void setPrefetchesTiles(@NonNull MapboxMapOptions options) {
setPrefetchesTiles(options.getPrefetchesTiles());
}
/**
* Enable or disable tile pre-fetching. Pre-fetching makes sure that a low-resolution
* tile is rendered as soon as possible at the expense of a little bandwidth.
*
* @param enable true to enable
*/
@UiThread
public void setPrefetchesTiles(boolean enable) {
nativeMapView.setPrefetchesTiles(enable);
}
/**
* Check whether tile pre-fetching is enabled or not.
*
* @return true if enabled
*
* @see MapboxMap#setPrefetchesTiles(boolean)
*/
@UiThread
public boolean getPrefetchesTiles() {
return nativeMapView.getPrefetchesTiles();
}
/**
* Retrieve all the layers in the style
*

View File

@@ -80,6 +80,7 @@ public class MapboxMapOptions implements Parcelable {
private int myLocationAccuracyTintColor;
private int myLocationAccuracyAlpha;
private float myLocationAccuracyThreshold;
private boolean prefetchesTiles = true;
private String apiBaseUrl;
@@ -154,6 +155,7 @@ public class MapboxMapOptions implements Parcelable {
style = in.readString();
apiBaseUrl = in.readString();
textureMode = in.readByte() != 0;
prefetchesTiles = in.readByte() != 0;
}
static Bitmap getBitmapFromDrawable(Drawable drawable) {
@@ -297,6 +299,8 @@ public class MapboxMapOptions implements Parcelable {
typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_myLocationAccuracyThreshold, 0));
mapboxMapOptions.textureMode(
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_renderTextureMode, false));
mapboxMapOptions.setPrefetchesTiles(
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_enableTilePrefetch, true));
} finally {
typedArray.recycle();
}
@@ -711,6 +715,30 @@ public class MapboxMapOptions implements Parcelable {
return this;
}
/**
* Enable tile pre-fetching. Loads tiles at a lower zoom-level to pre-render
* a low resolution preview while more detailed tiles are loaded.
*
* Enabled by default
*
* @param enable true to enable
*
* @return This
*/
public MapboxMapOptions setPrefetchesTiles(boolean enable) {
this.prefetchesTiles = enable;
return this;
}
/**
* Check whether tile pre-fetching is enabled.
*
* @return true if enabled
*/
public boolean getPrefetchesTiles() {
return prefetchesTiles;
}
/**
* Get the current configured API endpoint base URL.
*
@@ -1094,6 +1122,7 @@ public class MapboxMapOptions implements Parcelable {
dest.writeString(style);
dest.writeString(apiBaseUrl);
dest.writeByte((byte) (textureMode ? 1 : 0));
dest.writeByte((byte) (prefetchesTiles ? 1 : 0));
}
@Override
@@ -1217,6 +1246,9 @@ public class MapboxMapOptions implements Parcelable {
if (apiBaseUrl != null ? !apiBaseUrl.equals(options.apiBaseUrl) : options.apiBaseUrl != null) {
return false;
}
if (prefetchesTiles != options.prefetchesTiles) {
return false;
}
return false;
}
@@ -1263,6 +1295,7 @@ public class MapboxMapOptions implements Parcelable {
result = 31 * result + (apiBaseUrl != null ? apiBaseUrl.hashCode() : 0);
result = 31 * result + (textureMode ? 1 : 0);
result = 31 * result + (style != null ? style.hashCode() : 0);
result = 31 * result + (prefetchesTiles ? 1 : 0);
return result;
}
}

View File

@@ -638,6 +638,20 @@ final class NativeMapView {
return nativeGetCameraPosition();
}
public void setPrefetchesTiles(boolean enable) {
if (isDestroyedOn("setPrefetchesTiles")) {
return;
}
nativeSetPrefetchesTiles(enable);
}
public boolean getPrefetchesTiles() {
if (isDestroyedOn("getPrefetchesTiles")) {
return false;
}
return nativeGetPrefetchesTiles();
}
// Runtime style Api
public long getTransitionDuration() {
@@ -1072,6 +1086,10 @@ final class NativeMapView {
private native Light nativeGetLight();
private native void nativeSetPrefetchesTiles(boolean enable);
private native boolean nativeGetPrefetchesTiles();
int getWidth() {
if (isDestroyedOn("")) {
return 0;

View File

@@ -117,6 +117,8 @@
<!-- Deprecated to use TextureView-->
<attr name="mapbox_renderTextureMode" format="boolean"/>
<attr name="mapbox_enableTilePrefetch" format="boolean"/>
</declare-styleable>
<declare-styleable name="mapbox_BubbleLayout">

View File

@@ -186,5 +186,15 @@ public class MapboxMapOptionsTest {
assertEquals(Color.BLUE, new MapboxMapOptions()
.myLocationBackgroundTintColor(Color.BLUE).getMyLocationBackgroundTintColor());
}
@Test
public void testPrefetchesTiles() {
// Default value
assertTrue(new MapboxMapOptions().getPrefetchesTiles());
// Check mutations
assertTrue(new MapboxMapOptions().setPrefetchesTiles(true).getPrefetchesTiles());
assertFalse(new MapboxMapOptions().setPrefetchesTiles(false).getPrefetchesTiles());
}
}

View File

@@ -779,6 +779,22 @@ public class MapboxMapTest extends BaseActivityTest {
}));
}
// Tile pre-fetching
@Test
public void testTilePrefetch() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapboxMapAction(new InvokeViewAction() {
@Override
public void onViewAction(UiController uiController, View view) {
mapboxMap.setPrefetchesTiles(true);
assertTrue(mapboxMap.getPrefetchesTiles());
mapboxMap.setPrefetchesTiles(false);
assertFalse(mapboxMap.getPrefetchesTiles());
}
}));
}
private class MapboxMapAction implements ViewAction {
private InvokeViewAction invokeViewAction;

View File

@@ -1037,6 +1037,15 @@ void NativeMapView::removeImage(JNIEnv& env, jni::String name) {
map->getStyle().removeImage(jni::Make<std::string>(env, name));
}
void NativeMapView::setPrefetchesTiles(JNIEnv&, jni::jboolean enable) {
map->setPrefetchZoomDelta(enable ? util::DEFAULT_PREFETCH_ZOOM_DELTA : uint8_t(0));
}
jni::jboolean NativeMapView::getPrefetchesTiles(JNIEnv&) {
return jni::jboolean(map->getPrefetchZoomDelta() > 0);
}
// Private methods //
void NativeMapView::_initializeDisplay() {
@@ -1537,7 +1546,9 @@ void NativeMapView::registerNative(jni::JNIEnv& env) {
METHOD(&NativeMapView::removeSource, "nativeRemoveSource"),
METHOD(&NativeMapView::addImage, "nativeAddImage"),
METHOD(&NativeMapView::removeImage, "nativeRemoveImage"),
METHOD(&NativeMapView::setLatLngBounds, "nativeSetLatLngBounds")
METHOD(&NativeMapView::setLatLngBounds, "nativeSetLatLngBounds"),
METHOD(&NativeMapView::setPrefetchesTiles, "nativeSetPrefetchesTiles"),
METHOD(&NativeMapView::getPrefetchesTiles, "nativeGetPrefetchesTiles")
);
}

View File

@@ -255,6 +255,10 @@ public:
void removeImage(JNIEnv&, jni::String);
void setPrefetchesTiles(JNIEnv&, jni::jboolean);
jni::jboolean getPrefetchesTiles(JNIEnv&);
protected:
// mbgl::RendererBackend //