-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwithInCallAudioModule.js
More file actions
316 lines (261 loc) · 11.2 KB
/
withInCallAudioModule.js
File metadata and controls
316 lines (261 loc) · 11.2 KB
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
const { withDangerousMod, withMainApplication } = require('expo/config-plugins');
const fs = require('fs');
const path = require('path');
/**
* Android InCallAudioModule.kt content
* Uses SoundPool to play sounds on the VOICE_COMMUNICATION stream.
*/
const ANDROID_MODULE = `package {{PACKAGE_NAME}}
import android.content.Context
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.SoundPool
import android.util.Log
import com.facebook.react.bridge.*
class InCallAudioModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
companion object {
private const val TAG = "InCallAudioModule"
}
private var soundPool: SoundPool? = null
private val soundMap = HashMap<String, Int>()
private val loadedSounds = HashSet<Int>()
private var isInitialized = false
override fun getName(): String {
return "InCallAudioModule"
}
@ReactMethod
fun initializeAudio() {
if (isInitialized) return
val audioAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build()
soundPool = SoundPool.Builder()
.setMaxStreams(1)
.setAudioAttributes(audioAttributes)
.build()
soundPool?.setOnLoadCompleteListener { _, sampleId, status ->
if (status == 0) {
loadedSounds.add(sampleId)
Log.d(TAG, "Sound loaded successfully: $sampleId")
} else {
Log.e(TAG, "Failed to load sound $sampleId, status: $status")
}
}
isInitialized = true
Log.d(TAG, "InCallAudioModule initialized with USAGE_VOICE_COMMUNICATION")
}
@ReactMethod
fun loadSound(name: String, resourceName: String) {
if (!isInitialized) initializeAudio()
val context = reactApplicationContext
var resId = context.resources.getIdentifier(resourceName, "raw", context.packageName)
// Fallback: Try identifying without package name if first attempt fails (though context.packageName is usually correct)
if (resId == 0) {
Log.w(TAG, "Resource $resourceName not found in \${context.packageName}, trying simplified lookup")
// Reflection-based lookup if needed, but getIdentifier is standard.
}
if (resId != 0) {
soundPool?.let { pool ->
val soundId = pool.load(context, resId, 1)
soundMap[name] = soundId
Log.d(TAG, "Loading sound: $name from resource: $resourceName (id: $soundId, resId: $resId)")
}
} else {
Log.e(TAG, "Resource not found: $resourceName in package \${context.packageName}")
}
}
@ReactMethod
fun playSound(name: String) {
val soundId = soundMap[name]
if (soundId != null) {
if (loadedSounds.contains(soundId)) {
val streamId = soundPool?.play(soundId, 0.5f, 0.5f, 1, 0, 1.0f)
if (streamId == 0) {
Log.e(TAG, "Failed to play sound: $name (id: $soundId). StreamId is 0. Check Volume/Focus.")
} else {
Log.d(TAG, "Playing sound: $name (id: $soundId, stream: $streamId)")
}
} else {
Log.w(TAG, "Sound $name (id: $soundId) is not ready yet. Ignoring play request.")
}
} else {
Log.w(TAG, "Sound not found in map: $name")
}
}
@ReactMethod
fun setAudioRoute(route: String, promise: Promise) {
try {
val audioManager = reactApplicationContext.getSystemService(Context.AUDIO_SERVICE) as? AudioManager
if (audioManager == null) {
promise.reject("AUDIO_MANAGER_UNAVAILABLE", "AudioManager is not available")
return
}
val normalizedRoute = route.lowercase()
audioManager.mode = AudioManager.MODE_IN_COMMUNICATION
when (normalizedRoute) {
"bluetooth" -> {
audioManager.isSpeakerphoneOn = false
if (!audioManager.isBluetoothScoAvailableOffCall) {
audioManager.isBluetoothScoOn = false
promise.reject("BLUETOOTH_SCO_UNAVAILABLE", "Bluetooth SCO is not available off call")
return
}
audioManager.startBluetoothSco()
audioManager.isBluetoothScoOn = true
if (!audioManager.isBluetoothScoOn) {
promise.reject("BLUETOOTH_SCO_START_FAILED", "Failed to start Bluetooth SCO")
return
}
}
"speaker" -> {
audioManager.stopBluetoothSco()
audioManager.isBluetoothScoOn = false
audioManager.isSpeakerphoneOn = true
}
"earpiece", "default" -> {
audioManager.stopBluetoothSco()
audioManager.isBluetoothScoOn = false
audioManager.isSpeakerphoneOn = false
}
else -> {
promise.reject("INVALID_AUDIO_ROUTE", "Unsupported audio route: $route")
return
}
}
Log.d(TAG, "Audio route set to: $normalizedRoute")
promise.resolve(true)
} catch (error: Exception) {
Log.e(TAG, "Failed to set audio route: $route", error)
promise.reject("SET_AUDIO_ROUTE_FAILED", error.message, error)
}
}
@ReactMethod
fun cleanup() {
soundPool?.release()
soundPool = null
soundMap.clear()
loadedSounds.clear()
isInitialized = false
Log.d(TAG, "InCallAudioModule cleaned up")
}
}
`;
/**
* Android InCallAudioPackage.kt content
*/
const ANDROID_PACKAGE = `package {{PACKAGE_NAME}}
import android.view.View
import com.facebook.react.ReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ReactShadowNode
import com.facebook.react.uimanager.ViewManager
class InCallAudioPackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return listOf(InCallAudioModule(reactContext))
}
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<View, ReactShadowNode<*>>> {
return emptyList()
}
}
`;
/**
* Helper to resolve package name
*/
function resolveBasePackageName(projectRoot, fallback = 'com.resgrid.unit') {
const namespaceRegex = /namespace\s*(?:=)?\s*['"]([^'"]+)['"]/;
const groovyPath = path.join(projectRoot, 'android', 'app', 'build.gradle');
if (fs.existsSync(groovyPath)) {
const content = fs.readFileSync(groovyPath, 'utf-8');
const match = content.match(namespaceRegex);
if (match) return match[1];
}
const ktsPath = path.join(projectRoot, 'android', 'app', 'build.gradle.kts');
if (fs.existsSync(ktsPath)) {
const content = fs.readFileSync(ktsPath, 'utf-8');
const match = content.match(namespaceRegex);
if (match) return match[1];
}
return fallback;
}
const withInCallAudioModule = (config) => {
// 1. Copy Assets to Android res/raw
config = withDangerousMod(config, [
'android',
async (config) => {
const projectRoot = config.modRequest.projectRoot;
const resRawPath = path.join(projectRoot, 'android', 'app', 'src', 'main', 'res', 'raw');
if (!fs.existsSync(resRawPath)) {
fs.mkdirSync(resRawPath, { recursive: true });
}
const assets = ['software_interface_start.mp3', 'software_interface_back.mp3', 'positive_interface_beep.mp3', 'space_notification1.mp3', 'space_notification2.mp3'];
const sourceBase = path.join(projectRoot, 'assets', 'audio', 'ui');
assets.forEach((filename) => {
const sourcePath = path.join(sourceBase, filename);
const destPath = path.join(resRawPath, filename);
if (fs.existsSync(sourcePath)) {
fs.copyFileSync(sourcePath, destPath);
console.log(`[withInCallAudioModule] Copied ${filename} to res/raw/${filename}`);
} else {
console.warn(`[withInCallAudioModule] Source audio file not found: ${sourcePath}`);
}
});
return config;
},
]);
// 2. Add Native Module Code
config = withDangerousMod(config, [
'android',
async (config) => {
const projectRoot = config.modRequest.projectRoot;
const packageName = resolveBasePackageName(projectRoot);
const packagePath = packageName.replace(/\./g, '/');
const androidSrcPath = path.join(projectRoot, 'android', 'app', 'src', 'main', 'java', packagePath);
if (!fs.existsSync(androidSrcPath)) {
fs.mkdirSync(androidSrcPath, { recursive: true });
}
// InCallAudioModule.kt
const modulePath = path.join(androidSrcPath, 'InCallAudioModule.kt');
const moduleContent = ANDROID_MODULE.replace(/\{\{PACKAGE_NAME\}\}/g, packageName);
fs.writeFileSync(modulePath, moduleContent);
console.log('[withInCallAudioModule] Created InCallAudioModule.kt');
// InCallAudioPackage.kt
const packageFilePath = path.join(androidSrcPath, 'InCallAudioPackage.kt');
const packageContent = ANDROID_PACKAGE.replace(/\{\{PACKAGE_NAME\}\}/g, packageName);
fs.writeFileSync(packageFilePath, packageContent);
console.log('[withInCallAudioModule] Created InCallAudioPackage.kt');
return config;
},
]);
// 3. Register Package in MainApplication.kt
config = withMainApplication(config, (config) => {
const mainApplication = config.modResults;
const projectRoot = config.modRequest.projectRoot;
if (!mainApplication.contents.includes('InCallAudioPackage')) {
const basePackageName = resolveBasePackageName(projectRoot);
const importStatement = `import ${basePackageName}.InCallAudioPackage`;
if (!mainApplication.contents.includes(importStatement)) {
mainApplication.contents = mainApplication.contents.replace(/^(package\s+[^\n]+\n)/, `$1${importStatement}\n`);
}
const packagesPattern = /val packages = PackageList\(this\)\.packages(\.toMutableList\(\))?/;
const packagesMatch = mainApplication.contents.match(packagesPattern);
if (packagesMatch) {
// Using the simplest replacement that ensures toMutableList()
const replacement = `val packages = PackageList(this).packages.toMutableList()\n packages.add(InCallAudioPackage())`;
// Avoid double adding if MediaButtonPackage logic already changed it to mutable
if (mainApplication.contents.includes('packages.add(MediaButtonPackage()')) {
// Add ours after MediaButtonPackage
mainApplication.contents = mainApplication.contents.replace('packages.add(MediaButtonPackage())', 'packages.add(MediaButtonPackage())\n packages.add(InCallAudioPackage())');
} else {
// Standard replacement
mainApplication.contents = mainApplication.contents.replace(packagesPattern, replacement);
}
console.log('[withInCallAudioModule] Registered InCallAudioPackage in MainApplication.kt');
}
}
return config;
});
return config;
};
module.exports = withInCallAudioModule;