Skip to content

Commit

Permalink
Fem 1332- fix if app goes to bg/fg during ad loading -> content not p…
Browse files Browse the repository at this point in the history
…repared (kaltura#210)

* if ad loading during bg init ads again in fg
* do not prepare if mediaConfig is null

* fix - preroll plays again if all ads completed and going to bg and back

* move log after null check
add private

* ad log
  • Loading branch information
giladna authored Apr 9, 2017
1 parent df462a3 commit 75606fd
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public void prepare(@NonNull final PKMediaConfig mediaConfig) {

if (adsProvider != null) {
if (adsProvider.isAdRequested()) {
log.d("IMA calling super.prepare");
super.prepare(mediaConfig);
} else {
log.d("IMA setAdProviderListener");
adsProvider.setAdProviderListener(this);
}
}
Expand Down Expand Up @@ -81,27 +83,32 @@ public void seekTo(long position) {

@Override
public void play() {
log.d("PLAY isAdDisplayed = " + adsProvider.isAdDisplayed() + " isAdPaused = " + adsProvider.isAdPaused());
log.d("PLAY IMA decorator");
if (adsProvider != null) {
if (!adsProvider.isAdRequested()) {
adsProvider.start();
return;
} else if (adsProvider.isAdDisplayed()) {
adsProvider.resume();
return;
log.d("PLAY IMA decorator isAdDisplayed = " + adsProvider.isAdDisplayed() + " isAdPaused = " + adsProvider.isAdPaused() + " isAllAdsCompleted = " + adsProvider.isAllAdsCompleted());
if (!adsProvider.isAllAdsCompleted()) {
if (!adsProvider.isAdRequested()) {
adsProvider.start();
return;
} else if (adsProvider.isAdDisplayed()) {
adsProvider.resume();
return;
}
}
}
log.d("IMA decorator Calling player play");
getView().showVideoSurface();
super.play();

}

@Override
public void pause() {
log.d("PAUSE isAdDisplayed = " + adsProvider.isAdDisplayed() + " isAdPaused = " + adsProvider.isAdPaused());
log.d("PAUSE IMA decorator isAdDisplayed = " + adsProvider.isAdDisplayed() + " isAdPaused = " + adsProvider.isAdPaused() + " isAllAdsCompleted " + adsProvider.isAllAdsCompleted());
if (adsProvider.isAdDisplayed()) {
adsProvider.pause();
} else {
log.d("IMA decorator Calling player pause");
super.pause();
}
}
Expand All @@ -127,6 +134,10 @@ public AdController getAdController() {
@Override
public void onAdLoadingFinished() {
log.d("onAdLoadingFinished pkPrepareReason");
if (mediaConfig == null) {
log.e("IMA onAdLoadingFinished mediaConfig == null");
return;
}
prepare(mediaConfig);
if (adsProvider != null) {
adsProvider.removeAdProviderListener();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface AdsProvider {
boolean isAdDisplayed();
boolean isAdPaused();
boolean isAdRequested();
boolean isAllAdsCompleted();
long getDuration();
long getCurrentPosition();
void setAdProviderListener(AdEnabledPlayerController adEnabledPlayerController);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class IMAPlugin extends PKPlugin implements AdsProvider, com.google.ads.i
private boolean isInitWaiting = false;
private boolean appIsInBackground;
private boolean adManagerInitDuringBackground;
private boolean applicationInBackgroundDuringLoaded;
private boolean appInBackgroundDuringAdLoad;

////////////////////
private MessageBus messageBus;
Expand All @@ -109,6 +109,8 @@ public class IMAPlugin extends PKPlugin implements AdsProvider, com.google.ads.i
private boolean adPlaybackCancelled;
private Timer adDisplayedCheckTimer;
private boolean isContentPrepared;
private boolean isAllAdsCompleted;
private com.google.ads.interactivemedia.v3.api.AdEvent.AdEventType lastEventReceived;

public static final Factory factory = new Factory() {
@Override
Expand Down Expand Up @@ -148,12 +150,14 @@ protected void onLoad(Player player, Object config, final MessageBus messageBus,
log.e("Error, player instance is null.");
return;
}

this.isAllAdsCompleted = false;
this.context = context;
this.messageBus = messageBus;
this.messageBus.listen(new PKEvent.Listener() {
@Override
public void onEvent(PKEvent event) {
log.d("onLoad:PlayerEvent:" + event.eventType().name());
log.d("Received:PlayerEvent:" + event.eventType().name());
if (event.eventType() == PlayerEvent.Type.ENDED) {
contentCompleted();
}
Expand Down Expand Up @@ -203,8 +207,7 @@ public void onFinish() {
log.d("adManagerTimer.onFinish, adsManager=" + adsManager);
if (adsManager == null) {
log.d("adsManager is null, will play content");
preparePlayerOnAdLoadingFinished();

preparePlayer();
messageBus.post(new AdEvent(AdEvent.Type.AD_BREAK_IGNORED));
if (isAdRequested) {
adPlaybackCancelled = true;
Expand Down Expand Up @@ -289,6 +292,9 @@ protected void onApplicationPaused() {
appIsInBackground = true;
pause();
cancelAdDisplayedCheckTimer();
if (!isContentPrepared) {
resetIMA();
}
}

@Override
Expand All @@ -304,9 +310,9 @@ protected void onApplicationResumed() {
return;
}
if (adsManager != null) {
if (applicationInBackgroundDuringLoaded) {
if (appInBackgroundDuringAdLoad) {
player.getView().hideVideoSurface();
applicationInBackgroundDuringLoaded = false;
appInBackgroundDuringAdLoad = false;
adsManager.start();
} else if (isAdDisplayed) {
if (adsManager.getAdProgress().getDuration() - adsManager.getAdProgress().getCurrentTime() < 1) {
Expand All @@ -320,6 +326,24 @@ protected void onApplicationResumed() {
}
}
initAdDisplayedCheckTimer();

if (adsManager == null && !isContentPrepared && mediaConfig != null) {
if (adConfig != null) {
log.d("IMA on resume restart");
adManagerInitDuringBackground = false;
appInBackgroundDuringAdLoad = false;
onUpdateMedia(mediaConfig);
start();
return;
}
} else if (!isContentPrepared){
log.d("IMA onResume prepare Player");
preparePlayer();
}

if (lastEventReceived == com.google.ads.interactivemedia.v3.api.AdEvent.AdEventType.CONTENT_RESUME_REQUESTED && player != null) {
player.play();
}
}

@Override
Expand All @@ -329,10 +353,18 @@ public void destroy() {

@Override
protected void onDestroy() {
log.d("Start onDestroy");
cancelAdDisplayedCheckTimer();
log.d("IMA Start onDestroy");
resetIMA();
isContentPrepared = false;
removeAdProviderListener();
}

protected void resetIMA() {
log.d("Start resetIMA");
isAdRequested = false;
isAdDisplayed = false;

cancelAdDisplayedCheckTimer();
cancelAdManagerTimer();

adTagCuePoints = null;
Expand All @@ -349,7 +381,6 @@ protected void onDestroy() {
}
sdkFactory = null;
imaSdkSettings = null;
removeAdProviderListener();
}

private void cancelAdManagerTimer() {
Expand All @@ -376,6 +407,7 @@ private AdsLoader.AdsLoadedListener getAdsLoadedListener() {
public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) {

log.d("AdsManager loaded");

cancelAdManagerTimer();

// Ads were successfully loaded, so get the AdsManager instance. AdsManager has
Expand All @@ -389,9 +421,11 @@ public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) {

if (isInitWaiting) {
if (appIsInBackground) {
log.d("IMA app in background return");
adManagerInitDuringBackground = true;
return;
}
log.d("IMA adsManager.init called");
adsManager.init(renderingSettings);
isInitWaiting = false;
}
Expand All @@ -403,10 +437,12 @@ public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) {

@Override
public void start() {
log.d("Start adsManager.init");
log.d("Start adsManager.init");
isAdRequested = true;
if (adsManager != null) {
log.d("IMA adsManager.init called");
if (appIsInBackground) {
log.d("Start: Ad Manager Init : " + adManagerInitDuringBackground);
adManagerInitDuringBackground = true;
} else {
adsManager.init(renderingSettings);
Expand Down Expand Up @@ -492,6 +528,11 @@ public boolean isAdRequested() {
return isAdRequested;
}

@Override
public boolean isAllAdsCompleted() {
return isAllAdsCompleted;
}

@Override
public long getDuration() {
if (adsManager != null) {
Expand Down Expand Up @@ -529,23 +570,25 @@ public void skipAd() {

@Override
public void onAdEvent(com.google.ads.interactivemedia.v3.api.AdEvent adEvent) {
log.i("Event: " + adEvent.getType());
lastEventReceived = adEvent.getType();
log.d("Event Name: " + lastEventReceived.name());
if (adEvent.getAdData() != null) {
log.i("Event: " + adEvent.getAdData().toString());
log.d("Event: " + adEvent.getAdData().toString());
}

if (adsManager == null) {
return;
}
switch (adEvent.getType()) {
switch (lastEventReceived) {

case LOADED:
// AdEventType.LOADED will be fired when ads are ready to be played.
// AdsManager.start() begins ad playback. This method is ignored for VMAP or
// ad rules playlists, as the SDK will automatically start executing the
// playlist.
if (appIsInBackground) {
applicationInBackgroundDuringLoaded = true;
appInBackgroundDuringAdLoad = true;
adsManager.pause();
} else {
messageBus.post(new AdEvent(AdEvent.Type.LOADED));
if (adPlaybackCancelled) {
Expand All @@ -563,6 +606,11 @@ public void onAdEvent(com.google.ads.interactivemedia.v3.api.AdEvent adEvent) {
// ad is played.
log.d("AD_CONTENT_PAUSE_REQUESTED");

if (appIsInBackground) {
appInBackgroundDuringAdLoad = true;
adsManager.pause();
}

if (!adPlaybackCancelled) {
messageBus.post(new AdEvent(AdEvent.Type.CONTENT_PAUSE_REQUESTED));

Expand All @@ -584,7 +632,7 @@ public void onAdEvent(com.google.ads.interactivemedia.v3.api.AdEvent adEvent) {

if (!isContentPrepared) {
log.d("Content not prepared.. Preparing and calling play.");
if (pkAdProviderListener != null) {
if (pkAdProviderListener != null && !appIsInBackground) {
pkAdProviderListener.onAdLoadingFinished();
isContentPrepared = true;
if (player != null){
Expand All @@ -605,11 +653,13 @@ public void onAdEvent(com.google.ads.interactivemedia.v3.api.AdEvent adEvent) {
break;
case ALL_ADS_COMPLETED:
log.d("AD_ALL_ADS_COMPLETED");
isAllAdsCompleted = true;
isAdDisplayed = false;
messageBus.post(new AdEvent(AdEvent.Type.ALL_ADS_COMPLETED));
player.getView().showVideoSurface();
if (adsManager != null) {
log.d("AD_ALL_ADS_COMPLETED onDestroy");
onDestroy();
log.d("AD_ALL_ADS_COMPLETED resetIMA");
resetIMA();
}

break;
Expand All @@ -626,17 +676,14 @@ public void onAdEvent(com.google.ads.interactivemedia.v3.api.AdEvent adEvent) {
adInfo = createAdInfo(adEvent.getAd());
messageBus.post(new AdEvent.AdStartedEvent(adInfo));

if (!appIsInBackground) {
preparePlayerOnAdLoadingFinished();
}
preparePlayer();

if (adTagCuePoints == null) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
log.d("AD CUEPOINTS CHANGED TRIGGERED WITH DELAY");

adTagCuePoints = new AdCuePoints(getAdCuePoints());
messageBus.post(new AdEvent.AdCuePointsUpdateEvent(adTagCuePoints));
}
Expand Down Expand Up @@ -707,8 +754,9 @@ public void run() {
sendCuePointsUpdate();
break;
case LOG:
isAdRequested = true;
//for this case no AD ERROR is fired need to show view {type=adLoadError, errorCode=1009, errorMessage=The response does not contain any valid ads.}
preparePlayerOnAdLoadingFinished();
preparePlayer();
if (!isAdDisplayed) {
cancelAdDisplayedCheckTimer();
}
Expand All @@ -718,6 +766,7 @@ public void run() {
AdPodInfo adPoidInfo = adInfo.getAdPodInfo();
log.d("adPoidInfo.getAdPosition() = " + adPoidInfo.getAdPosition() + " adPoidInfo.getTotalAds() = " + adPoidInfo.getTotalAds());
if (adPoidInfo != null && adPoidInfo.getTotalAds() > 1 && adPoidInfo.getAdPosition() < adPoidInfo.getTotalAds()) {
log.d("LOG Error but continue to next ad in pod");
return;
} else {
adsManager.discardAdBreak();
Expand Down Expand Up @@ -748,6 +797,15 @@ public void run() {
}
}

private void preparePlayer() {
log.d("IMA prepare");
if (pkAdProviderListener != null && !appIsInBackground) {
log.d("IMA prepare player");
isContentPrepared = true;
pkAdProviderListener.onAdLoadingFinished();
}
}

private void initAdDisplayedCheckTimer() {
adDisplayedCheckTimer = new Timer();
TimerTask timerTask = new TimerTask() {
Expand Down Expand Up @@ -904,18 +962,10 @@ public void onAdError(AdErrorEvent adErrorEvent) {
messageBus.post(new AdError(AdError.Type.UNKNOWN_ERROR, errorMessage));
}

preparePlayerOnAdLoadingFinished();

preparePlayer();
if (player != null && player.getView() != null) {
player.getView().showVideoSurface();
player.play();
}
}

private void preparePlayerOnAdLoadingFinished() {
if (pkAdProviderListener != null) {
isContentPrepared = true;
pkAdProviderListener.onAdLoadingFinished();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ public void onEvent(PKEvent event) {
public void onEvent(PKEvent event) {
log.d("Ad Event AD_ALL_ADS_COMPLETED");
appProgressBar.setVisibility(View.INVISIBLE);
player.play();
}
}, AdEvent.Type.ALL_ADS_COMPLETED);
player.addEventListener(new PKEvent.Listener() {
Expand All @@ -335,7 +334,6 @@ public void onEvent(PKEvent event) {
@Override
public void onEvent(PKEvent event) {
nowPlaying = true;
player.play();
}
}, AdEvent.Type.SKIPPED);

Expand Down

0 comments on commit 75606fd

Please sign in to comment.