@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_group_simle_details);
    tv_name = (TextView) findViewById(R.id.name);
    tv_admin = (TextView) findViewById(R.id.tv_admin);
    btn_add_group = (Button) findViewById(R.id.btn_add_to_group);
    tv_introduction = (TextView) findViewById(R.id.tv_introduction);
    progressBar = (ProgressBar) findViewById(R.id.loading);

    EMGroupInfo groupInfo = (EMGroupInfo) getIntent().getSerializableExtra("groupinfo");
    String groupname = null;
    if (groupInfo != null) {
      groupname = groupInfo.getGroupName();
      groupid = groupInfo.getGroupId();
    } else {
      group = PublicGroupsSeachActivity.searchedGroup;
      if (group == null) return;
      groupname = group.getGroupName();
      groupid = group.getGroupId();
    }

    tv_name.setText(groupname);

    if (group != null) {
      showGroupDetail();
      return;
    }
    new Thread(
            new Runnable() {

              public void run() {
                // 从服务器获取详情
                try {
                  group = EMGroupManager.getInstance().getGroupFromServer(groupid);
                  runOnUiThread(
                      new Runnable() {
                        public void run() {
                          showGroupDetail();
                        }
                      });
                } catch (final EaseMobException e) {
                  e.printStackTrace();
                  final String st1 =
                      getResources().getString(R.string.Failed_to_get_group_chat_information);
                  runOnUiThread(
                      new Runnable() {
                        public void run() {
                          progressBar.setVisibility(View.INVISIBLE);
                          Toast.makeText(GroupSimpleDetailActivity.this, st1 + e.getMessage(), 1)
                              .show();
                        }
                      });
                }
              }
            })
        .start();
  }
 private void showGroupDetail() {
   progressBar.setVisibility(View.INVISIBLE);
   // 获取详情成功,并且自己不在群中,才让加入群聊按钮可点击
   if (!group.getMembers().contains(EMChatManager.getInstance().getCurrentUser()))
     btn_add_group.setEnabled(true);
   tv_name.setText(group.getGroupName());
   tv_admin.setText(group.getOwner());
   tv_introduction.setText(group.getDescription());
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_group_pick_contacts);

    // String groupName = getIntent().getStringExtra("groupName");
    String groupId = getIntent().getStringExtra("groupId");
    if (groupId == null) { // 创建群组
      isCreatingNewGroup = true;
    } else {
      // 获取此群组的成员列表
      EMGroup group = EMGroupManager.getInstance().getGroup(groupId);
      exitingMembers = group.getMembers();
    }
    if (exitingMembers == null) exitingMembers = new ArrayList<String>();
    // 获取好友列表
    final List<User> alluserList = new ArrayList<User>();
    for (User user : BeewayApplication.getInstance().getContactList().values()) {
      if (!user.getUsername().equals(Constant.NEW_FRIENDS_USERNAME)
          & !user.getUsername().equals(Constant.GROUP_USERNAME)
          & !user.getUsername().equals(Constant.CHAT_ROOM)
          & !user.getUsername().equals(Constant.CHAT_ROBOT)) alluserList.add(user);
    }
    friendDao = new FriendDao(this);
    // 对list进行排序
    Collections.sort(
        alluserList,
        new Comparator<User>() {
          @Override
          public int compare(User lhs, User rhs) {
            return (friendDao.getFriend(lhs.getUsername()).getLetter())
                .compareTo(friendDao.getFriend(rhs.getUsername()).getLetter());
            //				return (lhs.getUsername().compareTo(rhs.getUsername()));

          }
        });

    listView = (ListView) findViewById(R.id.list);
    contactAdapter = new PickContactAdapter(this, R.layout.row_contact_with_checkbox, alluserList);
    listView.setAdapter(contactAdapter);
    ((Sidebar) findViewById(R.id.sidebar)).setListView(listView);
    listView.setOnItemClickListener(
        new OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
            checkBox.toggle();
          }
        });
  }
  @Override
  public void onClick(View v) {
    if (v.getId() == RHelper.get("id.rl_switch_block_groupmsg")) {
      toggleBlockGroup();
    } else if (v.getId() == RHelper.get("id.clear_all_history")) {
      String st9 = getResources().getString(RHelper.get("string.sure_to_empty_this"));
      new EaseAlertDialog(
              GroupDetailsActivity.this,
              null,
              st9,
              null,
              new AlertDialogUser() {

                @Override
                public void onResult(boolean confirmed, Bundle bundle) {
                  if (confirmed) {
                    clearGroupHistory();
                  }
                }
              },
              true)
          .show();
    } else if (v.getId() == RHelper.get("id.rl_blacklist")) // 黑名单列表
    startActivity(
          new Intent(GroupDetailsActivity.this, GroupBlacklistActivity.class)
              .putExtra("groupId", groupId));
    else if (v.getId() == RHelper.get("id.rl_change_group_name"))
      startActivityForResult(
          new Intent(this, EditActivity.class).putExtra("data", group.getGroupName()),
          REQUEST_CODE_EDIT_GROUPNAME);
  }
    @Override
    protected FilterResults performFiltering(CharSequence prefix) {
      FilterResults results = new FilterResults();

      if (mOriginalValues == null) {
        mOriginalValues = new ArrayList<EMConversation>();
      }
      if (prefix == null || prefix.length() == 0) {
        results.values = copyConversationList;
        results.count = copyConversationList.size();
      } else {
        String prefixString = prefix.toString();
        final int count = mOriginalValues.size();
        final ArrayList<EMConversation> newValues = new ArrayList<EMConversation>();

        for (int i = 0; i < count; i++) {
          final EMConversation value = mOriginalValues.get(i);
          String username = value.getUserName();

          EMGroup group = EMGroupManager.getInstance().getGroup(username);
          if (group != null) {
            username = group.getGroupName();
          }

          // First match against the whole ,non-splitted value
          if (username.startsWith(prefixString)) {
            newValues.add(value);
          } else {
            final String[] words = username.split(" ");
            final int wordCount = words.length;

            // Start at index 0, in case valueText starts with space(s)
            for (int k = 0; k < wordCount; k++) {
              if (words[k].startsWith(prefixString)) {
                newValues.add(value);
                break;
              }
            }
          }
        }

        results.values = newValues;
        results.count = newValues.size();
      }
      return results;
    }
  private void refreshMembers() {
    adapter.clear();

    List<String> members = new ArrayList<String>();
    members.addAll(group.getMembers());
    adapter.addAll(members);

    adapter.notifyDataSetChanged();
  }
  /**
   * 获取有聊天记录的users和groups
   *
   * @param context
   * @return
   */
  private List<EMContact> loadUsersWithRecentChat() {
    List<EMContact> resultList = new ArrayList<EMContact>();
    // 获取有聊天记录的users,不包括陌生人
    for (User user : contactList.values()) {
      EMConversation conversation = EMChatManager.getInstance().getConversation(user.getUsername());
      if (conversation.getMsgCount() > 0) {
        resultList.add(user);
      }
    }
    for (EMGroup group : EMGroupManager.getInstance().getAllGroups()) {
      EMConversation conversation = EMChatManager.getInstance().getConversation(group.getGroupId());
      if (conversation.getMsgCount() > 0) {
        resultList.add(group);
      }
    }

    // 排序
    sortUserByLastChatTime(resultList);
    return resultList;
  }
예제 #8
0
    @Override
    public void onInvitationReceived(
        String groupId, String groupName, String inviter, String reason) {
      Log.i("TAG", "onInvitationReceived " + groupName + " " + inviter + " " + reason);
      boolean hasGroup = false;
      for (EMGroup group : EMGroupManager.getInstance().getAllGroups()) {
        if (group.getGroupId().equals(groupId)) {
          hasGroup = true;
          break;
        }
      }
      if (!hasGroup) return;

      // 被邀请
      //            String st3 = getResources().getString(R.string.Invite_you_to_join_a_group_chat);
      //            EMMessage msg = EMMessage.createReceiveMessage(Type.TXT);
      //            msg.setChatType(ChatType.GroupChat);
      //            msg.setFrom(inviter);
      //            msg.setTo(groupId);
      //            msg.setMsgId(UUID.randomUUID().toString());
      //            getNickName(inviter, msg, st3, ONINVITATIONRECEIVED);

    }
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case com.xuhai.telescopes.R.id.rl_switch_block_groupmsg: // 屏蔽或取消屏蔽群组
        toggleBlockGroup();
        break;

      case com.xuhai.telescopes.R.id.clear_all_history: // 清空聊天记录
        String st9 = getResources().getString(com.xuhai.telescopes.R.string.sure_to_empty_this);
        new EaseAlertDialog(
                GroupDetailsActivity.this,
                null,
                st9,
                null,
                new EaseAlertDialog.AlertDialogUser() {

                  @Override
                  public void onResult(boolean confirmed, Bundle bundle) {
                    if (confirmed) {
                      clearGroupHistory();
                    }
                  }
                },
                true)
            .show();

        break;

      case com.xuhai.telescopes.R.id.rl_blacklist: // 黑名单列表
        startActivity(
            new Intent(GroupDetailsActivity.this, GroupBlacklistActivity.class)
                .putExtra("groupId", groupId));
        break;

      case com.xuhai.telescopes.R.id.rl_change_group_name:
        startActivityForResult(
            new Intent(this, EditActivity.class).putExtra("data", group.getGroupName()),
            REQUEST_CODE_EDIT_GROUPNAME);
        break;

      default:
        break;
    }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // 获取传过来的groupid
    groupId = getIntent().getStringExtra("groupId");
    group = EMGroupManager.getInstance().getGroup(groupId);

    // we are not supposed to show the group if we don't find the group
    if (group == null) {
      finish();
      return;
    }

    setContentView(RHelper.get("layout.em_activity_group_details"));
    instance = this;
    st = getResources().getString(RHelper.get("string.people"));
    clearAllHistory = (RelativeLayout) findViewById(RHelper.get("id.clear_all_history"));
    userGridview = (EaseExpandGridView) findViewById(RHelper.get("id.gridview"));
    loadingPB = (ProgressBar) findViewById(RHelper.get("id.progressBar"));
    exitBtn = (Button) findViewById(RHelper.get("id.btn_exit_grp"));
    deleteBtn = (Button) findViewById(RHelper.get("id.btn_exitdel_grp"));
    blacklistLayout = (RelativeLayout) findViewById(RHelper.get("id.rl_blacklist"));
    changeGroupNameLayout = (RelativeLayout) findViewById(RHelper.get("id.rl_change_group_name"));
    idLayout = (RelativeLayout) findViewById(RHelper.get("id.rl_group_id"));
    idLayout.setVisibility(View.VISIBLE);
    idText = (TextView) findViewById(RHelper.get("id.tv_group_id_value"));

    rl_switch_block_groupmsg =
        (RelativeLayout) findViewById(RHelper.get("id.rl_switch_block_groupmsg"));
    switchButton = (EaseSwitchButton) findViewById(RHelper.get("id.switch_btn"));

    rl_switch_block_groupmsg.setOnClickListener(this);

    idText.setText(groupId);
    if (group.getOwner() == null
        || "".equals(group.getOwner())
        || !group.getOwner().equals(EMChatManager.getInstance().getCurrentUser())) {
      exitBtn.setVisibility(View.GONE);
      deleteBtn.setVisibility(View.GONE);
      blacklistLayout.setVisibility(View.GONE);
      changeGroupNameLayout.setVisibility(View.GONE);
    }
    // 如果自己是群主,显示解散按钮
    if (EMChatManager.getInstance().getCurrentUser().equals(group.getOwner())) {
      exitBtn.setVisibility(View.GONE);
      deleteBtn.setVisibility(View.VISIBLE);
    }

    groupRemoveListener = new GroupRemoveListener();
    EMGroupManager.getInstance().addGroupChangeListener(groupRemoveListener);

    ((TextView) findViewById(RHelper.get("id.group_name")))
        .setText(group.getGroupName() + "(" + group.getAffiliationsCount() + st);

    List<String> members = new ArrayList<String>();
    members.addAll(group.getMembers());

    adapter = new GridAdapter(this, RHelper.get("layout.em_grid"), members);
    userGridview.setAdapter(adapter);

    // 保证每次进详情看到的都是最新的group
    updateGroup();

    // 设置OnTouchListener
    userGridview.setOnTouchListener(
        new OnTouchListener() {

          @Override
          public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
                if (adapter.isInDeleteMode) {
                  adapter.isInDeleteMode = false;
                  adapter.notifyDataSetChanged();
                  return true;
                }
                break;
              default:
                break;
            }
            return false;
          }
        });

    clearAllHistory.setOnClickListener(this);
    blacklistLayout.setOnClickListener(this);
    changeGroupNameLayout.setOnClickListener(this);
  }
  /** 清空群聊天记录 */
  private void clearGroupHistory() {

    EMChatManager.getInstance().clearConversation(group.getGroupId());
    Toast.makeText(this, RHelper.get("string.messages_are_empty"), 0).show();
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = inflater.inflate(R.layout.row_chat_history, parent, false);
    }
    ViewHolder holder = (ViewHolder) convertView.getTag();
    if (holder == null) {
      holder = new ViewHolder();
      holder.name = (TextView) convertView.findViewById(R.id.name);
      holder.unreadLabel = (TextView) convertView.findViewById(R.id.unread_msg_number);
      holder.message = (TextView) convertView.findViewById(R.id.message);
      holder.time = (TextView) convertView.findViewById(R.id.time);
      holder.avatar = (ImageView) convertView.findViewById(R.id.avatar);
      holder.msgState = convertView.findViewById(R.id.msg_state);
      holder.list_item_layout = (RelativeLayout) convertView.findViewById(R.id.list_item_layout);
      convertView.setTag(holder);
    }
    if (position % 2 == 0) {
      holder.list_item_layout.setBackgroundResource(R.drawable.mm_listitem);
    } else {
      holder.list_item_layout.setBackgroundResource(R.drawable.mm_listitem_grey);
    }

    // 获取与此用户/群组的会话
    EMConversation conversation = getItem(position);
    // 获取用户username或者群组groupid
    String username = conversation.getUserName();
    if (conversation.getType() == EMConversationType.GroupChat) {
      // 群聊消息,显示群聊头像
      holder.avatar.setImageResource(R.drawable.group_icon);
      EMGroup group = EMGroupManager.getInstance().getGroup(username);
      holder.name.setText(group != null ? group.getGroupName() : username);
    } else if (conversation.getType() == EMConversationType.ChatRoom) {
      holder.avatar.setImageResource(R.drawable.group_icon);
      EMChatRoom room = EMChatManager.getInstance().getChatRoom(username);
      holder.name.setText(
          room != null && !TextUtils.isEmpty(room.getName()) ? room.getName() : username);
    } else {
      UserUtils.setUserAvatar(getContext(), username, holder.avatar);
      if (username.equals(Constant.GROUP_USERNAME)) {
        holder.name.setText("群聊");

      } else if (username.equals(Constant.NEW_FRIENDS_USERNAME)) {
        holder.name.setText("申请与通知");
      }
      Map<String, RobotUser> robotMap =
          ((DemoHXSDKHelper) HXSDKHelper.getInstance()).getRobotList();
      if (robotMap != null && robotMap.containsKey(username)) {
        String nick = robotMap.get(username).getNick();
        if (!TextUtils.isEmpty(nick)) {
          holder.name.setText(nick);
        } else {
          holder.name.setText(username);
        }
      } else {
        UserUtils.setUserNick(username, holder.name);
      }
    }

    if (conversation.getUnreadMsgCount() > 0) {
      // 显示与此用户的消息未读数
      holder.unreadLabel.setText(String.valueOf(conversation.getUnreadMsgCount()));
      holder.unreadLabel.setVisibility(View.VISIBLE);
    } else {
      holder.unreadLabel.setVisibility(View.INVISIBLE);
    }

    if (conversation.getMsgCount() != 0) {
      // 把最后一条消息的内容作为item的message内容
      EMMessage lastMessage = conversation.getLastMessage();
      holder.message.setText(
          SmileUtils.getSmiledText(
              getContext(), getMessageDigest(lastMessage, (this.getContext()))),
          BufferType.SPANNABLE);

      holder.time.setText(DateUtils.getTimestampString(new Date(lastMessage.getMsgTime())));
      if (lastMessage.direct == EMMessage.Direct.SEND
          && lastMessage.status == EMMessage.Status.FAIL) {
        holder.msgState.setVisibility(View.VISIBLE);
      } else {
        holder.msgState.setVisibility(View.GONE);
      }
    }

    return convertView;
  }
  /** 清空群聊天记录 */
  private void clearGroupHistory() {

    EMChatManager.getInstance().clearConversation(group.getGroupId());
    Toast.makeText(this, com.xuhai.telescopes.R.string.messages_are_empty, Toast.LENGTH_LONG)
        .show();
  }