Skip to content

Commit 4ab2e60

Browse files
committed
move proximity manager
1 parent 40a4880 commit 4ab2e60

File tree

3 files changed

+13
-33
lines changed

3 files changed

+13
-33
lines changed

packages/react-native-sdk/android/src/main/java/com/streamvideo/reactnative/audio/AudioDeviceManager.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import com.streamvideo.reactnative.audio.utils.AudioManagerUtil
3333
import com.streamvideo.reactnative.audio.utils.AudioManagerUtil.Companion.getAvailableAudioDevices
3434
import com.streamvideo.reactnative.audio.utils.AudioSetupStoreUtil
3535
import com.streamvideo.reactnative.audio.utils.CallAudioRole
36+
import com.streamvideo.reactnative.callmanager.ProximityManager
3637
import com.streamvideo.reactnative.callmanager.StreamInCallManagerModule
3738
import com.streamvideo.reactnative.model.AudioDeviceEndpoint
3839
import com.streamvideo.reactnative.model.AudioDeviceEndpoint.Companion.EndpointType
@@ -70,12 +71,15 @@ class AudioDeviceManager(
7071

7172
/** Returns the currently selected audio device. */
7273
private var _selectedAudioDeviceEndpoint: AudioDeviceEndpoint? = null
73-
private var selectedAudioDeviceEndpoint: AudioDeviceEndpoint?
74+
var selectedAudioDeviceEndpoint: AudioDeviceEndpoint?
7475
get() = _selectedAudioDeviceEndpoint
7576
set(value) {
7677
_selectedAudioDeviceEndpoint = value
7778
// send an event to the frontend everytime this endpoint changes
7879
sendAudioStatusEvent()
80+
if (callAudioRole == CallAudioRole.Communicator) {
81+
proximityManager.update()
82+
}
7983
}
8084

8185
// Default audio device; speaker phone for video calls or earpiece for audio only phone calls
@@ -101,6 +105,8 @@ class AudioDeviceManager(
101105

102106
val bluetoothManager = BluetoothManager(mReactContext, this)
103107

108+
private val proximityManager by lazy { ProximityManager(mReactContext, this) }
109+
104110
init {
105111
// Note that we will immediately receive a call to onDevicesAdded with the list of
106112
// devices which are currently connected.
@@ -120,6 +126,7 @@ class AudioDeviceManager(
120126
bluetoothManager.start()
121127
mAudioManager.registerAudioDeviceCallback(this, null)
122128
updateAudioDeviceState()
129+
proximityManager.start()
123130
} else {
124131
// Audio routing is handled automatically by the system in normal media mode
125132
// and bluetooth microphones may not work on some devices.
@@ -141,6 +148,7 @@ class AudioDeviceManager(
141148
mAudioManager.setSpeakerphoneOn(false)
142149
}
143150
bluetoothManager.stop()
151+
proximityManager.stop()
144152
}
145153
audioSetupStoreUtil.restoreOriginalAudioSetup()
146154
audioFocusUtil.abandonFocus()
@@ -221,6 +229,7 @@ class AudioDeviceManager(
221229

222230
override fun close() {
223231
mAudioManager.unregisterAudioDeviceCallback(this)
232+
proximityManager.onDestroy()
224233
}
225234

226235
override fun onAudioDevicesAdded(addedDevices: Array<out AudioDeviceInfo>?) {

packages/react-native-sdk/android/src/main/java/com/streamvideo/reactnative/callmanager/ProximityManager.kt

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.media.AudioDeviceInfo
88
import android.media.AudioManager
99
import android.os.PowerManager
1010
import android.util.Log
11+
import com.streamvideo.reactnative.audio.AudioDeviceManager
1112

1213
/**
1314
* Encapsulates Android proximity sensor handling for in-call UX.
@@ -20,6 +21,7 @@ import android.util.Log
2021
*/
2122
class ProximityManager(
2223
private val context: Context,
24+
private val audioDeviceManager: AudioDeviceManager,
2325
) {
2426

2527
companion object {
@@ -153,28 +155,6 @@ class ProximityManager(
153155
}
154156

155157
private fun isOnEarpiece(): Boolean {
156-
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
157-
// If speakerphone is on, not earpiece
158-
if (audioManager.isSpeakerphoneOn) return false
159-
160-
// Check if Bluetooth SCO/A2DP or wired headset is connected
161-
var hasBt = false
162-
var hasWired = false
163-
val outputs = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS)
164-
outputs.forEach { dev ->
165-
val type = dev.type
166-
if (type == AudioDeviceInfo.TYPE_BLUETOOTH_A2DP ||
167-
type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO
168-
) {
169-
hasBt = true
170-
} else if (type == AudioDeviceInfo.TYPE_WIRED_HEADPHONES
171-
|| type == AudioDeviceInfo.TYPE_WIRED_HEADSET
172-
|| type == AudioDeviceInfo.TYPE_USB_HEADSET
173-
) {
174-
hasWired = true
175-
}
176-
}
177-
178-
return !hasBt && !hasWired
158+
return audioDeviceManager.selectedAudioDeviceEndpoint?.isEarpieceType() ?: false
179159
}
180160
}

packages/react-native-sdk/android/src/main/java/com/streamvideo/reactnative/callmanager/StreamInCallManagerModule.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class StreamInCallManagerModule(reactContext: ReactApplicationContext) :
2020
private var audioManagerActivated = false
2121

2222
private val mAudioDeviceManager = AudioDeviceManager(reactContext)
23-
private val proximityManager = ProximityManager(reactContext)
2423

2524
override fun getName(): String {
2625
return TAG
@@ -89,8 +88,6 @@ class StreamInCallManagerModule(reactContext: ReactApplicationContext) :
8988
mAudioDeviceManager.start(it)
9089
setKeepScreenOn(true)
9190
audioManagerActivated = true
92-
// Initialize and evaluate proximity monitoring via controller
93-
proximityManager.start()
9491
}
9592
}
9693
}
@@ -103,8 +100,6 @@ class StreamInCallManagerModule(reactContext: ReactApplicationContext) :
103100
Log.d(TAG, "stop() mAudioDeviceManager")
104101
mAudioDeviceManager.stop()
105102
setMicrophoneMute(false)
106-
// Disable proximity monitoring via controller and clear keep-screen-on
107-
proximityManager.stop()
108103
setKeepScreenOn(false)
109104
audioManagerActivated = false
110105
}
@@ -133,8 +128,6 @@ class StreamInCallManagerModule(reactContext: ReactApplicationContext) :
133128
return
134129
}
135130
mAudioDeviceManager.setSpeakerphoneOn(enable)
136-
// Re-evaluate proximity monitoring when route may change
137-
this.proximityManager.update()
138131
}
139132

140133
@ReactMethod
@@ -160,8 +153,6 @@ class StreamInCallManagerModule(reactContext: ReactApplicationContext) :
160153
mAudioDeviceManager.switchDeviceFromDeviceName(
161154
endpointDeviceName
162155
)
163-
// Re-evaluate proximity monitoring when endpoint changes
164-
this.proximityManager.update()
165156
}
166157

167158
@ReactMethod

0 commit comments

Comments
 (0)