/**
   * Lookup the numbers for a given contact.
   *
   * <p>Usually this is not needed because most methods already return the contacts with all known
   * contact numbers. Make sure to call {@link #contactsReadModuleInstalled()} first.
   *
   * @param contact
   */
  public void lookupContactNumbersFor(Contact contact) {
    if (!contactsReadModuleInstalled()) return;

    String lookupKey = contact.getLookupKey();
    // @formatter:off
    final String[] projection =
        new String[] {Phone.NUMBER, Phone.TYPE, Phone.LABEL, Phone.IS_SUPER_PRIMARY};
    // @formatter:on
    final String selection =
        ContactsContract.PhoneLookup.LOOKUP_KEY
            + "=?"
            + AND
            + ContactsContract.Data.MIMETYPE
            + "='"
            + Phone.CONTENT_ITEM_TYPE
            + "'";
    final String[] selectionArgs = new String[] {lookupKey};
    Cursor c =
        mContentResolver.query(MAXS_DATA_CONTENT_URI, projection, selection, selectionArgs, null);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
      String number =
          c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
      int type = c.getInt(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.TYPE));
      String label =
          c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.LABEL));
      boolean superPrimary =
          c.getInt(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY))
                  > 0
              ? true
              : false;
      contact.addNumber(number, type, label, superPrimary);
    }
    c.close();
  }
  /**
   * Lookup a contact by a given nickname.
   *
   * <p>The returned contact will come with all known contact numbers and a lookup key.
   *
   * @param nickname
   * @return A contact, or null if none found or on error.
   */
  public Contact contactByNickname(String nickname) {
    if (!contactsReadModuleInstalled()) return null;

    final String[] projection = new String[] {Data.LOOKUP_KEY, DISPLAY_NAME, Nickname.NAME};
    final String selection =
        Nickname.NAME
            + "=?"
            + AND
            + Data.MIMETYPE
            + "='"
            + Nickname.CONTENT_ITEM_TYPE
            + "'"
            + LIMIT_1;
    final String[] selectionArgs = new String[] {nickname};
    Cursor c =
        mContentResolver.query(MAXS_DATA_CONTENT_URI, projection, selection, selectionArgs, null);

    Contact contact = null;
    if (c.moveToFirst()) {
      String lookupKey = c.getString(c.getColumnIndexOrThrow(Data.LOOKUP_KEY));
      String displayName = c.getString(c.getColumnIndexOrThrow(DISPLAY_NAME));
      nickname = c.getString(c.getColumnIndexOrThrow(Nickname.NAME));
      contact = new Contact(displayName, lookupKey);
      contact.setNickname(nickname);
      lookupContactNumbersFor(contact);
    }
    c.close();

    return contact;
  }
 /**
  * If the given collection contains only one contact with a number, then this contact is returned.
  * Otherwise, if more then one contact with a number exists or if none exists, null is returned.
  *
  * @param contacts
  * @return the one and only contact with number(s) from contacts or null
  */
 public static Contact getOnlyContactWithNumber(Collection<Contact> contacts) {
   Contact res = null;
   for (Contact contact : contacts) {
     if (contact.hasNumbers()) {
       if (res != null) {
         return null;
       } else {
         res = contact;
       }
     }
   }
   return res;
 }
 /**
  * Pretty print for a given contact and contactInfo. If contact is null, only contactInfo will be
  * returned. Otherwise {@code"<contact.getDisplayName()> (<contactInfo>)"} will get returned.
  *
  * @param contactInfo
  * @param contact
  * @return The contact as String
  */
 public static String prettyPrint(String contactInfo, Contact contact) {
   if (contact == null) return contactInfo;
   String displayName = contact.getDisplayName();
   // Contacts can be saved without a name, i.e. just a number as "contact".
   return (displayName == null ? "unknown" : displayName) + " (" + contactInfo + ")";
 }