The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { requestRecordingPermissionsAsync } from "expo-audio"; |
| 2 | import { AppState, Platform } from "react-native"; |
| 3 | |
| 4 | export function createVoiceRecordingPermission(onBackground: () => void) { |
| 5 | let requesting = false; |
| 6 | let disposed = false; |
| 7 | let resume: (() => void) | null = null; |
| 8 | const subscription = AppState.addEventListener("change", (state) => { |
| 9 | if (state === "active") resume?.(); |
| 10 | if (state === "background" && !requesting) onBackground(); |
| 11 | }); |
| 12 | return { |
| 13 | requestPermission: async () => { |
| 14 | if (Platform.OS !== "android") return requestRecordingPermissionsAsync(); |
| 15 | // Android's permission activity backgrounds the app; resume before preparing Google's model. |
| 16 | requesting = true; |
| 17 | try { |
| 18 | const permission = await requestRecordingPermissionsAsync(); |
| 19 | if (!disposed && AppState.currentState !== "active") { |
| 20 | await new Promise<void>((resolve) => { |
| 21 | resume = resolve; |
| 22 | }); |
| 23 | } |
| 24 | return permission; |
| 25 | } finally { |
| 26 | requesting = false; |
| 27 | resume = null; |
| 28 | } |
| 29 | }, |
| 30 | dispose: () => { |
| 31 | disposed = true; |
| 32 | subscription.remove(); |
| 33 | resume?.(); |
| 34 | }, |
| 35 | }; |
| 36 | } |