WIP: preview media before sending them

Note that this commit breaks a few things in that non video/image do not work and sharing media isn’t currently using the new mechanism either
This commit is contained in:
Daniel Gultsch 2018-09-11 09:56:29 +02:00
parent 7bde04877a
commit 3608b0eb44
11 changed files with 2902 additions and 2555 deletions

View File

@ -54,6 +54,7 @@ import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.ui.RecordingActivity;
import eu.siacs.conversations.ui.util.Attachment;
import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.utils.ExifHelper;
import eu.siacs.conversations.utils.FileUtils;
@ -105,16 +106,19 @@ public class FileBackend {
}
}
public static boolean allFilesUnderSize(Context context, List<Uri> uris, long max) {
public static boolean allFilesUnderSize(Context context, List<Attachment> attachments, long max) {
if (max <= 0) {
Log.d(Config.LOGTAG, "server did not report max file size for http upload");
return true; //exception to be compatible with HTTP Upload < v0.2
}
for (Uri uri : uris) {
String mime = context.getContentResolver().getType(uri);
for (Attachment attachment : attachments) {
if (attachment.getType() != Attachment.Type.FILE) {
continue;
}
String mime = attachment.getMime();
if (mime != null && mime.startsWith("video/")) {
try {
Dimensions dimensions = FileBackend.getVideoDimensions(context, uri);
Dimensions dimensions = FileBackend.getVideoDimensions(context, attachment.getUri());
if (dimensions.getMin() > 720) {
Log.d(Config.LOGTAG, "do not consider video file with min width larger than 720 for size check");
continue;
@ -123,7 +127,7 @@ public class FileBackend {
//ignore and fall through
}
}
if (FileBackend.getFileSize(context, uri) > max) {
if (FileBackend.getFileSize(context, attachment.getUri()) > max) {
Log.d(Config.LOGTAG, "not all files are under " + max + " bytes. suggesting falling back to jingle");
return false;
}
@ -217,6 +221,7 @@ public class FileBackend {
return calcSampleSize(options, size);
}
private static int calcSampleSize(BitmapFactory.Options options, int size) {
int height = options.outHeight;
int width = options.outWidth;
@ -234,6 +239,29 @@ public class FileBackend {
return inSampleSize;
}
public Bitmap getPreviewForUri(Attachment attachment, int size, boolean cacheOnly) {
final LruCache<String, Bitmap> cache = mXmppConnectionService.getBitmapCache();
Bitmap bitmap = cache.get(attachment.getUuid().toString());
if (bitmap != null || cacheOnly) {
return bitmap;
}
Log.d(Config.LOGTAG,"attachment mime="+attachment.getMime());
if (attachment.getMime() != null && attachment.getMime().startsWith("video/")) {
bitmap = cropCenterSquareVideo(attachment.getUri(), size);
drawOverlay(bitmap, paintOverlayBlack(bitmap) ? R.drawable.play_video_black : R.drawable.play_video_white, 0.75f);
} else {
bitmap = cropCenterSquare(attachment.getUri(), size);
if ("image/gif".equals(attachment.getMime())) {
Bitmap withGifOverlay = bitmap.copy(Bitmap.Config.ARGB_8888, true);
drawOverlay(withGifOverlay, paintOverlayBlack(withGifOverlay) ? R.drawable.play_gif_black : R.drawable.play_gif_white, 1.0f);
bitmap.recycle();
bitmap = withGifOverlay;
}
}
cache.put(attachment.getUuid().toString(), bitmap);
return bitmap;
}
private static Dimensions getVideoDimensions(Context context, Uri uri) throws NotAVideoFile {
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
try {
@ -733,6 +761,21 @@ public class FileBackend {
return record < 0;
}
private Bitmap cropCenterSquareVideo(Uri uri, int size) {
MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
Bitmap frame;
try {
metadataRetriever.setDataSource(mXmppConnectionService, uri);
frame = metadataRetriever.getFrameAtTime(0);
metadataRetriever.release();
return cropCenterSquare(frame, size);
} catch (Exception e) {
frame = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
frame.eraseColor(0xff000000);
return frame;
}
}
private Bitmap getVideoPreview(File file, int size) {
MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
Bitmap frame;

View File

@ -318,7 +318,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
if (account.httpUploadAvailable()
&& ((share.image && !neverCompressPictures())
|| conversation.getMode() == Conversation.MODE_MULTI
|| FileBackend.allFilesUnderSize(this, share.uris, max))) {
/*|| FileBackend.allFilesUnderSize(this, share.uris, max)*/)) {
callback.onPresenceSelected();
} else {
selectPresence(conversation, callback);

View File

@ -0,0 +1,179 @@
package eu.siacs.conversations.ui.adapter;
import android.app.Activity;
import android.content.res.Resources;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RejectedExecutionException;
import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.MediaPreviewBinding;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.ui.ConversationFragment;
import eu.siacs.conversations.ui.XmppActivity;
import eu.siacs.conversations.ui.util.Attachment;
import eu.siacs.conversations.utils.UIHelper;
public class MediaPreviewAdapter extends RecyclerView.Adapter<MediaPreviewAdapter.MediaPreviewViewHolder> {
private final List<Attachment> mediaPreviews = new ArrayList<>();
private final ConversationFragment conversationFragment;
public MediaPreviewAdapter(ConversationFragment fragment) {
this.conversationFragment = fragment;
}
@NonNull
@Override
public MediaPreviewViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
MediaPreviewBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.media_preview, parent, false);
return new MediaPreviewViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull MediaPreviewViewHolder holder, int position) {
final Attachment attachment = mediaPreviews.get(position);
loadPreview(attachment, holder.binding.mediaPreview);
holder.binding.deleteButton.setOnClickListener(v -> {
int pos = mediaPreviews.indexOf(attachment);
mediaPreviews.remove(pos);
notifyItemRemoved(pos);
conversationFragment.toggleInputMethod();
});
}
public void addMediaPreviews(List<Attachment> attachments) {
this.mediaPreviews.addAll(attachments);
notifyDataSetChanged();
}
private void loadPreview(Attachment attachment, ImageView imageView) {
if (cancelPotentialWork(attachment, imageView)) {
XmppActivity activity = (XmppActivity) conversationFragment.getActivity();
final Bitmap bm = activity.xmppConnectionService.getFileBackend().getPreviewForUri(attachment,Math.round(activity.getResources().getDimension(R.dimen.media_preview_size)),true);
if (bm != null) {
cancelPotentialWork(attachment, imageView);
imageView.setImageBitmap(bm);
imageView.setBackgroundColor(0x00000000);
} else {
imageView.setImageDrawable(null);
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(conversationFragment.getActivity().getResources(), null, task);
imageView.setImageDrawable(asyncDrawable);
try {
task.execute(attachment);
} catch (final RejectedExecutionException ignored) {
}
}
}
}
private static boolean cancelPotentialWork(Attachment attachment, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) {
final Attachment oldAttachment = bitmapWorkerTask.attachment;
if (oldAttachment == null || !oldAttachment.equals(attachment)) {
bitmapWorkerTask.cancel(true);
} else {
return false;
}
}
return true;
}
private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
if (imageView != null) {
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncDrawable) {
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
return asyncDrawable.getBitmapWorkerTask();
}
}
return null;
}
@Override
public int getItemCount() {
return mediaPreviews.size();
}
public boolean hasAttachments() {
return mediaPreviews.size() > 0;
}
public List<Attachment> getAttachments() {
return mediaPreviews;
}
class MediaPreviewViewHolder extends RecyclerView.ViewHolder {
private final MediaPreviewBinding binding;
MediaPreviewViewHolder(MediaPreviewBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
static class AsyncDrawable extends BitmapDrawable {
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
super(res, bitmap);
bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
}
BitmapWorkerTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get();
}
}
class BitmapWorkerTask extends AsyncTask<Attachment, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private Attachment attachment = null;
BitmapWorkerTask(ImageView imageView) {
imageViewReference = new WeakReference<>(imageView);
}
@Override
protected Bitmap doInBackground(Attachment... params) {
Activity activity = conversationFragment.getActivity();
if (activity instanceof XmppActivity) {
final XmppActivity xmppActivity = (XmppActivity) activity;
this.attachment = params[0];
return xmppActivity.xmppConnectionService.getFileBackend().getPreviewForUri(this.attachment, Math.round(xmppActivity.getResources().getDimension(R.dimen.media_preview_size)), false);
} else {
return null;
}
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null && !isCancelled()) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
imageView.setBackgroundColor(0x00000000);
}
}
}
}
}

View File

@ -0,0 +1,110 @@
/*
* Copyright (c) 2018, Daniel Gultsch All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package eu.siacs.conversations.ui.util;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.utils.MimeUtils;
public class Attachment {
public String getMime() {
return mime;
}
public Type getType() {
return type;
}
public enum Type {
FILE, IMAGE
}
private final Uri uri;
private final Type type;
private final UUID uuid;
private final String mime;
private Attachment(Uri uri, Type type, String mime) {
this.uri = uri;
this.type = type;
this.mime = mime;
this.uuid = UUID.randomUUID();
}
public static List<Attachment> of(final Context context, Uri uri, Type type) {
final String mime = MimeUtils.guessMimeTypeFromUri(context, uri);
return Collections.singletonList(new Attachment(uri, type, mime));
}
public static List<Attachment> extractAttachments(final Context context, final Intent intent, Type type) {
List<Attachment> uris = new ArrayList<>();
if (intent == null) {
return uris;
}
final String contentType = intent.getType();
final Uri data = intent.getData();
if (data == null) {
final ClipData clipData = intent.getClipData();
if (clipData != null) {
for (int i = 0; i < clipData.getItemCount(); ++i) {
final Uri uri = clipData.getItemAt(i).getUri();
Log.d(Config.LOGTAG,"uri="+uri+" contentType="+contentType);
final String mime = contentType != null ? contentType : MimeUtils.guessMimeTypeFromUri(context, uri);
Log.d(Config.LOGTAG,"mime="+mime);
uris.add(new Attachment(uri, type, mime));
}
}
} else {
final String mime = contentType != null ? contentType : MimeUtils.guessMimeTypeFromUri(context, data);
uris.add(new Attachment(data, type, mime));
}
return uris;
}
public Uri getUri() {
return uri;
}
public UUID getUuid() {
return uuid;
}
}

View File

@ -1,61 +0,0 @@
/*
* Copyright (c) 2018, Daniel Gultsch All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package eu.siacs.conversations.ui.util;
import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import java.util.ArrayList;
import java.util.List;
public class AttachmentTool {
@SuppressLint("NewApi")
public static List<Uri> extractUriFromIntent(final Intent intent) {
List<Uri> uris = new ArrayList<>();
if (intent == null) {
return uris;
}
final Uri uri = intent.getData();
if (uri == null) {
final ClipData clipData = intent.getClipData();
if (clipData != null) {
for (int i = 0; i < clipData.getItemCount(); ++i) {
uris.add(clipData.getItemAt(i).getUri());
}
}
} else {
uris.add(uri);
}
return uris;
}
}

View File

@ -13,7 +13,7 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/snackbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="?attr/color_background_secondary"
android:divider="@null"
@ -54,15 +54,30 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="?attr/color_background_primary">
<android.support.v7.widget.RecyclerView
android:visibility="gone"
android:id="@+id/media_preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/textSendButton"
tools:listitem="@layout/media_preview"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:paddingTop="8dp"
android:requiresFadingEdge="horizontal">
</android.support.v7.widget.RecyclerView>
<eu.siacs.conversations.ui.widget.EditMessage
android:id="@+id/textinput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/textSendButton"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/textSendButton"
android:background="?attr/color_background_primary"
android:ems="10"
style="@style/Widget.Conversations.EditText"
@ -83,7 +98,7 @@
android:id="@+id/textSendButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="?attr/color_background_primary"
android:contentDescription="@string/send_message"
@ -106,17 +121,17 @@
android:id="@+id/snackbar_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/snackbar_action"
android:paddingLeft="24dp"
android:layout_toStartOf="@+id/snackbar_action"
android:paddingStart="24dp"
android:textAppearance="@style/TextAppearance.Conversations.Body1.OnDark"/>
<TextView
android:id="@+id/snackbar_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:paddingBottom="16dp"
android:paddingLeft="24dp"

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/media_preview"
android:layout_width="@dimen/media_preview_size"
android:layout_height="@dimen/media_preview_size"
android:background="@color/black54"
android:layout_centerInParent="true"
android:layout_margin="12dp"/>
<ImageButton
android:id="@+id/delete_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:alpha="?attr/delete_icon_alpha"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="?attr/icon_cancel"/>
</RelativeLayout>
</layout>

View File

@ -50,6 +50,7 @@
<attr name="conversations_overview_background" format="reference|color"/>
<attr name="icon_alpha" format="float"/>
<attr name="delete_icon_alpha" format="float"/>
<attr name="icon_add_group" format="reference"/>
<attr name="icon_add_person" format="reference"/>

View File

@ -11,6 +11,7 @@
<dimen name="audio_player_width">224dp</dimen>
<dimen name="avatar_item_distance">16dp</dimen>
<dimen name="media_preview_size">80dp</dimen>
<dimen name="toolbar_elevation">4dp</dimen>
<dimen name="publish_avatar_top_margin">8dp</dimen>

View File

@ -58,6 +58,7 @@
<item name="conversations_overview_background">@color/green700</item>
<item type="float" name="icon_alpha">0.54</item>
<item type="float" name="delete_icon_alpha">0.70</item>
<item name="dialog_horizontal_padding">24dp</item>
<item name="dialog_vertical_padding">16dp</item>
@ -158,6 +159,7 @@
<item name="conversations_overview_background">@color/green900</item>
<item type="float" name="icon_alpha">0.7</item>
<item type="float" name="delete_icon_alpha">0.7</item>
<item name="dialog_horizontal_padding">24dp</item>
<item name="dialog_vertical_padding">16dp</item>