Skip to content

Commit 0b92524

Browse files
committed
refactor(ios): share voice permission support
1 parent 9afc4ef commit 0b92524

5 files changed

Lines changed: 54 additions & 121 deletions

File tree

‎apps/.i18n/native-source.json‎

Lines changed: 3 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎apps/ios/Sources/Voice/TalkModeManager.swift‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ final class TalkModeManager: NSObject {
695695
let micOk = if self.allowSimulatorCapture {
696696
true
697697
} else {
698-
await Self.requestMicrophonePermission()
698+
await VoicePermissionSupport.requestMicrophonePermission(timeoutErrorDomain: "TalkMode")
699699
}
700700
GatewayDiagnostics.log(
701701
"talk.timeline microphone permission ok=\(micOk) "
@@ -734,13 +734,13 @@ final class TalkModeManager: NSObject {
734734
let speechOk = if self.allowSimulatorCapture {
735735
true
736736
} else {
737-
await Self.requestSpeechPermission()
737+
await VoicePermissionSupport.requestSpeechPermission(timeoutErrorDomain: "TalkMode")
738738
}
739739
guard speechOk else {
740740
self.logger.warning("start blocked: speech permission denied")
741741
self.stopNativeCaptureAndDiscardTranscript()
742742
deactivateAudioSession()
743-
let status = Self.permissionMessage(
743+
let status = VoicePermissionSupport.speechPermissionMessage(
744744
kind: String(localized: "Speech recognition"),
745745
status: SFSpeechRecognizer.authorizationStatus())
746746
self.setStatus(
@@ -1278,7 +1278,7 @@ final class TalkModeManager: NSObject {
12781278
{
12791279
guard !self.allowSimulatorCapture else { return }
12801280

1281-
let micOk = await Self.requestMicrophonePermission()
1281+
let micOk = await VoicePermissionSupport.requestMicrophonePermission(timeoutErrorDomain: "TalkMode")
12821282
try self.ensurePushToTalkStartCurrent(captureId: captureId, canStartCapture: canStartCapture)
12831283
guard micOk else {
12841284
self.setStatus(
@@ -1290,10 +1290,10 @@ final class TalkModeManager: NSObject {
12901290
])
12911291
}
12921292

1293-
let speechOk = await Self.requestSpeechPermission()
1293+
let speechOk = await VoicePermissionSupport.requestSpeechPermission(timeoutErrorDomain: "TalkMode")
12941294
try self.ensurePushToTalkStartCurrent(captureId: captureId, canStartCapture: canStartCapture)
12951295
guard speechOk else {
1296-
let status = Self.permissionMessage(
1296+
let status = VoicePermissionSupport.speechPermissionMessage(
12971297
kind: String(localized: "Speech recognition"),
12981298
status: SFSpeechRecognizer.authorizationStatus())
12991299
self.setStatus(

apps/ios/Sources/Voice/TalkModeManager+Permissions.swift renamed to apps/ios/Sources/Voice/VoicePermissionSupport.swift

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@ import Foundation
33
import OpenClawKit
44
import Speech
55

6-
extension TalkModeManager {
7-
nonisolated static func requestMicrophonePermission() async -> Bool {
6+
enum VoicePermissionSupport {
7+
static func requestMicrophonePermission(timeoutErrorDomain: String) async -> Bool {
88
switch AVAudioApplication.shared.recordPermission {
99
case .granted:
1010
return true
1111
case .denied:
1212
return false
1313
case .undetermined:
14-
return await self.requestPermissionWithTimeout { completion in
15-
AVAudioApplication.requestRecordPermission(completionHandler: { ok in
16-
completion(ok)
17-
})
14+
return await self.requestPermissionWithTimeout(errorDomain: timeoutErrorDomain) { completion in
15+
AVAudioApplication.requestRecordPermission(completionHandler: completion)
1816
}
1917
@unknown default:
2018
return false
2119
}
2220
}
2321

24-
nonisolated static func requestSpeechPermission() async -> Bool {
22+
static func requestSpeechPermission(timeoutErrorDomain: String) async -> Bool {
2523
let status = SFSpeechRecognizer.authorizationStatus()
2624
switch status {
2725
case .authorized:
@@ -34,29 +32,14 @@ extension TalkModeManager {
3432
return false
3533
}
3634

37-
return await self.requestPermissionWithTimeout { completion in
35+
return await self.requestPermissionWithTimeout(errorDomain: timeoutErrorDomain) { completion in
3836
SFSpeechRecognizer.requestAuthorization { authStatus in
3937
completion(authStatus == .authorized)
4038
}
4139
}
4240
}
4341

44-
private nonisolated static func requestPermissionWithTimeout(
45-
_ operation: @escaping @Sendable (@escaping @Sendable (Bool) -> Void) -> Void) async -> Bool
46-
{
47-
do {
48-
return try await AsyncTimeout.withTimeout(
49-
seconds: 8,
50-
onTimeout: { NSError(domain: "TalkMode", code: 6, userInfo: [
51-
NSLocalizedDescriptionKey: "permission request timed out",
52-
]) },
53-
operation: { await PermissionRequestBridge.awaitRequest(operation) })
54-
} catch {
55-
return false
56-
}
57-
}
58-
59-
static func permissionMessage(
42+
static func speechPermissionMessage(
6043
kind: String,
6144
status: SFSpeechRecognizerAuthorizationStatus) -> String
6245
{
@@ -83,4 +66,20 @@ extension TalkModeManager {
8366
kind)
8467
}
8568
}
69+
70+
private static func requestPermissionWithTimeout(
71+
errorDomain: String,
72+
operation: @escaping @Sendable (@escaping @Sendable (Bool) -> Void) -> Void) async -> Bool
73+
{
74+
do {
75+
return try await AsyncTimeout.withTimeout(
76+
seconds: 8,
77+
onTimeout: { NSError(domain: errorDomain, code: 6, userInfo: [
78+
NSLocalizedDescriptionKey: "permission request timed out",
79+
]) },
80+
operation: { await PermissionRequestBridge.awaitRequest(operation) })
81+
} catch {
82+
return false
83+
}
84+
}
8685
}

‎apps/ios/Sources/Voice/VoiceWakeManager.swift‎

Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import AVFAudio
22
import Foundation
33
import Observation
4-
import OpenClawKit
54
import Speech
65
import SwabbleKit
76

@@ -279,17 +278,17 @@ final class VoiceWakeManager: NSObject {
279278

280279
self.statusText = String(localized: "Requesting permissions…")
281280

282-
let micOk = await Self.requestMicrophonePermission()
281+
let micOk = await VoicePermissionSupport.requestMicrophonePermission(timeoutErrorDomain: "VoiceWake")
283282
guard micOk else {
284283
self.statusText = Self.microphonePermissionMessage(
285284
kind: String(localized: "Microphone"))
286285
self.isListening = false
287286
return
288287
}
289288

290-
let speechOk = await Self.requestSpeechPermission()
289+
let speechOk = await VoicePermissionSupport.requestSpeechPermission(timeoutErrorDomain: "VoiceWake")
291290
guard speechOk else {
292-
self.statusText = Self.permissionMessage(
291+
self.statusText = VoicePermissionSupport.speechPermissionMessage(
293292
kind: String(localized: "Speech recognition"),
294293
status: SFSpeechRecognizer.authorizationStatus())
295294
self.isListening = false
@@ -549,93 +548,13 @@ final class VoiceWakeManager: NSObject {
549548
}
550549
}
551550

552-
private nonisolated static func requestMicrophonePermission() async -> Bool {
553-
switch AVAudioApplication.shared.recordPermission {
554-
case .granted:
555-
return true
556-
case .denied:
557-
return false
558-
case .undetermined:
559-
break
560-
@unknown default:
561-
return false
562-
}
563-
564-
return await self.requestPermissionWithTimeout { completion in
565-
AVAudioApplication.requestRecordPermission(completionHandler: completion)
566-
}
567-
}
568-
569551
private nonisolated static func microphonePermissionMessage(kind: String) -> String {
570552
let status = AVAudioApplication.shared.recordPermission
571553
return self.deniedByDefaultPermissionMessage(
572554
kind: kind,
573555
isUndetermined: status == .undetermined)
574556
}
575557

576-
private nonisolated static func requestSpeechPermission() async -> Bool {
577-
let status = SFSpeechRecognizer.authorizationStatus()
578-
switch status {
579-
case .authorized:
580-
return true
581-
case .denied, .restricted:
582-
return false
583-
case .notDetermined:
584-
break
585-
@unknown default:
586-
return false
587-
}
588-
589-
return await self.requestPermissionWithTimeout { completion in
590-
SFSpeechRecognizer.requestAuthorization { authStatus in
591-
completion(authStatus == .authorized)
592-
}
593-
}
594-
}
595-
596-
private nonisolated static func requestPermissionWithTimeout(
597-
_ operation: @escaping @Sendable (@escaping @Sendable (Bool) -> Void) -> Void) async -> Bool
598-
{
599-
do {
600-
return try await AsyncTimeout.withTimeout(
601-
seconds: 8,
602-
onTimeout: { NSError(domain: "VoiceWake", code: 6, userInfo: [
603-
NSLocalizedDescriptionKey: "permission request timed out",
604-
]) },
605-
operation: { await PermissionRequestBridge.awaitRequest(operation) })
606-
} catch {
607-
return false
608-
}
609-
}
610-
611-
private static func permissionMessage(
612-
kind: String,
613-
status: SFSpeechRecognizerAuthorizationStatus) -> String
614-
{
615-
switch status {
616-
case .denied:
617-
return String(
618-
format: String(localized: "%@ permission denied"),
619-
kind)
620-
case .restricted:
621-
return String(
622-
format: String(localized: "%@ permission restricted"),
623-
kind)
624-
case .notDetermined:
625-
return String(
626-
format: String(localized: "%@ permission not granted"),
627-
kind)
628-
case .authorized:
629-
return String(
630-
format: String(localized: "%@ permission denied"),
631-
kind)
632-
@unknown default:
633-
return String(
634-
format: String(localized: "%@ permission denied"),
635-
kind)
636-
}
637-
}
638-
639558
private nonisolated static func deniedByDefaultPermissionMessage(kind: String, isUndetermined: Bool) -> String {
640559
if isUndetermined {
641560
return String(
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Speech
2+
import Testing
3+
@testable import OpenClaw
4+
5+
struct VoicePermissionSupportTests {
6+
@Test func `speech permission messages preserve authorization detail`() {
7+
let kind = "Speech recognition"
8+
let cases: [(SFSpeechRecognizerAuthorizationStatus, String)] = [
9+
(.denied, "Speech recognition permission denied"),
10+
(.restricted, "Speech recognition permission restricted"),
11+
(.notDetermined, "Speech recognition permission not granted"),
12+
(.authorized, "Speech recognition permission denied"),
13+
]
14+
15+
for (status, expected) in cases {
16+
#expect(VoicePermissionSupport.speechPermissionMessage(kind: kind, status: status) == expected)
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)