diff --git a/src/com/android/settings/spa/app/appinfo/AppArchiveButton.kt b/src/com/android/settings/spa/app/appinfo/AppArchiveButton.kt index 38a84998e07..292c8b8f73d 100644 --- a/src/com/android/settings/spa/app/appinfo/AppArchiveButton.kt +++ b/src/com/android/settings/spa/app/appinfo/AppArchiveButton.kt @@ -21,6 +21,7 @@ import android.content.Intent import android.content.IntentFilter import android.content.pm.ApplicationInfo import android.content.pm.PackageInstaller +import android.content.pm.PackageManager import android.os.UserHandle import android.util.Log import android.widget.Toast @@ -87,11 +88,15 @@ class AppArchiveButton( } private fun ApplicationInfo.isActionButtonEnabled(): Boolean { - return !isArchived - && userPackageManager.isAppArchivable(packageName) - // We apply the same device policy for both the uninstallation and archive - // button. - && !appButtonRepository.isUninstallBlockedByAdmin(this) + return try { + (!isArchived + && userPackageManager.isAppArchivable(packageName) + // We apply the same device policy for both the uninstallation and archive + // button. + && !appButtonRepository.isUninstallBlockedByAdmin(this)) + } catch (e: PackageManager.NameNotFoundException) { + false + } } private fun onArchiveClicked(app: ApplicationInfo) { diff --git a/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppArchiveButtonTest.kt b/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppArchiveButtonTest.kt index 2afb3f1ee62..ac8275600b8 100644 --- a/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppArchiveButtonTest.kt +++ b/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppArchiveButtonTest.kt @@ -109,6 +109,19 @@ class AppArchiveButtonTest { assertThat(enabledActionButton.enabled).isFalse() } + @Test + fun appArchiveButton_whenPackageIsNotFound_isDisabled() { + val app = ApplicationInfo().apply { + packageName = PACKAGE_NAME + isArchived = false + } + whenever(userPackageManager.isAppArchivable(app.packageName)).thenThrow(PackageManager.NameNotFoundException()) + + val actionButton = setContent(app) + + assertThat(actionButton.enabled).isFalse() + } + @Test fun appArchiveButton_displaysRightTextAndIcon() { val app = ApplicationInfo().apply {