@@ -3,6 +3,12 @@ package com.cloud.pira
33import android.content.Context
44import android.content.pm.ApplicationInfo
55import android.content.pm.PackageManager
6+ import android.graphics.Bitmap
7+ import android.graphics.Canvas
8+ import android.graphics.drawable.BitmapDrawable
9+ import android.graphics.drawable.Drawable
10+ import android.util.Base64
11+ import androidx.core.graphics.drawable.toBitmap
612import io.flutter.embedding.engine.FlutterEngine
713import io.flutter.plugin.common.MethodCall
814import io.flutter.plugin.common.MethodChannel
@@ -12,6 +18,7 @@ import kotlinx.coroutines.CoroutineScope
1218import kotlinx.coroutines.Dispatchers
1319import kotlinx.coroutines.launch
1420import kotlinx.coroutines.withContext
21+ import java.io.ByteArrayOutputStream
1522
1623class AppListMethodChannel (private val context : Context ) : MethodCallHandler {
1724 companion object {
@@ -46,10 +53,21 @@ class AppListMethodChannel(private val context: Context) : MethodCallHandler {
4653 val appName = packageManager.getApplicationLabel(appInfo).toString()
4754 val packageName = appInfo.packageName
4855
56+ // Get app icon as base64 string
57+ var iconBase64 = " "
58+ try {
59+ val appIcon = packageManager.getApplicationIcon(appInfo.packageName)
60+ iconBase64 = drawableToBase64(appIcon)
61+ } catch (e: Exception ) {
62+ // If we can't get the icon, we'll just leave it empty
63+ iconBase64 = " "
64+ }
65+
4966 appList.add(mapOf (
5067 " name" to appName,
5168 " packageName" to packageName,
52- " isSystemApp" to ((appInfo.flags and ApplicationInfo .FLAG_SYSTEM ) != 0 )
69+ " isSystemApp" to ((appInfo.flags and ApplicationInfo .FLAG_SYSTEM ) != 0 ),
70+ " icon" to iconBase64
5371 ))
5472 }
5573
@@ -73,4 +91,21 @@ class AppListMethodChannel(private val context: Context) : MethodCallHandler {
7391 }
7492 }
7593 }
94+
95+ private fun drawableToBase64 (drawable : Drawable ): String {
96+ // Convert drawable to bitmap
97+ val bitmap = if (drawable is BitmapDrawable ) {
98+ drawable.bitmap
99+ } else {
100+ drawable.toBitmap(96 , 96 , Bitmap .Config .ARGB_8888 )
101+ }
102+
103+ // Compress bitmap to byte array
104+ val byteArrayOutputStream = ByteArrayOutputStream ()
105+ bitmap.compress(Bitmap .CompressFormat .PNG , 100 , byteArrayOutputStream)
106+ val byteArray = byteArrayOutputStream.toByteArray()
107+
108+ // Convert byte array to base64 string
109+ return Base64 .encodeToString(byteArray, Base64 .NO_WRAP )
110+ }
76111}
0 commit comments