forked from Blendman974/faceclaw
Add calendar app and accelerometer demo
This commit is contained in:
1 parent
3cdc5ba262
commit
ff447d5161
18 files changed
+911
-5
No files matched your search
@@ -0,0 +1,94 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
import { hasCalendarPermission } from "../g2/android-permissions";
|
||||
import { spanCurrent } from "./frame-timings";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
/**
|
||||
* Reads upcoming calendar events through the Android Calendar provider
|
||||
* (FaceclawCalendarProvider). Results are cached briefly so repeated paints of
|
||||
* the Calendar app don't re-run the content-provider query every frame.
|
||||
*/
|
||||
|
||||
export type CalendarEvent = {
|
||||
id: number;
|
||||
title: string;
|
||||
startMs: number;
|
||||
endMs: number;
|
||||
allDay: boolean;
|
||||
location: string;
|
||||
calendarName: string;
|
||||
};
|
||||
|
||||
const DEFAULT_MAX_EVENTS = 50;
|
||||
const DEFAULT_WINDOW_MS = 14 * 24 * 60 * 60 * 1000;
|
||||
const CACHE_MS = 30_000;
|
||||
|
||||
let cache: { events: CalendarEvent[]; atMs: number; maxEvents: number; windowMs: number } | null = null;
|
||||
|
||||
/**
|
||||
* Upcoming events from now through the given window, ordered by start time.
|
||||
* Returns [] when calendar permission is absent. Cached for CACHE_MS; pass
|
||||
* forceRefresh to bypass the cache (e.g. right after the permission grant).
|
||||
*/
|
||||
export function readUpcomingEvents(
|
||||
maxEvents = DEFAULT_MAX_EVENTS,
|
||||
windowMs = DEFAULT_WINDOW_MS,
|
||||
forceRefresh = false,
|
||||
): CalendarEvent[] {
|
||||
if (!global.isAndroid || !hasCalendarPermission()) return [];
|
||||
|
||||
const now = Date.now();
|
||||
if (
|
||||
!forceRefresh &&
|
||||
cache &&
|
||||
cache.maxEvents === maxEvents &&
|
||||
cache.windowMs === windowMs &&
|
||||
now - cache.atMs < CACHE_MS
|
||||
) {
|
||||
return cache.events;
|
||||
}
|
||||
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (!context) return [];
|
||||
|
||||
try {
|
||||
const json = spanCurrent("fetch-calendar-events", () =>
|
||||
String(
|
||||
com.faceclaw.app.FaceclawCalendarProvider.getUpcomingEventsJson(
|
||||
context,
|
||||
Math.max(0, Math.round(maxEvents)),
|
||||
Math.max(0, Math.round(windowMs)),
|
||||
),
|
||||
),
|
||||
);
|
||||
const parsed = JSON.parse(json);
|
||||
const events = Array.isArray(parsed)
|
||||
? parsed.map(normalizeEvent).filter((event): event is CalendarEvent => Boolean(event))
|
||||
: [];
|
||||
cache = { events, atMs: now, maxEvents, windowMs };
|
||||
return events;
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/** Drop the cached events so the next read re-queries the provider. */
|
||||
export function invalidateCalendarCache(): void {
|
||||
cache = null;
|
||||
}
|
||||
|
||||
function normalizeEvent(value: any): CalendarEvent | null {
|
||||
if (!value || typeof value !== "object") return null;
|
||||
const startMs = Number(value.startMs);
|
||||
if (!Number.isFinite(startMs)) return null;
|
||||
return {
|
||||
id: Number(value.id) || 0,
|
||||
title: String(value.title ?? ""),
|
||||
startMs,
|
||||
endMs: Number(value.endMs) || startMs,
|
||||
allDay: Boolean(value.allDay),
|
||||
location: String(value.location ?? ""),
|
||||
calendarName: String(value.calendarName ?? ""),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
declare const com: any;
|
||||
|
||||
/**
|
||||
* IMU (accelerometer) access. The G2 gates its accelerometer stream behind an
|
||||
* ImuCtrl control message; once enabled, the firmware reports x/y/z samples as
|
||||
* sys-events, which the Java communicator decodes and forwards here.
|
||||
*
|
||||
* Note: this stream has historically been unreliable — the firmware may not
|
||||
* populate IMU data at all. Treat it as experimental.
|
||||
*
|
||||
* Talks to the active communicator via the FaceclawBleCommunicator.getActive()
|
||||
* static, so it works from any isolate without threading the bridge through.
|
||||
*/
|
||||
|
||||
export type ImuReading = { x: number; y: number; z: number; source: number };
|
||||
|
||||
function activeCommunicator(): any {
|
||||
if (!global.isAndroid) return null;
|
||||
try {
|
||||
return com.faceclaw.app.FaceclawBleCommunicator.getActive();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Enable/disable the accelerometer report stream. No-op if not connected. */
|
||||
export function setImuReportEnabled(enable: boolean, reportFreq: number): void {
|
||||
const active = activeCommunicator();
|
||||
if (!active) return;
|
||||
try {
|
||||
active.setImuReportEnabled(enable, Math.max(0, Math.round(reportFreq)));
|
||||
} catch (error) {
|
||||
console.warn(`setImuReportEnabled failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Subscribe to IMU readings; returns an unsubscribe function. */
|
||||
export function addImuListener(listener: (reading: ImuReading) => void): () => void {
|
||||
const active = activeCommunicator();
|
||||
if (!active) return () => {};
|
||||
const proxy = new com.faceclaw.app.FaceclawImuListener({
|
||||
onImuData: (x: number, y: number, z: number, source: number) => {
|
||||
listener({ x: Number(x), y: Number(y), z: Number(z), source: Number(source) });
|
||||
},
|
||||
});
|
||||
active.addImuListener(proxy);
|
||||
return () => {
|
||||
try {
|
||||
activeCommunicator()?.removeImuListener(proxy);
|
||||
} catch {
|
||||
// The instance may be gone after a disconnect; nothing to clean up.
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/** Map an IMU sample's EventSource to a short label. */
|
||||
export function imuSourceLabel(source: number): string {
|
||||
switch (source) {
|
||||
case 1:
|
||||
return "R arm";
|
||||
case 2:
|
||||
return "ring";
|
||||
case 3:
|
||||
return "L arm";
|
||||
default:
|
||||
return "device";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user