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:
parent
79c30767fc
commit
bed9b8e4d9
@ -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
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user