Mr U9Q
09/18/2023, 7:04 PM@Composable
fun HomeScreen(navController: NavController) {
val context = LocalContext.current
val appInfos by remember {
mutableStateOf(getAllInstalledApps(context = context))
}
LazyVerticalGrid(
contentPadding = PaddingValues(8.dp),
columns = GridCells.Adaptive(minSize = 60.dp),
){
items(appInfos, key = {info -> info.activityInfo.packageName}){ info ->
AppItem(resolveInfo = info)
}
}
Text(
text = "Home Screen",
fontSize = 20.sp
)
}
@Composable
fun AppItem(resolveInfo: ResolveInfo){
val pm = LocalContext.current.packageManager
val resources = pm.getResourcesForApplication(resolveInfo.activityInfo.applicationInfo)
val appName = if (resolveInfo.activityInfo.labelRes != 0) {
// getting proper label from resources
resources.getString(resolveInfo.activityInfo.labelRes)
} else {
// getting it out of app info - equivalent to context.packageManager.getApplicationInfo
resolveInfo.activityInfo.applicationInfo.loadLabel(pm).toString()
}
val packageName = resolveInfo.activityInfo.packageName
val iconDrawable = resolveInfo.activityInfo.loadIcon(pm)
Column(
modifier = Modifier.padding(8.dp),
){
AsyncImage(
modifier = Modifier
.height(60.dp)
.width(60.dp),
model = iconDrawable,
contentDescription = "App icon image",
)
Text(
modifier = Modifier.fillMaxWidth(),
text = appName,
fontSize = 10.sp,
textAlign = TextAlign.Center,
maxLines = 2,
overflow = TextOverflow.Ellipsis,
lineHeight = 12.sp
)
}
}
private fun getAllInstalledApps(context: Context): List<ResolveInfo>{
val pm = context.packageManager
val mainIntent = Intent(Intent.ACTION_MAIN, null)
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER)
val allApps = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
pm.queryIntentActivities(
mainIntent,
PackageManager.ResolveInfoFlags.of(0)
)
} else {
pm.queryIntentActivities(mainIntent, 0)
}
val userApps = ArrayList<ResolveInfo>()
for (app in allApps){
if(isSystemPackage(app.activityInfo)){
userApps.add(app)
}
}
return userApps
}
private fun isSystemPackage(activityInfo: ActivityInfo): Boolean {
return activityInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM == 0
}