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

Commit 66892d1

Browse files
authored
Add canLaunch method to url_launcher plugin (#8)
1 parent 7372c60 commit 66892d1

File tree

10 files changed

+198
-106
lines changed

10 files changed

+198
-106
lines changed

packages/url-launcher/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## [0.3.0] - 2017-04-27
2+
3+
* Add `canLaunch` method.
14

25
## [0.2.0] - 2017-04-24
36

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
15
package io.flutter.plugins.url_launcher;
26

7+
import android.content.ComponentName;
38
import android.content.Intent;
49
import android.net.Uri;
510

@@ -13,34 +18,45 @@
1318
* UrlLauncherPlugin
1419
*/
1520
public class UrlLauncherPlugin implements MethodCallHandler {
16-
private FlutterActivity activity;
17-
18-
public static UrlLauncherPlugin register(FlutterActivity activity) {
19-
return new UrlLauncherPlugin(activity);
20-
}
21-
22-
private UrlLauncherPlugin(FlutterActivity activity) {
23-
this.activity = activity;
24-
new MethodChannel(
25-
activity.getFlutterView(), "plugins.flutter.io/URLLauncher").setMethodCallHandler(this);
26-
}
27-
28-
@Override
29-
public void onMethodCall(MethodCall call, Result result) {
30-
if (call.method.equals("UrlLauncher.launch")) {
31-
launchURL((String) call.arguments);
32-
result.success(null);
33-
} else {
34-
result.notImplemented();
21+
private FlutterActivity activity;
22+
23+
public static UrlLauncherPlugin register(FlutterActivity activity) {
24+
return new UrlLauncherPlugin(activity);
25+
}
26+
27+
private UrlLauncherPlugin(FlutterActivity activity) {
28+
this.activity = activity;
29+
new MethodChannel(
30+
activity.getFlutterView(), "plugins.flutter.io/url_launcher").setMethodCallHandler(this);
31+
}
32+
33+
@Override
34+
public void onMethodCall(MethodCall call, Result result) {
35+
String url = call.arguments();
36+
if (call.method.equals("canLaunch")) {
37+
canLaunch(url, result);
38+
} else if (call.method.equals("launch")) {
39+
launchURL(url, result);
40+
} else {
41+
result.notImplemented();
42+
}
43+
}
44+
45+
private void launchURL(String url, Result result) {
46+
Intent launchIntent = new Intent(Intent.ACTION_VIEW);
47+
launchIntent.setData(Uri.parse(url));
48+
activity.startActivity(launchIntent);
49+
result.success(null);
3550
}
36-
}
37-
private void launchURL(String url) {
38-
try {
39-
Intent launchIntent = new Intent(Intent.ACTION_VIEW);
40-
launchIntent.setData(Uri.parse(url));
41-
activity.startActivity(launchIntent);
42-
} catch (java.lang.Exception exception) {
43-
// Ignore parsing or ActivityNotFound errors
51+
52+
private void canLaunch(String url, Result result) {
53+
Intent launchIntent = new Intent(Intent.ACTION_VIEW);
54+
launchIntent.setData(Uri.parse(url));
55+
ComponentName componentName = launchIntent.resolveActivity(activity.getPackageManager());
56+
57+
boolean canLaunch = componentName != null &&
58+
!"{com.android.fallback/com.android.fallback.Fallback}".
59+
equals(componentName.toShortString());
60+
result.success(canLaunch);
4461
}
45-
}
4662
}

packages/url-launcher/example/ios/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 51 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/url-launcher/example/ios/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/url-launcher/example/ios/Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/url-launcher/example/ios/Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)