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

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