Skip to content

Commit

Permalink
Allow multiple listeners on TimeBar
Browse files Browse the repository at this point in the history
Issue: #3406

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=174214296
  • Loading branch information
ojw28 committed Nov 2, 2017
1 parent 8c42479 commit 3021897
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.google.android.exoplayer2.util.Util;
import java.util.Formatter;
import java.util.Locale;
import java.util.concurrent.CopyOnWriteArraySet;

/**
* A time bar that shows a current position, buffered position, duration and ad markers.
Expand Down Expand Up @@ -198,8 +199,8 @@ public class DefaultTimeBar extends View implements TimeBar {
private final StringBuilder formatBuilder;
private final Formatter formatter;
private final Runnable stopScrubbingRunnable;
private final CopyOnWriteArraySet<OnScrubListener> listeners;

private OnScrubListener listener;
private int keyCountIncrement;
private long keyTimeIncrement;
private int lastCoarseScrubXPosition;
Expand Down Expand Up @@ -231,6 +232,7 @@ public DefaultTimeBar(Context context, AttributeSet attrs) {
playedAdMarkerPaint = new Paint();
scrubberPaint = new Paint();
scrubberPaint.setAntiAlias(true);
listeners = new CopyOnWriteArraySet<>();

// Calculate the dimensions and paints for drawn elements.
Resources res = context.getResources();
Expand Down Expand Up @@ -325,8 +327,13 @@ public void run() {
}

@Override
public void setListener(OnScrubListener listener) {
this.listener = listener;
public void addListener(OnScrubListener listener) {
listeners.add(listener);
}

@Override
public void removeListener(OnScrubListener listener) {
listeners.remove(listener);
}

@Override
Expand Down Expand Up @@ -421,7 +428,7 @@ public boolean onTouchEvent(MotionEvent event) {
positionScrubber(x);
}
scrubPosition = getScrubberPosition();
if (listener != null) {
for (OnScrubListener listener : listeners) {
listener.onScrubMove(this, scrubPosition);
}
update();
Expand Down Expand Up @@ -584,7 +591,7 @@ private void startScrubbing() {
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
if (listener != null) {
for (OnScrubListener listener : listeners) {
listener.onScrubStart(this, getScrubberPosition());
}
}
Expand All @@ -597,7 +604,7 @@ private void stopScrubbing(boolean canceled) {
parent.requestDisallowInterceptTouchEvent(false);
}
invalidate();
if (listener != null) {
for (OnScrubListener listener : listeners) {
listener.onScrubStop(this, getScrubberPosition(), canceled);
}
}
Expand Down Expand Up @@ -735,7 +742,7 @@ private boolean scrubIncrementally(long positionChange) {
if (!scrubbing) {
startScrubbing();
}
if (listener != null) {
for (OnScrubListener listener : listeners) {
listener.onScrubMove(this, scrubPosition);
}
update();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public PlaybackControlView(Context context, AttributeSet attrs, int defStyleAttr
positionView = findViewById(R.id.exo_position);
timeBar = findViewById(R.id.exo_progress);
if (timeBar != null) {
timeBar.setListener(componentListener);
timeBar.addListener(componentListener);
}
playButton = findViewById(R.id.exo_play);
if (playButton != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,23 @@
public interface TimeBar {

/**
* @see View#isEnabled()
* Adds a listener for scrubbing events.
*
* @param listener The listener to add.
*/
void setEnabled(boolean enabled);
void addListener(OnScrubListener listener);

/**
* Sets the listener for the scrubbing events.
* Removes a listener for scrubbing events.
*
* @param listener The listener for scrubbing events.
* @param listener The listener to remove.
*/
void removeListener(OnScrubListener listener);

/**
* @see View#isEnabled()
*/
void setListener(OnScrubListener listener);
void setEnabled(boolean enabled);

/**
* Sets the position increment for key presses and accessibility actions, in milliseconds.
Expand Down

0 comments on commit 3021897

Please sign in to comment.