NekoX/TMessagesProj/src/main/java/org/telegram/ui/Cells/ChatActionCell.java

788 lines
32 KiB
Java
Raw Normal View History

2014-10-14 22:36:15 +02:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2014-10-14 22:36:15 +02:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
2014-10-14 22:36:15 +02:00
*/
package org.telegram.ui.Cells;
import android.content.Context;
2014-10-15 20:43:52 +02:00
import android.graphics.Bitmap;
2014-10-14 22:36:15 +02:00
import android.graphics.Canvas;
2021-09-20 07:54:41 +02:00
import android.graphics.Paint;
2021-06-25 02:43:10 +02:00
import android.graphics.Path;
import android.graphics.RectF;
2014-10-14 22:36:15 +02:00
import android.text.Layout;
2014-10-15 20:43:52 +02:00
import android.text.Spannable;
2014-10-14 22:36:15 +02:00
import android.text.StaticLayout;
2021-09-20 07:54:41 +02:00
import android.text.TextPaint;
2017-03-31 01:58:05 +02:00
import android.text.TextUtils;
2014-10-15 20:43:52 +02:00
import android.text.style.URLSpan;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
2021-06-25 02:43:10 +02:00
import android.view.ViewGroup;
2019-05-14 14:08:05 +02:00
import android.view.accessibility.AccessibilityNodeInfo;
2014-10-14 22:36:15 +02:00
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
2020-07-26 10:03:38 +02:00
import org.telegram.messenger.DownloadController;
2021-02-23 12:53:38 +01:00
import org.telegram.messenger.FileLoader;
import org.telegram.messenger.FileLog;
2020-07-26 10:03:38 +02:00
import org.telegram.messenger.ImageLoader;
2019-05-14 14:08:05 +02:00
import org.telegram.messenger.ImageLocation;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.ImageReceiver;
2017-03-31 01:58:05 +02:00
import org.telegram.messenger.LocaleController;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.MessageObject;
2021-12-30 11:52:40 +01:00
import org.telegram.messenger.NotificationCenter;
import org.telegram.messenger.R;
2020-07-26 10:03:38 +02:00
import org.telegram.messenger.SharedConfig;
2021-02-23 12:53:38 +01:00
import org.telegram.messenger.UserConfig;
2018-07-30 04:07:02 +02:00
import org.telegram.messenger.browser.Browser;
2021-02-23 12:53:38 +01:00
import org.telegram.tgnet.TLObject;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.TLRPC;
2016-04-22 15:49:00 +02:00
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.Components.AvatarDrawable;
2021-02-23 12:53:38 +01:00
import org.telegram.ui.Components.URLSpanNoUnderline;
2021-12-30 11:52:40 +01:00
import org.telegram.ui.Components.spoilers.SpoilerEffect;
2021-02-23 12:53:38 +01:00
import org.telegram.ui.PhotoViewer;
2014-10-14 22:36:15 +02:00
2021-06-25 02:43:10 +02:00
import java.util.ArrayList;
2021-12-30 11:52:40 +01:00
import java.util.List;
import java.util.Stack;
2021-06-25 02:43:10 +02:00
2021-12-30 11:52:40 +01:00
public class ChatActionCell extends BaseCell implements DownloadController.FileDownloadProgressListener, NotificationCenter.NotificationCenterDelegate {
@Override
public void didReceivedNotification(int id, int account, Object... args) {
if (id == NotificationCenter.startSpoilers) {
setSpoilersSuppressed(false);
} else if (id == NotificationCenter.stopSpoilers) {
setSpoilersSuppressed(true);
}
}
public void setSpoilersSuppressed(boolean s) {
for (SpoilerEffect eff : spoilers)
eff.setSuppressUpdates(s);
}
2014-10-14 22:36:15 +02:00
2021-11-05 11:06:49 +01:00
private boolean canDrawInParent;
public interface ChatActionCellDelegate {
2019-05-14 14:08:05 +02:00
default void didClickImage(ChatActionCell cell) {
2019-02-08 03:30:32 +01:00
}
2022-04-16 16:43:17 +02:00
default boolean didLongPress(ChatActionCell cell, float x, float y) {
return false;
2019-02-08 03:30:32 +01:00
}
2021-09-20 07:54:41 +02:00
default void needOpenUserProfile(long uid) {
2019-02-08 03:30:32 +01:00
}
2019-05-14 14:08:05 +02:00
default void didPressBotButton(MessageObject messageObject, TLRPC.KeyboardButton button) {
2019-02-08 03:30:32 +01:00
}
2019-05-14 14:08:05 +02:00
default void didPressReplyMessage(ChatActionCell cell, int id) {
2019-02-08 03:30:32 +01:00
}
2021-03-19 11:25:58 +01:00
default void needOpenInviteLink(TLRPC.TL_chatInviteExported invite) {
}
2014-10-15 20:43:52 +02:00
}
2021-09-20 07:54:41 +02:00
public interface ThemeDelegate extends Theme.ResourcesProvider {
int getCurrentColor();
}
2020-07-26 10:03:38 +02:00
private int TAG;
2014-10-15 20:43:52 +02:00
private URLSpan pressedLink;
2018-07-30 04:07:02 +02:00
private int currentAccount = UserConfig.selectedAccount;
2014-10-15 20:43:52 +02:00
private ImageReceiver imageReceiver;
private AvatarDrawable avatarDrawable;
2014-10-14 22:36:15 +02:00
private StaticLayout textLayout;
2019-05-14 14:08:05 +02:00
private int textWidth;
private int textHeight;
private int textX;
private int textY;
private int textXLeft;
private int previousWidth;
private boolean imagePressed;
2021-12-30 11:52:40 +01:00
public List<SpoilerEffect> spoilers = new ArrayList<>();
private Stack<SpoilerEffect> spoilersPool = new Stack<>();
2021-11-05 11:06:49 +01:00
TextPaint textPaint;
2021-06-25 02:43:10 +02:00
private float viewTop;
private int backgroundHeight;
private boolean visiblePartSet;
2020-07-26 10:03:38 +02:00
private ImageLocation currentVideoLocation;
2019-05-14 14:08:05 +02:00
private float lastTouchX;
private float lastTouchY;
private boolean wasLayout;
2014-10-14 22:36:15 +02:00
2016-03-16 13:26:32 +01:00
private boolean hasReplyMessage;
2014-10-14 22:36:15 +02:00
private MessageObject currentMessageObject;
2017-03-31 01:58:05 +02:00
private int customDate;
private CharSequence customText;
2014-10-14 22:36:15 +02:00
2020-02-13 19:26:53 +01:00
private String overrideBackground;
private String overrideText;
2021-09-20 07:54:41 +02:00
private Paint overrideBackgroundPaint;
private TextPaint overrideTextPaint;
2020-02-13 19:26:53 +01:00
private int overrideColor;
2021-06-25 02:43:10 +02:00
private ArrayList<Integer> lineWidths = new ArrayList<>();
private ArrayList<Integer> lineHeights = new ArrayList<>();
private Path backgroundPath = new Path();
private RectF rect = new RectF();
private boolean invalidatePath = true;
2021-09-20 07:54:41 +02:00
private boolean invalidateColors = false;
2020-02-13 19:26:53 +01:00
2014-10-15 20:43:52 +02:00
private ChatActionCellDelegate delegate;
2021-09-20 07:54:41 +02:00
private ThemeDelegate themeDelegate;
2014-10-15 20:43:52 +02:00
2014-10-14 22:36:15 +02:00
public ChatActionCell(Context context) {
2021-11-05 11:06:49 +01:00
this(context, false, null);
2021-09-20 07:54:41 +02:00
}
2021-11-05 11:06:49 +01:00
public ChatActionCell(Context context, boolean canDrawInParent, ThemeDelegate themeDelegate) {
2014-10-14 22:36:15 +02:00
super(context);
2021-11-05 11:06:49 +01:00
this.canDrawInParent = canDrawInParent;
2021-09-20 07:54:41 +02:00
this.themeDelegate = themeDelegate;
2014-10-15 20:43:52 +02:00
imageReceiver = new ImageReceiver(this);
2020-07-26 10:03:38 +02:00
imageReceiver.setRoundRadius(AndroidUtilities.roundMessageSize / 2);
avatarDrawable = new AvatarDrawable();
2020-07-26 10:03:38 +02:00
TAG = DownloadController.getInstance(currentAccount).generateObserverTag();
2014-10-14 22:36:15 +02:00
}
2014-10-15 20:43:52 +02:00
public void setDelegate(ChatActionCellDelegate delegate) {
this.delegate = delegate;
}
2019-12-31 14:08:08 +01:00
public void setCustomDate(int date, boolean scheduled, boolean inLayout) {
2021-07-15 16:24:57 +02:00
if (customDate == date || customDate / 3600 == date / 3600) {
2017-03-31 01:58:05 +02:00
return;
}
2019-09-10 12:56:11 +02:00
CharSequence newText;
if (scheduled) {
2019-12-31 14:08:08 +01:00
if (date == 0x7ffffffe) {
newText = LocaleController.getString("MessageScheduledUntilOnline", R.string.MessageScheduledUntilOnline);
} else {
newText = LocaleController.formatString("MessageScheduledOn", R.string.MessageScheduledOn, LocaleController.formatDateChat(date));
}
2019-09-10 12:56:11 +02:00
} else {
newText = LocaleController.formatDateChat(date);
}
2022-02-12 06:22:45 +01:00
customDate = date;
2017-03-31 01:58:05 +02:00
if (customText != null && TextUtils.equals(newText, customText)) {
return;
}
customText = newText;
2020-07-26 10:03:38 +02:00
updateTextInternal(inLayout);
}
private void updateTextInternal(boolean inLayout) {
2017-03-31 01:58:05 +02:00
if (getMeasuredWidth() != 0) {
createLayout(customText, getMeasuredWidth());
invalidate();
}
2019-05-14 14:08:05 +02:00
if (!wasLayout) {
2019-12-31 14:08:08 +01:00
if (inLayout) {
AndroidUtilities.runOnUIThread(this::requestLayout);
} else {
requestLayout();
}
2019-05-14 14:08:05 +02:00
} else {
buildLayout();
}
2017-03-31 01:58:05 +02:00
}
2020-12-23 08:48:30 +01:00
public void setCustomText(CharSequence text) {
2020-07-26 10:03:38 +02:00
customText = text;
if (customText != null) {
updateTextInternal(false);
}
}
2020-02-13 19:26:53 +01:00
public void setOverrideColor(String background, String text) {
overrideBackground = background;
overrideText = text;
}
2014-10-14 22:36:15 +02:00
public void setMessageObject(MessageObject messageObject) {
2020-09-30 15:48:47 +02:00
if (currentMessageObject == messageObject && (textLayout == null || TextUtils.equals(textLayout.getText(), messageObject.messageText)) && (hasReplyMessage || messageObject.replyMessageObject == null)) {
2014-10-14 22:36:15 +02:00
return;
}
currentMessageObject = messageObject;
2016-03-16 13:26:32 +01:00
hasReplyMessage = messageObject.replyMessageObject != null;
2020-07-26 10:03:38 +02:00
DownloadController.getInstance(currentAccount).removeLoadingFileObserver(this);
2014-10-15 20:43:52 +02:00
previousWidth = 0;
if (currentMessageObject.type == 11) {
2021-09-20 07:54:41 +02:00
long id = messageObject.getDialogId();
2019-09-10 12:56:11 +02:00
avatarDrawable.setInfo(id, null, null);
2014-10-15 20:43:52 +02:00
if (currentMessageObject.messageOwner.action instanceof TLRPC.TL_messageActionUserUpdatedPhoto) {
2019-05-14 14:08:05 +02:00
imageReceiver.setImage(null, null, avatarDrawable, null, currentMessageObject, 0);
2014-10-15 20:43:52 +02:00
} else {
2020-07-26 10:03:38 +02:00
TLRPC.PhotoSize strippedPhotoSize = null;
for (int a = 0, N = currentMessageObject.photoThumbs.size(); a < N; a++) {
TLRPC.PhotoSize photoSize = currentMessageObject.photoThumbs.get(a);
if (photoSize instanceof TLRPC.TL_photoStrippedSize) {
strippedPhotoSize = photoSize;
break;
}
}
TLRPC.PhotoSize photoSize = FileLoader.getClosestPhotoSizeWithSize(currentMessageObject.photoThumbs, 640);
if (photoSize != null) {
TLRPC.Photo photo = messageObject.messageOwner.action.photo;
TLRPC.VideoSize videoSize = null;
if (!photo.video_sizes.isEmpty() && SharedConfig.autoplayGifs) {
videoSize = photo.video_sizes.get(0);
if (!messageObject.mediaExists && !DownloadController.getInstance(currentAccount).canDownloadMedia(DownloadController.AUTODOWNLOAD_TYPE_VIDEO, videoSize.size)) {
currentVideoLocation = ImageLocation.getForPhoto(videoSize, photo);
String fileName = FileLoader.getAttachFileName(videoSize);
DownloadController.getInstance(currentAccount).addLoadingFileObserver(fileName, currentMessageObject, this);
videoSize = null;
}
}
if (videoSize != null) {
imageReceiver.setImage(ImageLocation.getForPhoto(videoSize, photo), ImageLoader.AUTOPLAY_FILTER, ImageLocation.getForObject(strippedPhotoSize, currentMessageObject.photoThumbsObject), "50_50_b", avatarDrawable, 0, null, currentMessageObject, 1);
} else {
imageReceiver.setImage(ImageLocation.getForObject(photoSize, currentMessageObject.photoThumbsObject), "150_150", ImageLocation.getForObject(strippedPhotoSize, currentMessageObject.photoThumbsObject), "50_50_b", avatarDrawable, 0, null, currentMessageObject, 1);
}
2014-10-15 20:43:52 +02:00
} else {
imageReceiver.setImageBitmap(avatarDrawable);
2014-10-14 22:36:15 +02:00
}
}
2018-07-30 04:07:02 +02:00
imageReceiver.setVisible(!PhotoViewer.isShowingImage(currentMessageObject), false);
2014-10-15 20:43:52 +02:00
} else {
2017-03-31 01:58:05 +02:00
imageReceiver.setImageBitmap((Bitmap) null);
2014-10-14 22:36:15 +02:00
}
requestLayout();
}
2014-10-15 20:43:52 +02:00
public MessageObject getMessageObject() {
return currentMessageObject;
}
public ImageReceiver getPhotoImage() {
return imageReceiver;
}
2021-06-25 02:43:10 +02:00
public void setVisiblePart(float visibleTop, int parentH) {
visiblePartSet = true;
backgroundHeight = parentH;
viewTop = visibleTop;
}
2014-10-14 22:36:15 +02:00
@Override
2021-07-30 16:49:55 +02:00
protected boolean onLongPress() {
2014-10-15 20:43:52 +02:00
if (delegate != null) {
2022-04-16 16:43:17 +02:00
return delegate.didLongPress(this, lastTouchX, lastTouchY);
2014-10-15 20:43:52 +02:00
}
2022-04-16 16:43:17 +02:00
return false;
2014-10-14 22:36:15 +02:00
}
2017-12-08 18:35:59 +01:00
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
2019-05-14 14:08:05 +02:00
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
2020-07-26 10:03:38 +02:00
DownloadController.getInstance(currentAccount).removeLoadingFileObserver(this);
2020-07-26 23:11:01 +02:00
imageReceiver.onDetachedFromWindow();
2019-05-14 14:08:05 +02:00
wasLayout = false;
}
2020-07-26 23:11:01 +02:00
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
imageReceiver.onAttachedToWindow();
}
2014-10-14 22:36:15 +02:00
@Override
2014-10-15 20:43:52 +02:00
public boolean onTouchEvent(MotionEvent event) {
2017-03-31 01:58:05 +02:00
if (currentMessageObject == null) {
return super.onTouchEvent(event);
}
2019-05-14 14:08:05 +02:00
float x = lastTouchX = event.getX();
float y = lastTouchY = event.getY();
2014-10-15 20:43:52 +02:00
boolean result = false;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (delegate != null) {
if (currentMessageObject.type == 11 && imageReceiver.isInsideImage(x, y)) {
imagePressed = true;
result = true;
}
if (result) {
startCheckLongPress();
}
}
} else {
if (event.getAction() != MotionEvent.ACTION_MOVE) {
cancelCheckLongPress();
}
if (imagePressed) {
if (event.getAction() == MotionEvent.ACTION_UP) {
imagePressed = false;
if (delegate != null) {
2019-05-14 14:08:05 +02:00
delegate.didClickImage(this);
2014-10-15 20:43:52 +02:00
playSoundEffect(SoundEffectConstants.CLICK);
}
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
imagePressed = false;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (!imageReceiver.isInsideImage(x, y)) {
imagePressed = false;
}
}
}
}
if (!result) {
if (event.getAction() == MotionEvent.ACTION_DOWN || pressedLink != null && event.getAction() == MotionEvent.ACTION_UP) {
if (x >= textX && y >= textY && x <= textX + textWidth && y <= textY + textHeight) {
y -= textY;
x -= textXLeft;
2019-05-14 14:08:05 +02:00
final int line = textLayout.getLineForVertical((int) y);
2014-10-15 20:43:52 +02:00
final int off = textLayout.getOffsetForHorizontal(line, x);
final float left = textLayout.getLineLeft(line);
2015-07-22 20:56:37 +02:00
if (left <= x && left + textLayout.getLineWidth(line) >= x && currentMessageObject.messageText instanceof Spannable) {
Spannable buffer = (Spannable) currentMessageObject.messageText;
2014-10-15 20:43:52 +02:00
URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
if (link.length != 0) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
pressedLink = link[0];
result = true;
} else {
if (link[0] == pressedLink) {
if (delegate != null) {
2016-10-11 13:57:01 +02:00
String url = link[0].getURL();
2021-03-19 11:25:58 +01:00
if (url.startsWith("invite") && pressedLink instanceof URLSpanNoUnderline) {
URLSpanNoUnderline spanNoUnderline = (URLSpanNoUnderline) pressedLink;
TLObject object = spanNoUnderline.getObject();
if (object instanceof TLRPC.TL_chatInviteExported) {
TLRPC.TL_chatInviteExported invite = (TLRPC.TL_chatInviteExported) object;
delegate.needOpenInviteLink(invite);
}
} else if (url.startsWith("game")) {
2020-09-30 15:48:47 +02:00
delegate.didPressReplyMessage(this, currentMessageObject.getReplyMsgId());
2016-10-11 13:57:01 +02:00
/*TLRPC.KeyboardButton gameButton = null;
MessageObject messageObject = currentMessageObject.replyMessageObject;
if (messageObject != null && messageObject.messageOwner.reply_markup != null) {
for (int a = 0; a < messageObject.messageOwner.reply_markup.rows.size(); a++) {
TLRPC.TL_keyboardButtonRow row = messageObject.messageOwner.reply_markup.rows.get(a);
for (int b = 0; b < row.buttons.size(); b++) {
TLRPC.KeyboardButton button = row.buttons.get(b);
if (button instanceof TLRPC.TL_keyboardButtonGame && button.game_id == currentMessageObject.messageOwner.action.game_id) {
gameButton = button;
break;
}
}
if (gameButton != null) {
break;
}
}
}
if (gameButton != null) {
2019-05-14 14:08:05 +02:00
delegate.didPressBotButton(messageObject, gameButton);
2016-10-11 13:57:01 +02:00
}*/
2018-07-30 04:07:02 +02:00
} else if (url.startsWith("http")) {
Browser.openUrl(getContext(), url);
2016-10-11 13:57:01 +02:00
} else {
2021-12-09 17:28:33 +01:00
delegate.needOpenUserProfile(Long.parseLong(url));
2016-10-11 13:57:01 +02:00
}
2014-10-15 20:43:52 +02:00
}
result = true;
}
}
} else {
pressedLink = null;
}
} else {
pressedLink = null;
}
} else {
pressedLink = null;
}
}
}
if (!result) {
result = super.onTouchEvent(event);
}
return result;
}
2017-03-31 01:58:05 +02:00
private void createLayout(CharSequence text, int width) {
int maxWidth = width - AndroidUtilities.dp(30);
2021-06-25 02:43:10 +02:00
invalidatePath = true;
2021-09-20 07:54:41 +02:00
textLayout = new StaticLayout(text, (TextPaint) getThemedPaint(Theme.key_paint_chatActionText), maxWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
2021-12-30 11:52:40 +01:00
spoilersPool.addAll(spoilers);
spoilers.clear();
if (text instanceof Spannable)
SpoilerEffect.addSpoilers(this, textLayout, (Spannable) text, spoilersPool, spoilers);
2017-03-31 01:58:05 +02:00
textHeight = 0;
textWidth = 0;
try {
int linesCount = textLayout.getLineCount();
for (int a = 0; a < linesCount; a++) {
float lineWidth;
try {
lineWidth = textLayout.getLineWidth(a);
if (lineWidth > maxWidth) {
lineWidth = maxWidth;
}
2019-05-14 14:08:05 +02:00
textHeight = (int) Math.max(textHeight, Math.ceil(textLayout.getLineBottom(a)));
2017-03-31 01:58:05 +02:00
} catch (Exception e) {
FileLog.e(e);
return;
}
2019-05-14 14:08:05 +02:00
textWidth = (int) Math.max(textWidth, Math.ceil(lineWidth));
2017-03-31 01:58:05 +02:00
}
} catch (Exception e) {
FileLog.e(e);
}
textX = (width - textWidth) / 2;
textY = AndroidUtilities.dp(7);
textXLeft = (width - textLayout.getWidth()) / 2;
}
2020-07-26 10:03:38 +02:00
2014-10-15 20:43:52 +02:00
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
2017-03-31 01:58:05 +02:00
if (currentMessageObject == null && customText == null) {
2014-10-15 20:43:52 +02:00
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), textHeight + AndroidUtilities.dp(14));
2014-10-14 22:36:15 +02:00
return;
}
2014-11-21 20:36:21 +01:00
int width = Math.max(AndroidUtilities.dp(30), MeasureSpec.getSize(widthMeasureSpec));
2019-05-14 14:08:05 +02:00
if (previousWidth != width) {
wasLayout = true;
previousWidth = width;
buildLayout();
}
2020-07-26 10:03:38 +02:00
setMeasuredDimension(width, textHeight + (currentMessageObject != null && currentMessageObject.type == 11 ? AndroidUtilities.roundMessageSize + AndroidUtilities.dp(10) : 0) + AndroidUtilities.dp(14));
2019-05-14 14:08:05 +02:00
}
private void buildLayout() {
CharSequence text;
if (currentMessageObject != null) {
if (currentMessageObject.messageOwner != null && currentMessageObject.messageOwner.media != null && currentMessageObject.messageOwner.media.ttl_seconds != 0) {
if (currentMessageObject.messageOwner.media.photo instanceof TLRPC.TL_photoEmpty) {
text = LocaleController.getString("AttachPhotoExpired", R.string.AttachPhotoExpired);
} else if (currentMessageObject.messageOwner.media.document instanceof TLRPC.TL_documentEmpty) {
text = LocaleController.getString("AttachVideoExpired", R.string.AttachVideoExpired);
} else {
text = currentMessageObject.messageText;
}
2017-03-31 01:58:05 +02:00
} else {
2019-05-14 14:08:05 +02:00
text = currentMessageObject.messageText;
2014-10-15 20:43:52 +02:00
}
2019-05-14 14:08:05 +02:00
} else {
text = customText;
}
createLayout(text, previousWidth);
if (currentMessageObject != null && currentMessageObject.type == 11) {
2020-07-26 10:03:38 +02:00
imageReceiver.setImageCoords((previousWidth - AndroidUtilities.roundMessageSize) / 2, textHeight + AndroidUtilities.dp(19), AndroidUtilities.roundMessageSize, AndroidUtilities.roundMessageSize);
2014-10-14 22:36:15 +02:00
}
2017-03-31 01:58:05 +02:00
}
public int getCustomDate() {
return customDate;
2014-10-14 22:36:15 +02:00
}
@Override
protected void onDraw(Canvas canvas) {
2017-03-31 01:58:05 +02:00
if (currentMessageObject != null && currentMessageObject.type == 11) {
2014-10-21 22:35:16 +02:00
imageReceiver.draw(canvas);
2014-10-15 20:43:52 +02:00
}
2020-02-13 19:26:53 +01:00
if (textLayout == null) {
return;
}
2017-03-31 01:58:05 +02:00
2021-11-05 11:06:49 +01:00
drawBackground(canvas, false);
if (textPaint != null) {
canvas.save();
canvas.translate(textXLeft, textY);
if (textLayout.getPaint() != textPaint) {
buildLayout();
}
2021-12-30 11:52:40 +01:00
canvas.save();
SpoilerEffect.clipOutCanvas(canvas, spoilers);
2021-11-05 11:06:49 +01:00
textLayout.draw(canvas);
canvas.restore();
2021-12-30 11:52:40 +01:00
for (SpoilerEffect eff : spoilers) {
eff.setColor(textLayout.getPaint().getColor());
eff.draw(canvas);
}
canvas.restore();
2021-11-05 11:06:49 +01:00
}
}
public void drawBackground(Canvas canvas, boolean fromParent) {
if (canDrawInParent) {
if (hasGradientService() && !fromParent) {
return;
}
if (!hasGradientService() && fromParent) {
return;
}
}
2021-09-20 07:54:41 +02:00
Paint backgroundPaint = getThemedPaint(Theme.key_paint_chatActionBackground);
2021-11-05 11:06:49 +01:00
textPaint = (TextPaint) getThemedPaint(Theme.key_paint_chatActionText);
2020-02-13 19:26:53 +01:00
if (overrideBackground != null) {
2021-09-20 07:54:41 +02:00
int color = getThemedColor(overrideBackground);
if (overrideBackgroundPaint == null) {
overrideBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
overrideBackgroundPaint.setColor(color);
overrideTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
overrideTextPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
overrideTextPaint.setTextSize(AndroidUtilities.dp(Math.max(16, SharedConfig.fontSize) - 2));
overrideTextPaint.setColor(getThemedColor(overrideText));
2020-02-13 19:26:53 +01:00
}
2021-09-20 07:54:41 +02:00
backgroundPaint = overrideBackgroundPaint;
textPaint = overrideTextPaint;
2020-02-13 19:26:53 +01:00
}
2021-06-25 02:43:10 +02:00
if (invalidatePath) {
invalidatePath = false;
lineWidths.clear();
final int count = textLayout.getLineCount();
final int corner = AndroidUtilities.dp(11);
final int cornerIn = AndroidUtilities.dp(8);
2020-02-13 19:26:53 +01:00
int prevLineWidth = 0;
2021-06-25 02:43:10 +02:00
for (int a = 0; a < count; a++) {
int lineWidth = (int) Math.ceil(textLayout.getLineWidth(a));
if (a != 0) {
int diff = prevLineWidth - lineWidth;
if (diff > 0 && diff <= corner + cornerIn) {
lineWidth = prevLineWidth;
}
2017-07-08 18:32:04 +02:00
}
2021-06-25 02:43:10 +02:00
lineWidths.add(lineWidth);
prevLineWidth = lineWidth;
2020-02-13 19:26:53 +01:00
}
2021-06-25 02:43:10 +02:00
for (int a = count - 2; a >= 0; a--) {
int lineWidth = lineWidths.get(a);
int diff = prevLineWidth - lineWidth;
if (diff > 0 && diff <= corner + cornerIn) {
lineWidth = prevLineWidth;
2017-07-08 18:32:04 +02:00
}
2021-06-25 02:43:10 +02:00
lineWidths.set(a, lineWidth);
prevLineWidth = lineWidth;
2020-02-13 19:26:53 +01:00
}
2017-07-08 18:32:04 +02:00
2021-06-25 02:43:10 +02:00
int y = AndroidUtilities.dp(4);
int x = getMeasuredWidth() / 2;
int previousLineBottom = 0;
2016-04-22 15:49:00 +02:00
2021-06-25 02:43:10 +02:00
final int cornerOffset = AndroidUtilities.dp(3);
final int cornerInSmall = AndroidUtilities.dp(6);
final int cornerRest = corner - cornerOffset;
2016-04-22 15:49:00 +02:00
2021-06-25 02:43:10 +02:00
lineHeights.clear();
backgroundPath.reset();
backgroundPath.moveTo(x, y);
2016-04-22 15:49:00 +02:00
2021-06-25 02:43:10 +02:00
for (int a = 0; a < count; a++) {
int lineWidth = lineWidths.get(a);
int lineBottom = textLayout.getLineBottom(a);
int nextLineWidth = a < count - 1 ? lineWidths.get(a + 1) : 0;
2016-04-22 15:49:00 +02:00
2021-06-25 02:43:10 +02:00
int height = lineBottom - previousLineBottom;
if (a == 0 || lineWidth > prevLineWidth) {
2020-02-13 19:26:53 +01:00
height += AndroidUtilities.dp(3);
2021-06-25 02:43:10 +02:00
}
if (a == count - 1 || lineWidth > nextLineWidth) {
2020-02-13 19:26:53 +01:00
height += AndroidUtilities.dp(3);
2021-06-25 02:43:10 +02:00
}
2016-04-22 15:49:00 +02:00
2021-06-25 02:43:10 +02:00
previousLineBottom = lineBottom;
2016-04-22 15:49:00 +02:00
2021-06-25 02:43:10 +02:00
float startX = x + lineWidth / 2.0f;
2016-04-22 15:49:00 +02:00
2021-06-25 02:43:10 +02:00
int innerCornerRad;
if (a != count - 1 && lineWidth < nextLineWidth && a != 0 && lineWidth < prevLineWidth) {
innerCornerRad = cornerInSmall;
2017-07-08 18:32:04 +02:00
} else {
2021-06-25 02:43:10 +02:00
innerCornerRad = cornerIn;
2017-07-08 18:32:04 +02:00
}
2016-04-22 15:49:00 +02:00
2021-06-25 02:43:10 +02:00
if (a == 0 || lineWidth > prevLineWidth) {
rect.set(startX - cornerOffset - corner, y, startX + cornerRest, y + corner * 2);
backgroundPath.arcTo(rect, -90, 90);
} else if (lineWidth < prevLineWidth) {
rect.set(startX + cornerRest, y, startX + cornerRest + innerCornerRad * 2, y + innerCornerRad * 2);
backgroundPath.arcTo(rect, -90, -90);
}
y += height;
int yOffset = y;
if (a != count - 1 && lineWidth < nextLineWidth) {
y -= AndroidUtilities.dp(3);
height -= AndroidUtilities.dp(3);
}
if (a != 0 && lineWidth < prevLineWidth) {
y -= AndroidUtilities.dp(3);
height -= AndroidUtilities.dp(3);
}
lineHeights.add(height);
if (a == count - 1 || lineWidth > nextLineWidth) {
rect.set(startX - cornerOffset - corner, y - corner * 2, startX + cornerRest, y);
backgroundPath.arcTo(rect, 0, 90);
} else if (lineWidth < nextLineWidth) {
rect.set(startX + cornerRest, y - innerCornerRad * 2, startX + cornerRest + innerCornerRad * 2, y);
backgroundPath.arcTo(rect, 180, -90);
}
2016-04-22 15:49:00 +02:00
2021-06-25 02:43:10 +02:00
prevLineWidth = lineWidth;
2020-02-13 19:26:53 +01:00
}
2021-06-25 02:43:10 +02:00
for (int a = count - 1; a >= 0; a--) {
prevLineWidth = a != 0 ? lineWidths.get(a - 1) : 0;
int lineWidth = lineWidths.get(a);
int nextLineWidth = a != count - 1 ? lineWidths.get(a + 1) : 0;
int lineBottom = textLayout.getLineBottom(a);
float startX = x - lineWidth / 2;
int innerCornerRad;
if (a != count - 1 && lineWidth < nextLineWidth && a != 0 && lineWidth < prevLineWidth) {
innerCornerRad = cornerInSmall;
} else {
innerCornerRad = cornerIn;
}
2016-04-22 15:49:00 +02:00
2021-06-25 02:43:10 +02:00
if (a == count - 1 || lineWidth > nextLineWidth) {
rect.set(startX - cornerRest, y - corner * 2, startX + cornerOffset + corner, y);
backgroundPath.arcTo(rect, 90, 90);
} else if (lineWidth < nextLineWidth) {
rect.set(startX - cornerRest - innerCornerRad * 2, y - innerCornerRad * 2, startX - cornerRest, y);
backgroundPath.arcTo(rect, 90, -90);
}
2016-04-22 15:49:00 +02:00
2021-06-25 02:43:10 +02:00
y -= lineHeights.get(a);
2019-01-23 18:03:33 +01:00
2021-06-25 02:43:10 +02:00
if (a == 0 || lineWidth > prevLineWidth) {
rect.set(startX - cornerRest, y, startX + cornerOffset + corner, y + corner * 2);
backgroundPath.arcTo(rect, 180, 90);
} else if (lineWidth < prevLineWidth) {
rect.set(startX - cornerRest - innerCornerRad * 2, y, startX - cornerRest, y + innerCornerRad * 2);
backgroundPath.arcTo(rect, 0, -90);
}
2016-04-22 15:49:00 +02:00
}
2021-06-25 02:43:10 +02:00
backgroundPath.close();
}
if (!visiblePartSet) {
ViewGroup parent = (ViewGroup) getParent();
backgroundHeight = parent.getMeasuredHeight();
}
2021-09-20 07:54:41 +02:00
if (themeDelegate != null) {
themeDelegate.applyServiceShaderMatrix(getMeasuredWidth(), backgroundHeight, 0, viewTop + AndroidUtilities.dp(4));
} else {
Theme.applyServiceShaderMatrix(getMeasuredWidth(), backgroundHeight, 0, viewTop + AndroidUtilities.dp(4));
}
2021-11-05 11:06:49 +01:00
int oldAlpha = -1;
int oldAlpha2 = -1;
if (fromParent && getAlpha() != 1f) {
oldAlpha = backgroundPaint.getAlpha();
oldAlpha2 = Theme.chat_actionBackgroundGradientDarkenPaint.getAlpha();
backgroundPaint.setAlpha((int) (oldAlpha * getAlpha()));
Theme.chat_actionBackgroundGradientDarkenPaint.setAlpha((int) (oldAlpha2 * getAlpha()));
}
2021-09-20 07:54:41 +02:00
canvas.drawPath(backgroundPath, backgroundPaint);
if (hasGradientService()) {
2021-06-25 02:43:10 +02:00
canvas.drawPath(backgroundPath, Theme.chat_actionBackgroundGradientDarkenPaint);
2020-02-13 19:26:53 +01:00
}
2021-11-05 11:06:49 +01:00
if (oldAlpha >= 0) {
backgroundPaint.setAlpha(oldAlpha);
Theme.chat_actionBackgroundGradientDarkenPaint.setAlpha(oldAlpha2);
2021-09-20 07:54:41 +02:00
}
}
2020-02-13 19:26:53 +01:00
2021-11-05 11:06:49 +01:00
public boolean hasGradientService() {
return overrideBackgroundPaint == null && (themeDelegate != null ? themeDelegate.hasGradientService() : Theme.hasGradientService());
2014-10-14 22:36:15 +02:00
}
2019-05-14 14:08:05 +02:00
2020-07-26 10:03:38 +02:00
@Override
public void onFailedDownload(String fileName, boolean canceled) {
}
@Override
public void onSuccessDownload(String fileName) {
if (currentMessageObject != null && currentMessageObject.type == 11) {
TLRPC.PhotoSize strippedPhotoSize = null;
for (int a = 0, N = currentMessageObject.photoThumbs.size(); a < N; a++) {
TLRPC.PhotoSize photoSize = currentMessageObject.photoThumbs.get(a);
if (photoSize instanceof TLRPC.TL_photoStrippedSize) {
strippedPhotoSize = photoSize;
break;
}
}
imageReceiver.setImage(currentVideoLocation, ImageLoader.AUTOPLAY_FILTER, ImageLocation.getForObject(strippedPhotoSize, currentMessageObject.photoThumbsObject), "50_50_b", avatarDrawable, 0, null, currentMessageObject, 1);
DownloadController.getInstance(currentAccount).removeLoadingFileObserver(this);
}
}
@Override
public void onProgressDownload(String fileName, long downloadSize, long totalSize) {
}
@Override
public void onProgressUpload(String fileName, long downloadSize, long totalSize, boolean isEncrypted) {
}
@Override
public int getObserverTag() {
return TAG;
}
2019-05-14 14:08:05 +02:00
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
if (TextUtils.isEmpty(customText) && currentMessageObject == null) {
return;
}
info.setText(!TextUtils.isEmpty(customText) ? customText : currentMessageObject.messageText);
info.setEnabled(true);
}
2021-09-20 07:54:41 +02:00
public void setInvalidateColors(boolean invalidate) {
if (invalidateColors == invalidate) {
return;
}
invalidateColors = invalidate;
invalidate();
}
private int getThemedColor(String key) {
Integer color = themeDelegate != null ? themeDelegate.getColor(key) : null;
return color != null ? color : Theme.getColor(key);
}
private Paint getThemedPaint(String paintKey) {
Paint paint = themeDelegate != null ? themeDelegate.getPaint(paintKey) : null;
return paint != null ? paint : Theme.getThemePaint(paintKey);
}
2014-10-14 22:36:15 +02:00
}