parse tkone cmd data.
authorTrillGates <williamtrillgates@gmail.com>
Tue, 6 Apr 2021 02:29:53 +0000 (10:29 +0800)
committerTrillGates <williamtrillgates@gmail.com>
Tue, 6 Apr 2021 02:29:53 +0000 (10:29 +0800)
app/src/main/java/com/enqualcomm/support/provider/FZDDatabaseHelper.java
app/src/main/java/com/ic/protocols/protocol_fzd.java

index a60554f..59f34f3 100644 (file)
@@ -26,6 +26,28 @@ public class FZDDatabaseHelper extends SQLiteOpenHelper {
     public static final String fCreateTime = "create_time";
     public static final String fUpdateTime = "update_time";
 
+
+    public interface MessageType {
+        int VOICE = 1;
+        int TEXT = 2;
+        int PIC = 3;
+    }
+
+    public interface MessageState {
+        int UNREAD = 0;
+        int READ = 1;
+        int NO_SEND = 2;
+        int SENDING = 3;
+        int SEND_FAILED = 4;
+        int SEND_SUCCESS = 5;
+    }
+
+    public interface InOrOut {
+        int in = 1;
+        int out = 2;
+    }
+
+
     //语音相关的表,字段有:id:ID,content:内容,type:类型,1表示语音,2表示文本,3表示图片
     //状态:应用端去处理(0表示未读,1表示已读,2表示未发送)协议处理的地方去修改(3表示发送中,4表示发送失败,5表示发送成功)
     // in_or_out:1表示来信,2表示发出去的内容,remote_num:对方的手机号码
index 9f9aaa6..eaa0e99 100644 (file)
@@ -20,6 +20,7 @@ import com.enqualcomm.support.bean.HealthInfo;
 import com.enqualcomm.support.bean.Runtime;
 import com.enqualcomm.support.bean.Silent;
 import com.enqualcomm.support.bean.StringMessage;
+import com.enqualcomm.support.provider.FZDDatabaseHelper;
 import com.enqualcomm.support.provider.FZDMembersProvider;
 import com.enqualcomm.support.provider.FZDMessageProvider;
 import com.enqualcomm.support.service.HeartRateManager;
@@ -38,6 +39,7 @@ import java.util.Date;
 import java.util.HashMap;
 
 import static com.enqualcomm.support.Constants.FROM_SERVER;
+import static com.enqualcomm.support.Constants.contact;
 import static com.enqualcomm.support.LocationClient.LOCATION_MODE;
 import static java.lang.Math.abs;
 
@@ -181,6 +183,7 @@ public class protocol_fzd {
         FIND,//找手表指令
         REMIND,//闹钟设置指令
         TK,//对讲功能(微聊发送语音)
+        TKOne,//一对一微聊
         PHL,//设置电话本+白名单(最多 50 条)
         wdstart,//远程测温
         bldstart,//远程测血压
@@ -387,6 +390,8 @@ public class protocol_fzd {
                     ICLogger.d("key is == > " + key);
                     if (key == CMD_TYPE.TK) {
                         handlerGroupChatVoiceContent(totalByte);
+                    } else if (key == CMD_TYPE.TKOne) {
+                        handlerOneChatVoiceContent(totalByte);
                     } else {
                         String data = content.replace(CMDHEADER, "").replace(cmd, "").replace(CMDEND, "");
                         if (data.startsWith(CMDSPLIT)) {
@@ -401,23 +406,47 @@ public class protocol_fzd {
         return true;
     }
 
-    private static byte[] byteMerger(byte[]... byteList) {
-        int lengthByte = 0;
-        //求出总的长度
-        for (int i = 0; i < byteList.length; i++) {
-            lengthByte += byteList[i].length;
-        }
-        //创建总的数组
-        byte[] allByte = new byte[lengthByte];
-        int countLength = 0;
-        for (int i = 0; i < byteList.length; i++) {
-            byte[] b = byteList[i];
-            System.arraycopy(b, 0, allByte, countLength, b.length);
-            countLength += b.length;
+    /**
+     * 一对一微聊
+     *
+     * @param totalByte
+     */
+    private void handlerOneChatVoiceContent(byte[] totalByte) {
+        try {
+            //对数据进行处理
+            //数据格式:[XT*YYYYYYYYYYYYYYY*LEN*TKOne,App 账号,秒数,AMR 格式音频数据]
+            String cmdStr = new String(totalByte);
+            String[] split = cmdStr.split(",");
+            String header = split[0];
+            //账号
+            String count = split[1];
+            //秒数
+            String duration = split[2];
+            //数据包,对数据进行切割
+            byte[] dataPackage = Util.subArray(totalByte, header.length() + contact.length() + duration.length() + 3,
+                    totalByte.length - 1);
+            //对数据进行转换
+            String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/Android/VoiceCache";
+            byte[] result = Util.fzdTransfer(dataPackage, false);
+            String amrFilepath = Util.saveFile(result,
+                    0, result.length,
+                    new File(dir),
+                    "receive_" + System.currentTimeMillis() + ".amr");
+            //插入到数据库中
+            voiceIn2Db(amrFilepath, FZDDatabaseHelper.MessageType.VOICE);
+            //返回结果
+            mConnection.send(TKOne("1"));
+        } catch (Exception e) {
+            e.printStackTrace();
+            mConnection.send(TKOne("0"));
         }
-        return allByte;
     }
 
+    /**
+     * 群聊
+     *
+     * @param bytes
+     */
     private void handlerGroupChatVoiceContent(byte[] bytes) {
         try {
             ICLogger.d("voice length == > " + bytes.length);
@@ -429,6 +458,7 @@ public class protocol_fzd {
                 //更新发送状态
                 String value = cmdStr.substring(cmdLength, cmdLength + 1);
                 ICLogger.d("value is == > " + cmdLength);
+                String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/Android/VoiceCache";
                 if ("1".equals(value)) {
                     //修改为发送成功
                     //不知道ID,所以无法修改消息的状态
@@ -459,21 +489,7 @@ public class protocol_fzd {
             //    ICLogger.d("playVoice: ERROR ");
             //}
             //包存到数据库中,这是群聊的内容,没有联系人的
-            ContentResolver resolver = mRuntime.getContextProxy().getResolver();
-            ContentValues contentValues = new ContentValues();
-            //路径
-            contentValues.put("content", amrFilepath);
-            //类型为声音
-            contentValues.put("type", 1);
-            //状态:0表示未读
-            contentValues.put("state", 0);
-            //接受还是发送,1表示来信
-            contentValues.put("in_or_out", 1);
-            //对方号码是没有的
-            contentValues.put("remote_num", "");
-            contentValues.put("create_time", System.currentTimeMillis());
-            contentValues.put("update_time", System.currentTimeMillis());
-            resolver.insert(Uri.parse(FZDMessageProvider.MESSAGES), contentValues);
+            voiceIn2Db(amrFilepath, FZDDatabaseHelper.MessageType.VOICE);
             //回复服务器状态
             mConnection.send(TK("1"));
             //通知有消息到
@@ -485,6 +501,41 @@ public class protocol_fzd {
         }
     }
 
+    private void voiceIn2Db(String amrFilepath, int type) {
+        ContentResolver resolver = mRuntime.getContextProxy().getResolver();
+        ContentValues contentValues = new ContentValues();
+        //路径
+        contentValues.put("content", amrFilepath);
+        //类型为声音
+        contentValues.put("type", type);
+        //状态:0表示未读
+        contentValues.put("state", 0);
+        //接受还是发送,1表示来信
+        contentValues.put("in_or_out", 1);
+        //对方号码是没有的
+        contentValues.put("remote_num", "");
+        contentValues.put("create_time", System.currentTimeMillis());
+        contentValues.put("update_time", System.currentTimeMillis());
+        resolver.insert(Uri.parse(FZDMessageProvider.MESSAGES), contentValues);
+    }
+
+    private static byte[] byteMerger(byte[]... byteList) {
+        int lengthByte = 0;
+        //求出总的长度
+        for (int i = 0; i < byteList.length; i++) {
+            lengthByte += byteList[i].length;
+        }
+        //创建总的数组
+        byte[] allByte = new byte[lengthByte];
+        int countLength = 0;
+        for (int i = 0; i < byteList.length; i++) {
+            byte[] b = byteList[i];
+            System.arraycopy(b, 0, allByte, countLength, b.length);
+            countLength += b.length;
+        }
+        return allByte;
+    }
+
     private static String getHexLen(int len) {
         StringBuilder sb = new StringBuilder();
         int v = len & 0xFFFF;
@@ -1122,26 +1173,15 @@ public class protocol_fzd {
         //解析数据
         String textData = Util.decodeUnicode2Utf8(data);
         ICLogger.d("message data == > " + textData);
-        ContentResolver resolver = mRuntime.getContextProxy().getResolver();
-        ContentValues values = new ContentValues();
-        //路径
-        values.put("content", textData);
-        //类型为2表示的是文本
-        values.put("type", 2);
-        //状态:0表示未读
-        values.put("state", 0);
-        //接受还是发送,1表示来信
-        values.put("in_or_out", 1);
-        //对方号码是没有的
-        values.put("remote_num", "");
-        values.put("create_time", System.currentTimeMillis());
-        values.put("update_time", System.currentTimeMillis());
-        //字段:内容,类型,来源,分类,是否已读,联系人ID
-        resolver.insert(Uri.parse(FZDMessageProvider.MESSAGES), values);
+        voiceIn2Db(textData, FZDDatabaseHelper.MessageType.TEXT);
         //入数据库
         mConnection.send(MESSAGE());
     }
 
+    private StringMessage TKOne(String result) {
+        return genFZDCMD(cmdMap.get(CMD_TYPE.TKOne), result);
+    }
+
     private StringMessage TK(String result) {
         return genFZDCMD(cmdMap.get(CMD_TYPE.TK), result);
     }