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

1104 lines
56 KiB
Java
Raw Normal View History

/*
* This is the source code of Telegram for Android v. 1.3.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Nikolai Kudashov, 2013-2016.
*/
package org.telegram.ui.Cells;
import android.content.Context;
import android.graphics.Canvas;
2015-04-09 20:00:14 +02:00
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
2015-05-21 23:27:27 +02:00
import android.os.Build;
2015-04-09 20:00:14 +02:00
import android.text.Layout;
import android.text.Spannable;
2015-04-09 20:00:14 +02:00
import android.text.SpannableStringBuilder;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
2016-03-06 02:49:31 +01:00
import android.text.style.URLSpan;
import android.view.MotionEvent;
2015-08-13 11:23:31 +02:00
import android.view.SoundEffectConstants;
2015-10-29 18:10:07 +01:00
import android.view.ViewStructure;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.ImageLoader;
import org.telegram.messenger.ImageReceiver;
import org.telegram.messenger.MediaController;
2015-04-09 20:00:14 +02:00
import org.telegram.messenger.FileLoader;
2014-03-10 10:27:49 +01:00
import org.telegram.messenger.FileLog;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.MessageObject;
2015-04-09 20:00:14 +02:00
import org.telegram.messenger.R;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.TLRPC;
2015-08-13 11:23:31 +02:00
import org.telegram.ui.Components.RadialProgress;
import org.telegram.ui.Components.ResourceLoader;
2015-04-09 20:00:14 +02:00
import org.telegram.ui.Components.StaticLayoutEx;
2015-10-29 18:10:07 +01:00
import org.telegram.ui.Components.URLSpanBotCommand;
import org.telegram.ui.Components.URLSpanNoUnderline;
2015-05-21 23:27:27 +02:00
import java.io.File;
2015-04-09 20:00:14 +02:00
import java.util.Locale;
public class ChatMessageCell extends ChatBaseCell {
private int textX, textY;
private int totalHeight = 0;
2015-04-09 20:00:14 +02:00
private int linkBlockNum;
private int lastVisibleBlockNum = 0;
private int firstVisibleBlockNum = 0;
private int totalVisibleBlocksCount = 0;
2015-08-13 11:23:31 +02:00
private RadialProgress radialProgress;
2015-04-09 20:00:14 +02:00
private ImageReceiver linkImageView;
private boolean isSmallImage;
2015-08-13 11:23:31 +02:00
private boolean drawImageButton;
private boolean isGifDocument;
2015-04-09 20:00:14 +02:00
private boolean drawLinkImageView;
private boolean hasLinkPreview;
private int linkPreviewHeight;
private boolean isInstagram;
private int descriptionY;
private int durationWidth;
2015-05-21 23:27:27 +02:00
private int descriptionX;
private int titleX;
private int authorX;
2015-04-09 20:00:14 +02:00
private StaticLayout siteNameLayout;
private StaticLayout titleLayout;
private StaticLayout descriptionLayout;
private StaticLayout durationLayout;
private StaticLayout authorLayout;
private static TextPaint durationPaint;
2015-08-13 11:23:31 +02:00
private int buttonX;
private int buttonY;
private int buttonState;
private boolean buttonPressed;
private boolean photoNotSet;
private TLRPC.PhotoSize currentPhotoObject;
private TLRPC.PhotoSize currentPhotoObjectThumb;
private String currentPhotoFilter;
private String currentPhotoFilterThumb;
private boolean cancelLoading;
2015-04-09 20:00:14 +02:00
private static Drawable igvideoDrawable;
public ChatMessageCell(Context context) {
2014-08-29 23:06:04 +02:00
super(context);
drawForwardedName = true;
2015-04-09 20:00:14 +02:00
linkImageView = new ImageReceiver(this);
2015-08-13 11:23:31 +02:00
radialProgress = new RadialProgress(this);
2015-04-09 20:00:14 +02:00
}
@Override
public boolean onTouchEvent(MotionEvent event) {
2015-04-09 20:00:14 +02:00
boolean result = false;
2015-06-29 19:12:11 +02:00
if (currentMessageObject != null && currentMessageObject.textLayoutBlocks != null && !currentMessageObject.textLayoutBlocks.isEmpty() && currentMessageObject.messageText instanceof Spannable && delegate.canPerformActions()) {
2015-08-13 11:23:31 +02:00
if (event.getAction() == MotionEvent.ACTION_DOWN || (linkPreviewPressed || pressedLink != null || buttonPressed) && event.getAction() == MotionEvent.ACTION_UP) {
int x = (int) event.getX();
int y = (int) event.getY();
if (x >= textX && y >= textY && x <= textX + currentMessageObject.textWidth && y <= textY + currentMessageObject.textHeight) {
y -= textY;
2014-05-18 23:44:30 +02:00
int blockNum = Math.max(0, y / currentMessageObject.blockHeight);
if (blockNum < currentMessageObject.textLayoutBlocks.size()) {
2014-10-22 22:01:07 +02:00
try {
MessageObject.TextLayoutBlock block = currentMessageObject.textLayoutBlocks.get(blockNum);
x -= textX - (int) Math.ceil(block.textXOffset);
2014-10-22 22:01:07 +02:00
y -= block.textYOffset;
final int line = block.textLayout.getLineForVertical(y);
final int off = block.textLayout.getOffsetForHorizontal(line, x) + block.charactersOffset;
2014-10-22 22:01:07 +02:00
final float left = block.textLayout.getLineLeft(line);
if (left <= x && left + block.textLayout.getLineWidth(line) >= x) {
Spannable buffer = (Spannable) currentMessageObject.messageText;
2014-10-22 22:01:07 +02:00
ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
2015-10-29 18:10:07 +01:00
boolean ignore = false;
if (link.length == 0 || link.length != 0 && link[0] instanceof URLSpanBotCommand && !URLSpanBotCommand.enabled) {
ignore = true;
}
if (!ignore) {
2014-10-22 22:01:07 +02:00
if (event.getAction() == MotionEvent.ACTION_DOWN) {
2015-04-09 20:00:14 +02:00
resetPressedLink();
2014-10-22 22:01:07 +02:00
pressedLink = link[0];
2015-04-09 20:00:14 +02:00
linkBlockNum = blockNum;
try {
int start = buffer.getSpanStart(pressedLink) - block.charactersOffset;
urlPath.setCurrentLayout(block.textLayout, start);
block.textLayout.getSelectionPath(start, buffer.getSpanEnd(pressedLink) - block.charactersOffset, urlPath);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
result = true;
2014-10-22 22:01:07 +02:00
} else {
if (link[0] == pressedLink) {
try {
2015-10-29 18:10:07 +01:00
delegate.didPressUrl(currentMessageObject, pressedLink, false);
2014-10-22 22:01:07 +02:00
} catch (Exception e) {
FileLog.e("tmessages", e);
}
2015-04-09 20:00:14 +02:00
resetPressedLink();
result = true;
2014-10-22 22:01:07 +02:00
}
}
2014-10-22 22:01:07 +02:00
} else {
2015-04-09 20:00:14 +02:00
resetPressedLink();
}
} else {
2015-04-09 20:00:14 +02:00
resetPressedLink();
}
2014-10-22 22:01:07 +02:00
} catch (Exception e) {
2015-04-09 20:00:14 +02:00
resetPressedLink();
2014-10-22 22:01:07 +02:00
FileLog.e("tmessages", e);
}
} else {
2015-04-09 20:00:14 +02:00
resetPressedLink();
}
} else if (hasLinkPreview && x >= textX && x <= textX + backgroundWidth && y >= textY + currentMessageObject.textHeight && y <= textY + currentMessageObject.textHeight + linkPreviewHeight + AndroidUtilities.dp(8)) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
resetPressedLink();
if (drawLinkImageView && linkImageView.isInsideImage(x, y)) {
2015-08-13 11:23:31 +02:00
if (drawImageButton && buttonState != -1 && x >= buttonX && x <= buttonX + AndroidUtilities.dp(48) && y >= buttonY && y <= buttonY + AndroidUtilities.dp(48)) {
buttonPressed = true;
result = true;
} else {
linkPreviewPressed = true;
result = true;
}
if (linkPreviewPressed && isGifDocument && buttonState == -1 && MediaController.getInstance().canAutoplayGifs()) {
linkPreviewPressed = false;
result = false;
}
2015-04-09 20:00:14 +02:00
} else {
if (descriptionLayout != null && y >= descriptionY) {
try {
2015-05-21 23:27:27 +02:00
x -= textX + AndroidUtilities.dp(10) + descriptionX;
2015-04-09 20:00:14 +02:00
y -= descriptionY;
final int line = descriptionLayout.getLineForVertical(y);
final int off = descriptionLayout.getOffsetForHorizontal(line, x);
final float left = descriptionLayout.getLineLeft(line);
if (left <= x && left + descriptionLayout.getLineWidth(line) >= x) {
Spannable buffer = (Spannable) currentMessageObject.linkDescription;
ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
2015-10-29 18:10:07 +01:00
boolean ignore = false;
if (link.length == 0 || link.length != 0 && link[0] instanceof URLSpanBotCommand && !URLSpanBotCommand.enabled) {
ignore = true;
}
if (!ignore) {
2015-04-09 20:00:14 +02:00
resetPressedLink();
pressedLink = link[0];
linkPreviewPressed = true;
linkBlockNum = -10;
result = true;
try {
int start = buffer.getSpanStart(pressedLink);
urlPath.setCurrentLayout(descriptionLayout, start);
descriptionLayout.getSelectionPath(start, buffer.getSpanEnd(pressedLink), urlPath);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
} else {
resetPressedLink();
}
} else {
resetPressedLink();
}
} catch (Exception e) {
resetPressedLink();
FileLog.e("tmessages", e);
}
}
}
} else if (linkPreviewPressed) {
try {
if (pressedLink != null) {
2016-03-06 02:49:31 +01:00
if (pressedLink instanceof URLSpan) {
AndroidUtilities.openUrl(getContext(), ((URLSpan) pressedLink).getURL());
} else {
pressedLink.onClick(this);
}
2015-04-09 20:00:14 +02:00
} else {
2015-08-13 11:23:31 +02:00
if (drawImageButton && delegate != null) {
if (isGifDocument) {
if (buttonState == -1) {
buttonState = 2;
currentMessageObject.audioProgress = 1;
linkImageView.setAllowStartAnimation(false);
linkImageView.stopAnimation();
radialProgress.setBackground(getDrawableForCurrentState(), false, false);
invalidate();
playSoundEffect(SoundEffectConstants.CLICK);
} else if (buttonState == 2 || buttonState == 0) {
didPressedButton(false);
playSoundEffect(SoundEffectConstants.CLICK);
}
} else if (buttonState == -1) {
2015-09-02 00:14:21 +02:00
delegate.didClickedImage(this);
playSoundEffect(SoundEffectConstants.CLICK);
2015-09-02 00:14:21 +02:00
}
2015-05-21 23:27:27 +02:00
} else {
2015-08-13 11:23:31 +02:00
TLRPC.WebPage webPage = currentMessageObject.messageOwner.media.webpage;
if (Build.VERSION.SDK_INT >= 16 && webPage.embed_url != null && webPage.embed_url.length() != 0) {
delegate.needOpenWebView(webPage.embed_url, webPage.site_name, webPage.url, webPage.embed_width, webPage.embed_height);
} else {
2016-03-06 02:49:31 +01:00
AndroidUtilities.openUrl(getContext(), webPage.url);
2015-08-13 11:23:31 +02:00
}
2015-05-21 23:27:27 +02:00
}
2015-04-09 20:00:14 +02:00
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
resetPressedLink();
result = true;
2015-08-13 11:23:31 +02:00
} else if (buttonPressed) {
if (event.getAction() == MotionEvent.ACTION_UP) {
buttonPressed = false;
playSoundEffect(SoundEffectConstants.CLICK);
didPressedButton(false);
invalidate();
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
buttonPressed = false;
invalidate();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (!(x >= buttonX && x <= buttonX + AndroidUtilities.dp(48) && y >= buttonY && y <= buttonY + AndroidUtilities.dp(48))) {
buttonPressed = false;
invalidate();
}
}
}
} else {
2015-04-09 20:00:14 +02:00
resetPressedLink();
}
2015-04-09 20:00:14 +02:00
} else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
resetPressedLink();
}
} else {
2015-04-09 20:00:14 +02:00
resetPressedLink();
}
if (result && event.getAction() == MotionEvent.ACTION_DOWN) {
startCheckLongPress();
}
2015-04-09 20:00:14 +02:00
if (event.getAction() != MotionEvent.ACTION_DOWN && event.getAction() != MotionEvent.ACTION_MOVE) {
cancelCheckLongPress();
}
return result || super.onTouchEvent(event);
}
public void setVisiblePart(int position, int height) {
2014-03-10 10:27:49 +01:00
if (currentMessageObject == null || currentMessageObject.textLayoutBlocks == null) {
return;
}
int newFirst = -1, newLast = -1, newCount = 0;
for (int a = Math.max(0, (position - textY) / currentMessageObject.blockHeight); a < currentMessageObject.textLayoutBlocks.size(); a++) {
MessageObject.TextLayoutBlock block = currentMessageObject.textLayoutBlocks.get(a);
float y = textY + block.textYOffset;
if (intersect(y, y + currentMessageObject.blockHeight, position, position + height)) {
if (newFirst == -1) {
newFirst = a;
}
newLast = a;
newCount++;
} else if (y > position) {
break;
}
}
if (lastVisibleBlockNum != newLast || firstVisibleBlockNum != newFirst || totalVisibleBlocksCount != newCount) {
lastVisibleBlockNum = newLast;
firstVisibleBlockNum = newFirst;
totalVisibleBlocksCount = newCount;
invalidate();
}
}
private boolean intersect(float left1, float right1, float left2, float right2) {
if (left1 <= left2) {
return right1 >= left2;
}
return left1 <= right2;
}
2015-08-13 11:23:31 +02:00
public static StaticLayout generateStaticLayout(CharSequence text, TextPaint paint, int maxWidth, int smallWidth, int linesCount, int maxLines) {
2015-04-09 20:00:14 +02:00
SpannableStringBuilder stringBuilder = new SpannableStringBuilder(text);
int addedChars = 0;
StaticLayout layout = new StaticLayout(text, paint, smallWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
for (int a = 0; a < linesCount; a++) {
Layout.Directions directions = layout.getLineDirections(a);
if (layout.getLineLeft(a) != 0 || Build.VERSION.SDK_INT >= 14 && (layout.isRtlCharAt(layout.getLineStart(a)) || layout.isRtlCharAt(layout.getLineEnd(a)))) {
2015-05-21 23:27:27 +02:00
maxWidth = smallWidth;
}
2015-04-09 20:00:14 +02:00
int pos = layout.getLineEnd(a);
if (pos == text.length()) {
break;
}
pos--;
if (stringBuilder.charAt(pos + addedChars) == ' ') {
stringBuilder.replace(pos + addedChars, pos + addedChars + 1, "\n");
} else if (stringBuilder.charAt(pos + addedChars) != '\n') {
2015-04-09 20:00:14 +02:00
stringBuilder.insert(pos + addedChars, "\n");
addedChars++;
}
if (a == layout.getLineCount() - 1 || a == maxLines - 1) {
break;
}
}
return StaticLayoutEx.createStaticLayout(stringBuilder, paint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, AndroidUtilities.dp(1), false, TextUtils.TruncateAt.END, maxWidth, maxLines);
}
@Override
protected boolean isUserDataChanged() {
if (!hasLinkPreview && currentMessageObject.messageOwner.media != null && currentMessageObject.messageOwner.media.webpage instanceof TLRPC.TL_webPage) {
2015-04-09 20:00:14 +02:00
return true;
}
//suppress warning
return super.isUserDataChanged();
}
2015-08-13 11:23:31 +02:00
@Override
public ImageReceiver getPhotoImage() {
return linkImageView;
}
2015-04-09 20:00:14 +02:00
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
linkImageView.onDetachedFromWindow();
2015-08-13 11:23:31 +02:00
MediaController.getInstance().removeLoadingFileObserver(this);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
2015-08-13 11:23:31 +02:00
if (linkImageView.onAttachedToWindow()) {
updateButtonState(false);
}
2015-04-09 20:00:14 +02:00
}
2015-10-29 18:10:07 +01:00
@Override
protected void onLongPress() {
if (pressedLink instanceof URLSpanNoUnderline) {
URLSpanNoUnderline url = (URLSpanNoUnderline) pressedLink;
if (url.getURL().startsWith("/")) {
delegate.didPressUrl(currentMessageObject, pressedLink, true);
return;
}
2016-03-06 02:49:31 +01:00
} else if (pressedLink instanceof URLSpan) {
delegate.didPressUrl(currentMessageObject, pressedLink, true);
return;
2015-10-29 18:10:07 +01:00
}
super.onLongPress();
}
@Override
public void setMessageObject(MessageObject messageObject) {
2015-08-13 11:23:31 +02:00
boolean dataChanged = currentMessageObject == messageObject && (isUserDataChanged() || photoNotSet);
if (currentMessageObject != messageObject || dataChanged) {
2014-03-10 10:27:49 +01:00
if (currentMessageObject != messageObject) {
firstVisibleBlockNum = 0;
lastVisibleBlockNum = 0;
}
2015-04-09 20:00:14 +02:00
drawLinkImageView = false;
hasLinkPreview = false;
resetPressedLink();
linkPreviewPressed = false;
2015-08-13 11:23:31 +02:00
buttonPressed = false;
2015-04-09 20:00:14 +02:00
linkPreviewHeight = 0;
isInstagram = false;
durationLayout = null;
isGifDocument = false;
2015-04-09 20:00:14 +02:00
descriptionLayout = null;
titleLayout = null;
siteNameLayout = null;
authorLayout = null;
2015-08-13 11:23:31 +02:00
drawImageButton = false;
currentPhotoObject = null;
currentPhotoObjectThumb = null;
currentPhotoFilter = null;
int maxWidth;
if (AndroidUtilities.isTablet()) {
2016-03-06 02:49:31 +01:00
if (isChat && !messageObject.isOutOwner() && messageObject.isFromUser()) {
maxWidth = AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(122);
drawName = true;
} else {
2015-10-29 18:10:07 +01:00
drawName = messageObject.messageOwner.to_id.channel_id != 0 && !messageObject.isOutOwner();
maxWidth = AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(80);
}
} else {
2016-03-06 02:49:31 +01:00
if (isChat && !messageObject.isOutOwner() && messageObject.isFromUser()) {
maxWidth = Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) - AndroidUtilities.dp(122);
drawName = true;
} else {
maxWidth = Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) - AndroidUtilities.dp(80);
2015-10-29 18:10:07 +01:00
drawName = messageObject.messageOwner.to_id.channel_id != 0 && !messageObject.isOutOwner();
}
}
backgroundWidth = maxWidth;
2016-03-06 02:49:31 +01:00
availableTimeWidth = backgroundWidth - AndroidUtilities.dp(29);
super.setMessageObject(messageObject);
backgroundWidth = messageObject.textWidth;
totalHeight = messageObject.textHeight + AndroidUtilities.dp(19.5f) + namesOffset;
int maxChildWidth = Math.max(backgroundWidth, nameWidth);
maxChildWidth = Math.max(maxChildWidth, forwardedNameWidth);
maxChildWidth = Math.max(maxChildWidth, replyNameWidth);
maxChildWidth = Math.max(maxChildWidth, replyTextWidth);
2015-05-21 23:27:27 +02:00
int maxWebWidth = 0;
int timeMore = timeWidth + AndroidUtilities.dp(6);
2015-09-24 22:52:02 +02:00
if (messageObject.isOutOwner()) {
timeMore += AndroidUtilities.dp(20.5f);
}
2015-04-09 20:00:14 +02:00
if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaWebPage && messageObject.messageOwner.media.webpage instanceof TLRPC.TL_webPage) {
int linkPreviewMaxWidth;
if (AndroidUtilities.isTablet()) {
2016-03-06 02:49:31 +01:00
if (messageObject.isFromUser() && (currentMessageObject.messageOwner.to_id.channel_id != 0 || currentMessageObject.messageOwner.to_id.chat_id != 0) && !currentMessageObject.isOut()) {
2015-04-09 20:00:14 +02:00
linkPreviewMaxWidth = AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(122);
} else {
linkPreviewMaxWidth = AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(80);
}
} else {
2016-03-06 02:49:31 +01:00
if (messageObject.isFromUser() && (currentMessageObject.messageOwner.to_id.channel_id != 0 || currentMessageObject.messageOwner.to_id.chat_id != 0) && !currentMessageObject.isOutOwner()) {
2015-04-09 20:00:14 +02:00
linkPreviewMaxWidth = Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) - AndroidUtilities.dp(122);
} else {
linkPreviewMaxWidth = Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) - AndroidUtilities.dp(80);
}
}
2016-03-06 02:49:31 +01:00
if (drawShareButton) {
linkPreviewMaxWidth -= AndroidUtilities.dp(20);
}
TLRPC.TL_webPage webPage = (TLRPC.TL_webPage) messageObject.messageOwner.media.webpage;
if (webPage.site_name != null && webPage.photo != null && webPage.site_name.toLowerCase().equals("instagram")) {
linkPreviewMaxWidth = Math.max(AndroidUtilities.displaySize.y / 3, currentMessageObject.textWidth);
}
2015-04-09 20:00:14 +02:00
int additinalWidth = AndroidUtilities.dp(10);
int restLinesCount = 3;
int additionalHeight = 0;
linkPreviewMaxWidth -= additinalWidth;
hasLinkPreview = true;
if (currentMessageObject.photoThumbs == null && webPage.photo != null) {
currentMessageObject.generateThumbs(true);
}
2015-05-21 23:27:27 +02:00
isSmallImage = webPage.description != null && webPage.type != null && (webPage.type.equals("app") || webPage.type.equals("profile") || webPage.type.equals("article")) && currentMessageObject.photoThumbs != null;
2015-04-09 20:00:14 +02:00
if (webPage.site_name != null) {
try {
int width = (int) Math.ceil(replyNamePaint.measureText(webPage.site_name));
siteNameLayout = new StaticLayout(webPage.site_name, replyNamePaint, Math.min(width, linkPreviewMaxWidth), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
int height = siteNameLayout.getLineBottom(siteNameLayout.getLineCount() - 1);
linkPreviewHeight += height;
totalHeight += height;
additionalHeight += height;
2015-05-21 23:27:27 +02:00
width = siteNameLayout.getWidth();
2015-04-09 20:00:14 +02:00
maxChildWidth = Math.max(maxChildWidth, width + additinalWidth);
2015-05-21 23:27:27 +02:00
maxWebWidth = Math.max(maxWebWidth, width + additinalWidth);
2015-04-09 20:00:14 +02:00
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
2015-05-21 23:27:27 +02:00
boolean titleIsRTL = false;
2015-04-09 20:00:14 +02:00
if (webPage.title != null) {
try {
2015-05-21 23:27:27 +02:00
titleX = 0;
2015-04-09 20:00:14 +02:00
if (linkPreviewHeight != 0) {
linkPreviewHeight += AndroidUtilities.dp(2);
totalHeight += AndroidUtilities.dp(2);
}
int restLines = 0;
if (!isSmallImage || webPage.description == null) {
2015-05-21 23:27:27 +02:00
titleLayout = StaticLayoutEx.createStaticLayout(webPage.title, replyNamePaint, linkPreviewMaxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, AndroidUtilities.dp(1), false, TextUtils.TruncateAt.END, linkPreviewMaxWidth, 4);
2015-04-09 20:00:14 +02:00
} else {
restLines = restLinesCount;
2015-05-21 23:27:27 +02:00
titleLayout = generateStaticLayout(webPage.title, replyNamePaint, linkPreviewMaxWidth, linkPreviewMaxWidth - AndroidUtilities.dp(48 + 2), restLinesCount, 4);
2015-04-09 20:00:14 +02:00
restLinesCount -= titleLayout.getLineCount();
}
int height = titleLayout.getLineBottom(titleLayout.getLineCount() - 1);
linkPreviewHeight += height;
totalHeight += height;
for (int a = 0; a < titleLayout.getLineCount(); a++) {
2015-05-21 23:27:27 +02:00
int lineLeft = (int) titleLayout.getLineLeft(a);
if (lineLeft != 0) {
titleIsRTL = true;
if (titleX == 0) {
titleX = -lineLeft;
} else {
titleX = Math.max(titleX, -lineLeft);
}
}
int width;
if (lineLeft != 0) {
width = titleLayout.getWidth() - lineLeft;
} else {
width = (int) Math.ceil(titleLayout.getLineWidth(a));
}
if (a < restLines || lineLeft != 0 && isSmallImage) {
2015-04-09 20:00:14 +02:00
width += AndroidUtilities.dp(48 + 2);
}
maxChildWidth = Math.max(maxChildWidth, width + additinalWidth);
2015-05-21 23:27:27 +02:00
maxWebWidth = Math.max(maxWebWidth, width + additinalWidth);
2015-04-09 20:00:14 +02:00
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
2015-11-26 22:04:02 +01:00
boolean authorIsRTL = false;
2015-04-09 20:00:14 +02:00
if (webPage.author != null) {
try {
if (linkPreviewHeight != 0) {
linkPreviewHeight += AndroidUtilities.dp(2);
totalHeight += AndroidUtilities.dp(2);
}
2015-11-26 22:04:02 +01:00
//int width = Math.min((int) Math.ceil(replyNamePaint.measureText(webPage.author)), linkPreviewMaxWidth);
2015-04-09 20:00:14 +02:00
if (restLinesCount == 3 && (!isSmallImage || webPage.description == null)) {
2015-11-26 22:04:02 +01:00
authorLayout = new StaticLayout(webPage.author, replyNamePaint, linkPreviewMaxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
2015-04-09 20:00:14 +02:00
} else {
2015-11-26 22:04:02 +01:00
authorLayout = generateStaticLayout(webPage.author, replyNamePaint, linkPreviewMaxWidth, linkPreviewMaxWidth - AndroidUtilities.dp(48 + 2), restLinesCount, 1);
2015-04-09 20:00:14 +02:00
restLinesCount -= authorLayout.getLineCount();
}
int height = authorLayout.getLineBottom(authorLayout.getLineCount() - 1);
linkPreviewHeight += height;
totalHeight += height;
2015-05-21 23:27:27 +02:00
int lineLeft = (int) authorLayout.getLineLeft(0);
authorX = -lineLeft;
2015-11-26 22:04:02 +01:00
int width;
if (lineLeft != 0) {
width = authorLayout.getWidth() - lineLeft;
authorIsRTL = true;
} else {
width = (int) Math.ceil(authorLayout.getLineWidth(0));
}
2015-04-09 20:00:14 +02:00
maxChildWidth = Math.max(maxChildWidth, width + additinalWidth);
2015-05-21 23:27:27 +02:00
maxWebWidth = Math.max(maxWebWidth, width + additinalWidth);
2015-04-09 20:00:14 +02:00
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
if (webPage.description != null) {
try {
2015-05-21 23:27:27 +02:00
descriptionX = 0;
2015-04-09 20:00:14 +02:00
currentMessageObject.generateLinkDescription();
if (linkPreviewHeight != 0) {
linkPreviewHeight += AndroidUtilities.dp(2);
totalHeight += AndroidUtilities.dp(2);
}
int restLines = 0;
if (restLinesCount == 3 && !isSmallImage) {
descriptionLayout = StaticLayoutEx.createStaticLayout(messageObject.linkDescription, replyTextPaint, linkPreviewMaxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, AndroidUtilities.dp(1), false, TextUtils.TruncateAt.END, linkPreviewMaxWidth, 6);
} else {
restLines = restLinesCount;
descriptionLayout = generateStaticLayout(messageObject.linkDescription, replyTextPaint, linkPreviewMaxWidth, linkPreviewMaxWidth - AndroidUtilities.dp(48 + 2), restLinesCount, 6);
}
int height = descriptionLayout.getLineBottom(descriptionLayout.getLineCount() - 1);
linkPreviewHeight += height;
totalHeight += height;
2015-11-26 22:04:02 +01:00
boolean hasRTL = false;
2015-04-09 20:00:14 +02:00
for (int a = 0; a < descriptionLayout.getLineCount(); a++) {
2015-05-21 23:27:27 +02:00
int lineLeft = (int) Math.ceil(descriptionLayout.getLineLeft(a));
2015-11-26 22:04:02 +01:00
if (lineLeft != 0) {
hasRTL = true;
if (descriptionX == 0) {
descriptionX = -lineLeft;
} else {
descriptionX = Math.max(descriptionX, -lineLeft);
}
}
}
for (int a = 0; a < descriptionLayout.getLineCount(); a++) {
int lineLeft = (int) Math.ceil(descriptionLayout.getLineLeft(a));
if (lineLeft == 0 && descriptionX != 0) {
descriptionX = 0;
2015-05-21 23:27:27 +02:00
}
int width;
if (lineLeft != 0) {
width = descriptionLayout.getWidth() - lineLeft;
} else {
2015-11-26 22:04:02 +01:00
width = hasRTL ? descriptionLayout.getWidth() : (int) Math.ceil(descriptionLayout.getLineWidth(a));
2015-05-21 23:27:27 +02:00
}
if (a < restLines || restLines != 0 && lineLeft != 0 && isSmallImage) {
2015-04-09 20:00:14 +02:00
width += AndroidUtilities.dp(48 + 2);
}
2015-05-21 23:27:27 +02:00
if (maxWebWidth < width + additinalWidth) {
if (titleIsRTL) {
titleX += (width + additinalWidth - maxWebWidth);
}
2015-11-26 22:04:02 +01:00
if (authorIsRTL) {
authorX += (width + additinalWidth - maxWebWidth);
}
2015-05-21 23:27:27 +02:00
maxWebWidth = width + additinalWidth;
}
if (restLines == 0 || !isSmallImage) {
if (titleIsRTL) {
titleX = -AndroidUtilities.dp(4);
}
if (authorIsRTL) {
authorX = -AndroidUtilities.dp(4);
}
}
2015-04-09 20:00:14 +02:00
maxChildWidth = Math.max(maxChildWidth, width + additinalWidth);
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
boolean smallImage = webPage.type != null && (webPage.type.equals("app") || webPage.type.equals("profile") || webPage.type.equals("article"));
if (smallImage && (descriptionLayout == null || descriptionLayout != null && descriptionLayout.getLineCount() == 1)) {
smallImage = false;
isSmallImage = false;
}
int maxPhotoWidth = smallImage ? AndroidUtilities.dp(48) : linkPreviewMaxWidth;
if (webPage.document != null && MessageObject.isGifDocument(webPage.document)) {
if (!MediaController.getInstance().canAutoplayGifs()) {
messageObject.audioProgress = 1;
2015-04-09 20:00:14 +02:00
}
linkImageView.setAllowStartAnimation(messageObject.audioProgress != 1);
currentPhotoObject = webPage.document.thumb;
isGifDocument = true;
} else if (webPage.photo != null) {
2015-08-13 11:23:31 +02:00
currentPhotoObject = FileLoader.getClosestPhotoSizeWithSize(messageObject.photoThumbs, drawImageButton ? AndroidUtilities.getPhotoSize() : maxPhotoWidth, !drawImageButton);
currentPhotoObjectThumb = FileLoader.getClosestPhotoSizeWithSize(messageObject.photoThumbs, 80);
2015-04-09 20:00:14 +02:00
if (currentPhotoObjectThumb == currentPhotoObject) {
currentPhotoObjectThumb = null;
}
}
2015-04-09 20:00:14 +02:00
if (currentPhotoObject != null) {
drawImageButton = webPage.type != null && (webPage.type.equals("photo") || webPage.type.equals("document") || webPage.type.equals("gif"));
if (linkPreviewHeight != 0) {
linkPreviewHeight += AndroidUtilities.dp(2);
totalHeight += AndroidUtilities.dp(2);
}
2015-04-09 20:00:14 +02:00
maxChildWidth = Math.max(maxChildWidth, maxPhotoWidth + additinalWidth);
currentPhotoObject.size = -1;
if (currentPhotoObjectThumb != null) {
currentPhotoObjectThumb.size = -1;
}
int width;
int height;
if (smallImage) {
width = height = maxPhotoWidth;
} else {
width = currentPhotoObject.w;
height = currentPhotoObject.h;
float scale = width / (float) maxPhotoWidth;
width /= scale;
height /= scale;
if (webPage.site_name == null || webPage.site_name != null && !webPage.site_name.toLowerCase().equals("instagram") && !isGifDocument) {
if (height > AndroidUtilities.displaySize.y / 3) {
height = AndroidUtilities.displaySize.y / 3;
2015-04-09 20:00:14 +02:00
}
}
}
if (isSmallImage) {
if (AndroidUtilities.dp(50) + additionalHeight > linkPreviewHeight) {
totalHeight += AndroidUtilities.dp(50) + additionalHeight - linkPreviewHeight + AndroidUtilities.dp(8);
linkPreviewHeight = AndroidUtilities.dp(50) + additionalHeight;
2015-04-09 20:00:14 +02:00
}
linkPreviewHeight -= AndroidUtilities.dp(8);
} else {
totalHeight += height + AndroidUtilities.dp(12);
linkPreviewHeight += height;
}
2015-04-09 20:00:14 +02:00
linkImageView.setImageCoords(0, 0, width, height);
2015-05-21 23:27:27 +02:00
currentPhotoFilter = String.format(Locale.US, "%d_%d", width, height);
currentPhotoFilterThumb = String.format(Locale.US, "%d_%d_b", width, height);
2015-05-21 23:27:27 +02:00
if (isGifDocument) {
boolean photoExist = true;
File cacheFile = FileLoader.getPathToAttach(webPage.document);
if (!cacheFile.exists()) {
photoExist = false;
}
String fileName = FileLoader.getAttachFileName(webPage.document);
if (photoExist || MediaController.getInstance().canDownloadMedia(MediaController.AUTODOWNLOAD_MASK_GIF) || FileLoader.getInstance().isLoadingFile(fileName)) {
photoNotSet = false;
linkImageView.setImage(webPage.document, null, currentPhotoObject.location, currentPhotoFilter, webPage.document.size, null, false);
} else {
photoNotSet = true;
linkImageView.setImage(null, null, currentPhotoObject.location, currentPhotoFilter, 0, null, false);
}
} else {
2015-05-21 23:27:27 +02:00
boolean photoExist = true;
File cacheFile = FileLoader.getPathToAttach(currentPhotoObject, true);
if (!cacheFile.exists()) {
photoExist = false;
}
String fileName = FileLoader.getAttachFileName(currentPhotoObject);
2015-05-21 23:27:27 +02:00
if (photoExist || MediaController.getInstance().canDownloadMedia(MediaController.AUTODOWNLOAD_MASK_PHOTO) || FileLoader.getInstance().isLoadingFile(fileName)) {
2015-08-13 11:23:31 +02:00
photoNotSet = false;
linkImageView.setImage(currentPhotoObject.location, currentPhotoFilter, currentPhotoObjectThumb != null ? currentPhotoObjectThumb.location : null, currentPhotoFilterThumb, 0, null, false);
2015-05-21 23:27:27 +02:00
} else {
2015-08-13 11:23:31 +02:00
photoNotSet = true;
2015-05-21 23:27:27 +02:00
if (currentPhotoObjectThumb != null) {
linkImageView.setImage(null, null, currentPhotoObjectThumb.location, String.format(Locale.US, "%d_%d_b", width, height), 0, null, false);
} else {
linkImageView.setImageBitmap((Drawable) null);
}
}
}
drawLinkImageView = true;
2015-04-09 20:00:14 +02:00
if (webPage.site_name != null) {
if (webPage.site_name.toLowerCase().equals("instagram") && webPage.type != null && webPage.type.equals("video")) {
isInstagram = true;
if (igvideoDrawable == null) {
igvideoDrawable = getResources().getDrawable(R.drawable.igvideo);
2015-04-09 20:00:14 +02:00
}
}
}
2015-06-29 19:12:11 +02:00
if (webPage.type != null && webPage.type.equals("video") && webPage.duration != 0) {
2015-04-09 20:00:14 +02:00
if (durationPaint == null) {
durationPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
durationPaint.setTextSize(AndroidUtilities.dp(12));
durationPaint.setColor(0xffffffff);
}
int minutes = webPage.duration / 60;
int seconds = webPage.duration - minutes * 60;
String str = String.format("%d:%02d", minutes, seconds);
durationWidth = (int) Math.ceil(durationPaint.measureText(str));
durationLayout = new StaticLayout(str, durationPaint, durationWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
}
} else {
2015-09-24 22:52:02 +02:00
linkImageView.setImageBitmap((Drawable) null);
2015-04-09 20:00:14 +02:00
linkPreviewHeight -= AndroidUtilities.dp(6);
totalHeight += AndroidUtilities.dp(4);
}
2015-09-24 22:52:02 +02:00
} else {
linkImageView.setImageBitmap((Drawable) null);
2015-04-09 20:00:14 +02:00
}
if (hasLinkPreview || maxWidth - messageObject.lastLineWidth < timeMore) {
totalHeight += AndroidUtilities.dp(14);
backgroundWidth = Math.max(maxChildWidth, messageObject.lastLineWidth) + AndroidUtilities.dp(29);
2016-03-06 02:49:31 +01:00
backgroundWidth = Math.max(backgroundWidth, timeWidth + AndroidUtilities.dp(29));
} else {
int diff = maxChildWidth - messageObject.lastLineWidth;
if (diff >= 0 && diff <= timeMore) {
backgroundWidth = maxChildWidth + timeMore - diff + AndroidUtilities.dp(29);
} else {
backgroundWidth = Math.max(maxChildWidth, messageObject.lastLineWidth + timeMore) + AndroidUtilities.dp(29);
}
}
}
2015-08-13 11:23:31 +02:00
updateButtonState(dataChanged);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), totalHeight);
}
2014-03-10 10:27:49 +01:00
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
2015-09-24 22:52:02 +02:00
if (currentMessageObject.isOutOwner()) {
textX = layoutWidth - backgroundWidth + AndroidUtilities.dp(10);
textY = AndroidUtilities.dp(10) + namesOffset;
2014-03-10 10:27:49 +01:00
} else {
2016-03-06 02:49:31 +01:00
textX = AndroidUtilities.dp(19) + (isChat && currentMessageObject.isFromUser() ? AndroidUtilities.dp(52) : 0);
textY = AndroidUtilities.dp(10) + namesOffset;
2014-03-10 10:27:49 +01:00
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
2015-04-09 20:00:14 +02:00
if (currentMessageObject == null || currentMessageObject.textLayoutBlocks == null || currentMessageObject.textLayoutBlocks.isEmpty()) {
return;
}
2015-09-24 22:52:02 +02:00
if (currentMessageObject.isOutOwner()) {
textX = layoutWidth - backgroundWidth + AndroidUtilities.dp(10);
textY = AndroidUtilities.dp(10) + namesOffset;
} else {
2016-03-06 02:49:31 +01:00
textX = AndroidUtilities.dp(19) + (isChat && currentMessageObject.isFromUser() ? AndroidUtilities.dp(52) : 0);
textY = AndroidUtilities.dp(10) + namesOffset;
}
2015-04-09 20:00:14 +02:00
if (firstVisibleBlockNum >= 0) {
for (int a = firstVisibleBlockNum; a <= lastVisibleBlockNum; a++) {
if (a >= currentMessageObject.textLayoutBlocks.size()) {
break;
}
MessageObject.TextLayoutBlock block = currentMessageObject.textLayoutBlocks.get(a);
canvas.save();
canvas.translate(textX - (int) Math.ceil(block.textXOffset), textY + block.textYOffset);
if (pressedLink != null && a == linkBlockNum) {
canvas.drawPath(urlPath, urlPaint);
}
try {
block.textLayout.draw(canvas);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
canvas.restore();
}
}
if (hasLinkPreview) {
int startY = textY + currentMessageObject.textHeight + AndroidUtilities.dp(8);
int linkPreviewY = startY;
int smallImageStartY = 0;
2015-09-24 22:52:02 +02:00
replyLinePaint.setColor(currentMessageObject.isOutOwner() ? 0xff8dc97a : 0xff6c9fd2);
2015-04-09 20:00:14 +02:00
canvas.drawRect(textX, linkPreviewY - AndroidUtilities.dp(3), textX + AndroidUtilities.dp(2), linkPreviewY + linkPreviewHeight + AndroidUtilities.dp(3), replyLinePaint);
if (siteNameLayout != null) {
2015-09-24 22:52:02 +02:00
replyNamePaint.setColor(currentMessageObject.isOutOwner() ? 0xff70b15c : 0xff4b91cf);
2015-04-09 20:00:14 +02:00
canvas.save();
canvas.translate(textX + AndroidUtilities.dp(10), linkPreviewY - AndroidUtilities.dp(3));
siteNameLayout.draw(canvas);
canvas.restore();
linkPreviewY += siteNameLayout.getLineBottom(siteNameLayout.getLineCount() - 1);
}
if (titleLayout != null) {
if (linkPreviewY != startY) {
linkPreviewY += AndroidUtilities.dp(2);
}
replyNamePaint.setColor(0xff000000);
smallImageStartY = linkPreviewY - AndroidUtilities.dp(1);
canvas.save();
2015-05-21 23:27:27 +02:00
canvas.translate(textX + AndroidUtilities.dp(10) + titleX, linkPreviewY - AndroidUtilities.dp(3));
2015-04-09 20:00:14 +02:00
titleLayout.draw(canvas);
canvas.restore();
linkPreviewY += titleLayout.getLineBottom(titleLayout.getLineCount() - 1);
}
if (authorLayout != null) {
if (linkPreviewY != startY) {
linkPreviewY += AndroidUtilities.dp(2);
}
if (smallImageStartY == 0) {
smallImageStartY = linkPreviewY - AndroidUtilities.dp(1);
}
replyNamePaint.setColor(0xff000000);
canvas.save();
2015-05-21 23:27:27 +02:00
canvas.translate(textX + AndroidUtilities.dp(10) + authorX, linkPreviewY - AndroidUtilities.dp(3));
2015-04-09 20:00:14 +02:00
authorLayout.draw(canvas);
canvas.restore();
linkPreviewY += authorLayout.getLineBottom(authorLayout.getLineCount() - 1);
}
if (descriptionLayout != null) {
if (linkPreviewY != startY) {
linkPreviewY += AndroidUtilities.dp(2);
}
if (smallImageStartY == 0) {
smallImageStartY = linkPreviewY - AndroidUtilities.dp(1);
}
replyTextPaint.setColor(0xff000000);
descriptionY = linkPreviewY - AndroidUtilities.dp(3);
canvas.save();
2015-05-21 23:27:27 +02:00
canvas.translate(textX + AndroidUtilities.dp(10) + descriptionX, descriptionY);
2015-04-09 20:00:14 +02:00
if (pressedLink != null && linkBlockNum == -10) {
canvas.drawPath(urlPath, urlPaint);
}
descriptionLayout.draw(canvas);
canvas.restore();
linkPreviewY += descriptionLayout.getLineBottom(descriptionLayout.getLineCount() - 1);
}
if (drawLinkImageView) {
if (linkPreviewY != startY) {
linkPreviewY += AndroidUtilities.dp(2);
}
if (isSmallImage) {
linkImageView.setImageCoords(textX + backgroundWidth - AndroidUtilities.dp(77), smallImageStartY, linkImageView.getImageWidth(), linkImageView.getImageHeight());
2015-04-09 20:00:14 +02:00
} else {
linkImageView.setImageCoords(textX + AndroidUtilities.dp(10), linkPreviewY, linkImageView.getImageWidth(), linkImageView.getImageHeight());
2015-08-13 11:23:31 +02:00
if (drawImageButton) {
int size = AndroidUtilities.dp(48);
buttonX = (int) (linkImageView.getImageX() + (linkImageView.getImageWidth() - size) / 2.0f);
2015-09-02 00:14:21 +02:00
buttonY = (int) (linkImageView.getImageY() + (linkImageView.getImageHeight() - size) / 2.0f);
2015-08-13 11:23:31 +02:00
radialProgress.setProgressRect(buttonX, buttonY, buttonX + AndroidUtilities.dp(48), buttonY + AndroidUtilities.dp(48));
}
2015-04-09 20:00:14 +02:00
}
linkImageView.draw(canvas);
2015-08-13 11:23:31 +02:00
if (drawImageButton) {
radialProgress.draw(canvas);
}
2015-04-09 20:00:14 +02:00
if (isInstagram && igvideoDrawable != null) {
int x = linkImageView.getImageX() + linkImageView.getImageWidth() - igvideoDrawable.getIntrinsicWidth() - AndroidUtilities.dp(4);
int y = linkImageView.getImageY() + AndroidUtilities.dp(4);
igvideoDrawable.setBounds(x, y, x + igvideoDrawable.getIntrinsicWidth(), y + igvideoDrawable.getIntrinsicHeight());
igvideoDrawable.draw(canvas);
}
if (durationLayout != null) {
int x = linkImageView.getImageX() + linkImageView.getImageWidth() - AndroidUtilities.dp(8) - durationWidth;
int y = linkImageView.getImageY() + linkImageView.getImageHeight() - AndroidUtilities.dp(19);
ResourceLoader.mediaBackgroundDrawable.setBounds(x - AndroidUtilities.dp(4), y - AndroidUtilities.dp(1.5f), x + durationWidth + AndroidUtilities.dp(4), y + AndroidUtilities.dp(14.5f));
ResourceLoader.mediaBackgroundDrawable.draw(canvas);
2015-04-09 20:00:14 +02:00
canvas.save();
canvas.translate(x, y);
durationLayout.draw(canvas);
canvas.restore();
}
2014-03-10 10:27:49 +01:00
}
}
}
2015-08-13 11:23:31 +02:00
private Drawable getDrawableForCurrentState() {
if (buttonState >= 0 && buttonState < 4) {
if (buttonState == 1) {
return ResourceLoader.buttonStatesDrawables[4];
} else {
return ResourceLoader.buttonStatesDrawables[buttonState];
}
}
return null;
}
public void updateButtonState(boolean animated) {
if (currentPhotoObject == null || !drawImageButton) {
return;
}
String fileName;
File cacheFile;
if (isGifDocument) {
fileName = FileLoader.getAttachFileName(currentMessageObject.messageOwner.media.webpage.document);
cacheFile = FileLoader.getPathToAttach(currentMessageObject.messageOwner.media.webpage.document);
} else {
fileName = FileLoader.getAttachFileName(currentPhotoObject);
cacheFile = FileLoader.getPathToAttach(currentPhotoObject, true);
}
2015-08-13 11:23:31 +02:00
if (fileName == null) {
radialProgress.setBackground(null, false, false);
return;
}
if (!cacheFile.exists()) {
MediaController.getInstance().addLoadingFileObserver(fileName, this);
float setProgress = 0;
boolean progressVisible = false;
if (!FileLoader.getInstance().isLoadingFile(fileName)) {
if (!cancelLoading &&
(!isGifDocument && MediaController.getInstance().canDownloadMedia(MediaController.AUTODOWNLOAD_MASK_PHOTO) ||
isGifDocument && MediaController.getInstance().canDownloadMedia(MediaController.AUTODOWNLOAD_MASK_GIF)) ) {
2015-08-13 11:23:31 +02:00
progressVisible = true;
buttonState = 1;
} else {
buttonState = 0;
2015-08-13 11:23:31 +02:00
}
} else {
progressVisible = true;
buttonState = 1;
Float progress = ImageLoader.getInstance().getFileProgress(fileName);
setProgress = progress != null ? progress : 0;
}
radialProgress.setProgress(setProgress, false);
radialProgress.setBackground(getDrawableForCurrentState(), progressVisible, animated);
invalidate();
} else {
MediaController.getInstance().removeLoadingFileObserver(this);
if (isGifDocument && !linkImageView.isAllowStartAnimation()) {
buttonState = 2;
} else {
buttonState = -1;
}
2015-08-13 11:23:31 +02:00
radialProgress.setBackground(getDrawableForCurrentState(), false, animated);
invalidate();
}
}
private void didPressedButton(boolean animated) {
if (buttonState == 0) {
cancelLoading = false;
radialProgress.setProgress(0, false);
if (isGifDocument) {
linkImageView.setImage(currentMessageObject.messageOwner.media.webpage.document, null, currentPhotoObject.location, currentPhotoFilter, currentMessageObject.messageOwner.media.webpage.document.size, null, false);
currentMessageObject.audioProgress = 2;
} else {
linkImageView.setImage(currentPhotoObject.location, currentPhotoFilter, currentPhotoObjectThumb != null ? currentPhotoObjectThumb.location : null, currentPhotoFilterThumb, 0, null, false);
}
2015-08-13 11:23:31 +02:00
buttonState = 1;
radialProgress.setBackground(getDrawableForCurrentState(), true, animated);
invalidate();
} else if (buttonState == 1) {
if (currentMessageObject.isOut() && currentMessageObject.isSending()) {
if (delegate != null) {
delegate.didPressedCancelSendButton(this);
}
} else {
cancelLoading = true;
linkImageView.cancelLoadImage();
buttonState = 0;
radialProgress.setBackground(getDrawableForCurrentState(), false, animated);
invalidate();
}
} else if (buttonState == 2) {
linkImageView.setAllowStartAnimation(true);
linkImageView.startAnimation();
currentMessageObject.audioProgress = 0;
buttonState = -1;
radialProgress.setBackground(getDrawableForCurrentState(), false, animated);
2015-08-13 11:23:31 +02:00
}
}
@Override
public void onFailedDownload(String fileName) {
updateButtonState(false);
}
@Override
public void onSuccessDownload(String fileName) {
radialProgress.setProgress(1, true);
if (isGifDocument && currentMessageObject.audioProgress != 1) {
buttonState = 2;
didPressedButton(true);
} else if (!photoNotSet) {
2015-08-13 11:23:31 +02:00
updateButtonState(true);
} else {
setMessageObject(currentMessageObject);
}
}
@Override
public void onProgressDownload(String fileName, float progress) {
radialProgress.setProgress(progress, true);
if (buttonState != 1) {
updateButtonState(false);
}
}
2015-10-29 18:10:07 +01:00
@Override
public void onProvideStructure(ViewStructure structure) {
super.onProvideStructure(structure);
if (allowAssistant && Build.VERSION.SDK_INT >= 23) {
structure.setText(currentMessageObject.messageText);
}
}
}