Add setter for companion ad slots

PiperOrigin-RevId: 322965540
This commit is contained in:
andrewlewis 2020-07-24 11:10:52 +01:00 committed by Oliver Woodman
parent 6fb28a8dc9
commit f24ba73b80
3 changed files with 30 additions and 2 deletions

View file

@ -252,6 +252,8 @@
* Migrate to new 'friendly obstruction' IMA SDK APIs, and allow apps to * Migrate to new 'friendly obstruction' IMA SDK APIs, and allow apps to
register a purpose and detail reason for overlay views via register a purpose and detail reason for overlay views via
`AdsLoader.AdViewProvider`. `AdsLoader.AdViewProvider`.
* Add `ImaAdsLoader.Builder.setCompanionAdSlots` so it's possible to set
companion ad slots without accessing the `AdDisplayContainer`.
* Demo app: * Demo app:
* Retain previous position in list of samples. * Retain previous position in list of samples.
* Replace the `extensions` variant with `decoderExtensions` and make the * Replace the `extensions` variant with `decoderExtensions` and make the

View file

@ -29,6 +29,7 @@ dependencies {
implementation project(modulePrefix + 'library-core') implementation project(modulePrefix + 'library-core')
implementation 'androidx.annotation:annotation:' + androidxAnnotationVersion implementation 'androidx.annotation:annotation:' + androidxAnnotationVersion
implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0' implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'
implementation 'com.google.guava:guava:' + guavaVersion
compileOnly 'org.checkerframework:checker-qual:' + checkerframeworkVersion compileOnly 'org.checkerframework:checker-qual:' + checkerframeworkVersion
compileOnly 'org.jetbrains.kotlin:kotlin-annotations-jvm:' + kotlinAnnotationsVersion compileOnly 'org.jetbrains.kotlin:kotlin-annotations-jvm:' + kotlinAnnotationsVersion
androidTestImplementation project(modulePrefix + 'testutils') androidTestImplementation project(modulePrefix + 'testutils')

View file

@ -46,6 +46,7 @@ import com.google.ads.interactivemedia.v3.api.AdsManager;
import com.google.ads.interactivemedia.v3.api.AdsManagerLoadedEvent; import com.google.ads.interactivemedia.v3.api.AdsManagerLoadedEvent;
import com.google.ads.interactivemedia.v3.api.AdsRenderingSettings; import com.google.ads.interactivemedia.v3.api.AdsRenderingSettings;
import com.google.ads.interactivemedia.v3.api.AdsRequest; import com.google.ads.interactivemedia.v3.api.AdsRequest;
import com.google.ads.interactivemedia.v3.api.CompanionAdSlot;
import com.google.ads.interactivemedia.v3.api.FriendlyObstruction; import com.google.ads.interactivemedia.v3.api.FriendlyObstruction;
import com.google.ads.interactivemedia.v3.api.FriendlyObstructionPurpose; import com.google.ads.interactivemedia.v3.api.FriendlyObstructionPurpose;
import com.google.ads.interactivemedia.v3.api.ImaSdkFactory; import com.google.ads.interactivemedia.v3.api.ImaSdkFactory;
@ -67,15 +68,17 @@ import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -122,6 +125,7 @@ public final class ImaAdsLoader
@Nullable private ImaSdkSettings imaSdkSettings; @Nullable private ImaSdkSettings imaSdkSettings;
@Nullable private AdEventListener adEventListener; @Nullable private AdEventListener adEventListener;
@Nullable private Set<UiElement> adUiElements; @Nullable private Set<UiElement> adUiElements;
@Nullable private Collection<CompanionAdSlot> companionAdSlots;
private long adPreloadTimeoutMs; private long adPreloadTimeoutMs;
private int vastLoadTimeoutMs; private int vastLoadTimeoutMs;
private int mediaLoadTimeoutMs; private int mediaLoadTimeoutMs;
@ -180,7 +184,19 @@ public final class ImaAdsLoader
* @see AdsRenderingSettings#setUiElements(Set) * @see AdsRenderingSettings#setUiElements(Set)
*/ */
public Builder setAdUiElements(Set<UiElement> adUiElements) { public Builder setAdUiElements(Set<UiElement> adUiElements) {
this.adUiElements = new HashSet<>(checkNotNull(adUiElements)); this.adUiElements = ImmutableSet.copyOf(checkNotNull(adUiElements));
return this;
}
/**
* Sets the slots to use for companion ads, if they are present in the loaded ad.
*
* @param companionAdSlots The slots to use for companion ads.
* @return This builder, for convenience.
* @see AdDisplayContainer#setCompanionSlots(Collection)
*/
public Builder setCompanionAdSlots(Collection<CompanionAdSlot> companionAdSlots) {
this.companionAdSlots = ImmutableList.copyOf(checkNotNull(companionAdSlots));
return this; return this;
} }
@ -298,6 +314,7 @@ public final class ImaAdsLoader
focusSkipButtonWhenAvailable, focusSkipButtonWhenAvailable,
playAdBeforeStartPosition, playAdBeforeStartPosition,
adUiElements, adUiElements,
companionAdSlots,
adEventListener, adEventListener,
imaFactory); imaFactory);
} }
@ -322,6 +339,7 @@ public final class ImaAdsLoader
focusSkipButtonWhenAvailable, focusSkipButtonWhenAvailable,
playAdBeforeStartPosition, playAdBeforeStartPosition,
adUiElements, adUiElements,
companionAdSlots,
adEventListener, adEventListener,
imaFactory); imaFactory);
} }
@ -388,6 +406,7 @@ public final class ImaAdsLoader
private final boolean playAdBeforeStartPosition; private final boolean playAdBeforeStartPosition;
private final int mediaBitrate; private final int mediaBitrate;
@Nullable private final Set<UiElement> adUiElements; @Nullable private final Set<UiElement> adUiElements;
@Nullable private final Collection<CompanionAdSlot> companionAdSlots;
@Nullable private final AdEventListener adEventListener; @Nullable private final AdEventListener adEventListener;
private final ImaFactory imaFactory; private final ImaFactory imaFactory;
private final ImaSdkSettings imaSdkSettings; private final ImaSdkSettings imaSdkSettings;
@ -495,6 +514,7 @@ public final class ImaAdsLoader
/* focusSkipButtonWhenAvailable= */ true, /* focusSkipButtonWhenAvailable= */ true,
/* playAdBeforeStartPosition= */ true, /* playAdBeforeStartPosition= */ true,
/* adUiElements= */ null, /* adUiElements= */ null,
/* companionAdSlots= */ null,
/* adEventListener= */ null, /* adEventListener= */ null,
/* imaFactory= */ new DefaultImaFactory()); /* imaFactory= */ new DefaultImaFactory());
} }
@ -512,6 +532,7 @@ public final class ImaAdsLoader
boolean focusSkipButtonWhenAvailable, boolean focusSkipButtonWhenAvailable,
boolean playAdBeforeStartPosition, boolean playAdBeforeStartPosition,
@Nullable Set<UiElement> adUiElements, @Nullable Set<UiElement> adUiElements,
@Nullable Collection<CompanionAdSlot> companionAdSlots,
@Nullable AdEventListener adEventListener, @Nullable AdEventListener adEventListener,
ImaFactory imaFactory) { ImaFactory imaFactory) {
checkArgument(adTagUri != null || adsResponse != null); checkArgument(adTagUri != null || adsResponse != null);
@ -525,6 +546,7 @@ public final class ImaAdsLoader
this.focusSkipButtonWhenAvailable = focusSkipButtonWhenAvailable; this.focusSkipButtonWhenAvailable = focusSkipButtonWhenAvailable;
this.playAdBeforeStartPosition = playAdBeforeStartPosition; this.playAdBeforeStartPosition = playAdBeforeStartPosition;
this.adUiElements = adUiElements; this.adUiElements = adUiElements;
this.companionAdSlots = companionAdSlots;
this.adEventListener = adEventListener; this.adEventListener = adEventListener;
this.imaFactory = imaFactory; this.imaFactory = imaFactory;
if (imaSdkSettings == null) { if (imaSdkSettings == null) {
@ -595,6 +617,9 @@ public final class ImaAdsLoader
} }
adDisplayContainer = adDisplayContainer =
imaFactory.createAdDisplayContainer(adViewGroup, /* player= */ componentListener); imaFactory.createAdDisplayContainer(adViewGroup, /* player= */ componentListener);
if (companionAdSlots != null) {
adDisplayContainer.setCompanionSlots(companionAdSlots);
}
adsLoader = imaFactory.createAdsLoader(context, imaSdkSettings, adDisplayContainer); adsLoader = imaFactory.createAdsLoader(context, imaSdkSettings, adDisplayContainer);
adsLoader.addAdErrorListener(componentListener); adsLoader.addAdErrorListener(componentListener);
adsLoader.addAdsLoadedListener(componentListener); adsLoader.addAdsLoadedListener(componentListener);