2019-07-19 15:43:55 +00:00
|
|
|
package org.thoughtcrime.securesms.mediapreview;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
2020-07-09 16:19:58 +00:00
|
|
|
import org.thoughtcrime.securesms.attachments.AttachmentId;
|
2021-11-18 17:36:52 +00:00
|
|
|
import org.thoughtcrime.securesms.database.SignalDatabase;
|
2020-07-09 16:19:58 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.PartUriParser;
|
2019-07-19 15:43:55 +00:00
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
2022-03-29 17:43:41 +00:00
|
|
|
import org.signal.core.util.concurrent.SimpleTask;
|
2020-07-09 16:19:58 +00:00
|
|
|
|
|
|
|
import java.util.Objects;
|
2019-07-19 15:43:55 +00:00
|
|
|
|
|
|
|
public abstract class MediaPreviewFragment extends Fragment {
|
|
|
|
|
2022-03-02 18:20:49 +00:00
|
|
|
public static final String DATA_URI = "DATA_URI";
|
|
|
|
|
2019-07-19 15:43:55 +00:00
|
|
|
static final String DATA_SIZE = "DATA_SIZE";
|
|
|
|
static final String DATA_CONTENT_TYPE = "DATA_CONTENT_TYPE";
|
|
|
|
static final String AUTO_PLAY = "AUTO_PLAY";
|
2021-06-09 13:23:41 +00:00
|
|
|
static final String VIDEO_GIF = "VIDEO_GIF";
|
2019-07-19 15:43:55 +00:00
|
|
|
|
2020-07-09 16:19:58 +00:00
|
|
|
private AttachmentId attachmentId;
|
|
|
|
protected Events events;
|
2019-07-19 15:43:55 +00:00
|
|
|
|
|
|
|
public static MediaPreviewFragment newInstance(@NonNull Attachment attachment, boolean autoPlay) {
|
2021-06-09 13:23:41 +00:00
|
|
|
return newInstance(attachment.getUri(), attachment.getContentType(), attachment.getSize(), autoPlay, attachment.isVideoGif());
|
2019-07-19 15:43:55 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 13:23:41 +00:00
|
|
|
public static MediaPreviewFragment newInstance(@NonNull Uri dataUri, @NonNull String contentType, long size, boolean autoPlay, boolean isVideoGif) {
|
2019-07-19 15:43:55 +00:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
|
|
|
|
args.putParcelable(MediaPreviewFragment.DATA_URI, dataUri);
|
|
|
|
args.putString(MediaPreviewFragment.DATA_CONTENT_TYPE, contentType);
|
|
|
|
args.putLong(MediaPreviewFragment.DATA_SIZE, size);
|
|
|
|
args.putBoolean(MediaPreviewFragment.AUTO_PLAY, autoPlay);
|
2021-06-09 13:23:41 +00:00
|
|
|
args.putBoolean(MediaPreviewFragment.VIDEO_GIF, isVideoGif);
|
2019-07-19 15:43:55 +00:00
|
|
|
|
|
|
|
MediaPreviewFragment fragment = createCorrectFragmentType(contentType);
|
|
|
|
|
|
|
|
fragment.setArguments(args);
|
|
|
|
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static MediaPreviewFragment createCorrectFragmentType(@NonNull String contentType) {
|
|
|
|
if (MediaUtil.isVideo(contentType)) {
|
|
|
|
return new VideoMediaPreviewFragment();
|
|
|
|
} else if (MediaUtil.isImageType(contentType)) {
|
|
|
|
return new ImageMediaPreviewFragment();
|
|
|
|
} else {
|
|
|
|
throw new AssertionError("Unexpected media type: " + contentType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttach(@NonNull Context context) {
|
|
|
|
super.onAttach(context);
|
|
|
|
|
2022-02-24 17:40:28 +00:00
|
|
|
if (context instanceof Events) {
|
|
|
|
events = (Events) context;
|
|
|
|
} else if (getParentFragment() instanceof Events) {
|
|
|
|
events = (Events) getParentFragment();
|
|
|
|
} else {
|
|
|
|
throw new AssertionError("Parent component must support " + Events.class);
|
|
|
|
}
|
2019-07-19 15:43:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 16:19:58 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
checkMediaStillAvailable();
|
2022-03-23 15:42:14 +00:00
|
|
|
requireActivity().supportStartPostponedEnterTransition();
|
2020-07-09 16:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 15:43:55 +00:00
|
|
|
public void cleanUp() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public void pause() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public @Nullable View getPlaybackControls() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-29 19:08:45 +00:00
|
|
|
private void checkMediaStillAvailable() {
|
2020-07-09 16:19:58 +00:00
|
|
|
if (attachmentId == null) {
|
|
|
|
attachmentId = new PartUriParser(Objects.requireNonNull(requireArguments().getParcelable(DATA_URI))).getPartId();
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleTask.run(getViewLifecycleOwner().getLifecycle(),
|
2021-11-18 17:36:52 +00:00
|
|
|
() -> SignalDatabase.attachments().hasAttachment(attachmentId),
|
2020-07-09 16:19:58 +00:00
|
|
|
hasAttachment -> { if (!hasAttachment) events.mediaNotAvailable(); });
|
|
|
|
}
|
2019-07-19 15:43:55 +00:00
|
|
|
|
2020-07-09 16:19:58 +00:00
|
|
|
public interface Events {
|
2019-07-19 15:43:55 +00:00
|
|
|
boolean singleTapOnMedia();
|
2020-07-09 16:19:58 +00:00
|
|
|
void mediaNotAvailable();
|
2022-02-24 21:39:56 +00:00
|
|
|
default @Nullable VideoControlsDelegate getVideoControlsDelegate() {
|
|
|
|
return null;
|
|
|
|
}
|
2019-07-19 15:43:55 +00:00
|
|
|
}
|
|
|
|
}
|