Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent app crash if MAUI View becomes null #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 51 additions & 32 deletions HybridWebView/Platforms/Android/AndroidHybridWebViewClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down