Commit 27969267 authored by Torsten Grote's avatar Torsten Grote

[android] Load image preview from database instead of content Uri

parent fc6275b0
......@@ -38,6 +38,7 @@ 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.Objects.requireNonNull;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.util.IoUtils.tryToClose;
......@@ -122,13 +123,13 @@ class AttachmentController {
}
@DatabaseExecutor
void createAttachmentHeader(ContentResolver contentResolver,
AttachmentItem createAttachmentHeader(ContentResolver contentResolver,
GroupId groupId, Uri uri, boolean needsSize)
throws IOException, DbException {
if (unsentItems.containsKey(uri)) {
// This can happen due to configuration (screen orientation) change.
// So don't create a new attachment, if we have one already.
return;
return requireNonNull(unsentItems.get(uri));
}
long start = now();
InputStream is = contentResolver.openInputStream(uri);
......@@ -145,6 +146,7 @@ class AttachmentController {
getAttachmentItem(contentResolver, uri, h, needsSize);
if (item.hasError()) throw new IOException();
unsentItems.put(uri, item);
return item;
}
boolean isValidMimeType(@Nullable String mimeType) {
......
......@@ -14,15 +14,19 @@ public class AttachmentResult {
@Nullable
private final Uri uri;
@Nullable
private final AttachmentItem item;
@Nullable
private final String errorMsg;
public AttachmentResult(Uri uri) {
public AttachmentResult(Uri uri, AttachmentItem item) {
this.uri = uri;
this.item = item;
this.errorMsg = null;
}
public AttachmentResult(@Nullable String errorMsg) {
this.uri = null;
this.item = null;
this.errorMsg = errorMsg;
}
......@@ -31,6 +35,11 @@ public class AttachmentResult {
return uri;
}
@Nullable
public AttachmentItem getItem() {
return item;
}
public boolean isError() {
return errorMsg != null;
}
......
......@@ -213,9 +213,10 @@ public class ConversationViewModel extends AndroidViewModel implements
if (groupId == null) throw new IllegalStateException();
long start = now();
try {
attachmentController.createAttachmentHeader(contentResolver,
groupId, uri, needsSize);
result.postValue(new AttachmentResult(uri));
AttachmentItem item = attachmentController
.createAttachmentHeader(contentResolver, groupId, uri,
needsSize);
result.postValue(new AttachmentResult(uri, item));
} catch(FileTooBigException e) {
logException(LOG, WARNING, e);
int mb = MAX_IMAGE_SIZE / 1024 / 1024;
......@@ -224,7 +225,7 @@ public class ConversationViewModel extends AndroidViewModel implements
result.postValue(new AttachmentResult(errorMsg));
} catch (DbException | IOException e) {
logException(LOG, WARNING, e);
result.postValue(new AttachmentResult((String) null));
result.postValue(new AttachmentResult(null));
}
logDuration(LOG, "Storing attachment", start);
}));
......
......@@ -9,6 +9,7 @@ import android.view.LayoutInflater;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.R;
import org.briarproject.briar.android.conversation.AttachmentResult;
import java.util.Collection;
......@@ -71,10 +72,10 @@ public class ImagePreview extends ConstraintLayout {
imageList.setAdapter(adapter);
}
void loadPreviewImage(ImagePreviewItem item) {
void loadPreviewImage(AttachmentResult result) {
ImagePreviewAdapter adapter =
((ImagePreviewAdapter) imageList.getAdapter());
requireNonNull(adapter).loadItemPreview(item);
requireNonNull(adapter).loadItemPreview(result);
}
interface ImagePreviewListener {
......
......@@ -8,6 +8,7 @@ import android.view.ViewGroup;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.R;
import org.briarproject.briar.android.conversation.AttachmentResult;
import org.briarproject.briar.android.view.ImagePreview.ImagePreviewListener;
import java.util.ArrayList;
......@@ -53,12 +54,14 @@ class ImagePreviewAdapter extends Adapter<ImagePreviewViewHolder> {
return items.size();
}
void loadItemPreview(ImagePreviewItem item) {
int pos = items.indexOf(item);
void loadItemPreview(AttachmentResult result) {
ImagePreviewItem newItem =
new ImagePreviewItem(requireNonNull(result.getUri()));
int pos = items.indexOf(newItem);
if (pos == NO_POSITION) throw new AssertionError();
ImagePreviewItem newItem = items.get(pos);
newItem.setWaitForLoading(false);
notifyItemChanged(pos, newItem);
ImagePreviewItem item = items.get(pos);
item.setItem(requireNonNull(result.getItem()));
notifyItemChanged(pos, item);
}
}
......@@ -4,6 +4,7 @@ import android.net.Uri;
import android.support.annotation.Nullable;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.android.conversation.AttachmentItem;
import java.util.ArrayList;
import java.util.Collection;
......@@ -13,22 +14,12 @@ import java.util.List;
class ImagePreviewItem {
private final Uri uri;
private boolean waitForLoading = true;
@Nullable
private AttachmentItem item;
ImagePreviewItem(Uri uri) {
this.uri = uri;
}
Uri getUri() {
return uri;
}
void setWaitForLoading(boolean waitForLoading) {
this.waitForLoading = waitForLoading;
}
boolean waitForLoading() {
return waitForLoading;
this.item = null;
}
static List<ImagePreviewItem> fromUris(Collection<Uri> uris) {
......@@ -39,6 +30,19 @@ class ImagePreviewItem {
return items;
}
Uri getUri() {
return uri;
}
public void setItem(AttachmentItem item) {
this.item = item;
}
@Nullable
public AttachmentItem getItem() {
return item;
}
@Override
public boolean equals(@Nullable Object o) {
return o instanceof ImagePreviewItem &&
......
......@@ -42,7 +42,7 @@ class ImagePreviewViewHolder extends ViewHolder {
}
void bind(ImagePreviewItem item) {
if (item.waitForLoading()) return;
if (item.getItem() == null) return;
GlideApp.with(imageView)
.load(item.getUri())
.diskCacheStrategy(NONE)
......
......@@ -161,8 +161,7 @@ public class TextAttachmentController extends TextSendController
if (result.isError() || result.getUri() == null) {
onError(result.getErrorMsg());
} else {
ImagePreviewItem item = new ImagePreviewItem(result.getUri());
imagePreview.loadPreviewImage(item);
imagePreview.loadPreviewImage(result);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment