Search in sources :

Example 1 with NormalFileMessageBody

use of com.easemob.chat.NormalFileMessageBody in project wechat by motianhuo.

the class MessageAdapter method handleFileMessage.

/**
	 * 文件消息
	 * 
	 * @param message
	 * @param holder
	 * @param position
	 * @param convertView
	 */
private void handleFileMessage(final EMMessage message, final ViewHolder holder, int position, View convertView) {
    final NormalFileMessageBody fileMessageBody = (NormalFileMessageBody) message.getBody();
    final String filePath = fileMessageBody.getLocalUrl();
    holder.tv_file_name.setText(fileMessageBody.getFileName());
    holder.tv_file_size.setText(TextFormater.getDataSize(fileMessageBody.getFileSize()));
    holder.ll_container.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            File file = new File(filePath);
            if (file != null && file.exists()) {
                // 文件存在,直接打开
                FileUtils.openFile(file, (Activity) context);
            } else {
                // 下载
                // TODO 下载文件
                context.startActivity(new Intent(context, ShowNormalFileActivity.class).putExtra("msgbody", fileMessageBody));
            }
            if (message.direct == EMMessage.Direct.RECEIVE && !message.isAcked) {
                try {
                    EMChatManager.getInstance().ackMessageRead(message.getFrom(), message.getMsgId());
                    message.isAcked = true;
                } catch (EaseMobException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });
    String st1 = context.getResources().getString(R.string.Have_downloaded);
    String st2 = context.getResources().getString(R.string.Did_not_download);
    if (message.direct == EMMessage.Direct.RECEIVE) {
        // 接收的消息
        System.err.println("it is receive msg");
        File file = new File(filePath);
        if (file != null && file.exists()) {
            holder.tv_file_download_state.setText(st1);
        } else {
            holder.tv_file_download_state.setText(st2);
        }
        return;
    }
    // until here, deal with send voice msg
    switch(message.status) {
        case SUCCESS:
            holder.pb.setVisibility(View.INVISIBLE);
            holder.tv.setVisibility(View.INVISIBLE);
            holder.staus_iv.setVisibility(View.INVISIBLE);
            break;
        case FAIL:
            holder.pb.setVisibility(View.INVISIBLE);
            holder.tv.setVisibility(View.INVISIBLE);
            holder.staus_iv.setVisibility(View.VISIBLE);
            break;
        case INPROGRESS:
            if (timers.containsKey(message.getMsgId()))
                return;
            // set a timer
            final Timer timer = new Timer();
            timers.put(message.getMsgId(), timer);
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    activity.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            holder.pb.setVisibility(View.VISIBLE);
                            holder.tv.setVisibility(View.VISIBLE);
                            holder.tv.setText(message.progress + "%");
                            if (message.status == EMMessage.Status.SUCCESS) {
                                holder.pb.setVisibility(View.INVISIBLE);
                                holder.tv.setVisibility(View.INVISIBLE);
                                timer.cancel();
                            } else if (message.status == EMMessage.Status.FAIL) {
                                holder.pb.setVisibility(View.INVISIBLE);
                                holder.tv.setVisibility(View.INVISIBLE);
                                holder.staus_iv.setVisibility(View.VISIBLE);
                                Toast.makeText(activity, activity.getString(R.string.send_fail) + activity.getString(R.string.connect_failuer_toast), 0).show();
                                timer.cancel();
                            }
                        }
                    });
                }
            }, 0, 500);
            break;
        default:
            // 发送消息
            sendMsgInBackground(message, holder);
    }
}
Also used : ShowNormalFileActivity(com.juns.wechat.chat.ShowNormalFileActivity) EaseMobException(com.easemob.exceptions.EaseMobException) BaiduMapActivity(com.juns.wechat.chat.BaiduMapActivity) ChatActivity(com.juns.wechat.chat.ChatActivity) ShowNormalFileActivity(com.juns.wechat.chat.ShowNormalFileActivity) Activity(android.app.Activity) Intent(android.content.Intent) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) NormalFileMessageBody(com.easemob.chat.NormalFileMessageBody) Timer(java.util.Timer) TimerTask(java.util.TimerTask) OnClickListener(android.view.View.OnClickListener) File(java.io.File)

Example 2 with NormalFileMessageBody

use of com.easemob.chat.NormalFileMessageBody in project wechat by motianhuo.

the class ChatActivity method sendFile.

/**
	 * 发送文件
	 * 
	 * @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.refresh();
    listView.setSelection(listView.getCount() - 1);
    setResult(RESULT_OK);
}
Also used : NormalFileMessageBody(com.easemob.chat.NormalFileMessageBody) Cursor(android.database.Cursor) File(java.io.File) EaseMobException(com.easemob.exceptions.EaseMobException) IOException(java.io.IOException) EMMessage(com.easemob.chat.EMMessage)

Aggregations

NormalFileMessageBody (com.easemob.chat.NormalFileMessageBody)2 EaseMobException (com.easemob.exceptions.EaseMobException)2 File (java.io.File)2 Activity (android.app.Activity)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 EMMessage (com.easemob.chat.EMMessage)1 BaiduMapActivity (com.juns.wechat.chat.BaiduMapActivity)1 ChatActivity (com.juns.wechat.chat.ChatActivity)1 ShowNormalFileActivity (com.juns.wechat.chat.ShowNormalFileActivity)1 IOException (java.io.IOException)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1