public static long sendImageMessage(ImgInfo imgInfo, ECMessage message) { ECChatManager manager = getInstance().mChatManager; if (manager != null) { // 调用接口发送IM消息 manager.sendMessage(message, getInstance().mListener); if (TextUtils.isEmpty(message.getMsgId())) { return -1; } imgInfo.setMsglocalid(message.getMsgId()); BitmapFactory.Options options = DemoUtils.getBitmapOptions( new File(FileAccessor.IMESSAGE_IMAGE, imgInfo.getThumbImgPath()).getAbsolutePath()); message.setUserData( "outWidth://" + options.outWidth + ",outHeight://" + options.outHeight + ",THUMBNAIL://" + message.getMsgId() + ",PICGIF://" + imgInfo.isGif); long row = IMessageSqlManager.insertIMessage(message, ECMessage.Direction.SEND.ordinal()); if (row != -1) { return ImgInfoSqlManager.getInstance().insertImageInfo(imgInfo); } } return -1; }
private synchronized void postReceiveMessage(ECMessage ecMessage, boolean showNotice) { int userId = Integer.parseInt(ecMessage.getSessionId()); Friend friend = FriendCache.getInstance(mContext).getFriend(userId); String message = ((ECTextMessageBody) ecMessage.getBody()).getMessage(); History history = new History(message, userId, mContext); HistoryCache.getInstance(mContext).updateHistory(history, friend); ConversationCache.getInstance() .updateConversation( new Conversation( userId, String.format(mContext.getString(R.string.send_bomb_message), history.getTime()))); if (history.getStatus() == History.accepted) { AlarmSetManager.setAlarm(mContext, history, friend); } else if (history.getStatus() == History.failed) { AlarmHelper.getInstance(mContext).stopAudio(); } LogUtil.d(TAG, ecMessage.getBody().toString()); if (mOnMessageReportCallback != null) { ArrayList<ECMessage> messages = new ArrayList<>(); messages.add(ecMessage); mOnMessageReportCallback.onPushMessage(); } // 是否状态栏提示 if (showNotice) showNotification(ecMessage); }
/** * 发送ECMessage 消息 * * @param msg */ public static long sendECMessage(ECMessage msg) { getInstance().checkChatManager(); // 获取一个聊天管理器 ECChatManager manager = getInstance().mChatManager; if (manager != null) { // 调用接口发送IM消息 msg.setMsgTime(System.currentTimeMillis()); manager.sendMessage(msg, getInstance().mListener); // 保存发送的消息到数据库 } else { msg.setMsgStatus(ECMessage.MessageStatus.FAILED); } return IMessageSqlManager.insertIMessage(msg, ECMessage.Direction.SEND.ordinal()); }
private void showNotification(ECMessage ecMessage) { CallUpNotificationManager.getInstance().forceCancelNotification(); int userId = Integer.parseInt(ecMessage.getForm()); Friend friend = FriendCache.getInstance(mContext).getFriend(userId); if (friend == null) { return; } CallUpNotificationManager.getInstance() .showCustomNewMessageNotification( BaseInfoUtil.getContext(), friend, ecMessage.getSessionId(), ecMessage.getType().ordinal()); }
private void postCheckDownFailMsg() { List<ECMessage> downdFailMsg = IMessageSqlManager.getDowndFailMsg(); if (downdFailMsg == null || downdFailMsg.isEmpty()) { return; } for (ECMessage msg : downdFailMsg) { ECImageMessageBody body = (ECImageMessageBody) msg.getBody(); body.setThumbnailFileUrl(body.getRemoteUrl() + "_thum"); if (syncMessage != null) { syncMessage.put(msg.getMsgId(), new SyncMsgEntry(false, true, msg)); } if (mChatManager != null) { mChatManager.downloadThumbnailMessage(msg, this); } } }
/** 下载 */ @Override public void onDownloadMessageComplete(ECError e, ECMessage message) { if (e.errorCode == SdkErrorCode.REQUEST_SUCCESS) { if (message == null) return; // 处理发送文件IM消息的时候进度回调 LogUtil.d(TAG, "[onDownloadMessageComplete] msgId:" + message.getMsgId()); postDowloadMessageResult(message); } else { // 重试下载3次 SyncMsgEntry remove = syncMessage.remove(message.getMsgId()); if (remove == null) { return; } LogUtil.d(TAG, "[onDownloadMessageComplete] download fail , retry :" + remove.retryCount); retryDownload(remove); } }
/** * 消息重发 * * @param msg * @return */ public static long reSendECMessage(ECMessage msg) { ECChatManager manager = getInstance().mChatManager; if (manager != null) { // 调用接口发送IM消息 String oldMsgId = msg.getMsgId(); manager.sendMessage(msg, getInstance().mListener); if (msg.getType() == ECMessage.Type.IMAGE) { ImgInfo imgInfo = ImgInfoSqlManager.getInstance().getImgInfo(oldMsgId); if (imgInfo == null || TextUtils.isEmpty(imgInfo.getBigImgPath())) { return -1; } String bigImagePath = new File(FileAccessor.getImagePathName(), imgInfo.getBigImgPath()).getAbsolutePath(); imgInfo.setMsglocalid(msg.getMsgId()); ECFileMessageBody body = (ECFileMessageBody) msg.getBody(); body.setLocalUrl(bigImagePath); BitmapFactory.Options options = DemoUtils.getBitmapOptions( new File(FileAccessor.IMESSAGE_IMAGE, imgInfo.getThumbImgPath()).getAbsolutePath()); msg.setUserData( "outWidth://" + options.outWidth + ",outHeight://" + options.outHeight + ",THUMBNAIL://" + msg.getMsgId()); ImgInfoSqlManager.getInstance().updateImageInfo(imgInfo); } // 保存发送的消息到数据库 return IMessageSqlManager.changeResendMsg(msg.getId(), msg); } return -1; }
@Override public void buildChattingData( Context context, BaseHolder baseHolder, final ECMessage detail, int position) { final VoiceRowViewHolder holder = (VoiceRowViewHolder) baseHolder; holder.voiceAnim.setVoiceFrom(false); if (detail != null) { if (detail.getMsgStatus() == ECMessage.MessageStatus.SENDING) { holder.voiceSending.setVisibility(View.VISIBLE); } else { holder.voiceSending.setVisibility(View.GONE); } File file = new File(((ECFileMessageBody) detail.getBody()).getLocalUrl()); long length = file.length(); VoiceRowViewHolder.initVoiceRow(holder, detail, position, (ChattingActivity) context, false); OnClickListener onClickListener = ((ChattingActivity) context).mChattingFragment.getChattingAdapter().getOnClickListener(); getMsgStateResId(position, holder, detail, onClickListener); } }
@Override public void onSendMessageComplete(ECError error, ECMessage message) { if (message == null) { return; } // 处理ECMessage的发送状态 if (message != null) { if (message.getType() == ECMessage.Type.VOICE) { try { DemoUtils.playNotifycationMusic( CCPAppManager.getContext(), "sound/voice_message_sent.mp3"); } catch (IOException e) { e.printStackTrace(); } } IMessageSqlManager.setIMessageSendStatus( message.getMsgId(), message.getMsgStatus().ordinal()); IMessageSqlManager.notifyMsgChanged(message.getSessionId()); if (mOnMessageReportCallback != null) { mOnMessageReportCallback.onMessageReport(error, message); } return; } }
public void sendMessage(final Friend friend, History history) { try { ECMessage msg = ECMessage.createECMessage(ECMessage.Type.TXT); msg.setForm(UserCache.getInstance().getClientUser().getUserId() + ""); msg.setMsgTime(System.currentTimeMillis()); msg.setTo(friend.getUserId() + ""); msg.setSessionId(friend.getUserId() + ""); msg.setDirection(ECMessage.Direction.SEND); String content = history.getScene().getSceneId() + "/" + history.getTime() + "/" + history.getStatus(); ECTextMessageBody msgBody = new ECTextMessageBody(content); LogUtil.d(TAG, "The content is " + content); msg.setBody(msgBody); ECChatManager manager = ECDevice.getECChatManager(); manager.sendMessage(msg, mOnSendMessageListener); } catch (Exception e) { LogUtil.e(TAG, "send message fail , e=" + e.getMessage()); } }
private synchronized void postDowloadMessageResult(ECMessage message) { if (message == null) { return; } if (message.getType() == ECMessage.Type.VOICE) { ECVoiceMessageBody voiceBody = (ECVoiceMessageBody) message.getBody(); voiceBody.setDuration(DemoUtils.calculateVoiceTime(voiceBody.getLocalUrl())); } else if (message.getType() == ECMessage.Type.IMAGE) { ImgInfo thumbImgInfo = ImgInfoSqlManager.getInstance().getThumbImgInfo(message); if (thumbImgInfo == null) { return; } ImgInfoSqlManager.getInstance().insertImageInfo(thumbImgInfo); BitmapFactory.Options options = DemoUtils.getBitmapOptions( new File(FileAccessor.getImagePathName(), thumbImgInfo.getThumbImgPath()) .getAbsolutePath()); message.setUserData( "outWidth://" + options.outWidth + ",outHeight://" + options.outHeight + ",THUMBNAIL://" + message.getMsgId() + ",PICGIF://" + thumbImgInfo.isGif); } if (IMessageSqlManager.updateIMessageDownload(message) <= 0) { return; } if (mOnMessageReportCallback != null) { mOnMessageReportCallback.onMessageReport(null, message); } boolean showNotice = true; SyncMsgEntry remove = syncMessage.remove(message.getMsgId()); if (remove != null) { showNotice = remove.showNotice; if (mOnMessageReportCallback != null && remove.msg != null) { ArrayList<ECMessage> msgs = new ArrayList<ECMessage>(); msgs.add(remove.msg); mOnMessageReportCallback.onPushMessage(remove.msg.getSessionId(), msgs); } } if (showNotice) showNotification(message); }
private static void showNotification(ECMessage msg) { if (checkNeedNotification(msg.getSessionId())) { ECNotificationManager.getInstance().forceCancelNotification(); String lastMsg = ""; if (msg.getType() == ECMessage.Type.TXT) { lastMsg = ((ECTextMessageBody) msg.getBody()).getMessage(); } ECContacts contact = ContactSqlManager.getContact(msg.getForm()); if (contact == null) { return; } ECNotificationManager.getInstance() .showCustomNewMessageNotification( CCPAppManager.getContext(), lastMsg, contact.getNickname(), msg.getSessionId(), msg.getType().ordinal()); } }
/** * 处理接收消息 * * @param msg * @param showNotice */ private synchronized void postReceiveMessage(ECMessage msg, boolean showNotice) { // 接收到的IM消息,根据IM消息类型做不同的处理 // IM消息类型:ECMessage.Type if (msg.getType() != ECMessage.Type.TXT) { ECFileMessageBody body = (ECFileMessageBody) msg.getBody(); FileAccessor.initFileAccess(); if (!TextUtils.isEmpty(body.getRemoteUrl())) { boolean thumbnail = false; String fileExt = DemoUtils.getExtensionName(body.getRemoteUrl()); if (msg.getType() == ECMessage.Type.VOICE) { body.setLocalUrl( new File( FileAccessor.getVoicePathName(), DemoUtils.md5(String.valueOf(System.currentTimeMillis())) + ".amr") .getAbsolutePath()); } else if (msg.getType() == ECMessage.Type.IMAGE) { ECImageMessageBody imageBody = (ECImageMessageBody) body; thumbnail = !TextUtils.isEmpty(imageBody.getThumbnailFileUrl()); imageBody.setLocalUrl( new File( FileAccessor.getImagePathName(), DemoUtils.md5( thumbnail ? imageBody.getThumbnailFileUrl() : imageBody.getRemoteUrl()) + "." + fileExt) .getAbsolutePath()); } else { body.setLocalUrl( new File( FileAccessor.getFilePathName(), DemoUtils.md5(String.valueOf(System.currentTimeMillis())) + "." + fileExt) .getAbsolutePath()); } if (syncMessage != null) { syncMessage.put(msg.getMsgId(), new SyncMsgEntry(showNotice, thumbnail, msg)); } if (mChatManager != null) { if (thumbnail) { mChatManager.downloadThumbnailMessage(msg, this); } else { mChatManager.downloadMediaMessage(msg, this); } } if (TextUtils.isEmpty(body.getFileName()) && !TextUtils.isEmpty(body.getRemoteUrl())) { body.setFileName(FileAccessor.getFileName(body.getRemoteUrl())); } msg.setUserData("fileName=" + body.getFileName()); if (IMessageSqlManager.insertIMessage(msg, msg.getDirection().ordinal()) > 0) { return; } } else { LogUtil.e(TAG, "ECMessage fileUrl: null"); } } if (IMessageSqlManager.insertIMessage(msg, msg.getDirection().ordinal()) <= 0) { return; } if (mOnMessageReportCallback != null) { ArrayList<ECMessage> msgs = new ArrayList<ECMessage>(); msgs.add(msg); mOnMessageReportCallback.onPushMessage(msg.getSessionId(), msgs); } // 是否状态栏提示 if (showNotice) showNotification(msg); }