Make permission utils instantiable and throw when permission request is null

This commit is contained in:
Johannes Marbach 2022-05-25 15:53:58 +02:00
parent 580bbd60e7
commit 6ec6d41aa9
2 changed files with 21 additions and 17 deletions

View File

@ -72,6 +72,7 @@ class WidgetFragment @Inject constructor() :
private val fragmentArgs: WidgetArgs by args()
private val viewModel: WidgetViewModel by activityViewModel()
private val permissionUtils = WebviewPermissionUtils()
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentRoomWidgetBinding {
return FragmentRoomWidgetBinding.inflate(inflater, container, false)
@ -277,16 +278,17 @@ class WidgetFragment @Inject constructor() :
}
private val permissionResultLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result ->
WebviewPermissionUtils.onPermissionResult(result)
permissionUtils.onPermissionResult(result)
}
override fun onPermissionRequest(request: PermissionRequest) {
WebviewPermissionUtils.promptForPermissions(
permissionUtils.promptForPermissions(
title = R.string.room_widget_resource_permission_title,
request = request,
context = requireContext(),
activity = requireActivity(),
activityResultLauncher = permissionResultLauncher)
activityResultLauncher = permissionResultLauncher
)
}
private fun displayTerms(displayTerms: WidgetViewEvents.DisplayTerms) {

View File

@ -25,8 +25,9 @@ import androidx.fragment.app.FragmentActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import im.vector.app.R
import im.vector.app.core.utils.checkPermissions
import java.lang.NullPointerException
object WebviewPermissionUtils {
class WebviewPermissionUtils {
private var permissionRequest: PermissionRequest? = null
private var selectedPermissions = listOf<String>()
@ -73,20 +74,21 @@ object WebviewPermissionUtils {
}
fun onPermissionResult(result: Map<String, Boolean>) {
permissionRequest?.let { request ->
val grantedPermissions = selectedPermissions.filter { webPermission ->
val androidPermission = webPermissionToAndroidPermission(webPermission)
?: return@filter true // No corresponding Android permission exists
return@filter result[androidPermission]
?: return@filter true // Android permission already granted before
}
if (grantedPermissions.isNotEmpty()) {
request.grant(grantedPermissions.toTypedArray())
} else {
request.deny()
}
reset()
if (permissionRequest == null) {
throw NullPointerException("permissionRequest was null! Make sure to call promptForPermissions first.")
}
val grantedPermissions = selectedPermissions.filter { webPermission ->
val androidPermission = webPermissionToAndroidPermission(webPermission)
?: return@filter true // No corresponding Android permission exists
return@filter result[androidPermission]
?: return@filter true // Android permission already granted before
}
if (grantedPermissions.isNotEmpty()) {
permissionRequest?.grant(grantedPermissions.toTypedArray())
} else {
permissionRequest?.deny()
}
reset()
}
private fun reset() {