diff --git a/vector/src/main/java/im/vector/app/features/widgets/WidgetFragment.kt b/vector/src/main/java/im/vector/app/features/widgets/WidgetFragment.kt index c8a13d11cf..ed2c69c908 100644 --- a/vector/src/main/java/im/vector/app/features/widgets/WidgetFragment.kt +++ b/vector/src/main/java/im/vector/app/features/widgets/WidgetFragment.kt @@ -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) { diff --git a/vector/src/main/java/im/vector/app/features/widgets/webview/WebviewPermissionUtils.kt b/vector/src/main/java/im/vector/app/features/widgets/webview/WebviewPermissionUtils.kt index f1111a4650..3a39153581 100644 --- a/vector/src/main/java/im/vector/app/features/widgets/webview/WebviewPermissionUtils.kt +++ b/vector/src/main/java/im/vector/app/features/widgets/webview/WebviewPermissionUtils.kt @@ -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() @@ -73,20 +74,21 @@ object WebviewPermissionUtils { } fun onPermissionResult(result: Map) { - 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() {