forked from andras-adam/virtualtag
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request andras-adam#19 from NeoAren/nfc-data
Nfc data
- Loading branch information
Showing
16 changed files
with
530 additions
and
108 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
app/src/main/java/com/virtualtag/app/data/MifareClassicInfo.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,61 @@ | ||
package com.virtualtag.app.data | ||
|
||
import android.nfc.tech.MifareClassic | ||
import android.nfc.tech.NfcA | ||
import android.util.Log | ||
import com.virtualtag.app.utils.toHex | ||
import java.io.IOException | ||
|
||
class MifareClassicInfo(tag: MifareClassic) { | ||
val timeout: Int | ||
val maxTransceiveLength: Int | ||
val atqa: String | ||
val sak: Int | ||
val size: Int | ||
val type: Int | ||
val sectorCount: Int | ||
val blockCount: Int | ||
val data: String | ||
|
||
init { | ||
timeout = tag.timeout | ||
maxTransceiveLength = tag.maxTransceiveLength | ||
size = tag.size | ||
type = tag.type | ||
sectorCount = tag.sectorCount | ||
blockCount = tag.blockCount | ||
// MifareClassic tags are also NfcA tags | ||
val nfca = NfcA.get(tag.tag) | ||
atqa = nfca.atqa.toHex() | ||
sak = nfca.sak.toInt() | ||
// Read memory data | ||
data = readData(tag) | ||
} | ||
|
||
private fun readData(tag: MifareClassic): String { | ||
try { | ||
var dataAccumulator = "" | ||
// Connect to the tag | ||
tag.connect() | ||
// Loop through sectors on the tag | ||
for (currentSector in 0 until tag.sectorCount) { | ||
// Authenticate sector with the default key | ||
val auth = tag.authenticateSectorWithKeyA(currentSector, MifareClassic.KEY_DEFAULT) | ||
if (!auth) throw IOException("Failed to authenticate sector") | ||
// Get block count in sector | ||
val sectorBlockCount = tag.getBlockCountInSector(currentSector) | ||
// Loop through blocks in the sector | ||
for (currentBlockInSector in 0 until sectorBlockCount) { | ||
// Calculate the block's index | ||
val blockIndex = tag.sectorToBlock(currentSector) + currentBlockInSector | ||
// Read the block's data and store as HEX | ||
dataAccumulator += tag.readBlock(blockIndex).toHex() | ||
} | ||
} | ||
return dataAccumulator | ||
} catch (e: IOException) { | ||
e.localizedMessage?.let { Log.e("MifareClassicHelper", it) } | ||
return "" | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
app/src/main/java/com/virtualtag/app/data/MifareUltralightInfo.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,52 @@ | ||
package com.virtualtag.app.data | ||
|
||
import android.nfc.tech.MifareUltralight | ||
import android.nfc.tech.NfcA | ||
import android.util.Log | ||
import com.virtualtag.app.utils.toHex | ||
import java.io.IOException | ||
|
||
class MifareUltralightInfo(tag: MifareUltralight) { | ||
val type: Int | ||
val timeout: Int | ||
val maxTransceiveLength: Int | ||
val atqa: String | ||
val sak: Int | ||
val data: String | ||
|
||
init { | ||
type = tag.type | ||
timeout = tag.timeout | ||
maxTransceiveLength = tag.maxTransceiveLength | ||
// MifareUltralight tags are also NfcA tags | ||
val nfca = NfcA.get(tag.tag) | ||
atqa = nfca.atqa.toHex() | ||
sak = nfca.sak.toInt() | ||
// Read memory data | ||
data = readData(tag) | ||
} | ||
|
||
private fun readData(tag: MifareUltralight): String { | ||
try { | ||
var dataAccumulator = "" | ||
// Connect to the tag | ||
tag.connect() | ||
// Get the number of readable pages | ||
val readablePagesCount = when (type) { | ||
// Ultralight has 16 pages, all readable | ||
MifareUltralight.TYPE_ULTRALIGHT -> 16 | ||
// Ultralight C has 48 pages, but the last 4 are unreadable | ||
MifareUltralight.TYPE_ULTRALIGHT_C -> 44 | ||
else -> 0 | ||
} | ||
// Read every fourth page, as 4 pages are read at a time by `readPages()` | ||
for (currentPage in 0 until readablePagesCount) { | ||
if (currentPage % 4 == 0) dataAccumulator += tag.readPages(currentPage).toHex() | ||
} | ||
return dataAccumulator | ||
} catch (e: IOException) { | ||
e.localizedMessage?.let { Log.e("MifareUltralightHelper", it) } | ||
return "" | ||
} | ||
} | ||
} |
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
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
52 changes: 52 additions & 0 deletions
52
app/src/main/java/com/virtualtag/app/ui/components/DataRow.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,52 @@ | ||
package com.virtualtag.app.ui.components | ||
|
||
import android.widget.Toast | ||
import androidx.compose.foundation.ExperimentalFoundationApi | ||
import androidx.compose.foundation.combinedClickable | ||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.material.Divider | ||
import androidx.compose.material.Icon | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.vector.ImageVector | ||
import androidx.compose.ui.platform.ClipboardManager | ||
import androidx.compose.ui.platform.LocalClipboardManager | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.text.AnnotatedString | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.unit.dp | ||
import com.virtualtag.app.R | ||
|
||
@OptIn(ExperimentalFoundationApi::class) | ||
@Composable | ||
fun DataRow(title: String, content: String, icon: ImageVector) { | ||
val context = LocalContext.current | ||
val clipboardManager: ClipboardManager = LocalClipboardManager.current | ||
Column(modifier = Modifier | ||
.fillMaxWidth() | ||
.combinedClickable( | ||
onLongClick = { | ||
clipboardManager.setText(AnnotatedString(content)) | ||
Toast | ||
.makeText(context, context.getString(R.string.clipboard), Toast.LENGTH_SHORT) | ||
.show() | ||
}, | ||
onClick = { /* do nothing */ })) { | ||
Row(modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(top = 8.dp)) { | ||
Icon(icon, contentDescription = null, modifier = Modifier | ||
.size(48.dp) | ||
.padding(horizontal = 4.dp)) | ||
Column(modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(top = 2.dp, end = 16.dp)) { | ||
Text(title, fontWeight = FontWeight.Bold, modifier = Modifier.padding(bottom = 4.dp)) | ||
Text(content, modifier = Modifier.padding(bottom = 8.dp)) | ||
} | ||
} | ||
Divider(color = Color.LightGray) | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
app/src/main/java/com/virtualtag/app/ui/components/MifareClassicView.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,106 @@ | ||
package com.virtualtag.app.ui.components | ||
|
||
import android.nfc.tech.MifareClassic | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.* | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import com.virtualtag.app.db.Card | ||
import com.virtualtag.app.utils.formatHex | ||
import com.virtualtag.app.R | ||
|
||
@Composable | ||
fun MifareClassicView(card: Card) { | ||
val id = formatHex(card.id) | ||
val techList = card.techList | ||
.replace("android.nfc.tech.", "") | ||
.replace(",", ", ") | ||
val type = when (card.mifareClassicType ?: MifareClassic.TYPE_UNKNOWN) { | ||
MifareClassic.TYPE_CLASSIC -> "MifareClassic Classic" | ||
MifareClassic.TYPE_PLUS -> "MifareClassic Plus" | ||
MifareClassic.TYPE_PRO -> "MifareClassic Pro" | ||
else -> "MifareClassic ${stringResource(R.string.unknown)}" | ||
} | ||
val sectorCount = card.mifareClassicSectorCount ?: 0 | ||
val blockCount = card.mifareClassicBlockCount ?: 0 | ||
val size = (card.mifareClassicSize ?: 0) * MifareClassic.BLOCK_SIZE | ||
val timeout = card.mifareClassicTimeout ?: 0 | ||
val maxTranscieveLength = card.mifareClassicMaxTransceiveLength ?: 0 | ||
val atqa = card.mifareClassicAtqa ?: "" | ||
val sak = card.mifareClassicSak ?: 0 | ||
val data = card.mifareClassicData ?: "" | ||
|
||
LazyColumn(modifier = Modifier.fillMaxWidth()) { | ||
item { | ||
DataRow( | ||
title = stringResource(R.string.serial_number), | ||
content = id, | ||
icon = Icons.Filled.Key) | ||
} | ||
item { | ||
DataRow( | ||
title = stringResource(R.string.technologies), | ||
content = techList, | ||
icon = Icons.Filled.Style) | ||
} | ||
item { | ||
DataRow( | ||
title = stringResource(R.string.type), | ||
content = type, | ||
icon = Icons.Filled.Category) | ||
} | ||
item { | ||
DataRow( | ||
title = stringResource(R.string.mem_size), | ||
content = "$size bytes", | ||
icon = Icons.Filled.Save) | ||
} | ||
item { | ||
DataRow( | ||
title = stringResource(R.string.mem_sector_count), | ||
content = "$sectorCount", | ||
icon = Icons.Filled.Save) | ||
} | ||
item { | ||
DataRow( | ||
title = stringResource(R.string.mem_block_count), | ||
content = "$blockCount", | ||
icon = Icons.Filled.Save) | ||
} | ||
item { | ||
DataRow( | ||
title = stringResource(R.string.transcieve_timeout), | ||
content = "$timeout ${stringResource(R.string.ms)}", | ||
icon = Icons.Filled.Sensors) | ||
} | ||
item { | ||
DataRow( | ||
title = stringResource(R.string.transcieve_max_length), | ||
content = "$maxTranscieveLength ${stringResource(R.string.bytes)}", | ||
icon = Icons.Filled.Sensors) | ||
} | ||
item { | ||
DataRow( | ||
title = stringResource(R.string.atqa), | ||
content = atqa, | ||
icon = Icons.Filled.Code) | ||
} | ||
item { | ||
DataRow( | ||
title = stringResource(R.string.sak), | ||
content = "$sak", | ||
icon = Icons.Filled.Code) | ||
} | ||
if (data.isNotEmpty()) { | ||
items(blockCount) { index -> | ||
DataRow( | ||
title = "${stringResource(R.string.mem_block)} ${index + 1}", | ||
content = formatHex(data.substring(index * 32, index * 32 + 32)), | ||
icon = Icons.Filled.Memory) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.