package com.fanxin.easeui.widget.chatrow;
import java.io.File;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.ChatType;
import com.hyphenate.chat.EMNormalFileMessageBody;
import com.hyphenate.easeui.R;
import com.fanxin.easeui.ui.EaseShowNormalFileActivity;
import com.hyphenate.exceptions.HyphenateException;
import com.hyphenate.util.FileUtils;
import com.hyphenate.util.TextFormater;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class EaseChatRowFile extends EaseChatRow{
protected TextView fileNameView;
protected TextView fileSizeView;
protected TextView fileStateView;
protected EMCallBack sendfileCallBack;
protected boolean isNotifyProcessed;
private EMNormalFileMessageBody fileMessageBody;
public EaseChatRowFile(Context context, EMMessage message, int position, BaseAdapter adapter) {
super(context, message, position, adapter);
}
@Override
protected void onInflatView() {
inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
R.layout.ease_row_received_file : R.layout.ease_row_sent_file, this);
}
@Override
protected void onFindViewById() {
fileNameView = (TextView) findViewById(R.id.tv_file_name);
fileSizeView = (TextView) findViewById(R.id.tv_file_size);
fileStateView = (TextView) findViewById(R.id.tv_file_state);
percentageView = (TextView) findViewById(R.id.percentage);
}
@Override
protected void onSetUpView() {
fileMessageBody = (EMNormalFileMessageBody) message.getBody();
String filePath = fileMessageBody.getLocalUrl();
fileNameView.setText(fileMessageBody.getFileName());
fileSizeView.setText(TextFormater.getDataSize(fileMessageBody.getFileSize()));
if (message.direct() == EMMessage.Direct.RECEIVE) {
File file = new File(filePath);
if (file != null && file.exists()) {
fileStateView.setText(R.string.Have_downloaded);
} else {
fileStateView.setText(R.string.Did_not_download);
}
return;
}
// until here, to sending message
handleSendMessage();
}
/**
* handle sending message
*/
protected void handleSendMessage() {
setMessageSendCallback();
switch (message.status()) {
case SUCCESS:
progressBar.setVisibility(View.INVISIBLE);
if(percentageView != null)
percentageView.setVisibility(View.INVISIBLE);
statusView.setVisibility(View.INVISIBLE);
break;
case FAIL:
progressBar.setVisibility(View.INVISIBLE);
if(percentageView != null)
percentageView.setVisibility(View.INVISIBLE);
statusView.setVisibility(View.VISIBLE);
break;
case INPROGRESS:
progressBar.setVisibility(View.VISIBLE);
if(percentageView != null){
percentageView.setVisibility(View.VISIBLE);
percentageView.setText(message.progress() + "%");
}
statusView.setVisibility(View.INVISIBLE);
break;
default:
progressBar.setVisibility(View.INVISIBLE);
if(percentageView != null)
percentageView.setVisibility(View.INVISIBLE);
statusView.setVisibility(View.VISIBLE);
break;
}
}
@Override
protected void onUpdateView() {
adapter.notifyDataSetChanged();
}
@Override
protected void onBubbleClick() {
String filePath = fileMessageBody.getLocalUrl();
File file = new File(filePath);
if (file != null && file.exists()) {
// open files if it exist
FileUtils.openFile(file, (Activity) context);
} else {
// download the file
context.startActivity(new Intent(context, EaseShowNormalFileActivity.class).putExtra("msgbody", message.getBody()));
}
if (message.direct() == EMMessage.Direct.RECEIVE && !message.isAcked() && message.getChatType() == ChatType.Chat) {
try {
EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
} catch (HyphenateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}