feat: 添加应用品牌信息支持,重构主页 URL 生成逻辑
This commit is contained in:
@@ -3,8 +3,13 @@ package io.openim.flutter.openim
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.drawable.BitmapDrawable
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.util.Base64
|
||||
import android.util.Log
|
||||
import androidx.core.content.FileProvider
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
@@ -12,10 +17,12 @@ import org.conscrypt.Conscrypt
|
||||
import java.security.Security
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
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 TAG = "MainActivity"
|
||||
|
||||
override fun onCreate(savedInstanceState: android.os.Bundle?) {
|
||||
@@ -80,6 +87,53 @@ class MainActivity : FlutterActivity() {
|
||||
}
|
||||
}
|
||||
}
|
||||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, SHELL_BRANDING_CHANNEL).setMethodCallHandler { call, result ->
|
||||
when (call.method) {
|
||||
"getShellBranding" -> {
|
||||
try {
|
||||
result.success(
|
||||
mapOf(
|
||||
"appName" to getCurrentAppName(),
|
||||
"appLogo" to getCurrentAppIconDataUrl()
|
||||
)
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
result.error("ERROR", "无法获取当前应用品牌信息: ${e.message}", null)
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
result.notImplemented()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getCurrentAppName(): String {
|
||||
val label = packageManager.getApplicationLabel(applicationInfo)
|
||||
return label?.toString() ?: ""
|
||||
}
|
||||
|
||||
private fun getCurrentAppIconDataUrl(): String {
|
||||
val icon = packageManager.getApplicationIcon(packageName)
|
||||
val bitmap = drawableToBitmap(icon)
|
||||
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 drawableToBitmap(drawable: Drawable): Bitmap {
|
||||
if (drawable is BitmapDrawable && drawable.bitmap != null) {
|
||||
return drawable.bitmap
|
||||
}
|
||||
|
||||
val width = if (drawable.intrinsicWidth > 0) drawable.intrinsicWidth else 192
|
||||
val height = if (drawable.intrinsicHeight > 0) drawable.intrinsicHeight else 192
|
||||
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
|
||||
val canvas = Canvas(bitmap)
|
||||
drawable.setBounds(0, 0, canvas.width, canvas.height)
|
||||
drawable.draw(canvas)
|
||||
return bitmap
|
||||
}
|
||||
|
||||
private fun getApkPackageName(apkPath: String): String? {
|
||||
|
||||
Reference in New Issue
Block a user