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

Add canLaunch method to url_launcher plugin #8

Merged
merged 12 commits into from
Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/url-launcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [0.3.0] - 2017-04-27

* Add `canLaunch` method.

## [0.2.0] - 2017-04-24

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.url_launcher;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;

Expand All @@ -13,34 +18,51 @@
* UrlLauncherPlugin
*/
public class UrlLauncherPlugin implements MethodCallHandler {
private FlutterActivity activity;

public static UrlLauncherPlugin register(FlutterActivity activity) {
return new UrlLauncherPlugin(activity);
}

private UrlLauncherPlugin(FlutterActivity activity) {
this.activity = activity;
new MethodChannel(
activity.getFlutterView(), "plugins.flutter.io/URLLauncher").setMethodCallHandler(this);
}

@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("UrlLauncher.launch")) {
launchURL((String) call.arguments);
result.success(null);
} else {
result.notImplemented();
private FlutterActivity activity;

public static UrlLauncherPlugin register(FlutterActivity activity) {
return new UrlLauncherPlugin(activity);
}
}
private void launchURL(String url) {
try {
Intent launchIntent = new Intent(Intent.ACTION_VIEW);
launchIntent.setData(Uri.parse(url));
activity.startActivity(launchIntent);
} catch (java.lang.Exception exception) {
// Ignore parsing or ActivityNotFound errors

private UrlLauncherPlugin(FlutterActivity activity) {
this.activity = activity;
new MethodChannel(
activity.getFlutterView(), "plugins.flutter.io/URLLauncher").setMethodCallHandler(this);
Copy link
Contributor

Choose a reason for hiding this comment

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

Decide on format for the channel name suffix: camelCase, multi_word, acronyms in lower or upper case, whatever. Just be consistent across plugins.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Choosing multi_word to be consistent with plugins in G3

}

@Override
public void onMethodCall(MethodCall call, Result result) {
String url = call.arguments();
if (call.method.equals("UrlLauncher.canLaunch")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why prefix the method names with the name of the plugin? The channel name should be enough to separate canLaunch and launch from other uses.

canLaunch(url, result);
} else if (call.method.equals("UrlLauncher.launch")) {
launchURL(url, result);
} else {
result.notImplemented();
}
}

private void launchURL(String url, Result result) {
try {
Intent launchIntent = new Intent(Intent.ACTION_VIEW);
launchIntent.setData(Uri.parse(url));
activity.startActivity(launchIntent);
result.success(null);
} catch (java.lang.Exception exception) {
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for the java.lang package name. It's implicit.

Does the code require you to catch Exception? The onMethodCall is called within a try-catch block by the framework, so any uncaught RuntimeExceptions are caught there, logged, and error results are sent back to the Dart side.

result.error("ERROR", exception.getMessage(), null);
}
}

private void canLaunch(String url, Result result) {
Intent launchIntent = new Intent(Intent.ACTION_VIEW);
launchIntent.setData(Uri.parse(url));
ComponentName componentName = launchIntent.resolveActivity(activity.getPackageManager());
if (componentName == null ||
"{com.android.fallback/com.android.fallback.Fallback}".
equals(componentName.toShortString())) {
result.success(false);
} else {
result.success(true);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: if (bExp) { xxx(true); } else { xxx(false); } should often be replaced by xxx(bExp);

Here, bExp is quite long and so it may be more readable to do

boolean answer = bExp;
xxx(answer);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course!

}
}
}
2 changes: 1 addition & 1 deletion packages/url-launcher/example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:2.3.1'
Copy link
Member

Choose a reason for hiding this comment

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

Should keep this at the same version as our templates

}
}

Expand Down
102 changes: 51 additions & 51 deletions packages/url-launcher/example/ios/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading