From a06c300ddeb2a7f012bd663fdcc7fe0cf4334d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Kri=C5=BEalkovi=C4=8D?= Date: Wed, 28 Feb 2024 16:29:32 +0000 Subject: [PATCH] Prevent app crash if MAUI View becomes null --- .../Android/AndroidHybridWebViewClient.cs | 83 ++++++++++++------- 1 file changed, 51 insertions(+), 32 deletions(-) diff --git a/HybridWebView/Platforms/Android/AndroidHybridWebViewClient.cs b/HybridWebView/Platforms/Android/AndroidHybridWebViewClient.cs index 7c50c0a..a8670db 100644 --- a/HybridWebView/Platforms/Android/AndroidHybridWebViewClient.cs +++ b/HybridWebView/Platforms/Android/AndroidHybridWebViewClient.cs @@ -20,53 +20,72 @@ public AndroidHybridWebViewClient(HybridWebViewHandler handler) : base(handler) if (new Uri(requestUri) is Uri uri && HybridWebView.AppOriginUri.IsBaseOf(uri)) { - var relativePath = HybridWebView.AppOriginUri.MakeRelativeUri(uri).ToString().Replace('/', '\\'); - - string contentType; - if (string.IsNullOrEmpty(relativePath)) - { - relativePath = ((HybridWebView)_handler.VirtualView).MainFile; - contentType = "text/html"; - } - else + try { - var requestExtension = Path.GetExtension(relativePath); - contentType = requestExtension switch + var relativePath = HybridWebView.AppOriginUri.MakeRelativeUri(uri).ToString().Replace('/', '\\'); + + if (relativePath?.Contains("throw-error") == true) { - ".htm" or ".html" => "text/html", - ".js" => "application/javascript", - ".css" => "text/css", - _ => "text/plain", - }; - } + throw new InvalidOperationException("Test exceptio thrown"); + } - var contentStream = KnownStaticFileProvider.GetKnownResourceStream(relativePath!); + string contentType; + if (string.IsNullOrEmpty(relativePath)) + { + relativePath = ((HybridWebView)_handler.VirtualView).MainFile; + contentType = "text/html"; + } + else + { + var requestExtension = Path.GetExtension(relativePath); + contentType = requestExtension switch + { + ".htm" or ".html" => "text/html", + ".js" => "application/javascript", + ".css" => "text/css", + _ => "text/plain", + }; + } - if (contentStream is null) - { - var assetPath = Path.Combine(((HybridWebView)_handler.VirtualView).HybridAssetRoot!, relativePath!); - contentStream = PlatformOpenAppPackageFile(assetPath); - } + var contentStream = KnownStaticFileProvider.GetKnownResourceStream(relativePath!); - if (contentStream is null) - { - var notFoundContent = "Resource not found (404)"; + if (contentStream is null) + { + var assetPath = Path.Combine(((HybridWebView)_handler.VirtualView).HybridAssetRoot!, relativePath!); + contentStream = PlatformOpenAppPackageFile(assetPath); + } - var notFoundByteArray = Encoding.UTF8.GetBytes(notFoundContent); - var notFoundContentStream = new MemoryStream(notFoundByteArray); + if (contentStream is null) + { + var notFoundContent = "Resource not found (404)"; - return new WebResourceResponse("text/plain", "UTF-8", 404, "Not Found", GetHeaders("text/plain"), notFoundContentStream); + var notFoundByteArray = Encoding.UTF8.GetBytes(notFoundContent); + var notFoundContentStream = new MemoryStream(notFoundByteArray); + + return new WebResourceResponse("text/plain", "UTF-8", 404, "Not Found", GetHeaders("text/plain"), notFoundContentStream); + } + else + { + // TODO: We don't know the content length because Android doesn't tell us. Seems to work without it! + return new WebResourceResponse(contentType, "UTF-8", 200, "OK", GetHeaders(contentType), contentStream); + } } - else + catch (Exception ex) { - // TODO: We don't know the content length because Android doesn't tell us. Seems to work without it! - return new WebResourceResponse(contentType, "UTF-8", 200, "OK", GetHeaders(contentType), contentStream); + var errorContent = "Exception: " + ex.ToString(); ; + + var errorContentByteArray = Encoding.UTF8.GetBytes(errorContent); + var errorContentStream = new MemoryStream(errorContentByteArray); + + return new WebResourceResponse("text/plain", "UTF-8", 500, "Internal Server Error - Webview Exception", GetHeaders("text/plain"), errorContentStream); } } else { return base.ShouldInterceptRequest(view, request); } + + } private Stream? PlatformOpenAppPackageFile(string filename)