Skip to content

Commit

Permalink
Small fixes of issues with brightness, background playback, gestures
Browse files Browse the repository at this point in the history
  • Loading branch information
avently committed Sep 3, 2020
1 parent a84b54f commit a3d366a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
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,38 @@ 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()) {
// 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
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

0 comments on commit a3d366a

Please sign in to comment.