diff --git a/lawnchair/src/app/lawnchair/icons/LawnchairIconProvider.kt b/lawnchair/src/app/lawnchair/icons/LawnchairIconProvider.kt index 1b797b3147..b250d84c4a 100644 --- a/lawnchair/src/app/lawnchair/icons/LawnchairIconProvider.kt +++ b/lawnchair/src/app/lawnchair/icons/LawnchairIconProvider.kt @@ -14,6 +14,8 @@ import android.content.Intent.ACTION_TIME_CHANGED import android.content.Intent.ACTION_TIME_TICK import android.content.IntentFilter import android.content.pm.ApplicationInfo +import android.content.pm.ComponentInfo +import android.content.pm.LauncherApps import android.content.pm.PackageItemInfo import android.content.res.Resources import android.graphics.drawable.Drawable @@ -32,6 +34,7 @@ import app.lawnchair.icons.picker.IconType import app.lawnchair.preferences.PreferenceManager import app.lawnchair.util.MultiSafeCloseable import app.lawnchair.util.isPackageInstalled +import app.lawnchair.util.requireSystemService import com.android.launcher3.R import com.android.launcher3.dagger.ApplicationContext import com.android.launcher3.dagger.LauncherAppSingleton @@ -107,14 +110,42 @@ class LawnchairIconProvider @Inject constructor( return iconPack.getIcon(componentName) } + /** + * Resolves the launch component for icon-pack lookup. + * + * Avoid [android.content.pm.PackageManager.getLaunchIntentForPackage], which only sees the + * current user — work/private profile apps would otherwise fall back to the system icon. + * Prefer the real component from [ComponentInfo], then [LauncherApps] for the app's user. + */ + private fun resolveComponentName( + info: PackageItemInfo, + appInfo: ApplicationInfo, + user: UserHandle, + ): ComponentName? { + if (info is ComponentInfo && !info.name.isNullOrEmpty()) { + return ComponentName(info.packageName ?: appInfo.packageName, info.name) + } + return resolveLaunchComponent(appInfo.packageName, user) + } + + private fun resolveLaunchComponent(packageName: String, user: UserHandle): ComponentName? { + return try { + val launcherApps: LauncherApps = context.requireSystemService() + launcherApps.getActivityList(packageName, user).firstOrNull()?.componentName + } catch (t: Throwable) { + Log.w(TAG, "Failed to resolve launch component for $packageName user=$user", t) + null + } + } + override fun getIcon( info: PackageItemInfo, appInfo: ApplicationInfo, iconDpi: Int, ): Drawable { val packageName = appInfo.packageName - val componentName = context.packageManager.getLaunchIntentForPackage(packageName)?.component val user = UserHandle.getUserHandleForUid(appInfo.uid) + val componentName = resolveComponentName(info, appInfo, user) var iconEntry: IconEntry? = null if (componentName != null) { diff --git a/lawnchair/src/app/lawnchair/icons/iconpack/SystemIconPack.kt b/lawnchair/src/app/lawnchair/icons/iconpack/SystemIconPack.kt index 530b398922..58c58a8dc8 100644 --- a/lawnchair/src/app/lawnchair/icons/iconpack/SystemIconPack.kt +++ b/lawnchair/src/app/lawnchair/icons/iconpack/SystemIconPack.kt @@ -26,7 +26,13 @@ class SystemIconPack(context: Context, pkg: String) : IconPack(context, pkg) { val profiles = UserCache.INSTANCE.get(context).userProfiles val launcherApps: LauncherApps = context.requireSystemService() profiles - .flatMap { launcherApps.getActivityList(null, Process.myUserHandle()) } + .flatMap { profile -> + try { + launcherApps.getActivityList(null, profile) + } catch (_: Throwable) { + emptyList() + } + } .associateBy { ComponentKey(it.componentName, it.user) } } @@ -34,7 +40,11 @@ class SystemIconPack(context: Context, pkg: String) : IconPack(context, pkg) { startLoad() } - override fun getIcon(componentName: ComponentName) = IconEntry(packPackageName, ComponentKey(componentName, Process.myUserHandle()).toString(), IconType.Normal) + override fun getIcon(componentName: ComponentName): IconEntry { + val matching = appMap.keys.firstOrNull { it.componentName == componentName } + val key = matching ?: ComponentKey(componentName, Process.myUserHandle()) + return IconEntry(packPackageName, key.toString(), IconType.Normal) + } override fun getCalendar(componentName: ComponentName): IconEntry? = null override fun getClock(entry: IconEntry): ClockMetadata? = null diff --git a/lawnchair/src/app/lawnchair/ui/preferences/destinations/SelectIconPreference.kt b/lawnchair/src/app/lawnchair/ui/preferences/destinations/SelectIconPreference.kt index ddaf0256f8..c9a370841a 100644 --- a/lawnchair/src/app/lawnchair/ui/preferences/destinations/SelectIconPreference.kt +++ b/lawnchair/src/app/lawnchair/ui/preferences/destinations/SelectIconPreference.kt @@ -3,6 +3,7 @@ package app.lawnchair.ui.preferences.destinations import android.app.Activity import android.content.Intent import android.content.pm.LauncherApps +import android.util.Log import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.remember @@ -27,14 +28,13 @@ import com.android.launcher3.R import com.android.launcher3.util.ComponentKey import kotlinx.coroutines.launch +private const val TAG = "SelectIconPreference" + @Composable fun SelectIconPreference(componentKey: ComponentKey) { val context = LocalContext.current val label = remember(componentKey) { - val launcherApps: LauncherApps = context.requireSystemService() - val intent = Intent().setComponent(componentKey.componentName) - val activity = launcherApps.resolveActivity(intent, componentKey.user) - activity.label.toString() + resolveAppLabel(context.requireSystemService(), componentKey) } val iconPacks by LocalPreferenceInteractor.current.iconPacks.collectAsStateWithLifecycle() val navController = LocalNavController.current @@ -96,3 +96,36 @@ fun SelectIconPreference(componentKey: ComponentKey) { } } } + +/** + * Resolves a display label for [componentKey], including work/private profiles. + * + * [LauncherApps.resolveActivity] can return null or throw for non-current users; fall back to + * [LauncherApps.getActivityList] and finally the activity class name. + */ +private fun resolveAppLabel(launcherApps: LauncherApps, componentKey: ComponentKey): String { + val componentName = componentKey.componentName + val user = componentKey.user + try { + val intent = Intent().setComponent(componentName) + launcherApps.resolveActivity(intent, user)?.label?.toString()?.let { return it } + } catch (t: Throwable) { + Log.w(TAG, "resolveActivity failed for $componentKey", t) + } + try { + val activities = launcherApps.getActivityList(componentName.packageName, user) + activities + .firstOrNull { it.componentName == componentName } + ?.label + ?.toString() + ?.let { return it } + activities + .firstOrNull() + ?.label + ?.toString() + ?.let { return it } + } catch (t: Throwable) { + Log.w(TAG, "getActivityList failed for $componentKey", t) + } + return componentName.shortClassName?.trimStart('.') ?: componentName.packageName +} diff --git a/src/com/android/launcher3/model/data/ItemInfoWithIcon.java b/src/com/android/launcher3/model/data/ItemInfoWithIcon.java index 68eb429d73..1652d925e1 100644 --- a/src/com/android/launcher3/model/data/ItemInfoWithIcon.java +++ b/src/com/android/launcher3/model/data/ItemInfoWithIcon.java @@ -16,7 +16,6 @@ package com.android.launcher3.model.data; -import static com.android.launcher3.icons.BitmapInfo.FLAG_NO_BADGE; import static com.android.launcher3.icons.BitmapInfo.FLAG_THEMED; import android.content.Context; @@ -324,7 +323,8 @@ public abstract class ItemInfoWithIcon extends ItemInfo { */ public FastBitmapDrawable newIcon(Context context) { var shouldTheme = PreferenceManager.getInstance(context).getThemedIcons().get(); - return newIcon(context, shouldTheme ? BitmapInfo.FLAG_THEMED : BitmapInfo.FLAG_NO_BADGE); + // Use 0 (not FLAG_NO_BADGE) when theming is off so work/clone profile badges still draw. + return newIcon(context, shouldTheme ? FLAG_THEMED : 0); } /**