Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android - Keep screen on #1117

Merged
merged 8 commits into from
Aug 6, 2018
44 changes: 43 additions & 1 deletion android/src/main/java/com/brentvatne/react/ReactVideoView.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.webkit.CookieManager;
import android.widget.MediaController;

Expand Down Expand Up @@ -89,7 +90,6 @@ public String toString() {
private Handler videoControlHandler = new Handler();
private MediaController mediaController;


private String mSrcUriString = null;
private String mSrcType = "mp4";
private ReadableMap mRequestHeaders = null;
Expand Down Expand Up @@ -358,10 +358,12 @@ public void setPausedModifier(final boolean paused) {
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);
Expand Down Expand Up @@ -457,6 +459,42 @@ public void setControls(boolean controls) {
this.mUseNativeControls = controls;
}

public boolean isPreventScreenFromDimmingFlagOn() {
int flags = mThemedReactContext.getCurrentActivity().getWindow().getAttributes().flags;

if ((flags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we need to keep this check for performance or other reasons, it can be written as:
return flags & WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON == 0

return false;
}

return true;
}

public void setPreventScreenFromDimmingFlag(final boolean state) {
if (!mMediaPlayerValid && mThemedReactContext == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be !mMediaPlayerValid || mThemedReactContext == null

return;
}

final boolean isFlagOn = isPreventScreenFromDimmingFlagOn();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this check and simply apply the flag every time.


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) {
Expand Down Expand Up @@ -591,13 +629,17 @@ protected void onDetachedFromWindow() {

mMediaPlayerValid = false;
super.onDetachedFromWindow();

setPreventScreenFromDimmingFlag(false);
}

@Override
protected void onAttachedToWindow() {

super.onAttachedToWindow();

setPreventScreenFromDimmingFlag(true);

if(mMainVer>0) {
setSrc(mSrcUriString, mSrcType, mSrcIsNetwork, mSrcIsAsset, mRequestHeaders, mMainVer, mPatchVer);
}
Expand Down