diff --git a/App_Resources/iOS/Info.plist b/App_Resources/iOS/Info.plist index de7e7ba7..29862f9f 100644 --- a/App_Resources/iOS/Info.plist +++ b/App_Resources/iOS/Info.plist @@ -22,6 +22,8 @@ Faceclaw uses Bluetooth to connect to your glasses and ring, display your apps, and receive gestures. NSSpeechRecognitionUsageDescription Faceclaw turns audio from your glasses microphone into text for the app you choose. Dictation uses on-device recognition. + NSCameraUsageDescription + Faceclaw uses the camera to scan QR codes for app URLs you open in Developer. NSLocationWhenInUseUsageDescription Faceclaw uses your location for local weather, turn-by-turn navigation, and true north on the glasses compass. NSLocationTemporaryUsageDescriptionDictionary diff --git a/App_Resources/iOS/src/FaceclawQrScanner.h b/App_Resources/iOS/src/FaceclawQrScanner.h new file mode 100644 index 00000000..fd6f0c6a --- /dev/null +++ b/App_Resources/iOS/src/FaceclawQrScanner.h @@ -0,0 +1,8 @@ +#import + +/** A single foreground QR scan. Completion runs once, after the camera closes. */ +@interface FaceclawQrScanner : NSObject ++ (BOOL)isAvailable; +- (void)startWithCompletion:(void (^)(NSString *text, NSString *error))completion; +- (void)cancel; +@end diff --git a/App_Resources/iOS/src/FaceclawQrScanner.m b/App_Resources/iOS/src/FaceclawQrScanner.m new file mode 100644 index 00000000..5762f6af --- /dev/null +++ b/App_Resources/iOS/src/FaceclawQrScanner.m @@ -0,0 +1,186 @@ +#import "FaceclawQrScanner.h" +#import +#import + +@interface FaceclawQrScreen : UIViewController +@property(nonatomic) AVCaptureVideoPreviewLayer *preview; +@property(nonatomic, copy) void (^cancelHandler)(void); +@end + +@implementation FaceclawQrScreen +- (void)viewDidLoad { + [super viewDidLoad]; + self.view.backgroundColor = UIColor.blackColor; + self.title = @"Scan app QR code"; + self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] + initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelScan)]; + UILabel *instructions = [UILabel new]; + instructions.text = @"Point the camera at the app's QR code."; + instructions.textColor = UIColor.whiteColor; + instructions.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.7]; + instructions.textAlignment = NSTextAlignmentCenter; + instructions.numberOfLines = 0; + instructions.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; + instructions.adjustsFontForContentSizeCategory = YES; + instructions.translatesAutoresizingMaskIntoConstraints = NO; + [self.view addSubview:instructions]; + [NSLayoutConstraint activateConstraints:@[ + [instructions.leadingAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.leadingAnchor constant:20], + [instructions.trailingAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.trailingAnchor constant:-20], + [instructions.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor constant:-24], + ]]; +} +- (void)cancelScan { if (_cancelHandler) _cancelHandler(); } +- (void)viewDidLayoutSubviews { + [super viewDidLayoutSubviews]; + _preview.frame = self.view.bounds; + UIInterfaceOrientation orientation = self.view.window.windowScene.interfaceOrientation; + if (_preview.connection.isVideoOrientationSupported && orientation != UIInterfaceOrientationUnknown) + _preview.connection.videoOrientation = (AVCaptureVideoOrientation)orientation; +} +@end + +@interface FaceclawQrScanner () +@property(nonatomic) AVCaptureSession *session; +@property(nonatomic) AVCaptureMetadataOutput *output; +@property(nonatomic) dispatch_queue_t cameraQueue; +@property(nonatomic) FaceclawQrScreen *screen; +@property(nonatomic) UINavigationController *navigation; +@property(nonatomic, copy) void (^completion)(NSString *, NSString *); +@property(atomic) BOOL finished; +@property(nonatomic) BOOL presenting; +@property(nonatomic) BOOL stopped; +@property(nonatomic) BOOL closing; +@property(nonatomic) NSString *resultText; +@property(nonatomic) NSString *resultError; +@end + +@implementation FaceclawQrScanner ++ (BOOL)isAvailable { return [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] != nil; } +- (void)startWithCompletion:(void (^)(NSString *, NSString *))completion { + NSAssert(NSThread.isMainThread, @"Start QR scanner on the main thread"); + if (_completion || _finished) { completion(nil, @"This QR scanner has already been used."); return; } + _completion = [completion copy]; + _cameraQueue = dispatch_queue_create("com.faceclaw.qr-camera", DISPATCH_QUEUE_SERIAL); + if (UIApplication.sharedApplication.applicationState != UIApplicationStateActive) { + [self finish:nil error:@"Open Faceclaw on your iPhone to scan a QR code."]; return; + } + if (![FaceclawQrScanner isAvailable]) { + [self finish:nil error:@"No camera is available on this device."]; return; + } + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(backgrounded:) + name:UIApplicationDidEnterBackgroundNotification object:nil]; + AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; + if (status == AVAuthorizationStatusNotDetermined) { + [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.finished) return; + if (granted) [self presentScanner]; + else [self finish:nil error:@"Camera access denied. Allow Camera for Faceclaw in iPhone Settings, then scan again."]; + }); + }]; + } else if (status == AVAuthorizationStatusAuthorized) [self presentScanner]; + else [self finish:nil error:@"Camera access is unavailable. Check Camera permission for Faceclaw in iPhone Settings."]; +} +- (void)presentScanner { + if (_finished) return; + UIViewController *presenter = nil; + for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) { + if (![scene isKindOfClass:UIWindowScene.class] || scene.activationState != UISceneActivationStateForegroundActive) continue; + for (UIWindow *window in ((UIWindowScene *)scene).windows) + if (window.isKeyWindow) presenter = window.rootViewController; + } + // NativeScript also supports the legacy, non-scene app lifecycle. + if (!presenter) presenter = UIApplication.sharedApplication.keyWindow.rootViewController; + while (presenter.presentedViewController) presenter = presenter.presentedViewController; + if (!presenter.view.window || presenter.isBeingDismissed || presenter.isBeingPresented || [presenter isKindOfClass:UIAlertController.class]) { + [self finish:nil error:@"Close the phone dialog and open Faceclaw to scan again."]; return; + } + _screen = [FaceclawQrScreen new]; + __weak FaceclawQrScanner *weakSelf = self; + _screen.cancelHandler = ^{ [weakSelf cancel]; }; + _navigation = [[UINavigationController alloc] initWithRootViewController:_screen]; + _navigation.modalPresentationStyle = UIModalPresentationFullScreen; + _navigation.overrideUserInterfaceStyle = UIUserInterfaceStyleDark; + _presenting = YES; + [presenter presentViewController:_navigation animated:YES completion:^{ + self.presenting = NO; + if (self.finished) [self closeScanner]; + else [self configureCamera]; + }]; +} +- (void)configureCamera { + _session = [AVCaptureSession new]; + _screen.preview = [AVCaptureVideoPreviewLayer layerWithSession:_session]; + _screen.preview.videoGravity = AVLayerVideoGravityResizeAspectFill; + [_screen.view.layer insertSublayer:_screen.preview atIndex:0]; + [_screen.view setNeedsLayout]; + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(cameraFailed:) + name:AVCaptureSessionRuntimeErrorNotification object:_session]; + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(cameraInterrupted:) + name:AVCaptureSessionWasInterruptedNotification object:_session]; + dispatch_async(_cameraQueue, ^{ + if (self.finished) return; + NSError *error = nil; + AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; + AVCaptureDeviceInput *input = camera ? [AVCaptureDeviceInput deviceInputWithDevice:camera error:&error] : nil; + AVCaptureMetadataOutput *output = [AVCaptureMetadataOutput new]; + [self.session beginConfiguration]; + if (input && [self.session canAddInput:input]) [self.session addInput:input]; + if (self.session.inputs.count && [self.session canAddOutput:output]) [self.session addOutput:output]; + BOOL ready = [output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeQRCode]; + if (ready) { + self.output = output; + [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; + output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode]; + } + [self.session commitConfiguration]; + if (!ready) { + dispatch_async(dispatch_get_main_queue(), ^{ [self finish:nil error:error.localizedDescription ?: @"Could not start the QR camera. Try scanning again."]; }); + return; + } + dispatch_async(dispatch_get_main_queue(), ^{ [self.screen.view setNeedsLayout]; }); + if (!self.finished) [self.session startRunning]; + }); +} +- (void)metadataOutput:(AVCaptureMetadataOutput *)output didOutputMetadataObjects:(NSArray<__kindof AVMetadataObject *> *)objects fromConnection:(AVCaptureConnection *)connection { + for (AVMetadataObject *object in objects) { + if (![object.type isEqual:AVMetadataObjectTypeQRCode] || ![object isKindOfClass:AVMetadataMachineReadableCodeObject.class]) continue; + NSString *text = ((AVMetadataMachineReadableCodeObject *)object).stringValue; + if (text.length) { [self finish:text error:nil]; return; } + } +} +- (void)cameraFailed:(NSNotification *)notification { + dispatch_async(dispatch_get_main_queue(), ^{ [self finish:nil error:@"The camera stopped. Try scanning again."]; }); +} +- (void)cameraInterrupted:(NSNotification *)notification { + dispatch_async(dispatch_get_main_queue(), ^{ [self finish:nil error:@"Camera interrupted. Return to Faceclaw and scan again."]; }); +} +- (void)backgrounded:(NSNotification *)notification { [self cancel]; } +- (void)cancel { [self finish:nil error:nil]; } +- (void)finish:(NSString *)text error:(NSString *)error { + if (_finished) return; + self.finished = YES; + _resultText = text; _resultError = error; + [NSNotificationCenter.defaultCenter removeObserver:self]; + // Serialize teardown after any pending setup/start. Never block the UI. + dispatch_async(_cameraQueue, ^{ + [self.output setMetadataObjectsDelegate:nil queue:NULL]; + [self.session stopRunning]; + dispatch_async(dispatch_get_main_queue(), ^{ self.stopped = YES; [self closeScanner]; }); + }); +} +- (void)closeScanner { + if (!_stopped || _presenting || _closing) return; + _closing = YES; + void (^done)(void) = ^{ + void (^completion)(NSString *, NSString *) = self.completion; + self.completion = nil; + self.screen.preview.session = nil; + self.screen = nil; self.navigation = nil; self.session = nil; self.output = nil; + if (completion) completion(self.resultText, self.resultError); + }; + if (_navigation.presentingViewController) [_navigation dismissViewControllerAnimated:YES completion:done]; + else done(); +} +@end diff --git a/App_Resources/iOS/src/module.modulemap b/App_Resources/iOS/src/module.modulemap index 7c750302..30f457d4 100644 --- a/App_Resources/iOS/src/module.modulemap +++ b/App_Resources/iOS/src/module.modulemap @@ -10,5 +10,6 @@ module FaceclawGraphics { header "FaceclawConfigPort.h" header "FaceclawEvenHubWebView.h" header "FaceclawCrypto.h" + header "FaceclawQrScanner.h" export * } diff --git a/app/apps/developer/developer-app.ts b/app/apps/developer/developer-app.ts index 1d68b04d..2e58a42b 100644 --- a/app/apps/developer/developer-app.ts +++ b/app/apps/developer/developer-app.ts @@ -116,8 +116,6 @@ export function createDeveloperAppWindow(appContext: AppContext, options: InProc }, { label: "Load app from QR code", - disabled: global.isIOS, - description: global.isIOS ? "Not available on iOS yet." : undefined, onSelect: (ctx) => { const layer = new LoadAppFromQrLayer(appContext); ctx.stack.push(layer); diff --git a/app/apps/developer/load-app.ts b/app/apps/developer/load-app.ts index f8ac5c2f..ba6c9555 100644 --- a/app/apps/developer/load-app.ts +++ b/app/apps/developer/load-app.ts @@ -7,7 +7,7 @@ import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/g import { Layer, type LayerContext } from "../../ui/layers"; import { lineStep } from "../../ui/metrics"; import { developerAppUrlSetting } from "../../ui/dashboard-settings"; -import { isQrScannerAvailable, scanQrCode } from "../../native/qr-scan"; +import { cancelQrScan, isQrScannerAvailable, scanQrCode } from "../../native/qr-scan"; const MARGIN = 20; @@ -114,20 +114,23 @@ export class LoadAppFromUrlLayer implements Layer { } /** - * "Load app from QR code": hand off to an installed scanner app, then launch + * "Load app from QR code": open the phone's scanner, then launch * whatever URL it decodes. The scanned URL is kept in developerAppUrlSetting so * the URL page can reload it later without rescanning. */ export class LoadAppFromQrLayer implements Layer { private status = "Starting scanner..."; private busy = false; + private removed = false; constructor(private readonly ctx: AppContext) {} /** Launches the scanner; call right after pushing the layer. */ open(ctx: LayerContext): void { + if (this.busy || this.removed) return; if (!isQrScannerAvailable()) { - this.status = "No QR scanner on this phone (needs Play Services or a scanner app)."; + this.status = global.isIOS ? "No camera available. Scan using a physical iPhone." + : "No QR scanner on this phone (needs Play Services or a scanner app)."; ctx.actions.requestRender(); return; } @@ -135,6 +138,11 @@ export class LoadAppFromQrLayer implements Layer { void this.scan(ctx); } + onRemoved(): void { + this.removed = true; + if (this.busy) cancelQrScan(); + } + paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); const { width, height } = ctx.stack.getBaseSize(); @@ -156,12 +164,7 @@ export class LoadAppFromQrLayer implements Layer { handleInput(event: InputEvent, ctx: LayerContext): void { switch (event.type) { case "click": - if (!this.busy && isQrScannerAvailable()) { - this.busy = true; - this.status = "Starting scanner..."; - ctx.actions.requestRender(); - void this.scan(ctx); - } + this.open(ctx); return; case "double-click": ctx.stack.pop(); @@ -176,6 +179,7 @@ export class LoadAppFromQrLayer implements Layer { this.status = "Point the phone camera at the QR code."; ctx.actions.requestRender(); const scanned = await scanQrCode(); + if (this.removed) return; if (scanned === null) { this.status = "Scan cancelled."; return; @@ -184,17 +188,18 @@ export class LoadAppFromQrLayer implements Layer { this.status = `Not a URL: ${scanned}`; return; } - developerAppUrlSetting.set(scanned); + developerAppUrlSetting.set(scanned.trim()); this.status = `Loading ${scanned}`; ctx.actions.requestRender(); await openEvenHubUrl(this.ctx, scanned); - ctx.stack.pop(); + if (!this.removed) ctx.stack.pop(); } catch (error) { + if (this.removed) return; this.status = cleanError(error); this.ctx.appendLog(`evenhub qr launch failed: ${this.status}`); } finally { this.busy = false; - ctx.actions.requestRender(); + if (!this.removed) ctx.actions.requestRender(); } } } diff --git a/app/native/qr-scan.ios.ts b/app/native/qr-scan.ios.ts new file mode 100644 index 00000000..bc74376a --- /dev/null +++ b/app/native/qr-scan.ios.ts @@ -0,0 +1,24 @@ +declare const FaceclawQrScanner: any + +let activeScanner: any = null + +export function isQrScannerAvailable(): boolean { return FaceclawQrScanner.isAvailable() } + +export function scanQrCode(): Promise { + if (activeScanner) return Promise.reject(new Error('A QR scan is already open.')) + return new Promise((resolve, reject) => { + const scanner = FaceclawQrScanner.new() + activeScanner = scanner + let settled = false + const finish = (text: string | null, error: string | null) => { + if (settled) return + settled = true + if (activeScanner === scanner) activeScanner = null + error ? reject(new Error(String(error))) : resolve(text == null ? null : String(text)) + } + try { scanner.startWithCompletion(finish) } + catch (error) { finish(null, String(error)) } + }) +} + +export function cancelQrScan(): void { activeScanner?.cancel() } diff --git a/app/native/qr-scan.ts b/app/native/qr-scan.ts index c91c7000..d415098f 100644 --- a/app/native/qr-scan.ts +++ b/app/native/qr-scan.ts @@ -22,6 +22,10 @@ const SCAN_ACTION = "com.google.zxing.client.android.SCAN"; // Arbitrary, only has to be distinct from other startActivityForResult callers. const SCAN_REQUEST_CODE = 0x51d0; +// Android's scanner is owned by Play Services or another app. The load layer +// ignores its late result if the user leaves; iOS also closes its own camera. +export function cancelQrScan(): void {} + function currentActivity(): any { return Application.android?.foregroundActivity ?? Application.android?.startActivity ?? null; }