public ProfileSearchCell(Context context) {
    super(context);

    if (namePaint == null) {
      namePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      namePaint.setColor(0xff212121);
      namePaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));

      nameEncryptedPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      nameEncryptedPaint.setColor(0xff00a60e);
      nameEncryptedPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));

      onlinePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      onlinePaint.setColor(Theme.MSG_LINK_TEXT_COLOR);

      offlinePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      offlinePaint.setColor(0xff999999);

      linePaint = new Paint();
      linePaint.setColor(0xffdcdcdc);

      countPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      countPaint.setColor(0xffffffff);
      countPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));

      broadcastDrawable = getResources().getDrawable(R.drawable.list_broadcast);
      lockDrawable = getResources().getDrawable(R.drawable.list_secret);
      groupDrawable = getResources().getDrawable(R.drawable.list_group);
      countDrawable = getResources().getDrawable(R.drawable.dialogs_badge);
      countDrawableGrey = getResources().getDrawable(R.drawable.dialogs_badge2);
      checkDrawable = getResources().getDrawable(R.drawable.check_list);
      botDrawable = getResources().getDrawable(R.drawable.bot_list);
    }

    namePaint.setTextSize(AndroidUtilities.dp(17));
    nameEncryptedPaint.setTextSize(AndroidUtilities.dp(17));
    onlinePaint.setTextSize(AndroidUtilities.dp(16));
    offlinePaint.setTextSize(AndroidUtilities.dp(16));
    countPaint.setTextSize(AndroidUtilities.dp(13));

    avatarImage = new ImageReceiver(this);
    avatarImage.setRoundRadius(AndroidUtilities.dp(26));
    avatarDrawable = new AvatarDrawable();
  }
  public ChatContactCell(Context context) {
    super(context);
    if (namePaint == null) {
      namePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      namePaint.setTextSize(AndroidUtilities.dp(15));

      phonePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
      phonePaint.setTextSize(AndroidUtilities.dp(15));
      phonePaint.setColor(theme.ChatContactCell_phonePaint_color);

      addContactDrawableIn =
          getResources().getDrawable(theme.ChatContactCell_addContactDrawableIn_R);
      addContactDrawableOut =
          getResources().getDrawable(theme.ChatContactCell_addContactDrawableOut_R);
    }
    avatarImage = new ImageReceiver(this);
    avatarImage.setRoundRadius(AndroidUtilities.dp(21));
    avatarDrawable = new AvatarDrawable();
  }
  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);

    if (currentMessageObject == null) {
      return;
    }

    int x;

    if (currentMessageObject.isOutOwner()) {
      x = layoutWidth - backgroundWidth + AndroidUtilities.dp(8);
    } else {
      if (isChat && currentMessageObject.messageOwner.from_id > 0) {
        x = AndroidUtilities.dp(69);
      } else {
        x = AndroidUtilities.dp(16);
      }
    }
    avatarImage.setImageCoords(
        x, AndroidUtilities.dp(9) + namesOffset, AndroidUtilities.dp(42), AndroidUtilities.dp(42));
  }
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (currentMessageObject == null) {
      return;
    }

    avatarImage.draw(canvas);

    if (nameLayout != null) {
      canvas.save();
      canvas.translate(
          avatarImage.getImageX() + avatarImage.getImageWidth() + AndroidUtilities.dp(9),
          AndroidUtilities.dp(10) + namesOffset);
      namePaint.setColor(
          AvatarDrawable.getColorForId(currentMessageObject.messageOwner.media.user_id));
      nameLayout.draw(canvas);
      canvas.restore();
    }
    if (phoneLayout != null) {
      canvas.save();
      canvas.translate(
          avatarImage.getImageX() + avatarImage.getImageWidth() + AndroidUtilities.dp(9),
          AndroidUtilities.dp(31) + namesOffset);
      phoneLayout.draw(canvas);
      canvas.restore();
    }

    if (drawAddButton) {
      Drawable addContactDrawable;
      if (currentMessageObject.isOutOwner()) {
        addContactDrawable = addContactDrawableOut;
      } else {
        addContactDrawable = addContactDrawableIn;
      }
      setDrawableBounds(
          addContactDrawable,
          avatarImage.getImageX() + namesWidth + AndroidUtilities.dp(78),
          AndroidUtilities.dp(13) + namesOffset);
      addContactDrawable.draw(canvas);
    }
  }
  @Override
  protected void onDraw(Canvas canvas) {
    if (user == null && chat == null && encryptedChat == null) {
      return;
    }

    if (useSeparator) {
      if (LocaleController.isRTL) {
        canvas.drawLine(
            0,
            getMeasuredHeight() - 1,
            getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline),
            getMeasuredHeight() - 1,
            linePaint);
      } else {
        canvas.drawLine(
            AndroidUtilities.dp(AndroidUtilities.leftBaseline),
            getMeasuredHeight() - 1,
            getMeasuredWidth(),
            getMeasuredHeight() - 1,
            linePaint);
      }
    }

    if (drawAlpha != 1) {
      canvas.saveLayerAlpha(
          0,
          0,
          canvas.getWidth(),
          canvas.getHeight(),
          (int) (255 * drawAlpha),
          Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
    }

    if (drawNameLock) {
      setDrawableBounds(lockDrawable, nameLockLeft, nameLockTop);
      lockDrawable.draw(canvas);
    } else if (drawNameGroup) {
      setDrawableBounds(groupDrawable, nameLockLeft, nameLockTop);
      groupDrawable.draw(canvas);
    } else if (drawNameBroadcast) {
      setDrawableBounds(broadcastDrawable, nameLockLeft, nameLockTop);
      broadcastDrawable.draw(canvas);
    } else if (drawNameBot) {
      setDrawableBounds(botDrawable, nameLockLeft, nameLockTop);
      botDrawable.draw(canvas);
    }

    if (nameLayout != null) {
      canvas.save();
      canvas.translate(nameLeft, nameTop);
      nameLayout.draw(canvas);
      canvas.restore();
      if (drawCheck) {
        if (LocaleController.isRTL) {
          setDrawableBounds(
              checkDrawable,
              nameLeft - AndroidUtilities.dp(4) - checkDrawable.getIntrinsicWidth(),
              nameLockTop);
        } else {
          setDrawableBounds(
              checkDrawable,
              nameLeft + (int) nameLayout.getLineWidth(0) + AndroidUtilities.dp(4),
              nameLockTop);
        }
        checkDrawable.draw(canvas);
      }
    }

    if (onlineLayout != null) {
      canvas.save();
      canvas.translate(onlineLeft, AndroidUtilities.dp(40));
      onlineLayout.draw(canvas);
      canvas.restore();
    }

    if (countLayout != null) {
      if (MessagesController.getInstance().isDialogMuted(dialog_id)) {
        setDrawableBounds(
            countDrawableGrey,
            countLeft - AndroidUtilities.dp(5.5f),
            countTop,
            countWidth + AndroidUtilities.dp(11),
            countDrawableGrey.getIntrinsicHeight());
        countDrawableGrey.draw(canvas);
      } else {
        setDrawableBounds(
            countDrawable,
            countLeft - AndroidUtilities.dp(5.5f),
            countTop,
            countWidth + AndroidUtilities.dp(11),
            countDrawable.getIntrinsicHeight());
        countDrawable.draw(canvas);
      }
      canvas.save();
      canvas.translate(countLeft, countTop + AndroidUtilities.dp(4));
      countLayout.draw(canvas);
      canvas.restore();
    }

    avatarImage.draw(canvas);
  }
  public void update(int mask) {
    TLRPC.FileLocation photo = null;
    if (user != null) {
      if (user.photo != null) {
        photo = user.photo.photo_small;
      }
      avatarDrawable.setInfo(user);
    } else if (chat != null) {
      if (chat.photo != null) {
        photo = chat.photo.photo_small;
      }
      avatarDrawable.setInfo(chat);
    } else {
      avatarDrawable.setInfo(0, null, null, false);
    }

    if (mask != 0) {
      boolean continueUpdate = false;
      if ((mask & MessagesController.UPDATE_MASK_AVATAR) != 0 && user != null
          || (mask & MessagesController.UPDATE_MASK_CHAT_AVATAR) != 0 && chat != null) {
        if (lastAvatar != null && photo == null
            || lastAvatar == null
                && photo != null
                && lastAvatar != null
                && photo != null
                && (lastAvatar.volume_id != photo.volume_id
                    || lastAvatar.local_id != photo.local_id)) {
          continueUpdate = true;
        }
      }
      if (!continueUpdate && (mask & MessagesController.UPDATE_MASK_STATUS) != 0 && user != null) {
        int newStatus = 0;
        if (user.status != null) {
          newStatus = user.status.expires;
        }
        if (newStatus != lastStatus) {
          continueUpdate = true;
        }
      }
      if (!continueUpdate && ((mask & MessagesController.UPDATE_MASK_NAME) != 0 && user != null)
          || (mask & MessagesController.UPDATE_MASK_CHAT_NAME) != 0 && chat != null) {
        String newName;
        if (user != null) {
          newName = user.first_name + user.last_name;
        } else {
          newName = chat.title;
        }
        if (!newName.equals(lastName)) {
          continueUpdate = true;
        }
      }
      if (!continueUpdate
          && drawCount
          && (mask & MessagesController.UPDATE_MASK_READ_DIALOG_MESSAGE) != 0) {
        TLRPC.TL_dialog dialog = MessagesController.getInstance().dialogs_dict.get(dialog_id);
        if (dialog != null && dialog.unread_count != lastUnreadCount) {
          continueUpdate = true;
        }
      }

      if (!continueUpdate) {
        return;
      }
    }

    if (user != null) {
      if (user.status != null) {
        lastStatus = user.status.expires;
      } else {
        lastStatus = 0;
      }
      lastName = user.first_name + user.last_name;
    } else if (chat != null) {
      lastName = chat.title;
    }

    lastAvatar = photo;
    avatarImage.setImage(photo, "50_50", avatarDrawable, null, false);

    if (getMeasuredWidth() != 0 || getMeasuredHeight() != 0) {
      buildLayout();
    } else {
      requestLayout();
    }
    postInvalidate();
  }
  public void buildLayout() {
    CharSequence nameString;
    TextPaint currentNamePaint;

    drawNameBroadcast = false;
    drawNameLock = false;
    drawNameGroup = false;
    drawCheck = false;
    drawNameBot = false;

    if (encryptedChat != null) {
      drawNameLock = true;
      dialog_id = ((long) encryptedChat.id) << 32;
      if (!LocaleController.isRTL) {
        nameLockLeft = AndroidUtilities.dp(AndroidUtilities.leftBaseline);
        nameLeft =
            AndroidUtilities.dp(AndroidUtilities.leftBaseline + 4)
                + lockDrawable.getIntrinsicWidth();
      } else {
        nameLockLeft =
            getMeasuredWidth()
                - AndroidUtilities.dp(AndroidUtilities.leftBaseline + 2)
                - lockDrawable.getIntrinsicWidth();
        nameLeft = AndroidUtilities.dp(11);
      }
      nameLockTop = AndroidUtilities.dp(16.5f);
    } else {
      if (chat != null) {
        if (chat.id < 0) {
          dialog_id = AndroidUtilities.makeBroadcastId(chat.id);
          drawNameBroadcast = true;
          nameLockTop = AndroidUtilities.dp(28.5f);
        } else {
          dialog_id = -chat.id;
          if (ChatObject.isChannel(chat) && !chat.megagroup) {
            drawNameBroadcast = true;
            nameLockTop = AndroidUtilities.dp(28.5f);
          } else {
            drawNameGroup = true;
            nameLockTop = AndroidUtilities.dp(30);
          }
        }
        drawCheck = chat.verified;
        if (!LocaleController.isRTL) {
          nameLockLeft = AndroidUtilities.dp(AndroidUtilities.leftBaseline);
          nameLeft =
              AndroidUtilities.dp(AndroidUtilities.leftBaseline + 4)
                  + (drawNameGroup
                      ? groupDrawable.getIntrinsicWidth()
                      : broadcastDrawable.getIntrinsicWidth());
        } else {
          nameLockLeft =
              getMeasuredWidth()
                  - AndroidUtilities.dp(AndroidUtilities.leftBaseline + 2)
                  - (drawNameGroup
                      ? groupDrawable.getIntrinsicWidth()
                      : broadcastDrawable.getIntrinsicWidth());
          nameLeft = AndroidUtilities.dp(11);
        }
      } else {
        dialog_id = user.id;
        if (!LocaleController.isRTL) {
          nameLeft = AndroidUtilities.dp(AndroidUtilities.leftBaseline);
        } else {
          nameLeft = AndroidUtilities.dp(11);
        }
        if (user.bot) {
          drawNameBot = true;
          if (!LocaleController.isRTL) {
            nameLockLeft = AndroidUtilities.dp(AndroidUtilities.leftBaseline);
            nameLeft =
                AndroidUtilities.dp(AndroidUtilities.leftBaseline + 4)
                    + botDrawable.getIntrinsicWidth();
          } else {
            nameLockLeft =
                getMeasuredWidth()
                    - AndroidUtilities.dp(AndroidUtilities.leftBaseline + 2)
                    - botDrawable.getIntrinsicWidth();
            nameLeft = AndroidUtilities.dp(11);
          }
          nameLockTop = AndroidUtilities.dp(16.5f);
        } else {
          nameLockTop = AndroidUtilities.dp(17);
        }
        drawCheck = user.verified;
      }
    }

    if (currentName != null) {
      nameString = currentName;
    } else {
      String nameString2 = "";
      if (chat != null) {
        nameString2 = chat.title;
      } else if (user != null) {
        nameString2 = UserObject.getUserName(user);
      }
      nameString = nameString2.replace('\n', ' ');
    }
    if (nameString.length() == 0) {
      if (user != null && user.phone != null && user.phone.length() != 0) {
        nameString = PhoneFormat.getInstance().format("+" + user.phone);
      } else {
        nameString = LocaleController.getString("HiddenName", R.string.HiddenName);
      }
    }
    if (encryptedChat != null) {
      currentNamePaint = nameEncryptedPaint;
    } else {
      currentNamePaint = namePaint;
    }

    int onlineWidth;
    int nameWidth;
    if (!LocaleController.isRTL) {
      onlineWidth = nameWidth = getMeasuredWidth() - nameLeft - AndroidUtilities.dp(14);
    } else {
      onlineWidth =
          nameWidth =
              getMeasuredWidth() - nameLeft - AndroidUtilities.dp(AndroidUtilities.leftBaseline);
    }
    if (drawNameLock) {
      nameWidth -= AndroidUtilities.dp(6) + lockDrawable.getIntrinsicWidth();
    } else if (drawNameBroadcast) {
      nameWidth -= AndroidUtilities.dp(6) + broadcastDrawable.getIntrinsicWidth();
    } else if (drawNameGroup) {
      nameWidth -= AndroidUtilities.dp(6) + groupDrawable.getIntrinsicWidth();
    } else if (drawNameBot) {
      nameWidth -= AndroidUtilities.dp(6) + botDrawable.getIntrinsicWidth();
    }

    if (drawCount) {
      TLRPC.TL_dialog dialog = MessagesController.getInstance().dialogs_dict.get(dialog_id);
      if (dialog != null && dialog.unread_count != 0) {
        lastUnreadCount = dialog.unread_count;
        String countString = String.format("%d", dialog.unread_count);
        countWidth =
            Math.max(AndroidUtilities.dp(12), (int) Math.ceil(countPaint.measureText(countString)));
        countLayout =
            new StaticLayout(
                countString,
                countPaint,
                countWidth,
                Layout.Alignment.ALIGN_CENTER,
                1.0f,
                0.0f,
                false);
        int w = countWidth + AndroidUtilities.dp(18);
        nameWidth -= w;
        if (!LocaleController.isRTL) {
          countLeft = getMeasuredWidth() - countWidth - AndroidUtilities.dp(19);
        } else {
          countLeft = AndroidUtilities.dp(19);
          nameLeft += w;
        }
      } else {
        lastUnreadCount = 0;
        countLayout = null;
      }
    } else {
      lastUnreadCount = 0;
      countLayout = null;
    }

    CharSequence nameStringFinal =
        TextUtils.ellipsize(
            nameString,
            currentNamePaint,
            nameWidth - AndroidUtilities.dp(12),
            TextUtils.TruncateAt.END);
    nameLayout =
        new StaticLayout(
            nameStringFinal,
            currentNamePaint,
            nameWidth,
            Layout.Alignment.ALIGN_NORMAL,
            1.0f,
            0.0f,
            false);

    if (chat == null || subLabel != null) {
      if (!LocaleController.isRTL) {
        onlineLeft = AndroidUtilities.dp(AndroidUtilities.leftBaseline);
      } else {
        onlineLeft = AndroidUtilities.dp(11);
      }

      CharSequence onlineString = "";
      TextPaint currentOnlinePaint = offlinePaint;

      if (subLabel != null) {
        onlineString = subLabel;
      } else if (user != null) {
        if (user.bot) {
          onlineString = LocaleController.getString("Bot", R.string.Bot);
        } else {
          onlineString = LocaleController.formatUserStatus(user);
          if (user != null
              && (user.id == UserConfig.getClientUserId()
                  || user.status != null
                      && user.status.expires > ConnectionsManager.getInstance().getCurrentTime())) {
            currentOnlinePaint = onlinePaint;
            onlineString = LocaleController.getString("Online", R.string.Online);
          }
        }
      }

      CharSequence onlineStringFinal =
          TextUtils.ellipsize(
              onlineString,
              currentOnlinePaint,
              onlineWidth - AndroidUtilities.dp(12),
              TextUtils.TruncateAt.END);
      onlineLayout =
          new StaticLayout(
              onlineStringFinal,
              currentOnlinePaint,
              onlineWidth,
              Layout.Alignment.ALIGN_NORMAL,
              1.0f,
              0.0f,
              false);
      nameTop = AndroidUtilities.dp(13);
      if (subLabel != null && chat != null) {
        nameLockTop -= AndroidUtilities.dp(12);
      }
    } else {
      onlineLayout = null;
      nameTop = AndroidUtilities.dp(25);
    }

    int avatarLeft;
    if (!LocaleController.isRTL) {
      avatarLeft = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 13 : 9);
    } else {
      avatarLeft = getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.isTablet() ? 65 : 61);
    }

    avatarImage.setImageCoords(
        avatarLeft, AndroidUtilities.dp(10), AndroidUtilities.dp(52), AndroidUtilities.dp(52));

    double widthpx;
    float left;
    if (LocaleController.isRTL) {
      if (nameLayout.getLineCount() > 0) {
        left = nameLayout.getLineLeft(0);
        if (left == 0) {
          widthpx = Math.ceil(nameLayout.getLineWidth(0));
          if (widthpx < nameWidth) {
            nameLeft += (nameWidth - widthpx);
          }
        }
      }
      if (onlineLayout != null && onlineLayout.getLineCount() > 0) {
        left = onlineLayout.getLineLeft(0);
        if (left == 0) {
          widthpx = Math.ceil(onlineLayout.getLineWidth(0));
          if (widthpx < onlineWidth) {
            onlineLeft += (onlineWidth - widthpx);
          }
        }
      }
    } else {
      if (nameLayout.getLineCount() > 0) {
        left = nameLayout.getLineRight(0);
        if (left == nameWidth) {
          widthpx = Math.ceil(nameLayout.getLineWidth(0));
          if (widthpx < nameWidth) {
            nameLeft -= (nameWidth - widthpx);
          }
        }
      }
      if (onlineLayout != null && onlineLayout.getLineCount() > 0) {
        left = onlineLayout.getLineRight(0);
        if (left == onlineWidth) {
          widthpx = Math.ceil(onlineLayout.getLineWidth(0));
          if (widthpx < onlineWidth) {
            onlineLeft -= (onlineWidth - widthpx);
          }
        }
      }
    }
  }
 @Override
 protected void onAttachedToWindow() {
   super.onAttachedToWindow();
   avatarImage.onAttachedToWindow();
 }
 @Override
 protected void onDetachedFromWindow() {
   super.onDetachedFromWindow();
   avatarImage.onDetachedFromWindow();
 }
  @Override
  public void setMessageObject(MessageObject messageObject) {
    if (currentMessageObject != messageObject || isUserDataChanged()) {

      int uid = messageObject.messageOwner.media.user_id;
      contactUser = MessagesController.getInstance().getUser(uid);

      drawAddButton =
          contactUser != null
              && uid != UserConfig.getClientUserId()
              && ContactsController.getInstance().contactsDict.get(uid) == null;

      int maxWidth;
      if (AndroidUtilities.isTablet()) {
        maxWidth = (int) (AndroidUtilities.getMinTabletSide() * 0.7f);
      } else {
        maxWidth =
            (int) (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) * 0.7f);
      }
      maxWidth -= AndroidUtilities.dp(58 + (drawAddButton ? 42 : 0));

      if (contactUser != null) {
        if (contactUser.photo != null) {
          currentPhoto = contactUser.photo.photo_small;
        } else {
          currentPhoto = null;
        }
        avatarDrawable.setInfo(contactUser);
      } else {
        currentPhoto = null;
        avatarDrawable.setInfo(uid, null, null, false);
      }
      avatarImage.setImage(currentPhoto, "50_50", avatarDrawable, null, false);

      String currentNameString =
          ContactsController.formatName(
              messageObject.messageOwner.media.first_name,
              messageObject.messageOwner.media.last_name);
      int nameWidth = Math.min((int) Math.ceil(namePaint.measureText(currentNameString)), maxWidth);

      CharSequence stringFinal =
          TextUtils.ellipsize(
              currentNameString.replace("\n", " "), namePaint, nameWidth, TextUtils.TruncateAt.END);
      nameLayout =
          new StaticLayout(
              stringFinal, namePaint, nameWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
      if (nameLayout.getLineCount() > 0) {
        nameWidth = (int) Math.ceil(nameLayout.getLineWidth(0));
      } else {
        nameWidth = 0;
      }

      String phone = messageObject.messageOwner.media.phone_number;
      if (phone != null && phone.length() != 0) {
        if (!phone.startsWith("+")) {
          phone = "+" + phone;
        }
        phone = PhoneFormat.getInstance().format(phone);
      } else {
        phone = LocaleController.getString("NumberUnknown", R.string.NumberUnknown);
      }
      int phoneWidth = Math.min((int) Math.ceil(phonePaint.measureText(phone)), maxWidth);
      stringFinal =
          TextUtils.ellipsize(
              phone.replace("\n", " "), phonePaint, phoneWidth, TextUtils.TruncateAt.END);
      phoneLayout =
          new StaticLayout(
              stringFinal,
              phonePaint,
              phoneWidth,
              Layout.Alignment.ALIGN_NORMAL,
              1.0f,
              0.0f,
              false);
      if (phoneLayout.getLineCount() > 0) {
        phoneWidth = (int) Math.ceil(phoneLayout.getLineWidth(0));
      } else {
        phoneWidth = 0;
      }

      namesWidth = Math.max(nameWidth, phoneWidth);
      backgroundWidth = AndroidUtilities.dp(77 + (drawAddButton ? 42 : 0)) + namesWidth;

      super.setMessageObject(messageObject);
    }
  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    boolean result = false;
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
      if (x >= avatarImage.getImageX()
          && x <= avatarImage.getImageX() + namesWidth + AndroidUtilities.dp(42)
          && y >= avatarImage.getImageY()
          && y <= avatarImage.getImageY() + avatarImage.getImageHeight()) {
        avatarPressed = true;
        result = true;
      } else if (x >= avatarImage.getImageX() + namesWidth + AndroidUtilities.dp(52)
          && y >= AndroidUtilities.dp(13) + namesOffset
          && x <= avatarImage.getImageX() + namesWidth + AndroidUtilities.dp(92)
          && y <= AndroidUtilities.dp(52) + namesOffset) {
        buttonPressed = true;
        result = true;
      }
      if (result) {
        startCheckLongPress();
      }
    } else {
      if (event.getAction() != MotionEvent.ACTION_MOVE) {
        cancelCheckLongPress();
      }
      if (avatarPressed) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
          avatarPressed = false;
          playSoundEffect(SoundEffectConstants.CLICK);
          if (contactUser != null) {
            if (delegate != null) {
              delegate.didPressedUserAvatar(this, contactUser);
            }
          } else {
            if (contactDelegate != null) {
              contactDelegate.didClickPhone(this);
            }
          }
        } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
          avatarPressed = false;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
          if (!(x >= avatarImage.getImageX()
              && x <= avatarImage.getImageX() + namesWidth + AndroidUtilities.dp(42)
              && y >= avatarImage.getImageY()
              && y <= avatarImage.getImageY() + avatarImage.getImageHeight())) {
            avatarPressed = false;
          }
        }
      } else if (buttonPressed) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
          buttonPressed = false;
          playSoundEffect(SoundEffectConstants.CLICK);
          if (contactUser != null && contactDelegate != null) {
            contactDelegate.didClickAddButton(this, contactUser);
          }
        } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
          buttonPressed = false;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
          if (!(x >= avatarImage.getImageX() + namesWidth + AndroidUtilities.dp(52)
              && y >= AndroidUtilities.dp(13) + namesOffset
              && x <= avatarImage.getImageX() + namesWidth + AndroidUtilities.dp(92)
              && y <= AndroidUtilities.dp(52) + namesOffset)) {
            buttonPressed = false;
          }
        }
      }
    }
    if (!result) {
      result = super.onTouchEvent(event);
    }

    return result;
  }