Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Fixes #2948 Added Handler that automatically hides volume slider afte…
Browse files Browse the repository at this point in the history
…r 1 second of no hover over volume. Fixes #2948
  • Loading branch information
daron-walters committed Apr 19, 2020
1 parent a1d0335 commit bb4a6f7
Showing 1 changed file with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
Expand All @@ -22,6 +23,8 @@
import org.mozilla.vrbrowser.ui.views.VolumeControl;
import org.mozilla.vrbrowser.ui.widgets.menus.VideoProjectionMenuWidget;

import java.util.Timer;

public class MediaControlsWidget extends UIWidget implements MediaElement.Delegate {

private Media mMedia;
Expand All @@ -42,6 +45,10 @@ public class MediaControlsWidget extends UIWidget implements MediaElement.Delega
private boolean mPlayOnSeekEnd;
private Rect mOffsetViewBounds;
private VideoProjectionMenuWidget mProjectionMenu;
static long VOLUME_SLIDER_CHECK_DELAY = 1000;
private Handler mVolumeCtrlHandler = new Handler();
private boolean mHideVolumeSlider = false;
private Runnable mVolumeCtrlRunnable;

public MediaControlsWidget(Context aContext) {
super(aContext);
Expand Down Expand Up @@ -75,7 +82,12 @@ private void initialize(Context aContext) {
mMutedIcon = aContext.getDrawable(R.drawable.ic_icon_media_volume_muted);
mVolumeIcon = aContext.getDrawable(R.drawable.ic_icon_media_volume);
mOffsetViewBounds = new Rect();

mVolumeCtrlRunnable = () -> {
if ((mHideVolumeSlider) && (mVolumeControl.getVisibility() == View.VISIBLE)) {
mVolumeControl.setVisibility(View.GONE);
stopVolumeCtrlHandler();
}
};
mMediaPlayButton.setOnClickListener(v -> {
if (mMedia.isEnded()) {
mMedia.seek(0);
Expand Down Expand Up @@ -179,8 +191,8 @@ public void onSeekPreview(String aText, double aRatio) {
childView.getDrawingRect(mOffsetViewBounds);
MediaControlsWidget.this.offsetDescendantRectToMyCoords(childView, mOffsetViewBounds);

FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)mMediaSeekLabel.getLayoutParams();
params.setMarginStart(mOffsetViewBounds.left + (int)(aRatio * mOffsetViewBounds.width()) - mMediaSeekLabel.getMeasuredWidth() / 2);
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mMediaSeekLabel.getLayoutParams();
params.setMarginStart(mOffsetViewBounds.left + (int) (aRatio * mOffsetViewBounds.width()) - mMediaSeekLabel.getMeasuredWidth() / 2);
mMediaSeekLabel.setLayoutParams(params);
}
});
Expand All @@ -197,34 +209,47 @@ public void onSeekPreview(String aText, double aRatio) {
if (mMedia == null) {
return false;
}
if(event.getX()<0)
{
mVolumeControl.setVisibility(View.GONE);
if (event.getX() < 0) {
this.mHideVolumeSlider = true;
this.startVolumeCtrlHandler();
return false;
}
if (event.getAction() == MotionEvent.ACTION_HOVER_MOVE || event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
float threshold = mMediaVolumeButton.getX();
boolean isVisible = mVolumeControl.getVisibility() == View.VISIBLE;
boolean makeVisible = event.getX() >= threshold;
if (isVisible != makeVisible) {
mVolumeControl.setVisibility(makeVisible ? View.VISIBLE : View.GONE);
if (makeVisible) {
mVolumeControl.setVisibility(View.VISIBLE);
this.mHideVolumeSlider = false;
this.stopVolumeCtrlHandler();
} else {
this.mHideVolumeSlider = true;
this.startVolumeCtrlHandler();
}
}
} else if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT && !mMediaVolumeButton.isHovered() && this.isInTouchMode()) {
mVolumeControl.setVisibility(View.INVISIBLE);
//mVolumeControl.setVisibility(View.INVISIBLE);
this.mHideVolumeSlider = true;
this.startVolumeCtrlHandler();
}
return false;
});

mMediaVolumeButton.setOnHoverListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER || event.getAction() == MotionEvent.ACTION_HOVER_MOVE) {
mVolumeControl.setVisibility(View.VISIBLE);
this.mHideVolumeSlider = false;
this.stopVolumeCtrlHandler();
}
return false;
});

mMediaProjectionButton.setOnHoverListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER || event.getAction() == MotionEvent.ACTION_HOVER_MOVE) {
mVolumeControl.setVisibility(View.INVISIBLE);
// mVolumeControl.setVisibility(View.INVISIBLE);
this.mHideVolumeSlider = true;
this.startVolumeCtrlHandler();
}
return false;
});
Expand Down Expand Up @@ -327,7 +352,7 @@ public void onMetadataChange(MediaElement mediaElement, MediaElement.Metadata me
@Override
public void onLoadProgress(MediaElement mediaElement, MediaElement.LoadProgressInfo progressInfo) {
if (progressInfo.buffered != null) {
mSeekBar.setBuffered(progressInfo.buffered[progressInfo.buffered.length -1].end);
mSeekBar.setBuffered(progressInfo.buffered[progressInfo.buffered.length - 1].end);
}
}

Expand Down Expand Up @@ -358,4 +383,17 @@ public void onFullscreenChange(MediaElement mediaElement, boolean fullscreen) {
public void onError(MediaElement mediaElement, int code) {
}

private void startVolumeCtrlHandler() {
mVolumeCtrlHandler.postDelayed(mVolumeCtrlRunnable, VOLUME_SLIDER_CHECK_DELAY);
}

public void stopVolumeCtrlHandler() {
mVolumeCtrlHandler.removeCallbacks(mVolumeCtrlRunnable);
}

public void restart() {
mVolumeCtrlHandler.removeCallbacks(mVolumeCtrlRunnable);
mVolumeCtrlHandler.postDelayed(mVolumeCtrlRunnable, VOLUME_SLIDER_CHECK_DELAY);
}

}

0 comments on commit bb4a6f7

Please sign in to comment.