【文件属性】:
文件名称:HXSDKHelper.java 环信Demo
文件大小:21KB
文件格式:JAVA
更新时间:2018-08-12 10:34:17
dfdsfsd
public class DemoHXSDKHelper extends HXSDKHelper{
private static final String TAG = "DemoHXSDKHelper";
/**
* EMEventListener
*/
protected EMEventListener eventListener = null;
/**
* contact list in cache
*/
private Map contactList;
/**
* robot list in cache
*/
private Map robotList;
private CallReceiver callReceiver;
/**
* 用来记录foreground Activity
*/
private List activityList = new ArrayList();
public void pushActivity(Activity activity){
if(!activityList.contains(activity)){
activityList.add(0,activity);
}
}
public void popActivity(Activity activity){
activityList.remove(activity);
}
@Override
protected void initHXOptions(){
super.initHXOptions();
// you can also get EMChatOptions to set related SDK options
EMChatOptions options = EMChatManager.getInstance().getChatOptions();
options.allowChatroomOwnerLeave(getModel().isChatroomOwnerLeaveAllowed());
}
@Override
protected void initListener(){
super.initListener();
IntentFilter callFilter = new IntentFilter(EMChatManager.getInstance().getIncomingCallBroadcastAction());
if(callReceiver == null){
callReceiver = new CallReceiver();
}
//注册通话广播接收者
appContext.registerReceiver(callReceiver, callFilter);
//注册消息事件监听
initEventListener();
}
/**
* 全局事件监听
* 因为可能会有UI页面先处理到这个消息,所以一般如果UI页面已经处理,这里就不需要再次处理
* activityList.size() <= 0 意味着所有页面都已经在后台运行,或者已经离开Activity Stack
*/
protected void initEventListener() {
eventListener = new EMEventListener() {
private BroadcastReceiver broadCastReceiver = null;
@Override
public void onEvent(EMNotifierEvent event) {
EMMessage message = null;
if(event.getData() instanceof EMMessage){
message = (EMMessage)event.getData();
EMLog.d(TAG, "receive the event : " + event.getEvent() + ",id : " + message.getMsgId());
}
switch (event.getEvent()) {
case EventNewMessage:
//应用在后台,不需要刷新UI,通知栏提示新消息
if(activityList.size() <= 0){
HXSDKHelper.getInstance().getNotifier().onNewMsg(message);
}
break;
case EventOfflineMessage:
if(activityList.size() <= 0){
EMLog.d(TAG, "received offline messages");
List messages = (List) event.getData();
HXSDKHelper.getInstance().getNotifier().onNewMesg(messages);
}
break;
// below is just giving a example to show a cmd toast, the app should not follow this
// so be careful of this
case EventNewCMDMessage:
{
EMLog.d(TAG, "收到透传消息");
//获取消息body
CmdMessageBody cmdMsgBody = (CmdMessageBody) message.getBody();
final String action = cmdMsgBody.action;//获取自定义action
//获取扩展属性 此处省略
//message.getStringAttribute("");
EMLog.d(TAG, String.format("透传消息:action:%s,message:%s", action,message.toString()));
final String str = appContext.getString(R.string.receive_the_passthrough);
final String CMD_TOAST_BROADCAST = "easemob.demo.cmd.toast";
IntentFilter cmdFilter = new IntentFilter(CMD_TOAST_BROADCAST);
if(broadCastReceiver == null){
broadCastReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(appContext, intent.getStringExtra("cmd_value"), Toast.LENGTH_SHORT).show();
}
};
//注册广播接收者
appContext.registerReceiver(broadCastReceiver,cmdFilter);
}
Intent broadcastIntent = new Intent(CMD_TOAST_BROADCAST);
broadcastIntent.putExtra("cmd_value", str+action);
appContext.sendBroadcast(broadcastIntent, null);
break;
}
case EventDeliveryAck:
message.setDelivered(true);
break;
case EventReadAck:
message.setAcked(true);
break;
// add other events in case you are interested in
default:
break;
}
}
};
EMChatManager.getInstance().registerEventListener(eventListener);
EMChatManager.getInstance().addChatRoomChangeListener(new EMChatRoomChangeListener(){
private final static String ROOM_CHANGE_BROADCAST = "easemob.demo.chatroom.changeevent.toast";
private final IntentFilter filter = new IntentFilter(ROOM_CHANGE_BROADCAST);
private boolean registered = false;
private void showToast(String value){
if(!registered){
//注册广播接收者
appContext.registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(appContext, intent.getStringExtra("value"), Toast.LENGTH_SHORT).show();
}
}, filter);
registered = true;
}
Intent broadcastIntent = new Intent(ROOM_CHANGE_BROADCAST);
broadcastIntent.putExtra("value", value);
appContext.sendBroadcast(broadcastIntent, null);
}
@Override
public void onChatRoomDestroyed(String roomId, String roomName) {
showToast(" room : " + roomId + " with room name : " + roomName + " was destroyed");
Log.i("info","onChatRoomDestroyed="+roomName);
}
@Override
public void onMemberJoined(String roomId, String participant) {
showToast("member : " + participant + " join the room : " + roomId);
Log.i("info", "onmemberjoined="+participant);
}
@Override
public void onMemberExited(String roomId, String roomName,
String participant) {
showToast("member : " + participant + " leave the room : " + roomId + " room name : " + roomName);
Log.i("info", "onMemberExited="+participant);
}
@Override
public void onMemberKicked(String roomId, String roomName,
String participant) {
showToast("member : " + participant + " was kicked from the room : " + roomId + " room name : " + roomName);
Log.i("info", "onMemberKicked="+participant);
}
});
}
/**
* 自定义通知栏提示内容
* @return
*/
@Override
protected HXNotificationInfoProvider getNotificationListener() {
//可以覆盖默认的设置
return new HXNotificationInfoProvider() {
@Override
public String getTitle(EMMessage message) {
//修改标题,这里使用默认
return null;
}
@Override
public int getSmallIcon(EMMessage message) {
//设置小图标,这里为默认
return 0;
}
@Override
public String getDisplayedText(EMMessage message) {
// 设置状态栏的消息提示,可以根据message的类型做相应提示
String ticker = CommonUtils.getMessageDigest(message, appContext);
if(message.getType() == Type.TXT){
ticker = ticker.replaceAll("\\[.{2,3}\\]", "[表情]");
}
Map robotMap=((DemoHXSDKHelper)HXSDKHelper.getInstance()).getRobotList();
if(robotMap!=null&&robotMap.containsKey(message.getFrom())){
String nick = robotMap.get(message.getFrom()).getNick();
if(!TextUtils.isEmpty(nick)){
return nick + ": " + ticker;
}else{
return message.getFrom() + ": " + ticker;
}
}else{
return message.getFrom() + ": " + ticker;
}
}
@Override
public String getLatestText(EMMessage message, int fromUsersNum, int messageNum) {
return null;
// return fromUsersNum + "个基友,发来了" + messageNum + "条消息";
}
@Override
public Intent getLaunchIntent(EMMessage message) {
//设置点击通知栏跳转事件
Intent intent = new Intent(appContext, ChatActivity.class);
//有电话时优先跳转到通话页面
if(isVideoCalling){
intent = new Intent(appContext, VideoCallActivity.class);
}else if(isVoiceCalling){
intent = new Intent(appContext, VoiceCallActivity.class);
}else{
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());
if(chatType == ChatType.GroupChat){
intent.putExtra("chatType", ChatActivity.CHATTYPE_GROUP);
}else{
intent.putExtra("chatType", ChatActivity.CHATTYPE_CHATROOM);
}
}
}
return intent;
}
};
}
@Override
protected void onConnectionConflict(){
Intent intent = new Intent(appContext, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("conflict", true);
appContext.startActivity(intent);
}
@Override
protected void onCurrentAccountRemoved(){
Intent intent = new Intent(appContext, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Constant.ACCOUNT_REMOVED, true);
appContext.startActivity(intent);
}
@Override
protected HXSDKModel createModel() {
return new DemoHXSDKModel(appContext);
}
@Override
public HXNotifier createNotifier(){
return new HXNotifier(){
public synchronized void onNewMsg(final EMMessage message) {
if(EMChatManager.getInstance().isSlientMessage(message)){
return;
}
String chatUsename = null;
List 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);
}
}
};
}
/**
* get demo HX SDK Model
*/
public DemoHXSDKModel getModel(){
return (DemoHXSDKModel) hxModel;
}
/**
* 获取内存中好友user list
*
* @return
*/
public Map getContactList() {
if (getHXId() != null && contactList == null) {
contactList = ((DemoHXSDKModel) getModel()).getContactList();
}