-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Add support for Android explicit intents #332
Conversation
d0c024d
to
892401e
Compare
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
intent.setPackage((String) call.argument("package")); | ||
} | ||
// If we cannot resolve an explicit intent fallback to an implicit one | ||
if (intent.getPackage() != 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.
Can we merge this conditional with the previous one like so:
if (call.argument("package") != null) {
intent.setPackage((String) call.argument("package"));
if (intent.resolveActivity(context.getPackageManager()) == null) {
intent.setPackage(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.
Yes. Good catch, easier to reason about.
// If we cannot resolve an explicit intent fallback to an implicit one | ||
if (intent.getPackage() != null | ||
&& intent.resolveActivity(context.getPackageManager()) == null) { | ||
intent.setPackage(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.
Log that we're ignoring the package?
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.
Added
onPressed: _openLinkInGoogleChrome, | ||
), | ||
], | ||
)); |
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.
Add trailing comma here to avoid extra indentation above.
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.
Added
@@ -31,6 +32,7 @@ class AndroidIntent { | |||
this.category, |
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 believe we'd could help dartfmt
produce something nicer with a trailing comma here:
const AndroidIntent({
@required this.action,
this.category,
this.data,
this.arguments,
this.package,
Platform platform,
})
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! The more I use Dart the more I'm becoming a trailing comma enthusiast :-)
package: 'com.android.chrome'); | ||
intent.launch(); | ||
} | ||
|
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.
Great examples, thanks!
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've restructured the example a bit to have the explicit intent test buttons grouped together in a separate view. It'll be easier for future tests to be added to the main view, instead of filling it up with explicit intent test buttons.
@@ -106,7 +108,10 @@ private boolean isStringKeyedMap(Object value) { | |||
|
|||
@Override | |||
public void onMethodCall(MethodCall call, Result result) { | |||
Context context = |
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 this logic be a candidate for a Context activeContext()
or similarly named method on 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.
Yes. Like so alibitek/engine@4f26a92 ?
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 it would be useful to have an activeContext()
method in the Registrar itself, as the way to obtain a context is common among plugins.
It would reduce the need to repeat this code. What do you think?
In the meantime, I've added an activeContext method to the android_intent
plugin.
Should I proceed with submitting the activeContext to the flutter/engine project? Any other changes there?
On iOS, the FlutterPluginRegistrar protocol doesn't have the same functionality, so no changes there.
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.
@alibitek I think it would make sense to add it, if
- Plugin authors often find themselves in a situation where they need a context and where it should be the current
Activity
, if one exists, but can live with theApplication
, if not; and - We can't just make the existing
context()
method return the active context---presumable because other callers need the returned context to be theApplication
.
If you think both of the above apply, then we'd much appreciate you moving forward with a flutter/engine
PR.
Adding an activeContext
method to the Registrar
interface is a breaking change whose impact is limited since we provide the default implementation of that interface ourselves. Still, we need to announce it to flutter-dev
.
Alternatively, we could create a utility class for such extensions:
public final class RegistrarExt implements Registrar {
private final Registrar registrar;
public RegistrarExt(Registrar registrar) {
this.registrar = registrar;
}
public Context activeContext() {
final Activity activity = registrar.activity();
return (activity == null) ? registrar.context() : activity;
}
// + delegate implementations of Registrar methods.
}
This would make sense if we expect additional extension methods to accrue over time. A more modern alternative is to use default methods, but that is Java 8 syntax, and not supported prior to Android SDK N, AFAIK.
Thanks @alibitek! Can you take a quick look at the review feedback, please? |
body = new Padding( | ||
return new Scaffold( | ||
appBar: new AppBar( | ||
title: const Text('Test explict intents'), |
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.
explict -> explicit
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.
Fixed
@@ -104,16 +155,15 @@ class MyHomePage extends StatelessWidget { | |||
child: const Text('Tap here to open link in Google Chrome.'), | |||
onPressed: _openLinkInGoogleChrome, | |||
), | |||
new RaisedButton( | |||
child: const Text( | |||
'Tap here to test explict intent fallback to implicit.'), |
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.
explict -> explicit
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.
Fixed
eadd522
to
d66a6dc
Compare
@mravn-google is this good to merge? |
LGTM, @alibitek are you done? |
With this pull request, yes. I need some guidance regarding #332 (comment) |
Allows to specify an explicit package name when sending an intent.
If there is no package installed in the system with that name or the package cannot handle the intent, it'll fallback to an implicit intent by removing the specified package from the intent.