From 9593f330dbdfb211c95bd22699bef8546fab9f3e Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 8 Oct 2018 23:21:29 -0400 Subject: [PATCH] target android sdk 26 with fixes to HybridWebView --- src/Android/Controls/HybridWebViewRenderer.cs | 35 ++++++++++++------- src/Android/Properties/AndroidManifest.xml | 2 +- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/Android/Controls/HybridWebViewRenderer.cs b/src/Android/Controls/HybridWebViewRenderer.cs index c5d84a0bf..5f5034993 100644 --- a/src/Android/Controls/HybridWebViewRenderer.cs +++ b/src/Android/Controls/HybridWebViewRenderer.cs @@ -7,7 +7,6 @@ using Android.Webkit; using AWebkit = Android.Webkit; using Java.Interop; using Android.Content; -using Plugin.CurrentActivity; [assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))] namespace Bit.Android.Controls @@ -16,9 +15,13 @@ namespace Bit.Android.Controls { private const string JSFunction = "function invokeCSharpAction(data){jsBridge.invokeAction(data);}"; + private readonly Context _context; + public HybridWebViewRenderer(Context context) : base(context) - { } + { + _context = context; + } protected override void OnElementChanged(ElementChangedEventArgs e) { @@ -26,8 +29,9 @@ namespace Bit.Android.Controls if(Control == null) { - var webView = new AWebkit.WebView(CrossCurrentActivity.Current.Activity); + var webView = new AWebkit.WebView(_context); webView.Settings.JavaScriptEnabled = true; + webView.SetWebViewClient(new JSWebViewClient(string.Format("javascript: {0}", JSFunction))); SetNativeControl(webView); } @@ -42,15 +46,6 @@ namespace Bit.Android.Controls { Control.AddJavascriptInterface(new JSBridge(this), "jsBridge"); Control.LoadUrl(Element.Uri); - InjectJS(JSFunction); - } - } - - private void InjectJS(string script) - { - if(Control != null) - { - Control.LoadUrl(string.Format("javascript: {0}", script)); } } @@ -74,5 +69,21 @@ namespace Bit.Android.Controls } } } + + public class JSWebViewClient : WebViewClient + { + private readonly string _javascript; + + public JSWebViewClient(string javascript) + { + _javascript = javascript; + + } + public override void OnPageFinished(AWebkit.WebView view, string url) + { + base.OnPageFinished(view, url); + view.EvaluateJavascript(_javascript, null); + } + } } } diff --git a/src/Android/Properties/AndroidManifest.xml b/src/Android/Properties/AndroidManifest.xml index dfc039d54..2df206548 100644 --- a/src/Android/Properties/AndroidManifest.xml +++ b/src/Android/Properties/AndroidManifest.xml @@ -1,6 +1,6 @@ - +