2014-12-30 09:36:51 +00:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
2017-04-20 04:23:57 +00:00
|
|
|
import android.content.ContentResolver;
|
2014-12-30 09:36:51 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.net.Uri;
|
2017-04-20 04:23:57 +00:00
|
|
|
import android.provider.MediaStore;
|
2015-08-24 22:24:31 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2018-03-20 18:27:11 +00:00
|
|
|
import android.support.annotation.WorkerThread;
|
2015-07-15 20:42:59 +00:00
|
|
|
import android.text.TextUtils;
|
2014-12-30 09:36:51 +00:00
|
|
|
import android.util.Log;
|
2018-03-20 18:27:11 +00:00
|
|
|
import android.util.Pair;
|
2015-07-15 20:42:59 +00:00
|
|
|
import android.webkit.MimeTypeMap;
|
2014-12-30 09:36:51 +00:00
|
|
|
|
2018-03-20 18:27:11 +00:00
|
|
|
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
|
|
|
import com.bumptech.glide.load.resource.gif.GifDrawable;
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
2015-01-19 02:11:30 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.AudioSlide;
|
2018-03-20 18:27:11 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
|
2017-03-28 19:05:30 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.DocumentSlide;
|
2015-07-15 20:42:59 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.GifSlide;
|
2018-03-20 18:27:11 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideApp;
|
2015-01-19 02:11:30 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.ImageSlide;
|
2017-01-20 23:26:17 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsSlide;
|
2015-01-02 23:43:28 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
2015-01-19 02:11:30 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.Slide;
|
|
|
|
import org.thoughtcrime.securesms.mms.VideoSlide;
|
2015-11-21 07:18:19 +00:00
|
|
|
import org.thoughtcrime.securesms.providers.PersistentBlobProvider;
|
2014-12-30 09:36:51 +00:00
|
|
|
|
2018-03-20 18:27:11 +00:00
|
|
|
import java.io.FileNotFoundException;
|
2014-12-30 09:36:51 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2018-03-20 18:27:11 +00:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2014-12-30 09:36:51 +00:00
|
|
|
|
|
|
|
public class MediaUtil {
|
2016-12-11 21:37:27 +00:00
|
|
|
|
2014-12-30 09:36:51 +00:00
|
|
|
private static final String TAG = MediaUtil.class.getSimpleName();
|
|
|
|
|
2017-05-08 22:32:59 +00:00
|
|
|
public static final String IMAGE_PNG = "image/png";
|
|
|
|
public static final String IMAGE_JPEG = "image/jpeg";
|
|
|
|
public static final String IMAGE_GIF = "image/gif";
|
|
|
|
public static final String AUDIO_AAC = "audio/aac";
|
|
|
|
public static final String AUDIO_UNSPECIFIED = "audio/*";
|
|
|
|
public static final String VIDEO_UNSPECIFIED = "video/*";
|
|
|
|
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public static Slide getSlideForAttachment(Context context, Attachment attachment) {
|
2015-01-19 02:11:30 +00:00
|
|
|
Slide slide = null;
|
2015-10-13 01:25:05 +00:00
|
|
|
if (isGif(attachment.getContentType())) {
|
|
|
|
slide = new GifSlide(context, attachment);
|
2017-05-08 22:32:59 +00:00
|
|
|
} else if (isImageType(attachment.getContentType())) {
|
2015-10-13 01:25:05 +00:00
|
|
|
slide = new ImageSlide(context, attachment);
|
2017-05-08 22:32:59 +00:00
|
|
|
} else if (isVideoType(attachment.getContentType())) {
|
2015-10-13 01:25:05 +00:00
|
|
|
slide = new VideoSlide(context, attachment);
|
2017-05-08 22:32:59 +00:00
|
|
|
} else if (isAudioType(attachment.getContentType())) {
|
2015-10-13 01:25:05 +00:00
|
|
|
slide = new AudioSlide(context, attachment);
|
2017-01-20 23:26:17 +00:00
|
|
|
} else if (isMms(attachment.getContentType())) {
|
|
|
|
slide = new MmsSlide(context, attachment);
|
2017-03-31 23:52:50 +00:00
|
|
|
} else if (attachment.getContentType() != null) {
|
2017-03-28 19:05:30 +00:00
|
|
|
slide = new DocumentSlide(context, attachment);
|
2015-01-19 02:11:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return slide;
|
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public static @Nullable String getMimeType(Context context, Uri uri) {
|
2016-04-12 06:40:48 +00:00
|
|
|
if (uri == null) return null;
|
|
|
|
|
2015-11-21 07:18:19 +00:00
|
|
|
if (PersistentBlobProvider.isAuthority(context, uri)) {
|
|
|
|
return PersistentBlobProvider.getMimeType(context, uri);
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:42:59 +00:00
|
|
|
String type = context.getContentResolver().getType(uri);
|
|
|
|
if (type == null) {
|
|
|
|
final String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
|
2015-11-21 07:18:19 +00:00
|
|
|
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
|
2015-07-15 20:42:59 +00:00
|
|
|
}
|
2015-11-21 08:04:19 +00:00
|
|
|
return getCorrectedMimeType(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static @Nullable String getCorrectedMimeType(@Nullable String mimeType) {
|
|
|
|
if (mimeType == null) return null;
|
|
|
|
|
|
|
|
switch(mimeType) {
|
|
|
|
case "image/jpg":
|
2017-05-08 22:32:59 +00:00
|
|
|
return MimeTypeMap.getSingleton().hasMimeType(IMAGE_JPEG)
|
|
|
|
? IMAGE_JPEG
|
2015-11-21 08:04:19 +00:00
|
|
|
: mimeType;
|
|
|
|
default:
|
|
|
|
return mimeType;
|
|
|
|
}
|
2015-07-15 20:42:59 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public static long getMediaSize(Context context, Uri uri) throws IOException {
|
|
|
|
InputStream in = PartAuthority.getAttachmentStream(context, uri);
|
2015-09-05 00:33:22 +00: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 18:27:11 +00: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-20 23:26:17 +00:00
|
|
|
public static boolean isMms(String contentType) {
|
|
|
|
return !TextUtils.isEmpty(contentType) && contentType.trim().equals("application/mms");
|
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public static boolean isGif(Attachment attachment) {
|
|
|
|
return isGif(attachment.getContentType());
|
2015-01-02 23:43:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 18:22:39 +00:00
|
|
|
public static boolean isJpeg(Attachment attachment) {
|
|
|
|
return isJpegType(attachment.getContentType());
|
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public static boolean isImage(Attachment attachment) {
|
2017-05-08 22:32:59 +00:00
|
|
|
return isImageType(attachment.getContentType());
|
2015-01-02 23:43:28 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public static boolean isAudio(Attachment attachment) {
|
2017-05-08 22:32:59 +00:00
|
|
|
return isAudioType(attachment.getContentType());
|
2015-01-02 23:43:28 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public static boolean isVideo(Attachment attachment) {
|
2017-05-08 22:32:59 +00:00
|
|
|
return isVideoType(attachment.getContentType());
|
2015-09-05 00:33:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 23:33:03 +00:00
|
|
|
public static boolean isVideo(String contentType) {
|
|
|
|
return !TextUtils.isEmpty(contentType) && contentType.trim().startsWith("video/");
|
|
|
|
}
|
|
|
|
|
2017-05-08 22:32:59 +00:00
|
|
|
public static boolean isGif(String contentType) {
|
|
|
|
return !TextUtils.isEmpty(contentType) && contentType.trim().equals("image/gif");
|
|
|
|
}
|
|
|
|
|
2018-03-19 18:22:39 +00:00
|
|
|
public static boolean isJpegType(String contentType) {
|
|
|
|
return !TextUtils.isEmpty(contentType) && contentType.trim().equals(IMAGE_JPEG);
|
|
|
|
}
|
|
|
|
|
2017-04-24 22:52:48 +00:00
|
|
|
public static boolean isFile(Attachment attachment) {
|
2017-04-25 17:01:09 +00:00
|
|
|
return !isGif(attachment) && !isImage(attachment) && !isAudio(attachment) && !isVideo(attachment);
|
2017-04-24 22:52:48 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 22:32:59 +00:00
|
|
|
public static boolean isTextType(String contentType) {
|
|
|
|
return (null != contentType) && contentType.startsWith("text/");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isImageType(String contentType) {
|
|
|
|
return (null != contentType) && contentType.startsWith("image/");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isAudioType(String contentType) {
|
|
|
|
return (null != contentType) && contentType.startsWith("audio/");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isVideoType(String contentType) {
|
|
|
|
return (null != contentType) && contentType.startsWith("video/");
|
|
|
|
}
|
|
|
|
|
2017-04-20 04:23:57 +00:00
|
|
|
public static boolean hasVideoThumbnail(Uri uri) {
|
|
|
|
Log.w(TAG, "Checking: " + uri);
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
|
|
|
|
return MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(),
|
|
|
|
videoId,
|
|
|
|
MediaStore.Images.Thumbnails.MINI_KIND,
|
|
|
|
null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-09-05 00:33:22 +00:00
|
|
|
public static @Nullable String getDiscreteMimeType(@NonNull String mimeType) {
|
|
|
|
final String[] sections = mimeType.split("/", 2);
|
2015-08-24 22:24:31 +00:00
|
|
|
return sections.length > 1 ? sections[0] : null;
|
|
|
|
}
|
|
|
|
|
2014-12-30 09:36:51 +00: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 00:15:39 +00:00
|
|
|
return BitmapUtil.toCompressedJpeg(bitmap);
|
2014-12-30 09:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|