From 8e32958da680942339ca09aeffa400481ba105db Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sat, 20 Mar 2021 13:33:37 +0900 Subject: [PATCH] Keep screen on while video is playing. Closes https://github.com/austinhuang0131/barinsta/issues/490 --- .../adapters/SliderCallbackAdapter.java | 3 ++ .../adapters/SliderItemsAdapter.java | 2 ++ .../viewholder/SliderVideoViewHolder.java | 7 +++++ .../VideoPlayerCallbackAdapter.java | 3 ++ .../customviews/VideoPlayerViewHelper.java | 5 +++ .../fragments/PostViewV2Fragment.java | 31 +++++++++++++++++++ .../java/awais/instagrabber/utils/Utils.java | 12 +++++++ 7 files changed, 63 insertions(+) diff --git a/app/src/main/java/awais/instagrabber/adapters/SliderCallbackAdapter.java b/app/src/main/java/awais/instagrabber/adapters/SliderCallbackAdapter.java index 803256af..1cfec468 100644 --- a/app/src/main/java/awais/instagrabber/adapters/SliderCallbackAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/SliderCallbackAdapter.java @@ -12,4 +12,7 @@ public class SliderCallbackAdapter implements SliderItemsAdapter.SliderCallback @Override public void onPlayerPause(final int position) {} + + @Override + public void onPlayerRelease(final int position) {} } diff --git a/app/src/main/java/awais/instagrabber/adapters/SliderItemsAdapter.java b/app/src/main/java/awais/instagrabber/adapters/SliderItemsAdapter.java index 5d4092c0..c805d316 100644 --- a/app/src/main/java/awais/instagrabber/adapters/SliderItemsAdapter.java +++ b/app/src/main/java/awais/instagrabber/adapters/SliderItemsAdapter.java @@ -148,5 +148,7 @@ public final class SliderItemsAdapter extends ListAdapter= 0 && sliderPosition < media.getCarouselMedia().size()) { @@ -1218,10 +1232,27 @@ public class PostViewV2Fragment extends SharedElementTransitionDialogFragment im @Override public void onPlay() { + final FragmentActivity activity = getActivity(); + if (activity == null) return; + Utils.enabledKeepScreenOn(activity); if (detailsVisible) { new Handler().postDelayed(() -> toggleDetails(), DETAILS_HIDE_DELAY_MILLIS); } } + + @Override + public void onPause() { + final FragmentActivity activity = getActivity(); + if (activity == null) return; + Utils.disableKeepScreenOn(activity); + } + + @Override + public void onRelease() { + final FragmentActivity activity = getActivity(); + if (activity == null) return; + Utils.disableKeepScreenOn(activity); + } }; final float aspectRatio = (float) media.getOriginalWidth() / media.getOriginalHeight(); String videoUrl = null; diff --git a/app/src/main/java/awais/instagrabber/utils/Utils.java b/app/src/main/java/awais/instagrabber/utils/Utils.java index 29425a27..bf716e5d 100644 --- a/app/src/main/java/awais/instagrabber/utils/Utils.java +++ b/app/src/main/java/awais/instagrabber/utils/Utils.java @@ -359,4 +359,16 @@ public final class Utils { } return drawable; } + + public static void enabledKeepScreenOn(@NonNull final Activity activity) { + final Window window = activity.getWindow(); + if (window == null) return; + window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + + public static void disableKeepScreenOn(@NonNull final Activity activity) { + final Window window = activity.getWindow(); + if (window == null) return; + window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } }