Check for NameNotFoundException when querying for PackageInfo

PackageManager.getPackageInfo will throw NameNotFoundException if package was/is being removed.

Test: atest FullScreenIntentPermissionPreferenceControllerTest
Bug: 296163845
Change-Id: I3242ac7e68fa7ed99fe99c7de7cd97aaf87948ee
This commit is contained in:
Valentin Iftime
2023-09-28 17:37:19 +02:00
parent a191396363
commit 68916a9bbf
2 changed files with 24 additions and 4 deletions

View File

@@ -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