Skip to content

Commit

Permalink
Fixed a demo app issue in the fullscreen video player where the syste…
Browse files Browse the repository at this point in the history
…m UI would get in to an inconsistent state with the video controls visibility
  • Loading branch information
brianwernick committed May 1, 2018
1 parent f5542b4 commit 0759ff5
Showing 1 changed file with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.devbrackets.android.exomediademo.ui.activity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import com.devbrackets.android.exomedia.listener.VideoControlsVisibilityListener;
import com.devbrackets.android.exomedia.ui.widget.VideoView;
import com.devbrackets.android.exomedia.ui.widget.VideoControls;
import com.devbrackets.android.exomedia.ui.widget.VideoView;

/**
* A simple example of making a fullscreen video player activity.
Expand All @@ -30,7 +28,12 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
public void onDestroy() {
super.onDestroy();
exitFullscreen();

// Resets the flags
View decorView = getWindow().getDecorView();
if (decorView != null) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}

private void goFullscreen() {
Expand All @@ -46,17 +49,9 @@ private void exitFullscreen() {
* between fullscreen and not
*/
private void initUiFlags() {
int flags = View.SYSTEM_UI_FLAG_VISIBLE;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
flags |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
}

View decorView = getWindow().getDecorView();
if (decorView != null) {
decorView.setSystemUiVisibility(flags);
decorView.setSystemUiVisibility(getStableUiFlags());
decorView.setOnSystemUiVisibilityChangeListener(fullScreenListener);
}
}
Expand All @@ -67,13 +62,10 @@ private void initUiFlags() {
*
* @param fullscreen True if entering fullscreen mode
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void setUiFlags(boolean fullscreen) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
View decorView = getWindow().getDecorView();
if (decorView != null) {
decorView.setSystemUiVisibility(fullscreen ? getFullscreenUiFlags() : View.SYSTEM_UI_FLAG_VISIBLE);
}
View decorView = getWindow().getDecorView();
if (decorView != null) {
decorView.setSystemUiVisibility(fullscreen ? getFullscreenUiFlags() : getStableUiFlags());
}
}

Expand All @@ -83,28 +75,36 @@ private void setUiFlags(boolean fullscreen) {
*
* @return The appropriate decor view flags to enter fullscreen mode when supported
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private int getFullscreenUiFlags() {
int flags = View.SYSTEM_UI_FLAG_LOW_PROFILE | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
flags |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
return View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
}

return flags;
private int getStableUiFlags() {
return View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
}

/**
* Listens to the system to determine when to show the default controls
* for the {@link VideoView}
*/
private class FullScreenListener implements View.OnSystemUiVisibilityChangeListener {
private int lastVisibility = 0;

@Override
public void onSystemUiVisibilityChange(int visibility) {
// NOTE: if the screen is double tapped in just the right way (or wrong way)
// the SYSTEM_UI_FLAG_HIDE_NAVIGATION flag is dropped. Because of this we
// no longer get notified of the temporary change when the screen is tapped
// (i.e. the VideoControls get the touch event instead of the OS). So we store
// the visibility off for use in the ControlsVisibilityListener for verification
lastVisibility = visibility;
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
videoView.showControls();
}
Expand All @@ -118,7 +118,9 @@ public void onSystemUiVisibilityChange(int visibility) {
private class ControlsVisibilityListener implements VideoControlsVisibilityListener {
@Override
public void onControlsShown() {
// No additional functionality performed
if (fullScreenListener.lastVisibility != View.SYSTEM_UI_FLAG_VISIBLE) {
exitFullscreen();
}
}

@Override
Expand Down

0 comments on commit 0759ff5

Please sign in to comment.