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

Small fixes of issues with brightness, background playback, gestures #4223

Merged
merged 1 commit into from
Sep 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ private void unbind(final Context context) {
stopPlayerListener();
playerService = null;
player = null;
saveCurrentAndRestoreDefaultBrightness();
}
}

Expand Down Expand Up @@ -425,7 +426,7 @@ public void onPause() {
if (currentWorker != null) {
currentWorker.dispose();
}
setupBrightness(true);
saveCurrentAndRestoreDefaultBrightness();
PreferenceManager.getDefaultSharedPreferences(getContext())
.edit()
.putString(getString(R.string.stream_info_selected_tab_key),
Expand All @@ -439,7 +440,7 @@ public void onResume() {

activity.sendBroadcast(new Intent(ACTION_VIDEO_FRAGMENT_RESUMED));

setupBrightness(false);
setupBrightness();

if (updateFlags != 0) {
if (!isLoading.get() && currentInfo != null) {
Expand Down Expand Up @@ -1908,6 +1909,7 @@ public void onServiceStopped() {

@Override
public void onFullscreenStateChanged(final boolean fullscreen) {
setupBrightness();
if (playerService.getView() == null || player.getParentActivity() == null) {
return;
}
Expand Down Expand Up @@ -2022,29 +2024,41 @@ private boolean playerIsNotStopped() {
&& 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() {
Expand Down Expand Up @@ -2167,6 +2181,7 @@ private void cleanUp() {
* @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);
Expand All @@ -2190,7 +2205,7 @@ private void moveFocusToMainFragment(final boolean toMain) {
* 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);
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/org/schabi/newpipe/player/BasePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,11 @@ public void savePlaybackState() {
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());
}

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/org/schabi/newpipe/player/MainPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ public void stop(final boolean autoplayEnabled) {
@Override
public void onTaskRemoved(final Intent rootIntent) {
super.onTaskRemoved(rootIntent);
if (!playerImpl.videoPlayerSelected()) {
return;
}
onDestroy();
// Unload from memory completely
Runtime.getRuntime().halt(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ private boolean onSingleTapConfirmedInMain(final MotionEvent e) {

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;
}

Expand Down