feat: 更新 WebView 数据目录,添加持久存储清理逻辑;优化 URL 生成和 H5 路由刷新时的品牌保持
This commit is contained in:
@@ -13,6 +13,7 @@ import android.util.Base64
|
||||
import android.util.Log
|
||||
import android.webkit.ServiceWorkerController
|
||||
import android.webkit.WebSettings
|
||||
import android.webkit.WebStorage
|
||||
import android.webkit.WebView
|
||||
import androidx.core.content.FileProvider
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
@@ -30,10 +31,14 @@ 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"
|
||||
private val WEBVIEW_DATA_DIRECTORY_SUFFIX = "h5_shell_fresh_profile_v3"
|
||||
private val WEBVIEW_STORAGE_RESET_PREFS = "h5_shell_webview_storage"
|
||||
private val WEBVIEW_STORAGE_RESET_KEY = "reset_version"
|
||||
private val WEBVIEW_STORAGE_RESET_VERSION = 3
|
||||
|
||||
override fun onCreate(savedInstanceState: android.os.Bundle?) {
|
||||
configureWebViewDataDirectory()
|
||||
clearWebViewPersistentStorageOnce()
|
||||
// 华为/荣耀/OPPO 等国产设备:在任意网络请求之前同步安装 Conscrypt,修复 SSL 握手失败(无 GMS 时系统 SSL 实现不完整)
|
||||
try {
|
||||
Security.insertProviderAt(Conscrypt.newProvider(), 1)
|
||||
@@ -150,6 +155,46 @@ class MainActivity : FlutterActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun clearWebViewPersistentStorageOnce() {
|
||||
val prefs = getSharedPreferences(WEBVIEW_STORAGE_RESET_PREFS, MODE_PRIVATE)
|
||||
if (prefs.getInt(WEBVIEW_STORAGE_RESET_KEY, 0) >= WEBVIEW_STORAGE_RESET_VERSION) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
WebStorage.getInstance().deleteAllData()
|
||||
Log.d(TAG, "WebView persistent storage cleared")
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "WebView persistent storage clear failed: ${e.message}")
|
||||
}
|
||||
|
||||
clearKnownWebViewCacheDirectories()
|
||||
prefs.edit().putInt(WEBVIEW_STORAGE_RESET_KEY, WEBVIEW_STORAGE_RESET_VERSION).apply()
|
||||
}
|
||||
|
||||
private fun clearKnownWebViewCacheDirectories() {
|
||||
val cacheDirectoryNames = listOf(
|
||||
"WebView",
|
||||
"webview",
|
||||
"org.chromium.android_webview",
|
||||
"com.android.webview"
|
||||
)
|
||||
|
||||
for (name in cacheDirectoryNames) {
|
||||
val directory = File(cacheDir, name)
|
||||
if (!directory.exists()) {
|
||||
continue
|
||||
}
|
||||
try {
|
||||
if (!directory.deleteRecursively()) {
|
||||
Log.w(TAG, "WebView cache directory delete returned false: ${directory.absolutePath}")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "WebView cache directory delete failed: ${directory.absolutePath}, ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun readLongArgument(value: Any?): Long? {
|
||||
return when (value) {
|
||||
is Int -> value.toLong()
|
||||
|
||||
Reference in New Issue
Block a user