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

View File

@ -199,7 +199,7 @@ class MainActivity : AppCompatActivity() {
if (device != null) {
Log.d(TAG, "Com device changed to type ${device.type} in mode ${am.mode}")
if (speakerIcon != null) {
if (device.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER)
if (Utils.isSpeakerPhoneOn(am))
speakerIcon!!.setIcon(R.drawable.speaker_on)
else
speakerIcon!!.setIcon(R.drawable.speaker_off)
@ -1326,11 +1326,13 @@ class MainActivity : AppCompatActivity() {
R.id.speakerIcon -> {
if (Build.VERSION.SDK_INT >= 31)
Log.d(TAG, "Toggling speakerphone when dev/mode is " +
"${am.communicationDevice!!.type}/${am.mode}")
Log.d(
TAG, "Toggling speakerphone when dev/mode is " +
"${am.communicationDevice!!.type}/${am.mode}"
)
Utils.toggleSpeakerPhone(ContextCompat.getMainExecutor(this), am)
if (Build.VERSION.SDK_INT < 31 && speakerIcon != null) {
if (am.isSpeakerphoneOn)
if (speakerIcon != null) {
if (Utils.isSpeakerPhoneOn(am))
speakerIcon!!.setIcon(R.drawable.speaker_on)
else
speakerIcon!!.setIcon(R.drawable.speaker_off)

View File

@ -892,14 +892,14 @@ object Utils {
}
fun isSpeakerPhoneOn(am: AudioManager): Boolean {
return if (Build.VERSION.SDK_INT >= 31)
am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER
return if (Build.VERSION.SDK_INT > 33)
am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER
else
am.isSpeakerphoneOn
}
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
Log.d(TAG, "Current com dev/mode is $current/${am.mode}")
var speakerDevice: AudioDeviceInfo? = null
@ -964,7 +964,7 @@ object Utils {
}
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)
setSpeakerPhone(executor, am, true)
else if (am.communicationDevice!!.type == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER)
@ -987,7 +987,7 @@ object Utils {
}
fun clearCommunicationDevice(am: AudioManager) {
if (Build.VERSION.SDK_INT >= 31) {
if (Build.VERSION.SDK_INT > 33) {
am.clearCommunicationDevice()
} else {
if (am.isSpeakerphoneOn)