Mode communication device related enhancements
This commit is contained in:
@@ -593,8 +593,8 @@ class BaresipService: Service() {
|
|||||||
|
|
||||||
var newEvent: String? = null
|
var newEvent: String? = null
|
||||||
|
|
||||||
for (account_index in uas.indices) {
|
for (accountIndex in uas.indices) {
|
||||||
if (uas[account_index].account.aor == aor) {
|
if (uas[accountIndex].account.aor == aor) {
|
||||||
when (ev[0]) {
|
when (ev[0]) {
|
||||||
"registering", "unregistering" -> {
|
"registering", "unregistering" -> {
|
||||||
ua.status = R.drawable.dot_yellow
|
ua.status = R.drawable.dot_yellow
|
||||||
@@ -1201,6 +1201,7 @@ class BaresipService: Service() {
|
|||||||
.build()
|
.build()
|
||||||
if (AudioManagerCompat.requestAudioFocus(am, audioFocusRequest!!) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
|
if (AudioManagerCompat.requestAudioFocus(am, audioFocusRequest!!) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
|
||||||
Log.d(TAG, "Audio focus granted")
|
Log.d(TAG, "Audio focus granted")
|
||||||
|
isAudioFocused = true
|
||||||
if (isBluetoothHeadsetConnected() && !am.isBluetoothScoOn) {
|
if (isBluetoothHeadsetConnected() && !am.isBluetoothScoOn) {
|
||||||
Log.d(TAG, "Starting Bluetooth SCO")
|
Log.d(TAG, "Starting Bluetooth SCO")
|
||||||
am.startBluetoothSco()
|
am.startBluetoothSco()
|
||||||
@@ -1208,6 +1209,7 @@ class BaresipService: Service() {
|
|||||||
} else {
|
} else {
|
||||||
Log.d(TAG, "Audio focus denied")
|
Log.d(TAG, "Audio focus denied")
|
||||||
audioFocusRequest = null
|
audioFocusRequest = null
|
||||||
|
isAudioFocused = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1215,6 +1217,7 @@ class BaresipService: Service() {
|
|||||||
if (audioFocusRequest != null) {
|
if (audioFocusRequest != null) {
|
||||||
AudioManagerCompat.abandonAudioFocusRequest(am, audioFocusRequest!!)
|
AudioManagerCompat.abandonAudioFocusRequest(am, audioFocusRequest!!)
|
||||||
audioFocusRequest = null
|
audioFocusRequest = null
|
||||||
|
isAudioFocused = false
|
||||||
Log.d(TAG, "Audio focus abandoned")
|
Log.d(TAG, "Audio focus abandoned")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1509,6 +1512,7 @@ class BaresipService: Service() {
|
|||||||
|
|
||||||
var isServiceRunning = false
|
var isServiceRunning = false
|
||||||
var isConfigInitialized = false
|
var isConfigInitialized = false
|
||||||
|
var isAudioFocused = false
|
||||||
var libraryLoaded = false
|
var libraryLoaded = false
|
||||||
var callVolume = 0
|
var callVolume = 0
|
||||||
var dynDns = false
|
var dynDns = false
|
||||||
@@ -1537,6 +1541,7 @@ class BaresipService: Service() {
|
|||||||
val serviceEvent = MutableLiveData<Event<Long>>()
|
val serviceEvent = MutableLiveData<Event<Long>>()
|
||||||
val serviceEvents = mutableListOf<ServiceEvent>()
|
val serviceEvents = mutableListOf<ServiceEvent>()
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
|||||||
@@ -196,10 +196,10 @@ class MainActivity : AppCompatActivity() {
|
|||||||
if (device != null) {
|
if (device != null) {
|
||||||
Log.d(TAG, "Com device changed to type ${device.type} in mode ${am.mode}")
|
Log.d(TAG, "Com device changed to type ${device.type} in mode ${am.mode}")
|
||||||
if (speakerIcon != null) {
|
if (speakerIcon != null) {
|
||||||
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER)
|
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE)
|
||||||
speakerIcon!!.setIcon(R.drawable.speaker_on)
|
|
||||||
else
|
|
||||||
speakerIcon!!.setIcon(R.drawable.speaker_off)
|
speakerIcon!!.setIcon(R.drawable.speaker_off)
|
||||||
|
else
|
||||||
|
speakerIcon!!.setIcon(R.drawable.speaker_on)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1258,6 +1258,9 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
R.id.speakerIcon -> {
|
R.id.speakerIcon -> {
|
||||||
|
if (Build.VERSION.SDK_INT >= 31)
|
||||||
|
Log.d(TAG, "Toggling speakerphone when dev/mode is " +
|
||||||
|
"${am.communicationDevice!!.type}/${am.mode}")
|
||||||
Utils.toggleSpeakerPhone(ContextCompat.getMainExecutor(this), am)
|
Utils.toggleSpeakerPhone(ContextCompat.getMainExecutor(this), am)
|
||||||
if (Build.VERSION.SDK_INT < 31 && speakerIcon != null) {
|
if (Build.VERSION.SDK_INT < 31 && speakerIcon != null) {
|
||||||
if (am.isSpeakerphoneOn)
|
if (am.isSpeakerphoneOn)
|
||||||
@@ -1840,7 +1843,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
hangupButton.isEnabled = false
|
hangupButton.isEnabled = false
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Log.d(TAG, "Audio mode changed to MODE_NORMAL using " +
|
Log.d(TAG, "Audio mode changed to mode ${am.mode} using " +
|
||||||
"device ${am.communicationDevice!!.type}")
|
"device ${am.communicationDevice!!.type}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -589,7 +589,6 @@ object Utils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
fun downloadsPath(fileName: String): String {
|
fun downloadsPath(fileName: String): String {
|
||||||
return Environment.getExternalStoragePublicDirectory(
|
return Environment.getExternalStoragePublicDirectory(
|
||||||
Environment.DIRECTORY_DOWNLOADS).path + "/$fileName"
|
Environment.DIRECTORY_DOWNLOADS).path + "/$fileName"
|
||||||
@@ -893,64 +892,75 @@ object Utils {
|
|||||||
|
|
||||||
fun isSpeakerPhoneOn(am: AudioManager): Boolean {
|
fun isSpeakerPhoneOn(am: AudioManager): Boolean {
|
||||||
return if (Build.VERSION.SDK_INT >= 31)
|
return if (Build.VERSION.SDK_INT >= 31)
|
||||||
am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER
|
am.communicationDevice!!.type != AudioDeviceInfo.TYPE_BUILTIN_EARPIECE
|
||||||
else
|
else
|
||||||
am.isSpeakerphoneOn
|
am.isSpeakerphoneOn
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setSpeakerPhone(executor: Executor, am: AudioManager, enable: Boolean) {
|
private fun setSpeakerPhone(executor: Executor, am: AudioManager, enable: Boolean) {
|
||||||
if (enable == isSpeakerPhoneOn(am))
|
|
||||||
return
|
|
||||||
if (enable) {
|
|
||||||
if (Build.VERSION.SDK_INT >= 31) {
|
if (Build.VERSION.SDK_INT >= 31) {
|
||||||
Log.d(TAG, "Setting current device from ${am.communicationDevice!!.type} to " +
|
val current = am.communicationDevice!!.type
|
||||||
"${AudioDeviceInfo.TYPE_BUILTIN_SPEAKER} in mode ${am.mode}")
|
Log.d(TAG, "Current com dev/mode is $current/${am.mode}")
|
||||||
var speakerDevice: AudioDeviceInfo? = null
|
var speakerDevice: AudioDeviceInfo? = null
|
||||||
|
if (enable) {
|
||||||
|
for (device in am.availableCommunicationDevices)
|
||||||
|
if (device.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {
|
||||||
|
speakerDevice = device
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if (speakerDevice == null)
|
||||||
for (device in am.availableCommunicationDevices)
|
for (device in am.availableCommunicationDevices)
|
||||||
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
|
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
|
||||||
speakerDevice = device
|
speakerDevice = device
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if (speakerDevice != null) {
|
|
||||||
if (am.mode == AudioManager.MODE_NORMAL) {
|
|
||||||
if (!am.setCommunicationDevice(speakerDevice))
|
|
||||||
Log.e(TAG, "Could not turn on speaker device")
|
|
||||||
Log.d(TAG, "Type of current device is ${am.communicationDevice!!.type}")
|
|
||||||
} else {
|
} else {
|
||||||
// Currently at API levels 31+, speakerphone needs to be turned on in normal mode
|
for (device in am.availableCommunicationDevices)
|
||||||
|
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) {
|
||||||
|
speakerDevice = device
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (speakerDevice != null && current != speakerDevice.type) {
|
||||||
|
if (speakerDevice.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) {
|
||||||
|
am.clearCommunicationDevice()
|
||||||
|
Log.d(TAG, "Setting com device to TYPE_BUILTIN_EARPIECE")
|
||||||
|
if (!am.setCommunicationDevice(speakerDevice))
|
||||||
|
Log.e(TAG, "Could not set com device")
|
||||||
|
if (BaresipService.isAudioFocused && am.mode == AudioManager.MODE_NORMAL) {
|
||||||
|
Log.d(TAG, "Setting mode to communication")
|
||||||
|
am.mode = AudioManager.MODE_IN_COMMUNICATION
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Currently at API levels 31+, speakerphone needs normal mode
|
||||||
|
if (am.mode == AudioManager.MODE_NORMAL) {
|
||||||
|
Log.d(TAG, "Setting com device to ${speakerDevice.type} in MODE_NORMAL")
|
||||||
|
if (!am.setCommunicationDevice(speakerDevice))
|
||||||
|
Log.e(TAG, "Could not set com device")
|
||||||
|
} else {
|
||||||
val normalListener = object : AudioManager.OnModeChangedListener {
|
val normalListener = object : AudioManager.OnModeChangedListener {
|
||||||
override fun onModeChanged(mode: Int) {
|
override fun onModeChanged(mode: Int) {
|
||||||
if (mode == AudioManager.MODE_NORMAL) {
|
if (mode == AudioManager.MODE_NORMAL) {
|
||||||
am.removeOnModeChangedListener(this)
|
am.removeOnModeChangedListener(this)
|
||||||
|
Log.d(TAG, "Setting com device to ${speakerDevice.type}" +
|
||||||
|
" in mode ${am.mode}")
|
||||||
if (!am.setCommunicationDevice(speakerDevice))
|
if (!am.setCommunicationDevice(speakerDevice))
|
||||||
Log.e(TAG, "Could not turn on speaker device")
|
Log.e(TAG, "Could not set com device")
|
||||||
Log.d(TAG, "Type of current device is ${am.communicationDevice!!.type}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
am.addOnModeChangedListener(executor, normalListener)
|
am.addOnModeChangedListener(executor, normalListener)
|
||||||
|
Log.d(TAG, "Setting mode to NORMAL")
|
||||||
am.mode = AudioManager.MODE_NORMAL
|
am.mode = AudioManager.MODE_NORMAL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
Log.d(TAG, "New com device/mode is ${am.communicationDevice!!.type}/${am.mode}")
|
||||||
am.isSpeakerphoneOn = true
|
|
||||||
Log.d(TAG, "Speakerphone is ${am.isSpeakerphoneOn}")
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (Build.VERSION.SDK_INT >= 31) {
|
am.isSpeakerphoneOn = enable
|
||||||
Log.d(TAG, "Setting current device from type ${am.communicationDevice!!.type} to " +
|
|
||||||
"${AudioDeviceInfo.TYPE_BUILTIN_EARPIECE} in mode ${am.mode}")
|
|
||||||
am.clearCommunicationDevice()
|
|
||||||
// Restore communication mode
|
|
||||||
if (Call.call("connected") != null && am.mode == AudioManager.MODE_NORMAL)
|
|
||||||
am.mode = AudioManager.MODE_IN_COMMUNICATION
|
|
||||||
Log.d(TAG, "Type of current device is ${am.communicationDevice!!.type}")
|
|
||||||
} else {
|
|
||||||
am.isSpeakerphoneOn = false
|
|
||||||
Log.d(TAG, "Speakerphone is ${am.isSpeakerphoneOn}")
|
Log.d(TAG, "Speakerphone is ${am.isSpeakerphoneOn}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fun toggleSpeakerPhone(executor: Executor, am: AudioManager) {
|
fun toggleSpeakerPhone(executor: Executor, am: AudioManager) {
|
||||||
if (Build.VERSION.SDK_INT >= 31)
|
if (Build.VERSION.SDK_INT >= 31)
|
||||||
|
|||||||
Reference in New Issue
Block a user