From 8d53a5e2739c3d6c720f336a00652b488f2c176c Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 22 Feb 2021 15:45:59 +0800 Subject: Continue adding Card Layout 2. --- .../adapters/PostRecyclerViewAdapter.java | 415 ++++++++++++++++++--- .../res/layout/item_post_card_2_video_autoplay.xml | 308 +++++++++++++++ 2 files changed, 665 insertions(+), 58 deletions(-) create mode 100644 app/src/main/res/layout/item_post_card_2_video_autoplay.xml (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index 3b9af3ca..83cce279 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -433,9 +433,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter preview.getPreviewWidth()) { + mGlide.load(preview.getPreviewUrl()).override(Target.SIZE_ORIGINAL).into(((PostCard2VideoAutoplayViewHolder) holder).previewImageView); + } else { + mGlide.load(preview.getPreviewUrl()).into(((PostCard2VideoAutoplayViewHolder) holder).previewImageView); + } + } else { + ((PostCard2VideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio(1); + } + ((PostCard2VideoAutoplayViewHolder) holder).setVolume(mMuteAutoplayingVideos || (post.isNSFW() && mMuteNSFWVideo) ? 0f : 1f); + + if (post.isGfycat() || post.isRedgifs() && !post.isLoadGfyOrRedgifsVideoSuccess()) { + ((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks = new FetchGfycatOrRedgifsVideoLinks(new FetchGfycatOrRedgifsVideoLinks.FetchGfycatOrRedgifsVideoLinksListener() { + @Override + public void success(String webm, String mp4) { + post.setVideoDownloadUrl(mp4); + post.setVideoUrl(webm); + post.setLoadGfyOrRedgifsVideoSuccess(true); + if (position == holder.getBindingAdapterPosition()) { + ((PostCard2VideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(post.getVideoUrl())); + } + } + + @Override + public void failed(int errorCode) { + if (position == holder.getBindingAdapterPosition()) { + ((PostCard2VideoAutoplayViewHolder) holder).errorLoadingGfycatImageView.setVisibility(View.VISIBLE); + } + } + }); + ((PostCard2VideoAutoplayViewHolder) holder).fetchGfycatOrRedgifsVideoLinks + .fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(mGfycatRetrofit, mRedgifsRetrofit, + post.getGfycatId(), post.isGfycat(), mAutomaticallyTryRedgifs); + } else { + ((PostCard2VideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(post.getVideoUrl())); + } + } else if (holder instanceof PostCard2WithPreviewViewHolder) { if (post.getPostType() == Post.VIDEO_TYPE) { - ((PostCard2ViewHolder) holder).videoOrGifIndicatorImageView.setVisibility(View.VISIBLE); - ((PostCard2ViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.video)); + ((PostCard2WithPreviewViewHolder) holder).videoOrGifIndicatorImageView.setVisibility(View.VISIBLE); + ((PostCard2WithPreviewViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.video)); } else if (post.getPostType() == Post.GIF_TYPE) { if (!mAutoplay) { - ((PostCard2ViewHolder) holder).videoOrGifIndicatorImageView.setVisibility(View.VISIBLE); + ((PostCard2WithPreviewViewHolder) holder).videoOrGifIndicatorImageView.setVisibility(View.VISIBLE); } - ((PostCard2ViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.gif)); + ((PostCard2WithPreviewViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.gif)); } else if (post.getPostType() == Post.IMAGE_TYPE) { - ((PostCard2ViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.image)); + ((PostCard2WithPreviewViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.image)); } else if (post.getPostType() == Post.LINK_TYPE || post.getPostType() == Post.NO_PREVIEW_LINK_TYPE) { - ((PostCard2ViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.link)); - ((PostCard2ViewHolder) holder).linkTextView.setVisibility(View.VISIBLE); + ((PostCard2WithPreviewViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.link)); + ((PostCard2WithPreviewViewHolder) holder).linkTextView.setVisibility(View.VISIBLE); String domain = Uri.parse(post.getUrl()).getHost(); - ((PostCard2ViewHolder) holder).linkTextView.setText(domain); + ((PostCard2WithPreviewViewHolder) holder).linkTextView.setText(domain); if (post.getPostType() == Post.NO_PREVIEW_LINK_TYPE) { - ((PostCard2ViewHolder) holder).noPreviewImageView.setVisibility(View.VISIBLE); - ((PostCard2ViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_link); + ((PostCard2WithPreviewViewHolder) holder).noPreviewImageView.setVisibility(View.VISIBLE); + ((PostCard2WithPreviewViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_link); } } else if (post.getPostType() == Post.GALLERY_TYPE) { - ((PostCard2ViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.gallery)); + ((PostCard2WithPreviewViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.gallery)); } if (post.getPostType() != Post.NO_PREVIEW_LINK_TYPE) { - ((PostCard2ViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostCard2WithPreviewViewHolder) holder).progressBar.setVisibility(View.VISIBLE); } if (mDataSavingMode && mDisableImagePreview) { - ((PostCard2ViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostCard2ViewHolder) holder).noPreviewImageView.setVisibility(View.VISIBLE); + ((PostCard2WithPreviewViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostCard2WithPreviewViewHolder) holder).noPreviewImageView.setVisibility(View.VISIBLE); if (post.getPostType() == Post.VIDEO_TYPE) { - ((PostCard2ViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_outline_video_24dp); - ((PostCard2ViewHolder) holder).videoOrGifIndicatorImageView.setVisibility(View.GONE); + ((PostCard2WithPreviewViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_outline_video_24dp); + ((PostCard2WithPreviewViewHolder) holder).videoOrGifIndicatorImageView.setVisibility(View.GONE); } else if (post.getPostType() == Post.IMAGE_TYPE || post.getPostType() == Post.GIF_TYPE) { - ((PostCard2ViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_image_24dp); - ((PostCard2ViewHolder) holder).videoOrGifIndicatorImageView.setVisibility(View.GONE); + ((PostCard2WithPreviewViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_image_24dp); + ((PostCard2WithPreviewViewHolder) holder).videoOrGifIndicatorImageView.setVisibility(View.GONE); } else if (post.getPostType() == Post.LINK_TYPE) { - ((PostCard2ViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_link); + ((PostCard2WithPreviewViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_link); } else if (post.getPostType() == Post.GALLERY_TYPE) { - ((PostCard2ViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_gallery_24dp); + ((PostCard2WithPreviewViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_gallery_24dp); } } else if (mDataSavingMode && mOnlyDisablePreviewInVideoAndGifPosts && (post.getPostType() == Post.VIDEO_TYPE || post.getPostType() == Post.GIF_TYPE)) { - ((PostCard2ViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostCard2ViewHolder) holder).noPreviewImageView.setVisibility(View.VISIBLE); - ((PostCard2ViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_outline_video_24dp); - ((PostCard2ViewHolder) holder).videoOrGifIndicatorImageView.setVisibility(View.GONE); + ((PostCard2WithPreviewViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostCard2WithPreviewViewHolder) holder).noPreviewImageView.setVisibility(View.VISIBLE); + ((PostCard2WithPreviewViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_outline_video_24dp); + ((PostCard2WithPreviewViewHolder) holder).videoOrGifIndicatorImageView.setVisibility(View.GONE); } else { Post.Preview preview = getSuitablePreview(post.getPreviews()); if (preview != null) { if (preview.getPreviewWidth() <= 0 || preview.getPreviewHeight() <= 0) { int height = (int) (400 * mScale); - ((PostCard2ViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); - ((PostCard2ViewHolder) holder).imageView.getLayoutParams().height = height; + ((PostCard2WithPreviewViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); + ((PostCard2WithPreviewViewHolder) holder).imageView.getLayoutParams().height = height; preview.setPreviewWidth(mImageViewWidth); preview.setPreviewHeight(height); } else { - ((PostCard2ViewHolder) holder).imageView + ((PostCard2WithPreviewViewHolder) holder).imageView .setRatio((float) preview.getPreviewHeight() / preview.getPreviewWidth()); } loadImage(holder, post, preview); } else { - ((PostCard2ViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostCard2ViewHolder) holder).noPreviewImageView.setVisibility(View.VISIBLE); + ((PostCard2WithPreviewViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostCard2WithPreviewViewHolder) holder).noPreviewImageView.setVisibility(View.VISIBLE); if (post.getPostType() == Post.VIDEO_TYPE) { - ((PostCard2ViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_outline_video_24dp); - ((PostCard2ViewHolder) holder).videoOrGifIndicatorImageView.setVisibility(View.GONE); + ((PostCard2WithPreviewViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_outline_video_24dp); + ((PostCard2WithPreviewViewHolder) holder).videoOrGifIndicatorImageView.setVisibility(View.GONE); } else if (post.getPostType() == Post.IMAGE_TYPE || post.getPostType() == Post.GIF_TYPE) { - ((PostCard2ViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_image_24dp); - ((PostCard2ViewHolder) holder).videoOrGifIndicatorImageView.setVisibility(View.GONE); + ((PostCard2WithPreviewViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_image_24dp); + ((PostCard2WithPreviewViewHolder) holder).videoOrGifIndicatorImageView.setVisibility(View.GONE); } else if (post.getPostType() == Post.LINK_TYPE) { - ((PostCard2ViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_link); + ((PostCard2WithPreviewViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_link); } else if (post.getPostType() == Post.GALLERY_TYPE) { - ((PostCard2ViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_gallery_24dp); + ((PostCard2WithPreviewViewHolder) holder).noPreviewImageView.setImageResource(R.drawable.ic_gallery_24dp); } } } @@ -1485,7 +1525,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter imageRequestBuilder = mGlide.load(url).listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostCard2ViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostCard2ViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); - ((PostCard2ViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { - ((PostCard2ViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostCard2ViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostCard2WithPreviewViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostCard2WithPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); + ((PostCard2WithPreviewViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { + ((PostCard2WithPreviewViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostCard2WithPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); loadImage(holder, post, preview); }); return false; @@ -1507,20 +1547,20 @@ public class PostRecyclerViewAdapter extends PagedListAdapter target, DataSource dataSource, boolean isFirstResource) { - ((PostCard2ViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - ((PostCard2ViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostCard2WithPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostCard2WithPreviewViewHolder) holder).progressBar.setVisibility(View.GONE); return false; } }); if ((post.isNSFW() && mNeedBlurNSFW && !(post.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || post.isSpoiler() && mNeedBlurSpoiler) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) - .into(((PostCard2ViewHolder) holder).imageView); + .into(((PostCard2WithPreviewViewHolder) holder).imageView); } else { if (mImageViewWidth > preview.getPreviewWidth()) { - imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostCard2ViewHolder) holder).imageView); + imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostCard2WithPreviewViewHolder) holder).imageView); } else { - imageRequestBuilder.into(((PostCard2ViewHolder) holder).imageView); + imageRequestBuilder.into(((PostCard2WithPreviewViewHolder) holder).imageView); } } } @@ -1741,13 +1781,13 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { + if (helper != null) { + if (helper.getVolume() != 0) { + muteButton.setImageDrawable(mActivity.getDrawable(R.drawable.ic_mute_white_rounded_18dp)); + helper.setVolume(0f); + volume = 0f; + } else { + muteButton.setImageDrawable(mActivity.getDrawable(R.drawable.ic_unmute_white_rounded_18dp)); + helper.setVolume(1f); + volume = 1f; + } + } + }); + + fullscreenButton.setOnClickListener(view -> { + int position = getBindingAdapterPosition(); + if (position < 0) { + return; + } + Post post = getItem(position); + if (post != null) { + markPostRead(post, true); + Intent intent = new Intent(mActivity, ViewVideoActivity.class); + if (post.isGfycat()) { + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_GFYCAT); + intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, post.getGfycatId()); + if (post.isLoadGfyOrRedgifsVideoSuccess()) { + intent.setData(Uri.parse(post.getVideoUrl())); + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); + } + } else if (post.isRedgifs()) { + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS); + intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, post.getGfycatId()); + if (post.isLoadGfyOrRedgifsVideoSuccess()) { + intent.setData(Uri.parse(post.getVideoUrl())); + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); + } + } else { + intent.setData(Uri.parse(post.getVideoUrl())); + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); + intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, post.getSubredditName()); + intent.putExtra(ViewVideoActivity.EXTRA_ID, post.getId()); + } + intent.putExtra(ViewVideoActivity.EXTRA_POST_TITLE, post.getTitle()); + if (helper != null) { + intent.putExtra(ViewVideoActivity.EXTRA_PROGRESS_SECONDS, helper.getLatestPlaybackInfo().getResumePosition()); + } + intent.putExtra(ViewVideoActivity.EXTRA_IS_NSFW, post.isNSFW()); + mActivity.startActivity(intent); + } + }); + + previewImageView.setOnLongClickListener(view -> fullscreenButton.performClick()); + videoPlayer.setOnLongClickListener(view -> fullscreenButton.performClick()); + } + + void bindVideoUri(Uri videoUri) { + mediaUri = videoUri; + } + + void setVolume(float volume) { + this.volume = volume; + } + + void resetVolume() { + volume = 0f; + } + + @NonNull + @Override + public View getPlayerView() { + return videoPlayer; + } + + @NonNull + @Override + public PlaybackInfo getCurrentPlaybackInfo() { + return helper != null && mediaUri != null ? helper.getLatestPlaybackInfo() : new PlaybackInfo(); + } + + @Override + public void initialize(@NonNull Container container, @NonNull PlaybackInfo playbackInfo) { + if (mediaUri == null) { + return; + } + if (helper == null) { + helper = new ExoPlayerViewHelper(this, mediaUri, null, mExoCreator); + helper.addEventListener(new Playable.EventListener() { + @Override + public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) { + if (!trackGroups.isEmpty()) { + for (int i = 0; i < trackGroups.length; i++) { + String mimeType = trackGroups.get(i).getFormat(0).sampleMimeType; + if (mimeType != null && mimeType.contains("audio")) { + helper.setVolume(volume); + muteButton.setVisibility(View.VISIBLE); + if (volume != 0f) { + muteButton.setImageDrawable(mActivity.getDrawable(R.drawable.ic_unmute_white_rounded_18dp)); + } else { + muteButton.setImageDrawable(mActivity.getDrawable(R.drawable.ic_mute_white_rounded_18dp)); + } + break; + } + } + } else { + muteButton.setVisibility(View.GONE); + } + } + + @Override + public void onMetadata(Metadata metadata) { + + } + + @Override + public void onCues(List cues) { + + } + + @Override + public void onRenderedFirstFrame() { + mGlide.clear(previewImageView); + previewImageView.setVisibility(View.GONE); + } + }); + } + helper.initialize(container, playbackInfo); + } + + @Override + public void play() { + if (helper != null && mediaUri != null) { + helper.play(); + } + } + + @Override + public void pause() { + if (helper != null) helper.pause(); + } + + @Override + public boolean isPlaying() { + return helper != null && helper.isPlaying(); + } + + @Override + public void release() { + if (helper != null) { + helper.release(); + helper = null; + } + } + + @Override + public boolean wantsToPlay() { + return mediaUri != null && ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= mStartAutoplayVisibleAreaOffset; + } + + @Override + public int getPlayerOrder() { + return getBindingAdapterPosition(); + } + } + + class PostCard2WithPreviewViewHolder extends PostBaseViewHolder { @BindView(R.id.icon_gif_image_view_item_post_card_2_with_preview) AspectRatioGifImageView iconGifImageView; @BindView(R.id.subreddit_name_text_view_item_post_card_2_with_preview) @@ -3698,7 +3997,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3