From feb7add1979c830e0d940286242a7c489f2f8f96 Mon Sep 17 00:00:00 2001 From: zuoxiao <470321431@qq.com> Date: 星期五, 27 九月 2024 09:23:34 +0800 Subject: [PATCH] 优化NFC模块架构,将一些方法抽调到父类中。 --- henanlibrary/src/main/java/com/dayu/henanlibrary/card/DomainCard.java | 174 ++++++++++++++++++++++++++++++++++++++++------------------ 1 files changed, 120 insertions(+), 54 deletions(-) diff --git a/henanlibrary/src/main/java/com/dayu/henanlibrary/card/DomainCard.java b/henanlibrary/src/main/java/com/dayu/henanlibrary/card/DomainCard.java index f395263..ec248e1 100644 --- a/henanlibrary/src/main/java/com/dayu/henanlibrary/card/DomainCard.java +++ b/henanlibrary/src/main/java/com/dayu/henanlibrary/card/DomainCard.java @@ -1,8 +1,10 @@ package com.dayu.henanlibrary.card; +import android.text.TextUtils; + import com.dayu.baselibrary.tools.BcdUtil; import com.dayu.baselibrary.tools.HexUtil; -import com.dayu.henanlibrary.utils.MyCommon; +import com.dayu.henanlibrary.utils.CardCommon; import com.tencent.bugly.crashreport.CrashReport; import java.io.Serializable; @@ -19,13 +21,23 @@ public class DomainCard implements Serializable { - public String cardType = MyCommon.DOMAIN_CARD_TYPE;//鍗$被鍨� + public String cardType = CardCommon.DOMAIN_CARD_TYPE;//鍗$被鍨� public String domainNumber;//鍩熷悕搴忓彿 (BCD鏍煎紡) public int domainLength;//鍩熷悕闀垮害 //绔彛鍙蜂负5涓瓧鑺傦紝涓嶈冻5浣嶉珮浣嶈ˉ0銆傜鍙e彿涓庡煙鍚嶄箣闂寸敤鈥�,鈥濋殧寮�銆傜鍙e彿鏈�鍚庝互鈥�#鈥濈粨鏉� public String domainName;//鍩熷悕 public int port;//绔彛 + + public int type;//鍩熷悕鍗℃柊涔1鍚庢湁01==0锛岄箍閭戞病鏈�01==1 + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } public String getDomainName() { return domainName; @@ -71,34 +83,80 @@ public static DomainCard toBean(List<byte[]> data) { try { - DomainCard domainCard = new DomainCard(); + DomainCard domainXinXiangCard = new DomainCard(); String domainNumber = BcdUtil.bcdToStr(data.get(0)[1]); - domainCard.domainNumber = domainNumber; - int domainLength = HexUtil.get16to10(HexUtil.byteToHex(data.get(0)[2])); - byte[] domainData = new byte[domainLength + 7]; - int index = 0; - for (int i = 0; i < 3; i++) { - int jIndex; - if (i == 0) { - jIndex = 3; - } else { - jIndex = 0; - } - for (int j = jIndex; j < 15; j++) { - if (index < domainData.length) { - domainData[index] = data.get(i)[j]; - index++; + if ("00".equals(domainNumber)||"01".equals(domainNumber)||"02".equals(domainNumber)){//鍋囧搴忓彿灏忎簬绛変簬2灏辫涓烘槸鏂颁埂鐨� + domainXinXiangCard.setType(0); + domainXinXiangCard.domainNumber = domainNumber; + int domainLength = HexUtil.get16to10(HexUtil.byteToHex(data.get(0)[2])); + byte[] domainData = new byte[domainLength + 7]; + int index = 0; + for (int i = 0; i < 3; i++) { + int jIndex; + if (i == 0) { + jIndex = 3; + } else { + jIndex = 0; } + for (int j = jIndex; j < 15; j++) { + if (index < domainData.length) { + domainData[index] = data.get(i)[j]; + index++; + } + } + } + String domainStr = new String(domainData, StandardCharsets.US_ASCII); + if (!TextUtils.isEmpty(domainStr)){ + try { + String[] parts = domainStr.split("#")[0].split(","); + if (parts.length >= 2) { + String domainName = parts[0]; + String port = parts[1].replace("#", ""); + domainXinXiangCard.setDomainName(domainName); + domainXinXiangCard.setPort(Integer.valueOf(port)); + } + } catch (NumberFormatException e) { + e.printStackTrace(); + } + } + }else { + domainXinXiangCard.setType(1); + int domainLength = HexUtil.get16to10(HexUtil.byteToHex(data.get(0)[1])); + byte[] domainData = new byte[domainLength + 7]; + int index = 0; + for (int i = 0; i < 3; i++) { + int jIndex; + if (i == 0) { + jIndex = 2; + } else { + jIndex = 0; + } + for (int j = jIndex; j < 15; j++) { + if (index < domainData.length) { + domainData[index] = data.get(i)[j]; + index++; + } + } + } + String domainStr = new String(domainData, StandardCharsets.US_ASCII); + + if (!TextUtils.isEmpty(domainStr)){ + try { + String[] parts = domainStr.split("#")[0].split(","); + if (parts.length >= 2) { + String domainName = parts[0]; + String port = parts[1].replace("#", ""); + domainXinXiangCard.setDomainName(domainName); + domainXinXiangCard.setPort(Integer.valueOf(port)); + } + } catch (NumberFormatException e) { + e.printStackTrace(); + } } } - String domainStr = new String(domainData, StandardCharsets.US_ASCII); - String domainName = domainStr.split(",")[0]; - String port = domainStr.split(",")[1].replace("#", ""); - domainCard.setDomainName(domainName); - domainCard.setPort(Integer.valueOf(port)); - return domainCard; + return domainXinXiangCard; } catch (Exception e) { e.printStackTrace(); CrashReport.postCatchedException(e); @@ -110,28 +168,39 @@ /** * 鐢ㄦ埛鍗�0鍧� */ - public class Zero extends BaseCard { + public class Zero extends HNBaseCard { public byte[] toByte() { byte[] data = new byte[16]; data[0] = HexUtil.hexToByte(cardType); - if (domainNumber != null) { - byte[] domainNumbers = BcdUtil.strToBcd(domainNumber); - data[1] = domainNumbers[0]; - } domainLength = domainName.length(); - data[2] = HexUtil.hexToByte(HexUtil.get10to16(domainLength)); - String portStr = String.valueOf(port); - int lenght = portStr.length(); - for (int i = 0; i < 5 - lenght; i++) { - portStr = "0" + portStr; - } - String ascii = IDN.toASCII(domainName + "," + portStr + "#"); - asciiByte = ascii.getBytes(); - if (asciiByte.length < 12) { - System.arraycopy(asciiByte, 0, data, 3, asciiByte.length); +// 鍩熷悕鍗℃柊涔1鍚庢湁01==0锛岄箍閭戞病鏈�01==1 + if (type == 0) { + if (domainNumber != null) { + byte[] domainNumbers = BcdUtil.strToBcd(domainNumber); + data[1] = domainNumbers[0]; + } + data[2] = HexUtil.hexToByte(HexUtil.get10to16(domainLength)); + String portStr = String.valueOf(port); + int lenght = portStr.length(); + for (int i = 0; i < 5 - lenght; i++) { + portStr = "0" + portStr; + } + String ascii = IDN.toASCII(domainName + "," + portStr + "#"); + asciiByte = ascii.getBytes(); + System.arraycopy(asciiByte, 0, data, 3, Math.min(asciiByte.length, 12)); } else { - System.arraycopy(asciiByte, 0, data, 3, 12); + data[1] = HexUtil.hexToByte(HexUtil.get10to16(domainLength)); + String portStr = String.valueOf(port); + int lenght = portStr.length(); + for (int i = 0; i < 5 - lenght; i++) { + portStr = "0" + portStr; + } + String ascii = IDN.toASCII(domainName + "," + portStr + "#"); + asciiByte = ascii.getBytes(); + System.arraycopy(asciiByte, 0, data, 2, Math.min(asciiByte.length, 13)); + } + data[15] = getByteSum(data); return data; } @@ -141,17 +210,16 @@ /** * 鐢ㄦ埛鍗�1鍧� */ - public class One extends BaseCard { + public class One extends HNBaseCard { public byte[] toBytes() { byte[] data = new byte[16]; - if (asciiByte.length > 12) { - if (asciiByte.length - 12 < 15) { - System.arraycopy(asciiByte, 12, data, 0, asciiByte.length - 12); - } else { - System.arraycopy(asciiByte, 12, data, 0, 15); - } + int offset = (type == 0) ? 12 : 13; + + if (asciiByte.length > offset) { + System.arraycopy(asciiByte, offset, data, 0, Math.min(asciiByte.length - offset, 15)); } + data[15] = getByteSum(data); return data; } @@ -162,15 +230,13 @@ /** * 鐢ㄦ埛鍗�2鍧� */ - public class Two extends BaseCard { + public class Two extends HNBaseCard { public byte[] toBytes() { byte[] data = new byte[16]; - if (asciiByte.length > (12 + 15)) { - if (asciiByte.length - (12 + 15) < 15) { - System.arraycopy(asciiByte, 12, data, 0, asciiByte.length - (12 + 15)); - } else { - System.arraycopy(asciiByte, 12, data, 0, 15); - } + int offset = (type == 0) ? 27 : 28; // 12 + 15 and 13 + 15 + if (asciiByte.length > offset) { + int lengthToCopy = Math.min(asciiByte.length - offset, 15); + System.arraycopy(asciiByte, offset, data, 0, lengthToCopy); } data[15] = getByteSum(data); return data; -- Gitblit v1.8.0