-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.plugin.js
111 lines (100 loc) · 2.98 KB
/
app.plugin.js
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
// @ts-check
const {
withAndroidManifest,
AndroidConfig,
withPlugins,
createRunOncePlugin,
} = require("expo/config-plugins");
/**
* @type {import("expo/config-plugins").ConfigPlugin<{ packages?: string[] }>}
*/
const withAndroidPackageVisibilityFiltering = (config, { packages = [] }) => {
return withAndroidManifest(config, (config) => {
// Default speech recognition packages
const allPackages =
packages.length > 0
? packages
: [
"com.google.android.googlequicksearchbox",
// "com.samsung.android.bixby.agent",
// "com.microsoft.cortana",
// "com.nuance.balerion",
// "com.htc.sense.hsp",
];
// Add speech recognition packages to the manifest if not already present
config.modResults.manifest.queries =
config.modResults.manifest.queries || [];
/**
Appends the following to our AndroidManifest:
<queries>
<package android:name="com.google.android.googlequicksearchbox" />
<intent>
<action android:name="android.speech.RecognitionService" />
</intent>
</queries>
*/
for (const pkg of allPackages) {
if (
!config.modResults.manifest.queries.some(
(query) => query.package?.[0]?.$?.["android:name"] === pkg,
)
) {
config.modResults.manifest.queries.push({
package: [{ $: { "android:name": pkg } }],
intent: [
{
action: [
{
$: { "android:name": "android.speech.RecognitionService" },
},
],
},
],
});
}
}
return config;
});
};
/**
* @type {import("expo/config-plugins").ConfigPlugin<{
* microphonePermission?: string;
* speechRecognitionPermission?: string;
* androidSpeechServicePackages?: string[];
* }|undefined>}
*/
const withExpoSpeechRecognition = (config, props) => {
if (!config.ios) {
config.ios = {};
}
if (!config.ios.infoPlist) {
config.ios.infoPlist = {};
}
config.ios.infoPlist.NSSpeechRecognitionUsageDescription =
props?.speechRecognitionPermission ||
config.ios.infoPlist.NSSpeechRecognitionUsageDescription ||
"Allow $(PRODUCT_NAME) to use speech recognition.";
config.ios.infoPlist.NSMicrophoneUsageDescription =
props?.microphonePermission ||
config.ios.infoPlist.NSMicrophoneUsageDescription ||
"Allow $(PRODUCT_NAME) to use the microphone.";
return withPlugins(config, [
// Android permissions
[
AndroidConfig.Permissions.withPermissions,
["android.permission.RECORD_AUDIO"],
],
// Android package visibility filtering (for the Google App "com.google.android.googlequicksearchbox")
[
withAndroidPackageVisibilityFiltering,
{
packages: props?.androidSpeechServicePackages,
},
],
]);
};
module.exports = createRunOncePlugin(
withExpoSpeechRecognition,
"expo-speech-recognition",
"1.0.0",
);