Remove support for GIF attachments on API < 24.

This commit is contained in:
akwizgran
2019-12-11 17:48:51 +00:00
parent 1000512c5b
commit 1e2ccd96a7
5 changed files with 63 additions and 39 deletions

View File

@@ -8,12 +8,12 @@ import android.provider.Settings;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static android.content.Context.MODE_PRIVATE;
import static android.os.Build.VERSION.SDK_INT;
import static java.util.Arrays.asList;
public class AndroidUtils {
@@ -26,7 +26,7 @@ public class AndroidUtils {
public static Collection<String> getSupportedArchitectures() {
List<String> abis = new ArrayList<>();
if (SDK_INT >= 21) {
abis.addAll(Arrays.asList(Build.SUPPORTED_ABIS));
abis.addAll(asList(Build.SUPPORTED_ABIS));
} else {
abis.add(Build.CPU_ABI);
if (Build.CPU_ABI2 != null) abis.add(Build.CPU_ABI2);
@@ -57,4 +57,15 @@ public class AndroidUtils {
public static File getReportDir(Context ctx) {
return ctx.getDir(STORED_REPORTS, MODE_PRIVATE);
}
/**
* Returns an array of supported content types for image attachments.
* GIFs can't be compressed on API < 24 so they're not supported.
* <p>
* TODO: Remove this restriction when large message support is added
*/
public static String[] getSupportedImageContentTypes() {
if (SDK_INT < 24) return new String[] {"image/jpeg", "image/png"};
else return new String[] {"image/jpeg", "image/png", "image/gif"};
}
}

View File

@@ -11,9 +11,11 @@ import java.util.logging.Logger;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import static android.os.Build.VERSION.SDK_INT;
import static androidx.test.InstrumentationRegistry.getContext;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static java.util.logging.Logger.getLogger;
import static org.junit.Assume.assumeTrue;
@RunWith(AndroidJUnit4.class)
public class AttachmentCreationTaskTest {
@@ -30,30 +32,55 @@ public class AttachmentCreationTaskTest {
imageSizeCalculator, null, null, true);
@Test
public void testCompress() throws Exception {
InputStream is = getAssetInputStream("animated.gif");
task.compressImage(is, "image/gif");
public void testCompress100x100Png() throws Exception {
testCompress("red-100x100.png", "image/png");
}
is = getAssetInputStream("animated2.gif");
task.compressImage(is, "image/gif");
@Test
public void testCompress500x500Png() throws Exception {
testCompress("blue-500x500.png", "image/png");
}
is = getAssetInputStream("blue-500x500.png");
task.compressImage(is, "image/png");
@Test
public void testCompress1000x2000Png() throws Exception {
testCompress("green-1000x2000.png", "image/png");
}
is = getAssetInputStream("error_high.jpg");
task.compressImage(is, "image/jpeg");
@Test
public void testCompressVeryHighJpg() throws Exception {
testCompress("error_high.jpg", "image/jpeg");
}
is = getAssetInputStream("error_large.gif");
task.compressImage(is, "image/gif");
@Test
public void testCompressVeryWideJpg() throws Exception {
testCompress("error_wide.jpg", "image/jpeg");
}
is = getAssetInputStream("error_wide.jpg");
task.compressImage(is, "image/jpeg");
@Test
public void testCompressAnimatedGif1() throws Exception {
// TODO: Remove this assumption when we support large messages
assumeTrue(SDK_INT >= 24);
testCompress("animated.gif", "image/gif");
}
is = getAssetInputStream("green-1000x2000.png");
task.compressImage(is, "image/png");
@Test
public void testCompressAnimatedGif2() throws Exception {
// TODO: Remove this assumption when we support large messages
assumeTrue(SDK_INT >= 24);
testCompress("animated2.gif", "image/gif");
}
is = getAssetInputStream("red-100x100.png");
task.compressImage(is, "image/png");
@Test
public void testCompressVeryLargeGif() throws Exception {
// TODO: Remove this assumption when we support large messages
assumeTrue(SDK_INT >= 24);
testCompress("error_large.gif", "image/gif");
}
private void testCompress(String filename, String contentType)
throws Exception {
InputStream is = getAssetInputStream(filename);
task.compressImage(is, contentType);
}
@Test

View File

@@ -26,14 +26,15 @@ import androidx.annotation.VisibleForTesting;
import static android.graphics.Bitmap.CompressFormat.JPEG;
import static android.graphics.BitmapFactory.decodeStream;
import static java.util.Arrays.asList;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.util.AndroidUtils.getSupportedImageContentTypes;
import static org.briarproject.bramble.util.IoUtils.tryToClose;
import static org.briarproject.bramble.util.LogUtils.logDuration;
import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.bramble.util.LogUtils.now;
import static org.briarproject.briar.api.messaging.MessagingConstants.IMAGE_MIME_TYPES;
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_IMAGE_SIZE;
@NotNullByDefault
@@ -108,7 +109,7 @@ class AttachmentCreationTask {
long start = now();
String contentType = contentResolver.getType(uri);
if (contentType == null) throw new IOException("null content type");
if (!isValidMimeType(contentType)) {
if (!asList(getSupportedImageContentTypes()).contains(contentType)) {
String uriString = uri.toString();
throw new UnsupportedMimeTypeException("", contentType, uriString);
}
@@ -124,13 +125,6 @@ class AttachmentCreationTask {
return h;
}
private boolean isValidMimeType(String mimeType) {
for (String supportedType : IMAGE_MIME_TYPES) {
if (supportedType.equals(mimeType)) return true;
}
return false;
}
@VisibleForTesting
InputStream compressImage(InputStream is, String contentType)
throws IOException {

View File

@@ -40,8 +40,8 @@ import static android.widget.Toast.LENGTH_LONG;
import static androidx.core.content.ContextCompat.getColor;
import static androidx.customview.view.AbsSavedState.EMPTY_STATE;
import static androidx.lifecycle.Lifecycle.State.DESTROYED;
import static org.briarproject.bramble.util.AndroidUtils.getSupportedImageContentTypes;
import static org.briarproject.briar.android.util.UiUtils.resolveColorAttribute;
import static org.briarproject.briar.api.messaging.MessagingConstants.IMAGE_MIME_TYPES;
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_ATTACHMENTS_PER_MESSAGE;
@UiThread
@@ -131,7 +131,8 @@ public class TextAttachmentController extends TextSendController
ACTION_OPEN_DOCUMENT : ACTION_GET_CONTENT);
intent.setType("image/*");
intent.addCategory(CATEGORY_OPENABLE);
if (SDK_INT >= 19) intent.putExtra(EXTRA_MIME_TYPES, IMAGE_MIME_TYPES);
if (SDK_INT >= 19)
intent.putExtra(EXTRA_MIME_TYPES, getSupportedImageContentTypes());
if (SDK_INT >= 18) intent.putExtra(EXTRA_ALLOW_MULTIPLE, true);
return intent;
}

View File

@@ -19,15 +19,6 @@ public interface MessagingConstants {
*/
int MAX_CONTENT_TYPE_BYTES = 50;
/**
* The supported mime types for image attachments.
*/
String[] IMAGE_MIME_TYPES = {
"image/jpeg",
"image/png",
"image/gif",
};
/**
* The maximum allowed size of image attachments.
* TODO: Different limit for GIFs?