From ad5c8af01957975bb8eb61dc7f3edf60e2e4aa39 Mon Sep 17 00:00:00 2001 From: andrewlewis Date: Fri, 28 Jul 2017 04:45:52 -0700 Subject: [PATCH] Allow apps to handle ad clicked/tapped events Issue: #3106 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=163455563 --- .../exoplayer2/ext/ima/ImaAdsLoader.java | 22 ++++++++++- .../exoplayer2/ext/ima/ImaAdsMediaSource.java | 38 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoader.java b/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoader.java index 6e2206d6ae..fc8000b397 100644 --- a/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoader.java +++ b/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoader.java @@ -61,7 +61,7 @@ public final class ImaAdsLoader implements Player.EventListener, VideoAdPlayer, /** * Listener for ad loader events. All methods are called on the main thread. */ - public interface EventListener { + /* package */ interface EventListener { /** * Called when the ad playback state has been updated. @@ -77,6 +77,16 @@ public final class ImaAdsLoader implements Player.EventListener, VideoAdPlayer, */ void onLoadError(IOException error); + /** + * Called when the user clicks through an ad (for example, following a 'learn more' link). + */ + void onAdClicked(); + + /** + * Called when the user taps a non-clickthrough part of an ad. + */ + void onAdTapped(); + } static { @@ -337,6 +347,16 @@ public final class ImaAdsLoader implements Player.EventListener, VideoAdPlayer, imaPausedContent = true; pauseContentInternal(); break; + case TAPPED: + if (eventListener != null) { + eventListener.onAdTapped(); + } + break; + case CLICKED: + if (eventListener != null) { + eventListener.onAdClicked(); + } + break; case CONTENT_RESUME_REQUESTED: imaPausedContent = false; resumeContentInternal(); diff --git a/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsMediaSource.java b/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsMediaSource.java index 9c2eb4b404..0bf5773d2c 100644 --- a/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsMediaSource.java +++ b/extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsMediaSource.java @@ -53,6 +53,16 @@ public final class ImaAdsMediaSource implements MediaSource { */ void onAdLoadError(IOException error); + /** + * Called when the user clicks through an ad (for example, following a 'learn more' link). + */ + void onAdClicked(); + + /** + * Called when the user taps a non-clickthrough part of an ad. + */ + void onAdTapped(); + } private static final String TAG = "ImaAdsMediaSource"; @@ -305,6 +315,34 @@ public final class ImaAdsMediaSource implements MediaSource { }); } + @Override + public void onAdClicked() { + if (eventHandler != null && eventListener != null) { + eventHandler.post(new Runnable() { + @Override + public void run() { + if (!released) { + eventListener.onAdClicked(); + } + } + }); + } + } + + @Override + public void onAdTapped() { + if (eventHandler != null && eventListener != null) { + eventHandler.post(new Runnable() { + @Override + public void run() { + if (!released) { + eventListener.onAdTapped(); + } + } + }); + } + } + } }