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-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;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2015-01-19 02:11:30 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.AudioSlide;
|
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
|
|
|
|
2015-01-02 23:43:28 +00:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2014-12-30 09:36:51 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import ws.com.google.android.mms.ContentType;
|
|
|
|
import ws.com.google.android.mms.pdu.PduPart;
|
|
|
|
|
|
|
|
public class MediaUtil {
|
|
|
|
private static final String TAG = MediaUtil.class.getSimpleName();
|
|
|
|
|
|
|
|
public static ThumbnailData generateThumbnail(Context context, MasterSecret masterSecret, Uri uri, String type)
|
|
|
|
throws IOException, BitmapDecodingException, OutOfMemoryError
|
|
|
|
{
|
|
|
|
long startMillis = System.currentTimeMillis();
|
|
|
|
ThumbnailData data;
|
|
|
|
if (ContentType.isImageType(type)) data = new ThumbnailData(generateImageThumbnail(context, masterSecret, uri));
|
|
|
|
else data = null;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-02 23:43:28 +00:00
|
|
|
public static byte[] getPartData(Context context, MasterSecret masterSecret, PduPart part)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
ByteArrayOutputStream os = part.getDataSize() > 0 && part.getDataSize() < Integer.MAX_VALUE
|
|
|
|
? new ByteArrayOutputStream((int) part.getDataSize())
|
|
|
|
: new ByteArrayOutputStream();
|
|
|
|
Util.copy(PartAuthority.getPartStream(context, masterSecret, part.getDataUri()), os);
|
|
|
|
return os.toByteArray();
|
|
|
|
}
|
|
|
|
|
2014-12-30 09:36:51 +00:00
|
|
|
private static Bitmap generateImageThumbnail(Context context, MasterSecret masterSecret, Uri uri)
|
|
|
|
throws IOException, BitmapDecodingException, OutOfMemoryError
|
|
|
|
{
|
|
|
|
int maxSize = context.getResources().getDimensionPixelSize(R.dimen.thumbnail_max_size);
|
|
|
|
return BitmapUtil.createScaledBitmap(context, masterSecret, uri, maxSize, maxSize);
|
|
|
|
}
|
|
|
|
|
2015-01-19 02:11:30 +00:00
|
|
|
public static Slide getSlideForPart(Context context, MasterSecret masterSecret, PduPart part, String contentType) {
|
|
|
|
Slide slide = null;
|
2015-07-15 20:42:59 +00:00
|
|
|
if (isGif(contentType)) {
|
|
|
|
slide = new GifSlide(context, masterSecret, part);
|
|
|
|
} else if (ContentType.isImageType(contentType)) {
|
2015-01-19 02:11:30 +00:00
|
|
|
slide = new ImageSlide(context, masterSecret, part);
|
|
|
|
} else if (ContentType.isVideoType(contentType)) {
|
|
|
|
slide = new VideoSlide(context, masterSecret, part);
|
|
|
|
} else if (ContentType.isAudioType(contentType)) {
|
|
|
|
slide = new AudioSlide(context, masterSecret, part);
|
|
|
|
}
|
|
|
|
|
|
|
|
return slide;
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:42:59 +00:00
|
|
|
public static String getMimeType(Context context, Uri uri) {
|
|
|
|
String type = context.getContentResolver().getType(uri);
|
|
|
|
if (type == null) {
|
|
|
|
final String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
|
|
|
|
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isGif(PduPart part) {
|
|
|
|
return isGif(Util.toIsoString(part.getContentType()));
|
|
|
|
}
|
|
|
|
|
2015-01-02 23:43:28 +00:00
|
|
|
public static boolean isImage(PduPart part) {
|
|
|
|
return ContentType.isImageType(Util.toIsoString(part.getContentType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isAudio(PduPart part) {
|
|
|
|
return ContentType.isAudioType(Util.toIsoString(part.getContentType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isVideo(PduPart part) {
|
|
|
|
return ContentType.isVideoType(Util.toIsoString(part.getContentType()));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|