forked from Blendman974/faceclaw
Merge branch 'main' into ios-port
This commit is contained in:
commit
a31ddae4d2
49 files changed
+2068
-212
No files matched your search
+23
-2
@@ -5,10 +5,23 @@ export const COMPASS_CHANGED = 15;
|
||||
export const COMPASS_CALIBRATION_STARTED = 16;
|
||||
export const COMPASS_CALIBRATION_COMPLETE = 17;
|
||||
|
||||
export type CompassDiagnostics = {
|
||||
/** 0 uncalibrated .. 3 well calibrated; -1 unavailable. */
|
||||
magneticAccuracy: number;
|
||||
/** 0 none, 1 small/temperature, 2 large; -1 unavailable. */
|
||||
magneticAnomalies: number;
|
||||
/** 0 unknown, 1 GRV (relative), 2 GMRV, 3 RV. */
|
||||
orientationSource: number;
|
||||
flags: number;
|
||||
sampleTimeMs: number;
|
||||
};
|
||||
|
||||
export type CompassEvent = {
|
||||
command: number;
|
||||
/** Magnetic heading in degrees, or -1 for calibration-only events. */
|
||||
headingDegrees: number;
|
||||
/** Absent with stock/older CFW or an unrecognized diagnostic extension. */
|
||||
diagnostics?: CompassDiagnostics;
|
||||
};
|
||||
|
||||
function activeCommunicator(): any {
|
||||
@@ -41,8 +54,16 @@ export function addCompassListener(listener: (event: CompassEvent) => void): ()
|
||||
const active = activeCommunicator();
|
||||
if (!active) return () => {};
|
||||
const proxy = new com.faceclaw.app.FaceclawCompassListener({
|
||||
onCompassEvent: (command: number, headingDegrees: number) => {
|
||||
listener({ command: Number(command), headingDegrees: Number(headingDegrees) });
|
||||
onCompassEvent: (command: number, headingDegrees: number, magneticAccuracy: number,
|
||||
magneticAnomalies: number, orientationSource: number, diagnosticFlags: number, sampleTimeMs: number) => {
|
||||
listener({
|
||||
command: Number(command), headingDegrees: Number(headingDegrees),
|
||||
diagnostics: diagnosticFlags >= 0 ? {
|
||||
magneticAccuracy: Number(magneticAccuracy), magneticAnomalies: Number(magneticAnomalies),
|
||||
orientationSource: Number(orientationSource), flags: Number(diagnosticFlags),
|
||||
sampleTimeMs: Number(sampleTimeMs),
|
||||
} : undefined,
|
||||
});
|
||||
},
|
||||
});
|
||||
active.addCompassListener(proxy);
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import { getStringSetting, setStringSetting } from "./settings-store";
|
||||
|
||||
export const MEDIA_APPS_KEY = "music.apps";
|
||||
|
||||
// Exact package IDs only: browsers, social feeds and messaging apps commonly
|
||||
// publish sessions for embedded clips. Users can explicitly enable any of them.
|
||||
export const DEFAULT_IGNORED_MEDIA_APPS: readonly string[] = [
|
||||
"com.android.chrome",
|
||||
"org.mozilla.firefox",
|
||||
"com.microsoft.emmx",
|
||||
"com.sec.android.app.sbrowser",
|
||||
"com.facebook.katana",
|
||||
"com.facebook.orca",
|
||||
"com.instagram.android",
|
||||
"com.whatsapp",
|
||||
"org.telegram.messenger",
|
||||
"com.snapchat.android",
|
||||
"com.reddit.frontpage",
|
||||
"com.twitter.android",
|
||||
"com.discord",
|
||||
];
|
||||
|
||||
export type MediaApp = {
|
||||
packageName: string;
|
||||
appName: string;
|
||||
/** Absent until the user chooses; built-in defaults can evolve independently. */
|
||||
enabled?: boolean;
|
||||
};
|
||||
|
||||
/** Keep discovered apps after their sessions end, including ignored apps. */
|
||||
export function readMediaApps(): MediaApp[] {
|
||||
try {
|
||||
const parsed = JSON.parse(getStringSetting(MEDIA_APPS_KEY, "[]"));
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
const apps = new Map<string, MediaApp>();
|
||||
for (const app of parsed) {
|
||||
if (!app || typeof app.packageName !== "string" || !app.packageName) continue;
|
||||
apps.set(app.packageName, {
|
||||
packageName: app.packageName,
|
||||
appName: typeof app.appName === "string" && app.appName ? app.appName : app.packageName,
|
||||
...(typeof app.enabled === "boolean" ? { enabled: app.enabled } : {}),
|
||||
});
|
||||
}
|
||||
return Array.from(apps.values());
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export function rememberMediaApps(discovered: { packageName: string; appName: string }[]): void {
|
||||
const apps = new Map(readMediaApps().map((app) => [app.packageName, app]));
|
||||
let changed = false;
|
||||
for (const app of discovered) {
|
||||
if (!app.packageName) continue;
|
||||
const previous = apps.get(app.packageName);
|
||||
const appName = app.appName || previous?.appName || app.packageName;
|
||||
if (!previous || previous.appName !== appName) {
|
||||
apps.set(app.packageName, { ...previous, packageName: app.packageName, appName });
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (changed) setStringSetting(MEDIA_APPS_KEY, JSON.stringify(Array.from(apps.values())));
|
||||
}
|
||||
|
||||
export function ignoredMediaPackages(): string[] {
|
||||
const ignored = new Set(DEFAULT_IGNORED_MEDIA_APPS);
|
||||
for (const app of readMediaApps()) {
|
||||
if (app.enabled === true) ignored.delete(app.packageName);
|
||||
else if (app.enabled === false) ignored.add(app.packageName);
|
||||
}
|
||||
return Array.from(ignored).sort();
|
||||
}
|
||||
|
||||
export function isMediaAppEnabled(packageName: string): boolean {
|
||||
return !ignoredMediaPackages().includes(packageName);
|
||||
}
|
||||
|
||||
export function setMediaAppEnabled(app: MediaApp, enabled: boolean): void {
|
||||
if (!app.packageName) return;
|
||||
const apps = new Map(readMediaApps().map((item) => [item.packageName, item]));
|
||||
apps.set(app.packageName, { packageName: app.packageName, appName: app.appName || app.packageName, enabled });
|
||||
setStringSetting(MEDIA_APPS_KEY, JSON.stringify(Array.from(apps.values())));
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
import { ignoredMediaPackages, isMediaAppEnabled, MEDIA_APPS_KEY, rememberMediaApps } from "./media-apps";
|
||||
import { onSettingsStoreChanged } from "./settings-store";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
@@ -34,11 +36,12 @@ export class FaceclawMediaBrowserBridge {
|
||||
private readonly pendingConnects = new Map<number, PendingRequest<string>>();
|
||||
private readonly pendingBrowses = new Map<number, PendingRequest<MediaBrowseItem[]>>();
|
||||
private appsCache: MediaBrowserApp[] | null = null;
|
||||
private connectedPackage = "";
|
||||
|
||||
/** Installed apps exposing a media browser service; cached after the first call. */
|
||||
listBrowsableApps(refresh = false): MediaBrowserApp[] {
|
||||
if (!global.isAndroid) return [];
|
||||
if (this.appsCache && !refresh) return this.appsCache;
|
||||
if (this.appsCache && !refresh) return this.enabledApps();
|
||||
this.ensureBrowser();
|
||||
if (!this.browser) return [];
|
||||
try {
|
||||
@@ -52,11 +55,17 @@ export class FaceclawMediaBrowserBridge {
|
||||
serviceClass: String(app.serviceClass ?? ""),
|
||||
appName: String(app.appName ?? ""),
|
||||
}));
|
||||
rememberMediaApps(this.appsCache);
|
||||
} catch (error) {
|
||||
console.warn(`media browser app list parse failed: ${error}`);
|
||||
this.appsCache = [];
|
||||
}
|
||||
return this.appsCache;
|
||||
return this.enabledApps();
|
||||
}
|
||||
|
||||
private enabledApps(): MediaBrowserApp[] {
|
||||
const ignored = new Set(ignoredMediaPackages());
|
||||
return (this.appsCache ?? []).filter((app) => !ignored.has(app.packageName));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,9 +74,11 @@ export class FaceclawMediaBrowserBridge {
|
||||
*/
|
||||
connect(app: MediaBrowserApp): Promise<string> {
|
||||
if (!global.isAndroid) return Promise.reject(new Error("Android only"));
|
||||
if (!isMediaAppEnabled(app.packageName)) return Promise.reject(new Error("App is ignored in Music settings"));
|
||||
this.ensureBrowser();
|
||||
if (!this.browser) return Promise.reject(new Error("Media browser unavailable"));
|
||||
const requestId = this.nextRequestId++;
|
||||
this.connectedPackage = app.packageName;
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
this.pendingConnects.set(requestId, { resolve, reject });
|
||||
this.browser.connect(requestId, app.packageName, app.serviceClass);
|
||||
@@ -77,6 +88,10 @@ export class FaceclawMediaBrowserBridge {
|
||||
/** Fetch one level of the connected service's content tree. */
|
||||
browse(parentId: string): Promise<MediaBrowseItem[]> {
|
||||
if (!global.isAndroid) return Promise.reject(new Error("Android only"));
|
||||
if (this.connectedPackage && !isMediaAppEnabled(this.connectedPackage)) {
|
||||
this.disconnect();
|
||||
return Promise.reject(new Error("App is ignored in Music settings"));
|
||||
}
|
||||
this.ensureBrowser();
|
||||
if (!this.browser) return Promise.reject(new Error("Media browser unavailable"));
|
||||
const requestId = this.nextRequestId++;
|
||||
@@ -89,6 +104,7 @@ export class FaceclawMediaBrowserBridge {
|
||||
/** Start playback of a browsed item through the connected player's session. */
|
||||
playFromMediaId(mediaId: string): void {
|
||||
if (!global.isAndroid) return;
|
||||
if (!this.connectedPackage || !isMediaAppEnabled(this.connectedPackage)) return;
|
||||
this.browser?.playFromMediaId(mediaId);
|
||||
}
|
||||
|
||||
@@ -96,6 +112,7 @@ export class FaceclawMediaBrowserBridge {
|
||||
disconnect(): void {
|
||||
if (!global.isAndroid) return;
|
||||
this.rejectAllPending("Disconnected");
|
||||
this.connectedPackage = "";
|
||||
this.browser?.disconnect();
|
||||
}
|
||||
|
||||
@@ -115,6 +132,11 @@ export class FaceclawMediaBrowserBridge {
|
||||
throw new Error("Android application context unavailable");
|
||||
}
|
||||
this.browser = new com.faceclaw.app.FaceclawMediaBrowser(context);
|
||||
onSettingsStoreChanged((key) => {
|
||||
if (key === MEDIA_APPS_KEY && this.connectedPackage && !isMediaAppEnabled(this.connectedPackage)) {
|
||||
this.disconnect();
|
||||
}
|
||||
});
|
||||
this.listenerProxy = new com.faceclaw.app.FaceclawMediaBrowserListener({
|
||||
onConnectResult: (requestId: number, connected: boolean, rootId: string, error: string) => {
|
||||
const pending = this.pendingConnects.get(Number(requestId));
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import { grayImageFromPacket, PHOTO_TONE } from "./image-files";
|
||||
import { ignoredMediaPackages, MEDIA_APPS_KEY, rememberMediaApps } from "./media-apps";
|
||||
import { onSettingsStoreChanged } from "./settings-store";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
@@ -173,7 +175,22 @@ export class FaceclawMediaControllerBridge {
|
||||
throw new Error("Android application context unavailable");
|
||||
}
|
||||
this.controller = new com.faceclaw.app.FaceclawMediaController(context);
|
||||
// Install defaults and saved overrides before starting session discovery.
|
||||
this.controller.setIgnoredPackagesJson(JSON.stringify(ignoredMediaPackages()));
|
||||
onSettingsStoreChanged((key) => {
|
||||
if (key === MEDIA_APPS_KEY) {
|
||||
this.controller.setIgnoredPackagesJson(JSON.stringify(ignoredMediaPackages()));
|
||||
}
|
||||
});
|
||||
this.listenerProxy = new com.faceclaw.app.FaceclawMediaControllerListener({
|
||||
onSessionAppsChanged: (appsJson: string) => {
|
||||
try {
|
||||
const apps = JSON.parse(String(appsJson));
|
||||
rememberMediaApps(apps);
|
||||
} catch (error) {
|
||||
console.warn(`media session apps parse failed: ${error}`);
|
||||
}
|
||||
},
|
||||
onStateChange: (
|
||||
playbackState: string,
|
||||
packageName: string,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { loadNightscoutSettings, nightscoutApiTokenSetting, nightscoutSiteUrlSetting } from "../ui/dashboard-settings";
|
||||
import { nightscoutApiTokenSetting, nightscoutSiteUrlSetting } from "../ui/dashboard-settings";
|
||||
import { fetchWithUserAgent } from "../util/http";
|
||||
|
||||
export type NightscoutPoint = {
|
||||
@@ -33,6 +33,10 @@ export type NightscoutState = {
|
||||
iob: number | null;
|
||||
cob: number | null;
|
||||
cageTimestampMs: number | null;
|
||||
reservoirUnits: number | null;
|
||||
batteryVoltage: number | null;
|
||||
loopTimestampMs: number | null;
|
||||
/** Pump communication age and operating state; measurements are separate. */
|
||||
pumpStatus: string;
|
||||
openapsStatusShort: string;
|
||||
carbs: NightscoutCarbEvent[];
|
||||
@@ -121,6 +125,9 @@ const DEFAULT_NIGHTSCOUT_STATE: NightscoutState = {
|
||||
iob: null,
|
||||
cob: null,
|
||||
cageTimestampMs: null,
|
||||
reservoirUnits: null,
|
||||
batteryVoltage: null,
|
||||
loopTimestampMs: null,
|
||||
pumpStatus: "",
|
||||
openapsStatusShort: "--",
|
||||
carbs: [],
|
||||
@@ -259,6 +266,9 @@ export class NightscoutBridge {
|
||||
iob: Number.isFinite(iob) ? iob : null,
|
||||
cob,
|
||||
cageTimestampMs: latestSiteChange > 0 ? latestSiteChange : null,
|
||||
reservoirUnits: firstFiniteNumber(latestDeviceStatus?.pump?.reservoir),
|
||||
batteryVoltage: firstFiniteNumber(latestDeviceStatus?.pump?.battery?.voltage),
|
||||
loopTimestampMs: loopTimestampMs > 0 ? loopTimestampMs : null,
|
||||
pumpStatus,
|
||||
openapsStatusShort: loopTimestampMs > 0 ? formatElapsedShort(nowMs - loopTimestampMs) : "--",
|
||||
carbs: parsedTreatments.carbs,
|
||||
@@ -359,6 +369,8 @@ function parseTimestampMs(value: unknown): number {
|
||||
|
||||
function firstFiniteNumber(...values: unknown[]): number | null {
|
||||
for (const value of values) {
|
||||
if (typeof value !== "number" && typeof value !== "string") continue;
|
||||
if (typeof value === "string" && !value.trim()) continue;
|
||||
const numeric = Number(value);
|
||||
if (Number.isFinite(numeric)) {
|
||||
return numeric;
|
||||
@@ -371,8 +383,6 @@ function formatPumpStatus(status: NightscoutDeviceStatusResponse | undefined, no
|
||||
if (!status?.pump) {
|
||||
return "--";
|
||||
}
|
||||
const reservoir = firstFiniteNumber(status.pump.reservoir);
|
||||
const voltage = firstFiniteNumber(status.pump.battery?.voltage);
|
||||
const commTimestampMs = Math.max(
|
||||
parseTimestampMs(status.pump.status?.timestamp),
|
||||
parseTimestampMs(status.pump.clock),
|
||||
@@ -380,12 +390,6 @@ function formatPumpStatus(status: NightscoutDeviceStatusResponse | undefined, no
|
||||
);
|
||||
const stateLabel = summarizePumpState(status);
|
||||
const parts: string[] = [];
|
||||
if (reservoir !== null) {
|
||||
parts.push(`${Math.round(reservoir)}U`);
|
||||
}
|
||||
if (voltage !== null) {
|
||||
parts.push(`${voltage.toFixed(2)}v`);
|
||||
}
|
||||
if (commTimestampMs > 0) {
|
||||
parts.push(`${formatElapsedShort(nowMs - commTimestampMs)} ago`);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import { logCurrent, spanCurrent } from "./frame-timings";
|
||||
import { toUint8Array } from "../util/array-util";
|
||||
import { rememberNotificationSources } from "./notification-sources";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
const ICON_SIZE = 24;
|
||||
/** Ask the native listener for every active source, including those below the list's display limit. */
|
||||
export const ALL_NOTIFICATIONS = 0x7fffffff;
|
||||
// Backstop TTL only: the icon caches are invalidated eagerly whenever a
|
||||
// notification is posted or dismissed (see invalidateIconCaches callers), so
|
||||
// the tray is kept fresh by invalidation, not by expiry. A short TTL just
|
||||
@@ -158,7 +161,9 @@ export function readActiveNotifications(maxNotifications = 50): AndroidNotificat
|
||||
);
|
||||
const parsed = JSON.parse(json);
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return parsed.map(normalizeNotification).filter((item): item is AndroidNotification => Boolean(item));
|
||||
const notifications = parsed.map(normalizeNotification).filter((item): item is AndroidNotification => Boolean(item));
|
||||
rememberNotificationSources(notifications);
|
||||
return notifications;
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import { getStringSetting, setStringSetting } from "./settings-store";
|
||||
|
||||
const SOURCES_KEY = "notifications.sources";
|
||||
|
||||
export type NotificationSource = {
|
||||
packageName: string;
|
||||
appName: string;
|
||||
showOnGlasses: boolean;
|
||||
};
|
||||
|
||||
/** Sources outlive their active notifications so a muted app can always be re-enabled. */
|
||||
export function readNotificationSources(): NotificationSource[] {
|
||||
try {
|
||||
const parsed = JSON.parse(getStringSetting(SOURCES_KEY, "[]"));
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return parsed.filter((source) => source && typeof source.packageName === "string" && source.packageName)
|
||||
.map((source) => ({
|
||||
packageName: source.packageName,
|
||||
appName: typeof source.appName === "string" && source.appName ? source.appName : source.packageName,
|
||||
showOnGlasses: source.showOnGlasses !== false,
|
||||
}));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export function rememberNotificationSources(notifications: { packageName: string; appName: string }[]): void {
|
||||
const sources = new Map(readNotificationSources().map((source) => [source.packageName, source]));
|
||||
let changed = false;
|
||||
for (const notification of notifications) {
|
||||
if (!notification.packageName) continue;
|
||||
const previous = sources.get(notification.packageName);
|
||||
const appName = notification.appName || previous?.appName || notification.packageName;
|
||||
if (!previous || previous.appName !== appName) {
|
||||
sources.set(notification.packageName, {
|
||||
packageName: notification.packageName,
|
||||
appName,
|
||||
showOnGlasses: previous?.showOnGlasses ?? true,
|
||||
});
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (changed) setStringSetting(SOURCES_KEY, JSON.stringify(Array.from(sources.values())));
|
||||
}
|
||||
|
||||
export function shouldShowNotificationOnGlasses(packageName: string): boolean {
|
||||
return readNotificationSources().find((source) => source.packageName === packageName)?.showOnGlasses ?? true;
|
||||
}
|
||||
|
||||
export function setNotificationSourceEnabled(source: { packageName: string; appName: string }, enabled: boolean): void {
|
||||
if (!source.packageName) return;
|
||||
const sources = new Map(readNotificationSources().map((item) => [item.packageName, item]));
|
||||
sources.set(source.packageName, {
|
||||
packageName: source.packageName,
|
||||
appName: source.appName || source.packageName,
|
||||
showOnGlasses: enabled,
|
||||
});
|
||||
setStringSetting(SOURCES_KEY, JSON.stringify(Array.from(sources.values())));
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Application, isAndroid } from "@nativescript/core";
|
||||
import { getStringSetting, onSettingsStoreChanged } from "./settings-store";
|
||||
|
||||
// Shared with phoneRotationSetting; keep startup independent of the glasses UI.
|
||||
const ROTATION_KEY = "phone.rotation";
|
||||
|
||||
/** Installed once on the UI thread; also covers navigation and activity recreation. */
|
||||
export function registerPhoneRotation(): void {
|
||||
if (!isAndroid) return;
|
||||
|
||||
const apply = (activity = Application.android.foregroundActivity ?? Application.android.startActivity): void => {
|
||||
if (!activity) return;
|
||||
const orientations = android.content.pm.ActivityInfo;
|
||||
const requested = {
|
||||
auto: orientations.SCREEN_ORIENTATION_UNSPECIFIED,
|
||||
portrait: orientations.SCREEN_ORIENTATION_PORTRAIT,
|
||||
landscape: orientations.SCREEN_ORIENTATION_SENSOR_LANDSCAPE,
|
||||
}[getStringSetting(ROTATION_KEY, "auto")] ?? orientations.SCREEN_ORIENTATION_UNSPECIFIED;
|
||||
if (activity.getRequestedOrientation() !== requested) {
|
||||
activity.setRequestedOrientation(requested);
|
||||
}
|
||||
};
|
||||
|
||||
Application.android.on(Application.android.activityCreatedEvent, (args) => apply(args.activity));
|
||||
Application.android.on(Application.android.activityResumedEvent, (args) => apply(args.activity));
|
||||
onSettingsStoreChanged((key) => {
|
||||
if (key === ROTATION_KEY) apply();
|
||||
});
|
||||
apply();
|
||||
}
|
||||
Reference in new issue
Block a user