Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 81bbcd4

Browse files
authored
[google_maps_flutter] Clean up google_maps_flutter plugin (#3206)
1. A few minor formatting changes and additions of @nullable annotations 2. Removed pass-through of activityHashCode. In the legacy plugin use case, the value is always -1 (which has been inlined). In the new plugin use case, the value should never be used because it uses DefaultLifecycleCallbacks instead of ActivityLifecycleCallbacks. 3. Replaced custom lifecycle state ints with androidx.lifecycle.Lifecycle.State enum. Also simplify paused/stopped states, which don't need their own dedicated states. Paused == started and stopped == created. 4. Fixed a bug where the Lifecycle object was being leaked onDetachFromActivity by nulling out the field. 5. Moved GoogleMapListener to its own file. Declaring multiple top level classes in the same file is bad practice.
1 parent 64d7334 commit 81bbcd4

File tree

9 files changed

+94
-119
lines changed

9 files changed

+94
-119
lines changed

packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 1.0.4
2+
3+
* Cleanup of Android code:
4+
* A few minor formatting changes and additions of `@Nullable` annotations.
5+
* Removed pass-through of `activityHashCode` to `GoogleMapController`.
6+
* Replaced custom lifecycle state ints with `androidx.lifecycle.Lifecycle.State` enum.
7+
* Fixed a bug where the Lifecycle object was being leaked `onDetachFromActivity`, by nulling out the field.
8+
* Moved GoogleMapListener to its own file. Declaring multiple top level classes in the same file is discouraged.
9+
110
## 1.0.3
211

312
* Update android compileSdkVersion to 29.

packages/google_maps_flutter/google_maps_flutter/android/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
}
99

1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:3.3.2'
11+
classpath 'com.android.tools.build:gradle:3.5.0'
1212
}
1313
}
1414

@@ -33,6 +33,7 @@ android {
3333
}
3434

3535
dependencies {
36+
implementation "androidx.annotation:annotation:1.1.0"
3637
implementation 'com.google.android.gms:play-services-maps:17.0.0'
3738
androidTestImplementation 'androidx.test:runner:1.2.0'
3839
androidTestImplementation 'androidx.test:rules:1.2.0'

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
import android.app.Application;
88
import android.content.Context;
99
import android.graphics.Rect;
10+
import androidx.annotation.Nullable;
1011
import androidx.lifecycle.Lifecycle;
12+
import androidx.lifecycle.Lifecycle.State;
1113
import com.google.android.gms.maps.GoogleMapOptions;
1214
import com.google.android.gms.maps.model.CameraPosition;
1315
import com.google.android.gms.maps.model.LatLngBounds;
1416
import io.flutter.plugin.common.BinaryMessenger;
1517
import io.flutter.plugin.common.PluginRegistry;
16-
import java.util.concurrent.atomic.AtomicInteger;
1718

1819
class GoogleMapBuilder implements GoogleMapOptionsSink {
1920
private final GoogleMapOptions options = new GoogleMapOptions();
@@ -32,24 +33,15 @@ class GoogleMapBuilder implements GoogleMapOptionsSink {
3233
GoogleMapController build(
3334
int id,
3435
Context context,
35-
AtomicInteger state,
36+
State lifecycleState,
3637
BinaryMessenger binaryMessenger,
37-
Application application,
38-
Lifecycle lifecycle,
39-
PluginRegistry.Registrar registrar,
40-
int activityHashCode) {
38+
@Nullable Application application,
39+
@Nullable Lifecycle lifecycle,
40+
@Nullable PluginRegistry.Registrar registrar) {
4141
final GoogleMapController controller =
4242
new GoogleMapController(
43-
id,
44-
context,
45-
state,
46-
binaryMessenger,
47-
application,
48-
lifecycle,
49-
registrar,
50-
activityHashCode,
51-
options);
52-
controller.init();
43+
id, context, binaryMessenger, application, lifecycle, registrar, options);
44+
controller.init(lifecycleState);
5345
controller.setMyLocationEnabled(myLocationEnabled);
5446
controller.setMyLocationButtonEnabled(myLocationButtonEnabled);
5547
controller.setIndoorEnabled(indoorEnabled);

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

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@
44

55
package io.flutter.plugins.googlemaps;
66

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-
147
import android.Manifest;
158
import android.annotation.SuppressLint;
169
import android.app.Activity;
@@ -26,6 +19,7 @@
2619
import androidx.annotation.Nullable;
2720
import androidx.lifecycle.DefaultLifecycleObserver;
2821
import androidx.lifecycle.Lifecycle;
22+
import androidx.lifecycle.Lifecycle.State;
2923
import androidx.lifecycle.LifecycleOwner;
3024
import com.google.android.gms.maps.CameraUpdate;
3125
import com.google.android.gms.maps.GoogleMap;
@@ -53,7 +47,6 @@
5347
import java.util.HashMap;
5448
import java.util.List;
5549
import java.util.Map;
56-
import java.util.concurrent.atomic.AtomicInteger;
5750

5851
/** Controller of a single GoogleMaps MapView instance. */
5952
final class GoogleMapController
@@ -68,7 +61,6 @@ final class GoogleMapController
6861

6962
private static final String TAG = "GoogleMapController";
7063
private final int id;
71-
private final AtomicInteger activityState;
7264
private final MethodChannel methodChannel;
7365
private final GoogleMapOptions options;
7466
@Nullable private MapView mapView;
@@ -83,13 +75,12 @@ final class GoogleMapController
8375
private boolean disposed = false;
8476
private final float density;
8577
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;
8979
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.
9384
private final MarkersController markersController;
9485
private final PolygonsController polygonsController;
9586
private final PolylinesController polylinesController;
@@ -102,16 +93,13 @@ final class GoogleMapController
10293
GoogleMapController(
10394
int id,
10495
Context context,
105-
AtomicInteger activityState,
10696
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,
111100
GoogleMapOptions options) {
112101
this.id = id;
113102
this.context = context;
114-
this.activityState = activityState;
115103
this.options = options;
116104
this.mapView = new MapView(context, options);
117105
this.density = context.getResources().getDisplayMetrics().density;
@@ -120,7 +108,6 @@ final class GoogleMapController
120108
mApplication = application;
121109
this.lifecycle = lifecycle;
122110
this.registrar = registrar;
123-
this.activityHashCode = registrarActivityHashCode;
124111
this.markersController = new MarkersController(methodChannel);
125112
this.polygonsController = new PolygonsController(methodChannel, density);
126113
this.polylinesController = new PolylinesController(methodChannel, density);
@@ -132,21 +119,8 @@ public View getView() {
132119
return mapView;
133120
}
134121

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) {
150124
case RESUMED:
151125
mapView.onCreate(null);
152126
mapView.onStart();
@@ -160,11 +134,9 @@ void init() {
160134
mapView.onCreate(null);
161135
break;
162136
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.
164139
break;
165-
default:
166-
throw new IllegalArgumentException(
167-
"Cannot interpret " + activityState.get() + " as an activity state");
168140
}
169141
if (lifecycle != null) {
170142
lifecycle.addObserver(this);
@@ -556,14 +528,14 @@ private void setGoogleMapListener(@Nullable GoogleMapListener listener) {
556528
// does. This will override it when available even with the annotation commented out.
557529
public void onInputConnectionLocked() {
558530
// TODO(mklim): Remove this empty override once https://github.com/flutter/flutter/issues/40126 is fixed in stable.
559-
};
531+
}
560532

561533
// @Override
562534
// The minimum supported version of Flutter doesn't have this method on the PlatformView interface, but the maximum
563535
// does. This will override it when available even with the annotation commented out.
564536
public void onInputConnectionUnlocked() {
565537
// TODO(mklim): Remove this empty override once https://github.com/flutter/flutter/issues/40126 is fixed in stable.
566-
};
538+
}
567539

568540
// Application.ActivityLifecycleCallbacks methods
569541
@Override
@@ -880,7 +852,9 @@ private int getActivityHashCode() {
880852
if (registrar != null && registrar.activity() != null) {
881853
return registrar.activity().hashCode();
882854
} 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;
884858
}
885859
}
886860

@@ -916,16 +890,3 @@ public void setBuildingsEnabled(boolean buildingsEnabled) {
916890
this.buildingsEnabled = buildingsEnabled;
917891
}
918892
}
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 {}

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

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,36 @@
66

77
import android.app.Application;
88
import android.content.Context;
9+
import androidx.annotation.Nullable;
910
import androidx.lifecycle.Lifecycle;
11+
import androidx.lifecycle.Lifecycle.State;
1012
import com.google.android.gms.maps.model.CameraPosition;
1113
import io.flutter.plugin.common.BinaryMessenger;
1214
import io.flutter.plugin.common.PluginRegistry;
1315
import io.flutter.plugin.common.StandardMessageCodec;
1416
import io.flutter.plugin.platform.PlatformView;
1517
import io.flutter.plugin.platform.PlatformViewFactory;
1618
import java.util.Map;
17-
import java.util.concurrent.atomic.AtomicInteger;
19+
import java.util.concurrent.atomic.AtomicReference;
1820

1921
public class GoogleMapFactory extends PlatformViewFactory {
2022

21-
private final AtomicInteger mActivityState;
23+
private final AtomicReference<State> lifecycleState;
2224
private final BinaryMessenger binaryMessenger;
23-
private final Application application;
24-
private final int activityHashCode;
25-
private final Lifecycle lifecycle;
26-
private final PluginRegistry.Registrar registrar; // V1 embedding only.
25+
@Nullable private final Application application;
26+
@Nullable private final Lifecycle lifecycle;
27+
@Nullable private final PluginRegistry.Registrar registrar; // V1 embedding only.
2728

2829
GoogleMapFactory(
29-
AtomicInteger state,
30+
AtomicReference<State> lifecycleState,
3031
BinaryMessenger binaryMessenger,
31-
Application application,
32-
Lifecycle lifecycle,
33-
PluginRegistry.Registrar registrar,
34-
int activityHashCode) {
32+
@Nullable Application application,
33+
@Nullable Lifecycle lifecycle,
34+
@Nullable PluginRegistry.Registrar registrar) {
3535
super(StandardMessageCodec.INSTANCE);
36-
mActivityState = state;
36+
this.lifecycleState = lifecycleState;
3737
this.binaryMessenger = binaryMessenger;
3838
this.application = application;
39-
this.activityHashCode = activityHashCode;
4039
this.lifecycle = lifecycle;
4140
this.registrar = registrar;
4241
}
@@ -65,13 +64,6 @@ public PlatformView create(Context context, int id, Object args) {
6564
builder.setInitialCircles(params.get("circlesToAdd"));
6665
}
6766
return builder.build(
68-
id,
69-
context,
70-
mActivityState,
71-
binaryMessenger,
72-
application,
73-
lifecycle,
74-
registrar,
75-
activityHashCode);
67+
id, context, lifecycleState.get(), binaryMessenger, application, lifecycle, registrar);
7668
}
7769
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package io.flutter.plugins.googlemaps;
6+
7+
import com.google.android.gms.maps.GoogleMap;
8+
9+
interface GoogleMapListener
10+
extends GoogleMap.OnCameraIdleListener,
11+
GoogleMap.OnCameraMoveListener,
12+
GoogleMap.OnCameraMoveStartedListener,
13+
GoogleMap.OnInfoWindowClickListener,
14+
GoogleMap.OnMarkerClickListener,
15+
GoogleMap.OnPolygonClickListener,
16+
GoogleMap.OnPolylineClickListener,
17+
GoogleMap.OnCircleClickListener,
18+
GoogleMap.OnMapClickListener,
19+
GoogleMap.OnMapLongClickListener,
20+
GoogleMap.OnMarkerDragListener {}

0 commit comments

Comments
 (0)