diff --git a/ease-call-kit/src/main/java/com/hyphenate/easecallkit/EaseCallKit.java b/ease-call-kit/src/main/java/com/hyphenate/easecallkit/EaseCallKit.java index 41a13e7..3ff2ffd 100644 --- a/ease-call-kit/src/main/java/com/hyphenate/easecallkit/EaseCallKit.java +++ b/ease-call-kit/src/main/java/com/hyphenate/easecallkit/EaseCallKit.java @@ -62,6 +62,7 @@ import java.util.List; import java.util.Map; import java.util.TimeZone; +import java.util.Objects; /** @@ -77,6 +78,7 @@ public class EaseCallKit { private EaseCallState callState = EaseCallState.CALL_IDLE; private String channelName; private String fromUserId; //被叫获取主叫的 + private String groupId; public static String deviceId = "android_"; public String clallee_devId; private String callID = null; @@ -272,26 +274,35 @@ public void startSingleCall(final EaseCallType type, final String user,final Ma * 邀请加入多人通话 * 注意:在相关activity结束时需要调用{@link #releaseCall()},防止出现内存泄漏 * @param users 用户ID列表(环信ID列表) + * @param groupId 群组id * @param ext 扩展字段(用户扩展字段) */ - public void startInviteMultipleCall(final String[] users,final Map ext){ - startInviteMultipleCall(users, ext, defaultMultiVideoCls); + public void startInviteMultipleCall(final String[] users,String groupId,final Map ext){ + startInviteMultipleCall(users, groupId,ext, defaultMultiVideoCls); } /** * 邀请加入多人通话 * 注意:在相关activity结束时需要调用{@link #releaseCall()},防止出现内存泄漏 * @param users 用户ID列表(环信ID列表) + * @param conversationId 群组id * @param ext 扩展字段(用户扩展字段) * @param cls 继承自{@link EaseMultipleVideoActivity}的activity */ - public void startInviteMultipleCall(final String[] users,final Map ext, Class cls){ + public void startInviteMultipleCall(final String[] users, String conversationId,final Map ext, Class cls){ if(callState != EaseCallState.CALL_IDLE && callType != EaseCallType.CONFERENCE_CALL){ if(callListener != null){ callListener.onCallError(EaseCallError.PROCESS_ERROR,CALL_PROCESS_ERROR.CALL_STATE_ERROR.code,"current state is busy"); } return; } + if (conversationId == null || conversationId.isEmpty()) { + if(callListener != null){ + callListener.onCallError(EaseCallError.PROCESS_ERROR,CALL_PROCESS_ERROR.CALL_STATE_ERROR.code,"conversation id doesn't be empty"); + } + return; + } + groupId = conversationId; if(users == null || users.length == 0) { if(curCallCls != null){ inviteeUsers.clear(); @@ -349,9 +360,10 @@ public Class getCurrentCallClass() { /** * If you call {@link #startSingleCall(EaseCallType, String, Map)}, {@link #startSingleCall(EaseCallType, String, Map, Class)} - * or {@link #startInviteMultipleCall(String[], Map)}, you should call the method of {@link #releaseCall()} when the {@link #curCallCls} is finishing. + * or {@link #startInviteMultipleCall(String[], String,Map)}, you should call the method of {@link #releaseCall()} when the {@link #curCallCls} is finishing. */ public void releaseCall() { + groupId = null; if(curCallCls != null) { curCallCls = null; } @@ -367,7 +379,7 @@ private void addMessageListener() { public void onMessageReceived(List messages) { for(EMMessage message: messages){ String messageType = message.getStringAttribute(EaseMsgUtils.CALL_MSG_TYPE, ""); - EMLog.d(TAG,"Receive msg:" + message.getMsgId() + " from:" + message.getFrom()+ " messageType:"+ messageType); + EMLog.d(TAG,"Receive msg:" + message.getMsgId() + " from:" + message.getFrom() + " to:" + message.getTo() + " messageType:"+ messageType); //有关通话控制信令 if(TextUtils.equals(messageType, EaseMsgUtils.CALL_MSG_INFO) && !TextUtils.equals(message.getFrom(), EMClient.getInstance().getCurrentUser())) { @@ -447,14 +459,20 @@ public void onMessageReceived(List messages) { public void onCmdMessageReceived(List messages) { for(EMMessage message: messages){ String messageType = message.getStringAttribute(EaseMsgUtils.CALL_MSG_TYPE, ""); - EMLog.d(TAG,"Receive cmdmsg:" + message.getMsgId() + " from:" + message.getFrom() + " messageType:"+ messageType); + EMLog.d(TAG,"Receive cmd msg:" + message.getMsgId() + " from:" + message.getFrom() + " to:" + message.getTo() + " messageType:"+ messageType); + if (message.getChatType() == EMMessage.ChatType.GroupChat) { + groupId = message.conversationId(); + } //有关通话控制信令 - if(TextUtils.equals(messageType, EaseMsgUtils.CALL_MSG_INFO) - && !TextUtils.equals(message.getFrom(), EMClient.getInstance().getCurrentUser())) { + if(TextUtils.equals(messageType, EaseMsgUtils.CALL_MSG_INFO) && !TextUtils.equals(message.getFrom(), EMClient.getInstance().getCurrentUser())) { String action = message.getStringAttribute(EaseMsgUtils.CALL_ACTION, ""); String callerDevId = message.getStringAttribute(EaseMsgUtils.CALL_DEVICE_ID, ""); String fromCallId = message.getStringAttribute(EaseMsgUtils.CLL_ID, ""); String fromUser = message.getFrom(); + if (message.getChatType() == EMMessage.ChatType.GroupChat) { + groupId = message.conversationId(); + fromUser = message.conversationId(); + } String channel = message.getStringAttribute(EaseMsgUtils.CALL_CHANNELNAME, ""); EaseCallAction callAction = EaseCallAction.getfrom(action); switch (callAction){ @@ -475,6 +493,8 @@ public void onCmdMessageReceived(List messages) { //发布消息 EaseLiveDataBus.get().with(EaseCallType.SINGLE_VIDEO_CALL.toString()).postValue(event); } + //清理群组id缓存 + groupId = null; break; case CALL_ALERT: String calleedDeviceId = message.getStringAttribute(EaseMsgUtils.CALLED_DEVICE_ID, ""); @@ -494,6 +514,8 @@ public void onCmdMessageReceived(List messages) { if(!vaild){ //通话无效 callInfoMap.remove(fromCallId); + //清理群组id缓存 + groupId = null; }else{ //收到callId 有效 if(callState == EaseCallState.CALL_IDLE){ @@ -515,6 +537,8 @@ public void onCmdMessageReceived(List messages) { //通话无效 callInfoMap.remove(fromCallId); timeHandler.stopTime(); + //清理群组id缓存 + groupId = null; } } } @@ -529,6 +553,8 @@ public void onCmdMessageReceived(List messages) { event.callerDevId = callerDevId; event.callId = fromCallId; event.userId = fromUser; + //清理群组id缓存 + groupId = null; //发布消息 EaseLiveDataBus.get().with(EaseCallType.SINGLE_VIDEO_CALL.toString()).postValue(event); break; @@ -682,6 +708,10 @@ public String getFromUserId(){ return fromUserId; } + public String getGroupId() { + return groupId; + } + public boolean getIsComingCall(){ return isComingCall; } @@ -727,11 +757,12 @@ private String getProcessNameByApplication() { */ private void sendCmdMsg(BaseEvent event,String username){ final EMMessage message = EMMessage.createSendMessage(EMMessage.Type.CMD); - message.setTo(username); String action="rtcCall"; EMCmdMessageBody cmdBody = new EMCmdMessageBody(action); message.addBody(cmdBody); + message.setTo(username); + EMLog.d(TAG, "发送cmd回复信息 " + " from: " + message.getFrom() + " to: " + message.getTo()); message.setAttribute(EaseMsgUtils.CALL_ACTION, event.callAction.state); message.setAttribute(EaseMsgUtils.CALL_DEVICE_ID, event.callerDevId); message.setAttribute(EaseMsgUtils.CLL_ID,event.callId); @@ -743,18 +774,18 @@ private void sendCmdMsg(BaseEvent event,String username){ }else if(event.callAction == EaseCallAction.CALL_ALERT){ message.setAttribute(EaseMsgUtils.CALLED_DEVICE_ID, deviceId); } - final EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username, EMConversation.EMConversationType.Chat, true); +// final EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username, EMConversation.EMConversationType.Chat, true); message.setMessageStatusCallback(new EMCallBack() { @Override public void onSuccess() { EMLog.d(TAG, "Invite call success"); - conversation.removeMessage(message.getMsgId()); +// conversation.removeMessage(message.getMsgId()); } @Override public void onError(int code, String error) { EMLog.e(TAG, "Invite call error " + code + ", " + error); - conversation.removeMessage(message.getMsgId()); +// conversation.removeMessage(message.getMsgId()); if(callListener != null){ callListener.onCallError(EaseCallError.IM_ERROR,code,error); } @@ -896,8 +927,10 @@ public void sendCmdMsg(BaseEvent event,String username, EMCallBack callBack){ final EMMessage message = EMMessage.createSendMessage(EMMessage.Type.CMD); String action="rtcCall"; EMCmdMessageBody cmdBody = new EMCmdMessageBody(action); - message.setTo(username); message.addBody(cmdBody); + + message.setTo(username); + if(event.callAction.equals(EaseCallAction.CALL_VIDEO_TO_VOICE) || event.callAction.equals(EaseCallAction.CALL_CANCEL)){ cmdBody.deliverOnlineOnly(false); @@ -922,12 +955,12 @@ public void sendCmdMsg(BaseEvent event,String username, EMCallBack callBack){ message.setAttribute(EaseMsgUtils.CALL_DEVICE_ID, ((AnswerEvent) event).callerDevId); message.setAttribute(EaseMsgUtils.CALLED_TRANSE_VOICE, ((AnswerEvent) event).transVoice); } - final EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username, EMConversation.EMConversationType.Chat, true); +// final EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username, EMConversation.EMConversationType.Chat, true); message.setMessageStatusCallback(new EMCallBack() { @Override public void onSuccess() { EMLog.d(TAG, "Invite call success"); - conversation.removeMessage(message.getMsgId()); +// conversation.removeMessage(message.getMsgId()); if(event.callAction == EaseCallAction.CALL_CANCEL){ exitCall(); }else if(event.callAction == EaseCallAction.CALL_CONFIRM_CALLEE){ @@ -942,9 +975,9 @@ public void onSuccess() { @Override public void onError(int code, String error) { EMLog.e(TAG, "Invite call error " + code + ", " + error); - if(conversation != null){ - conversation.removeMessage(message.getMsgId()); - } +// if(conversation != null){ +// conversation.removeMessage(message.getMsgId()); +// } if(event.callAction == EaseCallAction.CALL_CANCEL){ //退出频道 exitCall(); @@ -975,6 +1008,7 @@ public void run() { //重置状态 setCallState(EaseCallState.CALL_IDLE); setCallID(null); + groupId = null; } }); } diff --git a/ease-call-kit/src/main/java/com/hyphenate/easecallkit/base/EaseCallKitListener.java b/ease-call-kit/src/main/java/com/hyphenate/easecallkit/base/EaseCallKitListener.java index a28dc17..68b67df 100644 --- a/ease-call-kit/src/main/java/com/hyphenate/easecallkit/base/EaseCallKitListener.java +++ b/ease-call-kit/src/main/java/com/hyphenate/easecallkit/base/EaseCallKitListener.java @@ -13,12 +13,13 @@ */ public interface EaseCallKitListener{ /** - * 邀请好友进行多人通话 + * 邀请群成员进行多人通话 * @param context + * @param groupId 群组id * @param users 当前通话中已经存在的成员 * @param ext 自定义扩展字段 */ - void onInviteUsers(Context context,String []users,JSONObject ext); + void onInviteUsers(Context context,String groupId,String []users,JSONObject ext); /** diff --git a/ease-call-kit/src/main/java/com/hyphenate/easecallkit/ui/EaseMultipleVideoActivity.java b/ease-call-kit/src/main/java/com/hyphenate/easecallkit/ui/EaseMultipleVideoActivity.java index 4403200..e309775 100644 --- a/ease-call-kit/src/main/java/com/hyphenate/easecallkit/ui/EaseMultipleVideoActivity.java +++ b/ease-call-kit/src/main/java/com/hyphenate/easecallkit/ui/EaseMultipleVideoActivity.java @@ -136,6 +136,8 @@ public class EaseMultipleVideoActivity extends EaseBaseCallActivity implements V private String ringFile; private MediaPlayer mediaPlayer; private RelativeLayout viewGroupLayout; + //群组id + private String conversationId; volatile private boolean mConfirm_ring = false; @@ -609,6 +611,7 @@ private void initParams(Bundle bundle){ username = bundle.getString("username"); channelName = bundle.getString("channelName"); callType = EaseCallKit.getInstance().getCallType(); + conversationId = EaseCallKit.getInstance().getGroupId(); // invite_ext = bundle.getString(CALL_INVITE_EXT); isMuteState=bundle.getBoolean("isMuteState",false); isVideoMute = bundle.getBoolean("isVideoMute",true); @@ -618,6 +621,7 @@ private void initParams(Bundle bundle){ username = EaseCallKit.getInstance().getFromUserId(); channelName = EaseCallKit.getInstance().getChannelName(); callType = EaseCallKit.getInstance().getCallType(); + conversationId = EaseCallKit.getInstance().getGroupId(); } } @@ -799,6 +803,10 @@ public void onClick(View view) { showFloatWindow(); }else if(view.getId() == R.id.btn_invite){ if(listener != null){ + if (conversationId == null && conversationId.isEmpty()) { + listener.onCallError(EaseCallKit.EaseCallError.IM_ERROR, EaseCallKit.CALL_PROCESS_ERROR.CALL_STATE_ERROR.code, "group id doesn't be empty"); + return; + } Set userset = mUidsList.keySet(); int size = userset.size(); JSONObject object = EaseCallKit.getInstance().getInviteExt(); @@ -810,9 +818,9 @@ public void onClick(View view) { users[i++] = mUidsList.get(user).getUserAccount(); } } - listener.onInviteUsers(getApplicationContext(),users,object); + listener.onInviteUsers(getApplicationContext(),conversationId,users,object); }else{ - listener.onInviteUsers(getApplicationContext(),null,object); + listener.onInviteUsers(getApplicationContext(),conversationId,null,object); } } } @@ -828,6 +836,7 @@ protected void addLiveDataObserver(){ @Override public void onChanged(BaseEvent event) { if(event != null&&timehandler!=null) { + EMLog.d(TAG,"Receive call event:" + event.callAction + " from:" + event.callerDevId + " to:" + event.calleeDevId + " userId:"+ event.userId); switch (event.callAction){ case CALL_ALERT: AlertEvent alertEvent = (AlertEvent)event; @@ -1240,72 +1249,62 @@ public void run() { } }); - final EMMessage message = EMMessage.createTxtSendMessage(getApplicationContext().getString(R.string.invited_to_make_multi_party_call), username); - message.setAttribute(EaseMsgUtils.CALL_ACTION, EaseCallAction.CALL_INVITE.state); - message.setAttribute(EaseMsgUtils.CALL_CHANNELNAME, channelName); - message.setAttribute(EaseMsgUtils.CALL_TYPE, callType.code); - message.setAttribute(EaseMsgUtils.CALL_DEVICE_ID, EaseCallKit.deviceId); - JSONObject object = EaseCallKit.getInstance().getInviteExt(); - if (object != null) { - message.setAttribute(CALL_INVITE_EXT, object); - } else { - try { - JSONObject obj = new JSONObject(); - message.setAttribute(CALL_INVITE_EXT, obj); - } catch (Exception e) { - e.getStackTrace(); - } - } - if (EaseCallKit.getInstance().getCallID() == null) { - EaseCallKit.getInstance().setCallID(EaseCallKitUtils.getRandomString(10)); - } - message.setAttribute(EaseMsgUtils.CLL_ID, EaseCallKit.getInstance().getCallID()); - - message.setAttribute(EaseMsgUtils.CLL_TIMESTRAMEP, System.currentTimeMillis()); - message.setAttribute(EaseMsgUtils.CALL_MSG_TYPE, EaseMsgUtils.CALL_MSG_INFO); - - //增加推送字段 -// JSONObject extObject = new JSONObject(); -// try { -// String info = getApplication().getString(R.string.alert_request_multiple_video, EMClient.getInstance().getCurrentUser()); -// extObject.putOpt("em_push_title", info); -// extObject.putOpt("em_push_content", info); -// extObject.putOpt("isRtcCall", true); -// extObject.putOpt("callType", EaseCallType.CONFERENCE_CALL.code); -// } catch (JSONException e) { -// e.printStackTrace(); -// } -// message.setAttribute("em_apns_ext", extObject); - - final EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username, EMConversation.EMConversationType.Chat, true); - message.setMessageStatusCallback(new EMCallBack() { - @Override - public void onSuccess() { - EMLog.d(TAG, "Invite call success username:" + username); - if (listener != null) { - listener.onInViteCallMessageSent(); - } - } - @Override - public void onError(int code, String error) { - EMLog.e(TAG, "Invite call error " + code + ", " + error + " username:" + username); + } + } + //发送定向消息邀请群成员 + final EMMessage message = EMMessage.createTextSendMessage(getApplicationContext().getString(R.string.invited_to_make_multi_party_call), conversationId); + message.setChatType(EMMessage.ChatType.GroupChat); + message.setAttribute(EaseMsgUtils.CALL_ACTION, EaseCallAction.CALL_INVITE.state); + message.setAttribute(EaseMsgUtils.CALL_CHANNELNAME, channelName); + message.setAttribute(EaseMsgUtils.CALL_TYPE, callType.code); + message.setAttribute(EaseMsgUtils.CALL_DEVICE_ID, EaseCallKit.deviceId); + message.setReceiverList(userArray); + JSONObject object = EaseCallKit.getInstance().getInviteExt(); + if (object != null) { + message.setAttribute(CALL_INVITE_EXT, object); + } else { + try { + JSONObject obj = new JSONObject(); + message.setAttribute(CALL_INVITE_EXT, obj); + } catch (Exception e) { + e.getStackTrace(); + } + } + if (EaseCallKit.getInstance().getCallID() == null) { + EaseCallKit.getInstance().setCallID(EaseCallKitUtils.getRandomString(10)); + } + message.setAttribute(EaseMsgUtils.CLL_ID, EaseCallKit.getInstance().getCallID()); - if (listener != null) { - listener.onCallError(EaseCallKit.EaseCallError.IM_ERROR, code, error); - listener.onInViteCallMessageSent(); - } - } + message.setAttribute(EaseMsgUtils.CLL_TIMESTRAMEP, System.currentTimeMillis()); + message.setAttribute(EaseMsgUtils.CALL_MSG_TYPE, EaseMsgUtils.CALL_MSG_INFO); - @Override - public void onProgress(int progress, String status) { + final EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username, EMConversation.EMConversationType.Chat, true); + message.setMessageStatusCallback(new EMCallBack() { + @Override + public void onSuccess() { + EMLog.d(TAG, "Invite call success username:" + username); + if (listener != null) { + listener.onInViteCallMessageSent(); + } + } - } - }); - EMClient.getInstance().chatManager().sendMessage(message); + @Override + public void onError(int code, String error) { + EMLog.e(TAG, "Invite call error " + code + ", " + error + " username:" + username); + + if (listener != null) { + listener.onCallError(EaseCallKit.EaseCallError.IM_ERROR, code, error); + listener.onInViteCallMessageSent(); + } } - } + @Override + public void onProgress(int progress, String status) { + + } + }); + EMClient.getInstance().chatManager().sendMessage(message); //初始化邀请列表 EaseCallKit.getInstance().InitInviteeUsers(); } @@ -1326,7 +1325,6 @@ private void sendCmdMsg(BaseEvent event, String username){ }else{ cmdBody.deliverOnlineOnly(true); } - message.setAttribute(EaseMsgUtils.CALL_ACTION, event.callAction.state); message.setAttribute(EaseMsgUtils.CALL_DEVICE_ID, EaseCallKit.deviceId); message.setAttribute(EaseMsgUtils.CLL_ID, EaseCallKit.getInstance().getCallID()); @@ -1343,12 +1341,12 @@ private void sendCmdMsg(BaseEvent event, String username){ message.setAttribute(EaseMsgUtils.CALLED_DEVICE_ID, ((AnswerEvent) event).calleeDevId); message.setAttribute(EaseMsgUtils.CALL_DEVICE_ID, ((AnswerEvent) event).callerDevId); } - final EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username, EMConversation.EMConversationType.Chat, true); +// final EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username, EMConversation.EMConversationType.Chat, true); message.setMessageStatusCallback(new EMCallBack() { @Override public void onSuccess() { EMLog.d(TAG, "Invite call success"); - conversation.removeMessage(message.getMsgId()); +// conversation.removeMessage(message.getMsgId()); if(event.callAction == EaseCallAction.CALL_CANCEL){ //退出频道 //exitChannel(); @@ -1361,7 +1359,7 @@ public void onSuccess() { @Override public void onError(int code, String error) { EMLog.e(TAG, "Invite call error " + code + ", " + error); - conversation.removeMessage(message.getMsgId()); +// conversation.removeMessage(message.getMsgId()); if(listener != null){ listener.onCallError(EaseCallKit.EaseCallError.IM_ERROR,code,error); }