Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vaetas
Copy link

@vaetas vaetas commented Feb 20, 2024

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!

@@ -126,12 +126,35 @@ public InAppWebViewChromeClient(@NonNull final InAppWebViewFlutterPlugin plugin,
this.inAppBrowserDelegate.getActivityResultListeners().add(this);
}

if (plugin.registrar != null)
if (plugin.registrar != null) {

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)

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);

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.

@faaqi
Copy link

faaqi commented May 7, 2024

Hi @vaetas @balajiks-dev

Can u suggest any solution for this issue?

2108

@EArminjon
Copy link
Contributor

Hi @vaetas @balajiks-dev

Can u suggest any solution for this issue?

2108

Issue mentioned closed.

@EArminjon
Copy link
Contributor

Is this PR ready ?

@EArminjon
Copy link
Contributor

EArminjon commented Jun 3, 2024

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

<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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants