Example #1
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);
        }
      }
    }
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ItemViewTag viewTag;
    Bitmap bitmap;
    if (convertView == null) {
      convertView = mInflater.inflate(R.layout.group_grid_view_item, null);
      viewTag =
          new ItemViewTag(
              (CircleImageView) convertView.findViewById(R.id.grid_avatar),
              (TextView) convertView.findViewById(R.id.grid_name),
              (ImageView) convertView.findViewById(R.id.grid_delete_icon));
      convertView.setTag(viewTag);
    } else {
      viewTag = (ItemViewTag) convertView.getTag();
    }

    if (position < mCurrentNum) {
      UserInfo userInfo = mMemberList.get(position);
      viewTag = (ItemViewTag) convertView.getTag();
      viewTag.icon.setVisibility(View.VISIBLE);
      viewTag.name.setVisibility(View.VISIBLE);
      bitmap = NativeImageLoader.getInstance().getBitmapFromMemCache(userInfo.getUserName());
      if (bitmap != null) viewTag.icon.setImageBitmap(bitmap);
      else {
        // 如果mediaID为空,表明用户没有设置过头像,用默认头像
        if (TextUtils.isEmpty(userInfo.getAvatar())) {
          viewTag.icon.setImageResource(R.drawable.head_icon);
        } else {
          File file = userInfo.getAvatarFile();
          // 如果本地存在头像
          if (file != null && file.isFile()) {
            bitmap =
                BitmapLoader.getBitmapFromFile(file.getAbsolutePath(), mDefaultSize, mDefaultSize);
            NativeImageLoader.getInstance().updateBitmapFromCache(userInfo.getUserName(), bitmap);
            viewTag.icon.setImageBitmap(bitmap);
            // 从网上拿头像
          } else {
            viewTag.icon.setImageResource(R.drawable.head_icon);
            final String userName = userInfo.getUserName();
            userInfo.getAvatarFileAsync(
                new DownloadAvatarCallback() {
                  @Override
                  public void gotResult(int status, String desc, File file) {
                    if (status == 0) {
                      Bitmap bitmap =
                          BitmapLoader.getBitmapFromFile(
                              file.getAbsolutePath(), mDefaultSize, mDefaultSize);
                      NativeImageLoader.getInstance().updateBitmapFromCache(userName, bitmap);
                      notifyDataSetChanged();
                    }
                  }
                });
          }
        }
      }

      if (TextUtils.isEmpty(userInfo.getNickname())) {
        viewTag.name.setText(userInfo.getUserName());
      } else {
        viewTag.name.setText(userInfo.getNickname());
      }
    }
    // 是Delete状态
    if (mIsShowDelete) {
      if (position < mCurrentNum) {
        UserInfo userInfo = mMemberList.get(position);
        // 群主不能删除自己
        if (userInfo.getUserName().equals(JMessageClient.getMyInfo().getUserName()))
          viewTag.deleteIcon.setVisibility(View.GONE);
        else viewTag.deleteIcon.setVisibility(View.VISIBLE);

      } else {
        viewTag.deleteIcon.setVisibility(View.INVISIBLE);
        viewTag.icon.setVisibility(View.INVISIBLE);
        viewTag.name.setVisibility(View.INVISIBLE);
      }
      // 非Delete状态
    } else {
      viewTag.deleteIcon.setVisibility(View.INVISIBLE);
      if (position < mCurrentNum) {
        viewTag.icon.setVisibility(View.VISIBLE);
        viewTag.name.setVisibility(View.VISIBLE);
      } else if (position == mCurrentNum) {
        viewTag = (ItemViewTag) convertView.getTag();
        viewTag.icon.setImageResource(R.drawable.chat_detail_add);
        viewTag.icon.setVisibility(View.VISIBLE);
        viewTag.name.setVisibility(View.INVISIBLE);

        // 设置删除群成员按钮
      } else if (position == mCurrentNum + 1) {
        if (mIsCreator && mCurrentNum > 1) {
          viewTag = (ItemViewTag) convertView.getTag();
          viewTag.icon.setImageResource(R.drawable.chat_detail_del);
          viewTag.icon.setVisibility(View.VISIBLE);
          viewTag.name.setVisibility(View.INVISIBLE);
        } else {
          viewTag = (ItemViewTag) convertView.getTag();
          viewTag.icon.setVisibility(View.GONE);
          viewTag.name.setVisibility(View.GONE);
        }
        // 空白项
      } else {
        viewTag = (ItemViewTag) convertView.getTag();
        viewTag.icon.setVisibility(View.INVISIBLE);
        viewTag.name.setVisibility(View.INVISIBLE);
      }
    }

    return convertView;
  }