Skip to content

fix: handle permissions for getUserMedia#1895

Open
jcesarmobile wants to merge 2 commits intomasterfrom
getusermedia-perm
Open

fix: handle permissions for getUserMedia#1895
jcesarmobile wants to merge 2 commits intomasterfrom
getusermedia-perm

Conversation

@jcesarmobile
Copy link
Member

closes #1888

The code is based on Capacitor's implementation.

It uses newer ActivityResultCallback/ActivityResultLauncher instead of the old requestPermissions.

This could later on be extended to request geolocation permissions as at the moment it requires the Geolocation plugin to be installed.

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 61.43%. Comparing base (6b17223) to head (7106112).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
@breautek breautek added this to the 15.0.1 milestone Feb 25, 2026
Copy link
Member

@erisu erisu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Create a default Cordova application
  2. 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>
  1. 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-feature are not in the AndroidManifest file, nothing happens when launching the app.
  • If uses-permission/uses-feature are 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;
Copy link
Member

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.

void onPermissionSelect(Boolean isGranted);
}

private ActivityResultLauncher permissionLauncher;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Suggested change
private ActivityResultLauncher permissionLauncher;
private final ActivityResultLauncher<String[]> permissionLauncher;
request.deny();
}
};
permissionLauncher.launch(permissions);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants