mirror of
https://github.com/element-hq/element-android
synced 2024-11-28 13:38:49 +03:00
Make permission utils instantiable and throw when permission request is null
This commit is contained in:
parent
580bbd60e7
commit
6ec6d41aa9
2 changed files with 21 additions and 17 deletions
|
@ -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) {
|
||||||
|
|
|
@ -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,20 +74,21 @@ object WebviewPermissionUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onPermissionResult(result: Map<String, Boolean>) {
|
fun onPermissionResult(result: Map<String, Boolean>) {
|
||||||
permissionRequest?.let { request ->
|
if (permissionRequest == null) {
|
||||||
val grantedPermissions = selectedPermissions.filter { webPermission ->
|
throw NullPointerException("permissionRequest was null! Make sure to call promptForPermissions first.")
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
|
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() {
|
private fun reset() {
|
||||||
|
|
Loading…
Reference in a new issue