NekoX/TMessagesProj/src/main/java/org/telegram/messenger/MessageObject.java

1446 lines
68 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
2015-10-29 18:10:07 +01:00
* This is the source code of Telegram for Android v. 3.x.x.
2013-10-25 17:19:00 +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).
*
* Copyright Nikolai Kudashov, 2013-2016.
2013-10-25 17:19:00 +02:00
*/
2015-09-24 22:52:02 +02:00
package org.telegram.messenger;
2013-10-25 17:19:00 +02:00
import android.graphics.Paint;
2015-09-02 00:14:21 +02:00
import android.graphics.Typeface;
import android.text.Layout;
import android.text.Spannable;
2014-10-15 20:43:52 +02:00
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.StaticLayout;
import android.text.TextPaint;
2014-10-15 20:43:52 +02:00
import android.text.TextUtils;
2015-09-02 00:14:21 +02:00
import android.text.style.URLSpan;
import android.text.util.Linkify;
2013-10-25 17:19:00 +02:00
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.ConnectionsManager;
2015-11-26 22:04:02 +01:00
import org.telegram.tgnet.TLObject;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.TLRPC;
2015-09-02 00:14:21 +02:00
import org.telegram.ui.Components.TypefaceSpan;
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 org.telegram.ui.Components.URLSpanNoUnderlineBold;
2015-09-02 00:14:21 +02:00
import org.telegram.ui.Components.URLSpanReplacement;
2013-10-25 17:19:00 +02:00
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2013-10-25 17:19:00 +02:00
public class MessageObject {
public static final int MESSAGE_SEND_STATE_SENDING = 1;
public static final int MESSAGE_SEND_STATE_SENT = 0;
public static final int MESSAGE_SEND_STATE_SEND_ERROR = 2;
2013-10-25 17:19:00 +02:00
public TLRPC.Message messageOwner;
public CharSequence messageText;
2015-04-09 20:00:14 +02:00
public CharSequence linkDescription;
public CharSequence caption;
public MessageObject replyMessageObject;
2013-10-25 17:19:00 +02:00
public int type;
public int contentType;
2013-10-25 17:19:00 +02:00
public String dateKey;
public String monthKey;
2015-05-21 23:27:27 +02:00
public boolean deleted;
public float audioProgress;
public int audioProgressSec;
2015-02-01 19:51:02 +01:00
public ArrayList<TLRPC.PhotoSize> photoThumbs;
2015-05-21 23:27:27 +02:00
public VideoEditedInfo videoEditedInfo;
2015-09-24 22:52:02 +02:00
public boolean viewsReloaded;
public static TextPaint textPaint;
public int lastLineWidth;
public int textWidth;
public int textHeight;
public int blockHeight = Integer.MAX_VALUE;
2015-06-29 19:12:11 +02:00
public static Pattern urlPattern;
public static class TextLayoutBlock {
public StaticLayout textLayout;
public float textXOffset = 0;
public float textYOffset = 0;
public int charactersOffset = 0;
}
private static final int LINES_PER_BLOCK = 10;
public ArrayList<TextLayoutBlock> textLayoutBlocks;
2013-10-25 17:19:00 +02:00
2015-02-01 19:51:02 +01:00
public MessageObject(TLRPC.Message message, AbstractMap<Integer, TLRPC.User> users, boolean generateLayout) {
2015-11-26 22:04:02 +01:00
this(message, users, null, generateLayout);
}
public MessageObject(TLRPC.Message message, AbstractMap<Integer, TLRPC.User> users, AbstractMap<Integer, TLRPC.Chat> chats, boolean generateLayout) {
if (textPaint == null) {
textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(0xff000000);
textPaint.linkColor = 0xff316f9f;
}
textPaint.setTextSize(AndroidUtilities.dp(MessagesController.getInstance().fontSize));
2013-10-25 17:19:00 +02:00
messageOwner = message;
if (message.replyMessage != null) {
2015-11-26 22:04:02 +01:00
replyMessageObject = new MessageObject(message.replyMessage, users, chats, false);
}
2013-10-25 17:19:00 +02:00
if (message instanceof TLRPC.TL_messageService) {
if (message.action != null) {
TLRPC.User fromUser = null;
if (users != null) {
fromUser = users.get(message.from_id);
}
2013-10-25 17:19:00 +02:00
if (fromUser == null) {
fromUser = MessagesController.getInstance().getUser(message.from_id);
2013-10-25 17:19:00 +02:00
}
if (message.action instanceof TLRPC.TL_messageActionChatCreate) {
2015-03-26 18:34:47 +01:00
if (isOut()) {
messageText = LocaleController.getString("ActionYouCreateGroup", R.string.ActionYouCreateGroup);
2013-12-20 20:25:49 +01:00
} else {
if (fromUser != null) {
2014-10-15 20:43:52 +02:00
messageText = replaceWithLink(LocaleController.getString("ActionCreateGroup", R.string.ActionCreateGroup), "un1", fromUser);
2013-12-20 20:25:49 +01:00
} else {
messageText = LocaleController.getString("ActionCreateGroup", R.string.ActionCreateGroup).replace("un1", "");
2013-12-20 20:25:49 +01:00
}
}
2013-10-25 17:19:00 +02:00
} else if (message.action instanceof TLRPC.TL_messageActionChatDeleteUser) {
if (message.action.user_id == message.from_id) {
2015-03-26 18:34:47 +01:00
if (isOut()) {
messageText = LocaleController.getString("ActionYouLeftUser", R.string.ActionYouLeftUser);
2013-12-20 20:25:49 +01:00
} else {
if (fromUser != null) {
2014-10-15 20:43:52 +02:00
messageText = replaceWithLink(LocaleController.getString("ActionLeftUser", R.string.ActionLeftUser), "un1", fromUser);
2013-12-20 20:25:49 +01:00
} else {
messageText = LocaleController.getString("ActionLeftUser", R.string.ActionLeftUser).replace("un1", "");
2013-12-20 20:25:49 +01:00
}
}
2013-10-25 17:19:00 +02:00
} else {
2014-10-15 20:43:52 +02:00
TLRPC.User whoUser = null;
if (users != null) {
2014-10-15 20:43:52 +02:00
whoUser = users.get(message.action.user_id);
}
2014-10-15 20:43:52 +02:00
if (whoUser == null) {
whoUser = MessagesController.getInstance().getUser(message.action.user_id);
2013-12-20 20:25:49 +01:00
}
2014-10-15 20:43:52 +02:00
if (whoUser != null && fromUser != null) {
2015-03-26 18:34:47 +01:00
if (isOut()) {
2014-10-15 20:43:52 +02:00
messageText = replaceWithLink(LocaleController.getString("ActionYouKickUser", R.string.ActionYouKickUser), "un2", whoUser);
2014-06-13 12:42:21 +02:00
} else if (message.action.user_id == UserConfig.getClientUserId()) {
2014-10-15 20:43:52 +02:00
messageText = replaceWithLink(LocaleController.getString("ActionKickUserYou", R.string.ActionKickUserYou), "un1", fromUser);
2013-12-20 20:25:49 +01:00
} else {
2014-10-15 20:43:52 +02:00
messageText = replaceWithLink(LocaleController.getString("ActionKickUser", R.string.ActionKickUser), "un2", whoUser);
messageText = replaceWithLink(messageText, "un1", fromUser);
2013-12-20 20:25:49 +01:00
}
} else {
messageText = LocaleController.getString("ActionKickUser", R.string.ActionKickUser).replace("un2", "").replace("un1", "");
2013-12-20 20:25:49 +01:00
}
2013-10-25 17:19:00 +02:00
}
} else if (message.action instanceof TLRPC.TL_messageActionChatAddUser) {
2015-11-26 22:04:02 +01:00
int singleUserId = messageOwner.action.user_id;
if (singleUserId == 0 && messageOwner.action.users.size() == 1) {
singleUserId = messageOwner.action.users.get(0);
}
2015-11-26 22:04:02 +01:00
if (singleUserId != 0) {
TLRPC.User whoUser = null;
if (users != null) {
whoUser = users.get(singleUserId);
2015-09-24 22:52:02 +02:00
}
2015-11-26 22:04:02 +01:00
if (whoUser == null) {
whoUser = MessagesController.getInstance().getUser(singleUserId);
}
if (message.to_id.channel_id != 0 && !isMegagroup()) {
if (whoUser != null && whoUser.id != UserConfig.getClientUserId()) {
if (isMegagroup()) {
messageText = replaceWithLink(LocaleController.getString("MegaAddedBy", R.string.MegaAddedBy), "un1", whoUser);
2015-09-24 22:52:02 +02:00
} else {
2015-11-26 22:04:02 +01:00
messageText = replaceWithLink(LocaleController.getString("ChannelAddedBy", R.string.ChannelAddedBy), "un1", whoUser);
2015-09-24 22:52:02 +02:00
}
} else {
2015-11-26 22:04:02 +01:00
messageText = LocaleController.getString("ChannelJoined", R.string.ChannelJoined);
}
} else {
if (whoUser != null && fromUser != null) {
if (whoUser.id == fromUser.id) {
if (isOut()) {
messageText = LocaleController.getString("ActionAddUserSelfYou", R.string.ActionAddUserSelfYou);
} else {
messageText = replaceWithLink(LocaleController.getString("ActionAddUserSelf", R.string.ActionAddUserSelf), "un1", fromUser);
}
2015-09-24 22:52:02 +02:00
} else {
2015-11-26 22:04:02 +01:00
if (isOut()) {
messageText = replaceWithLink(LocaleController.getString("ActionYouAddUser", R.string.ActionYouAddUser), "un2", whoUser);
} else if (singleUserId == UserConfig.getClientUserId()) {
messageText = replaceWithLink(LocaleController.getString("ActionAddUserYou", R.string.ActionAddUserYou), "un1", fromUser);
} else {
messageText = replaceWithLink(LocaleController.getString("ActionAddUser", R.string.ActionAddUser), "un2", whoUser);
messageText = replaceWithLink(messageText, "un1", fromUser);
}
2015-09-24 22:52:02 +02:00
}
2015-11-26 22:04:02 +01:00
} else {
messageText = LocaleController.getString("ActionAddUser", R.string.ActionAddUser).replace("un2", "").replace("un1", "");
}
2015-11-26 22:04:02 +01:00
}
} else {
if (isOut()) {
messageText = replaceWithLink(LocaleController.getString("ActionYouAddUser", R.string.ActionYouAddUser), "un2", message.action.users, users);
2015-09-24 22:52:02 +02:00
} else {
2015-11-26 22:04:02 +01:00
messageText = replaceWithLink(LocaleController.getString("ActionAddUser", R.string.ActionAddUser), "un2", message.action.users, users);
messageText = replaceWithLink(messageText, "un1", fromUser);
2013-12-20 20:25:49 +01:00
}
}
} else if (message.action instanceof TLRPC.TL_messageActionChatJoinedByLink) {
if (fromUser != null) {
if (isOut()) {
messageText = LocaleController.getString("ActionInviteYou", R.string.ActionInviteYou);
} else {
messageText = replaceWithLink(LocaleController.getString("ActionInviteUser", R.string.ActionInviteUser), "un1", fromUser);
}
} else {
messageText = LocaleController.getString("ActionInviteUser", R.string.ActionInviteUser).replace("un1", "");
}
2013-10-25 17:19:00 +02:00
} else if (message.action instanceof TLRPC.TL_messageActionChatEditPhoto) {
2015-11-26 22:04:02 +01:00
if (message.to_id.channel_id != 0 && !isMegagroup()) {
2015-09-24 22:52:02 +02:00
messageText = LocaleController.getString("ActionChannelChangedPhoto", R.string.ActionChannelChangedPhoto);
2013-12-20 20:25:49 +01:00
} else {
2015-09-24 22:52:02 +02:00
if (isOut()) {
messageText = LocaleController.getString("ActionYouChangedPhoto", R.string.ActionYouChangedPhoto);
2013-12-20 20:25:49 +01:00
} else {
2015-09-24 22:52:02 +02:00
if (fromUser != null) {
messageText = replaceWithLink(LocaleController.getString("ActionChangedPhoto", R.string.ActionChangedPhoto), "un1", fromUser);
} else {
messageText = LocaleController.getString("ActionChangedPhoto", R.string.ActionChangedPhoto).replace("un1", "");
}
2013-12-20 20:25:49 +01:00
}
}
2013-10-25 17:19:00 +02:00
} else if (message.action instanceof TLRPC.TL_messageActionChatEditTitle) {
2015-11-26 22:04:02 +01:00
if (message.to_id.channel_id != 0 && !isMegagroup()) {
2015-09-24 22:52:02 +02:00
messageText = LocaleController.getString("ActionChannelChangedTitle", R.string.ActionChannelChangedTitle).replace("un2", message.action.title);
2013-12-20 20:25:49 +01:00
} else {
2015-09-24 22:52:02 +02:00
if (isOut()) {
messageText = LocaleController.getString("ActionYouChangedTitle", R.string.ActionYouChangedTitle).replace("un2", message.action.title);
2013-12-20 20:25:49 +01:00
} else {
2015-09-24 22:52:02 +02:00
if (fromUser != null) {
messageText = replaceWithLink(LocaleController.getString("ActionChangedTitle", R.string.ActionChangedTitle).replace("un2", message.action.title), "un1", fromUser);
} else {
messageText = LocaleController.getString("ActionChangedTitle", R.string.ActionChangedTitle).replace("un1", "").replace("un2", message.action.title);
}
2013-12-20 20:25:49 +01:00
}
}
2013-10-25 17:19:00 +02:00
} else if (message.action instanceof TLRPC.TL_messageActionChatDeletePhoto) {
2015-11-26 22:04:02 +01:00
if (message.to_id.channel_id != 0 && !isMegagroup()) {
2015-09-24 22:52:02 +02:00
messageText = LocaleController.getString("ActionChannelRemovedPhoto", R.string.ActionChannelRemovedPhoto);
2013-12-20 20:25:49 +01:00
} else {
2015-09-24 22:52:02 +02:00
if (isOut()) {
messageText = LocaleController.getString("ActionYouRemovedPhoto", R.string.ActionYouRemovedPhoto);
2013-12-20 20:25:49 +01:00
} else {
2015-09-24 22:52:02 +02:00
if (fromUser != null) {
messageText = replaceWithLink(LocaleController.getString("ActionRemovedPhoto", R.string.ActionRemovedPhoto), "un1", fromUser);
} else {
messageText = LocaleController.getString("ActionRemovedPhoto", R.string.ActionRemovedPhoto).replace("un1", "");
}
2013-12-20 20:25:49 +01:00
}
}
2013-10-25 17:19:00 +02:00
} else if (message.action instanceof TLRPC.TL_messageActionTTLChange) {
if (message.action.ttl != 0) {
2015-03-26 18:34:47 +01:00
if (isOut()) {
2014-10-14 22:36:15 +02:00
messageText = LocaleController.formatString("MessageLifetimeChangedOutgoing", R.string.MessageLifetimeChangedOutgoing, AndroidUtilities.formatTTLString(message.action.ttl));
2013-10-25 17:19:00 +02:00
} else {
2013-12-20 20:25:49 +01:00
if (fromUser != null) {
2015-06-29 19:12:11 +02:00
messageText = LocaleController.formatString("MessageLifetimeChanged", R.string.MessageLifetimeChanged, UserObject.getFirstName(fromUser), AndroidUtilities.formatTTLString(message.action.ttl));
2013-12-20 20:25:49 +01:00
} else {
2014-10-14 22:36:15 +02:00
messageText = LocaleController.formatString("MessageLifetimeChanged", R.string.MessageLifetimeChanged, "", AndroidUtilities.formatTTLString(message.action.ttl));
2013-12-20 20:25:49 +01:00
}
2013-10-25 17:19:00 +02:00
}
} else {
2015-03-26 18:34:47 +01:00
if (isOut()) {
messageText = LocaleController.getString("MessageLifetimeYouRemoved", R.string.MessageLifetimeYouRemoved);
2013-10-25 17:19:00 +02:00
} else {
2013-12-20 20:25:49 +01:00
if (fromUser != null) {
2015-06-29 19:12:11 +02:00
messageText = LocaleController.formatString("MessageLifetimeRemoved", R.string.MessageLifetimeRemoved, UserObject.getFirstName(fromUser));
2013-12-20 20:25:49 +01:00
} else {
messageText = LocaleController.formatString("MessageLifetimeRemoved", R.string.MessageLifetimeRemoved, "");
2013-12-20 20:25:49 +01:00
}
2013-10-25 17:19:00 +02:00
}
}
2013-12-20 20:25:49 +01:00
} else if (message.action instanceof TLRPC.TL_messageActionLoginUnknownLocation) {
2015-11-26 22:04:02 +01:00
String date;
long time = ((long) message.date) * 1000;
if (LocaleController.getInstance().formatterDay != null && LocaleController.getInstance().formatterYear != null) {
date = LocaleController.formatString("formatDateAtTime", R.string.formatDateAtTime, LocaleController.getInstance().formatterYear.format(time), LocaleController.getInstance().formatterDay.format(time));
} else {
date = "" + message.date;
}
TLRPC.User to_user = UserConfig.getCurrentUser();
if (to_user == null) {
if (users != null) {
to_user = users.get(messageOwner.to_id.user_id);
}
if (to_user == null) {
to_user = MessagesController.getInstance().getUser(messageOwner.to_id.user_id);
}
}
2015-06-29 19:12:11 +02:00
String name = to_user != null ? UserObject.getFirstName(to_user) : "";
messageText = LocaleController.formatString("NotificationUnrecognizedDevice", R.string.NotificationUnrecognizedDevice, name, date, message.action.title, message.action.address);
2013-12-20 20:25:49 +01:00
} else if (message.action instanceof TLRPC.TL_messageActionUserJoined) {
if (fromUser != null) {
2015-06-29 19:12:11 +02:00
messageText = LocaleController.formatString("NotificationContactJoined", R.string.NotificationContactJoined, UserObject.getUserName(fromUser));
2013-12-20 20:25:49 +01:00
} else {
messageText = LocaleController.formatString("NotificationContactJoined", R.string.NotificationContactJoined, "");
2013-12-20 20:25:49 +01:00
}
} else if (message.action instanceof TLRPC.TL_messageActionUserUpdatedPhoto) {
if (fromUser != null) {
2015-06-29 19:12:11 +02:00
messageText = LocaleController.formatString("NotificationContactNewPhoto", R.string.NotificationContactNewPhoto, UserObject.getUserName(fromUser));
2013-12-20 20:25:49 +01:00
} else {
messageText = LocaleController.formatString("NotificationContactNewPhoto", R.string.NotificationContactNewPhoto, "");
2013-12-20 20:25:49 +01:00
}
2014-10-22 22:01:07 +02:00
} else if (message.action instanceof TLRPC.TL_messageEncryptedAction) {
if (message.action.encryptedAction instanceof TLRPC.TL_decryptedMessageActionScreenshotMessages) {
2015-03-26 18:34:47 +01:00
if (isOut()) {
messageText = LocaleController.formatString("ActionTakeScreenshootYou", R.string.ActionTakeScreenshootYou);
} else {
if (fromUser != null) {
2014-10-15 20:43:52 +02:00
messageText = replaceWithLink(LocaleController.getString("ActionTakeScreenshoot", R.string.ActionTakeScreenshoot), "un1", fromUser);
} else {
messageText = LocaleController.formatString("ActionTakeScreenshoot", R.string.ActionTakeScreenshoot).replace("un1", "");
}
}
2014-10-22 22:01:07 +02:00
} else if (message.action.encryptedAction instanceof TLRPC.TL_decryptedMessageActionSetMessageTTL) {
TLRPC.TL_decryptedMessageActionSetMessageTTL action = (TLRPC.TL_decryptedMessageActionSetMessageTTL) message.action.encryptedAction;
if (action.ttl_seconds != 0) {
2015-03-26 18:34:47 +01:00
if (isOut()) {
2014-10-22 22:01:07 +02:00
messageText = LocaleController.formatString("MessageLifetimeChangedOutgoing", R.string.MessageLifetimeChangedOutgoing, AndroidUtilities.formatTTLString(action.ttl_seconds));
} else {
if (fromUser != null) {
2015-06-29 19:12:11 +02:00
messageText = LocaleController.formatString("MessageLifetimeChanged", R.string.MessageLifetimeChanged, UserObject.getFirstName(fromUser), AndroidUtilities.formatTTLString(action.ttl_seconds));
2014-10-22 22:01:07 +02:00
} else {
messageText = LocaleController.formatString("MessageLifetimeChanged", R.string.MessageLifetimeChanged, "", AndroidUtilities.formatTTLString(action.ttl_seconds));
}
}
} else {
2015-03-26 18:34:47 +01:00
if (isOut()) {
2014-10-22 22:01:07 +02:00
messageText = LocaleController.getString("MessageLifetimeYouRemoved", R.string.MessageLifetimeYouRemoved);
} else {
if (fromUser != null) {
2015-06-29 19:12:11 +02:00
messageText = LocaleController.formatString("MessageLifetimeRemoved", R.string.MessageLifetimeRemoved, UserObject.getFirstName(fromUser));
2014-10-22 22:01:07 +02:00
} else {
messageText = LocaleController.formatString("MessageLifetimeRemoved", R.string.MessageLifetimeRemoved, "");
}
}
}
}
} else if (message.action instanceof TLRPC.TL_messageActionCreatedBroadcastList) {
messageText = LocaleController.formatString("YouCreatedBroadcastList", R.string.YouCreatedBroadcastList);
2015-09-24 22:52:02 +02:00
} else if (message.action instanceof TLRPC.TL_messageActionChannelCreate) {
2015-11-26 22:04:02 +01:00
if (isMegagroup()) {
messageText = LocaleController.getString("ActionCreateMega", R.string.ActionCreateMega);
} else {
messageText = LocaleController.getString("ActionCreateChannel", R.string.ActionCreateChannel);
}
} else if (message.action instanceof TLRPC.TL_messageActionChatMigrateTo) {
messageText = LocaleController.getString("ActionMigrateFromGroup", R.string.ActionMigrateFromGroup);
} else if (message.action instanceof TLRPC.TL_messageActionChannelMigrateFrom) {
messageText = LocaleController.getString("ActionMigrateFromGroup", R.string.ActionMigrateFromGroup);
2013-10-25 17:19:00 +02:00
}
}
2015-04-09 20:00:14 +02:00
} else if (!isMediaEmpty()) {
2013-10-25 17:19:00 +02:00
if (message.media instanceof TLRPC.TL_messageMediaPhoto) {
messageText = LocaleController.getString("AttachPhoto", R.string.AttachPhoto);
2013-10-25 17:19:00 +02:00
} else if (message.media instanceof TLRPC.TL_messageMediaVideo) {
messageText = LocaleController.getString("AttachVideo", R.string.AttachVideo);
} else if (message.media instanceof TLRPC.TL_messageMediaGeo || message.media instanceof TLRPC.TL_messageMediaVenue) {
messageText = LocaleController.getString("AttachLocation", R.string.AttachLocation);
2013-10-25 17:19:00 +02:00
} else if (message.media instanceof TLRPC.TL_messageMediaContact) {
messageText = LocaleController.getString("AttachContact", R.string.AttachContact);
2013-12-20 20:25:49 +01:00
} else if (message.media instanceof TLRPC.TL_messageMediaUnsupported) {
messageText = LocaleController.getString("UnsupportedMedia", R.string.UnsupportedMedia);
2013-12-26 12:43:37 +01:00
} else if (message.media instanceof TLRPC.TL_messageMediaDocument) {
2015-01-02 23:15:07 +01:00
if (isSticker()) {
String sch = getStrickerChar();
if (sch != null && sch.length() > 0) {
messageText = String.format("%s %s", sch, LocaleController.getString("AttachSticker", R.string.AttachSticker));
} else {
messageText = LocaleController.getString("AttachSticker", R.string.AttachSticker);
}
2015-07-22 20:56:37 +02:00
} else if (isMusic()) {
messageText = LocaleController.getString("AttachMusic", R.string.AttachMusic);
} else if (isGif()) {
messageText = LocaleController.getString("AttachGif", R.string.AttachGif);
2015-01-02 23:15:07 +01:00
} else {
String name = FileLoader.getDocumentFileName(message.media.document);
if (name != null && name.length() > 0) {
messageText = name;
} else {
messageText = LocaleController.getString("AttachDocument", R.string.AttachDocument);
}
2015-01-02 23:15:07 +01:00
}
2013-12-26 12:43:37 +01:00
} else if (message.media instanceof TLRPC.TL_messageMediaAudio) {
messageText = LocaleController.getString("AttachAudio", R.string.AttachAudio);
2013-10-25 17:19:00 +02:00
}
} else {
messageText = message.message;
}
2015-11-26 22:04:02 +01:00
if (messageText == null) {
messageText = "";
}
2015-07-22 20:56:37 +02:00
if (generateLayout) {
messageText = Emoji.replaceEmoji(messageText, textPaint.getFontMetricsInt(), AndroidUtilities.dp(20), false);
}
2013-10-25 17:19:00 +02:00
if (message instanceof TLRPC.TL_message || message instanceof TLRPC.TL_messageForwarded_old2) {
2015-04-09 20:00:14 +02:00
if (isMediaEmpty()) {
contentType = type = 0;
2015-11-26 22:04:02 +01:00
if (messageText == null || messageText.length() == 0) {
2015-05-21 23:27:27 +02:00
messageText = "Empty message";
}
2015-04-09 20:00:14 +02:00
} else if (message.media instanceof TLRPC.TL_messageMediaPhoto) {
contentType = type = 1;
} else if (message.media instanceof TLRPC.TL_messageMediaGeo || message.media instanceof TLRPC.TL_messageMediaVenue) {
contentType = 1;
type = 4;
2015-04-09 20:00:14 +02:00
} else if (message.media instanceof TLRPC.TL_messageMediaVideo) {
contentType = 1;
type = 3;
2015-04-09 20:00:14 +02:00
} else if (message.media instanceof TLRPC.TL_messageMediaContact) {
2014-10-15 20:43:52 +02:00
contentType = 3;
type = 12;
2015-04-09 20:00:14 +02:00
} else if (message.media instanceof TLRPC.TL_messageMediaUnsupported) {
contentType = type = 0;
2015-04-09 20:00:14 +02:00
} else if (message.media instanceof TLRPC.TL_messageMediaDocument) {
2014-08-29 23:06:04 +02:00
contentType = 1;
2015-01-02 23:15:07 +01:00
if (message.media.document.mime_type != null) {
if (isGifDocument(message.media.document)) {
2015-01-02 23:15:07 +01:00
type = 8;
} else if (message.media.document.mime_type.equals("image/webp") && isSticker()) {
type = 13;
2015-07-22 20:56:37 +02:00
} else if (isMusic()) {
type = 14;
contentType = 8;
2015-01-02 23:15:07 +01:00
} else {
type = 9;
}
2013-12-26 12:43:37 +01:00
} else {
2014-08-29 23:06:04 +02:00
type = 9;
2013-12-26 12:43:37 +01:00
}
2015-04-09 20:00:14 +02:00
} else if (message.media instanceof TLRPC.TL_messageMediaAudio) {
contentType = type = 2;
2013-10-25 17:19:00 +02:00
}
} else if (message instanceof TLRPC.TL_messageService) {
if (message.action instanceof TLRPC.TL_messageActionLoginUnknownLocation) {
contentType = type = 0;
} else if (message.action instanceof TLRPC.TL_messageActionChatEditPhoto || message.action instanceof TLRPC.TL_messageActionUserUpdatedPhoto) {
2014-10-15 20:43:52 +02:00
contentType = 4;
2014-08-29 23:06:04 +02:00
type = 11;
2014-10-22 22:01:07 +02:00
} else if (message.action instanceof TLRPC.TL_messageEncryptedAction) {
if (message.action.encryptedAction instanceof TLRPC.TL_decryptedMessageActionScreenshotMessages || message.action.encryptedAction instanceof TLRPC.TL_decryptedMessageActionSetMessageTTL) {
contentType = 4;
type = 10;
} else {
contentType = -1;
type = -1;
}
2013-10-25 17:19:00 +02:00
} else {
2014-10-15 20:43:52 +02:00
contentType = 4;
2014-08-29 23:06:04 +02:00
type = 10;
2013-10-25 17:19:00 +02:00
}
}
Calendar rightNow = new GregorianCalendar();
rightNow.setTimeInMillis((long) (messageOwner.date) * 1000);
2013-10-25 17:19:00 +02:00
int dateDay = rightNow.get(Calendar.DAY_OF_YEAR);
int dateYear = rightNow.get(Calendar.YEAR);
int dateMonth = rightNow.get(Calendar.MONTH);
dateKey = String.format("%d_%02d_%02d", dateYear, dateMonth, dateDay);
2015-10-29 18:10:07 +01:00
if (contentType == 1 || contentType == 2 || contentType == 0 || contentType == 8) {
monthKey = String.format("%d_%02d", dateYear, dateMonth);
2015-11-26 22:04:02 +01:00
} else if (contentType == 9) {
//dateKey = "0_0_0";
}
2015-05-21 23:27:27 +02:00
if (messageOwner.message != null && messageOwner.id < 0 && messageOwner.message.length() > 6 && messageOwner.media instanceof TLRPC.TL_messageMediaVideo) {
videoEditedInfo = new VideoEditedInfo();
videoEditedInfo.parseString(messageOwner.message);
}
generateCaption();
2015-02-01 19:51:02 +01:00
if (generateLayout) {
2014-11-21 20:36:21 +01:00
generateLayout();
}
2015-02-01 19:51:02 +01:00
generateThumbs(false);
2014-10-15 20:43:52 +02:00
}
public static boolean isGifDocument(TLRPC.Document document) {
return document != null && document.thumb != null && document.mime_type != null && (document.mime_type.equals("image/gif") || isNewGifDocument(document));
}
public static boolean isNewGifDocument(TLRPC.Document document) {
if (document != null && document.mime_type != null && document.mime_type.equals("video/mp4")) {
for (int a = 0; a < document.attributes.size(); a++) {
if (document.attributes.get(a) instanceof TLRPC.TL_documentAttributeAnimated) {
return true;
}
}
}
return false;
}
2015-02-01 19:51:02 +01:00
public void generateThumbs(boolean update) {
2014-08-06 01:17:40 +02:00
if (messageOwner instanceof TLRPC.TL_messageService) {
if (messageOwner.action instanceof TLRPC.TL_messageActionChatEditPhoto) {
if (!update) {
2015-02-01 19:51:02 +01:00
photoThumbs = new ArrayList<>(messageOwner.action.photo.sizes);
2014-08-06 01:17:40 +02:00
} else if (photoThumbs != null && !photoThumbs.isEmpty()) {
for (int a = 0; a < photoThumbs.size(); a++) {
TLRPC.PhotoSize photoObject = photoThumbs.get(a);
for (int b = 0; b < messageOwner.action.photo.sizes.size(); b++) {
TLRPC.PhotoSize size = messageOwner.action.photo.sizes.get(b);
2014-08-06 01:17:40 +02:00
if (size instanceof TLRPC.TL_photoSizeEmpty) {
continue;
}
2015-02-01 19:51:02 +01:00
if (size.type.equals(photoObject.type)) {
photoObject.location = size.location;
2014-08-06 01:17:40 +02:00
break;
}
}
}
}
}
} else if (messageOwner.media != null && !(messageOwner.media instanceof TLRPC.TL_messageMediaEmpty)) {
if (messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
if (!update || photoThumbs != null && photoThumbs.size() != messageOwner.media.photo.sizes.size()) {
2015-02-01 19:51:02 +01:00
photoThumbs = new ArrayList<>(messageOwner.media.photo.sizes);
2014-08-06 01:17:40 +02:00
} else if (photoThumbs != null && !photoThumbs.isEmpty()) {
for (int a = 0; a < photoThumbs.size(); a++) {
TLRPC.PhotoSize photoObject = photoThumbs.get(a);
for (int b = 0; b < messageOwner.media.photo.sizes.size(); b++) {
TLRPC.PhotoSize size = messageOwner.media.photo.sizes.get(b);
2014-08-06 01:17:40 +02:00
if (size instanceof TLRPC.TL_photoSizeEmpty) {
continue;
}
2015-02-01 19:51:02 +01:00
if (size.type.equals(photoObject.type)) {
photoObject.location = size.location;
2014-08-06 01:17:40 +02:00
break;
}
}
}
}
} else if (messageOwner.media instanceof TLRPC.TL_messageMediaVideo) {
if (!update) {
2015-01-02 23:15:07 +01:00
photoThumbs = new ArrayList<>();
2015-02-01 19:51:02 +01:00
photoThumbs.add(messageOwner.media.video.thumb);
2014-08-06 01:17:40 +02:00
} else if (photoThumbs != null && !photoThumbs.isEmpty() && messageOwner.media.video.thumb != null) {
2015-02-01 19:51:02 +01:00
TLRPC.PhotoSize photoObject = photoThumbs.get(0);
photoObject.location = messageOwner.media.video.thumb.location;
2014-08-06 01:17:40 +02:00
}
2015-01-02 23:15:07 +01:00
} else if (messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
2014-08-06 01:17:40 +02:00
if (!(messageOwner.media.document.thumb instanceof TLRPC.TL_photoSizeEmpty)) {
if (!update) {
2015-01-02 23:15:07 +01:00
photoThumbs = new ArrayList<>();
2015-02-01 19:51:02 +01:00
photoThumbs.add(messageOwner.media.document.thumb);
2014-08-06 01:17:40 +02:00
} else if (photoThumbs != null && !photoThumbs.isEmpty() && messageOwner.media.document.thumb != null) {
2015-02-01 19:51:02 +01:00
TLRPC.PhotoSize photoObject = photoThumbs.get(0);
photoObject.location = messageOwner.media.document.thumb.location;
2014-08-06 01:17:40 +02:00
}
}
2015-04-09 20:00:14 +02:00
} else if (messageOwner.media instanceof TLRPC.TL_messageMediaWebPage) {
if (messageOwner.media.webpage.photo != null) {
if (!update || photoThumbs == null) {
photoThumbs = new ArrayList<>(messageOwner.media.webpage.photo.sizes);
2015-05-21 23:27:27 +02:00
} else if (!photoThumbs.isEmpty()) {
for (int a = 0; a < photoThumbs.size(); a++) {
TLRPC.PhotoSize photoObject = photoThumbs.get(a);
for (int b = 0; b < messageOwner.media.webpage.photo.sizes.size(); b++) {
TLRPC.PhotoSize size = messageOwner.media.webpage.photo.sizes.get(b);
2015-04-09 20:00:14 +02:00
if (size instanceof TLRPC.TL_photoSizeEmpty) {
continue;
}
if (size.type.equals(photoObject.type)) {
photoObject.location = size.location;
break;
}
}
}
}
}
2014-08-06 01:17:40 +02:00
}
}
2013-10-25 17:19:00 +02:00
}
2013-12-26 12:43:37 +01:00
2015-11-26 22:04:02 +01:00
public CharSequence replaceWithLink(CharSequence source, String param, ArrayList<Integer> uids, AbstractMap<Integer, TLRPC.User> usersDict) {
2015-02-01 19:51:02 +01:00
int start = TextUtils.indexOf(source, param);
2015-09-24 22:52:02 +02:00
if (start >= 0) {
2015-11-26 22:04:02 +01:00
SpannableStringBuilder names = new SpannableStringBuilder("");
for (int a = 0; a < uids.size(); a++) {
TLRPC.User user = null;
if (usersDict != null) {
user = usersDict.get(uids.get(a));
}
if (user == null) {
user = MessagesController.getInstance().getUser(uids.get(a));
}
if (user != null) {
String name = UserObject.getUserName(user);
start = names.length();
if (names.length() != 0) {
names.append(", ");
}
names.append(name);
names.setSpan(new URLSpanNoUnderlineBold("" + user.id), start, start + name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return TextUtils.replace(source, new String[]{param}, new CharSequence[]{names});
}
return source;
}
public CharSequence replaceWithLink(CharSequence source, String param, TLObject object) {
int start = TextUtils.indexOf(source, param);
if (start >= 0) {
String name;
int id;
if (object instanceof TLRPC.User) {
name = UserObject.getUserName((TLRPC.User) object);
id = ((TLRPC.User) object).id;
} else if (object instanceof TLRPC.Chat) {
name = ((TLRPC.Chat) object).title;
id = -((TLRPC.Chat) object).id;
} else {
name = "";
id = 0;
}
2015-09-24 22:52:02 +02:00
SpannableStringBuilder builder = new SpannableStringBuilder(TextUtils.replace(source, new String[]{param}, new String[]{name}));
2015-11-26 22:04:02 +01:00
builder.setSpan(new URLSpanNoUnderlineBold("" + id), start, start + name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2015-09-24 22:52:02 +02:00
return builder;
}
return source;
2015-02-01 19:51:02 +01:00
}
2015-10-29 18:10:07 +01:00
public String getExtension() {
String fileName = getFileName();
int idx = fileName.lastIndexOf(".");
String ext = null;
if (idx != -1) {
ext = fileName.substring(idx + 1);
}
if (ext == null || ext.length() == 0) {
ext = messageOwner.media.document.mime_type;
}
if (ext == null) {
ext = "";
}
ext = ext.toUpperCase();
return ext;
}
2013-12-26 12:43:37 +01:00
public String getFileName() {
if (messageOwner.media instanceof TLRPC.TL_messageMediaVideo) {
return FileLoader.getAttachFileName(messageOwner.media.video);
2013-12-26 12:43:37 +01:00
} else if (messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
return FileLoader.getAttachFileName(messageOwner.media.document);
} else if (messageOwner.media instanceof TLRPC.TL_messageMediaAudio) {
return FileLoader.getAttachFileName(messageOwner.media.audio);
} else if (messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
ArrayList<TLRPC.PhotoSize> sizes = messageOwner.media.photo.sizes;
if (sizes.size() > 0) {
2014-10-01 21:55:24 +02:00
TLRPC.PhotoSize sizeFull = FileLoader.getClosestPhotoSizeWithSize(sizes, AndroidUtilities.getPhotoSize());
if (sizeFull != null) {
return FileLoader.getAttachFileName(sizeFull);
}
}
2013-12-26 12:43:37 +01:00
}
return "";
}
2015-02-01 19:51:02 +01:00
public int getFileType() {
if (messageOwner.media instanceof TLRPC.TL_messageMediaVideo) {
return FileLoader.MEDIA_DIR_VIDEO;
} else if (messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
return FileLoader.MEDIA_DIR_DOCUMENT;
} else if (messageOwner.media instanceof TLRPC.TL_messageMediaAudio) {
return FileLoader.MEDIA_DIR_AUDIO;
} else if (messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
return FileLoader.MEDIA_DIR_IMAGE;
}
return FileLoader.MEDIA_DIR_CACHE;
}
2015-06-29 19:12:11 +02:00
private static boolean containsUrls(CharSequence message) {
if (message == null || message.length() < 2 || message.length() > 1024 * 20) {
return false;
}
int length = message.length();
int digitsInRow = 0;
int schemeSequence = 0;
int dotSequence = 0;
char lastChar = 0;
for (int i = 0; i < length; i++) {
char c = message.charAt(i);
if (c >= '0' && c <= '9') {
digitsInRow++;
if (digitsInRow >= 6) {
return true;
}
schemeSequence = 0;
dotSequence = 0;
2015-01-02 23:15:07 +01:00
} else if (!(c != ' ' && digitsInRow > 0)) {
digitsInRow = 0;
}
2015-06-29 19:12:11 +02:00
if ((c == '@' || c == '#' || c == '/') && i == 0 || i != 0 && (message.charAt(i - 1) == ' ' || message.charAt(i - 1) == '\n')) {
return true;
}
2015-01-02 23:15:07 +01:00
if (c == ':') {
if (schemeSequence == 0) {
schemeSequence = 1;
} else {
schemeSequence = 0;
}
} else if (c == '/') {
if (schemeSequence == 2) {
return true;
}
if (schemeSequence == 1) {
schemeSequence++;
} else {
schemeSequence = 0;
}
} else if (c == '.') {
if (dotSequence == 0 && lastChar != ' ') {
dotSequence++;
} else {
dotSequence = 0;
}
} else if (c != ' ' && lastChar == '.' && dotSequence == 1) {
return true;
2015-01-02 23:15:07 +01:00
} else {
dotSequence = 0;
}
lastChar = c;
}
return false;
}
2015-04-09 20:00:14 +02:00
public void generateLinkDescription() {
if (linkDescription != null) {
return;
}
if (messageOwner.media instanceof TLRPC.TL_messageMediaWebPage && messageOwner.media.webpage instanceof TLRPC.TL_webPage && messageOwner.media.webpage.description != null) {
linkDescription = Spannable.Factory.getInstance().newSpannable(messageOwner.media.webpage.description);
if (containsUrls(linkDescription)) {
2015-10-29 18:10:07 +01:00
try {
Linkify.addLinks((Spannable) linkDescription, Linkify.WEB_URLS);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
2015-04-09 20:00:14 +02:00
}
2015-11-26 22:04:02 +01:00
linkDescription = Emoji.replaceEmoji(linkDescription, textPaint.getFontMetricsInt(), AndroidUtilities.dp(20), false);
2015-04-09 20:00:14 +02:00
}
}
public void generateCaption() {
if (caption != null) {
return;
}
if (messageOwner.media != null && messageOwner.media.caption != null && messageOwner.media.caption.length() > 0) {
2015-07-22 20:56:37 +02:00
caption = Emoji.replaceEmoji(messageOwner.media.caption, textPaint.getFontMetricsInt(), AndroidUtilities.dp(20), false);
if (containsUrls(caption)) {
try {
Linkify.addLinks((Spannable) caption, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
2015-11-26 22:04:02 +01:00
addUsernamesAndHashtags(caption, true);
}
}
}
2015-11-26 22:04:02 +01:00
private static void addUsernamesAndHashtags(CharSequence charSequence, boolean botCommands) {
try {
2015-06-29 19:12:11 +02:00
if (urlPattern == null) {
urlPattern = Pattern.compile("(^|\\s)/[a-zA-Z@\\d_]{1,255}|(^|\\s)@[a-zA-Z\\d_]{3,32}|(^|\\s)#[\\w\\.]+");
2015-06-29 19:12:11 +02:00
}
Matcher matcher = urlPattern.matcher(charSequence);
while (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
2015-06-29 19:12:11 +02:00
if (charSequence.charAt(start) != '@' && charSequence.charAt(start) != '#' && charSequence.charAt(start) != '/') {
start++;
}
2015-11-26 22:04:02 +01:00
URLSpanNoUnderline url = null;
2015-10-29 18:10:07 +01:00
if (charSequence.charAt(start) == '/') {
2015-11-26 22:04:02 +01:00
if (botCommands) {
url = new URLSpanBotCommand(charSequence.subSequence(start, end).toString());
}
2015-10-29 18:10:07 +01:00
} else {
url = new URLSpanNoUnderline(charSequence.subSequence(start, end).toString());
}
2015-11-26 22:04:02 +01:00
if (url != null) {
((Spannable) charSequence).setSpan(url, start, end, 0);
}
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
2015-11-26 22:04:02 +01:00
2015-06-29 19:12:11 +02:00
public static void addLinks(CharSequence messageText) {
2015-11-26 22:04:02 +01:00
addLinks(messageText, true);
}
public static void addLinks(CharSequence messageText, boolean botCommands) {
2015-01-02 23:15:07 +01:00
if (messageText instanceof Spannable && containsUrls(messageText)) {
if (messageText.length() < 200) {
try {
Linkify.addLinks((Spannable) messageText, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
} else {
try {
Linkify.addLinks((Spannable) messageText, Linkify.WEB_URLS);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
2015-11-26 22:04:02 +01:00
addUsernamesAndHashtags(messageText, botCommands);
}
2015-06-29 19:12:11 +02:00
}
private void generateLayout() {
if (type != 0 || messageOwner.to_id == null || messageText == null || messageText.length() == 0) {
return;
}
generateLinkDescription();
textLayoutBlocks = new ArrayList<>();
2015-09-02 00:14:21 +02:00
boolean useManualParse = messageOwner.entities.isEmpty() && (
messageOwner instanceof TLRPC.TL_message_old ||
messageOwner instanceof TLRPC.TL_message_old2 ||
messageOwner instanceof TLRPC.TL_message_old3 ||
messageOwner instanceof TLRPC.TL_message_old4 ||
messageOwner instanceof TLRPC.TL_messageForwarded_old ||
messageOwner instanceof TLRPC.TL_messageForwarded_old2 ||
messageOwner instanceof TLRPC.TL_message_secret ||
isOut() && messageOwner.send_state != MESSAGE_SEND_STATE_SENT ||
messageOwner.id < 0 || messageOwner.media instanceof TLRPC.TL_messageMediaUnsupported);
2015-09-02 00:14:21 +02:00
if (useManualParse) {
addLinks(messageText);
2015-09-24 22:52:02 +02:00
} else {
if (messageText instanceof Spannable && messageText.length() < 200) {
2015-09-24 22:52:02 +02:00
try {
Linkify.addLinks((Spannable) messageText, Linkify.PHONE_NUMBERS);
} catch (Throwable e) {
FileLog.e("tmessages", e);
}
}
2015-09-02 00:14:21 +02:00
}
if (messageText instanceof Spannable) {
Spannable spannable = (Spannable) messageText;
int count = messageOwner.entities.size();
for (int a = 0; a < count; a++) {
TLRPC.MessageEntity entity = messageOwner.entities.get(a);
if (entity.length <= 0 || entity.offset < 0 || entity.offset >= messageOwner.message.length()) {
continue;
} else if (entity.offset + entity.length > messageOwner.message.length()) {
entity.length = messageOwner.message.length() - entity.offset;
}
if (entity instanceof TLRPC.TL_messageEntityBold) {
spannable.setSpan(new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf")), entity.offset, entity.offset + entity.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (entity instanceof TLRPC.TL_messageEntityItalic) {
spannable.setSpan(new TypefaceSpan(AndroidUtilities.getTypeface("fonts/ritalic.ttf")), entity.offset, entity.offset + entity.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2015-11-26 22:04:02 +01:00
} else if (entity instanceof TLRPC.TL_messageEntityCode || entity instanceof TLRPC.TL_messageEntityPre) {
spannable.setSpan(new TypefaceSpan(Typeface.MONOSPACE, AndroidUtilities.dp(MessagesController.getInstance().fontSize - 1)), entity.offset, entity.offset + entity.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2015-09-02 00:14:21 +02:00
} else if (!useManualParse) {
String url = messageOwner.message.substring(entity.offset, entity.offset + entity.length);
2015-10-29 18:10:07 +01:00
if (entity instanceof TLRPC.TL_messageEntityBotCommand) {
spannable.setSpan(new URLSpanBotCommand(url), entity.offset, entity.offset + entity.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (entity instanceof TLRPC.TL_messageEntityHashtag || entity instanceof TLRPC.TL_messageEntityMention) {
2015-09-02 00:14:21 +02:00
spannable.setSpan(new URLSpanNoUnderline(url), entity.offset, entity.offset + entity.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (entity instanceof TLRPC.TL_messageEntityEmail) {
spannable.setSpan(new URLSpanReplacement("mailto:" + url), entity.offset, entity.offset + entity.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (entity instanceof TLRPC.TL_messageEntityUrl) {
if (!url.toLowerCase().startsWith("http")) {
spannable.setSpan(new URLSpan("http://" + url), entity.offset, entity.offset + entity.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
spannable.setSpan(new URLSpan(url), entity.offset, entity.offset + entity.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} else if (entity instanceof TLRPC.TL_messageEntityTextUrl) {
spannable.setSpan(new URLSpanReplacement(entity.url), entity.offset, entity.offset + entity.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
}
int maxWidth;
if (AndroidUtilities.isTablet()) {
2015-09-24 22:52:02 +02:00
if (messageOwner.from_id > 0 && (messageOwner.to_id.channel_id != 0 || messageOwner.to_id.chat_id != 0) && !isOut()) {
maxWidth = AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(122);
} else {
maxWidth = AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(80);
}
} else {
2015-09-24 22:52:02 +02:00
if (messageOwner.from_id > 0 && (messageOwner.to_id.channel_id != 0 || messageOwner.to_id.chat_id != 0) && !isOut()) {
maxWidth = Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) - AndroidUtilities.dp(122);
} else {
maxWidth = Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) - AndroidUtilities.dp(80);
}
}
2015-05-21 23:27:27 +02:00
StaticLayout textLayout;
2014-03-10 10:27:49 +01:00
try {
textLayout = new StaticLayout(messageText, textPaint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
} catch (Exception e) {
FileLog.e("tmessages", e);
return;
}
textHeight = textLayout.getHeight();
int linesCount = textLayout.getLineCount();
int blocksCount = (int) Math.ceil((float) linesCount / LINES_PER_BLOCK);
int linesOffset = 0;
2014-05-18 23:44:30 +02:00
float prevOffset = 0;
for (int a = 0; a < blocksCount; a++) {
int currentBlockLinesCount = Math.min(LINES_PER_BLOCK, linesCount - linesOffset);
TextLayoutBlock block = new TextLayoutBlock();
if (blocksCount == 1) {
block.textLayout = textLayout;
block.textYOffset = 0;
block.charactersOffset = 0;
blockHeight = textHeight;
} else {
int startCharacter = textLayout.getLineStart(linesOffset);
int endCharacter = textLayout.getLineEnd(linesOffset + currentBlockLinesCount - 1);
if (endCharacter < startCharacter) {
continue;
}
block.charactersOffset = startCharacter;
2014-03-10 10:27:49 +01:00
try {
CharSequence str = messageText.subSequence(startCharacter, endCharacter);
block.textLayout = new StaticLayout(str, textPaint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
block.textYOffset = textLayout.getLineTop(linesOffset);
if (a != 0) {
blockHeight = Math.min(blockHeight, (int) (block.textYOffset - prevOffset));
}
prevOffset = block.textYOffset;
/*if (a != blocksCount - 1) {
int height = block.textLayout.getHeight();
2014-03-10 10:27:49 +01:00
blockHeight = Math.min(blockHeight, block.textLayout.getHeight());
2014-05-18 23:44:30 +02:00
prevOffset = block.textYOffset;
} else {
blockHeight = Math.min(blockHeight, (int)(block.textYOffset - prevOffset));
}*/
2014-03-10 10:27:49 +01:00
} catch (Exception e) {
FileLog.e("tmessages", e);
continue;
}
2015-10-29 18:10:07 +01:00
if (a == blocksCount - 1) {
currentBlockLinesCount = Math.max(currentBlockLinesCount, block.textLayout.getLineCount());
try {
textHeight = Math.max(textHeight, (int) (block.textYOffset + block.textLayout.getHeight()));
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
}
textLayoutBlocks.add(block);
2014-03-10 10:27:49 +01:00
float lastLeft = block.textXOffset = 0;
try {
lastLeft = block.textXOffset = block.textLayout.getLineLeft(currentBlockLinesCount - 1);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
float lastLine = 0;
try {
lastLine = block.textLayout.getLineWidth(currentBlockLinesCount - 1);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
int linesMaxWidth = (int) Math.ceil(lastLine);
int lastLineWidthWithLeft;
int linesMaxWidthWithLeft;
boolean hasNonRTL = false;
if (a == blocksCount - 1) {
lastLineWidth = linesMaxWidth;
}
linesMaxWidthWithLeft = lastLineWidthWithLeft = (int) Math.ceil(lastLine + lastLeft);
if (lastLeft == 0) {
hasNonRTL = true;
}
if (currentBlockLinesCount > 1) {
float textRealMaxWidth = 0, textRealMaxWidthWithLeft = 0, lineWidth, lineLeft;
2015-10-29 18:10:07 +01:00
for (int n = 0; n < currentBlockLinesCount; n++) {
try {
lineWidth = block.textLayout.getLineWidth(n);
2014-03-10 10:27:49 +01:00
} catch (Exception e) {
FileLog.e("tmessages", e);
lineWidth = 0;
}
2014-07-11 23:47:50 +02:00
if (lineWidth > maxWidth + 100) {
lineWidth = maxWidth;
2014-07-11 23:47:50 +02:00
}
2014-03-10 10:27:49 +01:00
try {
lineLeft = block.textLayout.getLineLeft(n);
} catch (Exception e) {
FileLog.e("tmessages", e);
2014-03-10 10:27:49 +01:00
lineLeft = 0;
}
2014-03-10 10:27:49 +01:00
block.textXOffset = Math.min(block.textXOffset, lineLeft);
if (lineLeft == 0) {
hasNonRTL = true;
}
textRealMaxWidth = Math.max(textRealMaxWidth, lineWidth);
textRealMaxWidthWithLeft = Math.max(textRealMaxWidthWithLeft, lineWidth + lineLeft);
linesMaxWidth = Math.max(linesMaxWidth, (int) Math.ceil(lineWidth));
linesMaxWidthWithLeft = Math.max(linesMaxWidthWithLeft, (int) Math.ceil(lineWidth + lineLeft));
}
if (hasNonRTL) {
textRealMaxWidth = textRealMaxWidthWithLeft;
if (a == blocksCount - 1) {
lastLineWidth = lastLineWidthWithLeft;
}
} else if (a == blocksCount - 1) {
lastLineWidth = linesMaxWidth;
}
textWidth = Math.max(textWidth, (int) Math.ceil(textRealMaxWidth));
} else {
textWidth = Math.max(textWidth, Math.min(maxWidth, linesMaxWidth));
}
if (hasNonRTL) {
block.textXOffset = 0;
}
linesOffset += currentBlockLinesCount;
}
if (blockHeight == 0) {
blockHeight = 1;
}
}
public boolean isOut() {
2015-11-26 22:04:02 +01:00
return messageOwner.out;
}
2015-09-24 22:52:02 +02:00
public boolean isOutOwner() {
2015-11-26 22:04:02 +01:00
return messageOwner.out && messageOwner.from_id > 0;
2015-09-24 22:52:02 +02:00
}
public boolean isUnread() {
2015-11-26 22:04:02 +01:00
return messageOwner.unread;
}
public boolean isContentUnread() {
2015-11-26 22:04:02 +01:00
return messageOwner.media_unread;
}
public void setIsRead() {
2015-11-26 22:04:02 +01:00
messageOwner.unread = false;
}
public int getUnradFlags() {
return getUnreadFlags(messageOwner);
}
public static int getUnreadFlags(TLRPC.Message message) {
int flags = 0;
2015-11-26 22:04:02 +01:00
if (!message.unread) {
flags |= 1;
}
2015-11-26 22:04:02 +01:00
if (!message.media_unread) {
flags |= 2;
}
return flags;
}
public void setContentIsRead() {
2015-11-26 22:04:02 +01:00
messageOwner.media_unread = false;
}
public int getId() {
return messageOwner.id;
}
2014-10-21 22:35:16 +02:00
public boolean isSecretPhoto() {
2015-11-26 22:04:02 +01:00
return messageOwner instanceof TLRPC.TL_message_secret && messageOwner.media instanceof TLRPC.TL_messageMediaPhoto && messageOwner.ttl > 0 && messageOwner.ttl <= 60;
}
2014-10-21 22:35:16 +02:00
public boolean isSecretMedia() {
return messageOwner instanceof TLRPC.TL_message_secret &&
2015-11-26 22:04:02 +01:00
(messageOwner.media instanceof TLRPC.TL_messageMediaPhoto && messageOwner.ttl > 0 && messageOwner.ttl <= 60 ||
messageOwner.media instanceof TLRPC.TL_messageMediaAudio ||
messageOwner.media instanceof TLRPC.TL_messageMediaVideo);
2014-10-21 22:35:16 +02:00
}
public static void setUnreadFlags(TLRPC.Message message, int flag) {
2015-11-26 22:04:02 +01:00
message.unread = (flag & 1) == 0;
message.media_unread = (flag & 2) == 0;
}
public static boolean isUnread(TLRPC.Message message) {
2015-11-26 22:04:02 +01:00
return message.unread;
}
public static boolean isContentUnread(TLRPC.Message message) {
2015-11-26 22:04:02 +01:00
return message.media_unread;
}
2015-09-24 22:52:02 +02:00
public boolean isImportant() {
return isImportant(messageOwner);
}
2015-11-26 22:04:02 +01:00
public boolean isMegagroup() {
return isMegagroup(messageOwner);
}
2015-09-24 22:52:02 +02:00
public static boolean isImportant(TLRPC.Message message) {
2015-11-26 22:04:02 +01:00
if (isMegagroup(message)) {
return message.from_id <= 0;
}
return message.to_id.channel_id != 0 && (message.from_id <= 0 || message.mentioned || message.out || (message.flags & TLRPC.MESSAGE_FLAG_HAS_FROM_ID) == 0);
}
public static boolean isMegagroup(TLRPC.Message message) {
return (message.flags & TLRPC.MESSAGE_FLAG_MEGAGROUP) != 0;
2015-09-24 22:52:02 +02:00
}
public static boolean isOut(TLRPC.Message message) {
2015-11-26 22:04:02 +01:00
return message.out;
2014-07-10 02:15:58 +02:00
}
public long getDialogId() {
2015-11-26 22:04:02 +01:00
return getDialogId(messageOwner);
}
public static long getDialogId(TLRPC.Message message) {
if (message.dialog_id == 0 && message.to_id != null) {
if (message.to_id.chat_id != 0) {
if (message.to_id.chat_id < 0) {
message.dialog_id = AndroidUtilities.makeBroadcastId(message.to_id.chat_id);
2015-09-24 22:52:02 +02:00
} else {
2015-11-26 22:04:02 +01:00
message.dialog_id = -message.to_id.chat_id;
2015-09-24 22:52:02 +02:00
}
2015-11-26 22:04:02 +01:00
} else if (message.to_id.channel_id != 0) {
message.dialog_id = -message.to_id.channel_id;
} else if (isOut(message)) {
message.dialog_id = message.to_id.user_id;
} else {
2015-11-26 22:04:02 +01:00
message.dialog_id = message.from_id;
}
}
2015-11-26 22:04:02 +01:00
return message.dialog_id;
}
2014-09-28 15:37:26 +02:00
public boolean isSending() {
2015-04-09 20:00:14 +02:00
return messageOwner.send_state == MESSAGE_SEND_STATE_SENDING && messageOwner.id < 0;
2014-09-28 15:37:26 +02:00
}
public boolean isSendError() {
2015-06-29 19:12:11 +02:00
return messageOwner.send_state == MESSAGE_SEND_STATE_SEND_ERROR && messageOwner.id < 0;
2014-09-28 15:37:26 +02:00
}
public boolean isSent() {
2015-04-09 20:00:14 +02:00
return messageOwner.send_state == MESSAGE_SEND_STATE_SENT || messageOwner.id > 0;
2014-09-28 15:37:26 +02:00
}
public String getSecretTimeString() {
if (!isSecretMedia()) {
return null;
}
int secondsLeft = messageOwner.ttl;
if (messageOwner.destroyTime != 0) {
secondsLeft = Math.max(0, messageOwner.destroyTime - ConnectionsManager.getInstance().getCurrentTime());
}
String str;
if (secondsLeft < 60) {
str = secondsLeft + "s";
} else {
2014-10-10 19:16:39 +02:00
str = secondsLeft / 60 + "m";
}
return str;
}
2015-01-02 23:15:07 +01:00
public String getDocumentName() {
if (messageOwner.media != null && messageOwner.media.document != null) {
return FileLoader.getDocumentFileName(messageOwner.media.document);
}
return "";
}
2015-02-01 19:51:02 +01:00
public static boolean isStickerMessage(TLRPC.Message message) {
if (message.media != null && message.media.document != null) {
for (TLRPC.DocumentAttribute attribute : message.media.document.attributes) {
2015-01-02 23:15:07 +01:00
if (attribute instanceof TLRPC.TL_documentAttributeSticker) {
return true;
}
}
}
return false;
}
2015-02-01 19:51:02 +01:00
2015-07-22 20:56:37 +02:00
public static boolean isMusicMessage(TLRPC.Message message) {
if (message.media != null && message.media.document != null) {
for (TLRPC.DocumentAttribute attribute : message.media.document.attributes) {
if (attribute instanceof TLRPC.TL_documentAttributeAudio) {
return true;
}
}
}
return false;
}
2015-05-21 23:27:27 +02:00
public static TLRPC.InputStickerSet getInputStickerSet(TLRPC.Message message) {
if (message.media != null && message.media.document != null) {
for (TLRPC.DocumentAttribute attribute : message.media.document.attributes) {
if (attribute instanceof TLRPC.TL_documentAttributeSticker) {
if (attribute.stickerset instanceof TLRPC.TL_inputStickerSetEmpty) {
return null;
}
return attribute.stickerset;
}
}
}
return null;
}
public String getStrickerChar() {
if (messageOwner.media != null && messageOwner.media.document != null) {
for (TLRPC.DocumentAttribute attribute : messageOwner.media.document.attributes) {
if (attribute instanceof TLRPC.TL_documentAttributeSticker) {
return attribute.alt;
}
}
}
return null;
}
public int getApproximateHeight() {
if (type == 0) {
return textHeight;
} else if (contentType == 2) {
return AndroidUtilities.dp(68);
} else if (contentType == 3) {
return AndroidUtilities.dp(71);
} else if (type == 9) {
return AndroidUtilities.dp(100);
} else if (type == 4) {
return AndroidUtilities.dp(114);
2015-07-22 20:56:37 +02:00
} else if (type == 14) {
return AndroidUtilities.dp(78);
} else if (type == 13) {
float maxHeight = AndroidUtilities.displaySize.y * 0.4f;
float maxWidth;
if (AndroidUtilities.isTablet()) {
maxWidth = AndroidUtilities.getMinTabletSide() * 0.5f;
} else {
maxWidth = AndroidUtilities.displaySize.x * 0.5f;
}
int photoHeight = 0;
int photoWidth = 0;
for (TLRPC.DocumentAttribute attribute : messageOwner.media.document.attributes) {
if (attribute instanceof TLRPC.TL_documentAttributeImageSize) {
photoWidth = attribute.w;
photoHeight = attribute.h;
break;
}
}
if (photoWidth == 0) {
photoHeight = (int) maxHeight;
photoWidth = photoHeight + AndroidUtilities.dp(100);
}
if (photoHeight > maxHeight) {
photoWidth *= maxHeight / photoHeight;
photoHeight = (int)maxHeight;
}
if (photoWidth > maxWidth) {
photoHeight *= maxWidth / photoWidth;
}
return photoHeight + AndroidUtilities.dp(14);
} else {
2015-05-21 23:27:27 +02:00
int photoHeight;
int photoWidth;
if (AndroidUtilities.isTablet()) {
photoWidth = (int) (AndroidUtilities.getMinTabletSide() * 0.7f);
} else {
photoWidth = (int) (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) * 0.7f);
}
photoHeight = photoWidth + AndroidUtilities.dp(100);
if (photoWidth > AndroidUtilities.getPhotoSize()) {
photoWidth = AndroidUtilities.getPhotoSize();
}
if (photoHeight > AndroidUtilities.getPhotoSize()) {
photoHeight = AndroidUtilities.getPhotoSize();
}
TLRPC.PhotoSize currentPhotoObject = FileLoader.getClosestPhotoSizeWithSize(photoThumbs, AndroidUtilities.getPhotoSize());
if (currentPhotoObject != null) {
float scale = (float) currentPhotoObject.w / (float) photoWidth;
int h = (int) (currentPhotoObject.h / scale);
if (h == 0) {
h = AndroidUtilities.dp(100);
}
if (h > photoHeight) {
h = photoHeight;
} else if (h < AndroidUtilities.dp(120)) {
h = AndroidUtilities.dp(120);
}
if (isSecretPhoto()) {
if (AndroidUtilities.isTablet()) {
2015-05-21 23:27:27 +02:00
h = (int) (AndroidUtilities.getMinTabletSide() * 0.5f);
} else {
2015-05-21 23:27:27 +02:00
h = (int) (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) * 0.5f);
}
}
photoHeight = h;
}
return photoHeight + AndroidUtilities.dp(14);
}
}
2015-02-01 19:51:02 +01:00
public boolean isSticker() {
return isStickerMessage(messageOwner);
}
2015-07-22 20:56:37 +02:00
public boolean isMusic() {
return isMusicMessage(messageOwner);
}
public boolean isGif() {
return isGifDocument(messageOwner.media.document);
}
public boolean isNewGif() {
return messageOwner.media != null && isNewGifDocument(messageOwner.media.document);
}
2015-07-22 20:56:37 +02:00
public String getMusicTitle() {
for (TLRPC.DocumentAttribute attribute : messageOwner.media.document.attributes) {
if (attribute instanceof TLRPC.TL_documentAttributeAudio) {
String title = attribute.title;
if (title == null || title.length() == 0) {
title = FileLoader.getDocumentFileName(messageOwner.media.document);
if (title == null || title.length() == 0) {
title = LocaleController.getString("AudioUnknownTitle", R.string.AudioUnknownTitle);
}
}
return title;
}
}
return "";
}
public String getMusicAuthor() {
for (TLRPC.DocumentAttribute attribute : messageOwner.media.document.attributes) {
if (attribute instanceof TLRPC.TL_documentAttributeAudio) {
String performer = attribute.performer;
if (performer == null || performer.length() == 0) {
performer = LocaleController.getString("AudioUnknownArtist", R.string.AudioUnknownArtist);
}
return performer;
}
}
return "";
}
2015-05-21 23:27:27 +02:00
public TLRPC.InputStickerSet getInputStickerSet() {
return getInputStickerSet(messageOwner);
}
public boolean isForwarded() {
return (messageOwner.flags & TLRPC.MESSAGE_FLAG_FWD) != 0;
}
public boolean isReply() {
return !(replyMessageObject != null && replyMessageObject.messageOwner instanceof TLRPC.TL_messageEmpty) && messageOwner.reply_to_msg_id != 0 && (messageOwner.flags & TLRPC.MESSAGE_FLAG_REPLY) != 0;
}
2015-04-09 20:00:14 +02:00
public boolean isMediaEmpty() {
return isMediaEmpty(messageOwner);
}
public static boolean isMediaEmpty(TLRPC.Message message) {
return message == null || message.media == null || message.media instanceof TLRPC.TL_messageMediaEmpty || message.media instanceof TLRPC.TL_messageMediaWebPage;
}
2015-09-24 22:52:02 +02:00
public boolean canDeleteMessage(TLRPC.Chat chat) {
return canDeleteMessage(messageOwner, chat);
}
public static boolean canDeleteMessage(TLRPC.Message message, TLRPC.Chat chat) {
2015-11-26 22:04:02 +01:00
if (message.id < 0) {
return true;
}
2015-09-24 22:52:02 +02:00
if (chat == null && message.to_id.channel_id != 0) {
chat = MessagesController.getInstance().getChat(message.to_id.channel_id);
}
if (ChatObject.isChannel(chat)) {
if (message.id == 1) {
return false;
}
2015-11-26 22:04:02 +01:00
if (chat.creator) {
2015-09-24 22:52:02 +02:00
return true;
2015-11-26 22:04:02 +01:00
} else if (chat.editor) {
2015-09-24 22:52:02 +02:00
if (isOut(message) || message.from_id > 0) {
return true;
}
2015-11-26 22:04:02 +01:00
} else if (chat.moderator) {
2015-09-24 22:52:02 +02:00
if (message.from_id > 0) {
return true;
}
} else if (isOut(message) && message.from_id > 0) {
return true;
}
}
return isOut(message) || !ChatObject.isChannel(chat);
}
public String getForwardedName() {
if (messageOwner.fwd_from_id instanceof TLRPC.TL_peerChannel) {
TLRPC.Chat chat = MessagesController.getInstance().getChat(messageOwner.fwd_from_id.channel_id);
if (chat != null) {
return chat.title;
}
} else if (messageOwner.fwd_from_id instanceof TLRPC.TL_peerUser) {
TLRPC.User user = MessagesController.getInstance().getUser(messageOwner.fwd_from_id.user_id);
if (user != null) {
return UserObject.getUserName(user);
}
}
return null;
}
2013-10-25 17:19:00 +02:00
}