This repository was archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Android] Fix WebView.Navigating event, make WebViewClient inheritable #3780
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
Xamarin.Forms.Platform.Android/Renderers/FormsWebViewClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
using Android.App; | ||
using Android.Content; | ||
using Android.Graphics; | ||
using Android.OS; | ||
using Android.Runtime; | ||
using Android.Views; | ||
using Android.Webkit; | ||
using Android.Widget; | ||
|
||
namespace Xamarin.Forms.Platform.Android | ||
{ | ||
public class FormsWebViewClient : WebViewClient | ||
{ | ||
WebNavigationResult _navigationResult = WebNavigationResult.Success; | ||
WebViewRenderer _renderer; | ||
|
||
public FormsWebViewClient(WebViewRenderer renderer) | ||
{ | ||
if (renderer == null) | ||
throw new ArgumentNullException("renderer"); | ||
_renderer = renderer; | ||
} | ||
|
||
protected FormsWebViewClient(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) | ||
{ | ||
|
||
} | ||
|
||
public override void OnPageStarted(global::Android.Webkit.WebView view, string url, Bitmap favicon) | ||
{ | ||
if (_renderer?.Element == null || url == WebViewRenderer.AssetBaseUrl) | ||
return; | ||
|
||
var args = new WebNavigatingEventArgs(WebNavigationEvent.NewPage, new UrlWebViewSource { Url = url }, url); | ||
|
||
_renderer.ElementController.SendNavigating(args); | ||
_navigationResult = WebNavigationResult.Success; | ||
|
||
_renderer.UpdateCanGoBackForward(); | ||
|
||
if (args.Cancel) | ||
{ | ||
_renderer.Control.StopLoading(); | ||
} | ||
else | ||
{ | ||
base.OnPageStarted(view, url, favicon); | ||
} | ||
} | ||
|
||
public override void OnPageFinished(global::Android.Webkit.WebView view, string url) | ||
{ | ||
if (_renderer?.Element == null || url == WebViewRenderer.AssetBaseUrl) | ||
return; | ||
|
||
var source = new UrlWebViewSource { Url = url }; | ||
_renderer.IgnoreSourceChanges = true; | ||
_renderer.ElementController.SetValueFromRenderer(WebView.SourceProperty, source); | ||
_renderer.IgnoreSourceChanges = false; | ||
|
||
var args = new WebNavigatedEventArgs(WebNavigationEvent.NewPage, source, url, _navigationResult); | ||
|
||
_renderer.ElementController.SendNavigated(args); | ||
|
||
_renderer.UpdateCanGoBackForward(); | ||
|
||
base.OnPageFinished(view, url); | ||
} | ||
|
||
[Obsolete("OnReceivedError is obsolete as of version 2.3.0. This method was deprecated in API level 23.")] | ||
public override void OnReceivedError(global::Android.Webkit.WebView view, ClientError errorCode, string description, string failingUrl) | ||
{ | ||
_navigationResult = WebNavigationResult.Failure; | ||
if (errorCode == ClientError.Timeout) | ||
_navigationResult = WebNavigationResult.Timeout; | ||
#pragma warning disable 618 | ||
base.OnReceivedError(view, errorCode, description, failingUrl); | ||
#pragma warning restore 618 | ||
} | ||
|
||
public override void OnReceivedError(global::Android.Webkit.WebView view, IWebResourceRequest request, WebResourceError error) | ||
{ | ||
_navigationResult = WebNavigationResult.Failure; | ||
if (error.ErrorCode == ClientError.Timeout) | ||
_navigationResult = WebNavigationResult.Timeout; | ||
base.OnReceivedError(view, request, error); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
base.Dispose(disposing); | ||
if (disposing) | ||
_renderer = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems this should be protected at best.