2014-12-30 10:36:51 +01:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
2017-04-20 06:23:57 +02:00
|
|
|
import android.content.ContentResolver;
|
2014-12-30 10:36:51 +01:00
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.net.Uri;
|
2017-04-20 06:23:57 +02:00
|
|
|
import android.provider.MediaStore;
|
2020-08-19 02:06:26 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.annotation.WorkerThread;
|
2015-07-15 22:42:59 +02:00
|
|
|
import android.text.TextUtils;
|
2018-03-20 19:27:11 +01:00
|
|
|
import android.util.Pair;
|
2015-07-15 22:42:59 +02:00
|
|
|
import android.webkit.MimeTypeMap;
|
2014-12-30 10:36:51 +01:00
|
|
|
|
2018-03-20 19:27:11 +01:00
|
|
|
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
|
|
|
import com.bumptech.glide.load.resource.gif.GifDrawable;
|
|
|
|
|
2021-01-14 00:28:07 +01:00
|
|
|
import org.session.libsession.messaging.sending_receiving.attachments.Attachment;
|
2021-02-01 07:00:32 +01:00
|
|
|
import org.session.libsession.utilities.MediaTypes;
|
2021-05-18 01:12:33 +02:00
|
|
|
import org.session.libsignal.utilities.Log;
|
2015-01-19 03:11:30 +01:00
|
|
|
import org.thoughtcrime.securesms.mms.AudioSlide;
|
2018-03-20 19:27:11 +01:00
|
|
|
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
|
2017-03-28 21:05:30 +02:00
|
|
|
import org.thoughtcrime.securesms.mms.DocumentSlide;
|
2015-07-15 22:42:59 +02:00
|
|
|
import org.thoughtcrime.securesms.mms.GifSlide;
|
2018-03-20 19:27:11 +01:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideApp;
|
2015-01-19 03:11:30 +01:00
|
|
|
import org.thoughtcrime.securesms.mms.ImageSlide;
|
2017-01-21 00:26:17 +01:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsSlide;
|
2015-01-03 00:43:28 +01:00
|
|
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
2015-01-19 03:11:30 +01:00
|
|
|
import org.thoughtcrime.securesms.mms.Slide;
|
2019-02-27 04:29:52 +01:00
|
|
|
import org.thoughtcrime.securesms.mms.TextSlide;
|
2015-01-19 03:11:30 +01:00
|
|
|
import org.thoughtcrime.securesms.mms.VideoSlide;
|
2014-12-30 10:36:51 +01:00
|
|
|
|
2018-03-20 19:27:11 +01:00
|
|
|
import java.io.FileNotFoundException;
|
2014-12-30 10:36:51 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2018-03-20 19:27:11 +01:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2014-12-30 10:36:51 +01:00
|
|
|
|
|
|
|
public class MediaUtil {
|
2016-12-11 22:37:27 +01:00
|
|
|
|
2014-12-30 10:36:51 +01:00
|
|
|
private static final String TAG = MediaUtil.class.getSimpleName();
|
|
|
|
|
2015-10-13 03:25:05 +02:00
|
|
|
public static Slide getSlideForAttachment(Context context, Attachment attachment) {
|
2015-01-19 03:11:30 +01:00
|
|
|
Slide slide = null;
|
2021-02-23 05:07:31 +01:00
|
|
|
if (isGif(attachment.getContentType())) {
|
2015-10-13 03:25:05 +02:00
|
|
|
slide = new GifSlide(context, attachment);
|
2017-05-09 00:32:59 +02:00
|
|
|
} else if (isImageType(attachment.getContentType())) {
|
2015-10-13 03:25:05 +02:00
|
|
|
slide = new ImageSlide(context, attachment);
|
2017-05-09 00:32:59 +02:00
|
|
|
} else if (isVideoType(attachment.getContentType())) {
|
2015-10-13 03:25:05 +02:00
|
|
|
slide = new VideoSlide(context, attachment);
|
2017-05-09 00:32:59 +02:00
|
|
|
} else if (isAudioType(attachment.getContentType())) {
|
2015-10-13 03:25:05 +02:00
|
|
|
slide = new AudioSlide(context, attachment);
|
2017-01-21 00:26:17 +01:00
|
|
|
} else if (isMms(attachment.getContentType())) {
|
|
|
|
slide = new MmsSlide(context, attachment);
|
2019-02-27 04:29:52 +01:00
|
|
|
} else if (isLongTextType(attachment.getContentType())) {
|
|
|
|
slide = new TextSlide(context, attachment);
|
2017-04-01 01:52:50 +02:00
|
|
|
} else if (attachment.getContentType() != null) {
|
2017-03-28 21:05:30 +02:00
|
|
|
slide = new DocumentSlide(context, attachment);
|
2015-01-19 03:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return slide;
|
|
|
|
}
|
|
|
|
|
2015-10-13 03:25:05 +02:00
|
|
|
public static @Nullable String getMimeType(Context context, Uri uri) {
|
2016-04-12 08:40:48 +02:00
|
|
|
if (uri == null) return null;
|
|
|
|
|
2018-08-13 22:55:58 +02:00
|
|
|
if (PartAuthority.isLocalUri(uri)) {
|
|
|
|
return PartAuthority.getAttachmentContentType(context, uri);
|
2015-11-21 08:18:19 +01:00
|
|
|
}
|
|
|
|
|
2015-07-15 22:42:59 +02:00
|
|
|
String type = context.getContentResolver().getType(uri);
|
|
|
|
if (type == null) {
|
|
|
|
final String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
|
2015-11-21 08:18:19 +01:00
|
|
|
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
|
2015-07-15 22:42:59 +02:00
|
|
|
}
|
2018-08-13 22:55:58 +02:00
|
|
|
|
2015-11-21 09:04:19 +01:00
|
|
|
return getCorrectedMimeType(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static @Nullable String getCorrectedMimeType(@Nullable String mimeType) {
|
|
|
|
if (mimeType == null) return null;
|
|
|
|
|
|
|
|
switch(mimeType) {
|
|
|
|
case "image/jpg":
|
2021-02-01 07:00:32 +01:00
|
|
|
return MimeTypeMap.getSingleton().hasMimeType(MediaTypes.IMAGE_JPEG)
|
|
|
|
? MediaTypes.IMAGE_JPEG
|
2015-11-21 09:04:19 +01:00
|
|
|
: mimeType;
|
|
|
|
default:
|
|
|
|
return mimeType;
|
|
|
|
}
|
2015-07-15 22:42:59 +02:00
|
|
|
}
|
|
|
|
|
2018-01-25 04:17:44 +01:00
|
|
|
public static long getMediaSize(Context context, Uri uri) throws IOException {
|
|
|
|
InputStream in = PartAuthority.getAttachmentStream(context, uri);
|
2015-09-05 02:33:22 +02:00
|
|
|
if (in == null) throw new IOException("Couldn't obtain input stream.");
|
|
|
|
|
|
|
|
long size = 0;
|
|
|
|
byte[] buffer = new byte[4096];
|
|
|
|
int read;
|
|
|
|
|
|
|
|
while ((read = in.read(buffer)) != -1) {
|
|
|
|
size += read;
|
|
|
|
}
|
|
|
|
in.close();
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2018-03-20 19:27:11 +01:00
|
|
|
@WorkerThread
|
|
|
|
public static Pair<Integer, Integer> getDimensions(@NonNull Context context, @Nullable String contentType, @Nullable Uri uri) {
|
|
|
|
if (uri == null || !MediaUtil.isImageType(contentType)) {
|
|
|
|
return new Pair<>(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Pair<Integer, Integer> dimens = null;
|
|
|
|
|
|
|
|
if (MediaUtil.isGif(contentType)) {
|
|
|
|
try {
|
|
|
|
GifDrawable drawable = GlideApp.with(context)
|
|
|
|
.asGif()
|
|
|
|
.skipMemoryCache(true)
|
|
|
|
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
|
|
|
.load(new DecryptableUri(uri))
|
|
|
|
.submit()
|
|
|
|
.get();
|
|
|
|
dimens = new Pair<>(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Log.w(TAG, "Was unable to complete work for GIF dimensions.", e);
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
Log.w(TAG, "Glide experienced an exception while trying to get GIF dimensions.", e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
InputStream attachmentStream = null;
|
|
|
|
try {
|
|
|
|
if (MediaUtil.isJpegType(contentType)) {
|
|
|
|
attachmentStream = PartAuthority.getAttachmentStream(context, uri);
|
|
|
|
dimens = BitmapUtil.getExifDimensions(attachmentStream);
|
|
|
|
attachmentStream.close();
|
|
|
|
attachmentStream = null;
|
|
|
|
}
|
|
|
|
if (dimens == null) {
|
|
|
|
attachmentStream = PartAuthority.getAttachmentStream(context, uri);
|
|
|
|
dimens = BitmapUtil.getDimensions(attachmentStream);
|
|
|
|
}
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
Log.w(TAG, "Failed to find file when retrieving media dimensions.", e);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, "Experienced a read error when retrieving media dimensions.", e);
|
|
|
|
} catch (BitmapDecodingException e) {
|
|
|
|
Log.w(TAG, "Bitmap decoding error when retrieving dimensions.", e);
|
|
|
|
} finally {
|
|
|
|
if (attachmentStream != null) {
|
|
|
|
try {
|
|
|
|
attachmentStream.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, "Failed to close stream after retrieving dimensions.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dimens == null) {
|
|
|
|
dimens = new Pair<>(0, 0);
|
|
|
|
}
|
|
|
|
Log.d(TAG, "Dimensions for [" + uri + "] are " + dimens.first + " x " + dimens.second);
|
|
|
|
return dimens;
|
|
|
|
}
|
|
|
|
|
2017-01-21 00:26:17 +01:00
|
|
|
public static boolean isMms(String contentType) {
|
|
|
|
return !TextUtils.isEmpty(contentType) && contentType.trim().equals("application/mms");
|
|
|
|
}
|
|
|
|
|
2015-10-13 03:25:05 +02:00
|
|
|
public static boolean isGif(Attachment attachment) {
|
|
|
|
return isGif(attachment.getContentType());
|
2015-01-03 00:43:28 +01:00
|
|
|
}
|
|
|
|
|
2018-03-19 19:22:39 +01:00
|
|
|
public static boolean isJpeg(Attachment attachment) {
|
|
|
|
return isJpegType(attachment.getContentType());
|
|
|
|
}
|
|
|
|
|
2015-10-13 03:25:05 +02:00
|
|
|
public static boolean isImage(Attachment attachment) {
|
2017-05-09 00:32:59 +02:00
|
|
|
return isImageType(attachment.getContentType());
|
2015-01-03 00:43:28 +01:00
|
|
|
}
|
|
|
|
|
2015-10-13 03:25:05 +02:00
|
|
|
public static boolean isAudio(Attachment attachment) {
|
2017-05-09 00:32:59 +02:00
|
|
|
return isAudioType(attachment.getContentType());
|
2015-01-03 00:43:28 +01:00
|
|
|
}
|
|
|
|
|
2015-10-13 03:25:05 +02:00
|
|
|
public static boolean isVideo(Attachment attachment) {
|
2017-05-09 00:32:59 +02:00
|
|
|
return isVideoType(attachment.getContentType());
|
2015-09-05 02:33:22 +02:00
|
|
|
}
|
|
|
|
|
2018-05-17 08:40:14 +02:00
|
|
|
public static boolean isVcard(String contentType) {
|
2021-02-01 07:00:32 +01:00
|
|
|
return !TextUtils.isEmpty(contentType) && contentType.trim().equals(MediaTypes.VCARD);
|
2018-05-17 08:40:14 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 00:32:59 +02:00
|
|
|
public static boolean isGif(String contentType) {
|
|
|
|
return !TextUtils.isEmpty(contentType) && contentType.trim().equals("image/gif");
|
|
|
|
}
|
|
|
|
|
2018-03-19 19:22:39 +01:00
|
|
|
public static boolean isJpegType(String contentType) {
|
2021-02-01 07:00:32 +01:00
|
|
|
return !TextUtils.isEmpty(contentType) && contentType.trim().equals(MediaTypes.IMAGE_JPEG);
|
2018-03-19 19:22:39 +01:00
|
|
|
}
|
|
|
|
|
2017-04-25 00:52:48 +02:00
|
|
|
public static boolean isFile(Attachment attachment) {
|
2017-04-25 19:01:09 +02:00
|
|
|
return !isGif(attachment) && !isImage(attachment) && !isAudio(attachment) && !isVideo(attachment);
|
2017-04-25 00:52:48 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 00:32:59 +02:00
|
|
|
public static boolean isImageType(String contentType) {
|
2020-09-03 05:27:02 +02:00
|
|
|
return (null != contentType)
|
|
|
|
&& contentType.startsWith("image/")
|
|
|
|
&& !contentType.contains("svg"); // Do not treat SVGs as regular images.
|
2017-05-09 00:32:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isAudioType(String contentType) {
|
|
|
|
return (null != contentType) && contentType.startsWith("audio/");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isVideoType(String contentType) {
|
|
|
|
return (null != contentType) && contentType.startsWith("video/");
|
|
|
|
}
|
|
|
|
|
2019-02-27 04:29:52 +01:00
|
|
|
public static boolean isLongTextType(String contentType) {
|
2021-02-01 07:00:32 +01:00
|
|
|
return (null != contentType) && contentType.equals(MediaTypes.LONG_TEXT);
|
2019-02-27 04:29:52 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 06:23:57 +02:00
|
|
|
public static boolean hasVideoThumbnail(Uri uri) {
|
2018-08-02 15:25:33 +02:00
|
|
|
Log.i(TAG, "Checking: " + uri);
|
2017-04-20 06:23:57 +02:00
|
|
|
|
|
|
|
if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
|
|
|
|
return uri.getLastPathSegment().contains("video");
|
2018-11-20 18:59:23 +01:00
|
|
|
} else if (uri.toString().startsWith(MediaStore.Video.Media.EXTERNAL_CONTENT_URI.toString())) {
|
|
|
|
return true;
|
2017-04-20 06:23:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static @Nullable Bitmap getVideoThumbnail(Context context, Uri uri) {
|
|
|
|
if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
|
|
|
|
long videoId = Long.parseLong(uri.getLastPathSegment().split(":")[1]);
|
|
|
|
|
2018-11-20 18:59:23 +01:00
|
|
|
return MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(),
|
|
|
|
videoId,
|
|
|
|
MediaStore.Images.Thumbnails.MINI_KIND,
|
|
|
|
null);
|
|
|
|
} else if (uri.toString().startsWith(MediaStore.Video.Media.EXTERNAL_CONTENT_URI.toString())) {
|
|
|
|
long videoId = Long.parseLong(uri.getLastPathSegment());
|
|
|
|
|
2017-04-20 06:23:57 +02:00
|
|
|
return MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(),
|
|
|
|
videoId,
|
|
|
|
MediaStore.Images.Thumbnails.MINI_KIND,
|
|
|
|
null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-09-05 02:33:22 +02:00
|
|
|
public static @Nullable String getDiscreteMimeType(@NonNull String mimeType) {
|
|
|
|
final String[] sections = mimeType.split("/", 2);
|
2015-08-25 00:24:31 +02:00
|
|
|
return sections.length > 1 ? sections[0] : null;
|
|
|
|
}
|
|
|
|
|
2014-12-30 10:36:51 +01:00
|
|
|
public static class ThumbnailData {
|
|
|
|
Bitmap bitmap;
|
|
|
|
float aspectRatio;
|
|
|
|
|
|
|
|
public ThumbnailData(Bitmap bitmap) {
|
|
|
|
this.bitmap = bitmap;
|
|
|
|
this.aspectRatio = (float) bitmap.getWidth() / (float) bitmap.getHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Bitmap getBitmap() {
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getAspectRatio() {
|
|
|
|
return aspectRatio;
|
|
|
|
}
|
|
|
|
|
|
|
|
public InputStream toDataStream() {
|
2015-01-14 01:15:39 +01:00
|
|
|
return BitmapUtil.toCompressedJpeg(bitmap);
|
2014-12-30 10:36:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|