fix: handle permissions for getUserMedia#1895
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1895 +/- ##
=======================================
Coverage 61.43% 61.43%
=======================================
Files 24 24
Lines 4922 4922
=======================================
Hits 3024 3024
Misses 1898 1898 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
erisu
left a comment
There was a problem hiding this comment.
Overall, the code looks good to me.
I left a couple of change requests. Can you confirm if they're OK?
I also tested the following use case, with and without my suggested changes:
- Create a default Cordova application
- Add the following to
config.xml:
<platform name="android">
<config-file target="AndroidManifest.xml" parent="/*" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
</config-file>
</platform>- Add the following to
index.html:
<video id="video" autoplay playsinline></video>
<script>
navigator.mediaDevices.getUserMedia({ video: true }).then(stream => {
const video = document.getElementById('video');
video.srcObject = stream;
}).catch(error => {
console.error('Camera Access Error:', error);
});
</script>- If
uses-permission/uses-featureare not in the AndroidManifest file, nothing happens when launching the app. - If
uses-permission/uses-featureare defined in the AndroidManifest file, a runtime permission dialog appears when the app launches.
| import android.widget.ProgressBar; | ||
| import android.widget.RelativeLayout; | ||
|
|
||
| import androidx.activity.result.ActivityResultCallback; |
There was a problem hiding this comment.
Can you confirm if this import is needed?
I don't see it used anywhere within code.
| void onPermissionSelect(Boolean isGranted); | ||
| } | ||
|
|
||
| private ActivityResultLauncher permissionLauncher; |
There was a problem hiding this comment.
There were a couple of warnings in Android Studio:
Raw use of parameterized class 'ActivityResultLauncher'
and
Field 'permissionLauncher' may be 'final'
Would it be safe to use the following?
| private ActivityResultLauncher permissionLauncher; | |
| private final ActivityResultLauncher<String[]> permissionLauncher; |
| request.deny(); | ||
| } | ||
| }; | ||
| permissionLauncher.launch(permissions); |
There was a problem hiding this comment.
The following warning appears when permissionLauncher is declared as ActivityResultLauncher instead of ActivityResultLauncher<String[]>.
Unchecked call to 'launch(I)' as a member of raw type 'androidx.activity.result.ActivityResultLauncher'
closes #1888
The code is based on Capacitor's implementation.
It uses newer
ActivityResultCallback/ActivityResultLauncherinstead of the oldrequestPermissions.This could later on be extended to request geolocation permissions as at the moment it requires the Geolocation plugin to be installed.