Skip to content

Commit

Permalink
Set agent mode support from selfiecapture screens: we want the selfie…
Browse files Browse the repository at this point in the history
… capture screens to check the agent support and alert the capture screens so that the

1. right permission is requested
2. we don't make duplicate agent support checks
  • Loading branch information
ayinloya committed Aug 26, 2024
1 parent 333fe08 commit bf06005
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import { version as COMPONENTS_VERSION } from '../../../package.json';

async function getPermissions(captureScreen, facingMode = 'user') {
try {
await SmartCamera.getMedia({ audio: false, video: { facingMode } });
const supportAgentMode = await SmartCamera.supportsAgentMode();
const _facingMode = supportAgentMode ? facingMode : 'user';
await SmartCamera.getMedia({ audio: false, video: { facingMode: _facingMode } });
captureScreen.removeAttribute('data-camera-error');
captureScreen.setAttribute('data-camera-ready', true);
if (supportAgentMode) {
captureScreen.setAttribute('has-agent-support', true);
}
} catch (error) {
captureScreen.removeAttribute('data-camera-ready');
captureScreen.setAttribute(
Expand Down Expand Up @@ -50,6 +55,7 @@ class SelfieCaptureScreens extends HTMLElement {
getPermissions(this.selfieCapture, this.getAgentMode());
}

// If the initial screen is selfie-capture, we need to get permissions
if (this.getAttribute('initial-screen') === 'selfie-capture') {
getPermissions(this.selfieCapture, this.getAgentMode());
this.setActiveScreen(this.selfieCapture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ class SelfieCaptureScreen extends HTMLElement {
return;
}

const supportAgentMode = await this.supportsAgentMode();
const supportAgentMode = this.hasAgentSupport ?? await SmartCamera.supportsAgentMode();

if (supportAgentMode || this.hasAttribute('show-agent-mode-for-tests')) {
this.switchCamera.hidden = false;
Expand All @@ -871,32 +871,8 @@ class SelfieCaptureScreen extends HTMLElement {
}
}

async supportsAgentMode() {
try {
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter(
(device) => device.kind === 'videoinput',
);

let hasBackCamera = false;

videoDevices.forEach((device) => {
// Check if the device label or device ID indicates a back camera
if (
device.label.toLowerCase().includes('back') ||
device.label.toLowerCase().includes('rear')
) {
hasBackCamera = true;
return true;
}
return false;
});

return hasBackCamera;
} catch (error) {
console.warn('Error accessing media devices: ', error);
return false;
}
get hasAgentSupport() {
return this.hasAttribute('has-agent-support');
}

get title() {
Expand Down
28 changes: 28 additions & 0 deletions packages/web-components/domain/camera/src/SmartCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,34 @@ class SmartCamera {
}
}

static async supportsAgentMode() {
try {
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter(
(device) => device.kind === 'videoinput',
);

let hasBackCamera = false;

videoDevices.forEach((device) => {
// Check if the device label or device ID indicates a back camera
if (
device.label.toLowerCase().includes('back') ||
device.label.toLowerCase().includes('rear')
) {
hasBackCamera = true;
return true;
}
return false;
});

return hasBackCamera;
} catch (error) {
console.warn('Error accessing media devices: ', error);
return false;
}
}

static isSamsungMultiCameraDevice() {
const matchedModelNumber = navigator.userAgent.match(/SM-[N|G]\d{3}/);
if (!matchedModelNumber) {
Expand Down

0 comments on commit bf06005

Please sign in to comment.