Skip to content

Commit 642e95b

Browse files
andrewlewisojw28
authored andcommitted
Workaround the skip ad button not being focused
Issue: google#3258 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=168669969
1 parent aacfb37 commit 642e95b

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎extensions/ima/src/main/java/com/google/android/exoplayer2/ext/ima/ImaAdsLoader.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.os.SystemClock;
2121
import android.util.Log;
2222
import android.view.ViewGroup;
23+
import android.webkit.WebView;
2324
import com.google.ads.interactivemedia.v3.api.Ad;
2425
import com.google.ads.interactivemedia.v3.api.AdDisplayContainer;
2526
import com.google.ads.interactivemedia.v3.api.AdErrorEvent;
@@ -112,6 +113,14 @@ public final class ImaAdsLoader implements Player.EventListener, VideoAdPlayer,
112113
*/
113114
private static final long END_OF_CONTENT_POSITION_THRESHOLD_MS = 5000;
114115

116+
/**
117+
* The "Skip ad" button rendered in the IMA WebView does not gain focus by default and cannot be
118+
* clicked via a keypress event. Workaround this issue by calling focus() on the HTML element in
119+
* the WebView directly when an ad starts. See [Internal: b/62371030].
120+
*/
121+
private static final String FOCUS_SKIP_BUTTON_WORKAROUND_JS = "javascript:"
122+
+ "try{ document.getElementsByClassName(\"videoAdUiSkipButton\")[0].focus(); } catch (e) {}";
123+
115124
private final Uri adTagUri;
116125
private final Timeline.Period period;
117126
private final List<VideoAdPlayerCallback> adCallbacks;
@@ -121,6 +130,7 @@ public final class ImaAdsLoader implements Player.EventListener, VideoAdPlayer,
121130

122131
private EventListener eventListener;
123132
private Player player;
133+
private ViewGroup adUiViewGroup;
124134
private VideoProgressUpdate lastContentProgress;
125135
private VideoProgressUpdate lastAdProgress;
126136

@@ -249,6 +259,7 @@ public ImaAdsLoader(Context context, Uri adTagUri, ImaSdkSettings imaSdkSettings
249259
ViewGroup adUiViewGroup) {
250260
this.player = player;
251261
this.eventListener = eventListener;
262+
this.adUiViewGroup = adUiViewGroup;
252263
lastAdProgress = null;
253264
lastContentProgress = null;
254265
adDisplayContainer.setAdContainer(adUiViewGroup);
@@ -278,6 +289,7 @@ public ImaAdsLoader(Context context, Uri adTagUri, ImaSdkSettings imaSdkSettings
278289
player.removeListener(this);
279290
player = null;
280291
eventListener = null;
292+
adUiViewGroup = null;
281293
}
282294

283295
/**
@@ -363,6 +375,11 @@ public void onAdEvent(AdEvent adEvent) {
363375
imaPausedContent = true;
364376
pauseContentInternal();
365377
break;
378+
case STARTED:
379+
if (ad.isSkippable()) {
380+
focusSkipButton();
381+
}
382+
break;
366383
case TAPPED:
367384
if (eventListener != null) {
368385
eventListener.onAdTapped();
@@ -727,4 +744,13 @@ private static long[] getAdGroupTimesUs(List<Float> cuePoints) {
727744
return adGroupTimesUs;
728745
}
729746

747+
private void focusSkipButton() {
748+
if (playingAd && adUiViewGroup != null && adUiViewGroup.getChildCount() > 0
749+
&& adUiViewGroup.getChildAt(0) instanceof WebView) {
750+
WebView webView = (WebView) (adUiViewGroup.getChildAt(0));
751+
webView.requestFocus();
752+
webView.loadUrl(FOCUS_SKIP_BUTTON_WORKAROUND_JS);
753+
}
754+
}
755+
730756
}

‎library/ui/src/main/java/com/google/android/exoplayer2/ui/SimpleExoPlayerView.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,13 @@ public void setUseController(boolean useController) {
515515

516516
@Override
517517
public boolean dispatchKeyEvent(KeyEvent event) {
518+
if (player != null && player.isPlayingAd()) {
519+
// Focus any overlay UI now, in case it's provided by a WebView whose contents may update
520+
// dynamically. This is needed to make the "Skip ad" button focused on Android TV when using
521+
// IMA [Internal: b/62371030].
522+
overlayFrameLayout.requestFocus();
523+
return super.dispatchKeyEvent(event);
524+
}
518525
maybeShowController(true);
519526
return dispatchMediaKeyEvent(event) || super.dispatchKeyEvent(event);
520527
}

0 commit comments

Comments
 (0)