diff --git a/android/src/main/java/com/brentvatne/receiver/AudioBecomingNoisyReceiver.java b/android/src/main/java/com/brentvatne/receiver/AudioBecomingNoisyReceiver.java deleted file mode 100644 index 1b0fb0de..00000000 --- a/android/src/main/java/com/brentvatne/receiver/AudioBecomingNoisyReceiver.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.brentvatne.receiver; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.media.AudioManager; - -import androidx.core.content.ContextCompat; - -public class AudioBecomingNoisyReceiver extends BroadcastReceiver { - - private final Context context; - private BecomingNoisyListener listener = BecomingNoisyListener.NO_OP; - - public AudioBecomingNoisyReceiver(Context context) { - this.context = context.getApplicationContext(); - } - - @Override - public void onReceive(Context context, Intent intent) { - if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) { - listener.onAudioBecomingNoisy(); - } - } - - public void setListener(BecomingNoisyListener listener) { - this.listener = listener; - IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY); - ContextCompat.registerReceiver(context, this, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED); - } - - public void removeListener() { - this.listener = BecomingNoisyListener.NO_OP; - try { - context.unregisterReceiver(this); - } catch (Exception ignore) { - // ignore if already unregistered - } - } -} diff --git a/android/src/main/java/com/brentvatne/receiver/AudioBecomingNoisyReceiver.kt b/android/src/main/java/com/brentvatne/receiver/AudioBecomingNoisyReceiver.kt new file mode 100644 index 00000000..8fb4670c --- /dev/null +++ b/android/src/main/java/com/brentvatne/receiver/AudioBecomingNoisyReceiver.kt @@ -0,0 +1,35 @@ +package com.brentvatne.receiver + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.media.AudioManager +import androidx.core.content.ContextCompat + +class AudioBecomingNoisyReceiver(context: Context) : BroadcastReceiver() { + private var listener: BecomingNoisyListener = BecomingNoisyListener.NO_OP + private val appContext: Context = context.applicationContext + override fun onReceive(context: Context?, intent: Intent?) { + if (intent != null) { + if (AudioManager.ACTION_AUDIO_BECOMING_NOISY == intent.action) { + listener.onAudioBecomingNoisy() + } + } + } + + fun setListener(listener: BecomingNoisyListener) { + this.listener = listener + val intentFilter = IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY) + ContextCompat.registerReceiver(appContext, this, intentFilter, ContextCompat.RECEIVER_NOT_EXPORTED) + } + + fun removeListener() { + this.listener = BecomingNoisyListener.NO_OP + try { + appContext.unregisterReceiver(this) + } catch (ignore: Exception) { + // ignore if already unregistered + } + } +}