-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathExpoSpeechRecognitionModule.kt
437 lines (388 loc) · 18.6 KB
/
ExpoSpeechRecognitionModule.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package expo.modules.speechrecognition
import android.Manifest.permission.RECORD_AUDIO
import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Handler
import android.provider.Settings
import android.speech.ModelDownloadListener
import android.speech.RecognitionService
import android.speech.RecognitionSupport
import android.speech.RecognitionSupportCallback
import android.speech.RecognizerIntent
import android.speech.SpeechRecognizer
import android.util.Log
import androidx.annotation.RequiresApi
import expo.modules.interfaces.permissions.Permissions.askForPermissionsWithPermissionsManager
import expo.modules.interfaces.permissions.Permissions.getPermissionsWithPermissionsManager
import expo.modules.kotlin.Promise
import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition
import java.util.concurrent.Executors
class ExpoSpeechRecognitionModule : Module() {
private val expoSpeechService by lazy {
ExpoSpeechService(appContext.reactContext!!) { name, body ->
val nonNullBody = body ?: emptyMap()
try {
sendEvent(name, nonNullBody)
} catch (e: IllegalArgumentException) {
// "Cannot create an event emitter for the module that isn't present in the module registry."
// Likely can occur after destroying the module
Log.e("ExpoSpeechRecognitionModule", "Failed to send event: $name", e)
}
}
}
// Each module class must implement the definition function. The definition consists of components
// that describes the module's functionality and behavior.
// See https://docs.expo.dev/modules/module-api for more details about available components.
override fun definition() =
ModuleDefinition {
// Sets the name of the module that JavaScript code will use to refer to the module. Takes a
// string as an argument.
// Can be inferred from module's class name, but it's recommended to set it explicitly for
// clarity.
// The module will be accessible from `requireNativeModule('ExpoSpeechRecognition')` in
// JavaScript.
Name("ExpoSpeechRecognition")
OnDestroy {
expoSpeechService.destroy()
}
// Defines event names that the module can send to JavaScript.
Events(
// Fired when the user agent has started to capture audio.
"audiostart",
// Fired when the user agent has finished capturing audio.
"audioend",
// Fired when the speech recognition service has disconnected.
"end",
// Fired when a speech recognition error occurs.
"error",
// Fired when the speech recognition service returns a final result with no significant
// recognition. This may involve some degree of recognition, which doesn't meet or
// exceed the confidence threshold.
"nomatch",
// Fired when the speech recognition service returns a result — a word or phrase has been
// positively recognized and this has been communicated back to the app.
"result",
// Fired when any sound — recognizable speech or not — has been detected.
"soundstart",
// Fired when any sound — recognizable speech or not — has stopped being detected.
"soundend",
// Fired when sound that is recognized by the speech recognition service as speech
// has been detected.
"speechstart",
// Fired when speech recognized by the speech recognition service has stopped being
// detected.
"speechend",
// Fired when the speech recognition service has begun listening to incoming audio with
// intent to recognize grammars associated with the current SpeechRecognition
"start",
// Called when there's results (as a string array, not API compliant)
"results",
)
Function("getDefaultRecognitionService") {
val defaultRecognitionService = getDefaultVoiceRecognitionService()?.packageName ?: ""
return@Function mapOf(
"packageName" to defaultRecognitionService,
)
}
Function("getAssistantService") {
val assistantServicePackage = getDefaultAssistantService()?.packageName ?: ""
return@Function mapOf(
"packageName" to assistantServicePackage,
)
}
Function("getSpeechRecognitionServices") {
val packageManager = appContext.reactContext?.packageManager
val serviceNames = mutableListOf<String>()
if (packageManager == null) {
return@Function serviceNames
}
val services =
packageManager.queryIntentServices(
Intent(RecognitionService.SERVICE_INTERFACE),
0,
)
for (service in services) {
serviceNames.add(service.serviceInfo.packageName)
}
return@Function serviceNames
}
AsyncFunction("requestPermissionsAsync") { promise: Promise ->
askForPermissionsWithPermissionsManager(
appContext.permissions,
promise,
RECORD_AUDIO,
)
}
AsyncFunction("getPermissionsAsync") { promise: Promise ->
getPermissionsWithPermissionsManager(
appContext.permissions,
promise,
RECORD_AUDIO,
)
}
AsyncFunction("getStateAsync") { promise: Promise ->
val state =
when (expoSpeechService.recognitionState) {
RecognitionState.INACTIVE -> "inactive"
RecognitionState.STARTING -> "starting"
RecognitionState.ACTIVE -> "recognizing"
RecognitionState.STOPPING -> "stopping"
else -> "inactive"
}
promise.resolve(state)
}
/** Start recognition with args: lang, interimResults, maxAlternatives */
Function("start") { options: SpeechRecognitionOptions ->
if (hasNotGrantedRecordPermissions()) {
sendEvent("error", mapOf("error" to "not-allowed", "message" to "Missing RECORD_AUDIO permissions."))
sendEvent("end")
return@Function
}
expoSpeechService.start(options)
}
Function("stop") {
expoSpeechService.stop()
}
Function("abort") {
sendEvent("error", mapOf("error" to "aborted", "message" to "Speech recognition aborted."))
expoSpeechService.abort()
}
AsyncFunction("getSupportedLocales") { options: GetSupportedLocaleOptions, promise: Promise ->
getSupportedLocales(options, appContext.reactContext!!, promise)
}
Function("supportsOnDeviceRecognition") {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
SpeechRecognizer.isOnDeviceRecognitionAvailable(appContext.reactContext!!)
} else {
false
}
}
Function("isRecognitionAvailable") {
SpeechRecognizer.isRecognitionAvailable(appContext.reactContext!!)
}
Function("supportsRecording") {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
}
// Not necessary for Android
Function("setCategoryIOS") { _: Any ->
// Do nothing
}
Function("getAudioSessionCategoryAndOptionsIOS") {
// Just return dummy data, not necessary for Android
return@Function mapOf(
"category" to "playAndRecord",
"categoryOptions" to listOf("defaultToSpeaker", "allowBluetooth"),
"mode" to "measurement",
)
}
Function("setAudioSessionActiveIOS") { _: Any ->
// Do nothing
}
var isDownloadingModel = false
AsyncFunction("androidTriggerOfflineModelDownload") { options: TriggerOfflineModelDownloadOptions, promise: Promise ->
if (isDownloadingModel) {
promise.reject(
"download_in_progress",
"An offline model download is already in progress.",
Throwable(),
)
return@AsyncFunction
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
promise.reject(
"not_supported",
"Android version is too old to trigger offline model download.",
Throwable(),
)
return@AsyncFunction
}
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, options.locale)
// API 33 (Android 13) -- Trigger the model download but resolve immediately
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
Handler(appContext.reactContext!!.mainLooper).post {
val recognizer =
SpeechRecognizer.createOnDeviceSpeechRecognizer(appContext.reactContext!!)
recognizer.triggerModelDownload(intent)
}
promise.resolve(
mapOf(
"status" to "opened_dialog",
"message" to "Opened the model download dialog.",
),
)
return@AsyncFunction
}
// API 34+ (Android 14+) -- Trigger the model download and listen to the progress
isDownloadingModel = true
Handler(appContext.reactContext!!.mainLooper).post {
val recognizer =
SpeechRecognizer.createOnDeviceSpeechRecognizer(appContext.reactContext!!)
recognizer.triggerModelDownload(
intent,
Executors.newSingleThreadExecutor(),
@SuppressLint("NewApi")
object : ModelDownloadListener {
override fun onProgress(p0: Int) {
// Todo: let user know the progress
}
override fun onSuccess() {
promise.resolve(
mapOf(
"status" to "download_success",
"message" to "Offline model download completed successfully.",
),
)
isDownloadingModel = false
recognizer.destroy()
}
override fun onScheduled() {
promise.resolve(
mapOf(
"status" to "download_canceled",
"message" to "The offline model download was canceled.",
),
)
}
override fun onError(error: Int) {
Log.e("ExpoSpeechService", "Error downloading model with code: $error")
isDownloadingModel = false
promise.reject(
"error_$error",
"Failed to download offline model download with error: $error",
Throwable(),
)
recognizer.destroy()
}
},
)
}
}
}
private fun hasNotGrantedRecordPermissions(): Boolean = appContext.permissions?.hasGrantedPermissions(RECORD_AUDIO)?.not() ?: false
private fun getDefaultAssistantService(): ComponentName? {
val contentResolver = appContext.reactContext?.contentResolver ?: return null
val defaultAssistant = Settings.Secure.getString(contentResolver, "assistant")
if (defaultAssistant.isNullOrEmpty()) {
return null
}
return ComponentName.unflattenFromString(defaultAssistant)
}
private fun getDefaultVoiceRecognitionService(): ComponentName? {
val contentResolver = appContext.reactContext?.contentResolver ?: return null
val defaultVoiceRecognitionService = Settings.Secure.getString(contentResolver, "voice_recognition_service")
if (defaultVoiceRecognitionService.isNullOrEmpty()) {
return null
}
return ComponentName.unflattenFromString(defaultVoiceRecognitionService)
}
private fun getSupportedLocales(
options: GetSupportedLocaleOptions,
appContext: Context,
promise: Promise,
) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
promise.resolve(mapOf(
"locales" to mutableListOf<String>(),
"installedLocales" to mutableListOf<String>(),
))
return
}
if (options.androidRecognitionServicePackage == null && !SpeechRecognizer.isOnDeviceRecognitionAvailable(appContext)) {
promise.resolve(mapOf(
"locales" to mutableListOf<String>(),
"installedLocales" to mutableListOf<String>(),
))
return
}
if (options.androidRecognitionServicePackage != null && !SpeechRecognizer.isRecognitionAvailable(appContext)) {
promise.resolve(mapOf(
"locales" to mutableListOf<String>(),
"installedLocales" to mutableListOf<String>(),
))
return
}
var serviceComponent: ComponentName? = null
try {
if (options.androidRecognitionServicePackage != null) {
serviceComponent =
ExpoSpeechService.findComponentNameByPackageName(
appContext,
options.androidRecognitionServicePackage,
)
}
} catch (e: Exception) {
Log.e("ExpoSpeechService", "Couldn't resolve package: ${options.androidRecognitionServicePackage}")
promise.reject(
"package_not_found",
"Failed to retrieve recognition service package",
e,
)
return
}
var didResolve = false
// Speech Recognizer can only be ran on main thread
Handler(appContext.mainLooper).post {
val recognizer =
if (serviceComponent != null) {
SpeechRecognizer.createSpeechRecognizer(
appContext,
serviceComponent,
)
} else {
SpeechRecognizer.createOnDeviceSpeechRecognizer(appContext)
}
val recognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
recognizer?.checkRecognitionSupport(
recognizerIntent,
Executors.newSingleThreadExecutor(),
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
object : RecognitionSupportCallback {
override fun onSupportResult(recognitionSupport: RecognitionSupport) {
Log.d("ExpoSpeechService", "onSupportResult() called with recognitionSupport: $recognitionSupport")
// Seems to get called twice when using `createSpeechRecognizer()`
if (didResolve) {
return
}
didResolve = true
// These languages are supported but need to be downloaded before use.
val installedLocales = recognitionSupport.installedOnDeviceLanguages
val locales =
recognitionSupport.supportedOnDeviceLanguages
.union(installedLocales)
.union(recognitionSupport.onlineLanguages)
.sorted()
promise.resolve(
mapOf(
"locales" to locales,
"installedLocales" to installedLocales,
),
)
recognizer.destroy()
}
override fun onError(error: Int) {
Log.e("ExpoSpeechService", "getSupportedLocales.onError() called with error code: $error")
// This is a workaround for when both the onSupportResult and onError callbacks are called
// This occurs when providing some packages such as com.google.android.tts
// com.samsung.android.bixby.agent usually errors though
Handler(appContext.mainLooper).postDelayed({
if (didResolve) {
return@postDelayed
}
promise.reject(
"error_$error",
"Failed to retrieve supported locales with error: $error",
Throwable(),
)
}, 50)
recognizer.destroy()
}
},
)
}
}
}