blob: 347a6f68af334b55af51c0634924b6b8f45f069f [file] [log] [blame]
// 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;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodCall;
/**
* 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/url_launcher").setMethodCallHandler(this);
}
@Override
public void onMethodCall(MethodCall call, Result result) {
String url = call.arguments();
if (call.method.equals("canLaunch")) {
canLaunch(url, result);
} else if (call.method.equals("launch")) {
launchURL(url, result);
} else {
result.notImplemented();
}
}
private void launchURL(String url, Result result) {
Intent launchIntent = new Intent(Intent.ACTION_VIEW);
launchIntent.setData(Uri.parse(url));
activity.startActivity(launchIntent);
result.success(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());
boolean canLaunch = componentName != null &&
!"{com.android.fallback/com.android.fallback.Fallback}".
equals(componentName.toShortString());
result.success(canLaunch);
}
}