This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2445 from vector-im/feature/media_previewer_review
MediaPreviewer : clean files and manage bitmap with Glide
- Loading branch information
Showing
6 changed files
with
193 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
227 changes: 85 additions & 142 deletions
227
vector/src/main/java/im/vector/activity/MediaPreviewerActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.