Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Android NativeMapView and move creation to the ModuleProvider #3040

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package org.maplibre.android;

import android.content.Context;
import androidx.annotation.NonNull;

import org.maplibre.android.http.HttpRequest;
import org.maplibre.android.maps.NativeMap;
import org.maplibre.android.maps.NativeMapView.ViewCallback;
import org.maplibre.android.maps.NativeMapView.StateCallback;
import org.maplibre.android.maps.renderer.MapRenderer;

/**
* Injects concrete instances of configurable abstractions
Expand All @@ -25,4 +30,12 @@ public interface ModuleProvider {
@NonNull
LibraryLoaderProvider createLibraryLoaderProvider();

/**
* Create and return a new NativeMap view
*
* @return a new instance implementing NativeMap
*/
@NonNull
NativeMap createNativeMapView(Context context, float pixelRatio, boolean crossSourceCollisions,
ViewCallback viewCallback, StateCallback stateCallback, MapRenderer mapRenderer);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package org.maplibre.android;

import android.content.Context;
import androidx.annotation.NonNull;

import org.maplibre.android.http.HttpRequest;
import org.maplibre.android.module.http.HttpRequestImpl;
import org.maplibre.android.module.loader.LibraryLoaderProviderImpl;
import org.maplibre.android.maps.NativeMap;
import org.maplibre.android.maps.NativeMapView;
import org.maplibre.android.maps.NativeMapView.ViewCallback;
import org.maplibre.android.maps.NativeMapView.StateCallback;
import org.maplibre.android.maps.renderer.MapRenderer;

public class ModuleProviderImpl implements ModuleProvider {

Expand All @@ -19,4 +25,12 @@ public HttpRequest createHttpRequest() {
public LibraryLoaderProvider createLibraryLoaderProvider() {
return new LibraryLoaderProviderImpl();
}

@NonNull
@Override
public NativeMap createNativeMapView(Context context, float pixelRatio, boolean crossSourceCollisions,
ViewCallback viewCallback, StateCallback stateCallback,
MapRenderer mapRenderer) {
return new NativeMapView(context, pixelRatio, crossSourceCollisions, viewCallback, stateCallback, mapRenderer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,9 @@ private void initializeDrawingSurface(MapLibreMapOptions options) {

addView(renderView, 0);

boolean crossSourceCollisions = options.getCrossSourceCollisions();
nativeMapView = new NativeMapView(
getContext(), getPixelRatio(), crossSourceCollisions, this, mapChangeReceiver, mapRenderer
);
boolean crossSourceCollisions = maplibreMapOptions.getCrossSourceCollisions();
nativeMapView = MapLibre.getModuleProvider().createNativeMapView(getContext(),
getPixelRatio(), crossSourceCollisions, this, mapChangeReceiver, mapRenderer);
}

private void onSurfaceCreated() {
Expand Down Expand Up @@ -520,6 +519,15 @@ public View getRenderView() {
return renderView;
}

/**
* Returns the underlying native map instance.
* The type of NativeMap returned is determined by the ModuleProvider's
* `createNativeMapView` method.
*/
public NativeMap getNativeMap() {
return nativeMapView;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isGestureDetectorInitialized()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import java.util.List;

interface NativeMap {
public interface NativeMap {

//
// Lifecycle API
Expand Down Expand Up @@ -293,4 +293,4 @@ List<Feature> queryRenderedFeatures(@NonNull RectF coordinates,
long getNativePtr();

void addSnapshotCallback(@NonNull MapLibreMap.SnapshotReadyCallback callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import java.util.List;

// Class that wraps the native methods for convenience
final class NativeMapView implements NativeMap {
public final class NativeMapView implements NativeMap {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we drop the final so it's easier to create a custom NativeMap with inheritance?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me do some testing, I'm currently uncertain if the JNI bindings will work as expected with a derived class.


private static final String TAG = "Mbgl-NativeMapView";

Expand Down Expand Up @@ -1668,7 +1668,7 @@ interface StyleCallback {
void onDidFinishLoadingStyle();
}

interface StateCallback extends StyleCallback {
public interface StateCallback extends StyleCallback {
void onCameraWillChange(boolean animated);

void onCameraIsChanging();
Expand Down
Loading