-
-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add first draft for NFC sensor * Rename NfcSensorManager.nfcSensor to nfcStateSensor * Move NfcSensorManager to the common module * Add BroadcastReceiver subscription to NfcAdapter.ACTION_ADAPTER_STATE_CHANGED in both the app and wear modules * Remove TODO comments * Change updateType to INTENT * Convert simple methods to expression body * Change mdi:nfc to mdi:nfc-variant * Fix docs URL * Reword NFC sensor manager according to the PR suggestions
- Loading branch information
1 parent
8c7e4e5
commit 2641422
Showing
6 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
common/src/main/java/io/homeassistant/companion/android/common/sensors/NfcSensorManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.homeassistant.companion.android.common.sensors | ||
|
||
import android.content.Context | ||
import android.nfc.NfcAdapter | ||
import io.homeassistant.companion.android.common.R as commonR | ||
|
||
class NfcSensorManager : SensorManager { | ||
companion object { | ||
private const val TAG = "NfcSensor" | ||
|
||
val nfcStateSensor = SensorManager.BasicSensor( | ||
"nfc_state", | ||
"binary_sensor", | ||
commonR.string.basic_sensor_name_nfc_state, | ||
commonR.string.sensor_description_nfc_state, | ||
"mdi:nfc-variant", | ||
entityCategory = SensorManager.ENTITY_CATEGORY_DIAGNOSTIC, | ||
updateType = SensorManager.BasicSensor.UpdateType.INTENT | ||
) | ||
} | ||
|
||
override fun docsLink() = "https://companion.home-assistant.io/docs/core/sensors#nfc-state-sensor" | ||
override val name = commonR.string.sensor_name_nfc | ||
|
||
override suspend fun getAvailableSensors(context: Context) = listOf(nfcStateSensor) | ||
|
||
override fun requiredPermissions(sensorId: String) = emptyArray<String>() | ||
|
||
override fun requestSensorUpdate(context: Context) = updateNfcState(context) | ||
|
||
override fun hasSensor(context: Context): Boolean { | ||
return NfcAdapter.getDefaultAdapter(context) != null | ||
} | ||
|
||
private fun updateNfcState(context: Context) { | ||
if (!isEnabled(context, nfcStateSensor)) { | ||
return | ||
} | ||
|
||
val nfcAdapter = NfcAdapter.getDefaultAdapter(context) | ||
val nfcEnabled = nfcAdapter?.isEnabled == true | ||
|
||
onSensorUpdated( | ||
context, | ||
nfcStateSensor, | ||
nfcEnabled, | ||
nfcStateSensor.statelessIcon, | ||
emptyMap() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters