Skip to content

Commit

Permalink
Added ability to handle new open windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolesnyk Sviatoslav committed Mar 21, 2017
1 parent a1ffad7 commit 164863d
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Threading;
using HybridWebControl.Droid;
using HybridWebControl;
using XLabs.Platform;

[assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))]
namespace HybridWebControl.Droid
Expand All @@ -31,6 +32,7 @@ public class HybridWebViewRenderer : ViewRenderer<HybridWebView, WebPlatformNati
public event Action<Uri> PageLoadFinished;
public event Action<Uri, string, int> PageLoadError;
public event Action<string> JavascriptExecuted;
public event Action<Uri> PageLoadInNewWindowRequest;

private const string NativeFuncCall = "Xamarin.call";
private const string NativeFunction = "function Native(action, data){Xamarin.call(JSON.stringify({ a: action, d: data }));}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace HybridWebControl.Droid
{
public class WebPlatformChromeClient : WebChromeClient
{
public event Action<Uri> OpenExternalWindow;

Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback;

public WebPlatformChromeClient(Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback)
Expand All @@ -25,5 +27,15 @@ public override void OnGeolocationPermissionsShowPrompt(string origin, Geolocati
{
callback.Invoke(origin, true, false);
}

public override bool OnCreateWindow(WebView view, bool isDialog, bool isUserGesture, Android.OS.Message resultMsg)
{
WebView newWebView = new WebView(view.Context);
newWebView.SetWebViewClient(new WebPlatformNewWindowViewClient(OpenExternalWindow));
WebView.WebViewTransport transport = (WebView.WebViewTransport)resultMsg.Obj;
transport.WebView = newWebView;
resultMsg.SendToTarget();
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using Android.Webkit;

namespace HybridWebControl.Droid
{
public class WebPlatformNewWindowViewClient : WebViewClient
{
Action<Uri> openExternalLink;

public WebPlatformNewWindowViewClient(Action<Uri> openExternalLink)
{
this.openExternalLink = openExternalLink;
}

public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
if (openExternalLink != null)
{
openExternalLink(new Uri(url));
}
view.Dispose();
return false;
}
}
}
1 change: 1 addition & 0 deletions HybridWebControl.Droid/HybridWebControl.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<Compile Include="ControlRenderers\HybridWebView\WebPlatformNativeView.cs" />
<Compile Include="ControlRenderers\HybridWebView\WebPlatformViewClient.cs" />
<Compile Include="ControlRenderers\HybridWebView\WebPlatformXamarinApi.cs" />
<Compile Include="ControlRenderers\HybridWebView\WebPlatformNewWindowViewClient.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class HybridWebViewRenderer : ViewRenderer<HybridWebView, WKWebView>, IWK
public event Action<Uri> PageLoadFinished;
public event Action<Uri, string, int> PageLoadError;
public event Action<string> JavascriptExecuted;
public event Action<Uri> PageLoadInNewWindowRequest;

private WKUserContentController userController;

Expand Down Expand Up @@ -145,6 +146,7 @@ private WebPlatformNavigationDelegate CreateNavidationalDelagate()
navigationDelegate.FinishedLoadingUrl += NavigationDelegate_FinishedLoadingUrl;
navigationDelegate.StartLoadingUrl += NavigationDelegate_StartLoadingUrl;
navigationDelegate.ShouldStartPageLoading += NavigationDelegate_ShouldStartPageLoading;
navigationDelegate.OpenExternalWindow += NavigationDelegate_OpenExternalWindow;

return navigationDelegate;
}
Expand Down Expand Up @@ -181,5 +183,13 @@ private bool NavigationDelegate_ShouldStartPageLoading(string arg)
}
return true;
}

private void NavigationDelegate_OpenExternalWindow(Uri obj)
{
if (PageLoadInNewWindowRequest != null)
{
this.PageLoadInNewWindowRequest(obj);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class WebPlatformNavigationDelegate : WKNavigationDelegate
public event Action<string> StartLoadingUrl;
public event Action<string> FinishedLoadingUrl;
public event Func<string, bool> ShouldStartPageLoading;
public event Action<Uri> OpenExternalWindow;

public WebPlatformNavigationDelegate()
{
Expand All @@ -19,6 +20,15 @@ public override void DecidePolicy(WKWebView webView, WKNavigationAction navigati
{
var action = WKNavigationActionPolicy.Allow;

if (navigationAction.TargetFrame == null)
{
if (OpenExternalWindow != null)
{
OpenExternalWindow(new Uri(navigationAction.Request.Url.AbsoluteString));
}
decisionHandler(WKNavigationActionPolicy.Cancel);
}

if (ShouldStartPageLoading != null)
{
action = ShouldStartPageLoading(navigationAction.Request.Url.AbsoluteString) ? WKNavigationActionPolicy.Allow : WKNavigationActionPolicy.Cancel;
Expand Down
11 changes: 11 additions & 0 deletions HybridWebControl/Control/HybridWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public string CurrentHashAnchor
public event Action<Uri> PageLoadStarted;
public event Action<Uri> PageLoadFinished;
public event Action<Uri, string, int> PageLoadError;
public event Action<Uri> NewWebBrowserWindowOpenRequest;

//Required Js actions

Expand Down Expand Up @@ -179,6 +180,7 @@ public void SetWebActionSource(IHybridWebViewActionSource source)
this.actionSource.PageLoadStarted += this.PageLoadStarted;
this.actionSource.PageLoadFinished += this.PageLoadFinished;
this.actionSource.PageLoadError += this.PageLoadError;
this.actionSource.PageLoadInNewWindowRequest += this.NewWebBrowserWindowOpenRequest;
}

internal bool TryGetAction(string name, out Action<string> action)
Expand Down Expand Up @@ -228,6 +230,14 @@ internal void HashAnchorChangedCallback(string newHash)
}
}

internal void OnNewWebBrowserWindowOpenRequest(Uri uri)
{
if (NewWebBrowserWindowOpenRequest != null)
{
NewWebBrowserWindowOpenRequest(uri);
}
}

internal static string GetInitialJsScript(string nativeFunction)
{
var builder = new StringBuilder();
Expand Down Expand Up @@ -256,6 +266,7 @@ private void UnregisterOldWebActionSource()
this.actionSource.PageLoadStarted -= this.PageLoadStarted;
this.actionSource.PageLoadFinished -= this.PageLoadFinished;
this.actionSource.PageLoadError -= this.PageLoadError;
this.actionSource.PageLoadInNewWindowRequest -= this.NewWebBrowserWindowOpenRequest;
}
}
}
1 change: 1 addition & 0 deletions HybridWebControl/Control/IHybridWebViewActionSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ string CurrentUrl
event Action<Uri> PageLoadFinished;
event Action<Uri, string, int> PageLoadError;
event Action<string> JavascriptExecuted;
event Action<Uri> PageLoadInNewWindowRequest;

void GoBack();
void GoForward();
Expand Down

0 comments on commit 164863d

Please sign in to comment.