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

Commit

Permalink
[android] - create device independent tests for camera position testing
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Jul 5, 2018
1 parent 2f9dbd1 commit c0d40aa
Show file tree
Hide file tree
Showing 17 changed files with 209 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ private void initialiseMap() {
nativeMapView.addOnMapChangedListener(mapCallback);

// callback for focal point invalidation
final FocalPointInvalidator focalPointInvalidator = new FocalPointInvalidator();
focalPointInvalidator.addListener(createFocalPointChangeListener());
final FocalPointInvalidator focalInvalidator = new FocalPointInvalidator();
focalInvalidator.addListener(createFocalPointChangeListener());

// callback for registering touch listeners
GesturesManagerInteractionListener registerTouchListener = new GesturesManagerInteractionListener();
Expand All @@ -157,7 +157,7 @@ private void initialiseMap() {

// setup components for MapboxMap creation
Projection proj = new Projection(nativeMapView);
UiSettings uiSettings = new UiSettings(proj, focalPointInvalidator, compassView, attrView, logoView);
UiSettings uiSettings = new UiSettings(proj, focalInvalidator, compassView, attrView, logoView, getPixelRatio());
LongSparseArray<Annotation> annotationsArray = new LongSparseArray<>();
MarkerViewManager markerViewManager = new MarkerViewManager((ViewGroup) findViewById(R.id.markerViewContainer));
IconManager iconManager = new IconManager(nativeMapView);
Expand Down Expand Up @@ -310,15 +310,12 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
addView(glSurfaceView, 0);
}

nativeMapView = new NativeMapView(getContext(), this, mapRenderer);
nativeMapView.addOnMapChangedListener(new OnMapChangedListener() {
@Override
public void onMapChanged(int change) {
// dispatch events to external listeners
if (!onMapChangedListeners.isEmpty()) {
for (OnMapChangedListener onMapChangedListener : onMapChangedListeners) {
onMapChangedListener.onMapChanged(change);
}
nativeMapView = new NativeMapView(getContext(), getPixelRatio(), this, mapRenderer);
nativeMapView.addOnMapChangedListener(change -> {
// dispatch events to external listeners
if (!onMapChangedListeners.isEmpty()) {
for (OnMapChangedListener onMapChangedListener : onMapChangedListeners) {
onMapChangedListener.onMapChanged(change);
}
}
});
Expand Down Expand Up @@ -583,6 +580,16 @@ protected void onSizeChanged(int width, int height, int oldw, int oldh) {
}
}

private float getPixelRatio() {
// check is user defined his own pixel ratio value
float pixelRatio = mapboxMapOptions.getPixelRatio();
if (pixelRatio == 0) {
// if not, get the one defined by the system
pixelRatio = getResources().getDisplayMetrics().density;
}
return pixelRatio;
}

//
// View events
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,7 @@ public void setLatLngBoundsForCameraTarget(@Nullable LatLngBounds latLngBounds)


/**
* Get a camera position that fits a provided bounds and the current camera tilt.
* Get a camera position that fits a provided bounds and the current camera tilt and bearing.
*
* @param latLngBounds the bounds to set the map with
* @return the camera position that fits the bounds
Expand All @@ -1581,7 +1581,7 @@ public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBound


/**
* Get a camera position that fits a provided bounds and padding and the current camera tilt.
* Get a camera position that fits a provided bounds and padding and the current camera tilt and bearing.
*
* @param latLngBounds the bounds to set the map with
* @param padding the padding to apply to the bounds
Expand All @@ -1596,12 +1596,12 @@ public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBound


/**
* Get a camera position that fits a provided bounds, padding and tilt.
* Get a camera position that fits a provided bounds, bearing and tilt.
*
* @param latLngBounds the bounds to set the map with
* @param bearing the bearing to transform the camera position with
* @param tilt to transform the camera position with
* @return the camera position that fits the bounds and padding
* @return the camera position that fits the bounds and given bearing and tilt
*/
@NonNull
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBounds,
Expand All @@ -1614,13 +1614,13 @@ public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBound


/**
* Get a camera position that fits a provided bounds, padding and tilt.
* Get a camera position that fits a provided bounds, padding, bearing and tilt.
*
* @param latLngBounds the bounds to set the map with
* @param padding the padding to apply to the bounds
* @param bearing the bearing to transform the camera position with
* @param tilt to transform the camera position with
* @return the camera position that fits the bounds and padding
* @return the camera position that fits the bounds, bearing and tilt
*/
@NonNull
public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBounds,
Expand All @@ -1633,10 +1633,10 @@ public CameraPosition getCameraForLatLngBounds(@NonNull LatLngBounds latLngBound
}

/**
* Get a camera position that fits a provided shape with a given bearing and padding.
* Get a camera position that fits a provided shape.
*
* @param geometry the geometry to constrain the map with
* @return the camera position that fits the bounds and padding
* @param geometry the geometry to wraps the map with
* @return the camera position that fits the geometry inside
*/
@NonNull
public CameraPosition getCameraForGeometry(@NonNull Geometry geometry) {
Expand All @@ -1645,11 +1645,11 @@ public CameraPosition getCameraForGeometry(@NonNull Geometry geometry) {
}

/**
* Get a camera position that fits a provided shape with a given bearing and padding.
* Get a camera position that fits a provided shape and padding.
*
* @param geometry the geometry to constrain the map with
* @param geometry the geometry to wraps the map with
* @param padding the padding to apply to the bounds
* @return the camera position that fits the bounds and padding
* @return the camera position that fits the geometry inside and padding
*/
@NonNull
public CameraPosition getCameraForGeometry(@NonNull Geometry geometry,
Expand All @@ -1659,12 +1659,12 @@ public CameraPosition getCameraForGeometry(@NonNull Geometry geometry,
}

/**
* Get a camera position that fits a provided shape with a given bearing and padding.
* Get a camera position that fits a provided shape with a given bearing and tilt.
*
* @param geometry the geometry to constrain the map with
* @param geometry the geometry to wraps the map with
* @param bearing the bearing at which to compute the geometry's bounds
* @param tilt the tilt at which to compute the geometry's bounds
* @return the camera position that fits the bounds and padding
* @return the camera position that the geometry inside with bearing and tilt
*/
@NonNull
public CameraPosition getCameraForGeometry(@NonNull Geometry geometry,
Expand All @@ -1676,13 +1676,13 @@ public CameraPosition getCameraForGeometry(@NonNull Geometry geometry,
}

/**
* Get a camera position that fits a provided shape with a given bearing and padding.
* Get a camera position that fits a provided shape with a given padding, bearing and tilt.
*
* @param geometry the geometry to constrain the map with
* @param geometry the geometry to wraps the map with
* @param padding the padding to apply to the bounds
* @param bearing the bearing at which to compute the geometry's bounds
* @param tilt the tilt at which to compute the geometry's bounds
* @return the camera position that fits the bounds and padding
* @return the camera position that fits the geometry inside with padding, bearing and tilt
*/
@NonNull
public CameraPosition getCameraForGeometry(@NonNull Geometry geometry,
Expand All @@ -1697,10 +1697,10 @@ public CameraPosition getCameraForGeometry(@NonNull Geometry geometry,
/**
* Get a camera position that fits a provided shape with a given bearing and padding.
*
* @param geometry the geometry to constrain the map with
* @param geometry the geometry to wraps the map with
* @param bearing the bearing at which to compute the geometry's bounds
* @param padding the padding to apply to the bounds
* @return the camera position that fits the bounds and padding
* @return the camera position that fits the geometry inside with padding and bearing
* @deprecated use Mapbox{@link #getCameraForGeometry(Geometry, int[], double, double)} instead
*/
@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import android.support.v4.content.res.ResourcesCompat;
import android.util.AttributeSet;
import android.view.Gravity;

import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
Expand Down Expand Up @@ -76,6 +75,8 @@ public class MapboxMapOptions implements Parcelable {

private String style;

private float pixelRatio;

/**
* Creates a new MapboxMapOptions object.
*/
Expand Down Expand Up @@ -122,6 +123,7 @@ private MapboxMapOptions(Parcel in) {
prefetchesTiles = in.readByte() != 0;
zMediaOverlay = in.readByte() != 0;
localIdeographFontFamily = in.readString();
pixelRatio = in.readFloat();
}

/**
Expand Down Expand Up @@ -218,6 +220,8 @@ public static MapboxMapOptions createFromAttributes(@NonNull Context context, @N
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_enableZMediaOverlay, false));
mapboxMapOptions.localIdeographFontFamily(
typedArray.getString(R.styleable.mapbox_MapView_mapbox_localIdeographFontFamily));
mapboxMapOptions.pixelRatio(
typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_pixelRatio, 0));
} finally {
typedArray.recycle();
}
Expand Down Expand Up @@ -545,6 +549,18 @@ public MapboxMapOptions localIdeographFontFamily(String fontFamily) {
return this;
}

/**
* Set the custom pixel ratio configuration to override the default value from resources.
* This ratio will be used to initialise the map with.
*
* @param pixelRatio the custom pixel ratio of the map under construction
* @return This
*/
public MapboxMapOptions pixelRatio(float pixelRatio) {
this.pixelRatio = pixelRatio;
return this;
}

/**
* Check whether tile pre-fetching is enabled.
*
Expand Down Expand Up @@ -813,6 +829,15 @@ public String getLocalIdeographFontFamily() {
return localIdeographFontFamily;
}

/**
* Return the custom configured pixel ratio, returns 0 if not configured.
*
* @return the pixel ratio used by the map under construction
*/
public float getPixelRatio() {
return pixelRatio;
}

public static final Parcelable.Creator<MapboxMapOptions> CREATOR = new Parcelable.Creator<MapboxMapOptions>() {
public MapboxMapOptions createFromParcel(Parcel in) {
return new MapboxMapOptions(in);
Expand Down Expand Up @@ -866,6 +891,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (prefetchesTiles ? 1 : 0));
dest.writeByte((byte) (zMediaOverlay ? 1 : 0));
dest.writeString(localIdeographFontFamily);
dest.writeFloat(pixelRatio);
}

@Override
Expand Down Expand Up @@ -959,7 +985,10 @@ public boolean equals(Object o) {
if (zMediaOverlay != options.zMediaOverlay) {
return false;
}
if (localIdeographFontFamily != options.localIdeographFontFamily) {
if (!localIdeographFontFamily.equals(options.localIdeographFontFamily)) {
return false;
}
if (pixelRatio != options.pixelRatio) {
return false;
}

Expand Down Expand Up @@ -1001,6 +1030,7 @@ public int hashCode() {
result = 31 * result + (prefetchesTiles ? 1 : 0);
result = 31 * result + (zMediaOverlay ? 1 : 0);
result = 31 * result + (localIdeographFontFamily != null ? localIdeographFontFamily.hashCode() : 0);
result = 31 * result + (int) pixelRatio;
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class UiSettings {
private final View logoView;
private final int[] logoMargins = new int[4];

private float pixelRatio;
private final float pixelRatio;

private boolean rotateGesturesEnabled = true;

Expand All @@ -68,15 +68,14 @@ public final class UiSettings {
private PointF userProvidedFocalPoint;

UiSettings(@NonNull Projection projection, @NonNull FocalPointChangeListener listener,
@NonNull CompassView compassView, @NonNull ImageView attributionsView, @NonNull View logoView) {
@NonNull CompassView compassView, @NonNull ImageView attributionsView, @NonNull View logoView,
float pixelRatio) {
this.projection = projection;
this.focalPointChangeListener = listener;
this.compassView = compassView;
this.attributionsView = attributionsView;
this.logoView = logoView;
if (logoView.getResources() != null) {
this.pixelRatio = logoView.getResources().getDisplayMetrics().density;
}
this.pixelRatio = pixelRatio;
}

void initialise(@NonNull Context context, @NonNull MapboxMapOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<public name="mapbox_styleUrl" type="attr" />
<public name="mapbox_apiBaseUrl" type="attr" />
<public name="mapbox_localIdeographFontFamily" type="attr" />
<public name="mapbox_pixelRatio" type="float" />

<!--Camera-->
<public name="mapbox_cameraTargetLng" type="attr" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@

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

</declare-styleable>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class UiSettingsTest {

@Before
public void beforeTest() {
uiSettings = new UiSettings(projection, focalPointChangeListener, compassView, imageView, logoView);
uiSettings = new UiSettings(projection, focalPointChangeListener, compassView, imageView, logoView, 1);
}

@Test
Expand Down
Loading

0 comments on commit c0d40aa

Please sign in to comment.