Add support for listening to NFC broadcasts (#1017)

This commit is contained in:
David Perez 2024-02-15 09:15:47 -06:00 committed by Álison Fernandes
parent 866dce2926
commit d6513a1ef7
3 changed files with 80 additions and 0 deletions

View file

@ -0,0 +1,20 @@
package com.x8bit.bitwarden.ui.platform.manager.nfc
/**
* Provides basic functionality for starting and stopping discovery of NFC devices.
*/
interface NfcManager {
/**
* Starts listening for NFC events.
*
* Note: This is a no-op if the device does not support NFC.
*/
fun start()
/**
* Stops listening for NFC events.
*
* Note: This is a no-op if the device does not support NFC.
*/
fun stop()
}

View file

@ -0,0 +1,50 @@
package com.x8bit.bitwarden.ui.platform.manager.nfc
import android.app.Activity
import android.app.PendingIntent
import android.content.Intent
import android.content.IntentFilter
import android.nfc.NfcAdapter
import com.x8bit.bitwarden.MainActivity
import com.x8bit.bitwarden.data.autofill.util.toPendingIntentMutabilityFlag
import com.x8bit.bitwarden.data.platform.annotation.OmitFromCoverage
/**
* The default implementation of the [NfcManager].
*/
@OmitFromCoverage
class NfcManagerImpl(
private val activity: Activity,
) : NfcManager {
private val nfcAdapter: NfcAdapter? = NfcAdapter.getDefaultAdapter(activity)
private val supportsNfc: Boolean get() = nfcAdapter?.isEnabled == true
override fun start() {
if (!supportsNfc) return
nfcAdapter?.enableForegroundDispatch(
activity,
PendingIntent.getActivity(
activity,
1,
Intent(activity, MainActivity::class.java).addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP,
),
PendingIntent.FLAG_UPDATE_CURRENT.toPendingIntentMutabilityFlag(),
),
arrayOf(
IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED).apply {
// Register for all NDEF tags starting with http or https
addDataScheme("http")
addDataScheme("https")
},
),
null,
)
}
override fun stop() {
if (!supportsNfc) return
nfcAdapter?.disableForegroundDispatch(activity)
}
}

View file

@ -29,6 +29,8 @@ import com.x8bit.bitwarden.ui.platform.manager.exit.ExitManager
import com.x8bit.bitwarden.ui.platform.manager.exit.ExitManagerImpl
import com.x8bit.bitwarden.ui.platform.manager.intent.IntentManager
import com.x8bit.bitwarden.ui.platform.manager.intent.IntentManagerImpl
import com.x8bit.bitwarden.ui.platform.manager.nfc.NfcManager
import com.x8bit.bitwarden.ui.platform.manager.nfc.NfcManagerImpl
import com.x8bit.bitwarden.ui.platform.manager.permissions.PermissionsManager
import com.x8bit.bitwarden.ui.platform.manager.permissions.PermissionsManagerImpl
@ -85,6 +87,7 @@ fun BitwardenTheme(
LocalIntentManager provides IntentManagerImpl(context),
LocalExitManager provides ExitManagerImpl(activity),
LocalBiometricsManager provides BiometricsManagerImpl(activity),
LocalNfcManager provides NfcManagerImpl(activity),
) {
// Set overall theme based on color scheme and typography settings
MaterialTheme(
@ -201,6 +204,13 @@ val LocalPermissionsManager: ProvidableCompositionLocal<PermissionsManager> = co
error("CompositionLocal LocalPermissionsManager not present")
}
/**
* Provides access to the NFC manager throughout the app.
*/
val LocalNfcManager: ProvidableCompositionLocal<NfcManager> = compositionLocalOf {
error("CompositionLocal NfcManager not present")
}
/**
* Provides access to non material theme typography throughout the app.
*/