Skip to content

Commit 11b339e

Browse files
[google_maps_flutter_android] Convert PlatformCameraUpdate to pigeon. (flutter#7507)
Because this also converts the platform interface Camera type, this will need to be a federated change, unless we come up with a better idea of how to convert that class. flutter#152928 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] page, which explains my responsibilities. - [x] I read and followed the [relevant style guides] and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use `dart format`.) - [ ] I signed the [CLA]. - [x] The title of the PR starts with the name of the package surrounded by square brackets, e.g. `[shared_preferences]` - [x] I [linked to at least one issue that this PR fixes] in the description above. - [x] I updated `pubspec.yaml` with an appropriate new version according to the [pub versioning philosophy], or this PR is [exempt from version changes]. - [x] I updated `CHANGELOG.md` to add a description of the change, [following repository CHANGELOG style], or this PR is [exempt from CHANGELOG changes]. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md [relevant style guides]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style [CLA]: https://cla.developers.google.com/ [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md [linked to at least one issue that this PR fixes]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview [pub versioning philosophy]: https://dart.dev/tools/pub/versioning [exempt from version changes]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#version [following repository CHANGELOG style]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog-style [exempt from CHANGELOG changes]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog [test-exempt]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests
1 parent 91caa7a commit 11b339e

File tree

10 files changed

+2362
-1239
lines changed

10 files changed

+2362
-1239
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.14.6
2+
3+
* Converts 'PlatformCameraUpdate' to pigeon.
4+
15
## 2.14.5
26

37
* Converts `JointType` to enum.

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

Lines changed: 59 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import java.io.InputStream;
4545
import java.util.ArrayList;
4646
import java.util.Arrays;
47-
import java.util.HashMap;
4847
import java.util.List;
4948
import java.util.Map;
5049

@@ -316,10 +315,6 @@ public static BitmapDescriptor getBitmapFromAsset(
316315
return bitmapDescriptorFactory.fromAsset(assetKey);
317316
}
318317

319-
private static boolean toBoolean(Object o) {
320-
return (Boolean) o;
321-
}
322-
323318
static @NonNull CameraPosition cameraPositionFromPigeon(
324319
@NonNull Messages.PlatformCameraPosition position) {
325320
final CameraPosition.Builder builder = CameraPosition.builder();
@@ -330,47 +325,57 @@ private static boolean toBoolean(Object o) {
330325
return builder.build();
331326
}
332327

333-
static CameraPosition toCameraPosition(Object o) {
334-
final Map<?, ?> data = toMap(o);
335-
final CameraPosition.Builder builder = CameraPosition.builder();
336-
builder.bearing(toFloat(data.get("bearing")));
337-
builder.target(toLatLng(data.get("target")));
338-
builder.tilt(toFloat(data.get("tilt")));
339-
builder.zoom(toFloat(data.get("zoom")));
340-
return builder.build();
341-
}
342-
343-
static CameraUpdate toCameraUpdate(Object o, float density) {
344-
final List<?> data = toList(o);
345-
switch (toString(data.get(0))) {
346-
case "newCameraPosition":
347-
return CameraUpdateFactory.newCameraPosition(toCameraPosition(data.get(1)));
348-
case "newLatLng":
349-
return CameraUpdateFactory.newLatLng(toLatLng(data.get(1)));
350-
case "newLatLngBounds":
351-
return CameraUpdateFactory.newLatLngBounds(
352-
toLatLngBounds(data.get(1)), toPixels(data.get(2), density));
353-
case "newLatLngZoom":
354-
return CameraUpdateFactory.newLatLngZoom(toLatLng(data.get(1)), toFloat(data.get(2)));
355-
case "scrollBy":
356-
return CameraUpdateFactory.scrollBy( //
357-
toFractionalPixels(data.get(1), density), //
358-
toFractionalPixels(data.get(2), density));
359-
case "zoomBy":
360-
if (data.size() == 2) {
361-
return CameraUpdateFactory.zoomBy(toFloat(data.get(1)));
362-
} else {
363-
return CameraUpdateFactory.zoomBy(toFloat(data.get(1)), toPoint(data.get(2), density));
364-
}
365-
case "zoomIn":
366-
return CameraUpdateFactory.zoomIn();
367-
case "zoomOut":
368-
return CameraUpdateFactory.zoomOut();
369-
case "zoomTo":
370-
return CameraUpdateFactory.zoomTo(toFloat(data.get(1)));
371-
default:
372-
throw new IllegalArgumentException("Cannot interpret " + o + " as CameraUpdate");
373-
}
328+
static CameraUpdate cameraUpdateFromPigeon(Messages.PlatformCameraUpdate update, float density) {
329+
Object cameraUpdate = update.getCameraUpdate();
330+
if (cameraUpdate instanceof Messages.PlatformCameraUpdateNewCameraPosition) {
331+
Messages.PlatformCameraUpdateNewCameraPosition newCameraPosition =
332+
(Messages.PlatformCameraUpdateNewCameraPosition) cameraUpdate;
333+
return CameraUpdateFactory.newCameraPosition(
334+
cameraPositionFromPigeon(newCameraPosition.getCameraPosition()));
335+
}
336+
if (cameraUpdate instanceof Messages.PlatformCameraUpdateNewLatLng) {
337+
Messages.PlatformCameraUpdateNewLatLng newLatLng =
338+
(Messages.PlatformCameraUpdateNewLatLng) cameraUpdate;
339+
return CameraUpdateFactory.newLatLng(latLngFromPigeon(newLatLng.getLatLng()));
340+
}
341+
if (cameraUpdate instanceof Messages.PlatformCameraUpdateNewLatLngZoom) {
342+
Messages.PlatformCameraUpdateNewLatLngZoom newLatLngZoom =
343+
(Messages.PlatformCameraUpdateNewLatLngZoom) cameraUpdate;
344+
return CameraUpdateFactory.newLatLngZoom(
345+
latLngFromPigeon(newLatLngZoom.getLatLng()), newLatLngZoom.getZoom().floatValue());
346+
}
347+
if (cameraUpdate instanceof Messages.PlatformCameraUpdateNewLatLngBounds) {
348+
Messages.PlatformCameraUpdateNewLatLngBounds newLatLngBounds =
349+
(Messages.PlatformCameraUpdateNewLatLngBounds) cameraUpdate;
350+
return CameraUpdateFactory.newLatLngBounds(
351+
latLngBoundsFromPigeon(newLatLngBounds.getBounds()),
352+
(int) (newLatLngBounds.getPadding() * density));
353+
}
354+
if (cameraUpdate instanceof Messages.PlatformCameraUpdateScrollBy) {
355+
Messages.PlatformCameraUpdateScrollBy scrollBy =
356+
(Messages.PlatformCameraUpdateScrollBy) cameraUpdate;
357+
return CameraUpdateFactory.scrollBy(
358+
scrollBy.getDx().floatValue() * density, scrollBy.getDy().floatValue() * density);
359+
}
360+
if (cameraUpdate instanceof Messages.PlatformCameraUpdateZoomBy) {
361+
Messages.PlatformCameraUpdateZoomBy zoomBy =
362+
(Messages.PlatformCameraUpdateZoomBy) cameraUpdate;
363+
final Point focus = pointFromPigeon(zoomBy.getFocus(), density);
364+
return (focus != null)
365+
? CameraUpdateFactory.zoomBy(zoomBy.getAmount().floatValue(), focus)
366+
: CameraUpdateFactory.zoomBy(zoomBy.getAmount().floatValue());
367+
}
368+
if (cameraUpdate instanceof Messages.PlatformCameraUpdateZoomTo) {
369+
Messages.PlatformCameraUpdateZoomTo zoomTo =
370+
(Messages.PlatformCameraUpdateZoomTo) cameraUpdate;
371+
return CameraUpdateFactory.zoomTo(zoomTo.getZoom().floatValue());
372+
}
373+
if (cameraUpdate instanceof Messages.PlatformCameraUpdateZoom) {
374+
Messages.PlatformCameraUpdateZoom zoom = (Messages.PlatformCameraUpdateZoom) cameraUpdate;
375+
return (zoom.getOut()) ? CameraUpdateFactory.zoomOut() : CameraUpdateFactory.zoomIn();
376+
}
377+
throw new IllegalArgumentException(
378+
"PlatformCameraUpdate's cameraUpdate field must be one of the PlatformCameraUpdate... case classes.");
374379
}
375380

376381
private static double toDouble(Object o) {
@@ -494,16 +499,16 @@ static Point pointFromPigeon(Messages.PlatformPoint point) {
494499
return new Point(point.getX().intValue(), point.getY().intValue());
495500
}
496501

497-
static Messages.PlatformPoint pointToPigeon(Point point) {
498-
return new Messages.PlatformPoint.Builder().setX((long) point.x).setY((long) point.y).build();
499-
}
500-
501-
private static LatLngBounds toLatLngBounds(Object o) {
502-
if (o == null) {
502+
@Nullable
503+
static Point pointFromPigeon(@Nullable Messages.PlatformOffset point, float density) {
504+
if (point == null) {
503505
return null;
504506
}
505-
final List<?> data = toList(o);
506-
return new LatLngBounds(toLatLng(data.get(0)), toLatLng(data.get(1)));
507+
return new Point((int) (point.getDx() * density), (int) (point.getDy() * density));
508+
}
509+
510+
static Messages.PlatformPoint pointToPigeon(Point point) {
511+
return new Messages.PlatformPoint.Builder().setX((long) point.x).setY((long) point.y).build();
507512
}
508513

509514
private static List<?> toList(Object o) {
@@ -514,26 +519,6 @@ private static List<?> toList(Object o) {
514519
return (Map<?, ?>) o;
515520
}
516521

517-
private static Map<String, Object> toObjectMap(Object o) {
518-
Map<String, Object> hashMap = new HashMap<>();
519-
Map<?, ?> map = (Map<?, ?>) o;
520-
for (Object key : map.keySet()) {
521-
Object object = map.get(key);
522-
if (object != null) {
523-
hashMap.put((String) key, object);
524-
}
525-
}
526-
return hashMap;
527-
}
528-
529-
private static float toFractionalPixels(Object o, float density) {
530-
return toFloat(o) * density;
531-
}
532-
533-
private static int toPixels(Object o, float density) {
534-
return (int) toFractionalPixels(o, density);
535-
}
536-
537522
private static Bitmap toBitmap(Object o) {
538523
byte[] bmpData = (byte[]) o;
539524
Bitmap bitmap = BitmapFactory.decodeByteArray(bmpData, 0, bmpData.length);
@@ -563,11 +548,6 @@ private static Bitmap toScaledBitmap(Bitmap bitmap, int width, int height) {
563548
return bitmap;
564549
}
565550

566-
private static Point toPoint(Object o, float density) {
567-
final List<?> data = toList(o);
568-
return new Point(toPixels(data.get(0), density), toPixels(data.get(1), density));
569-
}
570-
571551
private static String toString(Object o) {
572552
return (String) o;
573553
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ public void moveCamera(@NonNull Messages.PlatformCameraUpdate cameraUpdate) {
933933
throw new FlutterError(
934934
"GoogleMap uninitialized", "moveCamera called prior to map initialization", null);
935935
}
936-
googleMap.moveCamera(Convert.toCameraUpdate(cameraUpdate.getJson(), density));
936+
googleMap.moveCamera(Convert.cameraUpdateFromPigeon(cameraUpdate, density));
937937
}
938938

939939
@Override
@@ -942,7 +942,7 @@ public void animateCamera(@NonNull Messages.PlatformCameraUpdate cameraUpdate) {
942942
throw new FlutterError(
943943
"GoogleMap uninitialized", "animateCamera called prior to map initialization", null);
944944
}
945-
googleMap.animateCamera(Convert.toCameraUpdate(cameraUpdate.getJson(), density));
945+
googleMap.animateCamera(Convert.cameraUpdateFromPigeon(cameraUpdate, density));
946946
}
947947

948948
@Override

0 commit comments

Comments
 (0)