Repository navigation
feat(rendering): expose winning creative size on BannerView #1007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
5e5fbe0
b6e5326
78071c5
46de0e5
63f68c4
835a955
5268c60
caacfa1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,9 @@ public class BannerView extends FrameLayout { | |
| private boolean isBidAdLoaded; | ||
| private boolean expired; | ||
| private boolean isRefreshStopped; | ||
| // Size of the creative currently in the slot, captured when it is displayed. 0 x 0 when unknown. | ||
| private int creativeWidth; | ||
| private int creativeHeight; | ||
|
|
||
| private final ScreenStateReceiver screenStateReceiver = new ScreenStateReceiver(); | ||
|
|
||
|
|
@@ -132,6 +135,8 @@ public void onAdDisplayed() { | |
|
|
||
| @Override | ||
| public void onAdFailed(AdException exception) { | ||
| // The Prebid creative failed to render, so there is no creative size to report. | ||
| clearCreativeSize(); | ||
| if (bannerViewListener != null) { | ||
| bannerViewListener.onAdFailed(BannerView.this, exception); | ||
| } | ||
|
|
@@ -227,8 +232,20 @@ public void onPrebidSdkWin() { | |
|
|
||
| @Override | ||
| public void onAdServerWin(View view) { | ||
| onAdServerWin(view, null); | ||
| } | ||
|
|
||
| @Override | ||
| public void onAdServerWin(View view, @Nullable AdSize adSize) { | ||
| markPrimaryAdRequestFinished(); | ||
|
|
||
| // Report the ad server's creative, never the Prebid bid that lost to it. | ||
| if (view != null && adSize != null) { | ||
| setCreativeSize(adSize.getWidth(), adSize.getHeight()); | ||
| } else { | ||
| clearCreativeSize(); | ||
| } | ||
|
|
||
| notifyAdLoadedListener(); | ||
| displayAdServerView(view); | ||
| } | ||
|
|
@@ -363,6 +380,7 @@ public void destroy() { | |
| displayView.destroy(); | ||
| } | ||
| cancelExpiration(); | ||
| clearCreativeSize(); | ||
| bidRequesterListener = null; | ||
|
|
||
| PrebidMobilePluginRegister.getInstance().unregisterEventListener(adUnitConfig.getFingerprint()); | ||
|
|
@@ -552,6 +570,14 @@ private void displayPrebidView() { | |
|
|
||
| removeAllViews(); | ||
|
|
||
| // Captured before the creative is created, so it is set by the time onAdLoaded fires. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small doc mismatch. The Javadoc says the value doesn't change until the next creative is displayed, and that a refresh still loading keeps reporting the creative on screen. But on a Prebid win this runs right after Reading it in
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 835a955. |
||
| final Bid winnerBid = getWinnerBid(); | ||
| if (winnerBid != null) { | ||
| setCreativeSize(winnerBid.getWidth(), winnerBid.getHeight()); | ||
| } else { | ||
| clearCreativeSize(); | ||
| } | ||
|
|
||
| displayView = new DisplayView(getContext(), displayViewListener, displayVideoListener, adUnitConfig, bidResponse); | ||
| if (bidResponse.getPreferredPluginRendererName() == PrebidMobilePluginRegister.PREBID_MOBILE_RENDERER_NAME) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correction to my earlier comment on the plugin-renderer This predates the PR, and switching to
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 835a955. It now uses |
||
| final Pair<Integer, Integer> sizePair = bidResponse.getWinningBidWidthHeightPairDips(getContext()); | ||
|
|
@@ -657,6 +683,7 @@ private void expireAdIfNeeded() { | |
| displayView = null; | ||
| } | ||
| removeAllViews(); | ||
| clearCreativeSize(); | ||
| } | ||
|
|
||
| if (bannerViewListener != null) { | ||
|
|
@@ -672,6 +699,56 @@ public BidResponse getBidResponse() { | |
| return bidResponse; | ||
| } | ||
|
|
||
| @Nullable | ||
| final Bid getWinnerBid() { | ||
| return bidResponse != null ? bidResponse.getWinningBid() : null; | ||
| } | ||
|
|
||
| /** | ||
| * Declared width, in dp, of the creative currently displayed by this view. Read it in | ||
| * {@link BannerViewListener#onAdLoaded(BannerView)} to learn which of several requested | ||
| * sizes won, e.g. 300x250 or 320x50. | ||
| * <p> | ||
| * The value is captured when a creative is displayed and does not change until the next one | ||
| * is displayed, so a refresh that is still loading or that fails keeps reporting the creative | ||
| * on screen. | ||
| * <ul> | ||
| * <li>Prebid win: the winning bid's {@code w}. This is the size the bid declares, not a | ||
| * measured one: a third party plugin renderer lays its view out to match this view, and | ||
| * MRAID {@code resize()} or {@code expand()} can change the creative after it loads.</li> | ||
| * <li>Ad server win: the size the ad server's event handler reports for its creative | ||
| * (e.g. the GAM event handler), or 0 when the handler does not report one.</li> | ||
| * <li>0 when nothing is displayed: before the first ad, after a creative fails to | ||
| * render, after an expired ad is removed, and after {@link #destroy()}.</li> | ||
| * </ul> | ||
| */ | ||
| public int getCreativeWidth() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0 means two different things here. It's returned when nothing is displayed, and also when a creative is displayed but its size is unknown. That includes a GAM fluid ad, a handler that doesn't report a size (Next-Gen today, or any custom One fix that also simplifies the code: keep a single
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 835a955. There is now a single |
||
| return creativeWidth; | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When GAM wins, this returns the size of the bid that lost.
The PR mentions iOS parity, but iOS doesn't have this gap: the size travels with the view ( Suggested fix (also resolves the staleness comment below): record the size where a view is actually displayed rather than deriving it here — in
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 46de0e5. The size is now captured where a view is displayed. |
||
|
|
||
| /** | ||
| * Declared height, in dp, of the creative currently displayed by this view, or 0 when it is | ||
| * unknown. See {@link #getCreativeWidth()} for when the value is captured and cleared. | ||
| */ | ||
| public int getCreativeHeight() { | ||
| return creativeHeight; | ||
| } | ||
|
|
||
| private void setCreativeSize(int width, int height) { | ||
| // A non positive dimension, such as a fluid ad server size, is not a usable size. | ||
| if (width <= 0 || height <= 0) { | ||
| clearCreativeSize(); | ||
| return; | ||
| } | ||
| creativeWidth = width; | ||
| creativeHeight = height; | ||
| } | ||
|
|
||
| private void clearCreativeSize() { | ||
| creativeWidth = 0; | ||
| creativeHeight = 0; | ||
| } | ||
|
|
||
| @Nullable | ||
| public String getImpOrtbConfig() { | ||
| return adUnitConfig.getImpOrtbConfig(); | ||
|
|
@@ -706,11 +783,6 @@ final void setBidResponse(BidResponse response) { | |
| bidResponse = response; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| final Bid getWinnerBid() { | ||
| return bidResponse != null ? bidResponse.getWinningBid() : null; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| final boolean isPrimaryAdServerRequestInProgress() { | ||
| return isPrimaryAdServerRequestInProgress; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,13 +17,24 @@ | |
| package org.prebid.mobile.rendering.bidding.listeners; | ||
|
|
||
| import android.view.View; | ||
| import androidx.annotation.Nullable; | ||
| import org.prebid.mobile.AdSize; | ||
| import org.prebid.mobile.api.exceptions.AdException; | ||
|
|
||
| public interface BannerEventListener { | ||
| void onPrebidSdkWin(); | ||
|
|
||
| void onAdServerWin(View view); | ||
|
|
||
| /** | ||
| * Ad server win that also reports the size of the creative the ad server served, in dp. | ||
| * Event handlers that know the served size should call this instead of | ||
| * {@link #onAdServerWin(View)}, so the banner can report it. Pass null when the size is unknown. | ||
| */ | ||
| default void onAdServerWin(View view, @Nullable AdSize adSize) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional, but worth considering while this API is new: the two overloads delegate to each other. This default forwards two-arg → one-arg, and BannerView's one-arg override forwards back to two-arg. That works today because BannerView overrides both. But BannerView is the only implementer and event handlers are the callers, so the delegation runs the wrong way. If someone later drops the two-arg override, or writes a wrapper that follows this Javadoc and forwards one-arg → two-arg, it recurses until StackOverflowError. A wrapper that only overrides the one-arg method silently drops the size. Flipping it keeps every existing caller compiling, and matches iOS, where /** @deprecated Use {@link #onAdServerWin(View, AdSize)}. */
@Deprecated
default void onAdServerWin(View view) {
onAdServerWin(view, null);
}
void onAdServerWin(View view, @Nullable AdSize adSize);BannerView then only implements the two-arg method. It would only break code that implements
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 835a955. The two-arg |
||
| onAdServerWin(view); | ||
| } | ||
|
|
||
| void onAdFailed(AdException exception); | ||
|
|
||
| void onAdClicked(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A detached Prebid creative can clear the size of the ad on screen.
displayViewListeneris shared by everyDisplayViewthis banner creates.displayAdServerView()callsremoveAllViews()without destroyingdisplayView, anddisplayPrebidView()only destroys it while it's still a child. So after a Prebid → GAM refresh, the old creative is still alive and wired to this listener. If it reports a failure later (a video error, for example), this line sets the size to 0x0 while GAM's creative is on screen.It's an edge case, but the fix belongs one level down: destroy and null
displayViewindisplayAdServerView(), the same waydisplayPrebidView()does. That also stops the detached creative from leaking and from sending callbacks tobannerViewListener.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 835a955.
displayAdServerView()now destroys and nullsdisplayViewbefore removing views, andonAdServerWin_DestroysPreviousPrebidCreativecovers it.