Import apps/android/src/services/foreground-service.ts

This commit is contained in:
QQ 2026-11-14 14:17:46 +08:00
parent 4154cfaa08
commit b5cfc4e701
1 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,111 @@
interface ForegroundServiceModule {
notifyTask(hostId: string, sessionId: string, title: string, body: string): void
start(): Promise<void>
stop(): Promise<void>
setStatus(status: string): Promise<void>
waitUntilStopped(lifetimeId: string): Promise<void>
isIgnoringBatteryOptimizations?(): Promise<boolean>
requestIgnoreBatteryOptimizations?(): Promise<boolean>
updateLiveTask?(hostId: string, sessionId: string, badge: string, title: string, statusText: string, isWaitingInput: boolean, updatedAtMs: number, summary: string): Promise<void>
clearLiveTask?(): Promise<void>
}
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<void> {
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<void> {
const rn = reactNative()
if (rn?.Platform.OS !== 'android') return
await nativeModule(rn).stop()
},
async setStatus(status: 'connecting' | 'connected' | 'reconnecting'): Promise<void> {
const rn = reactNative()
if (rn?.Platform.OS !== 'android') return
await nativeModule(rn).setStatus(status)
},
async waitUntilStopped(lifetimeId: string): Promise<void> {
const rn = reactNative()
if (rn?.Platform.OS !== 'android') return
await nativeModule(rn).waitUntilStopped(lifetimeId)
},
async isIgnoringBatteryOptimizations(): Promise<boolean> {
const rn = reactNative()
if (rn?.Platform.OS !== 'android') return false
try {
return (await nativeModule(rn).isIgnoringBatteryOptimizations?.()) ?? false
} catch {
return false
}
},
async requestIgnoreBatteryOptimizations(): Promise<boolean> {
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<void> {
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<void> {
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.
}
},
}