Auto grant WebView permissions if they are already granted system level.

This commit is contained in:
Onuray Sahin 2022-07-06 15:22:28 +03:00
parent a0eb2e733c
commit f5ec7a312f
3 changed files with 36 additions and 3 deletions

View File

@ -19,6 +19,7 @@ package im.vector.app.core.utils
import android.Manifest
import android.app.Activity
import android.content.pm.PackageManager
import android.webkit.PermissionRequest
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
@ -137,6 +138,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.
*

View File

@ -81,7 +81,7 @@ class WidgetFragment @Inject constructor(
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
views.widgetWebView.setupForWidget(this)
views.widgetWebView.setupForWidget(requireActivity(), this)
if (fragmentArgs.kind.isAdmin()) {
viewModel.getPostAPIMediator().setWebView(views.widgetWebView)
}

View File

@ -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,9 +61,13 @@ fun WebView.setupForWidget(eventListener: WebEventListener) {
// Permission requests
webChromeClient = object : WebChromeClient() {
override fun onPermissionRequest(request: PermissionRequest) {
if (checkWebViewPermissions(activity, request)) {
request.grant(request.resources)
} else {
eventListener.onPermissionRequest(request)
}
}
}
webViewClient = VectorWebViewClient(eventListener)
val cookieManager = CookieManager.getInstance()