/**
         * 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);
    }
  }
 @SuppressWarnings({"unchecked", "rawtypes"})
 private void sendMail() {
   MFMailComposeViewController picker = new MFMailComposeViewController();
   picker.setMailComposeDelegate(
       new MFMailComposeViewControllerDelegate.Adapter() {
         @Override
         public void mailComposeControllerDidFinish(
             MFMailComposeViewController controller, MFMailComposeResult result, NSError error) {
           controller.dismissViewController(true, null);
         }
       });
   NSArray usersTo = new NSArray(new NSString("*****@*****.**"));
   picker.setToRecipients(usersTo);
   picker.setSubject("Test Mail");
   window.getRootViewController().presentViewController(picker, true, null);
 }
  private void presentMailComposeViewController(String recipient) {
    // Create the compose email view controller
    MFMailComposeViewController composeEmailViewController = new MFMailComposeViewController();

    // Set the recipient to the selected email and a default text
    composeEmailViewController.setMailComposeDelegate(mailComposeDelegate);
    composeEmailViewController.setSubject("Join me on Anypic");
    composeEmailViewController.setToRecipients(Arrays.asList(recipient));
    composeEmailViewController.setMessageBody(
        "<h2>Share your pictures, share your story.</h2><p><a href=\"http://anypic.org\">Anypic</a> is the easiest way to share photos with your friends. Get the app and share your fun photos with the world.</p><p><a href=\"http://anypic.org\">Anypic</a> is fully powered by <a href=\"http://parse.com\">Parse</a>.</p>",
        true);

    // Dismiss the current modal view controller and display the compose
    // email one.
    // Note that we do not animate them. Doing so would require us to
    // present the compose
    // mail one only *after* the address book is dismissed.
    dismissViewController(false, null);
    presentViewController(composeEmailViewController, false, null);
  }