2121import android .os .Bundle ;
2222import android .util .Log ;
2323import android .view .View ;
24+ import androidx .annotation .NonNull ;
25+ import androidx .lifecycle .DefaultLifecycleObserver ;
26+ import androidx .lifecycle .Lifecycle ;
27+ import androidx .lifecycle .LifecycleOwner ;
2428import com .google .android .gms .maps .CameraUpdate ;
2529import com .google .android .gms .maps .GoogleMap ;
2630import com .google .android .gms .maps .GoogleMapOptions ;
3438import com .google .android .gms .maps .model .Marker ;
3539import com .google .android .gms .maps .model .Polygon ;
3640import com .google .android .gms .maps .model .Polyline ;
41+ import io .flutter .embedding .engine .plugins .activity .ActivityPluginBinding ;
42+ import io .flutter .plugin .common .BinaryMessenger ;
3743import io .flutter .plugin .common .MethodCall ;
3844import io .flutter .plugin .common .MethodChannel ;
3945import io .flutter .plugin .common .PluginRegistry ;
4854/** Controller of a single GoogleMaps MapView instance. */
4955final class GoogleMapController
5056 implements Application .ActivityLifecycleCallbacks ,
57+ DefaultLifecycleObserver ,
58+ ActivityPluginBinding .OnSaveInstanceStateListener ,
5159 GoogleMap .OnCameraIdleListener ,
5260 GoogleMap .OnCameraMoveListener ,
5361 GoogleMap .OnCameraMoveStartedListener ,
@@ -68,7 +76,6 @@ final class GoogleMapController
6876 private final int id ;
6977 private final AtomicInteger activityState ;
7078 private final MethodChannel methodChannel ;
71- private final PluginRegistry .Registrar registrar ;
7279 private final MapView mapView ;
7380 private GoogleMap googleMap ;
7481 private boolean trackCameraPosition = false ;
@@ -80,8 +87,13 @@ final class GoogleMapController
8087 private boolean disposed = false ;
8188 private final float density ;
8289 private MethodChannel .Result mapReadyResult ;
83- private final int registrarActivityHashCode ;
90+ private final int
91+ activityHashCode ; // Do not use directly, use getActivityHashCode() instead to get correct hashCode for both v1 and v2 embedding.
92+ private final Lifecycle lifecycle ;
8493 private final Context context ;
94+ private final Application
95+ mApplication ; // Do not use direclty, use getApplication() instead to get correct application object for both v1 and v2 embedding.
96+ private final PluginRegistry .Registrar registrar ; // For v1 embedding only.
8597 private final MarkersController markersController ;
8698 private final PolygonsController polygonsController ;
8799 private final PolylinesController polylinesController ;
@@ -95,18 +107,23 @@ final class GoogleMapController
95107 int id ,
96108 Context context ,
97109 AtomicInteger activityState ,
110+ BinaryMessenger binaryMessenger ,
111+ Application application ,
112+ Lifecycle lifecycle ,
98113 PluginRegistry .Registrar registrar ,
114+ int registrarActivityHashCode ,
99115 GoogleMapOptions options ) {
100116 this .id = id ;
101117 this .context = context ;
102118 this .activityState = activityState ;
103- this .registrar = registrar ;
104119 this .mapView = new MapView (context , options );
105120 this .density = context .getResources ().getDisplayMetrics ().density ;
106- methodChannel =
107- new MethodChannel (registrar .messenger (), "plugins.flutter.io/google_maps_" + id );
121+ methodChannel = new MethodChannel (binaryMessenger , "plugins.flutter.io/google_maps_" + id );
108122 methodChannel .setMethodCallHandler (this );
109- this .registrarActivityHashCode = registrar .activity ().hashCode ();
123+ mApplication = application ;
124+ this .lifecycle = lifecycle ;
125+ this .registrar = registrar ;
126+ this .activityHashCode = registrarActivityHashCode ;
110127 this .markersController = new MarkersController (methodChannel );
111128 this .polygonsController = new PolygonsController (methodChannel );
112129 this .polylinesController = new PolylinesController (methodChannel , density );
@@ -152,7 +169,11 @@ void init() {
152169 throw new IllegalArgumentException (
153170 "Cannot interpret " + activityState .get () + " as an activity state" );
154171 }
155- registrar .activity ().getApplication ().registerActivityLifecycleCallbacks (this );
172+ if (lifecycle != null ) {
173+ lifecycle .addObserver (this );
174+ } else {
175+ getApplication ().registerActivityLifecycleCallbacks (this );
176+ }
156177 mapView .getMapAsync (this );
157178 }
158179
@@ -368,6 +389,10 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
368389 result .success (googleMap .isBuildingsEnabled ());
369390 break ;
370391 }
392+ case "map#getZoomLevel" :
393+ {
394+ result .success (googleMap .getCameraPosition ().zoom );
395+ }
371396 case "map#setStyle" :
372397 {
373398 String mapStyle = (String ) call .arguments ;
@@ -472,7 +497,7 @@ public void dispose() {
472497 disposed = true ;
473498 methodChannel .setMethodCallHandler (null );
474499 mapView .onDestroy ();
475- registrar . activity (). getApplication ().unregisterActivityLifecycleCallbacks (this );
500+ getApplication ().unregisterActivityLifecycleCallbacks (this );
476501 }
477502
478503 // @Override
@@ -489,62 +514,129 @@ public void onInputConnectionUnlocked() {
489514 // TODO(mklim): Remove this empty override once https://github.com/flutter/flutter/issues/40126 is fixed in stable.
490515 };
491516
517+ // Application.ActivityLifecycleCallbacks methods
492518 @ Override
493519 public void onActivityCreated (Activity activity , Bundle savedInstanceState ) {
494- if (disposed || activity .hashCode () != registrarActivityHashCode ) {
520+ if (disposed || activity .hashCode () != getActivityHashCode () ) {
495521 return ;
496522 }
497523 mapView .onCreate (savedInstanceState );
498524 }
499525
500526 @ Override
501527 public void onActivityStarted (Activity activity ) {
502- if (disposed || activity .hashCode () != registrarActivityHashCode ) {
528+ if (disposed || activity .hashCode () != getActivityHashCode () ) {
503529 return ;
504530 }
505531 mapView .onStart ();
506532 }
507533
508534 @ Override
509535 public void onActivityResumed (Activity activity ) {
510- if (disposed || activity .hashCode () != registrarActivityHashCode ) {
536+ if (disposed || activity .hashCode () != getActivityHashCode () ) {
511537 return ;
512538 }
513539 mapView .onResume ();
514540 }
515541
516542 @ Override
517543 public void onActivityPaused (Activity activity ) {
518- if (disposed || activity .hashCode () != registrarActivityHashCode ) {
544+ if (disposed || activity .hashCode () != getActivityHashCode () ) {
519545 return ;
520546 }
521547 mapView .onPause ();
522548 }
523549
524550 @ Override
525551 public void onActivityStopped (Activity activity ) {
526- if (disposed || activity .hashCode () != registrarActivityHashCode ) {
552+ if (disposed || activity .hashCode () != getActivityHashCode () ) {
527553 return ;
528554 }
529555 mapView .onStop ();
530556 }
531557
532558 @ Override
533559 public void onActivitySaveInstanceState (Activity activity , Bundle outState ) {
534- if (disposed || activity .hashCode () != registrarActivityHashCode ) {
560+ if (disposed || activity .hashCode () != getActivityHashCode () ) {
535561 return ;
536562 }
537563 mapView .onSaveInstanceState (outState );
538564 }
539565
540566 @ Override
541567 public void onActivityDestroyed (Activity activity ) {
542- if (disposed || activity .hashCode () != registrarActivityHashCode ) {
568+ if (disposed || activity .hashCode () != getActivityHashCode ()) {
569+ return ;
570+ }
571+ mapView .onDestroy ();
572+ }
573+
574+ // DefaultLifecycleObserver and OnSaveInstanceStateListener
575+
576+ @ Override
577+ public void onCreate (@ NonNull LifecycleOwner owner ) {
578+ if (disposed ) {
579+ return ;
580+ }
581+ mapView .onCreate (null );
582+ }
583+
584+ @ Override
585+ public void onStart (@ NonNull LifecycleOwner owner ) {
586+ if (disposed ) {
587+ return ;
588+ }
589+ mapView .onStart ();
590+ }
591+
592+ @ Override
593+ public void onResume (@ NonNull LifecycleOwner owner ) {
594+ if (disposed ) {
595+ return ;
596+ }
597+ mapView .onResume ();
598+ }
599+
600+ @ Override
601+ public void onPause (@ NonNull LifecycleOwner owner ) {
602+ if (disposed ) {
603+ return ;
604+ }
605+ mapView .onResume ();
606+ }
607+
608+ @ Override
609+ public void onStop (@ NonNull LifecycleOwner owner ) {
610+ if (disposed ) {
611+ return ;
612+ }
613+ mapView .onStop ();
614+ }
615+
616+ @ Override
617+ public void onDestroy (@ NonNull LifecycleOwner owner ) {
618+ if (disposed ) {
543619 return ;
544620 }
545621 mapView .onDestroy ();
546622 }
547623
624+ @ Override
625+ public void onRestoreInstanceState (Bundle bundle ) {
626+ if (disposed ) {
627+ return ;
628+ }
629+ mapView .onCreate (bundle );
630+ }
631+
632+ @ Override
633+ public void onSaveInstanceState (Bundle bundle ) {
634+ if (disposed ) {
635+ return ;
636+ }
637+ mapView .onSaveInstanceState (bundle );
638+ }
639+
548640 // GoogleMapOptionsSink methods
549641
550642 @ Override
@@ -716,6 +808,22 @@ private int checkSelfPermission(String permission) {
716808 permission , android .os .Process .myPid (), android .os .Process .myUid ());
717809 }
718810
811+ private int getActivityHashCode () {
812+ if (registrar != null && registrar .activity () != null ) {
813+ return registrar .activity ().hashCode ();
814+ } else {
815+ return activityHashCode ;
816+ }
817+ }
818+
819+ private Application getApplication () {
820+ if (registrar != null && registrar .activity () != null ) {
821+ return registrar .activity ().getApplication ();
822+ } else {
823+ return mApplication ;
824+ }
825+ }
826+
719827 public void setIndoorEnabled (boolean indoorEnabled ) {
720828 this .indoorEnabled = indoorEnabled ;
721829 }
0 commit comments