-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[google_maps_flutter] Clean up google_maps_flutter plugin #3206
Conversation
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.
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.
Thanks! and thanks for the great PR description, it really helps walking through this PR.
Left a few nits, overall looks good.
One thing to check - in the PR description it says that activityHashCode is -1
for the legacy plugin, my understanding is that it is -1
for the v2 (new) embedding, am I correct?
@@ -7,13 +7,14 @@ | |||
import android.app.Application; | |||
import android.content.Context; | |||
import android.graphics.Rect; | |||
import androidx.annotation.Nullable; |
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.
Do we need to add androidx.annotation
to the plugin's build.gradle?
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.
Various classes in the plugin already depend on this annotation or the NonNull equivalent, so it appears not.
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.
Probably wrong that we didn't have it so far, I think these get brought in by the Android embedding right now, though for consistency with other dependencies and for future compatibility (in the very unlikely event that the embedding is going to drop the dependency) I'd be explicit about the dependencies.
@@ -880,7 +856,7 @@ private int getActivityHashCode() { | |||
if (registrar != null && registrar.activity() != null) { | |||
return registrar.activity().hashCode(); | |||
} else { | |||
return activityHashCode; | |||
return -1; |
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.
Let's add a comment here explaining that registrar is null for the v2 embedding where the activityHashCode shouldn't be used. If we're certain this isn't being I think we better throw - we better get a clear crash than some strange value that may lead to easy to understand bugs.
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.
Done
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.
Though it's worth noting that this could be considered a breaking change.
@@ -552,18 +524,22 @@ private void setGoogleMapListener(@Nullable GoogleMapListener listener) { | |||
} | |||
|
|||
// @Override | |||
// The minimum supported version of Flutter doesn't have this method on the PlatformView interface, but the maximum | |||
// does. This will override it when available even with the annotation commented out. | |||
// The minimum supported version of Flutter doesn't have this method on the PlatformView |
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.
nit: Please avoid unneeded reformats to keep cleaner blame data.
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.
Done
@@ -0,0 +1,16 @@ | |||
package io.flutter.plugins.googlemaps; |
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.
Missing the license header (you can copy from any other file)
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.
Done, thanks
|
||
import com.google.android.gms.maps.GoogleMap; | ||
|
||
interface GoogleMapListener |
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.
I'm fine with moving this here if that's how you prefer it, curious though - is there a reference to a style guide / best practices doc with the determination that multiple top level classes is bad?
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.
I can't find any formal documentation, but it can cause compiler problems. See discussion in https://stackoverflow.com/q/2336692 for example.
1. Add license to GoogleMapListener.java 2. Revert some changes to comment linebreaking in GoogleMapController 3. In GoogleMapController#getActivityHashCode, throw an exception if the hash code cannot be obtained.
I updated the description to clarify that the value is derived from |
@@ -880,7 +852,11 @@ private int getActivityHashCode() { | |||
if (registrar != null && registrar.activity() != null) { |
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.
Mmmm staring at this snippet a little more I'm worried this may crash with the old embedding when the activity is in the background and some lifecycle event for the same application happens.
We used to cache the activityHashCode, and we stopped caching at #2488 cc @cyanglaz do you remember the context for this change? was it safe to stop caching the activity hashCode in that PR?
@@ -1,3 +1,7 @@ | |||
## 1.0.4 | |||
|
|||
* Minor cleanup of Android code. |
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.
nit: call out the specific things that were done (especially the leak fix).
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.
Done
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.
Thanks! this looks good, I'm waiting to hear from @cyanglaz regarding the activityHashCode before landing to make sure we're not introducing a crash with the activityHashCode.
please look at this issue has opened more than a year ago .you should fixed .it's happened with all version of the packages |
Thanks for the pointer. I don't think this change will address that, but I have some theories on how it might be fixed. I'll look into it in another pull request. |
@@ -880,7 +852,11 @@ private int getActivityHashCode() { | |||
if (registrar != null && registrar.activity() != null) { | |||
return registrar.activity().hashCode(); | |||
} else { | |||
return activityHashCode; | |||
// This method should only be invoked in cases where registrar != null, and only at lifecycle |
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.
This exception can actually throw when some lifecycle event happened when the activity is in the background as mentioned in https://github.com/flutter/plugins/pull/3206/files#r512392320
It is a regression caused by #2488. We should fix it by removing the getActivityHashCode()
and use a cached hashCode value like how it was.
I created an issue to track this fix: flutter/flutter#69128
@math1man Do you mind reverting it back to return -1 here and add a TODO to fix the regression?
Something like:
// TODO(cyanglaz): Remove the `getActivityHashCode()` and use a cached hashCode when creating the web view for V1 embedding.
// https://github.com/flutter/flutter/issues/69128
return activityHashCode;
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.
Done, thanks for creating that.
…accessed. Updating this behavior will be tracked in flutter/flutter#69128
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.
LGTM
Thanks!
Will land on green.
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.
…utter#3206)" This reverts commit a98bc81
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.
…utter#3206)" This reverts commit a98bc81
* master: (48 commits) [video_player] Add toString() to Caption (flutter#3233) [google_maps_flutter_web] Show one InfoWindow at a time. (flutter#3224) [in_app_purchase] Bump version (flutter#3227) [google_maps_flutter] Overhaul lifecycle management in GoogleMapsPlugin (flutter#3213) [in_app_purchase] Remove the custom analysis options, fix failing lints. (flutter#3220) [video_player]Fixes Playing video from asset on Android (flutter#3123) [camera] Added documentation about video not working correctly on Android emulators (flutter#3180) Revert "update api" update api [wifi_info_flutter] Method channel name fixed for android (flutter#3207) [share] Fix bug on iPad where `origin` is null and enable XCUITests in the repo (flutter#3210) [google_maps_flutter] Clean up google_maps_flutter plugin (flutter#3206) Exclude generated_plugin_registrant.cc (flutter#3198) broaden the constraint on package:vm_service (flutter#3208) Remove unnecessary work around from test in prep for vm_service migration (flutter#3209) Add windows directory to examples (flutter#3149) [video_player] Upgrade ExoPlayer (flutter#3204) [android_alarm_manager] Removed deprecated display1 (flutter#3200) [Connectivity] wifi removal (flutter#3173) [wifi_info_flutter] make it ready for initial 1.0.0 release (flutter#3191) ...
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.
Description
Clean up google_maps_flutter plugin. This change should not have any functional differences.
registrar.getActivity()
. In the new plugin use case, the value should never be used because it uses DefaultLifecycleCallbacks instead of ActivityLifecycleCallbacks.Checklist
Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]
). This will ensure a smooth and quick review process.///
).flutter analyze
) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?