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
Make MAUI blazor which required android api 24 compatible to api 21 #8375
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
using System.Runtime.Versioning; | ||
using System.Threading.Tasks; | ||
using Android.Webkit; | ||
using AndroidX.WebKit; | ||
using Microsoft.AspNetCore.Components.Web; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.FileProviders; | ||
|
@@ -16,7 +17,7 @@ namespace Microsoft.AspNetCore.Components.WebView.Maui | |
/// An implementation of <see cref="WebViewManager"/> that uses the Android WebKit WebView browser control | ||
/// to render web content. | ||
/// </summary> | ||
[SupportedOSPlatform("android23.0")] | ||
[SupportedOSPlatform("android21.0")] | ||
Comment on lines
-19
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove these attributes now since we have a min version in .NET itself that is 21. .NET 6 does not run on lower than 21. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed |
||
internal class AndroidWebKitWebViewManager : WebViewManager | ||
{ | ||
// Using an IP address means that WebView doesn't wait for any DNS resolution, | ||
|
@@ -63,7 +64,7 @@ protected override void NavigateCore(Uri absoluteUri) | |
/// <inheritdoc /> | ||
protected override void SendMessage(string message) | ||
{ | ||
_webview.PostWebMessage(new WebMessage(message), AndroidAppOriginUri); | ||
WebViewCompat.PostWebMessage(_webview, new WebMessageCompat(message), AndroidAppOriginUri); | ||
} | ||
|
||
internal bool TryGetResponseContentInternal(string uri, bool allowFallbackOnHostPage, out int statusCode, out string statusMessage, out Stream content, out IDictionary<string, string> headers) | ||
|
@@ -76,7 +77,7 @@ internal bool TryGetResponseContentInternal(string uri, bool allowFallbackOnHost | |
internal void SetUpMessageChannel() | ||
{ | ||
// These ports will be closed automatically when the webview gets disposed. | ||
var nativeToJSPorts = _webview.CreateWebMessageChannel(); | ||
var nativeToJSPorts = WebViewCompat.CreateWebMessageChannel(_webview); | ||
|
||
var nativeToJs = new BlazorWebMessageCallback(message => | ||
{ | ||
|
@@ -87,10 +88,10 @@ internal void SetUpMessageChannel() | |
|
||
nativeToJSPorts[0].SetWebMessageCallback(nativeToJs); | ||
|
||
_webview.PostWebMessage(new WebMessage("capturePort", destPort), AndroidAppOriginUri); | ||
WebViewCompat.PostWebMessage(_webview, new WebMessageCompat("capturePort", destPort), AndroidAppOriginUri); | ||
} | ||
|
||
private class BlazorWebMessageCallback : WebMessagePort.WebMessageCallback | ||
private class BlazorWebMessageCallback : WebMessagePortCompat.WebMessageCallbackCompat | ||
{ | ||
private readonly Action<string?> _onMessageReceived; | ||
|
||
|
@@ -99,7 +100,7 @@ public BlazorWebMessageCallback(Action<string?> onMessageReceived) | |
_onMessageReceived = onMessageReceived ?? throw new ArgumentNullException(nameof(onMessageReceived)); | ||
} | ||
|
||
public override void OnMessage(WebMessagePort? port, WebMessage? message) | ||
public override void OnMessage(WebMessagePortCompat? port, WebMessageCompat? message) | ||
{ | ||
if (message is null) | ||
{ | ||
|
@@ -108,6 +109,7 @@ public override void OnMessage(WebMessagePort? port, WebMessage? message) | |
|
||
_onMessageReceived(message.Data); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: extra line |
||
} | ||
} | ||
} |
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.
If this is an
Update
, how does this line do anything? It must already be a transitive dependency, and we can remove it?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.
@javiercn would you mind filing an issue first explaining the motivation for the change as well as the impact so that the team can assess whether or not we want to take this change in?
Please forgive me for not having time to reply a few days ago.
This is not necessary, but just a suggestion for the following reasons.
From Android API 24 to 23, it is not difficult for Android. They are all official.
API 21 app will run on approximately 98.6% of devices, and API 24 app only has 91.7%. The is from Android studio.
Using androidx.webkit increases the size of Android installation package a little and the performance loss is almost negligible.
It makes the Android device compatibility of Maui Blazor consistent with Maui.
My test method is temporarily modified the namespace of Microsoft.AspNetCore.Components.WebView.Maui.csproj and only compile it with min Android API 21, so that it can be directly added to the Maui blazor app project, and then tested on the real machine of Android 5(API 21). The startup speed is acceptable, which is 40% faster than blazor WebAssembly. This is stupid, but simple and effective.