4
4
5
5
package io .flutter .plugins .googlemaps ;
6
6
7
- import static io .flutter .plugins .googlemaps .GoogleMapsPlugin .CREATED ;
8
- import static io .flutter .plugins .googlemaps .GoogleMapsPlugin .DESTROYED ;
9
- import static io .flutter .plugins .googlemaps .GoogleMapsPlugin .PAUSED ;
10
- import static io .flutter .plugins .googlemaps .GoogleMapsPlugin .RESUMED ;
11
- import static io .flutter .plugins .googlemaps .GoogleMapsPlugin .STARTED ;
12
- import static io .flutter .plugins .googlemaps .GoogleMapsPlugin .STOPPED ;
13
-
14
7
import android .Manifest ;
15
8
import android .annotation .SuppressLint ;
16
9
import android .app .Activity ;
26
19
import androidx .annotation .Nullable ;
27
20
import androidx .lifecycle .DefaultLifecycleObserver ;
28
21
import androidx .lifecycle .Lifecycle ;
22
+ import androidx .lifecycle .Lifecycle .State ;
29
23
import androidx .lifecycle .LifecycleOwner ;
30
24
import com .google .android .gms .maps .CameraUpdate ;
31
25
import com .google .android .gms .maps .GoogleMap ;
53
47
import java .util .HashMap ;
54
48
import java .util .List ;
55
49
import java .util .Map ;
56
- import java .util .concurrent .atomic .AtomicInteger ;
57
50
58
51
/** Controller of a single GoogleMaps MapView instance. */
59
52
final class GoogleMapController
@@ -68,7 +61,6 @@ final class GoogleMapController
68
61
69
62
private static final String TAG = "GoogleMapController" ;
70
63
private final int id ;
71
- private final AtomicInteger activityState ;
72
64
private final MethodChannel methodChannel ;
73
65
private final GoogleMapOptions options ;
74
66
@ Nullable private MapView mapView ;
@@ -83,13 +75,12 @@ final class GoogleMapController
83
75
private boolean disposed = false ;
84
76
private final float density ;
85
77
private MethodChannel .Result mapReadyResult ;
86
- private final int
87
- activityHashCode ; // Do not use directly, use getActivityHashCode() instead to get correct hashCode for both v1 and v2 embedding.
88
- private final Lifecycle lifecycle ;
78
+ @ Nullable private final Lifecycle lifecycle ;
89
79
private final Context context ;
90
- private final Application
91
- mApplication ; // Do not use direclty, use getApplication() instead to get correct application object for both v1 and v2 embedding.
92
- private final PluginRegistry .Registrar registrar ; // For v1 embedding only.
80
+ // Do not use directly, use getApplication() instead to get correct application object for both v1
81
+ // and v2 embedding.
82
+ @ Nullable private final Application mApplication ;
83
+ @ Nullable private final PluginRegistry .Registrar registrar ; // For v1 embedding only.
93
84
private final MarkersController markersController ;
94
85
private final PolygonsController polygonsController ;
95
86
private final PolylinesController polylinesController ;
@@ -102,16 +93,13 @@ final class GoogleMapController
102
93
GoogleMapController (
103
94
int id ,
104
95
Context context ,
105
- AtomicInteger activityState ,
106
96
BinaryMessenger binaryMessenger ,
107
- Application application ,
108
- Lifecycle lifecycle ,
109
- PluginRegistry .Registrar registrar ,
110
- int registrarActivityHashCode ,
97
+ @ Nullable Application application ,
98
+ @ Nullable Lifecycle lifecycle ,
99
+ @ Nullable PluginRegistry .Registrar registrar ,
111
100
GoogleMapOptions options ) {
112
101
this .id = id ;
113
102
this .context = context ;
114
- this .activityState = activityState ;
115
103
this .options = options ;
116
104
this .mapView = new MapView (context , options );
117
105
this .density = context .getResources ().getDisplayMetrics ().density ;
@@ -120,7 +108,6 @@ final class GoogleMapController
120
108
mApplication = application ;
121
109
this .lifecycle = lifecycle ;
122
110
this .registrar = registrar ;
123
- this .activityHashCode = registrarActivityHashCode ;
124
111
this .markersController = new MarkersController (methodChannel );
125
112
this .polygonsController = new PolygonsController (methodChannel , density );
126
113
this .polylinesController = new PolylinesController (methodChannel , density );
@@ -132,21 +119,8 @@ public View getView() {
132
119
return mapView ;
133
120
}
134
121
135
- void init () {
136
- switch (activityState .get ()) {
137
- case STOPPED :
138
- mapView .onCreate (null );
139
- mapView .onStart ();
140
- mapView .onResume ();
141
- mapView .onPause ();
142
- mapView .onStop ();
143
- break ;
144
- case PAUSED :
145
- mapView .onCreate (null );
146
- mapView .onStart ();
147
- mapView .onResume ();
148
- mapView .onPause ();
149
- break ;
122
+ void init (State lifecycleState ) {
123
+ switch (lifecycleState ) {
150
124
case RESUMED :
151
125
mapView .onCreate (null );
152
126
mapView .onStart ();
@@ -160,11 +134,9 @@ void init() {
160
134
mapView .onCreate (null );
161
135
break ;
162
136
case DESTROYED :
163
- // Nothing to do, the activity has been completely destroyed.
137
+ case INITIALIZED :
138
+ // Nothing to do, the activity has been completely destroyed or not yet created.
164
139
break ;
165
- default :
166
- throw new IllegalArgumentException (
167
- "Cannot interpret " + activityState .get () + " as an activity state" );
168
140
}
169
141
if (lifecycle != null ) {
170
142
lifecycle .addObserver (this );
@@ -556,14 +528,14 @@ private void setGoogleMapListener(@Nullable GoogleMapListener listener) {
556
528
// does. This will override it when available even with the annotation commented out.
557
529
public void onInputConnectionLocked () {
558
530
// TODO(mklim): Remove this empty override once https://github.com/flutter/flutter/issues/40126 is fixed in stable.
559
- };
531
+ }
560
532
561
533
// @Override
562
534
// The minimum supported version of Flutter doesn't have this method on the PlatformView interface, but the maximum
563
535
// does. This will override it when available even with the annotation commented out.
564
536
public void onInputConnectionUnlocked () {
565
537
// TODO(mklim): Remove this empty override once https://github.com/flutter/flutter/issues/40126 is fixed in stable.
566
- };
538
+ }
567
539
568
540
// Application.ActivityLifecycleCallbacks methods
569
541
@ Override
@@ -880,7 +852,9 @@ private int getActivityHashCode() {
880
852
if (registrar != null && registrar .activity () != null ) {
881
853
return registrar .activity ().hashCode ();
882
854
} else {
883
- return activityHashCode ;
855
+ // TODO(cyanglaz): Remove `getActivityHashCode()` and use a cached hashCode when creating the view for V1 embedding.
856
+ // https://github.com/flutter/flutter/issues/69128
857
+ return -1 ;
884
858
}
885
859
}
886
860
@@ -916,16 +890,3 @@ public void setBuildingsEnabled(boolean buildingsEnabled) {
916
890
this .buildingsEnabled = buildingsEnabled ;
917
891
}
918
892
}
919
-
920
- interface GoogleMapListener
921
- extends GoogleMap .OnCameraIdleListener ,
922
- GoogleMap .OnCameraMoveListener ,
923
- GoogleMap .OnCameraMoveStartedListener ,
924
- GoogleMap .OnInfoWindowClickListener ,
925
- GoogleMap .OnMarkerClickListener ,
926
- GoogleMap .OnPolygonClickListener ,
927
- GoogleMap .OnPolylineClickListener ,
928
- GoogleMap .OnCircleClickListener ,
929
- GoogleMap .OnMapClickListener ,
930
- GoogleMap .OnMapLongClickListener ,
931
- GoogleMap .OnMarkerDragListener {}
0 commit comments