package com.community.easeim.section.chat;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;
import com.community.easeim.DemoApplication;
import com.community.easeim.DemoHelper;
import com.community.easeim.MainActivity;
import com.community.easeim.R;
import com.community.easeim.common.constant.DemoConstant;
import com.community.easeim.common.db.DemoDbHelper;
import com.community.easeim.common.db.dao.EmUserDao;
import com.community.easeim.common.db.entity.EmUserEntity;
import com.community.easeim.common.db.entity.InviteMessageStatus;
import com.community.easeim.common.interfaceOrImplement.ResultCallBack;
import com.community.easeim.common.leancloud.GroundManager;
import com.community.easeim.common.leancloud.LeanCloudManager;
import com.community.easeim.common.livedatas.LiveDataBus;
import com.community.easeim.common.manager.AppManager;
import com.community.easeim.common.manager.PushAndMessageHelper;
import com.community.easeim.common.repositories.EMContactManagerRepository;
import com.community.easeim.common.repositories.EMGroupManagerRepository;
import com.community.easeim.common.repositories.EMPushManagerRepository;
import com.community.easeim.imkit.constants.EaseConstant;
import com.community.easeim.imkit.domain.EaseUser;
import com.community.easeim.imkit.interfaces.EaseGroupListener;
import com.community.easeim.imkit.manager.EaseAtMessageHelper;
import com.community.easeim.imkit.manager.EaseChatPresenter;
import com.community.easeim.imkit.manager.EaseSystemMsgManager;
import com.community.easeim.imkit.model.EaseEvent;
import com.community.easeim.section.chat.activity.ChatActivity;
import com.community.easeim.section.dialog.CustomDialogFragment;
import com.community.easeim.section.ground.bean.GroundGroupBean;
import com.community.easeim.section.group.GroupHelper;
import com.community.easeim.section.home.dialog.CustomDialog;
import com.community.easeim.section.voice.VoiceTalkActivity;
import com.community.easeim.section.voice.bean.ChannelCmd;
import com.community.easeim.voice.CmdMediaCtrl;
import com.google.gson.Gson;
import com.hyphenate.EMChatRoomChangeListener;
import com.hyphenate.EMConnectionListener;
import com.hyphenate.EMContactListener;
import com.hyphenate.EMConversationListener;
import com.hyphenate.EMError;
import com.hyphenate.EMMultiDeviceListener;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMCmdMessageBody;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMucSharedFile;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.chat.adapter.EMAChatRoomManagerListener;
import com.hyphenate.exceptions.HyphenateException;
import com.hyphenate.util.EMLog;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* 主要用于chat过程中的全局监听,并对相应的事件进行处理
* {@link #init()}方法建议在登录成功以后进行调用
*/
public class ChatPresenter extends EaseChatPresenter {
private static final String TAG = ChatPresenter.class.getSimpleName();
private static final int HANDLER_SHOW_TOAST = 0;
private static ChatPresenter instance;
private LiveDataBus messageChangeLiveData;
private boolean isGroupsSyncedWithServer = false;
private boolean isContactsSyncedWithServer = false;
private boolean isBlackListSyncedWithServer = false;
private boolean isPushConfigsWithServer = false;
private Context appContext;
protected Handler handler;
Queue<String> msgQueue = new ConcurrentLinkedQueue<>();
private ChatPresenter() {
appContext = DemoApplication.getInstance();
initHandler(appContext.getMainLooper());
messageChangeLiveData = LiveDataBus.get();
//添加网络连接状态监听
DemoHelper.getInstance().getEMClient().addConnectionListener(new ChatConnectionListener());
//添加多端登录监听
DemoHelper.getInstance().getEMClient().addMultiDeviceListener(new ChatMultiDeviceListener());
//添加群组监听
DemoHelper.getInstance().getGroupManager().addGroupChangeListener(new ChatGroupListener());
//添加联系人监听
DemoHelper.getInstance().getContactManager().setContactListener(new ChatContactListener());
//添加聊天室监听
DemoHelper.getInstance().getChatroomManager().addChatRoomChangeListener(new ChatRoomListener());
//添加对会话的监听(监听已读回执)
DemoHelper.getInstance().getChatManager().addConversationListener(new ChatConversationListener());
}
public static ChatPresenter getInstance() {
if (instance == null) {
synchronized (ChatPresenter.class) {
if (instance == null) {
instance = new ChatPresenter();
}
}
}
return instance;
}
/**
* 将需要登录成功进入MainActivity中初始化的逻辑,放到此处进行处理
*/
public void init() {
}
public void initHandler(Looper looper) {
handler = new Handler(looper) {
@Override
public void handleMessage(Message msg) {
Object obj = msg.obj;
switch (msg.what) {
case HANDLER_SHOW_TOAST:
if (obj instanceof String) {
String str = (String) obj;
//ToastUtils.showToast(str);
Toast.makeText(appContext, str, Toast.LENGTH_SHORT).show();
}
break;
}
}
};
while (!msgQueue.isEmpty()) {
showToast(msgQueue.remove());
}
}
void showToast(@StringRes int mesId) {
showToast(context.getString(mesId));
}
void showToast(final String message) {
Log.d(TAG, "receive invitation to join the group:" + message);
if (handler != null) {
Message msg = Message.obtain(handler, HANDLER_SHOW_TOAST, message);
handler.sendMessage(msg);
} else {
msgQueue.add(message);
}
}
@Override
public void onMessageReceived(List<EMMessage> messages) {
super.onMessageReceived(messages);
EaseEvent event = EaseEvent.create(DemoConstant.MESSAGE_CHANGE_RECEIVE, EaseEvent.TYPE.MESSAGE);
messageChangeLiveData.with(DemoConstant.MESSAGE_CHANGE_CHANGE).postValue(event);
for (EMMessage message : messages) {
EMLog.d(TAG, "onMessageReceived id : " + message.getMsgId());
EMLog.d(TAG, "onMessageReceived: " + message.getType());
// 如果设置群组离线消息免打扰,则不进行消息通知
List<String> disabledIds = DemoHelper.getInstance().getPushManager().getNoPushGroups();
if (disabledIds != null && disabledIds.contains(message.conversationId())) {
return;
}
// in background, do not refresh UI, notify it in notification bar
if (!DemoApplication.getInstance().getLifecycleCallbacks().isFront()) {
getNotifier().notify(message);
}
//notify new message
getNotifier().vibrateAndPlayTone(message);
}
}
/**
* 判断是否已经启动�
lly202406
- 粉丝: 3024
- 资源: 5526
最新资源
- 毕设和企业适用springboot企业知识管理平台类及在线音乐平台源码+论文+视频.zip
- 毕设和企业适用springboot企业知识管理平台类及智慧社区管理平台源码+论文+视频.zip
- 毕设和企业适用springboot人才招聘类及企业资源规划平台源码+论文+视频.zip
- 毕设和企业适用springboot人才招聘类及社区服务平台源码+论文+视频.zip
- 毕设和企业适用springboot人才招聘类及食品配送管理平台源码+论文+视频.zip
- 毕设和企业适用springboot企业资源规划类及AI数据标注平台源码+论文+视频.zip
- 毕设和企业适用springboot企业资源规划类及车载智能管理平台源码+论文+视频.zip
- 毕设和企业适用springboot企业资源规划类及大数据实时处理系统源码+论文+视频.zip
- 毕设和企业适用springboot人才招聘类及视频内容管理平台源码+论文+视频.zip
- 毕设和企业适用springboot汽车管理类及网络营销平台源码+论文+视频.zip
- 毕设和企业适用springboot汽车管理类及物流信息平台源码+论文+视频.zip
- 毕设和企业适用springboot汽车管理类及销售管理平台源码+论文+视频.zip
- 毕设和企业适用springboot汽车管理类及物流追踪系统源码+论文+视频.zip
- 毕设和企业适用springboot汽车管理类及消费品管理平台源码+论文+视频.zip
- 毕设和企业适用springboot企业资源规划类及个性化广告平台源码+论文+视频.zip
- 毕设和企业适用springboot企业资源规划类及教育评价系统源码+论文+视频.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈