1import { Capacitor, registerPlugin, type PluginListenerHandle } from "@capacitor/core";23export type FoldPosture = "flat" | "tabletop" | "book" | "unknown";45export interface PostureChangedEvent {6 posture: FoldPosture;7 hingeTop: number;8 hingeBottom: number;9 hingeLeft: number;10 hingeRight: number;11 hingeHeight: number;12}1314interface FoldPlugin {15 startMonitoring(): Promise<{ posture: FoldPosture }>;16 stopMonitoring(): Promise<void>;17 addListener(18 event: "postureChanged",19 fn: (e: PostureChangedEvent) => void,20 ): Promise<PluginListenerHandle> | PluginListenerHandle;21}2223const Fold = registerPlugin<FoldPlugin>("Fold");2425export async function startFoldMonitoring(26 onChange: (e: PostureChangedEvent) => void,27): Promise<() => void> {28 if (!Capacitor.isNativePlatform() || Capacitor.getPlatform() !== "android") {29 return () => {};30 }31 let initial: FoldPosture = "flat";32 try {33 const r = await Fold.startMonitoring();34 initial = r.posture;35 } catch {36 return () => {};37 }38 onChange({39 posture: initial,40 hingeTop: 0,41 hingeBottom: 0,42 hingeLeft: 0,43 hingeRight: 0,44 hingeHeight: 0,45 });46 const handle = await Fold.addListener("postureChanged", onChange);47 return () => {48 void handle.remove();49 void Fold.stopMonitoring().catch(() => {});50 };51}