diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFMessageUploadModule.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFMessageUploadModule.java index ea99227..62335a3 100644 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFMessageUploadModule.java +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFMessageUploadModule.java @@ -3,6 +3,9 @@ package com.sunvote.xpadapi.service.logic; import android.os.Handler; import android.os.HandlerThread; +import com.sunvote.xpadapi.service.listener.IUploadListener; +import com.sunvote.xpadapi.util.LogUtil; + import java.io.UnsupportedEncodingException; /** @@ -164,7 +167,7 @@ public class RFMessageUploadModule { @Override public void run() { - XPadApi.getInstance().applyFileUpload(this.length,this.filename,PACKTYPE,RFFileUploadModule.index); + XpadApiServiceInfoProxyManager.getInstance().getService().applyFileUpload(this.length,this.filename,PACKTYPE,RFFileUploadModule.index); } } @@ -188,7 +191,7 @@ public class RFMessageUploadModule { @Override public void run() { - XPadApi.getInstance().packetConfirmation(this.keyid,this.packid,this.packH,this.packL,this.names,PACKTYPE); + XpadApiServiceInfoProxyManager.getInstance().getService().packetConfirmation(this.keyid,this.packid,this.packH,this.packL,this.names,PACKTYPE); times ++ ; if(task != null && times < 5){ handler.postDelayed(task,1000); @@ -229,7 +232,7 @@ public class RFMessageUploadModule { for(byte packL = 0 ;packL < 16 ;packL ++) { if((packLsi & (1 << packL)) != 0){ offset = ((packid * 65535) + (packH) * 256 + 16 * (packL)) & 0xFFFF; - XPadApi.getInstance().uploadFileData(this.keyid, this.packid, this.packH, (byte)(packL), this.datas, offset, length,PACKTYPE); + XpadApiServiceInfoProxyManager.getInstance().getService().uploadFileData(this.keyid, this.packid, this.packH, (byte)(packL), this.datas, offset, length,PACKTYPE); try{ Thread.sleep(50); } catch (Exception e) { @@ -259,7 +262,7 @@ public class RFMessageUploadModule { @Override public void run() { - XPadApi.getInstance().packetReceptionConfirmed(this.keyid,this.packid,this.packH,PACKTYPE); + XpadApiServiceInfoProxyManager.getInstance().getService().packetReceptionConfirmed(this.keyid,this.packid,this.packH,PACKTYPE); if(uploadListener != null){ uploadListener.onUploadStop(); } diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/util/LogUtil.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/util/LogUtil.java new file mode 100644 index 0000000..dd67e27 --- /dev/null +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/util/LogUtil.java @@ -0,0 +1,323 @@ +package com.sunvote.xpadapi.util; + +import android.os.Environment; +import android.util.Log; + +import com.sunvote.util.ByteUtils; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Created by Elvis on 2017/8/15 15:03 + * Email:Eluis@psunsky.com + * 版权所有:长沙中天电子设计开发有限公司 + * Description: 工具包 + */ +public class LogUtil { + + private static FileWriter fileWriter; + + public static final int VERBOSE_LEVER = 2; + public static final int DEBUG_LEVER = 3; + public static final int INFO_LEVER = 4; + public static final int WARN_LEVER = 5; + public static final int ERROR_LEVER = 6; + public static final int ASSERT_LEVER = 7; + + public static int lever = VERBOSE_LEVER - 1 ; + + private static boolean logToFile = false; + private static boolean logToLogcat = true ; + + public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); + + LogUtil() { + throw new RuntimeException("Stub!"); + } + + public static int v(String tag, String msg) { + if (VERBOSE_LEVER > lever) { + if(logToLogcat){ + Log.v(tag, msg); + } + inputToFile("(V):" + msg ); + } + return -1; + } + + private static void init(){ + if(fileWriter == null){ + synchronized (LogUtil.class) { + if(fileWriter == null) { + try { + File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Sunvote/"); + if (!path.exists()) { + path.mkdirs(); + } + File file = new File(Environment.getExternalStorageDirectory().getPath() + + "/Sunvote/log" + simpleDateFormat.format(new Date())+".txt"); + if (!file.exists()) { + file.createNewFile(); + } + fileWriter = new FileWriter(file, true); + + } catch (Exception ex) { + ex.printStackTrace(); + fileWriter = null; + } + } + } + } + } + + public static void enableLogToFile(){ + logToFile = true; + } + + public static void disabelLogToFile(){ + logToFile = false; + } + + public static void enableLogToLogcat(){ + logToLogcat = true; + } + + public static void disableLogToLogcat(){ + logToLogcat = false; + } + + public static void enableLog(){ + lever = VERBOSE_LEVER - 1; + } + + public static void disableLog(){ + lever = ASSERT_LEVER ; + } + + public static int v(String tag, String msg, Throwable tr) { + if(VERBOSE_LEVER > lever){ + if(logToLogcat) { + Log.v(tag, msg, tr); + } + inputToFile("(V):" + tag + " " + msg + Log.getStackTraceString(tr)); + } + return -1; + } + + public static int d(String tag, String msg) { + if(DEBUG_LEVER > lever){ + if(logToLogcat) { + Log.d(tag, msg); + } + inputToFile("(D):" + tag + " " + msg ); + } + return -1; + } + + public static int d(String tag, String msg, Throwable tr) { + if(DEBUG_LEVER > lever){ + if(logToLogcat){ + Log.d(tag,msg,tr); + } + inputToFile("(D):" + tag + " " + msg + Log.getStackTraceString(tr)); + } + return -1; + } + + public static int i(String tag, String msg) { + if(INFO_LEVER > lever){ + if(logToLogcat){ + Log.i(tag,msg); + } + inputToFile("(I):" + tag + " " + msg ); + } + return -1; + } + + public static int i(String tag, String msg, Throwable tr) { + if(INFO_LEVER > lever){ + if(logToLogcat){ + Log.i(tag,msg,tr); + } + inputToFile("(I):" + tag + " " + msg + Log.getStackTraceString(tr)); + } + return -1; + } + + public static int i(String tag,byte[] msg){ + String msgStr = ByteUtils.bytesToHexString(msg); + return i(tag,msgStr); + } + + public static int i(String tag,String msgTag, byte[] msg){ + String msgStr = ByteUtils.bytesToHexString(msg); + return i(tag,msgTag + ":\r\n" + msgStr); + } + + public static int v(String tag,String msgTag, byte[] msg){ + String msgStr = ByteUtils.bytesToHexString(msg); + return v(tag,msgTag + ":\r\n" + msgStr); + } + + public static int i(String tag,String msgTag, byte[] msg,int length){ + String msgStr = ByteUtils.bytesToHexString(msg,length); + return i(tag,msgTag + ":\r\n" + msgStr); + } + + public static int v(String tag,String msgTag, byte[] msg,int length){ + String msgStr = ByteUtils.bytesToHexString(msg,length); + return v(tag,msgTag + ":\r\n" + msgStr); + } + + public static int i(String tag,byte[] msg,Throwable tr){ + String msgStr = ByteUtils.bytesToHexString(msg); + return i(tag,msgStr,tr); + } + + public static int i(String tag,String msgTag,byte[] msg,Throwable tr){ + String msgStr = ByteUtils.bytesToHexString(msg); + return i(tag,msgTag + ":\r\n" + msgStr,tr); + } + + public static int w(String tag, String msg) { + if(WARN_LEVER > lever){ + if(logToLogcat){ + Log.w(tag,msg); + } + inputToFile("(V):" + msg); + } + return -1; + } + + public static int w(String tag, String msg, Throwable tr) { + if(WARN_LEVER > lever){ + if(logToLogcat){ + Log.w(tag,msg,tr); + } + inputToFile("(W):" + tag + " " + msg + Log.getStackTraceString(tr)); + } + return -1; + } + + public static boolean isLoggable(String s, int i){ + return Log.isLoggable(s,i); + } + + public static int w(String tag, Throwable tr) { + if(WARN_LEVER > lever){ + if(logToLogcat){ + Log.w(tag,tr); + } + inputToFile("(W):" + tag + " " + Log.getStackTraceString(tr)); + } + return -1; + } + + public static int e(String tag, String msg) { + if(ERROR_LEVER > lever){ + if(logToLogcat){ + Log.e(tag,msg); + } + inputToFile("(E):" + tag + " " + msg); + } + return -1; + } + + public static int e(String tag,Throwable tr){ + String message = "ERROR" ; + if(tr != null && tr.getMessage() != null){ + message = tr.getMessage(); + } + return e(tag,message,tr); + } + + public static int e(String tag, String msg, Throwable tr) { + if(ERROR_LEVER > lever){ + if(logToLogcat){ + Log.e(tag,msg,tr); + } + inputToFile("(E):" + tag + " " + msg + Log.getStackTraceString(tr)); + } + return -1; + } + + public static int wtf(String tag, String msg) { + if(ASSERT_LEVER > lever){ + if(logToLogcat){ + Log.wtf(tag,msg); + } + inputToFile("(WTF):" + tag + " " + msg); + } + return -1; + } + + public static int wtf(String tag, Throwable tr) { + if(ASSERT_LEVER > lever){ + if(logToLogcat){ + Log.wtf(tag,tr); + } + inputToFile("(WTF):" + tag + " " + Log.getStackTraceString(tr)); + } + return -1; + } + + public static int wtf(String tag, String msg, Throwable tr) { + if(ASSERT_LEVER > lever){ + if(logToLogcat){ + Log.wtf(tag,msg,tr); + } + inputToFile("(WTF):" + tag + " " + msg + Log.getStackTraceString(tr)); + } + return -1; + } + + public static String getStackTraceString(Throwable tr) { + return Log.getStackTraceString(tr); + } + + + public static void stack(){ + Throwable throwable = new Throwable(); + // 需要处理TAG 要读出上面class method的信息,后续添上 + i("STACK",getStackTraceString(throwable)); + } + + private synchronized static void inputToFile(String msg){ + if(logToFile) { + String time = simpleDateFormat.format(new Date()); + try { + init(); + String log = time + "(" + Thread.currentThread().getName() + ",id=" + Thread.currentThread().getId() + ")" + msg + "\r\n"; + if(onLogMessage != null){ + onLogMessage.onLog(time + ":" + msg + "\r\n"); + } + fileWriter.write(log); + fileWriter.flush(); + } catch (Exception ex) { + ex.printStackTrace(); + if(fileWriter != null){ + try { + fileWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + fileWriter = null; + } + } + } + + private static OnLogMessage onLogMessage; + + public static void setOnLogMessage(OnLogMessage onLogMessage) { + LogUtil.onLogMessage = onLogMessage; + } + + public static interface OnLogMessage{ + void onLog(String log); + } +} diff --git a/C5/xpadprotocal/build/libs/xpadprotocal.jar b/C5/xpadprotocal/build/libs/xpadprotocal.jar index d4ff4a9..9993d50 100644 --- a/C5/xpadprotocal/build/libs/xpadprotocal.jar +++ b/C5/xpadprotocal/build/libs/xpadprotocal.jar