/**
         * Called when the user selects a property of a person in their address book (ex. phone,
         * email, location,...) This method will allow them to send a text or email inviting them to
         * Anypic.
         */
        @Override
        public boolean shouldContinueAfterSelectingPerson(
            ABPeoplePickerNavigationController peoplePicker,
            ABPerson person,
            ABProperty property,
            int identifier) {

          if (property == ABPersonProperty.Email) {
            String email = person.getEmailAddresses().get(identifier).getAddress();
            selectedEmailAddress = email;

            if (MFMailComposeViewController.canSendMail()
                && MFMessageComposeViewController.canSendText()) {
              // ask user
              UIActionSheet actionSheet =
                  new UIActionSheet(
                      "Invite",
                      new UIActionSheetDelegateAdapter() {
                        @Override
                        public void clicked(UIActionSheet actionSheet, long buttonIndex) {
                          if (buttonIndex == actionSheet.getCancelButtonIndex()) {
                            return;
                          }

                          if (buttonIndex == 0) {
                            presentMailComposeViewController(selectedEmailAddress);
                          } else if (buttonIndex == 1) {
                            presentMessageComposeViewController(selectedEmailAddress);
                          }
                        }
                      },
                      "Cancel",
                      null,
                      "Email",
                      "iMessage");
              actionSheet.showFrom(getTabBarController().getTabBar());
            } else if (MFMailComposeViewController.canSendMail()) {
              // go directly to mail
              presentMailComposeViewController(email);
            } else if (MFMessageComposeViewController.canSendText()) {
              // go directly to iMessage
              presentMessageComposeViewController(email);
            }
          } else if (property == ABPersonProperty.Phone) {
            String phone = person.getPhoneNumbers().get(identifier).getNumber();

            if (MFMessageComposeViewController.canSendText()) {
              presentMessageComposeViewController(phone);
            }
          }

          return false;
        }
        @Override
        public void onTouchUpInside(UIControl control, UIEvent event) {
          ABPeoplePickerNavigationController addressBook = new ABPeoplePickerNavigationController();
          addressBook.setPeoplePickerDelegate(peoplePickerDelegate);

          if (MFMailComposeViewController.canSendMail()
              && MFMessageComposeViewController.canSendText()) {
            addressBook.setDisplayedProperties(
                Arrays.asList(ABPersonProperty.Email, ABPersonProperty.Phone));
          } else if (MFMailComposeViewController.canSendMail()) {
            addressBook.setDisplayedProperties(Arrays.asList(ABPersonProperty.Email));
          } else if (MFMessageComposeViewController.canSendText()) {
            addressBook.setDisplayedProperties(Arrays.asList(ABPersonProperty.Phone));
          }
          presentViewController(addressBook, true, null);
        }
  @Override
  public void viewDidLoad() {
    super.viewDidLoad();

    getTableView().setSeparatorStyle(UITableViewCellSeparatorStyle.None);
    getTableView().setBackgroundColor(UIColor.black());

    getNavigationItem().setTitleView(new UIImageView(UIImage.getImage("TitleFindFriends")));

    if (getNavigationController().getViewControllers().first() == this) {
      UIBarButtonItem dismissLeftBarButtonItem =
          new UIBarButtonItem(
              "Back",
              UIBarButtonItemStyle.Plain,
              new UIBarButtonItem.OnClickListener() {
                @Override
                public void onClick(UIBarButtonItem barButtonItem) {
                  getNavigationController().dismissViewController(true, null);
                }
              });
      getNavigationItem().setLeftBarButtonItem(dismissLeftBarButtonItem);
    } else {
      getNavigationItem().setLeftBarButtonItem(null);
    }

    if (MFMailComposeViewController.canSendMail() || MFMessageComposeViewController.canSendText()) {
      headerView = new UIView(new CGRect(0, 0, 320, 67));
      headerView.setBackgroundColor(UIColor.black());
      UIButton clearButton = new UIButton(UIButtonType.Custom);
      clearButton.setBackgroundColor(UIColor.clear());
      clearButton.addOnTouchUpInsideListener(inviteFriendsButtonAction);
      clearButton.setFrame(headerView.getFrame());
      headerView.addSubview(clearButton);
      String inviteString = "Invite friends";
      CGRect boundingRect =
          NSString.getBoundingRect(
              inviteString,
              new CGSize(310, Float.MAX_VALUE),
              NSStringDrawingOptions.with(
                  NSStringDrawingOptions.TruncatesLastVisibleLine,
                  NSStringDrawingOptions.UsesLineFragmentOrigin),
              new NSAttributedStringAttributes().setFont(UIFont.getBoldSystemFont(18)),
              null);
      CGSize inviteStringSize = boundingRect.getSize();

      UILabel inviteLabel =
          new UILabel(
              new CGRect(
                  10,
                  (headerView.getFrame().getSize().getHeight() - inviteStringSize.getHeight()) / 2,
                  inviteStringSize.getWidth(),
                  inviteStringSize.getHeight()));
      inviteLabel.setText(inviteString);
      inviteLabel.setFont(UIFont.getBoldSystemFont(18));
      inviteLabel.setTextColor(UIColor.white());
      inviteLabel.setBackgroundColor(UIColor.clear());
      headerView.addSubview(inviteLabel);
      getTableView().setTableHeaderView(headerView);
    }
  }