com.easemob.chat.EMMessage.ChatType Java Examples

The following examples show how to use com.easemob.chat.EMMessage.ChatType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EaseChatFragment.java    From monolog-android with MIT License 6 votes vote down vote up
protected void sendMessage(EMMessage message){
    if(chatFragmentListener != null){
        //设置扩展属性
        chatFragmentListener.onSetMessageAttributes(message);
    }
    // 如果是群聊,设置chattype,默认是单聊
    if (chatType == EaseConstant.CHATTYPE_GROUP){
        message.setChatType(ChatType.GroupChat);
    }else if(chatType == EaseConstant.CHATTYPE_CHATROOM){
        message.setChatType(ChatType.ChatRoom);
    }
    //发送消息
    EMChatManager.getInstance().sendMessage(message, null);
    //刷新ui
    messageList.refreshSelectLast();
}
 
Example #2
Source File: LoadImageTask.java    From school_shop with MIT License 6 votes vote down vote up
@Override
protected Bitmap doInBackground(Object... args) {
	thumbnailPath = (String) args[0];
	localFullSizePath = (String) args[1];
	remotePath = (String) args[2];
	chatType = (ChatType) args[3];
	iv = (ImageView) args[4];
	// if(args[2] != null) {
	activity = (Activity) args[5];
	// }
	message = (EMMessage) args[6];
	File file = new File(thumbnailPath);
	if (file.exists()) {
		return ImageUtils.decodeScaleImage(thumbnailPath, 160, 160);
	} else {
		if (message.direct == EMMessage.Direct.SEND) {
			return ImageUtils.decodeScaleImage(localFullSizePath, 160, 160);
		} else {
			return null;
		}
	}
	

}
 
Example #3
Source File: EaseChatRowImage.java    From monolog-android with MIT License 6 votes vote down vote up
@Override
protected void onBubbleClick() {
    Intent intent = new Intent(context, EaseShowBigImageActivity.class);
    File file = new File(imgBody.getLocalUrl());
    if (file.exists()) {
        Uri uri = Uri.fromFile(file);
        intent.putExtra("uri", uri);
    } else {
        // The local full size pic does not exist yet.
        // ShowBigImage needs to download it from the server
        // first
        intent.putExtra("secret", imgBody.getSecret());
        intent.putExtra("remotepath", imgBody.getRemoteUrl());
    }
    if (message != null && message.direct == EMMessage.Direct.RECEIVE && !message.isAcked
            && message.getChatType() != ChatType.GroupChat) {
        try {
            EMChatManager.getInstance().ackMessageRead(message.getFrom(), message.getMsgId());
            message.isAcked = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    context.startActivity(intent);
}
 
Example #4
Source File: EaseChatRowVideo.java    From monolog-android with MIT License 6 votes vote down vote up
@Override
protected void onBubbleClick() {
    VideoMessageBody videoBody = (VideoMessageBody) message.getBody();
       EMLog.d(TAG, "video view is on click");
       Intent intent = new Intent(context, EaseShowVideoActivity.class);
       intent.putExtra("localpath", videoBody.getLocalUrl());
       intent.putExtra("secret", videoBody.getSecret());
       intent.putExtra("remotepath", videoBody.getRemoteUrl());
       if (message != null && message.direct == EMMessage.Direct.RECEIVE && !message.isAcked
               && message.getChatType() != ChatType.GroupChat) {
           message.isAcked = true;
           try {
               EMChatManager.getInstance().ackMessageRead(message.getFrom(), message.getMsgId());
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
       activity.startActivity(intent);
}
 
Example #5
Source File: ChatActivity.java    From school_shop with MIT License 6 votes vote down vote up
/**
 * 发送图片
 * 
 * @param filePath
 */
private void sendPicture(final String filePath) {
	String to = toChatUsername;
	// create and add image message in view
	final EMMessage message = EMMessage.createSendMessage(EMMessage.Type.IMAGE);
	// 如果是群聊,设置chattype,默认是单聊
	if (chatType == CHATTYPE_GROUP)
		message.setChatType(ChatType.GroupChat);

	message.setReceipt(to);
	ImageMessageBody body = new ImageMessageBody(new File(filePath));
	// 默认超过100k的图片会压缩后发给对方,可以设置成发送原图
	// body.setSendOriginalImage(true);
	message.addBody(body);
	conversation.addMessage(message);

	listView.setAdapter(adapter);
	adapter.refreshSelectLast();
	setResult(RESULT_OK);
	// more(more);
}
 
Example #6
Source File: ChatActivity.java    From school_shop with MIT License 6 votes vote down vote up
/**
 * 发送语音
 * 
 * @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.refreshSelectLast();
		setResult(RESULT_OK);
		// send file
		// sendVoiceSub(filePath, fileName, message);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example #7
Source File: ChatActivity.java    From school_shop with MIT License 6 votes vote down vote up
/**
 * 发送文本消息
 * 
 * @param content
 *            message content
 * @param isResend
 *            boolean resend
 */
private void sendText(String content) {

	if (content.length() > 0) {
		EMMessage message = EMMessage.createSendMessage(EMMessage.Type.TXT);
		// 如果是群聊,设置chattype,默认是单聊
		if (chatType == CHATTYPE_GROUP)
			message.setChatType(ChatType.GroupChat);
		TextMessageBody txtBody = new TextMessageBody(content);
		// 设置消息body
		message.addBody(txtBody);
		// 设置要发给谁,用户username或者群聊groupid
		message.setReceipt(toChatUsername);
		// 把messgage加到conversation中
		conversation.addMessage(message);
		// 通知adapter有消息变动,adapter会根据加入的这条message显示消息和调用sdk的发送方法
		adapter.refreshSelectLast();
		mEditTextContent.setText("");

		setResult(RESULT_OK);

	}
}
 
Example #8
Source File: LoadImageTask.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected Bitmap doInBackground(Object... args) {
	thumbnailPath = (String) args[0];
	localFullSizePath = (String) args[1];
	remotePath = (String) args[2];
	chatType = (ChatType) args[3];
	iv = (ImageView) args[4];
	// if(args[2] != null) {
	activity = (Activity) args[5];
	// }
	message = (EMMessage) args[6];
	File file = new File(thumbnailPath);
	if (file.exists()) {
		return ImageUtils.decodeScaleImage(thumbnailPath, 160, 160);
	} else {
		if (message.direct == EMMessage.Direct.SEND) {
			return ImageUtils.decodeScaleImage(localFullSizePath, 160, 160);
		} else {
			return null;
		}
	}
	

}
 
Example #9
Source File: ChatActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 发送位置信息
 * 
 * @param latitude
 * @param longitude
 * @param imagePath
 * @param locationAddress
 */
private void sendLocationMsg(double latitude, double longitude,
        String imagePath, String locationAddress) {
    EMMessage message = EMMessage
            .createSendMessage(EMMessage.Type.LOCATION);
    // 如果是群聊,设置chattype,默认是单聊
    if (chatType == CHATTYPE_GROUP)
        message.setChatType(ChatType.GroupChat);
    LocationMessageBody locBody = new LocationMessageBody(locationAddress,
            latitude, longitude);
    message.addBody(locBody);
    message.setReceipt(toChatUsername);
    message.setAttribute("toUserNick", toUserNick);
    message.setAttribute("toUserAvatar", toUserAvatar);
    message.setAttribute("useravatar", myUserAvatar);
    message.setAttribute("usernick", myUserNick);
    conversation.addMessage(message);
    listView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    listView.setSelection(listView.getCount() - 1);
    setResult(RESULT_OK);

}
 
Example #10
Source File: DemoHXSDKHelper.java    From FanXin-based-HuanXin with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected OnNotificationClickListener getNotificationClickListener() {
    return new OnNotificationClickListener() {

        @Override
        public Intent onNotificationClick(EMMessage message) {
            Intent intent = new Intent(appContext, ChatActivity.class);
            ChatType chatType = message.getChatType();
            if (chatType == ChatType.Chat) { // 单聊信息
                intent.putExtra("userId", message.getFrom());
                intent.putExtra("chatType", ChatActivity.CHATTYPE_SINGLE);
            } else { // 群聊信息
                     // message.getTo()为群聊id
                intent.putExtra("groupId", message.getTo());
                intent.putExtra("chatType", ChatActivity.CHATTYPE_GROUP);
            }
            return intent;
        }
    };
}
 
Example #11
Source File: MainActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onApplicationAccept(String groupId, String groupName,
        String accepter) {
    String st4 = getResources().getString(
            R.string.Agreed_to_your_group_chat_application);
    // 加群申请被同意
    EMMessage msg = EMMessage.createReceiveMessage(Type.TXT);
    msg.setChatType(ChatType.GroupChat);
    msg.setFrom(accepter);
    msg.setTo(groupId);
    msg.setMsgId(UUID.randomUUID().toString());
    msg.addBody(new TextMessageBody(accepter + st4));
    // 保存同意消息
    EMChatManager.getInstance().saveMessage(msg);
    // 提醒新消息
    EMNotifier.getInstance(getApplicationContext()).notifyOnNewMsg();

    runOnUiThread(new Runnable() {
        public void run() {
            updateUnreadLabel();
            // 刷新ui
            if (currentTabIndex == 0)
                homefragment.refresh();
            // if (CommonUtils.getTopActivity(MainActivity.this).equals(
            // GroupsActivity.class.getName())) {
            // GroupsActivity.instance.onResume();
            // }
        }
    });
}
 
Example #12
Source File: DemoHXSDKHelper.java    From school_shop with MIT License 5 votes vote down vote up
@Override
public HXNotifier createNotifier(){
    return new HXNotifier(){
        public synchronized void onNewMsg(final EMMessage message) {
            if(EMChatManager.getInstance().isSlientMessage(message)){
                return;
            }
            
            String chatUsename = null;
            List<String> notNotifyIds = null;
            // 获取设置的不提示新消息的用户或者群组ids
            if (message.getChatType() == ChatType.Chat) {
                chatUsename = message.getFrom();
                notNotifyIds = ((DemoHXSDKModel) hxModel).getDisabledGroups();
            } else {
                chatUsename = message.getTo();
                notNotifyIds = ((DemoHXSDKModel) hxModel).getDisabledIds();
            }

            if (notNotifyIds == null || !notNotifyIds.contains(chatUsename)) {
                // 判断app是否在后台
                if (!EasyUtils.isAppRunningForeground(appContext)) {
                    EMLog.d(TAG, "app is running in backgroud");
                    sendNotification(message, false);
                } else {
                    sendNotification(message, true);

                }
                
                viberateAndPlayTone(message);
            }
        }
    };
}
 
Example #13
Source File: MainActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onInvitationReceived(String groupId, String groupName,
        String inviter, String reason) {

    // 被邀请
    String st3 = getResources().getString(
            R.string.Invite_you_to_join_a_group_chat);
    User user = MYApplication.getInstance().getContactList()
            .get(inviter);
    if (user != null) {
        EMMessage msg = EMMessage.createReceiveMessage(Type.TXT);
        msg.setChatType(ChatType.GroupChat);
        msg.setFrom(inviter);
        msg.setTo(groupId);
        msg.setMsgId(UUID.randomUUID().toString());
        msg.addBody(new TextMessageBody(user.getNick() + st3));
        msg.setAttribute("useravatar", user.getAvatar());
        msg.setAttribute("usernick", user.getNick());
        // 保存邀请消息
        EMChatManager.getInstance().saveMessage(msg);
        // 提醒新消息
        EMNotifier.getInstance(getApplicationContext())
                .notifyOnNewMsg();
    }
    runOnUiThread(new Runnable() {
        public void run() {
            updateUnreadLabel();
            // 刷新ui
            if (currentTabIndex == 0)
                homefragment.refresh();
            // if (CommonUtils.getTopActivity(MainActivity.this).equals(
            // GroupsActivity.class.getName())) {
            // GroupsActivity.instance.onResume();
            // }
        }
    });

}
 
Example #14
Source File: MainActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    // 主页面收到消息后,主要为了提示未读,实际消息内容需要到chat页面查看

    String from = intent.getStringExtra("from");
    // 消息id
    String msgId = intent.getStringExtra("msgid");
    EMMessage message = EMChatManager.getInstance().getMessage(msgId);
    // 2014-10-22 修复在某些机器上,在聊天页面对方发消息过来时不立即显示内容的bug
    if (ChatActivity.activityInstance != null) {
        if (message.getChatType() == ChatType.GroupChat) {
            if (message.getTo().equals(
                    ChatActivity.activityInstance.getToChatUsername()))
                return;
        } else {
            if (from.equals(ChatActivity.activityInstance
                    .getToChatUsername()))
                return;
        }
    }

    // 注销广播接收者,否则在ChatActivity中会收到这个广播
    abortBroadcast();

    notifyNewMessage(message);

    // 刷新bottom bar消息未读数
    updateUnreadLabel();
    if (currentTabIndex == 0) {
        // 当前页面如果为聊天历史页面,刷新此页面
        if (homefragment != null) {
            homefragment.refresh();
        }
    }

}
 
Example #15
Source File: MainActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    abortBroadcast();

    String msgid = intent.getStringExtra("msgid");
    String from = intent.getStringExtra("from");

    EMConversation conversation = EMChatManager.getInstance()
            .getConversation(from);
    if (conversation != null) {
        // 把message设为已读
        EMMessage msg = conversation.getMessage(msgid);

        if (msg != null) {

            // 2014-11-5 修复在某些机器上,在聊天页面对方发送已读回执时不立即显示已读的bug
            if (ChatActivity.activityInstance != null) {
                if (msg.getChatType() == ChatType.Chat) {
                    if (from.equals(ChatActivity.activityInstance
                            .getToChatUsername()))
                        return;
                }
            }

            msg.isAcked = true;
        }
    }

}
 
Example #16
Source File: EaseChatFragment.java    From monolog-android with MIT License 5 votes vote down vote up
/**
 * 转发消息
 * 
 * @param forward_msg_id
 */
protected void forwardMessage(String forward_msg_id) {
    final EMMessage forward_msg = EMChatManager.getInstance().getMessage(forward_msg_id);
    EMMessage.Type type = forward_msg.getType();
    switch (type) {
    case TXT:
        // 获取消息内容,发送消息
        String content = ((TextMessageBody) forward_msg.getBody()).getMessage();
        sendTextMessage(content);
        break;
    case IMAGE:
        // 发送图片
        String filePath = ((ImageMessageBody) forward_msg.getBody()).getLocalUrl();
        if (filePath != null) {
            File file = new File(filePath);
            if (!file.exists()) {
                // 不存在大图发送缩略图
                filePath = EaseImageUtils.getThumbnailImagePath(filePath);
            }
            sendImageMessage(filePath);
        }
        break;
    default:
        break;
    }
    
    if(forward_msg.getChatType() == EMMessage.ChatType.ChatRoom){
        EMChatManager.getInstance().leaveChatRoom(forward_msg.getTo());
    }
}
 
Example #17
Source File: ChatActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 发送视频消息
 */
private void sendVideo(final String filePath, final String thumbPath,
        final int length) {
    final File videoFile = new File(filePath);
    if (!videoFile.exists()) {
        return;
    }
    try {
        EMMessage message = EMMessage
                .createSendMessage(EMMessage.Type.VIDEO);
        // 如果是群聊,设置chattype,默认是单聊
        if (chatType == CHATTYPE_GROUP)
            message.setChatType(ChatType.GroupChat);
        String to = toChatUsername;
        message.setReceipt(to);
        message.setAttribute("toUserNick", toUserNick);
        message.setAttribute("toUserAvatar", toUserAvatar);
        message.setAttribute("useravatar", myUserAvatar);
        message.setAttribute("usernick", myUserNick);
        VideoMessageBody body = new VideoMessageBody(videoFile, thumbPath,
                length, videoFile.length());
        message.addBody(body);
        conversation.addMessage(message);
        listView.setAdapter(adapter);
        adapter.refresh();
        listView.setSelection(listView.getCount() - 1);
        setResult(RESULT_OK);
    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
Example #18
Source File: ChatActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 发送文本消息
 * 
 * @param content
 *            message content
 * @param isResend
 *            boolean resend
 */
private void sendText(String content) {

    if (content.length() > 0) {
        EMMessage message = EMMessage.createSendMessage(EMMessage.Type.TXT);
        // 如果是群聊,设置chattype,默认是单聊
        if (chatType == CHATTYPE_GROUP)
            message.setChatType(ChatType.GroupChat);
        TextMessageBody txtBody = new TextMessageBody(content);
        // 设置消息body
        message.addBody(txtBody);
        // 设置要发给谁,用户username或者群聊groupid
        message.setReceipt(toChatUsername);
        message.setAttribute("toUserNick", toUserNick);
        message.setAttribute("toUserAvatar", toUserAvatar);
        message.setAttribute("useravatar", myUserAvatar);
        message.setAttribute("usernick", myUserNick);
        // 把messgage加到conversation中
        conversation.addMessage(message);
        // 通知adapter有消息变动,adapter会根据加入的这条message显示消息和调用sdk的发送方法
        adapter.refresh();
        listView.setSelection(listView.getCount() - 1);
        mEditTextContent.setText("");

        setResult(RESULT_OK);

    }
}
 
Example #19
Source File: ChatActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 发送语音
 * 
 * @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);
        message.setAttribute("toUserNick", toUserNick);
        message.setAttribute("toUserAvatar", toUserAvatar);
        message.setAttribute("useravatar", myUserAvatar);
        message.setAttribute("usernick", myUserNick);
        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();
    }
}
 
Example #20
Source File: ChatActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 发送图片
 * 
 * @param filePath
 */
private void sendPicture(final String filePath, boolean is_share) {
    Log.e("filePath------>>>>", filePath);
    String to = toChatUsername;
    // create and add image message in view
    final EMMessage message = EMMessage
            .createSendMessage(EMMessage.Type.IMAGE);
    // 如果是群聊,设置chattype,默认是单聊
    if (chatType == CHATTYPE_GROUP)
        message.setChatType(ChatType.GroupChat);

    message.setReceipt(to);
    message.setAttribute("toUserNick", toUserNick);
    message.setAttribute("toUserAvatar", toUserAvatar);
    message.setAttribute("useravatar", myUserAvatar);
    message.setAttribute("usernick", myUserNick);
    if (is_share) {
        message.setAttribute("isShare", "yes");
    }
    ImageMessageBody body = new ImageMessageBody(new File(filePath));
    // 默认超过100k的图片会压缩后发给对方,可以设置成发送原图
    // body.setSendOriginalImage(true);
    message.addBody(body);
    conversation.addMessage(message);

    listView.setAdapter(adapter);
    adapter.refresh();
    listView.setSelection(listView.getCount() - 1);
    setResult(RESULT_OK);
    // more(more);
}
 
Example #21
Source File: LoadImageTask.java    From school_shop with MIT License 4 votes vote down vote up
protected void onPostExecute(Bitmap image) {
	if (image != null) {
		iv.setImageBitmap(image);
		ImageCache.getInstance().put(thumbnailPath, image);
		iv.setClickable(true);
		iv.setTag(thumbnailPath);
		iv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (thumbnailPath != null) {

					Intent intent = new Intent(activity, ShowBigImage.class);
					File file = new File(localFullSizePath);
					if (file.exists()) {
						Uri uri = Uri.fromFile(file);
						intent.putExtra("uri", uri);
					} else {
						// The local full size pic does not exist yet.
						// ShowBigImage needs to download it from the server
						// first
						intent.putExtra("remotepath", remotePath);
					}
					if (message.getChatType() != ChatType.Chat) {
						// delete the image from server after download
					}
					if (message != null && message.direct == EMMessage.Direct.RECEIVE && !message.isAcked) {
						message.isAcked = true;
						try {
							// 看了大图后发个已读回执给对方
							EMChatManager.getInstance().ackMessageRead(message.getFrom(), message.getMsgId());
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					activity.startActivity(intent);
				}
			}
		});
	} else {
		if (message.status == EMMessage.Status.FAIL) {
			if (CommonUtils.isNetWorkConnected(activity)) {
				new Thread(new Runnable() {

					@Override
					public void run() {
						EMChatManager.getInstance().asyncFetchMessage(message);
					}
				}).start();
			}
		}

	}
}
 
Example #22
Source File: EaseChatRowText.java    From monolog-android with MIT License 4 votes vote down vote up
@Override
    public void onSetUpView() {
        TextMessageBody txtBody = (TextMessageBody) message.getBody();
        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
        // 设置内容
        contentView.setText(span, BufferType.SPANNABLE);

        if (message.direct == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status) {
            case CREATE: 
                progressBar.setVisibility(View.VISIBLE);
                statusView.setVisibility(View.GONE);
                // 发送消息
//                sendMsgInBackground(message);
                break;
            case SUCCESS: // 发送成功
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.GONE);
                break;
            case FAIL: // 发送失败
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.VISIBLE);
                break;
            case INPROGRESS: // 发送中
                progressBar.setVisibility(View.VISIBLE);
                statusView.setVisibility(View.GONE);
                break;
            default:
               break;
            }
        }else{
            if(!message.isAcked() && message.getChatType() == ChatType.Chat){
                try {
                    EMChatManager.getInstance().ackMessageRead(message.getFrom(), message.getMsgId());
                    message.isAcked = true;
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
Example #23
Source File: ChatActivity.java    From school_shop with MIT License 4 votes vote down vote up
/**
 * 发送文件
 * 
 * @param uri
 */
private void sendFile(Uri uri) {
	String filePath = null;
	if ("content".equalsIgnoreCase(uri.getScheme())) {
		String[] projection = { "_data" };
		Cursor cursor = null;

		try {
			cursor = getContentResolver().query(uri, projection, null, null, null);
			int column_index = cursor.getColumnIndexOrThrow("_data");
			if (cursor.moveToFirst()) {
				filePath = cursor.getString(column_index);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	} else if ("file".equalsIgnoreCase(uri.getScheme())) {
		filePath = uri.getPath();
	}
	File file = new File(filePath);
	if (file == null || !file.exists()) {
		String st7 = getResources().getString(R.string.File_does_not_exist);
		Toast.makeText(getApplicationContext(), st7, 0).show();
		return;
	}
	if (file.length() > 10 * 1024 * 1024) {
		String st6 = getResources().getString(R.string.The_file_is_not_greater_than_10_m);
		Toast.makeText(getApplicationContext(), st6, 0).show();
		return;
	}

	// 创建一个文件消息
	EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE);
	// 如果是群聊,设置chattype,默认是单聊
	if (chatType == CHATTYPE_GROUP)
		message.setChatType(ChatType.GroupChat);

	message.setReceipt(toChatUsername);
	// add message body
	NormalFileMessageBody body = new NormalFileMessageBody(new File(filePath));
	message.addBody(body);
	conversation.addMessage(message);
	listView.setAdapter(adapter);
	adapter.refreshSelectLast();
	setResult(RESULT_OK);
}
 
Example #24
Source File: ChatActivity.java    From school_shop with MIT License 4 votes vote down vote up
/**
	 * 事件监听
	 * 
	 * see {@link EMNotifierEvent}
     */
    @Override
    public void onEvent(EMNotifierEvent event) {
        switch (event.getEvent()) {
        case EventNewMessage:
        {
            //获取到message
            EMMessage message = (EMMessage) event.getData();
            
            String username = null;
            //群组消息
            if(message.getChatType() == ChatType.GroupChat){
                username = message.getTo();
            }
            else{
                //单聊消息
                username = message.getFrom();
            }

            //如果是当前会话的消息,刷新聊天页面
            if(username.equals(getToChatUsername())){
                refreshUIWithNewMessage();
                //声音和震动提示有新消息
                HXSDKHelper.getInstance().getNotifier().viberateAndPlayTone(message);
            }else{
                //如果消息不是和当前聊天ID的消息
                HXSDKHelper.getInstance().getNotifier().onNewMsg(message);
            }

            break;
        }
//        case EventDeliveryAck:
//        {
//            //获取到message
//            EMMessage message = (EMMessage) event.getData();
//            refreshUI();
//            break;
//        }
//        case EventReadAck:
//        {
//            //获取到message
//            EMMessage message = (EMMessage) event.getData();
//            refreshUI();
//            break;
//        }
        case EventOfflineMessage:
        {
            //a list of offline messages 
            //List<EMMessage> offlineMessages = (List<EMMessage>) event.getData();
            refreshUI();
            break;
        }
        default:
            break;
        }
        
    }
 
Example #25
Source File: MessageAdapter.java    From school_shop with MIT License 4 votes vote down vote up
/**
 * load image into image view
 * 
 * @param thumbernailPath
 * @param iv
 * @param position
 * @return the image exists or not
 */
private boolean showImageView(final String thumbernailPath, final ImageView iv, final String localFullSizePath, String remoteDir,
		final EMMessage message) {
	// String imagename =
	// localFullSizePath.substring(localFullSizePath.lastIndexOf("/") + 1,
	// localFullSizePath.length());
	// final String remote = remoteDir != null ? remoteDir+imagename :
	// imagename;
	final String remote = remoteDir;
	EMLog.d("###", "local = " + localFullSizePath + " remote: " + remote);
	// first check if the thumbnail image already loaded into cache
	Bitmap bitmap = ImageCache.getInstance().get(thumbernailPath);
	if (bitmap != null) {
		// thumbnail image is already loaded, reuse the drawable
		iv.setImageBitmap(bitmap);
		iv.setClickable(true); 
		iv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				System.err.println("image view on click");
				Intent intent = new Intent(activity, ShowBigImage.class);
				File file = new File(localFullSizePath);
				if (file.exists()) {
					Uri uri = Uri.fromFile(file);
					intent.putExtra("uri", uri);
					System.err.println("here need to check why download everytime");
				} else {
					// The local full size pic does not exist yet.
					// ShowBigImage needs to download it from the server
					// first
					// intent.putExtra("", message.get);
					ImageMessageBody body = (ImageMessageBody) message.getBody();
					intent.putExtra("secret", body.getSecret());
					intent.putExtra("remotepath", remote);
				}
				if (message != null && message.direct == EMMessage.Direct.RECEIVE && !message.isAcked
						&& message.getChatType() != ChatType.GroupChat) {
					try {
						EMChatManager.getInstance().ackMessageRead(message.getFrom(), message.getMsgId());
						message.isAcked = true;
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				activity.startActivity(intent);
			}
		});
		return true;
	} else {

		new LoadImageTask().execute(thumbernailPath, localFullSizePath, remote, message.getChatType(), iv, activity, message);
		return true;
	}

}
 
Example #26
Source File: LoadImageTask.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
protected void onPostExecute(Bitmap image) {
	if (image != null) {
		iv.setImageBitmap(image);
		ImageCache.getInstance().put(thumbnailPath, image);
		iv.setClickable(true);
		iv.setTag(thumbnailPath);
		iv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (thumbnailPath != null) {

					Intent intent = new Intent(activity, ShowBigImage.class);
					File file = new File(localFullSizePath);
					if (file.exists()) {
						Uri uri = Uri.fromFile(file);
						intent.putExtra("uri", uri);
					} else {
						// The local full size pic does not exist yet.
						// ShowBigImage needs to download it from the server
						// first
						intent.putExtra("remotepath", remotePath);
					}
					if (message.getChatType() != ChatType.Chat) {
						// delete the image from server after download
					}
					if (message != null && message.direct == EMMessage.Direct.RECEIVE && !message.isAcked) {
						message.isAcked = true;
						try {
							// 看了大图后发个已读回执给对方
							EMChatManager.getInstance().ackMessageRead(message.getFrom(), message.getMsgId());
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					activity.startActivity(intent);
				}
			}
		});
	} else {
		if (message.status == EMMessage.Status.FAIL) {
			if (CommonUtils.isNetWorkConnected(activity)) {
				new Thread(new Runnable() {

					@Override
					public void run() {
						EMChatManager.getInstance().asyncFetchMessage(message);
					}
				}).start();
			}
		}

	}
}
 
Example #27
Source File: ChatActivity.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 发送文件
 * 
 * @param uri
 */
private void sendFile(Uri uri) {
    String filePath = null;
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = getContentResolver().query(uri, projection, null,
                    null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                filePath = cursor.getString(column_index);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        filePath = uri.getPath();
    }
    File file = new File(filePath);
    if (file == null || !file.exists()) {
        Toast.makeText(getApplicationContext(), "文件不存在", Toast.LENGTH_SHORT)
                .show();
        return;
    }
    if (file.length() > 10 * 1024 * 1024) {
        Toast.makeText(getApplicationContext(), "文件不能大于10M",
                Toast.LENGTH_SHORT).show();
        return;
    }

    // 创建一个文件消息
    EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE);
    // 如果是群聊,设置chattype,默认是单聊
    if (chatType == CHATTYPE_GROUP)
        message.setChatType(ChatType.GroupChat);

    message.setReceipt(toChatUsername);
    // add message body
    NormalFileMessageBody body = new NormalFileMessageBody(new File(
            filePath));
    message.addBody(body);
    message.setAttribute("toUserNick", toUserNick);
    message.setAttribute("toUserAvatar", toUserAvatar);
    message.setAttribute("useravatar", myUserAvatar);
    message.setAttribute("usernick", myUserNick);
    conversation.addMessage(message);
    listView.setAdapter(adapter);
    adapter.refresh();
    listView.setSelection(listView.getCount() - 1);
    setResult(RESULT_OK);
}
 
Example #28
Source File: MessageAdapter.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 展示视频缩略图
 * 
 * @param localThumb
 *            本地缩略图路径
 * @param iv
 * @param thumbnailUrl
 *            远程缩略图路径
 * @param message
 */
private void showVideoThumbView(String localThumb, ImageView iv,
        String thumbnailUrl, final EMMessage message) {
    // first check if the thumbnail image already loaded into cache
    Bitmap bitmap = ImageCache.getInstance().get(localThumb);
    if (bitmap != null) {
        // thumbnail image is already loaded, reuse the drawable
        iv.setImageBitmap(bitmap);
        iv.setClickable(true);
        iv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                VideoMessageBody videoBody = (VideoMessageBody) message
                        .getBody();
                System.err.println("video view is on click");
                Intent intent = new Intent(activity,
                        ShowVideoActivity.class);
                intent.putExtra("localpath", videoBody.getLocalUrl());
                intent.putExtra("secret", videoBody.getSecret());
                intent.putExtra("remotepath", videoBody.getRemoteUrl());
                if (message != null
                        && message.direct == EMMessage.Direct.RECEIVE
                        && !message.isAcked
                        && message.getChatType() != ChatType.GroupChat) {
                    message.isAcked = true;
                    try {
                        EMChatManager.getInstance().ackMessageRead(
                                message.getFrom(), message.getMsgId());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                activity.startActivity(intent);

            }
        });

    } else {
        new LoadVideoImageTask().execute(localThumb, thumbnailUrl, iv,
                activity, message, this);
    }

}
 
Example #29
Source File: MessageAdapter.java    From FanXin-based-HuanXin with GNU General Public License v2.0 4 votes vote down vote up
/**
 * load image into image view
 * 
 * @param thumbernailPath
 * @param iv
 * @param position
 * @return the image exists or not
 */
private boolean showImageView(final String thumbernailPath,
        final ImageView iv, final String localFullSizePath,
        String remoteDir, final EMMessage message) {
    // String imagename =
    // localFullSizePath.substring(localFullSizePath.lastIndexOf("/") + 1,
    // localFullSizePath.length());
    // final String remote = remoteDir != null ? remoteDir+imagename :
    // imagename;
    final String remote = remoteDir;
    EMLog.d("###", "local = " + localFullSizePath + " remote: " + remote);
    // first check if the thumbnail image already loaded into cache
    Bitmap bitmap = ImageCache.getInstance().get(thumbernailPath);
    if (bitmap != null) {
        // thumbnail image is already loaded, reuse the drawable
        iv.setImageBitmap(bitmap);
        iv.setClickable(true);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.err.println("image view on click");
                Intent intent = new Intent(activity, ShowBigImage.class);
                File file = new File(localFullSizePath);
                if (file.exists()) {
                    Uri uri = Uri.fromFile(file);
                    intent.putExtra("uri", uri);
                    System.err
                            .println("here need to check why download everytime");
                } else {
                    // The local full size pic does not exist yet.
                    // ShowBigImage needs to download it from the server
                    // first
                    // intent.putExtra("", message.get);
                    ImageMessageBody body = (ImageMessageBody) message
                            .getBody();
                    intent.putExtra("secret", body.getSecret());
                    intent.putExtra("remotepath", remote);
                }
                if (message != null
                        && message.direct == EMMessage.Direct.RECEIVE
                        && !message.isAcked
                        && message.getChatType() != ChatType.GroupChat) {
                    try {
                        EMChatManager.getInstance().ackMessageRead(
                                message.getFrom(), message.getMsgId());
                        message.isAcked = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                activity.startActivity(intent);
            }
        });
        return true;
    } else {

        new LoadImageTask().execute(thumbernailPath, localFullSizePath,
                remote, message.getChatType(), iv, activity, message);
        return true;
    }

}
 
Example #30
Source File: EaseChatFragment.java    From monolog-android with MIT License 4 votes vote down vote up
/**
 * 事件监听,registerEventListener后的回调事件
 * 
 * see {@link EMNotifierEvent}
 */
@Override
public void onEvent(EMNotifierEvent event) {
    switch (event.getEvent()) {
    case EventNewMessage:
        // 获取到message
        EMMessage message = (EMMessage) event.getData();

        String username = null;
        // 群组消息
        if (message.getChatType() == ChatType.GroupChat || message.getChatType() == ChatType.ChatRoom) {
            username = message.getTo();
        } else {
            // 单聊消息
            username = message.getFrom();
        }

        // 如果是当前会话的消息,刷新聊天页面
        if (username.equals(toChatUsername)) {
            messageList.refreshSelectLast();
            // 声音和震动提示有新消息
            EaseUI.getInstance().getNotifier().viberateAndPlayTone(message);
        } else {
            // 如果消息不是和当前聊天ID的消息
            EaseUI.getInstance().getNotifier().onNewMsg(message);
        }

        break;
    case EventDeliveryAck:
    case EventReadAck:
        // 获取到message
        messageList.refresh();
        break;
    case EventOfflineMessage:
        // a list of offline messages
        // List<EMMessage> offlineMessages = (List<EMMessage>)
        // event.getData();
        messageList.refresh();
        break;
    default:
        break;
    }

}