Handle long presses: push-to-talk, with a tap-like fallback
On a real G2 a touch held slightly too long is reported as a long press (sysEvent type 9, then type 10 on release; source 1 right temple, 3 left), not as a tap, and was ignored. A long press now does what a tap does. If it started a recording and is held at least 700 ms, releasing it sends (push-to-talk); released sooner, it behaves like a tap and the recording continues until the next tap or press. Tests: 219 unit/integration; simulator 7 scenarios (long_press / long_press_release through the automation API). Not reviewed, not deployed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -64,6 +64,8 @@ export interface ControllerOptions {
|
||||
submitRetryMs?: number;
|
||||
/** No audio frames for this long while recording means the microphone is gone. */
|
||||
micStallMs?: number;
|
||||
/** A long press held at least this long, then released, sends what was recorded (push-to-talk). */
|
||||
holdToTalkMs?: number;
|
||||
}
|
||||
|
||||
interface Capture {
|
||||
@@ -107,6 +109,9 @@ export class Controller {
|
||||
private readonly submitAttempts: number;
|
||||
private readonly submitRetryMs: number;
|
||||
private readonly micStallMs: number;
|
||||
private readonly holdToTalkMs: number;
|
||||
/** Set while a long press that STARTED a recording is still held down. */
|
||||
private heldPress: { generation: number; at: number } | null = null;
|
||||
|
||||
constructor(private readonly options: ControllerOptions) {
|
||||
this.device = options.device;
|
||||
@@ -116,6 +121,7 @@ export class Controller {
|
||||
this.submitAttempts = options.submitAttempts ?? 4;
|
||||
this.submitRetryMs = options.submitRetryMs ?? 1500;
|
||||
this.micStallMs = options.micStallMs ?? 3000;
|
||||
this.holdToTalkMs = options.holdToTalkMs ?? 700;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ public
|
||||
@@ -640,8 +646,29 @@ export class Controller {
|
||||
case 'audio':
|
||||
return this.onAudio(event.pcm);
|
||||
case 'tap':
|
||||
this.heldPress = null;
|
||||
return this.dispatch({ type: 'activate', freshId: this.newId() });
|
||||
case 'long_press': {
|
||||
// First seen on a real G2 (2026-11-21): a touch held a little too long arrives as a
|
||||
// long press, not a tap. It does what a tap does, so the user need not care which the
|
||||
// glasses decided it was. If it started a recording, remember that the finger is down.
|
||||
const wasListening = this.state.phase === 'listening';
|
||||
this.dispatch({ type: 'activate', freshId: this.newId() });
|
||||
this.heldPress =
|
||||
!wasListening && this.state.phase === 'listening' ? { generation: this.state.generation, at: this.now() } : null;
|
||||
return;
|
||||
}
|
||||
case 'long_press_release': {
|
||||
// Push-to-talk: held while speaking, then released, sends. A press released quickly is
|
||||
// treated like a tap and leaves the recording running until the next tap or press.
|
||||
const held = this.heldPress;
|
||||
this.heldPress = null;
|
||||
if (!held || held.generation !== this.state.generation || this.state.phase !== 'listening') return;
|
||||
if (this.now() - held.at < this.holdToTalkMs) return;
|
||||
return this.dispatch({ type: 'activate', freshId: this.newId() });
|
||||
}
|
||||
case 'double_tap':
|
||||
this.heldPress = null;
|
||||
return this.dispatch({ type: 'back' });
|
||||
case 'swipe_down':
|
||||
return this.dispatch({ type: 'next_page' });
|
||||
|
||||
@@ -64,7 +64,7 @@ export function renderHud(state: AppState, nowMs: number): HudFrame {
|
||||
const cap = state.captureMaxSeconds;
|
||||
return {
|
||||
body: `Listening…\n\nRecording stops by itself after ${cap} seconds.`,
|
||||
status: `● REC ${clock(elapsed)} / ${clock(cap)} · Tap: send · 2×: cancel`,
|
||||
status: `● REC ${clock(elapsed)} / ${clock(cap)} · Tap or release: send · 2×: cancel`,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,10 @@ export const MENU_ITEM = { NEW_CONVERSATION: 1, CANCEL: 2 } as const;
|
||||
export type DeviceEvent =
|
||||
| { type: 'tap' }
|
||||
| { type: 'double_tap' }
|
||||
/** The touch has been held long enough for the glasses to call it a long press. */
|
||||
| { type: 'long_press' }
|
||||
/** The finger was lifted after a long press. */
|
||||
| { type: 'long_press_release' }
|
||||
| { type: 'swipe_up' }
|
||||
| { type: 'swipe_down' }
|
||||
| { type: 'menu'; itemId: number }
|
||||
@@ -89,11 +93,17 @@ export function normalizeEvent(event: RawEvenHubEvent): DeviceEvent[] {
|
||||
case OS_EVENT.SYSTEM_EXIT:
|
||||
out.push({ type: 'system_exit' });
|
||||
break;
|
||||
case OS_EVENT.LONG_PRESS:
|
||||
out.push({ type: 'long_press' });
|
||||
break;
|
||||
case OS_EVENT.LONG_PRESS_RELEASE:
|
||||
out.push({ type: 'long_press_release' });
|
||||
break;
|
||||
case OS_EVENT.CLICK:
|
||||
out.push({ type: 'tap' });
|
||||
break;
|
||||
default:
|
||||
// IMU reports and long press are not part of the interaction contract.
|
||||
// IMU reports are not part of the interaction contract.
|
||||
break;
|
||||
}
|
||||
return out;
|
||||
|
||||
@@ -64,10 +64,15 @@ describe('gestures through the SDK parser', () => {
|
||||
expect(parse({ type: 'menuItemClickEvent', jsonData: { itemID: 2 } })).toEqual([{ type: 'menu', itemId: 2 }]);
|
||||
});
|
||||
|
||||
it('IMU reports and long presses are outside the interaction contract', () => {
|
||||
it('IMU reports are outside the interaction contract', () => {
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 8, imuData: { x: 1, y: 2, z: 3 } } })).toEqual([]);
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 9 } })).toEqual([]);
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 10 } })).toEqual([]);
|
||||
});
|
||||
|
||||
it('long press and its release are recognised (a real G2 reported a slow tap as type 9)', () => {
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 9, eventSource: 1 } })).toEqual([{ type: 'long_press' }]);
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 10, eventSource: 1 } })).toEqual([{ type: 'long_press_release' }]);
|
||||
expect(parse({ type: 'textEvent', jsonData: { eventType: 9 } })).toEqual([{ type: 'long_press' }]);
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 'LONG_PRESS_EVENT' } })).toEqual([{ type: 'long_press' }]);
|
||||
});
|
||||
|
||||
it('an empty or unknown event produces nothing', () => {
|
||||
|
||||
@@ -70,6 +70,8 @@ export class FakeDevice implements DevicePort {
|
||||
}
|
||||
tap = () => this.emit({ type: 'tap' });
|
||||
doubleTap = () => this.emit({ type: 'double_tap' });
|
||||
longPress = () => this.emit({ type: 'long_press' });
|
||||
longPressRelease = () => this.emit({ type: 'long_press_release' });
|
||||
swipeDown = () => this.emit({ type: 'swipe_down' });
|
||||
swipeUp = () => this.emit({ type: 'swipe_up' });
|
||||
menu = (itemId: number) => this.emit({ type: 'menu', itemId });
|
||||
|
||||
@@ -43,6 +43,7 @@ function makeController(dev: FakeDevice): Controller {
|
||||
submitAttempts: 3,
|
||||
submitRetryMs: 30,
|
||||
micStallMs: 300,
|
||||
holdToTalkMs: 150,
|
||||
makeClient: (baseUrl, token) =>
|
||||
new BridgeClient({
|
||||
baseUrl,
|
||||
@@ -608,3 +609,75 @@ describe('CODY-009: exactly one conversation is created, whatever the timing', (
|
||||
expect(h.hermes.executions).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('long press (a real G2 reported a slow tap as a long press)', () => {
|
||||
it('push-to-talk: hold starts recording, speaking while held, releasing sends', async () => {
|
||||
await pairedController();
|
||||
h.stt.next({ text: 'held question' });
|
||||
device.longPress();
|
||||
await until('listening with mic open', () => phase() === 'listening' && device.micOpen);
|
||||
expect(device.frame.status).toContain('● REC');
|
||||
device.speak(0.5);
|
||||
await new Promise((r) => setTimeout(r, 200)); // held longer than the push-to-talk threshold
|
||||
device.longPressRelease();
|
||||
await until('answer', () => phase() === 'answer');
|
||||
expect(h.hermes.lastRun()!.input).toBe('held question');
|
||||
expect(device.micOpen).toBe(false);
|
||||
expect(h.hermes.executions).toBe(1);
|
||||
});
|
||||
|
||||
it('a press released quickly behaves like a tap: recording continues until the next tap', async () => {
|
||||
await pairedController();
|
||||
device.longPress();
|
||||
await until('listening', () => phase() === 'listening' && device.micOpen);
|
||||
device.longPressRelease(); // released at once: not push-to-talk
|
||||
await new Promise((r) => setTimeout(r, 250));
|
||||
expect(phase()).toBe('listening');
|
||||
expect(device.micOpen).toBe(true);
|
||||
expect(h.stt.calls).toBe(0);
|
||||
device.speak(0.4);
|
||||
device.tap();
|
||||
await until('answer', () => phase() === 'answer');
|
||||
expect(h.hermes.executions).toBe(1);
|
||||
});
|
||||
|
||||
it('a long press while already recording sends, and its release does nothing more', async () => {
|
||||
await pairedController();
|
||||
device.tap();
|
||||
await until('listening', () => phase() === 'listening' && device.micOpen);
|
||||
device.speak(0.4);
|
||||
device.longPress();
|
||||
await until('answer', () => phase() === 'answer');
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
device.longPressRelease();
|
||||
await new Promise((r) => setTimeout(r, 100));
|
||||
expect(phase()).toBe('answer'); // the late release did not start a new recording
|
||||
expect(device.micOpens).toBe(1);
|
||||
expect(h.hermes.executions).toBe(1);
|
||||
});
|
||||
|
||||
it('a release with no press, or after a cancel, is ignored', async () => {
|
||||
await pairedController();
|
||||
device.longPressRelease();
|
||||
expect(phase()).toBe('ready');
|
||||
device.longPress();
|
||||
await until('listening', () => phase() === 'listening' && device.micOpen);
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
device.doubleTap(); // cancel while still holding
|
||||
expect(phase()).toBe('cancelled');
|
||||
device.longPressRelease();
|
||||
await new Promise((r) => setTimeout(r, 100));
|
||||
expect(phase()).toBe('cancelled');
|
||||
expect(h.stt.calls).toBe(0);
|
||||
expect(h.hermes.executions).toBe(0);
|
||||
});
|
||||
|
||||
it('a long press on an answer starts a follow-up, like a tap', async () => {
|
||||
await pairedController();
|
||||
controller.askText('first');
|
||||
await until('answer', () => phase() === 'answer');
|
||||
device.longPress();
|
||||
await until('listening', () => phase() === 'listening' && device.micOpen);
|
||||
expect(snapshot.state.conversationId).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -191,13 +191,17 @@ describe('start-up when a callable Even host appears during the wait', () => {
|
||||
await wait(20);
|
||||
const afterTap = diagnostics()['Glasses input']!;
|
||||
expect(afterTap).toContain('1 gesture/lifecycle event(s)');
|
||||
expect(afterTap).not.toContain('ignored;');
|
||||
expect(afterTap).toContain('sysEvent(type=none,source=1) -> tap');
|
||||
// The tap really drove the app: unpaired, so the state stays put, but it was delivered.
|
||||
push('sysEvent', { eventType: 9, eventSource: 1 }); // long press: outside the contract
|
||||
push('sysEvent', { eventType: 9, eventSource: 1 }); // long press: recognised
|
||||
await wait(20);
|
||||
expect(diagnostics()['Glasses input']).toContain('sysEvent(type=9,source=1) -> long_press');
|
||||
push('sysEvent', { eventType: 8, imuData: { x: 1, y: 2, z: 3 } }); // IMU: outside the contract
|
||||
await wait(20);
|
||||
const afterLongPress = diagnostics()['Glasses input']!;
|
||||
expect(afterLongPress).toContain('1 ignored');
|
||||
expect(afterLongPress).toContain('sysEvent(type=9,source=1) -> ignored');
|
||||
expect(afterLongPress).toContain('sysEvent(type=8) -> ignored');
|
||||
push('audioEvent', { audioPcm: [1, 2, 3, 4] });
|
||||
await wait(20);
|
||||
push('sysEvent', { eventType: 3 });
|
||||
|
||||
@@ -304,6 +304,26 @@ describe('Even Hub simulator 0.9.5', () => {
|
||||
expect(h.hermes.executions).toBe(2);
|
||||
});
|
||||
|
||||
it('hold to talk: a long press starts recording and releasing it sends', async () => {
|
||||
h.stt.next({ text: 'simulated held question' });
|
||||
const before = h.hermes.executions;
|
||||
const mark = consoleLines.length;
|
||||
await input('long_press');
|
||||
await waitConsole('-> listening', 10_000, mark);
|
||||
// Held well past the push-to-talk threshold. (With the unpaced null audio device the capture
|
||||
// cap may be reached first; either way the recording must end and be answered once.)
|
||||
await new Promise((r) => setTimeout(r, 1000));
|
||||
await input('long_press_release');
|
||||
await waitConsole('-> answer', 20_000, mark);
|
||||
expect(h.hermes.executions).toBe(before + 1);
|
||||
expect(h.hermes.lastRun()!.input).toBe('simulated held question');
|
||||
await new Promise((r) => setTimeout(r, 500));
|
||||
await screenshot('09-hold-to-talk-answer');
|
||||
// Leave the app at rest for the checks that follow.
|
||||
await input('double_click');
|
||||
await waitConsole('state: answer -> ready', 10_000, mark);
|
||||
});
|
||||
|
||||
it('no uncaught errors or failed fetches were reported by the webview', async () => {
|
||||
await pumpConsole();
|
||||
const problems = consoleLines.filter((l) => /\[uncaught\]|\[unhandledrejection\]|\[fetch\]/.test(l));
|
||||
|
||||
Reference in New Issue
Block a user