use of com.easemob.chat.EMMessage in project wechat by motianhuo.
the class ChatActivity method sendVoice.
/**
* 发送语音
*
* @param filePath
* @param fileName
* @param length
* @param isResend
*/
private void sendVoice(String filePath, String fileName, String length, boolean isResend) {
if (!(new File(filePath).exists())) {
return;
}
try {
final EMMessage message = EMMessage.createSendMessage(EMMessage.Type.VOICE);
// 如果是群聊,设置chattype,默认是单聊
if (chatType == CHATTYPE_GROUP)
message.setChatType(ChatType.GroupChat);
message.setReceipt(toChatUsername);
int len = Integer.parseInt(length);
VoiceMessageBody body = new VoiceMessageBody(new File(filePath), len);
message.addBody(body);
conversation.addMessage(message);
adapter.refresh();
listView.setSelection(listView.getCount() - 1);
setResult(RESULT_OK);
// send file
// sendVoiceSub(filePath, fileName, message);
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.easemob.chat.EMMessage in project wechat by motianhuo.
the class ChatActivity method onActivityResult.
/**
* onActivityResult
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
btnContainer.setVisibility(View.GONE);
listView.setSelection(listView.getCount());
if (resultCode == RESULT_CODE_EXIT_GROUP) {
setResult(RESULT_OK);
finish();
return;
}
if (requestCode == REQUEST_CODE_CONTEXT_MENU) {
switch(resultCode) {
case // 复制消息
RESULT_CODE_COPY:
EMMessage copyMsg = ((EMMessage) adapter.getItem(data.getIntExtra("position", -1)));
// clipboard.setText(SmileUtils.getSmiledText(ChatActivity.this,
// ((TextMessageBody) copyMsg.getBody()).getMessage()));
clipboard.setText(((TextMessageBody) copyMsg.getBody()).getMessage());
break;
case // 删除消息
RESULT_CODE_DELETE:
EMMessage deleteMsg = (EMMessage) adapter.getItem(data.getIntExtra("position", -1));
conversation.removeMessage(deleteMsg.getMsgId());
adapter.refresh();
listView.setSelection(data.getIntExtra("position", adapter.getCount()) - 1);
break;
case // 转发消息
RESULT_CODE_FORWARD:
break;
default:
break;
}
}
if (resultCode == RESULT_OK) {
// 清空消息
if (requestCode == REQUEST_CODE_EMPTY_HISTORY) {
// 清空会话
EMChatManager.getInstance().clearConversation(toChatUsername);
adapter.refresh();
} else if (requestCode == REQUEST_CODE_CAMERA) {
// 发送照片
if (cameraFile != null && cameraFile.exists())
sendPicture(cameraFile.getAbsolutePath());
} else if (requestCode == REQUEST_CODE_SELECT_VIDEO) {
// 发送本地选择的视频
int duration = data.getIntExtra("dur", 0);
String videoPath = data.getStringExtra("path");
File file = new File(PathUtil.getInstance().getImagePath(), "thvideo" + System.currentTimeMillis());
Bitmap bitmap = null;
FileOutputStream fos = null;
try {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, 3);
if (bitmap == null) {
EMLog.d("chatactivity", "problem load video thumbnail bitmap,use default icon");
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.app_panel_video_icon);
}
fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
fos = null;
}
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
}
sendVideo(videoPath, file.getAbsolutePath(), duration / 1000);
} else if (requestCode == REQUEST_CODE_LOCAL) {
// 发送本地图片
if (data != null) {
Uri selectedImage = data.getData();
if (selectedImage != null) {
sendPicByUri(selectedImage);
}
}
} else if (requestCode == REQUEST_CODE_SELECT_FILE) {
// 发送选择的文件
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
sendFile(uri);
}
}
} else if (requestCode == REQUEST_CODE_MAP) {
// 地图
double latitude = data.getDoubleExtra("latitude", 0);
double longitude = data.getDoubleExtra("longitude", 0);
String locationAddress = data.getStringExtra("address");
if (locationAddress != null && !locationAddress.equals("")) {
more(more);
sendLocationMsg(latitude, longitude, "", locationAddress);
} else {
String st = getResources().getString(R.string.unable_to_get_loaction);
Toast.makeText(this, st, 0).show();
}
// 重发消息
} else if (requestCode == REQUEST_CODE_TEXT || requestCode == REQUEST_CODE_VOICE || requestCode == REQUEST_CODE_PICTURE || requestCode == REQUEST_CODE_LOCATION || requestCode == REQUEST_CODE_VIDEO || requestCode == REQUEST_CODE_FILE) {
resendMessage();
} else if (requestCode == REQUEST_CODE_COPY_AND_PASTE) {
// 粘贴
if (!TextUtils.isEmpty(clipboard.getText())) {
String pasteText = clipboard.getText().toString();
if (pasteText.startsWith(COPY_IMAGE)) {
// 把图片前缀去掉,还原成正常的path
sendPicture(pasteText.replace(COPY_IMAGE, ""));
}
}
} else if (requestCode == REQUEST_CODE_ADD_TO_BLACKLIST) {
// 移入黑名单
EMMessage deleteMsg = (EMMessage) adapter.getItem(data.getIntExtra("position", -1));
addUserToBlacklist(deleteMsg.getFrom());
} else if (conversation.getMsgCount() > 0) {
adapter.refresh();
setResult(RESULT_OK);
} else if (requestCode == REQUEST_CODE_GROUP_DETAIL) {
adapter.refresh();
}
}
}
use of com.easemob.chat.EMMessage in project wechat by motianhuo.
the class MessageAdapter method getView.
@SuppressLint("NewApi")
public View getView(final int position, View convertView, ViewGroup parent) {
final EMMessage message = getItem(position);
ChatType chatType = message.getChatType();
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = createViewByMessage(message, position);
if (message.getType() == EMMessage.Type.IMAGE) {
try {
holder.iv = ((ImageView) convertView.findViewById(R.id.iv_sendPicture));
holder.head_iv = (ImageView) convertView.findViewById(R.id.iv_userhead);
holder.tv = (TextView) convertView.findViewById(R.id.percentage);
holder.pb = (ProgressBar) convertView.findViewById(R.id.progressBar);
holder.staus_iv = (ImageView) convertView.findViewById(R.id.msg_status);
holder.tv_userId = (TextView) convertView.findViewById(R.id.tv_userid);
} catch (Exception e) {
}
} else if (message.getType() == EMMessage.Type.TXT) {
try {
holder.pb = (ProgressBar) convertView.findViewById(R.id.pb_sending);
holder.staus_iv = (ImageView) convertView.findViewById(R.id.msg_status);
holder.head_iv = (ImageView) convertView.findViewById(R.id.iv_userhead);
// 这里是文字内容
holder.tv = (TextView) convertView.findViewById(R.id.tv_chatcontent);
holder.tv_userId = (TextView) convertView.findViewById(R.id.tv_userid);
} catch (Exception e) {
}
// 语音通话及视频通话
if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false) || message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VIDEO_CALL, false)) {
holder.iv = (ImageView) convertView.findViewById(R.id.iv_call_icon);
holder.tv = (TextView) convertView.findViewById(R.id.tv_chatcontent);
}
} else if (message.getType() == EMMessage.Type.VOICE) {
try {
holder.iv = ((ImageView) convertView.findViewById(R.id.iv_voice));
holder.head_iv = (ImageView) convertView.findViewById(R.id.iv_userhead);
holder.tv = (TextView) convertView.findViewById(R.id.tv_length);
holder.pb = (ProgressBar) convertView.findViewById(R.id.pb_sending);
holder.staus_iv = (ImageView) convertView.findViewById(R.id.msg_status);
holder.tv_userId = (TextView) convertView.findViewById(R.id.tv_userid);
holder.iv_read_status = (ImageView) convertView.findViewById(R.id.iv_unread_voice);
} catch (Exception e) {
}
} else if (message.getType() == EMMessage.Type.LOCATION) {
try {
holder.head_iv = (ImageView) convertView.findViewById(R.id.iv_userhead);
holder.tv = (TextView) convertView.findViewById(R.id.tv_location);
holder.pb = (ProgressBar) convertView.findViewById(R.id.pb_sending);
holder.staus_iv = (ImageView) convertView.findViewById(R.id.msg_status);
holder.tv_userId = (TextView) convertView.findViewById(R.id.tv_userid);
} catch (Exception e) {
}
} else if (message.getType() == EMMessage.Type.VIDEO) {
try {
holder.iv = ((ImageView) convertView.findViewById(R.id.chatting_content_iv));
holder.head_iv = (ImageView) convertView.findViewById(R.id.iv_userhead);
holder.tv = (TextView) convertView.findViewById(R.id.percentage);
holder.pb = (ProgressBar) convertView.findViewById(R.id.progressBar);
holder.staus_iv = (ImageView) convertView.findViewById(R.id.msg_status);
holder.size = (TextView) convertView.findViewById(R.id.chatting_size_iv);
holder.timeLength = (TextView) convertView.findViewById(R.id.chatting_length_iv);
holder.playBtn = (ImageView) convertView.findViewById(R.id.chatting_status_btn);
holder.container_status_btn = (LinearLayout) convertView.findViewById(R.id.container_status_btn);
holder.tv_userId = (TextView) convertView.findViewById(R.id.tv_userid);
} catch (Exception e) {
}
} else if (message.getType() == EMMessage.Type.FILE) {
try {
holder.head_iv = (ImageView) convertView.findViewById(R.id.iv_userhead);
holder.tv_file_name = (TextView) convertView.findViewById(R.id.tv_file_name);
holder.tv_file_size = (TextView) convertView.findViewById(R.id.tv_file_size);
holder.pb = (ProgressBar) convertView.findViewById(R.id.pb_sending);
holder.staus_iv = (ImageView) convertView.findViewById(R.id.msg_status);
holder.tv_file_download_state = (TextView) convertView.findViewById(R.id.tv_file_state);
holder.ll_container = (LinearLayout) convertView.findViewById(R.id.ll_file_container);
// 这里是进度值
holder.tv = (TextView) convertView.findViewById(R.id.percentage);
} catch (Exception e) {
}
try {
holder.tv_userId = (TextView) convertView.findViewById(R.id.tv_userid);
} catch (Exception e) {
}
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// 群聊时,显示接收的消息的发送人的名称
if (chatType == ChatType.GroupChat && message.direct == EMMessage.Direct.RECEIVE) {
// juns 好友名字
// User user = GloableParams.Users.get(message.getFrom());
// holder.tv_userId.setText(user.getUserName());
}
// 如果是发送的消息并且不是群聊消息,显示已读textview
if (message.direct == EMMessage.Direct.SEND && chatType != ChatType.GroupChat) {
holder.tv_ack = (TextView) convertView.findViewById(R.id.tv_ack);
holder.tv_delivered = (TextView) convertView.findViewById(R.id.tv_delivered);
if (holder.tv_ack != null) {
if (message.isAcked) {
if (holder.tv_delivered != null) {
holder.tv_delivered.setVisibility(View.INVISIBLE);
}
holder.tv_ack.setVisibility(View.VISIBLE);
} else {
holder.tv_ack.setVisibility(View.INVISIBLE);
// check and display msg delivered ack status
if (holder.tv_delivered != null) {
if (message.isDelivered) {
holder.tv_delivered.setVisibility(View.VISIBLE);
} else {
holder.tv_delivered.setVisibility(View.INVISIBLE);
}
}
}
}
} else {
// 如果是文本或者地图消息并且不是group messgae,显示的时候给对方发送已读回执
if ((message.getType() == Type.TXT || message.getType() == Type.LOCATION) && !message.isAcked && chatType != ChatType.GroupChat) {
// 不是语音通话记录
if (!message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false)) {
try {
EMChatManager.getInstance().ackMessageRead(message.getFrom(), message.getMsgId());
// 发送已读回执
message.isAcked = true;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
switch(message.getType()) {
// 根据消息type显示item
case // 图片
IMAGE:
handleImageMessage(message, holder, position, convertView);
break;
case // 文本
TXT:
if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false) || message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VIDEO_CALL, false))
// 音视频通话
handleCallMessage(message, holder, position);
else
handleTextMessage(message, holder, position);
break;
case // 位置
LOCATION:
handleLocationMessage(message, holder, position, convertView);
break;
case // 语音
VOICE:
handleVoiceMessage(message, holder, position, convertView);
break;
case // 视频
VIDEO:
handleVideoMessage(message, holder, position, convertView);
break;
case // 一般文件
FILE:
handleFileMessage(message, holder, position, convertView);
break;
default:
}
if (message.direct == EMMessage.Direct.SEND) {
View statusView = convertView.findViewById(R.id.msg_status);
// 重发按钮点击事件
statusView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 显示重发消息的自定义alertdialog
Intent intent = new Intent(activity, AlertDialog.class);
intent.putExtra("msg", activity.getString(R.string.confirm_resend));
intent.putExtra("title", activity.getString(R.string.resend));
intent.putExtra("cancel", true);
intent.putExtra("position", position);
if (message.getType() == EMMessage.Type.TXT)
activity.startActivityForResult(intent, ChatActivity.REQUEST_CODE_TEXT);
else if (message.getType() == EMMessage.Type.VOICE)
activity.startActivityForResult(intent, ChatActivity.REQUEST_CODE_VOICE);
else if (message.getType() == EMMessage.Type.IMAGE)
activity.startActivityForResult(intent, ChatActivity.REQUEST_CODE_PICTURE);
else if (message.getType() == EMMessage.Type.LOCATION)
activity.startActivityForResult(intent, ChatActivity.REQUEST_CODE_LOCATION);
else if (message.getType() == EMMessage.Type.FILE)
activity.startActivityForResult(intent, ChatActivity.REQUEST_CODE_FILE);
else if (message.getType() == EMMessage.Type.VIDEO)
activity.startActivityForResult(intent, ChatActivity.REQUEST_CODE_VIDEO);
}
});
} else {
final String st = context.getResources().getString(R.string.Into_the_blacklist);
// 长按头像,移入黑名单
holder.head_iv.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Intent intent = new Intent(activity, AlertDialog.class);
intent.putExtra("msg", st);
intent.putExtra("cancel", true);
intent.putExtra("position", position);
activity.startActivityForResult(intent, ChatActivity.REQUEST_CODE_ADD_TO_BLACKLIST);
return true;
}
});
}
TextView timestamp = (TextView) convertView.findViewById(R.id.timestamp);
if (position == 0) {
timestamp.setText(DateUtils.getTimestampString(new Date(message.getMsgTime())));
timestamp.setVisibility(View.VISIBLE);
} else {
// 两条消息时间离得如果稍长,显示时间
if (DateUtils.isCloseEnough(message.getMsgTime(), conversation.getMessage(position - 1).getMsgTime())) {
timestamp.setVisibility(View.GONE);
} else {
timestamp.setText(DateUtils.getTimestampString(new Date(message.getMsgTime())));
timestamp.setVisibility(View.VISIBLE);
}
}
return convertView;
}
Aggregations