-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[android_intent] Migrate to the new embedding #2143
Conversation
| } | ||
|
|
||
| /** This exists for legacy compatibility purposes. */ | ||
| public static void registerWith(Registrar registrar) { |
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.
Doesn't seem to be called in this PR.
We might not need this. The io MainAcitivity can be left unchanged.
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.
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) { |
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.
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.
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 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.
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 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.
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.
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?
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.
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.
matthew-carroll
left a comment
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.
Generally LGTM. Thanks for migrating, and thanks for adding channel message tests.
...roid_intent/android/src/main/java/dev/flutter/plugins/androidintent/AndroidIntentPlugin.java
Outdated
Show resolved
Hide resolved
...roid_intent/android/src/main/java/dev/flutter/plugins/androidintent/AndroidIntentPlugin.java
Outdated
Show resolved
Hide resolved
...ges/android_intent/android/src/main/java/dev/flutter/plugins/androidintent/IntentSender.java
Show resolved
Hide resolved
| * <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) { |
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 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 { |
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.
Given that this is public, would it make sense to specify what kind of method calls this is handling? e.g., IntentMethodCallHandlerImpl?
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 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.
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.
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.
...id_intent/android/src/main/java/dev/flutter/plugins/androidintent/MethodCallHandlerImpl.java
Show resolved
Hide resolved
...id_intent/android/src/main/java/dev/flutter/plugins/androidintent/MethodCallHandlerImpl.java
Outdated
Show resolved
Hide resolved
...ntent/android/src/test/java/dev/flutter/plugins/androidintent/MethodCallHandlerImplTest.java
Outdated
Show resolved
Hide resolved
|
I updated this PR with the two extra changes we discussed offline: diff. |
matthew-carroll
left a comment
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
| @Override | ||
| public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { | ||
| sender.setApplicationContext(binding.getApplicationContext()); | ||
| sender.setActivity(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.
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...
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.
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.
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.
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
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.
cyanglaz
left a comment
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!
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.
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.
Description
Migrate android intent to the new embedding.
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.///).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?