From 9fac7a29bac2fe95ea4d3fc85cab0425cf5e8ea5 Mon Sep 17 00:00:00 2001 From: Om Alve Date: Mon, 2 Nov 2026 13:09:48 +0530 Subject: [PATCH] Voice: don't need an Activity to check already-granted permissions ensurePermissions looked up the Activity before checking whether any permission was missing, and getActivity() throws when there is none. Android starts the app in the background with no Activity (a home-widget update, a notification-listener rebind), and the glasses session runs from there until the app is opened. So in that state every "Hey Even" failed in ensureVoicePermissions even though the mic permission was granted, and the voice dialog kept showing the idle "Voice control stopped.". The same throw hit ensureBlePermissions. The Activity is now only looked up when a permission actually has to be requested. A capture that fails to start for any reason other than a denied permission now says so in the dialog instead of leaving the idle status up. --- app/g2/android-permissions.ts | 7 ++- app/g2/dashboard-controller.ts | 1 + app/native/voice-control.ts | 8 ++++ tests/android-permissions.test.cjs | 74 ++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tests/android-permissions.test.cjs diff --git a/app/g2/android-permissions.ts b/app/g2/android-permissions.ts index e3c4a949..4abfd012 100644 --- a/app/g2/android-permissions.ts +++ b/app/g2/android-permissions.ts @@ -65,11 +65,16 @@ async function ensurePermissions( ): Promise { if (!global.isAndroid) return; - const activity = getActivity(); const missing = permissions.filter((permission) => !isPermissionGranted(permission)); if (missing.length === 0) return; + // Only a request needs an Activity. Checking must not: Android can start + // the app in the background (a home-widget update, a notification-listener + // rebind) with none, and the glasses session and voice input run from + // there until the app is opened. + const activity = getActivity(); + await new Promise((resolve, reject) => { const callback = (args: { requestCode: number; diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 87e67ac7..1e4618e9 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -2258,6 +2258,7 @@ class DashboardController { .catch((error) => { this.appendLog(`voice permission failed: ${this.formatError(error)}`); if (!hasMicrophonePermission()) voiceControlBridge.reportPermissionDenied(); + else voiceControlBridge.reportStartFailed(this.formatError(error)); }); } diff --git a/app/native/voice-control.ts b/app/native/voice-control.ts index 038c8ceb..d6a7e37b 100644 --- a/app/native/voice-control.ts +++ b/app/native/voice-control.ts @@ -199,6 +199,14 @@ export class FaceclawVoiceControlBridge { ); } + /** + * Capture could not be started for a reason other than a denied permission. + * Without this the dialog keeps showing the idle "Voice control stopped." + */ + reportStartFailed(reason: string): void { + this.setStatus("Voice input couldn't start.", reason); + } + /** Begin push-to-talk capture (momentary; released with stopPushToTalk). */ startPushToTalk(options: PushToTalkOptions): void { this.acquireCapture("ptt", options); diff --git a/tests/android-permissions.test.cjs b/tests/android-permissions.test.cjs new file mode 100644 index 00000000..5807e780 --- /dev/null +++ b/tests/android-permissions.test.cjs @@ -0,0 +1,74 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { loader } = require('./helpers/load-typescript.cjs'); + +const GRANTED = 0; +const DENIED = -1; +const RECORD_AUDIO = 'android.permission.RECORD_AUDIO'; + +/** + * app/g2/android-permissions.ts against a stubbed Android: `granted` is the set + * of permissions already held, `activity` the foreground/start Activity (null + * when Android started the process in the background, e.g. for a home-widget + * update, and the app has not been opened yet). + */ +function permissions({ granted = [], activity = null } = {}) { + const held = new Set(granted); + const requests = []; + const Application = { + android: { + foregroundActivity: activity, + startActivity: activity, + activityRequestPermissionsEvent: 'activityRequestPermissions', + on() {}, + off() {}, + }, + }; + const globals = { + global: { isAndroid: true }, + Array: { create: (_type, length) => new Array(length) }, + android: { + content: { pm: { PackageManager: { PERMISSION_GRANTED: GRANTED } } }, + os: { Build: { VERSION: { SDK_INT: 34 } } }, + Manifest: { permission: { BLUETOOTH_SCAN: 'android.permission.BLUETOOTH_SCAN', BLUETOOTH_CONNECT: 'android.permission.BLUETOOTH_CONNECT' } }, + }, + androidx: { + core: { + content: { ContextCompat: { checkSelfPermission: (_context, permission) => (held.has(permission) ? GRANTED : DENIED) } }, + app: { ActivityCompat: { requestPermissions: (requester, list, code) => requests.push({ requester, list: [...list], code }) } }, + }, + }, + }; + const load = loader(globals, { + '@nativescript/core': { Application, Utils: { android: { getApplicationContext: () => ({}) } } }, + }); + return { api: load('app/g2/android-permissions.ts'), requests }; +} + +test('a granted permission needs no Activity, so voice works after a background start', async () => { + const { api, requests } = permissions({ granted: [RECORD_AUDIO], activity: null }); + await api.ensureVoicePermissions(); + assert.equal(requests.length, 0); +}); + +test('BLE permissions that are all granted need no Activity either', async () => { + const { api } = permissions({ + granted: ['android.permission.BLUETOOTH_SCAN', 'android.permission.BLUETOOTH_CONNECT', 'android.permission.POST_NOTIFICATIONS'], + activity: null, + }); + await api.ensureBlePermissions(); +}); + +test('a missing permission is requested through the Activity', async () => { + const activity = { name: 'main' }; + const { api, requests } = permissions({ granted: [], activity }); + void api.ensureVoicePermissions(); + assert.equal(requests.length, 1); + assert.equal(requests[0].requester, activity); + assert.deepEqual(requests[0].list, [RECORD_AUDIO]); +}); + +test('a missing permission with no Activity still fails, since nothing can show the prompt', async () => { + const { api } = permissions({ granted: [], activity: null }); + await assert.rejects(api.ensureVoicePermissions(), /No Android activity/); +});