Skip to content

Commit

Permalink
Merge pull request #67 from NordicPlayground/broadcast-receivers-fix
Browse files Browse the repository at this point in the history
Adding flags to broadcast received registration
  • Loading branch information
philips77 authored Sep 20, 2023
2 parents 75e06e0 + 4b604d8 commit 4d5a59b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ android {
dependencies {
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.core)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ import android.content.IntentFilter
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.platform.LocalContext
import androidx.core.content.ContextCompat

@SuppressLint("ComposableNaming")
@Composable
fun registerReceiver(intentFilter: IntentFilter, onEvent: (Intent?) -> Unit) {
fun registerReceiver(
intentFilter: IntentFilter,
@ContextCompat.RegisterReceiverFlags flags: Int = ContextCompat.RECEIVER_NOT_EXPORTED,
onEvent: (Intent?) -> Unit
) {
val context = LocalContext.current

DisposableEffect(context) {
Expand All @@ -51,7 +56,7 @@ fun registerReceiver(intentFilter: IntentFilter, onEvent: (Intent?) -> Unit) {
onEvent(intent)
}
}
context.registerReceiver(broadcast, intentFilter)
ContextCompat.registerReceiver(context, broadcast, intentFilter, flags)
onDispose {
context.unregisterReceiver(broadcast)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.core.content.ContextCompat
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow
Expand Down Expand Up @@ -67,7 +68,7 @@ class BluetoothStateManager @Inject constructor(
addAction(BluetoothAdapter.ACTION_STATE_CHANGED)
addAction(REFRESH_PERMISSIONS)
}
context.registerReceiver(bluetoothStateChangeHandler, filter)
ContextCompat.registerReceiver(context, bluetoothStateChangeHandler, filter, ContextCompat.RECEIVER_NOT_EXPORTED)
awaitClose {
context.unregisterReceiver(bluetoothStateChangeHandler)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.location.LocationManager
import androidx.core.content.ContextCompat
import androidx.core.location.LocationManagerCompat
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.channels.awaitClose
Expand Down Expand Up @@ -69,7 +70,7 @@ internal class LocationStateManager @Inject constructor(
addAction(LocationManager.MODE_CHANGED_ACTION)
addAction(REFRESH_PERMISSIONS)
}
context.registerReceiver(locationStateChangeHandler, filter)
ContextCompat.registerReceiver(context, locationStateChangeHandler, filter, ContextCompat.RECEIVER_NOT_EXPORTED)
awaitClose {
context.unregisterReceiver(locationStateChangeHandler)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.nfc.NfcAdapter
import androidx.core.content.ContextCompat
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow
Expand Down Expand Up @@ -72,7 +73,7 @@ internal class NfcStateManager @Inject constructor(
val filter = IntentFilter().apply {
addAction(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)
}
context.registerReceiver(nfcStateChangeHandler, filter)
ContextCompat.registerReceiver(context, nfcStateChangeHandler, filter, ContextCompat.RECEIVER_NOT_EXPORTED)
awaitClose {
context.unregisterReceiver(nfcStateChangeHandler)
}
Expand Down

0 comments on commit 4d5a59b

Please sign in to comment.