Commit ee08e7dc by olly Committed by Oliver Woodman

Fix key and accessibility scrubbing

Issue: #5767
PiperOrigin-RevId: 243811443
parent 0be4bee2
...@@ -433,7 +433,7 @@ public class DefaultTimeBar extends View implements TimeBar { ...@@ -433,7 +433,7 @@ public class DefaultTimeBar extends View implements TimeBar {
public void setDuration(long duration) { public void setDuration(long duration) {
this.duration = duration; this.duration = duration;
if (scrubbing && duration == C.TIME_UNSET) { if (scrubbing && duration == C.TIME_UNSET) {
stopScrubbing(true); stopScrubbing(/* canceled= */ true);
} }
update(); update();
} }
...@@ -463,7 +463,7 @@ public class DefaultTimeBar extends View implements TimeBar { ...@@ -463,7 +463,7 @@ public class DefaultTimeBar extends View implements TimeBar {
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
super.setEnabled(enabled); super.setEnabled(enabled);
if (scrubbing && !enabled) { if (scrubbing && !enabled) {
stopScrubbing(true); stopScrubbing(/* canceled= */ true);
} }
} }
...@@ -487,8 +487,7 @@ public class DefaultTimeBar extends View implements TimeBar { ...@@ -487,8 +487,7 @@ public class DefaultTimeBar extends View implements TimeBar {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
if (isInSeekBar(x, y)) { if (isInSeekBar(x, y)) {
positionScrubber(x); positionScrubber(x);
startScrubbing(); startScrubbing(getScrubberPosition());
scrubPosition = getScrubberPosition();
update(); update();
invalidate(); invalidate();
return true; return true;
...@@ -503,10 +502,7 @@ public class DefaultTimeBar extends View implements TimeBar { ...@@ -503,10 +502,7 @@ public class DefaultTimeBar extends View implements TimeBar {
lastCoarseScrubXPosition = x; lastCoarseScrubXPosition = x;
positionScrubber(x); positionScrubber(x);
} }
scrubPosition = getScrubberPosition(); updateScrubbing(getScrubberPosition());
for (OnScrubListener listener : listeners) {
listener.onScrubMove(this, scrubPosition);
}
update(); update();
invalidate(); invalidate();
return true; return true;
...@@ -515,7 +511,7 @@ public class DefaultTimeBar extends View implements TimeBar { ...@@ -515,7 +511,7 @@ public class DefaultTimeBar extends View implements TimeBar {
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_CANCEL:
if (scrubbing) { if (scrubbing) {
stopScrubbing(event.getAction() == MotionEvent.ACTION_CANCEL); stopScrubbing(/* canceled= */ event.getAction() == MotionEvent.ACTION_CANCEL);
return true; return true;
} }
break; break;
...@@ -543,8 +539,7 @@ public class DefaultTimeBar extends View implements TimeBar { ...@@ -543,8 +539,7 @@ public class DefaultTimeBar extends View implements TimeBar {
case KeyEvent.KEYCODE_DPAD_CENTER: case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER: case KeyEvent.KEYCODE_ENTER:
if (scrubbing) { if (scrubbing) {
removeCallbacks(stopScrubbingRunnable); stopScrubbing(/* canceled= */ false);
stopScrubbingRunnable.run();
return true; return true;
} }
break; break;
...@@ -556,6 +551,15 @@ public class DefaultTimeBar extends View implements TimeBar { ...@@ -556,6 +551,15 @@ public class DefaultTimeBar extends View implements TimeBar {
} }
@Override @Override
protected void onFocusChanged(
boolean gainFocus, int direction, @Nullable Rect previouslyFocusedRect) {
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
if (scrubbing && !gainFocus) {
stopScrubbing(/* canceled= */ false);
}
}
@Override
protected void drawableStateChanged() { protected void drawableStateChanged() {
super.drawableStateChanged(); super.drawableStateChanged();
updateDrawableState(); updateDrawableState();
...@@ -637,11 +641,11 @@ public class DefaultTimeBar extends View implements TimeBar { ...@@ -637,11 +641,11 @@ public class DefaultTimeBar extends View implements TimeBar {
} }
if (action == AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD) { if (action == AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD) {
if (scrubIncrementally(-getPositionIncrement())) { if (scrubIncrementally(-getPositionIncrement())) {
stopScrubbing(false); stopScrubbing(/* canceled= */ false);
} }
} else if (action == AccessibilityNodeInfo.ACTION_SCROLL_FORWARD) { } else if (action == AccessibilityNodeInfo.ACTION_SCROLL_FORWARD) {
if (scrubIncrementally(getPositionIncrement())) { if (scrubIncrementally(getPositionIncrement())) {
stopScrubbing(false); stopScrubbing(/* canceled= */ false);
} }
} else { } else {
return false; return false;
...@@ -652,7 +656,8 @@ public class DefaultTimeBar extends View implements TimeBar { ...@@ -652,7 +656,8 @@ public class DefaultTimeBar extends View implements TimeBar {
// Internal methods. // Internal methods.
private void startScrubbing() { private void startScrubbing(long scrubPosition) {
this.scrubPosition = scrubPosition;
scrubbing = true; scrubbing = true;
setPressed(true); setPressed(true);
ViewParent parent = getParent(); ViewParent parent = getParent();
...@@ -660,11 +665,22 @@ public class DefaultTimeBar extends View implements TimeBar { ...@@ -660,11 +665,22 @@ public class DefaultTimeBar extends View implements TimeBar {
parent.requestDisallowInterceptTouchEvent(true); parent.requestDisallowInterceptTouchEvent(true);
} }
for (OnScrubListener listener : listeners) { for (OnScrubListener listener : listeners) {
listener.onScrubStart(this, getScrubberPosition()); listener.onScrubStart(this, scrubPosition);
}
}
private void updateScrubbing(long scrubPosition) {
if (this.scrubPosition == scrubPosition) {
return;
}
this.scrubPosition = scrubPosition;
for (OnScrubListener listener : listeners) {
listener.onScrubMove(this, scrubPosition);
} }
} }
private void stopScrubbing(boolean canceled) { private void stopScrubbing(boolean canceled) {
removeCallbacks(stopScrubbingRunnable);
scrubbing = false; scrubbing = false;
setPressed(false); setPressed(false);
ViewParent parent = getParent(); ViewParent parent = getParent();
...@@ -673,10 +689,34 @@ public class DefaultTimeBar extends View implements TimeBar { ...@@ -673,10 +689,34 @@ public class DefaultTimeBar extends View implements TimeBar {
} }
invalidate(); invalidate();
for (OnScrubListener listener : listeners) { for (OnScrubListener listener : listeners) {
listener.onScrubStop(this, getScrubberPosition(), canceled); listener.onScrubStop(this, scrubPosition, canceled);
} }
} }
/**
* Incrementally scrubs the position by {@code positionChange}.
*
* @param positionChange The change in the scrubber position, in milliseconds. May be negative.
* @return Returns whether the scrubber position changed.
*/
private boolean scrubIncrementally(long positionChange) {
if (duration <= 0) {
return false;
}
long previousPosition = scrubbing ? scrubPosition : position;
long scrubPosition = Util.constrainValue(previousPosition + positionChange, 0, duration);
if (scrubPosition == previousPosition) {
return false;
}
if (!scrubbing) {
startScrubbing(scrubPosition);
} else {
updateScrubbing(scrubPosition);
}
update();
return true;
}
private void update() { private void update() {
bufferedBar.set(progressBar); bufferedBar.set(progressBar);
scrubberBar.set(progressBar); scrubberBar.set(progressBar);
...@@ -793,31 +833,6 @@ public class DefaultTimeBar extends View implements TimeBar { ...@@ -793,31 +833,6 @@ public class DefaultTimeBar extends View implements TimeBar {
? (duration == C.TIME_UNSET ? 0 : (duration / keyCountIncrement)) : keyTimeIncrement; ? (duration == C.TIME_UNSET ? 0 : (duration / keyCountIncrement)) : keyTimeIncrement;
} }
/**
* Incrementally scrubs the position by {@code positionChange}.
*
* @param positionChange The change in the scrubber position, in milliseconds. May be negative.
* @return Returns whether the scrubber position changed.
*/
private boolean scrubIncrementally(long positionChange) {
if (duration <= 0) {
return false;
}
long scrubberPosition = getScrubberPosition();
scrubPosition = Util.constrainValue(scrubberPosition + positionChange, 0, duration);
if (scrubPosition == scrubberPosition) {
return false;
}
if (!scrubbing) {
startScrubbing();
}
for (OnScrubListener listener : listeners) {
listener.onScrubMove(this, scrubPosition);
}
update();
return true;
}
private boolean setDrawableLayoutDirection(Drawable drawable) { private boolean setDrawableLayoutDirection(Drawable drawable) {
return Util.SDK_INT >= 23 && setDrawableLayoutDirection(drawable, getLayoutDirection()); return Util.SDK_INT >= 23 && setDrawableLayoutDirection(drawable, getLayoutDirection());
} }
......
...@@ -1109,6 +1109,9 @@ public class PlayerControlView extends FrameLayout { ...@@ -1109,6 +1109,9 @@ public class PlayerControlView extends FrameLayout {
@Override @Override
public void onScrubStart(TimeBar timeBar, long position) { public void onScrubStart(TimeBar timeBar, long position) {
scrubbing = true; scrubbing = true;
if (positionView != null) {
positionView.setText(Util.getStringForTime(formatBuilder, formatter, position));
}
} }
@Override @Override
......
...@@ -114,7 +114,7 @@ public interface TimeBar { ...@@ -114,7 +114,7 @@ public interface TimeBar {
* Called when the user starts moving the scrubber. * Called when the user starts moving the scrubber.
* *
* @param timeBar The time bar. * @param timeBar The time bar.
* @param position The position of the scrubber, in milliseconds. * @param position The scrub position in milliseconds.
*/ */
void onScrubStart(TimeBar timeBar, long position); void onScrubStart(TimeBar timeBar, long position);
...@@ -122,7 +122,7 @@ public interface TimeBar { ...@@ -122,7 +122,7 @@ public interface TimeBar {
* Called when the user moves the scrubber. * Called when the user moves the scrubber.
* *
* @param timeBar The time bar. * @param timeBar The time bar.
* @param position The position of the scrubber, in milliseconds. * @param position The scrub position in milliseconds.
*/ */
void onScrubMove(TimeBar timeBar, long position); void onScrubMove(TimeBar timeBar, long position);
...@@ -130,11 +130,10 @@ public interface TimeBar { ...@@ -130,11 +130,10 @@ public interface TimeBar {
* Called when the user stops moving the scrubber. * Called when the user stops moving the scrubber.
* *
* @param timeBar The time bar. * @param timeBar The time bar.
* @param position The position of the scrubber, in milliseconds. * @param position The scrub position in milliseconds.
* @param canceled Whether scrubbing was canceled. * @param canceled Whether scrubbing was canceled.
*/ */
void onScrubStop(TimeBar timeBar, long position, boolean canceled); void onScrubStop(TimeBar timeBar, long position, boolean canceled);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment