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;
|
2015-07-15 20:42:59 +00:00
|
|
|
import android.text.TextUtils;
|
2014-12-30 09:36:51 +00:00
|
|
|
import android.util.Log;
|
2015-07-15 20:42:59 +00:00
|
|
|
import android.webkit.MimeTypeMap;
|
2014-12-30 09:36:51 +00:00
|
|
|
|
2016-12-14 18:21:14 +00:00
|
|
|
import com.bumptech.glide.Glide;
|
|
|
|
|
2014-12-30 09:36:51 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
2014-12-30 09:36:51 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2015-01-19 02:11:30 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.AudioSlide;
|
2015-07-25 00:07:33 +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;
|
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
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2016-12-14 18:21:14 +00:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2014-12-30 09:36:51 +00:00
|
|
|
|
|
|
|
import ws.com.google.android.mms.ContentType;
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public static @Nullable ThumbnailData generateThumbnail(Context context, MasterSecret masterSecret, String contentType, Uri uri)
|
2016-01-30 23:22:55 +00:00
|
|
|
throws BitmapDecodingException
|
2014-12-30 09:36:51 +00:00
|
|
|
{
|
|
|
|
long startMillis = System.currentTimeMillis();
|
2015-10-13 01:25:05 +00:00
|
|
|
ThumbnailData data = null;
|
|
|
|
|
|
|
|
if (ContentType.isImageType(contentType)) {
|
|
|
|
data = new ThumbnailData(generateImageThumbnail(context, masterSecret, uri));
|
|
|
|
}
|
2014-12-30 09:36:51 +00:00
|
|
|
|
|
|
|
if (data != null) {
|
|
|
|
Log.w(TAG, String.format("generated thumbnail for part, %dx%d (%.3f:1) in %dms",
|
|
|
|
data.getBitmap().getWidth(), data.getBitmap().getHeight(),
|
|
|
|
data.getAspectRatio(), System.currentTimeMillis() - startMillis));
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Bitmap generateImageThumbnail(Context context, MasterSecret masterSecret, Uri uri)
|
2016-01-30 23:22:55 +00:00
|
|
|
throws BitmapDecodingException
|
2014-12-30 09:36:51 +00:00
|
|
|
{
|
2016-12-14 18:21:14 +00:00
|
|
|
try {
|
|
|
|
int maxSize = context.getResources().getDimensionPixelSize(R.dimen.media_bubble_height);
|
|
|
|
return Glide.with(context)
|
|
|
|
.load(new DecryptableUri(masterSecret, uri))
|
|
|
|
.asBitmap()
|
|
|
|
.centerCrop()
|
|
|
|
.into(maxSize, maxSize)
|
|
|
|
.get();
|
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
throw new BitmapDecodingException(e);
|
|
|
|
}
|
2014-12-30 09:36:51 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
} else if (ContentType.isImageType(attachment.getContentType())) {
|
|
|
|
slide = new ImageSlide(context, attachment);
|
|
|
|
} else if (ContentType.isVideoType(attachment.getContentType())) {
|
|
|
|
slide = new VideoSlide(context, attachment);
|
|
|
|
} else if (ContentType.isAudioType(attachment.getContentType())) {
|
|
|
|
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":
|
|
|
|
return MimeTypeMap.getSingleton().hasMimeType(ContentType.IMAGE_JPEG)
|
|
|
|
? ContentType.IMAGE_JPEG
|
|
|
|
: mimeType;
|
|
|
|
default:
|
|
|
|
return mimeType;
|
|
|
|
}
|
2015-07-15 20:42:59 +00:00
|
|
|
}
|
|
|
|
|
2015-09-05 00:33:22 +00:00
|
|
|
public static long getMediaSize(Context context, MasterSecret masterSecret, Uri uri) throws IOException {
|
2015-10-13 01:25:05 +00:00
|
|
|
InputStream in = PartAuthority.getAttachmentStream(context, masterSecret, 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;
|
|
|
|
}
|
|
|
|
|
2017-01-20 23:26:17 +00:00
|
|
|
public static boolean isMms(String contentType) {
|
|
|
|
return !TextUtils.isEmpty(contentType) && contentType.trim().equals("application/mms");
|
|
|
|
}
|
|
|
|
|
2015-07-24 21:05:48 +00:00
|
|
|
public static boolean isGif(String contentType) {
|
2015-07-15 20:42:59 +00:00
|
|
|
return !TextUtils.isEmpty(contentType) && contentType.trim().equals("image/gif");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
public static boolean isImage(Attachment attachment) {
|
|
|
|
return ContentType.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) {
|
|
|
|
return ContentType.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) {
|
|
|
|
return ContentType.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-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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|