From a0a3c5196024fdb36e16ad011f92ad7e9a4c75a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joris=20Pelgr=C3=B6m?= Date: Mon, 24 Jul 2023 16:30:44 +0200 Subject: [PATCH] Show application names, not only package names, in sensor settings (#3700) Show application labels in addition to/instead of package names in sensor settings --- .../settings/sensor/SensorDetailViewModel.kt | 77 +++++++++++++++---- 1 file changed, 63 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/io/homeassistant/companion/android/settings/sensor/SensorDetailViewModel.kt b/app/src/main/java/io/homeassistant/companion/android/settings/sensor/SensorDetailViewModel.kt index dafa71a6c9d..1e1e6a0a0f0 100644 --- a/app/src/main/java/io/homeassistant/companion/android/settings/sensor/SensorDetailViewModel.kt +++ b/app/src/main/java/io/homeassistant/companion/android/settings/sensor/SensorDetailViewModel.kt @@ -2,7 +2,9 @@ package io.homeassistant.companion.android.settings.sensor import android.Manifest import android.app.Application +import android.content.pm.ApplicationInfo import android.content.pm.PackageManager +import android.content.pm.PackageManager.NameNotFoundException import android.os.Build import android.util.Log import androidx.annotation.StringRes @@ -233,6 +235,7 @@ class SensorDetailViewModel @Inject constructor( setting = setting, entries = when { setting.valueType == SensorSettingType.LIST || + setting.valueType == SensorSettingType.LIST_APPS || setting.valueType == SensorSettingType.LIST_BLUETOOTH || setting.valueType == SensorSettingType.LIST_ZONES -> listKeys.zip(listEntries) @@ -243,6 +246,7 @@ class SensorDetailViewModel @Inject constructor( }, entriesSelected = when { setting.valueType == SensorSettingType.LIST || + setting.valueType == SensorSettingType.LIST_APPS || setting.valueType == SensorSettingType.LIST_BLUETOOTH || setting.valueType == SensorSettingType.LIST_ZONES -> setting.value.split(", ").filter { listKeys.contains(it) } @@ -351,10 +355,52 @@ class SensorDetailViewModel @Inject constructor( return stringVars.toTypedArray() } + /** @return list of [ApplicationInfo] for the [entries] or all applications if `null`*/ + private fun getApplicationInfoForEntries(entries: List?): List { + val packageManager = getApplication().packageManager + return if (entries?.isNotEmpty() == true) { + entries.map { + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + packageManager.getApplicationInfo(it, PackageManager.ApplicationInfoFlags.of(0)) + } else { + @Suppress("DEPRECATION") + packageManager.getApplicationInfo(it, 0) + } + } catch (e: NameNotFoundException) { + null + } + } + } else { + val appInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + packageManager?.getInstalledApplications(PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong())) + } else { + @Suppress("DEPRECATION") + packageManager?.getInstalledApplications(PackageManager.GET_META_DATA) + } + appInfo.orEmpty() + } + } + private fun getSettingKeys(setting: SensorSetting): List { return when (setting.valueType) { SensorSettingType.LIST -> setting.entries + SensorSettingType.LIST_APPS -> { + val packageManager = getApplication().packageManager + getApplicationInfoForEntries(null) + .filterNotNull() + .sortedBy { + packageManager.getApplicationLabel(it).let { label -> + when { + label.isBlank() -> it.packageName + label != it.packageName -> "$label\n(${it.packageName}" + else -> label.toString() + } + }.lowercase() + } + .map { it.packageName } + } SensorSettingType.LIST_BLUETOOTH -> BluetoothUtils.getBluetoothDevices(getApplication()).map { it.address } SensorSettingType.LIST_ZONES -> @@ -375,21 +421,24 @@ class SensorDetailViewModel @Inject constructor( return when (setting.valueType) { SensorSettingType.LIST -> getSettingTranslatedEntries(setting.name, entries ?: setting.entries) - SensorSettingType.LIST_APPS -> - entries ?: run { - val apps = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - getApplication().packageManager - ?.getInstalledApplications(PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong())) - } else { - @Suppress("DEPRECATION") - getApplication().packageManager - ?.getInstalledApplications(PackageManager.GET_META_DATA) + SensorSettingType.LIST_APPS -> { + val packageManager = getApplication().packageManager + val apps = getApplicationInfoForEntries(entries) + apps + .mapIndexed { index, info -> + if (info == null) return@mapIndexed entries?.get(index) ?: "" + val label = packageManager.getApplicationLabel(info) + when { + label.isBlank() -> + info.packageName + label != info.packageName -> + if (entries?.isNotEmpty() == true) label.toString() else "$label\n(${info.packageName})" + else -> + label.toString() + } } - apps - ?.map { packageItem -> packageItem.packageName } - ?.sorted() - .orEmpty() - } + .sortedBy { it.lowercase() } + } SensorSettingType.LIST_BLUETOOTH -> { val devices = BluetoothUtils.getBluetoothDevices(getApplication()) .filter { entries == null || entries.contains(it.address) }