-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
android: request camera permission for inputs #2033
base: master
Are you sure you want to change the base?
android: request camera permission for inputs #2033
Conversation
@@ -126,12 +126,35 @@ public InAppWebViewChromeClient(@NonNull final InAppWebViewFlutterPlugin plugin, | |||
this.inAppBrowserDelegate.getActivityResultListeners().add(this); | |||
} | |||
|
|||
if (plugin.registrar != null) | |||
if (plugin.registrar != null) { |
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.
Check the brackets
@@ -126,12 +126,35 @@ public InAppWebViewChromeClient(@NonNull final InAppWebViewFlutterPlugin plugin, | |||
this.inAppBrowserDelegate.getActivityResultListeners().add(this); | |||
} | |||
|
|||
if (plugin.registrar != null) |
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.
Duplication of line
// Request camera permission first. The picker intent will be started once the | ||
// permission is either granted or denied. | ||
if (captureEnabled && needsCameraPermission() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
getActivity().requestPermissions(new String[]{Manifest.permission.CAMERA}, CAMERA_PICKER); |
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.
Add permission for gallery and local files.
Can u suggest any solution for this issue? |
Issue mentioned closed. |
Is this PR ready ? |
I don't think this PR is valid because it didn't use onPermissionRequest. To use onPermissionRequest I did as follow : Inside onWebViewCreated callback : if (Platform.isAndroid) {
controller.addUserScript(
userScript: UserScript(
source: """
var fileInput = document.querySelector('input[type="file"][accept]');
if (fileInput) {
var handleClick = async function(event) {
event.preventDefault();
// check permission
await navigator.mediaDevices.getUserMedia({ video: true, audio: false })
// getUserMedia start camera, we need to stop it
.then(function(stream) {
stream.getTracks().forEach(function(track) {
track.stop();
});
})
// We didn't need to manage exception but we need to catch them
.catch(function() {});
fileInput.removeEventListener('click', handleClick);
fileInput.click();
fileInput.addEventListener('click', handleClick);
};
fileInput.addEventListener('click', handleClick);
}
""",
forMainFrameOnly: true,
injectionTime: UserScriptInjectionTime.AT_DOCUMENT_END,
),
);
} That well trigger onPermissionRequest. Inside onPermissionRequest callback : static Future<PermissionResponse?> onPermissionRequest(
InAppWebViewController controller,
PermissionRequest request,
) async {
if (request.resources.contains(PermissionResourceType.CAMERA)) {
// request permission using https://pub.dev/packages/permission_handler
final bool result = await getIt<PermissionService>().requestCameraPermission();
return PermissionResponse(
resources: request.resources,
action: result
? PermissionResponseAction.GRANT
: PermissionResponseAction.PROMPT,
);
}
return null;
} Under your activity inside AndroidManifest.xml <uses-permission android:name="android.permission.VIDEO_CAPTURE"/>
<uses-permission android:name="android.permission.CAMERA"/>
or
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries> As for Ajax Handlers, maybe a similar code can be implemented inside the package to automatically do the same process. |
It's because of an another package. The issue mentioned is already Closed. |
This PR tried to implement requesting user permissions on Android when the input field contains
capture
param. Requesting camera permission on app startup is not the best UX. This PR tries to delay the permission request until the user actually requires this action.Testing and Review Notes
You can pass this URL with into the example project WebView: https://12t031.csb.app/
One input is with
capture
while the other one is regular.Clicking on
capture
input should on Android request permissions. When the request is either accepted or denied then the file picker is resumed. When user accepts the permission the file picker contains the option to take a picture with camera.To Do
I don't regularly touch Java code and there is high chance this is not the best approach. However, this feature is important for me. Let me know if there are any changes need so it could be possibly merged and published.
Thank you!