session-android/src/org/thoughtcrime/securesms/mms/MediaConstraints.java

84 lines
3.6 KiB
Java
Raw Normal View History

2015-01-02 23:43:28 +00:00
package org.thoughtcrime.securesms.mms;
import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
2018-08-01 15:09:24 +00:00
import org.thoughtcrime.securesms.logging.Log;
2015-01-02 23:43:28 +00:00
import android.util.Pair;
import org.thoughtcrime.securesms.attachments.Attachment;
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
import org.thoughtcrime.securesms.util.BitmapDecodingException;
2015-01-02 23:43:28 +00:00
import org.thoughtcrime.securesms.util.BitmapUtil;
import org.thoughtcrime.securesms.util.MediaUtil;
import java.io.ByteArrayInputStream;
2015-01-02 23:43:28 +00:00
import java.io.IOException;
import java.io.InputStream;
public abstract class MediaConstraints {
private static final String TAG = MediaConstraints.class.getSimpleName();
public static MediaConstraints getPushMediaConstraints() {
return new PushMediaConstraints();
}
public static MediaConstraints getMmsMediaConstraints(int subscriptionId) {
return new MmsMediaConstraints(subscriptionId);
}
2015-01-02 23:43:28 +00:00
public abstract int getImageMaxWidth(Context context);
public abstract int getImageMaxHeight(Context context);
public abstract int getImageMaxSize(Context context);
2015-01-02 23:43:28 +00:00
public abstract int getGifMaxSize(Context context);
public abstract int getVideoMaxSize(Context context);
public abstract int getAudioMaxSize(Context context);
public abstract int getDocumentMaxSize(Context context);
public boolean isSatisfied(@NonNull Context context, @NonNull Attachment attachment) {
2015-01-02 23:43:28 +00:00
try {
return (MediaUtil.isGif(attachment) && attachment.getSize() <= getGifMaxSize(context) && isWithinBounds(context, attachment.getDataUri())) ||
(MediaUtil.isImage(attachment) && attachment.getSize() <= getImageMaxSize(context) && isWithinBounds(context, attachment.getDataUri())) ||
(MediaUtil.isAudio(attachment) && attachment.getSize() <= getAudioMaxSize(context)) ||
(MediaUtil.isVideo(attachment) && attachment.getSize() <= getVideoMaxSize(context)) ||
(MediaUtil.isFile(attachment) && attachment.getSize() <= getDocumentMaxSize(context));
2015-01-02 23:43:28 +00:00
} catch (IOException ioe) {
Log.w(TAG, "Failed to determine if media's constraints are satisfied.", ioe);
return false;
}
}
private boolean isWithinBounds(Context context, Uri uri) throws IOException {
try {
InputStream is = PartAuthority.getAttachmentStream(context, uri);
Pair<Integer, Integer> dimensions = BitmapUtil.getDimensions(is);
return dimensions.first > 0 && dimensions.first <= getImageMaxWidth(context) &&
dimensions.second > 0 && dimensions.second <= getImageMaxHeight(context);
} catch (BitmapDecodingException e) {
throw new IOException(e);
}
2015-01-02 23:43:28 +00:00
}
public boolean canResize(@Nullable Attachment attachment) {
return attachment != null && MediaUtil.isImage(attachment) && !MediaUtil.isGif(attachment);
2015-01-02 23:43:28 +00:00
}
public MediaStream getResizedMedia(@NonNull Context context, @NonNull Attachment attachment)
2015-01-02 23:43:28 +00:00
throws IOException
{
if (!canResize(attachment)) {
2015-01-02 23:43:28 +00:00
throw new UnsupportedOperationException("Cannot resize this content type");
}
2015-01-02 23:43:28 +00:00
try {
// XXX - This is loading everything into memory! We want the send path to be stream-like.
BitmapUtil.ScaleResult scaleResult = BitmapUtil.createScaledBytes(context, new DecryptableUri(attachment.getDataUri()), this);
return new MediaStream(new ByteArrayInputStream(scaleResult.getBitmap()), MediaUtil.IMAGE_JPEG, scaleResult.getWidth(), scaleResult.getHeight());
} catch (BitmapDecodingException e) {
throw new IOException(e);
2015-01-02 23:43:28 +00:00
}
}
}