From c382512292c8376372a12abce86d2f5eb8b41049 Mon Sep 17 00:00:00 2001 From: scria1000 <91804886+scria1000@users.noreply.github.com> Date: Wed, 25 Jan 2023 17:11:13 +0000 Subject: [PATCH] Remember paused state for autoplay videos (#1331) * Remember paused state for autoplay videos * Update cached playback info Update cached playback info instead of keeping track of it with a local variable * Add missing null check * Move logic into play() --- .../HistoryPostRecyclerViewAdapter.java | 142 ++++++++++++++++-- .../PostDetailRecyclerViewAdapter.java | 67 ++++++++- .../adapters/PostRecyclerViewAdapter.java | 142 ++++++++++++++++-- 3 files changed, 322 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java index 69fef736..22a070c9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/HistoryPostRecyclerViewAdapter.java @@ -47,7 +47,9 @@ import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; import com.google.android.exoplayer2.Tracks; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; +import com.google.android.exoplayer2.ui.DefaultTimeBar; import com.google.android.exoplayer2.ui.PlayerView; +import com.google.android.exoplayer2.ui.TimeBar; import com.google.common.collect.ImmutableList; import com.libRG.CustomTextView; @@ -708,10 +710,12 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter fetchGfycatOrStreamableVideoCall; + private boolean isManuallyPaused; PostVideoAutoplayViewHolder(View itemView) { super(itemView); @@ -2754,6 +2771,36 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter { + pause(); + isManuallyPaused = true; + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + }); + + playButton.setOnClickListener(view -> { + isManuallyPaused = false; + play(); + }); + + progressBar.addListener(new TimeBar.OnScrubListener() { + @Override + public void onScrubStart(TimeBar timeBar, long position) { + + } + + @Override + public void onScrubMove(TimeBar timeBar, long position) { + + } + + @Override + public void onScrubStop(TimeBar timeBar, long position, boolean canceled) { + if (!canceled) { + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + } + } + }); + previewImageView.setOnClickListener(view -> fullscreenButton.performClick()); videoPlayer.setOnClickListener(view -> { @@ -2775,6 +2822,10 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter= mStartAutoplayVisibleAreaOffset; + return canPlayVideo && ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= mStartAutoplayVisibleAreaOffset; } @Override @@ -4296,6 +4358,12 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter fetchGfycatOrStreamableVideoCall; + private boolean isManuallyPaused; PostCard2VideoAutoplayViewHolder(View itemView) { super(itemView); @@ -4366,6 +4437,36 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter { + pause(); + isManuallyPaused = true; + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + }); + + playButton.setOnClickListener(view -> { + isManuallyPaused = false; + play(); + }); + + progressBar.addListener(new TimeBar.OnScrubListener() { + @Override + public void onScrubStart(TimeBar timeBar, long position) { + + } + + @Override + public void onScrubMove(TimeBar timeBar, long position) { + + } + + @Override + public void onScrubStop(TimeBar timeBar, long position, boolean canceled) { + if (!canceled) { + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + } + } + }); + fullscreenButton.setOnClickListener(view -> { int position = getBindingAdapterPosition(); if (position < 0) { @@ -4430,6 +4531,10 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter= mStartAutoplayVisibleAreaOffset; + return canPlayVideo && ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= mStartAutoplayVisibleAreaOffset; } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java index cbafd113..62974d8f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -45,7 +45,9 @@ import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; import com.google.android.exoplayer2.Tracks; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; +import com.google.android.exoplayer2.ui.DefaultTimeBar; import com.google.android.exoplayer2.ui.PlayerView; +import com.google.android.exoplayer2.ui.TimeBar; import com.google.common.collect.ImmutableList; import com.libRG.CustomTextView; @@ -656,7 +658,9 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { + pause(); + isManuallyPaused = true; + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + }); + + playButton.setOnClickListener(view -> { + isManuallyPaused = false; + play(); + }); + + progressBar.addListener(new TimeBar.OnScrubListener() { + @Override + public void onScrubStart(TimeBar timeBar, long position) { + + } + + @Override + public void onScrubMove(TimeBar timeBar, long position) { + + } + + @Override + public void onScrubStop(TimeBar timeBar, long position, boolean canceled) { + if (!canceled) { + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + } + } + }); + previewImageView.setOnClickListener(view -> fullscreenButton.performClick()); playerView.setOnClickListener(view -> { if (mEasierToWatchInFullScreen && playerView.isControllerVisible()) { @@ -1682,6 +1727,10 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter fetchGfycatOrStreamableVideoCall; + private boolean isManuallyPaused; PostVideoAutoplayViewHolder(View itemView) { super(itemView); @@ -2889,6 +2906,36 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + pause(); + isManuallyPaused = true; + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + }); + + playButton.setOnClickListener(view -> { + isManuallyPaused = false; + play(); + }); + + progressBar.addListener(new TimeBar.OnScrubListener() { + @Override + public void onScrubStart(TimeBar timeBar, long position) { + + } + + @Override + public void onScrubMove(TimeBar timeBar, long position) { + + } + + @Override + public void onScrubStop(TimeBar timeBar, long position, boolean canceled) { + if (!canceled) { + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + } + } + }); + previewImageView.setOnClickListener(view -> fullscreenButton.performClick()); videoPlayer.setOnClickListener(view -> { @@ -2910,6 +2957,10 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter= mStartAutoplayVisibleAreaOffset; + return canPlayVideo && ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= mStartAutoplayVisibleAreaOffset; } @Override @@ -4497,6 +4559,12 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter fetchGfycatOrStreamableVideoCall; + private boolean isManuallyPaused; PostCard2VideoAutoplayViewHolder(View itemView) { super(itemView); @@ -4611,6 +4682,36 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + pause(); + isManuallyPaused = true; + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + }); + + playButton.setOnClickListener(view -> { + isManuallyPaused = false; + play(); + }); + + progressBar.addListener(new TimeBar.OnScrubListener() { + @Override + public void onScrubStart(TimeBar timeBar, long position) { + + } + + @Override + public void onScrubMove(TimeBar timeBar, long position) { + + } + + @Override + public void onScrubStop(TimeBar timeBar, long position, boolean canceled) { + if (!canceled) { + savePlaybackInfo(getPlayerOrder(), getCurrentPlaybackInfo()); + } + } + }); + previewImageView.setOnClickListener(view -> fullscreenButton.performClick()); videoPlayer.setOnClickListener(view -> { @@ -4632,6 +4733,10 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter= mStartAutoplayVisibleAreaOffset; + return canPlayVideo && ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= mStartAutoplayVisibleAreaOffset; } @Override