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

Conversation

@mklim
Copy link
Contributor

@mklim mklim commented Oct 3, 2019

Description

Migrate android intent to the new embedding.

  • Refactors the existing plugin logic into two new classes.
  • Adds an implementation of the plugin for the new embedding.
  • Adds a unit test.

Related Issues

Fixes flutter/flutter#41831.

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.

}

/** This exists for legacy compatibility purposes. */
public static void registerWith(Registrar registrar) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't seem to be called in this PR.
We might not need this. The io MainAcitivity can be left unchanged.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, right. I was thinking there was some magic here but since I didn't delete the io. class this isn't necessary. Deleted.

* <p>Also resets the cached {@code activity} to null, since the given activity should no longer
* be valid after applicationContext changes.
*/
void setApplicationContext(@Nullable Context applicationContext) {
Copy link
Contributor

Choose a reason for hiding this comment

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

How do we force people to set the ApplicationContext first and then Activity?
For example:
I want to set a new application context and a new activity. I have to know to set the application context first before calling set activity.

Copy link
Contributor

Choose a reason for hiding this comment

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

I might be missing some context (no pun intended), but why would we not be able to cache both? The app context is always there. It outlives all Activitys. Then an Activity comes and goes.

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 was thinking that if the application context changed for some reason we know that any previous activity reference we've set is invalid, because the application context should outlive the activities. But @cyanglaz has a good point that a dev may try to call both and happen to call this first so this is kind of a footgun. I removed the setActivity(null) logic from here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can an application context change? I believe the application context is the Application class, which I believe is instantiated exactly one time at process launch. Do you know if that's right?

Copy link
Contributor Author

@mklim mklim Oct 4, 2019

Choose a reason for hiding this comment

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

That sounds roughly right to me, but I'm not confident enough to totally rule it changing out. I'm suspicious of what would happen in a multiprocess app. I felt like it was safer to just always treat it as changed once we got a new binding in onAttachedToEngine.

Copy link
Contributor

@matthew-carroll matthew-carroll left a comment

Choose a reason for hiding this comment

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

Generally LGTM. Thanks for migrating, and thanks for adding channel message tests.

* <p>Also resets the cached {@code activity} to null, since the given activity should no longer
* be valid after applicationContext changes.
*/
void setApplicationContext(@Nullable Context applicationContext) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I might be missing some context (no pun intended), but why would we not be able to cache both? The app context is always there. It outlives all Activitys. Then an Activity comes and goes.

import java.util.Map;

/** Forwards incoming {@link MethodCall}s to {@link IntentSender#send}. */
public class MethodCallHandlerImpl implements MethodCallHandler {
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that this is public, would it make sense to specify what kind of method calls this is handling? e.g., IntentMethodCallHandlerImpl?

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 think that's already implied from the package name, but I don't feel super strongly on this if you'd still prefer a rename.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. You may want to unify the approach on the team. The connectivity plugin chose to prefix:
#2142

Either seems acceptable, but it also seems like one of those details that doesn't have any cause to randomly change between 1P plugins.

@mklim
Copy link
Contributor Author

mklim commented Oct 4, 2019

I updated this PR with the two extra changes we discussed offline: diff.

Copy link
Contributor

@matthew-carroll matthew-carroll left a comment

Choose a reason for hiding this comment

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

LGTM

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
sender.setApplicationContext(binding.getApplicationContext());
sender.setActivity(null);
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make more senes to set activity to null in the appropriate ActivityAware methods? The execution of this method doesn't really imply anything about the Activity...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would a user potentially be changing the plugin's engine, but expecting it to hold on to an existing activity reference? I was assuming that if the engine changes, the activity itself is also invalid. I also figured that the Activity callbacks should be called anyway so I am updating the Activity there too, but I wanted to add an extra check here just to guarantee that they never got out of state based on some callback issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can add an extra call, but if that extra call ever has an effect, it indicates a bug in the embedding. The Activity calls are supposed to be strictly between engine calls:

attach engine
attach activity
detach activity
detach engine

Michael Klimushyn added 7 commits October 11, 2019 16:40
Split out the business logic into a new IntentSender class.

Split out the MethodCall parsing into MethodCallHandlerImpl.
Create a new dev.Plugin class with the new embedding. Add it to the
manifest and use it in a new dev.MainActivity.
1. Move all classes into `dev`.
2. Clean up the MethodChannel based on the engine lifecycle handlers.
@mklim mklim removed the WIP label Oct 12, 2019
Copy link
Contributor

@cyanglaz cyanglaz left a comment

Choose a reason for hiding this comment

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

LGTM!

@mklim mklim merged commit 0359295 into flutter:master Oct 14, 2019
@mklim mklim deleted the android_intent_migrate branch October 14, 2019 17:24
mormih pushed a commit to mormih/plugins that referenced this pull request Nov 17, 2019
Migrate android intent to the new embedding.

- Refactors the existing plugin logic into two new classes.
- Adds an implementation of the plugin for the new embedding.
- Adds a unit test.
sungmin-park pushed a commit to sungmin-park/flutter-plugins that referenced this pull request Dec 17, 2019
Migrate android intent to the new embedding.

- Refactors the existing plugin logic into two new classes.
- Adds an implementation of the plugin for the new embedding.
- Adds a unit test.
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.

[android_intent] Support the v2 Android embedder plugins API

4 participants