Thanks to visit codestin.com
Credit goes to github.com

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

[google_maps_flutter] Clean up google_maps_flutter plugin #3206

Merged
merged 15 commits into from
Oct 27, 2020

Conversation

math1man
Copy link
Contributor

@math1man math1man commented Oct 26, 2020

Description

Clean up google_maps_flutter plugin. This change should not have any functional differences.

  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 derived from registrar.getActivity(). 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.

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.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (flutter analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate a breaking change in CHANGELOG.md and increment major revision).
  • No, this is not a breaking change.

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.
@google-cla google-cla bot added the cla: yes label Oct 26, 2020
@math1man math1man marked this pull request as ready for review October 26, 2020 22:10
@math1man math1man requested a review from cyanglaz as a code owner October 26, 2020 22:10
Copy link
Contributor

@amirh amirh left a 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;
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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;
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

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;
Copy link
Contributor

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)

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

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.
@math1man
Copy link
Contributor Author

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?

I updated the description to clarify that the value is derived from registrar.getActivity(). What I had been referring to was the legacy new GoogleMapFactory call site in GoogleMapsPlugin#registerWith passed in a value of -1.

@@ -880,7 +852,11 @@ private int getActivityHashCode() {
if (registrar != null && registrar.activity() != null) {
Copy link
Contributor

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.
Copy link
Contributor

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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@amirh amirh left a 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.

@tarektaamali
Copy link

please look at this issue has opened more than a year ago .you should fixed .it's happened with all version of the packages

@math1man
Copy link
Contributor Author

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
Copy link
Contributor

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;

Copy link
Contributor Author

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.

Copy link
Contributor

@amirh amirh left a 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.

@math1man math1man requested a review from amirh October 27, 2020 18:23
@amirh amirh merged commit 81bbcd4 into flutter:master Oct 27, 2020
math1man added a commit to math1man/plugins that referenced this pull request Oct 27, 2020
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.
math1man added a commit to math1man/plugins that referenced this pull request Oct 27, 2020
math1man added a commit to math1man/plugins that referenced this pull request Oct 27, 2020
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.
math1man added a commit to math1man/plugins that referenced this pull request Oct 27, 2020
yasargil added a commit to yasargil/plugins that referenced this pull request Oct 30, 2020
* 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)
  ...
FlutterSu pushed a commit to FlutterSu/flutter-plugins that referenced this pull request Nov 20, 2020
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.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants