Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] Expose pre-fetching zoom delta. (#15769)
Browse files Browse the repository at this point in the history
* [android] Expose pre-fetching zoom delta.

* [android] Fix Clang format.

* [android] Add MapboxMap unit test.

* [android] Add prefetching zoom delta into MapboxMapOptions.

* [android] Deprecate setPrefetchesTiles to migrate to setPrefetchZoomDelta.

* [android] Deprecate getPrefetchesTiles() and migrate to setPrefetchZoomDelta().

* [android] Add unit test to NativeMapViewTest.

* [android] Add IntRange annotation to getPrefetchZoomDelta.
  • Loading branch information
pengdev authored Oct 8, 2019
1 parent 4eaaa90 commit 7139abd
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.FloatRange;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.Size;
Expand Down Expand Up @@ -246,20 +247,26 @@ long getNativeMapPtr() {
// Style

/**
* Sets tile pre-fetching from MapboxOptions.
* Sets tile pre-fetching zoom delta from MapboxOptions.
*
* @param options the options object
*/
private void setPrefetchesTiles(@NonNull MapboxMapOptions options) {
setPrefetchesTiles(options.getPrefetchesTiles());
if (!options.getPrefetchesTiles()) {
setPrefetchZoomDelta(0);
} else {
setPrefetchZoomDelta(options.getPrefetchZoomDelta());
}
}

/**
* 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
* @deprecated Use {@link #setPrefetchZoomDelta(int)} instead.
*/
@Deprecated
public void setPrefetchesTiles(boolean enable) {
nativeMapView.setPrefetchTiles(enable);
}
Expand All @@ -269,11 +276,38 @@ public void setPrefetchesTiles(boolean enable) {
*
* @return true if enabled
* @see MapboxMap#setPrefetchesTiles(boolean)
* @deprecated Use {@link #getPrefetchZoomDelta()} instead.
*/
@Deprecated
public boolean getPrefetchesTiles() {
return nativeMapView.getPrefetchTiles();
}

/**
* Set the tile pre-fetching zoom delta. Pre-fetching makes sure that a low-resolution
* tile at the (current_zoom_level - delta) is rendered as soon as possible at the
* expense of a little bandwidth.
* Note: This operation will override the MapboxMapOptions#setPrefetchesTiles(boolean)
* Setting zoom delta to 0 will disable pre-fetching.
* Default zoom delta is 4.
*
* @param delta zoom delta
*/
public void setPrefetchZoomDelta(@IntRange(from = 0) int delta) {
nativeMapView.setPrefetchZoomDelta(delta);
}

/**
* Check current pre-fetching zoom delta.
*
* @return current zoom delta.
* @see MapboxMap#setPrefetchZoomDelta(int)
*/
@IntRange(from = 0)
public int getPrefetchZoomDelta() {
return nativeMapView.getPrefetchZoomDelta();
}

//
// MinZoom
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.ColorInt;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
Expand Down Expand Up @@ -70,6 +71,7 @@ public class MapboxMapOptions implements Parcelable {
private boolean quickZoomGesturesEnabled = true;

private boolean prefetchesTiles = true;
private int prefetchZoomDelta = 4;
private boolean zMediaOverlay = false;

private boolean localIdeographFontFamilyEnabled = true;
Expand Down Expand Up @@ -134,6 +136,7 @@ private MapboxMapOptions(Parcel in) {
textureMode = in.readByte() != 0;
translucentTextureSurface = in.readByte() != 0;
prefetchesTiles = in.readByte() != 0;
prefetchZoomDelta = in.readInt();
zMediaOverlay = in.readByte() != 0;
localIdeographFontFamilyEnabled = in.readByte() != 0;
localIdeographFontFamily = in.readString();
Expand Down Expand Up @@ -257,6 +260,8 @@ static MapboxMapOptions createFromAttributes(@NonNull MapboxMapOptions mapboxMap
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_renderTextureTranslucentSurface, false));
mapboxMapOptions.setPrefetchesTiles(
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_enableTilePrefetch, true));
mapboxMapOptions.setPrefetchZoomDelta(
typedArray.getInt(R.styleable.mapbox_MapView_mapbox_prefetchZoomDelta, 4));
mapboxMapOptions.renderSurfaceOnTop(
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_enableZMediaOverlay, false));

Expand Down Expand Up @@ -633,13 +638,32 @@ public MapboxMapOptions foregroundLoadColor(@ColorInt int loadColor) {
*
* @param enable true to enable
* @return This
* @deprecated Use {@link #setPrefetchZoomDelta(int)} instead.
*/
@Deprecated
@NonNull
public MapboxMapOptions setPrefetchesTiles(boolean enable) {
this.prefetchesTiles = enable;
return this;
}

/**
* Set the tile pre-fetching zoom delta. Pre-fetching makes sure that a low-resolution
* tile at the (current_zoom_level - delta) is rendered as soon as possible at the
* expense of a little bandwidth.
* Note: This operation will override the MapboxMapOptions#setPrefetchesTiles(boolean)
* Setting zoom delta to 0 will disable pre-fetching.
* Default zoom delta is 4.
*
* @param delta zoom delta
* @return This
*/
@NonNull
public MapboxMapOptions setPrefetchZoomDelta(@IntRange(from = 0) int delta) {
this.prefetchZoomDelta = delta;
return this;
}

/**
* Enable cross-source symbol collision detection, defaults to true.
* <p>
Expand Down Expand Up @@ -721,11 +745,23 @@ public MapboxMapOptions pixelRatio(float pixelRatio) {
* Check whether tile pre-fetching is enabled.
*
* @return true if enabled
* @deprecated Use {@link #getPrefetchZoomDelta()} instead.
*/
@Deprecated
public boolean getPrefetchesTiles() {
return prefetchesTiles;
}

/**
* Check current pre-fetching zoom delta.
*
* @return current zoom delta.
*/
@IntRange(from = 0)
public int getPrefetchZoomDelta() {
return prefetchZoomDelta;
}

/**
* Check whether cross-source symbol collision detection is enabled.
*
Expand Down Expand Up @@ -1081,6 +1117,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeByte((byte) (textureMode ? 1 : 0));
dest.writeByte((byte) (translucentTextureSurface ? 1 : 0));
dest.writeByte((byte) (prefetchesTiles ? 1 : 0));
dest.writeInt(prefetchZoomDelta);
dest.writeByte((byte) (zMediaOverlay ? 1 : 0));
dest.writeByte((byte) (localIdeographFontFamilyEnabled ? 1 : 0));
dest.writeString(localIdeographFontFamily);
Expand Down Expand Up @@ -1175,6 +1212,9 @@ public boolean equals(@Nullable Object o) {
if (prefetchesTiles != options.prefetchesTiles) {
return false;
}
if (prefetchZoomDelta != options.prefetchZoomDelta) {
return false;
}
if (zMediaOverlay != options.zMediaOverlay) {
return false;
}
Expand Down Expand Up @@ -1231,6 +1271,7 @@ public int hashCode() {
result = 31 * result + (textureMode ? 1 : 0);
result = 31 * result + (translucentTextureSurface ? 1 : 0);
result = 31 * result + (prefetchesTiles ? 1 : 0);
result = 31 * result + prefetchZoomDelta;
result = 31 * result + (zMediaOverlay ? 1 : 0);
result = 31 * result + (localIdeographFontFamilyEnabled ? 1 : 0);
result = 31 * result + (localIdeographFontFamily != null ? localIdeographFontFamily.hashCode() : 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.mapbox.geojson.Feature;
import com.mapbox.geojson.Geometry;
import com.mapbox.mapboxsdk.annotations.Marker;
Expand Down Expand Up @@ -214,6 +215,11 @@ List<Feature> queryRenderedFeatures(@NonNull RectF coordinates,

boolean getPrefetchTiles();

void setPrefetchZoomDelta(@IntRange(from = 0) int delta);

@IntRange(from = 0)
int getPrefetchZoomDelta();

void setGestureInProgress(boolean inProgress);

float getPixelRatio();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,23 @@ public boolean getPrefetchTiles() {
return nativeGetPrefetchTiles();
}

@Override
public void setPrefetchZoomDelta(@IntRange(from = 0) int delta) {
if (checkState("nativeSetPrefetchZoomDelta")) {
return;
}
nativeSetPrefetchZoomDelta(delta);
}

@Override
@IntRange(from = 0)
public int getPrefetchZoomDelta() {
if (checkState("nativeGetPrefetchZoomDelta")) {
return 0;
}
return nativeGetPrefetchZoomDelta();
}

// Runtime style Api

@Override
Expand Down Expand Up @@ -1383,6 +1400,12 @@ private native Feature[] nativeQueryRenderedFeaturesForBox(float left, float top
@Keep
private native boolean nativeGetPrefetchTiles();

@Keep
private native void nativeSetPrefetchZoomDelta(int delta);

@Keep
private native int nativeGetPrefetchZoomDelta();

@Override
public long getNativePtr() {
return nativePtr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<public name="mapbox_foregroundLoadColor" type="attr" />

<public name="mapbox_enableTilePrefetch" type="attr" />
<public name="mapbox_prefetchZoomDelta" type="attr"/>
<public name="mapbox_enableZMediaOverlay" type="attr" />

<!-- Exposed content descriptions -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<attr name="mapbox_foregroundLoadColor" format="color"/>

<attr name="mapbox_enableTilePrefetch" format="boolean"/>
<attr name="mapbox_prefetchZoomDelta" format="integer"/>
<attr name="mapbox_enableZMediaOverlay" format="boolean"/>
<attr name="mapbox_pixelRatio" format="float"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ public void testPrefetchesTiles() {
assertFalse(new MapboxMapOptions().setPrefetchesTiles(false).getPrefetchesTiles());
}

@Test
public void testPrefetchZoomDelta() {
// Default value
assertEquals(4, new MapboxMapOptions().getPrefetchZoomDelta());

// Check mutations
assertEquals(5, new MapboxMapOptions().setPrefetchZoomDelta(5).getPrefetchZoomDelta());
}


@Test
public void testCrossSourceCollisions() {
// Default value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ class MapboxMapTest {
verify { nativeMapView.prefetchTiles = true }
}

@Test
fun testGetPrefetchZoomDelta() {
every { nativeMapView.prefetchZoomDelta } answers { 3 }
assertEquals(3, mapboxMap.prefetchZoomDelta)
}

@Test
fun testSetPrefetchZoomDelta() {
mapboxMap.prefetchZoomDelta = 2
verify { nativeMapView.prefetchZoomDelta = 2 }
}

@Test
fun testCameraForLatLngBounds() {
val bounds = LatLngBounds.Builder().include(LatLng()).include(LatLng(1.0, 1.0)).build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,23 @@ class NativeMapViewTest : AppCenter() {
assertEquals("Flag should match", expected, actual)
}

@Test
@UiThreadTest
fun testPrefetchZoomDelta() {
val expected = 2
nativeMapView.prefetchZoomDelta = 2
val actual = nativeMapView.prefetchZoomDelta
assertEquals("Prefetch zoom delta should match", expected, actual)
}

@Test
@UiThreadTest
fun testPrefetchZoomDeltaDefault() {
val expected = 4
val actual = nativeMapView.prefetchZoomDelta
assertEquals("Prefetch zoom delta should match", expected, actual)
}

@Test
@UiThreadTest
fun testSetContentPadding() {
Expand Down
Loading

0 comments on commit 7139abd

Please sign in to comment.