// 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.urllauncher;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import io.flutter.plugin.common.MethodCall;
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.PluginRegistry.Registrar;

/** UrlLauncherPlugin */
public class UrlLauncherPlugin implements MethodCallHandler {
  private final Activity activity;

  public static void registerWith(Registrar registrar) {
    MethodChannel channel =
        new MethodChannel(registrar.messenger(), "plugins.flutter.io/url_launcher");
    UrlLauncherPlugin instance = new UrlLauncherPlugin(registrar.activity());
    channel.setMethodCallHandler(instance);
  }

  private UrlLauncherPlugin(Activity activity) {
    this.activity = activity;
  }

  @Override
  public void onMethodCall(MethodCall call, Result result) {
    String url = call.argument("url");
    if (call.method.equals("canLaunch")) {
      canLaunch(url, result);
    } else if (call.method.equals("launch")) {
      Intent launchIntent;
      boolean useWebView = call.argument("useWebView");
      if (useWebView) {
        launchIntent = new Intent(activity, WebViewActivity.class);
        launchIntent.putExtra("url", url);
      } else {
        launchIntent = new Intent(Intent.ACTION_VIEW);
        launchIntent.setData(Uri.parse(url));
      }
      activity.startActivity(launchIntent);
      result.success(null);
    } else {
      result.notImplemented();
    }
  }

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

  /*  Launches WebView activity */
  public static class WebViewActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      WebView webview = new WebView(this);
      setContentView(webview);
      // Get the Intent that started this activity and extract the string
      Intent intent = getIntent();
      String url = intent.getStringExtra("url");
      webview.loadUrl(url);
    }
  }
}

@@ -367,22 +367,131 @@
 oid.
-webkit.WebView
+view.KeyEvent;%0Aimport android.webkit.WebResourceRequest;%0Aimport android.webkit.WebView;%0Aimport android.webkit.WebViewClient
 ;%0Aim
@@ -2519,16 +2519,45 @@
 tivity %7B
+%0A    private WebView webview;
 %0A%0A    @O
@@ -2666,24 +2666,16 @@
 ;%0A      
-WebView 
 webview 
@@ -2880,24 +2880,24 @@
 tra(%22url%22);%0A
-
       webvie
@@ -2908,24 +2908,620 @@
 adUrl(url);%0A
+      // Open new urls inside the webview itself.%0A      webview.setWebViewClient(%0A          new WebViewClient() %7B%0A            @Override%0A            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) %7B%0A              view.loadUrl(request.getUrl().toString());%0A              return false;%0A            %7D%0A          %7D);%0A    %7D%0A%0A    @Override%0A    public boolean onKeyDown(int keyCode, KeyEvent event) %7B%0A      if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) %7B%0A        webview.goBack();%0A        return true;%0A      %7D%0A      return super.onKeyDown(keyCode, event);%0A
     %7D%0A  %7D%0A%7D%0A

Handle URL links within the webview. Also handle history properly. (#284)