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;
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;
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;
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();
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)+数据
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");
}