@Override public void onResendClick() { try { if (mType == DBConstant.SHOW_AUDIO_TYPE || mType == DBConstant.SHOW_ORIGIN_TEXT_TYPE) { if (mMsgInfo.getDisplayType() == DBConstant.SHOW_AUDIO_TYPE) { if (mMsgInfo.getSendContent().length < 4) { return; } } } else if (mType == DBConstant.SHOW_IMAGE_TYPE) { logger.d("pic#resend"); // 之前的状态是什么 上传没有成功继续上传 // 上传成功,发送消息 ImageMessage imageMessage = (ImageMessage) mMsgInfo; if (TextUtils.isEmpty(imageMessage.getPath())) { Toast.makeText(ctx, ctx.getString(R.string.image_path_unavaluable), Toast.LENGTH_LONG) .show(); return; } } mMsgInfo.setStatus(MessageConstant.MSG_SENDING); msgObjectList.remove(mPosition); addItem(mMsgInfo); if (imService != null) { imService.getMessageManager().resendMessage(mMsgInfo); } } catch (Exception e) { logger.e("chat#exception:" + e.toString()); } }
private View GifImageMsgRender( final int position, View convertView, final ViewGroup parent, final boolean isMine) { GifImageRenderView imageRenderView; final ImageMessage imageMessage = (ImageMessage) msgObjectList.get(position); UserEntity userEntity = imService.getContactManager().findContact(imageMessage.getFromId()); if (null == convertView) { imageRenderView = GifImageRenderView.inflater(ctx, parent, isMine); } else { imageRenderView = (GifImageRenderView) convertView; } GifView imageView = imageRenderView.getMessageContent(); imageView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View arg0) { final String url = imageMessage.getUrl(); Intent intent = new Intent(ctx, PreviewGifActivity.class); intent.putExtra(IntentConstant.PREVIEW_TEXT_CONTENT, url); ctx.startActivity(intent); ((Activity) ctx).overridePendingTransition(R.anim.tt_image_enter, R.anim.tt_stay); } }); imageRenderView.render(imageMessage, userEntity, ctx); return imageRenderView; }
@Override public int getItemViewType(int position) { try { /** 默认是失败类型 */ RenderType type = RenderType.MESSAGE_TYPE_INVALID; Object obj = msgObjectList.get(position); if (obj instanceof Integer) { type = RenderType.MESSAGE_TYPE_TIME_TITLE; } else if (obj instanceof MessageEntity) { MessageEntity info = (MessageEntity) obj; boolean isMine = info.getFromId() == loginUser.getPeerId(); switch (info.getDisplayType()) { case DBConstant.SHOW_AUDIO_TYPE: type = isMine ? RenderType.MESSAGE_TYPE_MINE_AUDIO : RenderType.MESSAGE_TYPE_OTHER_AUDIO; break; case DBConstant.SHOW_IMAGE_TYPE: ImageMessage imageMessage = (ImageMessage) info; if (CommonUtil.gifCheck(imageMessage.getUrl())) { type = isMine ? RenderType.MESSAGE_TYPE_MINE_GIF_IMAGE : RenderType.MESSAGE_TYPE_OTHER_GIF_IMAGE; } else { type = isMine ? RenderType.MESSAGE_TYPE_MINE_IMAGE : RenderType.MESSAGE_TYPE_OTHER_IMAGE; } break; case DBConstant.SHOW_ORIGIN_TEXT_TYPE: if (info.isGIfEmo()) { type = isMine ? RenderType.MESSAGE_TYPE_MINE_GIF : RenderType.MESSAGE_TYPE_OTHER_GIF; } else { type = isMine ? RenderType.MESSAGE_TYPE_MINE_TETX : RenderType.MESSAGE_TYPE_OTHER_TEXT; } break; case DBConstant.SHOW_MIX_TEXT: // logger.e("混合的消息类型%s", obj); default: break; } } return type.ordinal(); } catch (Exception e) { logger.e(e.getMessage()); return RenderType.MESSAGE_TYPE_INVALID.ordinal(); } }
/** 获取图片消息列表 */ private void getImageList() { for (int i = msgObjectList.size() - 1; i >= 0; --i) { Object item = msgObjectList.get(i); if (item instanceof ImageMessage) { ImageMessage.addToImageMessageList((ImageMessage) item); } } }
/** @param data @Description 处理拍照后的数据 应该是从某个 activity回来的 */ private void handleTakePhotoData(Intent data) { ImageMessage imageMessage = ImageMessage.buildForSend(takePhotoSavePath, loginUser, peerEntity); List<ImageMessage> sendList = new ArrayList<>(1); sendList.add(imageMessage); imService.getMessageManager().sendImages(sendList); // 格式有些问题 pushList(imageMessage); messageEdt.clearFocus(); // 消除焦点 }
private void handleImagePickData(List<ImageItem> list) { ArrayList<ImageMessage> listMsg = new ArrayList<>(); ArrayList<ImageItem> itemList = (ArrayList<ImageItem>) list; for (ImageItem item : itemList) { ImageMessage imageMessage = ImageMessage.buildForSend(item, loginUser, peerEntity); listMsg.add(imageMessage); pushList(imageMessage); } imService.getMessageManager().sendImages(listMsg); }
@Override protected void onDestroy() { logger.d("message_activity#onDestroy:%s", this); historyTimes = 0; imServiceConnector.disconnect(this); EventBus.getDefault().unregister(this); adapter.clearItem(); albumList.clear(); sensorManager.unregisterListener(this, sensor); ImageMessage.clearImageMessageList(); unregisterReceiver(receiver); super.onDestroy(); }
// 触发条件,imservice链接成功,或者newIntent private void initData() { historyTimes = 0; adapter.clearItem(); ImageMessage.clearImageMessageList(); loginUser = imService.getLoginManager().getLoginInfo(); peerEntity = imService.getSessionManager().findPeerEntity(currentSessionKey); // 头像、历史消息加载、取消通知 setTitleByUser(); reqHistoryMsg(); adapter.setImService(imService, loginUser); imService.getUnReadMsgManager().readUnreadSession(currentSessionKey); imService.getNotificationManager().cancelSessionNotifications(currentSessionKey); }
/** 对于混合消息的特殊处理 */ public void updateItemState(final MessageEntity messageEntity) { long dbId = messageEntity.getId(); int msgId = messageEntity.getMsgId(); int len = msgObjectList.size(); for (int index = len - 1; index > 0; index--) { Object object = msgObjectList.get(index); if (object instanceof MessageEntity) { MessageEntity entity = (MessageEntity) object; if (object instanceof ImageMessage) { ImageMessage.addToImageMessageList((ImageMessage) object); } if (entity.getId() == dbId && entity.getMsgId() == msgId) { msgObjectList.set(index, messageEntity); break; } } } notifyDataSetChanged(); }
/** ----------------------添加历史消息----------------- */ public void addItem(final MessageEntity msg) { if (msg.getDisplayType() == DBConstant.MSG_TYPE_SINGLE_TEXT) { if (isMsgGif(msg)) { msg.setGIfEmo(true); } else { msg.setGIfEmo(false); } } int nextTime = msg.getCreated(); if (getCount() > 0) { Object object = msgObjectList.get(getCount() - 1); if (object instanceof MessageEntity) { int preTime = ((MessageEntity) object).getCreated(); boolean needTime = DateUtil.needDisplayTime(preTime, nextTime); if (needTime) { Integer in = nextTime; msgObjectList.add(in); } } } else { Integer in = msg.getCreated(); msgObjectList.add(in); } /** 消息的判断 */ if (msg.getDisplayType() == DBConstant.SHOW_MIX_TEXT) { MixMessage mixMessage = (MixMessage) msg; msgObjectList.addAll(mixMessage.getMsgList()); } else { msgObjectList.add(msg); } if (msg instanceof ImageMessage) { ImageMessage.addToImageMessageList((ImageMessage) msg); } logger.d("#messageAdapter#addItem"); notifyDataSetChanged(); }
/** * 1.头像事件 mine:事件 other事件 图片的状态 消息收到,没收到,图片展示成功,没有成功 触发图片的事件 【长按】 * * <p>图片消息类型的render * * @param position * @param convertView * @param parent * @param isMine * @return */ private View imageMsgRender( final int position, View convertView, final ViewGroup parent, final boolean isMine) { ImageRenderView imageRenderView; final ImageMessage imageMessage = (ImageMessage) msgObjectList.get(position); UserEntity userEntity = imService.getContactManager().findContact(imageMessage.getFromId()); /** 保存在本地的path */ final String imagePath = imageMessage.getPath(); /** 消息中的image路径 */ final String imageUrl = imageMessage.getUrl(); if (null == convertView) { imageRenderView = ImageRenderView.inflater(ctx, parent, isMine); } else { imageRenderView = (ImageRenderView) convertView; } final ImageView messageImage = imageRenderView.getMessageImage(); final int msgId = imageMessage.getMsgId(); imageRenderView.setBtnImageListener( new ImageRenderView.BtnImageListener() { @Override public void onMsgFailure() { /** * 多端同步也不会拉到本地失败的数据 只有isMine才有的状态,消息发送失败 1. 图片上传失败。点击图片重新上传??[也是重新发送] 2. 图片上传成功,但是发送失败。 * 点击重新发送?? */ if (FileUtil.isSdCardAvailuable()) { // // imageMessage.setLoadStatus(MessageStatus.IMAGE_UNLOAD);//如果是图片已经上传成功呢? imageMessage.setStatus(MessageConstant.MSG_SENDING); if (imService != null) { imService.getMessageManager().resendMessage(imageMessage); } updateItemState(msgId, imageMessage); } else { Toast.makeText(ctx, ctx.getString(R.string.sdcard_unavaluable), Toast.LENGTH_LONG) .show(); } } // DetailPortraitActivity 以前用的是DisplayImageActivity 这个类 @Override public void onMsgSuccess() { Intent i = new Intent(ctx, PreviewMessageImagesActivity.class); Bundle bundle = new Bundle(); bundle.putSerializable(IntentConstant.CUR_MESSAGE, imageMessage); i.putExtras(bundle); ctx.startActivity(i); ((Activity) ctx).overridePendingTransition(R.anim.tt_image_enter, R.anim.tt_stay); } }); // 设定触发loadImage的事件 imageRenderView.setImageLoadListener( new ImageRenderView.ImageLoadListener() { @Override public void onLoadComplete(String loaclPath) { logger.d("chat#pic#save image ok"); logger.d("pic#setsavepath:%s", loaclPath); // imageMessage.setPath(loaclPath);//下载的本地路径不再存储 imageMessage.setLoadStatus(MessageConstant.IMAGE_LOADED_SUCCESS); updateItemState(imageMessage); } @Override public void onLoadFailed() { logger.d("chat#pic#onBitmapFailed"); imageMessage.setLoadStatus(MessageConstant.IMAGE_LOADED_FAILURE); updateItemState(imageMessage); logger.d("download failed"); } }); final View messageLayout = imageRenderView.getMessageLayout(); messageImage.setOnLongClickListener( new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { // 创建一个pop对象,然后 分支判断状态,然后显示需要的内容 MessageOperatePopup popup = getPopMenu(parent, new OperateItemClickListener(imageMessage, position)); boolean bResend = (imageMessage.getStatus() == MessageConstant.MSG_FAILURE) || (imageMessage.getLoadStatus() == MessageConstant.IMAGE_UNLOAD); popup.show(messageLayout, DBConstant.SHOW_IMAGE_TYPE, bResend, isMine); return true; } }); /** 父类控件中的发送失败view */ imageRenderView .getMessageFailed() .setOnClickListener( new View.OnClickListener() { @Override public void onClick(View arg0) { // 重发或者重新加载 MessageOperatePopup popup = getPopMenu(parent, new OperateItemClickListener(imageMessage, position)); popup.show(messageLayout, DBConstant.SHOW_IMAGE_TYPE, true, isMine); } }); imageRenderView.render(imageMessage, userEntity, ctx); return imageRenderView; }