Skip to content

Commit

Permalink
Show played ad groups
Browse files Browse the repository at this point in the history
By default played ad groups are shown faded out. This helps the user know
whether they will see an ad when they seek to a given position.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=161098491
  • Loading branch information
andrewlewis authored and ojw28 committed Jul 10, 2017
1 parent a0aa9a3 commit 4180b96
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class DefaultTimeBar extends View implements TimeBar {
private final Paint bufferedPaint;
private final Paint unplayedPaint;
private final Paint adMarkerPaint;
private final Paint playedAdMarkerPaint;
private final Paint scrubberPaint;
private final int barHeight;
private final int touchTargetHeight;
Expand Down Expand Up @@ -103,6 +104,7 @@ public class DefaultTimeBar extends View implements TimeBar {
private long bufferedPosition;
private int adGroupCount;
private long[] adGroupTimesMs;
private boolean[] playedAdGroups;

/**
* Creates a new time bar.
Expand All @@ -117,6 +119,7 @@ public DefaultTimeBar(Context context, AttributeSet attrs) {
bufferedPaint = new Paint();
unplayedPaint = new Paint();
adMarkerPaint = new Paint();
playedAdMarkerPaint = new Paint();
scrubberPaint = new Paint();
scrubberPaint.setAntiAlias(true);

Expand Down Expand Up @@ -155,11 +158,14 @@ public DefaultTimeBar(Context context, AttributeSet attrs) {
getDefaultUnplayedColor(playedColor));
int adMarkerColor = a.getInt(R.styleable.DefaultTimeBar_ad_marker_color,
DEFAULT_AD_MARKER_COLOR);
int playedAdMarkerColor = a.getInt(R.styleable.DefaultTimeBar_played_ad_marker_color,
getDefaultPlayedAdMarkerColor(adMarkerColor));
playedPaint.setColor(playedColor);
scrubberPaint.setColor(scrubberColor);
bufferedPaint.setColor(bufferedColor);
unplayedPaint.setColor(unplayedColor);
adMarkerPaint.setColor(adMarkerColor);
playedAdMarkerPaint.setColor(playedAdMarkerColor);
} finally {
a.recycle();
}
Expand Down Expand Up @@ -238,10 +244,13 @@ public void setDuration(long duration) {
}

@Override
public void setAdGroupTimesMs(@Nullable long[] adGroupTimesMs, int adGroupCount) {
Assertions.checkArgument(adGroupCount == 0 || adGroupTimesMs != null);
public void setAdGroupTimesMs(@Nullable long[] adGroupTimesMs, @Nullable boolean[] playedAdGroups,
int adGroupCount) {
Assertions.checkArgument(adGroupCount == 0
|| (adGroupTimesMs != null && playedAdGroups != null));
this.adGroupCount = adGroupCount;
this.adGroupTimesMs = adGroupTimesMs;
this.playedAdGroups = playedAdGroups;
update();
}

Expand Down Expand Up @@ -524,7 +533,8 @@ private void drawTimeBar(Canvas canvas) {
(int) (progressBar.width() * adGroupTimeMs / duration) - adMarkerOffset;
int markerLeft = progressBar.left + Math.min(progressBar.width() - adMarkerWidth,
Math.max(0, markerPositionOffset));
canvas.drawRect(markerLeft, barTop, markerLeft + adMarkerWidth, barBottom, adMarkerPaint);
Paint paint = playedAdGroups[i] ? playedAdMarkerPaint : adMarkerPaint;
canvas.drawRect(markerLeft, barTop, markerLeft + adMarkerWidth, barBottom, paint);
}
}

Expand Down Expand Up @@ -590,4 +600,8 @@ private static int getDefaultBufferedColor(int playedColor) {
return 0xCC000000 | (playedColor & 0x00FFFFFF);
}

private static int getDefaultPlayedAdMarkerColor(int adMarkerColor) {
return 0x33000000 | (adMarkerColor & 0x00FFFFFF);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ public boolean dispatchSetRepeatMode(ExoPlayer player, @ExoPlayer.RepeatMode int
private @RepeatToggleModes int repeatToggleModes;
private long hideAtMs;
private long[] adGroupTimesMs;
private boolean[] playedAdGroups;

private final Runnable updateProgressAction = new Runnable() {
@Override
Expand Down Expand Up @@ -364,6 +365,7 @@ public PlaybackControlView(Context context, AttributeSet attrs, int defStyleAttr
formatBuilder = new StringBuilder();
formatter = new Formatter(formatBuilder, Locale.getDefault());
adGroupTimesMs = new long[0];
playedAdGroups = new boolean[0];
componentListener = new ComponentListener();
controlDispatcher = DEFAULT_CONTROL_DISPATCHER;

Expand Down Expand Up @@ -724,10 +726,6 @@ private void updateProgress() {
}
for (int adGroupIndex = 0; adGroupIndex < period.getAdGroupCount(); adGroupIndex++) {
long adGroupTimeUs = period.getAdGroupTimeUs(adGroupIndex);
if (period.hasPlayedAdGroup(adGroupIndex)) {
// Don't show played ad groups.
continue;
}
if (adGroupTimeUs == C.TIME_END_OF_SOURCE) {
adGroupTimeUs = periodDurationUs;
}
Expand All @@ -736,10 +734,13 @@ private void updateProgress() {
}
if (adGroupTimeUs >= 0 && adGroupTimeUs <= window.durationUs) {
if (adGroupTimesMsCount == adGroupTimesMs.length) {
adGroupTimesMs = Arrays.copyOf(adGroupTimesMs,
adGroupTimesMs.length == 0 ? 1 : adGroupTimesMs.length * 2);
int newLength = adGroupTimesMs.length == 0 ? 1 : adGroupTimesMs.length * 2;
adGroupTimesMs = Arrays.copyOf(adGroupTimesMs, newLength);
playedAdGroups = Arrays.copyOf(playedAdGroups, newLength);
}
adGroupTimesMs[adGroupTimesMsCount++] = C.usToMs(durationUs + adGroupTimeUs);
adGroupTimesMs[adGroupTimesMsCount] = C.usToMs(durationUs + adGroupTimeUs);
playedAdGroups[adGroupTimesMsCount] = period.hasPlayedAdGroup(adGroupIndex);
adGroupTimesMsCount++;
}
}
if (i < periodIndex) {
Expand All @@ -757,7 +758,7 @@ private void updateProgress() {
bufferedPosition += player.getBufferedPosition();
}
if (timeBar != null) {
timeBar.setAdGroupTimesMs(adGroupTimesMs, adGroupTimesMsCount);
timeBar.setAdGroupTimesMs(adGroupTimesMs, playedAdGroups, adGroupTimesMsCount);
}
} else {
position = player.getCurrentPosition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,17 @@ public interface TimeBar {
void setDuration(long duration);

/**
* Sets the times of ad groups.
* Sets the times of ad groups and whether each ad group has been played.
*
* @param adGroupTimesMs An array where the first {@code adGroupCount} elements are the times of
* ad groups in milliseconds. May be {@code null} if there are no ad groups.
* @param playedAdGroups An array where the first {@code adGroupCount} elements indicate whether
* the corresponding ad groups have been played. May be {@code null} if there are no ad
* groups.
* @param adGroupCount The number of ad groups.
*/
void setAdGroupTimesMs(@Nullable long[] adGroupTimesMs, int adGroupCount);
void setAdGroupTimesMs(@Nullable long[] adGroupTimesMs, @Nullable boolean[] playedAdGroups,
int adGroupCount);

/**
* Listener for scrubbing events.
Expand Down
1 change: 1 addition & 0 deletions library/ui/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<attr name="buffered_color" format="color"/>
<attr name="unplayed_color" format="color"/>
<attr name="ad_marker_color" format="color"/>
<attr name="played_ad_marker_color" format="color"/>
</declare-styleable>

</resources>

0 comments on commit 4180b96

Please sign in to comment.