From 0608882127023a60df8f7386af455170bab2fae7 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Tue, 25 Aug 2020 00:38:27 +0900 Subject: [PATCH] Re-fix play/stop autoplaying videos when navigating away --- .../java/awais/instagrabber/MainHelper.java | 32 +++++++++---------- .../instagrabber/activities/MainActivity.java | 4 +-- .../helpers/VideoAwareRecyclerScroller.java | 14 ++++++++ 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/MainHelper.java b/app/src/main/java/awais/instagrabber/MainHelper.java index c5f6bef5..16f7ec1d 100755 --- a/app/src/main/java/awais/instagrabber/MainHelper.java +++ b/app/src/main/java/awais/instagrabber/MainHelper.java @@ -39,7 +39,6 @@ import com.facebook.datasource.BaseDataSubscriber; import com.facebook.datasource.DataSource; import com.facebook.drawee.backends.pipeline.Fresco; import com.facebook.imagepipeline.request.ImageRequest; -import com.google.android.exoplayer2.SimpleExoPlayer; import java.io.DataOutputStream; import java.net.HttpURLConnection; @@ -107,6 +106,7 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener { private static final String TAG = "MainHelper"; private static final double MAX_VIDEO_HEIGHT = 0.9 * Utils.displayMetrics.heightPixels; private static final int RESIZED_VIDEO_HEIGHT = (int) (0.8 * Utils.displayMetrics.heightPixels); + public static final boolean SHOULD_AUTO_PLAY = settingsHelper.getBoolean(Constants.AUTOPLAY_VIDEOS); private static AsyncTask currentlyExecuting; private AsyncTask prevStoriesFetcher; @@ -279,7 +279,7 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener { if (result != null) { topicIds = result.getIds(); rankToken = result.getToken(); - ArrayAdapter spinnerArrayAdapter = new ArrayAdapter( + ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>( mainActivity, android.R.layout.simple_spinner_dropdown_item, result.getNames()); mainActivity.mainBinding.discoverType.setAdapter(spinnerArrayAdapter); } @@ -340,10 +340,10 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener { private FeedAdapter feedAdapter; private RecyclerLazyLoader feedLazyLoader, discoverLazyLoader; private DiscoverAdapter discoverAdapter; - public SimpleExoPlayer currentFeedPlayer; // hack for remix drawer layout private String cookie = settingsHelper.getString(Constants.COOKIE); private boolean isLoggedIn; private RequestManager glide; + private VideoAwareRecyclerScroller videoAwareRecyclerScroller; public MainHelper(@NonNull final MainActivity mainActivity) { stopCurrentExecutor(); @@ -443,9 +443,10 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener { @Override public void onDrawerOpened(@NonNull final View drawerView, @MouseDrawer.EdgeGravity final int gravity) { if (gravity == GravityCompat.START || drawerView == mainActivity.mainBinding.feedView.feedLayout) { - if (currentFeedPlayer != null) { - final boolean shouldAutoplay = settingsHelper.getBoolean(Constants.AUTOPLAY_VIDEOS); - currentFeedPlayer.setPlayWhenReady(shouldAutoplay); + if (videoAwareRecyclerScroller != null) { + if (SHOULD_AUTO_PLAY) { + videoAwareRecyclerScroller.startPlaying(); + } } } else { // clear selection @@ -456,8 +457,8 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener { @Override public void onDrawerClosed(@NonNull final View drawerView, @MouseDrawer.EdgeGravity final int gravity) { if (gravity == GravityCompat.START || drawerView == mainActivity.mainBinding.feedView.feedLayout) { - if (currentFeedPlayer != null) { - currentFeedPlayer.setPlayWhenReady(false); + if (videoAwareRecyclerScroller != null) { + videoAwareRecyclerScroller.stopPlaying(); } } else { // clear selection @@ -674,9 +675,9 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener { } })); - final boolean shouldAutoPlay = settingsHelper.getBoolean(Constants.AUTOPLAY_VIDEOS); - if (shouldAutoPlay) { - mainActivity.mainBinding.feedView.feedPosts.addOnScrollListener(new VideoAwareRecyclerScroller()); + if (SHOULD_AUTO_PLAY) { + videoAwareRecyclerScroller = new VideoAwareRecyclerScroller(); + mainActivity.mainBinding.feedView.feedPosts.addOnScrollListener(videoAwareRecyclerScroller); } mainActivity.mainBinding.feedView.feedPosts.addOnScrollListener(new PauseGlideOnFlingScrollListener(glide)); @@ -1338,15 +1339,14 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener { } public void onPause() { - if (currentFeedPlayer != null) { - currentFeedPlayer.setPlayWhenReady(false); + if (videoAwareRecyclerScroller != null) { + videoAwareRecyclerScroller.stopPlaying(); } } public void onResume() { - if (currentFeedPlayer != null) { - final boolean shouldAutoplay = settingsHelper.getBoolean(Constants.AUTOPLAY_VIDEOS); - currentFeedPlayer.setPlayWhenReady(shouldAutoplay); + if (videoAwareRecyclerScroller != null && SHOULD_AUTO_PLAY) { + videoAwareRecyclerScroller.startPlaying(); } } diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 359fb1ad..eddb3a3e 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -551,8 +551,8 @@ public final class MainActivity extends BaseLanguageActivity { finish(); else if (requestCode == 6007) Utils.showImportExportDialog(this); - else if (requestCode == 6969 && mainHelper.currentFeedPlayer != null) - mainHelper.currentFeedPlayer.setPlayWhenReady(true); + // else if (requestCode == 6969 && mainHelper.currentFeedPlayer != null) + // mainHelper.currentFeedPlayer.setPlayWhenReady(true); } @Override diff --git a/app/src/main/java/awais/instagrabber/customviews/helpers/VideoAwareRecyclerScroller.java b/app/src/main/java/awais/instagrabber/customviews/helpers/VideoAwareRecyclerScroller.java index ae237d60..2cd49f14 100755 --- a/app/src/main/java/awais/instagrabber/customviews/helpers/VideoAwareRecyclerScroller.java +++ b/app/src/main/java/awais/instagrabber/customviews/helpers/VideoAwareRecyclerScroller.java @@ -216,6 +216,20 @@ public class VideoAwareRecyclerScroller extends RecyclerView.OnScrollListener { return null; } + public void startPlaying() { + if (currentlyPlayingViewHolder == null) { + return; + } + currentlyPlayingViewHolder.startPlaying(); + } + + public void stopPlaying() { + if (currentlyPlayingViewHolder == null) { + return; + } + currentlyPlayingViewHolder.stopPlaying(); + } + // private synchronized void attachVideo(final int itemPos, final RecyclerView recyclerView, final View itemView) { // synchronized (LOCK) { // if (recyclerView != null) {