From e7b621c088e7a8882c7e5207081c8c705b48b87c Mon Sep 17 00:00:00 2001 From: Joao Leonardo Pereira Date: Tue, 27 Aug 2024 13:53:43 -0300 Subject: [PATCH 1/3] test flashlight fix --- src/app/home/home.page.ts | 7 ++- src/app/utils/global-utils.ts | 110 +++++++++++++++++++++++++++++++++- 2 files changed, 114 insertions(+), 3 deletions(-) diff --git a/src/app/home/home.page.ts b/src/app/home/home.page.ts index 1a4a60a..0b6d022 100644 --- a/src/app/home/home.page.ts +++ b/src/app/home/home.page.ts @@ -8,6 +8,7 @@ import { LogoService } from '../services/logo.service'; import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; import { NgxScannerQrcodeComponent, ScannerQRCodeConfig, ScannerQRCodeResult } from 'ngx-scanner-qrcode'; import { LocalStorageService } from '../services/local-storage.service'; +import { GlobalUtils } from '../utils/global-utils'; @Component({ selector: 'app-home', @@ -354,8 +355,10 @@ export class HomePage implements OnInit { } async toggleTorch() { - const currentState = await firstValueFrom(this.qrscanner.torcher()) - this.qrscanner.isTorch = !currentState + // const currentState = await firstValueFrom(this.qrscanner.torcher()) + // this.qrscanner.isTorch = !currentState + const currentState = await GlobalUtils.getFlashlightStatus() + await GlobalUtils.setFlashlightStatus(!currentState) } private processQRCode(evt: string) { diff --git a/src/app/utils/global-utils.ts b/src/app/utils/global-utils.ts index b1ba82f..2c3a0ff 100644 --- a/src/app/utils/global-utils.ts +++ b/src/app/utils/global-utils.ts @@ -2,4 +2,112 @@ export class GlobalUtils { static isMobile() { return window.innerWidth <= 768; } -} \ No newline at end of file + + static track: any + + static async accessFlashlight() { + //Test browser support + if (!('mediaDevices' in window.navigator)) { + console.log("Media Devices not available. Use HTTPS!"); + return; + }; + + //Get the environment camera (usually the second one) + const devices = await window.navigator.mediaDevices.enumerateDevices() + + const cameras = devices.filter((device) => device.kind === 'videoinput'); + if (cameras.length === 0) { + console.log("No camera found. If your device has camera available, check permissions."); + return; + }; + + const camera = cameras[cameras.length - 1]; + + const stream = await window.navigator.mediaDevices.getUserMedia({ + video: { deviceId: camera.deviceId } + }) + + this.track = stream.getVideoTracks()[0]; + + if (!(this.track.getCapabilities().torch)) { + console.log("No torch available."); + } + + } + + static async getFlashlightStatus() { + if (!this.track) { + console.log("Flashlight not accessed. Trying to access now."); + await this.accessFlashlight(); + } + if (!this.track) { + console.log("Flashlight not accessed."); + return; + } + const settings = this.track.getSettings(); + return settings.torch; + } + + static async setFlashlightStatus(status: boolean) { + if (!this.track) { + console.log("Flashlight not accessed. Trying to access now."); + await this.accessFlashlight(); + } + if (!this.track) { + console.log("Flashlight not accessed."); + return; + } + this.track.applyConstraints({ + advanced: [{ + torch: status + }] + }); + } +} + +/** + * class flashlightHandler { + + static track; //the video track which is used to turn on/off the flashlight + + static accessFlashlight() { + //Test browser support + if (!('mediaDevices' in window.navigator)) { + alert("Media Devices not available. Use HTTPS!"); + return; + }; + + //Get the environment camera (usually the second one) + window.navigator.mediaDevices.enumerateDevices().then((devices) => { + + const cameras = devices.filter((device) => device.kind === 'videoinput'); + if (cameras.length === 0) { + alert("No camera found. If your device has camera available, check permissions."); + return; + }; + + const camera = cameras[cameras.length - 1]; + + window.navigator.mediaDevices.getUserMedia({ + video: { + deviceId: camera.deviceId + } + }).then((stream) => { + this.track = stream.getVideoTracks()[0]; + + if (!(this.track.getCapabilities().torch)) { + alert("No torch available."); + }; + }); + }); + } + + static setFlashlightStatus(status) { + this.track.applyConstraints({ + advanced: [{ + torch: status + }] + }); + } +} + */ \ No newline at end of file From af56eb918994813982713e2c96f6ef2c81f5fffd Mon Sep 17 00:00:00 2001 From: Joao Leonardo Pereira Date: Tue, 27 Aug 2024 14:00:53 -0300 Subject: [PATCH 2/3] get the proper active track to toggle flash --- src/app/utils/global-utils.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/app/utils/global-utils.ts b/src/app/utils/global-utils.ts index 2c3a0ff..ccd57ad 100644 --- a/src/app/utils/global-utils.ts +++ b/src/app/utils/global-utils.ts @@ -27,7 +27,13 @@ export class GlobalUtils { video: { deviceId: camera.deviceId } }) - this.track = stream.getVideoTracks()[0]; + const track = stream.getVideoTracks().find(track => track.readyState === 'live'); + if (!track) { + console.log("No Active track found."); + return; + } + + this.track = track; if (!(this.track.getCapabilities().torch)) { console.log("No torch available."); @@ -36,10 +42,7 @@ export class GlobalUtils { } static async getFlashlightStatus() { - if (!this.track) { - console.log("Flashlight not accessed. Trying to access now."); - await this.accessFlashlight(); - } + await this.accessFlashlight(); if (!this.track) { console.log("Flashlight not accessed."); return; @@ -49,10 +52,7 @@ export class GlobalUtils { } static async setFlashlightStatus(status: boolean) { - if (!this.track) { - console.log("Flashlight not accessed. Trying to access now."); - await this.accessFlashlight(); - } + await this.accessFlashlight(); if (!this.track) { console.log("Flashlight not accessed."); return; From 213d9b1858a7134570279cea362b5ae4354ca699 Mon Sep 17 00:00:00 2001 From: Joao Leonardo Pereira Date: Wed, 28 Aug 2024 09:28:59 -0300 Subject: [PATCH 3/3] Remove flashlight button - We actually don't need the torch, as we'll almost always read from a computer screen --- src/app/home/home.page.html | 3 - src/app/home/home.page.ts | 10 +--- src/app/utils/global-utils.ts | 108 ---------------------------------- 3 files changed, 2 insertions(+), 119 deletions(-) diff --git a/src/app/home/home.page.html b/src/app/home/home.page.html index edff62b..5139ccf 100644 --- a/src/app/home/home.page.html +++ b/src/app/home/home.page.html @@ -79,9 +79,6 @@ - - - diff --git a/src/app/home/home.page.ts b/src/app/home/home.page.ts index 0b6d022..de6df36 100644 --- a/src/app/home/home.page.ts +++ b/src/app/home/home.page.ts @@ -354,15 +354,9 @@ export class HomePage implements OnInit { this.qrscanner.playDevice(nextDevice.deviceId) } - async toggleTorch() { - // const currentState = await firstValueFrom(this.qrscanner.torcher()) - // this.qrscanner.isTorch = !currentState - const currentState = await GlobalUtils.getFlashlightStatus() - await GlobalUtils.setFlashlightStatus(!currentState) - } - private processQRCode(evt: string) { - // otpauth://totp/Google:My%20Account?secret=JBSWY3D&issuer=Google&algorithm=SHA1&digits=6&period=30 + // The URI format and params is described in https://github.com/google/google-authenticator/wiki/Key-Uri-Format + // otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30 try { const account = Account2FA.fromOTPAuthURL(evt) console.log({account}) diff --git a/src/app/utils/global-utils.ts b/src/app/utils/global-utils.ts index ccd57ad..11f5381 100644 --- a/src/app/utils/global-utils.ts +++ b/src/app/utils/global-utils.ts @@ -2,112 +2,4 @@ export class GlobalUtils { static isMobile() { return window.innerWidth <= 768; } - - static track: any - - static async accessFlashlight() { - //Test browser support - if (!('mediaDevices' in window.navigator)) { - console.log("Media Devices not available. Use HTTPS!"); - return; - }; - - //Get the environment camera (usually the second one) - const devices = await window.navigator.mediaDevices.enumerateDevices() - - const cameras = devices.filter((device) => device.kind === 'videoinput'); - if (cameras.length === 0) { - console.log("No camera found. If your device has camera available, check permissions."); - return; - }; - - const camera = cameras[cameras.length - 1]; - - const stream = await window.navigator.mediaDevices.getUserMedia({ - video: { deviceId: camera.deviceId } - }) - - const track = stream.getVideoTracks().find(track => track.readyState === 'live'); - if (!track) { - console.log("No Active track found."); - return; - } - - this.track = track; - - if (!(this.track.getCapabilities().torch)) { - console.log("No torch available."); - } - - } - - static async getFlashlightStatus() { - await this.accessFlashlight(); - if (!this.track) { - console.log("Flashlight not accessed."); - return; - } - const settings = this.track.getSettings(); - return settings.torch; - } - - static async setFlashlightStatus(status: boolean) { - await this.accessFlashlight(); - if (!this.track) { - console.log("Flashlight not accessed."); - return; - } - this.track.applyConstraints({ - advanced: [{ - torch: status - }] - }); - } -} - -/** - * class flashlightHandler { - - static track; //the video track which is used to turn on/off the flashlight - - static accessFlashlight() { - //Test browser support - if (!('mediaDevices' in window.navigator)) { - alert("Media Devices not available. Use HTTPS!"); - return; - }; - - //Get the environment camera (usually the second one) - window.navigator.mediaDevices.enumerateDevices().then((devices) => { - - const cameras = devices.filter((device) => device.kind === 'videoinput'); - if (cameras.length === 0) { - alert("No camera found. If your device has camera available, check permissions."); - return; - }; - - const camera = cameras[cameras.length - 1]; - - window.navigator.mediaDevices.getUserMedia({ - video: { - deviceId: camera.deviceId - } - }).then((stream) => { - this.track = stream.getVideoTracks()[0]; - - if (!(this.track.getCapabilities().torch)) { - alert("No torch available."); - }; - }); - }); - } - - static setFlashlightStatus(status) { - this.track.applyConstraints({ - advanced: [{ - torch: status - }] - }); - } } - */ \ No newline at end of file