From a8751388c9a86ae95714a15279994d8aa426a26d Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期五, 13 六月 2025 14:59:02 +0800
Subject: [PATCH] feat(generallibrary): 优化充值功能并添加水价获取- 在 activity_recharge_detail.xml 中移除了不必要的 bold 样式- 在 EditText 中添加了金额输入限制(数字和小数点)- 在 BaseApplication 中添加了水价信息存储 - 在 MainActivity 中实现了延时获取水价的功能- 优化了 RechargeDetailActivity 中的水价检查和使用逻辑 - 在 README.md 中添加了金额格式化和水价获取的最佳实践指南
---
baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcReadHelper.java | 129 +++++++++++++++++++++++++++++++++++++-----
1 files changed, 112 insertions(+), 17 deletions(-)
diff --git a/baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcReadHelper.java b/baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcReadHelper.java
index bcad792..cb24575 100644
--- a/baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcReadHelper.java
+++ b/baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcReadHelper.java
@@ -7,6 +7,8 @@
import android.nfc.tech.MifareClassic;
import android.util.Log;
+import androidx.annotation.NonNull;
+
import com.dayu.baselibrary.bean.BaseManagerToUserCard;
import com.dayu.baselibrary.bean.BaseUserCardCard;
import com.dayu.baselibrary.tools.HexUtil;
@@ -31,9 +33,11 @@
private static NativeNfcReadHelper helper;
- public NativeNfcReadHelper(Intent intent, Activity activity) {
+ @Override
+ public void setIntent(Intent intent) {
this.tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
+
/**
* 鍗曚緥鍒濆鍖�
@@ -43,8 +47,9 @@
*/
public static NativeNfcReadHelper getInstence(Intent intent, Activity activity) {
if (helper == null) {
- helper = new NativeNfcReadHelper(intent, activity);
+ helper = new NativeNfcReadHelper();
}
+ helper.setIntent(intent);
return helper;
}
@@ -53,8 +58,9 @@
*
* @return
*/
+ @Override
public BaseUserCardCard getUserCardData(BaseUserCardCard userCardCard) {
- if (userCardCard!=null){
+ if (userCardCard != null) {
BaseUserCardCard userCard = null;
Map<String, List<byte[]>> map = new HashMap<>();
MifareClassic mfc = MifareClassic.get(tag);
@@ -117,8 +123,9 @@
return null;
}
- public String getCradType() {
+ @Override
+ public String getCradType() {
MifareClassic mfc = MifareClassic.get(tag);
if (null != mfc) {
try {
@@ -161,6 +168,7 @@
*
* @param callback
*/
+ @Override
public void getAllData(final NFCCallMapback callback) {
Map<String, List<byte[]>> map = new HashMap<>();
MifareClassic mfc = MifareClassic.get(tag);
@@ -232,6 +240,7 @@
*
* @param callback
*/
+ @Override
public void getOneSectorData(NFCCallListback callback) {
@@ -305,6 +314,7 @@
* @param b 鍧�
* @param callback
*/
+ @Override
public void getData(final int a, final int b, final NFCCallByteback callback) {
new Thread(new Runnable() {
@Override
@@ -375,18 +385,30 @@
@Override
public String getCardNumber() {
+ if (tag == null) {
+ return "";
+ }
MifareClassic mfc = MifareClassic.get(tag);
if (null != mfc) {
try {
mfc.connect();
//鑾峰彇褰撳墠鍗″彿
boolean isOpen = false;
- for (int i = 0; i < listKeyA.size(); i++) {
- if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) {
+ if (!listKeyA.isEmpty()) {
+ for (int i = 0; i < listKeyA.size(); i++) {
+ if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) {
+ isOpen = true;
+ break;
+ }
+ }
+ } else if (!listA_PS.isEmpty()) {
+ if (mfc.authenticateSectorWithKeyA(0, defauleKey)) {
isOpen = true;
- break;
+ } else if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) {
+ isOpen = true;
}
}
+
if (isOpen) {
int bIndex = mfc.sectorToBlock(0);
byte[] data = mfc.readBlock(bIndex + 0);
@@ -412,13 +434,65 @@
return "";
}
+
+ public String getCardNumberNoClose() {
+ if (tag == null) {
+ return "";
+ }
+ MifareClassic mfc = MifareClassic.get(tag);
+ if (null != mfc) {
+ try {
+ mfc.connect();
+ //鑾峰彇褰撳墠鍗″彿
+ boolean isOpen = false;
+ if (!listKeyA.isEmpty()) {
+ for (int i = 0; i < listKeyA.size(); i++) {
+ if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) {
+ isOpen = true;
+ break;
+ }
+ }
+ } else if (!listA_PS.isEmpty()) {
+ if (mfc.authenticateSectorWithKeyA(0, defauleKey)) {
+ isOpen = true;
+ } else if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) {
+ isOpen = true;
+ }
+ }
+
+ if (isOpen) {
+ int bIndex = mfc.sectorToBlock(0);
+ byte[] data = mfc.readBlock(bIndex + 0);
+ if (data != null && data.length > 0) {
+ String hex = HexUtil.bytesToHex(Arrays.copyOfRange(data, 0, 4));
+ hex = HexUtil.spaceHex(hex);
+ hex = HexUtil.HighLowHex(hex);
+ Log.i("NFCWreatActivity", "hex===" + hex);
+ return hex.toUpperCase();
+ }
+ }
+
+ } catch (IOException e) {
+ Log.i("NFCWreatActivity", e.toString());
+ return BaseCommon.CARD_TYPE_ERROR1;
+ }
+ }
+ return "";
+ }
+
+ @Override
+ public String getCradTypeAndCardNumber() {
+ return getCradTypeAndCardNumber(1);
+ }
+
+
/**
* 鑾峰彇鍗$墖绫诲瀷鍜屽崱鍙�
*
* @return
*/
- @Override
- public String getCradTypeAndCardNumber() {
+
+ public String getCradTypeAndCardNumber(int sectorIndex) {
MifareClassic mfc = MifareClassic.get(tag);
if (null != mfc) {
@@ -427,10 +501,18 @@
StringBuilder strData = new StringBuilder();
//鑾峰彇褰撳墠鍗″彿
boolean isOpen = false;
- for (int i = 0; i < listKeyA.size(); i++) {
- if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) {
+ if (!listKeyA.isEmpty()) {
+ for (int i = 0; i < listKeyA.size(); i++) {
+ if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) {
+ isOpen = true;
+ break;
+ }
+ }
+ } else if (!listA_PS.isEmpty()) {
+ if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) {
isOpen = true;
- break;
+ } else if (mfc.authenticateSectorWithKeyA(0, defauleKey)) {
+ isOpen = true;
}
}
if (isOpen) {
@@ -446,15 +528,23 @@
}
}
//鑾峰彇鍗$墖绫诲瀷
- for (int i = 0; i < listKeyA.size(); i++) {
- if (mfc.authenticateSectorWithKeyA(1, listKeyA.get(i))) {
+ if (!listKeyA.isEmpty()) {
+ for (int i = 0; i < listKeyA.size(); i++) {
+ if (mfc.authenticateSectorWithKeyA(sectorIndex, listKeyA.get(i))) {
+ isOpen = true;
+ break;
+ }
+ }
+ } else if (!listA_PS.isEmpty()) {
+ if (mfc.authenticateSectorWithKeyA(sectorIndex, listA_PS.get(sectorIndex))) {
isOpen = true;
- break;
+ } else if (mfc.authenticateSectorWithKeyA(sectorIndex, defauleKey)) {
+ isOpen = true;
}
}
if (isOpen) {
- int bIndex = mfc.sectorToBlock(1);
- byte[] data = mfc.readBlock(bIndex + 0);
+ int bIndex = mfc.sectorToBlock(sectorIndex);
+ byte[] data = mfc.readBlock(bIndex + sectorIndex);
if (data != null && data.length > 0) {
String hex = HexUtil.byteToHex(data[0]);
strData.append(hex);
@@ -543,6 +633,7 @@
*
* @return
*/
+ @Override
public BaseManagerToUserCard getManagerToUserCardData(BaseManagerToUserCard baseManagerToUserCard) {
BaseManagerToUserCard managerToUserCard = null;
@@ -602,4 +693,8 @@
}
return null;
}
+
+
+
+
}
--
Gitblit v1.8.0