Skip to content

Commit

Permalink
Disable speakerphone when headphones are plugged
Browse files Browse the repository at this point in the history
  • Loading branch information
theAkito committed Nov 18, 2022
1 parent 25dd10d commit e67dfb3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
41 changes: 41 additions & 0 deletions atox/src/main/kotlin/ui/call/CallFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@
package ltd.evilcorp.atox.ui.call

import android.Manifest
import android.media.AudioDeviceInfo.TYPE_BLE_HEADSET
import android.media.AudioDeviceInfo.TYPE_BLE_SPEAKER
import android.media.AudioDeviceInfo.TYPE_BLUETOOTH_SCO
import android.media.AudioDeviceInfo.TYPE_WIRED_HEADPHONES
import android.media.AudioDeviceInfo.TYPE_WIRED_HEADSET
import android.media.AudioManager
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat.getSystemService
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
Expand All @@ -25,9 +34,23 @@ import ltd.evilcorp.atox.vmFactory
import ltd.evilcorp.domain.feature.CallState
import ltd.evilcorp.domain.tox.PublicKey


private const val PERMISSION = Manifest.permission.RECORD_AUDIO

class CallFragment : BaseFragment<FragmentCallBinding>(FragmentCallBinding::inflate) {

companion object {
private var hasCalled = false
@RequiresApi(Build.VERSION_CODES.S)
private val audioOutputDevices = arrayOf(
TYPE_WIRED_HEADPHONES,
TYPE_WIRED_HEADSET,
TYPE_BLE_HEADSET,
TYPE_BLE_SPEAKER,
TYPE_BLUETOOTH_SCO
)
}

private val vm: CallViewModel by viewModels { vmFactory }

private val requestPermissionLauncher = registerForActivityResult(
Expand All @@ -40,6 +63,7 @@ class CallFragment : BaseFragment<FragmentCallBinding>(FragmentCallBinding::infl
}
}

@RequiresApi(Build.VERSION_CODES.S)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) = binding.run {
ViewCompat.setOnApplyWindowInsetsListener(view) { _, compat ->
val insets = compat.getInsets(WindowInsetsCompat.Type.systemBars())
Expand Down Expand Up @@ -77,6 +101,7 @@ class CallFragment : BaseFragment<FragmentCallBinding>(FragmentCallBinding::infl
}
}

updateSpeakerphoneOnDetectHeadphones()
updateSpeakerphoneIcon()
speakerphone.setOnClickListener {
vm.toggleSpeakerphone()
Expand Down Expand Up @@ -108,12 +133,28 @@ class CallFragment : BaseFragment<FragmentCallBinding>(FragmentCallBinding::infl
binding.speakerphone.setImageResource(icon)
}


@RequiresApi(Build.VERSION_CODES.S)
private fun updateSpeakerphoneOnDetectHeadphones() {
vm.disableSpeakerphone()
}

private fun startCall() {
vm.startCall()
vm.inCall.asLiveData().observe(viewLifecycleOwner) { inCall ->
if (inCall == CallState.NotInCall) {
findNavController().popBackStack()
hasCalled = false
}
}
}

@RequiresApi(Build.VERSION_CODES.S)
private fun headphonesArePlugged(): Boolean {
val ctx = context ?: return false
val audioManager = getSystemService(ctx, AudioManager::class.java) ?: return false
return audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS).any { info ->
audioOutputDevices.contains(info.type)
}
}
}
13 changes: 11 additions & 2 deletions atox/src/main/kotlin/ui/call/CallViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ class CallViewModel @Inject constructor(
fun startSendingAudio() = callManager.startSendingAudio()
fun stopSendingAudio() = callManager.stopSendingAudio()

fun toggleSpeakerphone() {
speakerphoneOn = !speakerphoneOn
fun applyProximityScreenSetting() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (speakerphoneOn) {
proximityScreenOff.release()
Expand All @@ -60,6 +59,16 @@ class CallViewModel @Inject constructor(
}
}

fun disableSpeakerphone() {
speakerphoneOn = false
applyProximityScreenSetting()
}

fun toggleSpeakerphone() {
speakerphoneOn = !speakerphoneOn
applyProximityScreenSetting()
}

val inCall = callManager.inCall
val sendingAudio = callManager.sendingAudio

Expand Down

0 comments on commit e67dfb3

Please sign in to comment.