Use isSpeakerphoneOn function when API level is <= 33

This commit is contained in:
Juha Heinanen
2023-03-13 00:36:17 +02:00
parent 0ff7dd7e3c
commit 12df05a3f8
2 changed files with 12 additions and 10 deletions
@@ -199,7 +199,7 @@ 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 (Utils.isSpeakerPhoneOn(am))
speakerIcon!!.setIcon(R.drawable.speaker_on) speakerIcon!!.setIcon(R.drawable.speaker_on)
else else
speakerIcon!!.setIcon(R.drawable.speaker_off) speakerIcon!!.setIcon(R.drawable.speaker_off)
@@ -1326,11 +1326,13 @@ class MainActivity : AppCompatActivity() {
R.id.speakerIcon -> { R.id.speakerIcon -> {
if (Build.VERSION.SDK_INT >= 31) if (Build.VERSION.SDK_INT >= 31)
Log.d(TAG, "Toggling speakerphone when dev/mode is " + Log.d(
"${am.communicationDevice!!.type}/${am.mode}") 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 (speakerIcon != null) {
if (am.isSpeakerphoneOn) if (Utils.isSpeakerPhoneOn(am))
speakerIcon!!.setIcon(R.drawable.speaker_on) speakerIcon!!.setIcon(R.drawable.speaker_on)
else else
speakerIcon!!.setIcon(R.drawable.speaker_off) speakerIcon!!.setIcon(R.drawable.speaker_off)
@@ -892,14 +892,14 @@ 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 > 33)
am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER
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 (Build.VERSION.SDK_INT >= 31) { if (Build.VERSION.SDK_INT > 33) {
val current = am.communicationDevice!!.type val current = am.communicationDevice!!.type
Log.d(TAG, "Current com dev/mode is $current/${am.mode}") Log.d(TAG, "Current com dev/mode is $current/${am.mode}")
var speakerDevice: AudioDeviceInfo? = null var speakerDevice: AudioDeviceInfo? = null
@@ -964,7 +964,7 @@ object Utils {
} }
fun toggleSpeakerPhone(executor: Executor, am: AudioManager) { fun toggleSpeakerPhone(executor: Executor, am: AudioManager) {
if (Build.VERSION.SDK_INT >= 31) { if (Build.VERSION.SDK_INT > 33) {
if (am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) if (am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE)
setSpeakerPhone(executor, am, true) setSpeakerPhone(executor, am, true)
else if (am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) else if (am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER)
@@ -987,7 +987,7 @@ object Utils {
} }
fun clearCommunicationDevice(am: AudioManager) { fun clearCommunicationDevice(am: AudioManager) {
if (Build.VERSION.SDK_INT >= 31) { if (Build.VERSION.SDK_INT > 33) {
am.clearCommunicationDevice() am.clearCommunicationDevice()
} else { } else {
if (am.isSpeakerphoneOn) if (am.isSpeakerphoneOn)