From 2f0b694b38029a6260d5dbe0394133f7f29a6769 Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Wed, 11 Jul 2018 12:28:26 +0200 Subject: [PATCH] Android: MediaPlayer - Prevent screen from dimming --- .../com/brentvatne/react/ReactVideoView.java | 56 +++++++++++++------ 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/android/src/main/java/com/brentvatne/react/ReactVideoView.java b/android/src/main/java/com/brentvatne/react/ReactVideoView.java index 6568f860..b933c5d1 100644 --- a/android/src/main/java/com/brentvatne/react/ReactVideoView.java +++ b/android/src/main/java/com/brentvatne/react/ReactVideoView.java @@ -358,10 +358,12 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP if (mPaused) { if (mMediaPlayer.isPlaying()) { pause(); + setPreventScreenFromDimmingFlag(false); } } else { if (!mMediaPlayer.isPlaying()) { start(); + setPreventScreenFromDimmingFlag(true); // Setting the rate unpauses, so we have to wait for an unpause if (mRate != mActiveRate) { setRateModifier(mRate); @@ -457,6 +459,42 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP this.mUseNativeControls = controls; } + public boolean isPreventScreenFromDimmingFlagOn() { + int flags = mThemedReactContext.getCurrentActivity().getWindow().getAttributes().flags; + + if ((flags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) == 0) { + return false; + } + + return true; + } + + public void setPreventScreenFromDimmingFlag(final boolean state) { + if (!mMediaPlayerValid && mThemedReactContext == null) { + return; + } + + final boolean isFlagOn = isPreventScreenFromDimmingFlagOn(); + + if (state && !isFlagOn) { + mThemedReactContext.getCurrentActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + mThemedReactContext.getCurrentActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + }); + } + + if (!state && isFlagOn) { + mThemedReactContext.getCurrentActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + mThemedReactContext.getCurrentActivity().getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + } + }); + } + } + @Override public void onPrepared(MediaPlayer mp) { @@ -592,14 +630,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP mMediaPlayerValid = false; super.onDetachedFromWindow(); - if (mThemedReactContext != null) { - mThemedReactContext.getCurrentActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - mThemedReactContext.getCurrentActivity().getWindow().clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - } - }); - } + setPreventScreenFromDimmingFlag(false); } @Override @@ -607,14 +638,7 @@ public class ReactVideoView extends ScalableVideoView implements MediaPlayer.OnP super.onAttachedToWindow(); - if (mThemedReactContext != null) { - mThemedReactContext.getCurrentActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - mThemedReactContext.getCurrentActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - } - }); - } + setPreventScreenFromDimmingFlag(true); if(mMainVer>0) { setSrc(mSrcUriString, mSrcType, mSrcIsNetwork, mSrcIsAsset, mRequestHeaders, mMainVer, mPatchVer);