diff --git a/src/com/android/settings/notification/app/FullScreenIntentPermissionPreferenceController.kt b/src/com/android/settings/notification/app/FullScreenIntentPermissionPreferenceController.kt index 2cc26f680bd..4349b4c9f4e 100644 --- a/src/com/android/settings/notification/app/FullScreenIntentPermissionPreferenceController.kt +++ b/src/com/android/settings/notification/app/FullScreenIntentPermissionPreferenceController.kt @@ -21,6 +21,7 @@ import android.app.AppOpsManager import android.app.AppOpsManager.OP_USE_FULL_SCREEN_INTENT import android.content.AttributionSource import android.content.Context +import android.content.pm.PackageManager.NameNotFoundException import android.content.pm.PackageManager.FLAG_PERMISSION_USER_SET import android.content.pm.PackageManager.GET_PERMISSIONS import android.os.UserHandle @@ -79,12 +80,16 @@ class FullScreenIntentPermissionPreferenceController( } private fun isPermissionRequested(): Boolean { - val packageInfo = packageManager.getPackageInfo(packageName, GET_PERMISSIONS) + try { + val packageInfo = packageManager.getPackageInfo(packageName, GET_PERMISSIONS) - for (requestedPermission in packageInfo.requestedPermissions.orEmpty()) { - if (USE_FULL_SCREEN_INTENT.equals(requestedPermission)) { - return true + for (requestedPermission in packageInfo.requestedPermissions.orEmpty()) { + if (USE_FULL_SCREEN_INTENT.equals(requestedPermission)) { + return true + } } + } catch (exception: NameNotFoundException) { + Log.e(TAG, "isPermissionRequested failed: $exception") } return false diff --git a/tests/robotests/src/com/android/settings/notification/app/FullScreenIntentPermissionPreferenceControllerTest.kt b/tests/robotests/src/com/android/settings/notification/app/FullScreenIntentPermissionPreferenceControllerTest.kt index 64bb849b7e3..89e33eef509 100644 --- a/tests/robotests/src/com/android/settings/notification/app/FullScreenIntentPermissionPreferenceControllerTest.kt +++ b/tests/robotests/src/com/android/settings/notification/app/FullScreenIntentPermissionPreferenceControllerTest.kt @@ -30,6 +30,7 @@ import android.content.pm.PackageInfo import android.content.pm.PackageManager import android.content.pm.PackageManager.FLAG_PERMISSION_USER_SET import android.content.pm.PackageManager.GET_PERMISSIONS +import android.content.pm.PackageManager.NameNotFoundException import android.os.UserHandle import android.permission.PermissionManager.PERMISSION_GRANTED import android.permission.PermissionManager.PERMISSION_HARD_DENIED @@ -133,6 +134,14 @@ class FullScreenIntentPermissionPreferenceControllerTest { assertTrue(controller.isAvailable) } + @Test + fun testIsAvailable_notWhenPackageNotFound() { + setPackageInfoNotFound() + initController() + + assertFalse(controller.isAvailable) + } + @Test fun testIsEnabled_notWhenDisabledByAdmin() { setPermissionRequestedInManifest() @@ -242,6 +251,12 @@ class FullScreenIntentPermissionPreferenceControllerTest { }) } + private fun setPackageInfoNotFound() { + whenever(packageManager.getPackageInfo(TEST_PACKAGE, GET_PERMISSIONS)).thenThrow( + NameNotFoundException(TEST_PACKAGE) + ) + } + private fun setPermissionResult(@PermissionResult result: Int) { ShadowPermissionChecker.setResult(TEST_PACKAGE, USE_FULL_SCREEN_INTENT, result) }