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
This commit is contained in:
Seyed Mostafa Hasani 2024-07-15 11:47:07 +03:30 committed by GitHub
parent 79c30767fc
commit bed9b8e4d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 41 deletions

View File

@ -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
}
}
}

View File

@ -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
}
}
}