/**
   * Build a {@link Uri} into the {@link Data} table for the requested {@link
   * Contacts#CONTENT_LOOKUP_URI} style {@link Uri}.
   */
  private Uri getDataUri(Uri lookupUri) {
    // TODO: Formalize method of extracting LOOKUP_KEY
    final List<String> path = lookupUri.getPathSegments();
    final boolean validLookup = path.size() >= 3 && "lookup".equals(path.get(1));
    if (!validLookup) {
      // We only accept valid lookup-style Uris
      throw new IllegalArgumentException("Expecting lookup-style Uri");
    } else if (path.size() == 3) {
      // No direct _ID provided, so force a lookup
      lookupUri = TContacts.lookupContact(mContext.getContentResolver(), lookupUri);
    }

    final long contactId = ContentUris.parseId(lookupUri);
    return Uri.withAppendedPath(
        ContentUris.withAppendedId(TContacts.CONTENT_URI, contactId),
        Contacts.Data.CONTENT_DIRECTORY);
  }
    @Override
    protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
      try {
        if (this != mQueryHandler) {
          Log.d(TAG, "onQueryComplete: discard result, the query handler is reset!");
          return;
        }

        switch (token) {
          case TOKEN_PHOTO_QUERY:
            {
              // Set the photo
              Bitmap photoBitmap = null;
              if (cursor != null && cursor.moveToFirst() && !cursor.isNull(PhotoQuery.PHOTO)) {
                byte[] photoData = cursor.getBlob(PhotoQuery.PHOTO);
                photoBitmap = BitmapFactory.decodeByteArray(photoData, 0, photoData.length, null);
              }

              if (photoBitmap == null) {
                photoBitmap = loadPlaceholderPhoto(null);
              }
              mPhotoView.setImageBitmap(photoBitmap);
              if (cookie != null && cookie instanceof Uri) {
                mPhotoView.assignContactUri((Uri) cookie);
              }
              invalidate();
              break;
            }
          case TOKEN_CONTACT_INFO:
            {
              if (cursor != null && cursor.moveToFirst()) {
                bindContactInfo(cursor);
                Uri lookupUri =
                    TContacts.getLookupUri(
                        cursor.getLong(ContactQuery._ID),
                        cursor.getString(ContactQuery.LOOKUP_KEY));
                startPhotoQuery(
                    cursor.getLong(ContactQuery.PHOTO_ID),
                    lookupUri,
                    false /* don't reset query handler */);
                invalidate();
              } else {
                // shouldn't really happen
                setDisplayName(null, null);
                setSocialSnippet(null);
                setPhoto(loadPlaceholderPhoto(null));
              }
              break;
            }
          case TOKEN_PHONE_LOOKUP:
            {
              if (cursor != null && cursor.moveToFirst()) {
                long contactId = cursor.getLong(PHONE_LOOKUP_CONTACT_ID_COLUMN_INDEX);
                String lookupKey = cursor.getString(PHONE_LOOKUP_CONTACT_LOOKUP_KEY_COLUMN_INDEX);
                bindFromContactUriInternal(
                    TContacts.getLookupUri(contactId, lookupKey),
                    false /* don't reset query handler */);
              } else {
                String phoneNumber = (String) cookie;
                setDisplayName(phoneNumber, null);
                setSocialSnippet(null);
                setPhoto(loadPlaceholderPhoto(null));
                mPhotoView.assignContactFromPhone(phoneNumber, true);
              }
              break;
            }
          case TOKEN_EMAIL_LOOKUP:
            {
              if (cursor != null && cursor.moveToFirst()) {
                long contactId = cursor.getLong(EMAIL_LOOKUP_CONTACT_ID_COLUMN_INDEX);
                String lookupKey = cursor.getString(EMAIL_LOOKUP_CONTACT_LOOKUP_KEY_COLUMN_INDEX);
                bindFromContactUriInternal(
                    TContacts.getLookupUri(contactId, lookupKey),
                    false /* don't reset query handler */);
              } else {
                String emailAddress = (String) cookie;
                setDisplayName(emailAddress, null);
                setSocialSnippet(null);
                setPhoto(loadPlaceholderPhoto(null));
                mPhotoView.assignContactFromEmail(emailAddress, true);
              }
              break;
            }
        }
      } finally {
        if (cursor != null) {
          cursor.close();
        }
      }
    }