mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 20:59:54 +01:00
Refactor ImageManager to ImageHelper.
This commit is contained in:
@@ -13,6 +13,7 @@ import org.briarproject.bramble.api.db.DatabaseExecutor;
|
|||||||
import org.briarproject.bramble.api.db.DbException;
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.sync.MessageId;
|
import org.briarproject.bramble.api.sync.MessageId;
|
||||||
|
import org.briarproject.briar.android.conversation.ImageHelper.DecodeResult;
|
||||||
import org.briarproject.briar.api.messaging.Attachment;
|
import org.briarproject.briar.api.messaging.Attachment;
|
||||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||||
@@ -48,7 +49,7 @@ class AttachmentController {
|
|||||||
private static final int READ_LIMIT = 1024 * 8192;
|
private static final int READ_LIMIT = 1024 * 8192;
|
||||||
|
|
||||||
private final MessagingManager messagingManager;
|
private final MessagingManager messagingManager;
|
||||||
private final ImageManager imageManager;
|
private final ImageHelper imageHelper;
|
||||||
private final int defaultSize;
|
private final int defaultSize;
|
||||||
private final int minWidth, maxWidth;
|
private final int minWidth, maxWidth;
|
||||||
private final int minHeight, maxHeight;
|
private final int minHeight, maxHeight;
|
||||||
@@ -57,9 +58,9 @@ class AttachmentController {
|
|||||||
new ConcurrentHashMap<>();
|
new ConcurrentHashMap<>();
|
||||||
|
|
||||||
AttachmentController(MessagingManager messagingManager,
|
AttachmentController(MessagingManager messagingManager,
|
||||||
AttachmentDimensions dimensions, ImageManager imageManager) {
|
AttachmentDimensions dimensions, ImageHelper imageHelper) {
|
||||||
this.messagingManager = messagingManager;
|
this.messagingManager = messagingManager;
|
||||||
this.imageManager = imageManager;
|
this.imageHelper = imageHelper;
|
||||||
defaultSize = dimensions.defaultSize;
|
defaultSize = dimensions.defaultSize;
|
||||||
minWidth = dimensions.minWidth;
|
minWidth = dimensions.minWidth;
|
||||||
maxWidth = dimensions.maxWidth;
|
maxWidth = dimensions.maxWidth;
|
||||||
@@ -69,10 +70,16 @@ class AttachmentController {
|
|||||||
|
|
||||||
AttachmentController(MessagingManager messagingManager,
|
AttachmentController(MessagingManager messagingManager,
|
||||||
AttachmentDimensions dimensions) {
|
AttachmentDimensions dimensions) {
|
||||||
this(messagingManager, dimensions, new ImageManager() {
|
this(messagingManager, dimensions, new ImageHelper() {
|
||||||
@Override
|
@Override
|
||||||
public void decodeStream(InputStream is, Options options) {
|
public DecodeResult decodeStream(InputStream is) {
|
||||||
|
Options options = new Options();
|
||||||
|
options.inJustDecodeBounds = true;
|
||||||
BitmapFactory.decodeStream(is, null, options);
|
BitmapFactory.decodeStream(is, null, options);
|
||||||
|
String mimeType = options.outMimeType;
|
||||||
|
if (mimeType == null) mimeType = "";
|
||||||
|
return new DecodeResult(options.outWidth, options.outHeight,
|
||||||
|
mimeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -133,7 +140,7 @@ class AttachmentController {
|
|||||||
MessageId messageId = h.getMessageId();
|
MessageId messageId = h.getMessageId();
|
||||||
if (!needsSize) {
|
if (!needsSize) {
|
||||||
String mimeType = h.getContentType();
|
String mimeType = h.getContentType();
|
||||||
String extension = imageManager.getExtensionFromMimeType(mimeType);
|
String extension = imageHelper.getExtensionFromMimeType(mimeType);
|
||||||
boolean hasError = false;
|
boolean hasError = false;
|
||||||
if (extension == null) {
|
if (extension == null) {
|
||||||
extension = "";
|
extension = "";
|
||||||
@@ -176,7 +183,7 @@ class AttachmentController {
|
|||||||
getThumbnailSize(size.width, size.height, size.mimeType);
|
getThumbnailSize(size.width, size.height, size.mimeType);
|
||||||
}
|
}
|
||||||
// get file extension
|
// get file extension
|
||||||
String extension = imageManager.getExtensionFromMimeType(size.mimeType);
|
String extension = imageHelper.getExtensionFromMimeType(size.mimeType);
|
||||||
boolean hasError = extension == null || size.error;
|
boolean hasError = extension == null || size.error;
|
||||||
if (extension == null) extension = "";
|
if (extension == null) extension = "";
|
||||||
return new AttachmentItem(messageId, size.width, size.height,
|
return new AttachmentItem(messageId, size.width, size.height,
|
||||||
@@ -208,13 +215,9 @@ class AttachmentController {
|
|||||||
* Gets the size of any image {@link InputStream}.
|
* Gets the size of any image {@link InputStream}.
|
||||||
*/
|
*/
|
||||||
private Size getSizeFromBitmap(InputStream is) {
|
private Size getSizeFromBitmap(InputStream is) {
|
||||||
Options options = new Options();
|
DecodeResult result = imageHelper.decodeStream(is);
|
||||||
options.inJustDecodeBounds = true;
|
if (result.width < 1 || result.height < 1) return new Size();
|
||||||
imageManager.decodeStream(is, options);
|
return new Size(result.width, result.height, result.mimeType);
|
||||||
if (options.outWidth < 1 || options.outHeight < 1)
|
|
||||||
return new Size();
|
|
||||||
return new Size(options.outWidth, options.outHeight,
|
|
||||||
options.outMimeType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Size getThumbnailSize(int width, int height, String mimeType) {
|
private Size getThumbnailSize(int width, int height, String mimeType) {
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package org.briarproject.briar.android.conversation;
|
||||||
|
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
|
interface ImageHelper {
|
||||||
|
|
||||||
|
DecodeResult decodeStream(InputStream is);
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
String getExtensionFromMimeType(String mimeType);
|
||||||
|
|
||||||
|
class DecodeResult {
|
||||||
|
|
||||||
|
final int width;
|
||||||
|
final int height;
|
||||||
|
final String mimeType;
|
||||||
|
|
||||||
|
DecodeResult(int width, int height, String mimeType) {
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
this.mimeType = mimeType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
package org.briarproject.briar.android.conversation;
|
|
||||||
|
|
||||||
import android.graphics.BitmapFactory;
|
|
||||||
import android.support.annotation.Nullable;
|
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
@NotNullByDefault
|
|
||||||
interface ImageManager {
|
|
||||||
|
|
||||||
void decodeStream(InputStream is, BitmapFactory.Options options);
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
String getExtensionFromMimeType(String mimeType);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -2,6 +2,7 @@ package org.briarproject.briar.android.conversation;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.sync.MessageId;
|
import org.briarproject.bramble.api.sync.MessageId;
|
||||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||||
|
import org.briarproject.briar.android.conversation.ImageHelper.DecodeResult;
|
||||||
import org.briarproject.briar.api.messaging.Attachment;
|
import org.briarproject.briar.api.messaging.Attachment;
|
||||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||||
@@ -10,6 +11,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||||
@@ -29,12 +31,12 @@ public class AttachmentControllerTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
private final MessagingManager messagingManager =
|
private final MessagingManager messagingManager =
|
||||||
context.mock(MessagingManager.class);
|
context.mock(MessagingManager.class);
|
||||||
private final ImageManager imageManager = context.mock(ImageManager.class);
|
private final ImageHelper imageHelper = context.mock(ImageHelper.class);
|
||||||
private final AttachmentController controller =
|
private final AttachmentController controller =
|
||||||
new AttachmentController(
|
new AttachmentController(
|
||||||
messagingManager,
|
messagingManager,
|
||||||
dimensions,
|
dimensions,
|
||||||
imageManager
|
imageHelper
|
||||||
);
|
);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -43,7 +45,7 @@ public class AttachmentControllerTest extends BrambleMockTestCase {
|
|||||||
AttachmentHeader h = getAttachmentHeader(mimeType);
|
AttachmentHeader h = getAttachmentHeader(mimeType);
|
||||||
|
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(imageManager).getExtensionFromMimeType(mimeType);
|
oneOf(imageHelper).getExtensionFromMimeType(mimeType);
|
||||||
will(returnValue("jpg"));
|
will(returnValue("jpg"));
|
||||||
}});
|
}});
|
||||||
|
|
||||||
@@ -60,7 +62,7 @@ public class AttachmentControllerTest extends BrambleMockTestCase {
|
|||||||
AttachmentHeader h = getAttachmentHeader(mimeType);
|
AttachmentHeader h = getAttachmentHeader(mimeType);
|
||||||
|
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
oneOf(imageManager).getExtensionFromMimeType(mimeType);
|
oneOf(imageHelper).getExtensionFromMimeType(mimeType);
|
||||||
will(returnValue(null));
|
will(returnValue(null));
|
||||||
}});
|
}});
|
||||||
|
|
||||||
@@ -74,13 +76,10 @@ public class AttachmentControllerTest extends BrambleMockTestCase {
|
|||||||
String mimeType = "image/jpeg";
|
String mimeType = "image/jpeg";
|
||||||
AttachmentHeader h = getAttachmentHeader(mimeType);
|
AttachmentHeader h = getAttachmentHeader(mimeType);
|
||||||
|
|
||||||
context.checking(new BitmapDecoderExpectations() {{
|
context.checking(new Expectations() {{
|
||||||
withDecodeStream(imageManager, options -> {
|
oneOf(imageHelper).decodeStream(with(any(InputStream.class)));
|
||||||
options.outWidth = 160;
|
will(returnValue(new DecodeResult(160, 240, mimeType)));
|
||||||
options.outHeight = 240;
|
oneOf(imageHelper).getExtensionFromMimeType(mimeType);
|
||||||
options.outMimeType = mimeType;
|
|
||||||
});
|
|
||||||
oneOf(imageManager).getExtensionFromMimeType(mimeType);
|
|
||||||
will(returnValue("jpg"));
|
will(returnValue("jpg"));
|
||||||
}});
|
}});
|
||||||
|
|
||||||
@@ -99,13 +98,10 @@ public class AttachmentControllerTest extends BrambleMockTestCase {
|
|||||||
public void testImageHealsWrongMimeType() {
|
public void testImageHealsWrongMimeType() {
|
||||||
AttachmentHeader h = getAttachmentHeader("image/png");
|
AttachmentHeader h = getAttachmentHeader("image/png");
|
||||||
|
|
||||||
context.checking(new BitmapDecoderExpectations() {{
|
context.checking(new Expectations() {{
|
||||||
withDecodeStream(imageManager, options -> {
|
oneOf(imageHelper).decodeStream(with(any(InputStream.class)));
|
||||||
options.outWidth = 160;
|
will(returnValue(new DecodeResult(160, 240, "image/jpeg")));
|
||||||
options.outHeight = 240;
|
oneOf(imageHelper).getExtensionFromMimeType("image/jpeg");
|
||||||
options.outMimeType = "image/jpeg";
|
|
||||||
});
|
|
||||||
oneOf(imageManager).getExtensionFromMimeType("image/jpeg");
|
|
||||||
will(returnValue("jpg"));
|
will(returnValue("jpg"));
|
||||||
}});
|
}});
|
||||||
|
|
||||||
@@ -120,13 +116,10 @@ public class AttachmentControllerTest extends BrambleMockTestCase {
|
|||||||
String mimeType = "image/jpeg";
|
String mimeType = "image/jpeg";
|
||||||
AttachmentHeader h = getAttachmentHeader(mimeType);
|
AttachmentHeader h = getAttachmentHeader(mimeType);
|
||||||
|
|
||||||
context.checking(new BitmapDecoderExpectations() {{
|
context.checking(new Expectations() {{
|
||||||
withDecodeStream(imageManager, options -> {
|
oneOf(imageHelper).decodeStream(with(any(InputStream.class)));
|
||||||
options.outWidth = 1728;
|
will(returnValue(new DecodeResult(1728, 2592, mimeType)));
|
||||||
options.outHeight = 2592;
|
oneOf(imageHelper).getExtensionFromMimeType(mimeType);
|
||||||
options.outMimeType = mimeType;
|
|
||||||
});
|
|
||||||
oneOf(imageManager).getExtensionFromMimeType(mimeType);
|
|
||||||
will(returnValue("jpg"));
|
will(returnValue("jpg"));
|
||||||
}});
|
}});
|
||||||
|
|
||||||
@@ -142,12 +135,10 @@ public class AttachmentControllerTest extends BrambleMockTestCase {
|
|||||||
public void testMalformedError() {
|
public void testMalformedError() {
|
||||||
AttachmentHeader h = getAttachmentHeader("image/jpeg");
|
AttachmentHeader h = getAttachmentHeader("image/jpeg");
|
||||||
|
|
||||||
context.checking(new BitmapDecoderExpectations() {{
|
context.checking(new Expectations() {{
|
||||||
withDecodeStream(imageManager, options -> {
|
oneOf(imageHelper).decodeStream(with(any(InputStream.class)));
|
||||||
options.outWidth = 0;
|
will(returnValue(new DecodeResult(0, 0, "")));
|
||||||
options.outHeight = 0;
|
oneOf(imageHelper).getExtensionFromMimeType("");
|
||||||
});
|
|
||||||
oneOf(imageManager).getExtensionFromMimeType("");
|
|
||||||
will(returnValue(null));
|
will(returnValue(null));
|
||||||
}});
|
}});
|
||||||
|
|
||||||
|
|||||||
@@ -1,48 +0,0 @@
|
|||||||
package org.briarproject.briar.android.conversation;
|
|
||||||
|
|
||||||
import android.graphics.BitmapFactory.Options;
|
|
||||||
import android.support.annotation.Nullable;
|
|
||||||
|
|
||||||
import org.hamcrest.Description;
|
|
||||||
import org.jmock.Expectations;
|
|
||||||
import org.jmock.api.Action;
|
|
||||||
import org.jmock.api.Invocation;
|
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
class BitmapDecoderExpectations extends Expectations {
|
|
||||||
|
|
||||||
protected void withDecodeStream(ImageManager imageManager,
|
|
||||||
OptionsModifier optionsModifier) {
|
|
||||||
oneOf(imageManager).decodeStream(with(any(InputStream.class)),
|
|
||||||
with(any(Options.class)));
|
|
||||||
currentBuilder().setAction(new BitmapDecoderAction(optionsModifier));
|
|
||||||
}
|
|
||||||
|
|
||||||
private class BitmapDecoderAction implements Action {
|
|
||||||
|
|
||||||
private final OptionsModifier optionsModifier;
|
|
||||||
|
|
||||||
private BitmapDecoderAction(OptionsModifier optionsModifier) {
|
|
||||||
this.optionsModifier = optionsModifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public Object invoke(Invocation invocation) {
|
|
||||||
Options options = (Options) invocation.getParameter(1);
|
|
||||||
optionsModifier.modifyOptions(options);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void describeTo(Description description) {
|
|
||||||
description.appendText("decodes a Bitmap InputStream");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
interface OptionsModifier {
|
|
||||||
void modifyOptions(Options options);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user