/**
   * 添加contact
   *
   * @param view
   */
  public void addContact(View view) {
    if (EMChatManager.getInstance().getCurrentUser().equals(nameText.getText().toString())) {
      new EaseAlertDialog(this, R.string.not_add_myself).show();
      return;
    }

    if (DemoHelper.getInstance().getContactList().containsKey(nameText.getText().toString())) {
      // 提示已在好友列表中(在黑名单列表里),无需添加
      if (EMContactManager.getInstance()
          .getBlackListUsernames()
          .contains(nameText.getText().toString())) {
        new EaseAlertDialog(this, R.string.user_already_in_contactlist).show();
        return;
      }
      new EaseAlertDialog(this, R.string.This_user_is_already_your_friend).show();
      return;
    }

    progressDialog = new ProgressDialog(this);
    String stri = getResources().getString(R.string.Is_sending_a_request);
    progressDialog.setMessage(stri);
    progressDialog.setCanceledOnTouchOutside(false);
    progressDialog.show();

    new Thread(
            new Runnable() {
              public void run() {

                try {
                  // demo写死了个reason,实际应该让用户手动填入
                  String s = getResources().getString(R.string.Add_a_friend);
                  EMContactManager.getInstance().addContact(toAddUsername, s);
                  runOnUiThread(
                      new Runnable() {
                        public void run() {
                          progressDialog.dismiss();
                          String s1 = getResources().getString(R.string.send_successful);
                          Toast.makeText(getApplicationContext(), s1, 1).show();
                        }
                      });
                } catch (final Exception e) {
                  runOnUiThread(
                      new Runnable() {
                        public void run() {
                          progressDialog.dismiss();
                          String s2 = getResources().getString(R.string.Request_add_buddy_failure);
                          Toast.makeText(getApplicationContext(), s2 + e.getMessage(), 1).show();
                        }
                      });
                }
              }
            })
        .start();
  }
 /** 监听群,好友,连接 by zjb */
 public void setListener() {
   // if (MyApplication.getInstance().isLogin()) {
   Log.i(
       "TAG",
       "HXSDKHelper.getInstance().isLogined() " + DemoHXSDKHelper.getInstance().isLogined());
   if (DemoHXSDKHelper.getInstance().isLogined()) {
     //            inviteMessgeDao = new InviteMessgeDao(this);
     //            userDao = new UserDao(this);
     //            // setContactListener监听联系人的变化等
     EMContactManager.getInstance().setContactListener(new MyContactListener());
     // 注册一个监听连接状态的listener
     EMChatManager.getInstance().addConnectionListener(new MyConnectionListener());
     // 注册群聊相关的listener
     EMGroupManager.getInstance().addGroupChangeListener(new MyGroupChangeListener());
     // 通知sdk,UI 已经初始化完毕,注册了相应的receiver和listener, 可以接受broadcast了
     EMChat.getInstance().setAppInited();
   }
 }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // 防止被T后,没点确定按钮然后按了home键,长期在后台又进app导致的crash
    if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false)) return;
    inputMethodManager =
        (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    listView = (ListView) getView().findViewById(R.id.list);
    sidebar = (Sidebar) getView().findViewById(R.id.sidebar);
    sidebar.setListView(listView);

    // 黑名单列表
    blackList = EMContactManager.getInstance().getBlackListUsernames();
    contactList = new ArrayList<User>();
    // 获取设置contactlist
    getContactList();

    // 搜索框
    query = (EditText) getView().findViewById(R.id.query);
    query.setHint(R.string.search);
    clearSearch = (ImageButton) getView().findViewById(R.id.search_clear);
    query.addTextChangedListener(
        new TextWatcher() {
          public void onTextChanged(CharSequence s, int start, int before, int count) {
            adapter.getFilter().filter(s);
            if (s.length() > 0) {
              clearSearch.setVisibility(View.VISIBLE);
            } else {
              clearSearch.setVisibility(View.INVISIBLE);
            }
          }

          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          public void afterTextChanged(Editable s) {}
        });
    clearSearch.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            query.getText().clear();
            hideSoftKeyboard();
          }
        });

    // 设置adapter
    adapter = new ContactAdapter(getActivity(), R.layout.easeui_row_contact, contactList);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(
        new OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String username = adapter.getItem(position).getUsername();
            if (Constant.NEW_FRIENDS_USERNAME.equals(username)) {
              // 进入申请与通知页面
              User user =
                  ((DemoHXSDKHelper) HXSDKHelper.getInstance())
                      .getContactList()
                      .get(Constant.NEW_FRIENDS_USERNAME);
              user.setUnreadMsgCount(0);
              startActivity(new Intent(getActivity(), NewFriendsMsgActivity.class));
            } else if (Constant.GROUP_USERNAME.equals(username)) {
              // 进入群聊列表页面
              startActivity(new Intent(getActivity(), GroupsActivity.class));
            } else if (Constant.CHAT_ROOM.equals(username)) {
              // 进入聊天室列表页面
              startActivity(new Intent(getActivity(), PublicChatRoomsActivity.class));
            } else if (Constant.CHAT_ROBOT.equals(username)) {
              // 进入Robot列表页面
              startActivity(new Intent(getActivity(), RobotsActivity.class));
            } else {
              // demo中直接进入聊天页面,实际一般是进入用户详情页
              startActivity(
                  new Intent(getActivity(), ChatActivity.class)
                      .putExtra("userId", adapter.getItem(position).getUsername()));
            }
          }
        });
    listView.setOnTouchListener(
        new OnTouchListener() {

          @Override
          public boolean onTouch(View v, MotionEvent event) {
            // 隐藏软键盘
            if (getActivity().getWindow().getAttributes().softInputMode
                != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
              if (getActivity().getCurrentFocus() != null)
                inputMethodManager.hideSoftInputFromWindow(
                    getActivity().getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
          }
        });

    ImageView addContactView = (ImageView) getView().findViewById(R.id.iv_new_contact);
    // 进入添加好友页
    addContactView.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            startActivity(new Intent(getActivity(), AddContactActivity.class));
          }
        });
    registerForContextMenu(listView);

    progressBar = (View) getView().findViewById(R.id.progress_bar);

    contactSyncListener = new HXContactSyncListener();
    HXSDKHelper.getInstance().addSyncContactListener(contactSyncListener);

    blackListSyncListener = new HXBlackListSyncListener();
    HXSDKHelper.getInstance().addSyncBlackListListener(blackListSyncListener);

    contactInfoSyncListener = new HXContactInfoSyncListener();
    ((DemoHXSDKHelper) HXSDKHelper.getInstance())
        .getUserProfileManager()
        .addSyncContactInfoListener(contactInfoSyncListener);

    if (!HXSDKHelper.getInstance().isContactsSyncedWithServer()) {
      progressBar.setVisibility(View.VISIBLE);
    } else {
      progressBar.setVisibility(View.GONE);
    }
  }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // 防止被T后,没点确定按钮然后按了home键,长期在后台又进app导致的crash
    if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false)) return;
    inputMethodManager =
        (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    listView = (ListView) getView().findViewById(R.id.list);
    sidebar = (Sidebar) getView().findViewById(R.id.sidebar);
    sidebar.setListView(listView);
    // 黑名单列表
    blackList = EMContactManager.getInstance().getBlackListUsernames();
    contactList = new ArrayList<User>();
    // 获取设置contactlist
    getContactList();
    // 设置adapter
    // adapter = new ContactAdapter(getActivity(), R.layout.row_contact,
    // contactList, sidebar);
    adapter = new ContactAdapter(getActivity(), R.layout.row_contact, contactList, sidebar, null);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(
        new OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String username = adapter.getItem(position).getUsername();
            if (Constant.NEW_FRIENDS_USERNAME.equals(username)) {
              // 进入申请与通知页面
              User user =
                  ApplicationControl.getInstance()
                      .getContactList()
                      .get(Constant.NEW_FRIENDS_USERNAME);
              user.setUnreadMsgCount(0);
              startActivity(new Intent(getActivity(), NewFriendsMsgActivity.class));
            } else if (Constant.GROUP_USERNAME.equals(username)) {
              // 进入群聊列表页面
              Intent intent = new Intent(getActivity(), GroupsActivity.class);
              intent.putExtra("isFromShare", false);
              startActivity(intent);
            } else {
              // demo中直接进入聊天页面,实际一般是进入用户详情页
              startActivity(
                  new Intent(getActivity(), ChatActivity.class)
                      .putExtra("userId", adapter.getItem(position).getUsername()));
            }
          }
        });
    listView.setOnTouchListener(
        new OnTouchListener() {

          @Override
          public boolean onTouch(View v, MotionEvent event) {
            // 隐藏软键盘
            if (getActivity().getWindow().getAttributes().softInputMode
                != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
              if (getActivity().getCurrentFocus() != null)
                inputMethodManager.hideSoftInputFromWindow(
                    getActivity().getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
          }
        });

    ImageView addContactView = (ImageView) getView().findViewById(R.id.iv_new_contact);
    // 进入添加好友页
    addContactView.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            startActivity(new Intent(getActivity(), AddContactActivity.class));
          }
        });
    registerForContextMenu(listView);
  }