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

Add support for Android explicit intents #332

Merged
merged 4 commits into from
Jan 4, 2018

Conversation

alibitek
Copy link
Contributor

@alibitek alibitek commented Dec 31, 2017

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.

@alibitek alibitek force-pushed the android_intent_package branch from d0c024d to 892401e Compare January 3, 2018 11:58
Copy link
Contributor

@mravn-google mravn-google left a 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
Copy link
Contributor

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);
  }
}

Copy link
Contributor Author

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

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

onPressed: _openLinkInGoogleChrome,
),
],
));
Copy link
Contributor

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.

Copy link
Contributor Author

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

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,
})

Copy link
Contributor Author

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();
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Great examples, thanks!

Copy link
Contributor Author

@alibitek alibitek Jan 4, 2018

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

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?

Copy link
Contributor Author

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 ?

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

Copy link
Contributor

@mravn-google mravn-google Jan 8, 2018

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

  1. 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 the Application, if not; and
  2. We can't just make the existing context() method return the active context---presumable because other callers need the returned context to be the Application.

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.

@mit-mit
Copy link
Member

mit-mit commented Jan 4, 2018

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'),
Copy link
Contributor

Choose a reason for hiding this comment

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

explict -> explicit

Copy link
Contributor Author

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

Choose a reason for hiding this comment

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

explict -> explicit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@alibitek alibitek force-pushed the android_intent_package branch from eadd522 to d66a6dc Compare January 4, 2018 13:13
@mit-mit
Copy link
Member

mit-mit commented Jan 4, 2018

@mravn-google is this good to merge?

@mravn-google
Copy link
Contributor

LGTM, @alibitek are you done?

@alibitek
Copy link
Contributor Author

alibitek commented Jan 4, 2018

With this pull request, yes. I need some guidance regarding #332 (comment)

@mravn-google mravn-google merged commit e23ed1c into flutter:master Jan 4, 2018
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