-
Notifications
You must be signed in to change notification settings - Fork 731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make widget web view request system permissions for camera and microphone (PSF-1061) #6149
Changes from 1 commit
59c13bf
9469027
580bbd6
6ec6d41
9e084ec
92a140b
73d7864
d757914
769b217
75da988
5b64946
70bb2b7
3739919
4ebb26d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if we could use existing methods in PermissionTools file to avoid having other logic for permissions handling? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The registration methods available there, sadly don't expose the map of permission results: https://github.com/vector-im/element-android/blob/4094a66f3ce65d5fa92ac31b7749c731b3f33cf5/vector/src/main/java/im/vector/app/core/utils/PermissionsTools.kt#L67 |
||
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) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little weary of including mutable static state in the what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had also thought of this, yeah. I had previously decided against putting the state into the fragment because that way state and logic are split between the fragment and |
||
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.") | ||
Johennes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
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() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we want to inject this via dagger (not really needed whilst the
WebviewPermissionUtils
has no dependencies of its own) we can include the instance via the injectable constructor of the fragmentbecomes...
and the
WebviewPermissionUtils
itself will need to be marked as injectableFragments in Element are injectable because of the fragment module
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah nice! Thanks a lot for explaining. Made the change to be a good citizen and help remember for next time. 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
future us will appreciate the injectable boilerplate being done 😄