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

673 lines
34 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
2013-12-20 20:25:49 +01:00
* This is the source code of Telegram for Android v. 1.3.2.
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.
*/
package org.telegram.android;
2013-10-25 17:19:00 +02:00
import android.graphics.Bitmap;
import android.graphics.Paint;
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;
import android.text.util.Linkify;
2013-10-25 17:19:00 +02:00
import org.telegram.messenger.ConnectionsManager;
import org.telegram.messenger.FileLoader;
import org.telegram.messenger.FileLog;
import org.telegram.messenger.TLRPC;
2013-10-25 17:19:00 +02:00
import org.telegram.messenger.R;
import org.telegram.messenger.UserConfig;
2014-10-15 20:43:52 +02:00
import org.telegram.ui.Views.URLSpanNoUnderline;
2013-10-25 17:19:00 +02:00
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
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;
public int type;
public int contentType;
2013-10-25 17:19:00 +02:00
public ArrayList<PhotoObject> photoThumbs;
public Bitmap imagePreview;
public String dateKey;
public boolean deleted = false;
public float audioProgress;
public int audioProgressSec;
private static TextPaint textPaint;
public int lastLineWidth;
public int textWidth;
public int textHeight;
public int blockHeight = Integer.MAX_VALUE;
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
public MessageObject(TLRPC.Message message, AbstractMap<Integer, TLRPC.User> users) {
this(message, users, 1);
}
public MessageObject(TLRPC.Message message, AbstractMap<Integer, TLRPC.User> users, int preview) {
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 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) {
if (isFromMe()) {
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) {
if (isFromMe()) {
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) {
if (isFromMe()) {
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) {
TLRPC.User whoUser = null;
if (users != null) {
whoUser = users.get(message.action.user_id);
}
2013-12-20 20:25:49 +01:00
if (whoUser == null) {
whoUser = MessagesController.getInstance().getUser(message.action.user_id);
2013-12-20 20:25:49 +01:00
}
if (whoUser != null && fromUser != null) {
if (isFromMe()) {
2014-10-15 20:43:52 +02:00
messageText = replaceWithLink(LocaleController.getString("ActionYouAddUser", R.string.ActionYouAddUser), "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("ActionAddUserYou", R.string.ActionAddUserYou), "un1", fromUser);
2013-12-20 20:25:49 +01:00
} else {
2014-10-15 20:43:52 +02:00
messageText = replaceWithLink(LocaleController.getString("ActionAddUser", R.string.ActionAddUser), "un2", whoUser);
messageText = replaceWithLink(messageText, "un1", fromUser);
2013-12-20 20:25:49 +01:00
}
} else {
messageText = LocaleController.getString("ActionAddUser", R.string.ActionAddUser).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_messageActionChatEditPhoto) {
if (isFromMe()) {
messageText = LocaleController.getString("ActionYouChangedPhoto", R.string.ActionYouChangedPhoto);
2013-12-20 20:25:49 +01:00
} else {
if (fromUser != null) {
2014-10-15 20:43:52 +02:00
messageText = replaceWithLink(LocaleController.getString("ActionChangedPhoto", R.string.ActionChangedPhoto), "un1", fromUser);
2013-12-20 20:25:49 +01:00
} 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) {
if (isFromMe()) {
messageText = LocaleController.getString("ActionYouChangedTitle", R.string.ActionYouChangedTitle).replace("un2", message.action.title);
2013-12-20 20:25:49 +01:00
} else {
if (fromUser != null) {
2014-10-15 20:43:52 +02:00
messageText = replaceWithLink(LocaleController.getString("ActionChangedTitle", R.string.ActionChangedTitle).replace("un2", message.action.title), "un1", fromUser);
2013-12-20 20:25:49 +01:00
} 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) {
if (isFromMe()) {
messageText = LocaleController.getString("ActionYouRemovedPhoto", R.string.ActionYouRemovedPhoto);
2013-12-20 20:25:49 +01:00
} else {
if (fromUser != null) {
2014-10-15 20:43:52 +02:00
messageText = replaceWithLink(LocaleController.getString("ActionRemovedPhoto", R.string.ActionRemovedPhoto), "un1", fromUser);
2013-12-20 20:25:49 +01:00
} 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) {
if (isFromMe()) {
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) {
2014-10-14 22:36:15 +02:00
messageText = LocaleController.formatString("MessageLifetimeChanged", R.string.MessageLifetimeChanged, fromUser.first_name, 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 {
if (isFromMe()) {
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) {
messageText = LocaleController.formatString("MessageLifetimeRemoved", R.string.MessageLifetimeRemoved, fromUser.first_name);
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) {
String date = LocaleController.formatString("formatDateAtTime", R.string.formatDateAtTime, LocaleController.formatterYear.format(((long)message.date) * 1000), LocaleController.formatterDay.format(((long)message.date) * 1000));
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);
}
}
String name = "";
if (to_user != null) {
name = to_user.first_name;
}
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) {
messageText = LocaleController.formatString("NotificationContactJoined", R.string.NotificationContactJoined, ContactsController.formatName(fromUser.first_name, fromUser.last_name));
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) {
messageText = LocaleController.formatString("NotificationContactNewPhoto", R.string.NotificationContactNewPhoto, ContactsController.formatName(fromUser.first_name, fromUser.last_name));
2013-12-20 20:25:49 +01:00
} else {
messageText = LocaleController.formatString("NotificationContactNewPhoto", R.string.NotificationContactNewPhoto, "");
2013-12-20 20:25:49 +01:00
}
} else if (message.action instanceof TLRPC.TL_messageEcryptedAction) {
if (message.action.encryptedAction instanceof TLRPC.TL_decryptedMessageActionScreenshotMessages) {
if (isFromMe()) {
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", "");
}
}
}
} else if (message.action instanceof TLRPC.TL_messageActionCreatedBroadcastList) {
messageText = LocaleController.formatString("YouCreatedBroadcastList", R.string.YouCreatedBroadcastList);
2013-10-25 17:19:00 +02:00
}
}
} else if (message.media != null && !(message.media instanceof TLRPC.TL_messageMediaEmpty)) {
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);
2013-10-25 17:19:00 +02:00
} else if (message.media instanceof TLRPC.TL_messageMediaGeo) {
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("UnsuppotedMedia", R.string.UnsuppotedMedia);
2013-12-26 12:43:37 +01:00
} else if (message.media instanceof TLRPC.TL_messageMediaDocument) {
messageText = LocaleController.getString("AttachDocument", R.string.AttachDocument);
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;
}
messageText = Emoji.replaceEmoji(messageText, textPaint.getFontMetricsInt(), AndroidUtilities.dp(20));
2013-10-25 17:19:00 +02:00
2014-10-15 20:43:52 +02:00
if (message instanceof TLRPC.TL_message || message instanceof TLRPC.TL_messageForwarded) {
2013-10-25 17:19:00 +02:00
if (message.media == null || message.media instanceof TLRPC.TL_messageMediaEmpty) {
contentType = type = 0;
2013-10-25 17:19:00 +02:00
} else if (message.media != null && message.media instanceof TLRPC.TL_messageMediaPhoto) {
contentType = type = 1;
2013-10-25 17:19:00 +02:00
} else if (message.media != null && message.media instanceof TLRPC.TL_messageMediaGeo) {
contentType = 1;
type = 4;
2013-10-25 17:19:00 +02:00
} else if (message.media != null && message.media instanceof TLRPC.TL_messageMediaVideo) {
contentType = 1;
type = 3;
2013-10-25 17:19:00 +02:00
} else if (message.media != null && message.media instanceof TLRPC.TL_messageMediaContact) {
2014-10-15 20:43:52 +02:00
contentType = 3;
type = 12;
2013-12-20 20:25:49 +01:00
} else if (message.media != null && message.media instanceof TLRPC.TL_messageMediaUnsupported) {
contentType = type = 0;
2013-12-26 12:43:37 +01:00
} else if (message.media != null && message.media instanceof TLRPC.TL_messageMediaDocument) {
2014-08-29 23:06:04 +02:00
contentType = 1;
if (message.media.document.thumb != null && !(message.media.document.thumb instanceof TLRPC.TL_photoSizeEmpty) && message.media.document.mime_type != null && message.media.document.mime_type.equals("image/gif")) {
type = 8;
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
}
} else if (message.media != null && 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;
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);
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);
generateLayout();
2014-08-06 01:17:40 +02:00
generateThumbs(false, preview);
}
2014-10-15 20:43:52 +02:00
public CharSequence replaceWithLink(CharSequence source, String param, TLRPC.User user) {
String name = ContactsController.formatName(user.first_name, user.last_name);
int start = TextUtils.indexOf(source, param);
URLSpanNoUnderline span = new URLSpanNoUnderline("" + user.id);
SpannableStringBuilder builder = new SpannableStringBuilder(TextUtils.replace(source, new String[]{param}, new String[]{name}));
builder.setSpan(span, start, start + name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return builder;
}
2014-08-06 01:17:40 +02:00
public void generateThumbs(boolean update, int preview) {
if (messageOwner instanceof TLRPC.TL_messageService) {
if (messageOwner.action instanceof TLRPC.TL_messageActionChatEditPhoto) {
if (!update) {
photoThumbs = new ArrayList<PhotoObject>();
for (TLRPC.PhotoSize size : messageOwner.action.photo.sizes) {
photoThumbs.add(new PhotoObject(size, preview, isSecretMedia()));
2014-08-06 01:17:40 +02:00
}
} else if (photoThumbs != null && !photoThumbs.isEmpty()) {
for (PhotoObject photoObject : photoThumbs) {
for (TLRPC.PhotoSize size : messageOwner.action.photo.sizes) {
if (size instanceof TLRPC.TL_photoSizeEmpty) {
continue;
}
if (size.type.equals(photoObject.photoOwner.type)) {
photoObject.photoOwner.location = size.location;
break;
}
}
}
}
}
} else if (messageOwner.media != null && !(messageOwner.media instanceof TLRPC.TL_messageMediaEmpty)) {
if (messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
if (!update) {
photoThumbs = new ArrayList<PhotoObject>();
for (TLRPC.PhotoSize size : messageOwner.media.photo.sizes) {
PhotoObject obj = new PhotoObject(size, preview, isSecretMedia());
2014-08-06 01:17:40 +02:00
photoThumbs.add(obj);
if (imagePreview == null && obj.image != null) {
imagePreview = obj.image;
}
}
} else if (photoThumbs != null && !photoThumbs.isEmpty()) {
for (PhotoObject photoObject : photoThumbs) {
for (TLRPC.PhotoSize size : messageOwner.media.photo.sizes) {
if (size instanceof TLRPC.TL_photoSizeEmpty) {
continue;
}
if (size.type.equals(photoObject.photoOwner.type)) {
photoObject.photoOwner.location = size.location;
break;
}
}
}
}
} else if (messageOwner.media instanceof TLRPC.TL_messageMediaVideo) {
if (!update) {
photoThumbs = new ArrayList<PhotoObject>();
PhotoObject obj = new PhotoObject(messageOwner.media.video.thumb, preview, isSecretMedia());
2014-08-06 01:17:40 +02:00
photoThumbs.add(obj);
if (imagePreview == null && obj.image != null) {
imagePreview = obj.image;
}
} else if (photoThumbs != null && !photoThumbs.isEmpty() && messageOwner.media.video.thumb != null) {
PhotoObject photoObject = photoThumbs.get(0);
photoObject.photoOwner.location = messageOwner.media.video.thumb.location;
}
} if (messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
if (!(messageOwner.media.document.thumb instanceof TLRPC.TL_photoSizeEmpty)) {
if (!update) {
photoThumbs = new ArrayList<PhotoObject>();
PhotoObject obj = new PhotoObject(messageOwner.media.document.thumb, preview, isSecretMedia());
2014-08-06 01:17:40 +02:00
photoThumbs.add(obj);
} else if (photoThumbs != null && !photoThumbs.isEmpty() && messageOwner.media.document.thumb != null) {
PhotoObject photoObject = photoThumbs.get(0);
2014-08-06 23:27:24 +02:00
photoObject.photoOwner.location = messageOwner.media.document.thumb.location;
2014-08-06 01:17:40 +02:00
}
}
}
}
2013-10-25 17:19:00 +02:00
}
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 "";
}
private void generateLayout() {
2014-08-29 23:06:04 +02:00
if (type != 0 || messageOwner.to_id == null || messageText == null || messageText.length() == 0) {
return;
}
textLayoutBlocks = new ArrayList<TextLayoutBlock>();
if (messageText instanceof Spannable) {
2014-03-02 10:23:06 +01:00
if (messageOwner.message != null && messageOwner.message.contains(".") && (messageOwner.message.contains(".com") || messageOwner.message.contains("http") || messageOwner.message.contains(".ru") || messageOwner.message.contains(".org") || messageOwner.message.contains(".net"))) {
Linkify.addLinks((Spannable)messageText, Linkify.WEB_URLS);
2014-03-02 10:23:06 +01:00
} else if (messageText.length() < 100) {
Linkify.addLinks((Spannable)messageText, Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS);
}
}
int maxWidth;
if (AndroidUtilities.isTablet()) {
if (messageOwner.to_id.chat_id != 0) {
maxWidth = AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(122);
} else {
maxWidth = AndroidUtilities.getMinTabletSide() - AndroidUtilities.dp(80);
}
} else {
if (messageOwner.to_id.chat_id != 0) {
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);
}
}
2014-03-10 10:27:49 +01:00
StaticLayout textLayout = null;
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;
}
}
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);
}
2014-07-11 23:47:50 +02:00
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;
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) {
int start = block.textLayout.getLineStart(n);
int end = block.textLayout.getLineEnd(n);
CharSequence text = block.textLayout.getText().subSequence(start, end);
continue;
}
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;
}
linesMaxWidth = linesMaxWidthWithLeft;
} 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() {
return (messageOwner.flags & TLRPC.MESSAGE_FLAG_OUT) != 0;
}
public boolean isFromMe() {
2014-06-13 12:42:21 +02:00
return messageOwner.from_id == UserConfig.getClientUserId();
}
public boolean isUnread() {
return (messageOwner.flags & TLRPC.MESSAGE_FLAG_UNREAD) != 0;
}
public void setIsRead() {
messageOwner.flags &=~ TLRPC.MESSAGE_FLAG_UNREAD;
}
public boolean isSecretMedia() {
2014-10-14 10:13:16 +02:00
return messageOwner instanceof TLRPC.TL_message_secret && messageOwner.media instanceof TLRPC.TL_messageMediaPhoto && messageOwner.ttl != 0 && messageOwner.ttl <= 60;
}
public static void setIsUnread(TLRPC.Message message, boolean unread) {
if (unread) {
message.flags |= TLRPC.MESSAGE_FLAG_UNREAD;
} else {
message.flags &=~ TLRPC.MESSAGE_FLAG_UNREAD;
}
}
public static boolean isUnread(TLRPC.Message message) {
return (message.flags & TLRPC.MESSAGE_FLAG_UNREAD) != 0;
}
public static boolean isOut(TLRPC.Message message) {
return (message.flags & TLRPC.MESSAGE_FLAG_OUT) != 0;
2014-07-10 02:15:58 +02:00
}
public long getDialogId() {
if (messageOwner.dialog_id != 0) {
return messageOwner.dialog_id;
} else {
if (messageOwner.to_id.chat_id != 0) {
return -messageOwner.to_id.chat_id;
} else if (isFromMe()) {
return messageOwner.to_id.user_id;
} else {
return messageOwner.from_id;
}
}
}
2014-09-28 15:37:26 +02:00
public boolean isSending() {
return messageOwner.send_state == MESSAGE_SEND_STATE_SENDING;
}
public boolean isSendError() {
return messageOwner.send_state == MESSAGE_SEND_STATE_SEND_ERROR;
}
public boolean isSent() {
return messageOwner.send_state == MESSAGE_SEND_STATE_SENT;
}
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;
}
2013-10-25 17:19:00 +02:00
}