QR code loading works

This commit is contained in:
James Babcock committed 2026-08-22 00:24:36 -07:00
1 parent 5db6df93dd
commit 0ed242a36f
6 files changed
+179 -25

No files matched your search

+4
View File
@@ -8,6 +8,10 @@ dependencies {
// WebViewCompat.addDocumentStartJavaScript, for injecting the EvenHub bridge
// shim into remotely hosted apps (developer "Load app from URL").
implementation 'androidx.webkit:webkit:1.12.1'
// Play Services' ML Kit code scanner, for the developer "Load app from QR
// code" entry. Scanner UI and camera live in Play Services, so Faceclaw
// needs no camera permission and no third-party scanner app.
implementation 'com.google.android.gms:play-services-code-scanner:16.1.0'
}
def faceclawSherpaOnnxVersion = "1.13.0"
@@ -55,6 +55,13 @@
android:usesCleartextTraffic="true"
android:hardwareAccelerated="true">
<!-- Ask Play Services to fetch the ML Kit barcode scanner module at
install time, so the Developer app's "Load app from QR code" does
not have to download it on first use. -->
<meta-data
android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="barcode_ui" />
<!-- Read-only content URIs for cached documents handed to system viewers. -->
<provider
android:name="androidx.core.content.FileProvider"
@@ -0,0 +1,81 @@
package com.faceclaw.app;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.tasks.OnCanceledListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.mlkit.vision.barcode.common.Barcode;
import com.google.mlkit.vision.codescanner.GmsBarcodeScanner;
import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions;
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning;
/**
* QR scanning through Play Services' ML Kit code scanner. The scanner UI and
* the camera both live inside Play Services, so Faceclaw needs no camera
* permission of its own and no third-party scanner app — which is what the
* earlier ZXing SCAN intent needed, and no app on a stock Pixel registers for
* it.
*
* The scanner module is downloaded on demand the first time it is used (the
* manifest's com.google.mlkit.vision.DEPENDENCIES meta-data asks Play Services
* to fetch it at install time instead); a scan attempted before that finishes
* fails with a module-unavailable error rather than blocking.
*
* Callbacks fire on the main thread.
*/
public final class FaceclawQrScanner {
private static final String TAG = "FaceclawQrScanner";
private FaceclawQrScanner() {}
/** Whether Play Services is present and current enough to scan. */
public static boolean isAvailable(Context context) {
try {
int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context);
return status == ConnectionResult.SUCCESS;
} catch (Throwable error) {
Log.w(TAG, "Play Services availability check failed", error);
return false;
}
}
/** Open the scanner UI; the listener gets the decoded text, or a cancel/error. */
public static void scan(Activity activity, final FaceclawQrScannerListener listener) {
GmsBarcodeScannerOptions options = new GmsBarcodeScannerOptions.Builder()
.setBarcodeFormats(Barcode.FORMAT_QR_CODE)
.enableAutoZoom()
.build();
GmsBarcodeScanner scanner = GmsBarcodeScanning.getClient(activity, options);
scanner.startScan()
.addOnSuccessListener(new OnSuccessListener<Barcode>() {
@Override
public void onSuccess(Barcode barcode) {
String value = barcode == null ? null : barcode.getRawValue();
if (value == null) {
listener.onError("The code held no text.");
} else {
listener.onResult(value);
}
}
})
.addOnCanceledListener(new OnCanceledListener() {
@Override
public void onCanceled() {
listener.onCancelled();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception error) {
Log.w(TAG, "scan failed", error);
String message = error.getMessage();
listener.onError(message == null ? error.toString() : message);
}
});
}
}
@@ -0,0 +1,10 @@
package com.faceclaw.app;
/** Callbacks for one FaceclawQrScanner.scan; exactly one method fires. */
public interface FaceclawQrScannerListener {
void onResult(String text);
void onCancelled();
void onError(String message);
}
+1 -1
View File
@@ -127,7 +127,7 @@ export class LoadAppFromQrLayer implements Layer {
/** Launches the scanner; call right after pushing the layer. */
open(ctx: LayerContext): void {
if (!isQrScannerAvailable()) {
this.status = "No QR scanner app is installed on the phone.";
this.status = "No QR scanner on this phone (needs Play Services or a scanner app).";
ctx.actions.requestRender();
return;
}
+76 -24
View File
@@ -1,26 +1,31 @@
import { Application } from "@nativescript/core";
import { Application, Utils } from "@nativescript/core";
declare const android: any;
declare const com: any;
declare const global: any;
/**
* QR scanning by delegating to whatever scanner app the phone has installed,
* via the de-facto-standard ZXing "SCAN" intent (Barcode Scanner, Binary Eye,
* QR & Barcode Scanner, and others register for it). Faceclaw ships no decoder
* and needs no camera permission of its own: the scanner app owns the camera
* and hands back the decoded text.
* QR scanning for the Developer app's "Load app from QR code".
*
* The cost is that scanning only works if such an app is installed;
* isQrScannerAvailable reports that up front so the caller can say so instead
* of launching into nothing. (resolveActivity can see other apps only because
* the manifest already holds QUERY_ALL_PACKAGES for notification icons;
* without it Android 11+ would need a <queries> entry for the action.)
* Primary path is Play Services' ML Kit code scanner
* (FaceclawQrScanner): the scanner UI and the camera live inside Play
* Services, so Faceclaw declares no camera permission and needs no other app
* installed. That last part is why it is the primary path — the ZXing "SCAN"
* intent, which is the usual delegate-to-a-scanner-app trick, resolves to
* nothing on a stock phone because no preinstalled app registers for it.
*
* The intent is kept as a fallback for a phone without Play Services, where
* the user has presumably installed a scanner app of their own.
*/
const SCAN_ACTION = "com.google.zxing.client.android.SCAN";
// Arbitrary, only has to be distinct from other startActivityForResult callers.
const SCAN_REQUEST_CODE = 0x51d0;
function currentActivity(): any {
return Application.android?.foregroundActivity ?? Application.android?.startActivity ?? null;
}
function buildScanIntent(): any {
const intent = new android.content.Intent(SCAN_ACTION);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
@@ -29,10 +34,21 @@ function buildScanIntent(): any {
return intent;
}
/** Whether any installed app answers the scan intent. */
export function isQrScannerAvailable(): boolean {
if (!global.isAndroid) return false;
const activity = Application.android?.foregroundActivity ?? Application.android?.startActivity;
/** Whether the Play Services scanner is usable on this phone. */
function isMlKitScannerAvailable(): boolean {
const context = Utils.android.getApplicationContext();
if (!context) return false;
try {
return com.faceclaw.app.FaceclawQrScanner.isAvailable(context) === true;
} catch (error) {
console.warn(`qr: ML Kit availability check failed: ${error}`);
return false;
}
}
/** Whether any installed app answers the ZXing scan intent. */
function isScanIntentAvailable(): boolean {
const activity = currentActivity();
if (!activity) return false;
try {
return buildScanIntent().resolveActivity(activity.getPackageManager()) !== null;
@@ -41,19 +57,55 @@ export function isQrScannerAvailable(): boolean {
}
}
/** Whether scanning can be attempted at all, by either route. */
export function isQrScannerAvailable(): boolean {
if (!global.isAndroid) return false;
return isMlKitScannerAvailable() || isScanIntentAvailable();
}
/**
* Launch the scanner and resolve with the decoded text, or null if the user
* backed out. Rejects when no scanner app is installed or the activity can't
* be reached.
* Open a scanner and resolve with the decoded text, or null if the user backed
* out. Rejects when no scanner is reachable or the scan itself fails.
*/
export function scanQrCode(): Promise<string | null> {
if (!global.isAndroid) return Promise.reject(new Error("QR scanning needs Android."));
const activity = Application.android?.foregroundActivity ?? Application.android?.startActivity;
const activity = currentActivity();
if (!activity) return Promise.reject(new Error("No foreground activity to scan from."));
const intent = buildScanIntent();
if (intent.resolveActivity(activity.getPackageManager()) === null) {
return Promise.reject(new Error("No QR scanner app installed."));
}
if (isMlKitScannerAvailable()) return scanWithMlKit(activity);
if (isScanIntentAvailable()) return scanWithIntent(activity);
return Promise.reject(new Error("No QR scanner available (needs Play Services or a scanner app)."));
}
function scanWithMlKit(activity: any): Promise<string | null> {
return new Promise<string | null>((resolve, reject) => {
let settled = false;
const listener = new com.faceclaw.app.FaceclawQrScannerListener({
onResult: (text: string) => {
if (settled) return;
settled = true;
resolve(String(text));
},
onCancelled: () => {
if (settled) return;
settled = true;
resolve(null);
},
onError: (message: string) => {
if (settled) return;
settled = true;
reject(new Error(String(message)));
},
});
try {
com.faceclaw.app.FaceclawQrScanner.scan(activity, listener);
} catch (error) {
settled = true;
reject(error instanceof Error ? error : new Error(String(error)));
}
});
}
function scanWithIntent(activity: any): Promise<string | null> {
return new Promise<string | null>((resolve, reject) => {
const onResult = (args: { requestCode: number; resultCode: number; intent: any }) => {
if (args.requestCode !== SCAN_REQUEST_CODE) return;
@@ -67,7 +119,7 @@ export function scanQrCode(): Promise<string | null> {
};
Application.android.on(Application.android.activityResultEvent, onResult);
try {
activity.startActivityForResult(intent, SCAN_REQUEST_CODE);
activity.startActivityForResult(buildScanIntent(), SCAN_REQUEST_CODE);
} catch (error) {
Application.android.off(Application.android.activityResultEvent, onResult);
reject(error instanceof Error ? error : new Error(String(error)));