2018-01-04 11:11:49 -08:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.annotation.RequiresApi;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.Window;
|
|
|
|
import android.widget.FrameLayout;
|
2018-11-08 23:33:37 -08:00
|
|
|
import android.widget.TextView;
|
2018-01-04 11:11:49 -08:00
|
|
|
|
2019-07-24 12:30:23 +10:00
|
|
|
import network.loki.messenger.R;
|
2018-01-04 11:11:49 -08:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideRequests;
|
|
|
|
import org.thoughtcrime.securesms.mms.VideoSlide;
|
2018-01-16 11:21:58 -08:00
|
|
|
import org.thoughtcrime.securesms.util.views.Stub;
|
2018-01-04 11:11:49 -08:00
|
|
|
import org.thoughtcrime.securesms.video.VideoPlayer;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
public class MediaView extends FrameLayout {
|
|
|
|
|
2018-01-16 11:21:58 -08:00
|
|
|
private ZoomingImageView imageView;
|
|
|
|
private Stub<VideoPlayer> videoView;
|
2018-01-04 11:11:49 -08:00
|
|
|
|
|
|
|
public MediaView(@NonNull Context context) {
|
|
|
|
super(context);
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
public MediaView(@NonNull Context context, @Nullable AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
public MediaView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
|
|
public MediaView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
|
|
|
super(context, attrs, defStyleAttr, defStyleRes);
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initialize() {
|
|
|
|
inflate(getContext(), R.layout.media_view, this);
|
|
|
|
|
|
|
|
this.imageView = findViewById(R.id.image);
|
2018-01-16 11:21:58 -08:00
|
|
|
this.videoView = new Stub<>(findViewById(R.id.video_player_stub));
|
2018-01-04 11:11:49 -08:00
|
|
|
}
|
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
public void set(@NonNull GlideRequests glideRequests,
|
2018-01-04 11:11:49 -08:00
|
|
|
@NonNull Window window,
|
|
|
|
@NonNull Uri source,
|
|
|
|
@NonNull String mediaType,
|
|
|
|
long size,
|
|
|
|
boolean autoplay)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
if (mediaType.startsWith("image/")) {
|
|
|
|
imageView.setVisibility(View.VISIBLE);
|
2018-01-16 11:21:58 -08:00
|
|
|
if (videoView.resolved()) videoView.get().setVisibility(View.GONE);
|
2018-01-24 19:17:44 -08:00
|
|
|
imageView.setImageUri(glideRequests, source, mediaType);
|
2018-01-04 11:11:49 -08:00
|
|
|
} else if (mediaType.startsWith("video/")) {
|
|
|
|
imageView.setVisibility(View.GONE);
|
2018-01-16 11:21:58 -08:00
|
|
|
videoView.get().setVisibility(View.VISIBLE);
|
|
|
|
videoView.get().setWindow(window);
|
2018-01-24 19:17:44 -08:00
|
|
|
videoView.get().setVideoSource(new VideoSlide(getContext(), source, size), autoplay);
|
2018-01-04 11:11:49 -08:00
|
|
|
} else {
|
|
|
|
throw new IOException("Unsupported media type: " + mediaType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void pause() {
|
2018-01-16 11:21:58 -08:00
|
|
|
if (this.videoView.resolved()){
|
|
|
|
this.videoView.get().pause();
|
|
|
|
}
|
2018-01-04 11:11:49 -08:00
|
|
|
}
|
|
|
|
|
2018-11-08 23:33:37 -08:00
|
|
|
public void hideControls() {
|
|
|
|
if (this.videoView.resolved()){
|
|
|
|
this.videoView.get().hideControls();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public @Nullable View getPlaybackControls() {
|
|
|
|
if (this.videoView.resolved()){
|
|
|
|
return this.videoView.get().getControlView();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-01-04 11:11:49 -08:00
|
|
|
public void cleanup() {
|
|
|
|
this.imageView.cleanup();
|
2018-01-16 11:21:58 -08:00
|
|
|
if (this.videoView.resolved()) {
|
|
|
|
this.videoView.get().cleanup();
|
|
|
|
}
|
2018-01-04 11:11:49 -08:00
|
|
|
}
|
|
|
|
}
|