1
0
mirror of https://github.com/TeamNewPipe/NewPipe.git synced 2024-11-22 11:02:35 +01:00

Player gestures: Fix respecting brightness-volume-gesture settings

This commit is contained in:
vkay94 2020-11-20 21:08:02 +01:00
parent 66d15ea635
commit b5321152fd

View File

@ -36,15 +36,10 @@ public class PlayerGestureListener
private static final String TAG = ".PlayerGestureListener"; private static final String TAG = ".PlayerGestureListener";
private static final boolean DEBUG = BasePlayer.DEBUG; private static final boolean DEBUG = BasePlayer.DEBUG;
private final boolean isVolumeGestureEnabled;
private final boolean isBrightnessGestureEnabled;
private final int maxVolume; private final int maxVolume;
public PlayerGestureListener(final VideoPlayerImpl playerImpl, final MainPlayer service) { public PlayerGestureListener(final VideoPlayerImpl playerImpl, final MainPlayer service) {
super(playerImpl, service); super(playerImpl, service);
isVolumeGestureEnabled = PlayerHelper.isVolumeGestureEnabled(service);
isBrightnessGestureEnabled = PlayerHelper.isBrightnessGestureEnabled(service);
maxVolume = playerImpl.getAudioReactor().getMaxVolume(); maxVolume = playerImpl.getAudioReactor().getMaxVolume();
} }
@ -110,10 +105,20 @@ public class PlayerGestureListener
+ portion + "]"); + portion + "]");
} }
if (playerType == MainPlayer.PlayerType.VIDEO) { if (playerType == MainPlayer.PlayerType.VIDEO) {
if (portion == DisplayPortion.LEFT_HALF) { final boolean isBrightnessGestureEnabled =
onScrollMainBrightness(distanceX, distanceY); PlayerHelper.isBrightnessGestureEnabled(service);
final boolean isVolumeGestureEnabled = PlayerHelper.isVolumeGestureEnabled(service);
} else /* DisplayPortion.RIGHT_HALF */ { if (isBrightnessGestureEnabled && isVolumeGestureEnabled) {
if (portion == DisplayPortion.LEFT_HALF) {
onScrollMainBrightness(distanceX, distanceY);
} else /* DisplayPortion.RIGHT_HALF */ {
onScrollMainVolume(distanceX, distanceY);
}
} else if (isBrightnessGestureEnabled) {
onScrollMainBrightness(distanceX, distanceY);
} else if (isVolumeGestureEnabled) {
onScrollMainVolume(distanceX, distanceY); onScrollMainVolume(distanceX, distanceY);
} }
@ -132,75 +137,71 @@ public class PlayerGestureListener
} }
private void onScrollMainVolume(final float distanceX, final float distanceY) { private void onScrollMainVolume(final float distanceX, final float distanceY) {
if (isVolumeGestureEnabled) { playerImpl.getVolumeProgressBar().incrementProgressBy((int) distanceY);
playerImpl.getVolumeProgressBar().incrementProgressBy((int) distanceY); final float currentProgressPercent = (float) playerImpl
final float currentProgressPercent = (float) playerImpl .getVolumeProgressBar().getProgress() / playerImpl.getMaxGestureLength();
.getVolumeProgressBar().getProgress() / playerImpl.getMaxGestureLength(); final int currentVolume = (int) (maxVolume * currentProgressPercent);
final int currentVolume = (int) (maxVolume * currentProgressPercent); playerImpl.getAudioReactor().setVolume(currentVolume);
playerImpl.getAudioReactor().setVolume(currentVolume);
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "onScroll().volumeControl, currentVolume = " + currentVolume); Log.d(TAG, "onScroll().volumeControl, currentVolume = " + currentVolume);
} }
playerImpl.getVolumeImageView().setImageDrawable( playerImpl.getVolumeImageView().setImageDrawable(
AppCompatResources.getDrawable(service, currentProgressPercent <= 0 AppCompatResources.getDrawable(service, currentProgressPercent <= 0
? R.drawable.ic_volume_off_white_24dp ? R.drawable.ic_volume_off_white_24dp
: currentProgressPercent < 0.25 ? R.drawable.ic_volume_mute_white_24dp : currentProgressPercent < 0.25 ? R.drawable.ic_volume_mute_white_24dp
: currentProgressPercent < 0.75 ? R.drawable.ic_volume_down_white_24dp : currentProgressPercent < 0.75 ? R.drawable.ic_volume_down_white_24dp
: R.drawable.ic_volume_up_white_24dp) : R.drawable.ic_volume_up_white_24dp)
); );
if (playerImpl.getVolumeRelativeLayout().getVisibility() != View.VISIBLE) { if (playerImpl.getVolumeRelativeLayout().getVisibility() != View.VISIBLE) {
animateView(playerImpl.getVolumeRelativeLayout(), SCALE_AND_ALPHA, true, 200); animateView(playerImpl.getVolumeRelativeLayout(), SCALE_AND_ALPHA, true, 200);
} }
if (playerImpl.getBrightnessRelativeLayout().getVisibility() == View.VISIBLE) { if (playerImpl.getBrightnessRelativeLayout().getVisibility() == View.VISIBLE) {
playerImpl.getBrightnessRelativeLayout().setVisibility(View.GONE); playerImpl.getBrightnessRelativeLayout().setVisibility(View.GONE);
}
} }
} }
private void onScrollMainBrightness(final float distanceX, final float distanceY) { private void onScrollMainBrightness(final float distanceX, final float distanceY) {
if (isBrightnessGestureEnabled) { final Activity parent = playerImpl.getParentActivity();
final Activity parent = playerImpl.getParentActivity(); if (parent == null) {
if (parent == null) { return;
return; }
}
final Window window = parent.getWindow(); final Window window = parent.getWindow();
final WindowManager.LayoutParams layoutParams = window.getAttributes(); final WindowManager.LayoutParams layoutParams = window.getAttributes();
final ProgressBar bar = playerImpl.getBrightnessProgressBar(); final ProgressBar bar = playerImpl.getBrightnessProgressBar();
final float oldBrightness = layoutParams.screenBrightness; final float oldBrightness = layoutParams.screenBrightness;
bar.setProgress((int) (bar.getMax() * Math.max(0, Math.min(1, oldBrightness)))); bar.setProgress((int) (bar.getMax() * Math.max(0, Math.min(1, oldBrightness))));
bar.incrementProgressBy((int) distanceY); bar.incrementProgressBy((int) distanceY);
final float currentProgressPercent = (float) bar.getProgress() / bar.getMax(); final float currentProgressPercent = (float) bar.getProgress() / bar.getMax();
layoutParams.screenBrightness = currentProgressPercent; layoutParams.screenBrightness = currentProgressPercent;
window.setAttributes(layoutParams); window.setAttributes(layoutParams);
// Save current brightness level // Save current brightness level
PlayerHelper.setScreenBrightness(parent, currentProgressPercent); PlayerHelper.setScreenBrightness(parent, currentProgressPercent);
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "onScroll().brightnessControl, " Log.d(TAG, "onScroll().brightnessControl, "
+ "currentBrightness = " + currentProgressPercent); + "currentBrightness = " + currentProgressPercent);
} }
playerImpl.getBrightnessImageView().setImageDrawable( playerImpl.getBrightnessImageView().setImageDrawable(
AppCompatResources.getDrawable(service, AppCompatResources.getDrawable(service,
currentProgressPercent < 0.25 currentProgressPercent < 0.25
? R.drawable.ic_brightness_low_white_24dp ? R.drawable.ic_brightness_low_white_24dp
: currentProgressPercent < 0.75 : currentProgressPercent < 0.75
? R.drawable.ic_brightness_medium_white_24dp ? R.drawable.ic_brightness_medium_white_24dp
: R.drawable.ic_brightness_high_white_24dp) : R.drawable.ic_brightness_high_white_24dp)
); );
if (playerImpl.getBrightnessRelativeLayout().getVisibility() != View.VISIBLE) { if (playerImpl.getBrightnessRelativeLayout().getVisibility() != View.VISIBLE) {
animateView(playerImpl.getBrightnessRelativeLayout(), SCALE_AND_ALPHA, true, 200); animateView(playerImpl.getBrightnessRelativeLayout(), SCALE_AND_ALPHA, true, 200);
} }
if (playerImpl.getVolumeRelativeLayout().getVisibility() == View.VISIBLE) { if (playerImpl.getVolumeRelativeLayout().getVisibility() == View.VISIBLE) {
playerImpl.getVolumeRelativeLayout().setVisibility(View.GONE); playerImpl.getVolumeRelativeLayout().setVisibility(View.GONE);
}
} }
} }