Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

MediaPreviewer : clean files and manage bitmap with Glide #2445

Merged
merged 6 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changes in Riot 0.8.13 (2018-XX-XX)
Features:
- Resurrect performance metrics (#2391)
- Telemetry to report incidence of UISIs (#2330)
- Add a previewer for previewing media before sending it into the room (#1742)
- Add a previewer for previewing media before sending it into the room (#1742|#2445)

Improvements:
- Piwik: Update the way how stats are reported (#2402)
Expand Down
227 changes: 85 additions & 142 deletions vector/src/main/java/im/vector/activity/MediaPreviewerActivity.java
Original file line number Diff line number Diff line change
@@ -1,213 +1,156 @@
package im.vector.activity;

import android.net.Uri;
import android.os.Build;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import org.jetbrains.annotations.NotNull;
import com.bumptech.glide.Glide;

import org.matrix.androidsdk.data.RoomMediaMessage;
import org.matrix.androidsdk.util.Log;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import im.vector.R;
import im.vector.adapters.MediaPreviewAdapter;
import im.vector.listeners.ItemPositionChangedListener;
import kotlin.Pair;

/**
* Previews media selected to be send.
*/
public class MediaPreviewerActivity extends MXCActionBarActivity implements ItemPositionChangedListener {

/**
* the picture uri if a picture is taken with the camera
*/
public static final String EXTRA_CAMERA_PICTURE_URI = "EXTRA_CAMERA_PICTURE_URI";
/**
* the room title (string)
*/
public static final String EXTRA_ROOM_TITLE = "EXTRA_ROOM_TITLE";
public class MediaPreviewerActivity extends MXCActionBarActivity implements MediaPreviewAdapter.EventListener {

private static final String LOG_TAG = MediaPreviewerActivity.class.getSimpleName();

@BindView(R.id.images_preview)
RecyclerView mImagesPreview;

@BindView(R.id.image_previewer)
ImageView mImagePreview;

@BindView(R.id.web_previewer)
WebView mWebPreview;

@BindView(R.id.video_previewer)
VideoView mVideoPreview;

@BindView(R.id.file_previewer)
ImageView mFilePreview;

@BindView(R.id.file_name)
TextView mFileNameView;
//the picture uri if a picture is taken with the camera
public static final String EXTRA_CAMERA_PICTURE_URI = "EXTRA_CAMERA_PICTURE_URI";
// the room title (string)
public static final String EXTRA_ROOM_TITLE = "EXTRA_ROOM_TITLE";

private RoomMediaMessage mCurrentRoomMediaMessage;

List<RoomMediaMessage> mSharedDataItems;
@BindView(R.id.media_previewer_image_view)
ImageView mPreviewerImageView;
@BindView(R.id.media_previewer_video_view)
VideoView mPreviewerVideoView;
@BindView(R.id.media_previewer_list)
RecyclerView mPreviewerRecyclerView;
@BindView(R.id.media_previewer_file_name)
TextView mFileNameView;

@NotNull
@Override
public Pair getOtherThemes() {
return new Pair(R.style.AppTheme_NoActionBar_Dark, R.style.AppTheme_NoActionBar_Black);
}

@Override
public int getLayoutRes() {
return R.layout.activity_media_previewer;
}

@Override
public void onItemPositionChangedListener(int position) {
setPreview(mSharedDataItems.get(position));
}

@SuppressLint("ClickableViewAccessibility")
@Override
public void initUiAndData() {

if (CommonActivityUtils.shouldRestartApp(this)) {
Log.d(LOG_TAG, "onCreate : restart the application");
CommonActivityUtils.restartApp(this);
return;
}

if (CommonActivityUtils.isGoingToSplash(this)) {
Log.d(LOG_TAG, "onCreate : Going to splash screen");
return;
}

mPreviewerVideoView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
onVideoPreviewClicked();
return false;
}
});
configureToolbar();

String roomTitle = (String) getIntent().getExtras().get(EXTRA_ROOM_TITLE);
if (!TextUtils.isEmpty(roomTitle)) {
getSupportActionBar().setTitle(roomTitle);
}

setStatusBarColor(findViewById(R.id.status_bar_background),
ContextCompat.getColor(this, R.color.transparent_dark));

// Resize web content to prevent scrollbars.
mWebPreview.getSettings().setUseWideViewPort(true);
mWebPreview.getSettings().setLoadWithOverviewMode(true);

mSharedDataItems = new ArrayList<>(RoomMediaMessage.listRoomMediaMessages(getIntent(), RoomMediaMessage.class.getClassLoader()));

if (mSharedDataItems.isEmpty()) {
mSharedDataItems.add(new RoomMediaMessage(Uri.parse(getIntent().getStringExtra(EXTRA_CAMERA_PICTURE_URI))));
}

if (!mSharedDataItems.isEmpty()) {
LinearLayoutManager imagesPreviewLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
mImagesPreview.setLayoutManager(imagesPreviewLinearLayoutManager);

MediaPreviewAdapter mediaPreviewAdapter = new MediaPreviewAdapter(mSharedDataItems, this);
mImagesPreview.setAdapter(mediaPreviewAdapter);

setPreview(mSharedDataItems.get(0));
}
final String roomTitle = getIntent().getExtras().getString(EXTRA_ROOM_TITLE);
getSupportActionBar().setTitle(roomTitle);
final List<RoomMediaMessage> sharedDataItems = getSharedItems();
setupRecyclerView(sharedDataItems);
}

@OnClick(R.id.send_floating_action_button)
@OnClick(R.id.media_previewer_send_button)
public void onClick() {
setResult(Activity.RESULT_OK, getIntent());
finish();
}

private void setPreview(RoomMediaMessage roomMediaMessage) {
//region MediaPreviewAdapter.EventListener

// Prevent blinking when tapping on the same item multiple times.
@Override
public void onMediaMessagePreviewClicked(@NonNull final RoomMediaMessage roomMediaMessage) {
if (roomMediaMessage != mCurrentRoomMediaMessage) {
mCurrentRoomMediaMessage = roomMediaMessage;
updatePreview(roomMediaMessage);
}
}

mWebPreview.setVisibility(View.GONE);
mImagePreview.setVisibility(View.GONE);
mVideoPreview.setVisibility(View.GONE);
mFilePreview.setVisibility(View.GONE);
mFileNameView.setVisibility(View.GONE);

String mimeType = roomMediaMessage.getMimeType(this);
Uri uri = roomMediaMessage.getUri();

if (mimeType != null) {
if (mimeType.startsWith("image")) {
if (mimeType.endsWith("gif")) {
mWebPreview.loadUrl(uri.toString());
mWebPreview.setVisibility(View.VISIBLE);
} else {
mImagePreview.setImageURI(uri);
mImagePreview.setVisibility(View.VISIBLE);
}
} else if (mimeType.startsWith("video")) {
mVideoPreview.setVideoURI(uri);
mVideoPreview.seekTo(100);
mVideoPreview.setVisibility(View.VISIBLE);

// Pause/play video on click.
mVideoPreview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (!mVideoPreview.isPlaying()) {
mVideoPreview.start();
} else {
mVideoPreview.pause();
}
return false;
}
});
} else {
// As other files can't be previewed, show a generic file image.
mFilePreview.setVisibility(View.VISIBLE);
}

mFileNameView.setText(roomMediaMessage.getFileName(this));
mFileNameView.setVisibility(View.VISIBLE);
//endregion

//region Private methods

private void updatePreview(final RoomMediaMessage roomMediaMessage) {
mPreviewerVideoView.pause();
mFileNameView.setText(roomMediaMessage.getFileName(this));
final String mimeType = roomMediaMessage.getMimeType(this);
final Uri uri = roomMediaMessage.getUri();
if (mimeType != null) {
if (mimeType.startsWith("image")) {
mPreviewerImageView.setVisibility(View.VISIBLE);
mPreviewerVideoView.setVisibility(View.GONE);
Glide.with(this)
.asBitmap()
.load(uri)
.into(mPreviewerImageView);
} else if (mimeType.startsWith("video")) {
mPreviewerImageView.setVisibility(View.GONE);
mPreviewerVideoView.setVisibility(View.VISIBLE);
mPreviewerVideoView.setVideoURI(uri);
mPreviewerVideoView.seekTo(100);
} else {
mPreviewerImageView.setVisibility(View.VISIBLE);
mPreviewerVideoView.setVisibility(View.GONE);
mPreviewerImageView.setImageResource(R.drawable.filetype_attachment);
}
}
}

private void setupRecyclerView(@NonNull final List<RoomMediaMessage> sharedDataItems) {
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
mPreviewerRecyclerView.setLayoutManager(linearLayoutManager);
final MediaPreviewAdapter mediaPreviewAdapter = new MediaPreviewAdapter(sharedDataItems, this);
mPreviewerRecyclerView.setAdapter(mediaPreviewAdapter);
final RoomMediaMessage firstRoomMedia = sharedDataItems.get(0);
updatePreview(firstRoomMedia);
}

private void setStatusBarColor(View statusBar, int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

int statusBarHeight = getStatusBarHeight();

statusBar.getLayoutParams().height = statusBarHeight;
statusBar.setBackgroundColor(color);
private List<RoomMediaMessage> getSharedItems() {
final List<RoomMediaMessage> sharedDataItems = RoomMediaMessage.listRoomMediaMessages(getIntent());
if (sharedDataItems.isEmpty()) {
final Uri roomMediaUri = Uri.parse(getIntent().getStringExtra(EXTRA_CAMERA_PICTURE_URI));
final RoomMediaMessage roomMediaMessage = new RoomMediaMessage(roomMediaUri);
sharedDataItems.add(roomMediaMessage);
}
return sharedDataItems;
}

private int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
private void onVideoPreviewClicked() {
if (!mPreviewerVideoView.isPlaying()) {
mPreviewerVideoView.start();
} else {
mPreviewerVideoView.pause();
}

return result;
}

//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -1425,8 +1425,7 @@ protected void onActivityResult(int requestCode, int resultCode, final Intent da
mVectorMessageListFragment.scrollToBottom(0);
break;
case CONFIRM_MEDIA_REQUEST_CODE:
List<RoomMediaMessage> sharedDataItems =
new ArrayList<>(RoomMediaMessage.listRoomMediaMessages(data, RoomMediaMessage.class.getClassLoader()));
List<RoomMediaMessage> sharedDataItems = new ArrayList<>(RoomMediaMessage.listRoomMediaMessages(data));
if (0 == sharedDataItems.size()) {
sharedDataItems.add(new RoomMediaMessage(Uri.parse(data.getStringExtra(MediaPreviewerActivity.EXTRA_CAMERA_PICTURE_URI))));
}
Expand Down Expand Up @@ -2039,7 +2038,7 @@ private void sendMediasIntent(Intent intent) {
List<RoomMediaMessage> sharedDataItems = new ArrayList<>();

if (null != intent) {
sharedDataItems = new ArrayList<>(RoomMediaMessage.listRoomMediaMessages(intent, RoomMediaMessage.class.getClassLoader()));
sharedDataItems = new ArrayList<>(RoomMediaMessage.listRoomMediaMessages(intent));
}

// check the extras
Expand Down
Loading