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
MediaPreviewer : clean files and manage bitmap with Glide #2445
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3076a0b
MediaPreviewer : clean files and manage bitmap with Glide
ganfra 1e19d77
MediaPreviewer : add more cleaning and fix bad checking
ganfra c10b4b3
MediaPreviewer : add background color to file name view
ganfra 54a2549
MediaPreviewer : update CHANGES.rst file
ganfra cdd651b
MediaPreviewer : optimize imports
ganfra 5ff6ea4
MediaPreviewer : finish cleaning files
ganfra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,21 +1,23 @@ | ||
package im.vector.activity; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.annotation.TargetApi; | ||
import android.app.Activity; | ||
import android.net.Uri; | ||
import android.os.Build; | ||
import android.app.Activity; | ||
import android.support.annotation.ColorRes; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.content.ContextCompat; | ||
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 com.bumptech.glide.Glide; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.matrix.androidsdk.data.RoomMediaMessage; | ||
import org.matrix.androidsdk.util.Log; | ||
|
@@ -27,187 +29,129 @@ | |
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)); | ||
|
||
final String roomTitle = getIntent().getExtras().getString(EXTRA_ROOM_TITLE); | ||
getSupportActionBar().setTitle(roomTitle); | ||
// 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)))); | ||
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); | ||
} | ||
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); | ||
mPreviewerRecyclerView.setLayoutManager(linearLayoutManager); | ||
final MediaPreviewAdapter mediaPreviewAdapter = new MediaPreviewAdapter(sharedDataItems, this); | ||
mPreviewerRecyclerView.setAdapter(mediaPreviewAdapter); | ||
|
||
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)); | ||
if (!sharedDataItems.isEmpty()) { | ||
updatePreview(sharedDataItems.get(0)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's strange, it's working, but I think this line should be outside the else block, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually it's not working if you select "Use native camera" in the settings if if you take a photo. Can you check this case pls? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, just saw that too |
||
} | ||
} | ||
|
||
@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; | ||
|
||
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); | ||
} | ||
updatePreview(roomMediaMessage); | ||
} | ||
} | ||
|
||
|
||
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); | ||
//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 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this cannot be empty, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup