session-android/src/org/thoughtcrime/securesms/util/MediaUtil.java
Jake McGinty 551274f167 Large attachment support
Closes #4019
// FREEBIE
2015-09-17 12:20:25 -07:00

160 lines
5.3 KiB
Java

package org.thoughtcrime.securesms.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;
import com.bumptech.glide.Glide;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.mms.AudioSlide;
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
import org.thoughtcrime.securesms.mms.GifSlide;
import org.thoughtcrime.securesms.mms.ImageSlide;
import org.thoughtcrime.securesms.mms.PartAuthority;
import org.thoughtcrime.securesms.mms.Slide;
import org.thoughtcrime.securesms.mms.VideoSlide;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;
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 ExecutionException
{
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;
}
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();
}
private static Bitmap generateImageThumbnail(Context context, MasterSecret masterSecret, Uri uri)
throws ExecutionException
{
int maxSize = context.getResources().getDimensionPixelSize(R.dimen.media_bubble_height);
return BitmapUtil.createScaledBitmap(context, new DecryptableUri(masterSecret, uri), maxSize, maxSize);
}
public static Slide getSlideForPart(Context context, PduPart part, String contentType) {
Slide slide = null;
if (isGif(contentType)) {
slide = new GifSlide(context, part);
} else if (ContentType.isImageType(contentType)) {
slide = new ImageSlide(context, part);
} else if (ContentType.isVideoType(contentType)) {
slide = new VideoSlide(context, part);
} else if (ContentType.isAudioType(contentType)) {
slide = new AudioSlide(context, part);
}
return slide;
}
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;
}
public static long getMediaSize(Context context, MasterSecret masterSecret, Uri uri) throws IOException {
InputStream in = PartAuthority.getPartStream(context, masterSecret, uri);
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;
}
public static boolean isGif(String contentType) {
return !TextUtils.isEmpty(contentType) && contentType.trim().equals("image/gif");
}
public static boolean isGif(PduPart part) {
return isGif(Util.toIsoString(part.getContentType()));
}
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()));
}
public static @Nullable String getDiscreteMimeType(@NonNull PduPart part) {
return getDiscreteMimeType(Util.toIsoString(part.getContentType()));
}
public static @Nullable String getDiscreteMimeType(@NonNull String mimeType) {
final String[] sections = mimeType.split("/", 2);
return sections.length > 1 ? sections[0] : null;
}
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() {
return BitmapUtil.toCompressedJpeg(bitmap);
}
}
}