Changes according to the code review

This commit is contained in:
Arnold Szabo 2019-02-13 19:31:11 +02:00
parent 62e6455dce
commit ecfce46296
3 changed files with 19 additions and 22 deletions

View File

@ -205,6 +205,7 @@ public class DefaultTimeBar extends View implements TimeBar {
private final CopyOnWriteArraySet<OnScrubListener> listeners; private final CopyOnWriteArraySet<OnScrubListener> listeners;
private final int[] locationOnScreen; private final int[] locationOnScreen;
private final Point touchPosition; private final Point touchPosition;
private final float density;
private int keyCountIncrement; private int keyCountIncrement;
private long keyTimeIncrement; private long keyTimeIncrement;
@ -219,8 +220,6 @@ public class DefaultTimeBar extends View implements TimeBar {
private @Nullable long[] adGroupTimesMs; private @Nullable long[] adGroupTimesMs;
private @Nullable boolean[] playedAdGroups; private @Nullable boolean[] playedAdGroups;
private int densityDpi;
/** Creates a new time bar. */ /** Creates a new time bar. */
// Suppress warnings due to usage of View methods in the constructor. // Suppress warnings due to usage of View methods in the constructor.
@SuppressWarnings("nullness:method.invocation.invalid") @SuppressWarnings("nullness:method.invocation.invalid")
@ -244,7 +243,7 @@ public class DefaultTimeBar extends View implements TimeBar {
// Calculate the dimensions and paints for drawn elements. // Calculate the dimensions and paints for drawn elements.
Resources res = context.getResources(); Resources res = context.getResources();
DisplayMetrics displayMetrics = res.getDisplayMetrics(); DisplayMetrics displayMetrics = res.getDisplayMetrics();
densityDpi = displayMetrics.densityDpi; density = displayMetrics.density;
fineScrubYThreshold = dpToPx(displayMetrics, FINE_SCRUB_Y_THRESHOLD_DP); fineScrubYThreshold = dpToPx(displayMetrics, FINE_SCRUB_Y_THRESHOLD_DP);
int defaultBarHeight = dpToPx(displayMetrics, DEFAULT_BAR_HEIGHT_DP); int defaultBarHeight = dpToPx(displayMetrics, DEFAULT_BAR_HEIGHT_DP);
int defaultTouchTargetHeight = dpToPx(displayMetrics, DEFAULT_TOUCH_TARGET_HEIGHT_DP); int defaultTouchTargetHeight = dpToPx(displayMetrics, DEFAULT_TOUCH_TARGET_HEIGHT_DP);
@ -451,8 +450,8 @@ public class DefaultTimeBar extends View implements TimeBar {
} }
@Override @Override
public int getTimeBarWidth() { public int getTimeBarWidthDp() {
return pxToDp(densityDpi, getWidth()); return pxToDp(density, getWidth());
} }
// View methods. // View methods.
@ -844,7 +843,7 @@ public class DefaultTimeBar extends View implements TimeBar {
return (int) (dps * displayMetrics.density + 0.5f); return (int) (dps * displayMetrics.density + 0.5f);
} }
private static int pxToDp(int densityDpi, int px) { private static int pxToDp(float density, int px) {
return (int) (px / ((float) densityDpi / DisplayMetrics.DENSITY_DEFAULT)); return (int) (px / density);
} }
} }

View File

@ -836,11 +836,11 @@ public class PlayerControlView extends FrameLayout {
long delayMs; long delayMs;
if (player.getPlayWhenReady() && playbackState == Player.STATE_READY) { if (player.getPlayWhenReady() && playbackState == Player.STATE_READY) {
float playbackSpeed = player.getPlaybackParameters().speed; float playbackSpeed = player.getPlaybackParameters().speed;
if (playbackSpeed <= 0.1f) { int timeBarWidth = timeBar.getTimeBarWidthDp();
delayMs = 1000;
} else if (playbackSpeed <= 5f) {
int timeBarWidth = timeBar.getTimeBarWidth(); if (timeBarWidth == 0 || duration == 0) {
delayMs = MAX_UPDATE_FREQUENCY_MS;
} else {
// Calculate how many updates needs to be done with DEFAULT_UPDATE_DP // Calculate how many updates needs to be done with DEFAULT_UPDATE_DP
// to fill up the time bar // to fill up the time bar
int numberOfUpdates = timeBarWidth / DEFAULT_UPDATE_DP; int numberOfUpdates = timeBarWidth / DEFAULT_UPDATE_DP;
@ -848,13 +848,6 @@ public class PlayerControlView extends FrameLayout {
// Calculate the designated update interval, taking duration into consideration as well // Calculate the designated update interval, taking duration into consideration as well
long mediaTimeUpdatePeriodMs = duration / numberOfUpdates; long mediaTimeUpdatePeriodMs = duration / numberOfUpdates;
// Limit the designated update interval, to avoid too frequent / infrequent updates
if (mediaTimeUpdatePeriodMs < MIN_UPDATE_FREQUENCY_MS) {
mediaTimeUpdatePeriodMs = MIN_UPDATE_FREQUENCY_MS;
} else if (mediaTimeUpdatePeriodMs >= MAX_UPDATE_FREQUENCY_MS) {
mediaTimeUpdatePeriodMs = MAX_UPDATE_FREQUENCY_MS;
}
// Calculate the delay needed from the current position until the next update is due // Calculate the delay needed from the current position until the next update is due
long mediaTimeDelayMs = mediaTimeUpdatePeriodMs - (position % mediaTimeUpdatePeriodMs); long mediaTimeDelayMs = mediaTimeUpdatePeriodMs - (position % mediaTimeUpdatePeriodMs);
@ -866,11 +859,16 @@ public class PlayerControlView extends FrameLayout {
// Calculate the delay until the next update (in real time), taking // Calculate the delay until the next update (in real time), taking
// playbackSpeed into consideration // playbackSpeed into consideration
delayMs = playbackSpeed == 1 ? mediaTimeDelayMs : (long) (mediaTimeDelayMs / playbackSpeed); delayMs = playbackSpeed == 1 ? mediaTimeDelayMs : (long) (mediaTimeDelayMs / playbackSpeed);
} else {
delayMs = 200; // Limit the delay if needed, to avoid too frequent / infrequent updates
if (delayMs < MIN_UPDATE_FREQUENCY_MS) {
delayMs = MIN_UPDATE_FREQUENCY_MS;
} else if (delayMs >= MAX_UPDATE_FREQUENCY_MS) {
delayMs = MAX_UPDATE_FREQUENCY_MS;
}
} }
} else { } else {
delayMs = 1000; delayMs = MAX_UPDATE_FREQUENCY_MS;
} }
postDelayed(updateProgressAction, delayMs); postDelayed(updateProgressAction, delayMs);
} }

View File

@ -102,7 +102,7 @@ public interface TimeBar {
* *
* @return Width of time bar in dps * @return Width of time bar in dps
*/ */
int getTimeBarWidth(); int getTimeBarWidthDp();
/** /**
* Listener for scrubbing events. * Listener for scrubbing events.