From b5cfc4e7011fc5a2c61caa751767609b065725b4 Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:17:46 +0800 Subject: [PATCH] Import apps/android/src/services/foreground-service.ts --- .../src/services/foreground-service.ts | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 apps/android/src/services/foreground-service.ts diff --git a/apps/android/src/services/foreground-service.ts b/apps/android/src/services/foreground-service.ts new file mode 100644 index 0000000..74e6305 --- /dev/null +++ b/apps/android/src/services/foreground-service.ts @@ -0,0 +1,111 @@ +interface ForegroundServiceModule { + notifyTask(hostId: string, sessionId: string, title: string, body: string): void + start(): Promise + stop(): Promise + setStatus(status: string): Promise + waitUntilStopped(lifetimeId: string): Promise + isIgnoringBatteryOptimizations?(): Promise + requestIgnoreBatteryOptimizations?(): Promise + updateLiveTask?(hostId: string, sessionId: string, badge: string, title: string, statusText: string, isWaitingInput: boolean, updatedAtMs: number, summary: string): Promise + clearLiveTask?(): Promise +} + +function reactNative(): typeof import('react-native') | undefined { + // Keep Node/Vitest service tests independent from React Native's native + // entrypoint. Metro resolves this require in the Android bundle. + try { + return require('react-native') as typeof import('react-native') + } catch { + return undefined + } +} + +function nativeModule(rn: typeof import('react-native')): ForegroundServiceModule { + const native = rn.NativeModules.DshRemoteForegroundService as ForegroundServiceModule | undefined + if (native === undefined) throw new Error('Android connection service is missing. Rebuild and reinstall the APK.') + return native +} + +/** iOS/web/Node skip this Android-only service; Android requires a rebuilt native module. */ +export const foregroundService = { + notifyTask(hostId: string, sessionId: string, title: string, body: string): void { + const rn = reactNative() + if (rn?.Platform.OS !== 'android') return + nativeModule(rn).notifyTask(hostId, sessionId, title.slice(0, 100), body) + }, + async start(): Promise { + const rn = reactNative() + if (rn?.Platform.OS !== 'android') return + const native = nativeModule(rn) + // Notification denial does not prohibit an FGS, but without this request + // Android 13+ hides its notification from the drawer. + if (Number(rn.Platform.Version) >= 33 && rn.AppState.currentState === 'active') { + const permission = rn.PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS + if (!await rn.PermissionsAndroid.check(permission)) await rn.PermissionsAndroid.request(permission) + } + await native.start() + }, + async stop(): Promise { + const rn = reactNative() + if (rn?.Platform.OS !== 'android') return + await nativeModule(rn).stop() + }, + async setStatus(status: 'connecting' | 'connected' | 'reconnecting'): Promise { + const rn = reactNative() + if (rn?.Platform.OS !== 'android') return + await nativeModule(rn).setStatus(status) + }, + async waitUntilStopped(lifetimeId: string): Promise { + const rn = reactNative() + if (rn?.Platform.OS !== 'android') return + await nativeModule(rn).waitUntilStopped(lifetimeId) + }, + async isIgnoringBatteryOptimizations(): Promise { + const rn = reactNative() + if (rn?.Platform.OS !== 'android') return false + try { + return (await nativeModule(rn).isIgnoringBatteryOptimizations?.()) ?? false + } catch { + return false + } + }, + async requestIgnoreBatteryOptimizations(): Promise { + const rn = reactNative() + if (rn?.Platform.OS !== 'android') return true + try { + return (await nativeModule(rn).requestIgnoreBatteryOptimizations?.()) ?? false + } catch { + return false + } + }, + async updateLiveTask( + hostId: string, + sessionId: string, + badge: string, + title: string, + statusText: string, + isWaitingInput: boolean, + updatedAtMs: number, + summary: string, + ): Promise { + const rn = reactNative() + if (rn?.Platform.OS !== 'android') return + try { + const native = rn.NativeModules.DshRemoteForegroundService as ForegroundServiceModule | undefined + await native?.updateLiveTask?.(hostId, sessionId, badge, title, statusText, isWaitingInput, updatedAtMs, summary) + } catch { + // Best-effort live task update. + } + }, + async clearLiveTask(): Promise { + const rn = reactNative() + if (rn?.Platform.OS !== 'android') return + try { + const native = rn.NativeModules.DshRemoteForegroundService as ForegroundServiceModule | undefined + await native?.clearLiveTask?.() + } catch { + // Best-effort live task clear. + } + }, +} +