@Override
  protected void onDraw(Canvas canvas) {
    if (user == null && chat == null && encryptedChat == null) {
      return;
    }

    if (cellLayout == null) {
      requestLayout();
      return;
    }

    if (useSeparator) {
      int h = getMeasuredHeight();
      if (!usePadding) {
        canvas.drawLine(0, h - 1, getMeasuredWidth(), h - 1, linePaint);
      } else {
        canvas.drawLine(
            AndroidUtilities.dp(11),
            h - 1,
            getMeasuredWidth() - AndroidUtilities.dp(11),
            h - 1,
            linePaint);
      }
    }

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

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

    canvas.save();
    canvas.translate(cellLayout.nameLeft, cellLayout.nameTop);
    cellLayout.nameLayout.draw(canvas);
    canvas.restore();

    if (cellLayout.onlineLayout != null) {
      canvas.save();
      canvas.translate(cellLayout.onlineLeft, cellLayout.onlineTop);
      cellLayout.onlineLayout.draw(canvas);
      canvas.restore();
    }

    avatarImage.draw(canvas);
  }
 @Override
 protected void onDetachedFromWindow() {
   super.onDetachedFromWindow();
   if (avatarImage != null) {
     avatarImage.clearImage();
     lastAvatar = null;
   }
 }
  public void update(int mask) {
    int placeHolderId = 0;
    TLRPC.FileLocation photo = null;
    if (user != null) {
      if (user.photo != null) {
        photo = user.photo.photo_small;
      }
      placeHolderId = AndroidUtilities.getUserAvatarForId(user.id);
    } else if (chat != null) {
      if (chat.photo != null) {
        photo = chat.photo.photo_small;
      }
      if (chat.id > 0) {
        placeHolderId = AndroidUtilities.getGroupAvatarForId(chat.id);
      } else {
        placeHolderId = AndroidUtilities.getBroadcastAvatarForId(chat.id);
      }
    }

    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) {
        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",
        placeHolderId == 0 ? null : getResources().getDrawable(placeHolderId),
        false);

    if (getMeasuredWidth() != 0 || getMeasuredHeight() != 0) {
      buildLayout();
    } else {
      requestLayout();
    }
    postInvalidate();
  }