feat: 添加图标大小限制和无缓存请求头,优化 WebView 加载逻辑

This commit is contained in:
Booker
2026-05-26 15:15:46 +07:00
parent b85422c1bc
commit 81f0f342a9
2 changed files with 45 additions and 3 deletions

View File

@@ -24,6 +24,7 @@ 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 TAG = "MainActivity"
private val MAX_BRANDING_ICON_SIZE = 192
override fun onCreate(savedInstanceState: android.os.Bundle?) {
// 华为/荣耀/OPPO 等国产设备:在任意网络请求之前同步安装 Conscrypt修复 SSL 握手失败(无 GMS 时系统 SSL 实现不完整)
@@ -115,13 +116,27 @@ class MainActivity : FlutterActivity() {
private fun getCurrentAppIconDataUrl(): String {
val icon = packageManager.getApplicationIcon(packageName)
val bitmap = drawableToBitmap(icon)
val bitmap = resizeBitmap(drawableToBitmap(icon), MAX_BRANDING_ICON_SIZE)
val output = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output)
val base64 = Base64.encodeToString(output.toByteArray(), Base64.NO_WRAP)
return "data:image/png;base64,$base64"
}
private fun resizeBitmap(bitmap: Bitmap, maxSize: Int): Bitmap {
val width = bitmap.width
val height = bitmap.height
val longestSide = maxOf(width, height)
if (longestSide <= maxSize) {
return bitmap
}
val scale = maxSize.toFloat() / longestSide.toFloat()
val scaledWidth = (width * scale).toInt().coerceAtLeast(1)
val scaledHeight = (height * scale).toInt().coerceAtLeast(1)
return Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true)
}
private fun drawableToBitmap(drawable: Drawable): Bitmap {
if (drawable is BitmapDrawable && drawable.bitmap != null) {
return drawable.bitmap