feat: 配置 WebView 数据目录,优化 WebView 性能;更新 homeUrl 方法以支持更好的 URL 生成逻辑

This commit is contained in:
Booker
2026-05-26 19:17:15 +07:00
parent ca25c2d706
commit 48e4229cb7
3 changed files with 29 additions and 5 deletions

View File

@@ -13,6 +13,7 @@ import android.util.Base64
import android.util.Log
import android.webkit.ServiceWorkerController
import android.webkit.WebSettings
import android.webkit.WebView
import androidx.core.content.FileProvider
import io.flutter.embedding.android.FlutterActivity
import org.conscrypt.Conscrypt
@@ -29,8 +30,10 @@ class MainActivity : FlutterActivity() {
private val WEBVIEW_CACHE_CHANNEL = "io.openim.flutter.im_webview_app/webview_cache"
private val TAG = "MainActivity"
private val MAX_BRANDING_ICON_SIZE = 192
private val WEBVIEW_DATA_DIRECTORY_SUFFIX = "h5_shell_fresh_profile_v2"
override fun onCreate(savedInstanceState: android.os.Bundle?) {
configureWebViewDataDirectory()
// 华为/荣耀/OPPO 等国产设备:在任意网络请求之前同步安装 Conscrypt修复 SSL 握手失败(无 GMS 时系统 SSL 实现不完整)
try {
Security.insertProviderAt(Conscrypt.newProvider(), 1)
@@ -133,6 +136,20 @@ class MainActivity : FlutterActivity() {
}
}
private fun configureWebViewDataDirectory() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
return
}
try {
WebView.setDataDirectorySuffix(WEBVIEW_DATA_DIRECTORY_SUFFIX)
} catch (e: IllegalStateException) {
Log.w(TAG, "WebView data directory already initialized: ${e.message}")
} catch (e: Exception) {
Log.w(TAG, "WebView data directory configure failed: ${e.message}")
}
}
private fun readLongArgument(value: Any?): Long? {
return when (value) {
is Int -> value.toLong()