Fix handling of delayed AdsLoader.start

AdsMediaSource posts AdsLoader.start to the main thread during preparation, but
the app may call AdsLoader.setPlayer(null) before it actually runs (e.g., if
initializing then quickly backgrounding the player).

This is valid usage of the API so handle this case instead of asserting. Because
not calling setPlayer at all is a pitfall of the API, track whether setPlayer
has been called and still assert that in AdsLoader.start.

PiperOrigin-RevId: 264329632
This commit is contained in:
andrewlewis 2019-08-20 08:40:44 +01:00 committed by Oliver Woodman
parent c60b355f9c
commit 5b2b21da86

View File

@ -327,6 +327,7 @@ public final class ImaAdsLoader
private final AdDisplayContainer adDisplayContainer;
private final com.google.ads.interactivemedia.v3.api.AdsLoader adsLoader;
private boolean wasSetPlayerCalled;
@Nullable private Player nextPlayer;
private Object pendingAdRequestContext;
private List<String> supportedMimeTypes;
@ -558,6 +559,7 @@ public final class ImaAdsLoader
Assertions.checkState(
player == null || player.getApplicationLooper() == Looper.getMainLooper());
nextPlayer = player;
wasSetPlayerCalled = true;
}
@Override
@ -585,9 +587,12 @@ public final class ImaAdsLoader
@Override
public void start(EventListener eventListener, AdViewProvider adViewProvider) {
Assertions.checkNotNull(
nextPlayer, "Set player using adsLoader.setPlayer before preparing the player.");
Assertions.checkState(
wasSetPlayerCalled, "Set player using adsLoader.setPlayer before preparing the player.");
player = nextPlayer;
if (player == null) {
return;
}
this.eventListener = eventListener;
lastVolumePercentage = 0;
lastAdProgress = null;
@ -617,6 +622,9 @@ public final class ImaAdsLoader
@Override
public void stop() {
if (player == null) {
return;
}
if (adsManager != null && imaPausedContent) {
adPlaybackState =
adPlaybackState.withAdResumePositionUs(