44
44
import java .io .InputStream ;
45
45
import java .util .ArrayList ;
46
46
import java .util .Arrays ;
47
- import java .util .HashMap ;
48
47
import java .util .List ;
49
48
import java .util .Map ;
50
49
@@ -316,10 +315,6 @@ public static BitmapDescriptor getBitmapFromAsset(
316
315
return bitmapDescriptorFactory .fromAsset (assetKey );
317
316
}
318
317
319
- private static boolean toBoolean (Object o ) {
320
- return (Boolean ) o ;
321
- }
322
-
323
318
static @ NonNull CameraPosition cameraPositionFromPigeon (
324
319
@ NonNull Messages .PlatformCameraPosition position ) {
325
320
final CameraPosition .Builder builder = CameraPosition .builder ();
@@ -330,47 +325,57 @@ private static boolean toBoolean(Object o) {
330
325
return builder .build ();
331
326
}
332
327
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." );
374
379
}
375
380
376
381
private static double toDouble (Object o ) {
@@ -494,16 +499,16 @@ static Point pointFromPigeon(Messages.PlatformPoint point) {
494
499
return new Point (point .getX ().intValue (), point .getY ().intValue ());
495
500
}
496
501
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 ) {
503
505
return null ;
504
506
}
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 ();
507
512
}
508
513
509
514
private static List <?> toList (Object o ) {
@@ -514,26 +519,6 @@ private static List<?> toList(Object o) {
514
519
return (Map <?, ?>) o ;
515
520
}
516
521
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
-
537
522
private static Bitmap toBitmap (Object o ) {
538
523
byte [] bmpData = (byte []) o ;
539
524
Bitmap bitmap = BitmapFactory .decodeByteArray (bmpData , 0 , bmpData .length );
@@ -563,11 +548,6 @@ private static Bitmap toScaledBitmap(Bitmap bitmap, int width, int height) {
563
548
return bitmap ;
564
549
}
565
550
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
-
571
551
private static String toString (Object o ) {
572
552
return (String ) o ;
573
553
}
0 commit comments