2015-01-02 15:43:28 -08:00
|
|
|
package org.thoughtcrime.securesms.mms;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.net.Uri;
|
2015-10-12 18:25:05 -07:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2015-01-02 15:43:28 -08:00
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Pair;
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
2015-01-02 15:43:28 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2015-07-24 17:07:33 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
|
2016-01-30 15:22:55 -08:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
2015-01-02 15:43:28 -08:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapUtil;
|
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
import java.io.ByteArrayInputStream;
|
2015-01-02 15:43:28 -08:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
public abstract class MediaConstraints {
|
|
|
|
private static final String TAG = MediaConstraints.class.getSimpleName();
|
|
|
|
|
2017-05-08 15:32:59 -07:00
|
|
|
public static MediaConstraints getPushMediaConstraints() {
|
|
|
|
return new PushMediaConstraints();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static MediaConstraints getMmsMediaConstraints(int subscriptionId) {
|
|
|
|
return new MmsMediaConstraints(subscriptionId);
|
|
|
|
}
|
2015-01-02 15:43:28 -08:00
|
|
|
|
2015-06-04 14:19:10 -07:00
|
|
|
public abstract int getImageMaxWidth(Context context);
|
|
|
|
public abstract int getImageMaxHeight(Context context);
|
2017-05-08 15:32:59 -07:00
|
|
|
public abstract int getImageMaxSize(Context context);
|
2015-01-02 15:43:28 -08:00
|
|
|
|
2017-05-08 15:32:59 -07: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);
|
2017-04-24 15:52:48 -07:00
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
public boolean isSatisfied(@NonNull Context context, @NonNull MasterSecret masterSecret, @NonNull Attachment attachment) {
|
2015-01-02 15:43:28 -08:00
|
|
|
try {
|
2017-05-08 15:32:59 -07:00
|
|
|
return (MediaUtil.isGif(attachment) && attachment.getSize() <= getGifMaxSize(context) && isWithinBounds(context, masterSecret, attachment.getDataUri())) ||
|
|
|
|
(MediaUtil.isImage(attachment) && attachment.getSize() <= getImageMaxSize(context) && isWithinBounds(context, masterSecret, 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 15:43:28 -08:00
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, "Failed to determine if media's constraints are satisfied.", ioe);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 10:21:14 -08:00
|
|
|
private boolean isWithinBounds(Context context, MasterSecret masterSecret, Uri uri) throws IOException {
|
2016-01-30 15:22:55 -08:00
|
|
|
try {
|
|
|
|
InputStream is = PartAuthority.getAttachmentStream(context, masterSecret, 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 15:43:28 -08:00
|
|
|
}
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
public boolean canResize(@Nullable Attachment attachment) {
|
|
|
|
return attachment != null && MediaUtil.isImage(attachment) && !MediaUtil.isGif(attachment);
|
2015-01-02 15:43:28 -08:00
|
|
|
}
|
|
|
|
|
2015-11-23 13:18:54 +09:00
|
|
|
public MediaStream getResizedMedia(@NonNull Context context,
|
2015-10-12 18:25:05 -07:00
|
|
|
@NonNull MasterSecret masterSecret,
|
|
|
|
@NonNull Attachment attachment)
|
2015-01-02 15:43:28 -08:00
|
|
|
throws IOException
|
|
|
|
{
|
2015-10-12 18:25:05 -07:00
|
|
|
if (!canResize(attachment)) {
|
2015-01-02 15:43:28 -08:00
|
|
|
throw new UnsupportedOperationException("Cannot resize this content type");
|
|
|
|
}
|
2015-10-12 18:25:05 -07:00
|
|
|
|
2015-01-02 15:43:28 -08:00
|
|
|
try {
|
2015-10-12 18:25:05 -07:00
|
|
|
// XXX - This is loading everything into memory! We want the send path to be stream-like.
|
2015-11-23 13:18:54 +09:00
|
|
|
return new MediaStream(new ByteArrayInputStream(BitmapUtil.createScaledBytes(context, new DecryptableUri(masterSecret, attachment.getDataUri()), this)),
|
2017-05-08 15:32:59 -07:00
|
|
|
MediaUtil.IMAGE_JPEG);
|
2016-01-30 15:22:55 -08:00
|
|
|
} catch (BitmapDecodingException e) {
|
|
|
|
throw new IOException(e);
|
2015-01-02 15:43:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|