Convert AudioFocusHandler to Kotlin

This commit is contained in:
tzugen 2021-04-22 09:18:15 +02:00
parent 0550116b35
commit 37632dd031
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
1 changed files with 52 additions and 76 deletions

View File

@ -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<MediaPlayerController> 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
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 (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")
}
}
}, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN)
Timber.v("Got Audio Focus")
}
}
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();
}
}
}
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 (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");
}
companion object {
private var hasFocus = false
private var pauseFocus = false
private var lowerFocus = false
}
}