Skip to content

Commit 0b57eca

Browse files
[google_maps_flutter] Moves Java->Dart calls to Pigeon (flutter#7040)
Converts all of the Java->Dart calls from raw method channels to Pigeon. This finishes conversion of all of the invocations in `google_maps_flutter_android` Part of flutter#117907
1 parent 97bad7e commit 0b57eca

21 files changed

+1894
-518
lines changed

packages/google_maps_flutter/google_maps_flutter_android/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.12.0
2+
3+
* Converts Java->Dart calls to Pigeon.
4+
15
## 2.11.1
26

37
* Fixes handling of Circle updates.

packages/google_maps_flutter/google_maps_flutter_android/android/src/main/java/io/flutter/plugins/googlemaps/CirclesController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
import com.google.android.gms.maps.GoogleMap;
1010
import com.google.android.gms.maps.model.Circle;
1111
import com.google.android.gms.maps.model.CircleOptions;
12-
import io.flutter.plugin.common.MethodChannel;
12+
import io.flutter.plugins.googlemaps.Messages.MapsCallbackApi;
1313
import java.util.HashMap;
1414
import java.util.List;
1515
import java.util.Map;
1616

1717
class CirclesController {
1818
@VisibleForTesting final Map<String, CircleController> circleIdToController;
1919
private final Map<String, String> googleMapsCircleIdToDartCircleId;
20-
private final MethodChannel methodChannel;
20+
private final @NonNull MapsCallbackApi flutterApi;
2121
private final float density;
2222
private GoogleMap googleMap;
2323

24-
CirclesController(MethodChannel methodChannel, float density) {
24+
CirclesController(@NonNull MapsCallbackApi flutterApi, float density) {
2525
this.circleIdToController = new HashMap<>();
2626
this.googleMapsCircleIdToDartCircleId = new HashMap<>();
27-
this.methodChannel = methodChannel;
27+
this.flutterApi = flutterApi;
2828
this.density = density;
2929
}
3030

@@ -69,7 +69,7 @@ boolean onCircleTap(String googleCircleId) {
6969
if (circleId == null) {
7070
return false;
7171
}
72-
methodChannel.invokeMethod("circle#onTap", Convert.circleIdToJson(circleId));
72+
flutterApi.onCircleTap(circleId, new NoOpVoidResult());
7373
CircleController circleController = circleIdToController.get(circleId);
7474
if (circleController != null) {
7575
return circleController.consumeTapEvents();

packages/google_maps_flutter/google_maps_flutter_android/android/src/main/java/io/flutter/plugins/googlemaps/ClusterManagersController.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import com.google.maps.android.clustering.ClusterManager;
1616
import com.google.maps.android.clustering.view.DefaultClusterRenderer;
1717
import com.google.maps.android.collections.MarkerManager;
18-
import io.flutter.plugin.common.MethodChannel;
18+
import io.flutter.plugins.googlemaps.Messages.MapsCallbackApi;
1919
import java.util.HashMap;
2020
import java.util.List;
2121
import java.util.Map;
@@ -30,7 +30,7 @@ class ClusterManagersController
3030
ClusterManager.OnClusterClickListener<MarkerBuilder> {
3131
@NonNull private final Context context;
3232
@NonNull private final HashMap<String, ClusterManager<MarkerBuilder>> clusterManagerIdToManager;
33-
@NonNull private final MethodChannel methodChannel;
33+
@NonNull private final MapsCallbackApi flutterApi;
3434
@Nullable private MarkerManager markerManager;
3535
@Nullable private GoogleMap googleMap;
3636

@@ -41,10 +41,10 @@ class ClusterManagersController
4141
private ClusterManagersController.OnClusterItemRendered<MarkerBuilder>
4242
clusterItemRenderedListener;
4343

44-
ClusterManagersController(MethodChannel methodChannel, Context context) {
44+
ClusterManagersController(@NonNull MapsCallbackApi flutterApi, Context context) {
4545
this.clusterManagerIdToManager = new HashMap<>();
4646
this.context = context;
47-
this.methodChannel = methodChannel;
47+
this.flutterApi = flutterApi;
4848
}
4949

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

203204
// Return false to allow the default behavior of the cluster click event to occur.

packages/google_maps_flutter/google_maps_flutter_android/android/src/main/java/io/flutter/plugins/googlemaps/Convert.java

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -373,16 +373,14 @@ private static int toInt(Object o) {
373373
return null;
374374
}
375375

376-
static Object cameraPositionToJson(CameraPosition position) {
377-
if (position == null) {
378-
return null;
379-
}
380-
final Map<String, Object> data = new HashMap<>();
381-
data.put("bearing", position.bearing);
382-
data.put("target", latLngToJson(position.target));
383-
data.put("tilt", position.tilt);
384-
data.put("zoom", position.zoom);
385-
return data;
376+
static @NonNull Messages.PlatformCameraPosition cameraPositionToPigeon(
377+
@NonNull CameraPosition position) {
378+
return new Messages.PlatformCameraPosition.Builder()
379+
.setBearing((double) position.bearing)
380+
.setTarget(latLngToPigeon(position.target))
381+
.setTilt((double) position.tilt)
382+
.setZoom((double) position.zoom)
383+
.build();
386384
}
387385

388386
static Object latLngBoundsToJson(LatLngBounds latLngBounds) {
@@ -426,29 +424,6 @@ static Object polylineIdToJson(String polylineId) {
426424
return data;
427425
}
428426

429-
static Object circleIdToJson(String circleId) {
430-
if (circleId == null) {
431-
return null;
432-
}
433-
final Map<String, Object> data = new HashMap<>(1);
434-
data.put("circleId", circleId);
435-
return data;
436-
}
437-
438-
static Map<String, Object> tileOverlayArgumentsToJson(
439-
String tileOverlayId, int x, int y, int zoom) {
440-
441-
if (tileOverlayId == null) {
442-
return null;
443-
}
444-
final Map<String, Object> data = new HashMap<>(4);
445-
data.put("tileOverlayId", tileOverlayId);
446-
data.put("x", x);
447-
data.put("y", y);
448-
data.put("zoom", zoom);
449-
return data;
450-
}
451-
452427
static Object latLngToJson(LatLng latLng) {
453428
return Arrays.asList(latLng.latitude, latLng.longitude);
454429
}
@@ -464,36 +439,6 @@ static LatLng latLngFromPigeon(Messages.PlatformLatLng latLng) {
464439
return new LatLng(latLng.getLatitude(), latLng.getLongitude());
465440
}
466441

467-
static Object clusterToJson(String clusterManagerId, Cluster<MarkerBuilder> cluster) {
468-
int clusterSize = cluster.getSize();
469-
LatLngBounds.Builder latLngBoundsBuilder = LatLngBounds.builder();
470-
471-
String[] markerIds = new String[clusterSize];
472-
MarkerBuilder[] markerBuilders = cluster.getItems().toArray(new MarkerBuilder[clusterSize]);
473-
474-
// Loops though cluster items and reads markers position for the LatLngBounds
475-
// builder
476-
// and also builds list of marker ids on the cluster.
477-
for (int i = 0; i < clusterSize; i++) {
478-
MarkerBuilder markerBuilder = markerBuilders[i];
479-
latLngBoundsBuilder.include(markerBuilder.getPosition());
480-
markerIds[i] = markerBuilder.markerId();
481-
}
482-
483-
Object position = latLngToJson(cluster.getPosition());
484-
Object bounds = latLngBoundsToJson(latLngBoundsBuilder.build());
485-
486-
final Map<String, Object> data = new HashMap<>(4);
487-
488-
// For dart side implementation see parseCluster method at
489-
// packages/google_maps_flutter/google_maps_flutter_android/lib/src/google_maps_flutter_android.dart
490-
data.put("clusterManagerId", clusterManagerId);
491-
data.put("position", position);
492-
data.put("bounds", bounds);
493-
data.put("markerIds", Arrays.asList(markerIds));
494-
return data;
495-
}
496-
497442
static Messages.PlatformCluster clusterToPigeon(
498443
String clusterManagerId, Cluster<MarkerBuilder> cluster) {
499444
int clusterSize = cluster.getSize();
@@ -994,14 +939,8 @@ static String interpretTileOverlayOptions(Map<String, ?> data, TileOverlaySink s
994939
}
995940
}
996941

997-
static Tile interpretTile(Map<String, ?> data) {
998-
int width = toInt(data.get("width"));
999-
int height = toInt(data.get("height"));
1000-
byte[] dataArray = null;
1001-
if (data.get("data") != null) {
1002-
dataArray = (byte[]) data.get("data");
1003-
}
1004-
return new Tile(width, height, dataArray);
942+
static Tile tileFromPigeon(Messages.PlatformTile tile) {
943+
return new Tile(tile.getWidth().intValue(), tile.getHeight().intValue(), tile.getData());
1005944
}
1006945

1007946
@VisibleForTesting

packages/google_maps_flutter/google_maps_flutter_android/android/src/main/java/io/flutter/plugins/googlemaps/GoogleMapController.java

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.google.android.gms.maps.GoogleMapOptions;
3131
import com.google.android.gms.maps.MapView;
3232
import com.google.android.gms.maps.OnMapReadyCallback;
33-
import com.google.android.gms.maps.model.CameraPosition;
3433
import com.google.android.gms.maps.model.Circle;
3534
import com.google.android.gms.maps.model.LatLng;
3635
import com.google.android.gms.maps.model.LatLngBounds;
@@ -44,15 +43,13 @@
4443
import com.google.maps.android.collections.MarkerManager;
4544
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
4645
import io.flutter.plugin.common.BinaryMessenger;
47-
import io.flutter.plugin.common.MethodChannel;
4846
import io.flutter.plugin.platform.PlatformView;
4947
import io.flutter.plugins.googlemaps.Messages.FlutterError;
5048
import io.flutter.plugins.googlemaps.Messages.MapsApi;
49+
import io.flutter.plugins.googlemaps.Messages.MapsCallbackApi;
5150
import io.flutter.plugins.googlemaps.Messages.MapsInspectorApi;
5251
import java.io.ByteArrayOutputStream;
5352
import java.util.ArrayList;
54-
import java.util.Collections;
55-
import java.util.HashMap;
5653
import java.util.List;
5754
import java.util.Map;
5855
import java.util.Objects;
@@ -73,7 +70,7 @@ class GoogleMapController
7370

7471
private static final String TAG = "GoogleMapController";
7572
private final int id;
76-
private final MethodChannel methodChannel;
73+
private final MapsCallbackApi flutterApi;
7774
private final BinaryMessenger binaryMessenger;
7875
private final GoogleMapOptions options;
7976
@Nullable private MapView mapView;
@@ -121,19 +118,18 @@ class GoogleMapController
121118
this.mapView = new MapView(context, options);
122119
this.density = context.getResources().getDisplayMetrics().density;
123120
this.binaryMessenger = binaryMessenger;
124-
methodChannel =
125-
new MethodChannel(binaryMessenger, "plugins.flutter.dev/google_maps_android_" + id);
121+
flutterApi = new MapsCallbackApi(binaryMessenger, Integer.toString(id));
126122
MapsApi.setUp(binaryMessenger, Integer.toString(id), this);
127123
MapsInspectorApi.setUp(binaryMessenger, Integer.toString(id), this);
128124
AssetManager assetManager = context.getAssets();
129125
this.lifecycleProvider = lifecycleProvider;
130-
this.clusterManagersController = new ClusterManagersController(methodChannel, context);
126+
this.clusterManagersController = new ClusterManagersController(flutterApi, context);
131127
this.markersController =
132-
new MarkersController(methodChannel, clusterManagersController, assetManager, density);
133-
this.polygonsController = new PolygonsController(methodChannel, density);
134-
this.polylinesController = new PolylinesController(methodChannel, assetManager, density);
135-
this.circlesController = new CirclesController(methodChannel, density);
136-
this.tileOverlaysController = new TileOverlaysController(methodChannel);
128+
new MarkersController(flutterApi, clusterManagersController, assetManager, density);
129+
this.polygonsController = new PolygonsController(flutterApi, density);
130+
this.polylinesController = new PolylinesController(flutterApi, assetManager, density);
131+
this.circlesController = new CirclesController(flutterApi, density);
132+
this.tileOverlaysController = new TileOverlaysController(flutterApi);
137133
}
138134

139135
// Constructor for testing purposes only
@@ -142,7 +138,7 @@ class GoogleMapController
142138
int id,
143139
Context context,
144140
BinaryMessenger binaryMessenger,
145-
MethodChannel methodChannel,
141+
MapsCallbackApi flutterApi,
146142
LifecycleProvider lifecycleProvider,
147143
GoogleMapOptions options,
148144
ClusterManagersController clusterManagersController,
@@ -154,7 +150,7 @@ class GoogleMapController
154150
this.id = id;
155151
this.context = context;
156152
this.binaryMessenger = binaryMessenger;
157-
this.methodChannel = methodChannel;
153+
this.flutterApi = flutterApi;
158154
this.options = options;
159155
this.mapView = new MapView(context, options);
160156
this.density = context.getResources().getDisplayMetrics().density;
@@ -182,10 +178,6 @@ void init() {
182178
mapView.getMapAsync(this);
183179
}
184180

185-
private CameraPosition getCameraPosition() {
186-
return trackCameraPosition ? googleMap.getCameraPosition() : null;
187-
}
188-
189181
@Override
190182
public void onMapReady(@NonNull GoogleMap googleMap) {
191183
this.googleMap = googleMap;
@@ -298,24 +290,17 @@ public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
298290

299291
@Override
300292
public void onMapClick(@NonNull LatLng latLng) {
301-
final Map<String, Object> arguments = new HashMap<>(2);
302-
arguments.put("position", Convert.latLngToJson(latLng));
303-
methodChannel.invokeMethod("map#onTap", arguments);
293+
flutterApi.onTap(Convert.latLngToPigeon(latLng), new NoOpVoidResult());
304294
}
305295

306296
@Override
307297
public void onMapLongClick(@NonNull LatLng latLng) {
308-
final Map<String, Object> arguments = new HashMap<>(2);
309-
arguments.put("position", Convert.latLngToJson(latLng));
310-
methodChannel.invokeMethod("map#onLongPress", arguments);
298+
flutterApi.onLongPress(Convert.latLngToPigeon(latLng), new NoOpVoidResult());
311299
}
312300

313301
@Override
314302
public void onCameraMoveStarted(int reason) {
315-
final Map<String, Object> arguments = new HashMap<>(2);
316-
boolean isGesture = reason == GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE;
317-
arguments.put("isGesture", isGesture);
318-
methodChannel.invokeMethod("camera#onMoveStarted", arguments);
303+
flutterApi.onCameraMoveStarted(new NoOpVoidResult());
319304
}
320305

321306
@Override
@@ -328,15 +313,14 @@ public void onCameraMove() {
328313
if (!trackCameraPosition) {
329314
return;
330315
}
331-
final Map<String, Object> arguments = new HashMap<>(2);
332-
arguments.put("position", Convert.cameraPositionToJson(googleMap.getCameraPosition()));
333-
methodChannel.invokeMethod("camera#onMove", arguments);
316+
flutterApi.onCameraMove(
317+
Convert.cameraPositionToPigeon(googleMap.getCameraPosition()), new NoOpVoidResult());
334318
}
335319

336320
@Override
337321
public void onCameraIdle() {
338322
clusterManagersController.onCameraIdle();
339-
methodChannel.invokeMethod("camera#onIdle", Collections.singletonMap("map", id));
323+
flutterApi.onCameraIdle(new NoOpVoidResult());
340324
}
341325

342326
@Override

0 commit comments

Comments
 (0)