-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwithMediaButtonModule.js
More file actions
544 lines (450 loc) · 20.3 KB
/
withMediaButtonModule.js
File metadata and controls
544 lines (450 loc) · 20.3 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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
const { withDangerousMod, withMainApplication } = require('expo/config-plugins');
const fs = require('fs');
const path = require('path');
/**
* iOS MediaButtonModule.swift content
*/
const IOS_SWIFT_MODULE = `import Foundation
import MediaPlayer
import React
@objc(MediaButtonModule)
class MediaButtonModule: RCTEventEmitter {
private var hasListeners = false
private var commandCenter: MPRemoteCommandCenter?
override init() {
super.init()
commandCenter = MPRemoteCommandCenter.shared()
}
override static func moduleName() -> String! {
return "MediaButtonModule"
}
override static func requiresMainQueueSetup() -> Bool {
return true
}
override func supportedEvents() -> [String]! {
return [
"onMediaButtonPlayPause",
"onMediaButtonPlay",
"onMediaButtonPause",
"onMediaButtonToggle",
"onMediaButtonNext",
"onMediaButtonPrevious"
]
}
override func startObserving() {
hasListeners = true
}
override func stopObserving() {
hasListeners = false
}
@objc
func startListening() {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
// Setup play/pause toggle command (primary PTT trigger)
self.commandCenter?.togglePlayPauseCommand.isEnabled = true
self.commandCenter?.togglePlayPauseCommand.addTarget { [weak self] event in
self?.sendEventIfListening("onMediaButtonToggle", body: ["timestamp": Date().timeIntervalSince1970 * 1000])
return .success
}
// Setup play command
self.commandCenter?.playCommand.isEnabled = true
self.commandCenter?.playCommand.addTarget { [weak self] event in
self?.sendEventIfListening("onMediaButtonPlay", body: ["timestamp": Date().timeIntervalSince1970 * 1000])
return .success
}
// Setup pause command
self.commandCenter?.pauseCommand.isEnabled = true
self.commandCenter?.pauseCommand.addTarget { [weak self] event in
self?.sendEventIfListening("onMediaButtonPause", body: ["timestamp": Date().timeIntervalSince1970 * 1000])
return .success
}
// Setup next track command (optional - can be used for other PTT actions)
self.commandCenter?.nextTrackCommand.isEnabled = true
self.commandCenter?.nextTrackCommand.addTarget { [weak self] event in
self?.sendEventIfListening("onMediaButtonNext", body: ["timestamp": Date().timeIntervalSince1970 * 1000])
return .success
}
// Setup previous track command (optional)
self.commandCenter?.previousTrackCommand.isEnabled = true
self.commandCenter?.previousTrackCommand.addTarget { [weak self] event in
self?.sendEventIfListening("onMediaButtonPrevious", body: ["timestamp": Date().timeIntervalSince1970 * 1000])
return .success
}
// Setup now playing info to enable media controls
var nowPlayingInfo = [String: Any]()
nowPlayingInfo[MPMediaItemPropertyTitle] = "PTT Active"
nowPlayingInfo[MPMediaItemPropertyArtist] = "Resgrid"
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 1.0
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = 0.0
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
print("[MediaButtonModule] Started listening for media button events")
}
}
@objc
func stopListening() {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.commandCenter?.togglePlayPauseCommand.removeTarget(nil)
self.commandCenter?.playCommand.removeTarget(nil)
self.commandCenter?.pauseCommand.removeTarget(nil)
self.commandCenter?.nextTrackCommand.removeTarget(nil)
self.commandCenter?.previousTrackCommand.removeTarget(nil)
self.commandCenter?.togglePlayPauseCommand.isEnabled = false
self.commandCenter?.playCommand.isEnabled = false
self.commandCenter?.pauseCommand.isEnabled = false
self.commandCenter?.nextTrackCommand.isEnabled = false
self.commandCenter?.previousTrackCommand.isEnabled = false
MPNowPlayingInfoCenter.default().nowPlayingInfo = nil
print("[MediaButtonModule] Stopped listening for media button events")
}
}
private func sendEventIfListening(_ eventName: String, body: [String: Any]?) {
guard hasListeners else { return }
sendEvent(withName: eventName, body: body)
}
}
`;
/**
* iOS MediaButtonModule.m (Objective-C bridge) content
*/
const IOS_OBJC_BRIDGE = `#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
@interface RCT_EXTERN_MODULE(MediaButtonModule, RCTEventEmitter)
RCT_EXTERN_METHOD(startListening)
RCT_EXTERN_METHOD(stopListening)
@end
`;
/**
* Android MediaButtonModule.kt content
*/
const ANDROID_MODULE = `package {{PACKAGE_NAME}}
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.media.session.MediaSession
import android.media.session.PlaybackState
import android.os.Build
import android.util.Log
import android.view.KeyEvent
import com.facebook.react.bridge.*
import com.facebook.react.modules.core.DeviceEventManagerModule
class MediaButtonModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext), LifecycleEventListener {
companion object {
private const val TAG = "MediaButtonModule"
}
private var mediaSession: MediaSession? = null
private var isListening = false
private var mediaButtonReceiver: BroadcastReceiver? = null
init {
reactContext.addLifecycleEventListener(this)
}
override fun getName(): String {
return "MediaButtonModule"
}
@ReactMethod
fun startListening() {
if (isListening) return
val context = reactApplicationContext ?: return
// Create media session for capturing media button events
mediaSession = MediaSession(context, "ResgridPTT").apply {
// Set the media button callback
setCallback(object : MediaSession.Callback() {
override fun onPlay() {
sendEvent("onMediaButtonEvent", createParams(KeyEvent.KEYCODE_MEDIA_PLAY, "ACTION_DOWN"))
}
override fun onPause() {
sendEvent("onMediaButtonEvent", createParams(KeyEvent.KEYCODE_MEDIA_PAUSE, "ACTION_DOWN"))
}
override fun onStop() {
sendEvent("onMediaButtonEvent", createParams(KeyEvent.KEYCODE_MEDIA_STOP, "ACTION_DOWN"))
}
override fun onSkipToNext() {
sendEvent("onMediaButtonEvent", createParams(KeyEvent.KEYCODE_MEDIA_NEXT, "ACTION_DOWN"))
}
override fun onSkipToPrevious() {
sendEvent("onMediaButtonEvent", createParams(KeyEvent.KEYCODE_MEDIA_PREVIOUS, "ACTION_DOWN"))
}
override fun onMediaButtonEvent(mediaButtonEvent: Intent): Boolean {
val keyEvent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT, KeyEvent::class.java)
} else {
@Suppress("DEPRECATION")
mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT)
}
keyEvent?.let { event ->
val action = when (event.action) {
KeyEvent.ACTION_DOWN -> "ACTION_DOWN"
KeyEvent.ACTION_UP -> "ACTION_UP"
else -> "UNKNOWN"
}
// Handle play/pause toggle and headset hook (primary PTT triggers)
when (event.keyCode) {
KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE,
KeyEvent.KEYCODE_HEADSETHOOK,
KeyEvent.KEYCODE_MEDIA_PLAY,
KeyEvent.KEYCODE_MEDIA_PAUSE -> {
sendEvent("onMediaButtonEvent", createParams(event.keyCode, action))
return true
}
}
}
return super.onMediaButtonEvent(mediaButtonEvent)
}
})
// Set playback state to enable media button handling
val playbackState = PlaybackState.Builder()
.setActions(
PlaybackState.ACTION_PLAY or
PlaybackState.ACTION_PAUSE or
PlaybackState.ACTION_PLAY_PAUSE or
PlaybackState.ACTION_STOP or
PlaybackState.ACTION_SKIP_TO_NEXT or
PlaybackState.ACTION_SKIP_TO_PREVIOUS
)
.setState(PlaybackState.STATE_PLAYING, 0, 1.0f)
.build()
setPlaybackState(playbackState)
// Activate the session
isActive = true
}
// Register a broadcast receiver for media button events (fallback)
mediaButtonReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (Intent.ACTION_MEDIA_BUTTON == intent?.action) {
val keyEvent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT, KeyEvent::class.java)
} else {
@Suppress("DEPRECATION")
intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT)
}
keyEvent?.let { event ->
val action = when (event.action) {
KeyEvent.ACTION_DOWN -> "ACTION_DOWN"
KeyEvent.ACTION_UP -> "ACTION_UP"
else -> "UNKNOWN"
}
sendEvent("onMediaButtonEvent", createParams(event.keyCode, action))
}
}
}
}
val filter = IntentFilter(Intent.ACTION_MEDIA_BUTTON)
filter.priority = IntentFilter.SYSTEM_HIGH_PRIORITY
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.registerReceiver(mediaButtonReceiver, filter, Context.RECEIVER_EXPORTED)
} else {
context.registerReceiver(mediaButtonReceiver, filter)
}
isListening = true
}
@ReactMethod
fun stopListening() {
if (!isListening) return
mediaSession?.apply {
isActive = false
release()
}
mediaSession = null
mediaButtonReceiver?.let {
try {
reactApplicationContext.unregisterReceiver(it)
} catch (e: Exception) {
Log.d(TAG, "Failed to unregister media button receiver: \${e.message}")
}
}
mediaButtonReceiver = null
isListening = false
}
private fun createParams(keyCode: Int, action: String): WritableMap {
return Arguments.createMap().apply {
putInt("keyCode", keyCode)
putString("action", action)
putDouble("timestamp", System.currentTimeMillis().toDouble())
}
}
private fun sendEvent(eventName: String, params: WritableMap?) {
reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit(eventName, params)
}
override fun onHostResume() {
// App is in foreground - ensure media session is active
mediaSession?.isActive = true
}
override fun onHostPause() {
// App is in background - keep media session active for background audio
}
override fun onHostDestroy() {
stopListening()
}
@ReactMethod
fun addListener(eventName: String) {
// Required for RN built-in Event Emitter Support
}
@ReactMethod
fun removeListeners(count: Int) {
// Required for RN built-in Event Emitter Support
}
}
`;
/**
* Android MediaButtonPackage.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 MediaButtonPackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return listOf(MediaButtonModule(reactContext))
}
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<View, ReactShadowNode<*>>> {
return emptyList()
}
}
`;
/**
* Resolves the Android namespace/package name from build.gradle or build.gradle.kts.
* Probes both Groovy and Kotlin DSL files and uses a regex that handles both syntaxes:
* - Groovy: namespace 'com.example.app' or namespace "com.example.app"
* - Kotlin DSL: namespace = "com.example.app"
*
* @param {string} projectRoot - The project root directory
* @param {string} fallback - Fallback package name if not found (default: 'com.resgrid.unit')
* @returns {string} The resolved namespace or fallback
*/
function resolveBasePackageName(projectRoot, fallback = 'com.resgrid.unit') {
// Regex that accepts optional equals sign for both Groovy and Kotlin DSL
const namespaceRegex = /namespace\s*(?:=)?\s*['"]([^'"]+)['"]/;
// Probe build.gradle (Groovy DSL) first
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];
}
}
// Probe build.gradle.kts (Kotlin DSL) as fallback
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;
}
/**
* Expo config plugin to add MediaButtonModule for AirPods/earbuds PTT support.
*
* This plugin:
* 1. Creates the MediaButtonModule.swift and MediaButtonModule.m files in the iOS project
* 2. Updates the bridging header to include necessary imports
* 3. Creates MediaButtonModule.kt and MediaButtonPackage.kt for Android
* 4. Registers the package in MainApplication.kt
*/
const withMediaButtonModule = (config) => {
// Add iOS native module files
config = withDangerousMod(config, [
'ios',
async (config) => {
const projectRoot = config.modRequest.projectRoot;
const projectName = config.modRequest.projectName;
const iosProjectPath = path.join(projectRoot, 'ios', projectName);
// Ensure the directory exists
if (!fs.existsSync(iosProjectPath)) {
fs.mkdirSync(iosProjectPath, { recursive: true });
}
// Write MediaButtonModule.swift
const swiftPath = path.join(iosProjectPath, 'MediaButtonModule.swift');
fs.writeFileSync(swiftPath, IOS_SWIFT_MODULE);
console.log('[withMediaButtonModule] Created MediaButtonModule.swift');
// Write MediaButtonModule.m (Objective-C bridge)
const objcPath = path.join(iosProjectPath, 'MediaButtonModule.m');
fs.writeFileSync(objcPath, IOS_OBJC_BRIDGE);
console.log('[withMediaButtonModule] Created MediaButtonModule.m');
// Update bridging header
const bridgingHeaderPath = path.join(iosProjectPath, `${projectName}-Bridging-Header.h`);
if (fs.existsSync(bridgingHeaderPath)) {
let bridgingHeaderContents = fs.readFileSync(bridgingHeaderPath, 'utf-8');
const requiredImports = ['#import <React/RCTBridgeModule.h>', '#import <React/RCTEventEmitter.h>'];
let modified = false;
for (const importLine of requiredImports) {
if (!bridgingHeaderContents.includes(importLine)) {
bridgingHeaderContents += `\n${importLine}`;
modified = true;
}
}
if (modified) {
fs.writeFileSync(bridgingHeaderPath, bridgingHeaderContents);
console.log('[withMediaButtonModule] Updated bridging header with React Native imports');
}
}
return config;
},
]);
// Add Android native module files
config = withDangerousMod(config, [
'android',
async (config) => {
const projectRoot = config.modRequest.projectRoot;
// Resolve the package name from build.gradle or build.gradle.kts
const packageName = resolveBasePackageName(projectRoot);
const packagePath = packageName.replace(/\./g, '/');
const androidSrcPath = path.join(projectRoot, 'android', 'app', 'src', 'main', 'java', packagePath);
// Ensure the directory exists
if (!fs.existsSync(androidSrcPath)) {
fs.mkdirSync(androidSrcPath, { recursive: true });
}
// Write MediaButtonModule.kt
const modulePath = path.join(androidSrcPath, 'MediaButtonModule.kt');
const moduleContent = ANDROID_MODULE.replace(/\{\{PACKAGE_NAME\}\}/g, packageName);
fs.writeFileSync(modulePath, moduleContent);
console.log('[withMediaButtonModule] Created MediaButtonModule.kt');
// Write MediaButtonPackage.kt
const packageFilePath = path.join(androidSrcPath, 'MediaButtonPackage.kt');
const packageContent = ANDROID_PACKAGE.replace(/\{\{PACKAGE_NAME\}\}/g, packageName);
fs.writeFileSync(packageFilePath, packageContent);
console.log('[withMediaButtonModule] Created MediaButtonPackage.kt');
return config;
},
]);
// Update MainApplication.kt to register the package
config = withMainApplication(config, (config) => {
const mainApplication = config.modResults;
const projectRoot = config.modRequest.projectRoot;
// Check if MediaButtonPackage is already imported/added
if (!mainApplication.contents.includes('MediaButtonPackage')) {
// Resolve the BASE package name from build.gradle or build.gradle.kts
// This is where the native module files are actually created
const basePackageName = resolveBasePackageName(projectRoot);
// Add import statement using the BASE package name (not the variant-specific package)
// The native module files are created in the base package, not in variant packages like 'development'
const importStatement = `import ${basePackageName}.MediaButtonPackage`;
if (!mainApplication.contents.includes(importStatement)) {
// Add import after the package declaration line
mainApplication.contents = mainApplication.contents.replace(/^(package\s+[^\n]+\n)/, `$1${importStatement}\n`);
console.log(`[withMediaButtonModule] Added MediaButtonPackage import from base package: ${basePackageName}`);
}
// Add the package to getPackages()
// Find the packages list and add our package
const packagesPattern = /val packages = PackageList\(this\)\.packages(\.toMutableList\(\))?/;
const packagesMatch = mainApplication.contents.match(packagesPattern);
if (packagesMatch) {
// Replace the packages declaration, ensuring toMutableList() is present so we can add our package
mainApplication.contents = mainApplication.contents.replace(packagesPattern, `val packages = PackageList(this).packages.toMutableList()\n packages.add(MediaButtonPackage())`);
console.log('[withMediaButtonModule] Registered MediaButtonPackage in MainApplication.kt');
}
}
return config;
});
return config;
};
module.exports = withMediaButtonModule;