mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Compare commits
17 Commits
attachment
...
alpha-1.3.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4fd012c31a | ||
|
|
95d06770bf | ||
|
|
428247b7b2 | ||
|
|
a921361a56 | ||
|
|
fe7dfa721e | ||
|
|
92eb06a9e9 | ||
|
|
5beed1a748 | ||
|
|
774047d856 | ||
|
|
fc28e7aa88 | ||
|
|
78459499b2 | ||
|
|
c2973608d7 | ||
|
|
be1c33cb42 | ||
|
|
c955466bda | ||
|
|
593a0c4632 | ||
|
|
ed20b2d8d6 | ||
|
|
9ab9e02f8a | ||
|
|
3f70ae3c8c |
@@ -5,6 +5,10 @@
|
||||
|
||||
# QR codes
|
||||
-keep class com.google.zxing.Result
|
||||
-keepclassmembers enum * {
|
||||
public static **[] values();
|
||||
public static ** valueOf(java.lang.String);
|
||||
}
|
||||
|
||||
# RSS libraries
|
||||
-keep,includedescriptorclasses class com.rometools.rome.feed.synd.impl.** { *; }
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.briarproject.bramble.BrambleAndroidModule;
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.bramble.account.BriarAccountModule;
|
||||
import org.briarproject.briar.BriarCoreModule;
|
||||
import org.briarproject.briar.android.attachment.AttachmentModule;
|
||||
import org.briarproject.briar.android.navdrawer.NavDrawerActivityTest;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
@@ -13,6 +14,7 @@ import dagger.Component;
|
||||
@Singleton
|
||||
@Component(modules = {
|
||||
AppModule.class,
|
||||
AttachmentModule.class,
|
||||
BriarCoreModule.class,
|
||||
BrambleAndroidModule.class,
|
||||
BriarAccountModule.class,
|
||||
|
||||
@@ -47,15 +47,17 @@ public class AttachmentRetrieverIntegrationTest {
|
||||
);
|
||||
private final MessageId msgId = new MessageId(getRandomId());
|
||||
|
||||
private final ImageHelper imageHelper = new ImageHelperImpl();
|
||||
private final AttachmentRetriever retriever =
|
||||
new AttachmentRetriever(null, dimensions);
|
||||
new AttachmentRetrieverImpl(null, dimensions, imageHelper,
|
||||
new ImageSizeCalculator(imageHelper));
|
||||
|
||||
@Test
|
||||
public void testSmallJpegImage() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(smallKitten);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(msgId, item.getMessageId());
|
||||
assertEquals(160, item.getWidth());
|
||||
assertEquals(240, item.getHeight());
|
||||
@@ -70,8 +72,8 @@ public class AttachmentRetrieverIntegrationTest {
|
||||
public void testBigJpegImage() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(originalKitten);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(msgId, item.getMessageId());
|
||||
assertEquals(1728, item.getWidth());
|
||||
assertEquals(2592, item.getHeight());
|
||||
@@ -86,8 +88,8 @@ public class AttachmentRetrieverIntegrationTest {
|
||||
public void testSmallPngImage() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/png");
|
||||
InputStream is = getUrlInputStream(pngKitten);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(msgId, item.getMessageId());
|
||||
assertEquals(737, item.getWidth());
|
||||
assertEquals(510, item.getHeight());
|
||||
@@ -102,8 +104,8 @@ public class AttachmentRetrieverIntegrationTest {
|
||||
public void testUberGif() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(uberGif);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(1, item.getWidth());
|
||||
assertEquals(1, item.getHeight());
|
||||
assertEquals(dimensions.minHeight, item.getThumbnailWidth());
|
||||
@@ -117,8 +119,8 @@ public class AttachmentRetrieverIntegrationTest {
|
||||
public void testLottaPixels() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(lottaPixel);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(64250, item.getWidth());
|
||||
assertEquals(64250, item.getHeight());
|
||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||
@@ -132,8 +134,8 @@ public class AttachmentRetrieverIntegrationTest {
|
||||
public void testImageIoCrash() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(imageIoCrash);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(1184, item.getWidth());
|
||||
assertEquals(448, item.getHeight());
|
||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||
@@ -147,8 +149,8 @@ public class AttachmentRetrieverIntegrationTest {
|
||||
public void testGimpCrash() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(gimpCrash);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(1, item.getWidth());
|
||||
assertEquals(1, item.getHeight());
|
||||
assertEquals(dimensions.minHeight, item.getThumbnailWidth());
|
||||
@@ -162,8 +164,8 @@ public class AttachmentRetrieverIntegrationTest {
|
||||
public void testOptiPngAfl() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(optiPngAfl);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(32, item.getWidth());
|
||||
assertEquals(32, item.getHeight());
|
||||
assertEquals(dimensions.minHeight, item.getThumbnailWidth());
|
||||
@@ -177,8 +179,8 @@ public class AttachmentRetrieverIntegrationTest {
|
||||
public void testLibrawError() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getUrlInputStream(librawError);
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertTrue(item.hasError());
|
||||
}
|
||||
|
||||
@@ -186,8 +188,8 @@ public class AttachmentRetrieverIntegrationTest {
|
||||
public void testSmallAnimatedGifMaxDimensions() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
||||
InputStream is = getAssetInputStream("animated.gif");
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(65535, item.getWidth());
|
||||
assertEquals(65535, item.getHeight());
|
||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||
@@ -201,8 +203,8 @@ public class AttachmentRetrieverIntegrationTest {
|
||||
public void testSmallAnimatedGifHugeDimensions() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
||||
InputStream is = getAssetInputStream("animated2.gif");
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(10000, item.getWidth());
|
||||
assertEquals(10000, item.getHeight());
|
||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||
@@ -216,8 +218,8 @@ public class AttachmentRetrieverIntegrationTest {
|
||||
public void testSmallGifLargeDimensions() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/gif");
|
||||
InputStream is = getAssetInputStream("error_large.gif");
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(16384, item.getWidth());
|
||||
assertEquals(16384, item.getHeight());
|
||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||
@@ -231,8 +233,8 @@ public class AttachmentRetrieverIntegrationTest {
|
||||
public void testHighError() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getAssetInputStream("error_high.jpg");
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(1, item.getWidth());
|
||||
assertEquals(10000, item.getHeight());
|
||||
assertEquals(dimensions.minWidth, item.getThumbnailWidth());
|
||||
@@ -246,8 +248,8 @@ public class AttachmentRetrieverIntegrationTest {
|
||||
public void testWideError() throws Exception {
|
||||
AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg");
|
||||
InputStream is = getAssetInputStream("error_wide.jpg");
|
||||
Attachment a = new Attachment(is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(h, a, true);
|
||||
Attachment a = new Attachment(h, is);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, true);
|
||||
assertEquals(1920, item.getWidth());
|
||||
assertEquals(1, item.getHeight());
|
||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.briarproject.bramble.api.system.LocationUtils;
|
||||
import org.briarproject.bramble.plugin.tor.CircumventionProvider;
|
||||
import org.briarproject.briar.BriarCoreEagerSingletons;
|
||||
import org.briarproject.briar.BriarCoreModule;
|
||||
import org.briarproject.briar.android.attachment.AttachmentModule;
|
||||
import org.briarproject.briar.android.conversation.glide.BriarModelLoader;
|
||||
import org.briarproject.briar.android.login.SignInReminderReceiver;
|
||||
import org.briarproject.briar.android.reporting.BriarReportSender;
|
||||
@@ -68,7 +69,8 @@ import dagger.Component;
|
||||
BriarCoreModule.class,
|
||||
BrambleAndroidModule.class,
|
||||
BriarAccountModule.class,
|
||||
AppModule.class
|
||||
AppModule.class,
|
||||
AttachmentModule.class
|
||||
})
|
||||
public interface AndroidComponent
|
||||
extends BrambleCoreEagerSingletons, BrambleAndroidEagerSingletons,
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.briarproject.briar.android.account;
|
||||
import android.os.Bundle;
|
||||
import android.support.design.widget.TextInputEditText;
|
||||
import android.support.design.widget.TextInputLayout;
|
||||
import android.text.Editable;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@@ -10,7 +11,6 @@ import android.widget.Button;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
|
||||
@@ -20,6 +20,7 @@ import static android.view.inputmethod.EditorInfo.IME_ACTION_NEXT;
|
||||
import static android.view.inputmethod.EditorInfo.IME_ACTION_NONE;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||
import static org.briarproject.bramble.util.StringUtils.toUtf8;
|
||||
import static org.briarproject.briar.android.util.UiUtils.setError;
|
||||
import static org.briarproject.briar.android.util.UiUtils.showSoftKeyboard;
|
||||
|
||||
@@ -77,7 +78,7 @@ public class AuthorNameFragment extends SetupFragment {
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence authorName, int i, int i1, int i2) {
|
||||
int authorNameLength = StringUtils.toUtf8(authorName.toString()).length;
|
||||
int authorNameLength = toUtf8(authorName.toString().trim()).length;
|
||||
boolean error = authorNameLength > MAX_AUTHOR_NAME_LENGTH;
|
||||
setError(authorNameWrapper, getString(R.string.name_too_long), error);
|
||||
boolean enabled = authorNameLength > 0 && !error;
|
||||
@@ -89,8 +90,11 @@ public class AuthorNameFragment extends SetupFragment {
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
setupController.setAuthorName(authorNameInput.getText().toString());
|
||||
setupController.showPasswordFragment();
|
||||
Editable text = authorNameInput.getText();
|
||||
if (text != null) {
|
||||
setupController.setAuthorName(text.toString().trim());
|
||||
setupController.showPasswordFragment();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,32 +1,29 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.arch.lifecycle.MutableLiveData;
|
||||
import android.arch.lifecycle.Observer;
|
||||
import android.content.ContentResolver;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory.Options;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.briar.api.messaging.Attachment;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.jsoup.UnsupportedMimeTypeException;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static android.graphics.Bitmap.CompressFormat.JPEG;
|
||||
import static android.graphics.BitmapFactory.decodeStream;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
@@ -35,98 +32,71 @@ 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;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
class AttachmentCreationTask {
|
||||
|
||||
private static Logger LOG =
|
||||
getLogger(AttachmentCreationTask.class.getName());
|
||||
|
||||
private final Executor ioExecutor;
|
||||
private static final int MAX_ATTACHMENT_DIMENSION = 1000;
|
||||
|
||||
private final MessagingManager messagingManager;
|
||||
private final ContentResolver contentResolver;
|
||||
private final AttachmentRetriever retriever;
|
||||
private final ImageSizeCalculator imageSizeCalculator;
|
||||
private final GroupId groupId;
|
||||
private final Collection<Uri> uris;
|
||||
private final boolean needsSize;
|
||||
private final MutableLiveData<AttachmentResult> result;
|
||||
@Nullable
|
||||
private volatile AttachmentCreator attachmentCreator;
|
||||
|
||||
private volatile boolean canceled = false;
|
||||
|
||||
AttachmentCreationTask(Executor ioExecutor,
|
||||
MessagingManager messagingManager, ContentResolver contentResolver,
|
||||
AttachmentRetriever retriever, GroupId groupId,
|
||||
Collection<Uri> uris, boolean needsSize) {
|
||||
this.ioExecutor = ioExecutor;
|
||||
AttachmentCreationTask(MessagingManager messagingManager,
|
||||
ContentResolver contentResolver,
|
||||
AttachmentCreator attachmentCreator,
|
||||
ImageSizeCalculator imageSizeCalculator,
|
||||
GroupId groupId, Collection<Uri> uris, boolean needsSize) {
|
||||
this.messagingManager = messagingManager;
|
||||
this.contentResolver = contentResolver;
|
||||
this.retriever = retriever;
|
||||
this.imageSizeCalculator = imageSizeCalculator;
|
||||
this.groupId = groupId;
|
||||
this.uris = uris;
|
||||
this.needsSize = needsSize;
|
||||
result = new MutableLiveData<>();
|
||||
this.attachmentCreator = attachmentCreator;
|
||||
}
|
||||
|
||||
LiveData<AttachmentResult> getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the task, asynchronously waits for it to finish, and deletes any
|
||||
* created attachments.
|
||||
*/
|
||||
void cancel() {
|
||||
canceled = true;
|
||||
// Observe the task until it finishes (which may already have happened)
|
||||
result.observeForever(new Observer<AttachmentResult>() {
|
||||
@Override
|
||||
public void onChanged(@Nullable AttachmentResult attachmentResult) {
|
||||
requireNonNull(attachmentResult);
|
||||
if (attachmentResult.isFinished()) {
|
||||
deleteUnsentAttachments(attachmentResult.getItemResults());
|
||||
result.removeObserver(this);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously creates and stores the attachments.
|
||||
*/
|
||||
void storeAttachments() {
|
||||
ioExecutor.execute(() -> {
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Storing " + uris.size() + " attachments");
|
||||
List<AttachmentItemResult> results = new ArrayList<>();
|
||||
for (Uri uri : uris) {
|
||||
if (canceled) break;
|
||||
results.add(processUri(uri));
|
||||
result.postValue(new AttachmentResult(new ArrayList<>(results),
|
||||
false, false));
|
||||
}
|
||||
result.postValue(new AttachmentResult(new ArrayList<>(results),
|
||||
true, !canceled));
|
||||
});
|
||||
attachmentCreator = null;
|
||||
}
|
||||
|
||||
@IoExecutor
|
||||
private AttachmentItemResult processUri(Uri uri) {
|
||||
AttachmentHeader header = null;
|
||||
void storeAttachments() {
|
||||
for (Uri uri : uris) processUri(uri);
|
||||
AttachmentCreator attachmentCreator = this.attachmentCreator;
|
||||
if (!canceled && attachmentCreator != null)
|
||||
attachmentCreator.onAttachmentCreationFinished();
|
||||
this.attachmentCreator = null;
|
||||
}
|
||||
|
||||
@IoExecutor
|
||||
private void processUri(Uri uri) {
|
||||
if (canceled) return;
|
||||
try {
|
||||
header = storeAttachment(uri);
|
||||
Attachment a = retriever.getMessageAttachment(header);
|
||||
AttachmentItem item =
|
||||
retriever.getAttachmentItem(header, a, needsSize);
|
||||
if (item.hasError()) throw new IOException();
|
||||
if (needsSize) retriever.cachePut(item);
|
||||
return new AttachmentItemResult(uri, item);
|
||||
AttachmentHeader h = storeAttachment(uri);
|
||||
AttachmentCreator attachmentCreator = this.attachmentCreator;
|
||||
if (attachmentCreator != null) {
|
||||
attachmentCreator.onAttachmentHeaderReceived(uri, h, needsSize);
|
||||
}
|
||||
} catch (DbException | IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
// If the attachment was already stored, delete it
|
||||
tryToRemove(header);
|
||||
AttachmentCreator attachmentCreator = this.attachmentCreator;
|
||||
if (attachmentCreator != null) {
|
||||
attachmentCreator.onAttachmentError(uri, e);
|
||||
}
|
||||
canceled = true;
|
||||
return new AttachmentItemResult(uri, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,6 +112,8 @@ class AttachmentCreationTask {
|
||||
}
|
||||
InputStream is = contentResolver.openInputStream(uri);
|
||||
if (is == null) throw new IOException();
|
||||
is = compressImage(is, contentType);
|
||||
contentType = "image/jpeg";
|
||||
long timestamp = System.currentTimeMillis();
|
||||
AttachmentHeader h = messagingManager
|
||||
.addLocalAttachment(groupId, timestamp, contentType, is);
|
||||
@@ -157,31 +129,48 @@ class AttachmentCreationTask {
|
||||
return false;
|
||||
}
|
||||
|
||||
private void tryToRemove(@Nullable AttachmentHeader h) {
|
||||
private InputStream compressImage(InputStream is, String contentType)
|
||||
throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try {
|
||||
if (h != null) messagingManager.removeAttachment(h);
|
||||
} catch (DbException e1) {
|
||||
logException(LOG, WARNING, e1);
|
||||
Bitmap bitmap = createBitmap(is, contentType);
|
||||
for (int quality = 100; quality >= 0; quality -= 10) {
|
||||
if (!bitmap.compress(JPEG, quality, out))
|
||||
throw new IOException();
|
||||
if (out.size() <= MAX_IMAGE_SIZE) {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Compressed image to "
|
||||
+ out.size() + " bytes, quality " + quality);
|
||||
}
|
||||
return new ByteArrayInputStream(out.toByteArray());
|
||||
}
|
||||
out.reset();
|
||||
}
|
||||
throw new IOException();
|
||||
} finally {
|
||||
tryToClose(is, LOG, WARNING);
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteUnsentAttachments(
|
||||
Collection<AttachmentItemResult> itemResults) {
|
||||
List<AttachmentHeader> headers = new ArrayList<>(itemResults.size());
|
||||
for (AttachmentItemResult itemResult : itemResults) {
|
||||
AttachmentItem item = itemResult.getItem();
|
||||
if (item != null) headers.add(item.getHeader());
|
||||
private Bitmap createBitmap(InputStream is, String contentType)
|
||||
throws IOException {
|
||||
is = new BufferedInputStream(is);
|
||||
Size size = imageSizeCalculator.getSize(is, contentType);
|
||||
if (size.error) throw new IOException();
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Original image size: " + size.width + "x" + size.height);
|
||||
int dimension = Math.max(size.width, size.height);
|
||||
int inSampleSize = 1;
|
||||
while (dimension > MAX_ATTACHMENT_DIMENSION) {
|
||||
inSampleSize *= 2;
|
||||
dimension /= 2;
|
||||
}
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Deleting " + headers.size() + " unsent attachments");
|
||||
ioExecutor.execute(() -> {
|
||||
for (AttachmentHeader header : headers) {
|
||||
try {
|
||||
messagingManager.removeAttachment(header);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
});
|
||||
LOG.info("Scaling attachment by factor of " + inSampleSize);
|
||||
Options options = new Options();
|
||||
options.inSampleSize = inSampleSize;
|
||||
Bitmap bitmap = decodeStream(is, null, options);
|
||||
if (bitmap == null) throw new IOException();
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +1,24 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.app.Application;
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.UiThread;
|
||||
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
|
||||
@NotNullByDefault
|
||||
public class AttachmentCreator {
|
||||
public interface AttachmentCreator {
|
||||
|
||||
private final Application app;
|
||||
@IoExecutor
|
||||
private final Executor ioExecutor;
|
||||
private final MessagingManager messagingManager;
|
||||
private final AttachmentRetriever retriever;
|
||||
|
||||
@Nullable
|
||||
private AttachmentCreationTask task;
|
||||
|
||||
public AttachmentCreator(Application app, @IoExecutor Executor ioExecutor,
|
||||
MessagingManager messagingManager, AttachmentRetriever retriever) {
|
||||
this.app = app;
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.messagingManager = messagingManager;
|
||||
this.retriever = retriever;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a background task to create attachments from the given URIs and
|
||||
* returns a LiveData to monitor the progress of the task.
|
||||
*/
|
||||
@UiThread
|
||||
public LiveData<AttachmentResult> storeAttachments(GroupId groupId,
|
||||
Collection<Uri> uris) {
|
||||
if (task != null) throw new IllegalStateException();
|
||||
boolean needsSize = uris.size() == 1;
|
||||
task = new AttachmentCreationTask(ioExecutor, messagingManager,
|
||||
app.getContentResolver(), retriever, groupId, uris, needsSize);
|
||||
task.storeAttachments();
|
||||
return task.getResult();
|
||||
}
|
||||
LiveData<AttachmentResult> storeAttachments(LiveData<GroupId> groupId,
|
||||
Collection<Uri> newUris);
|
||||
|
||||
/**
|
||||
* This should be only called after configuration changes.
|
||||
@@ -60,50 +26,35 @@ public class AttachmentCreator {
|
||||
* They are already being created and returned by the existing LiveData.
|
||||
*/
|
||||
@UiThread
|
||||
public LiveData<AttachmentResult> getLiveAttachments() {
|
||||
if (task == null) throw new IllegalStateException();
|
||||
// A task is already running. It will update the result LiveData.
|
||||
// So nothing more to do here.
|
||||
return task.getResult();
|
||||
}
|
||||
LiveData<AttachmentResult> getLiveAttachments();
|
||||
|
||||
@UiThread
|
||||
List<AttachmentHeader> getAttachmentHeadersForSending();
|
||||
|
||||
/**
|
||||
* Returns the headers of any attachments created by
|
||||
* {@link #storeAttachments(GroupId, Collection)}, unless
|
||||
* {@link #onAttachmentsSent()} or {@link #cancel()} has been called.
|
||||
* Marks the attachments as sent and adds the items to the cache for display
|
||||
*
|
||||
* @param id The MessageId of the sent message.
|
||||
*/
|
||||
@UiThread
|
||||
public List<AttachmentHeader> getAttachmentHeadersForSending() {
|
||||
if (task == null) return emptyList();
|
||||
AttachmentResult result = task.getResult().getValue();
|
||||
if (result == null) return emptyList();
|
||||
List<AttachmentHeader> headers = new ArrayList<>();
|
||||
for (AttachmentItemResult itemResult : result.getItemResults()) {
|
||||
AttachmentItem item = itemResult.getItem();
|
||||
if (item != null) headers.add(item.getHeader());
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
void onAttachmentsSent(MessageId id);
|
||||
|
||||
/**
|
||||
* Informs the AttachmentCreator that the attachments created by
|
||||
* {@link #storeAttachments(GroupId, Collection)} will be sent.
|
||||
* Needs to be called when created attachments will not be sent anymore.
|
||||
*/
|
||||
@UiThread
|
||||
public void onAttachmentsSent() {
|
||||
task = null; // Prevent cancel() from cancelling the task
|
||||
}
|
||||
void cancel();
|
||||
|
||||
/**
|
||||
* Cancels the task started by
|
||||
* {@link #storeAttachments(GroupId, Collection)}, if any, unless
|
||||
* {@link #onAttachmentsSent()} has been called.
|
||||
*/
|
||||
@UiThread
|
||||
public void cancel() {
|
||||
if (task != null) {
|
||||
task.cancel();
|
||||
task = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
void deleteUnsentAttachments();
|
||||
|
||||
@IoExecutor
|
||||
void onAttachmentHeaderReceived(Uri uri, AttachmentHeader h,
|
||||
boolean needsSize);
|
||||
|
||||
@IoExecutor
|
||||
void onAttachmentError(Uri uri, Throwable t);
|
||||
|
||||
@IoExecutor
|
||||
void onAttachmentCreationFinished();
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
|
||||
import android.app.Application;
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.arch.lifecycle.MutableLiveData;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.UiThread;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.api.messaging.Attachment;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.FileTooBigException;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.jsoup.UnsupportedMimeTypeException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.briar.android.util.UiUtils.observeForeverOnce;
|
||||
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_IMAGE_SIZE;
|
||||
|
||||
@NotNullByDefault
|
||||
class AttachmentCreatorImpl implements AttachmentCreator {
|
||||
|
||||
private static Logger LOG =
|
||||
getLogger(AttachmentCreatorImpl.class.getName());
|
||||
|
||||
private final Application app;
|
||||
@IoExecutor
|
||||
private final Executor ioExecutor;
|
||||
private final MessagingManager messagingManager;
|
||||
private final AttachmentRetriever retriever;
|
||||
private final ImageSizeCalculator imageSizeCalculator;
|
||||
|
||||
private final CopyOnWriteArrayList<Uri> uris = new CopyOnWriteArrayList<>();
|
||||
private final CopyOnWriteArrayList<AttachmentItemResult> itemResults =
|
||||
new CopyOnWriteArrayList<>();
|
||||
|
||||
@Nullable
|
||||
private AttachmentCreationTask task;
|
||||
|
||||
@Nullable
|
||||
private volatile MutableLiveData<AttachmentResult> result;
|
||||
|
||||
@Inject
|
||||
AttachmentCreatorImpl(Application app, @IoExecutor Executor ioExecutor,
|
||||
MessagingManager messagingManager, AttachmentRetriever retriever,
|
||||
ImageSizeCalculator imageSizeCalculator) {
|
||||
this.app = app;
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.messagingManager = messagingManager;
|
||||
this.retriever = retriever;
|
||||
this.imageSizeCalculator = imageSizeCalculator;
|
||||
}
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public LiveData<AttachmentResult> storeAttachments(
|
||||
LiveData<GroupId> groupId, Collection<Uri> newUris) {
|
||||
if (task != null || result != null || !uris.isEmpty())
|
||||
throw new IllegalStateException();
|
||||
MutableLiveData<AttachmentResult> result = new MutableLiveData<>();
|
||||
this.result = result;
|
||||
uris.addAll(newUris);
|
||||
observeForeverOnce(groupId, id -> {
|
||||
if (id == null) throw new IllegalStateException();
|
||||
boolean needsSize = uris.size() == 1;
|
||||
task = new AttachmentCreationTask(messagingManager,
|
||||
app.getContentResolver(), this, imageSizeCalculator, id,
|
||||
uris, needsSize);
|
||||
ioExecutor.execute(() -> task.storeAttachments());
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public LiveData<AttachmentResult> getLiveAttachments() {
|
||||
MutableLiveData<AttachmentResult> result = this.result;
|
||||
if (task == null || result == null || uris.isEmpty())
|
||||
throw new IllegalStateException();
|
||||
// A task is already running. It will update the result LiveData.
|
||||
// So nothing more to do here.
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@IoExecutor
|
||||
public void onAttachmentHeaderReceived(Uri uri, AttachmentHeader h,
|
||||
boolean needsSize) {
|
||||
// get and cache AttachmentItem for ImagePreview
|
||||
try {
|
||||
Attachment a = retriever.getMessageAttachment(h);
|
||||
AttachmentItem item = retriever.getAttachmentItem(a, needsSize);
|
||||
if (item.hasError()) throw new IOException();
|
||||
AttachmentItemResult itemResult =
|
||||
new AttachmentItemResult(uri, item);
|
||||
itemResults.add(itemResult);
|
||||
MutableLiveData<AttachmentResult> result = this.result;
|
||||
if (result != null) result.postValue(getResult(false));
|
||||
} catch (IOException | DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
onAttachmentError(uri, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@IoExecutor
|
||||
public void onAttachmentError(Uri uri, Throwable t) {
|
||||
// get error message
|
||||
String errorMsg;
|
||||
if (t instanceof UnsupportedMimeTypeException) {
|
||||
String mimeType = ((UnsupportedMimeTypeException) t).getMimeType();
|
||||
errorMsg = app.getString(
|
||||
R.string.image_attach_error_invalid_mime_type, mimeType);
|
||||
} else if (t instanceof FileTooBigException) {
|
||||
int mb = MAX_IMAGE_SIZE / 1024 / 1024;
|
||||
errorMsg = app.getString(R.string.image_attach_error_too_big, mb);
|
||||
} else {
|
||||
errorMsg = null; // generic error
|
||||
}
|
||||
AttachmentItemResult itemResult =
|
||||
new AttachmentItemResult(uri, errorMsg);
|
||||
itemResults.add(itemResult);
|
||||
MutableLiveData<AttachmentResult> result = this.result;
|
||||
if (result != null) result.postValue(getResult(false));
|
||||
// expect to receive a cancel from the UI
|
||||
}
|
||||
|
||||
@Override
|
||||
@IoExecutor
|
||||
public void onAttachmentCreationFinished() {
|
||||
MutableLiveData<AttachmentResult> result = this.result;
|
||||
if (result != null) result.postValue(getResult(true));
|
||||
}
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public List<AttachmentHeader> getAttachmentHeadersForSending() {
|
||||
List<AttachmentHeader> headers = new ArrayList<>(itemResults.size());
|
||||
for (AttachmentItemResult itemResult : itemResults) {
|
||||
// check if we are trying to send attachment items with errors
|
||||
if (itemResult.getItem() == null) throw new IllegalStateException();
|
||||
headers.add(itemResult.getItem().getHeader());
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public void onAttachmentsSent(MessageId id) {
|
||||
List<AttachmentItem> items = new ArrayList<>(itemResults.size());
|
||||
for (AttachmentItemResult itemResult : itemResults) {
|
||||
// check if we are trying to send attachment items with errors
|
||||
if (itemResult.getItem() == null) throw new IllegalStateException();
|
||||
items.add(itemResult.getItem());
|
||||
}
|
||||
retriever.cachePut(id, items);
|
||||
resetState();
|
||||
}
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public void cancel() {
|
||||
if (task == null) throw new AssertionError();
|
||||
task.cancel();
|
||||
deleteUnsentAttachments();
|
||||
resetState();
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void resetState() {
|
||||
task = null;
|
||||
uris.clear();
|
||||
itemResults.clear();
|
||||
MutableLiveData<AttachmentResult> result = this.result;
|
||||
if (result != null) {
|
||||
result.setValue(null);
|
||||
this.result = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@UiThread
|
||||
public void deleteUnsentAttachments() {
|
||||
// Make a copy for the IoExecutor as we clear the itemResults soon
|
||||
List<AttachmentHeader> headers = new ArrayList<>(itemResults.size());
|
||||
for (AttachmentItemResult itemResult : itemResults) {
|
||||
// check if we are trying to send attachment items with errors
|
||||
if (itemResult.getItem() != null)
|
||||
headers.add(itemResult.getItem().getHeader());
|
||||
}
|
||||
ioExecutor.execute(() -> {
|
||||
for (AttachmentHeader header : headers) {
|
||||
try {
|
||||
messagingManager.removeAttachment(header);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private AttachmentResult getResult(boolean finished) {
|
||||
// Make a copy of the list,
|
||||
// because our copy will continue to change in the background.
|
||||
// (As it's a CopyOnWriteArrayList,
|
||||
// the code that receives the result can safely do simple things
|
||||
// like iterating over the list,
|
||||
// but anything that involves calling more than one list method
|
||||
// is still unsafe.)
|
||||
Collection<AttachmentItemResult> items = new ArrayList<>(itemResults);
|
||||
return new AttachmentResult(items, finished);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class AttachmentDimensions {
|
||||
class AttachmentDimensions {
|
||||
|
||||
final int defaultSize;
|
||||
final int minWidth, maxWidth;
|
||||
@@ -26,7 +26,7 @@ public class AttachmentDimensions {
|
||||
this.maxHeight = maxHeight;
|
||||
}
|
||||
|
||||
public static AttachmentDimensions getAttachmentDimensions(Resources res) {
|
||||
static AttachmentDimensions getAttachmentDimensions(Resources res) {
|
||||
int defaultSize =
|
||||
res.getDimensionPixelSize(R.dimen.message_bubble_image_default);
|
||||
int minWidth = res.getDimensionPixelSize(
|
||||
|
||||
@@ -68,7 +68,7 @@ public class AttachmentItem implements Parcelable {
|
||||
header = new AttachmentHeader(messageId, mimeType);
|
||||
}
|
||||
|
||||
AttachmentHeader getHeader() {
|
||||
public AttachmentHeader getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,18 +15,18 @@ public class AttachmentItemResult {
|
||||
@Nullable
|
||||
private final AttachmentItem item;
|
||||
@Nullable
|
||||
private final Exception exception;
|
||||
private final String errorMsg;
|
||||
|
||||
AttachmentItemResult(Uri uri, AttachmentItem item) {
|
||||
this.uri = uri;
|
||||
this.item = item;
|
||||
this.exception = null;
|
||||
this.errorMsg = null;
|
||||
}
|
||||
|
||||
AttachmentItemResult(Uri uri, Exception exception) {
|
||||
AttachmentItemResult(Uri uri, @Nullable String errorMsg) {
|
||||
this.uri = uri;
|
||||
this.item = null;
|
||||
this.exception = exception;
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
|
||||
public Uri getUri() {
|
||||
@@ -43,8 +43,8 @@ public class AttachmentItemResult {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Exception getException() {
|
||||
return exception;
|
||||
public String getErrorMsg() {
|
||||
return errorMsg;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,12 +4,14 @@ import android.arch.lifecycle.LiveData;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.UiThread;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@UiThread
|
||||
@NotNullByDefault
|
||||
public interface AttachmentManager {
|
||||
|
||||
LiveData<AttachmentResult> storeAttachments(Collection<Uri> uri,
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
import static org.briarproject.briar.android.attachment.AttachmentDimensions.getAttachmentDimensions;
|
||||
|
||||
@Module
|
||||
public class AttachmentModule {
|
||||
|
||||
@Provides
|
||||
ImageHelper provideImageHelper(ImageHelperImpl imageHelper) {
|
||||
return imageHelper;
|
||||
}
|
||||
|
||||
@Provides
|
||||
ImageSizeCalculator provideImageSizeCalculator(ImageHelper imageHelper) {
|
||||
return new ImageSizeCalculator(imageHelper);
|
||||
}
|
||||
|
||||
@Provides
|
||||
AttachmentDimensions provideAttachmentDimensions(Application app) {
|
||||
return getAttachmentDimensions(app.getResources());
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
AttachmentRetriever provideAttachmentRetriever(
|
||||
AttachmentRetrieverImpl attachmentRetriever) {
|
||||
return attachmentRetriever;
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
AttachmentCreator provideAttachmentCreator(
|
||||
AttachmentCreatorImpl attachmentCreator) {
|
||||
return attachmentCreator;
|
||||
}
|
||||
}
|
||||
@@ -12,13 +12,11 @@ public class AttachmentResult {
|
||||
|
||||
private final Collection<AttachmentItemResult> itemResults;
|
||||
private final boolean finished;
|
||||
private final boolean success;
|
||||
|
||||
AttachmentResult(Collection<AttachmentItemResult> itemResults,
|
||||
boolean finished, boolean success) {
|
||||
public AttachmentResult(Collection<AttachmentItemResult> itemResults,
|
||||
boolean finished) {
|
||||
this.itemResults = itemResults;
|
||||
this.finished = finished;
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public Collection<AttachmentItemResult> getItemResults() {
|
||||
@@ -29,7 +27,4 @@ public class AttachmentResult {
|
||||
return finished;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,276 +1,29 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.BitmapFactory.Options;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.media.ExifInterface;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import com.bumptech.glide.util.MarkEnforcingInputStream;
|
||||
|
||||
import org.briarproject.bramble.api.Pair;
|
||||
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.attachment.ImageHelper.DecodeResult;
|
||||
import org.briarproject.briar.api.messaging.Attachment;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static android.support.media.ExifInterface.ORIENTATION_ROTATE_270;
|
||||
import static android.support.media.ExifInterface.ORIENTATION_ROTATE_90;
|
||||
import static android.support.media.ExifInterface.ORIENTATION_TRANSPOSE;
|
||||
import static android.support.media.ExifInterface.ORIENTATION_TRANSVERSE;
|
||||
import static android.support.media.ExifInterface.TAG_IMAGE_LENGTH;
|
||||
import static android.support.media.ExifInterface.TAG_IMAGE_WIDTH;
|
||||
import static android.support.media.ExifInterface.TAG_ORIENTATION;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
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;
|
||||
|
||||
@NotNullByDefault
|
||||
public class AttachmentRetriever {
|
||||
public interface AttachmentRetriever {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(AttachmentRetriever.class.getName());
|
||||
|
||||
private static final int READ_LIMIT = 1024 * 8192;
|
||||
|
||||
private final MessagingManager messagingManager;
|
||||
private final ImageHelper imageHelper;
|
||||
private final int defaultSize;
|
||||
private final int minWidth, maxWidth;
|
||||
private final int minHeight, maxHeight;
|
||||
|
||||
private final Map<MessageId, AttachmentItem> attachmentCache =
|
||||
new ConcurrentHashMap<>();
|
||||
|
||||
@VisibleForTesting
|
||||
AttachmentRetriever(MessagingManager messagingManager,
|
||||
AttachmentDimensions dimensions, ImageHelper imageHelper) {
|
||||
this.messagingManager = messagingManager;
|
||||
this.imageHelper = imageHelper;
|
||||
defaultSize = dimensions.defaultSize;
|
||||
minWidth = dimensions.minWidth;
|
||||
maxWidth = dimensions.maxWidth;
|
||||
minHeight = dimensions.minHeight;
|
||||
maxHeight = dimensions.maxHeight;
|
||||
}
|
||||
|
||||
public AttachmentRetriever(MessagingManager messagingManager,
|
||||
AttachmentDimensions dimensions) {
|
||||
this(messagingManager, dimensions, new ImageHelper() {
|
||||
@Override
|
||||
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
|
||||
@Override
|
||||
public String getExtensionFromMimeType(String mimeType) {
|
||||
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
||||
return mimeTypeMap.getExtensionFromMimeType(mimeType);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void cachePut(AttachmentItem item) {
|
||||
attachmentCache.put(item.getMessageId(), item);
|
||||
}
|
||||
void cachePut(MessageId messageId, List<AttachmentItem> attachments);
|
||||
|
||||
@Nullable
|
||||
public AttachmentItem cacheGet(MessageId attachmentId) {
|
||||
return attachmentCache.get(attachmentId);
|
||||
}
|
||||
List<AttachmentItem> cacheGet(MessageId messageId);
|
||||
|
||||
@DatabaseExecutor
|
||||
public List<Pair<AttachmentHeader, Attachment>> getMessageAttachments(
|
||||
List<AttachmentHeader> headers) throws DbException {
|
||||
long start = now();
|
||||
List<Pair<AttachmentHeader, Attachment>> attachments =
|
||||
new ArrayList<>(headers.size());
|
||||
for (AttachmentHeader h : headers) {
|
||||
Attachment a = messagingManager.getAttachment(h.getMessageId());
|
||||
attachments.add(new Pair<>(h, a));
|
||||
}
|
||||
logDuration(LOG, "Loading attachments", start);
|
||||
return attachments;
|
||||
}
|
||||
|
||||
Attachment getMessageAttachment(AttachmentHeader h) throws DbException {
|
||||
return messagingManager.getAttachment(h.getMessageId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates {@link AttachmentItem}s from the passed headers and Attachments.
|
||||
* <p>
|
||||
* Note: This closes the {@link Attachment}'s {@link InputStream}.
|
||||
*/
|
||||
public List<AttachmentItem> getAttachmentItems(
|
||||
List<Pair<AttachmentHeader, Attachment>> attachments) {
|
||||
boolean needsSize = attachments.size() == 1;
|
||||
List<AttachmentItem> items = new ArrayList<>(attachments.size());
|
||||
for (Pair<AttachmentHeader, Attachment> a : attachments) {
|
||||
AttachmentItem item =
|
||||
getAttachmentItem(a.getFirst(), a.getSecond(), needsSize);
|
||||
items.add(item);
|
||||
}
|
||||
return items;
|
||||
}
|
||||
Attachment getMessageAttachment(AttachmentHeader h) throws DbException;
|
||||
|
||||
/**
|
||||
* Creates an {@link AttachmentItem} from the {@link Attachment}'s
|
||||
* {@link InputStream} which will be closed when this method returns.
|
||||
*/
|
||||
AttachmentItem getAttachmentItem(AttachmentHeader h, Attachment a,
|
||||
boolean needsSize) {
|
||||
if (!needsSize) {
|
||||
String extension =
|
||||
imageHelper.getExtensionFromMimeType(h.getContentType());
|
||||
boolean hasError = false;
|
||||
if (extension == null) {
|
||||
extension = "";
|
||||
hasError = true;
|
||||
}
|
||||
return new AttachmentItem(h, 0, 0, extension, 0, 0, hasError);
|
||||
}
|
||||
|
||||
Size size = new Size();
|
||||
InputStream is = new MarkEnforcingInputStream(
|
||||
new BufferedInputStream(a.getStream()));
|
||||
is.mark(READ_LIMIT);
|
||||
try {
|
||||
// use exif to get size
|
||||
if (h.getContentType().equals("image/jpeg")) {
|
||||
size = getSizeFromExif(is);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
try {
|
||||
// use BitmapFactory to get size
|
||||
if (size.error) {
|
||||
is.reset();
|
||||
// need to mark again to re-add read limit
|
||||
is.mark(READ_LIMIT);
|
||||
size = getSizeFromBitmap(is);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
} finally {
|
||||
tryToClose(is, LOG, WARNING);
|
||||
}
|
||||
|
||||
// calculate thumbnail size
|
||||
Size thumbnailSize = new Size(defaultSize, defaultSize, size.mimeType);
|
||||
if (!size.error) {
|
||||
thumbnailSize =
|
||||
getThumbnailSize(size.width, size.height, size.mimeType);
|
||||
}
|
||||
// get file extension
|
||||
String extension = imageHelper.getExtensionFromMimeType(size.mimeType);
|
||||
boolean hasError = extension == null || size.error;
|
||||
if (!h.getContentType().equals(size.mimeType)) {
|
||||
if (LOG.isLoggable(WARNING)) {
|
||||
LOG.warning("Header has different mime type (" +
|
||||
h.getContentType() + ") than image (" + size.mimeType +
|
||||
").");
|
||||
}
|
||||
hasError = true;
|
||||
}
|
||||
if (extension == null) extension = "";
|
||||
return new AttachmentItem(h, size.width, size.height, extension,
|
||||
thumbnailSize.width, thumbnailSize.height, hasError);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the size of a JPEG {@link InputStream} if EXIF info is available.
|
||||
*/
|
||||
private Size getSizeFromExif(InputStream is) throws IOException {
|
||||
ExifInterface exif = new ExifInterface(is);
|
||||
// these can return 0 independent of default value
|
||||
int width = exif.getAttributeInt(TAG_IMAGE_WIDTH, 0);
|
||||
int height = exif.getAttributeInt(TAG_IMAGE_LENGTH, 0);
|
||||
if (width == 0 || height == 0) return new Size();
|
||||
int orientation = exif.getAttributeInt(TAG_ORIENTATION, 0);
|
||||
if (orientation == ORIENTATION_ROTATE_90 ||
|
||||
orientation == ORIENTATION_ROTATE_270 ||
|
||||
orientation == ORIENTATION_TRANSVERSE ||
|
||||
orientation == ORIENTATION_TRANSPOSE) {
|
||||
//noinspection SuspiciousNameCombination
|
||||
return new Size(height, width, "image/jpeg");
|
||||
}
|
||||
return new Size(width, height, "image/jpeg");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the size of any image {@link InputStream}.
|
||||
*/
|
||||
private Size getSizeFromBitmap(InputStream is) {
|
||||
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) {
|
||||
float widthPercentage = maxWidth / (float) width;
|
||||
float heightPercentage = maxHeight / (float) height;
|
||||
float scaleFactor = Math.min(widthPercentage, heightPercentage);
|
||||
if (scaleFactor > 1) scaleFactor = 1f;
|
||||
int thumbnailWidth = (int) (width * scaleFactor);
|
||||
int thumbnailHeight = (int) (height * scaleFactor);
|
||||
if (thumbnailWidth < minWidth || thumbnailHeight < minHeight) {
|
||||
widthPercentage = minWidth / (float) width;
|
||||
heightPercentage = minHeight / (float) height;
|
||||
scaleFactor = Math.max(widthPercentage, heightPercentage);
|
||||
thumbnailWidth = (int) (width * scaleFactor);
|
||||
thumbnailHeight = (int) (height * scaleFactor);
|
||||
if (thumbnailWidth > maxWidth) thumbnailWidth = maxWidth;
|
||||
if (thumbnailHeight > maxHeight) thumbnailHeight = maxHeight;
|
||||
}
|
||||
return new Size(thumbnailWidth, thumbnailHeight, mimeType);
|
||||
}
|
||||
|
||||
private static class Size {
|
||||
|
||||
private final int width;
|
||||
private final int height;
|
||||
private final String mimeType;
|
||||
private final boolean error;
|
||||
|
||||
private Size(int width, int height, String mimeType) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.mimeType = mimeType;
|
||||
this.error = false;
|
||||
}
|
||||
|
||||
private Size() {
|
||||
this.width = 0;
|
||||
this.height = 0;
|
||||
this.mimeType = "";
|
||||
this.error = true;
|
||||
}
|
||||
}
|
||||
|
||||
AttachmentItem getAttachmentItem(Attachment a, boolean needsSize);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
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.api.messaging.Attachment;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
|
||||
@NotNullByDefault
|
||||
class AttachmentRetrieverImpl implements AttachmentRetriever {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(AttachmentRetrieverImpl.class.getName());
|
||||
|
||||
private final MessagingManager messagingManager;
|
||||
private final ImageHelper imageHelper;
|
||||
private final ImageSizeCalculator imageSizeCalculator;
|
||||
private final int defaultSize;
|
||||
private final int minWidth, maxWidth;
|
||||
private final int minHeight, maxHeight;
|
||||
|
||||
private final Map<MessageId, List<AttachmentItem>> attachmentCache =
|
||||
new ConcurrentHashMap<>();
|
||||
|
||||
@Inject
|
||||
AttachmentRetrieverImpl(MessagingManager messagingManager,
|
||||
AttachmentDimensions dimensions, ImageHelper imageHelper,
|
||||
ImageSizeCalculator imageSizeCalculator) {
|
||||
this.messagingManager = messagingManager;
|
||||
this.imageHelper = imageHelper;
|
||||
this.imageSizeCalculator = imageSizeCalculator;
|
||||
defaultSize = dimensions.defaultSize;
|
||||
minWidth = dimensions.minWidth;
|
||||
maxWidth = dimensions.maxWidth;
|
||||
minHeight = dimensions.minHeight;
|
||||
maxHeight = dimensions.maxHeight;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cachePut(MessageId messageId,
|
||||
List<AttachmentItem> attachments) {
|
||||
attachmentCache.put(messageId, attachments);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public List<AttachmentItem> cacheGet(MessageId messageId) {
|
||||
return attachmentCache.get(messageId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Attachment getMessageAttachment(AttachmentHeader h)
|
||||
throws DbException {
|
||||
return messagingManager.getAttachment(h);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AttachmentItem getAttachmentItem(Attachment a, boolean needsSize) {
|
||||
AttachmentHeader h = a.getHeader();
|
||||
if (!needsSize) {
|
||||
String extension =
|
||||
imageHelper.getExtensionFromMimeType(h.getContentType());
|
||||
boolean hasError = false;
|
||||
if (extension == null) {
|
||||
extension = "";
|
||||
hasError = true;
|
||||
}
|
||||
return new AttachmentItem(h, 0, 0, extension, 0, 0, hasError);
|
||||
}
|
||||
|
||||
InputStream is = new BufferedInputStream(a.getStream());
|
||||
Size size = imageSizeCalculator.getSize(is, h.getContentType());
|
||||
|
||||
// calculate thumbnail size
|
||||
Size thumbnailSize = new Size(defaultSize, defaultSize, size.mimeType);
|
||||
if (!size.error) {
|
||||
thumbnailSize =
|
||||
getThumbnailSize(size.width, size.height, size.mimeType);
|
||||
}
|
||||
// get file extension
|
||||
String extension = imageHelper.getExtensionFromMimeType(size.mimeType);
|
||||
boolean hasError = extension == null || size.error;
|
||||
if (!h.getContentType().equals(size.mimeType)) {
|
||||
if (LOG.isLoggable(WARNING)) {
|
||||
LOG.warning("Header has different mime type (" +
|
||||
h.getContentType() + ") than image (" + size.mimeType +
|
||||
").");
|
||||
}
|
||||
hasError = true;
|
||||
}
|
||||
if (extension == null) extension = "";
|
||||
return new AttachmentItem(h, size.width, size.height, extension,
|
||||
thumbnailSize.width, thumbnailSize.height, hasError);
|
||||
}
|
||||
|
||||
private Size getThumbnailSize(int width, int height, String mimeType) {
|
||||
float widthPercentage = maxWidth / (float) width;
|
||||
float heightPercentage = maxHeight / (float) height;
|
||||
float scaleFactor = Math.min(widthPercentage, heightPercentage);
|
||||
if (scaleFactor > 1) scaleFactor = 1f;
|
||||
int thumbnailWidth = (int) (width * scaleFactor);
|
||||
int thumbnailHeight = (int) (height * scaleFactor);
|
||||
if (thumbnailWidth < minWidth || thumbnailHeight < minHeight) {
|
||||
widthPercentage = minWidth / (float) width;
|
||||
heightPercentage = minHeight / (float) height;
|
||||
scaleFactor = Math.max(widthPercentage, heightPercentage);
|
||||
thumbnailWidth = (int) (width * scaleFactor);
|
||||
thumbnailHeight = (int) (height * scaleFactor);
|
||||
if (thumbnailWidth > maxWidth) thumbnailWidth = maxWidth;
|
||||
if (thumbnailHeight > maxHeight) thumbnailHeight = maxHeight;
|
||||
}
|
||||
return new Size(thumbnailWidth, thumbnailHeight, mimeType);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import java.io.InputStream;
|
||||
|
||||
@NotNullByDefault
|
||||
interface ImageHelper {
|
||||
public interface ImageHelper {
|
||||
|
||||
DecodeResult decodeStream(InputStream is);
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
class ImageHelperImpl implements ImageHelper {
|
||||
|
||||
@Inject
|
||||
ImageHelperImpl() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DecodeResult decodeStream(InputStream is) {
|
||||
BitmapFactory.Options options = new BitmapFactory.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
|
||||
@Override
|
||||
public String getExtensionFromMimeType(String mimeType) {
|
||||
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
||||
return mimeTypeMap.getExtensionFromMimeType(mimeType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
import android.support.media.ExifInterface;
|
||||
|
||||
import com.bumptech.glide.util.MarkEnforcingInputStream;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.android.attachment.ImageHelper.DecodeResult;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static android.support.media.ExifInterface.ORIENTATION_ROTATE_270;
|
||||
import static android.support.media.ExifInterface.ORIENTATION_ROTATE_90;
|
||||
import static android.support.media.ExifInterface.ORIENTATION_TRANSPOSE;
|
||||
import static android.support.media.ExifInterface.ORIENTATION_TRANSVERSE;
|
||||
import static android.support.media.ExifInterface.TAG_IMAGE_LENGTH;
|
||||
import static android.support.media.ExifInterface.TAG_IMAGE_WIDTH;
|
||||
import static android.support.media.ExifInterface.TAG_ORIENTATION;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@NotNullByDefault
|
||||
class ImageSizeCalculator {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(ImageSizeCalculator.class.getName());
|
||||
|
||||
private static final int READ_LIMIT = 1024 * 8192;
|
||||
|
||||
private final ImageHelper imageHelper;
|
||||
|
||||
ImageSizeCalculator(ImageHelper imageHelper) {
|
||||
this.imageHelper = imageHelper;
|
||||
}
|
||||
|
||||
Size getSize(InputStream is, String contentType) {
|
||||
Size size = new Size();
|
||||
is = new MarkEnforcingInputStream(is);
|
||||
is.mark(READ_LIMIT);
|
||||
if (contentType.equals("image/jpeg")) {
|
||||
try {
|
||||
// use exif to get size
|
||||
size = getSizeFromExif(is);
|
||||
is.reset();
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
if (size.error) {
|
||||
// need to mark again to re-add read limit
|
||||
is.mark(READ_LIMIT);
|
||||
try {
|
||||
// use BitmapFactory to get size
|
||||
size = getSizeFromBitmap(is);
|
||||
is.reset();
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the size of a JPEG {@link InputStream} if EXIF info is available.
|
||||
*/
|
||||
private Size getSizeFromExif(InputStream is) throws IOException {
|
||||
ExifInterface exif = new ExifInterface(is);
|
||||
// these can return 0 independent of default value
|
||||
int width = exif.getAttributeInt(TAG_IMAGE_WIDTH, 0);
|
||||
int height = exif.getAttributeInt(TAG_IMAGE_LENGTH, 0);
|
||||
if (width == 0 || height == 0) return new Size();
|
||||
int orientation = exif.getAttributeInt(TAG_ORIENTATION, 0);
|
||||
if (orientation == ORIENTATION_ROTATE_90 ||
|
||||
orientation == ORIENTATION_ROTATE_270 ||
|
||||
orientation == ORIENTATION_TRANSVERSE ||
|
||||
orientation == ORIENTATION_TRANSPOSE) {
|
||||
//noinspection SuspiciousNameCombination
|
||||
return new Size(height, width, "image/jpeg");
|
||||
}
|
||||
return new Size(width, height, "image/jpeg");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the size of any image {@link InputStream}.
|
||||
*/
|
||||
private Size getSizeFromBitmap(InputStream is) {
|
||||
DecodeResult result = imageHelper.decodeStream(is);
|
||||
if (result.width < 1 || result.height < 1) return new Size();
|
||||
return new Size(result.width, result.height, result.mimeType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.briarproject.briar.android.attachment;
|
||||
|
||||
class Size {
|
||||
|
||||
final int width;
|
||||
final int height;
|
||||
final String mimeType;
|
||||
final boolean error;
|
||||
|
||||
Size(int width, int height, String mimeType) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.mimeType = mimeType;
|
||||
this.error = false;
|
||||
}
|
||||
|
||||
Size() {
|
||||
this.width = 0;
|
||||
this.height = 0;
|
||||
this.mimeType = "";
|
||||
this.error = true;
|
||||
}
|
||||
}
|
||||
@@ -82,19 +82,20 @@ public class NicknameFragment extends BaseFragment {
|
||||
|
||||
@Nullable
|
||||
private String getNicknameOrNull() {
|
||||
Editable name = contactNameInput.getText();
|
||||
if (name == null || name.toString().trim().length() == 0) {
|
||||
Editable text = contactNameInput.getText();
|
||||
if (text == null || text.toString().trim().length() == 0) {
|
||||
contactNameLayout.setError(getString(R.string.nickname_missing));
|
||||
contactNameInput.requestFocus();
|
||||
return null;
|
||||
}
|
||||
if (utf8IsTooLong(name.toString(), MAX_AUTHOR_NAME_LENGTH)) {
|
||||
String name = text.toString().trim();
|
||||
if (utf8IsTooLong(name, MAX_AUTHOR_NAME_LENGTH)) {
|
||||
contactNameLayout.setError(getString(R.string.name_too_long));
|
||||
contactNameInput.requestFocus();
|
||||
return null;
|
||||
}
|
||||
contactNameLayout.setError(null);
|
||||
return name.toString().trim();
|
||||
return name;
|
||||
}
|
||||
|
||||
private void onAddButtonClicked() {
|
||||
|
||||
@@ -82,7 +82,7 @@ public class AliasDialogFragment extends AppCompatDialogFragment {
|
||||
}
|
||||
|
||||
private void onSetButtonClicked() {
|
||||
String alias = aliasEditText.getText().toString();
|
||||
String alias = aliasEditText.getText().toString().trim();
|
||||
if (toUtf8(alias).length > MAX_AUTHOR_NAME_LENGTH) {
|
||||
aliasEditLayout.setError(getString(R.string.name_too_long));
|
||||
} else {
|
||||
|
||||
@@ -37,6 +37,7 @@ import org.briarproject.bramble.api.contact.event.ContactRemovedEvent;
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.NoSuchContactException;
|
||||
import org.briarproject.bramble.api.db.NoSuchMessageException;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
@@ -81,6 +82,7 @@ import org.briarproject.briar.api.messaging.Attachment;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||
import org.briarproject.briar.api.messaging.event.AttachmentReceivedEvent;
|
||||
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -107,10 +109,12 @@ import static android.support.v7.util.SortedList.INVALID_POSITION;
|
||||
import static android.view.Gravity.RIGHT;
|
||||
import static android.widget.Toast.LENGTH_SHORT;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.Collections.sort;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
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.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
@@ -138,7 +142,7 @@ public class ConversationActivity extends BriarActivity
|
||||
public static final String CONTACT_ID = "briar.CONTACT_ID";
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ConversationActivity.class.getName());
|
||||
getLogger(ConversationActivity.class.getName());
|
||||
|
||||
private static final int TRANSITION_DURATION_MS = 500;
|
||||
private static final int ONBOARDING_DELAY_MS = 250;
|
||||
@@ -171,6 +175,8 @@ public class ConversationActivity extends BriarActivity
|
||||
volatile GroupInvitationManager groupInvitationManager;
|
||||
|
||||
private final Map<MessageId, String> textCache = new ConcurrentHashMap<>();
|
||||
private final Map<MessageId, PrivateMessageHeader> missingAttachments =
|
||||
new ConcurrentHashMap<>();
|
||||
private final Observer<String> contactNameObserver = name -> {
|
||||
requireNonNull(name);
|
||||
loadMessages();
|
||||
@@ -434,31 +440,40 @@ public class ConversationActivity extends BriarActivity
|
||||
});
|
||||
}
|
||||
|
||||
private void eagerlyLoadMessageSize(PrivateMessageHeader h)
|
||||
throws DbException {
|
||||
MessageId id = h.getId();
|
||||
// If the message has text, load it
|
||||
if (h.hasText()) {
|
||||
String text = textCache.get(id);
|
||||
if (text == null) {
|
||||
LOG.info("Eagerly loading text for latest message");
|
||||
text = messagingManager.getMessageText(id);
|
||||
textCache.put(id, requireNonNull(text));
|
||||
private void eagerlyLoadMessageSize(PrivateMessageHeader h) {
|
||||
try {
|
||||
MessageId id = h.getId();
|
||||
// If the message has text, load it
|
||||
if (h.hasText()) {
|
||||
String text = textCache.get(id);
|
||||
if (text == null) {
|
||||
LOG.info("Eagerly loading text for latest message");
|
||||
text = messagingManager.getMessageText(id);
|
||||
textCache.put(id, requireNonNull(text));
|
||||
}
|
||||
}
|
||||
}
|
||||
// If the message has a single image, load its size - for multiple
|
||||
// images we use a grid so the size is fixed
|
||||
List<AttachmentHeader> headers = h.getAttachmentHeaders();
|
||||
if (headers.size() == 1) {
|
||||
MessageId attachmentId = headers.get(0).getMessageId();
|
||||
AttachmentItem item = attachmentRetriever.cacheGet(attachmentId);
|
||||
if (item == null) {
|
||||
LOG.info("Eagerly loading image size for latest message");
|
||||
item = attachmentRetriever.getAttachmentItems(
|
||||
attachmentRetriever.getMessageAttachments(headers))
|
||||
.get(0);
|
||||
attachmentRetriever.cachePut(item);
|
||||
// If the message has a single image, load its size - for multiple
|
||||
// images we use a grid so the size is fixed
|
||||
List<AttachmentHeader> headers = h.getAttachmentHeaders();
|
||||
if (headers.size() == 1) {
|
||||
List<AttachmentItem> items = attachmentRetriever.cacheGet(id);
|
||||
if (items == null) {
|
||||
LOG.info("Eagerly loading image size for latest message");
|
||||
AttachmentHeader header = headers.get(0);
|
||||
try {
|
||||
Attachment a = attachmentRetriever
|
||||
.getMessageAttachment(header);
|
||||
AttachmentItem item =
|
||||
attachmentRetriever.getAttachmentItem(a, true);
|
||||
attachmentRetriever.cachePut(id, singletonList(item));
|
||||
} catch (NoSuchMessageException e) {
|
||||
LOG.info("Attachment not received yet");
|
||||
missingAttachments.put(header.getMessageId(), h);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -536,18 +551,30 @@ public class ConversationActivity extends BriarActivity
|
||||
&& adapter.isScrolledToBottom(layoutManager);
|
||||
}
|
||||
|
||||
private void loadMessageAttachments(MessageId messageId,
|
||||
List<AttachmentHeader> headers) {
|
||||
private void loadMessageAttachments(PrivateMessageHeader h) {
|
||||
// TODO: Use placeholders for missing/invalid attachments
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
List<Pair<AttachmentHeader, Attachment>> attachments =
|
||||
attachmentRetriever.getMessageAttachments(headers);
|
||||
// TODO move getting the items off to IoExecutor, if size == 1
|
||||
List<AttachmentItem> items =
|
||||
attachmentRetriever.getAttachmentItems(attachments);
|
||||
if (items.size() == 1)
|
||||
attachmentRetriever.cachePut(items.get(0));
|
||||
displayMessageAttachments(messageId, items);
|
||||
List<AttachmentHeader> headers = h.getAttachmentHeaders();
|
||||
boolean needsSize = headers.size() == 1;
|
||||
List<AttachmentItem> items = new ArrayList<>(headers.size());
|
||||
for (AttachmentHeader header : headers) {
|
||||
try {
|
||||
Attachment a = attachmentRetriever
|
||||
.getMessageAttachment(header);
|
||||
AttachmentItem item = attachmentRetriever
|
||||
.getAttachmentItem(a, needsSize);
|
||||
items.add(item);
|
||||
} catch (NoSuchMessageException e) {
|
||||
LOG.info("Attachment not received yet");
|
||||
missingAttachments.put(header.getMessageId(), h);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Don't cache items unless all are present and valid
|
||||
attachmentRetriever.cachePut(h.getId(), items);
|
||||
displayMessageAttachments(h.getId(), items);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
@@ -570,6 +597,13 @@ public class ConversationActivity extends BriarActivity
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof AttachmentReceivedEvent) {
|
||||
AttachmentReceivedEvent a = (AttachmentReceivedEvent) e;
|
||||
if (a.getContactId().equals(contactId)) {
|
||||
LOG.info("Attachment received");
|
||||
onAttachmentReceived(a.getMessageId());
|
||||
}
|
||||
}
|
||||
if (e instanceof ContactRemovedEvent) {
|
||||
ContactRemovedEvent c = (ContactRemovedEvent) e;
|
||||
if (c.getContactId().equals(contactId)) {
|
||||
@@ -620,6 +654,15 @@ public class ConversationActivity extends BriarActivity
|
||||
scrollToBottom();
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void onAttachmentReceived(MessageId attachmentId) {
|
||||
PrivateMessageHeader h = missingAttachments.remove(attachmentId);
|
||||
if (h != null) {
|
||||
LOG.info("Missing attachment received");
|
||||
loadMessageAttachments(h);
|
||||
}
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void onNewConversationMessage(ConversationMessageHeader h) {
|
||||
if (h instanceof ConversationRequest ||
|
||||
@@ -906,19 +949,14 @@ public class ConversationActivity extends BriarActivity
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AttachmentItem> getAttachmentItems(MessageId m,
|
||||
List<AttachmentHeader> headers) {
|
||||
List<AttachmentItem> items = new ArrayList<>(headers.size());
|
||||
for (AttachmentHeader header : headers) {
|
||||
AttachmentItem item =
|
||||
attachmentRetriever.cacheGet(header.getMessageId());
|
||||
if (item == null) {
|
||||
loadMessageAttachments(m, headers);
|
||||
return emptyList();
|
||||
}
|
||||
items.add(item);
|
||||
public List<AttachmentItem> getAttachmentItems(PrivateMessageHeader h) {
|
||||
List<AttachmentItem> attachments =
|
||||
attachmentRetriever.cacheGet(h.getId());
|
||||
if (attachments == null) {
|
||||
loadMessageAttachments(h);
|
||||
return emptyList();
|
||||
}
|
||||
return items;
|
||||
return attachments;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import android.app.Application;
|
||||
import android.arch.lifecycle.AndroidViewModel;
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.arch.lifecycle.MutableLiveData;
|
||||
import android.arch.lifecycle.Observer;
|
||||
import android.arch.lifecycle.Transformations;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.Nullable;
|
||||
@@ -19,7 +18,6 @@ import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.NoSuchContactException;
|
||||
import org.briarproject.bramble.api.db.TransactionManager;
|
||||
import org.briarproject.bramble.api.identity.AuthorId;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.settings.Settings;
|
||||
import org.briarproject.bramble.api.settings.SettingsManager;
|
||||
@@ -52,7 +50,6 @@ import static java.util.logging.Logger.getLogger;
|
||||
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.android.attachment.AttachmentDimensions.getAttachmentDimensions;
|
||||
import static org.briarproject.briar.android.settings.SettingsFragment.SETTINGS_NAMESPACE;
|
||||
import static org.briarproject.briar.android.util.UiUtils.observeForeverOnce;
|
||||
|
||||
@@ -102,10 +99,13 @@ public class ConversationViewModel extends AndroidViewModel
|
||||
@Inject
|
||||
ConversationViewModel(Application application,
|
||||
@DatabaseExecutor Executor dbExecutor,
|
||||
@IoExecutor Executor ioExecutor, TransactionManager db,
|
||||
MessagingManager messagingManager, ContactManager contactManager,
|
||||
TransactionManager db,
|
||||
MessagingManager messagingManager,
|
||||
ContactManager contactManager,
|
||||
SettingsManager settingsManager,
|
||||
PrivateMessageFactory privateMessageFactory) {
|
||||
PrivateMessageFactory privateMessageFactory,
|
||||
AttachmentRetriever attachmentRetriever,
|
||||
AttachmentCreator attachmentCreator) {
|
||||
super(application);
|
||||
this.dbExecutor = dbExecutor;
|
||||
this.db = db;
|
||||
@@ -113,10 +113,8 @@ public class ConversationViewModel extends AndroidViewModel
|
||||
this.contactManager = contactManager;
|
||||
this.settingsManager = settingsManager;
|
||||
this.privateMessageFactory = privateMessageFactory;
|
||||
this.attachmentRetriever = new AttachmentRetriever(messagingManager,
|
||||
getAttachmentDimensions(application.getResources()));
|
||||
this.attachmentCreator = new AttachmentCreator(getApplication(),
|
||||
ioExecutor, messagingManager, attachmentRetriever);
|
||||
this.attachmentRetriever = attachmentRetriever;
|
||||
this.attachmentCreator = attachmentCreator;
|
||||
messagingGroupId = Transformations
|
||||
.map(contact, c -> messagingManager.getContactGroup(c).getId());
|
||||
contactDeleted.setValue(false);
|
||||
@@ -125,7 +123,7 @@ public class ConversationViewModel extends AndroidViewModel
|
||||
@Override
|
||||
protected void onCleared() {
|
||||
super.onCleared();
|
||||
attachmentCreator.cancel();
|
||||
attachmentCreator.deleteUnsentAttachments();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -201,23 +199,12 @@ public class ConversationViewModel extends AndroidViewModel
|
||||
@UiThread
|
||||
public LiveData<AttachmentResult> storeAttachments(Collection<Uri> uris,
|
||||
boolean restart) {
|
||||
MutableLiveData<AttachmentResult> delegate = new MutableLiveData<>();
|
||||
// messagingGroupId is loaded with the contact
|
||||
observeForeverOnce(messagingGroupId, groupId -> {
|
||||
requireNonNull(groupId);
|
||||
LiveData<AttachmentResult> result;
|
||||
if (restart) result = attachmentCreator.getLiveAttachments();
|
||||
else result = attachmentCreator.storeAttachments(groupId, uris);
|
||||
result.observeForever(new Observer<AttachmentResult>() {
|
||||
@Override
|
||||
public void onChanged(@Nullable AttachmentResult value) {
|
||||
requireNonNull(value);
|
||||
if (value.isFinished()) result.removeObserver(this);
|
||||
delegate.setValue(value);
|
||||
}
|
||||
});
|
||||
});
|
||||
return delegate;
|
||||
if (restart) {
|
||||
return attachmentCreator.getLiveAttachments();
|
||||
} else {
|
||||
// messagingGroupId is loaded with the contact
|
||||
return attachmentCreator.storeAttachments(messagingGroupId, uris);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -306,7 +293,7 @@ public class ConversationViewModel extends AndroidViewModel
|
||||
}
|
||||
|
||||
private void storeMessage(PrivateMessage m) {
|
||||
attachmentCreator.onAttachmentsSent();
|
||||
attachmentCreator.onAttachmentsSent(m.getMessage().getId());
|
||||
dbExecutor.execute(() -> {
|
||||
try {
|
||||
long start = now();
|
||||
|
||||
@@ -15,7 +15,6 @@ import org.briarproject.briar.api.forum.ForumInvitationRequest;
|
||||
import org.briarproject.briar.api.forum.ForumInvitationResponse;
|
||||
import org.briarproject.briar.api.introduction.IntroductionRequest;
|
||||
import org.briarproject.briar.api.introduction.IntroductionResponse;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest;
|
||||
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse;
|
||||
@@ -56,8 +55,7 @@ class ConversationVisitor implements
|
||||
if (h.getAttachmentHeaders().isEmpty()) {
|
||||
attachments = emptyList();
|
||||
} else {
|
||||
attachments = attachmentCache
|
||||
.getAttachmentItems(h.getId(), h.getAttachmentHeaders());
|
||||
attachments = attachmentCache.getAttachmentItems(h);
|
||||
}
|
||||
if (h.isLocal()) {
|
||||
item = new ConversationMessageItem(
|
||||
@@ -295,7 +293,6 @@ class ConversationVisitor implements
|
||||
}
|
||||
|
||||
interface AttachmentCache {
|
||||
List<AttachmentItem> getAttachmentItems(MessageId m,
|
||||
List<AttachmentHeader> headers);
|
||||
List<AttachmentItem> getAttachmentItems(PrivateMessageHeader h);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.android.attachment.AttachmentItem;
|
||||
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
||||
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
||||
@@ -135,10 +134,10 @@ public class ImageViewModel extends AndroidViewModel {
|
||||
|
||||
private void saveImage(AttachmentItem attachment, OutputStreamProvider osp,
|
||||
@Nullable Runnable afterCopy) {
|
||||
MessageId messageId = attachment.getMessageId();
|
||||
dbExecutor.execute(() -> {
|
||||
try {
|
||||
Attachment a = messagingManager.getAttachment(messageId);
|
||||
Attachment a =
|
||||
messagingManager.getAttachment(attachment.getHeader());
|
||||
copyImageFromDb(a, osp, afterCopy);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
|
||||
@@ -9,8 +9,8 @@ import com.bumptech.glide.load.data.DataFetcher;
|
||||
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.attachment.AttachmentItem;
|
||||
import org.briarproject.briar.api.messaging.Attachment;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
|
||||
import java.io.InputStream;
|
||||
@@ -50,11 +50,12 @@ class BriarDataFetcher implements DataFetcher<InputStream> {
|
||||
@Override
|
||||
public void loadData(Priority priority,
|
||||
DataCallback<? super InputStream> callback) {
|
||||
MessageId id = attachment.getMessageId();
|
||||
dbExecutor.execute(() -> {
|
||||
if (cancel) return;
|
||||
try {
|
||||
inputStream = messagingManager.getAttachment(id).getStream();
|
||||
Attachment a =
|
||||
messagingManager.getAttachment(attachment.getHeader());
|
||||
inputStream = a.getStream();
|
||||
callback.onDataReady(inputStream);
|
||||
} catch (DbException e) {
|
||||
callback.onLoadFailed(e);
|
||||
|
||||
@@ -22,8 +22,6 @@ import org.briarproject.briar.android.attachment.AttachmentItemResult;
|
||||
import org.briarproject.briar.android.attachment.AttachmentManager;
|
||||
import org.briarproject.briar.android.attachment.AttachmentResult;
|
||||
import org.briarproject.briar.android.view.ImagePreview.ImagePreviewListener;
|
||||
import org.briarproject.briar.api.messaging.FileTooBigException;
|
||||
import org.jsoup.UnsupportedMimeTypeException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -43,11 +41,9 @@ import static android.support.v4.content.ContextCompat.getColor;
|
||||
import static android.support.v4.view.AbsSavedState.EMPTY_STATE;
|
||||
import static android.view.View.GONE;
|
||||
import static android.widget.Toast.LENGTH_LONG;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
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;
|
||||
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_IMAGE_SIZE;
|
||||
import static uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt.STATE_DISMISSED;
|
||||
import static uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt.STATE_FINISHED;
|
||||
|
||||
@@ -190,16 +186,18 @@ public class TextAttachmentController extends TextSendController
|
||||
result.observe(attachmentListener, new Observer<AttachmentResult>() {
|
||||
@Override
|
||||
public void onChanged(@Nullable AttachmentResult attachmentResult) {
|
||||
requireNonNull(attachmentResult);
|
||||
boolean finished = attachmentResult.isFinished();
|
||||
boolean success = attachmentResult.isSuccess();
|
||||
if (finished) {
|
||||
if (attachmentResult == null) {
|
||||
// The fresh LiveData was deliberately set to null.
|
||||
// This means that we can stop observing it.
|
||||
result.removeObserver(this);
|
||||
if (!success) return;
|
||||
} else {
|
||||
boolean noError = onNewAttachmentItemResults(
|
||||
attachmentResult.getItemResults());
|
||||
if (noError && attachmentResult.isFinished()) {
|
||||
onAllAttachmentsCreated();
|
||||
result.removeObserver(this);
|
||||
}
|
||||
}
|
||||
boolean noError = onNewAttachmentItemResults(
|
||||
attachmentResult.getItemResults());
|
||||
if (noError && success) onAllAttachmentsCreated();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -209,7 +207,7 @@ public class TextAttachmentController extends TextSendController
|
||||
if (!loadingUris) throw new AssertionError();
|
||||
for (AttachmentItemResult result : itemResults) {
|
||||
if (result.hasError()) {
|
||||
onError(requireNonNull(result.getException()));
|
||||
onError(result.getErrorMsg());
|
||||
return false;
|
||||
} else {
|
||||
imagePreview.loadPreviewImage(result);
|
||||
@@ -255,20 +253,12 @@ public class TextAttachmentController extends TextSendController
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void onError(Exception e) {
|
||||
String errorMsg;
|
||||
Context ctx = imagePreview.getContext();
|
||||
if (e instanceof UnsupportedMimeTypeException) {
|
||||
String mimeType = ((UnsupportedMimeTypeException) e).getMimeType();
|
||||
errorMsg = ctx.getString(
|
||||
R.string.image_attach_error_invalid_mime_type, mimeType);
|
||||
} else if (e instanceof FileTooBigException) {
|
||||
int mb = MAX_IMAGE_SIZE / 1024 / 1024;
|
||||
errorMsg = ctx.getString(R.string.image_attach_error_too_big, mb);
|
||||
} else {
|
||||
errorMsg = ctx.getString(R.string.image_attach_error);
|
||||
private void onError(@Nullable String errorMsg) {
|
||||
if (errorMsg == null) {
|
||||
errorMsg = imagePreview.getContext()
|
||||
.getString(R.string.image_attach_error);
|
||||
}
|
||||
Toast.makeText(ctx, errorMsg, LENGTH_LONG).show();
|
||||
Toast.makeText(textInput.getContext(), errorMsg, LENGTH_LONG).show();
|
||||
onCancel();
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:hint="@string/set_contact_alias_hint"
|
||||
android:inputType="textPersonName"
|
||||
android:inputType="text|textCapWords"
|
||||
android:textColor="?android:attr/textColorPrimary"
|
||||
android:textSize="@dimen/text_size_medium"/>
|
||||
|
||||
|
||||
@@ -2,14 +2,13 @@ package org.briarproject.briar.android.attachment;
|
||||
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.briar.android.attachment.ImageHelper.DecodeResult;
|
||||
import org.briarproject.briar.api.messaging.Attachment;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.jmock.Expectations;
|
||||
import org.jmock.lib.legacy.ClassImposteriser;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
@@ -25,32 +24,30 @@ public class AttachmentRetrieverTest extends BrambleMockTestCase {
|
||||
100, 50, 200, 75, 300
|
||||
);
|
||||
private final MessageId msgId = new MessageId(getRandomId());
|
||||
private final Attachment attachment = new Attachment(
|
||||
new BufferedInputStream(
|
||||
new ByteArrayInputStream(getRandomBytes(42))));
|
||||
|
||||
private final MessagingManager messagingManager =
|
||||
context.mock(MessagingManager.class);
|
||||
private final ImageHelper imageHelper = context.mock(ImageHelper.class);
|
||||
private final AttachmentRetriever controller =
|
||||
new AttachmentRetriever(
|
||||
messagingManager,
|
||||
dimensions,
|
||||
imageHelper
|
||||
);
|
||||
private final ImageSizeCalculator imageSizeCalculator;
|
||||
private final AttachmentRetriever retriever;
|
||||
|
||||
public AttachmentRetrieverTest() {
|
||||
context.setImposteriser(ClassImposteriser.INSTANCE);
|
||||
MessagingManager messagingManager =
|
||||
context.mock(MessagingManager.class);
|
||||
imageSizeCalculator = context.mock(ImageSizeCalculator.class);
|
||||
retriever = new AttachmentRetrieverImpl(messagingManager, dimensions,
|
||||
imageHelper, imageSizeCalculator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoSize() {
|
||||
String mimeType = "image/jpeg";
|
||||
AttachmentHeader h = getAttachmentHeader(mimeType);
|
||||
Attachment attachment = getAttachment(mimeType);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(imageHelper).getExtensionFromMimeType(mimeType);
|
||||
will(returnValue("jpg"));
|
||||
}});
|
||||
|
||||
AttachmentItem item =
|
||||
controller.getAttachmentItem(h, attachment, false);
|
||||
AttachmentItem item = retriever.getAttachmentItem(attachment, false);
|
||||
assertEquals(mimeType, item.getMimeType());
|
||||
assertEquals("jpg", item.getExtension());
|
||||
assertFalse(item.hasError());
|
||||
@@ -59,31 +56,31 @@ public class AttachmentRetrieverTest extends BrambleMockTestCase {
|
||||
@Test
|
||||
public void testNoSizeWrongMimeTypeProducesError() {
|
||||
String mimeType = "application/octet-stream";
|
||||
AttachmentHeader h = getAttachmentHeader(mimeType);
|
||||
Attachment attachment = getAttachment(mimeType);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(imageHelper).getExtensionFromMimeType(mimeType);
|
||||
will(returnValue(null));
|
||||
}});
|
||||
|
||||
AttachmentItem item =
|
||||
controller.getAttachmentItem(h, attachment, false);
|
||||
AttachmentItem item = retriever.getAttachmentItem(attachment, false);
|
||||
assertTrue(item.hasError());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSmallJpegImage() {
|
||||
String mimeType = "image/jpeg";
|
||||
AttachmentHeader h = getAttachmentHeader(mimeType);
|
||||
Attachment attachment = getAttachment(mimeType);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(imageHelper).decodeStream(with(any(InputStream.class)));
|
||||
will(returnValue(new DecodeResult(160, 240, mimeType)));
|
||||
oneOf(imageSizeCalculator).getSize(with(any(InputStream.class)),
|
||||
with(mimeType));
|
||||
will(returnValue(new Size(160, 240, mimeType)));
|
||||
oneOf(imageHelper).getExtensionFromMimeType(mimeType);
|
||||
will(returnValue("jpg"));
|
||||
}});
|
||||
|
||||
AttachmentItem item = controller.getAttachmentItem(h, attachment, true);
|
||||
AttachmentItem item = retriever.getAttachmentItem(attachment, true);
|
||||
assertEquals(msgId, item.getMessageId());
|
||||
assertEquals(160, item.getWidth());
|
||||
assertEquals(240, item.getHeight());
|
||||
@@ -97,16 +94,17 @@ public class AttachmentRetrieverTest extends BrambleMockTestCase {
|
||||
@Test
|
||||
public void testBigJpegImage() {
|
||||
String mimeType = "image/jpeg";
|
||||
AttachmentHeader h = getAttachmentHeader(mimeType);
|
||||
Attachment attachment = getAttachment(mimeType);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(imageHelper).decodeStream(with(any(InputStream.class)));
|
||||
will(returnValue(new DecodeResult(1728, 2592, mimeType)));
|
||||
oneOf(imageSizeCalculator).getSize(with(any(InputStream.class)),
|
||||
with(mimeType));
|
||||
will(returnValue(new Size(1728, 2592, mimeType)));
|
||||
oneOf(imageHelper).getExtensionFromMimeType(mimeType);
|
||||
will(returnValue("jpg"));
|
||||
}});
|
||||
|
||||
AttachmentItem item = controller.getAttachmentItem(h, attachment, true);
|
||||
AttachmentItem item = retriever.getAttachmentItem(attachment, true);
|
||||
assertEquals(1728, item.getWidth());
|
||||
assertEquals(2592, item.getHeight());
|
||||
assertEquals(dimensions.maxWidth, item.getThumbnailWidth());
|
||||
@@ -116,21 +114,24 @@ public class AttachmentRetrieverTest extends BrambleMockTestCase {
|
||||
|
||||
@Test
|
||||
public void testMalformedError() {
|
||||
AttachmentHeader h = getAttachmentHeader("image/jpeg");
|
||||
String mimeType = "image/jpeg";
|
||||
Attachment attachment = getAttachment(mimeType);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(imageHelper).decodeStream(with(any(InputStream.class)));
|
||||
will(returnValue(new DecodeResult(0, 0, "")));
|
||||
oneOf(imageSizeCalculator).getSize(with(any(InputStream.class)),
|
||||
with(mimeType));
|
||||
will(returnValue(new Size()));
|
||||
oneOf(imageHelper).getExtensionFromMimeType("");
|
||||
will(returnValue(null));
|
||||
}});
|
||||
|
||||
AttachmentItem item = controller.getAttachmentItem(h, attachment, true);
|
||||
AttachmentItem item = retriever.getAttachmentItem(attachment, true);
|
||||
assertTrue(item.hasError());
|
||||
}
|
||||
|
||||
private AttachmentHeader getAttachmentHeader(String contentType) {
|
||||
return new AttachmentHeader(msgId, contentType);
|
||||
private Attachment getAttachment(String contentType) {
|
||||
AttachmentHeader header = new AttachmentHeader(msgId, contentType);
|
||||
InputStream in = new ByteArrayInputStream(getRandomBytes(42));
|
||||
return new Attachment(header, in);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,15 +1,27 @@
|
||||
package org.briarproject.briar.api.messaging;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class Attachment {
|
||||
|
||||
private final AttachmentHeader header;
|
||||
private final InputStream stream;
|
||||
|
||||
public Attachment(InputStream stream) {
|
||||
public Attachment(AttachmentHeader header, InputStream stream) {
|
||||
this.header = header;
|
||||
this.stream = stream;
|
||||
}
|
||||
|
||||
public AttachmentHeader getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public InputStream getStream() {
|
||||
return stream;
|
||||
}
|
||||
|
||||
@@ -2,8 +2,5 @@ package org.briarproject.briar.api.messaging;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* An exception that is thrown when a file is too big to attach to a message.
|
||||
*/
|
||||
public class FileTooBigException extends IOException {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.briarproject.briar.api.messaging;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
/**
|
||||
* An exception that is thrown when an {@link AttachmentHeader} is used to
|
||||
* load an {@link Attachment}, and the header refers to a message that is not
|
||||
* an attachment, or to an attachment that does not have the expected content
|
||||
* type.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
public class InvalidAttachmentException extends DbException {
|
||||
}
|
||||
@@ -40,7 +40,7 @@ public interface MessagingManager extends ConversationClient {
|
||||
/**
|
||||
* Stores a local attachment message.
|
||||
*
|
||||
* @throws FileTooBigException
|
||||
* @throws FileTooBigException If the attachment is too big
|
||||
*/
|
||||
AttachmentHeader addLocalAttachment(GroupId groupId, long timestamp,
|
||||
String contentType, InputStream is) throws DbException, IOException;
|
||||
@@ -68,9 +68,13 @@ public interface MessagingManager extends ConversationClient {
|
||||
String getMessageText(MessageId m) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the attachment with the given ID.
|
||||
* Returns the attachment with the given message ID and content type.
|
||||
*
|
||||
* @throws InvalidAttachmentException If the header refers to a message
|
||||
* that is not an attachment, or to an attachment that does not have the
|
||||
* expected content type
|
||||
*/
|
||||
Attachment getAttachment(MessageId m) throws DbException;
|
||||
Attachment getAttachment(AttachmentHeader h) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns true if the contact with the given {@link ContactId} does support
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
||||
import org.briarproject.briar.api.messaging.Attachment;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.FileTooBigException;
|
||||
import org.briarproject.briar.api.messaging.InvalidAttachmentException;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessage;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||
@@ -374,13 +375,20 @@ class MessagingManagerImpl implements MessagingManager, IncomingMessageHook,
|
||||
}
|
||||
|
||||
@Override
|
||||
public Attachment getAttachment(MessageId m) throws DbException {
|
||||
public Attachment getAttachment(AttachmentHeader h) throws DbException {
|
||||
// TODO: Support large messages
|
||||
MessageId m = h.getMessageId();
|
||||
byte[] body = clientHelper.getMessage(m).getBody();
|
||||
try {
|
||||
BdfDictionary meta = clientHelper.getMessageMetadataAsDictionary(m);
|
||||
Long messageType = meta.getOptionalLong(MSG_KEY_MSG_TYPE);
|
||||
if (messageType == null || messageType != ATTACHMENT)
|
||||
throw new InvalidAttachmentException();
|
||||
String contentType = meta.getString(MSG_KEY_CONTENT_TYPE);
|
||||
if (!contentType.equals(h.getContentType()))
|
||||
throw new InvalidAttachmentException();
|
||||
int offset = meta.getLong(MSG_KEY_DESCRIPTOR_LENGTH).intValue();
|
||||
return new Attachment(new ByteArrayInputStream(body, offset,
|
||||
return new Attachment(h, new ByteArrayInputStream(body, offset,
|
||||
body.length - offset));
|
||||
} catch (FormatException e) {
|
||||
throw new DbException(e);
|
||||
|
||||
Reference in New Issue
Block a user