2014-12-30 09:36:51 +00:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.net.Uri;
|
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
|
|
|
|
|
|
|
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;
|
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;
|
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;
|
2014-12-30 09:36:51 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2015-07-25 00:07:33 +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 {
|
|
|
|
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)
|
2015-07-25 00:07:33 +00:00
|
|
|
throws ExecutionException
|
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)
|
2015-07-25 00:07:33 +00:00
|
|
|
throws ExecutionException
|
2014-12-30 09:36:51 +00:00
|
|
|
{
|
2015-07-25 00:07:33 +00:00
|
|
|
int maxSize = context.getResources().getDimensionPixelSize(R.dimen.media_bubble_height);
|
|
|
|
return BitmapUtil.createScaledBitmap(context, new DecryptableUri(masterSecret, uri), maxSize, maxSize);
|
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);
|
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) {
|
2015-07-15 20:42:59 +00:00
|
|
|
String type = context.getContentResolver().getType(uri);
|
|
|
|
if (type == null) {
|
|
|
|
final String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
|
|
|
|
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|