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 fragmentArgs: WidgetArgs by args()
private val viewModel: WidgetViewModel by activityViewModel() private val viewModel: WidgetViewModel by activityViewModel()
private val permissionUtils = WebviewPermissionUtils()
override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentRoomWidgetBinding { override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentRoomWidgetBinding {
return FragmentRoomWidgetBinding.inflate(inflater, container, false) return FragmentRoomWidgetBinding.inflate(inflater, container, false)
@ -277,16 +278,17 @@ class WidgetFragment @Inject constructor() :
} }
private val permissionResultLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result -> private val permissionResultLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result ->
WebviewPermissionUtils.onPermissionResult(result) permissionUtils.onPermissionResult(result)
} }
override fun onPermissionRequest(request: PermissionRequest) { override fun onPermissionRequest(request: PermissionRequest) {
WebviewPermissionUtils.promptForPermissions( permissionUtils.promptForPermissions(
title = R.string.room_widget_resource_permission_title, title = R.string.room_widget_resource_permission_title,
request = request, request = request,
context = requireContext(), context = requireContext(),
activity = requireActivity(), activity = requireActivity(),
activityResultLauncher = permissionResultLauncher) activityResultLauncher = permissionResultLauncher
)
} }
private fun displayTerms(displayTerms: WidgetViewEvents.DisplayTerms) { 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 com.google.android.material.dialog.MaterialAlertDialogBuilder
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.utils.checkPermissions import im.vector.app.core.utils.checkPermissions
import java.lang.NullPointerException
object WebviewPermissionUtils { class WebviewPermissionUtils {
private var permissionRequest: PermissionRequest? = null private var permissionRequest: PermissionRequest? = null
private var selectedPermissions = listOf<String>() private var selectedPermissions = listOf<String>()
@ -73,7 +74,9 @@ object WebviewPermissionUtils {
} }
fun onPermissionResult(result: Map<String, Boolean>) { fun onPermissionResult(result: Map<String, Boolean>) {
permissionRequest?.let { request -> if (permissionRequest == null) {
throw NullPointerException("permissionRequest was null! Make sure to call promptForPermissions first.")
}
val grantedPermissions = selectedPermissions.filter { webPermission -> val grantedPermissions = selectedPermissions.filter { webPermission ->
val androidPermission = webPermissionToAndroidPermission(webPermission) val androidPermission = webPermissionToAndroidPermission(webPermission)
?: return@filter true // No corresponding Android permission exists ?: return@filter true // No corresponding Android permission exists
@ -81,13 +84,12 @@ object WebviewPermissionUtils {
?: return@filter true // Android permission already granted before ?: return@filter true // Android permission already granted before
} }
if (grantedPermissions.isNotEmpty()) { if (grantedPermissions.isNotEmpty()) {
request.grant(grantedPermissions.toTypedArray()) permissionRequest?.grant(grantedPermissions.toTypedArray())
} else { } else {
request.deny() permissionRequest?.deny()
} }
reset() reset()
} }
}
private fun reset() { private fun reset() {
permissionRequest = null permissionRequest = null