2020import android .os .SystemClock ;
2121import android .util .Log ;
2222import android .view .ViewGroup ;
23+ import android .webkit .WebView ;
2324import com .google .ads .interactivemedia .v3 .api .Ad ;
2425import com .google .ads .interactivemedia .v3 .api .AdDisplayContainer ;
2526import 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}
0 commit comments