Skip to content

[google_maps_flutter] Moves Java->Dart calls to Pigeon #7040

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

Merged
Merged
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,3 +1,7 @@
## 2.12.0

* Converts Java->Dart calls to Pigeon.

## 2.11.1

* Fixes handling of Circle updates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.googlemaps.Messages.MapsCallbackApi;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class CirclesController {
@VisibleForTesting final Map<String, CircleController> circleIdToController;
private final Map<String, String> googleMapsCircleIdToDartCircleId;
private final MethodChannel methodChannel;
private final @NonNull MapsCallbackApi flutterApi;
private final float density;
private GoogleMap googleMap;

CirclesController(MethodChannel methodChannel, float density) {
CirclesController(@NonNull MapsCallbackApi flutterApi, float density) {
this.circleIdToController = new HashMap<>();
this.googleMapsCircleIdToDartCircleId = new HashMap<>();
this.methodChannel = methodChannel;
this.flutterApi = flutterApi;
this.density = density;
}

Expand Down Expand Up @@ -69,7 +69,7 @@ boolean onCircleTap(String googleCircleId) {
if (circleId == null) {
return false;
}
methodChannel.invokeMethod("circle#onTap", Convert.circleIdToJson(circleId));
flutterApi.onCircleTap(circleId, new NoOpVoidResult());
CircleController circleController = circleIdToController.get(circleId);
if (circleController != null) {
return circleController.consumeTapEvents();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.google.maps.android.clustering.ClusterManager;
import com.google.maps.android.clustering.view.DefaultClusterRenderer;
import com.google.maps.android.collections.MarkerManager;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.googlemaps.Messages.MapsCallbackApi;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -30,7 +30,7 @@ class ClusterManagersController
ClusterManager.OnClusterClickListener<MarkerBuilder> {
@NonNull private final Context context;
@NonNull private final HashMap<String, ClusterManager<MarkerBuilder>> clusterManagerIdToManager;
@NonNull private final MethodChannel methodChannel;
@NonNull private final MapsCallbackApi flutterApi;
@Nullable private MarkerManager markerManager;
@Nullable private GoogleMap googleMap;

Expand All @@ -41,10 +41,10 @@ class ClusterManagersController
private ClusterManagersController.OnClusterItemRendered<MarkerBuilder>
clusterItemRenderedListener;

ClusterManagersController(MethodChannel methodChannel, Context context) {
ClusterManagersController(@NonNull MapsCallbackApi flutterApi, Context context) {
this.clusterManagerIdToManager = new HashMap<>();
this.context = context;
this.methodChannel = methodChannel;
this.flutterApi = flutterApi;
}

void init(GoogleMap googleMap, MarkerManager markerManager) {
Expand Down Expand Up @@ -197,7 +197,8 @@ public boolean onClusterClick(Cluster<MarkerBuilder> cluster) {
if (cluster.getSize() > 0) {
MarkerBuilder[] builders = cluster.getItems().toArray(new MarkerBuilder[0]);
String clusterManagerId = builders[0].clusterManagerId();
methodChannel.invokeMethod("cluster#onTap", Convert.clusterToJson(clusterManagerId, cluster));
flutterApi.onClusterTap(
Convert.clusterToPigeon(clusterManagerId, cluster), new NoOpVoidResult());
}

// Return false to allow the default behavior of the cluster click event to occur.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,14 @@ private static int toInt(Object o) {
return null;
}

static Object cameraPositionToJson(CameraPosition position) {
if (position == null) {
return null;
}
final Map<String, Object> data = new HashMap<>();
data.put("bearing", position.bearing);
data.put("target", latLngToJson(position.target));
data.put("tilt", position.tilt);
data.put("zoom", position.zoom);
return data;
static @NonNull Messages.PlatformCameraPosition cameraPositionToPigeon(
@NonNull CameraPosition position) {
return new Messages.PlatformCameraPosition.Builder()
.setBearing((double) position.bearing)
.setTarget(latLngToPigeon(position.target))
.setTilt((double) position.tilt)
.setZoom((double) position.zoom)
.build();
}

static Object latLngBoundsToJson(LatLngBounds latLngBounds) {
Expand Down Expand Up @@ -426,29 +424,6 @@ static Object polylineIdToJson(String polylineId) {
return data;
}

static Object circleIdToJson(String circleId) {
if (circleId == null) {
return null;
}
final Map<String, Object> data = new HashMap<>(1);
data.put("circleId", circleId);
return data;
}

static Map<String, Object> tileOverlayArgumentsToJson(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can change this to stay here and do a Pigeon build if you prefer, but since we have to pass all the arguments individually anyway (vs passing a single object to convert) it seemed easier to just inline it.

String tileOverlayId, int x, int y, int zoom) {

if (tileOverlayId == null) {
return null;
}
final Map<String, Object> data = new HashMap<>(4);
data.put("tileOverlayId", tileOverlayId);
data.put("x", x);
data.put("y", y);
data.put("zoom", zoom);
return data;
}

static Object latLngToJson(LatLng latLng) {
return Arrays.asList(latLng.latitude, latLng.longitude);
}
Expand All @@ -464,36 +439,6 @@ static LatLng latLngFromPigeon(Messages.PlatformLatLng latLng) {
return new LatLng(latLng.getLatitude(), latLng.getLongitude());
}

static Object clusterToJson(String clusterManagerId, Cluster<MarkerBuilder> cluster) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is pure removal because the Pigeon version (just below) was added in a previous PR.

int clusterSize = cluster.getSize();
LatLngBounds.Builder latLngBoundsBuilder = LatLngBounds.builder();

String[] markerIds = new String[clusterSize];
MarkerBuilder[] markerBuilders = cluster.getItems().toArray(new MarkerBuilder[clusterSize]);

// Loops though cluster items and reads markers position for the LatLngBounds
// builder
// and also builds list of marker ids on the cluster.
for (int i = 0; i < clusterSize; i++) {
MarkerBuilder markerBuilder = markerBuilders[i];
latLngBoundsBuilder.include(markerBuilder.getPosition());
markerIds[i] = markerBuilder.markerId();
}

Object position = latLngToJson(cluster.getPosition());
Object bounds = latLngBoundsToJson(latLngBoundsBuilder.build());

final Map<String, Object> data = new HashMap<>(4);

// For dart side implementation see parseCluster method at
// packages/google_maps_flutter/google_maps_flutter_android/lib/src/google_maps_flutter_android.dart
data.put("clusterManagerId", clusterManagerId);
data.put("position", position);
data.put("bounds", bounds);
data.put("markerIds", Arrays.asList(markerIds));
return data;
}

static Messages.PlatformCluster clusterToPigeon(
String clusterManagerId, Cluster<MarkerBuilder> cluster) {
int clusterSize = cluster.getSize();
Expand Down Expand Up @@ -994,14 +939,8 @@ static String interpretTileOverlayOptions(Map<String, ?> data, TileOverlaySink s
}
}

static Tile interpretTile(Map<String, ?> data) {
int width = toInt(data.get("width"));
int height = toInt(data.get("height"));
byte[] dataArray = null;
if (data.get("data") != null) {
dataArray = (byte[]) data.get("data");
}
return new Tile(width, height, dataArray);
static Tile tileFromPigeon(Messages.PlatformTile tile) {
return new Tile(tile.getWidth().intValue(), tile.getHeight().intValue(), tile.getData());
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
Expand All @@ -44,15 +43,13 @@
import com.google.maps.android.collections.MarkerManager;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugins.googlemaps.Messages.FlutterError;
import io.flutter.plugins.googlemaps.Messages.MapsApi;
import io.flutter.plugins.googlemaps.Messages.MapsCallbackApi;
import io.flutter.plugins.googlemaps.Messages.MapsInspectorApi;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -73,7 +70,7 @@ class GoogleMapController

private static final String TAG = "GoogleMapController";
private final int id;
private final MethodChannel methodChannel;
private final MapsCallbackApi flutterApi;
private final BinaryMessenger binaryMessenger;
private final GoogleMapOptions options;
@Nullable private MapView mapView;
Expand Down Expand Up @@ -121,19 +118,18 @@ class GoogleMapController
this.mapView = new MapView(context, options);
this.density = context.getResources().getDisplayMetrics().density;
this.binaryMessenger = binaryMessenger;
methodChannel =
new MethodChannel(binaryMessenger, "plugins.flutter.dev/google_maps_android_" + id);
flutterApi = new MapsCallbackApi(binaryMessenger, Integer.toString(id));
MapsApi.setUp(binaryMessenger, Integer.toString(id), this);
MapsInspectorApi.setUp(binaryMessenger, Integer.toString(id), this);
AssetManager assetManager = context.getAssets();
this.lifecycleProvider = lifecycleProvider;
this.clusterManagersController = new ClusterManagersController(methodChannel, context);
this.clusterManagersController = new ClusterManagersController(flutterApi, context);
this.markersController =
new MarkersController(methodChannel, clusterManagersController, assetManager, density);
this.polygonsController = new PolygonsController(methodChannel, density);
this.polylinesController = new PolylinesController(methodChannel, assetManager, density);
this.circlesController = new CirclesController(methodChannel, density);
this.tileOverlaysController = new TileOverlaysController(methodChannel);
new MarkersController(flutterApi, clusterManagersController, assetManager, density);
this.polygonsController = new PolygonsController(flutterApi, density);
this.polylinesController = new PolylinesController(flutterApi, assetManager, density);
this.circlesController = new CirclesController(flutterApi, density);
this.tileOverlaysController = new TileOverlaysController(flutterApi);
}

// Constructor for testing purposes only
Expand All @@ -142,7 +138,7 @@ class GoogleMapController
int id,
Context context,
BinaryMessenger binaryMessenger,
MethodChannel methodChannel,
MapsCallbackApi flutterApi,
LifecycleProvider lifecycleProvider,
GoogleMapOptions options,
ClusterManagersController clusterManagersController,
Expand All @@ -154,7 +150,7 @@ class GoogleMapController
this.id = id;
this.context = context;
this.binaryMessenger = binaryMessenger;
this.methodChannel = methodChannel;
this.flutterApi = flutterApi;
this.options = options;
this.mapView = new MapView(context, options);
this.density = context.getResources().getDisplayMetrics().density;
Expand Down Expand Up @@ -182,10 +178,6 @@ void init() {
mapView.getMapAsync(this);
}

private CameraPosition getCameraPosition() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opportunistic cleanup; the IDE flagged that this was unused.

return trackCameraPosition ? googleMap.getCameraPosition() : null;
}

@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
this.googleMap = googleMap;
Expand Down Expand Up @@ -298,24 +290,17 @@ public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {

@Override
public void onMapClick(@NonNull LatLng latLng) {
final Map<String, Object> arguments = new HashMap<>(2);
arguments.put("position", Convert.latLngToJson(latLng));
methodChannel.invokeMethod("map#onTap", arguments);
flutterApi.onTap(Convert.latLngToPigeon(latLng), new NoOpVoidResult());
}

@Override
public void onMapLongClick(@NonNull LatLng latLng) {
final Map<String, Object> arguments = new HashMap<>(2);
arguments.put("position", Convert.latLngToJson(latLng));
methodChannel.invokeMethod("map#onLongPress", arguments);
flutterApi.onLongPress(Convert.latLngToPigeon(latLng), new NoOpVoidResult());
}

@Override
public void onCameraMoveStarted(int reason) {
final Map<String, Object> arguments = new HashMap<>(2);
boolean isGesture = reason == GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE;
arguments.put("isGesture", isGesture);
methodChannel.invokeMethod("camera#onMoveStarted", arguments);
flutterApi.onCameraMoveStarted(new NoOpVoidResult());
}

@Override
Expand All @@ -328,15 +313,14 @@ public void onCameraMove() {
if (!trackCameraPosition) {
return;
}
final Map<String, Object> arguments = new HashMap<>(2);
arguments.put("position", Convert.cameraPositionToJson(googleMap.getCameraPosition()));
methodChannel.invokeMethod("camera#onMove", arguments);
flutterApi.onCameraMove(
Convert.cameraPositionToPigeon(googleMap.getCameraPosition()), new NoOpVoidResult());
}

@Override
public void onCameraIdle() {
clusterManagersController.onCameraIdle();
methodChannel.invokeMethod("camera#onIdle", Collections.singletonMap("map", id));
flutterApi.onCameraIdle(new NoOpVoidResult());
}

@Override
Expand Down
Loading