From 1add172770e7f4e2b1498eff797ee4553344eafb Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Sat, 22 Apr 2017 14:09:40 +0200 Subject: [PATCH] Play GIFs. --- MessengerProj/jni/mrwrapper.c | 2 +- .../com/b44t/messenger/AndroidUtilities.java | 2 +- .../java/com/b44t/messenger/ImageLoader.java | 2 -- .../com/b44t/messenger/ImageReceiver.java | 2 +- .../com/b44t/messenger/MessageObject.java | 17 +--------- .../java/com/b44t/messenger/MrMailbox.java | 1 + .../main/java/com/b44t/messenger/MrMsg.java | 16 +++++++-- .../b44t/messenger/SendMessagesHelper.java | 3 +- .../com/b44t/ui/Cells/ChatMessageCell.java | 33 ++----------------- .../main/java/com/b44t/ui/ChatActivity.java | 2 +- .../main/java/com/b44t/ui/PhotoViewer.java | 2 +- 11 files changed, 25 insertions(+), 57 deletions(-) diff --git a/MessengerProj/jni/mrwrapper.c b/MessengerProj/jni/mrwrapper.c index d9f5ffb5d..18e7e2a80 100644 --- a/MessengerProj/jni/mrwrapper.c +++ b/MessengerProj/jni/mrwrapper.c @@ -697,7 +697,7 @@ JNIEXPORT jint Java_com_b44t_messenger_MrChat_sendMedia(JNIEnv *env, jclass cls, CHAR_REF(file); mrparam_set(msg->m_param, 'f', filePtr); CHAR_UNREF(file); - if( type == MR_MSG_IMAGE || type == MR_MSG_VIDEO ) { + if( type == MR_MSG_IMAGE || type == MR_MSG_GIF || type == MR_MSG_VIDEO ) { mrparam_set_int(msg->m_param, 'w', w); mrparam_set_int(msg->m_param, 'h', h); } diff --git a/MessengerProj/src/main/java/com/b44t/messenger/AndroidUtilities.java b/MessengerProj/src/main/java/com/b44t/messenger/AndroidUtilities.java index b532d90f2..a29bbb39a 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/AndroidUtilities.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/AndroidUtilities.java @@ -972,7 +972,7 @@ public class AndroidUtilities { // get destination path File destPath = null; boolean add_to_download_manager = false; - if (msg_type == MrMsg.MR_MSG_IMAGE || msg_type == MrMsg.MR_MSG_VIDEO) { + if (msg_type == MrMsg.MR_MSG_IMAGE || msg_type == MrMsg.MR_MSG_GIF || msg_type == MrMsg.MR_MSG_VIDEO) { destPath = AndroidUtilities.getAlbumDir(); } else if (msg_type == MrMsg.MR_MSG_FILE) { destPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); diff --git a/MessengerProj/src/main/java/com/b44t/messenger/ImageLoader.java b/MessengerProj/src/main/java/com/b44t/messenger/ImageLoader.java index 7b5180ee2..43278b747 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/ImageLoader.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/ImageLoader.java @@ -37,7 +37,6 @@ import android.media.ThumbnailUtils; import android.net.Uri; import android.os.Build; import android.provider.MediaStore; -import android.util.Log; import com.b44t.ui.Components.AnimatedFileDrawable; @@ -51,7 +50,6 @@ import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.HashMap; -import java.util.concurrent.ConcurrentHashMap; public class ImageLoader { diff --git a/MessengerProj/src/main/java/com/b44t/messenger/ImageReceiver.java b/MessengerProj/src/main/java/com/b44t/messenger/ImageReceiver.java index c95b0fe4e..ebc53111f 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/ImageReceiver.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/ImageReceiver.java @@ -140,7 +140,7 @@ public class ImageReceiver implements NotificationCenter.NotificationCenterDeleg setImage(path, null, filter, thumb, null, null, size, ext, cacheOnly); } - public void setImage(String httpUrl, String filter, Drawable thumb, String ext, int size) { + public void setImageByPath(String httpUrl, String filter, Drawable thumb, String ext, int size) { setImage(null, httpUrl, filter, thumb, null, null, size, ext, true); } diff --git a/MessengerProj/src/main/java/com/b44t/messenger/MessageObject.java b/MessengerProj/src/main/java/com/b44t/messenger/MessageObject.java index 47d389e9b..0bcc2bcd0 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/MessageObject.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/MessageObject.java @@ -161,18 +161,7 @@ public class MessageObject { } 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; + return document != null && document.thumb != null && document.mime_type != null && document.mime_type.equals("image/gif"); } private void generateThumbs(boolean update) { @@ -720,10 +709,6 @@ public class MessageObject { return messageOwner.media instanceof TLRPC.TL_messageMediaDocument && isGifDocument(messageOwner.media.document); } - public boolean isNewGif() { - return messageOwner.media != null && isNewGifDocument(messageOwner.media.document); - } - public int getDuration() { TLRPC.Document document; document = messageOwner.media.document; diff --git a/MessengerProj/src/main/java/com/b44t/messenger/MrMailbox.java b/MessengerProj/src/main/java/com/b44t/messenger/MrMailbox.java index edaf091d0..49e36a056 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/MrMailbox.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/MrMailbox.java @@ -326,6 +326,7 @@ public class MrMailbox { case 20: s = ApplicationLoader.applicationContext.getString(R.string.Error); break; case 21: s = ApplicationLoader.applicationContext.getString(R.string.ErrSelfNotInGroup); break; case 22: s = ApplicationLoader.applicationContext.getString(R.string.NoNetwork); break; + case 23: s = ApplicationLoader.applicationContext.getString(R.string.AttachGif); break; } return String2CPtr(s); diff --git a/MessengerProj/src/main/java/com/b44t/messenger/MrMsg.java b/MessengerProj/src/main/java/com/b44t/messenger/MrMsg.java index d11ff6610..acaf6c6b1 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/MrMsg.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/MrMsg.java @@ -41,6 +41,7 @@ public class MrMsg { public final static int MR_MSG_UNDEFINED = 0; public final static int MR_MSG_TEXT = 10; public final static int MR_MSG_IMAGE = 20; + public final static int MR_MSG_GIF = 21; public final static int MR_MSG_AUDIO = 40; public final static int MR_MSG_VOICE = 41; public final static int MR_MSG_VIDEO = 50; @@ -195,7 +196,7 @@ public class MrMsg { ret.message = ""; } } - else if( type == MR_MSG_AUDIO || type == MR_MSG_VOICE || type == MR_MSG_VIDEO || type == MR_MSG_FILE ) { + else if( type == MR_MSG_GIF || type == MR_MSG_AUDIO || type == MR_MSG_VOICE || type == MR_MSG_VIDEO || type == MR_MSG_FILE ) { String path = getParam('f', ""); if( !path.isEmpty()) { ret.message = "-1"; // may be misused for video editing information @@ -205,7 +206,18 @@ public class MrMsg { ret.media.document.file_name = getFilename(); ret.media.document.mr_path = path; ret.media.document.size = getBytes(); - if( type == MR_MSG_AUDIO || type == MR_MSG_VOICE ) { + if( type == MR_MSG_GIF ) { + ret.media.document.mime_type = getParam('m', "image/gif"); + TLRPC.PhotoSize size = new TLRPC.PhotoSize(); + size.location = new TLRPC.TL_fileLocation(); + size.location.mr_path = path; + size.location.local_id = -ret.id; + size.w = getParamInt('w', 320); + size.h = getParamInt('h', 240); + size.type = "s"; + ret.media.document.thumb = size; + } + else if( type == MR_MSG_AUDIO || type == MR_MSG_VOICE ) { TLRPC.TL_documentAttributeAudio attr = new TLRPC.TL_documentAttributeAudio(); attr.voice = type == MR_MSG_VOICE; attr.duration = getParamInt('d', 0) / 1000; diff --git a/MessengerProj/src/main/java/com/b44t/messenger/SendMessagesHelper.java b/MessengerProj/src/main/java/com/b44t/messenger/SendMessagesHelper.java index a45606c5e..763ac1b51 100644 --- a/MessengerProj/src/main/java/com/b44t/messenger/SendMessagesHelper.java +++ b/MessengerProj/src/main/java/com/b44t/messenger/SendMessagesHelper.java @@ -540,7 +540,7 @@ public class SendMessagesHelper implements NotificationCenter.NotificationCenter String originalPath = path; String tempPath = path; if (tempPath == null && uri != null) { - tempPath = AndroidUtilities.getPath(uri); + tempPath = AndroidUtilities.getPath(uri); // convert uri to path, this may fail originalPath = uri.toString(); } @@ -553,6 +553,7 @@ public class SendMessagesHelper implements NotificationCenter.NotificationCenter } isDocument = true; } else if (tempPath == null && uri != null) { + // if conversion from uri to path fails above, we have a look at the content of the file if (MediaController.isGif(uri)) { isDocument = true; originalPath = uri.toString(); diff --git a/MessengerProj/src/main/java/com/b44t/ui/Cells/ChatMessageCell.java b/MessengerProj/src/main/java/com/b44t/ui/Cells/ChatMessageCell.java index d994641fe..8c0087cd6 100644 --- a/MessengerProj/src/main/java/com/b44t/ui/Cells/ChatMessageCell.java +++ b/MessengerProj/src/main/java/com/b44t/ui/Cells/ChatMessageCell.java @@ -66,7 +66,6 @@ import com.b44t.ui.Components.StaticLayoutEx; import com.b44t.ui.ActionBar.Theme; import com.b44t.ui.PhotoViewer; -import java.io.File; import java.util.ArrayList; import java.util.Locale; @@ -137,7 +136,6 @@ public class ChatMessageCell extends BaseCell implements SeekBar.SeekBarDelegate private int buttonPressed; private boolean imagePressed; - private boolean photoNotSet; private RectF deleteProgressRect = new RectF(); private RectF rect = new RectF(); private TLRPC.PhotoSize currentPhotoObject; @@ -868,11 +866,6 @@ public class ChatMessageCell extends BaseCell implements SeekBar.SeekBarDelegate } if (currentPhotoObject == null || currentPhotoObject.location instanceof TLRPC.TL_fileLocationUnavailable) { return true; - } else if (currentMessageObject != null && photoNotSet) { - File cacheFile = FileLoader.getPathToMessage(currentMessageObject.messageOwner); - if (cacheFile.exists()) { //TODO - return true; - } } return false; } @@ -1214,7 +1207,7 @@ public class ChatMessageCell extends BaseCell implements SeekBar.SeekBarDelegate public void setMessageObject(MessageObject messageObject) { boolean messageChanged = currentMessageObject != messageObject || messageObject.forceUpdate; - boolean dataChanged = currentMessageObject == messageObject && (isUserDataChanged() || photoNotSet); + boolean dataChanged = currentMessageObject == messageObject && isUserDataChanged(); if (messageChanged || dataChanged || isPhotoDataChanged(messageObject)) { currentMessageObject = messageObject; lastSendState = messageObject.messageOwner.send_state; @@ -1244,7 +1237,6 @@ public class ChatMessageCell extends BaseCell implements SeekBar.SeekBarDelegate currentPhotoFilter = null; infoLayout = null; buttonState = -1; - photoNotSet = false; drawBackground = true; drawName = false; useSeekBarWaveform = false; @@ -1596,7 +1588,6 @@ public class ChatMessageCell extends BaseCell implements SeekBar.SeekBarDelegate if (photoExist /*|| MediaController.getInstance().canDownloadMedia(MediaController.AUTODOWNLOAD_MASK_PHOTO) || FileLoader.getInstance().isLoadingFile(fileName)*/) { photoImage.setImage(currentPhotoObject.location, currentPhotoFilter, currentPhotoObjectThumb != null ? currentPhotoObjectThumb.location : null, currentPhotoFilter, noSize ? 0 : currentPhotoObject.size, null, false); } else { - photoNotSet = true; if (currentPhotoObjectThumb != null) { photoImage.setImage(null, null, currentPhotoObjectThumb.location, currentPhotoFilter, 0, null, false); } else { @@ -1607,23 +1598,7 @@ public class ChatMessageCell extends BaseCell implements SeekBar.SeekBarDelegate photoImage.setImageBitmap((BitmapDrawable) null); } } else if (messageObject.type == MessageObject.MO_TYPE8_GIF) { - String fileName = FileLoader.getAttachFileName(messageObject.messageOwner.media.document); - int localFile = 0; - if (messageObject.attachPathExists) { - localFile = 1; - } else if (messageObject.mediaExists) { - localFile = 2; - } - if (!messageObject.isSending() && (localFile != 0 /*|| MediaController.getInstance().canDownloadMedia(MediaController.AUTODOWNLOAD_MASK_GIF) && MessageObject.isNewGifDocument(messageObject.messageOwner.media.document)*/ || FileLoader.getInstance().isLoadingFile(fileName))) { - if (localFile == 1) { - photoImage.setImage(null, messageObject.isSendError() ? null : messageObject.messageOwner.attachPath, null, null, currentPhotoObject != null ? currentPhotoObject.location : null, currentPhotoFilter, 0, null, false); - } else { - photoImage.setImage(messageObject.messageOwner.media.document, null, currentPhotoObject != null ? currentPhotoObject.location : null, currentPhotoFilter, messageObject.messageOwner.media.document.size, null, false); - } - } else { - photoNotSet = true; - photoImage.setImage(null, null, currentPhotoObject != null ? currentPhotoObject.location : null, currentPhotoFilter, 0, null, false); - } + photoImage.setImageByPath(currentPhotoObject.location.mr_path, null, null, null, 0); } else { photoImage.setImage(null, null, currentPhotoObject != null ? currentPhotoObject.location : null, currentPhotoFilter, 0, null, false); } @@ -2042,10 +2017,6 @@ public class ChatMessageCell extends BaseCell implements SeekBar.SeekBarDelegate } else { buttonState = -1; } - - if (photoNotSet) { - setMessageObject(currentMessageObject); - } invalidate(); } } diff --git a/MessengerProj/src/main/java/com/b44t/ui/ChatActivity.java b/MessengerProj/src/main/java/com/b44t/ui/ChatActivity.java index 675a51323..4c9aa4dc7 100644 --- a/MessengerProj/src/main/java/com/b44t/ui/ChatActivity.java +++ b/MessengerProj/src/main/java/com/b44t/ui/ChatActivity.java @@ -1556,7 +1556,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not hasAttachment = true; m_saveToXXMenuItem.setText(R.string.SaveToMusic); } - if( type==MrMsg.MR_MSG_IMAGE || type == MrMsg.MR_MSG_VIDEO ) { + if( type==MrMsg.MR_MSG_IMAGE || type==MrMsg.MR_MSG_GIF || type == MrMsg.MR_MSG_VIDEO ) { hasAttachment = true; m_saveToXXMenuItem.setText(R.string.SaveToGallery); } diff --git a/MessengerProj/src/main/java/com/b44t/ui/PhotoViewer.java b/MessengerProj/src/main/java/com/b44t/ui/PhotoViewer.java index 4e09afcdf..80c91e6a1 100644 --- a/MessengerProj/src/main/java/com/b44t/ui/PhotoViewer.java +++ b/MessengerProj/src/main/java/com/b44t/ui/PhotoViewer.java @@ -2796,7 +2796,7 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat if (document != null) { imageReceiver.setImage(document, null, "d", placeHolder != null ? new BitmapDrawable(null, placeHolder) : null, placeHolder == null ? document.thumb.location : null, String.format(Locale.US, "%d_%d", size, size), imageSize, null, false); } else { - imageReceiver.setImage(path, filter, placeHolder != null ? new BitmapDrawable(null, placeHolder) : null, null, imageSize); + imageReceiver.setImageByPath(path, filter, placeHolder != null ? new BitmapDrawable(null, placeHolder) : null, null, imageSize); } } else { imageReceiver.setImageBitmap((Bitmap) null);