Auto grant WebView permissions if they are already granted system level.
This commit is contained in:
parent
75ab0aef53
commit
9090e37a0f
@ -20,6 +20,7 @@ import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.webkit.PermissionRequest
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
@ -43,6 +44,7 @@ val PERMISSIONS_FOR_ROOM_AVATAR = listOf(Manifest.permission.CAMERA)
|
||||
val PERMISSIONS_FOR_WRITING_FILES = listOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||
val PERMISSIONS_FOR_PICKING_CONTACT = listOf(Manifest.permission.READ_CONTACTS)
|
||||
val PERMISSIONS_FOR_FOREGROUND_LOCATION_SHARING = listOf(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
|
||||
// See https://developer.android.com/guide/topics/connectivity/bluetooth/permissions
|
||||
val PERMISSIONS_FOR_BLUETOOTH = when {
|
||||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
|
||||
@ -164,6 +166,32 @@ fun checkPermissions(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if required WebView permissions are already granted system level.
|
||||
* @param activity the calling Activity that is requesting the permissions (or fragment parent)
|
||||
* @param request WebView permission request of onPermissionRequest function
|
||||
* @return true if WebView permissions are already granted, false otherwise
|
||||
*/
|
||||
fun checkWebViewPermissions(activity: Activity, request: PermissionRequest): Boolean {
|
||||
return request.resources.all {
|
||||
when (it) {
|
||||
PermissionRequest.RESOURCE_AUDIO_CAPTURE -> {
|
||||
PERMISSIONS_FOR_AUDIO_IP_CALL.all { permission ->
|
||||
ContextCompat.checkSelfPermission(activity.applicationContext, permission) == PackageManager.PERMISSION_GRANTED
|
||||
}
|
||||
}
|
||||
PermissionRequest.RESOURCE_VIDEO_CAPTURE -> {
|
||||
PERMISSIONS_FOR_VIDEO_IP_CALL.all { permission ->
|
||||
ContextCompat.checkSelfPermission(activity.applicationContext, permission) == PackageManager.PERMISSION_GRANTED
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* To be call after the permission request.
|
||||
*
|
||||
|
||||
@ -105,7 +105,7 @@ class WidgetFragment @Inject constructor(
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
setHasOptionsMenu(true)
|
||||
views.widgetWebView.setupForWidget(this)
|
||||
views.widgetWebView.setupForWidget(requireActivity(), this)
|
||||
if (fragmentArgs.kind.isAdmin()) {
|
||||
viewModel.getPostAPIMediator().setWebView(views.widgetWebView)
|
||||
}
|
||||
|
||||
@ -17,18 +17,20 @@
|
||||
package im.vector.app.features.widgets.webview
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.view.ViewGroup
|
||||
import android.webkit.CookieManager
|
||||
import android.webkit.PermissionRequest
|
||||
import android.webkit.WebChromeClient
|
||||
import android.webkit.WebView
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.utils.checkWebViewPermissions
|
||||
import im.vector.app.features.themes.ThemeUtils
|
||||
import im.vector.app.features.webview.VectorWebViewClient
|
||||
import im.vector.app.features.webview.WebEventListener
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
fun WebView.setupForWidget(eventListener: WebEventListener) {
|
||||
fun WebView.setupForWidget(activity: Activity, eventListener: WebEventListener) {
|
||||
// xml value seems ignored
|
||||
setBackgroundColor(ThemeUtils.getColor(context, R.attr.colorSurface))
|
||||
|
||||
@ -59,7 +61,11 @@ fun WebView.setupForWidget(eventListener: WebEventListener) {
|
||||
// Permission requests
|
||||
webChromeClient = object : WebChromeClient() {
|
||||
override fun onPermissionRequest(request: PermissionRequest) {
|
||||
eventListener.onPermissionRequest(request)
|
||||
if (checkWebViewPermissions(activity, request)) {
|
||||
request.grant(request.resources)
|
||||
} else {
|
||||
eventListener.onPermissionRequest(request)
|
||||
}
|
||||
}
|
||||
}
|
||||
webViewClient = VectorWebViewClient(eventListener)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user