临时提交Gator 协议
authorSnickerSun <scy@incartech.cn>
Fri, 2 Jul 2021 14:52:11 +0000 (22:52 +0800)
committerSnickerSun <scy@incartech.cn>
Fri, 2 Jul 2021 14:52:11 +0000 (22:52 +0800)
修复BPxx的发送消息长度计算错误
添加微聊语音接收

app/src/main/java/com/ic/protocols/protocol_gator.java
local.properties

index e462c45..7873343 100644 (file)
@@ -1,6 +1,8 @@
 package com.ic.protocols;
 
 import android.content.ComponentName;
+import android.content.ContentResolver;
+import android.content.ContentValues;
 import android.content.Context;
 import android.content.IClipboard;
 import android.content.Intent;
@@ -23,6 +25,8 @@ import com.enqualcomm.support.bean.Runtime;
 import com.enqualcomm.support.bean.StringMessage;
 import com.enqualcomm.support.bean.VoiceContent;
 import com.enqualcomm.support.bean.voicePacket;
+import com.enqualcomm.support.provider.FZDDatabaseHelper;
+import com.enqualcomm.support.provider.FZDMessageProvider;
 import com.enqualcomm.support.service.HeartRateManager;
 import com.enqualcomm.support.utils.AESCipher;
 import com.enqualcomm.support.utils.CameraUtil;
@@ -40,6 +44,8 @@ import com.ic.view.msg.MsgMustReadDialog;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.RandomAccessFile;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
@@ -260,16 +266,21 @@ public class protocol_gator {
         String encreptedStr="";
         String encreptionMSGHeader="GT";
         encreptedStr=AESCipher.aesEncryptStringFixedKey(msg);
-        String len= String.format("%04x",encreptedStr.length()+8);
+        String len= String.format("%04X",encreptedStr.length()+8);
         return encreptionMSGHeader+len+"01"+encreptedStr;
     }
     //根据协议添加头尾以及IMEI号
     private static String AddHeader(Runtime runtime,String msg){
-        /*(  CHAR    1
-%04X   HEX_STRING      4
-0106   C_STRING        4
-       C_STRING        15*/
-        String len= String.format("%04x",msg.length());
+        /*
+        (    CHAR            1
+        %04X   HEX_STRING      4
+        0106   C_STRING        4
+           C_STRING            15
+           byte[]              msg.lenght
+           )  CHAR             1
+           */
+
+        String len= String.format("%04X",1+4+4+15+msg.length()+1);
         return new StringBuffer(CMDHEADER).append(len).append("0106").append(runtime.IMEI()).append(msg).append(
                 CMDEND).toString();
 
@@ -646,6 +657,87 @@ uncle:13141516175
         return ret;
 
     }
+
+    /**
+     * 获取声音时长,代码来源于网络,单位:秒
+     *
+     * @param file
+     * @return
+     * @throws IOException
+     */
+    public int getAmrDuration(File file) throws IOException {
+        long duration = -1;
+        int[] packedSize = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0,
+                0, 0};
+        RandomAccessFile randomAccessFile = null;
+        try {
+            randomAccessFile = new RandomAccessFile(file, "rw");
+            // 文件的长度
+            long length = file.length();
+            // 设置初始位置
+            int pos = 6;
+            // 初始帧数
+            int frameCount = 0;
+            int packedPos = -1;
+            // 初始数据值
+            byte[] data = new byte[1];
+            while (pos <= length) {
+                randomAccessFile.seek(pos);
+                if (randomAccessFile.read(data, 0, 1) != 1) {
+                    duration = length > 0 ? ((length - 6) / 650) : 0;
+                    break;
+                }
+                packedPos = (data[0] >> 3) & 0x0F;
+                pos += packedSize[packedPos] + 1;
+                frameCount++;
+            }
+            // 帧数*20
+            duration += frameCount * 20;
+        } finally {
+            if (randomAccessFile != null) {
+                randomAccessFile.close();
+            }
+        }
+        return (int) ((duration / 1000) + 1);
+    }
+    private void message2Db(String remoteId, String amrFilepath, int type) {
+//        try {
+//            //通知有消息到
+//            Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
+//            Ringtone ringtone = RingtoneManager.getRingtone(mRuntime.getContextProxy().context, uri);
+//            ringtone.play();
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+        //计算文件时长
+        int amrDuration = 0;
+        if (type == FZDDatabaseHelper.MessageType.VOICE) {
+            try {
+                File file = new File(amrFilepath);
+                amrDuration = getAmrDuration(file);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+        ContentResolver resolver = mRuntime.getContextProxy().getResolver();
+        ContentValues contentValues = new ContentValues();
+        //路径
+        contentValues.put("content", amrFilepath);
+        //类型为声音
+        contentValues.put("type", type);
+        //状态:0表示未读
+        contentValues.put("state", 0);
+        //文件时长
+        contentValues.put("duration", amrDuration);
+        //接受还是发送,1表示来信
+        contentValues.put("in_or_out", 1);
+        //对方号码是没有的
+        contentValues.put("remote_num", remoteId);
+        contentValues.put("create_time", System.currentTimeMillis());
+        contentValues.put("update_time", System.currentTimeMillis());
+        resolver.insert(Uri.parse(FZDMessageProvider.MESSAGES), contentValues);
+    }
+
     //服务器下发微聊消息
     public void handleAP12(Runtime ds,Connection connection,byte[]  bytes) {
 //        AP12,亲情号码,YYMMDDHHMMSSSSSS,分包总数(%d),当分包索引(%d),分包语音数据长度(%03d<=900)+数据
@@ -666,14 +758,38 @@ uncle:13141516175
             String packageslen=cmdlist[5];
             int index=0;
             int count=0;
+            int ipackagelen=0;
             try {
                 index=Integer.parseInt(packageindex);
                 count=Integer.parseInt(packagescount);
+                ipackagelen=Integer.parseInt(packageslen);
             }catch (Exception e){
                 ICLogger.e(e.getMessage(),e);
             }
             ICLogger.d("receive msg from "+sourceNumb+" "+index+" of "+count + " len "+packageslen+" time "+time);
             connection.send(BP12(ds,sourceNumb,count,index,time,true));
+            byte packetData[] = Util.subArray(bytes, cmdlist[0].length() + cmdlist[1].length() + cmdlist[2].length() + cmdlist[3].length() + cmdlist[4].length() + cmdlist[5].length() + 6, bytes.length - 1);
+
+            voicePacket.getInstance().appendVoice(sourceNumb, time, count, index, ipackagelen, packetData);
+            log("================" + voicePacket.getInstance().toString());
+            if (voicePacket.getInstance().isFinished()) {
+
+                String dir = Environment.getExternalStorageDirectory() + "/Android/VoiceCache";
+                String amrFilepath = Util.saveFile(voicePacket.getInstance().getVoicePacketBytes(), 0, voicePacket.getInstance().getVoicePacketLen(), new File(dir), "receive_" + voicePacket.getInstance().getPacketTime() + ".amr");
+                ICLogger.i("save Amr file to " + amrFilepath);
+                voicePacket.getInstance().clearPacket();
+                VoiceContent voiceContent = VoiceContent.placeHolderIn(Content.CONTACT_CATEGORY_GROUP, GROUP_ALL, amrFilepath);
+                final Uri uri2 = ds.getDbHelper().insertMessage(voiceContent);
+                //回复UI消息,表示已经接收到
+                voiceContent.status = 3;
+                voiceContent.serverTime = TimeUtil.getSystemTime3();
+                voiceContent.preview = String.valueOf(Util.getDuration(voiceContent.content));
+                ds.getDbHelper().updateMessage(voiceContent, uri2);
+                //插入到数据库中
+                message2Db("", amrFilepath, FZDDatabaseHelper.MessageType.VOICE);
+//                runtime.getContextProxy().sendBroadcast(ACTION_IMESSAGE, runtime);
+                connection.DeliverCMDMessageToSystem("NewVoice", amrFilepath);//通知下面线程状态变化
+            }
         }else {
             ICLogger.e("AP12 format error");
         }
index 3ff6dd5..c6dcf0c 100644 (file)
@@ -4,5 +4,5 @@
 # Location of the SDK. This is only used by Gradle.\r
 # For customization when using a Version Control System, please read the\r
 # header note.\r
-#Tue Jun 29 20:13:18 CST 2021\r
-sdk.dir=E\:\\Android\\Sdk
\ No newline at end of file
+#Fri Jul 02 14:52:39 CST 2021\r
+sdk.dir=C\:\\Users\\incar\\AppData\\Local\\Android\\Sdk\r