From bed9b8e4d991f18fd4fc0dcd563bd9012916dddf Mon Sep 17 00:00:00 2001 From: Seyed Mostafa Hasani Date: Mon, 15 Jul 2024 11:47:07 +0330 Subject: [PATCH] refactor(android): migrate AudioBecomingNoisyReceiver to Kotlin (#3997) * Rename .java to .kt * refactor(android): migrate AudioBecomingNoisyReceiver to Kotlin * fix: lint error * chore: initialize appContext with context.applicationContext --- .../receiver/AudioBecomingNoisyReceiver.java | 41 ------------------- .../receiver/AudioBecomingNoisyReceiver.kt | 35 ++++++++++++++++ 2 files changed, 35 insertions(+), 41 deletions(-) delete mode 100644 android/src/main/java/com/brentvatne/receiver/AudioBecomingNoisyReceiver.java create mode 100644 android/src/main/java/com/brentvatne/receiver/AudioBecomingNoisyReceiver.kt 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 + } + } +}