chore: remove temporary H5 cache diagnostics
This commit is contained in:
@@ -11,34 +11,22 @@ import android.net.Uri
|
||||
import android.os.Build
|
||||
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
|
||||
import org.conscrypt.Conscrypt
|
||||
import java.security.Security
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.plugins.webviewflutter.WebViewFlutterAndroidExternalApi
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
|
||||
class MainActivity : FlutterActivity() {
|
||||
private val CHANNEL = "io.openim.flutter.openim/apk_info"
|
||||
private val SHELL_BRANDING_CHANNEL = "io.openim.flutter.im_webview_app/shell_branding"
|
||||
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_v5"
|
||||
private val WEBVIEW_STORAGE_RESET_PREFS = "h5_shell_webview_storage"
|
||||
private val WEBVIEW_STORAGE_RESET_KEY = "reset_version"
|
||||
private val WEBVIEW_STORAGE_RESET_VERSION = 5
|
||||
|
||||
override fun onCreate(savedInstanceState: android.os.Bundle?) {
|
||||
configureWebViewDataDirectory()
|
||||
clearWebViewPersistentStorageOnce()
|
||||
// 华为/荣耀/OPPO 等国产设备:在任意网络请求之前同步安装 Conscrypt,修复 SSL 握手失败(无 GMS 时系统 SSL 实现不完整)
|
||||
try {
|
||||
Security.insertProviderAt(Conscrypt.newProvider(), 1)
|
||||
@@ -119,109 +107,6 @@ class MainActivity : FlutterActivity() {
|
||||
}
|
||||
}
|
||||
}
|
||||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, WEBVIEW_CACHE_CHANNEL).setMethodCallHandler { call, result ->
|
||||
when (call.method) {
|
||||
"configureNoCache" -> {
|
||||
val webViewId = readLongArgument(call.argument<Any>("webViewId"))
|
||||
if (webViewId == null) {
|
||||
result.error("INVALID_ARGUMENT", "webViewId 不能为空", null)
|
||||
return@setMethodCallHandler
|
||||
}
|
||||
|
||||
try {
|
||||
result.success(configureWebViewNoCache(flutterEngine, webViewId))
|
||||
} catch (e: Exception) {
|
||||
result.error("ERROR", "无法配置 WebView 缓存策略: ${e.message}", null)
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
result.notImplemented()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 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()
|
||||
is Long -> value
|
||||
is Number -> value.toLong()
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
private fun configureWebViewNoCache(flutterEngine: FlutterEngine, webViewId: Long): Boolean {
|
||||
val webView = WebViewFlutterAndroidExternalApi.getWebView(flutterEngine, webViewId) ?: return false
|
||||
|
||||
webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
|
||||
webView.clearCache(true)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
try {
|
||||
ServiceWorkerController
|
||||
.getInstance()
|
||||
.serviceWorkerWebSettings
|
||||
.cacheMode = WebSettings.LOAD_NO_CACHE
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "ServiceWorker cache mode configure failed: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
private fun getCurrentAppName(): String {
|
||||
|
||||
Reference in New Issue
Block a user