@Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_tick:
        // create group.
        String groupName = edtName.getText().toString().trim();
        if (Helper.isEmpty(groupName)) {
          Toast.makeText(getActivity(), "Please input the group name.", Toast.LENGTH_SHORT).show();
          return false;
        }

        String inviteUsers = clientId;
        for (int i = 0; i < mSourceDataList.size(); i++) {
          if (mSourceDataList.get(i).getIsSelected()) {
            if (Helper.isEmpty(inviteUsers)) {
              inviteUsers = mSourceDataList.get(i).getFriend_userId();
            } else {
              inviteUsers += "," + mSourceDataList.get(i).getFriend_userId();
            }

            mSourceDataList.get(i).setIsSelected(false);
          }
        }

        if (Helper.isEmpty(inviteUsers)) {
          Toast.makeText(getActivity(), "Please select one friend at least.", Toast.LENGTH_SHORT)
              .show();
          return false;
        }
        BroadcastManager.sendCreateGroupMessage(getActivity(), groupName, "", inviteUsers);
        dialog.show();
        return true;
      case android.R.id.home:
        Helper.simulateKey(KeyEvent.KEYCODE_BACK);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }
  private void initView() {

    btnCreateGroup.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {

            if (selectFriend) {
              // create group.
              String groupName = edtName.getText().toString().trim();
              if (Helper.isEmpty(groupName)) {
                Toast.makeText(getActivity(), "Please input the group name.", Toast.LENGTH_SHORT)
                    .show();
              }

              String inviteUsers = clientId;
              for (int i = 0; i < mSourceDataList.size(); i++) {
                if (mSourceDataList.get(i).getIsSelected()) {
                  if (Helper.isEmpty(inviteUsers)) {
                    inviteUsers = mSourceDataList.get(i).getFriend_userId();
                  } else {
                    inviteUsers += "," + mSourceDataList.get(i).getFriend_userId();
                  }

                  mSourceDataList.get(i).setIsSelected(false);
                }
              }

              if (Helper.isEmpty(inviteUsers)) {
                Toast.makeText(
                        getActivity(), "Please select one friend at least.", Toast.LENGTH_SHORT)
                    .show();
              }
              BroadcastManager.sendCreateGroupMessage(getActivity(), groupName, "", inviteUsers);
              dialog.show();
            } else {
              FragmentFriends f = new FragmentFriends();
              f.setSelectFriend(true);
              home().setFragment(f);
            }
          }
        });

    dialog = new ProgressDialog(getActivity());
    dialog.setMessage(getResources().getString(R.string.loading));

    sbContacts.setTextView(tvDialog);
    sbContacts.setOnTouchingLetterChangedListener(
        new ContactsSideBar.OnTouchingLetterChangedListener() {

          @Override
          public void onTouchingLetterChanged(String s) {

            int position = mFriendAdapter.getPositionForSection(s.charAt(0));
            if (position != -1) {
              lvContacts.setSelection(position);
            }
          }
        });

    lvContacts.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            if (!selectFriend) {
              home().setFragment(new FragmentChatList());
              home().changeTabBar(0);
              DBFriend friend = mSourceDataList.get(position);

              long dbChannelId;
              // check if the chat already exist;  if not create a new chat;
              List<DBChat> chatList =
                  home()
                      .getDBManager()
                      .getChat(Constants.PRIVATE_CHANNEL_ID, friend.getFriend_userId());
              if (chatList.size() == 0) {
                // create new chat
                DBChat chat =
                    new DBChat(
                        null,
                        clientId,
                        Constants.PRIVATE_CHANNEL_ID,
                        friend.getFriend_userId(),
                        friend.getName(),
                        "",
                        "",
                        friend.getAvatar(),
                        "0");
                dbChannelId = home().getDBManager().saveChat(chat);
              } else {
                dbChannelId = chatList.get(0).getId();
              }

              home()
                  .setFragment(
                      new FragmentChat(
                          dbChannelId,
                          Constants.PRIVATE_CHANNEL_ID,
                          friend.getFriend_userId(),
                          friend.getName()));
            } else {
              if (mSourceDataList.get(position).getIsSelected()) {
                mSourceDataList.get(position).setIsSelected(false);
              } else {
                mSourceDataList.get(position).setIsSelected(true);
              }
              mFriendAdapter.notifyDataSetChanged();
            }
          }
        });

    lvContacts.setOnScrollListener(
        new AbsListView.OnScrollListener() {
          @Override
          public void onScrollStateChanged(AbsListView view, int scrollState) {}

          @Override
          public void onScroll(
              AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            if (mSourceDataList.size() > 0) {
              int section = getSectionForPosition(firstVisibleItem);
              int nextSection = getSectionForPosition(firstVisibleItem + 1);
              int nextSecPosition = getPositionForSection(+nextSection);
              if (firstVisibleItem != lastFirstVisibleItem) {
                ViewGroup.MarginLayoutParams params =
                    (ViewGroup.MarginLayoutParams) titleLayout.getLayoutParams();
                params.topMargin = 0;
                titleLayout.setLayoutParams(params);
                title.setText(mSourceDataList.get(getPositionForSection(section)).getSortLetters());
              }
              if (nextSecPosition == firstVisibleItem + 1) {
                View childView = view.getChildAt(0);
                if (childView != null) {
                  int titleHeight = titleLayout.getHeight();
                  int bottom = childView.getBottom();
                  ViewGroup.MarginLayoutParams params =
                      (ViewGroup.MarginLayoutParams) titleLayout.getLayoutParams();
                  if (bottom < titleHeight) {
                    float pushedDistance = bottom - titleHeight;
                    params.topMargin = (int) pushedDistance;
                    titleLayout.setLayoutParams(params);
                  } else {
                    if (params.topMargin != 0) {
                      params.topMargin = 0;
                      titleLayout.setLayoutParams(params);
                    }
                  }
                }
              }
              lastFirstVisibleItem = firstVisibleItem;
            }
          }
        });

    home()
        .setMessageListener(
            new HomeActivity.MessageReceiveListener() {
              @Override
              public void onReceive(String cmd, String data) {
                try {
                  dialog.dismiss();
                  JSONObject json = new JSONObject(data);
                  if (cmd.equals(Constants.CMD_FRIEND_LIST)) {
                    JSONArray jsonFriends = json.getJSONArray(Keys.DATA);
                    mSourceDataList.clear();
                    for (int i = 0; i < jsonFriends.length(); i++) {
                      String friendId = jsonFriends.getJSONObject(i).getString(Keys.USER_ID);
                      String username = jsonFriends.getJSONObject(i).getString(Keys.USER_NAME);
                      String avatar = jsonFriends.getJSONObject(i).getString(Keys.USER_AVATAR);
                      DBFriend friend =
                          new DBFriend(null, clientId, friendId, username, avatar, "", "");
                      mSourceDataList.add(friend);
                      // save friend
                      home().getDBManager().saveFriend(friend);
                    }

                    filledData();
                    mFriendAdapter = new SortFriendsAdapter(getActivity(), mSourceDataList);
                    lvContacts.setAdapter(mFriendAdapter);

                  } else if (cmd.equals(Constants.CMD_CREATE_GROUP)
                      && json.getInt(Keys.STATUS_CODE) == Constants.CREATE_GROUP_SUCCESSFULLY) {
                    //                        selectFriend = false;
                    // create group successfully.
                    JSONObject groupData = json.getJSONObject(Keys.DATA);
                    String channalId = groupData.getString(Keys.ID);
                    String groupName = groupData.getString(Keys.NAME);
                    String groupAvatar = groupData.getString(Keys.USER_AVATAR);

                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATE_TIME_JSON);

                    DBChat chat =
                        new DBChat(
                            null,
                            clientId,
                            channalId,
                            Constants.GROUP_USER_ID,
                            groupName,
                            "",
                            sdf.format(date),
                            groupAvatar,
                            "0");
                    long dbChannelId = home().getDBManager().saveChat(chat);
                    home().popBackstack();
                    home()
                        .setFragment(
                            new FragmentChat(
                                dbChannelId, channalId, Constants.GROUP_USER_ID, groupName));
                  } else {
                    dialog.dismiss();
                    if (json.has(Keys.MESSAGE)) {
                      Helper.showAlert(getActivity(), json.getString(Keys.MESSAGE));
                    }
                  }

                } catch (Exception e) {
                  Toast.makeText(getActivity(), e.getMessage().toString(), Toast.LENGTH_SHORT)
                      .show();
                }
              }
            });

    //        //get friend from database first.
    //        List<DBFriend> list = home().getDBManager().searchDB(Constants.DB_FRIEND);
    //        if (list.size() > 0) {
    //            mSourceDataList.clear();
    //            mSourceDataList.addAll(list);
    //            filledData();
    //            mFriendAdapter = new SortFriendsAdapter(getActivity(), mSourceDataList);
    //            lvContacts.setAdapter(mFriendAdapter);
    //        } else {
    // get friends
    BroadcastManager.sendGetFriends(getActivity());
    //        }
  }