From 0a22f21410ada89a8ff313ae83031cb95a7c83f7 Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Fri, 4 Sep 2020 05:39:55 +0300 Subject: [PATCH] Small fixes of issues with brightness, background playback, gestures --- .../fragments/detail/VideoDetailFragment.java | 43 +++++++++++++------ .../org/schabi/newpipe/player/BasePlayer.java | 5 +++ .../org/schabi/newpipe/player/MainPlayer.java | 3 ++ .../player/event/PlayerGestureListener.java | 3 +- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 50ad1ae57..b92becbd1 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -337,6 +337,7 @@ public class VideoDetailFragment stopPlayerListener(); playerService = null; player = null; + saveCurrentAndRestoreDefaultBrightness(); } } @@ -425,7 +426,7 @@ public class VideoDetailFragment if (currentWorker != null) { currentWorker.dispose(); } - setupBrightness(true); + saveCurrentAndRestoreDefaultBrightness(); PreferenceManager.getDefaultSharedPreferences(getContext()) .edit() .putString(getString(R.string.stream_info_selected_tab_key), @@ -439,7 +440,7 @@ public class VideoDetailFragment activity.sendBroadcast(new Intent(ACTION_VIDEO_FRAGMENT_RESUMED)); - setupBrightness(false); + setupBrightness(); if (updateFlags != 0) { if (!isLoading.get() && currentInfo != null) { @@ -1908,6 +1909,7 @@ public class VideoDetailFragment @Override public void onFullscreenStateChanged(final boolean fullscreen) { + setupBrightness(); if (playerService.getView() == null || player.getParentActivity() == null) { return; } @@ -2022,29 +2024,41 @@ public class VideoDetailFragment && player.getPlayer().getPlaybackState() != Player.STATE_IDLE; } - private void setupBrightness(final boolean save) { + private void saveCurrentAndRestoreDefaultBrightness() { + final WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); + if (lp.screenBrightness == -1) { + return; + } + // Save current brightness level + PlayerHelper.setScreenBrightness(activity, lp.screenBrightness); + + // Restore the old brightness when fragment.onPause() called or + // when a player is in portrait + lp.screenBrightness = -1; + activity.getWindow().setAttributes(lp); + } + + private void setupBrightness() { if (activity == null) { return; } final WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); - if (save) { - // Save current brightness level - PlayerHelper.setScreenBrightness(activity, lp.screenBrightness); - - // Restore the old brightness when fragment.onPause() called. - // It means when user leaves this fragment brightness will be set to system brightness - lp.screenBrightness = -1; + if (player == null + || !player.videoPlayerSelected() + || !player.isFullscreen() + || bottomSheetState != BottomSheetBehavior.STATE_EXPANDED) { + // Apply system brightness when the player is not in fullscreen + saveCurrentAndRestoreDefaultBrightness(); } else { // Restore already saved brightness level final float brightnessLevel = PlayerHelper.getScreenBrightness(activity); - if (brightnessLevel <= 0.0f && brightnessLevel > 1.0f) { + if (brightnessLevel == lp.screenBrightness) { return; } - lp.screenBrightness = brightnessLevel; + activity.getWindow().setAttributes(lp); } - activity.getWindow().setAttributes(lp); } private void checkLandscape() { @@ -2167,6 +2181,7 @@ public class VideoDetailFragment * @param toMain if true than the main fragment will be focused or the player otherwise */ private void moveFocusToMainFragment(final boolean toMain) { + setupBrightness(); final ViewGroup mainFragment = requireActivity().findViewById(R.id.fragment_holder); // Hamburger button steels a focus even under bottomSheet final Toolbar toolbar = requireActivity().findViewById(R.id.toolbar); @@ -2190,7 +2205,7 @@ public class VideoDetailFragment * Bottom padding should be equal to the mini player's height in this case * * @param showMore whether main fragment should be expanded or not - * */ + */ private void manageSpaceAtTheBottom(final boolean showMore) { final int peekHeight = getResources().getDimensionPixelSize(R.dimen.mini_player_height); final ViewGroup holder = requireActivity().findViewById(R.id.fragment_holder); diff --git a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java index 33e0db9bc..5e0f9ab94 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java @@ -1339,6 +1339,11 @@ public abstract class BasePlayer implements return; } final StreamInfo currentInfo = currentMetadata.getMetadata(); + if (playQueue != null) { + // Save current position. It will help to restore this position once a user + // wants to play prev or next stream from the queue + playQueue.setRecovery(playQueue.getIndex(), simpleExoPlayer.getContentPosition()); + } savePlaybackState(currentInfo, simpleExoPlayer.getCurrentPosition()); } diff --git a/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java b/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java index f62cf27b4..8709174dc 100644 --- a/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java @@ -184,6 +184,9 @@ public final class MainPlayer extends Service { @Override public void onTaskRemoved(final Intent rootIntent) { super.onTaskRemoved(rootIntent); + if (!playerImpl.videoPlayerSelected()) { + return; + } onDestroy(); // Unload from memory completely Runtime.getRuntime().halt(0); diff --git a/app/src/main/java/org/schabi/newpipe/player/event/PlayerGestureListener.java b/app/src/main/java/org/schabi/newpipe/player/event/PlayerGestureListener.java index 5ee8485cf..4aa6070eb 100644 --- a/app/src/main/java/org/schabi/newpipe/player/event/PlayerGestureListener.java +++ b/app/src/main/java/org/schabi/newpipe/player/event/PlayerGestureListener.java @@ -202,7 +202,8 @@ public class PlayerGestureListener private boolean onScrollInMain(final MotionEvent initialEvent, final MotionEvent movingEvent, final float distanceX, final float distanceY) { - if (!isVolumeGestureEnabled && !isBrightnessGestureEnabled) { + if ((!isVolumeGestureEnabled && !isBrightnessGestureEnabled) + || !playerImpl.isFullscreen()) { return false; }