-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix: handle permissions for getUserMedia #1895
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -23,6 +23,9 @@ Licensed to the Apache Software Foundation (ASF) under one | |||||
| import java.util.Arrays; | ||||||
| import java.util.ArrayList; | ||||||
| import java.util.List; | ||||||
| import java.util.Map; | ||||||
|
|
||||||
| import android.Manifest; | ||||||
| import android.app.Activity; | ||||||
| import android.content.ClipData; | ||||||
| import android.content.Context; | ||||||
|
|
@@ -45,6 +48,10 @@ Licensed to the Apache Software Foundation (ASF) under one | |||||
| import android.widget.LinearLayout; | ||||||
| import android.widget.ProgressBar; | ||||||
| import android.widget.RelativeLayout; | ||||||
|
|
||||||
| import androidx.activity.result.ActivityResultCallback; | ||||||
| import androidx.activity.result.ActivityResultLauncher; | ||||||
| import androidx.activity.result.contract.ActivityResultContracts; | ||||||
| import androidx.core.content.FileProvider; | ||||||
|
|
||||||
| import org.apache.cordova.CordovaDialogsHelper; | ||||||
|
|
@@ -59,6 +66,12 @@ Licensed to the Apache Software Foundation (ASF) under one | |||||
| */ | ||||||
| public class SystemWebChromeClient extends WebChromeClient { | ||||||
|
|
||||||
| private interface PermissionListener { | ||||||
| void onPermissionSelect(Boolean isGranted); | ||||||
| } | ||||||
|
|
||||||
| private ActivityResultLauncher permissionLauncher; | ||||||
|
Member
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. There were a couple of warnings in Android Studio:
and
Would it be safe to use the following?
Suggested change
|
||||||
| private PermissionListener permissionListener; | ||||||
| private static final int FILECHOOSER_RESULTCODE = 5173; | ||||||
| private static final String LOG_TAG = "SystemWebChromeClient"; | ||||||
| private long MAX_QUOTA = 100 * 1024 * 1024; | ||||||
|
|
@@ -77,6 +90,15 @@ public SystemWebChromeClient(SystemWebViewEngine parentEngine) { | |||||
| this.parentEngine = parentEngine; | ||||||
| appContext = parentEngine.webView.getContext(); | ||||||
| dialogsHelper = new CordovaDialogsHelper(appContext); | ||||||
| permissionLauncher = parentEngine.cordova.getActivity().registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), isGranted -> { | ||||||
| if (permissionListener != null) { | ||||||
| boolean granted = true; | ||||||
| for (Map.Entry<String, Boolean> permission : isGranted.entrySet()) { | ||||||
| if (!permission.getValue()) granted = false; | ||||||
| } | ||||||
| permissionListener.onPermissionSelect(granted); | ||||||
| } | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -325,7 +347,27 @@ private Uri createUriForFile(Context context, File tempFile) throws IOException | |||||
| @Override | ||||||
| public void onPermissionRequest(final PermissionRequest request) { | ||||||
| LOG.d(LOG_TAG, "onPermissionRequest: " + Arrays.toString(request.getResources())); | ||||||
| request.grant(request.getResources()); | ||||||
| List<String> permissionList = new ArrayList<>(); | ||||||
| if (Arrays.asList(request.getResources()).contains("android.webkit.resource.VIDEO_CAPTURE")) { | ||||||
| permissionList.add(Manifest.permission.CAMERA); | ||||||
| } | ||||||
| if (Arrays.asList(request.getResources()).contains("android.webkit.resource.AUDIO_CAPTURE")) { | ||||||
| permissionList.add(Manifest.permission.MODIFY_AUDIO_SETTINGS); | ||||||
| permissionList.add(Manifest.permission.RECORD_AUDIO); | ||||||
| } | ||||||
| if (!permissionList.isEmpty()) { | ||||||
| String[] permissions = permissionList.toArray(new String[0]); | ||||||
| permissionListener = (isGranted) -> { | ||||||
| if (isGranted) { | ||||||
| request.grant(request.getResources()); | ||||||
| } else { | ||||||
| request.deny(); | ||||||
| } | ||||||
| }; | ||||||
| permissionLauncher.launch(permissions); | ||||||
|
Member
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 following warning appears when
|
||||||
| } else { | ||||||
| request.grant(request.getResources()); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| public void destroyLastDialog(){ | ||||||
|
|
||||||
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.
Can you confirm if this import is needed?
I don't see it used anywhere within code.