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:
@@ -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
|
||||
|
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user