improved readability on BluetoothStateReceiver

and renamed `intent` to `intentFilter` to properly reflect type
This commit is contained in:
Ken Van Hoeylandt
2020-09-17 20:47:54 +02:00
parent b98a8ea34b
commit 8219ef42ed
3 changed files with 16 additions and 18 deletions

View File

@@ -371,7 +371,7 @@ class MainActivity : AppCompatActivity(), Logging,
updateBluetoothEnabled()
/// We now want to be informed of bluetooth state
registerReceiver(btStateReceiver, btStateReceiver.intent)
registerReceiver(btStateReceiver, btStateReceiver.intentFilter)
// if (!isInTestLab) - very important - even in test lab we must request permissions because we need location perms for some of our tests to pass
requestPermission()

View File

@@ -10,23 +10,21 @@ import com.geeksville.util.exceptionReporter
/**
* A helper class to call onChanged when bluetooth is enabled or disabled
*/
class BluetoothStateReceiver(val onChanged: (Boolean) -> Unit) : BroadcastReceiver() {
val intent =
IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED) // Can be used for registering
class BluetoothStateReceiver(
private val onChanged: (Boolean) -> Unit
) : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) =
exceptionReporter {
if (intent.action == BluetoothAdapter.ACTION_STATE_CHANGED) {
when (intent.getIntExtra(
BluetoothAdapter.EXTRA_STATE,
-1
)) {
// Simulate a disconnection if the user disables bluetooth entirely
BluetoothAdapter.STATE_OFF -> onChanged(
false
)
BluetoothAdapter.STATE_ON -> onChanged(true)
}
val intentFilter get() = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED) // Can be used for registering
override fun onReceive(context: Context, intent: Intent) = exceptionReporter {
if (intent.action == BluetoothAdapter.ACTION_STATE_CHANGED) {
when (intent.bluetoothAdapterState) {
// Simulate a disconnection if the user disables bluetooth entirely
BluetoothAdapter.STATE_OFF -> onChanged(false)
BluetoothAdapter.STATE_ON -> onChanged(true)
}
}
}
private val Intent.bluetoothAdapterState: Int get() = getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
}

View File

@@ -195,7 +195,7 @@ class RadioInterfaceService : Service(), Logging {
override fun onCreate() {
runningService = this
super.onCreate()
registerReceiver(bluetoothStateReceiver, bluetoothStateReceiver.intent)
registerReceiver(bluetoothStateReceiver, bluetoothStateReceiver.intentFilter)
}
override fun onDestroy() {