diff --git a/App_Resources/Android/app.gradle b/App_Resources/Android/app.gradle
index fa5876b6..7569cf33 100644
--- a/App_Resources/Android/app.gradle
+++ b/App_Resources/Android/app.gradle
@@ -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"
diff --git a/App_Resources/Android/src/main/AndroidManifest.xml b/App_Resources/Android/src/main/AndroidManifest.xml
index 19659720..48dda00b 100644
--- a/App_Resources/Android/src/main/AndroidManifest.xml
+++ b/App_Resources/Android/src/main/AndroidManifest.xml
@@ -55,6 +55,13 @@
android:usesCleartextTraffic="true"
android:hardwareAccelerated="true">
+
+
+
() {
+ @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);
+ }
+ });
+ }
+}
diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScannerListener.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScannerListener.java
new file mode 100644
index 00000000..b255991e
--- /dev/null
+++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawQrScannerListener.java
@@ -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);
+}
diff --git a/app/apps/developer/load-app.ts b/app/apps/developer/load-app.ts
index d69d8b63..6eda1249 100644
--- a/app/apps/developer/load-app.ts
+++ b/app/apps/developer/load-app.ts
@@ -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;
}
diff --git a/app/native/qr-scan.ts b/app/native/qr-scan.ts
index 9b56d988..c91c7000 100644
--- a/app/native/qr-scan.ts
+++ b/app/native/qr-scan.ts
@@ -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 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 {
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 {
+ return new Promise((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 {
return new Promise((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 {
};
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)));