forked from Blendman974/faceclaw
74 lines
2.2 KiB
TypeScript
74 lines
2.2 KiB
TypeScript
import { Utils } from "@nativescript/core";
|
|
|
|
declare const com: any;
|
|
|
|
export type VoiceControlState = {
|
|
status: string;
|
|
};
|
|
|
|
export class FaceclawVoiceControlBridge {
|
|
private readonly statusListeners = new Set<(state: VoiceControlState) => void>();
|
|
private readonly wakeWordListeners = new Set<(keyword: string) => void>();
|
|
private controller: any | null = null;
|
|
private listenerProxy: any | null = null;
|
|
private status = "Voice control stopped.";
|
|
private started = false;
|
|
|
|
onStatus(listener: (state: VoiceControlState) => void): () => void {
|
|
this.statusListeners.add(listener);
|
|
listener({ status: this.status });
|
|
return () => this.statusListeners.delete(listener);
|
|
}
|
|
|
|
onWakeWord(listener: (keyword: string) => void): () => void {
|
|
this.wakeWordListeners.add(listener);
|
|
return () => this.wakeWordListeners.delete(listener);
|
|
}
|
|
|
|
start(communicator?: any): void {
|
|
if (this.started || !global.isAndroid) return;
|
|
this.ensureController();
|
|
if (communicator) {
|
|
this.controller?.setCommunicator(communicator);
|
|
}
|
|
this.started = true;
|
|
this.controller?.start();
|
|
}
|
|
|
|
stop(): void {
|
|
if (!this.started || !global.isAndroid) return;
|
|
this.started = false;
|
|
this.controller?.stop();
|
|
this.setStatus("Voice control stopped.");
|
|
}
|
|
|
|
private ensureController(): void {
|
|
if (!global.isAndroid || this.controller) return;
|
|
const context = Utils.android.getApplicationContext();
|
|
if (!context) {
|
|
throw new Error("Android application context unavailable");
|
|
}
|
|
this.controller = new com.faceclaw.app.FaceclawVoiceController(context);
|
|
this.listenerProxy = new com.faceclaw.app.FaceclawVoiceControllerListener({
|
|
onStatus: (status: string) => {
|
|
this.setStatus(String(status));
|
|
},
|
|
onWakeWord: (keyword: string) => {
|
|
for (const listener of this.wakeWordListeners) {
|
|
listener(String(keyword));
|
|
}
|
|
},
|
|
});
|
|
this.controller.setListener(this.listenerProxy);
|
|
}
|
|
|
|
private setStatus(status: string): void {
|
|
this.status = status;
|
|
for (const listener of this.statusListeners) {
|
|
listener({ status });
|
|
}
|
|
}
|
|
}
|
|
|
|
export const voiceControlBridge = new FaceclawVoiceControlBridge();
|