private void refreshPage(State state) {
    progressBar.setVisibility(View.GONE);
    errorMessage.setVisibility(View.GONE);
    listView.setVisibility(View.GONE);
    switch (state) {
      case LOADING:
        progressBar.setVisibility(View.VISIBLE);
        break;
      case LOADED:
        if (friendListAdapter.isEmpty()) {
          errorMessage.setVisibility(View.VISIBLE);

          errorMessage.setText(R.string.error_no_friends_connected);
          searchText.setEnabled(false);
        } else {
          listView.setVisibility(View.VISIBLE);

          searchText.setEnabled(true);
        }
        break;
      case ERROR:
        errorMessage.setVisibility(View.VISIBLE);
        break;
    }
    refreshMenu();
  }
  @Override
  public void onSaveInstanceState(Bundle outState) {

    Set<Long> toInvite = friendListAdapter.getSelectedList();
    Long inviteLongArray[] = toInvite.toArray(new Long[toInvite.size()]);

    Set<Long> alreadyGrouped = friendListAdapter.getAlreadyInGroupList();
    Long alreadyGroupedLongArray[] = alreadyGrouped.toArray(new Long[alreadyGrouped.size()]);

    outState.putParcelableArrayList(
        FRIEND_LIST_MODELS, new ArrayList<Parcelable>(friendListAdapter.getItems()));
    outState.putLongArray(FRIEND_LIST_STATES, JavaUtil.toPrimitive(inviteLongArray));
    outState.putLongArray(
        FRIEND_LIST_ALREADY_IN_GROUP, JavaUtil.toPrimitive(alreadyGroupedLongArray));

    super.onSaveInstanceState(outState);
  }
  // On friend list fetch success
  @Override
  public void onSuccess(List<SocialMember> response) {

    if (response != null && response.size() > 0) {

      friendListAdapter.setItems(response);
    }
    refreshPage(State.LOADED);
  }
  private void onDone() {
    ArrayList<SocialMember> result =
        (ArrayList<SocialMember>) friendListAdapter.getSelectedFriends();

    Intent returnIntent = new Intent();
    returnIntent.putParcelableArrayListExtra(RESULT_FRIEND_LIST, result);
    getActivity().setResult(Activity.RESULT_OK, returnIntent);
    getActivity().finish();
  }
  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (savedInstanceState != null) {
      ArrayList<SocialMember> friends =
          savedInstanceState.getParcelableArrayList(FRIEND_LIST_MODELS);

      friendListAdapter.setItems(friends);

      // Restore the saved selected set
      long[] selected = savedInstanceState.getLongArray(FRIEND_LIST_STATES);
      if (selected != null) {
        friendListAdapter.setSelectedList(JavaUtil.primitiveLongToSet(selected));
      }
      // Restore the saved already in group set
      long[] alreadyInGroup = savedInstanceState.getLongArray(FRIEND_LIST_ALREADY_IN_GROUP);
      if (alreadyInGroup != null) {
        friendListAdapter.setAlreadyInGroupList(JavaUtil.primitiveLongToSet(alreadyInGroup));
      }
      refreshPage(State.LOADED);
    } else if (getArguments() != null && getArguments().containsKey(EXTRA_OPT_FRIENDS_LIST)) {
      List<SocialMember> friends = getArguments().getParcelableArrayList(EXTRA_OPT_FRIENDS_LIST);
      friendListAdapter.setItems(friends);
      refreshPage(State.LOADED);
    } else {
      friendProvider.getMyFriends(getActivity(), this);
      refreshPage(State.LOADING);
    }

    // Retrieve the already in group list
    if (getArguments() != null && getArguments().containsKey(EXTRA_OPT_IN_GROUP)) {

      long[] alreadyInGroup = getArguments().getLongArray(EXTRA_OPT_IN_GROUP);
      friendListAdapter.setAlreadyInGroupList(JavaUtil.primitiveLongToSet(alreadyInGroup));
    }
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setHasOptionsMenu(true);

    friendProvider = new FacebookProvider();
    friendListAdapter = new FriendListAdapter(getActivity());
    friendListAdapter.setOnSelectChangeListener(
        new FriendListAdapter.OnSelectedItemCountChangeListener() {

          @Override
          public void onSelectionItemCountChange(int itemSelectCount) {
            refreshMenu();
          }
        });

    try {
      segIO.screenViewsTracking("Social Friend Picker");
    } catch (Exception e) {
      logger.error(e);
    }
  }
 private void refreshMenu() {
   if (doneMenuItem != null) {
     TextView doneText = (TextView) doneMenuItem.getActionView();
     doneText.setEnabled(!friendListAdapter.getSelectedFriends().isEmpty());
   }
 }