private static Cursor doQuery(
     CharSequence constraint,
     int limit,
     Long directoryId,
     Account account,
     ContentResolver resolver,
     Query query) {
   final Uri.Builder builder =
       query
           .getContentFilterUri()
           .buildUpon()
           .appendPath(constraint.toString())
           .appendQueryParameter(
               ContactsContract.LIMIT_PARAM_KEY,
               String.valueOf(limit + BaseRecipientAdapter.ALLOWANCE_FOR_DUPLICATES));
   if (directoryId != null) {
     builder.appendQueryParameter(
         ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(directoryId));
   }
   if (account != null) {
     builder.appendQueryParameter(BaseRecipientAdapter.PRIMARY_ACCOUNT_NAME, account.name);
     builder.appendQueryParameter(BaseRecipientAdapter.PRIMARY_ACCOUNT_TYPE, account.type);
   }
   final Cursor cursor = resolver.query(builder.build(), query.getProjection(), null, null, null);
   return cursor;
 }
  // TODO: this is VERY similar to the BaseRecipientAdapter. Can we combine
  // somehow?
  @Override
  public void bindView(View view, Context context, Cursor cursor) {
    int position = cursor.getPosition();

    TextView display = (TextView) view.findViewById(android.R.id.title);
    ImageView imageView = (ImageView) view.findViewById(android.R.id.icon);
    RecipientEntry entry = getRecipientEntry(position);
    if (position == 0) {
      display.setText(cursor.getString(Queries.Query.NAME));
      display.setVisibility(View.VISIBLE);
      // TODO: see if this needs to be done outside the main thread
      // as it may be too slow to get immediately.
      imageView.setImageURI(entry.getPhotoThumbnailUri());
      imageView.setVisibility(View.VISIBLE);
    } else {
      display.setVisibility(View.GONE);
      imageView.setVisibility(View.GONE);
    }
    TextView destination = (TextView) view.findViewById(android.R.id.text1);
    destination.setText(cursor.getString(Queries.Query.DESTINATION));

    TextView destinationType = (TextView) view.findViewById(android.R.id.text2);
    if (destinationType != null) {
      destinationType.setText(
          mQuery
              .getTypeLabel(
                  context.getResources(),
                  cursor.getInt(Queries.Query.DESTINATION_TYPE),
                  cursor.getString(Queries.Query.DESTINATION_LABEL))
              .toString()
              .toUpperCase());
    }
  }