From 9ef1b4decd3782c879bd0239a9c7a232063531c8 Mon Sep 17 00:00:00 2001 From: scria1000 <91804886+scria1000@users.noreply.github.com> Date: Mon, 13 Dec 2021 12:46:22 +0000 Subject: Fix autoplay video continuing in background (#585) Fix autoplay video continuing in background --- .../adapters/PostDetailRecyclerViewAdapter.java | 162 ++++++++++++--------- .../adapters/PostRecyclerViewAdapter.java | 76 +++++----- .../customviews/CustomToroContainer.java | 18 +++ .../infinityforreddit/fragments/PostFragment.java | 13 +- .../fragments/ViewPostDetailFragment.java | 16 +- 5 files changed, 178 insertions(+), 107 deletions(-) (limited to 'app/src/main/java/ml') 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 47330304..0c9b0ca3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -14,6 +14,7 @@ import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.text.Spanned; +import android.text.TextUtils; import android.text.util.Linkify; import android.view.LayoutInflater; import android.view.View; @@ -206,6 +207,8 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { - Intent intent = new Intent(mActivity, ViewVideoActivity.class); - if (mPost.isGfycat()) { - intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_GFYCAT); - intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, mPost.getGfycatId()); - if (mPost.isLoadGfycatOrStreamableVideoSuccess()) { + if (canStartActivity) { + canStartActivity = false; + Intent intent = new Intent(mActivity, ViewVideoActivity.class); + if (mPost.isGfycat()) { + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_GFYCAT); + intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, mPost.getGfycatId()); + if (mPost.isLoadGfycatOrStreamableVideoSuccess()) { + intent.setData(Uri.parse(mPost.getVideoUrl())); + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, mPost.getVideoDownloadUrl()); + } + } else if (mPost.isRedgifs()) { + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS); + intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, mPost.getGfycatId()); + if (mPost.isLoadGfycatOrStreamableVideoSuccess()) { + intent.setData(Uri.parse(mPost.getVideoUrl())); + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, mPost.getVideoDownloadUrl()); + } + } else if (mPost.isStreamable()) { + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_STREAMABLE); + intent.putExtra(ViewVideoActivity.EXTRA_STREAMABLE_SHORT_CODE, mPost.getStreamableShortCode()); + } else { intent.setData(Uri.parse(mPost.getVideoUrl())); intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, mPost.getVideoDownloadUrl()); + intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, mPost.getSubredditName()); + intent.putExtra(ViewVideoActivity.EXTRA_ID, mPost.getId()); } - } else if (mPost.isRedgifs()) { - intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS); - intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, mPost.getGfycatId()); - if (mPost.isLoadGfycatOrStreamableVideoSuccess()) { - intent.setData(Uri.parse(mPost.getVideoUrl())); - intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, mPost.getVideoDownloadUrl()); + intent.putExtra(ViewVideoActivity.EXTRA_POST_TITLE, mPost.getTitle()); + if (helper != null) { + intent.putExtra(ViewVideoActivity.EXTRA_PROGRESS_SECONDS, helper.getLatestPlaybackInfo().getResumePosition()); } - } else if (mPost.isStreamable()) { - intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_STREAMABLE); - intent.putExtra(ViewVideoActivity.EXTRA_STREAMABLE_SHORT_CODE, mPost.getStreamableShortCode()); - } else { - intent.setData(Uri.parse(mPost.getVideoUrl())); - intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, mPost.getVideoDownloadUrl()); - intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, mPost.getSubredditName()); - intent.putExtra(ViewVideoActivity.EXTRA_ID, mPost.getId()); - } - intent.putExtra(ViewVideoActivity.EXTRA_POST_TITLE, mPost.getTitle()); - if (helper != null) { - intent.putExtra(ViewVideoActivity.EXTRA_PROGRESS_SECONDS, helper.getLatestPlaybackInfo().getResumePosition()); + intent.putExtra(ViewVideoActivity.EXTRA_IS_NSFW, mPost.isNSFW()); + mActivity.startActivity(intent); } - intent.putExtra(ViewVideoActivity.EXTRA_IS_NSFW, mPost.isNSFW()); - mActivity.startActivity(intent); }); previewImageView.setOnLongClickListener(view -> fullscreenButton.performClick()); @@ -1767,7 +1781,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter= mStartAutoplayVisibleAreaOffset; + return canPlayVideo && ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= mStartAutoplayVisibleAreaOffset; } @Override @@ -1864,34 +1878,37 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { - if (mPost.getPostType() == Post.VIDEO_TYPE) { - Intent intent = new Intent(mActivity, ViewVideoActivity.class); - if (mPost.isGfycat()) { - intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_GFYCAT); - intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, mPost.getGfycatId()); - } else if (mPost.isRedgifs()) { - intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS); - intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, mPost.getGfycatId()); - } else if (mPost.isStreamable()) { - intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_STREAMABLE); - intent.putExtra(ViewVideoActivity.EXTRA_STREAMABLE_SHORT_CODE, mPost.getStreamableShortCode()); - } else { - intent.setData(Uri.parse(mPost.getVideoUrl())); - intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, mPost.getSubredditName()); - intent.putExtra(ViewVideoActivity.EXTRA_ID, mPost.getId()); - intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, mPost.getVideoDownloadUrl()); + if (canStartActivity) { + canStartActivity = false; + if (mPost.getPostType() == Post.VIDEO_TYPE) { + Intent intent = new Intent(mActivity, ViewVideoActivity.class); + if (mPost.isGfycat()) { + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_GFYCAT); + intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, mPost.getGfycatId()); + } else if (mPost.isRedgifs()) { + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS); + intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, mPost.getGfycatId()); + } else if (mPost.isStreamable()) { + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_STREAMABLE); + intent.putExtra(ViewVideoActivity.EXTRA_STREAMABLE_SHORT_CODE, mPost.getStreamableShortCode()); + } else { + intent.setData(Uri.parse(mPost.getVideoUrl())); + intent.putExtra(ViewVideoActivity.EXTRA_SUBREDDIT, mPost.getSubredditName()); + intent.putExtra(ViewVideoActivity.EXTRA_ID, mPost.getId()); + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, mPost.getVideoDownloadUrl()); + } + intent.putExtra(ViewVideoActivity.EXTRA_POST_TITLE, mPost.getTitle()); + intent.putExtra(ViewVideoActivity.EXTRA_IS_NSFW, mPost.isNSFW()); + mActivity.startActivity(intent); + } else if (mPost.getPostType() == Post.GIF_TYPE) { + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mPost.getSubredditName() + + "-" + mPost.getId() + ".gif"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mPost.getVideoUrl()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, mPost.getTitle()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, mPost.getSubredditName()); + mActivity.startActivity(intent); } - intent.putExtra(ViewVideoActivity.EXTRA_POST_TITLE, mPost.getTitle()); - intent.putExtra(ViewVideoActivity.EXTRA_IS_NSFW, mPost.isNSFW()); - mActivity.startActivity(intent); - } else if (mPost.getPostType() == Post.GIF_TYPE) { - Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); - intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mPost.getSubredditName() - + "-" + mPost.getId() + ".gif"); - intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mPost.getVideoUrl()); - intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, mPost.getTitle()); - intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, mPost.getSubredditName()); - mActivity.startActivity(intent); } }); } @@ -1983,22 +2000,25 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter { - if (mPost.getPostType() == Post.IMAGE_TYPE) { - Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); - intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mPost.getUrl()); - intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mPost.getSubredditNamePrefixed().substring(2) - + "-" + mPost.getId().substring(3) + ".jpg"); - intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, mPost.getTitle()); - intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, mPost.getSubredditName()); - mActivity.startActivity(intent); - } else if (mPost.getPostType() == Post.GIF_TYPE) { - Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); - intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mPost.getSubredditName() - + "-" + mPost.getId() + ".gif"); - intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mPost.getVideoUrl()); - intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, mPost.getTitle()); - intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, mPost.getSubredditName()); - mActivity.startActivity(intent); + if (canStartActivity) { + canStartActivity = false; + if (mPost.getPostType() == Post.IMAGE_TYPE) { + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.EXTRA_IMAGE_URL_KEY, mPost.getUrl()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mPost.getSubredditNamePrefixed().substring(2) + + "-" + mPost.getId().substring(3) + ".jpg"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, mPost.getTitle()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, mPost.getSubredditName()); + mActivity.startActivity(intent); + } else if (mPost.getPostType() == Post.GIF_TYPE) { + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.EXTRA_FILE_NAME_KEY, mPost.getSubredditName() + + "-" + mPost.getId() + ".gif"); + intent.putExtra(ViewImageOrGifActivity.EXTRA_GIF_URL_KEY, mPost.getVideoUrl()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_POST_TITLE_KEY, mPost.getTitle()); + intent.putExtra(ViewImageOrGifActivity.EXTRA_SUBREDDIT_OR_USERNAME_KEY, mPost.getSubredditName()); + mActivity.startActivity(intent); + } } }); } 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 852f8c93..edd0ee82 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -209,6 +209,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - 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.isLoadGfycatOrStreamableVideoSuccess()) { + if (canStartActivity) { + canStartActivity = false; + 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.isLoadGfycatOrStreamableVideoSuccess()) { + 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.isLoadGfycatOrStreamableVideoSuccess()) { + intent.setData(Uri.parse(post.getVideoUrl())); + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); + } + } else if (post.isStreamable()) { + intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_STREAMABLE); + intent.putExtra(ViewVideoActivity.EXTRA_STREAMABLE_SHORT_CODE, post.getStreamableShortCode()); + } 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()); } - } else if (post.isRedgifs()) { - intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_REDGIFS); - intent.putExtra(ViewVideoActivity.EXTRA_GFYCAT_ID, post.getGfycatId()); - if (post.isLoadGfycatOrStreamableVideoSuccess()) { - intent.setData(Uri.parse(post.getVideoUrl())); - intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_DOWNLOAD_URL, post.getVideoDownloadUrl()); + intent.putExtra(ViewVideoActivity.EXTRA_POST_TITLE, post.getTitle()); + if (helper != null) { + intent.putExtra(ViewVideoActivity.EXTRA_PROGRESS_SECONDS, helper.getLatestPlaybackInfo().getResumePosition()); } - } else if (post.isStreamable()) { - intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_STREAMABLE); - intent.putExtra(ViewVideoActivity.EXTRA_STREAMABLE_SHORT_CODE, post.getStreamableShortCode()); - } 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); } - intent.putExtra(ViewVideoActivity.EXTRA_IS_NSFW, post.isNSFW()); - mActivity.startActivity(intent); } }); @@ -2801,7 +2809,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter= mStartAutoplayVisibleAreaOffset; + return canPlayVideo && mediaUri != null && ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= mStartAutoplayVisibleAreaOffset; } @Override @@ -4085,7 +4093,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter= mStartAutoplayVisibleAreaOffset; + return canPlayVideo && mediaUri != null && ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= mStartAutoplayVisibleAreaOffset; } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/CustomToroContainer.java b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/CustomToroContainer.java index 3df8b083..8d925ae6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/CustomToroContainer.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/customviews/CustomToroContainer.java @@ -8,6 +8,8 @@ import androidx.annotation.Nullable; import im.ene.toro.widget.Container; public class CustomToroContainer extends Container { + private OnWindowFocusChangedListener listener; + public CustomToroContainer(Context context) { super(context); } @@ -24,4 +26,20 @@ public class CustomToroContainer extends Container { public void onWindowVisibilityChanged(int visibility) { super.onWindowVisibilityChanged(visibility); } + + @Override + public void onWindowFocusChanged(boolean hasWindowFocus) { + super.onWindowFocusChanged(hasWindowFocus); + if (listener != null) { + listener.onWindowFocusChanged(hasWindowFocus); + } + } + + public void addOnWindowFocusChangedListener(OnWindowFocusChangedListener onWindowFocusChangedListener) { + this.listener = onWindowFocusChangedListener; + } + + public interface OnWindowFocusChangedListener { + void onWindowFocusChanged(boolean hasWindowsFocus); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index 1b83d8a8..9bd9bff8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -315,6 +315,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { applyTheme(); + mPostRecyclerView.addOnWindowFocusChangedListener(this::onWindowFocusChanged); + lazyModeHandler = new Handler(); lazyModeInterval = Float.parseFloat(mSharedPreferences.getString(SharedPreferencesUtils.LAZY_MODE_INTERVAL_KEY, "2.5")); @@ -2115,9 +2117,18 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void onDestroy() { EventBus.getDefault().unregister(this); + if (mPostRecyclerView != null) { + mPostRecyclerView.addOnWindowFocusChangedListener(null); + } super.onDestroy(); } + private void onWindowFocusChanged(boolean hasWindowsFocus) { + if (mAdapter != null) { + mAdapter.setCanPlayVideo(hasWindowsFocus); + } + } + private static abstract class LazyModeRunnable implements Runnable { private int currentPosition = -1; @@ -2184,4 +2195,4 @@ public class PostFragment extends Fragment implements FragmentCommunicator { public interface LoadIconListener { void loadIconSuccess(String subredditOrUserName, String iconUrl); } -} \ No newline at end of file +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java index eb94b01e..7f51721d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java @@ -263,6 +263,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic applyTheme(); + mRecyclerView.addOnWindowFocusChangedListener(this::onWindowFocusChanged); + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); @@ -1101,6 +1103,9 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic @Override public void onResume() { super.onResume(); + if (mPostAdapter != null) { + mPostAdapter.setCanStartActivity(true); + } if (mRecyclerView != null) { mRecyclerView.onWindowVisibilityChanged(View.VISIBLE); } @@ -1131,6 +1136,9 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic @Override public void onDestroy() { EventBus.getDefault().unregister(this); + if (mRecyclerView != null) { + mRecyclerView.addOnWindowFocusChangedListener(null); + } super.onDestroy(); } @@ -1899,4 +1907,10 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic mSwipeRefreshLayout.setColorSchemeColors(mCustomThemeWrapper.getColorAccent()); mFetchPostInfoTextView.setTextColor(mCustomThemeWrapper.getSecondaryTextColor()); } -} \ No newline at end of file + + private void onWindowFocusChanged(boolean hasWindowsFocus) { + if (mPostAdapter != null) { + mPostAdapter.setCanPlayVideo(hasWindowsFocus); + } + } +} -- cgit v1.2.3