/** * 鍦ㄤ細璇濆垪琛ㄤ腑鎺ユ敹娑堟伅 * * @param event */ public void onEventMainThread(MessageEvent event) { Log.i(TAG, "onEventMainThread MessageEvent execute"); Message msg = event.getMessage(); String targetID = msg.getTargetID(); ConversationType convType = msg.getTargetType(); Conversation conv; if (convType == ConversationType.group) { conv = JMessageClient.getGroupConversation(Integer.parseInt(targetID)); } else { conv = JMessageClient.getSingleConversation(targetID); } if (conv != null && convType == ConversationType.single) { // 濡傛灉缂撳瓨浜嗗ご鍍忥紝鐩存帴鍒锋柊浼氳瘽鍒楄〃 if (NativeImageLoader.getInstance().getBitmapFromMemCache(targetID) != null) { Log.i("Test", "conversation "); mConvListController.refreshConvList(); // 娌℃湁澶村儚锛屼粠Conversation鎷� } else { File file = conv.getAvatarFile(); // 鎷垮埌鍚庣紦瀛樺苟鍒锋柊 if (file != null) { mConvListController.loadAvatarAndRefresh(targetID, file.getAbsolutePath()); // conversation涓病鏈夊ご鍍忥紝鐩存帴鍒锋柊锛孲DK浼氬湪鍚庡彴鑾峰緱澶村儚锛屾嬁鍒板悗浼氭墽琛宱nEvent(ConversationRefreshEvent // conversationRefreshEvent) } else mConvListController.refreshConvList(); } } else { mConvListController.refreshConvList(); } }
public void sendSingleTextMessage(JSONArray data, CallbackContext callbackContext) { Log.i(TAG, " sendSingleTextMessage \n" + data); final CallbackContext cb = callbackContext; try { String username = data.getString(0); String text = data.getString(1); Conversation conversation = JMessageClient.getSingleConversation(username); if (conversation == null) { conversation = Conversation.createSingleConversation(username); } if (conversation == null) { callbackContext.error("无法创建对话"); return; } TextContent content = new TextContent(text); final Message msg = conversation.createSendMessage(content); JMessageClient.sendMessage(msg); callbackContext.success("正在发送"); } catch (JSONException e) { e.printStackTrace(); callbackContext.error("error reading id json"); } }
/** * 褰撹Е鍙慓etUserInfo鍚庯紝寰楀埌Conversation鍚庯紝鍒锋柊鐣岄潰 閫氬父瑙﹀彂鐨勬儏鍐垫槸鏂颁細璇濆垱寤烘椂鍒锋柊鐩爣澶村儚 * * @param conversationRefreshEvent */ public void onEvent(ConversationRefreshEvent conversationRefreshEvent) { Log.i(TAG, "ConversationRefreshEvent execute"); Conversation conv = conversationRefreshEvent.getConversation(); if (conv.getType() == ConversationType.single) { File file = conv.getAvatarFile(); if (file != null) { mConvListController.loadAvatarAndRefresh(conv.getTargetId(), file.getAbsolutePath()); } } else { mConvListController.getAdapter().notifyDataSetChanged(); } }
public void getAllSingleConversation(JSONArray data, CallbackContext callbackContext) { Log.i(TAG, " getAllSingleConversation \n" + data); List<Conversation> list = JMessageClient.getConversationList(); Log.i(TAG, "JMessageGetAllSingleConversation" + list.size()); JSONArray jsonRusult = new JSONArray(); for (int i = 0; i < list.size(); ++i) { Conversation conv = list.get(i); if (conv.getType() == ConversationType.single) { UserInfo info = (UserInfo) conv.getTargetInfo(); Message msg = conv.getLatestMessage(); String contentText = ""; if (msg != null) { switch (msg.getContentType()) { case text: { contentText = ((TextContent) msg.getContent()).getText(); } break; default: break; } } JSONObject jsonItem = new JSONObject(); try { jsonItem.put("username", info.getUserName()); jsonItem.put("nickname", info.getNickname()); // jsonItem.put("avatar", info.getAvatar()); jsonItem.put("lastMessage", contentText); jsonItem.put("unreadCount", conv.getUnReadMsgCnt()); jsonRusult.put(jsonItem); } catch (JSONException e) { e.printStackTrace(); } } } callbackContext.success(jsonRusult); }
public MsgListAdapter(Context context, long groupId) { initData(context); this.mGroupId = groupId; this.mIsGroup = true; this.mConv = JMessageClient.getGroupConversation(mGroupId); this.mMsgList = mConv.getMessagesFromNewest(0, mOffset); reverse(mMsgList); mStart = mOffset; checkSendingImgMsg(); }
private void refreshGroupNum() { Conversation conv = JMessageClient.getGroupConversation(mChatController.getGroupId()); GroupInfo groupInfo = (GroupInfo) conv.getTargetInfo(); if (!TextUtils.isEmpty(groupInfo.getGroupName())) { android.os.Message handleMessage = mHandler.obtainMessage(); handleMessage.what = JChatDemoApplication.REFRESH_GROUP_NAME; Bundle bundle = new Bundle(); bundle.putString(JChatDemoApplication.GROUP_NAME, groupInfo.getGroupName()); bundle.putInt("membersCount", groupInfo.getGroupMembers().size()); handleMessage.setData(bundle); handleMessage.sendToTarget(); } else { android.os.Message handleMessage = mHandler.obtainMessage(); handleMessage.what = JChatDemoApplication.REFRESH_GROUP_NUM; Bundle bundle = new Bundle(); bundle.putInt("membersCount", groupInfo.getGroupMembers().size()); handleMessage.setData(bundle); handleMessage.sendToTarget(); } }
public void getSingleConversationHistoryMessage(JSONArray data, CallbackContext callbackContext) { Log.i(TAG, " getSingleConversationHistoryMessage \n" + data); try { String username = data.getString(0); int from = data.getInt(1); int limit = data.getInt(2); if (limit <= 0 || from < 0) { Log.w(TAG, " JMessageGetSingleHistoryMessage from: " + from + "limit" + limit); return; } Conversation conversation = JMessageClient.getSingleConversation(username); if (conversation == null) { conversation = Conversation.createSingleConversation(username); } if (conversation == null) { callbackContext.error("无法创建对话"); return; } List<Message> list = conversation.getMessagesFromNewest(from, limit); Log.i(TAG, "JMessageGetSingleHistoryMessage list size is" + list.size()); JSONArray jsonRusult = new JSONArray(); for (int i = 0; i < list.size(); ++i) { Message msg = list.get(i); JSONObject obj = this.getJSonFormMessage(msg); jsonRusult.put(obj); } callbackContext.success(jsonRusult); } catch (JSONException e) { e.printStackTrace(); callbackContext.error("error reading id json"); } }
public ChatListAdapter(Context context, String targetID) { this.context = context; this.mActivity = (Activity) context; this.mTargetID = targetID; this.mConv = JMessageClient.getSingleConversation(mTargetID); this.mMsgList = mConv.getMessagesFromNewest(0, mOffset); reverse(mMsgList); mStart = mOffset; List<String> userIDList = new ArrayList<>(); userIDList.add(targetID); userIDList.add(JMessageClient.getMyInfo().getUserName()); }
public MsgListAdapter(Context context, String targetId) { initData(context); this.mTargetId = targetId; this.mConv = JMessageClient.getSingleConversation(mTargetId); this.mMsgList = mConv.getMessagesFromNewest(0, mOffset); reverse(mMsgList); mStart = mOffset; UserInfo userInfo = (UserInfo) mConv.getTargetInfo(); if (!TextUtils.isEmpty(userInfo.getAvatar())) { userInfo.getAvatarBitmap( new GetAvatarBitmapCallback() { @Override public void gotResult(int status, String desc, Bitmap bitmap) { if (status == 0) { notifyDataSetChanged(); } else { HandleResponseCode.onHandle(mContext, status, false); } } }); } checkSendingImgMsg(); }
public void setSendImg(long groupId, int[] msgIds) { Message msg; mConv = JMessageClient.getGroupConversation(groupId); for (int msgId : msgIds) { msg = mConv.getMessage(msgId); if (msg != null) { mMsgList.add(msg); incrementStartPosition(); mMsgQueue.offer(msg); } } Message message = mMsgQueue.element(); sendNextImgMsg(message); notifyDataSetChanged(); }
public void dropDownToRefresh() { if (mConv != null) { List<Message> msgList = mConv.getMessagesFromNewest(mStart, 18); if (msgList != null) { for (Message msg : msgList) { mMsgList.add(0, msg); } if (msgList.size() > 0) { mOffset = msgList.size(); mHasLastPage = true; } else { mHasLastPage = false; } doNotify(); } } }
public void dropDownToRefresh() { if (mConv != null) { List<Message> msgList = mConv.getMessagesFromNewest(mStart, PAGE_MESSAGE_COUNT); if (msgList != null) { for (Message msg : msgList) { mMsgList.add(0, msg); } if (msgList.size() > 0) { checkSendingImgMsg(); mOffset = msgList.size(); mHasLastPage = true; } else { mOffset = 0; mHasLastPage = false; } notifyDataSetChanged(); } } }
private void handleVoiceMsg( final Message msg, final ViewHolder holder, final int position, OnLongClickListener longClickListener) { final VoiceContent content = (VoiceContent) msg.getContent(); final MessageDirect msgDirect = msg.getDirect(); int length = content.getDuration(); String voiceLength = length + mContext.getString(R.string.symbol_second); holder.voiceLength.setText(voiceLength); // 控制语音长度显示,长度增幅随语音长度逐渐缩小 int width = (int) (-0.04 * length * length + 4.526 * length + 75.214); holder.txtContent.setWidth((int) (width * mDensity)); holder.txtContent.setOnLongClickListener(longClickListener); if (msgDirect == MessageDirect.send) { holder.voice.setImageResource(R.drawable.jmui_send_3); final Animation sendingAnim = AnimationUtils.loadAnimation(mContext, R.anim.jmui_rotate); LinearInterpolator lin = new LinearInterpolator(); sendingAnim.setInterpolator(lin); switch (msg.getStatus()) { case send_success: holder.sendingIv.clearAnimation(); holder.sendingIv.setVisibility(View.GONE); holder.resend.setVisibility(View.GONE); break; case send_fail: holder.sendingIv.clearAnimation(); holder.sendingIv.setVisibility(View.GONE); holder.resend.setVisibility(View.VISIBLE); break; case send_going: sendingTextOrVoice(holder, sendingAnim, msg); break; default: } holder.resend.setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { if (msg.getContent() != null) { showResendDialog(holder, sendingAnim, msg); } else { Toast.makeText( mContext, mContext.getString(R.string.sdcard_not_exist_toast), Toast.LENGTH_SHORT) .show(); } } }); } else switch (msg.getStatus()) { case receive_success: if (mIsGroup) { holder.displayName.setVisibility(View.VISIBLE); if (TextUtils.isEmpty(msg.getFromUser().getNickname())) { holder.displayName.setText(msg.getFromUser().getUserName()); } else { holder.displayName.setText(msg.getFromUser().getNickname()); } } holder.voice.setImageResource(R.drawable.jmui_receive_3); // 收到语音,设置未读 if (msg.getContent().getBooleanExtra("isReaded") == null || !msg.getContent().getBooleanExtra("isReaded")) { mConv.updateMessageExtra(msg, "isReaded", false); holder.readStatus.setVisibility(View.VISIBLE); if (mIndexList.size() > 0) { if (!mIndexList.contains(position)) { addTolistAndSort(position); } } else { addTolistAndSort(position); } if (nextPlayPosition == position && autoPlay) { playVoice(position, holder, false); } } else if (msg.getContent().getBooleanExtra("isReaded").equals(true)) { holder.readStatus.setVisibility(View.GONE); } break; case receive_fail: holder.voice.setImageResource(R.drawable.jmui_receive_3); // 接收失败,从服务器上下载 content.downloadVoiceFile( msg, new DownloadCompletionCallback() { @Override public void onComplete(int status, String desc, File file) { if (status != 0) { Toast.makeText( mContext, mContext.getString(R.string.voice_fetch_failed_toast), Toast.LENGTH_SHORT) .show(); } else { Log.i("VoiceMessage", "reload success"); } } }); break; case receive_going: break; default: } holder.txtContent.setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { if (!FileHelper.isSdCardExist() && msg.getDirect() == MessageDirect.send) { Toast.makeText( mContext, mContext.getString(R.string.sdcard_not_exist_toast), Toast.LENGTH_SHORT) .show(); return; } // 如果之前存在播放动画,无论这次点击触发的是暂停还是播放,停止上次播放的动画 if (mVoiceAnimation != null) { mVoiceAnimation.stop(); } // 播放中点击了正在播放的Item 则暂停播放 if (mp.isPlaying() && mPosition == position) { if (msgDirect == MessageDirect.send) { holder.voice.setImageResource(R.anim.jmui_voice_send); } else { holder.voice.setImageResource(R.anim.jmui_voice_receive); } mVoiceAnimation = (AnimationDrawable) holder.voice.getDrawable(); pauseVoice(); mVoiceAnimation.stop(); // 开始播放录音 } else if (msgDirect == MessageDirect.send) { holder.voice.setImageResource(R.anim.jmui_voice_send); mVoiceAnimation = (AnimationDrawable) holder.voice.getDrawable(); // 继续播放之前暂停的录音 if (mSetData && mPosition == position) { mVoiceAnimation.start(); mp.start(); // 否则重新播放该录音或者其他录音 } else { playVoice(position, holder, true); } // 语音接收方特殊处理,自动连续播放未读语音 } else { try { // 继续播放之前暂停的录音 if (mSetData && mPosition == position) { if (mVoiceAnimation != null) { mVoiceAnimation.start(); } mp.start(); // 否则开始播放另一条录音 } else { // 选中的录音是否已经播放过,如果未播放,自动连续播放这条语音之后未播放的语音 if (msg.getContent().getBooleanExtra("isReaded") == null || !msg.getContent().getBooleanExtra("isReaded")) { autoPlay = true; playVoice(position, holder, false); // 否则直接播放选中的语音 } else { holder.voice.setImageResource(R.anim.jmui_voice_receive); mVoiceAnimation = (AnimationDrawable) holder.voice.getDrawable(); playVoice(position, holder, false); } } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } } } }); }
private void playVoice(final int position, final ViewHolder holder, final boolean isSender) { // 记录播放录音的位置 mPosition = position; Message msg = mMsgList.get(position); if (autoPlay) { mConv.updateMessageExtra(msg, "isReaded", true); holder.readStatus.setVisibility(View.GONE); if (mVoiceAnimation != null) { mVoiceAnimation.stop(); mVoiceAnimation = null; } holder.voice.setImageResource(R.anim.jmui_voice_receive); mVoiceAnimation = (AnimationDrawable) holder.voice.getDrawable(); } try { mp.reset(); VoiceContent vc = (VoiceContent) msg.getContent(); Log.i(TAG, "content.getLocalPath:" + vc.getLocalPath()); mFIS = new FileInputStream(vc.getLocalPath()); mFD = mFIS.getFD(); mp.setDataSource(mFD); if (mIsEarPhoneOn) { mp.setAudioStreamType(AudioManager.STREAM_VOICE_CALL); } else { mp.setAudioStreamType(AudioManager.STREAM_MUSIC); } mp.prepare(); mp.setOnPreparedListener( new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mVoiceAnimation.start(); mp.start(); } }); mp.setOnCompletionListener( new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mVoiceAnimation.stop(); mp.reset(); mSetData = false; if (isSender) { holder.voice.setImageResource(R.drawable.jmui_send_3); } else { holder.voice.setImageResource(R.drawable.jmui_receive_3); } if (autoPlay) { int curCount = mIndexList.indexOf(position); Log.d(TAG, "curCount = " + curCount); if (curCount + 1 >= mIndexList.size()) { nextPlayPosition = -1; autoPlay = false; } else { nextPlayPosition = mIndexList.get(curCount + 1); notifyDataSetChanged(); } mIndexList.remove(curCount); } } }); } catch (FileNotFoundException e) { Toast.makeText( mContext, mContext.getString(R.string.file_not_found_toast), Toast.LENGTH_SHORT) .show(); } catch (IOException e) { Toast.makeText( mActivity, mContext.getString(R.string.file_not_found_toast), Toast.LENGTH_SHORT) .show(); } finally { try { if (mFIS != null) { mFIS.close(); } } catch (IOException e) { e.printStackTrace(); } } }
private void initData() { Intent intent = mContext.getIntent(); mTargetID = intent.getStringExtra("targetID"); Log.i("ChatController", "mTargetID " + mTargetID); mGroupID = intent.getLongExtra("groupID", 0); mIsGroup = intent.getBooleanExtra("isGroup", false); boolean fromGroup = intent.getBooleanExtra("fromGroup", false); // 如果是群组,特别处理 if (mIsGroup) { Log.i("Tag", "mGroupID is " + mGroupID); // 判断是否从创建群组跳转过来 if (fromGroup) { String groupName = intent.getStringExtra("groupName"); mChatView.setChatTitle(groupName); mConv = JMessageClient.getGroupConversation(mGroupID); } else { if (mTargetID != null) mGroupID = Long.parseLong(mTargetID); mConv = JMessageClient.getGroupConversation(mGroupID); // 判断自己如果不在群聊中,隐藏群聊详情按钮 JMessageClient.getGroupMembers( mGroupID, new GetGroupMembersCallback(false) { @Override public void gotResult( final int status, final String desc, final List<String> memberList) { mContext.runOnUiThread( new Runnable() { @Override public void run() { if (status == 0) { // 群主解散后,返回memberList为空 if (memberList.isEmpty()) { mChatView.dismissRightBtn(); // 判断自己如果不在memberList中,则隐藏聊天详情按钮 } else if (!memberList.contains(JMessageClient.getMyInfo().getUserName())) mChatView.dismissRightBtn(); else mChatView.showRightBtn(); } else { if (memberList.isEmpty()) { mChatView.dismissRightBtn(); } HandleResponseCode.onHandle(mContext, status); } } }); } }); } // 聊天信息标志改变 mChatView.setGroupIcon(); } else { // 用targetID得到会话 Log.i("Tag", "targetID is " + mTargetID); mConv = JMessageClient.getSingleConversation(mTargetID); } // 如果之前沒有会话记录并且是群聊 if (mConv == null && mIsGroup) { mConv = Conversation.createConversation(ConversationType.group, mGroupID); Log.i("ChatController", "create group success"); // 是单聊 } else if (mConv == null && !mIsGroup) { mConv = Conversation.createConversation(ConversationType.single, mTargetID); } if (mConv != null) { mChatView.setChatTitle(mConv.getDisplayName()); mConv.resetUnreadCount(); } mChatAdapter = new MsgListAdapter(mContext, mIsGroup, mTargetID, mGroupID); mChatView.setChatListAdapter(mChatAdapter); // 滑动到底部 mChatView.setToBottom(); }
public void refresh() { mChatView.setChatTitle(mConv.getDisplayName()); mChatAdapter.refresh(); }
@Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { // 返回按钮 case R.id.return_btn: mConv.resetUnreadCount(); JMessageClient.exitConversaion(); mContext.finish(); break; // 聊天详细信息 case R.id.right_btn: if (mIsShowMoreMenu) { mChatView.dismissMoreMenu(); dismissSoftInput(); mIsShowMoreMenu = false; } mContext.StartChatDetailActivity(mIsGroup, mTargetID, mGroupID); break; // 切换输入 case R.id.switch_voice_ib: mChatView.dismissMoreMenu(); isInputByKeyBoard = !isInputByKeyBoard; if (isInputByKeyBoard) { mChatView.isKeyBoard(); mChatView.mChatInputEt.requestFocus(); mIsShowMoreMenu = true; mChatView.focusToInput(true); } else { mChatView.notKeyBoard(mConv, mChatAdapter); mIsShowMoreMenu = false; Log.i("ChatController", "setConversation success"); // 关闭软键盘 dismissSoftInput(); } break; case R.id.chat_input_et: mChatView.showMoreMenu(); // mChatView.invisibleMoreMenu(); mIsShowMoreMenu = true; showSoftInput(); break; // 发送文本消息 case R.id.send_msg_btn: String msgContent = mChatView.getChatInput(); mChatView.clearInput(); mChatView.setToBottom(); if (msgContent.equals("")) { return; } TextContent content = new TextContent(msgContent); final Message msg = mConv.createSendMessage(content); msg.setOnSendCompleteCallback( new BasicCallback() { @Override public void gotResult(final int status, String desc) { Log.i("ChatController", "send callback " + status + " desc " + desc); if (status != 0) { mContext.runOnUiThread( new Runnable() { @Override public void run() { HandleResponseCode.onHandle(mContext, status); } }); } // 发送成功或失败都要刷新一次 android.os.Message msg = handler.obtainMessage(); msg.what = UPDATE_CHAT_LISTVIEW; Bundle bundle = new Bundle(); bundle.putString("desc", desc); msg.setData(bundle); msg.sendToTarget(); } }); mChatAdapter.addMsgToList(msg); JMessageClient.sendMessage(msg); break; case R.id.expression_btn: // if (mMoreMenuVisible) { // mChatView.invisibleMoreMenu(); // mMoreMenuVisible = false; // // } break; // 点击添加按钮,弹出更多选项菜单 case R.id.add_file_btn: // 如果在语音输入时点击了添加按钮,则显示菜单并切换到输入框 if (!isInputByKeyBoard) { mChatView.isKeyBoard(); isInputByKeyBoard = true; mChatView.showMoreMenu(); mIsShowMoreMenu = true; mChatView.focusToInput(false); } else { if (mIsShowMoreMenu) { if (mMoreMenuVisible) { mChatView.focusToInput(true); showSoftInput(); mMoreMenuVisible = false; } else { dismissSoftInput(); mChatView.focusToInput(false); mMoreMenuVisible = true; } } else { mChatView.focusToInput(false); mChatView.showMoreMenu(); mIsShowMoreMenu = true; mMoreMenuVisible = true; } } break; // 拍照 case R.id.pick_from_camera_btn: takePhoto(); if (mIsShowMoreMenu) { mChatView.dismissMoreMenu(); dismissSoftInput(); mIsShowMoreMenu = false; } break; case R.id.pick_from_local_btn: if (mIsShowMoreMenu) { mChatView.dismissMoreMenu(); dismissSoftInput(); mIsShowMoreMenu = false; } Intent intent = new Intent(); if (mIsGroup) { intent.putExtra("groupID", mGroupID); } else { intent.putExtra("targetID", mTargetID); } intent.putExtra("isGroup", mIsGroup); mContext.StartPickPictureTotalActivity(intent); break; case R.id.send_location_btn: break; } }