This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[google_maps_flutter] Overhaul lifecycle management in GoogleMapsPlugin #3213
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c977679
GoogleMapController is now uniformly driven by implementing DefaultLi…
math1man e934a18
Update CHANGELOG.md and version
math1man 9bdc386
Merge remote-tracking branch 'origin/master'
math1man 96cf26b
Fix formatting issues
math1man 62476dc
Update GoogleMapControllerTest
math1man a8e02a4
Merge remote-tracking branch 'origin/master'
math1man b920973
Remove extraneous import
math1man ea016b8
Update GoogleMapsPlugin.ProxyLifecycleProvider to use handleLifecycle…
math1man d6c9014
Merge remote-tracking branch 'origin/master'
math1man 5f479f0
Update version from 1.1.0 to 1.0.5
math1man 2c182a9
Merge remote-tracking branch 'origin/master'
math1man 039e3ce
Add javadoc for ProxyLifecycleProvider and move initial ActivityLifec…
math1man 85e592e
Merge remote-tracking branch 'origin/master'
math1man df2345d
Remove LifecycleProvider as an interface of GoogleMapsPlugin and inst…
math1man File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,6 @@ | |
|
||
import android.Manifest; | ||
import android.annotation.SuppressLint; | ||
import android.app.Activity; | ||
import android.app.Application; | ||
import android.content.Context; | ||
import android.content.pm.PackageManager; | ||
import android.graphics.Bitmap; | ||
|
@@ -19,7 +17,6 @@ | |
import androidx.annotation.Nullable; | ||
import androidx.lifecycle.DefaultLifecycleObserver; | ||
import androidx.lifecycle.Lifecycle; | ||
import androidx.lifecycle.Lifecycle.State; | ||
import androidx.lifecycle.LifecycleOwner; | ||
import com.google.android.gms.maps.CameraUpdate; | ||
import com.google.android.gms.maps.GoogleMap; | ||
|
@@ -39,7 +36,6 @@ | |
import io.flutter.plugin.common.BinaryMessenger; | ||
import io.flutter.plugin.common.MethodCall; | ||
import io.flutter.plugin.common.MethodChannel; | ||
import io.flutter.plugin.common.PluginRegistry; | ||
import io.flutter.plugin.platform.PlatformView; | ||
import java.io.ByteArrayOutputStream; | ||
import java.util.ArrayList; | ||
|
@@ -50,8 +46,7 @@ | |
|
||
/** Controller of a single GoogleMaps MapView instance. */ | ||
final class GoogleMapController | ||
implements Application.ActivityLifecycleCallbacks, | ||
DefaultLifecycleObserver, | ||
implements DefaultLifecycleObserver, | ||
ActivityPluginBinding.OnSaveInstanceStateListener, | ||
GoogleMapOptionsSink, | ||
MethodChannel.MethodCallHandler, | ||
|
@@ -75,12 +70,8 @@ final class GoogleMapController | |
private boolean disposed = false; | ||
private final float density; | ||
private MethodChannel.Result mapReadyResult; | ||
@Nullable private final Lifecycle lifecycle; | ||
private final Context context; | ||
// Do not use directly, use getApplication() instead to get correct application object for both v1 | ||
// and v2 embedding. | ||
@Nullable private final Application mApplication; | ||
@Nullable private final PluginRegistry.Registrar registrar; // For v1 embedding only. | ||
private final LifecycleProvider lifecycleProvider; | ||
private final MarkersController markersController; | ||
private final PolygonsController polygonsController; | ||
private final PolylinesController polylinesController; | ||
|
@@ -94,9 +85,7 @@ final class GoogleMapController | |
int id, | ||
Context context, | ||
BinaryMessenger binaryMessenger, | ||
@Nullable Application application, | ||
@Nullable Lifecycle lifecycle, | ||
@Nullable PluginRegistry.Registrar registrar, | ||
LifecycleProvider lifecycleProvider, | ||
GoogleMapOptions options) { | ||
this.id = id; | ||
this.context = context; | ||
|
@@ -105,9 +94,7 @@ final class GoogleMapController | |
this.density = context.getResources().getDisplayMetrics().density; | ||
methodChannel = new MethodChannel(binaryMessenger, "plugins.flutter.io/google_maps_" + id); | ||
methodChannel.setMethodCallHandler(this); | ||
mApplication = application; | ||
this.lifecycle = lifecycle; | ||
this.registrar = registrar; | ||
this.lifecycleProvider = lifecycleProvider; | ||
this.markersController = new MarkersController(methodChannel); | ||
this.polygonsController = new PolygonsController(methodChannel, density); | ||
this.polylinesController = new PolylinesController(methodChannel, density); | ||
|
@@ -119,30 +106,8 @@ public View getView() { | |
return mapView; | ||
} | ||
|
||
void init(State lifecycleState) { | ||
switch (lifecycleState) { | ||
case RESUMED: | ||
mapView.onCreate(null); | ||
mapView.onStart(); | ||
mapView.onResume(); | ||
break; | ||
case STARTED: | ||
mapView.onCreate(null); | ||
mapView.onStart(); | ||
break; | ||
case CREATED: | ||
mapView.onCreate(null); | ||
break; | ||
case DESTROYED: | ||
case INITIALIZED: | ||
// Nothing to do, the activity has been completely destroyed or not yet created. | ||
break; | ||
} | ||
if (lifecycle != null) { | ||
lifecycle.addObserver(this); | ||
} else { | ||
getApplication().registerActivityLifecycleCallbacks(this); | ||
} | ||
void init() { | ||
lifecycleProvider.getLifecycle().addObserver(this); | ||
mapView.getMapAsync(this); | ||
} | ||
|
||
|
@@ -507,7 +472,10 @@ public void dispose() { | |
methodChannel.setMethodCallHandler(null); | ||
setGoogleMapListener(null); | ||
destroyMapViewIfNecessary(); | ||
getApplication().unregisterActivityLifecycleCallbacks(this); | ||
Lifecycle lifecycle = lifecycleProvider.getLifecycle(); | ||
if (lifecycle != null) { | ||
lifecycle.removeObserver(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (not sure) do we need to protect against calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, operation is idempotent |
||
} | ||
} | ||
|
||
private void setGoogleMapListener(@Nullable GoogleMapListener listener) { | ||
|
@@ -537,64 +505,7 @@ public void onInputConnectionUnlocked() { | |
// TODO(mklim): Remove this empty override once https://github.com/flutter/flutter/issues/40126 is fixed in stable. | ||
} | ||
|
||
// Application.ActivityLifecycleCallbacks methods | ||
@Override | ||
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { | ||
if (disposed || activity.hashCode() != getActivityHashCode()) { | ||
return; | ||
} | ||
mapView.onCreate(savedInstanceState); | ||
} | ||
|
||
@Override | ||
public void onActivityStarted(Activity activity) { | ||
if (disposed || activity.hashCode() != getActivityHashCode()) { | ||
return; | ||
} | ||
mapView.onStart(); | ||
} | ||
|
||
@Override | ||
public void onActivityResumed(Activity activity) { | ||
if (disposed || activity.hashCode() != getActivityHashCode()) { | ||
return; | ||
} | ||
mapView.onResume(); | ||
} | ||
|
||
@Override | ||
public void onActivityPaused(Activity activity) { | ||
if (disposed || activity.hashCode() != getActivityHashCode()) { | ||
return; | ||
} | ||
mapView.onPause(); | ||
} | ||
|
||
@Override | ||
public void onActivityStopped(Activity activity) { | ||
if (disposed || activity.hashCode() != getActivityHashCode()) { | ||
return; | ||
} | ||
mapView.onStop(); | ||
} | ||
|
||
@Override | ||
public void onActivitySaveInstanceState(Activity activity, Bundle outState) { | ||
if (disposed || activity.hashCode() != getActivityHashCode()) { | ||
return; | ||
} | ||
mapView.onSaveInstanceState(outState); | ||
} | ||
|
||
@Override | ||
public void onActivityDestroyed(Activity activity) { | ||
if (disposed || activity.hashCode() != getActivityHashCode()) { | ||
return; | ||
} | ||
destroyMapViewIfNecessary(); | ||
} | ||
|
||
// DefaultLifecycleObserver and OnSaveInstanceStateListener | ||
// DefaultLifecycleObserver | ||
|
||
@Override | ||
public void onCreate(@NonNull LifecycleOwner owner) { | ||
|
@@ -638,6 +549,7 @@ public void onStop(@NonNull LifecycleOwner owner) { | |
|
||
@Override | ||
public void onDestroy(@NonNull LifecycleOwner owner) { | ||
owner.getLifecycle().removeObserver(this); | ||
if (disposed) { | ||
return; | ||
} | ||
|
@@ -848,24 +760,6 @@ private int checkSelfPermission(String permission) { | |
permission, android.os.Process.myPid(), android.os.Process.myUid()); | ||
} | ||
|
||
private int getActivityHashCode() { | ||
if (registrar != null && registrar.activity() != null) { | ||
return registrar.activity().hashCode(); | ||
} else { | ||
// TODO(cyanglaz): Remove `getActivityHashCode()` and use a cached hashCode when creating the view for V1 embedding. | ||
// https://github.com/flutter/flutter/issues/69128 | ||
return -1; | ||
} | ||
} | ||
|
||
private Application getApplication() { | ||
if (registrar != null && registrar.activity() != null) { | ||
return registrar.activity().getApplication(); | ||
} else { | ||
return mApplication; | ||
} | ||
} | ||
|
||
private void destroyMapViewIfNecessary() { | ||
if (mapView == null) { | ||
return; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure so double checking - will we get the initial notification for the current state?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, from
addObserver
's documentation: