Merge pull request #164 from KenVanHoeylandt/bluetooth-state-receiver-readability

Improved readability on BluetoothStateReceiver
pull/167/head
Kevin Hester 2020-09-19 07:51:50 -07:00 zatwierdzone przez GitHub
commit a179c46b50
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
3 zmienionych plików z 16 dodań i 18 usunięć

Wyświetl plik

@ -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()

Wyświetl plik

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

Wyświetl plik

@ -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() {