-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Android - Keep screen on #1117
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
240444c
Android keep screen on
2f0b694
Android: MediaPlayer - Prevent screen from dimming
5435e5d
Merge branch 'master' of https://github.com/react-native-community/re…
7e5647d
Android: MediaPlayer - Prevent screen from dimming, fixed bug and sim…
ab273ed
Android: MediaPlayer - Prevent screen from dimming, check for activity
99591e4
Android: MediaPlayer - Prevent screen from dimming, make variable as …
bd48a00
Switch to using setKeepScreenOn to prevent screen timeouts
cobarx 3a1d819
Allow screen to sleep when video finishes
cobarx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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; | ||
|
@@ -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); | ||
|
@@ -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) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public void setPreventScreenFromDimmingFlag(final boolean state) { | ||
if (!mMediaPlayerValid && mThemedReactContext == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be |
||
return; | ||
} | ||
|
||
final boolean isFlagOn = isPreventScreenFromDimmingFlagOn(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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