From 37632dd031bc24467f98564097eaf2543b911103 Mon Sep 17 00:00:00 2001 From: tzugen Date: Thu, 22 Apr 2021 09:18:15 +0200 Subject: [PATCH] Convert AudioFocusHandler to Kotlin --- .../ultrasonic/service/AudioFocusHandler.kt | 128 +++++++----------- 1 file changed, 52 insertions(+), 76 deletions(-) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/AudioFocusHandler.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/AudioFocusHandler.kt index b625067b..c3641729 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/AudioFocusHandler.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/AudioFocusHandler.kt @@ -1,88 +1,64 @@ -package org.moire.ultrasonic.service; +package org.moire.ultrasonic.service -import android.content.Context; -import android.content.SharedPreferences; -import android.media.AudioManager; -import timber.log.Timber; - -import org.moire.ultrasonic.domain.PlayerState; -import org.moire.ultrasonic.util.Constants; -import org.moire.ultrasonic.util.Util; - -import kotlin.Lazy; - -import static org.koin.java.KoinJavaComponent.inject; - -public class AudioFocusHandler -{ - private static boolean hasFocus; - private static boolean pauseFocus; - private static boolean lowerFocus; +import android.content.Context +import android.media.AudioManager +import android.media.AudioManager.OnAudioFocusChangeListener +import org.koin.java.KoinJavaComponent.inject +import org.moire.ultrasonic.domain.PlayerState +import org.moire.ultrasonic.util.Constants +import org.moire.ultrasonic.util.Util +import timber.log.Timber +class AudioFocusHandler(private val context: Context) { // TODO: This is a circular reference, try to remove it - private Lazy mediaPlayerControllerLazy = inject(MediaPlayerController.class); - private Context context; + private val mediaPlayerControllerLazy = inject(MediaPlayerController::class.java) - public AudioFocusHandler(Context context) - { - this.context = context; - } + fun requestAudioFocus() { + if (!hasFocus) { + val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager + hasFocus = true - public void requestAudioFocus() - { - if (!hasFocus) - { - final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); - hasFocus = true; - audioManager.requestAudioFocus(new AudioManager.OnAudioFocusChangeListener() - { - @Override - public void onAudioFocusChange(int focusChange) - { - MediaPlayerController mediaPlayerController = mediaPlayerControllerLazy.getValue(); - if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !mediaPlayerController.isJukeboxEnabled()) - { - Timber.v("Lost Audio Focus"); - if (mediaPlayerController.getPlayerState() == PlayerState.STARTED) - { - SharedPreferences preferences = Util.getPreferences(context); - int lossPref = Integer.parseInt(preferences.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1")); - if (lossPref == 2 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)) - { - lowerFocus = true; - mediaPlayerController.setVolume(0.1f); - } - else if (lossPref == 0 || (lossPref == 1)) - { - pauseFocus = true; - mediaPlayerController.pause(); + + audioManager.requestAudioFocus(object : OnAudioFocusChangeListener { + override fun onAudioFocusChange(focusChange: Int) { + val mediaPlayerController = mediaPlayerControllerLazy.value + if ((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !mediaPlayerController.isJukeboxEnabled) { + Timber.v("Lost Audio Focus") + if (mediaPlayerController.playerState === PlayerState.STARTED) { + val preferences = Util.getPreferences(context) + val lossPref = preferences.getString(Constants.PREFERENCES_KEY_TEMP_LOSS, "1")!!.toInt() + if (lossPref == 2 || lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) { + lowerFocus = true + mediaPlayerController.setVolume(0.1f) + } else if (lossPref == 0 || lossPref == 1) { + pauseFocus = true + mediaPlayerController.pause() } } - } - else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) - { - Timber.v("Regained Audio Focus"); - if (pauseFocus) - { - pauseFocus = false; - mediaPlayerController.start(); + } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { + Timber.v("Regained Audio Focus") + if (pauseFocus) { + pauseFocus = false + mediaPlayerController.start() + } else if (lowerFocus) { + lowerFocus = false + mediaPlayerController.setVolume(1.0f) } - else if (lowerFocus) - { - lowerFocus = false; - mediaPlayerController.setVolume(1.0f); - } - } - else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !mediaPlayerController.isJukeboxEnabled()) - { - hasFocus = false; - mediaPlayerController.pause(); - audioManager.abandonAudioFocus(this); - Timber.v("Abandoned Audio Focus"); + } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS && !mediaPlayerController.isJukeboxEnabled) { + hasFocus = false + mediaPlayerController.pause() + audioManager.abandonAudioFocus(this) + Timber.v("Abandoned Audio Focus") } } - }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); - Timber.v("Got Audio Focus"); + }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN) + Timber.v("Got Audio Focus") } } -} + + companion object { + private var hasFocus = false + private var pauseFocus = false + private var lowerFocus = false + } +} \ No newline at end of file