From e5a3c1f181174b4a8969195ca24d19d06a4a3bcc Mon Sep 17 00:00:00 2001 From: yyalan Date: Thu, 13 Apr 2023 15:50:14 +0000 Subject: [PATCH] [MediaProjection] Avoid SysUI crash by handling NameNotFoundException When the AppSelector fails to load the recent app's label, the exception causes SysUI to crash. Avoid this case by handling potential errors. Fixes: 274912947 Test: manual Change-Id: I56c33c7ff2af9c74e5a492e959fe05b8bb2de0aa --- .../appselector/data/RecentTaskLabelLoader.kt | 26 +++++++++++++------ .../appselector/view/RecentTaskViewHolder.kt | 2 +- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/mediaprojection/appselector/data/RecentTaskLabelLoader.kt b/packages/SystemUI/src/com/android/systemui/mediaprojection/appselector/data/RecentTaskLabelLoader.kt index eadcb93a7f948..1be8b70c1d42a 100644 --- a/packages/SystemUI/src/com/android/systemui/mediaprojection/appselector/data/RecentTaskLabelLoader.kt +++ b/packages/SystemUI/src/com/android/systemui/mediaprojection/appselector/data/RecentTaskLabelLoader.kt @@ -20,6 +20,7 @@ import android.annotation.UserIdInt import android.content.ComponentName import android.content.pm.PackageManager import android.os.UserHandle +import android.util.Log import com.android.systemui.dagger.qualifiers.Background import javax.inject.Inject import kotlinx.coroutines.CoroutineDispatcher @@ -36,18 +37,27 @@ constructor( private val packageManager: PackageManager ) : RecentTaskLabelLoader { + private val TAG = "RecentTaskLabelLoader" + override suspend fun loadLabel( @UserIdInt userId: Int, componentName: ComponentName ): CharSequence? = withContext(coroutineDispatcher) { - val userHandle = UserHandle(userId) - val appInfo = - packageManager.getApplicationInfo( - componentName.packageName, - PackageManager.ApplicationInfoFlags.of(0 /* no flags */) - ) - val label = packageManager.getApplicationLabel(appInfo) - return@withContext packageManager.getUserBadgedLabel(label, userHandle) + var badgedLabel: CharSequence? = null + try { + val appInfo = + packageManager.getApplicationInfoAsUser( + componentName.packageName, + PackageManager.ApplicationInfoFlags.of(0 /* no flags */), + userId + ) + val label = packageManager.getApplicationLabel(appInfo) + val userHandle = UserHandle(userId) + badgedLabel = packageManager.getUserBadgedLabel(label, userHandle) + } catch (e: PackageManager.NameNotFoundException) { + Log.e(TAG, "Unable to get application info", e) + } + return@withContext badgedLabel } } diff --git a/packages/SystemUI/src/com/android/systemui/mediaprojection/appselector/view/RecentTaskViewHolder.kt b/packages/SystemUI/src/com/android/systemui/mediaprojection/appselector/view/RecentTaskViewHolder.kt index 64f97f2faacc0..2d75359bf8350 100644 --- a/packages/SystemUI/src/com/android/systemui/mediaprojection/appselector/view/RecentTaskViewHolder.kt +++ b/packages/SystemUI/src/com/android/systemui/mediaprojection/appselector/view/RecentTaskViewHolder.kt @@ -68,7 +68,7 @@ constructor( } launch { val label = labelLoader.loadLabel(task.userId, component) - root.contentDescription = label + root.contentDescription = label ?: root.context.getString(R.string.unknown) } } launch {