Refactor ImageManager to ImageHelper.

This commit is contained in:
akwizgran
2019-01-15 17:14:57 +00:00
parent 9557afabc6
commit 986d884b40
5 changed files with 68 additions and 111 deletions

View File

@@ -13,6 +13,7 @@ import org.briarproject.bramble.api.db.DatabaseExecutor;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
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.AttachmentHeader;
import org.briarproject.briar.api.messaging.MessagingManager;
@@ -48,7 +49,7 @@ class AttachmentController {
private static final int READ_LIMIT = 1024 * 8192;
private final MessagingManager messagingManager;
private final ImageManager imageManager;
private final ImageHelper imageHelper;
private final int defaultSize;
private final int minWidth, maxWidth;
private final int minHeight, maxHeight;
@@ -57,9 +58,9 @@ class AttachmentController {
new ConcurrentHashMap<>();
AttachmentController(MessagingManager messagingManager,
AttachmentDimensions dimensions, ImageManager imageManager) {
AttachmentDimensions dimensions, ImageHelper imageHelper) {
this.messagingManager = messagingManager;
this.imageManager = imageManager;
this.imageHelper = imageHelper;
defaultSize = dimensions.defaultSize;
minWidth = dimensions.minWidth;
maxWidth = dimensions.maxWidth;
@@ -69,10 +70,16 @@ class AttachmentController {
AttachmentController(MessagingManager messagingManager,
AttachmentDimensions dimensions) {
this(messagingManager, dimensions, new ImageManager() {
this(messagingManager, dimensions, new ImageHelper() {
@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);
String mimeType = options.outMimeType;
if (mimeType == null) mimeType = "";
return new DecodeResult(options.outWidth, options.outHeight,
mimeType);
}
@Nullable
@@ -133,7 +140,7 @@ class AttachmentController {
MessageId messageId = h.getMessageId();
if (!needsSize) {
String mimeType = h.getContentType();
String extension = imageManager.getExtensionFromMimeType(mimeType);
String extension = imageHelper.getExtensionFromMimeType(mimeType);
boolean hasError = false;
if (extension == null) {
extension = "";
@@ -176,7 +183,7 @@ class AttachmentController {
getThumbnailSize(size.width, size.height, size.mimeType);
}
// get file extension
String extension = imageManager.getExtensionFromMimeType(size.mimeType);
String extension = imageHelper.getExtensionFromMimeType(size.mimeType);
boolean hasError = extension == null || size.error;
if (extension == null) extension = "";
return new AttachmentItem(messageId, size.width, size.height,
@@ -208,13 +215,9 @@ class AttachmentController {
* Gets the size of any image {@link InputStream}.
*/
private Size getSizeFromBitmap(InputStream is) {
Options options = new Options();
options.inJustDecodeBounds = true;
imageManager.decodeStream(is, options);
if (options.outWidth < 1 || options.outHeight < 1)
return new Size();
return new Size(options.outWidth, options.outHeight,
options.outMimeType);
DecodeResult result = imageHelper.decodeStream(is);
if (result.width < 1 || result.height < 1) return new Size();
return new Size(result.width, result.height, result.mimeType);
}
private Size getThumbnailSize(int width, int height, String mimeType) {

View File

@@ -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;
}
}
}

View File

@@ -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);
}

View File

@@ -2,6 +2,7 @@ package org.briarproject.briar.android.conversation;
import org.briarproject.bramble.api.sync.MessageId;
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.AttachmentHeader;
import org.briarproject.briar.api.messaging.MessagingManager;
@@ -10,6 +11,7 @@ import org.junit.Test;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
import static org.briarproject.bramble.test.TestUtils.getRandomId;
@@ -29,12 +31,12 @@ public class AttachmentControllerTest extends BrambleMockTestCase {
private final MessagingManager messagingManager =
context.mock(MessagingManager.class);
private final ImageManager imageManager = context.mock(ImageManager.class);
private final ImageHelper imageHelper = context.mock(ImageHelper.class);
private final AttachmentController controller =
new AttachmentController(
messagingManager,
dimensions,
imageManager
imageHelper
);
@Test
@@ -43,7 +45,7 @@ public class AttachmentControllerTest extends BrambleMockTestCase {
AttachmentHeader h = getAttachmentHeader(mimeType);
context.checking(new Expectations() {{
oneOf(imageManager).getExtensionFromMimeType(mimeType);
oneOf(imageHelper).getExtensionFromMimeType(mimeType);
will(returnValue("jpg"));
}});
@@ -60,7 +62,7 @@ public class AttachmentControllerTest extends BrambleMockTestCase {
AttachmentHeader h = getAttachmentHeader(mimeType);
context.checking(new Expectations() {{
oneOf(imageManager).getExtensionFromMimeType(mimeType);
oneOf(imageHelper).getExtensionFromMimeType(mimeType);
will(returnValue(null));
}});
@@ -74,13 +76,10 @@ public class AttachmentControllerTest extends BrambleMockTestCase {
String mimeType = "image/jpeg";
AttachmentHeader h = getAttachmentHeader(mimeType);
context.checking(new BitmapDecoderExpectations() {{
withDecodeStream(imageManager, options -> {
options.outWidth = 160;
options.outHeight = 240;
options.outMimeType = mimeType;
});
oneOf(imageManager).getExtensionFromMimeType(mimeType);
context.checking(new Expectations() {{
oneOf(imageHelper).decodeStream(with(any(InputStream.class)));
will(returnValue(new DecodeResult(160, 240, mimeType)));
oneOf(imageHelper).getExtensionFromMimeType(mimeType);
will(returnValue("jpg"));
}});
@@ -99,13 +98,10 @@ public class AttachmentControllerTest extends BrambleMockTestCase {
public void testImageHealsWrongMimeType() {
AttachmentHeader h = getAttachmentHeader("image/png");
context.checking(new BitmapDecoderExpectations() {{
withDecodeStream(imageManager, options -> {
options.outWidth = 160;
options.outHeight = 240;
options.outMimeType = "image/jpeg";
});
oneOf(imageManager).getExtensionFromMimeType("image/jpeg");
context.checking(new Expectations() {{
oneOf(imageHelper).decodeStream(with(any(InputStream.class)));
will(returnValue(new DecodeResult(160, 240, "image/jpeg")));
oneOf(imageHelper).getExtensionFromMimeType("image/jpeg");
will(returnValue("jpg"));
}});
@@ -120,13 +116,10 @@ public class AttachmentControllerTest extends BrambleMockTestCase {
String mimeType = "image/jpeg";
AttachmentHeader h = getAttachmentHeader(mimeType);
context.checking(new BitmapDecoderExpectations() {{
withDecodeStream(imageManager, options -> {
options.outWidth = 1728;
options.outHeight = 2592;
options.outMimeType = mimeType;
});
oneOf(imageManager).getExtensionFromMimeType(mimeType);
context.checking(new Expectations() {{
oneOf(imageHelper).decodeStream(with(any(InputStream.class)));
will(returnValue(new DecodeResult(1728, 2592, mimeType)));
oneOf(imageHelper).getExtensionFromMimeType(mimeType);
will(returnValue("jpg"));
}});
@@ -142,12 +135,10 @@ public class AttachmentControllerTest extends BrambleMockTestCase {
public void testMalformedError() {
AttachmentHeader h = getAttachmentHeader("image/jpeg");
context.checking(new BitmapDecoderExpectations() {{
withDecodeStream(imageManager, options -> {
options.outWidth = 0;
options.outHeight = 0;
});
oneOf(imageManager).getExtensionFromMimeType("");
context.checking(new Expectations() {{
oneOf(imageHelper).decodeStream(with(any(InputStream.class)));
will(returnValue(new DecodeResult(0, 0, "")));
oneOf(imageHelper).getExtensionFromMimeType("");
will(returnValue(null));
}});

View File

@@ -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);
}
}