private void resendImage(final ViewHolder holder, Animation sendingAnim, Message msg) {
   ImageContent imgContent = (ImageContent) msg.getContent();
   final String path = imgContent.getLocalThumbnailPath();
   holder.sendingIv.setVisibility(View.VISIBLE);
   holder.sendingIv.startAnimation(sendingAnim);
   holder.picture.setAlpha(0.75f);
   holder.resend.setVisibility(View.GONE);
   holder.progressTv.setVisibility(View.VISIBLE);
   try {
     // 显示上传进度
     msg.setOnContentUploadProgressCallback(
         new ProgressUpdateCallback() {
           @Override
           public void onProgressUpdate(final double progress) {
             mActivity.runOnUiThread(
                 new Runnable() {
                   @Override
                   public void run() {
                     String progressStr = (int) (progress * 100) + "%";
                     holder.progressTv.setText(progressStr);
                   }
                 });
           }
         });
     if (!msg.isSendCompleteCallbackExists()) {
       msg.setOnSendCompleteCallback(
           new BasicCallback() {
             @Override
             public void gotResult(final int status, String desc) {
               holder.sendingIv.clearAnimation();
               holder.sendingIv.setVisibility(View.GONE);
               holder.progressTv.setVisibility(View.GONE);
               holder.picture.setAlpha(1.0f);
               if (status != 0) {
                 HandleResponseCode.onHandle(mContext, status, false);
                 holder.resend.setVisibility(View.VISIBLE);
               }
             }
           });
     }
     JMessageClient.sendMessage(msg);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Пример #2
0
  /**
   * 用于处理拍照发送图片返回结果以及从其他界面回来后刷新聊天标题 或者聊天消息
   *
   * @param requestCode 请求码
   * @param resultCode 返回码
   * @param data intent
   */
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_CANCELED) {
      return;
    }
    if (requestCode == JChatDemoApplication.REQUEST_CODE_TAKE_PHOTO) {
      final Conversation conv = mChatController.getConversation();
      try {
        String originPath = mChatController.getPhotoPath();
        Bitmap bitmap = BitmapLoader.getBitmapFromFile(originPath, 720, 1280);
        ImageContent.createImageContentAsync(
            bitmap,
            new ImageContent.CreateImageContentCallback() {
              @Override
              public void gotResult(int status, String desc, ImageContent imageContent) {
                if (status == 0) {
                  Message msg = conv.createSendMessage(imageContent);
                  Intent intent = new Intent();
                  intent.putExtra(JChatDemoApplication.MsgIDs, new int[] {msg.getId()});
                  if (conv.getType() == ConversationType.group) {
                    intent.putExtra(
                        JChatDemoApplication.GROUP_ID,
                        ((GroupInfo) conv.getTargetInfo()).getGroupID());
                  } else {
                    intent.putExtra(
                        JChatDemoApplication.TARGET_ID,
                        ((UserInfo) conv.getTargetInfo()).getUserName());
                  }
                  handleImgRefresh(intent);
                }
              }
            });
      } catch (NullPointerException e) {
        Log.i(TAG, "onActivityResult unexpected result");
      }
    } else if (resultCode == JChatDemoApplication.RESULT_CODE_SELECT_PICTURE) {
      handleImgRefresh(data);
    } else if (resultCode == JChatDemoApplication.RESULT_CODE_CHAT_DETAIL) {
      if (mChatController.isGroup()) {
        GroupInfo groupInfo = (GroupInfo) mChatController.getConversation().getTargetInfo();
        UserInfo userInfo = groupInfo.getGroupMemberInfo(JMessageClient.getMyInfo().getUserName());
        // 如果自己在群聊中,同时显示群人数
        if (userInfo != null) {
          if (TextUtils.isEmpty(data.getStringExtra(JChatDemoApplication.NAME))) {
            mChatView.setChatTitle(
                this.getString(R.string.group), data.getIntExtra("currentCount", 0));
          } else {
            mChatView.setChatTitle(
                data.getStringExtra(JChatDemoApplication.NAME),
                data.getIntExtra("currentCount", 0));
          }
        } else {
          if (TextUtils.isEmpty(data.getStringExtra(JChatDemoApplication.NAME))) {
            mChatView.setChatTitle(this.getString(R.string.group));
            mChatView.dismissGroupNum();
          } else {
            mChatView.setChatTitle(data.getStringExtra(JChatDemoApplication.NAME));
            mChatView.dismissGroupNum();
          }
        }

      } else mChatView.setChatTitle(data.getStringExtra(JChatDemoApplication.NAME));
      if (data.getBooleanExtra("deleteMsg", false)) {
        mChatController.getAdapter().clearMsgList();
      }
    } else if (resultCode == JChatDemoApplication.RESULT_CODE_FRIEND_INFO) {
      if (!mChatController.isGroup()) {
        String nickname = data.getStringExtra(JChatDemoApplication.NICKNAME);
        if (nickname != null) {
          mChatView.setChatTitle(nickname);
        }
      }
    }
  }
  // 处理图片
  private void handleImgMsg(final Message msg, final ViewHolder holder, final int position) {
    final ImageContent imgContent = (ImageContent) msg.getContent();
    // 先拿本地缩略图
    final String path = imgContent.getLocalThumbnailPath();
    // 接收图片
    if (msg.getDirect() == MessageDirect.receive) {
      if (path == null) {
        // 从服务器上拿缩略图
        imgContent.downloadThumbnailImage(
            msg,
            new DownloadCompletionCallback() {
              @Override
              public void onComplete(int status, String desc, File file) {
                if (status == 0) {
                  Picasso.with(mContext).load(file).into(holder.picture);
                }
              }
            });
      } else {
        setPictureScale(path, holder.picture);
        Picasso.with(mContext).load(new File(path)).into(holder.picture);
      }
      // 群聊中显示昵称
      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());
        }
      }

      switch (msg.getStatus()) {
        case receive_fail:
          holder.picture.setImageResource(R.drawable.jmui_fetch_failed);
          break;
        default:
      }
      // 发送图片方,直接加载缩略图
    } else {
      try {
        setPictureScale(path, holder.picture);
        Picasso.with(mContext).load(new File(path)).into(holder.picture);
      } catch (NullPointerException e) {
        Picasso.with(mContext).load(R.drawable.jmui_picture_not_found).into(holder.picture);
      }

      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.picture.setAlpha(1.0f);
          holder.progressTv.setVisibility(View.GONE);
          holder.resend.setVisibility(View.GONE);
          break;
        case send_fail:
          holder.sendingIv.clearAnimation();
          holder.sendingIv.setVisibility(View.GONE);
          holder.picture.setAlpha(1.0f);
          holder.progressTv.setVisibility(View.GONE);
          holder.resend.setVisibility(View.VISIBLE);
          break;
        case send_going:
          sendingImage(holder, sendingAnim, msg);
          break;
        default:
          holder.picture.setAlpha(0.75f);
          holder.sendingIv.setVisibility(View.VISIBLE);
          holder.sendingIv.startAnimation(sendingAnim);
          holder.progressTv.setVisibility(View.VISIBLE);
          holder.progressTv.setText("0%");
          holder.resend.setVisibility(View.GONE);
          // 从别的界面返回聊天界面,继续发送
          if (!mMsgQueue.isEmpty()) {
            Message message = mMsgQueue.element();
            if (message.getId() == msg.getId()) {
              Log.d(TAG, "Start sending message");
              JMessageClient.sendMessage(message);
              mSendMsgId = message.getId();
              sendingImage(holder, sendingAnim, message);
            }
          }
      }
      // 点击重发按钮,重发图片
      holder.resend.setOnClickListener(
          new OnClickListener() {
            @Override
            public void onClick(View arg0) {
              showResendDialog(holder, sendingAnim, msg);
            }
          });
    }
    if (holder.picture != null) {

      // TODO 点击图片事件

      holder.picture.setOnLongClickListener(
          new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
              String name = msg.getFromUser().getUserName();
              OnClickListener listener =
                  new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                      switch (v.getId()) {
                        case R.id.copy_msg_btn:
                          break;
                        case R.id.forward_msg_btn:
                          mDialog.dismiss();
                          break;
                        case R.id.delete_msg_btn:
                          mConv.deleteMessage(msg.getId());
                          mMsgList.remove(position);
                          notifyDataSetChanged();
                          mDialog.dismiss();
                          break;
                      }
                    }
                  };
              mDialog = DialogCreator.createLongPressMessageDialog(mContext, name, true, listener);
              mDialog.show();
              mDialog
                  .getWindow()
                  .setLayout((int) (0.8 * mWidth), WindowManager.LayoutParams.WRAP_CONTENT);
              return true;
            }
          });
    }
  }